Skip to content

Commit

Permalink
auto-complete code ticks in live answers
Browse files Browse the repository at this point in the history
  • Loading branch information
Shackless committed Apr 17, 2023
1 parent 61d12ff commit c8901a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/lib/Chat.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import type { Chat } from '$misc/shared';
import { afterUpdate, beforeUpdate, onMount } from 'svelte';
import { ProgressRadial } from '@skeletonlabs/skeleton';
import snarkdown from 'snarkdown';
import { afterNavigate } from '$app/navigation';
import { chatStore, isLoadingAnswerStore, liveAnswerStore } from '$misc/stores';
import type { Chat } from '$misc/shared';
import { chatStore, enhancedLiveAnswerStore, isLoadingAnswerStore } from '$misc/stores';
import ChatMessages from './ChatMessages.svelte';
export let slug: string;
Expand Down Expand Up @@ -52,7 +52,7 @@
{#if $isLoadingAnswerStore}
<div class="place-self-start">
<div class="p-5 rounded-2xl variant-ghost-tertiary rounded-tl-none">
{@html snarkdown($liveAnswerStore.content)}
{@html snarkdown($enhancedLiveAnswerStore.content)}
</div>
</div>
{/if}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ChatInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
eventSourceStore,
isLoadingAnswerStore,
liveAnswerStore,
enhancedLiveAnswerStore,
settingsStore
} from '$misc/stores';
import { countTokens } from '$misc/openai';
Expand Down Expand Up @@ -149,7 +150,7 @@
function addCompletionToChat(isAborted = false) {
const messageToAdd: ChatMessage = !isAborted
? { ...$liveAnswerStore }
: { ...$liveAnswerStore, isAborted: true };
: { ...$enhancedLiveAnswerStore, isAborted: true };
chatStore.addMessageToChat(slug, messageToAdd, lastUserMessage || undefined);
$isLoadingAnswerStore = false;
Expand Down
21 changes: 21 additions & 0 deletions src/misc/markdownHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function containsMarkdownCodeBlock(input: string): boolean {
const oneTickCodeBlockRegex = /`[^`]*`/;
const threeTicksCodeBlockRegex = /```[\s\S]*?```/;

return oneTickCodeBlockRegex.test(input) || threeTicksCodeBlockRegex.test(input);
}

export function closeOpenedCodeTicks(input: string) {
const oneTickMatches = input.match(/(?<!`)`(?!`)/g) || [];
const threeTickMatches = input.match(/(?<!``)```(?!``)/g) || [];

if (oneTickMatches.length % 2 !== 0) {
input += '`';
}

if (threeTickMatches.length % 2 !== 0) {
input += '\n```';
}

return input;
}
17 changes: 16 additions & 1 deletion src/misc/stores.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { ChatCompletionRequestMessage } from 'openai';
import { writable, type Readable, type Writable, readable, get } from 'svelte/store';
import { writable, type Readable, type Writable, readable, get, derived } from 'svelte/store';
import { localStorageStore } from '@skeletonlabs/skeleton';
import { v4 as uuidv4 } from 'uuid';
import { EventSource } from './eventSource';
import { ChatStorekeeper } from './chatStorekeeper';
import type { Chat, ChatMessage, ClientSettings } from './shared';
import { closeOpenedCodeTicks } from './markdownHelper';

export const settingsStore: Writable<ClientSettings> = localStorageStore('settingsStore', {});

Expand All @@ -13,6 +14,20 @@ export const liveAnswerStore: Writable<ChatCompletionRequestMessage> = writable(
content: ''
});

export const enhancedLiveAnswerStore = derived(liveAnswerStore, ($liveAnswer) => {
if (!$liveAnswer?.content) {
return $liveAnswer;
}

let { content } = $liveAnswer;
content = closeOpenedCodeTicks(content);

return {
...$liveAnswer,
content
} as ChatCompletionRequestMessage;
});

export const isLoadingAnswerStore: Writable<boolean> = writable(false);

export const isTimeagoInitializedStore: Writable<boolean> = writable(false);
Expand Down

0 comments on commit c8901a0

Please sign in to comment.