Skip to content

Commit 9abdf73

Browse files
author
Jicheng Lu
committed
add js interpreter
1 parent c05ed7d commit 9abdf73

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed

src/lib/helpers/enums.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const richType = {
2727
Button: 'button_template',
2828
MultiSelect: 'multi-select_template',
2929
Generic: 'generic_template',
30-
Upload: 'upload_template'
30+
Upload: 'upload_template',
31+
JsCode: 'js_code',
3132
}
3233
export const RichType = Object.freeze(richType);
3334

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<script>
2+
import { onMount } from "svelte";
3+
import { marked } from "marked";
4+
import 'overlayscrollbars/overlayscrollbars.css';
5+
import { OverlayScrollbars } from 'overlayscrollbars';
6+
import { v4 as uuidv4 } from 'uuid';
7+
8+
/** @type {import('$conversationTypes').ChatResponseModel?} */
9+
export let message;
10+
11+
/** @type {boolean} */
12+
export let scrollable = false;
13+
14+
const scrollbarId = uuidv4();
15+
const options = {
16+
scrollbars: {
17+
visibility: 'auto',
18+
autoHide: 'move',
19+
autoHideDelay: 100,
20+
dragScroll: true,
21+
clickScroll: false,
22+
theme: 'os-theme-dark',
23+
pointers: ['mouse', 'touch', 'pen']
24+
}
25+
};
26+
27+
onMount(() => {
28+
if (scrollable) {
29+
initScrollbar();
30+
}
31+
32+
initCode();
33+
});
34+
35+
function initScrollbar() {
36+
const elem = document.querySelector(`#js-interpreter-scrollbar-${scrollbarId}`);
37+
if (elem) {
38+
// @ts-ignore
39+
const scrollbar = OverlayScrollbars(elem, options);
40+
}
41+
}
42+
43+
function initCode() {
44+
const text = message?.rich_content?.message?.text || message?.text || '';
45+
const parsedText = marked.lexer(text);
46+
// @ts-ignore
47+
const codeText = parsedText.find(token => token.type === 'code' || token.type === 'html')?.text || '';
48+
const code = codeText.replace(/<script\b[^>]*>([\s\S]*?)<\/script>/i, '$1');
49+
eval(code);
50+
}
51+
</script>
52+
53+
<div id={`js-interpreter-scrollbar-${scrollbarId}`}>
54+
<div id={`chart-${message?.message_id}`}></div>
55+
</div>
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<script>
22
import Markdown from "$lib/common/markdown/Markdown.svelte";
3+
import { RichType } from "$lib/helpers/enums";
4+
import RcJsInterpreter from "./rc-js-interpreter.svelte";
35
4-
/** @type {any} */
6+
/** @type {import('$conversationTypes').ChatResponseModel?} */
57
export let message;
68
79
/** @type {string} */
@@ -12,13 +14,21 @@
1214
1315
/** @type {string} */
1416
export let markdownClasses = '';
17+
18+
$: text = message?.rich_content?.message?.text || message?.text || '';
1519
</script>
1620
17-
<div
18-
class={`ctext-wrap bg-primary ${containerClasses}`}
19-
style={`${containerStyles}`}
20-
>
21-
<div class="flex-shrink-0 align-self-center">
22-
<Markdown containerClasses={markdownClasses} text={message?.rich_content?.message?.text || message?.text} rawText />
21+
{#if text}
22+
<div
23+
class={`ctext-wrap bg-primary ${containerClasses}`}
24+
style={`${containerStyles}`}
25+
>
26+
<div class="flex-shrink-0 align-self-center">
27+
{#if message?.rich_content?.message?.rich_type === RichType.JsCode}
28+
<RcJsInterpreter message={message} scrollable />
29+
{:else}
30+
<Markdown containerClasses={markdownClasses} text={text} rawText />
31+
{/if}
32+
</div>
2333
</div>
24-
</div>
34+
{/if}

src/routes/chat/[agentId]/[conversationId]/rich-content/rich-content.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
}
4949
</script>
5050
51+
52+
5153
{#if message?.rich_content?.editor === EditorType.File}
5254
<ChatAttachmentOptions options={options} disabled={disabled} onConfirm={(title, payload) => handleConfirm(title, payload)} />
53-
{/if}
54-
55-
{#if message?.rich_content?.editor !== EditorType.File}
55+
{:else}
5656
{#if !isComplexElement}
5757
<RcPlainOptions options={options} isMultiSelect={isMultiSelect} disabled={disabled} onConfirm={(title, payload) => handleConfirm(title, payload)} />
5858
{:else}

0 commit comments

Comments
 (0)