Skip to content

Commit ee9b04f

Browse files
committed
Allow using URL Params to jumpstart NewChat with a given message.
For example, ``` https://localhost:5173/#/chat/new?message=hello ``` This will start a new chat and then send the message out to kickoff the session. Issue: #418
1 parent b3595d2 commit ee9b04f

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/lib/NewChat.svelte

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
<script lang="ts">
2-
import { querystring } from 'svelte-spa-router'
3-
import { addChat, setChatSettingValueByKey } from './Storage.svelte'
4-
import { replace } from 'svelte-spa-router'
5-
import { getProfile, restartProfile } from './Profiles.svelte'
6-
import { getChatDefaults, hasChatSetting } from './Settings.svelte'
2+
import { querystring } from "svelte-spa-router";
3+
import {
4+
addChat,
5+
addMessage,
6+
setChatSettingValueByKey,
7+
submitExitingPromptsNow,
8+
} from "./Storage.svelte";
9+
import { replace } from "svelte-spa-router";
10+
import { getProfile, restartProfile } from "./Profiles.svelte";
11+
import { getChatDefaults, hasChatSetting } from "./Settings.svelte";
12+
import { v4 as uuidv4 } from "uuid";
13+
import { type Message } from "./Types.svelte";
714
815
// Create the new chat instance then redirect to it
916
10-
const urlParams: URLSearchParams = new URLSearchParams($querystring)
11-
const chatId = urlParams.has('p') ? addChat(getProfile(urlParams.get('p') || '')) : addChat()
12-
Object.keys(getChatDefaults()).forEach(k => {
17+
const urlParams: URLSearchParams = new URLSearchParams($querystring);
18+
const chatId = urlParams.has("p")
19+
? addChat(getProfile(urlParams.get("p") || ""))
20+
: addChat();
21+
Object.keys(getChatDefaults()).forEach((k) => {
1322
if (urlParams.has(k) && hasChatSetting(k as any)) {
14-
setChatSettingValueByKey(chatId, k as any, urlParams.get(k))
23+
setChatSettingValueByKey(chatId, k as any, urlParams.get(k));
1524
}
16-
})
17-
18-
restartProfile(chatId)
19-
replace(`/chat/${chatId}`)
25+
});
26+
27+
restartProfile(chatId);
28+
const messageContent = urlParams.get("message");
29+
if (messageContent !== null) {
30+
const uuid = uuidv4();
31+
const inputMessage: Message = {
32+
role: "user",
33+
content: messageContent,
34+
uuid,
35+
};
36+
addMessage(chatId, inputMessage);
37+
$submitExitingPromptsNow = true;
38+
}
39+
40+
replace(`/chat/${chatId}`);
2041
</script>

0 commit comments

Comments
 (0)