freddyaboulton HF Staff commited on
Commit
6f9c9e5
·
verified ·
1 Parent(s): 083c6e3

Upload folder using huggingface_hub

Browse files
6.0.0/chatbot/Index.svelte ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BaseChatBot } from "./shared/ChatBot.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import ChatBot from "./shared/ChatBot.svelte";
7
+ import { Block, BlockLabel } from "@gradio/atoms";
8
+ import { Chat } from "@gradio/icons";
9
+ import { StatusTracker } from "@gradio/statustracker";
10
+ import type { Message, ExampleMessage, NormalisedMessage } from "./types";
11
+ import type { ChatbotProps, ChatbotEvents } from "./types";
12
+ import { normalise_messages } from "./shared/utils";
13
+ import { Gradio } from "@gradio/utils";
14
+
15
+ let props = $props();
16
+
17
+ const gradio = new Gradio<ChatbotEvents, ChatbotProps>(props);
18
+
19
+ let _value: NormalisedMessage[] | null = $derived(
20
+ normalise_messages(gradio.props.value as Message[], gradio.shared.root)
21
+ );
22
+
23
+ let show_progress = $derived.by(() => {
24
+ if (gradio.shared.loading_status.status === "error") {
25
+ return "full";
26
+ }
27
+ return gradio.shared.loading_status.show_progress === "hidden"
28
+ ? "hidden"
29
+ : "minimal";
30
+ });
31
+ </script>
32
+
33
+ <Block
34
+ elem_id={gradio.shared.elem_id}
35
+ elem_classes={gradio.shared.elem_classes}
36
+ visible={gradio.shared.visible}
37
+ padding={false}
38
+ scale={gradio.shared.scale}
39
+ min_width={gradio.shared.min_width}
40
+ height={gradio.props.height}
41
+ resizable={gradio.props.resizable}
42
+ min_height={gradio.props.min_height}
43
+ max_height={gradio.props.max_height}
44
+ allow_overflow={true}
45
+ flex={true}
46
+ overflow_behavior="auto"
47
+ >
48
+ {#if gradio.shared.loading_status}
49
+ <StatusTracker
50
+ autoscroll={gradio.shared.autoscroll}
51
+ i18n={gradio.i18n}
52
+ {...gradio.shared.loading_status}
53
+ {show_progress}
54
+ on_clear_status={() =>
55
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
56
+ />
57
+ {/if}
58
+ <div class="wrapper">
59
+ {#if gradio.shared.show_label}
60
+ <BlockLabel
61
+ show_label={gradio.shared.show_label}
62
+ Icon={Chat}
63
+ float={true}
64
+ label={gradio.shared.label || "Chatbot"}
65
+ />
66
+ {/if}
67
+ <ChatBot
68
+ i18n={gradio.i18n}
69
+ selectable={gradio.props._selectable}
70
+ likeable={gradio.props.likeable}
71
+ feedback_options={gradio.props.feedback_options}
72
+ feedback_value={gradio.props.feedback_value}
73
+ show_share_button={(gradio.props.buttons ?? ["share"]).includes("share")}
74
+ show_copy_all_button={(gradio.props.buttons ?? ["copy_all"]).includes(
75
+ "copy_all"
76
+ )}
77
+ value={_value}
78
+ latex_delimiters={gradio.props.latex_delimiters}
79
+ display_consecutive_in_same_bubble={gradio.props
80
+ .group_consecutive_messages}
81
+ render_markdown={gradio.props.render_markdown}
82
+ theme_mode={gradio.shared.theme_mode}
83
+ editable={gradio.props.editable}
84
+ pending_message={gradio.shared.loading_status?.status === "pending"}
85
+ generating={gradio.shared.loading_status?.status === "generating"}
86
+ rtl={gradio.props.rtl}
87
+ show_copy_button={(gradio.props.buttons ?? ["copy"]).includes("copy")}
88
+ like_user_message={gradio.props.like_user_message}
89
+ show_progress={gradio.shared.loading_status?.show_progress || "full"}
90
+ on:change={() => (
91
+ (gradio.props.value = gradio.props.value),
92
+ gradio.dispatch("change", gradio.props.value)
93
+ )}
94
+ on:select={(e) => gradio.dispatch("select", e.detail)}
95
+ on:like={(e) => gradio.dispatch("like", e.detail)}
96
+ on:share={(e) => gradio.dispatch("share", e.detail)}
97
+ on:error={(e) => gradio.dispatch("error", e.detail)}
98
+ on:example_select={(e) => gradio.dispatch("example_select", e.detail)}
99
+ on:option_select={(e) => gradio.dispatch("option_select", e.detail)}
100
+ on:retry={(e) => gradio.dispatch("retry", e.detail)}
101
+ on:undo={(e) => gradio.dispatch("undo", e.detail)}
102
+ on:clear={() => {
103
+ gradio.props.value = [];
104
+ gradio.dispatch("clear");
105
+ }}
106
+ on:copy={(e) => gradio.dispatch("copy", e.detail)}
107
+ on:edit={(e) => {
108
+ if (gradio.props.value === null || gradio.props.value.length === 0)
109
+ return;
110
+ //@ts-ignore
111
+ gradio.props.value[e.detail.index].content = [
112
+ { text: e.detail.value, type: "text" }
113
+ ];
114
+ gradio.dispatch("edit", e.detail);
115
+ }}
116
+ avatar_images={gradio.props.avatar_images}
117
+ sanitize_html={gradio.props.sanitize_html}
118
+ line_breaks={gradio.props.line_breaks}
119
+ autoscroll={gradio.props.autoscroll}
120
+ layout={gradio.props.layout}
121
+ placeholder={gradio.props.placeholder}
122
+ examples={gradio.props.examples}
123
+ _retryable={gradio.props._retryable}
124
+ _undoable={gradio.props._undoable}
125
+ upload={(...args) => gradio.shared.client.upload(...args)}
126
+ _fetch={(...args) => gradio.shared.client.fetch(...args)}
127
+ load_component={gradio.shared.load_component}
128
+ allow_file_downloads={gradio.props.allow_file_downloads}
129
+ allow_tags={gradio.props.allow_tags}
130
+ watermark={gradio.props.watermark}
131
+ />
132
+ </div>
133
+ </Block>
134
+
135
+ <style>
136
+ .wrapper {
137
+ display: flex;
138
+ position: relative;
139
+ flex-direction: column;
140
+ align-items: start;
141
+ width: 100%;
142
+ height: 100%;
143
+ flex-grow: 1;
144
+ }
145
+
146
+ :global(.progress-text) {
147
+ right: auto;
148
+ }
149
+ </style>
6.0.0/chatbot/package.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/chatbot",
3
+ "version": "0.28.0",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/gallery": "workspace:^",
13
+ "@gradio/icons": "workspace:^",
14
+ "@gradio/markdown-code": "workspace:^",
15
+ "@gradio/plot": "workspace:^",
16
+ "@gradio/statustracker": "workspace:^",
17
+ "@gradio/theme": "workspace:^",
18
+ "@gradio/upload": "workspace:^",
19
+ "@gradio/utils": "workspace:^",
20
+ "@types/katex": "^0.16.7",
21
+ "@types/prismjs": "1.26.5",
22
+ "dequal": "^2.0.3"
23
+ },
24
+ "devDependencies": {
25
+ "@gradio/audio": "workspace:^",
26
+ "@gradio/image": "workspace:^",
27
+ "@gradio/preview": "workspace:^",
28
+ "@gradio/video": "workspace:^"
29
+ },
30
+ "main_changeset": true,
31
+ "main": "./Index.svelte",
32
+ "exports": {
33
+ "./package.json": "./package.json",
34
+ ".": {
35
+ "gradio": "./Index.svelte",
36
+ "svelte": "./dist/Index.svelte",
37
+ "types": "./dist/Index.svelte.d.ts"
38
+ }
39
+ },
40
+ "peerDependencies": {
41
+ "svelte": "^5.43.4"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/gradio-app/gradio.git",
46
+ "directory": "js/chatbot"
47
+ }
48
+ }
6.0.0/chatbot/shared/ButtonPanel.svelte ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import LikeDislike from "./LikeDislike.svelte";
3
+ import Copy from "./Copy.svelte";
4
+ import type { FileData } from "@gradio/client";
5
+ import type { NormalisedMessage } from "../types";
6
+ import { Retry, Undo, Edit, Check, Clear, Download } from "@gradio/icons";
7
+ import {
8
+ IconButtonWrapper,
9
+ IconButton,
10
+ DownloadLink,
11
+ ShareButton
12
+ } from "@gradio/atoms";
13
+ import { all_text, is_all_text } from "./utils";
14
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
15
+ import { uploadToHuggingFace } from "@gradio/utils";
16
+
17
+ export let i18n: I18nFormatter;
18
+ export let likeable: boolean;
19
+ export let feedback_options: string[];
20
+ export let show_retry: boolean;
21
+ export let show_undo: boolean;
22
+ export let show_edit: boolean;
23
+ export let in_edit_mode: boolean;
24
+ export let show_copy_button: boolean;
25
+ export let watermark: string | null = null;
26
+ export let message: NormalisedMessage | NormalisedMessage[];
27
+ export let position: "right" | "left";
28
+ export let avatar: FileData | null;
29
+ export let generating: boolean;
30
+ export let current_feedback: string | null;
31
+ export let file: FileData | null = null;
32
+ export let show_download_button = false;
33
+ export let show_share_button = false;
34
+
35
+ export let handle_action: (selected: string | null) => void;
36
+ export let layout: "bubble" | "panel";
37
+ export let dispatch: any;
38
+
39
+ $: message_text = is_all_text(message) ? all_text(message) : "";
40
+ $: show_copy = show_copy_button && message && is_all_text(message);
41
+ </script>
42
+
43
+ {#if show_copy || show_retry || show_undo || show_edit || likeable || show_download_button || show_share_button}
44
+ <div
45
+ class="message-buttons-{position} {layout} message-buttons {avatar !==
46
+ null && 'with-avatar'}"
47
+ >
48
+ <IconButtonWrapper top_panel={false}>
49
+ {#if in_edit_mode}
50
+ <IconButton
51
+ label={i18n("chatbot.submit")}
52
+ Icon={Check}
53
+ on:click={() => handle_action("edit_submit")}
54
+ disabled={generating}
55
+ />
56
+ <IconButton
57
+ label={i18n("chatbot.cancel")}
58
+ Icon={Clear}
59
+ on:click={() => handle_action("edit_cancel")}
60
+ disabled={generating}
61
+ />
62
+ {:else}
63
+ {#if show_copy}
64
+ <Copy
65
+ value={message_text}
66
+ on:copy={(e) => dispatch("copy", e.detail)}
67
+ {watermark}
68
+ {i18n}
69
+ />
70
+ {/if}
71
+ {#if show_download_button && file?.url}
72
+ <DownloadLink
73
+ href={file.is_stream
74
+ ? file.url?.replace("playlist.m3u8", "playlist-file")
75
+ : file.url}
76
+ download={file.orig_name || file.path || "file"}
77
+ >
78
+ <IconButton Icon={Download} label={i18n("common.download")} />
79
+ </DownloadLink>
80
+ {/if}
81
+ {#if show_share_button && file}
82
+ <ShareButton
83
+ {i18n}
84
+ on:error={(e) => dispatch("error", e.detail)}
85
+ on:share={(e) => dispatch("share", e.detail)}
86
+ formatter={async (value) => {
87
+ if (!value) return "";
88
+ let url = await uploadToHuggingFace(value.url, "url");
89
+ const mime_type = value.mime_type || "";
90
+ if (mime_type.startsWith("audio/")) {
91
+ return `<audio controls src="${url}"></audio>`;
92
+ } else if (mime_type.startsWith("video/")) {
93
+ return `<video controls src="${url}"></video>`;
94
+ } else if (mime_type.startsWith("image/")) {
95
+ return `<img src="${url}" />`;
96
+ }
97
+ return "";
98
+ }}
99
+ value={file}
100
+ />
101
+ {/if}
102
+ {#if show_retry}
103
+ <IconButton
104
+ Icon={Retry}
105
+ label={i18n("chatbot.retry")}
106
+ on:click={() => handle_action("retry")}
107
+ disabled={generating}
108
+ />
109
+ {/if}
110
+ {#if show_undo}
111
+ <IconButton
112
+ label={i18n("chatbot.undo")}
113
+ Icon={Undo}
114
+ on:click={() => handle_action("undo")}
115
+ disabled={generating}
116
+ />
117
+ {/if}
118
+ {#if show_edit}
119
+ <IconButton
120
+ label={i18n("chatbot.edit")}
121
+ Icon={Edit}
122
+ on:click={() => handle_action("edit")}
123
+ disabled={generating}
124
+ />
125
+ {/if}
126
+ {#if likeable}
127
+ <LikeDislike
128
+ {handle_action}
129
+ {feedback_options}
130
+ selected={current_feedback}
131
+ {i18n}
132
+ />
133
+ {/if}
134
+ {/if}
135
+ </IconButtonWrapper>
136
+ </div>
137
+ {/if}
138
+
139
+ <style>
140
+ .bubble :global(.icon-button-wrapper) {
141
+ margin: 0px calc(var(--spacing-xl) * 2);
142
+ }
143
+
144
+ .message-buttons {
145
+ z-index: var(--layer-1);
146
+ }
147
+ .message-buttons-left {
148
+ align-self: flex-start;
149
+ }
150
+
151
+ .bubble.message-buttons-right {
152
+ align-self: flex-end;
153
+ }
154
+
155
+ .message-buttons-right :global(.icon-button-wrapper) {
156
+ margin-left: auto;
157
+ }
158
+
159
+ .bubble.with-avatar {
160
+ margin-left: calc(var(--spacing-xl) * 5);
161
+ margin-right: calc(var(--spacing-xl) * 5);
162
+ }
163
+
164
+ .panel {
165
+ display: flex;
166
+ align-self: flex-start;
167
+ z-index: var(--layer-1);
168
+ }
169
+ </style>
6.0.0/chatbot/shared/ChatBot.svelte ADDED
@@ -0,0 +1,532 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import {
3
+ format_chat_for_sharing,
4
+ type UndoRetryData,
5
+ type EditData,
6
+ is_last_bot_message,
7
+ group_messages,
8
+ load_components,
9
+ get_components_from_messages
10
+ } from "./utils";
11
+ import type { NormalisedMessage, Option } from "../types";
12
+ import { copy } from "@gradio/utils";
13
+ import type { CopyData } from "@gradio/utils";
14
+ import Message from "./Message.svelte";
15
+
16
+ import { dequal } from "dequal/lite";
17
+ import {
18
+ createEventDispatcher,
19
+ type SvelteComponent,
20
+ type ComponentType,
21
+ tick,
22
+ onMount
23
+ } from "svelte";
24
+
25
+ import { Trash, Community, ScrollDownArrow } from "@gradio/icons";
26
+ import { IconButtonWrapper, IconButton } from "@gradio/atoms";
27
+ import type { SelectData, LikeData } from "@gradio/utils";
28
+ import type { ExampleMessage } from "../types";
29
+ import type { FileData, Client } from "@gradio/client";
30
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
31
+ import Pending from "./Pending.svelte";
32
+ import { ShareError } from "@gradio/utils";
33
+ import { Gradio } from "@gradio/utils";
34
+
35
+ import Examples from "./Examples.svelte";
36
+
37
+ export let value: NormalisedMessage[] | null = [];
38
+ let old_value: NormalisedMessage[] | null = null;
39
+
40
+ import CopyAll from "./CopyAll.svelte";
41
+
42
+ export let _fetch: typeof fetch;
43
+ export let load_component: Gradio["load_component"];
44
+ export let allow_file_downloads: boolean;
45
+ export let display_consecutive_in_same_bubble: boolean;
46
+
47
+ let _components: Record<string, ComponentType<SvelteComponent>> = {};
48
+
49
+ const is_browser = typeof window !== "undefined";
50
+
51
+ async function update_components(): Promise<void> {
52
+ _components = await load_components(
53
+ get_components_from_messages(value),
54
+ _components,
55
+ load_component
56
+ );
57
+ }
58
+
59
+ $: component_names = get_components_from_messages(value).sort().join(", ");
60
+
61
+ $: (component_names, update_components());
62
+
63
+ export let latex_delimiters: {
64
+ left: string;
65
+ right: string;
66
+ display: boolean;
67
+ }[];
68
+ export let pending_message = false;
69
+ export let generating = false;
70
+ export let selectable = false;
71
+ export let likeable = false;
72
+ export let feedback_options: string[];
73
+ export let feedback_value: (string | null)[] | null = null;
74
+ export let editable: "user" | "all" | null = null;
75
+ export let show_share_button = false;
76
+ export let show_copy_all_button = false;
77
+ export let rtl = false;
78
+ export let show_copy_button = false;
79
+ export let avatar_images: [FileData | null, FileData | null] = [null, null];
80
+ export let sanitize_html = true;
81
+ export let render_markdown = true;
82
+ export let line_breaks = true;
83
+ export let autoscroll = true;
84
+ export let theme_mode: "system" | "light" | "dark";
85
+ export let i18n: I18nFormatter;
86
+ export let layout: "bubble" | "panel" = "bubble";
87
+ export let placeholder: string | null = null;
88
+ export let upload: Client["upload"];
89
+ export let examples: ExampleMessage[] | null = null;
90
+ export let _retryable = false;
91
+ export let _undoable = false;
92
+ export let like_user_message = false;
93
+ export let allow_tags: string[] | boolean = false;
94
+ export let watermark: string | null = null;
95
+ export let show_progress: "full" | "minimal" | "hidden" = "full";
96
+
97
+ let target: HTMLElement | null = null;
98
+ let edit_index: number | null = null;
99
+ let edit_messages: string[] = [];
100
+
101
+ onMount(() => {
102
+ target = document.querySelector("div.gradio-container");
103
+ });
104
+
105
+ let div: HTMLDivElement;
106
+
107
+ let show_scroll_button = false;
108
+
109
+ const dispatch = createEventDispatcher<{
110
+ change: undefined;
111
+ select: SelectData;
112
+ like: LikeData;
113
+ edit: EditData;
114
+ undo: UndoRetryData;
115
+ retry: UndoRetryData;
116
+ clear: undefined;
117
+ share: any;
118
+ error: string;
119
+ example_select: SelectData;
120
+ option_select: SelectData;
121
+ copy: CopyData;
122
+ }>();
123
+
124
+ function is_at_bottom(): boolean {
125
+ return div && div.offsetHeight + div.scrollTop > div.scrollHeight - 100;
126
+ }
127
+
128
+ function scroll_to_bottom(): void {
129
+ if (!div) return;
130
+ div.scrollTo(0, div.scrollHeight);
131
+ show_scroll_button = false;
132
+ }
133
+
134
+ let scroll_after_component_load = false;
135
+
136
+ async function scroll_on_value_update(): Promise<void> {
137
+ if (!autoscroll) return;
138
+ if (is_at_bottom()) {
139
+ // Child components may be loaded asynchronously,
140
+ // so trigger the scroll again after they load.
141
+ scroll_after_component_load = true;
142
+ await tick(); // Wait for the DOM to update so that the scrollHeight is correct
143
+ await new Promise((resolve) => setTimeout(resolve, 300));
144
+ scroll_to_bottom();
145
+ }
146
+ }
147
+ onMount(() => {
148
+ if (autoscroll) {
149
+ scroll_to_bottom();
150
+ }
151
+ scroll_on_value_update();
152
+ });
153
+ $: if (value || pending_message || _components) {
154
+ scroll_on_value_update();
155
+ }
156
+
157
+ onMount(() => {
158
+ function handle_scroll(): void {
159
+ if (is_at_bottom()) {
160
+ show_scroll_button = false;
161
+ } else {
162
+ scroll_after_component_load = false;
163
+ show_scroll_button = true;
164
+ }
165
+ }
166
+
167
+ div?.addEventListener("scroll", handle_scroll);
168
+ return () => {
169
+ div?.removeEventListener("scroll", handle_scroll);
170
+ };
171
+ });
172
+
173
+ $: {
174
+ if (!dequal(value, old_value)) {
175
+ old_value = value;
176
+ dispatch("change");
177
+ }
178
+ }
179
+ $: groupedMessages =
180
+ value && group_messages(value, display_consecutive_in_same_bubble);
181
+ $: options = value && get_last_bot_options();
182
+
183
+ function handle_action(
184
+ i: number,
185
+ message: NormalisedMessage,
186
+ selected: string | null
187
+ ): void {
188
+ if (selected === "undo" || selected === "retry") {
189
+ const val_ = value as NormalisedMessage[];
190
+ // iterate through messages until we find the last user message
191
+ // the index of this message is where the user needs to edit the chat history
192
+ let last_index = val_.length - 1;
193
+ while (val_[last_index].role === "assistant") {
194
+ last_index--;
195
+ }
196
+ dispatch(selected, {
197
+ index: val_[last_index].index,
198
+ value: val_[last_index].content
199
+ });
200
+ } else if (selected == "edit") {
201
+ edit_index = i;
202
+ edit_messages.push(message.content as string);
203
+ } else if (selected == "edit_cancel") {
204
+ edit_index = null;
205
+ } else if (selected == "edit_submit") {
206
+ edit_index = null;
207
+ dispatch("edit", {
208
+ index: message.index,
209
+ _dispatch_value: [{ type: "text", text: edit_messages[i].slice() }],
210
+ value: edit_messages[i].slice(),
211
+ previous_value: message.content as string
212
+ });
213
+ } else {
214
+ let feedback =
215
+ selected === "Like"
216
+ ? true
217
+ : selected === "Dislike"
218
+ ? false
219
+ : selected || "";
220
+ if (!groupedMessages) return;
221
+
222
+ const message_group = groupedMessages[i];
223
+ const [first, last] = [
224
+ message_group[0],
225
+ message_group[message_group.length - 1]
226
+ ];
227
+
228
+ dispatch("like", {
229
+ index: first.index as number,
230
+ value: message_group.map((m) => m.content),
231
+ liked: feedback
232
+ });
233
+ }
234
+ }
235
+
236
+ function get_last_bot_options(): Option[] | undefined {
237
+ if (!value || !groupedMessages || groupedMessages.length === 0)
238
+ return undefined;
239
+ const last_group = groupedMessages[groupedMessages.length - 1];
240
+ if (last_group[0].role !== "assistant") return undefined;
241
+ return last_group[last_group.length - 1].options;
242
+ }
243
+ </script>
244
+
245
+ {#if value !== null && value.length > 0}
246
+ <IconButtonWrapper>
247
+ {#if show_share_button}
248
+ <IconButton
249
+ Icon={Community}
250
+ on:click={async () => {
251
+ try {
252
+ // @ts-ignore
253
+ const formatted = await format_chat_for_sharing(value);
254
+ dispatch("share", {
255
+ description: formatted
256
+ });
257
+ } catch (e) {
258
+ console.error(e);
259
+ let message = e instanceof ShareError ? e.message : "Share failed.";
260
+ dispatch("error", message);
261
+ }
262
+ }}
263
+ />
264
+ {/if}
265
+ <IconButton
266
+ Icon={Trash}
267
+ on:click={() => dispatch("clear")}
268
+ label={i18n("chatbot.clear")}
269
+ ></IconButton>
270
+ {#if show_copy_all_button}
271
+ <CopyAll {value} {watermark} />
272
+ {/if}
273
+ </IconButtonWrapper>
274
+ {/if}
275
+
276
+ <div
277
+ class={layout === "bubble" ? "bubble-wrap" : "panel-wrap"}
278
+ bind:this={div}
279
+ role="log"
280
+ aria-label="chatbot conversation"
281
+ aria-live="polite"
282
+ >
283
+ {#if value !== null && value.length > 0 && groupedMessages !== null}
284
+ <div class="message-wrap" use:copy>
285
+ {#each groupedMessages as messages, i}
286
+ {@const role = messages[0].role === "user" ? "user" : "bot"}
287
+ {@const avatar_img = avatar_images[role === "user" ? 0 : 1]}
288
+ {@const opposite_avatar_img = avatar_images[role === "user" ? 0 : 1]}
289
+ {@const feedback_index = groupedMessages
290
+ .slice(0, i)
291
+ .filter((m) => m[0].role === "assistant").length}
292
+ {@const current_feedback =
293
+ role === "bot" && feedback_value && feedback_value[feedback_index]
294
+ ? feedback_value[feedback_index]
295
+ : null}
296
+ <Message
297
+ {messages}
298
+ {display_consecutive_in_same_bubble}
299
+ {opposite_avatar_img}
300
+ {avatar_img}
301
+ {role}
302
+ {layout}
303
+ {dispatch}
304
+ {i18n}
305
+ {_fetch}
306
+ {line_breaks}
307
+ {theme_mode}
308
+ {target}
309
+ {upload}
310
+ {selectable}
311
+ {sanitize_html}
312
+ {render_markdown}
313
+ {rtl}
314
+ {i}
315
+ {value}
316
+ {latex_delimiters}
317
+ {_components}
318
+ {generating}
319
+ {feedback_options}
320
+ {current_feedback}
321
+ {allow_tags}
322
+ {watermark}
323
+ show_like={role === "user" ? likeable && like_user_message : likeable}
324
+ show_retry={_retryable && is_last_bot_message(messages, value)}
325
+ show_undo={_undoable && is_last_bot_message(messages, value)}
326
+ show_edit={editable === "all" ||
327
+ (editable == "user" &&
328
+ role === "user" &&
329
+ messages.length > 0 &&
330
+ messages[messages.length - 1].type == "text")}
331
+ in_edit_mode={edit_index === i}
332
+ bind:edit_messages
333
+ {show_copy_button}
334
+ handle_action={(selected) => {
335
+ if (selected == "edit") {
336
+ edit_messages.splice(0, edit_messages.length);
337
+ }
338
+ if (selected === "edit" || selected === "edit_submit") {
339
+ messages.forEach((msg, index) => {
340
+ handle_action(selected === "edit" ? i : index, msg, selected);
341
+ });
342
+ } else {
343
+ handle_action(i, messages[0], selected);
344
+ }
345
+ }}
346
+ scroll={is_browser ? scroll : () => {}}
347
+ {allow_file_downloads}
348
+ on:copy={(e) => dispatch("copy", e.detail)}
349
+ />
350
+ {#if show_progress !== "hidden" && generating && messages[messages.length - 1].role === "assistant" && messages[messages.length - 1].metadata?.status === "done"}
351
+ <Pending {layout} {avatar_images} />
352
+ {/if}
353
+ {/each}
354
+ {#if show_progress !== "hidden" && pending_message}
355
+ <Pending {layout} {avatar_images} />
356
+ {:else if options}
357
+ <div class="options">
358
+ {#each options as option, index}
359
+ <button
360
+ class="option"
361
+ on:click={() =>
362
+ dispatch("option_select", {
363
+ index: index,
364
+ value: option.value
365
+ })}
366
+ >
367
+ {option.label || option.value}
368
+ </button>
369
+ {/each}
370
+ </div>
371
+ {/if}
372
+ </div>
373
+ {:else}
374
+ <Examples
375
+ {examples}
376
+ {placeholder}
377
+ {latex_delimiters}
378
+ on:example_select={(e) => dispatch("example_select", e.detail)}
379
+ />
380
+ {/if}
381
+ </div>
382
+
383
+ {#if show_scroll_button}
384
+ <div class="scroll-down-button-container">
385
+ <IconButton
386
+ Icon={ScrollDownArrow}
387
+ label="Scroll down"
388
+ size="large"
389
+ on:click={scroll_to_bottom}
390
+ />
391
+ </div>
392
+ {/if}
393
+
394
+ <style>
395
+ .panel-wrap {
396
+ width: 100%;
397
+ overflow-y: auto;
398
+ }
399
+
400
+ .bubble-wrap {
401
+ width: 100%;
402
+ overflow-y: auto;
403
+ height: 100%;
404
+ padding-top: var(--spacing-xxl);
405
+ }
406
+
407
+ @media (prefers-color-scheme: dark) {
408
+ .bubble-wrap {
409
+ background: var(--background-fill-secondary);
410
+ }
411
+ }
412
+
413
+ .message-wrap :global(.prose.chatbot.md) {
414
+ opacity: 0.8;
415
+ overflow-wrap: break-word;
416
+ }
417
+
418
+ .message-wrap :global(.message-row .md img) {
419
+ border-radius: var(--radius-xl);
420
+ margin: var(--size-2);
421
+ width: 400px;
422
+ max-width: 30vw;
423
+ max-height: 30vw;
424
+ }
425
+
426
+ /* link styles */
427
+ .message-wrap :global(.message a) {
428
+ color: var(--color-text-link);
429
+ text-decoration: underline;
430
+ }
431
+
432
+ /* table styles */
433
+ .message-wrap :global(.bot:not(:has(.table-wrap)) table),
434
+ .message-wrap :global(.bot:not(:has(.table-wrap)) tr),
435
+ .message-wrap :global(.bot:not(:has(.table-wrap)) td),
436
+ .message-wrap :global(.bot:not(:has(.table-wrap)) th) {
437
+ border: 1px solid var(--border-color-primary);
438
+ }
439
+
440
+ .message-wrap :global(.user table),
441
+ .message-wrap :global(.user tr),
442
+ .message-wrap :global(.user td),
443
+ .message-wrap :global(.user th) {
444
+ border: 1px solid var(--border-color-accent);
445
+ }
446
+
447
+ /* KaTeX */
448
+ .message-wrap :global(span.katex) {
449
+ font-size: var(--text-lg);
450
+ direction: ltr;
451
+ }
452
+
453
+ .message-wrap :global(span.katex-display) {
454
+ margin-top: 0;
455
+ }
456
+
457
+ .message-wrap :global(pre) {
458
+ position: relative;
459
+ }
460
+
461
+ .message-wrap :global(.grid-wrap) {
462
+ max-height: 80% !important;
463
+ max-width: 600px;
464
+ object-fit: contain;
465
+ }
466
+
467
+ .message-wrap > div :global(p:not(:first-child)) {
468
+ margin-top: var(--spacing-xxl);
469
+ }
470
+
471
+ .message-wrap {
472
+ display: flex;
473
+ flex-direction: column;
474
+ justify-content: space-between;
475
+ margin-bottom: var(--spacing-xxl);
476
+ }
477
+
478
+ .panel-wrap :global(.message-row:first-child) {
479
+ padding-top: calc(var(--spacing-xxl) * 2);
480
+ }
481
+
482
+ .scroll-down-button-container {
483
+ position: absolute;
484
+ bottom: 10px;
485
+ left: 50%;
486
+ transform: translateX(-50%);
487
+ z-index: var(--layer-top);
488
+ }
489
+ .scroll-down-button-container :global(button) {
490
+ border-radius: 50%;
491
+ box-shadow: var(--shadow-drop);
492
+ transition:
493
+ box-shadow 0.2s ease-in-out,
494
+ transform 0.2s ease-in-out;
495
+ }
496
+ .scroll-down-button-container :global(button:hover) {
497
+ box-shadow:
498
+ var(--shadow-drop),
499
+ 0 2px 2px rgba(0, 0, 0, 0.05);
500
+ transform: translateY(-2px);
501
+ }
502
+
503
+ .options {
504
+ margin-left: auto;
505
+ padding: var(--spacing-xxl);
506
+ display: grid;
507
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
508
+ gap: var(--spacing-xxl);
509
+ max-width: calc(min(4 * 200px + 5 * var(--spacing-xxl), 100%));
510
+ justify-content: end;
511
+ }
512
+
513
+ .option {
514
+ display: flex;
515
+ flex-direction: column;
516
+ align-items: center;
517
+ padding: var(--spacing-xl);
518
+ border: 1px dashed var(--border-color-primary);
519
+ border-radius: var(--radius-md);
520
+ background-color: var(--background-fill-secondary);
521
+ cursor: pointer;
522
+ transition: var(--button-transition);
523
+ max-width: var(--size-56);
524
+ width: 100%;
525
+ justify-content: center;
526
+ }
527
+
528
+ .option:hover {
529
+ background-color: var(--color-accent-soft);
530
+ border-color: var(--border-color-accent);
531
+ }
532
+ </style>
6.0.0/chatbot/shared/Component.svelte ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let type:
3
+ | "gallery"
4
+ | "plot"
5
+ | "audio"
6
+ | "video"
7
+ | "image"
8
+ | "dataframe"
9
+ | "model3d"
10
+ | string;
11
+ export let components;
12
+ export let value;
13
+ export let target;
14
+ export let theme_mode;
15
+ export let props;
16
+ export let i18n;
17
+ export let upload;
18
+ export let _fetch;
19
+ export let allow_file_downloads: boolean;
20
+ export let display_icon_button_wrapper_top_corner = false;
21
+
22
+ let image_fullscreen = false;
23
+ let image_container: HTMLElement;
24
+
25
+ function handle_fullscreen(event: CustomEvent<boolean>): void {
26
+ image_fullscreen = event.detail;
27
+ if (image_fullscreen && image_container) {
28
+ image_container.requestFullscreen?.();
29
+ } else if (document.fullscreenElement) {
30
+ document.exitFullscreen?.();
31
+ }
32
+ }
33
+ </script>
34
+
35
+ {#if type === "gallery"}
36
+ <svelte:component
37
+ this={components[type]}
38
+ {...props}
39
+ {value}
40
+ display_icon_button_wrapper_top_corner={false}
41
+ show_label={props.label ? true : false}
42
+ {i18n}
43
+ {_fetch}
44
+ allow_preview={false}
45
+ interactive={false}
46
+ mode="minimal"
47
+ fixed_height={1}
48
+ on:load
49
+ />
50
+ {:else if type === "dataframe"}
51
+ <svelte:component
52
+ this={components[type]}
53
+ {...props}
54
+ {value}
55
+ show_label={props.label ? true : false}
56
+ {i18n}
57
+ interactive={false}
58
+ line_breaks={props.line_breaks}
59
+ wrap={true}
60
+ root=""
61
+ gradio={{ dispatch: () => {}, i18n }}
62
+ datatype={props.datatype}
63
+ latex_delimiters={props.latex_delimiters}
64
+ col_count={props.col_count}
65
+ row_count={props.row_count}
66
+ on:load
67
+ />
68
+ {:else if type === "plot"}
69
+ <svelte:component
70
+ this={components[type]}
71
+ {...props}
72
+ {value}
73
+ {target}
74
+ {theme_mode}
75
+ bokeh_version={props.bokeh_version}
76
+ caption={props.caption || ""}
77
+ show_actions_button={true}
78
+ on:load
79
+ />
80
+ {:else if type === "audio"}
81
+ <svelte:component
82
+ this={components[type]}
83
+ {...props}
84
+ {value}
85
+ show_label={props.label ? true : false}
86
+ show_share_button={false}
87
+ {i18n}
88
+ waveform_settings={{
89
+ ...props.waveform_settings,
90
+ autoplay: props.autoplay
91
+ }}
92
+ show_download_button={false}
93
+ display_icon_button_wrapper_top_corner={false}
94
+ minimal={true}
95
+ on:load
96
+ />
97
+ {:else if type === "video"}
98
+ <svelte:component
99
+ this={components[type]}
100
+ {...props}
101
+ autoplay={props.autoplay}
102
+ value={value.video || value}
103
+ show_label={props.label ? true : false}
104
+ show_share_button={false}
105
+ {i18n}
106
+ {upload}
107
+ display_icon_button_wrapper_top_corner={false}
108
+ show_download_button={false}
109
+ on:load
110
+ >
111
+ <track kind="captions" />
112
+ </svelte:component>
113
+ {:else if type === "image"}
114
+ <div bind:this={image_container}>
115
+ <svelte:component
116
+ this={components[type]}
117
+ {...props}
118
+ {value}
119
+ show_label={props.label ? true : false}
120
+ display_icon_button_wrapper_top_corner={false}
121
+ buttons={["fullscreen"]}
122
+ fullscreen={image_fullscreen}
123
+ show_button_background={false}
124
+ on:fullscreen={handle_fullscreen}
125
+ on:load
126
+ {i18n}
127
+ />
128
+ </div>
129
+ {:else if type === "html"}
130
+ <svelte:component
131
+ this={components[type]}
132
+ {...props}
133
+ {value}
134
+ show_label={false}
135
+ show_share_button={false}
136
+ {i18n}
137
+ gradio={{ dispatch: () => {} }}
138
+ on:load
139
+ />
140
+ {:else if type === "model3d"}
141
+ <svelte:component
142
+ this={components[type]}
143
+ {...props}
144
+ {value}
145
+ clear_color={props.clear_color}
146
+ display_mode={props.display_mode}
147
+ zoom_speed={props.zoom_speed}
148
+ pan_speed={props.pan_speed}
149
+ {...props.camera_position !== undefined && {
150
+ camera_position: props.camera_position
151
+ }}
152
+ has_change_history={true}
153
+ show_label={props.label ? true : false}
154
+ root=""
155
+ interactive={false}
156
+ show_share_button={false}
157
+ gradio={{ dispatch: () => {}, i18n }}
158
+ on:load
159
+ {i18n}
160
+ />
161
+ {/if}
6.0.0/chatbot/shared/Copy.svelte ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } from "svelte";
3
+ import { onDestroy } from "svelte";
4
+ import { Copy, Check } from "@gradio/icons";
5
+ import { IconButton } from "@gradio/atoms";
6
+ import type { CopyData } from "@gradio/utils";
7
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
8
+ const dispatch = createEventDispatcher<{
9
+ change: undefined;
10
+ copy: CopyData;
11
+ }>();
12
+
13
+ let copied = false;
14
+ export let value: string;
15
+ export let watermark: string | null = null;
16
+ export let i18n: I18nFormatter;
17
+ let timer: NodeJS.Timeout;
18
+
19
+ function copy_feedback(): void {
20
+ copied = true;
21
+ if (timer) clearTimeout(timer);
22
+ timer = setTimeout(() => {
23
+ copied = false;
24
+ }, 2000);
25
+ }
26
+
27
+ async function handle_copy(): Promise<void> {
28
+ if ("clipboard" in navigator) {
29
+ dispatch("copy", { value: value });
30
+ const text_to_copy = watermark ? `${value}\n\n${watermark}` : value;
31
+ await navigator.clipboard.writeText(text_to_copy);
32
+ copy_feedback();
33
+ } else {
34
+ const textArea = document.createElement("textarea");
35
+ const text_to_copy = watermark ? `${value}\n\n${watermark}` : value;
36
+ textArea.value = text_to_copy;
37
+
38
+ textArea.style.position = "absolute";
39
+ textArea.style.left = "-999999px";
40
+
41
+ document.body.prepend(textArea);
42
+ textArea.select();
43
+
44
+ try {
45
+ document.execCommand("copy");
46
+ copy_feedback();
47
+ } catch (error) {
48
+ console.error(error);
49
+ } finally {
50
+ textArea.remove();
51
+ }
52
+ }
53
+ }
54
+
55
+ onDestroy(() => {
56
+ if (timer) clearTimeout(timer);
57
+ });
58
+ </script>
59
+
60
+ <IconButton
61
+ on:click={handle_copy}
62
+ label={copied ? i18n("chatbot.copied_message") : i18n("chatbot.copy_message")}
63
+ Icon={copied ? Check : Copy}
64
+ />
6.0.0/chatbot/shared/CopyAll.svelte ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onDestroy } from "svelte";
3
+ import { Copy, Check } from "@gradio/icons";
4
+ import type { NormalisedMessage } from "../types";
5
+ import { IconButton } from "@gradio/atoms";
6
+
7
+ let copied = false;
8
+ export let value: NormalisedMessage[] | null;
9
+ export let watermark: string | null = null;
10
+
11
+ let timer: NodeJS.Timeout;
12
+
13
+ function copy_feedback(): void {
14
+ copied = true;
15
+ if (timer) clearTimeout(timer);
16
+ timer = setTimeout(() => {
17
+ copied = false;
18
+ }, 1000);
19
+ }
20
+
21
+ const copy_conversation = (): void => {
22
+ if (value) {
23
+ const conversation_value = value
24
+ .map((message) => {
25
+ if (message.type === "text") {
26
+ return `${message.role}: ${message.content}`;
27
+ }
28
+ return `${message.role}: ${message.content.value.url}`;
29
+ })
30
+ .join("\n\n");
31
+
32
+ const text_to_copy = watermark
33
+ ? `${conversation_value}\n\n${watermark}`
34
+ : conversation_value;
35
+
36
+ navigator.clipboard.writeText(text_to_copy).catch((err) => {
37
+ console.error("Failed to copy conversation: ", err);
38
+ });
39
+ }
40
+ };
41
+
42
+ async function handle_copy(): Promise<void> {
43
+ if ("clipboard" in navigator) {
44
+ copy_conversation();
45
+ copy_feedback();
46
+ }
47
+ }
48
+
49
+ onDestroy(() => {
50
+ if (timer) clearTimeout(timer);
51
+ });
52
+ </script>
53
+
54
+ <IconButton
55
+ Icon={copied ? Check : Copy}
56
+ on:click={handle_copy}
57
+ label={copied ? "Copied conversation" : "Copy conversation"}
58
+ ></IconButton>
6.0.0/chatbot/shared/Download.svelte ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="16"
3
+ height="16"
4
+ viewBox="0 0 12 12"
5
+ fill="none"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ >
8
+ <path
9
+ d="M6.27701 8.253C6.24187 8.29143 6.19912 8.32212 6.15147 8.34311C6.10383 8.36411 6.05233 8.37495 6.00026 8.37495C5.94819 8.37495 5.89669 8.36411 5.84905 8.34311C5.8014 8.32212 5.75865 8.29143 5.72351 8.253L3.72351 6.0655C3.65798 5.99185 3.62408 5.89536 3.62916 5.79691C3.63424 5.69846 3.67788 5.60596 3.75064 5.53945C3.8234 5.47293 3.91943 5.43774 4.01794 5.44149C4.11645 5.44525 4.20952 5.48764 4.27701 5.5595L5.62501 7.0345V1.5C5.62501 1.40054 5.66452 1.30516 5.73485 1.23483C5.80517 1.16451 5.90055 1.125 6.00001 1.125C6.09947 1.125 6.19485 1.16451 6.26517 1.23483C6.3355 1.30516 6.37501 1.40054 6.37501 1.5V7.034L7.72351 5.559C7.79068 5.4856 7.88425 5.44189 7.98364 5.43748C8.08304 5.43308 8.18011 5.46833 8.25351 5.5355C8.32691 5.60267 8.37062 5.69624 8.37503 5.79563C8.37943 5.89503 8.34418 5.9921 8.27701 6.0655L6.27701 8.253Z"
10
+ fill="currentColor"
11
+ />
12
+ <path
13
+ d="M1.875 7.39258C1.875 7.29312 1.83549 7.19774 1.76517 7.12741C1.69484 7.05709 1.59946 7.01758 1.5 7.01758C1.40054 7.01758 1.30516 7.05709 1.23483 7.12741C1.16451 7.19774 1.125 7.29312 1.125 7.39258V7.42008C1.125 8.10358 1.125 8.65508 1.1835 9.08858C1.2435 9.53858 1.3735 9.91758 1.674 10.2186C1.975 10.5196 2.354 10.6486 2.804 10.7096C3.2375 10.7676 3.789 10.7676 4.4725 10.7676H7.5275C8.211 10.7676 8.7625 10.7676 9.196 10.7096C9.646 10.6486 10.025 10.5196 10.326 10.2186C10.627 9.91758 10.756 9.53858 10.817 9.08858C10.875 8.65508 10.875 8.10358 10.875 7.42008V7.39258C10.875 7.29312 10.8355 7.19774 10.7652 7.12741C10.6948 7.05709 10.5995 7.01758 10.5 7.01758C10.4005 7.01758 10.3052 7.05709 10.2348 7.12741C10.1645 7.19774 10.125 7.29312 10.125 7.39258C10.125 8.11008 10.124 8.61058 10.0735 8.98858C10.024 9.35558 9.9335 9.54958 9.7955 9.68808C9.657 9.82658 9.463 9.91658 9.0955 9.96608C8.718 10.0166 8.2175 10.0176 7.5 10.0176H4.5C3.7825 10.0176 3.2815 10.0166 2.904 9.96608C2.537 9.91658 2.343 9.82608 2.2045 9.68808C2.066 9.54958 1.976 9.35558 1.9265 8.98808C1.876 8.61058 1.875 8.11008 1.875 7.39258Z"
14
+ fill="currentColor"
15
+ />
16
+ </svg>
6.0.0/chatbot/shared/Examples.svelte ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Image } from "@gradio/image/shared";
3
+ import { MarkdownCode as Markdown } from "@gradio/markdown-code";
4
+ import { File, Music, Video } from "@gradio/icons";
5
+ import type { ExampleMessage } from "../types";
6
+ import { createEventDispatcher } from "svelte";
7
+ import type { SelectData } from "@gradio/utils";
8
+ import type { FileData } from "@gradio/client";
9
+
10
+ export let examples: ExampleMessage[] | null = null;
11
+ export let placeholder: string | null = null;
12
+ export let latex_delimiters: {
13
+ left: string;
14
+ right: string;
15
+ display: boolean;
16
+ }[];
17
+
18
+ const dispatch = createEventDispatcher<{
19
+ example_select: SelectData;
20
+ }>();
21
+
22
+ function handle_example_select(
23
+ i: number,
24
+ example: ExampleMessage | string
25
+ ): void {
26
+ const example_obj =
27
+ typeof example === "string" ? { text: example } : example;
28
+ dispatch("example_select", {
29
+ index: i,
30
+ value: { text: example_obj.text, files: example_obj.files }
31
+ });
32
+ }
33
+ </script>
34
+
35
+ <div class="placeholder-content" role="complementary">
36
+ {#if placeholder !== null}
37
+ <div class="placeholder">
38
+ <Markdown message={placeholder} {latex_delimiters} />
39
+ </div>
40
+ {/if}
41
+ {#if examples !== null}
42
+ <div class="examples" role="list">
43
+ {#each examples as example, i}
44
+ <button
45
+ class="example"
46
+ on:click={() =>
47
+ handle_example_select(
48
+ i,
49
+ typeof example === "string" ? { text: example } : example
50
+ )}
51
+ aria-label={`Select example ${i + 1}: ${example.display_text || example.text}`}
52
+ >
53
+ <div class="example-content">
54
+ {#if example?.icon?.url}
55
+ <div class="example-image-container">
56
+ <Image
57
+ class="example-image"
58
+ src={example.icon.url}
59
+ alt="Example icon"
60
+ />
61
+ </div>
62
+ {:else if example?.icon?.mime_type === "text"}
63
+ <div class="example-icon" aria-hidden="true">
64
+ <span class="text-icon-aa">Aa</span>
65
+ </div>
66
+ {:else if example.files !== undefined && example.files.length > 0}
67
+ {#if example.files.length > 1}
68
+ <div
69
+ class="example-icons-grid"
70
+ role="group"
71
+ aria-label="Example attachments"
72
+ >
73
+ {#each example.files.slice(0, 4) as file, i}
74
+ {#if file.mime_type?.includes("image")}
75
+ <div class="example-image-container">
76
+ <Image
77
+ class="example-image"
78
+ src={file.url}
79
+ alt={file.orig_name || `Example image ${i + 1}`}
80
+ />
81
+ {#if i === 3 && example.files.length > 4}
82
+ <div
83
+ class="image-overlay"
84
+ role="status"
85
+ aria-label={`${example.files.length - 4} more files`}
86
+ >
87
+ +{example.files.length - 4}
88
+ </div>
89
+ {/if}
90
+ </div>
91
+ {:else if file.mime_type?.includes("video")}
92
+ <div class="example-image-container">
93
+ <video
94
+ class="example-image"
95
+ src={file.url}
96
+ aria-hidden="true"
97
+ />
98
+ {#if i === 3 && example.files.length > 4}
99
+ <div
100
+ class="image-overlay"
101
+ role="status"
102
+ aria-label={`${example.files.length - 4} more files`}
103
+ >
104
+ +{example.files.length - 4}
105
+ </div>
106
+ {/if}
107
+ </div>
108
+ {:else}
109
+ <div
110
+ class="example-icon"
111
+ aria-label={`File: ${file.orig_name}`}
112
+ >
113
+ {#if file.mime_type?.includes("audio")}
114
+ <Music />
115
+ {:else}
116
+ <File />
117
+ {/if}
118
+ </div>
119
+ {/if}
120
+ {/each}
121
+ {#if example.files.length > 4}
122
+ <div class="example-icon">
123
+ <div
124
+ class="file-overlay"
125
+ role="status"
126
+ aria-label={`${example.files.length - 4} more files`}
127
+ >
128
+ +{example.files.length - 4}
129
+ </div>
130
+ </div>
131
+ {/if}
132
+ </div>
133
+ {:else if example.files[0].mime_type?.includes("image")}
134
+ <div class="example-image-container">
135
+ <Image
136
+ class="example-image"
137
+ src={example.files[0].url}
138
+ alt={example.files[0].orig_name || "Example image"}
139
+ />
140
+ </div>
141
+ {:else if example.files[0].mime_type?.includes("video")}
142
+ <div class="example-image-container">
143
+ <video
144
+ class="example-image"
145
+ src={example.files[0].url}
146
+ aria-hidden="true"
147
+ />
148
+ </div>
149
+ {:else if example.files[0].mime_type?.includes("audio")}
150
+ <div
151
+ class="example-icon"
152
+ aria-label={`File: ${example.files[0].orig_name}`}
153
+ >
154
+ <Music />
155
+ </div>
156
+ {:else}
157
+ <div
158
+ class="example-icon"
159
+ aria-label={`File: ${example.files[0].orig_name}`}
160
+ >
161
+ <File />
162
+ </div>
163
+ {/if}
164
+ {/if}
165
+
166
+ <div class="example-text-content">
167
+ <span class="example-text"
168
+ >{example.display_text || example.text}</span
169
+ >
170
+ </div>
171
+ </div>
172
+ </button>
173
+ {/each}
174
+ </div>
175
+ {/if}
176
+ </div>
177
+
178
+ <style>
179
+ .placeholder-content {
180
+ display: flex;
181
+ flex-direction: column;
182
+ height: 100%;
183
+ }
184
+
185
+ .placeholder {
186
+ align-items: center;
187
+ display: flex;
188
+ justify-content: center;
189
+ height: 100%;
190
+ flex-grow: 1;
191
+ }
192
+
193
+ .examples :global(img) {
194
+ pointer-events: none;
195
+ }
196
+
197
+ .examples {
198
+ margin: auto;
199
+ padding: var(--spacing-xxl);
200
+ display: grid;
201
+ grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
202
+ gap: var(--spacing-xl);
203
+ max-width: calc(min(4 * 240px + 5 * var(--spacing-xxl), 100%));
204
+ }
205
+
206
+ .example {
207
+ display: flex;
208
+ flex-direction: column;
209
+ align-items: flex-start;
210
+ padding: var(--spacing-xxl);
211
+ border: none;
212
+ border-radius: var(--radius-lg);
213
+ background-color: var(--block-background-fill);
214
+ cursor: pointer;
215
+ transition: all 150ms ease-in-out;
216
+ width: 100%;
217
+ gap: var(--spacing-sm);
218
+ border: var(--block-border-width) solid var(--block-border-color);
219
+ transform: translateY(0px);
220
+ }
221
+
222
+ .example:hover {
223
+ transform: translateY(-2px);
224
+ background-color: var(--color-accent-soft);
225
+ }
226
+
227
+ .example-content {
228
+ display: flex;
229
+ flex-direction: column;
230
+ align-items: flex-start;
231
+ width: 100%;
232
+ height: 100%;
233
+ }
234
+
235
+ .example-text-content {
236
+ margin-top: var(--spacing-sm);
237
+ text-align: left;
238
+ }
239
+
240
+ .example-text {
241
+ font-size: var(--text-md);
242
+ text-align: left;
243
+ overflow: hidden;
244
+ text-overflow: ellipsis;
245
+ }
246
+
247
+ .example-icons-grid {
248
+ display: flex;
249
+ gap: var(--spacing-sm);
250
+ width: 100%;
251
+ }
252
+
253
+ .example-icon {
254
+ flex-shrink: 0;
255
+ width: var(--size-8);
256
+ height: var(--size-8);
257
+ display: flex;
258
+ align-items: center;
259
+ justify-content: center;
260
+ border-radius: var(--radius-lg);
261
+ border: var(--block-border-width) solid var(--block-border-color);
262
+ background-color: var(--block-background-fill);
263
+ position: relative;
264
+ }
265
+
266
+ .example-icon :global(svg) {
267
+ width: var(--size-4);
268
+ height: var(--size-4);
269
+ color: var(--color-text-secondary);
270
+ }
271
+
272
+ .text-icon-aa {
273
+ font-size: var(--text-sm);
274
+ font-weight: var(--weight-semibold);
275
+ color: var(--color-text-secondary);
276
+ line-height: 1;
277
+ }
278
+
279
+ .example-image-container {
280
+ width: var(--size-8);
281
+ height: var(--size-8);
282
+ border-radius: var(--radius-lg);
283
+ overflow: hidden;
284
+ position: relative;
285
+ }
286
+
287
+ .example-image-container :global(img) {
288
+ width: 100%;
289
+ height: 100%;
290
+ object-fit: cover;
291
+ }
292
+
293
+ .image-overlay {
294
+ position: absolute;
295
+ top: 0;
296
+ left: 0;
297
+ right: 0;
298
+ bottom: 0;
299
+ background: rgba(0, 0, 0, 0.6);
300
+ color: white;
301
+ display: flex;
302
+ align-items: center;
303
+ justify-content: center;
304
+ font-size: var(--text-lg);
305
+ font-weight: var(--weight-semibold);
306
+ border-radius: var(--radius-lg);
307
+ }
308
+
309
+ .file-overlay {
310
+ position: absolute;
311
+ inset: 0;
312
+ background: rgba(0, 0, 0, 0.6);
313
+ color: white;
314
+ display: flex;
315
+ align-items: center;
316
+ justify-content: center;
317
+ font-size: var(--text-sm);
318
+ font-weight: var(--weight-semibold);
319
+ border-radius: var(--radius-lg);
320
+ }
321
+ </style>
6.0.0/chatbot/shared/Flag.svelte ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ id="icon"
3
+ xmlns="http://www.w3.org/2000/svg"
4
+ viewBox="0 0 32 32"
5
+ fill="none"
6
+ ><path
7
+ fill="currentColor"
8
+ d="M6,30H4V2H28l-5.8,9L28,20H6ZM6,18H24.33L19.8,11l4.53-7H6Z"
9
+ /></svg
10
+ >
6.0.0/chatbot/shared/FlagActive.svelte ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ <svg
2
+ id="icon"
3
+ xmlns="http://www.w3.org/2000/svg"
4
+ viewBox="0 0 32 32"
5
+ fill="none"
6
+ ><path fill="currentColor" d="M4,2H28l-5.8,9L28,20H6v10H4V2z" /></svg
7
+ >
6.0.0/chatbot/shared/LikeDislike.svelte ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { IconButton } from "@gradio/atoms";
3
+ import ThumbDownActive from "./ThumbDownActive.svelte";
4
+ import ThumbDownDefault from "./ThumbDownDefault.svelte";
5
+ import ThumbUpActive from "./ThumbUpActive.svelte";
6
+ import ThumbUpDefault from "./ThumbUpDefault.svelte";
7
+ import Flag from "./Flag.svelte";
8
+ import FlagActive from "./FlagActive.svelte";
9
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
10
+
11
+ export let i18n: I18nFormatter;
12
+ export let handle_action: (selected: string | null) => void;
13
+ export let feedback_options: string[];
14
+ export let selected: string | null = null;
15
+ $: extra_feedback = feedback_options.filter(
16
+ (option) => option !== "Like" && option !== "Dislike"
17
+ );
18
+
19
+ function toggleSelection(newSelection: string): void {
20
+ selected = selected === newSelection ? null : newSelection;
21
+ handle_action(selected);
22
+ }
23
+ </script>
24
+
25
+ {#if feedback_options.includes("Like") || feedback_options.includes("Dislike")}
26
+ {#if feedback_options.includes("Dislike")}
27
+ <IconButton
28
+ Icon={selected === "Dislike" ? ThumbDownActive : ThumbDownDefault}
29
+ label={selected === "Dislike" ? "Disliked" : i18n("chatbot.dislike")}
30
+ color={selected === "Dislike"
31
+ ? "var(--color-accent)"
32
+ : "var(--block-label-text-color)"}
33
+ on:click={() => toggleSelection("Dislike")}
34
+ />
35
+ {/if}
36
+ {#if feedback_options.includes("Like")}
37
+ <IconButton
38
+ Icon={selected === "Like" ? ThumbUpActive : ThumbUpDefault}
39
+ label={selected === "Like" ? "Liked" : i18n("chatbot.like")}
40
+ color={selected === "Like"
41
+ ? "var(--color-accent)"
42
+ : "var(--block-label-text-color)"}
43
+ on:click={() => toggleSelection("Like")}
44
+ />
45
+ {/if}
46
+ {/if}
47
+
48
+ {#if extra_feedback.length > 0}
49
+ <div class="extra-feedback no-border">
50
+ <IconButton
51
+ Icon={selected && extra_feedback.includes(selected) ? FlagActive : Flag}
52
+ label="Feedback"
53
+ color={selected && extra_feedback.includes(selected)
54
+ ? "var(--color-accent)"
55
+ : "var(--block-label-text-color)"}
56
+ />
57
+ <div class="extra-feedback-options">
58
+ {#each extra_feedback as option}
59
+ <button
60
+ class="extra-feedback-option"
61
+ style:font-weight={selected === option ? "bold" : "normal"}
62
+ on:click={() => {
63
+ toggleSelection(option);
64
+ handle_action(selected ? selected : null);
65
+ }}>{option}</button
66
+ >
67
+ {/each}
68
+ </div>
69
+ </div>
70
+ {/if}
71
+
72
+ <style>
73
+ .extra-feedback {
74
+ display: flex;
75
+ align-items: center;
76
+ position: relative;
77
+ }
78
+ .extra-feedback-options {
79
+ display: none;
80
+ position: absolute;
81
+ padding: var(--spacing-md) 0;
82
+ flex-direction: column;
83
+ gap: var(--spacing-sm);
84
+ top: 100%;
85
+ }
86
+ .extra-feedback:hover .extra-feedback-options {
87
+ display: flex;
88
+ }
89
+ .extra-feedback-option {
90
+ border: 1px solid var(--border-color-primary);
91
+ border-radius: var(--radius-sm);
92
+ color: var(--block-label-text-color);
93
+ background-color: var(--block-background-fill);
94
+ font-size: var(--text-xs);
95
+ padding: var(--spacing-xxs) var(--spacing-sm);
96
+ width: max-content;
97
+ }
98
+ </style>
6.0.0/chatbot/shared/Message.svelte ADDED
@@ -0,0 +1,655 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { is_component_message } from "../shared/utils";
3
+ import { Image } from "@gradio/image/shared";
4
+ import type { FileData, Client } from "@gradio/client";
5
+ import type { NormalisedMessage } from "../types";
6
+
7
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
8
+ import type { ComponentType, SvelteComponent } from "svelte";
9
+ import ButtonPanel from "./ButtonPanel.svelte";
10
+ import MessageContent from "./MessageContent.svelte";
11
+ import Thought from "./Thought.svelte";
12
+
13
+ export let value: NormalisedMessage[];
14
+ export let avatar_img: FileData | null;
15
+ export let opposite_avatar_img: FileData | null = null;
16
+ export let role = "user";
17
+ export let messages: NormalisedMessage[] = [];
18
+ export let layout: "bubble" | "panel";
19
+ export let render_markdown: boolean;
20
+ export let latex_delimiters: {
21
+ left: string;
22
+ right: string;
23
+ display: boolean;
24
+ }[];
25
+ export let sanitize_html: boolean;
26
+ export let selectable: boolean;
27
+ export let _fetch: typeof fetch;
28
+ export let rtl: boolean;
29
+ export let dispatch: any;
30
+ export let i18n: I18nFormatter;
31
+ export let line_breaks: boolean;
32
+ export let upload: Client["upload"];
33
+ export let target: HTMLElement | null;
34
+ export let theme_mode: "light" | "dark" | "system";
35
+ export let _components: Record<string, ComponentType<SvelteComponent>>;
36
+ export let i: number;
37
+ export let show_copy_button: boolean;
38
+ export let generating: boolean;
39
+ export let feedback_options: string[];
40
+ export let show_like: boolean;
41
+ export let show_edit: boolean;
42
+ export let show_retry: boolean;
43
+ export let show_undo: boolean;
44
+ export let handle_action: (selected: string | null) => void;
45
+ export let scroll: () => void;
46
+ export let allow_file_downloads: boolean;
47
+ export let in_edit_mode: boolean;
48
+ export let edit_messages: string[];
49
+ export let display_consecutive_in_same_bubble: boolean;
50
+ export let current_feedback: string | null = null;
51
+ export let allow_tags: string[] | boolean = false;
52
+ export let watermark: string | null = null;
53
+ let messageElements: HTMLDivElement[] = [];
54
+ let previous_edit_mode = false;
55
+ let message_widths: number[] = Array(messages.length).fill(160);
56
+ let message_heights: number[] = Array(messages.length).fill(0);
57
+
58
+ $: if (in_edit_mode && !previous_edit_mode) {
59
+ const offset = messageElements.length - messages.length;
60
+ for (let idx = offset; idx < messageElements.length; idx++) {
61
+ if (idx >= 0) {
62
+ message_widths[idx - offset] = messageElements[idx]?.clientWidth;
63
+ message_heights[idx - offset] = messageElements[idx]?.clientHeight;
64
+ }
65
+ }
66
+ }
67
+
68
+ function handle_select(i: number, message: NormalisedMessage): void {
69
+ dispatch("select", {
70
+ index: message.index,
71
+ value: message.content
72
+ });
73
+ }
74
+
75
+ function get_message_label_data(message: NormalisedMessage): string {
76
+ if (message.type === "text") {
77
+ return message.content;
78
+ } else if (
79
+ message.type === "component" &&
80
+ message.content.component === "file"
81
+ ) {
82
+ if (Array.isArray(message.content.value)) {
83
+ return `file of extension type: ${message.content.value[0].orig_name?.split(".").pop()}`;
84
+ }
85
+ return (
86
+ `file of extension type: ${message.content.value?.orig_name?.split(".").pop()}` +
87
+ (message.content.value?.orig_name ?? "")
88
+ );
89
+ }
90
+ return `a component of type ${message.content.component ?? "unknown"}`;
91
+ }
92
+
93
+ function get_file(messages: NormalisedMessage[]): FileData | null {
94
+ for (const message of messages) {
95
+ if (
96
+ message.type === "component" &&
97
+ (message.content.component === "audio" ||
98
+ message.content.component === "video" ||
99
+ message.content.component === "image" ||
100
+ message.content.component === "file") &&
101
+ message.content.value
102
+ ) {
103
+ return message.content.value as FileData;
104
+ }
105
+ }
106
+ return null;
107
+ }
108
+
109
+ type ButtonPanelProps = {
110
+ handle_action: (selected: string | null) => void;
111
+ likeable: boolean;
112
+ feedback_options: string[];
113
+ show_retry: boolean;
114
+ show_undo: boolean;
115
+ show_edit: boolean;
116
+ in_edit_mode: boolean;
117
+ generating: boolean;
118
+ show_copy_button: boolean;
119
+ message: NormalisedMessage[] | NormalisedMessage;
120
+ position: "left" | "right";
121
+ layout: "bubble" | "panel";
122
+ avatar: FileData | null;
123
+ dispatch: any;
124
+ current_feedback: string | null;
125
+ watermark: string | null;
126
+ file: FileData | null;
127
+ show_download_button: boolean;
128
+ show_share_button: boolean;
129
+ };
130
+
131
+ let button_panel_props: ButtonPanelProps;
132
+ $: button_panel_props = {
133
+ handle_action,
134
+ likeable: show_like,
135
+ feedback_options,
136
+ show_retry,
137
+ show_undo,
138
+ show_edit,
139
+ in_edit_mode,
140
+ generating,
141
+ show_copy_button,
142
+ message: messages,
143
+ position: role === "user" ? "right" : "left",
144
+ avatar: avatar_img,
145
+ layout,
146
+ dispatch,
147
+ current_feedback,
148
+ watermark,
149
+ file: get_file(messages),
150
+ show_download_button: allow_file_downloads,
151
+ show_share_button: true
152
+ };
153
+ </script>
154
+
155
+ <div
156
+ class="message-row {layout} {role}-row"
157
+ class:with_avatar={avatar_img !== null}
158
+ class:with_opposite_avatar={opposite_avatar_img !== null}
159
+ >
160
+ {#if avatar_img !== null}
161
+ <div class="avatar-container">
162
+ <Image class="avatar-image" src={avatar_img?.url} alt="{role} avatar" />
163
+ </div>
164
+ {/if}
165
+ <div
166
+ class:role
167
+ class="flex-wrap"
168
+ class:component-wrap={messages[0].type === "component"}
169
+ >
170
+ <div
171
+ class:message={display_consecutive_in_same_bubble}
172
+ class={display_consecutive_in_same_bubble ? role : ""}
173
+ >
174
+ {#each messages as message, thought_index}
175
+ <div
176
+ class="message {!display_consecutive_in_same_bubble ? role : ''}"
177
+ class:panel-full-width={true}
178
+ class:message-markdown-disabled={!render_markdown}
179
+ class:component={message.type === "component"}
180
+ class:html={is_component_message(message) &&
181
+ message.content.component === "html"}
182
+ class:thought={thought_index > 0}
183
+ >
184
+ {#if in_edit_mode && message.type === "text"}
185
+ <!-- svelte-ignore a11y-autofocus -->
186
+ <textarea
187
+ class="edit-textarea"
188
+ style:width={`max(${message_widths[thought_index]}px, 160px)`}
189
+ style:min-height={`${message_heights[thought_index]}px`}
190
+ autofocus
191
+ bind:value={edit_messages[thought_index]}
192
+ />
193
+ {:else}
194
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
195
+ <div
196
+ data-testid={role}
197
+ class:latest={i === value.length - 1}
198
+ class:message-markdown-disabled={!render_markdown}
199
+ style:user-select="text"
200
+ class:selectable
201
+ style:cursor={selectable ? "pointer" : "auto"}
202
+ style:text-align={rtl ? "right" : "left"}
203
+ bind:this={messageElements[thought_index]}
204
+ on:click={() => handle_select(i, message)}
205
+ on:keydown={(e) => {
206
+ if (e.key === "Enter") {
207
+ handle_select(i, message);
208
+ }
209
+ }}
210
+ dir={rtl ? "rtl" : "ltr"}
211
+ aria-label={role +
212
+ "'s message: " +
213
+ get_message_label_data(message)}
214
+ >
215
+ {#if message?.metadata?.title}
216
+ <Thought
217
+ thought={message}
218
+ {rtl}
219
+ {sanitize_html}
220
+ {allow_tags}
221
+ {latex_delimiters}
222
+ {render_markdown}
223
+ {_components}
224
+ {upload}
225
+ {thought_index}
226
+ {target}
227
+ {theme_mode}
228
+ {_fetch}
229
+ {scroll}
230
+ {allow_file_downloads}
231
+ {display_consecutive_in_same_bubble}
232
+ {i18n}
233
+ {line_breaks}
234
+ />
235
+ {:else}
236
+ <MessageContent
237
+ {message}
238
+ {sanitize_html}
239
+ {allow_tags}
240
+ {latex_delimiters}
241
+ {render_markdown}
242
+ {_components}
243
+ {upload}
244
+ {thought_index}
245
+ {target}
246
+ {theme_mode}
247
+ {_fetch}
248
+ {scroll}
249
+ {allow_file_downloads}
250
+ {display_consecutive_in_same_bubble}
251
+ {i18n}
252
+ {line_breaks}
253
+ />
254
+ {/if}
255
+ </div>
256
+ {/if}
257
+ </div>
258
+
259
+ {#if layout === "panel"}
260
+ <ButtonPanel
261
+ {...button_panel_props}
262
+ {current_feedback}
263
+ {watermark}
264
+ on:copy={(e) => dispatch("copy", e.detail)}
265
+ {i18n}
266
+ />
267
+ {/if}
268
+ {/each}
269
+ </div>
270
+ </div>
271
+ </div>
272
+
273
+ {#if layout === "bubble"}
274
+ <ButtonPanel {...button_panel_props} {i18n} />
275
+ {/if}
276
+
277
+ <style>
278
+ .message {
279
+ position: relative;
280
+ width: 100%;
281
+ }
282
+
283
+ .message.display_consecutive_in_same_bubble {
284
+ margin-top: 0;
285
+ }
286
+
287
+ .message + .message {
288
+ margin-top: var(--spacing-sm);
289
+ }
290
+
291
+ /* avatar styles */
292
+ .avatar-container {
293
+ flex-shrink: 0;
294
+ border-radius: 50%;
295
+ border: 1px solid var(--border-color-primary);
296
+ overflow: hidden;
297
+ }
298
+
299
+ .avatar-container :global(img) {
300
+ object-fit: cover;
301
+ }
302
+
303
+ /* message wrapper */
304
+ .flex-wrap {
305
+ display: flex;
306
+ flex-direction: column;
307
+ width: calc(100% - var(--spacing-xxl));
308
+ max-width: 100%;
309
+ color: var(--body-text-color);
310
+ font-size: var(--chatbot-text-size);
311
+ overflow-wrap: break-word;
312
+ width: 100%;
313
+ height: 100%;
314
+ }
315
+
316
+ .component {
317
+ padding: 0;
318
+ border-radius: var(--radius-md);
319
+ width: fit-content;
320
+ overflow: hidden;
321
+ }
322
+
323
+ .component.gallery {
324
+ border: none;
325
+ }
326
+
327
+ .bot:has(.model3D),
328
+ .user:has(.model3D) {
329
+ border: none;
330
+ max-width: 75%;
331
+ }
332
+
333
+ .message-row :not(.avatar-container) :global(img) {
334
+ margin: var(--size-2);
335
+ max-height: 300px;
336
+ }
337
+
338
+ .file-pil {
339
+ display: block;
340
+ width: fit-content;
341
+ padding: var(--spacing-sm) var(--spacing-lg);
342
+ border-radius: var(--radius-md);
343
+ background: var(--background-fill-secondary);
344
+ color: var(--body-text-color);
345
+ text-decoration: none;
346
+ margin: 0;
347
+ font-family: var(--font-mono);
348
+ font-size: var(--text-sm);
349
+ }
350
+
351
+ .file {
352
+ width: auto !important;
353
+ max-width: fit-content !important;
354
+ }
355
+
356
+ @media (max-width: 600px) or (max-width: 480px) {
357
+ .component {
358
+ width: 100%;
359
+ }
360
+ }
361
+
362
+ .message :global(.prose) {
363
+ font-size: var(--chatbot-text-size);
364
+ }
365
+
366
+ .message-bubble-border {
367
+ border-width: 1px;
368
+ border-radius: var(--radius-md);
369
+ }
370
+
371
+ .panel-full-width {
372
+ width: 100%;
373
+ }
374
+ .message-markdown-disabled {
375
+ white-space: pre-line;
376
+ }
377
+
378
+ .user {
379
+ border-radius: var(--radius-md);
380
+ align-self: flex-end;
381
+ border-bottom-right-radius: 0;
382
+ box-shadow: var(--shadow-drop);
383
+ border: 1px solid var(--border-color-accent-subdued);
384
+ background-color: var(--color-accent-soft);
385
+ padding: var(--spacing-sm) var(--spacing-xl);
386
+ }
387
+
388
+ .bot {
389
+ border: 1px solid var(--border-color-primary);
390
+ border-radius: var(--radius-md);
391
+ border-color: var(--border-color-primary);
392
+ background-color: var(--background-fill-secondary);
393
+ box-shadow: var(--shadow-drop);
394
+ align-self: flex-start;
395
+ text-align: right;
396
+ border-bottom-left-radius: 0;
397
+ padding: var(--spacing-sm) var(--spacing-xl);
398
+ }
399
+
400
+ .bot:has(.table-wrap) {
401
+ border: none;
402
+ box-shadow: none;
403
+ background: none;
404
+ }
405
+
406
+ .panel .user :global(*) {
407
+ text-align: right;
408
+ }
409
+
410
+ /* Colors */
411
+
412
+ .message-row {
413
+ display: flex;
414
+ position: relative;
415
+ }
416
+
417
+ /* bubble mode styles */
418
+ .bubble {
419
+ margin: calc(var(--spacing-xl) * 2);
420
+ margin-bottom: var(--spacing-xl);
421
+ }
422
+
423
+ .bubble.user-row {
424
+ align-self: flex-end;
425
+ max-width: calc(100% - var(--spacing-xl) * 6);
426
+ }
427
+
428
+ .bubble.bot-row {
429
+ align-self: flex-start;
430
+ max-width: calc(100% - var(--spacing-xl) * 6);
431
+ }
432
+
433
+ .bubble .user-row {
434
+ flex-direction: row;
435
+ justify-content: flex-end;
436
+ }
437
+
438
+ .bubble .with_avatar.user-row {
439
+ margin-right: calc(var(--spacing-xl) * 2) !important;
440
+ }
441
+
442
+ .bubble .with_avatar.bot-row {
443
+ margin-left: calc(var(--spacing-xl) * 2) !important;
444
+ }
445
+
446
+ .bubble .with_opposite_avatar.user-row {
447
+ margin-left: calc(var(--spacing-xxl) + 35px + var(--spacing-xxl));
448
+ }
449
+
450
+ /* panel mode styles */
451
+ .panel {
452
+ margin: 0;
453
+ padding: calc(var(--spacing-lg) * 2) calc(var(--spacing-lg) * 2);
454
+ }
455
+
456
+ .panel.bot-row {
457
+ background: var(--background-fill-secondary);
458
+ }
459
+
460
+ .panel .with_avatar {
461
+ padding-left: calc(var(--spacing-xl) * 2) !important;
462
+ padding-right: calc(var(--spacing-xl) * 2) !important;
463
+ }
464
+
465
+ .panel .panel-full-width {
466
+ width: 100%;
467
+ }
468
+
469
+ .panel .user :global(*) {
470
+ text-align: right;
471
+ }
472
+
473
+ /* message content */
474
+ .flex-wrap {
475
+ display: flex;
476
+ flex-direction: column;
477
+ max-width: 100%;
478
+ color: var(--body-text-color);
479
+ font-size: var(--chatbot-text-size);
480
+ overflow-wrap: break-word;
481
+ }
482
+
483
+ @media (max-width: 480px) {
484
+ .user-row.bubble {
485
+ align-self: flex-end;
486
+ }
487
+
488
+ .bot-row.bubble {
489
+ align-self: flex-start;
490
+ }
491
+ .message {
492
+ width: 100%;
493
+ }
494
+ }
495
+
496
+ .avatar-container {
497
+ align-self: flex-start;
498
+ position: relative;
499
+ display: flex;
500
+ justify-content: flex-start;
501
+ align-items: flex-start;
502
+ width: 35px;
503
+ height: 35px;
504
+ flex-shrink: 0;
505
+ bottom: 0;
506
+ border-radius: 50%;
507
+ border: 1px solid var(--border-color-primary);
508
+ }
509
+ .user-row > .avatar-container {
510
+ order: 2;
511
+ }
512
+
513
+ .user-row.bubble > .avatar-container {
514
+ margin-left: var(--spacing-xxl);
515
+ }
516
+
517
+ .bot-row.bubble > .avatar-container {
518
+ margin-left: var(--spacing-xxl);
519
+ }
520
+
521
+ .panel.user-row > .avatar-container {
522
+ order: 0;
523
+ }
524
+
525
+ .bot-row.bubble > .avatar-container {
526
+ margin-right: var(--spacing-xxl);
527
+ margin-left: 0;
528
+ }
529
+
530
+ .avatar-container:not(.thumbnail-item) :global(img) {
531
+ width: 100%;
532
+ height: 100%;
533
+ object-fit: cover;
534
+ border-radius: 50%;
535
+ padding: var(--size-1-5);
536
+ }
537
+
538
+ .selectable {
539
+ cursor: pointer;
540
+ }
541
+
542
+ @keyframes dot-flashing {
543
+ 0% {
544
+ opacity: 0.8;
545
+ }
546
+ 50% {
547
+ opacity: 0.5;
548
+ }
549
+ 100% {
550
+ opacity: 0.8;
551
+ }
552
+ }
553
+
554
+ /* Image preview */
555
+ .message :global(.preview) {
556
+ object-fit: contain;
557
+ width: 95%;
558
+ max-height: 93%;
559
+ }
560
+ .image-preview {
561
+ position: absolute;
562
+ z-index: 999;
563
+ left: 0;
564
+ top: 0;
565
+ width: 100%;
566
+ height: 100%;
567
+ overflow: auto;
568
+ background-color: rgba(0, 0, 0, 0.9);
569
+ display: flex;
570
+ justify-content: center;
571
+ align-items: center;
572
+ }
573
+ .image-preview :global(svg) {
574
+ stroke: white;
575
+ }
576
+ .image-preview-close-button {
577
+ position: absolute;
578
+ top: 10px;
579
+ right: 10px;
580
+ background: none;
581
+ border: none;
582
+ font-size: 1.5em;
583
+ cursor: pointer;
584
+ height: 30px;
585
+ width: 30px;
586
+ padding: 3px;
587
+ background: var(--bg-color);
588
+ box-shadow: var(--shadow-drop);
589
+ border: 1px solid var(--button-secondary-border-color);
590
+ border-radius: var(--radius-lg);
591
+ }
592
+
593
+ .message > div {
594
+ width: 100%;
595
+ }
596
+ .html {
597
+ padding: 0;
598
+ border: none;
599
+ background: none;
600
+ }
601
+
602
+ .panel .bot,
603
+ .panel .user {
604
+ border: none;
605
+ box-shadow: none;
606
+ background-color: var(--background-fill-secondary);
607
+ }
608
+
609
+ textarea {
610
+ background: none;
611
+ border-radius: var(--radius-lg);
612
+ border: none;
613
+ display: block;
614
+ max-width: 100%;
615
+ }
616
+ .user textarea {
617
+ border-bottom-right-radius: 0;
618
+ }
619
+ .bot textarea {
620
+ border-bottom-left-radius: 0;
621
+ }
622
+ .user textarea:focus {
623
+ outline: 2px solid var(--border-color-accent);
624
+ }
625
+ .bot textarea:focus {
626
+ outline: 2px solid var(--border-color-primary);
627
+ }
628
+
629
+ .panel.user-row {
630
+ background-color: var(--color-accent-soft);
631
+ }
632
+
633
+ .panel .user-row,
634
+ .panel .bot-row {
635
+ align-self: flex-start;
636
+ }
637
+
638
+ .panel .user :global(*),
639
+ .panel .bot :global(*) {
640
+ text-align: left;
641
+ }
642
+
643
+ .panel .user {
644
+ background-color: var(--color-accent-soft);
645
+ }
646
+
647
+ .panel .user-row {
648
+ background-color: var(--color-accent-soft);
649
+ align-self: flex-start;
650
+ }
651
+
652
+ .panel .message {
653
+ margin-bottom: var(--spacing-md);
654
+ }
655
+ </style>
6.0.0/chatbot/shared/MessageContent.svelte ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { File } from "@gradio/icons";
3
+ import Component from "./Component.svelte";
4
+ import { MarkdownCode as Markdown } from "@gradio/markdown-code";
5
+ import type { NormalisedMessage } from "../types";
6
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
7
+ import type { Client } from "@gradio/client";
8
+ import type { ComponentType, SvelteComponent } from "svelte";
9
+
10
+ export let latex_delimiters: {
11
+ left: string;
12
+ right: string;
13
+ display: boolean;
14
+ }[];
15
+ export let sanitize_html: boolean;
16
+ export let _fetch: typeof fetch;
17
+ export let i18n: I18nFormatter;
18
+ export let line_breaks: boolean;
19
+ export let upload: Client["upload"];
20
+ export let target: HTMLElement | null;
21
+ export let theme_mode: "light" | "dark" | "system";
22
+ export let _components: Record<string, ComponentType<SvelteComponent>>;
23
+ export let render_markdown: boolean;
24
+ export let scroll: () => void;
25
+ export let allow_file_downloads: boolean;
26
+ export let display_consecutive_in_same_bubble: boolean;
27
+ export let thought_index: number;
28
+ export let allow_tags: string[] | boolean = false;
29
+
30
+ export let message: NormalisedMessage;
31
+ </script>
32
+
33
+ {#if message.type === "text"}
34
+ <div class="message-content">
35
+ <Markdown
36
+ message={message.content}
37
+ {latex_delimiters}
38
+ {sanitize_html}
39
+ {render_markdown}
40
+ {line_breaks}
41
+ on:load={scroll}
42
+ {allow_tags}
43
+ {theme_mode}
44
+ />
45
+ </div>
46
+ {:else if message.type === "component" && message.content.component in _components}
47
+ <Component
48
+ {target}
49
+ {theme_mode}
50
+ props={message.content.props}
51
+ type={message.content.component}
52
+ components={_components}
53
+ value={message.content.value}
54
+ display_icon_button_wrapper_top_corner={thought_index > 0 &&
55
+ display_consecutive_in_same_bubble}
56
+ {i18n}
57
+ {upload}
58
+ {_fetch}
59
+ on:load={() => scroll()}
60
+ {allow_file_downloads}
61
+ />
62
+ {:else if message.type === "component" && message.content.component === "file"}
63
+ <div class="file-container">
64
+ <div class="file-icon">
65
+ <File />
66
+ </div>
67
+ <div class="file-info">
68
+ <a
69
+ data-testid="chatbot-file"
70
+ class="file-link"
71
+ href={message.content.value.url}
72
+ target="_blank"
73
+ download={window.__is_colab__
74
+ ? null
75
+ : message.content.value?.orig_name ||
76
+ message.content.value?.path.split("/").pop() ||
77
+ "file"}
78
+ >
79
+ <span class="file-name"
80
+ >{message.content.value?.orig_name ||
81
+ message.content.value?.path.split("/").pop() ||
82
+ "file"}</span
83
+ >
84
+ </a>
85
+ <span class="file-type"
86
+ >{(
87
+ message.content.value?.orig_name ||
88
+ message.content.value?.path ||
89
+ ""
90
+ )
91
+ .split(".")
92
+ .pop()
93
+ .toUpperCase()}</span
94
+ >
95
+ </div>
96
+ </div>
97
+ {/if}
98
+
99
+ <style>
100
+ .file-container {
101
+ display: flex;
102
+ align-items: center;
103
+ gap: var(--spacing-lg);
104
+ padding: var(--spacing-lg);
105
+ border-radius: var(--radius-lg);
106
+ width: fit-content;
107
+ margin: var(--spacing-sm) 0;
108
+ }
109
+
110
+ .file-icon {
111
+ display: flex;
112
+ align-items: center;
113
+ justify-content: center;
114
+ color: var(--body-text-color);
115
+ }
116
+
117
+ .file-icon :global(svg) {
118
+ width: var(--size-7);
119
+ height: var(--size-7);
120
+ }
121
+
122
+ .file-info {
123
+ display: flex;
124
+ flex-direction: column;
125
+ }
126
+
127
+ .file-link {
128
+ text-decoration: none;
129
+ color: var(--body-text-color);
130
+ display: flex;
131
+ flex-direction: column;
132
+ gap: var(--spacing-xs);
133
+ }
134
+
135
+ .file-name {
136
+ font-family: var(--font);
137
+ font-size: var(--text-md);
138
+ font-weight: 500;
139
+ }
140
+
141
+ .file-type {
142
+ font-family: var(--font);
143
+ font-size: var(--text-sm);
144
+ color: var(--body-text-color-subdued);
145
+ text-transform: uppercase;
146
+ }
147
+ </style>
6.0.0/chatbot/shared/Pending.svelte ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Image } from "@gradio/image/shared";
3
+ import type { FileData } from "@gradio/client";
4
+
5
+ export let layout = "bubble";
6
+ export let avatar_images: [FileData | null, FileData | null] = [null, null];
7
+ </script>
8
+
9
+ <div class="container">
10
+ {#if avatar_images[1] !== null}
11
+ <div class="avatar-container">
12
+ <Image class="avatar-image" src={avatar_images[1].url} alt="bot avatar" />
13
+ </div>
14
+ {/if}
15
+
16
+ <div
17
+ class="message bot pending {layout}"
18
+ class:with_avatar={avatar_images[1] !== null}
19
+ class:with_opposite_avatar={avatar_images[0] !== null}
20
+ role="status"
21
+ aria-label="Loading response"
22
+ aria-live="polite"
23
+ >
24
+ <div class="message-content">
25
+ <span class="sr-only">Loading content</span>
26
+ <div class="dots">
27
+ <div class="dot" />
28
+ <div class="dot" />
29
+ <div class="dot" />
30
+ </div>
31
+ </div>
32
+ </div>
33
+ </div>
34
+
35
+ <style>
36
+ .container {
37
+ display: flex;
38
+ margin: calc(var(--spacing-xl) * 2);
39
+ }
40
+
41
+ .bubble.pending {
42
+ border-width: 1px;
43
+ border-radius: var(--radius-lg);
44
+ border-bottom-left-radius: 0;
45
+ border-color: var(--border-color-primary);
46
+ background-color: var(--background-fill-secondary);
47
+ box-shadow: var(--shadow-drop);
48
+ align-self: flex-start;
49
+ width: fit-content;
50
+ margin-bottom: var(--spacing-xl);
51
+ }
52
+
53
+ .bubble.with_opposite_avatar {
54
+ margin-right: calc(var(--spacing-xxl) + 35px + var(--spacing-xxl));
55
+ }
56
+
57
+ .panel.pending {
58
+ margin: 0;
59
+ padding: calc(var(--spacing-lg) * 2) calc(var(--spacing-lg) * 2);
60
+ width: 100%;
61
+ border: none;
62
+ background: none;
63
+ box-shadow: none;
64
+ border-radius: 0;
65
+ }
66
+
67
+ .panel.with_avatar {
68
+ padding-left: calc(var(--spacing-xl) * 2) !important;
69
+ padding-right: calc(var(--spacing-xl) * 2) !important;
70
+ }
71
+
72
+ .avatar-container {
73
+ align-self: flex-start;
74
+ position: relative;
75
+ display: flex;
76
+ justify-content: flex-start;
77
+ align-items: flex-start;
78
+ width: 35px;
79
+ height: 35px;
80
+ flex-shrink: 0;
81
+ bottom: 0;
82
+ border-radius: 50%;
83
+ border: 1px solid var(--border-color-primary);
84
+ margin-right: var(--spacing-xxl);
85
+ }
86
+
87
+ .avatar-container:not(.thumbnail-item) :global(img) {
88
+ width: 100%;
89
+ height: 100%;
90
+ object-fit: cover;
91
+ border-radius: 50%;
92
+ padding: var(--size-1-5);
93
+ }
94
+
95
+ .message-content {
96
+ padding: var(--spacing-sm) var(--spacing-xl);
97
+ min-height: var(--size-8);
98
+ display: flex;
99
+ align-items: center;
100
+ }
101
+
102
+ .dots {
103
+ display: flex;
104
+ gap: var(--spacing-xs);
105
+ align-items: center;
106
+ }
107
+
108
+ .dot {
109
+ width: var(--size-1-5);
110
+ height: var(--size-1-5);
111
+ margin-right: var(--spacing-xs);
112
+ border-radius: 50%;
113
+ background-color: var(--body-text-color);
114
+ opacity: 0.5;
115
+ animation: pulse 1.5s infinite;
116
+ }
117
+
118
+ .dot:nth-child(2) {
119
+ animation-delay: 0.2s;
120
+ }
121
+
122
+ .dot:nth-child(3) {
123
+ animation-delay: 0.4s;
124
+ }
125
+
126
+ @keyframes pulse {
127
+ 0%,
128
+ 100% {
129
+ opacity: 0.4;
130
+ transform: scale(1);
131
+ }
132
+ 50% {
133
+ opacity: 1;
134
+ transform: scale(1.1);
135
+ }
136
+ }
137
+ </style>
6.0.0/chatbot/shared/Remove.svelte ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ id="fi_3502539"
3
+ fill="currentColor"
4
+ height="100%"
5
+ viewBox="0 0 24 24"
6
+ width="100%"
7
+ xmlns="http://www.w3.org/2000/svg"
8
+ ><path
9
+ d="m7 16a1 1 0 0 0 .71-.3 1 1 0 0 0 0-1.41l-3.33-3.29h10.13a4.49 4.49 0 0 1 0 9h-2.51a1 1 0 0 0 0 2h2.51a6.49 6.49 0 0 0 0-13h-10.14l3.33-3.29a1 1 0 0 0 -1.4-1.42l-5.07 5a1 1 0 0 0 -.29.71 1 1 0 0 0 .3.71l5.06 5a1 1 0 0 0 .7.29z"
10
+ ></path></svg
11
+ >
6.0.0/chatbot/shared/Thought.svelte ADDED
@@ -0,0 +1,298 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { Client } from "@gradio/client";
3
+ import type { NormalisedMessage, ThoughtNode } from "../types";
4
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
5
+ import type { ComponentType, SvelteComponent } from "svelte";
6
+ import MessageContent from "./MessageContent.svelte";
7
+ import { DropdownCircularArrow } from "@gradio/icons";
8
+ import { IconButton } from "@gradio/atoms";
9
+ import { slide } from "svelte/transition";
10
+ import { MarkdownCode as Markdown } from "@gradio/markdown-code";
11
+
12
+ let {
13
+ thought,
14
+ rtl,
15
+ sanitize_html,
16
+ latex_delimiters,
17
+ render_markdown,
18
+ _components,
19
+ upload,
20
+ thought_index,
21
+ target,
22
+ theme_mode,
23
+ _fetch,
24
+ scroll,
25
+ allow_file_downloads,
26
+ display_consecutive_in_same_bubble,
27
+ i18n,
28
+ line_breaks,
29
+ allow_tags
30
+ }: {
31
+ thought: NormalisedMessage;
32
+ rtl: boolean;
33
+ sanitize_html: boolean;
34
+ latex_delimiters: { left: string; right: string; display: boolean }[];
35
+ render_markdown: boolean;
36
+ _components: Record<string, ComponentType<SvelteComponent>>;
37
+ upload: Client["upload"];
38
+ thought_index: number;
39
+ target: HTMLElement | null;
40
+ theme_mode: "light" | "dark" | "system";
41
+ _fetch: typeof fetch;
42
+ scroll: () => void;
43
+ allow_file_downloads: boolean;
44
+ display_consecutive_in_same_bubble: boolean;
45
+ i18n: I18nFormatter;
46
+ line_breaks: boolean;
47
+ allow_tags: string[] | boolean;
48
+ } = $props();
49
+
50
+ function is_thought_node(msg: NormalisedMessage): msg is ThoughtNode {
51
+ return "children" in msg;
52
+ }
53
+
54
+ let user_expanded_toggled = $state(false);
55
+ let content_preview_element: HTMLElement;
56
+ let user_is_scrolling = $state(false);
57
+ let thought_node: ThoughtNode = $derived.by(() => ({
58
+ ...thought,
59
+ children: is_thought_node(thought) ? thought.children : []
60
+ }));
61
+
62
+ let expanded = $state(false);
63
+
64
+ $effect(() => {
65
+ if (!user_expanded_toggled) {
66
+ expanded = thought_node?.metadata?.status !== "done";
67
+ }
68
+ });
69
+
70
+ function toggleExpanded(): void {
71
+ expanded = !expanded;
72
+ user_expanded_toggled = true;
73
+ }
74
+
75
+ function scrollToBottom(): void {
76
+ if (content_preview_element && !user_is_scrolling) {
77
+ content_preview_element.scrollTop = content_preview_element.scrollHeight;
78
+ }
79
+ }
80
+
81
+ function handleScroll(): void {
82
+ if (content_preview_element) {
83
+ const is_at_bottom =
84
+ content_preview_element.scrollHeight -
85
+ content_preview_element.scrollTop <=
86
+ content_preview_element.clientHeight + 10;
87
+ if (!is_at_bottom) {
88
+ user_is_scrolling = true;
89
+ }
90
+ }
91
+ }
92
+
93
+ $effect(() => {
94
+ if (thought_node.content && thought_node.metadata?.status !== "done")
95
+ setTimeout(scrollToBottom, 0);
96
+ });
97
+ </script>
98
+
99
+ <div class="thought-group">
100
+ <div
101
+ class="title"
102
+ class:expanded
103
+ on:click|stopPropagation={toggleExpanded}
104
+ aria-busy={thought_node.content === "" || thought_node.content === null}
105
+ role="button"
106
+ tabindex="0"
107
+ on:keydown={(e) => e.key === "Enter" && toggleExpanded()}
108
+ >
109
+ <span
110
+ class="arrow"
111
+ style:transform={expanded ? "rotate(180deg)" : "rotate(0deg)"}
112
+ >
113
+ <IconButton Icon={DropdownCircularArrow} />
114
+ </span>
115
+ <Markdown
116
+ message={thought_node.metadata?.title || ""}
117
+ {render_markdown}
118
+ {latex_delimiters}
119
+ {sanitize_html}
120
+ allow_tags={allow_tags || false}
121
+ />
122
+ {#if thought_node.metadata?.status === "pending"}
123
+ <span class="loading-spinner"></span>
124
+ {/if}
125
+ {#if thought_node?.metadata?.log || thought_node?.metadata?.duration}
126
+ <span class="duration">
127
+ {#if thought_node.metadata.log}
128
+ {thought_node.metadata.log}
129
+ {/if}
130
+ {#if thought_node.metadata.duration !== undefined}
131
+ ({#if Number.isInteger(thought_node.metadata.duration)}{thought_node
132
+ .metadata
133
+ .duration}s{:else if thought_node.metadata.duration >= 0.1}{thought_node.metadata.duration.toFixed(
134
+ 1
135
+ )}s{:else}{(thought_node.metadata.duration * 1000).toFixed(
136
+ 1
137
+ )}ms{/if})
138
+ {/if}
139
+ </span>
140
+ {/if}
141
+ </div>
142
+
143
+ {#if expanded}
144
+ <div
145
+ class:content={expanded}
146
+ class:content-preview={!expanded &&
147
+ thought_node.metadata?.status !== "done"}
148
+ bind:this={content_preview_element}
149
+ on:scroll={handleScroll}
150
+ transition:slide
151
+ >
152
+ <MessageContent
153
+ message={thought_node}
154
+ {sanitize_html}
155
+ {allow_tags}
156
+ {latex_delimiters}
157
+ {render_markdown}
158
+ {_components}
159
+ {upload}
160
+ {thought_index}
161
+ {target}
162
+ {theme_mode}
163
+ {_fetch}
164
+ {scroll}
165
+ {allow_file_downloads}
166
+ {display_consecutive_in_same_bubble}
167
+ {i18n}
168
+ {line_breaks}
169
+ />
170
+
171
+ {#if thought_node.children?.length > 0}
172
+ <div class="children">
173
+ {#each thought_node.children as child, index}
174
+ <svelte:self
175
+ thought={child}
176
+ rtc={rtl || false}
177
+ {sanitize_html}
178
+ {latex_delimiters}
179
+ {render_markdown}
180
+ {_components}
181
+ {upload}
182
+ thought_index={thought_index + 1}
183
+ {target}
184
+ {theme_mode}
185
+ {_fetch}
186
+ {scroll}
187
+ {allow_file_downloads}
188
+ {display_consecutive_in_same_bubble}
189
+ {i18n}
190
+ {line_breaks}
191
+ />
192
+ {/each}
193
+ </div>
194
+ {/if}
195
+ </div>
196
+ {/if}
197
+ </div>
198
+
199
+ <style>
200
+ .thought-group {
201
+ background: var(--background-fill-primary);
202
+ border: 1px solid var(--border-color-primary);
203
+ border-radius: var(--radius-sm);
204
+ padding: var(--spacing-md);
205
+ margin: var(--spacing-md) 0;
206
+ font-size: var(--text-sm);
207
+ }
208
+
209
+ .children :global(.thought-group) {
210
+ border: none;
211
+ margin: 0;
212
+ padding-bottom: 0;
213
+ }
214
+
215
+ .children {
216
+ padding-left: var(--spacing-md);
217
+ }
218
+
219
+ .title {
220
+ display: flex;
221
+ align-items: center;
222
+ color: var(--body-text-color);
223
+ cursor: pointer;
224
+ width: 100%;
225
+ }
226
+
227
+ .title :global(.md) {
228
+ font-size: var(--text-sm) !important;
229
+ }
230
+
231
+ .content,
232
+ .content-preview {
233
+ overflow-wrap: break-word;
234
+ word-break: break-word;
235
+ margin-left: var(--spacing-lg);
236
+ margin-bottom: var(--spacing-sm);
237
+ }
238
+
239
+ .content-preview {
240
+ position: relative;
241
+ max-height: calc(5 * 1.5em);
242
+ overflow-y: auto;
243
+ overscroll-behavior: contain;
244
+ cursor: default;
245
+ }
246
+
247
+ .content :global(*),
248
+ .content-preview :global(*) {
249
+ font-size: var(--text-sm);
250
+ color: var(--body-text-color);
251
+ }
252
+
253
+ .thought-group :global(.thought:not(.nested)) {
254
+ border: none;
255
+ background: none;
256
+ }
257
+
258
+ .duration {
259
+ color: var(--body-text-color-subdued);
260
+ font-size: var(--text-sm);
261
+ margin-left: var(--size-1);
262
+ }
263
+
264
+ .arrow {
265
+ opacity: 0.8;
266
+ width: var(--size-8);
267
+ height: var(--size-8);
268
+ display: flex;
269
+ align-items: center;
270
+ justify-content: center;
271
+ }
272
+
273
+ .arrow :global(button) {
274
+ background-color: transparent;
275
+ }
276
+
277
+ .loading-spinner {
278
+ display: inline-block;
279
+ width: 12px;
280
+ height: 12px;
281
+ border: 2px solid var(--body-text-color);
282
+ border-radius: 50%;
283
+ border-top-color: transparent;
284
+ animation: spin 1s linear infinite;
285
+ margin: 0 var(--size-1) -1px var(--size-2);
286
+ opacity: 0.8;
287
+ }
288
+
289
+ @keyframes spin {
290
+ to {
291
+ transform: rotate(360deg);
292
+ }
293
+ }
294
+
295
+ .thought-group :global(.message-content) {
296
+ opacity: 0.8;
297
+ }
298
+ </style>
6.0.0/chatbot/shared/ThumbDownActive.svelte ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 12 12"
5
+ fill="none"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ >
8
+ <path
9
+ d="M11.25 6.61523H9.375V1.36523H11.25V6.61523ZM3.375 1.36523H8.625V6.91636L7.48425 8.62748L7.16737 10.8464C7.14108 11.0248 7.05166 11.1879 6.91535 11.3061C6.77904 11.4242 6.60488 11.4896 6.4245 11.4902H6.375C6.07672 11.4899 5.79075 11.3713 5.57983 11.1604C5.36892 10.9495 5.2503 10.6635 5.25 10.3652V8.11523H2.25C1.85233 8.11474 1.47109 7.95654 1.18989 7.67535C0.908691 7.39415 0.750496 7.01291 0.75 6.61523V3.99023C0.750992 3.29435 1.02787 2.62724 1.51994 2.13517C2.01201 1.64311 2.67911 1.36623 3.375 1.36523Z"
10
+ fill="currentColor"
11
+ />
12
+ </svg>
6.0.0/chatbot/shared/ThumbDownDefault.svelte ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 12 12"
5
+ fill="none"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ >
8
+ <path
9
+ d="M2.25 8.11523H4.5V10.3652C4.5003 10.6635 4.61892 10.9495 4.82983 11.1604C5.04075 11.3713 5.32672 11.4899 5.625 11.4902H6.42488C6.60519 11.4895 6.77926 11.4241 6.91549 11.3059C7.05172 11.1878 7.14109 11.0248 7.16737 10.8464L7.48425 8.62748L8.82562 6.61523H11.25V1.36523H3.375C2.67911 1.36623 2.01201 1.64311 1.51994 2.13517C1.02787 2.62724 0.750992 3.29435 0.75 3.99023V6.61523C0.750496 7.01291 0.908691 7.39415 1.18989 7.67535C1.47109 7.95654 1.85233 8.11474 2.25 8.11523ZM9 2.11523H10.5V5.86523H9V2.11523ZM1.5 3.99023C1.5006 3.49314 1.69833 3.01657 2.04983 2.66507C2.40133 2.31356 2.8779 2.11583 3.375 2.11523H8.25V6.12661L6.76575 8.35298L6.4245 10.7402H5.625C5.52554 10.7402 5.43016 10.7007 5.35983 10.6304C5.28951 10.5601 5.25 10.4647 5.25 10.3652V7.36523H2.25C2.05118 7.36494 1.86059 7.28582 1.72 7.14524C1.57941 7.00465 1.5003 6.81406 1.5 6.61523V3.99023Z"
10
+ fill="currentColor"
11
+ />
12
+ </svg>
6.0.0/chatbot/shared/ThumbUpActive.svelte ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 12 12"
5
+ fill="none"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ >
8
+ <path
9
+ d="M0.75 6.24023H2.625V11.4902H0.75V6.24023ZM8.625 11.4902H3.375V5.93911L4.51575 4.22798L4.83263 2.00911C4.85892 1.83065 4.94834 1.66754 5.08465 1.5494C5.22096 1.43125 5.39512 1.36591 5.5755 1.36523H5.625C5.92328 1.36553 6.20925 1.48415 6.42017 1.69507C6.63108 1.90598 6.7497 2.19196 6.75 2.49023V4.74023H9.75C10.1477 4.74073 10.5289 4.89893 10.8101 5.18012C11.0913 5.46132 11.2495 5.84256 11.25 6.24023V8.86523C11.249 9.56112 10.9721 10.2282 10.4801 10.7203C9.98799 11.2124 9.32089 11.4892 8.625 11.4902Z"
10
+ fill="currentColor"
11
+ />
12
+ </svg>
6.0.0/chatbot/shared/ThumbUpDefault.svelte ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 12 12"
5
+ fill="none"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ >
8
+ <path
9
+ d="M9.75 4.74023H7.5V2.49023C7.4997 2.19196 7.38108 1.90598 7.17017 1.69507C6.95925 1.48415 6.67328 1.36553 6.375 1.36523H5.57512C5.39481 1.366 5.22074 1.43138 5.08451 1.54952C4.94828 1.66766 4.85891 1.83072 4.83262 2.00911L4.51575 4.22798L3.17438 6.24023H0.75V11.4902H8.625C9.32089 11.4892 9.98799 11.2124 10.4801 10.7203C10.9721 10.2282 11.249 9.56112 11.25 8.86523V6.24023C11.2495 5.84256 11.0913 5.46132 10.8101 5.18012C10.5289 4.89893 10.1477 4.74073 9.75 4.74023ZM3 10.7402H1.5V6.99023H3V10.7402ZM10.5 8.86523C10.4994 9.36233 10.3017 9.8389 9.95017 10.1904C9.59867 10.5419 9.1221 10.7396 8.625 10.7402H3.75V6.72886L5.23425 4.50248L5.5755 2.11523H6.375C6.47446 2.11523 6.56984 2.15474 6.64017 2.22507C6.71049 2.2954 6.75 2.39078 6.75 2.49023V5.49023H9.75C9.94882 5.49053 10.1394 5.56965 10.28 5.71023C10.4206 5.85082 10.4997 6.04141 10.5 6.24023V8.86523Z"
10
+ fill="currentColor"
11
+ />
12
+ </svg>
6.0.0/chatbot/shared/autorender.d.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ declare module "katex/dist/contrib/auto-render.js";
6.0.0/chatbot/shared/utils.ts ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { ComponentType, SvelteComponent } from "svelte";
3
+ import { uploadToHuggingFace } from "@gradio/utils";
4
+ import type {
5
+ ComponentMessage,
6
+ ComponentData,
7
+ TextMessage,
8
+ NormalisedMessage,
9
+ Message,
10
+ MessageRole,
11
+ ThoughtNode,
12
+ Text,
13
+ Component,
14
+ File
15
+ } from "../types";
16
+ import type { LoadedComponent } from "../../core/src/types";
17
+ import { Gradio } from "@gradio/utils";
18
+
19
+ export const format_chat_for_sharing = async (
20
+ chat: NormalisedMessage[],
21
+ url_length_limit = 1800
22
+ ): Promise<string> => {
23
+ let messages_to_share = [...chat];
24
+ let formatted = await format_messages(messages_to_share);
25
+
26
+ if (formatted.length > url_length_limit && messages_to_share.length > 2) {
27
+ const first_message = messages_to_share[0];
28
+ const last_message = messages_to_share[messages_to_share.length - 1];
29
+ messages_to_share = [first_message, last_message];
30
+ formatted = await format_messages(messages_to_share);
31
+ }
32
+
33
+ if (formatted.length > url_length_limit && messages_to_share.length > 0) {
34
+ const truncated_messages = messages_to_share.map((msg) => {
35
+ if (msg.type === "text") {
36
+ const max_length =
37
+ Math.floor(url_length_limit / messages_to_share.length) - 20;
38
+ if (msg.content.length > max_length) {
39
+ return {
40
+ ...msg,
41
+ content: msg.content.substring(0, max_length) + "..."
42
+ };
43
+ }
44
+ }
45
+ return msg;
46
+ });
47
+
48
+ messages_to_share = truncated_messages;
49
+ formatted = await format_messages(messages_to_share);
50
+ }
51
+
52
+ return formatted;
53
+ };
54
+
55
+ const format_messages = async (chat: NormalisedMessage[]): Promise<string> => {
56
+ let messages = await Promise.all(
57
+ chat.map(async (message) => {
58
+ if (message.role === "system") return "";
59
+ let speaker_emoji = message.role === "user" ? "😃" : "🤖";
60
+ let html_content = "";
61
+
62
+ if (message.type === "text") {
63
+ const regexPatterns = {
64
+ audio: /<audio.*?src="(\/file=.*?)"/g,
65
+ video: /<video.*?src="(\/file=.*?)"/g,
66
+ image: /<img.*?src="(\/file=.*?)".*?\/>|!\[.*?\]\((\/file=.*?)\)/g
67
+ };
68
+
69
+ html_content = message.content;
70
+
71
+ for (let [_, regex] of Object.entries(regexPatterns)) {
72
+ let match;
73
+
74
+ while ((match = regex.exec(message.content)) !== null) {
75
+ const fileUrl = match[1] || match[2];
76
+ const newUrl = await uploadToHuggingFace(fileUrl, "url");
77
+ html_content = html_content.replace(fileUrl, newUrl);
78
+ }
79
+ }
80
+ } else {
81
+ if (!message.content.value) return "";
82
+ const url =
83
+ message.content.component === "video"
84
+ ? message.content.value?.video.path
85
+ : message.content.value;
86
+ const file_url = await uploadToHuggingFace(url, "url");
87
+ if (message.content.component === "audio") {
88
+ html_content = `<audio controls src="${file_url}"></audio>`;
89
+ } else if (message.content.component === "video") {
90
+ html_content = file_url;
91
+ } else if (message.content.component === "image") {
92
+ html_content = `<img src="${file_url}" />`;
93
+ }
94
+ }
95
+
96
+ return `${speaker_emoji}: ${html_content}`;
97
+ })
98
+ );
99
+ return messages.filter((msg) => msg !== "").join("\n");
100
+ };
101
+
102
+ export interface UndoRetryData {
103
+ index: number | [number, number];
104
+ value: string | FileData | ComponentData;
105
+ }
106
+
107
+ export interface EditData {
108
+ index: number | [number, number];
109
+ value: string;
110
+ previous_value: string;
111
+ _dispatch_value: { type: "text"; text: string }[];
112
+ }
113
+
114
+ const redirect_src_url = (src: string, root: string): string =>
115
+ src.replace('src="/file', `src="${root}file`);
116
+
117
+ function get_component_for_mime_type(
118
+ mime_type: string | null | undefined,
119
+ file?: { path?: string }
120
+ ): string {
121
+ if (!mime_type) {
122
+ const path = file?.path;
123
+ if (path) {
124
+ const lower_path = path.toLowerCase();
125
+ if (
126
+ lower_path.endsWith(".glb") ||
127
+ lower_path.endsWith(".gltf") ||
128
+ lower_path.endsWith(".obj") ||
129
+ lower_path.endsWith(".stl") ||
130
+ lower_path.endsWith(".splat") ||
131
+ lower_path.endsWith(".ply")
132
+ ) {
133
+ return "model3d";
134
+ }
135
+ }
136
+ return "file";
137
+ }
138
+ if (mime_type.includes("audio")) return "audio";
139
+ if (mime_type.includes("video")) return "video";
140
+ if (mime_type.includes("image")) return "image";
141
+ if (mime_type.includes("model")) return "model3d";
142
+ return "file";
143
+ }
144
+
145
+ function convert_file_message_to_component_message(
146
+ message: File
147
+ ): ComponentData {
148
+ const _file = Array.isArray(message.file) ? message.file[0] : message.file;
149
+ return {
150
+ component: get_component_for_mime_type(_file?.mime_type, _file),
151
+ value: message.file,
152
+ alt_text: message.alt_text,
153
+ constructor_args: {},
154
+ props: {}
155
+ } as ComponentData;
156
+ }
157
+
158
+ function normalise_message(
159
+ message: Message,
160
+ content: Text | File | Component,
161
+ root: string,
162
+ i: number
163
+ ): NormalisedMessage {
164
+ let normalized: NormalisedMessage;
165
+ if (content.type === "text") {
166
+ normalized = {
167
+ role: message.role,
168
+ metadata: message.metadata,
169
+ content: redirect_src_url(content.text, root),
170
+ type: "text",
171
+ index: i,
172
+ options: message.options
173
+ };
174
+ } else if (content.type === "file") {
175
+ normalized = {
176
+ role: message.role,
177
+ metadata: message.metadata,
178
+ content: convert_file_message_to_component_message(content),
179
+ type: "component",
180
+ index: i,
181
+ options: message.options
182
+ };
183
+ } else {
184
+ normalized = {
185
+ role: message.role,
186
+ metadata: message.metadata,
187
+ content: content,
188
+ type: "component",
189
+ index: i,
190
+ options: message.options
191
+ };
192
+ }
193
+ return normalized;
194
+ }
195
+
196
+ export function normalise_messages(
197
+ messages: Message[] | null,
198
+ root: string
199
+ ): NormalisedMessage[] | null {
200
+ if (messages === null) return messages;
201
+
202
+ const thought_map = new Map<string, ThoughtNode>();
203
+
204
+ return messages
205
+ .flatMap((message, i) => {
206
+ const normalized: NormalisedMessage[] = message.content.map((content) =>
207
+ normalise_message(message, content, root, i)
208
+ );
209
+ for (const msg of normalized) {
210
+ const { id, title, parent_id } = message.metadata || {};
211
+ if (parent_id) {
212
+ const parent = thought_map.get(String(parent_id));
213
+ if (parent) {
214
+ const thought = { ...msg, children: [] } as ThoughtNode;
215
+ parent.children.push(thought);
216
+ if (id && title) {
217
+ thought_map.set(String(id), thought);
218
+ }
219
+ return null;
220
+ }
221
+ }
222
+ if (id && title) {
223
+ const thought = { ...msg, children: [] } as ThoughtNode;
224
+ thought_map.set(String(id), thought);
225
+ return thought;
226
+ }
227
+ }
228
+ return normalized;
229
+ })
230
+ .filter((msg): msg is NormalisedMessage => msg !== null);
231
+ }
232
+
233
+ export function is_component_message(
234
+ message: NormalisedMessage
235
+ ): message is ComponentMessage {
236
+ return message.type === "component";
237
+ }
238
+
239
+ export function is_last_bot_message(
240
+ messages: NormalisedMessage[],
241
+ all_messages: NormalisedMessage[]
242
+ ): boolean {
243
+ const is_bot = messages[messages.length - 1].role === "assistant";
244
+ const last_index = messages[messages.length - 1].index;
245
+ // use JSON.stringify to handle both the number and tuple cases
246
+ // when msg_format is tuples, last_index is an array and when it is messages, it is a number
247
+ const is_last =
248
+ JSON.stringify(last_index) ===
249
+ JSON.stringify(all_messages[all_messages.length - 1].index);
250
+ return is_last && is_bot;
251
+ }
252
+
253
+ export function group_messages(
254
+ messages: NormalisedMessage[],
255
+ display_consecutive_in_same_bubble = true
256
+ ): NormalisedMessage[][] {
257
+ const groupedMessages: NormalisedMessage[][] = [];
258
+ let currentGroup: NormalisedMessage[] = [];
259
+ let currentRole: MessageRole | null = null;
260
+
261
+ for (const message of messages) {
262
+ if (!(message.role === "assistant" || message.role === "user")) {
263
+ continue;
264
+ }
265
+
266
+ // If display_consecutive_in_same_bubble is false, each message should be its own group
267
+ if (!display_consecutive_in_same_bubble) {
268
+ groupedMessages.push([message]);
269
+ continue;
270
+ }
271
+
272
+ if (message.role === currentRole) {
273
+ currentGroup.push(message);
274
+ } else {
275
+ if (currentGroup.length > 0) {
276
+ groupedMessages.push(currentGroup);
277
+ }
278
+ currentGroup = [message];
279
+ currentRole = message.role;
280
+ }
281
+ }
282
+
283
+ if (currentGroup.length > 0) {
284
+ groupedMessages.push(currentGroup);
285
+ }
286
+
287
+ return groupedMessages;
288
+ }
289
+
290
+ export async function load_components(
291
+ component_names: string[],
292
+ _components: Record<string, ComponentType<SvelteComponent>>,
293
+ load_component: Gradio["load_component"]
294
+ ): Promise<Record<string, ComponentType<SvelteComponent>>> {
295
+ for (const component_name of component_names) {
296
+ if (_components[component_name] || component_name === "file") {
297
+ continue;
298
+ }
299
+ const variant = component_name === "dataframe" ? "component" : "base";
300
+ const comp = await load_component(component_name, variant);
301
+ // @ts-ignore
302
+ _components[component_name] = comp.default;
303
+ }
304
+ return _components;
305
+ }
306
+
307
+ export function get_components_from_messages(
308
+ messages: NormalisedMessage[] | null
309
+ ): string[] {
310
+ if (!messages) return [];
311
+ let components: Set<string> = new Set();
312
+ messages.forEach((message) => {
313
+ if (message.type === "component") {
314
+ components.add(message.content.component);
315
+ }
316
+ });
317
+ return Array.from(components);
318
+ }
319
+
320
+ export function get_thought_content(msg: NormalisedMessage, depth = 0): string {
321
+ let content = "";
322
+ const indent = " ".repeat(depth);
323
+
324
+ if (msg.metadata?.title) {
325
+ content += `${indent}${depth > 0 ? "- " : ""}${msg.metadata.title}\n`;
326
+ }
327
+ if (typeof msg.content === "string") {
328
+ content += `${indent} ${msg.content}\n`;
329
+ }
330
+ const thought = msg as ThoughtNode;
331
+ if (thought.children?.length > 0) {
332
+ content += thought.children
333
+ .map((child) => get_thought_content(child, depth + 1))
334
+ .join("");
335
+ }
336
+ return content;
337
+ }
338
+
339
+ export function all_text(message: TextMessage[] | TextMessage): string {
340
+ if (Array.isArray(message)) {
341
+ return message
342
+ .map((m) => {
343
+ if (m.metadata?.title) {
344
+ return get_thought_content(m);
345
+ }
346
+ return m.content;
347
+ })
348
+ .join("\n");
349
+ }
350
+ if (message.metadata?.title) {
351
+ return get_thought_content(message);
352
+ }
353
+ return message.content;
354
+ }
355
+
356
+ export function is_all_text(
357
+ message: NormalisedMessage[] | NormalisedMessage
358
+ ): message is TextMessage[] | TextMessage {
359
+ return (
360
+ (Array.isArray(message) &&
361
+ message.every((m) => typeof m.content === "string")) ||
362
+ (!Array.isArray(message) && typeof message.content === "string")
363
+ );
364
+ }
6.0.0/chatbot/types.ts ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { UndoRetryData } from "./shared/utils";
3
+ import type { Gradio, SelectData, LikeData, CopyData } from "@gradio/utils";
4
+ import type { LoadingStatus } from "@gradio/statustracker";
5
+
6
+ export type MessageRole = "system" | "user" | "assistant";
7
+
8
+ export interface Metadata {
9
+ title: string | null;
10
+ id?: number | string | null;
11
+ parent_id?: number | string | null;
12
+ duration?: number;
13
+ log?: string;
14
+ status?: "pending" | "done" | null;
15
+ }
16
+
17
+ export interface ComponentData {
18
+ component: string;
19
+ constructor_args: any;
20
+ props: any;
21
+ value: any;
22
+ alt_text: string | null;
23
+ }
24
+
25
+ export interface Option {
26
+ label?: string;
27
+ value: string;
28
+ }
29
+
30
+ export interface Text {
31
+ type: "text";
32
+ text: string;
33
+ }
34
+
35
+ export interface Component {
36
+ type: "component";
37
+ component: string;
38
+ constructor_args: object;
39
+ props: object;
40
+ value: any;
41
+ alt_text: string | null;
42
+ }
43
+
44
+ export interface File {
45
+ type: "file";
46
+ file: FileData;
47
+ alt_text: string | null;
48
+ }
49
+
50
+ export interface Message {
51
+ role: MessageRole;
52
+ metadata: Metadata;
53
+ content: (Text | File | Component)[];
54
+ index: number | [number, number];
55
+ options?: Option[];
56
+ }
57
+
58
+ export interface TextMessage {
59
+ type: "text";
60
+ content: string;
61
+ index: number | [number, number];
62
+ options?: Option[];
63
+ role: MessageRole;
64
+ metadata: Metadata;
65
+ }
66
+
67
+ export interface ComponentMessage {
68
+ type: "component";
69
+ content: ComponentData;
70
+ index: number | [number, number];
71
+ options?: Option[];
72
+ role: MessageRole;
73
+ metadata: Metadata;
74
+ }
75
+
76
+ export interface ExampleMessage {
77
+ icon?: FileData;
78
+ display_text?: string;
79
+ text: string;
80
+ files?: FileData[];
81
+ }
82
+
83
+ export type message_data =
84
+ | string
85
+ | { file: FileData | FileData[]; alt_text: string | null }
86
+ | { component: string; value: any; constructor_args: any; props: any }
87
+ | null;
88
+
89
+ export type NormalisedMessage = TextMessage | ComponentMessage;
90
+
91
+ export type ThoughtNode = NormalisedMessage & { children: ThoughtNode[] };
92
+
93
+ export interface ChatbotEvents {
94
+ change: Message[];
95
+ select: SelectData;
96
+ share: ShareData;
97
+ error: string;
98
+ like: LikeData;
99
+ clear_status: LoadingStatus;
100
+ example_select: SelectData;
101
+ option_select: SelectData;
102
+ edit: SelectData;
103
+ retry: UndoRetryData;
104
+ undo: UndoRetryData;
105
+ clear: null;
106
+ copy: CopyData;
107
+ }
108
+
109
+ export interface ChatbotProps {
110
+ autoscroll: boolean;
111
+ messages: Message[];
112
+ examples: ExampleMessage[] | null;
113
+ allow_undo: boolean;
114
+ allow_retry: boolean;
115
+ root: string;
116
+ proxy_url: null | string;
117
+ max_messages: number | null;
118
+ avatar_images: [FileData | null, FileData | null];
119
+ like_user_message: boolean;
120
+ height: number | string | undefined;
121
+ resizable: boolean;
122
+ min_height: number | string | undefined;
123
+ max_height: number | string | undefined;
124
+ editable: "user" | "all" | null;
125
+ placeholder: string | null;
126
+ allow_file_downloads: boolean;
127
+ watermark: string | null;
128
+ value: Message[];
129
+ _selectable: boolean;
130
+ likeable: boolean;
131
+ feedback_options: string[];
132
+ feedback_value: (string | null)[] | null;
133
+ buttons: string[] | null;
134
+ rtl: boolean;
135
+ sanitize_html: boolean;
136
+ layout: "bubble" | "panel";
137
+ type: "tuples" | "messages";
138
+ render_markdown: boolean;
139
+ line_breaks: boolean;
140
+ group_consecutive_messages: boolean;
141
+ allow_tags: string[] | boolean;
142
+ latex_delimiters: {
143
+ left: string;
144
+ right: string;
145
+ display: boolean;
146
+ }[];
147
+ _retryable: boolean;
148
+ _undoable: boolean;
149
+ }