gradio-pr-bot commited on
Commit
c545f75
·
verified ·
1 Parent(s): 2b6fd8e

Upload folder using huggingface_hub

Browse files
6.20.0/chatbot/Index.svelte ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 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
+ rtl={gradio.props.rtl}
48
+ >
49
+ {#if gradio.shared.loading_status}
50
+ <StatusTracker
51
+ autoscroll={gradio.shared.autoscroll}
52
+ i18n={gradio.i18n}
53
+ {...gradio.shared.loading_status}
54
+ {show_progress}
55
+ on_clear_status={() =>
56
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
57
+ />
58
+ {/if}
59
+ <div class="wrapper">
60
+ {#if gradio.shared.show_label}
61
+ <BlockLabel
62
+ show_label={gradio.shared.show_label}
63
+ Icon={Chat}
64
+ float={true}
65
+ label={gradio.shared.label || "Chatbot"}
66
+ />
67
+ {/if}
68
+ <ChatBot
69
+ i18n={gradio.i18n}
70
+ selectable={gradio.props._selectable}
71
+ likeable={gradio.props.likeable}
72
+ feedback_options={gradio.props.feedback_options}
73
+ feedback_value={gradio.props.feedback_value}
74
+ show_share_button={(gradio.props.buttons ?? ["share"]).some(
75
+ (btn) => typeof btn === "string" && btn === "share"
76
+ )}
77
+ show_copy_all_button={(gradio.props.buttons ?? ["copy_all"]).some(
78
+ (btn) => typeof btn === "string" && btn === "copy_all"
79
+ )}
80
+ value={_value}
81
+ latex_delimiters={gradio.props.latex_delimiters}
82
+ display_consecutive_in_same_bubble={gradio.props
83
+ .group_consecutive_messages}
84
+ render_markdown={gradio.props.render_markdown}
85
+ theme_mode={gradio.shared.theme_mode}
86
+ editable={gradio.props.editable}
87
+ pending_message={gradio.shared.loading_status?.status === "pending"}
88
+ generating={gradio.shared.loading_status?.status === "generating"}
89
+ rtl={gradio.props.rtl}
90
+ show_copy_button={(gradio.props.buttons ?? ["copy"]).some(
91
+ (btn) => typeof btn === "string" && btn === "copy"
92
+ )}
93
+ buttons={gradio.props.buttons}
94
+ on_custom_button_click={(id) => {
95
+ gradio.dispatch("custom_button_click", { id });
96
+ }}
97
+ like_user_message={gradio.props.like_user_message}
98
+ show_progress={gradio.shared.loading_status?.show_progress || "full"}
99
+ onchange={() => {
100
+ gradio.props.value = gradio.props.value;
101
+ gradio.dispatch("change", gradio.props.value);
102
+ }}
103
+ onselect={(data) => gradio.dispatch("select", data)}
104
+ onlike={(data) => gradio.dispatch("like", data)}
105
+ onshare={(data) => gradio.dispatch("share", data)}
106
+ onerror={(message) => gradio.dispatch("error", message)}
107
+ onexampleselect={(data) => gradio.dispatch("example_select", data)}
108
+ onoptionselect={(data) => gradio.dispatch("option_select", data)}
109
+ onretry={(data) => gradio.dispatch("retry", data)}
110
+ onundo={(data) => gradio.dispatch("undo", data)}
111
+ onclear={() => {
112
+ gradio.props.value = [];
113
+ gradio.dispatch("clear");
114
+ }}
115
+ oncopy={(data) => gradio.dispatch("copy", data)}
116
+ onedit={(data) => {
117
+ if (gradio.props.value === null || gradio.props.value.length === 0)
118
+ return;
119
+ //@ts-ignore
120
+ gradio.props.value[data.index].content = [
121
+ { text: data.value, type: "text" }
122
+ ];
123
+ gradio.dispatch("edit", data);
124
+ }}
125
+ avatar_images={gradio.props.avatar_images}
126
+ sanitize_html={gradio.props.sanitize_html}
127
+ line_breaks={gradio.props.line_breaks}
128
+ autoscroll={gradio.props.autoscroll}
129
+ layout={gradio.props.layout}
130
+ placeholder={gradio.props.placeholder}
131
+ examples={gradio.props.examples}
132
+ _retryable={gradio.props._retryable}
133
+ _undoable={gradio.props._undoable}
134
+ upload={(...args) => gradio.shared.client.upload(...args)}
135
+ _fetch={(...args) => gradio.shared.client.fetch(...args)}
136
+ load_component={gradio.shared.load_component}
137
+ allow_file_downloads={gradio.props.allow_file_downloads}
138
+ allow_tags={gradio.props.allow_tags}
139
+ watermark={gradio.props.watermark}
140
+ />
141
+ </div>
142
+ </Block>
143
+
144
+ <style>
145
+ .wrapper {
146
+ display: flex;
147
+ position: relative;
148
+ flex-direction: column;
149
+ align-items: start;
150
+ width: 100%;
151
+ height: 100%;
152
+ flex-grow: 1;
153
+ }
154
+
155
+ :global(.progress-text) {
156
+ right: auto;
157
+ }
158
+ </style>
6.20.0/chatbot/package.json ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/chatbot",
3
+ "version": "0.31.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.48.0"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/gradio-app/gradio.git",
46
+ "directory": "js/chatbot"
47
+ }
48
+ }
6.20.0/chatbot/shared/ButtonPanel.svelte ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import type { CopyData } from "@gradio/utils";
17
+
18
+ let {
19
+ i18n,
20
+ likeable,
21
+ feedback_options,
22
+ show_retry,
23
+ show_undo,
24
+ show_edit,
25
+ in_edit_mode,
26
+ show_copy_button,
27
+ watermark = null,
28
+ message,
29
+ position,
30
+ avatar,
31
+ generating,
32
+ current_feedback,
33
+ file = null,
34
+ show_download_button = false,
35
+ show_share_button = false,
36
+ handle_action,
37
+ layout,
38
+ oncopy,
39
+ onerror,
40
+ onshare
41
+ }: {
42
+ i18n: I18nFormatter;
43
+ likeable: boolean;
44
+ feedback_options: string[];
45
+ show_retry: boolean;
46
+ show_undo: boolean;
47
+ show_edit: boolean;
48
+ in_edit_mode: boolean;
49
+ show_copy_button: boolean;
50
+ watermark?: string | null;
51
+ message: NormalisedMessage | NormalisedMessage[];
52
+ position: "right" | "left";
53
+ avatar: FileData | null;
54
+ generating: boolean;
55
+ current_feedback: string | null;
56
+ file?: FileData | null;
57
+ show_download_button?: boolean;
58
+ show_share_button?: boolean;
59
+ handle_action: (selected: string | null) => void;
60
+ layout: "bubble" | "panel";
61
+ oncopy?: (data: CopyData) => void;
62
+ onerror?: (message: string) => void;
63
+ onshare?: (data: any) => void;
64
+ } = $props();
65
+
66
+ let message_text = $derived(is_all_text(message) ? all_text(message) : "");
67
+ let show_copy = $derived(show_copy_button && message && is_all_text(message));
68
+ </script>
69
+
70
+ {#if show_copy || show_retry || show_undo || show_edit || likeable || (show_download_button && file?.url) || (show_share_button && file)}
71
+ <div
72
+ class="message-buttons-{position} {layout} message-buttons {avatar !==
73
+ null && 'with-avatar'}"
74
+ >
75
+ <IconButtonWrapper top_panel={false}>
76
+ {#if in_edit_mode}
77
+ <IconButton
78
+ label={i18n("chatbot.submit")}
79
+ Icon={Check}
80
+ onclick={() => handle_action("edit_submit")}
81
+ disabled={generating}
82
+ />
83
+ <IconButton
84
+ label={i18n("chatbot.cancel")}
85
+ Icon={Clear}
86
+ onclick={() => handle_action("edit_cancel")}
87
+ disabled={generating}
88
+ />
89
+ {:else}
90
+ {#if show_copy}
91
+ <Copy value={message_text} {oncopy} {watermark} {i18n} />
92
+ {/if}
93
+ {#if show_download_button && file?.url}
94
+ <DownloadLink
95
+ href={file.is_stream
96
+ ? file.url?.replace("playlist.m3u8", "playlist-file")
97
+ : file.url}
98
+ download={file.orig_name || file.path || "file"}
99
+ >
100
+ <IconButton Icon={Download} label={i18n("common.download")} />
101
+ </DownloadLink>
102
+ {/if}
103
+ {#if show_share_button && file}
104
+ <ShareButton
105
+ {i18n}
106
+ on:error={(e) => onerror?.(e.detail)}
107
+ on:share={(e) => onshare?.(e.detail)}
108
+ formatter={async (value) => {
109
+ if (!value) return "";
110
+ let url = await uploadToHuggingFace(value.url, "url");
111
+ const mime_type = value.mime_type || "";
112
+ if (mime_type.startsWith("audio/")) {
113
+ return `<audio controls src="${url}"></audio>`;
114
+ } else if (mime_type.startsWith("video/")) {
115
+ return `<video controls src="${url}"></video>`;
116
+ } else if (mime_type.startsWith("image/")) {
117
+ return `<img src="${url}" />`;
118
+ }
119
+ return "";
120
+ }}
121
+ value={file}
122
+ />
123
+ {/if}
124
+ {#if show_retry}
125
+ <IconButton
126
+ Icon={Retry}
127
+ label={i18n("chatbot.retry")}
128
+ onclick={() => handle_action("retry")}
129
+ disabled={generating}
130
+ />
131
+ {/if}
132
+ {#if show_undo}
133
+ <IconButton
134
+ label={i18n("chatbot.undo")}
135
+ Icon={Undo}
136
+ onclick={() => handle_action("undo")}
137
+ disabled={generating}
138
+ />
139
+ {/if}
140
+ {#if show_edit}
141
+ <IconButton
142
+ label={i18n("chatbot.edit")}
143
+ Icon={Edit}
144
+ onclick={() => handle_action("edit")}
145
+ disabled={generating}
146
+ />
147
+ {/if}
148
+ {#if likeable}
149
+ <LikeDislike
150
+ {handle_action}
151
+ {feedback_options}
152
+ selected={current_feedback}
153
+ {i18n}
154
+ />
155
+ {/if}
156
+ {/if}
157
+ </IconButtonWrapper>
158
+ </div>
159
+ {/if}
160
+
161
+ <style>
162
+ .bubble :global(.icon-button-wrapper) {
163
+ margin: 0px calc(var(--spacing-xl) * 2);
164
+ }
165
+
166
+ .message-buttons {
167
+ z-index: var(--layer-1);
168
+ }
169
+ .message-buttons-left {
170
+ align-self: flex-start;
171
+ }
172
+
173
+ .bubble.message-buttons-right {
174
+ align-self: flex-end;
175
+ }
176
+
177
+ .message-buttons-right :global(.icon-button-wrapper) {
178
+ margin-left: auto;
179
+ }
180
+
181
+ .bubble.with-avatar {
182
+ margin-left: calc(var(--spacing-xl) * 5);
183
+ margin-right: calc(var(--spacing-xl) * 5);
184
+ }
185
+
186
+ .panel {
187
+ display: flex;
188
+ align-self: flex-start;
189
+ z-index: var(--layer-1);
190
+ }
191
+ </style>
6.20.0/chatbot/shared/ChatBot.svelte ADDED
@@ -0,0 +1,592 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ type SvelteComponent,
19
+ type ComponentType,
20
+ tick,
21
+ untrack
22
+ } from "svelte";
23
+
24
+ import { Trash, Community, ScrollDownArrow } from "@gradio/icons";
25
+ import { IconButtonWrapper, IconButton } from "@gradio/atoms";
26
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
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
+ import CopyAll from "./CopyAll.svelte";
37
+
38
+ let {
39
+ value = null,
40
+ _fetch,
41
+ load_component,
42
+ allow_file_downloads,
43
+ display_consecutive_in_same_bubble,
44
+ latex_delimiters,
45
+ pending_message = false,
46
+ generating = false,
47
+ selectable = false,
48
+ likeable = false,
49
+ feedback_options,
50
+ feedback_value = null,
51
+ editable = null,
52
+ show_share_button = false,
53
+ show_copy_all_button = false,
54
+ rtl = false,
55
+ show_copy_button = false,
56
+ buttons = null,
57
+ on_custom_button_click = null,
58
+ avatar_images = [null, null] as [FileData | null, FileData | null],
59
+ sanitize_html = true,
60
+ render_markdown = true,
61
+ line_breaks = true,
62
+ autoscroll = true,
63
+ theme_mode,
64
+ i18n,
65
+ layout = "bubble",
66
+ placeholder = null,
67
+ upload,
68
+ examples = null,
69
+ _retryable = false,
70
+ _undoable = false,
71
+ like_user_message = false,
72
+ allow_tags = false,
73
+ watermark = null,
74
+ show_progress = "full",
75
+ onchange,
76
+ onselect,
77
+ onlike,
78
+ onedit,
79
+ onundo,
80
+ onretry,
81
+ onclear,
82
+ onshare,
83
+ onerror,
84
+ onexampleselect,
85
+ onoptionselect,
86
+ oncopy
87
+ }: {
88
+ value?: NormalisedMessage[] | null;
89
+ _fetch: typeof fetch;
90
+ load_component: Gradio["load_component"];
91
+ allow_file_downloads: boolean;
92
+ display_consecutive_in_same_bubble: boolean;
93
+ latex_delimiters: {
94
+ left: string;
95
+ right: string;
96
+ display: boolean;
97
+ }[];
98
+ pending_message?: boolean;
99
+ generating?: boolean;
100
+ selectable?: boolean;
101
+ likeable?: boolean;
102
+ feedback_options: string[];
103
+ feedback_value?: (string | null)[] | null;
104
+ editable?: "user" | "all" | null;
105
+ show_share_button?: boolean;
106
+ show_copy_all_button?: boolean;
107
+ rtl?: boolean;
108
+ show_copy_button?: boolean;
109
+ buttons?: (string | CustomButtonType)[] | null;
110
+ on_custom_button_click?: ((id: number) => void) | null;
111
+ avatar_images?: [FileData | null, FileData | null];
112
+ sanitize_html?: boolean;
113
+ render_markdown?: boolean;
114
+ line_breaks?: boolean;
115
+ autoscroll?: boolean;
116
+ theme_mode: "system" | "light" | "dark";
117
+ i18n: I18nFormatter;
118
+ layout?: "bubble" | "panel";
119
+ placeholder?: string | null;
120
+ upload: Client["upload"];
121
+ examples?: ExampleMessage[] | null;
122
+ _retryable?: boolean;
123
+ _undoable?: boolean;
124
+ like_user_message?: boolean;
125
+ allow_tags?: string[] | boolean;
126
+ watermark?: string | null;
127
+ show_progress?: "full" | "minimal" | "hidden";
128
+ onchange?: () => void;
129
+ onselect?: (data: SelectData) => void;
130
+ onlike?: (data: LikeData) => void;
131
+ onedit?: (data: EditData) => void;
132
+ onundo?: (data: UndoRetryData) => void;
133
+ onretry?: (data: UndoRetryData) => void;
134
+ onclear?: () => void;
135
+ onshare?: (data: any) => void;
136
+ onerror?: (message: string) => void;
137
+ onexampleselect?: (data: SelectData) => void;
138
+ onoptionselect?: (data: SelectData) => void;
139
+ oncopy?: (data: CopyData) => void;
140
+ } = $props();
141
+
142
+ let old_value: NormalisedMessage[] | null = $state(null);
143
+ let _components: Record<string, ComponentType<SvelteComponent>> = $state({});
144
+ let component_names = $derived(
145
+ get_components_from_messages(value).sort().join(",")
146
+ );
147
+
148
+ async function update_components(component_names: string): Promise<void> {
149
+ _components = await load_components(
150
+ component_names ? component_names.split(",") : [],
151
+ _components,
152
+ load_component
153
+ );
154
+ }
155
+
156
+ $effect(() => {
157
+ const names = component_names;
158
+ untrack(() => update_components(names));
159
+ });
160
+
161
+ const is_browser = typeof window !== "undefined";
162
+
163
+ let target: HTMLElement | null = $state(null);
164
+ let edit_index: number | null = $state(null);
165
+ let edit_messages: string[] = $state([]);
166
+
167
+ $effect(() => {
168
+ target = document.querySelector("div.gradio-container");
169
+ });
170
+
171
+ let div: HTMLDivElement;
172
+ let show_scroll_button = $state(false);
173
+
174
+ function is_at_bottom(): boolean {
175
+ return div && div.offsetHeight + div.scrollTop > div.scrollHeight - 100;
176
+ }
177
+
178
+ function scroll_to_bottom(): void {
179
+ if (!div) return;
180
+ div.scrollTo(0, div.scrollHeight);
181
+ show_scroll_button = false;
182
+ }
183
+
184
+ let scroll_after_component_load = $state(false);
185
+ let has_scrolled_on_mount = false;
186
+
187
+ function scroll_to_bottom_after_component_load(): void {
188
+ if (scroll_after_component_load) {
189
+ scroll_to_bottom();
190
+ }
191
+ }
192
+
193
+ async function scroll_on_value_update(): Promise<void> {
194
+ if (!autoscroll) return;
195
+ if (is_at_bottom()) {
196
+ scroll_after_component_load = true;
197
+ await tick();
198
+ await new Promise((resolve) => setTimeout(resolve, 300));
199
+ scroll_to_bottom();
200
+ }
201
+ }
202
+
203
+ $effect(() => {
204
+ value;
205
+ pending_message;
206
+ _components;
207
+
208
+ if (!has_scrolled_on_mount) {
209
+ has_scrolled_on_mount = true;
210
+ if (autoscroll && div) {
211
+ scroll_to_bottom();
212
+ }
213
+ }
214
+
215
+ scroll_on_value_update();
216
+ });
217
+
218
+ $effect(() => {
219
+ if (!div) return;
220
+ function handle_scroll(): void {
221
+ if (is_at_bottom()) {
222
+ show_scroll_button = false;
223
+ } else {
224
+ scroll_after_component_load = false;
225
+ show_scroll_button = true;
226
+ }
227
+ }
228
+
229
+ div.addEventListener("scroll", handle_scroll);
230
+ return () => {
231
+ div.removeEventListener("scroll", handle_scroll);
232
+ };
233
+ });
234
+
235
+ $effect(() => {
236
+ if (!dequal(value, old_value)) {
237
+ old_value = value;
238
+ onchange?.();
239
+ }
240
+ });
241
+
242
+ let groupedMessages = $derived(
243
+ value ? group_messages(value, display_consecutive_in_same_bubble) : null
244
+ );
245
+
246
+ let options = $derived.by(() => {
247
+ if (!value || !groupedMessages || groupedMessages.length === 0)
248
+ return undefined;
249
+ const last_group = groupedMessages[groupedMessages.length - 1];
250
+ if (last_group[0].role !== "assistant") return undefined;
251
+ return last_group[last_group.length - 1].options;
252
+ });
253
+
254
+ function handle_action(
255
+ i: number,
256
+ message: NormalisedMessage,
257
+ selected: string | null
258
+ ): void {
259
+ if (selected === "undo" || selected === "retry") {
260
+ const val_ = value as NormalisedMessage[];
261
+ let last_index = val_.length - 1;
262
+ while (val_[last_index].role === "assistant") {
263
+ last_index--;
264
+ }
265
+ const handler = selected === "undo" ? onundo : onretry;
266
+ handler?.({
267
+ index: val_[last_index].index,
268
+ value: val_[last_index].content
269
+ });
270
+ } else if (selected == "edit") {
271
+ edit_index = i;
272
+ edit_messages.push(message.content as string);
273
+ } else if (selected == "edit_cancel") {
274
+ edit_index = null;
275
+ } else if (selected == "edit_submit") {
276
+ edit_index = null;
277
+ onedit?.({
278
+ index: message.index,
279
+ _dispatch_value: [{ type: "text", text: edit_messages[i].slice() }],
280
+ value: edit_messages[i].slice(),
281
+ previous_value: message.content as string
282
+ });
283
+ } else {
284
+ let feedback =
285
+ selected === "Like"
286
+ ? true
287
+ : selected === "Dislike"
288
+ ? false
289
+ : selected || "";
290
+ if (!groupedMessages) return;
291
+
292
+ const message_group = groupedMessages[i];
293
+ const [first] = [
294
+ message_group[0],
295
+ message_group[message_group.length - 1]
296
+ ];
297
+
298
+ onlike?.({
299
+ index: first.index as number,
300
+ value: message_group.map((m) => m.content),
301
+ liked: feedback
302
+ });
303
+ }
304
+ }
305
+ </script>
306
+
307
+ {#if value !== null && value.length > 0}
308
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
309
+ {#if show_share_button}
310
+ <IconButton
311
+ Icon={Community}
312
+ onclick={async () => {
313
+ try {
314
+ // @ts-ignore
315
+ const formatted = await format_chat_for_sharing(value);
316
+ onshare?.({
317
+ description: formatted
318
+ });
319
+ } catch (e) {
320
+ console.error(e);
321
+ let message = e instanceof ShareError ? e.message : "Share failed.";
322
+ onerror?.(message);
323
+ }
324
+ }}
325
+ />
326
+ {/if}
327
+ <IconButton
328
+ Icon={Trash}
329
+ onclick={() => onclear?.()}
330
+ label={i18n("chatbot.clear")}
331
+ />
332
+ {#if show_copy_all_button}
333
+ <CopyAll {value} {watermark} />
334
+ {/if}
335
+ </IconButtonWrapper>
336
+ {/if}
337
+
338
+ <div
339
+ class={layout === "bubble" ? "bubble-wrap" : "panel-wrap"}
340
+ bind:this={div}
341
+ role="log"
342
+ aria-label="chatbot conversation"
343
+ aria-live="polite"
344
+ >
345
+ {#if value !== null && value.length > 0 && groupedMessages !== null}
346
+ <div class="message-wrap" use:copy>
347
+ {#each groupedMessages as messages, i}
348
+ {@const role = messages[0].role === "user" ? "user" : "bot"}
349
+ {@const avatar_img = avatar_images[role === "user" ? 0 : 1]}
350
+ {@const opposite_avatar_img = avatar_images[role === "user" ? 0 : 1]}
351
+ {@const feedback_index = groupedMessages
352
+ .slice(0, i)
353
+ .filter((m) => m[0].role === "assistant").length}
354
+ {@const current_feedback =
355
+ role === "bot" && feedback_value && feedback_value[feedback_index]
356
+ ? feedback_value[feedback_index]
357
+ : null}
358
+ <Message
359
+ {messages}
360
+ {display_consecutive_in_same_bubble}
361
+ {opposite_avatar_img}
362
+ {avatar_img}
363
+ {role}
364
+ {layout}
365
+ {onselect}
366
+ {i18n}
367
+ {_fetch}
368
+ {line_breaks}
369
+ {theme_mode}
370
+ {target}
371
+ {upload}
372
+ {selectable}
373
+ {sanitize_html}
374
+ {render_markdown}
375
+ {rtl}
376
+ {i}
377
+ {value}
378
+ {latex_delimiters}
379
+ {_components}
380
+ {generating}
381
+ {feedback_options}
382
+ {current_feedback}
383
+ {allow_tags}
384
+ {watermark}
385
+ show_like={role === "user" ? likeable && like_user_message : likeable}
386
+ show_retry={_retryable && is_last_bot_message(messages, value)}
387
+ show_undo={_undoable && is_last_bot_message(messages, value)}
388
+ show_edit={editable === "all" ||
389
+ (editable == "user" &&
390
+ role === "user" &&
391
+ messages.length > 0 &&
392
+ messages[messages.length - 1].type == "text")}
393
+ in_edit_mode={edit_index === i}
394
+ bind:edit_messages
395
+ {show_copy_button}
396
+ handle_action={(selected) => {
397
+ if (selected == "edit") {
398
+ edit_messages.splice(0, edit_messages.length);
399
+ }
400
+ if (selected === "edit" || selected === "edit_submit") {
401
+ messages.forEach((msg, index) => {
402
+ handle_action(selected === "edit" ? i : index, msg, selected);
403
+ });
404
+ } else {
405
+ handle_action(i, messages[0], selected);
406
+ }
407
+ }}
408
+ scroll={is_browser ? scroll_to_bottom_after_component_load : () => {}}
409
+ {allow_file_downloads}
410
+ {onerror}
411
+ {onshare}
412
+ {oncopy}
413
+ />
414
+ {#if show_progress !== "hidden" && generating && messages[messages.length - 1].role === "assistant" && messages[messages.length - 1].metadata?.status === "done"}
415
+ <Pending {layout} {avatar_images} />
416
+ {/if}
417
+ {/each}
418
+ {#if show_progress !== "hidden" && pending_message}
419
+ <Pending {layout} {avatar_images} />
420
+ {:else if options}
421
+ <div class="options">
422
+ {#each options as option, index}
423
+ <button
424
+ class="option"
425
+ onclick={() =>
426
+ onoptionselect?.({
427
+ index: index,
428
+ value: option.value
429
+ })}
430
+ >
431
+ {option.label || option.value}
432
+ </button>
433
+ {/each}
434
+ </div>
435
+ {/if}
436
+ </div>
437
+ {:else}
438
+ <Examples {examples} {placeholder} {latex_delimiters} {onexampleselect} />
439
+ {/if}
440
+ </div>
441
+
442
+ {#if show_scroll_button}
443
+ <div class="scroll-down-button-container">
444
+ <IconButton
445
+ Icon={ScrollDownArrow}
446
+ label="Scroll down"
447
+ size="large"
448
+ onclick={scroll_to_bottom}
449
+ />
450
+ </div>
451
+ {/if}
452
+
453
+ <style>
454
+ .panel-wrap {
455
+ width: 100%;
456
+ overflow-y: auto;
457
+ }
458
+
459
+ .bubble-wrap {
460
+ width: 100%;
461
+ overflow-y: auto;
462
+ height: 100%;
463
+ padding-top: var(--spacing-xxl);
464
+ }
465
+
466
+ @media (prefers-color-scheme: dark) {
467
+ .bubble-wrap {
468
+ background: var(--background-fill-secondary);
469
+ }
470
+ }
471
+
472
+ .message-wrap :global(.prose.chatbot.md) {
473
+ opacity: 0.8;
474
+ overflow-wrap: break-word;
475
+ }
476
+
477
+ .message-wrap :global(.message-row .md img) {
478
+ border-radius: var(--radius-xl);
479
+ margin: var(--size-2);
480
+ width: 400px;
481
+ max-width: 30vw;
482
+ max-height: 30vw;
483
+ }
484
+
485
+ /* link styles */
486
+ .message-wrap :global(.message a) {
487
+ color: var(--color-text-link);
488
+ text-decoration: underline;
489
+ }
490
+
491
+ /* table styles */
492
+ .message-wrap :global(.bot:not(:has(.table-wrap)) table),
493
+ .message-wrap :global(.bot:not(:has(.table-wrap)) tr),
494
+ .message-wrap :global(.bot:not(:has(.table-wrap)) td),
495
+ .message-wrap :global(.bot:not(:has(.table-wrap)) th) {
496
+ border: 1px solid var(--border-color-primary);
497
+ }
498
+
499
+ .message-wrap :global(.user table),
500
+ .message-wrap :global(.user tr),
501
+ .message-wrap :global(.user td),
502
+ .message-wrap :global(.user th) {
503
+ border: 1px solid var(--border-color-accent);
504
+ }
505
+
506
+ /* KaTeX */
507
+ .message-wrap :global(span.katex) {
508
+ font-size: var(--text-lg);
509
+ direction: ltr;
510
+ }
511
+
512
+ .message-wrap :global(span.katex-display) {
513
+ margin-top: 0;
514
+ }
515
+
516
+ .message-wrap :global(pre) {
517
+ position: relative;
518
+ }
519
+
520
+ .message-wrap :global(.grid-wrap) {
521
+ max-height: 80% !important;
522
+ max-width: 600px;
523
+ object-fit: contain;
524
+ }
525
+
526
+ .message-wrap > div :global(p:not(:first-child)) {
527
+ margin-top: var(--spacing-xxl);
528
+ }
529
+
530
+ .message-wrap {
531
+ display: flex;
532
+ flex-direction: column;
533
+ justify-content: space-between;
534
+ margin-bottom: var(--spacing-xxl);
535
+ direction: ltr;
536
+ }
537
+
538
+ .panel-wrap :global(.message-row:first-child) {
539
+ padding-top: calc(var(--spacing-xxl) * 2);
540
+ }
541
+
542
+ .scroll-down-button-container {
543
+ position: absolute;
544
+ bottom: 10px;
545
+ left: 50%;
546
+ transform: translateX(-50%);
547
+ z-index: var(--layer-top);
548
+ }
549
+ .scroll-down-button-container :global(button) {
550
+ border-radius: 50%;
551
+ box-shadow: var(--shadow-drop);
552
+ transition:
553
+ box-shadow 0.2s ease-in-out,
554
+ transform 0.2s ease-in-out;
555
+ }
556
+ .scroll-down-button-container :global(button:hover) {
557
+ box-shadow:
558
+ var(--shadow-drop),
559
+ 0 2px 2px rgba(0, 0, 0, 0.05);
560
+ transform: translateY(-2px);
561
+ }
562
+
563
+ .options {
564
+ margin-left: auto;
565
+ padding: var(--spacing-xxl);
566
+ display: grid;
567
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
568
+ gap: var(--spacing-xxl);
569
+ max-width: calc(min(4 * 200px + 5 * var(--spacing-xxl), 100%));
570
+ justify-content: flex-end;
571
+ }
572
+
573
+ .option {
574
+ display: flex;
575
+ flex-direction: column;
576
+ align-items: center;
577
+ padding: var(--spacing-xl);
578
+ border: 1px dashed var(--border-color-primary);
579
+ border-radius: var(--radius-md);
580
+ background-color: var(--background-fill-secondary);
581
+ cursor: pointer;
582
+ transition: var(--button-transition);
583
+ max-width: var(--size-56);
584
+ width: 100%;
585
+ justify-content: center;
586
+ }
587
+
588
+ .option:hover {
589
+ background-color: var(--color-accent-soft);
590
+ border-color: var(--border-color-accent);
591
+ }
592
+ </style>
6.20.0/chatbot/shared/Component.svelte ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
3
+ import type { Client } from "@gradio/client";
4
+ import type { ComponentType, SvelteComponent } from "svelte";
5
+
6
+ let {
7
+ type,
8
+ components,
9
+ value,
10
+ target,
11
+ theme_mode,
12
+ props,
13
+ i18n,
14
+ upload,
15
+ _fetch,
16
+ allow_file_downloads,
17
+ display_icon_button_wrapper_top_corner = false,
18
+ onload
19
+ }: {
20
+ type:
21
+ | "gallery"
22
+ | "plot"
23
+ | "audio"
24
+ | "video"
25
+ | "image"
26
+ | "dataframe"
27
+ | "model3d"
28
+ | string;
29
+ components: Record<string, ComponentType<SvelteComponent>>;
30
+ value: any;
31
+ target: HTMLElement | null;
32
+ theme_mode: "light" | "dark" | "system";
33
+ props: any;
34
+ i18n: I18nFormatter;
35
+ upload: Client["upload"];
36
+ _fetch: typeof fetch;
37
+ allow_file_downloads: boolean;
38
+ display_icon_button_wrapper_top_corner?: boolean;
39
+ onload?: () => void;
40
+ } = $props();
41
+
42
+ let image_fullscreen = $state(false);
43
+ let image_container: HTMLElement;
44
+
45
+ function handle_fullscreen(event: CustomEvent<boolean>): void {
46
+ image_fullscreen = event.detail;
47
+ if (image_fullscreen && image_container) {
48
+ image_container.requestFullscreen?.();
49
+ } else if (document.fullscreenElement) {
50
+ document.exitFullscreen?.();
51
+ }
52
+ }
53
+
54
+ function handle_load(): void {
55
+ onload?.();
56
+ }
57
+ </script>
58
+
59
+ {#if type === "gallery"}
60
+ <svelte:component
61
+ this={components[type]}
62
+ {...props}
63
+ {value}
64
+ display_icon_button_wrapper_top_corner={false}
65
+ show_label={props.label ? true : false}
66
+ {i18n}
67
+ {_fetch}
68
+ allow_preview={false}
69
+ interactive={false}
70
+ mode="minimal"
71
+ fixed_height={1}
72
+ on:load={handle_load}
73
+ />
74
+ {:else if type === "dataframe"}
75
+ <svelte:component
76
+ this={components[type]}
77
+ {...props}
78
+ {value}
79
+ show_label={props.label ? true : false}
80
+ {i18n}
81
+ interactive={false}
82
+ line_breaks={props.line_breaks}
83
+ wrap={true}
84
+ root=""
85
+ gradio={{ dispatch: () => {}, i18n }}
86
+ datatype={props.datatype}
87
+ latex_delimiters={props.latex_delimiters}
88
+ col_count={props.col_count}
89
+ row_count={props.row_count}
90
+ on:load={handle_load}
91
+ />
92
+ {:else if type === "plot"}
93
+ <svelte:component
94
+ this={components[type]}
95
+ {...props}
96
+ {value}
97
+ {target}
98
+ {theme_mode}
99
+ on_change={() => {}}
100
+ bokeh_version={props.bokeh_version}
101
+ caption={props.caption || ""}
102
+ show_actions_button={true}
103
+ on:load={handle_load}
104
+ />
105
+ {:else if type === "audio"}
106
+ <svelte:component
107
+ this={components[type]}
108
+ {...props}
109
+ {value}
110
+ show_label={props.label ? true : false}
111
+ show_share_button={false}
112
+ {i18n}
113
+ waveform_settings={{
114
+ ...props.waveform_settings,
115
+ autoplay: props.autoplay
116
+ }}
117
+ show_download_button={false}
118
+ display_icon_button_wrapper_top_corner={false}
119
+ minimal={true}
120
+ on:load={handle_load}
121
+ />
122
+ {:else if type === "video"}
123
+ <svelte:component
124
+ this={components[type]}
125
+ {...props}
126
+ autoplay={props.autoplay}
127
+ value={value.video || value}
128
+ show_label={props.label ? true : false}
129
+ show_share_button={false}
130
+ {i18n}
131
+ {upload}
132
+ display_icon_button_wrapper_top_corner={false}
133
+ show_download_button={false}
134
+ on:load={handle_load}
135
+ >
136
+ <track kind="captions" />
137
+ </svelte:component>
138
+ {:else if type === "image"}
139
+ <div bind:this={image_container}>
140
+ <svelte:component
141
+ this={components[type]}
142
+ {...props}
143
+ {value}
144
+ show_label={props.label ? true : false}
145
+ display_icon_button_wrapper_top_corner={false}
146
+ buttons={["fullscreen"]}
147
+ fullscreen={image_fullscreen}
148
+ show_button_background={false}
149
+ on:fullscreen={handle_fullscreen}
150
+ on:load={handle_load}
151
+ {i18n}
152
+ />
153
+ </div>
154
+ {:else if type === "html"}
155
+ <svelte:component
156
+ this={components[type]}
157
+ {...props}
158
+ props={{ value }}
159
+ on:load={handle_load}
160
+ />
161
+ {:else if type === "model3d"}
162
+ <svelte:component
163
+ this={components[type]}
164
+ {...props}
165
+ {value}
166
+ clear_color={props.clear_color}
167
+ display_mode={props.display_mode}
168
+ zoom_speed={props.zoom_speed}
169
+ pan_speed={props.pan_speed}
170
+ {...props.camera_position !== undefined && {
171
+ camera_position: props.camera_position
172
+ }}
173
+ has_change_history={true}
174
+ show_label={props.label ? true : false}
175
+ root=""
176
+ interactive={false}
177
+ show_share_button={false}
178
+ gradio={{ dispatch: () => {}, i18n }}
179
+ on:load={handle_load}
180
+ {i18n}
181
+ />
182
+ {/if}
6.20.0/chatbot/shared/Copy.svelte ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Copy, Check } from "@gradio/icons";
3
+ import { IconButton } from "@gradio/atoms";
4
+ import type { CopyData } from "@gradio/utils";
5
+ import type { I18nFormatter } from "js/core/src/gradio_helper";
6
+
7
+ let {
8
+ value,
9
+ watermark = null,
10
+ i18n,
11
+ oncopy
12
+ }: {
13
+ value: string;
14
+ watermark?: string | null;
15
+ i18n: I18nFormatter;
16
+ oncopy?: (data: CopyData) => void;
17
+ } = $props();
18
+
19
+ let copied = $state(false);
20
+ let timer: NodeJS.Timeout;
21
+
22
+ function copy_feedback(): void {
23
+ copied = true;
24
+ if (timer) clearTimeout(timer);
25
+ timer = setTimeout(() => {
26
+ copied = false;
27
+ }, 2000);
28
+ }
29
+
30
+ async function handle_copy(): Promise<void> {
31
+ if ("clipboard" in navigator) {
32
+ oncopy?.({ value: value });
33
+ const text_to_copy = watermark ? `${value}\n\n${watermark}` : value;
34
+ await navigator.clipboard.writeText(text_to_copy);
35
+ copy_feedback();
36
+ } else {
37
+ const textArea = document.createElement("textarea");
38
+ const text_to_copy = watermark ? `${value}\n\n${watermark}` : value;
39
+ textArea.value = text_to_copy;
40
+
41
+ textArea.style.position = "absolute";
42
+ textArea.style.left = "-999999px";
43
+
44
+ document.body.prepend(textArea);
45
+ textArea.select();
46
+
47
+ try {
48
+ document.execCommand("copy");
49
+ copy_feedback();
50
+ } catch (error) {
51
+ console.error(error);
52
+ } finally {
53
+ textArea.remove();
54
+ }
55
+ }
56
+ }
57
+
58
+ $effect(() => {
59
+ return () => {
60
+ if (timer) clearTimeout(timer);
61
+ };
62
+ });
63
+ </script>
64
+
65
+ <IconButton
66
+ onclick={handle_copy}
67
+ label={copied ? i18n("chatbot.copied_message") : i18n("chatbot.copy_message")}
68
+ Icon={copied ? Check : Copy}
69
+ />
6.20.0/chatbot/shared/CopyAll.svelte ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Copy, Check } from "@gradio/icons";
3
+ import type { NormalisedMessage } from "../types";
4
+ import { IconButton } from "@gradio/atoms";
5
+
6
+ let {
7
+ value,
8
+ watermark = null
9
+ }: {
10
+ value: NormalisedMessage[] | null;
11
+ watermark?: string | null;
12
+ } = $props();
13
+
14
+ let copied = $state(false);
15
+ let timer: NodeJS.Timeout;
16
+
17
+ function copy_feedback(): void {
18
+ copied = true;
19
+ if (timer) clearTimeout(timer);
20
+ timer = setTimeout(() => {
21
+ copied = false;
22
+ }, 1000);
23
+ }
24
+
25
+ const copy_conversation = (): void => {
26
+ if (value) {
27
+ const conversation_value = value
28
+ .map((message) => {
29
+ if (message.type === "text") {
30
+ return `${message.role}: ${message.content}`;
31
+ }
32
+ return `${message.role}: ${message.content.value.url}`;
33
+ })
34
+ .join("\n\n");
35
+
36
+ const text_to_copy = watermark
37
+ ? `${conversation_value}\n\n${watermark}`
38
+ : conversation_value;
39
+
40
+ navigator.clipboard.writeText(text_to_copy).catch((err) => {
41
+ console.error("Failed to copy conversation: ", err);
42
+ });
43
+ }
44
+ };
45
+
46
+ async function handle_copy(): Promise<void> {
47
+ if ("clipboard" in navigator) {
48
+ copy_conversation();
49
+ copy_feedback();
50
+ }
51
+ }
52
+
53
+ $effect(() => {
54
+ return () => {
55
+ if (timer) clearTimeout(timer);
56
+ };
57
+ });
58
+ </script>
59
+
60
+ <IconButton
61
+ Icon={copied ? Check : Copy}
62
+ onclick={handle_copy}
63
+ label={copied ? "Copied conversation" : "Copy conversation"}
64
+ />
6.20.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.20.0/chatbot/shared/Examples.svelte ADDED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 type { SelectData } from "@gradio/utils";
7
+
8
+ let {
9
+ examples = null,
10
+ placeholder = null,
11
+ latex_delimiters,
12
+ onexampleselect
13
+ }: {
14
+ examples?: ExampleMessage[] | null;
15
+ placeholder?: string | null;
16
+ latex_delimiters: {
17
+ left: string;
18
+ right: string;
19
+ display: boolean;
20
+ }[];
21
+ onexampleselect?: (data: SelectData) => void;
22
+ } = $props();
23
+
24
+ function handle_example_select(
25
+ i: number,
26
+ example: ExampleMessage | string
27
+ ): void {
28
+ const example_obj =
29
+ typeof example === "string" ? { text: example } : example;
30
+ onexampleselect?.({
31
+ index: i,
32
+ value: { text: example_obj.text, files: example_obj.files }
33
+ });
34
+ }
35
+ </script>
36
+
37
+ <div class="placeholder-content" role="complementary">
38
+ {#if placeholder !== null}
39
+ <div class="placeholder">
40
+ <Markdown message={placeholder} {latex_delimiters} />
41
+ </div>
42
+ {/if}
43
+ {#if examples !== null}
44
+ <div class="examples" role="list">
45
+ {#each examples as example, i}
46
+ <button
47
+ class="example"
48
+ onclick={() =>
49
+ handle_example_select(
50
+ i,
51
+ typeof example === "string" ? { text: example } : example
52
+ )}
53
+ aria-label={`Select example ${i + 1}: ${example.display_text || example.text}`}
54
+ >
55
+ <div class="example-content">
56
+ {#if example?.icon?.url}
57
+ <div class="example-image-container">
58
+ <Image
59
+ class="example-image"
60
+ src={example.icon.url}
61
+ alt="Example icon"
62
+ />
63
+ </div>
64
+ {:else if example?.icon?.mime_type === "text"}
65
+ <div class="example-icon" aria-hidden="true">
66
+ <span class="text-icon-aa">Aa</span>
67
+ </div>
68
+ {:else if example.files !== undefined && example.files.length > 0}
69
+ {#if example.files.length > 1}
70
+ <div
71
+ class="example-icons-grid"
72
+ role="group"
73
+ aria-label="Example attachments"
74
+ >
75
+ {#each example.files.slice(0, 4) as file, i}
76
+ {#if file.mime_type?.includes("image")}
77
+ <div class="example-image-container">
78
+ <Image
79
+ class="example-image"
80
+ src={file.url}
81
+ alt={file.orig_name || `Example image ${i + 1}`}
82
+ />
83
+ {#if i === 3 && example.files.length > 4}
84
+ <div
85
+ class="image-overlay"
86
+ role="status"
87
+ aria-label={`${example.files.length - 4} more files`}
88
+ >
89
+ +{example.files.length - 4}
90
+ </div>
91
+ {/if}
92
+ </div>
93
+ {:else if file.mime_type?.includes("video")}
94
+ <div class="example-image-container">
95
+ <video
96
+ class="example-image"
97
+ src={file.url}
98
+ aria-hidden="true"
99
+ />
100
+ {#if i === 3 && example.files.length > 4}
101
+ <div
102
+ class="image-overlay"
103
+ role="status"
104
+ aria-label={`${example.files.length - 4} more files`}
105
+ >
106
+ +{example.files.length - 4}
107
+ </div>
108
+ {/if}
109
+ </div>
110
+ {:else}
111
+ <div
112
+ class="example-icon"
113
+ aria-label={`File: ${file.orig_name}`}
114
+ >
115
+ {#if file.mime_type?.includes("audio")}
116
+ <Music />
117
+ {:else}
118
+ <File />
119
+ {/if}
120
+ </div>
121
+ {/if}
122
+ {/each}
123
+ {#if example.files.length > 4}
124
+ <div class="example-icon">
125
+ <div
126
+ class="file-overlay"
127
+ role="status"
128
+ aria-label={`${example.files.length - 4} more files`}
129
+ >
130
+ +{example.files.length - 4}
131
+ </div>
132
+ </div>
133
+ {/if}
134
+ </div>
135
+ {:else if example.files[0].mime_type?.includes("image")}
136
+ <div class="example-image-container">
137
+ <Image
138
+ class="example-image"
139
+ src={example.files[0].url}
140
+ alt={example.files[0].orig_name || "Example image"}
141
+ />
142
+ </div>
143
+ {:else if example.files[0].mime_type?.includes("video")}
144
+ <div class="example-image-container">
145
+ <video
146
+ class="example-image"
147
+ src={example.files[0].url}
148
+ aria-hidden="true"
149
+ />
150
+ </div>
151
+ {:else if example.files[0].mime_type?.includes("audio")}
152
+ <div
153
+ class="example-icon"
154
+ aria-label={`File: ${example.files[0].orig_name}`}
155
+ >
156
+ <Music />
157
+ </div>
158
+ {:else}
159
+ <div
160
+ class="example-icon"
161
+ aria-label={`File: ${example.files[0].orig_name}`}
162
+ >
163
+ <File />
164
+ </div>
165
+ {/if}
166
+ {/if}
167
+
168
+ <div class="example-text-content">
169
+ <span class="example-text"
170
+ >{example.display_text || example.text}</span
171
+ >
172
+ </div>
173
+ </div>
174
+ </button>
175
+ {/each}
176
+ </div>
177
+ {/if}
178
+ </div>
179
+
180
+ <style>
181
+ .placeholder-content {
182
+ display: flex;
183
+ flex-direction: column;
184
+ height: 100%;
185
+ }
186
+
187
+ .placeholder {
188
+ align-items: center;
189
+ display: flex;
190
+ justify-content: center;
191
+ height: 100%;
192
+ flex-grow: 1;
193
+ }
194
+
195
+ .examples :global(img) {
196
+ pointer-events: none;
197
+ }
198
+
199
+ .examples {
200
+ margin: auto;
201
+ padding: var(--spacing-xxl);
202
+ display: grid;
203
+ grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
204
+ gap: var(--spacing-xl);
205
+ max-width: calc(min(4 * 240px + 5 * var(--spacing-xxl), 100%));
206
+ }
207
+
208
+ .example {
209
+ display: flex;
210
+ flex-direction: column;
211
+ align-items: flex-start;
212
+ padding: var(--spacing-xxl);
213
+ border: none;
214
+ border-radius: var(--radius-lg);
215
+ background-color: var(--block-background-fill);
216
+ cursor: pointer;
217
+ transition: all 150ms ease-in-out;
218
+ width: 100%;
219
+ gap: var(--spacing-sm);
220
+ border: var(--block-border-width) solid var(--block-border-color);
221
+ transform: translateY(0px);
222
+ }
223
+
224
+ .example:hover {
225
+ transform: translateY(-2px);
226
+ background-color: var(--color-accent-soft);
227
+ }
228
+
229
+ .example-content {
230
+ display: flex;
231
+ flex-direction: column;
232
+ align-items: flex-start;
233
+ width: 100%;
234
+ height: 100%;
235
+ }
236
+
237
+ .example-text-content {
238
+ margin-top: var(--spacing-sm);
239
+ text-align: start;
240
+ }
241
+
242
+ .example-text {
243
+ font-size: var(--text-md);
244
+ text-align: start;
245
+ overflow: hidden;
246
+ text-overflow: ellipsis;
247
+ }
248
+
249
+ .example-icons-grid {
250
+ display: flex;
251
+ gap: var(--spacing-sm);
252
+ width: 100%;
253
+ }
254
+
255
+ .example-icon {
256
+ flex-shrink: 0;
257
+ width: var(--size-8);
258
+ height: var(--size-8);
259
+ display: flex;
260
+ align-items: center;
261
+ justify-content: center;
262
+ border-radius: var(--radius-lg);
263
+ border: var(--block-border-width) solid var(--block-border-color);
264
+ background-color: var(--block-background-fill);
265
+ position: relative;
266
+ }
267
+
268
+ .example-icon :global(svg) {
269
+ width: var(--size-4);
270
+ height: var(--size-4);
271
+ color: var(--color-text-secondary);
272
+ }
273
+
274
+ .text-icon-aa {
275
+ font-size: var(--text-sm);
276
+ font-weight: var(--weight-semibold);
277
+ color: var(--color-text-secondary);
278
+ line-height: 1;
279
+ }
280
+
281
+ .example-image-container {
282
+ width: var(--size-8);
283
+ height: var(--size-8);
284
+ border-radius: var(--radius-lg);
285
+ overflow: hidden;
286
+ position: relative;
287
+ }
288
+
289
+ .example-image-container :global(img) {
290
+ width: 100%;
291
+ height: 100%;
292
+ object-fit: cover;
293
+ }
294
+
295
+ .image-overlay {
296
+ position: absolute;
297
+ top: 0;
298
+ left: 0;
299
+ right: 0;
300
+ bottom: 0;
301
+ background: rgba(0, 0, 0, 0.6);
302
+ color: white;
303
+ display: flex;
304
+ align-items: center;
305
+ justify-content: center;
306
+ font-size: var(--text-lg);
307
+ font-weight: var(--weight-semibold);
308
+ border-radius: var(--radius-lg);
309
+ }
310
+
311
+ .file-overlay {
312
+ position: absolute;
313
+ inset: 0;
314
+ background: rgba(0, 0, 0, 0.6);
315
+ color: white;
316
+ display: flex;
317
+ align-items: center;
318
+ justify-content: center;
319
+ font-size: var(--text-sm);
320
+ font-weight: var(--weight-semibold);
321
+ border-radius: var(--radius-lg);
322
+ }
323
+ </style>
6.20.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.20.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.20.0/chatbot/shared/LikeDislike.svelte ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ let {
12
+ i18n,
13
+ handle_action,
14
+ feedback_options,
15
+ selected: selected_prop = null
16
+ }: {
17
+ i18n: I18nFormatter;
18
+ handle_action: (selected: string | null) => void;
19
+ feedback_options: string[];
20
+ selected?: string | null;
21
+ } = $props();
22
+
23
+ let selected = $state(selected_prop);
24
+
25
+ $effect(() => {
26
+ selected = selected_prop;
27
+ });
28
+
29
+ let extra_feedback = $derived(
30
+ feedback_options.filter(
31
+ (option) => option !== "Like" && option !== "Dislike"
32
+ )
33
+ );
34
+
35
+ function toggleSelection(newSelection: string): void {
36
+ selected = selected === newSelection ? null : newSelection;
37
+ handle_action(selected);
38
+ }
39
+ </script>
40
+
41
+ {#if feedback_options.includes("Like") || feedback_options.includes("Dislike")}
42
+ {#if feedback_options.includes("Dislike")}
43
+ <IconButton
44
+ Icon={selected === "Dislike" ? ThumbDownActive : ThumbDownDefault}
45
+ label={selected === "Dislike" ? "Disliked" : i18n("chatbot.dislike")}
46
+ color={selected === "Dislike"
47
+ ? "var(--color-accent)"
48
+ : "var(--block-label-text-color)"}
49
+ onclick={() => toggleSelection("Dislike")}
50
+ />
51
+ {/if}
52
+ {#if feedback_options.includes("Like")}
53
+ <IconButton
54
+ Icon={selected === "Like" ? ThumbUpActive : ThumbUpDefault}
55
+ label={selected === "Like" ? "Liked" : i18n("chatbot.like")}
56
+ color={selected === "Like"
57
+ ? "var(--color-accent)"
58
+ : "var(--block-label-text-color)"}
59
+ onclick={() => toggleSelection("Like")}
60
+ />
61
+ {/if}
62
+ {/if}
63
+
64
+ {#if extra_feedback.length > 0}
65
+ <div class="extra-feedback no-border">
66
+ <IconButton
67
+ Icon={selected && extra_feedback.includes(selected) ? FlagActive : Flag}
68
+ label="Feedback"
69
+ color={selected && extra_feedback.includes(selected)
70
+ ? "var(--color-accent)"
71
+ : "var(--block-label-text-color)"}
72
+ />
73
+ <div class="extra-feedback-options">
74
+ {#each extra_feedback as option}
75
+ <button
76
+ class="extra-feedback-option"
77
+ style:font-weight={selected === option ? "bold" : "normal"}
78
+ onclick={() => {
79
+ toggleSelection(option);
80
+ handle_action(selected ? selected : null);
81
+ }}>{option}</button
82
+ >
83
+ {/each}
84
+ </div>
85
+ </div>
86
+ {/if}
87
+
88
+ <style>
89
+ .extra-feedback {
90
+ display: flex;
91
+ align-items: center;
92
+ position: relative;
93
+ }
94
+ .extra-feedback-options {
95
+ display: none;
96
+ position: absolute;
97
+ padding: var(--spacing-md) 0;
98
+ flex-direction: column;
99
+ gap: var(--spacing-sm);
100
+ top: 100%;
101
+ }
102
+ .extra-feedback:hover .extra-feedback-options {
103
+ display: flex;
104
+ }
105
+ .extra-feedback-option {
106
+ border: 1px solid var(--border-color-primary);
107
+ border-radius: var(--radius-sm);
108
+ color: var(--block-label-text-color);
109
+ background-color: var(--block-background-fill);
110
+ font-size: var(--text-xs);
111
+ padding: var(--spacing-xxs) var(--spacing-sm);
112
+ width: max-content;
113
+ }
114
+ </style>
6.20.0/chatbot/shared/Message.svelte ADDED
@@ -0,0 +1,679 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import type { CopyData, SelectData } from "@gradio/utils";
13
+
14
+ let {
15
+ value,
16
+ avatar_img,
17
+ opposite_avatar_img = null,
18
+ role = "user",
19
+ messages = [],
20
+ layout,
21
+ render_markdown,
22
+ latex_delimiters,
23
+ sanitize_html,
24
+ selectable,
25
+ _fetch,
26
+ rtl,
27
+ onselect,
28
+ i18n,
29
+ line_breaks,
30
+ upload,
31
+ target,
32
+ theme_mode,
33
+ _components,
34
+ i,
35
+ show_copy_button,
36
+ generating,
37
+ feedback_options,
38
+ show_like,
39
+ show_edit,
40
+ show_retry,
41
+ show_undo,
42
+ handle_action,
43
+ scroll,
44
+ allow_file_downloads,
45
+ in_edit_mode,
46
+ edit_messages = $bindable([]),
47
+ display_consecutive_in_same_bubble,
48
+ current_feedback = null,
49
+ allow_tags = false,
50
+ watermark = null,
51
+ onerror,
52
+ onshare,
53
+ oncopy
54
+ }: {
55
+ value: NormalisedMessage[];
56
+ avatar_img: FileData | null;
57
+ opposite_avatar_img?: FileData | null;
58
+ role?: string;
59
+ messages?: NormalisedMessage[];
60
+ layout: "bubble" | "panel";
61
+ render_markdown: boolean;
62
+ latex_delimiters: {
63
+ left: string;
64
+ right: string;
65
+ display: boolean;
66
+ }[];
67
+ sanitize_html: boolean;
68
+ selectable: boolean;
69
+ _fetch: typeof fetch;
70
+ rtl: boolean;
71
+ onselect?: (data: SelectData) => void;
72
+ i18n: I18nFormatter;
73
+ line_breaks: boolean;
74
+ upload: Client["upload"];
75
+ target: HTMLElement | null;
76
+ theme_mode: "light" | "dark" | "system";
77
+ _components: Record<string, ComponentType<SvelteComponent>>;
78
+ i: number;
79
+ show_copy_button: boolean;
80
+ generating: boolean;
81
+ feedback_options: string[];
82
+ show_like: boolean;
83
+ show_edit: boolean;
84
+ show_retry: boolean;
85
+ show_undo: boolean;
86
+ handle_action: (selected: string | null) => void;
87
+ scroll: () => void;
88
+ allow_file_downloads: boolean;
89
+ in_edit_mode: boolean;
90
+ edit_messages?: string[];
91
+ display_consecutive_in_same_bubble: boolean;
92
+ current_feedback?: string | null;
93
+ allow_tags?: string[] | boolean;
94
+ watermark?: string | null;
95
+ onerror?: (message: string) => void;
96
+ onshare?: (data: any) => void;
97
+ oncopy?: (data: CopyData) => void;
98
+ } = $props();
99
+
100
+ let messageElements: HTMLDivElement[] = [];
101
+ let previous_edit_mode = $state(false);
102
+ let message_widths: number[] = $state(Array(messages.length).fill(160));
103
+ let message_heights: number[] = $state(Array(messages.length).fill(0));
104
+
105
+ $effect(() => {
106
+ if (in_edit_mode && !previous_edit_mode) {
107
+ const offset = messageElements.length - messages.length;
108
+ for (let idx = offset; idx < messageElements.length; idx++) {
109
+ if (idx >= 0) {
110
+ message_widths[idx - offset] = messageElements[idx]?.clientWidth;
111
+ message_heights[idx - offset] = messageElements[idx]?.clientHeight;
112
+ }
113
+ }
114
+ }
115
+ previous_edit_mode = in_edit_mode;
116
+ });
117
+
118
+ function handle_select(i: number, message: NormalisedMessage): void {
119
+ onselect?.({
120
+ index: message.index,
121
+ value: message.content
122
+ });
123
+ }
124
+
125
+ function get_message_label_data(message: NormalisedMessage): string {
126
+ if (message.type === "text") {
127
+ return message.content;
128
+ } else if (
129
+ message.type === "component" &&
130
+ message.content.component === "file"
131
+ ) {
132
+ if (Array.isArray(message.content.value)) {
133
+ return `file of extension type: ${message.content.value[0].orig_name?.split(".").pop()}`;
134
+ }
135
+ return (
136
+ `file of extension type: ${message.content.value?.orig_name?.split(".").pop()}` +
137
+ (message.content.value?.orig_name ?? "")
138
+ );
139
+ }
140
+ return `a component of type ${message.content.component ?? "unknown"}`;
141
+ }
142
+
143
+ function get_file(messages: NormalisedMessage[]): FileData | null {
144
+ for (const message of messages) {
145
+ if (
146
+ message.type === "component" &&
147
+ (message.content.component === "audio" ||
148
+ message.content.component === "video" ||
149
+ message.content.component === "image" ||
150
+ message.content.component === "file") &&
151
+ message.content.value
152
+ ) {
153
+ return message.content.value as FileData;
154
+ }
155
+ }
156
+ return null;
157
+ }
158
+
159
+ let button_panel_props = $derived({
160
+ handle_action,
161
+ likeable: show_like,
162
+ feedback_options,
163
+ show_retry,
164
+ show_undo,
165
+ show_edit,
166
+ in_edit_mode,
167
+ generating,
168
+ show_copy_button,
169
+ message: messages,
170
+ position: (role === "user" ? "right" : "left") as "left" | "right",
171
+ avatar: avatar_img,
172
+ layout,
173
+ current_feedback,
174
+ watermark,
175
+ file: get_file(messages),
176
+ show_download_button: allow_file_downloads,
177
+ show_share_button: true,
178
+ oncopy
179
+ });
180
+ </script>
181
+
182
+ <div
183
+ class="message-row {layout} {role}-row"
184
+ class:with_avatar={avatar_img !== null}
185
+ class:with_opposite_avatar={opposite_avatar_img !== null}
186
+ >
187
+ {#if avatar_img !== null}
188
+ <div class="avatar-container">
189
+ <Image class="avatar-image" src={avatar_img?.url} alt="{role} avatar" />
190
+ </div>
191
+ {/if}
192
+ <div
193
+ class:role
194
+ class="flex-wrap"
195
+ class:component-wrap={messages[0].type === "component"}
196
+ >
197
+ <div
198
+ class:message={display_consecutive_in_same_bubble}
199
+ class={display_consecutive_in_same_bubble ? role : ""}
200
+ >
201
+ {#each messages as message, thought_index}
202
+ <div
203
+ class="message {!display_consecutive_in_same_bubble ? role : ''}"
204
+ class:panel-full-width={true}
205
+ class:message-markdown-disabled={!render_markdown}
206
+ class:component={message.type === "component"}
207
+ class:html={is_component_message(message) &&
208
+ message.content.component === "html"}
209
+ class:thought={thought_index > 0}
210
+ >
211
+ {#if in_edit_mode && message.type === "text"}
212
+ <!-- svelte-ignore a11y_autofocus -->
213
+ <textarea
214
+ class="edit-textarea"
215
+ style:width={`max(${message_widths[thought_index]}px, 160px)`}
216
+ style:min-height={`${message_heights[thought_index]}px`}
217
+ autofocus
218
+ bind:value={edit_messages[thought_index]}
219
+ />
220
+ {:else}
221
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
222
+ <div
223
+ data-testid={role}
224
+ class:latest={i === value.length - 1}
225
+ class:message-markdown-disabled={!render_markdown}
226
+ style:user-select="text"
227
+ class:selectable
228
+ style:cursor={selectable ? "pointer" : "auto"}
229
+ style:text-align={rtl ? "right" : "left"}
230
+ bind:this={messageElements[thought_index]}
231
+ onclick={() => handle_select(i, message)}
232
+ onkeydown={(e) => {
233
+ if (e.key === "Enter") {
234
+ handle_select(i, message);
235
+ }
236
+ }}
237
+ dir={rtl ? "rtl" : "ltr"}
238
+ aria-label={role +
239
+ "'s message: " +
240
+ get_message_label_data(message)}
241
+ >
242
+ {#if message?.metadata?.title}
243
+ <Thought
244
+ thought={message}
245
+ {rtl}
246
+ {sanitize_html}
247
+ {allow_tags}
248
+ {latex_delimiters}
249
+ {render_markdown}
250
+ {_components}
251
+ {upload}
252
+ {thought_index}
253
+ {target}
254
+ {theme_mode}
255
+ {_fetch}
256
+ {scroll}
257
+ {allow_file_downloads}
258
+ {display_consecutive_in_same_bubble}
259
+ {i18n}
260
+ {line_breaks}
261
+ />
262
+ {:else}
263
+ <MessageContent
264
+ {message}
265
+ {sanitize_html}
266
+ {allow_tags}
267
+ {latex_delimiters}
268
+ {render_markdown}
269
+ {_components}
270
+ {upload}
271
+ {thought_index}
272
+ {target}
273
+ {theme_mode}
274
+ {_fetch}
275
+ {scroll}
276
+ {allow_file_downloads}
277
+ {display_consecutive_in_same_bubble}
278
+ {i18n}
279
+ {line_breaks}
280
+ />
281
+ {/if}
282
+ </div>
283
+ {/if}
284
+ </div>
285
+
286
+ {#if layout === "panel"}
287
+ <ButtonPanel
288
+ {...button_panel_props}
289
+ {current_feedback}
290
+ {watermark}
291
+ {onerror}
292
+ {onshare}
293
+ {i18n}
294
+ />
295
+ {/if}
296
+ {/each}
297
+ </div>
298
+ </div>
299
+ </div>
300
+
301
+ {#if layout === "bubble"}
302
+ <ButtonPanel {...button_panel_props} {onerror} {onshare} {i18n} />
303
+ {/if}
304
+
305
+ <style>
306
+ .message {
307
+ position: relative;
308
+ width: 100%;
309
+ }
310
+
311
+ .message.display_consecutive_in_same_bubble {
312
+ margin-top: 0;
313
+ }
314
+
315
+ .message + .message {
316
+ margin-top: var(--spacing-sm);
317
+ }
318
+
319
+ /* avatar styles */
320
+ .avatar-container {
321
+ flex-shrink: 0;
322
+ border-radius: 50%;
323
+ border: 1px solid var(--border-color-primary);
324
+ overflow: hidden;
325
+ }
326
+
327
+ .avatar-container :global(img) {
328
+ object-fit: cover;
329
+ }
330
+
331
+ /* message wrapper */
332
+ .flex-wrap {
333
+ display: flex;
334
+ flex-direction: column;
335
+ width: calc(100% - var(--spacing-xxl));
336
+ max-width: 100%;
337
+ color: var(--body-text-color);
338
+ font-size: var(--chatbot-text-size);
339
+ overflow-wrap: break-word;
340
+ width: 100%;
341
+ height: 100%;
342
+ }
343
+
344
+ .component {
345
+ padding: 0;
346
+ border-radius: var(--radius-md);
347
+ width: fit-content;
348
+ overflow: hidden;
349
+ }
350
+
351
+ .component.gallery {
352
+ border: none;
353
+ }
354
+
355
+ .bot:has(.model3D),
356
+ .user:has(.model3D) {
357
+ border: none;
358
+ max-width: 75%;
359
+ }
360
+
361
+ .message-row :not(.avatar-container) :global(img) {
362
+ margin: var(--size-2);
363
+ max-height: 300px;
364
+ }
365
+
366
+ .file-pil {
367
+ display: block;
368
+ width: fit-content;
369
+ padding: var(--spacing-sm) var(--spacing-lg);
370
+ border-radius: var(--radius-md);
371
+ background: var(--background-fill-secondary);
372
+ color: var(--body-text-color);
373
+ text-decoration: none;
374
+ margin: 0;
375
+ font-family: var(--font-mono);
376
+ font-size: var(--text-sm);
377
+ }
378
+
379
+ .file {
380
+ width: auto !important;
381
+ max-width: fit-content !important;
382
+ }
383
+
384
+ @media (max-width: 600px) or (max-width: 480px) {
385
+ .component {
386
+ width: 100%;
387
+ }
388
+ }
389
+
390
+ .message :global(.prose) {
391
+ font-size: var(--chatbot-text-size);
392
+ }
393
+
394
+ .message-bubble-border {
395
+ border-width: 1px;
396
+ border-radius: var(--radius-md);
397
+ }
398
+
399
+ .panel-full-width {
400
+ width: 100%;
401
+ }
402
+ .message-markdown-disabled {
403
+ white-space: pre-line;
404
+ }
405
+
406
+ .user {
407
+ border-radius: var(--radius-md);
408
+ align-self: flex-end;
409
+ border-bottom-right-radius: 0;
410
+ box-shadow: var(--shadow-drop);
411
+ border: 1px solid var(--border-color-accent-subdued);
412
+ background-color: var(--color-accent-soft);
413
+ padding: var(--spacing-sm) var(--spacing-xl);
414
+ }
415
+
416
+ .bot {
417
+ border: 1px solid var(--border-color-primary);
418
+ border-radius: var(--radius-md);
419
+ border-color: var(--border-color-primary);
420
+ background-color: var(--background-fill-secondary);
421
+ box-shadow: var(--shadow-drop);
422
+ align-self: flex-start;
423
+ text-align: left;
424
+ border-bottom-left-radius: 0;
425
+ padding: var(--spacing-sm) var(--spacing-xl);
426
+ }
427
+
428
+ .bot:has(.table-wrap) {
429
+ border: none;
430
+ box-shadow: none;
431
+ background: none;
432
+ }
433
+
434
+ .panel .user :global(*) {
435
+ text-align: right;
436
+ }
437
+
438
+ /* Colors */
439
+
440
+ .message-row {
441
+ display: flex;
442
+ position: relative;
443
+ }
444
+
445
+ /* bubble mode styles */
446
+ .bubble {
447
+ margin: calc(var(--spacing-xl) * 2);
448
+ margin-bottom: var(--spacing-xl);
449
+ }
450
+
451
+ .bubble.user-row {
452
+ align-self: flex-end;
453
+ max-width: calc(100% - var(--spacing-xl) * 6);
454
+ }
455
+
456
+ .bubble.bot-row {
457
+ align-self: flex-start;
458
+ max-width: calc(100% - var(--spacing-xl) * 6);
459
+ }
460
+
461
+ .bubble .user-row {
462
+ flex-direction: row;
463
+ justify-content: flex-end;
464
+ }
465
+
466
+ .bubble .with_avatar.user-row {
467
+ margin-right: calc(var(--spacing-xl) * 2) !important;
468
+ }
469
+
470
+ .bubble .with_avatar.bot-row {
471
+ margin-left: calc(var(--spacing-xl) * 2) !important;
472
+ }
473
+
474
+ .bubble .with_opposite_avatar.user-row {
475
+ margin-left: calc(var(--spacing-xxl) + 35px + var(--spacing-xxl));
476
+ }
477
+
478
+ /* panel mode styles */
479
+ .panel {
480
+ margin: 0;
481
+ padding: calc(var(--spacing-lg) * 2) calc(var(--spacing-lg) * 2);
482
+ }
483
+
484
+ .panel.bot-row {
485
+ background: var(--background-fill-secondary);
486
+ }
487
+
488
+ .panel .with_avatar {
489
+ padding-left: calc(var(--spacing-xl) * 2) !important;
490
+ padding-right: calc(var(--spacing-xl) * 2) !important;
491
+ }
492
+
493
+ .panel .panel-full-width {
494
+ width: 100%;
495
+ }
496
+
497
+ .panel .user :global(*) {
498
+ text-align: right;
499
+ }
500
+
501
+ /* message content */
502
+ .flex-wrap {
503
+ display: flex;
504
+ flex-direction: column;
505
+ max-width: 100%;
506
+ color: var(--body-text-color);
507
+ font-size: var(--chatbot-text-size);
508
+ overflow-wrap: break-word;
509
+ }
510
+
511
+ @media (max-width: 480px) {
512
+ .user-row.bubble {
513
+ align-self: flex-end;
514
+ }
515
+
516
+ .bot-row.bubble {
517
+ align-self: flex-start;
518
+ }
519
+ .message {
520
+ width: 100%;
521
+ }
522
+ }
523
+
524
+ .avatar-container {
525
+ align-self: flex-start;
526
+ position: relative;
527
+ display: flex;
528
+ justify-content: flex-start;
529
+ align-items: flex-start;
530
+ width: 35px;
531
+ height: 35px;
532
+ flex-shrink: 0;
533
+ bottom: 0;
534
+ border-radius: 50%;
535
+ border: 1px solid var(--border-color-primary);
536
+ }
537
+ .user-row > .avatar-container {
538
+ order: 2;
539
+ }
540
+
541
+ .user-row.bubble > .avatar-container {
542
+ margin-left: var(--spacing-xxl);
543
+ }
544
+
545
+ .bot-row.bubble > .avatar-container {
546
+ margin-right: var(--spacing-xxl);
547
+ margin-left: 0;
548
+ }
549
+
550
+ .panel.user-row > .avatar-container {
551
+ order: 0;
552
+ }
553
+
554
+ .avatar-container:not(.thumbnail-item) :global(img) {
555
+ width: 100%;
556
+ height: 100%;
557
+ object-fit: cover;
558
+ border-radius: 50%;
559
+ padding: var(--size-1-5);
560
+ }
561
+
562
+ .selectable {
563
+ cursor: pointer;
564
+ }
565
+
566
+ @keyframes dot-flashing {
567
+ 0% {
568
+ opacity: 0.8;
569
+ }
570
+ 50% {
571
+ opacity: 0.5;
572
+ }
573
+ 100% {
574
+ opacity: 0.8;
575
+ }
576
+ }
577
+
578
+ /* Image preview */
579
+ .message :global(.preview) {
580
+ object-fit: contain;
581
+ width: 95%;
582
+ max-height: 93%;
583
+ }
584
+ .image-preview {
585
+ position: absolute;
586
+ z-index: 999;
587
+ left: 0;
588
+ top: 0;
589
+ width: 100%;
590
+ height: 100%;
591
+ overflow: auto;
592
+ background-color: rgba(0, 0, 0, 0.9);
593
+ display: flex;
594
+ justify-content: center;
595
+ align-items: center;
596
+ }
597
+ .image-preview :global(svg) {
598
+ stroke: white;
599
+ }
600
+ .image-preview-close-button {
601
+ position: absolute;
602
+ top: 10px;
603
+ right: 10px;
604
+ background: none;
605
+ border: none;
606
+ font-size: 1.5em;
607
+ cursor: pointer;
608
+ height: 30px;
609
+ width: 30px;
610
+ padding: 3px;
611
+ background: var(--bg-color);
612
+ box-shadow: var(--shadow-drop);
613
+ border: 1px solid var(--button-secondary-border-color);
614
+ border-radius: var(--radius-lg);
615
+ }
616
+
617
+ .message > div {
618
+ width: 100%;
619
+ }
620
+ .html {
621
+ padding: 0;
622
+ border: none;
623
+ background: none;
624
+ }
625
+
626
+ .panel .bot,
627
+ .panel .user {
628
+ border: none;
629
+ box-shadow: none;
630
+ background-color: var(--background-fill-secondary);
631
+ }
632
+
633
+ textarea {
634
+ background: none;
635
+ border-radius: var(--radius-lg);
636
+ border: none;
637
+ display: block;
638
+ max-width: 100%;
639
+ }
640
+ .user textarea {
641
+ border-bottom-right-radius: 0;
642
+ }
643
+ .bot textarea {
644
+ border-bottom-left-radius: 0;
645
+ }
646
+ .user textarea:focus {
647
+ outline: 2px solid var(--border-color-accent);
648
+ }
649
+ .bot textarea:focus {
650
+ outline: 2px solid var(--border-color-primary);
651
+ }
652
+
653
+ .panel.user-row {
654
+ background-color: var(--color-accent-soft);
655
+ }
656
+
657
+ .panel .user-row,
658
+ .panel .bot-row {
659
+ align-self: flex-start;
660
+ }
661
+
662
+ .panel .user :global(*),
663
+ .panel .bot :global(*) {
664
+ text-align: inherit;
665
+ }
666
+
667
+ .panel .user {
668
+ background-color: var(--color-accent-soft);
669
+ }
670
+
671
+ .panel .user-row {
672
+ background-color: var(--color-accent-soft);
673
+ align-self: start;
674
+ }
675
+
676
+ .panel .message {
677
+ margin-bottom: var(--spacing-md);
678
+ }
679
+ </style>
6.20.0/chatbot/shared/MessageContent.svelte ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, FileData } from "@gradio/client";
8
+ import type { ComponentType, SvelteComponent } from "svelte";
9
+
10
+ let {
11
+ latex_delimiters,
12
+ sanitize_html,
13
+ _fetch,
14
+ i18n,
15
+ line_breaks,
16
+ upload,
17
+ target,
18
+ theme_mode,
19
+ _components,
20
+ render_markdown,
21
+ scroll,
22
+ allow_file_downloads,
23
+ display_consecutive_in_same_bubble,
24
+ thought_index,
25
+ allow_tags = false,
26
+ message
27
+ }: {
28
+ latex_delimiters: {
29
+ left: string;
30
+ right: string;
31
+ display: boolean;
32
+ }[];
33
+ sanitize_html: boolean;
34
+ _fetch: typeof fetch;
35
+ i18n: I18nFormatter;
36
+ line_breaks: boolean;
37
+ upload: Client["upload"];
38
+ target: HTMLElement | null;
39
+ theme_mode: "light" | "dark" | "system";
40
+ _components: Record<string, ComponentType<SvelteComponent>>;
41
+ render_markdown: boolean;
42
+ scroll: () => void;
43
+ allow_file_downloads: boolean;
44
+ display_consecutive_in_same_bubble: boolean;
45
+ thought_index: number;
46
+ allow_tags?: string[] | boolean;
47
+ message: NormalisedMessage;
48
+ } = $props();
49
+ </script>
50
+
51
+ {#if message.type === "text"}
52
+ <div class="message-content">
53
+ <Markdown
54
+ message={message.content}
55
+ {latex_delimiters}
56
+ {sanitize_html}
57
+ {render_markdown}
58
+ {line_breaks}
59
+ onload={scroll}
60
+ {allow_tags}
61
+ {theme_mode}
62
+ />
63
+ </div>
64
+ {:else if message.type === "component" && message.content.component in _components}
65
+ <Component
66
+ {target}
67
+ {theme_mode}
68
+ props={message.content.props}
69
+ type={message.content.component}
70
+ components={_components}
71
+ value={message.content.value}
72
+ display_icon_button_wrapper_top_corner={thought_index > 0 &&
73
+ display_consecutive_in_same_bubble}
74
+ {i18n}
75
+ {upload}
76
+ {_fetch}
77
+ onload={scroll}
78
+ {allow_file_downloads}
79
+ />
80
+ {:else if message.type === "component" && message.content.component === "file"}
81
+ {#each message.content.value as file_}
82
+ {@const file: FileData = file_ as FileData}
83
+ <div class="file-container">
84
+ <div class="file-icon">
85
+ <File />
86
+ </div>
87
+ <div class="file-info">
88
+ <a
89
+ data-testid="chatbot-file"
90
+ class="file-link"
91
+ href={file.url}
92
+ target="_blank"
93
+ download={window.__is_colab__
94
+ ? null
95
+ : file?.orig_name || file?.path.split("/").pop() || "file"}
96
+ >
97
+ <span class="file-name"
98
+ >{file?.orig_name || file?.path.split("/").pop() || "file"}</span
99
+ >
100
+ </a>
101
+ <span class="file-type"
102
+ >{(file.orig_name || file.path || "")
103
+ .split(".")
104
+ .pop()
105
+ ?.toUpperCase() ?? "FILE"}</span
106
+ >
107
+ </div>
108
+ </div>
109
+ {/each}
110
+ {/if}
111
+
112
+ <style>
113
+ .file-container {
114
+ display: flex;
115
+ align-items: center;
116
+ gap: var(--spacing-lg);
117
+ padding: var(--spacing-lg);
118
+ border-radius: var(--radius-lg);
119
+ width: fit-content;
120
+ margin: var(--spacing-sm) 0;
121
+ }
122
+
123
+ .file-icon {
124
+ display: flex;
125
+ align-items: center;
126
+ justify-content: center;
127
+ color: var(--body-text-color);
128
+ }
129
+
130
+ .file-icon :global(svg) {
131
+ width: var(--size-7);
132
+ height: var(--size-7);
133
+ }
134
+
135
+ .file-info {
136
+ display: flex;
137
+ flex-direction: column;
138
+ }
139
+
140
+ .file-link {
141
+ text-decoration: none;
142
+ color: var(--body-text-color);
143
+ display: flex;
144
+ flex-direction: column;
145
+ gap: var(--spacing-xs);
146
+ }
147
+
148
+ .file-name {
149
+ font-family: var(--font);
150
+ font-size: var(--text-md);
151
+ font-weight: 500;
152
+ }
153
+
154
+ .file-type {
155
+ font-family: var(--font);
156
+ font-size: var(--text-sm);
157
+ color: var(--body-text-color-subdued);
158
+ text-transform: uppercase;
159
+ }
160
+ </style>
6.20.0/chatbot/shared/Pending.svelte ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Image } from "@gradio/image/shared";
3
+ import type { FileData } from "@gradio/client";
4
+
5
+ let {
6
+ layout = "bubble",
7
+ avatar_images = [null, null] as [FileData | null, FileData | null]
8
+ }: {
9
+ layout?: "bubble" | "panel";
10
+ avatar_images?: [FileData | null, FileData | null];
11
+ } = $props();
12
+ </script>
13
+
14
+ <div class="container">
15
+ {#if avatar_images[1] !== null}
16
+ <div class="avatar-container">
17
+ <Image class="avatar-image" src={avatar_images[1].url} alt="bot avatar" />
18
+ </div>
19
+ {/if}
20
+
21
+ <div
22
+ class="message bot pending {layout}"
23
+ class:with_avatar={avatar_images[1] !== null}
24
+ class:with_opposite_avatar={avatar_images[0] !== null}
25
+ role="status"
26
+ aria-label="Loading response"
27
+ aria-live="polite"
28
+ >
29
+ <div class="message-content">
30
+ <span class="sr-only">Loading content</span>
31
+ <div class="dots">
32
+ <div class="dot" />
33
+ <div class="dot" />
34
+ <div class="dot" />
35
+ </div>
36
+ </div>
37
+ </div>
38
+ </div>
39
+
40
+ <style>
41
+ .container {
42
+ display: flex;
43
+ margin: calc(var(--spacing-xl) * 2);
44
+ }
45
+
46
+ .bubble.pending {
47
+ border-width: 1px;
48
+ border-radius: var(--radius-lg);
49
+ border-bottom-left-radius: 0;
50
+ border-color: var(--border-color-primary);
51
+ background-color: var(--background-fill-secondary);
52
+ box-shadow: var(--shadow-drop);
53
+ align-self: flex-start;
54
+ width: fit-content;
55
+ margin-bottom: var(--spacing-xl);
56
+ }
57
+
58
+ .bubble.with_opposite_avatar {
59
+ margin-right: calc(var(--spacing-xxl) + 35px + var(--spacing-xxl));
60
+ }
61
+
62
+ .panel.pending {
63
+ margin: 0;
64
+ padding: calc(var(--spacing-lg) * 2) calc(var(--spacing-lg) * 2);
65
+ width: 100%;
66
+ border: none;
67
+ background: none;
68
+ box-shadow: none;
69
+ border-radius: 0;
70
+ }
71
+
72
+ .panel.with_avatar {
73
+ padding-left: calc(var(--spacing-xl) * 2) !important;
74
+ padding-right: calc(var(--spacing-xl) * 2) !important;
75
+ }
76
+
77
+ .avatar-container {
78
+ align-self: flex-start;
79
+ position: relative;
80
+ display: flex;
81
+ justify-content: flex-start;
82
+ align-items: flex-start;
83
+ width: 35px;
84
+ height: 35px;
85
+ flex-shrink: 0;
86
+ bottom: 0;
87
+ border-radius: 50%;
88
+ border: 1px solid var(--border-color-primary);
89
+ margin-right: var(--spacing-xxl);
90
+ }
91
+
92
+ .avatar-container:not(.thumbnail-item) :global(img) {
93
+ width: 100%;
94
+ height: 100%;
95
+ object-fit: cover;
96
+ border-radius: 50%;
97
+ padding: var(--size-1-5);
98
+ }
99
+
100
+ .message-content {
101
+ padding: var(--spacing-sm) var(--spacing-xl);
102
+ min-height: var(--size-8);
103
+ display: flex;
104
+ align-items: center;
105
+ }
106
+
107
+ .dots {
108
+ display: flex;
109
+ gap: var(--spacing-xs);
110
+ align-items: center;
111
+ }
112
+
113
+ .dot {
114
+ width: var(--size-1-5);
115
+ height: var(--size-1-5);
116
+ margin-right: var(--spacing-xs);
117
+ border-radius: 50%;
118
+ background-color: var(--body-text-color);
119
+ opacity: 0.5;
120
+ animation: pulse 1.5s infinite;
121
+ }
122
+
123
+ .dot:nth-child(2) {
124
+ animation-delay: 0.2s;
125
+ }
126
+
127
+ .dot:nth-child(3) {
128
+ animation-delay: 0.4s;
129
+ }
130
+
131
+ @keyframes pulse {
132
+ 0%,
133
+ 100% {
134
+ opacity: 0.4;
135
+ transform: scale(1);
136
+ }
137
+ 50% {
138
+ opacity: 1;
139
+ transform: scale(1.1);
140
+ }
141
+ }
142
+ </style>
6.20.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.20.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.20.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.20.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.20.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.20.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.20.0/chatbot/shared/autorender.d.ts ADDED
@@ -0,0 +1 @@
 
 
1
+ declare module "katex/dist/contrib/auto-render.js";
6.20.0/chatbot/shared/utils.ts ADDED
@@ -0,0 +1,372 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ const component = get_component_for_mime_type(_file?.mime_type, _file);
150
+ // Ensure that value is always an array for files
151
+ return {
152
+ component: component,
153
+ value:
154
+ component === "file"
155
+ ? Array.isArray(message.file)
156
+ ? message.file
157
+ : [message.file]
158
+ : message.file,
159
+ alt_text: message.alt_text,
160
+ constructor_args: {},
161
+ props: {}
162
+ } as ComponentData;
163
+ }
164
+
165
+ function normalise_message(
166
+ message: Message,
167
+ content: Text | File | Component,
168
+ root: string,
169
+ i: number
170
+ ): NormalisedMessage {
171
+ let normalized: NormalisedMessage;
172
+ if (content.type === "text") {
173
+ normalized = {
174
+ role: message.role,
175
+ metadata: message.metadata,
176
+ content: redirect_src_url(content.text, root),
177
+ type: "text",
178
+ index: i,
179
+ options: message.options
180
+ };
181
+ } else if (content.type === "file") {
182
+ normalized = {
183
+ role: message.role,
184
+ metadata: message.metadata,
185
+ content: convert_file_message_to_component_message(content),
186
+ type: "component",
187
+ index: i,
188
+ options: message.options
189
+ };
190
+ } else {
191
+ normalized = {
192
+ role: message.role,
193
+ metadata: message.metadata,
194
+ content: content,
195
+ type: "component",
196
+ index: i,
197
+ options: message.options
198
+ };
199
+ }
200
+ return normalized;
201
+ }
202
+
203
+ export function normalise_messages(
204
+ messages: Message[] | null,
205
+ root: string
206
+ ): NormalisedMessage[] | null {
207
+ if (messages === null) return messages;
208
+
209
+ const thought_map = new Map<string, ThoughtNode>();
210
+
211
+ return messages
212
+ .flatMap((message, i) => {
213
+ const normalized: NormalisedMessage[] = message.content.map((content) =>
214
+ normalise_message(message, content, root, i)
215
+ );
216
+ for (const msg of normalized) {
217
+ const { id, title, parent_id } = message.metadata || {};
218
+ if (parent_id) {
219
+ const parent = thought_map.get(String(parent_id));
220
+ if (parent) {
221
+ const thought = { ...msg, children: [] } as ThoughtNode;
222
+ parent.children.push(thought);
223
+ if (id && title) {
224
+ thought_map.set(String(id), thought);
225
+ }
226
+ return null;
227
+ }
228
+ }
229
+ if (id && title) {
230
+ const thought = { ...msg, children: [] } as ThoughtNode;
231
+ thought_map.set(String(id), thought);
232
+ return thought;
233
+ }
234
+ }
235
+ return normalized;
236
+ })
237
+ .filter((msg): msg is NormalisedMessage => msg !== null);
238
+ }
239
+
240
+ export function is_component_message(
241
+ message: NormalisedMessage
242
+ ): message is ComponentMessage {
243
+ return message.type === "component";
244
+ }
245
+
246
+ export function is_last_bot_message(
247
+ messages: NormalisedMessage[],
248
+ all_messages: NormalisedMessage[]
249
+ ): boolean {
250
+ const is_bot = messages[messages.length - 1].role === "assistant";
251
+ const last_index = messages[messages.length - 1].index;
252
+ // use JSON.stringify to handle both the number and tuple cases
253
+ // when msg_format is tuples, last_index is an array and when it is messages, it is a number
254
+ const is_last =
255
+ JSON.stringify(last_index) ===
256
+ JSON.stringify(all_messages[all_messages.length - 1].index);
257
+ return is_last && is_bot;
258
+ }
259
+
260
+ export function group_messages(
261
+ messages: NormalisedMessage[],
262
+ display_consecutive_in_same_bubble = true
263
+ ): NormalisedMessage[][] {
264
+ const groupedMessages: NormalisedMessage[][] = [];
265
+ let currentGroup: NormalisedMessage[] = [];
266
+ let currentRole: MessageRole | null = null;
267
+
268
+ for (const message of messages) {
269
+ if (!(message.role === "assistant" || message.role === "user")) {
270
+ continue;
271
+ }
272
+
273
+ // If display_consecutive_in_same_bubble is false, each message should be its own group
274
+ if (!display_consecutive_in_same_bubble) {
275
+ groupedMessages.push([message]);
276
+ continue;
277
+ }
278
+
279
+ if (message.role === currentRole) {
280
+ currentGroup.push(message);
281
+ } else {
282
+ if (currentGroup.length > 0) {
283
+ groupedMessages.push(currentGroup);
284
+ }
285
+ currentGroup = [message];
286
+ currentRole = message.role;
287
+ }
288
+ }
289
+
290
+ if (currentGroup.length > 0) {
291
+ groupedMessages.push(currentGroup);
292
+ }
293
+
294
+ return groupedMessages;
295
+ }
296
+
297
+ export async function load_components(
298
+ component_names: string[],
299
+ _components: Record<string, ComponentType<SvelteComponent>>,
300
+ load_component: Gradio["load_component"]
301
+ ): Promise<Record<string, ComponentType<SvelteComponent>>> {
302
+ for (const component_name of component_names) {
303
+ if (_components[component_name] || component_name === "file") {
304
+ continue;
305
+ }
306
+ const variant = component_name === "dataframe" ? "component" : "base";
307
+ const { component } = load_component(component_name, variant);
308
+ const comp = await component;
309
+ // @ts-ignore
310
+ _components[component_name] = comp.default;
311
+ }
312
+ return _components;
313
+ }
314
+
315
+ export function get_components_from_messages(
316
+ messages: NormalisedMessage[] | null
317
+ ): string[] {
318
+ if (!messages) return [];
319
+ let components: Set<string> = new Set();
320
+ messages.forEach((message) => {
321
+ if (message.type === "component") {
322
+ components.add(message.content.component);
323
+ }
324
+ });
325
+ return Array.from(components);
326
+ }
327
+
328
+ export function get_thought_content(msg: NormalisedMessage, depth = 0): string {
329
+ let content = "";
330
+ const indent = " ".repeat(depth);
331
+
332
+ if (msg.metadata?.title) {
333
+ content += `${indent}${depth > 0 ? "- " : ""}${msg.metadata.title}\n`;
334
+ }
335
+ if (typeof msg.content === "string") {
336
+ content += `${indent} ${msg.content}\n`;
337
+ }
338
+ const thought = msg as ThoughtNode;
339
+ if (thought.children?.length > 0) {
340
+ content += thought.children
341
+ .map((child) => get_thought_content(child, depth + 1))
342
+ .join("");
343
+ }
344
+ return content;
345
+ }
346
+
347
+ export function all_text(message: TextMessage[] | TextMessage): string {
348
+ if (Array.isArray(message)) {
349
+ return message
350
+ .map((m) => {
351
+ if (m.metadata?.title) {
352
+ return get_thought_content(m);
353
+ }
354
+ return m.content;
355
+ })
356
+ .join("\n");
357
+ }
358
+ if (message.metadata?.title) {
359
+ return get_thought_content(message);
360
+ }
361
+ return message.content;
362
+ }
363
+
364
+ export function is_all_text(
365
+ message: NormalisedMessage[] | NormalisedMessage
366
+ ): message is TextMessage[] | TextMessage {
367
+ return (
368
+ (Array.isArray(message) &&
369
+ message.every((m) => typeof m.content === "string")) ||
370
+ (!Array.isArray(message) && typeof message.content === "string")
371
+ );
372
+ }
6.20.0/chatbot/types.ts ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { FileData } from "@gradio/client";
2
+ import type { UndoRetryData } from "./shared/utils";
3
+ import type {
4
+ Gradio,
5
+ SelectData,
6
+ LikeData,
7
+ CopyData,
8
+ CustomButton
9
+ } from "@gradio/utils";
10
+ import type { ILoadingStatus as LoadingStatus } from "@gradio/statustracker";
11
+
12
+ export type MessageRole = "system" | "user" | "assistant";
13
+
14
+ export interface Metadata {
15
+ title: string | null;
16
+ id?: number | string | null;
17
+ parent_id?: number | string | null;
18
+ duration?: number;
19
+ log?: string;
20
+ status?: "pending" | "done" | null;
21
+ }
22
+
23
+ export interface ComponentData {
24
+ component: string;
25
+ constructor_args: any;
26
+ props: any;
27
+ value: any;
28
+ alt_text: string | null;
29
+ }
30
+
31
+ export interface Option {
32
+ label?: string;
33
+ value: string;
34
+ }
35
+
36
+ export interface Text {
37
+ type: "text";
38
+ text: string;
39
+ }
40
+
41
+ export interface Component {
42
+ type: "component";
43
+ component: string;
44
+ constructor_args: object;
45
+ props: object;
46
+ value: any;
47
+ alt_text: string | null;
48
+ }
49
+
50
+ export interface File {
51
+ type: "file";
52
+ file: FileData;
53
+ alt_text: string | null;
54
+ }
55
+
56
+ export interface Message {
57
+ role: MessageRole;
58
+ metadata: Metadata;
59
+ content: (Text | File | Component)[];
60
+ index: number | [number, number];
61
+ options?: Option[];
62
+ }
63
+
64
+ export interface TextMessage {
65
+ type: "text";
66
+ content: string;
67
+ index: number | [number, number];
68
+ options?: Option[];
69
+ role: MessageRole;
70
+ metadata: Metadata;
71
+ }
72
+
73
+ export interface ComponentMessage {
74
+ type: "component";
75
+ content: ComponentData;
76
+ index: number | [number, number];
77
+ options?: Option[];
78
+ role: MessageRole;
79
+ metadata: Metadata;
80
+ }
81
+
82
+ export interface ExampleMessage {
83
+ icon?: FileData;
84
+ display_text?: string;
85
+ text: string;
86
+ files?: FileData[];
87
+ }
88
+
89
+ export type message_data =
90
+ | string
91
+ | { file: FileData | FileData[]; alt_text: string | null }
92
+ | { component: string; value: any; constructor_args: any; props: any }
93
+ | null;
94
+
95
+ export type NormalisedMessage = TextMessage | ComponentMessage;
96
+
97
+ export type ThoughtNode = NormalisedMessage & { children: ThoughtNode[] };
98
+
99
+ export interface ChatbotEvents {
100
+ change: Message[];
101
+ select: SelectData;
102
+ share: ShareData;
103
+ error: string;
104
+ like: LikeData;
105
+ clear_status: LoadingStatus;
106
+ example_select: SelectData;
107
+ option_select: SelectData;
108
+ edit: SelectData;
109
+ retry: UndoRetryData;
110
+ undo: UndoRetryData;
111
+ clear: null;
112
+ copy: CopyData;
113
+ custom_button_click: { id: number };
114
+ }
115
+
116
+ export interface ChatbotProps {
117
+ autoscroll: boolean;
118
+ messages: Message[];
119
+ examples: ExampleMessage[] | null;
120
+ allow_undo: boolean;
121
+ allow_retry: boolean;
122
+ root: string;
123
+ proxy_url: null | string;
124
+ max_messages: number | null;
125
+ avatar_images: [FileData | null, FileData | null];
126
+ like_user_message: boolean;
127
+ height: number | string | undefined;
128
+ resizable: boolean;
129
+ min_height: number | string | undefined;
130
+ max_height: number | string | undefined;
131
+ editable: "user" | "all" | null;
132
+ placeholder: string | null;
133
+ allow_file_downloads: boolean;
134
+ watermark: string | null;
135
+ value: Message[];
136
+ _selectable: boolean;
137
+ likeable: boolean;
138
+ feedback_options: string[];
139
+ feedback_value: (string | null)[] | null;
140
+ buttons: (string | CustomButton)[] | null;
141
+ rtl: boolean;
142
+ sanitize_html: boolean;
143
+ layout: "bubble" | "panel";
144
+ type: "tuples" | "messages";
145
+ render_markdown: boolean;
146
+ line_breaks: boolean;
147
+ group_consecutive_messages: boolean;
148
+ allow_tags: string[] | boolean;
149
+ latex_delimiters: {
150
+ left: string;
151
+ right: string;
152
+ display: boolean;
153
+ }[];
154
+ _retryable: boolean;
155
+ _undoable: boolean;
156
+ }