Skip to content

Commit

Permalink
Allow using URL Params to jumpstart NewChat with a given message.
Browse files Browse the repository at this point in the history
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: Niek#418
  • Loading branch information
lazywei committed Feb 10, 2024
1 parent b3595d2 commit 7ec7904
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/lib/NewChat.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
<script lang="ts">
import { querystring } from 'svelte-spa-router'
import { addChat, setChatSettingValueByKey } from './Storage.svelte'
import {
addChat,
addMessage,
setChatSettingValueByKey,
submitExitingPromptsNow,
} from './Storage.svelte'
import { replace } from 'svelte-spa-router'
import { getProfile, restartProfile } from './Profiles.svelte'
import { getChatDefaults, hasChatSetting } from './Settings.svelte'
import { v4 as uuidv4 } from 'uuid'
import { type Message } from './Types.svelte'
// Create the new chat instance then redirect to it
const urlParams: URLSearchParams = new URLSearchParams($querystring)
const chatId = urlParams.has('p') ? addChat(getProfile(urlParams.get('p') || '')) : addChat()
Object.keys(getChatDefaults()).forEach(k => {
const chatId = urlParams.has("p")
? addChat(getProfile(urlParams.get("p") || ""))
: addChat()
Object.keys(getChatDefaults()).forEach((k) => {
if (urlParams.has(k) && hasChatSetting(k as any)) {
setChatSettingValueByKey(chatId, k as any, urlParams.get(k))
}
})
restartProfile(chatId)
const messageContent = urlParams.get("message")
if (messageContent !== null) {
const uuid = uuidv4()
const inputMessage: Message = {
role: "user",
content: messageContent,
uuid,
}
addMessage(chatId, inputMessage)
$submitExitingPromptsNow = true
}
replace(`/chat/${chatId}`)
</script>

0 comments on commit 7ec7904

Please sign in to comment.