Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "New Chat" button to context message #27

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/misc/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,27 @@ export interface ChatCost {
maxTokensForModel: number;
}

export function createNewChat() {
export function createNewChat(template?: {
context?: string;
title?: string;
settings?: OpenAiSettings;
messages?: ChatCompletionRequestMessage[];
}) {
const slug = generateSlug();
const chat: Chat = {
title: slug,
settings: { ...defaultOpenAiSettings },
title: template?.title || slug,
settings: { ...(template?.settings || defaultOpenAiSettings) },
contextMessage: {
role: 'system',
content: ''
content: template?.context || ''
},
messages: [],
messages: template?.messages || [],
created: new Date()
};

chatStore.updateChat(slug, chat);

goto(`/${slug}`);
goto(`/${slug}`, { invalidateAll: true });
}

export function showModalComponent(
Expand Down
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
// update the local store
$chatStore = {};

goto('/', { replaceState: true });
goto('/');
}

$: sortedChats = Object.entries($chatStore).sort((a, b) => {
Expand All @@ -83,7 +83,7 @@
class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 2xl:grid-cols-5 gap-6 px-4 md:px-8"
>
<!-- Add button -->
<button class="card p-4 grid variant-ghost-primary" on:click={createNewChat}>
<button class="card p-4 grid variant-ghost-primary" on:click={() => createNewChat()}>
<div class="flex space-x-2 md:space-x-4 items-center self-center justify-self-center">
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
38 changes: 18 additions & 20 deletions src/routes/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import HintMessage from '$lib/HintMessage.svelte';
import TokenCost from '$lib/TokenCost.svelte';
import { countTokens, estimateChatCost } from '$misc/openai';
import { showModalComponent, track } from '$misc/shared';
import { createNewChat, showModalComponent, track } from '$misc/shared';
import snarkdown from 'snarkdown';

export let data: PageData;
const { slug } = data;
$: ({ slug } = data);

$: chat = $chatStore[slug];
$: cost = chat ? estimateChatCost(chat) : null;
Expand Down Expand Up @@ -234,25 +234,23 @@
<TokenCost tokens={countTokens(chat.contextMessage)} />
</div>
{/if}
<button
class="btn btn-lg self-center variant-filled-secondary"
on:click={() => showModalComponent('ContextModal', { slug })}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke-width="1.5"
stroke="currentColor"
class="w-6 h-6"
<div class="flex flex-row md:flex-col space-x-2 space-y-2">
<button
class="btn self-center variant-filled-primary"
on:click={() => showModalComponent('ContextModal', { slug })}
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10"
/>
</svg>
</button>
Edit
</button>
{#if hasContext}
<button
class="btn self-center variant-filled-tertiary"
on:click={() =>
createNewChat({ context: chat.contextMessage.content, settings: chat.settings })}
>
New Chat
</button>
{/if}
</div>
</svelte:fragment>
</HintMessage>
</svelte:fragment>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/shared/[slug]/+error.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</p>

<!-- Add button -->
<button class="card p-4 grid variant-ghost-primary" on:click={createNewChat}>
<button class="card p-4 grid variant-ghost-primary" on:click={() => createNewChat()}>
<div class="flex space-x-2 md:space-x-4 items-center self-center justify-self-center">
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down