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

Fix: markdown and styles in ai assistant #1561

Merged
merged 1 commit into from
Dec 16, 2024
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"plausible-tracker": "^0.3.9",
"pretty-bytes": "^6.1.1",
"prismjs": "^1.29.0",
"remarkable": "^2.0.1",
"svelte-confetti": "^1.4.0",
"tippy.js": "^6.3.7"
},
Expand All @@ -52,6 +53,7 @@
"@testing-library/user-event": "^14.5.2",
"@types/deep-equal": "^1.0.4",
"@types/prismjs": "^1.26.5",
"@types/remarkable": "^2.0.8",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@vitest/ui": "^1.6.0",
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

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

59 changes: 53 additions & 6 deletions src/lib/commandCenter/panels/ai.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<script lang="ts">
import { Remarkable } from 'remarkable';
import Template from './template.svelte';

const markdownInstance = new Remarkable();

import { Alert, AvatarInitials, Code, LoadingDots, SvgIcon } from '$lib/components';
import { user } from '$lib/stores/user';
import { useCompletion } from 'ai/svelte';
Expand All @@ -19,8 +22,6 @@
credentials: 'include'
});

let question = $input;

const examples = [
'How to add platform in the console?',
'How can I manage users, permissions, and access control in Appwrite?',
Expand Down Expand Up @@ -87,10 +88,47 @@

$: answer = parseCompletion($completion);

function renderMarkdown(answer: string): string {
const trimmedAnswer = answer
.trim()
.replace(/[ \t]+/g, ' ')
.replace(/\n[ \t]+/g, '\n')
.replace(/\n+/g, '\n');

// targeting links in plain text.
const processedAnswer = trimmedAnswer
.replace(/(\[(.*?)]\((.*?)\))|https?:\/\/\S+/g, (match, fullMarkdownLink, _, __) =>
fullMarkdownLink ? match : `[${match}](${match})`
)
.replace(/https?:\/\/\S+##/g, (url) => url.replace(/##/, '#'));

const formattedAnswer = processedAnswer.replace(
/(^|\n)Sources:/g,
(_, prefix) => `${prefix}\nSources:`
);

let renderedHTML = markdownInstance.render(formattedAnswer);

// add target blank to open links in a new tab.
renderedHTML = renderedHTML.replace(/<a\s+href="([^"]+)"/g, '<a href="$1" target="_blank"');

return renderedHTML;
}

function getInitials(name: string) {
const [first, last] = name.split(' ');
return `${first?.[0] ?? ''}${last?.[0] ?? ''}`;
}

let previousQuestion = '';
$: if ($input) {
previousQuestion = $input;
}

$: if (!$isLoading && answer) {
// reset input if answer received.
$input = '';
}
</script>

<Template
Expand Down Expand Up @@ -142,7 +180,7 @@
<div class="content">
<div class="u-flex u-gap-8 u-cross-center">
<div class="avatar is-size-x-small">{getInitials($user.name)}</div>
<p class="u-opacity-75">{question}</p>
<p class="u-opacity-75">{previousQuestion}</p>
</div>
<div class="u-flex u-gap-8 u-margin-block-start-24">
<div class="logo">
Expand All @@ -154,7 +192,7 @@
{:else}
{#each answer as part}
{#if part.type === 'text'}
<p>{part.value.trimStart()}</p>
<p>{@html renderMarkdown(part.value.trim())}</p>
{:else if part.type === 'code'}
{#key part.value}
<div
Expand Down Expand Up @@ -196,7 +234,6 @@
class="input-text-wrapper u-width-full-line"
style="--amount-of-buttons: 1;"
on:submit|preventDefault={(e) => {
question = $input;
handleSubmit(e);
}}>
<!-- svelte-ignore a11y-autofocus -->
Expand Down Expand Up @@ -269,10 +306,20 @@
.answer {
overflow: hidden;

p {
p:first-of-type {
white-space: pre-wrap;
}
}

:global(.answer ul),
:global(.answer ol) {
gap: 1rem;
display: grid;
}

:global(.answer a) {
text-decoration: underline;
}
}

.footer {
Expand Down
Loading