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: #418
  • Loading branch information
lazywei committed Feb 10, 2024
1 parent b3595d2 commit ee9b04f
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 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 { replace } from 'svelte-spa-router'
import { getProfile, restartProfile } from './Profiles.svelte'
import { getChatDefaults, hasChatSetting } from './Settings.svelte'
import { querystring } from "svelte-spa-router";

Check failure on line 2 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 2 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
import {
addChat,
addMessage,
setChatSettingValueByKey,
submitExitingPromptsNow,

Check failure on line 7 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Unexpected trailing comma
} from "./Storage.svelte";

Check failure on line 8 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 8 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
import { replace } from "svelte-spa-router";

Check failure on line 9 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 9 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
import { getProfile, restartProfile } from "./Profiles.svelte";

Check failure on line 10 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote

Check failure on line 10 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Extra semicolon
import { getChatDefaults, hasChatSetting } from "./Settings.svelte";

Check failure on line 11 in src/lib/NewChat.svelte

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
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 urlParams: URLSearchParams = new URLSearchParams($querystring);
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))
setChatSettingValueByKey(chatId, k as any, urlParams.get(k));
}
})
restartProfile(chatId)
replace(`/chat/${chatId}`)
});
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 ee9b04f

Please sign in to comment.