Skip to content

Commit

Permalink
556 code blocks (#745)
Browse files Browse the repository at this point in the history
* add formatting for code blocks.
  • Loading branch information
andrewrisse authored Jul 11, 2024
1 parent 74889d3 commit 13cd46f
Show file tree
Hide file tree
Showing 14 changed files with 555 additions and 94 deletions.
118 changes: 116 additions & 2 deletions src/leapfrogai_ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/leapfrogai_ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@sveltejs/kit": "^2.5.7",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/svelte": "^5.1.0",
"@types/dompurify": "^3.0.5",
"@types/eslint": "^8.56.0",
"@types/lodash": "^4.17.4",
"@types/uuid": "^9.0.8",
Expand Down Expand Up @@ -77,7 +78,11 @@
"ai": "^3.1.11",
"carbon-pictograms-svelte": "^12.10.0",
"concurrently": "^8.2.2",
"dompurify": "^3.1.6",
"fuse.js": "^7.0.0",
"highlight.js": "^11.10.0",
"lit": "^3.1.4",
"markdown-it": "^14.1.0",
"msw": "^2.2.14",
"openai": "^4.47.1",
"playwright": "^1.42.1",
Expand Down
26 changes: 26 additions & 0 deletions src/leapfrogai_ui/src/lib/components/CopyToClipboardBtn.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script lang="ts">
import { Copy } from 'carbon-icons-svelte';
import { Button } from 'carbon-components-svelte';
import { toastStore } from '$stores';
export let value: string;
export let toastTitle = 'Copied';
export let btnText = 'Copy';
export let size: 'small' | 'default' | 'field' | 'lg' | 'xl' | undefined = 'small';
export let testId: string;
const handleClick = async () => {
if (value) {
await navigator.clipboard.writeText(value);
toastStore.addToast({
kind: 'info',
title: toastTitle,
subtitle: ''
});
}
};
</script>

<Button data-testid={testId} kind="tertiary" icon={Copy} {size} on:click={handleClick}
>{btnText}</Button
>
26 changes: 24 additions & 2 deletions src/leapfrogai_ui/src/lib/components/Message.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { Button, Tile } from 'carbon-components-svelte';
import { Copy, Edit, Reset, UserAvatar } from 'carbon-icons-svelte';
import { type Message as VercelAIMessage } from 'ai/svelte';
import markdownit from 'markdown-it';
import hljs from 'highlight.js';
import { LFTextArea } from '$components';
import frog from '$assets/frog.png';
import { writable } from 'svelte/store';
Expand All @@ -17,6 +19,7 @@
} from '$helpers/chatHelpers';
import DynamicPictogram from '$components/DynamicPictogram.svelte';
import type { AppendFunction, ReloadFunction, VercelOrOpenAIMessage } from '$lib/types/messages';
import DOMPurify from 'dompurify';
export let allStreamedMessages: VercelOrOpenAIMessage[];
export let message: VercelOrOpenAIMessage;
Expand All @@ -26,6 +29,24 @@
export let append: AppendFunction;
export let reload: ReloadFunction;
// used for code formatting and handling
const md = markdownit({
highlight: function (str: string, language: string) {
let code: string;
if (language && hljs.getLanguage(language)) {
try {
code = md.utils.escapeHtml(hljs.highlight(str, { language }).value);
} catch (__) {
code = md.utils.escapeHtml(str);
}
} else {
code = md.utils.escapeHtml(str);
}
return `<pre><code><code-block code="${code}" language="${language}"></code></pre>`;
}
});
let assistantImage = isRunAssistantResponse(message)
? getAssistantImage(...[$page.data.assistants || []], message.assistant_id)
: null;
Expand Down Expand Up @@ -126,7 +147,8 @@
<div style="font-weight: bold">
{message.role === 'user' ? 'You' : getAssistantName(message.assistant_id)}
</div>
<div>{getMessageText(message)}</div>
<!--eslint-disable-next-line svelte/no-at-html-tags -- We use DomPurity to sanitize the code snippet-->
<div>{@html md.render(DOMPurify.sanitize(getMessageText(message)))}</div>
</div></Tile
>
{/if}
Expand Down Expand Up @@ -203,14 +225,14 @@
flex-direction: column;
width: 100%;
gap: layout.$spacing-02;
overflow: hidden;
}
.hide {
opacity: 0;
transition: opacity 0.2s;
}
.message {
display: flex;
white-space: pre-line;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<script lang="ts">
import { formatKeyLong } from '$helpers/apiKeyHelpers.js';
import { Copy } from 'carbon-icons-svelte';
import { calculateDays, formatDate } from '$helpers/dates.js';
import { Button, Modal, TextInput } from 'carbon-components-svelte';
import { Modal, TextInput } from 'carbon-components-svelte';
import type { APIKeyRow } from '$lib/types/apiKeys';
import CopyToClipboardBtn from '$components/CopyToClipboardBtn.svelte';
export let copyKeyModalOpen: boolean;
export let handleCloseCopyKeyModal: () => void;
export let handleCopyKey: () => void;
export let createdKey: APIKeyRow | null;
let saveKeyModalRef: HTMLDivElement;
Expand Down Expand Up @@ -35,7 +34,7 @@
labelText="Key"
value={formatKeyLong(createdKey.api_key, saveKeyModalRef?.offsetWidth || 200)}
/>
<Button kind="tertiary" icon={Copy} size="small" on:click={handleCopyKey}>Copy</Button>
<CopyToClipboardBtn value={createdKey.api_key} toastTitle="API Key Copied" />
</div>
<div style="width: 100%">
<label for="saved-expiration" class:bx--label={true}>Expiration</label>
Expand Down
1 change: 1 addition & 0 deletions src/leapfrogai_ui/src/lib/types/markdown-it.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'markdown-it';
Loading

0 comments on commit 13cd46f

Please sign in to comment.