@yuriai/web 0.2.27-beta.8 → 0.2.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{AgentAvatar-BJ42lGZD.js → AgentAvatar-DNm_jMna.js} +1 -1
- package/dist/assets/AssistantContactStatus-BPRpgMFh.js +3 -0
- package/dist/assets/{AssistantUnreadBadge-QwhBfXpC.js → AssistantUnreadBadge-BssH-RvV.js} +1 -1
- package/dist/assets/{AssistantView-D7ryIKL0.js → AssistantView-CxQv-9Ms.js} +1 -1
- package/dist/assets/{DiagramView-ERrGEBDp.js → DiagramView-DPpG0fwx.js} +1 -1
- package/dist/assets/{FadeInOnReveal-Bu2GvaM9.js → FadeInOnReveal-S6UbimjB.js} +1 -1
- package/dist/assets/{GuiPluginSurface-MzQgEZNr.js → GuiPluginSurface-BOf0JA1T.js} +1 -1
- package/dist/assets/{GuiPluginWindow-DZMmlQzx.js → GuiPluginWindow-OrYkfQ4m.js} +1 -1
- package/dist/assets/{Icon-Dj4CCqCP.js → Icon-BE4mEwzV.js} +1 -1
- package/dist/assets/{ImageLoadingPlaceholder-BnFXgUuO.js → ImageLoadingPlaceholder-CBFJC_KD.js} +1 -1
- package/dist/assets/{LoadingImage-PBnivDKv.js → LoadingImage-NLq7_P-i.js} +1 -1
- package/dist/assets/MarkdownRenderer-BaMotAOQ.js +2 -0
- package/dist/assets/{MarkdownRendererImpl-CfyXL4rg.js → MarkdownRendererImpl-Cv6UnGG7.js} +1 -1
- package/dist/assets/{MarkstreamRendererImpl-BGadklK1.js → MarkstreamRendererImpl-Rb9O6wkW.js} +1 -1
- package/dist/assets/{MobileDetailNavigation-DqajQdB4.js → MobileDetailNavigation-CK4KbkeS.js} +1 -1
- package/dist/assets/{MobileResizableSheet-BvdUgwA3.js → MobileResizableSheet-Deidw1br.js} +1 -1
- package/dist/assets/{MobileShareBridge-3oPoYnry.js → MobileShareBridge-CO_OWEfq.js} +2 -2
- package/dist/assets/MobileTabPageHeader-DbC8L6A4.js +2 -0
- package/dist/assets/{MultiRunWindow-yhs0Ih5_.js → MultiRunWindow-DtVW9NTV.js} +1 -1
- package/dist/assets/{OnboardingScreen-Bi1VWFQC.js → OnboardingScreen-C8ld4wO7.js} +1 -1
- package/dist/assets/{OverlayScrollbar-BBDwGvGO.js → OverlayScrollbar-B3auC2WH.js} +1 -1
- package/dist/assets/{SettingsView-BSf-SJUA.js → SettingsView-Cm7woZWl.js} +14 -14
- package/dist/assets/{SettingsWindow-BvSXVMTm.js → SettingsWindow-CdL4PFxW.js} +1 -1
- package/dist/assets/{TerminalView-DhL01N7e.js → TerminalView-CwRB6Yyr.js} +1 -1
- package/dist/assets/{ToolOutputDialog-C8lnH4Ky.js → ToolOutputDialog-B4BfmoVH.js} +1 -1
- package/dist/assets/{YuriLogo-D8asj1CS.js → YuriLogo-BOx0s0Ue.js} +1 -1
- package/dist/assets/{appThemeRegistry-C-maYSdv.js → appThemeRegistry-B6woJIX5.js} +1 -1
- package/dist/assets/{button-vXOdzd8x.js → button-BXxwtxsG.js} +1 -1
- package/dist/assets/{capgoAdapter-DJ_h2x5D.js → capgoAdapter-C5HFS-rx.js} +1 -1
- package/dist/assets/{collapsible-DwDQs3Wa.js → collapsible-BEm0787p.js} +1 -1
- package/dist/assets/{context-menu-BabBujQ-.js → context-menu-DUwvVUDY.js} +1 -1
- package/dist/assets/device-BcRP6Tjd.js +2 -0
- package/dist/assets/{diagram-HwgrQxY5.js → diagram-DyY8dxjZ.js} +1 -1
- package/dist/assets/{dialog-Dnf626tb.js → dialog-CLca6ojn.js} +1 -1
- package/dist/assets/{dropdown-menu.styles-CBc_a-Nv.js → dropdown-menu.styles-DGaALKP0.js} +1 -1
- package/dist/assets/{es-BQkO-Odr.js → es-CrsHvgue.js} +2 -2
- package/dist/assets/fileMentionAutocompleteState-BZJqfgae.js +2 -0
- package/dist/assets/{fileMentionTouchSelection-CKPnEetl.js → fileMentionTouchSelection-C4dvP8GY.js} +2 -2
- package/dist/assets/{fileReferenceActions-g7KZLi_G.js → fileReferenceActions-CrIAce3K.js} +1 -1
- package/dist/assets/{fr-C852roK5.js → fr-CEgiRlfa.js} +2 -2
- package/dist/assets/{gitApi-CVq5oKCd.js → gitApi-D2G9ny2O.js} +1 -1
- package/dist/assets/{gitApiHttp-BYOIbYIh.js → gitApiHttp-B7vYwgBh.js} +4 -4
- package/dist/assets/{gui-M_Rbwkyu.js → gui-CJjEj3l4.js} +1 -1
- package/dist/assets/host-CnVx34Dh.js +1 -0
- package/dist/assets/{input-BtM712b1.js → input-D59Q2pm8.js} +1 -1
- package/dist/assets/insertionBoundaries-CedfTSM5.js +1 -0
- package/dist/assets/{ja-BqC39qK6.js → ja-CX-_p9jZ.js} +2 -2
- package/dist/assets/{ko-BMaChAWh.js → ko-D5xlLYJM.js} +2 -2
- package/dist/assets/main-CxpUooVS.js +2 -0
- package/dist/assets/{main-Mey7bCWf.js → main-Dpm4KxWX.js} +12 -12
- package/dist/assets/{markdownHeightCache-Bf8iisO4.js → markdownHeightCache-D2VWb_4E.js} +1 -1
- package/dist/assets/{mcpQueries-DpXqegX5.js → mcpQueries-DQ3KthDF.js} +1 -1
- package/dist/assets/miniChat-CTdj8Ug9.js +2 -0
- package/dist/assets/mobile-ChbA3Q8j.js +2 -0
- package/dist/assets/{mobileBackNavigation-DZcI7aZg.js → mobileBackNavigation-BJqt3oHk.js} +1 -1
- package/dist/assets/{multirun-hzspPepd.js → multirun-BILeETbd.js} +1 -1
- package/dist/assets/{native-H7K64Ulc.js → native-BYMhr-xH.js} +1 -1
- package/dist/assets/{native-config-CddZtLQ1.js → native-config-BIY0Cr-l.js} +1 -1
- package/dist/assets/{persistence-BT8045Ue.js → persistence-CWuLIq1J.js} +2 -2
- package/dist/assets/{pl-Ubs8p28T.js → pl-BjH81A_q.js} +2 -2
- package/dist/assets/{projectMeta-CEf0QPmD.js → projectMeta-B9_Uh8YH.js} +1 -1
- package/dist/assets/{pt-BR-8rAOjJrs.js → pt-BR-Dp5oK2x3.js} +2 -2
- package/dist/assets/queue-abort-optimistic-CS5jTJq-.js +1 -0
- package/dist/assets/{readiness-Dyr7314z.js → readiness-BNIgH81X.js} +1 -1
- package/dist/assets/renderElectronMiniChatApp-asOqV7FB.js +2 -0
- package/dist/assets/renderMobileApp-DAfque7-.js +28 -0
- package/dist/assets/{runtimeConfig-B8VHHNxj.js → runtimeConfig-BX8JirIc.js} +1 -1
- package/dist/assets/runtimeEndpointReset-CFQgUiUr.js +149 -0
- package/dist/assets/{scheduledTaskNavigation-Zm87Nydi.js → scheduledTaskNavigation-EomDNVu2.js} +1 -1
- package/dist/assets/sessionLookup-BxAaxPOw.js +1 -0
- package/dist/assets/{sortable-tabs-strip-D4pVoodN.js → sortable-tabs-strip-DpbW5MqX.js} +1 -1
- package/dist/assets/{speechSettingsQueries-RlCLMBtr.js → speechSettingsQueries-CC0fusUo.js} +2 -2
- package/dist/assets/src-BMKjN2ip.css +2 -0
- package/dist/assets/{store-ME7oHni1.js → store-CkIHVrmT.js} +4 -3
- package/dist/assets/{terminalApi-BSaclMDJ.js → terminalApi-rl9mZAlB.js} +1 -1
- package/dist/assets/{textarea-B98rsLs7.js → textarea-7eTptEtD.js} +1 -1
- package/dist/assets/{tooltip-B9810jbK.js → tooltip-BPwoSTzO.js} +1 -1
- package/dist/assets/types-CUtSV1V8.js +2 -0
- package/dist/assets/{uk-LzwzUApL.js → uk-BEfkaLtM.js} +2 -2
- package/dist/assets/{useAppFontEffects-D2C1sBYM.js → useAppFontEffects-Ck2PdQRx.js} +37 -37
- package/dist/assets/{useAssistantUIStore-TUN_ycy5.js → useAssistantUIStore-CfaQV3ID.js} +1 -1
- package/dist/assets/useDesktopWindowControlsLayout-DxxOUZj1.js +1 -0
- package/dist/assets/useEffectiveDirectory-CNCQfN_x.js +1 -0
- package/dist/assets/{useFontPreferences-BlhlCJCX.js → useFontPreferences-DeqW9nRx.js} +1 -1
- package/dist/assets/useMobileNavigationStore-BmVr5a5c.js +17 -0
- package/dist/assets/{useUIStore-DRx6OgCp.js → useUIStore-C6CE42oh.js} +1 -1
- package/dist/assets/{useWorkerHighlightedLines-D093jNsH.js → useWorkerHighlightedLines-CN_RGaSw.js} +1 -1
- package/dist/assets/{vendor-effect-DV9F8PTZ.js → vendor-effect-X1Hqc7ba.js} +13 -13
- package/dist/assets/vendor-opencode-client-cQ47ywrT.js +23 -0
- package/dist/assets/{vendor-opencode-plugin-BBOP6WNx.js → vendor-opencode-plugin-BmdTE4Te.js} +1 -1
- package/dist/assets/vendor-opencode-schema-DWT45VBX.js +1 -0
- package/dist/assets/vendor-solid-js-D98iXT7C.js +1 -0
- package/dist/assets/vendor-zod-DZzbCm1s.js +55 -0
- package/dist/assets/{zh-CN-D-FVCeks.js → zh-CN-DipfA56P.js} +2 -2
- package/dist/assets/{zh-TW-B9iH_NXL.js → zh-TW-B7pXMVjt.js} +2 -2
- package/dist/index.html +12 -12
- package/dist/mini-chat.html +12 -12
- package/dist/mobile.html +12 -12
- package/package.json +1 -1
- package/server/index.js +2 -145
- package/server/lib/assistants/DOCUMENTATION.md +56 -10
- package/server/lib/assistants/cards.js +1 -1
- package/server/lib/assistants/channel-image.js +92 -0
- package/server/lib/assistants/channel-image.test.js +76 -0
- package/server/lib/assistants/channel-output-store.test.js +41 -0
- package/server/lib/assistants/channel-output.integration.test.js +114 -0
- package/server/lib/assistants/channel-output.js +90 -0
- package/server/lib/assistants/channel-output.test.js +79 -0
- package/server/lib/assistants/channels/DOCUMENTATION.md +52 -14
- package/server/lib/assistants/channels/PROTOCOLS.md +23 -1
- package/server/lib/assistants/channels/dingtalk-chat-adapter.js +19 -7
- package/server/lib/assistants/channels/dingtalk-chat-adapter.test.js +8 -0
- package/server/lib/assistants/channels/routes.js +12 -0
- package/server/lib/assistants/channels/routes.test.js +34 -0
- package/server/lib/assistants/channels/runtime.js +117 -26
- package/server/lib/assistants/channels/runtime.test.js +179 -5
- package/server/lib/assistants/channels/sdk-adapters.js +15 -3
- package/server/lib/assistants/channels/sdk-adapters.test.js +17 -0
- package/server/lib/assistants/channels/store.js +9 -6
- package/server/lib/assistants/channels/store.test.js +21 -11
- package/server/lib/assistants/channels/transport.js +2 -1
- package/server/lib/assistants/channels/wecom-chat-adapter.js +16 -2
- package/server/lib/assistants/channels/wecom-chat-adapter.test.js +21 -0
- package/server/lib/assistants/contact-store.js +9 -3
- package/server/lib/assistants/contact-store.test.js +27 -0
- package/server/lib/assistants/contact-tools.js +65 -75
- package/server/lib/assistants/contact-tools.test.js +114 -22
- package/server/lib/assistants/harness.js +79 -34
- package/server/lib/assistants/harness.test.js +52 -20
- package/server/lib/assistants/resource-reference.integration.test.js +284 -0
- package/server/lib/assistants/routes.js +0 -3
- package/server/lib/assistants/service.js +116 -69
- package/server/lib/assistants/service.test.js +31 -17
- package/server/lib/assistants/weixin/DOCUMENTATION.md +34 -9
- package/server/lib/assistants/weixin/api.js +43 -1
- package/server/lib/assistants/weixin/api.test.js +28 -0
- package/server/lib/assistants/weixin/chat-adapter.js +8 -2
- package/server/lib/assistants/weixin/inbound.js +5 -3
- package/server/lib/assistants/weixin/inbound.test.js +20 -0
- package/server/lib/assistants/weixin/runtime.js +27 -18
- package/server/lib/assistants/weixin/runtime.test.js +36 -0
- package/server/lib/assistants/weixin/store.js +8 -4
- package/server/lib/assistants/weixin/store.test.js +21 -0
- package/server/lib/assistants/yuri-project.http.test.js +9 -3
- package/server/lib/notifications/DOCUMENTATION.md +6 -0
- package/server/lib/notifications/runtime.js +26 -15
- package/server/lib/notifications/runtime.test.js +14 -13
- package/server/lib/opencode/DOCUMENTATION.md +9 -0
- package/server/lib/opencode/auth.d.ts +6 -2
- package/server/lib/opencode/core-routes.js +0 -1
- package/server/lib/opencode/feature-routes-runtime.js +0 -24
- package/server/lib/opencode/location-release.d.ts +5 -1
- package/server/lib/opencode/managed-capabilities/scheduled-task.js +1 -1
- package/server/lib/opencode/managed-capabilities-runtime.test.js +1 -1
- package/server/lib/opencode/native-goal.js +15 -0
- package/server/lib/opencode/native-goal.test.js +17 -0
- package/server/lib/opencode/settings-helpers.js +0 -6
- package/server/lib/opencode/shutdown-runtime.js +0 -2
- package/server/lib/projects/project-config.js +3 -6
- package/server/lib/scheduled-tasks/DOCUMENTATION.md +10 -35
- package/server/lib/scheduled-tasks/managed-tool-route.js +1 -30
- package/server/lib/scheduled-tasks/routes.js +0 -26
- package/server/lib/scheduled-tasks/runtime.js +33 -257
- package/server/lib/scheduled-tasks/runtime.test.js +24 -65
- package/server/lib/session-metadata/DOCUMENTATION.md +9 -12
- package/server/lib/session-metadata/routes.js +4 -38
- package/server/lib/session-metadata/routes.test.js +2 -2
- package/dist/assets/AssistantContactStatus-dtkObamV.js +0 -3
- package/dist/assets/MarkdownRenderer-BajF4gUU.js +0 -2
- package/dist/assets/MobileTabPageHeader-B6cRyIRH.js +0 -2
- package/dist/assets/device-BjRYYXNT.js +0 -2
- package/dist/assets/host-BW3FEJdU.js +0 -1
- package/dist/assets/insertionBoundaries-Dx3WAY6i.js +0 -1
- package/dist/assets/main-9uP2z1qT.js +0 -2
- package/dist/assets/miniChat-DqZvVkme.js +0 -2
- package/dist/assets/mobile-DXAWjYcN.js +0 -2
- package/dist/assets/renderElectronMiniChatApp-DoOq8Cuo.js +0 -2
- package/dist/assets/renderMobileApp-TTxtOb8b.js +0 -28
- package/dist/assets/runtimeEndpointReset-BC6XWntz.js +0 -149
- package/dist/assets/sessionLookup-De53jDwM.js +0 -1
- package/dist/assets/src-lE4uqaHS.css +0 -2
- package/dist/assets/types-DFf9vd1b.js +0 -2
- package/dist/assets/useDesktopWindowControlsLayout-BjKaHN0x.js +0 -1
- package/dist/assets/useEffectiveDirectory-TEvyiQSC.js +0 -1
- package/dist/assets/useMobileNavigationStore-DTvJV9s-.js +0 -23
- package/dist/assets/vendor-opencode-client-ZeOZ4Efz.js +0 -23
- package/dist/assets/vendor-solid-js-9wmPZH9g.js +0 -1
- package/dist/assets/vendor-zod-Dzk5kdUU.js +0 -39
- package/server/lib/session-goal/DOCUMENTATION.md +0 -371
- package/server/lib/session-goal/capability.js +0 -57
- package/server/lib/session-goal/capability.test.js +0 -33
- package/server/lib/session-goal/objectives.js +0 -61
- package/server/lib/session-goal/routes.js +0 -274
- package/server/lib/session-goal/routes.test.js +0 -211
- package/server/lib/session-goal/runtime.js +0 -2452
- package/server/lib/session-goal/runtime.test.js +0 -2125
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { lookup } from 'node:dns/promises';
|
|
6
|
+
import { request } from 'node:https';
|
|
7
|
+
import { BlockList, isIP } from 'node:net';
|
|
8
|
+
import { collectRequestBytes, decodeDataUrlStrict, assertImageContentMatchesMime } from '../fs/prompt-attachment-store.js';
|
|
9
|
+
|
|
10
|
+
const MAX_BYTES = 20 * 1024 * 1024;
|
|
11
|
+
const blocked = new BlockList();
|
|
12
|
+
for (const [ip, prefix] of [['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10], ['127.0.0.0', 8], ['169.254.0.0', 16], ['172.16.0.0', 12], ['192.0.0.0', 24], ['192.0.2.0', 24], ['192.168.0.0', 16], ['198.18.0.0', 15], ['198.51.100.0', 24], ['203.0.113.0', 24], ['224.0.0.0', 3]]) blocked.addSubnet(ip, prefix, 'ipv4');
|
|
13
|
+
const globalV6 = new BlockList();
|
|
14
|
+
globalV6.addSubnet('2000::', 3, 'ipv6');
|
|
15
|
+
blocked.addSubnet('2001::', 23, 'ipv6');
|
|
16
|
+
blocked.addSubnet('2001:db8::', 32, 'ipv6');
|
|
17
|
+
blocked.addSubnet('2002::', 16, 'ipv6');
|
|
18
|
+
|
|
19
|
+
const fail = () => { throw Object.assign(new Error('image_unavailable'), { code: 'image_unavailable' }); };
|
|
20
|
+
const within = (root, value) => value === root || value.startsWith(`${root}${path.sep}`);
|
|
21
|
+
|
|
22
|
+
/** HTTPS only, pinned public DNS, no redirects, cookies, auth forwarding or proxy. */
|
|
23
|
+
async function download(url, signal) {
|
|
24
|
+
const target = new URL(url);
|
|
25
|
+
if (target.protocol !== 'https:' || target.port || target.username || target.password || target.hash) fail();
|
|
26
|
+
const host = target.hostname.replace(/^\[|\]$/g, '');
|
|
27
|
+
const addresses = isIP(host) ? [{ address: host, family: isIP(host) }] : await lookup(host, { all: true });
|
|
28
|
+
if (!addresses.length || addresses.some(({ address, family }) => family === 6
|
|
29
|
+
? !globalV6.check(address, 'ipv6') || blocked.check(address, 'ipv6')
|
|
30
|
+
: blocked.check(address, 'ipv4'))) fail();
|
|
31
|
+
signal.throwIfAborted();
|
|
32
|
+
const pinned = addresses[0];
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const req = request(target, { signal, lookup: (_host, options, callback) => options?.all
|
|
35
|
+
? callback(null, [pinned]) : callback(null, pinned.address, pinned.family) }, async (response) => {
|
|
36
|
+
try {
|
|
37
|
+
if (response.statusCode !== 200 || Number(response.headers['content-length']) > MAX_BYTES) {
|
|
38
|
+
response.destroy();
|
|
39
|
+
fail();
|
|
40
|
+
}
|
|
41
|
+
const { buffer } = await collectRequestBytes(response, { maxBytes: MAX_BYTES, signal });
|
|
42
|
+
resolve(buffer);
|
|
43
|
+
} catch (error) { response.destroy(); reject(error); }
|
|
44
|
+
});
|
|
45
|
+
req.on('error', reject);
|
|
46
|
+
req.end();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function imageMime(buffer) {
|
|
51
|
+
if (buffer.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))) return 'image/png';
|
|
52
|
+
if (buffer[0] === 255 && buffer[1] === 216 && buffer[2] === 255) return 'image/jpeg';
|
|
53
|
+
if (/^GIF8[79]a$/.test(buffer.subarray(0, 6).toString())) return 'image/gif';
|
|
54
|
+
if (buffer.subarray(0, 4).toString() === 'RIFF' && buffer.subarray(8, 12).toString() === 'WEBP') return 'image/webp';
|
|
55
|
+
if (buffer.subarray(0, 2).toString() === 'BM') return 'image/bmp';
|
|
56
|
+
fail();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Local authority is the Bot cwd, registered projects and generator temp files. */
|
|
60
|
+
export async function loadChannelImage(source, { cwd, allowedRoots, signal, readAttachment }) {
|
|
61
|
+
const deadline = AbortSignal.any([...(signal ? [signal] : []), AbortSignal.timeout(20_000)]);
|
|
62
|
+
deadline.throwIfAborted();
|
|
63
|
+
let buffer;
|
|
64
|
+
if (source.attachmentID) {
|
|
65
|
+
({ buffer } = await readAttachment(source.attachmentID));
|
|
66
|
+
} else if (typeof source.url !== 'string' || source.url.length > MAX_BYTES * 1.4) {
|
|
67
|
+
fail();
|
|
68
|
+
} else if (source.url.startsWith('data:')) {
|
|
69
|
+
({ buffer } = decodeDataUrlStrict(source.url, { maxBytes: MAX_BYTES }));
|
|
70
|
+
} else if (/^https?:/i.test(source.url)) {
|
|
71
|
+
buffer = await download(source.url, deadline);
|
|
72
|
+
} else {
|
|
73
|
+
let candidate = source.url;
|
|
74
|
+
if (candidate.startsWith('file:')) candidate = fileURLToPath(candidate);
|
|
75
|
+
else if ((!path.isAbsolute(candidate) && /^[a-z][a-z0-9+.-]*:/i.test(candidate)) || candidate.startsWith('//') || candidate.startsWith('\\\\')) fail();
|
|
76
|
+
if (candidate.startsWith('~/')) candidate = path.join(os.homedir(), candidate.slice(2));
|
|
77
|
+
const resolved = await fs.realpath(path.resolve(cwd, candidate));
|
|
78
|
+
const roots = await Promise.all([cwd, ...allowedRoots, os.tmpdir()].map((root) => fs.realpath(root).catch(() => null)));
|
|
79
|
+
if (!roots.some((root) => root && within(root, resolved))) fail();
|
|
80
|
+
const handle = await fs.open(resolved, 'r');
|
|
81
|
+
try {
|
|
82
|
+
const stat = await handle.stat();
|
|
83
|
+
if (!stat.isFile() || stat.size > MAX_BYTES) fail();
|
|
84
|
+
({ buffer } = await collectRequestBytes(handle.createReadStream({ autoClose: false }), { maxBytes: MAX_BYTES, signal: deadline }));
|
|
85
|
+
} finally { await handle.close(); }
|
|
86
|
+
}
|
|
87
|
+
deadline.throwIfAborted();
|
|
88
|
+
if (!buffer?.length || buffer.length > MAX_BYTES) fail();
|
|
89
|
+
const mime = imageMime(buffer);
|
|
90
|
+
assertImageContentMatchesMime(buffer, mime);
|
|
91
|
+
return { buffer, mime, filename: `image.${mime === 'image/jpeg' ? 'jpg' : mime.slice(6)}` };
|
|
92
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
const io = vi.hoisted(() => ({ lookup: vi.fn(), request: vi.fn() }));
|
|
6
|
+
vi.mock('node:dns/promises', () => ({ lookup: io.lookup }));
|
|
7
|
+
vi.mock('node:https', () => ({ request: io.request }));
|
|
8
|
+
import { loadChannelImage } from './channel-image.js';
|
|
9
|
+
|
|
10
|
+
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aKxkAAAAASUVORK5CYII=', 'base64');
|
|
11
|
+
const source = { url: 'https://images.example.com/generated.png?signature=image-capability' };
|
|
12
|
+
const input = { cwd: process.cwd(), allowedRoots: [] };
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
vi.resetAllMocks();
|
|
15
|
+
io.lookup.mockResolvedValue([{ address: '8.8.8.8', family: 4 }]);
|
|
16
|
+
io.request.mockImplementation((_url, _options, callback) => {
|
|
17
|
+
const request = new EventEmitter();
|
|
18
|
+
request.end = () => {
|
|
19
|
+
const response = Readable.from([png]);
|
|
20
|
+
response.statusCode = 200;
|
|
21
|
+
response.headers = {};
|
|
22
|
+
callback(response);
|
|
23
|
+
};
|
|
24
|
+
return request;
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('public HTTPS image boundary', () => {
|
|
29
|
+
it('pins the validated DNS answer instead of looking it up again at connection time', async () => {
|
|
30
|
+
expect(await loadChannelImage(source, input)).toMatchObject({ buffer: png, mime: 'image/png' });
|
|
31
|
+
expect(io.lookup).toHaveBeenCalledOnce();
|
|
32
|
+
const [url, options] = io.request.mock.calls[0];
|
|
33
|
+
expect(url.hostname).toBe('images.example.com');
|
|
34
|
+
expect(options.headers).toBeUndefined();
|
|
35
|
+
expect(options.signal).toBeInstanceOf(AbortSignal);
|
|
36
|
+
const callback = vi.fn();
|
|
37
|
+
options.lookup('images.example.com', { all: true }, callback);
|
|
38
|
+
expect(callback).toHaveBeenCalledWith(null, [{ address: '8.8.8.8', family: 4 }]);
|
|
39
|
+
expect(io.lookup).toHaveBeenCalledOnce();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('rejects any private DNS result before opening a connection', async () => {
|
|
43
|
+
io.lookup.mockResolvedValue([{ address: '8.8.8.8', family: 4 }, { address: '127.0.0.1', family: 4 }]);
|
|
44
|
+
await expect(loadChannelImage(source, input)).rejects.toMatchObject({ code: 'image_unavailable' });
|
|
45
|
+
expect(io.request).not.toHaveBeenCalled();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it.each([302, 401, 500])('rejects status %i without following redirects or accepting an empty image', async (status) => {
|
|
49
|
+
io.request.mockImplementation((_url, _options, callback) => {
|
|
50
|
+
const request = new EventEmitter();
|
|
51
|
+
request.end = () => {
|
|
52
|
+
const response = Readable.from([png]);
|
|
53
|
+
response.statusCode = status;
|
|
54
|
+
response.headers = { location: 'https://127.0.0.1/private' };
|
|
55
|
+
callback(response);
|
|
56
|
+
};
|
|
57
|
+
return request;
|
|
58
|
+
});
|
|
59
|
+
await expect(loadChannelImage(source, input)).rejects.toMatchObject({ code: 'image_unavailable' });
|
|
60
|
+
expect(io.request).toHaveBeenCalledOnce();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('rejects oversized chunked bodies even without content-length', async () => {
|
|
64
|
+
io.request.mockImplementation((_url, _options, callback) => {
|
|
65
|
+
const request = new EventEmitter();
|
|
66
|
+
request.end = () => {
|
|
67
|
+
const response = Readable.from([Buffer.alloc(20 * 1024 * 1024 + 1)]);
|
|
68
|
+
response.statusCode = 200;
|
|
69
|
+
response.headers = {};
|
|
70
|
+
callback(response);
|
|
71
|
+
};
|
|
72
|
+
return request;
|
|
73
|
+
});
|
|
74
|
+
await expect(loadChannelImage(source, input)).rejects.toMatchObject({ code: 'PROMPT_ATTACHMENT_TOO_LARGE' });
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { createRequire } from 'node:module';
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { createAssistantChannelStore } from './channels/store.js';
|
|
7
|
+
import { createWeixinStore } from './weixin/store.js';
|
|
8
|
+
|
|
9
|
+
const Database = createRequire(import.meta.url)('better-sqlite3');
|
|
10
|
+
describe.each(['weixin', 'enterprise'])('%s durable image outbox', (kind) => {
|
|
11
|
+
it('migrates text-only queues and preserves image snapshots through restart/unknown recovery', () => {
|
|
12
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'yuri-image-outbox-'));
|
|
13
|
+
const filename = path.join(directory, 'channel.sqlite');
|
|
14
|
+
const legacy = new Database(filename);
|
|
15
|
+
legacy.exec(`CREATE TABLE outbox(id TEXT NOT NULL UNIQUE,epoch TEXT NOT NULL,text TEXT NOT NULL,state TEXT NOT NULL DEFAULT 'pending',sequence INTEGER PRIMARY KEY AUTOINCREMENT${kind === 'enterprise' ? ",target TEXT NOT NULL DEFAULT '',attempts INTEGER NOT NULL DEFAULT 0" : ''});`);
|
|
16
|
+
legacy.close();
|
|
17
|
+
const create = kind === 'weixin' ? createWeixinStore : createAssistantChannelStore;
|
|
18
|
+
let store = create(directory);
|
|
19
|
+
try {
|
|
20
|
+
const binding = store.bind(kind === 'weixin'
|
|
21
|
+
? { assistant_id: 'bot', account_id: 'account', peer_id: 'owner', token: 'test', base_url: 'https://ilinkai.weixin.qq.com', language: 'en', generation: 0, ordinal: 0, updated_at: 1 }
|
|
22
|
+
: { assistant_id: 'bot', provider: 'feishu', client_id: 'client', client_secret: 'test', owner_id: 'owner', language: 'en', generation: 0, ordinal: 0, updated_at: 1 });
|
|
23
|
+
const image = { attachmentID: 'att_snapshot', mime: 'image/png', size: 68 };
|
|
24
|
+
store.collect(binding, { generation: 0, ordinal: 1 }, [{ id: 'image', text: '', image }, { id: 'later', text: 'later' }], 1);
|
|
25
|
+
expect(JSON.parse(store.nextDelivery(binding.epoch).image)).toEqual(image);
|
|
26
|
+
store.close();
|
|
27
|
+
store = create(directory);
|
|
28
|
+
expect(JSON.parse(store.nextDelivery(binding.epoch).image)).toEqual(image);
|
|
29
|
+
store.markDelivery('image', 'sending');
|
|
30
|
+
store.close();
|
|
31
|
+
store = create(directory);
|
|
32
|
+
store.recover(binding.epoch, 'interrupted');
|
|
33
|
+
expect(store.hasUnknown(binding.epoch)).toBe(true);
|
|
34
|
+
expect(store.nextDelivery(binding.epoch).id).toBe('later');
|
|
35
|
+
store.sent('later');
|
|
36
|
+
expect(store.nextDelivery(binding.epoch)).toBeUndefined();
|
|
37
|
+
store.collect(store.get('bot', 'feishu'), { generation: 1, ordinal: 0 }, [], 2);
|
|
38
|
+
expect(store.hasUnknown(binding.epoch)).toBe(false);
|
|
39
|
+
} finally { store.close(); fs.rmSync(directory, { recursive: true, force: true }); }
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { createAssistantsService } from './service.js';
|
|
6
|
+
import { createAssistantChannelsRuntime } from './channels/runtime.js';
|
|
7
|
+
import { createWeixinRuntime } from './weixin/runtime.js';
|
|
8
|
+
import { createWeixinStore } from './weixin/store.js';
|
|
9
|
+
import { createAssistantChannelStore } from './channels/store.js';
|
|
10
|
+
|
|
11
|
+
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aKxkAAAAASUVORK5CYII=', 'base64');
|
|
12
|
+
describe('real contact output → all native image channels', () => {
|
|
13
|
+
it.each(['weixin', 'wecom', 'feishu', 'dingtalk'])('returns images only to %s, keeps app replies local and broadcasts scheduled results', async (source) => {
|
|
14
|
+
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'yuri-image-pipeline-'));
|
|
15
|
+
const imagePath = path.join(directory, 'generated.png');
|
|
16
|
+
await fs.writeFile(imagePath, png);
|
|
17
|
+
let enterprise;
|
|
18
|
+
let weixin;
|
|
19
|
+
const service = createAssistantsService({ dbPath: path.join(directory, 'assistants.sqlite'), dataDir: directory,
|
|
20
|
+
buildOpenCodeUrl: () => 'http://127.0.0.1:1', getOpenCodeAuthHeaders: () => ({}), getAllowedRoots: () => [directory],
|
|
21
|
+
clientFactory: () => ({ message: { list: async () => ({ data: [], cursor: {} }) } }),
|
|
22
|
+
listProjects: async () => [{ id: 'project', path: directory }],
|
|
23
|
+
upsertScheduledTask: async (_project, task) => ({ task: { ...task, id: 'task' } }),
|
|
24
|
+
syncScheduledTaskProject: async () => {},
|
|
25
|
+
createChatCompletion: async () => ({ choices: [] }),
|
|
26
|
+
runContactTurn: async ({ userText, tools, onBubbleDelta }) => {
|
|
27
|
+
if (userText === 'draw again') {
|
|
28
|
+
const scheduled = await tools.find((tool) => tool.name === 'schedule_task').execute('schedule', {
|
|
29
|
+
name: 'Draw daily', prompt: 'Draw an image', time: '18:00', timezone: 'UTC',
|
|
30
|
+
});
|
|
31
|
+
expect(scheduled.isError).not.toBe(true);
|
|
32
|
+
}
|
|
33
|
+
const image = ``;
|
|
34
|
+
onBubbleDelta?.(0, image, true);
|
|
35
|
+
return { bubbles: [image] };
|
|
36
|
+
},
|
|
37
|
+
onContactTurnEvent: (event) => { enterprise?.onTurnEvent(event); weixin?.onTurnEvent(event); } });
|
|
38
|
+
const bot = service.createAssistant({ name: 'Image pipeline', providerID: 'p', modelID: 'm', workspacePath: directory });
|
|
39
|
+
const connections = new Map();
|
|
40
|
+
const sends = Object.fromEntries(['weixin', 'wecom', 'feishu', 'dingtalk'].map((provider) => [provider, vi.fn(async () => {})]));
|
|
41
|
+
const adapters = Object.fromEntries(['wecom', 'feishu', 'dingtalk'].map((provider) => [provider, {
|
|
42
|
+
connect: async (input) => { connections.set(provider, input); input.onReady();
|
|
43
|
+
await new Promise((resolve) => input.signal.aborted ? resolve() : input.signal.addEventListener('abort', resolve, { once: true })); },
|
|
44
|
+
send: sends[provider],
|
|
45
|
+
}]));
|
|
46
|
+
const incoming = [];
|
|
47
|
+
const api = { getQRCode: async () => ({ qrcode: 'qr', qrcode_img_content: 'qr-content' }),
|
|
48
|
+
getQRStatus: async () => ({ status: 'confirmed', bot_token: 'test', ilink_bot_id: 'account', ilink_user_id: 'owner', baseurl: 'https://ilinkai.weixin.qq.com' }),
|
|
49
|
+
getUpdates: async () => ({ msgs: incoming.splice(0), get_updates_buf: 'cursor' }),
|
|
50
|
+
sendImage: async (_binding, delivery) => sends.weixin(delivery), sendText: vi.fn(async () => {}),
|
|
51
|
+
getConfig: async () => ({ ret: 0 }), sendTyping: async () => {} };
|
|
52
|
+
try {
|
|
53
|
+
enterprise = createAssistantChannelsRuntime({ directory: path.join(directory, 'channels'), service, adapters, env: {}, intervalMs: 10 });
|
|
54
|
+
weixin = createWeixinRuntime({ directory: path.join(directory, 'weixin'), service, api, env: {}, intervalMs: 10 });
|
|
55
|
+
for (const provider of Object.keys(adapters)) {
|
|
56
|
+
await enterprise.save(bot.id, provider, { clientID: provider, clientSecret: 'test', ownerID: 'owner', language: 'zh-CN' });
|
|
57
|
+
}
|
|
58
|
+
await weixin.startLogin(bot.id, 'zh-CN');
|
|
59
|
+
await vi.waitFor(() => expect(weixin.status(bot.id).status).toBe('connected'));
|
|
60
|
+
const weixinStore = createWeixinStore(path.join(directory, 'weixin'));
|
|
61
|
+
weixinStore.patch(weixinStore.get(bot.id).epoch, { context_token: 'test-context' });
|
|
62
|
+
weixinStore.close();
|
|
63
|
+
await vi.waitFor(() => expect(connections.size).toBe(3));
|
|
64
|
+
const cards = [
|
|
65
|
+
{ cardType: 'session', sessionID: 'ses_card', directory, title: 'Internal session', status: 'complete' },
|
|
66
|
+
{ cardType: 'assistant', assistantID: bot.id, name: 'Internal Bot', providerID: 'p', modelID: 'm' },
|
|
67
|
+
{ cardType: 'schedule', taskID: 'task_card', projectID: 'project_card', name: 'Internal schedule' },
|
|
68
|
+
];
|
|
69
|
+
for (const card of cards) service.appendContactCard(bot.id, card);
|
|
70
|
+
const tip = service.channelPage(bot.id, { tipOnly: true }).tipOrdinal;
|
|
71
|
+
const channelStore = createAssistantChannelStore(path.join(directory, 'channels'));
|
|
72
|
+
const personalStore = createWeixinStore(path.join(directory, 'weixin'));
|
|
73
|
+
try {
|
|
74
|
+
await vi.waitFor(() => {
|
|
75
|
+
expect(personalStore.get(bot.id).ordinal).toBe(tip);
|
|
76
|
+
for (const provider of Object.keys(adapters)) expect(channelStore.get(bot.id, provider).ordinal).toBe(tip);
|
|
77
|
+
});
|
|
78
|
+
for (const send of Object.values(sends)) expect(send).not.toHaveBeenCalled();
|
|
79
|
+
expect(api.sendText).not.toHaveBeenCalled();
|
|
80
|
+
expect(weixin.status(bot.id).error).toBeNull();
|
|
81
|
+
for (const provider of Object.keys(adapters)) expect(enterprise.status(bot.id, provider).error).toBeNull();
|
|
82
|
+
expect(service.contactMessages(bot.id, { limit: 20 }).messages.flatMap((message) => message.cards)).toEqual(cards.map((card) => expect.objectContaining(card)));
|
|
83
|
+
} finally { channelStore.close(); personalStore.close(); }
|
|
84
|
+
if (source === 'weixin') incoming.push({ message_id: 'in', from_user_id: 'owner', message_type: 1,
|
|
85
|
+
context_token: 'test-context', item_list: [{ type: 1, text_item: { text: 'draw' } }] });
|
|
86
|
+
else await connections.get(source).onMessage({ id: 'in', senderID: 'owner', chatType: 'private', text: 'draw' });
|
|
87
|
+
await vi.waitFor(() => expect(sends[source]).toHaveBeenCalledOnce());
|
|
88
|
+
expect(sends[source].mock.calls[0][0].image.buffer).toEqual(png);
|
|
89
|
+
for (const [provider, send] of Object.entries(sends)) if (provider !== source) expect(send).not.toHaveBeenCalled();
|
|
90
|
+
await service.send(bot.id, { messageID: 'app-turn', parts: [{ type: 'text', text: 'draw again' }] });
|
|
91
|
+
await vi.waitFor(() => {
|
|
92
|
+
expect(service.contactMessages(bot.id, { limit: 20 }).messages.some((message) => message.messageID === 'app-turn:bubble:1')).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
const appTip = service.channelPage(bot.id, { tipOnly: true }).tipOrdinal;
|
|
95
|
+
const appChannels = createAssistantChannelStore(path.join(directory, 'channels'));
|
|
96
|
+
const appWeixin = createWeixinStore(path.join(directory, 'weixin'));
|
|
97
|
+
try {
|
|
98
|
+
await vi.waitFor(() => {
|
|
99
|
+
expect(appWeixin.get(bot.id).ordinal).toBe(appTip);
|
|
100
|
+
for (const provider of Object.keys(adapters)) expect(appChannels.get(bot.id, provider).ordinal).toBe(appTip);
|
|
101
|
+
});
|
|
102
|
+
for (const [provider, send] of Object.entries(sends)) expect(send).toHaveBeenCalledTimes(provider === source ? 1 : 0);
|
|
103
|
+
} finally { appChannels.close(); appWeixin.close(); }
|
|
104
|
+
expect(await service.reportScheduledTaskResult({ id: 'run', projectId: 'project', taskId: 'task',
|
|
105
|
+
taskName: 'Draw daily', status: 'success', sessionId: 'worker', directory,
|
|
106
|
+
startedAt: Date.now(), finishedAt: Date.now(), prompt: 'Draw an image' })).toBe(true);
|
|
107
|
+
await vi.waitFor(() => {
|
|
108
|
+
for (const [provider, send] of Object.entries(sends)) expect(send).toHaveBeenCalledTimes(provider === source ? 2 : 1);
|
|
109
|
+
});
|
|
110
|
+
for (const send of Object.values(sends)) expect(send.mock.calls.at(-1)[0].image.buffer).toEqual(png);
|
|
111
|
+
expect(api.sendText).not.toHaveBeenCalled();
|
|
112
|
+
} finally { await enterprise?.close(); await weixin?.close(); service.close(); await fs.rm(directory, { recursive: true, force: true }); }
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { parseMarkdown } from 'chat';
|
|
3
|
+
|
|
4
|
+
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
|
5
|
+
export const channelImageFailure = (language) => String(language).startsWith('zh')
|
|
6
|
+
? '图片未能发送,请在 Yuri 中查看原图。'
|
|
7
|
+
: 'The image could not be sent. View the original in Yuri.';
|
|
8
|
+
|
|
9
|
+
/** Project only published output; code examples and ordinary links are not media. */
|
|
10
|
+
function outputParts(message) {
|
|
11
|
+
const result = [];
|
|
12
|
+
const parts = message.parts?.length ? message.parts : [{ type: 'text', text: message.text ?? '' }];
|
|
13
|
+
for (const part of parts) {
|
|
14
|
+
if (part.type === 'file' && part.mime?.startsWith('image/')) {
|
|
15
|
+
result.push({ type: 'image', source: part });
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (part.type !== 'text' || !part.text?.trim() || part.text.trim().startsWith('oc.settle.')) continue;
|
|
19
|
+
if (!part.text.includes('![')) {
|
|
20
|
+
result.push({ type: 'text', text: part.text.trim() });
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const tree = parseMarkdown(part.text);
|
|
24
|
+
const definitions = new Map(tree.children.filter((node) => node.type === 'definition').map((node) => [node.identifier, node.url]));
|
|
25
|
+
const spans = [];
|
|
26
|
+
const walk = (node) => {
|
|
27
|
+
const url = node.type === 'image' ? node.url : node.type === 'imageReference' ? definitions.get(node.identifier) : null;
|
|
28
|
+
if (url) spans.push({ start: node.position.start.offset, end: node.position.end.offset, url });
|
|
29
|
+
for (const child of node.children ?? []) walk(child);
|
|
30
|
+
};
|
|
31
|
+
walk(tree);
|
|
32
|
+
const used = new Set(spans.map((span) => span.url));
|
|
33
|
+
for (const node of tree.children) {
|
|
34
|
+
if (node.type === 'definition' && used.has(node.url)) spans.push({ start: node.position.start.offset, end: node.position.end.offset });
|
|
35
|
+
}
|
|
36
|
+
spans.sort((a, b) => a.start - b.start);
|
|
37
|
+
let offset = 0;
|
|
38
|
+
for (const span of spans) {
|
|
39
|
+
if (span.start < offset) continue;
|
|
40
|
+
const text = part.text.slice(offset, span.start).trim();
|
|
41
|
+
if (text) result.push({ type: 'text', text });
|
|
42
|
+
if (span.url) result.push({ type: 'image', source: { url: span.url } });
|
|
43
|
+
offset = span.end;
|
|
44
|
+
}
|
|
45
|
+
const text = part.text.slice(offset).trim();
|
|
46
|
+
if (text) result.push({ type: 'text', text });
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Snapshot images into the existing attachment store before advancing the feed. */
|
|
52
|
+
export async function prepareChannelOutput(message, { service, assistantID, generation, language, failureText, signal, assertCurrent }) {
|
|
53
|
+
const deliveries = [];
|
|
54
|
+
const parts = message.status === 'error' ? [{ type: 'text', text: failureText }] : outputParts(message);
|
|
55
|
+
let images = 0;
|
|
56
|
+
let imageBytes = 0;
|
|
57
|
+
for (const part of parts) {
|
|
58
|
+
signal?.throwIfAborted();
|
|
59
|
+
assertCurrent();
|
|
60
|
+
if (part.type === 'image') {
|
|
61
|
+
images += 1;
|
|
62
|
+
try {
|
|
63
|
+
if (images > 8) throw new Error('image_limit');
|
|
64
|
+
const image = await service.prepareChannelImage(assistantID, part.source, {
|
|
65
|
+
generation, signal, uploadID: `channel-${digest(`${generation}:${message.messageID}:${deliveries.length}`)}`,
|
|
66
|
+
});
|
|
67
|
+
imageBytes += image.size ?? 0;
|
|
68
|
+
if (imageBytes > 50 * 1024 * 1024) throw new Error('image_budget');
|
|
69
|
+
assertCurrent();
|
|
70
|
+
deliveries.push({ text: '', image });
|
|
71
|
+
} catch {
|
|
72
|
+
assertCurrent();
|
|
73
|
+
signal?.throwIfAborted();
|
|
74
|
+
deliveries.push({ text: channelImageFailure(language) });
|
|
75
|
+
}
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const chars = Array.from(part.text ?? '');
|
|
79
|
+
for (let index = 0; index < chars.length; index += 3500) deliveries.push({ text: chars.slice(index, index + 3500).join('') });
|
|
80
|
+
}
|
|
81
|
+
return deliveries;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function readChannelDeliveryImage(service, assistantID, image, signal) {
|
|
85
|
+
if (!image) return undefined;
|
|
86
|
+
signal?.throwIfAborted();
|
|
87
|
+
const { buffer, mime, filename } = await service.getAssistantContactAttachment(assistantID, image.attachmentID);
|
|
88
|
+
signal?.throwIfAborted();
|
|
89
|
+
return { buffer, mime, filename: filename || 'image.png' };
|
|
90
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { prepareChannelOutput, readChannelDeliveryImage } from './channel-output.js';
|
|
6
|
+
import { loadChannelImage } from './channel-image.js';
|
|
7
|
+
import { createAssistantsService } from './service.js';
|
|
8
|
+
|
|
9
|
+
const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aKxkAAAAASUVORK5CYII=', 'base64');
|
|
10
|
+
const cleanups = [];
|
|
11
|
+
afterEach(async () => { while (cleanups.length) await cleanups.pop()(); });
|
|
12
|
+
const options = (overrides = {}) => ({ service: { prepareChannelImage: vi.fn(async () => ({ attachmentID: 'att_image' })) },
|
|
13
|
+
assistantID: 'bot', generation: 0, language: 'zh-CN', failureText: 'failed', assertCurrent: () => {}, ...overrides });
|
|
14
|
+
|
|
15
|
+
describe('shared channel image output', () => {
|
|
16
|
+
it.each(['session', 'assistant', 'schedule', 'future-card'])('silently excludes %s cards instead of sending title/link fallbacks', async (cardType) => {
|
|
17
|
+
const input = options();
|
|
18
|
+
const card = { type: 'card', cardType, title: 'Internal title', name: 'Internal name', url: 'yuri://internal' };
|
|
19
|
+
expect(await prepareChannelOutput({ messageID: 'card', text: '', cards: [card] }, input)).toEqual([]);
|
|
20
|
+
expect(await prepareChannelOutput({ messageID: 'card-parts', parts: [card], cards: [card] }, input)).toEqual([]);
|
|
21
|
+
expect(input.service.prepareChannelImage).not.toHaveBeenCalled();
|
|
22
|
+
expect(await prepareChannelOutput({ messageID: 'mixed', parts: [{ type: 'text', text: 'Normal reply' }, card,
|
|
23
|
+
{ type: 'file', mime: 'image/png', attachmentID: 'att_image' }] }, input))
|
|
24
|
+
.toEqual([{ text: 'Normal reply' }, { text: '', image: { attachmentID: 'att_image' } }]);
|
|
25
|
+
});
|
|
26
|
+
it('keeps prose/image order, parses escaped/spaced/reference URLs, ignores code and ordinary links', async () => {
|
|
27
|
+
const input = options();
|
|
28
|
+
const output = await prepareChannelOutput({ messageID: 'm', text: 'Before  after ![b][ref]\n\n[ref]: https://example.com/b.png\n\n`` [link](/file.png)' }, input);
|
|
29
|
+
expect(output.map((part) => part.image ? 'image' : part.text)).toEqual(['Before', 'image', 'after', 'image', '`` [link](/file.png)']);
|
|
30
|
+
expect(input.service.prepareChannelImage.mock.calls.map(([, source]) => source.url)).toEqual(['/a b.png', 'https://example.com/b.png']);
|
|
31
|
+
expect(JSON.stringify(output)).not.toContain('/a b.png');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('supports attachment-only replies and partial failure without dropping later content', async () => {
|
|
35
|
+
const input = options();
|
|
36
|
+
input.service.prepareChannelImage.mockRejectedValueOnce(new Error('private path and credential'));
|
|
37
|
+
const result = await prepareChannelOutput({ messageID: 'm', parts: [{ type: 'file', mime: 'image/png', attachmentID: 'one' },
|
|
38
|
+
{ type: 'text', text: 'next' }, { type: 'file', mime: 'image/jpeg', url: 'data:image/jpeg;base64,AA==' }] }, input);
|
|
39
|
+
expect(result).toEqual([{ text: '图片未能发送,请在 Yuri 中查看原图。' }, { text: 'next' }, { text: '', image: { attachmentID: 'att_image' } }]);
|
|
40
|
+
expect(JSON.stringify(result)).not.toMatch(/private|credential|data:/);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('fences late image preparation after a reset', async () => {
|
|
44
|
+
let current = true;
|
|
45
|
+
const input = options({ assertCurrent: () => { if (!current) throw new Error('cancelled'); } });
|
|
46
|
+
input.service.prepareChannelImage.mockImplementation(async () => { current = false; return { attachmentID: 'old' }; });
|
|
47
|
+
await expect(prepareChannelOutput({ messageID: 'm', text: '' }, input)).rejects.toThrow('cancelled');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('snapshots local generated images into owned durable attachments and reuses them after source removal', async () => {
|
|
51
|
+
const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'yuri-channel-images-'));
|
|
52
|
+
cleanups.push(() => fs.rm(directory, { recursive: true, force: true }));
|
|
53
|
+
const service = createAssistantsService({ dbPath: path.join(directory, 'assistants.sqlite'), dataDir: directory,
|
|
54
|
+
buildOpenCodeUrl: () => 'http://127.0.0.1:1', getOpenCodeAuthHeaders: () => ({}),
|
|
55
|
+
getAllowedRoots: () => [directory], createChatCompletion: async () => ({ choices: [] }) });
|
|
56
|
+
cleanups.push(() => service.close());
|
|
57
|
+
const bot = service.createAssistant({ name: 'Image test', providerID: 'p', modelID: 'm', workspacePath: directory });
|
|
58
|
+
const source = path.join(directory, 'generated.png');
|
|
59
|
+
await fs.writeFile(source, png);
|
|
60
|
+
const image = await service.prepareChannelImage(bot.id, { url: source }, { generation: 0, uploadID: 'test-image' });
|
|
61
|
+
await fs.unlink(source);
|
|
62
|
+
expect(await service.prepareChannelImage(bot.id, { url: source }, { generation: 0, uploadID: 'test-image' })).toEqual(image);
|
|
63
|
+
expect(await readChannelDeliveryImage(service, bot.id, image)).toMatchObject({ buffer: png, mime: 'image/png' });
|
|
64
|
+
const other = service.createAssistant({ name: 'Other', providerID: 'p', modelID: 'm' });
|
|
65
|
+
await expect(readChannelDeliveryImage(service, other.id, image)).rejects.toMatchObject({ code: 'not_found' });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it.each(['https://127.0.0.1/a.png', 'https://[::1]/a.png', 'https://[::ffff:127.0.0.1]/a.png', 'http://example.com/a.png',
|
|
69
|
+
'https://user:password@example.com/a.png', 'https://169.254.169.254/a.png', 'https://10.0.0.1/a.png'])('rejects unsafe remote image %s', async (url) => {
|
|
70
|
+
await expect(loadChannelImage({ url }, { cwd: os.tmpdir(), allowedRoots: [] })).rejects.toMatchObject({ code: 'image_unavailable' });
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('rejects disguised HTML/SVG and bounds inline bytes', async () => {
|
|
74
|
+
const input = { cwd: os.tmpdir(), allowedRoots: [] };
|
|
75
|
+
await expect(loadChannelImage({ url: 'data:image/png;base64,PGh0bWw+' }, input)).rejects.toThrow();
|
|
76
|
+
await expect(loadChannelImage({ url: 'data:image/svg+xml;base64,PHN2Zz4=' }, input)).rejects.toThrow();
|
|
77
|
+
expect(await loadChannelImage({ url: `data:image/png;base64,${png.toString('base64')}` }, input)).toMatchObject({ buffer: png, mime: 'image/png' });
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -55,7 +55,8 @@ The seam is deliberately narrow so an SDK upgrade changes only the bridge:
|
|
|
55
55
|
|
|
56
56
|
`service.channelPage()` is the internal bounded, ascending public-output seam. It
|
|
57
57
|
validates the Bot, uses the contact-store generation and returns only assistant
|
|
58
|
-
rows
|
|
58
|
+
rows eligible for external delivery, excluding replies to built-in Yuri IM input.
|
|
59
|
+
It does not expose credentials or replace the contact execution engine.
|
|
59
60
|
|
|
60
61
|
## Runtime API
|
|
61
62
|
|
|
@@ -67,6 +68,8 @@ createAssistantChannelsRuntime({
|
|
|
67
68
|
|
|
68
69
|
- `status(assistantID, provider)` — sync read; validates the Bot first.
|
|
69
70
|
- `save(assistantID, provider, input)` — save configuration and connect.
|
|
71
|
+
- `discover(assistantID, provider, input, { signal })` — temporarily connect and
|
|
72
|
+
return the first private sender's `{ ownerID }`, without saving or running tools.
|
|
70
73
|
- `transfer(assistantID, provider, input)` — explicitly move a local binding using
|
|
71
74
|
`{ sourceAssistantID, revision }`, without returning or re-entering its secret.
|
|
72
75
|
- `retry(assistantID, provider)` — explicit reconnect only.
|
|
@@ -83,16 +86,31 @@ createAssistantChannelsRuntime({
|
|
|
83
86
|
| PUT | same | save + connect |
|
|
84
87
|
| POST | same `/retry` | reconnect without resending unknown output |
|
|
85
88
|
| POST | same `/transfer` | confirmed local rebind with source revision check |
|
|
89
|
+
| POST | same `/discover` | bounded, abortable user-ID discovery before Save |
|
|
86
90
|
| DELETE | same | disconnect |
|
|
87
91
|
|
|
88
|
-
Save input is `{ clientID, clientSecret?, ownerID
|
|
89
|
-
`ownerID`
|
|
90
|
-
|
|
92
|
+
Save input is `{ clientID, clientSecret?, ownerID, language? }`. A non-empty
|
|
93
|
+
`ownerID` is required on every save (`owner_required` otherwise) and restricts the
|
|
94
|
+
channel to that private sender. Existing unrestricted bindings remain readable and
|
|
95
|
+
operational until explicitly updated; no silent owner selection/migration occurs.
|
|
96
|
+
The first save
|
|
91
97
|
requires `clientSecret`; an update with the same `clientID` and an empty or omitted
|
|
92
98
|
secret keeps the stored one. A new `clientID` requires a new secret.
|
|
93
99
|
Responses are `Cache-Control: no-store` and contain only safe error codes — no
|
|
94
100
|
upstream bodies, secrets or routing targets.
|
|
95
101
|
|
|
102
|
+
Discovery takes `{ clientID, clientSecret? }` and the same credential-preservation
|
|
103
|
+
rules as Save. It returns only `{ ownerID }` after the first valid private message;
|
|
104
|
+
groups and missing/oversized sender IDs do not qualify. The user confirms that ID
|
|
105
|
+
through Save. The discovery message never enters the assistant execution pipeline.
|
|
106
|
+
Credentials stay in the temporary connection, not SQLite or response DTOs.
|
|
107
|
+
The request expires after 90 seconds (`discovery_timeout`), and HTTP disconnect,
|
|
108
|
+
Bot deletion, lease loss and Host shutdown abort it. A discovery owns its lane and
|
|
109
|
+
reserves its provider/account against concurrent saves or probes. It cannot take
|
|
110
|
+
another Bot's bound account. An existing connection on this lane is drained first
|
|
111
|
+
and resumed unchanged after discovery completes, fails or is cancelled. No other
|
|
112
|
+
provider/account is stopped. Platform ACK/reconnect behavior remains adapter-owned.
|
|
113
|
+
|
|
96
114
|
Status shape:
|
|
97
115
|
|
|
98
116
|
```json
|
|
@@ -144,7 +162,7 @@ Adapters are injected by provider key and must implement exactly:
|
|
|
144
162
|
```js
|
|
145
163
|
{
|
|
146
164
|
connect({ config, signal, onReady, onMessage, onError }): Promise<void>,
|
|
147
|
-
send({ config, target, text, id, signal }): Promise<void>,
|
|
165
|
+
send({ config, target, text, image?, id, signal, beforeSend? }): Promise<void>,
|
|
148
166
|
}
|
|
149
167
|
```
|
|
150
168
|
|
|
@@ -164,6 +182,22 @@ Adapters are injected by provider key and must implement exactly:
|
|
|
164
182
|
user-controlled URL. An abort after submission must reject with `ambiguous: true`
|
|
165
183
|
so the row becomes `unknown` instead of being retried.
|
|
166
184
|
- Adapters must honor `signal`; shutdown and disconnect abort platform I/O.
|
|
185
|
+
- Output is either plain `text` or one `image: { buffer, mime, filename }`.
|
|
186
|
+
The shared `../channel-output.js` projector snapshots published Markdown/file
|
|
187
|
+
images through the existing contact attachment store. Outbox's additive `image`
|
|
188
|
+
column defaults to empty for legacy text rows and stores only the descriptor;
|
|
189
|
+
bytes are materialized immediately before dispatch and cleared from the row on
|
|
190
|
+
success. Each image retains its own target and stable ID, so partial failure or
|
|
191
|
+
restart cannot resend earlier text. A blocked image produces a safe failure
|
|
192
|
+
notice; unknown delivery stays unknown and is not automatically resent.
|
|
193
|
+
After upload, adapters call `beforeSend()` to fence a reset/rebind before the
|
|
194
|
+
actual platform send. Upload failure is definite (no chat message was submitted),
|
|
195
|
+
unlike an interrupted/ambiguous final send. Source/size bounds live in
|
|
196
|
+
`../DOCUMENTATION.md` under message channel output.
|
|
197
|
+
- Internal Yuri cards of every type are unsupported output: the shared projector
|
|
198
|
+
silently skips them without title/anchor/deeplink fallback or an error notice.
|
|
199
|
+
Card-only rows advance the feed without creating deliveries/retries; ordinary
|
|
200
|
+
text and images in mixed output still send. The original Yuri cards are unchanged.
|
|
167
201
|
|
|
168
202
|
## Authorization and persistence
|
|
169
203
|
|
|
@@ -228,7 +262,7 @@ removes all three rows, receipts and deliveries in one transaction.
|
|
|
228
262
|
without a new slot, even when the backlog is full.
|
|
229
263
|
- **Isolation:** each account has one receiver, one serial inbound worker and one
|
|
230
264
|
serial outbound scheduler. A slow Bot cannot block another account.
|
|
231
|
-
- **Output source:**
|
|
265
|
+
- **Output source:** externally eligible public assistant replies, including
|
|
232
266
|
background replies, come from `channelPage`. Binding starts at the current
|
|
233
267
|
contact tip, so previous history/greetings are not sent. Internal settle markers
|
|
234
268
|
are omitted; failures use a safe generic message.
|
|
@@ -236,8 +270,11 @@ removes all three rows, receipts and deliveries in one transaction.
|
|
|
236
270
|
go only to the channel that admitted the original input. Its persisted contact
|
|
237
271
|
`turnID` is the reserved `channel:*`/`weixin:*` receipt ID, and exact receipt
|
|
238
272
|
ownership by binding epoch gates delivery; another channel never broadcasts it.
|
|
239
|
-
|
|
240
|
-
|
|
273
|
+
Built-in Yuri IM replies stay in the Yuri conversation: the shared contact feed
|
|
274
|
+
resolves their persisted `turnID` to the original admitted user row and excludes
|
|
275
|
+
them from every external channel. Only proactive turns without a channel origin,
|
|
276
|
+
such as scheduled-task result reviews, may be pushed to all eligible bound
|
|
277
|
+
platforms. Selection never uses the latest conversation turn.
|
|
241
278
|
Each enterprise receipt keeps its own private reply target, so a later sender
|
|
242
279
|
cannot redirect an earlier reply. The inbox target column is added in place for
|
|
243
280
|
existing databases; legacy receipts without a target and without a fixed owner
|
|
@@ -249,11 +286,12 @@ removes all three rows, receipts and deliveries in one transaction.
|
|
|
249
286
|
- **Unknown delivery:** a crash or abort during `sending`, or an ambiguous adapter
|
|
250
287
|
error, marks the row `unknown`. It is never automatically resent and status
|
|
251
288
|
reports `delivery_unknown`. `retry` only reconnects and does not clear it.
|
|
252
|
-
Definitively failed rows may be retried explicitly
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
289
|
+
Definitively failed rows may be retried explicitly after capped automatic attempts.
|
|
290
|
+
Pending rows retain their sequence order, but `unknown` and `blocked` rows are
|
|
291
|
+
bypassed so one failed notification cannot prevent later replies or Bot execution.
|
|
292
|
+
Their durable state and error indicator survive later successful sends and restart.
|
|
293
|
+
Saving the same account/owner preserves unknown rows; disconnecting discards that
|
|
294
|
+
channel's queued replies as well as its credentials.
|
|
257
295
|
- **Reconnect:** connection failures retry with 1–30 second capped exponential
|
|
258
296
|
backoff. Authentication failure and SDK `connection_replaced` become a sticky
|
|
259
297
|
`error` until an explicit save or retry, so bad credentials or mutual-kick loops
|
|
@@ -278,7 +316,7 @@ removes all three rows, receipts and deliveries in one transaction.
|
|
|
278
316
|
|
|
279
317
|
## Not in this layer
|
|
280
318
|
|
|
281
|
-
Live typing/presence, platform media download, template cards, interactive
|
|
319
|
+
Live typing/presence, inbound platform media download, template cards, interactive
|
|
282
320
|
callbacks and per-provider protocol routing are intentionally absent. Those belong
|
|
283
321
|
to adapters or a shared SDK. This layer also does not promise server-side
|
|
284
322
|
exactly-once delivery or platform token revocation.
|