Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

feat(embed): support pasting screenshots into chat

hahahahahayesyeseysd8fa5b49a7f4
diff --git a/frontend/src/components/EmbedInputField.test.ts b/frontend/src/components/EmbedInputField.test.ts
new file mode 100644
index 000000000..2257f7d0f
--- /dev/null
+++ b/frontend/src/components/EmbedInputField.test.ts
@@ -0,0 +1,119 @@
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import { createRequire } from 'node:module'
+import { fileURLToPath } from 'node:url'
+import { runInNewContext } from 'node:vm'
+import test from 'node:test'
+import { compileScript, parse } from '@vue/compiler-sfc'
+import ts from 'typescript'
+import { createRenderer, defineComponent, h, nextTick } from 'vue'
+
+const require = createRequire(import.meta.url)
+const filename = fileURLToPath(new URL('./EmbedInputField.vue', import.meta.url))
+const { descriptor } = parse(readFileSync(filename, 'utf8'), { filename })
+const compiled = ts.transpileModule(compileScript(descriptor, {
+ id: 'embed-input', inlineTemplate: true,
+}).content, {
+ compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
+}).outputText
+
+type Node = { tag: string; props: Record<string, any>; children: Node[]; parent?: Node; querySelector: () => null }
+const node = (tag: string): Node => ({ tag, props: {}, children: [], querySelector: () => null })
+
+async function fixture(showFileUploadToggle = true) {
+ const sent: any[][] = [], alerts: string[] = [], revoked: string[] = []
+ const exports: any = {}
+ runInNewContext(compiled, {
+ exports, HTMLTextAreaElement: class {},
+ URL: { createObjectURL: URL.createObjectURL, revokeObjectURL(url: string) { revoked.push(url); URL.revokeObjectURL(url) } },
+ require(name: string) {
+ if (name === 'vue') return require('vue')
+ if (name === 'vue-i18n') return { useI18n: () => ({ t: (key: string) => key }) }
+ if (name === '@/utils/embedToast') return { embedToast: (message: string) => alerts.push(message) }
+ if (name === '@/utils/embedFile') return require('../utils/embedFile.ts')
+ if (name.endsWith('.svg')) return 'send.svg'
+ throw new Error(`Unexpected import: ${name}`)
+ },
+ })
+ const renderer = createRenderer<Node, Node>({
+ createElement: node, createText: () => node('#text'), createComment: () => node('#comment'),
+ insert(child, parent) { child.parent = parent; parent.children.push(child) },
+ remove(child) { const siblings = child.parent?.children; if (siblings) siblings.splice(siblings.indexOf(child), 1) },
+ setElementText() {}, setText() {}, patchProp(el, key, _old, value) { el.props[key] = value },
+ parentNode: el => el.parent || null, nextSibling: () => null,
+ })
+ const app = renderer.createApp(exports.default, {
+ isReplying: false, showFileUploadToggle,
+ onSendMsg: (...args: any[]) => sent.push(args),
+ })
+ app.component('t-textarea', defineComponent({ props: ['modelValue'], setup: (props, { attrs }) => () => h('textarea', { ...attrs, value: props.modelValue }) }))
+ app.component('t-icon', { render: () => null })
+ app.component('t-tooltip', defineComponent({ props: ['placement', 'content'], setup: (_props, { slots }) => () => slots.default?.() }))
+ const root = node('root')
+ app.mount(root)
+ await nextTick(); await nextTick()
+ const all = (el: Node = root): Node[] => [el, ...el.children.flatMap(child => all(child))]
+ const images = () => all().filter(el => el.tag === 'img' && el.props.src?.startsWith('blob:'))
+ const click = async (cssClass: string) => {
+ const button = all().find(el => el.tag === 'button' && String(el.props.class).split(' ').includes(cssClass))
+ assert.ok(button, `Missing button: ${cssClass}`)
+ button.props.onClick(); await nextTick()
+ }
+ const paste = async (files: File[] = [], text = '') => {
+ let prevented = false
+ const event = { clipboardData: { items: [
+ ...files.map(file => ({ type: file.type, getAsFile: () => file })),
+ ...(text ? [{ type: 'text/plain', getAsFile: () => null }] : []),
+ ] }, preventDefault() { prevented = true } }
+ all().find(el => el.tag === 'textarea')?.props.onPaste?.(event)
+ await nextTick()
+ return prevented
+ }
+ return { sent, alerts, revoked, images, click, paste, dispose: () => app.unmount() }
+}
+const file = (type = 'image/png', size = 10) => new File([new Uint8Array(size)], 'screenshot', { type })
+
+test('pasted screenshot is previewed, sent without text, and its preview is released', async () => {
+ const f = await fixture()
+ try {
+ const screenshot = file()
+ await f.paste([screenshot])
+ assert.equal(f.images().length, 1)
+ await f.click('embed-send-btn')
+ assert.equal(f.sent.length, 1)
+ assert.equal(f.sent[0][0], '')
+ assert.equal(f.sent[0][1][0], screenshot)
+ assert.equal(f.sent[0][2].length, 0)
+ assert.equal(f.images().length, 0)
+ assert.equal(f.revoked.length, 1)
+ } finally { f.dispose() }
+})
+
+test('disabled uploads and text-only paste retain native paste behavior', async () => {
+ for (const enabled of [false, true]) {
+ const f = await fixture(enabled)
+ try {
+ assert.equal(await f.paste([], 'hello'), false)
+ if (!enabled) assert.equal(await f.paste([file()], 'hello'), false)
+ assert.equal(f.images().length, 0)
+ assert.equal(f.alerts.length, 0)
+ } finally { f.dispose() }
+ }
+})
+
+test('pasted images use the existing type, size and count limits', async () => {
+ const f = await fixture()
+ try {
+ await f.paste([file('image/svg+xml'), file('image/png', 10 * 1024 * 1024 + 1)])
+ assert.equal(f.images().length, 0)
+ assert.deepEqual(f.alerts, ['chat.imageTypeSizeError', 'chat.imageTypeSizeError'])
+ await f.paste([file('image/png', 10 * 1024 * 1024), file('image/jpeg'), file('image/gif'), file('image/webp'), file(), file()])
+ assert.equal(f.images().length, 5)
+ assert.equal(f.alerts.at(-1), 'chat.imageTooMany')
+ await f.click('embed-image-thumb__remove')
+ assert.equal(f.images().length, 4)
+ await f.paste([file()])
+ assert.equal(f.images().length, 5)
+ } finally { f.dispose() }
+ assert.equal(f.revoked.length, 6)
+})
diff --git a/frontend/src/components/EmbedInputField.vue b/frontend/src/components/EmbedInputField.vue
index cd7bde751..844802e47 100644
--- a/frontend/src/components/EmbedInputField.vue
+++ b/frontend/src/components/EmbedInputField.vue
@@ -26,6 +26,7 @@
:placeholder="t('input.placeholder')"
:autosize="{ minRows: 2, maxRows: 6 }"
@keydown="onKeydown"
+ @paste="onPaste"
@compositionstart="isComposing = true"
@compositionend="isComposing = false"
/>
@@ -201,6 +202,23 @@ const handleImageSelect = (event: Event) => {
input.value = ''
}
+const onPaste = (event: ClipboardEvent) => {
+ if (!props.showFileUploadToggle) return
+ const items = event.clipboardData?.items
+ if (!items) return
+ const imageFiles: File[] = []
+ for (const item of items) {
+ if (item.type.startsWith('image/')) {
+ const file = item.getAsFile()
+ if (file) imageFiles.push(file)
+ }
+ }
+ if (imageFiles.length > 0) {
+ event.preventDefault()
+ addImageFiles(imageFiles)
+ }
+}
+
const addAttachmentFiles = (files: File[]) => {
const maxSize = 20 * 1024 * 1024
for (const file of files) {
diff --git a/website-docs/03-features/13-embed-channel.md b/website-docs/03-features/13-embed-channel.md
index 2aa8dfe9a..286671c0b 100644
--- a/website-docs/03-features/13-embed-channel.md
+++ b/website-docs/03-features/13-embed-channel.md
@@ -218,6 +218,8 @@ type EmbedChannel struct {
| `webhook_url` | string | 空 | 事件回调地址,**必须为 HTTPS 且通过 SSRF 校验**(禁止内网/链路本地地址) |
| `webhook_secret` | string | 空 | webhook 签名密钥(API 响应中永不回显) |
+渠道和绑定 Agent 均开启图片上传后,访客可在聊天输入框中粘贴截图,确认预览后发送,也可移除预览中的图片。粘贴与文件选择共用限制:JPEG、PNG、GIF、WebP,每张不超过 10 MB,每次最多 5 张。纯文本粘贴保持原有行为;剪贴板同时包含图片和文本时,按主聊天的方式仅添加图片。
+
### 管理 API(需登录鉴权)
由 `RegisterEmbedChannelRoutes`(`internal/router/routes_agent.go`)注册,支持 API Key 的 `ManageChannels` 能力: