@yuriai/web 0.2.27-beta.6 → 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/README.md +8 -0
- package/dist/assets/{AgentAvatar-Bi1vvWPC.js → AgentAvatar-DNm_jMna.js} +1 -1
- package/dist/assets/AssistantContactStatus-BPRpgMFh.js +3 -0
- package/dist/assets/{AssistantUnreadBadge-DdJyySwn.js → AssistantUnreadBadge-BssH-RvV.js} +1 -1
- package/dist/assets/AssistantView-CxQv-9Ms.js +1 -0
- package/dist/assets/{DiagramView-CI4bTtxy.js → DiagramView-DPpG0fwx.js} +1 -1
- package/dist/assets/{FadeInOnReveal-BB4qEnAn.js → FadeInOnReveal-S6UbimjB.js} +1 -1
- package/dist/assets/{GuiPluginSurface-CguxFUbM.js → GuiPluginSurface-BOf0JA1T.js} +1 -1
- package/dist/assets/{GuiPluginWindow-DOJ7wuRH.js → GuiPluginWindow-OrYkfQ4m.js} +1 -1
- package/dist/assets/{Icon-t2xvbAyN.js → Icon-BE4mEwzV.js} +1 -1
- package/dist/assets/{ImageLoadingPlaceholder-D1L13TK0.js → ImageLoadingPlaceholder-CBFJC_KD.js} +1 -1
- package/dist/assets/{LoadingImage-5rY_edNm.js → LoadingImage-NLq7_P-i.js} +1 -1
- package/dist/assets/MarkdownRenderer-BaMotAOQ.js +2 -0
- package/dist/assets/{MarkdownRendererImpl-cE-0AYRg.js → MarkdownRendererImpl-Cv6UnGG7.js} +1 -1
- package/dist/assets/{MarkstreamRendererImpl-BZyqDl82.js → MarkstreamRendererImpl-Rb9O6wkW.js} +1 -1
- package/dist/assets/{MobileDetailNavigation-tbfsMfB-.js → MobileDetailNavigation-CK4KbkeS.js} +1 -1
- package/dist/assets/{MobileResizableSheet-BeRmT6_x.js → MobileResizableSheet-Deidw1br.js} +1 -1
- package/dist/assets/{MobileShareBridge-C3Equ3P_.js → MobileShareBridge-CO_OWEfq.js} +2 -2
- package/dist/assets/MobileTabPageHeader-DbC8L6A4.js +2 -0
- package/dist/assets/{MultiRunWindow-CAM2XPII.js → MultiRunWindow-DtVW9NTV.js} +1 -1
- package/dist/assets/{OnboardingScreen-4ftRerP5.js → OnboardingScreen-C8ld4wO7.js} +1 -1
- package/dist/assets/{OverlayScrollbar-CF0PCvQz.js → OverlayScrollbar-B3auC2WH.js} +1 -1
- package/dist/assets/{SettingsView-2tk4WH7R.js → SettingsView-Cm7woZWl.js} +15 -15
- package/dist/assets/{SettingsWindow-B9zbeJaW.js → SettingsWindow-CdL4PFxW.js} +1 -1
- package/dist/assets/{TerminalView-DfxvTHye.js → TerminalView-CwRB6Yyr.js} +1 -1
- package/dist/assets/{ToolOutputDialog-CrjDIhru.js → ToolOutputDialog-B4BfmoVH.js} +1 -1
- package/dist/assets/{YuriLogo-QvS1ZEHi.js → YuriLogo-BOx0s0Ue.js} +1 -1
- package/dist/assets/{appThemeRegistry-Bb_DtiV9.js → appThemeRegistry-B6woJIX5.js} +1 -1
- package/dist/assets/{assistant-attachment-cache-B9iHLlm2.js → assistant-attachment-cache-B_qRvy-k.js} +1 -1
- package/dist/assets/{button-DinGM3tu.js → button-BXxwtxsG.js} +1 -1
- package/dist/assets/{capgoAdapter-DufzjXRs.js → capgoAdapter-C5HFS-rx.js} +1 -1
- package/dist/assets/{collapsible-Bh1GLNYV.js → collapsible-BEm0787p.js} +1 -1
- package/dist/assets/context-menu-DUwvVUDY.js +1 -0
- package/dist/assets/{desktopRelayRestore-CoLLVJw8.js → desktopRelayRestore-D1UX7HW2.js} +1 -1
- package/dist/assets/device-BcRP6Tjd.js +2 -0
- package/dist/assets/{diagram-BmG-7Jb4.js → diagram-DyY8dxjZ.js} +1 -1
- package/dist/assets/{dialog-BXssfGXA.js → dialog-CLca6ojn.js} +1 -1
- package/dist/assets/dropdown-menu.styles-DGaALKP0.js +1 -0
- package/dist/assets/{es-HP_Ise0X.js → es-CrsHvgue.js} +2 -2
- package/dist/assets/feishu-Ct1ZxlzQ.svg +6 -0
- package/dist/assets/fileMentionAutocompleteState-BZJqfgae.js +2 -0
- package/dist/assets/{fileMentionTouchSelection-DSl0tj2r.js → fileMentionTouchSelection-C4dvP8GY.js} +2 -2
- package/dist/assets/{fileQueries-5a1jkme2.js → fileQueries-BeXkd3o6.js} +1 -1
- package/dist/assets/{fileReferenceActions-CGpBSFq4.js → fileReferenceActions-CrIAce3K.js} +1 -1
- package/dist/assets/{fr-D0kJXJ6U.js → fr-CEgiRlfa.js} +2 -2
- package/dist/assets/{gitApi-Wo0IZow9.js → gitApi-D2G9ny2O.js} +1 -1
- package/dist/assets/{gitApiHttp-De9liKnG.js → gitApiHttp-B7vYwgBh.js} +4 -4
- package/dist/assets/{gui-9enrif_1.js → gui-CJjEj3l4.js} +1 -1
- package/dist/assets/host-CnVx34Dh.js +1 -0
- package/dist/assets/{image-loading-Dq1g_wpm.js → image-loading-D5UhMj7Z.js} +1 -1
- package/dist/assets/{input-v8-rVbo-.js → input-D59Q2pm8.js} +1 -1
- package/dist/assets/insertionBoundaries-CedfTSM5.js +1 -0
- package/dist/assets/{ja-BjHKXazE.js → ja-CX-_p9jZ.js} +2 -2
- package/dist/assets/{ko-Llwnl876.js → ko-D5xlLYJM.js} +2 -2
- package/dist/assets/main-CxpUooVS.js +2 -0
- package/dist/assets/main-Dpm4KxWX.js +169 -0
- package/dist/assets/{markdownHeightCache-BVV2dktw.js → markdownHeightCache-D2VWb_4E.js} +1 -1
- package/dist/assets/{mcpQueries-BoBrB4a4.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-DMSzAvb6.js → mobileBackNavigation-BJqt3oHk.js} +1 -1
- package/dist/assets/{multirun-BhWttsRQ.js → multirun-BILeETbd.js} +1 -1
- package/dist/assets/{native-DSvNnKAm.js → native-BYMhr-xH.js} +1 -1
- package/dist/assets/{native-config-DjdrW_9n.js → native-config-BIY0Cr-l.js} +1 -1
- package/dist/assets/{persistence-CfrHIiIS.js → persistence-CWuLIq1J.js} +2 -2
- package/dist/assets/{pl-BOd_idqn.js → pl-BjH81A_q.js} +2 -2
- package/dist/assets/projectMeta-B9_Uh8YH.js +1 -0
- package/dist/assets/{pt-BR-B8djxMsl.js → pt-BR-Dp5oK2x3.js} +2 -2
- package/dist/assets/{queryRuntime-Dkpnq3Z1.js → queryRuntime-CTcigFdj.js} +1 -1
- package/dist/assets/queue-abort-optimistic-CS5jTJq-.js +1 -0
- package/dist/assets/react-DRe85o5s.js +1 -0
- package/dist/assets/readiness-BNIgH81X.js +1 -0
- package/dist/assets/renderElectronMiniChatApp-asOqV7FB.js +2 -0
- package/dist/assets/renderMobileApp-DAfque7-.js +28 -0
- package/dist/assets/runtime-fetch-D0YWp3FJ.js +1 -0
- package/dist/assets/{runtime-file-download-CcNIVp7u.js → runtime-file-download-Cf1WxfeW.js} +1 -1
- package/dist/assets/{runtime-file-resource-6np6lzoV.js → runtime-file-resource-UJjHUF2C.js} +1 -1
- package/dist/assets/runtimeConfig-BX8JirIc.js +2 -0
- package/dist/assets/runtimeEndpointReset-CFQgUiUr.js +149 -0
- package/dist/assets/scheduledTaskNavigation-EomDNVu2.js +1 -0
- package/dist/assets/sessionLookup-BxAaxPOw.js +1 -0
- package/dist/assets/{sortable-tabs-strip-yKyqsqut.js → sortable-tabs-strip-DpbW5MqX.js} +1 -1
- package/dist/assets/speechSettingsQueries-CC0fusUo.js +4 -0
- package/dist/assets/src-BMKjN2ip.css +2 -0
- package/dist/assets/{store-CUBymvAC.js → store-CkIHVrmT.js} +4 -3
- package/dist/assets/{terminalApi-BblSkqch.js → terminalApi-rl9mZAlB.js} +1 -1
- package/dist/assets/{textarea-B39OCNPV.js → textarea-7eTptEtD.js} +1 -1
- package/dist/assets/{tooltip-d8TNN4lx.js → tooltip-BPwoSTzO.js} +1 -1
- package/dist/assets/types-CUtSV1V8.js +2 -0
- package/dist/assets/{uk-BNd7CjVO.js → uk-BEfkaLtM.js} +2 -2
- package/dist/assets/{useAppFontEffects-BVoVpJYO.js → useAppFontEffects-Ck2PdQRx.js} +52 -52
- package/dist/assets/{useAssistantUIStore-Bp6TxG5E.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-BFyBjtQ6.js → useFontPreferences-DeqW9nRx.js} +1 -1
- package/dist/assets/useMobileNavigationStore-BmVr5a5c.js +17 -0
- package/dist/assets/useUIStore-C6CE42oh.js +1 -0
- package/dist/assets/{useWorkerHighlightedLines-CPxG8Fya.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-CgKz9Hcy.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-BkTAEOZG.js → zh-CN-DipfA56P.js} +2 -2
- package/dist/assets/{zh-TW-x8SfMzfc.js → zh-TW-B7pXMVjt.js} +2 -2
- package/dist/index.html +18 -18
- package/dist/mini-chat.html +18 -18
- package/dist/mobile.html +18 -18
- package/package.json +6 -1
- package/server/index.js +2 -145
- package/server/lib/assistants/DOCUMENTATION.md +69 -11
- 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 +329 -0
- package/server/lib/assistants/channels/PROTOCOLS.md +250 -0
- package/server/lib/assistants/channels/dingtalk-chat-adapter.js +645 -0
- package/server/lib/assistants/channels/dingtalk-chat-adapter.test.js +423 -0
- package/server/lib/assistants/channels/integration.test.js +44 -0
- package/server/lib/assistants/channels/routes.js +59 -0
- package/server/lib/assistants/channels/routes.test.js +139 -0
- package/server/lib/assistants/channels/runtime.js +958 -0
- package/server/lib/assistants/channels/runtime.test.js +897 -0
- package/server/lib/assistants/channels/sdk-adapters.js +383 -0
- package/server/lib/assistants/channels/sdk-adapters.test.js +384 -0
- package/server/lib/assistants/channels/store.js +249 -0
- package/server/lib/assistants/channels/store.test.js +296 -0
- package/server/lib/assistants/channels/transport.js +42 -0
- package/server/lib/assistants/channels/transport.test.js +23 -0
- package/server/lib/assistants/channels/wecom-chat-adapter.js +703 -0
- package/server/lib/assistants/channels/wecom-chat-adapter.test.js +130 -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/message-channel-origin.js +7 -0
- package/server/lib/assistants/resource-reference.integration.test.js +284 -0
- package/server/lib/assistants/routes.js +14 -4
- package/server/lib/assistants/service.js +116 -69
- package/server/lib/assistants/service.test.js +31 -17
- package/server/lib/assistants/weixin/DOCUMENTATION.md +121 -14
- package/server/lib/assistants/weixin/api.js +45 -1
- package/server/lib/assistants/weixin/api.test.js +28 -0
- package/server/lib/assistants/weixin/chat-adapter.js +113 -0
- package/server/lib/assistants/weixin/chat-adapter.test.js +62 -0
- package/server/lib/assistants/weixin/inbound.js +149 -0
- package/server/lib/assistants/weixin/inbound.test.js +141 -0
- package/server/lib/assistants/weixin/media.js +79 -0
- package/server/lib/assistants/weixin/media.test.js +66 -0
- package/server/lib/assistants/weixin/runtime.js +105 -74
- package/server/lib/assistants/weixin/runtime.test.js +192 -4
- package/server/lib/assistants/weixin/store.js +16 -8
- package/server/lib/assistants/weixin/store.test.js +93 -0
- package/server/lib/assistants/weixin/toon.js +49 -0
- package/server/lib/assistants/weixin/toon.test.js +25 -0
- package/server/lib/assistants/yuri-project.http.test.js +9 -3
- package/server/lib/github/DOCUMENTATION.md +9 -8
- package/server/lib/llm/bridge-plugin.bundle.js +188 -188
- 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 +10 -1
- 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-CWq-PDz6.js +0 -3
- package/dist/assets/AssistantView-Br1iETTs.js +0 -1
- package/dist/assets/MarkdownRenderer-BlGxjOei.js +0 -2
- package/dist/assets/MobileTabPageHeader-DQRqAAEf.js +0 -2
- package/dist/assets/context-menu-DpbkRkk1.js +0 -1
- package/dist/assets/device-BjRYYXNT.js +0 -2
- package/dist/assets/host-FR_grJxM.js +0 -1
- package/dist/assets/insertionBoundaries-CZz7TWJv.js +0 -1
- package/dist/assets/main-Bp9HHqTr.js +0 -2
- package/dist/assets/main-DOwAfmxJ.js +0 -169
- package/dist/assets/miniChat-BJDgCrfF.js +0 -2
- package/dist/assets/mobile-GM9931J_.js +0 -2
- package/dist/assets/projectMeta-31PobC-M.js +0 -1
- package/dist/assets/react-B9v0P7kT.js +0 -1
- package/dist/assets/readiness-CYNnsekT.js +0 -1
- package/dist/assets/renderElectronMiniChatApp-N2r999Bf.js +0 -2
- package/dist/assets/renderMobileApp-CU1ec6Ak.js +0 -28
- package/dist/assets/runtime-fetch-D4ja813o.js +0 -1
- package/dist/assets/runtimeConfig-Db7yfo6L.js +0 -2
- package/dist/assets/runtimeEndpointReset-DO5WVetV.js +0 -149
- package/dist/assets/scheduledTaskNavigation-Bsrhv3Ow.js +0 -1
- package/dist/assets/sessionLookup-DRVy7LM1.js +0 -1
- package/dist/assets/speechSettingsQueries-DU25rmGc.js +0 -4
- package/dist/assets/src-Bckdo2XS.css +0 -2
- package/dist/assets/types-BqFQhQTt.js +0 -2
- package/dist/assets/useDesktopWindowControlsLayout-CYx1LNO5.js +0 -1
- package/dist/assets/useEffectiveDirectory-D8FFE5Yn.js +0 -1
- package/dist/assets/useMobileNavigationStore-BHMFeMjo.js +0 -23
- package/dist/assets/useUIStore-BbYD420L.js +0 -1
- package/dist/assets/vendor-opencode-client-UL_TecvQ.js +0 -12
- package/dist/assets/vendor-solid-js-0T254UHG.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,383 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { createLarkChannel, normalize } from '@larksuite/channel';
|
|
3
|
+
import { createDingtalkSdkAdapter } from './dingtalk-chat-adapter.js';
|
|
4
|
+
import { createWecomSdkAdapter as createOfficialWecomSdkAdapter } from './wecom-chat-adapter.js';
|
|
5
|
+
import { channelError, requestJSON } from './transport.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Bridges the vendor-maintained SDKs into Yuri's channel contract.
|
|
9
|
+
*
|
|
10
|
+
* Feishu inbound uses the official `@larksuite/channel` long connection; the
|
|
11
|
+
* Chat SDK adapter is kept for its standard `Message` parser only. Its raw-event
|
|
12
|
+
* hook is public and installed from `channelFactory` before `connect()`, so the
|
|
13
|
+
* platform ACK always waits for the durable receipt.
|
|
14
|
+
*
|
|
15
|
+
* WeCom uses the official SDK through `wecom-chat-adapter.js`, including awaited
|
|
16
|
+
* platform send acknowledgements. No community fire-and-forget path is used.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const FEISHU_ORIGIN = 'https://open.feishu.cn';
|
|
20
|
+
const FEISHU_TOKEN_PATH = '/open-apis/auth/v3/tenant_access_token/internal';
|
|
21
|
+
const FEISHU_MESSAGE_PATH = '/open-apis/im/v1/messages';
|
|
22
|
+
const TOKEN_CACHE_LIMIT = 4;
|
|
23
|
+
const TOKEN_EXPIRY_MARGIN_MS = 180_000;
|
|
24
|
+
const INIT_TIMEOUT_MS = 20_000;
|
|
25
|
+
const DISCONNECT_TIMEOUT_MS = 5_000;
|
|
26
|
+
const MONITOR_INTERVAL_MS = 1_000;
|
|
27
|
+
const MAX_TEXT_CHARS = 20_000;
|
|
28
|
+
const MAX_RESOURCES = 8;
|
|
29
|
+
const MAX_NAME_CHARS = 120;
|
|
30
|
+
|
|
31
|
+
function nonEmptyString(value) {
|
|
32
|
+
return typeof value === 'string' && value.length > 0;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function requireConfig(config) {
|
|
36
|
+
if (!config || typeof config !== 'object') throw channelError('invalid_response');
|
|
37
|
+
if (!nonEmptyString(config.clientID) || !nonEmptyString(config.clientSecret)
|
|
38
|
+
|| (config.ownerID !== undefined && typeof config.ownerID !== 'string')) {
|
|
39
|
+
throw channelError('invalid_response');
|
|
40
|
+
}
|
|
41
|
+
return { clientID: config.clientID, clientSecret: config.clientSecret, ownerID: config.ownerID ?? '' };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function credentialKey(clientID, clientSecret) {
|
|
45
|
+
return `${clientID}\u0000${clientSecret}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function truncateText(value, limit = MAX_TEXT_CHARS) {
|
|
49
|
+
const chars = Array.from(typeof value === 'string' ? value : '');
|
|
50
|
+
if (chars.length <= limit) return typeof value === 'string' ? value : '';
|
|
51
|
+
return chars.slice(0, limit).join('');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function truncateName(value) {
|
|
55
|
+
if (!nonEmptyString(value)) return undefined;
|
|
56
|
+
return Array.from(value).slice(0, MAX_NAME_CHARS).join('');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Map vendor failures to the safe codes the channel runtime understands. */
|
|
60
|
+
function vendorCode(error) {
|
|
61
|
+
const status = Number(error?.statusCode ?? error?.status ?? error?.response?.status);
|
|
62
|
+
if (status === 401 || status === 403) return 'auth_expired';
|
|
63
|
+
if (status >= 400) return 'network_error';
|
|
64
|
+
const name = String(error?.name ?? '');
|
|
65
|
+
if (/auth/i.test(name)) return 'auth_expired';
|
|
66
|
+
return 'network_error';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Inert logger. Vendor log messages can contain URLs, IDs or upstream bodies,
|
|
71
|
+
* so nothing is forwarded: only Yuri's own error codes leave this module.
|
|
72
|
+
*/
|
|
73
|
+
function noopLogger() {
|
|
74
|
+
const logger = {
|
|
75
|
+
child: () => logger,
|
|
76
|
+
debug: () => {},
|
|
77
|
+
info: () => {},
|
|
78
|
+
warn: () => {},
|
|
79
|
+
error: () => {},
|
|
80
|
+
trace: () => {},
|
|
81
|
+
};
|
|
82
|
+
return logger;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function pruneCache(cache, limit) {
|
|
86
|
+
while (cache.size > limit) cache.delete(cache.keys().next().value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** True only for Yuri channel errors, which always carry a boolean `ambiguous`. */
|
|
90
|
+
function isYuriError(error) {
|
|
91
|
+
return typeof error?.code === 'string' && typeof error?.ambiguous === 'boolean';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function bounded(promise, signal, { timeoutMs = INIT_TIMEOUT_MS, ambiguous = false } = {}) {
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
let settled = false;
|
|
97
|
+
const finish = (settle, value) => {
|
|
98
|
+
if (settled) return;
|
|
99
|
+
settled = true;
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
signal?.removeEventListener('abort', onAbort);
|
|
102
|
+
settle(value);
|
|
103
|
+
};
|
|
104
|
+
const timer = setTimeout(() => finish(reject, channelError('network_error', ambiguous)), timeoutMs);
|
|
105
|
+
timer.unref?.();
|
|
106
|
+
const onAbort = () => finish(reject, channelError('cancelled', ambiguous));
|
|
107
|
+
if (signal?.aborted) {
|
|
108
|
+
onAbort();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
112
|
+
promise.then(
|
|
113
|
+
(value) => finish(resolve, value),
|
|
114
|
+
(error) => finish(reject, isYuriError(error) ? error : channelError('network_error', ambiguous)),
|
|
115
|
+
);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function settleQuietly(promise) {
|
|
120
|
+
try {
|
|
121
|
+
await bounded(Promise.resolve(promise), undefined, { timeoutMs: DISCONNECT_TIMEOUT_MS });
|
|
122
|
+
} catch {}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function feishuAccessToken(tokens, { clientID, clientSecret, signal, fetchImpl }) {
|
|
126
|
+
const key = credentialKey(clientID, clientSecret);
|
|
127
|
+
const cached = tokens.get(key);
|
|
128
|
+
if (cached && cached.expiresAt > Date.now()) return cached.value;
|
|
129
|
+
tokens.delete(key);
|
|
130
|
+
const json = await requestJSON(`${FEISHU_ORIGIN}${FEISHU_TOKEN_PATH}`, {
|
|
131
|
+
body: { app_id: clientID, app_secret: clientSecret },
|
|
132
|
+
signal,
|
|
133
|
+
fetchImpl,
|
|
134
|
+
});
|
|
135
|
+
if (json?.code !== 0 || !nonEmptyString(json?.tenant_access_token)) throw channelError('auth_expired');
|
|
136
|
+
const expiresIn = Number(json.expire);
|
|
137
|
+
tokens.set(key, {
|
|
138
|
+
value: json.tenant_access_token,
|
|
139
|
+
expiresAt: Date.now() + Math.max(60_000, (Number.isFinite(expiresIn) ? expiresIn * 1000 : 0) - TOKEN_EXPIRY_MARGIN_MS),
|
|
140
|
+
});
|
|
141
|
+
// Tokens are pure caches: evicting one only costs a re-fetch, never a
|
|
142
|
+
// connection. Active sockets are never pruned.
|
|
143
|
+
pruneCache(tokens, TOKEN_CACHE_LIMIT);
|
|
144
|
+
return json.tenant_access_token;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Standard Chat `Message` → Yuri projection. */
|
|
148
|
+
function projectFeishu(message) {
|
|
149
|
+
const normalized = message?.raw;
|
|
150
|
+
if (!normalized || normalized.chatType !== 'p2p' || normalized.senderType !== 'user') return null;
|
|
151
|
+
const senderID = message.author?.userId ?? normalized.senderId;
|
|
152
|
+
const resources = Array.isArray(normalized.resources)
|
|
153
|
+
? normalized.resources.slice(0, MAX_RESOURCES).map((resource) => {
|
|
154
|
+
const projected = { type: nonEmptyString(resource?.type) ? resource.type : 'file' };
|
|
155
|
+
const name = truncateName(resource?.fileName);
|
|
156
|
+
if (name) projected.name = name;
|
|
157
|
+
const duration = Number(resource?.durationMs);
|
|
158
|
+
if (Number.isFinite(duration) && duration >= 0) projected.durationMs = duration;
|
|
159
|
+
return projected;
|
|
160
|
+
})
|
|
161
|
+
: [];
|
|
162
|
+
const metadata = { platform: 'feishu', chatType: 'private' };
|
|
163
|
+
if (resources.length > 0) {
|
|
164
|
+
metadata.notDownloaded = true;
|
|
165
|
+
metadata.resources = resources;
|
|
166
|
+
}
|
|
167
|
+
const mentionNames = Array.isArray(normalized.mentions)
|
|
168
|
+
? normalized.mentions.map((mention) => truncateName(mention?.name)).filter(Boolean).slice(0, MAX_RESOURCES)
|
|
169
|
+
: [];
|
|
170
|
+
if (mentionNames.length > 0) metadata.mentions = mentionNames;
|
|
171
|
+
return {
|
|
172
|
+
id: String(message?.id ?? normalized.messageId ?? ''),
|
|
173
|
+
senderID: String(senderID ?? ''),
|
|
174
|
+
chatType: 'private',
|
|
175
|
+
text: truncateText(message?.text ?? normalized.content),
|
|
176
|
+
metadata,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function acceptsOwner(projected, ownerID) {
|
|
181
|
+
return Boolean(projected) && projected.senderID.length > 0 && (!ownerID || projected.senderID === ownerID);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Minimal ChatInstance substitute. Chat core state, locks, dedupe keys and
|
|
186
|
+
* thread history are not used; the Feishu adapter gets a logger and inert
|
|
187
|
+
* message sinks because the raw gate owns delivery.
|
|
188
|
+
*/
|
|
189
|
+
function createLarkHost() {
|
|
190
|
+
const logger = noopLogger();
|
|
191
|
+
return {
|
|
192
|
+
getLogger: () => logger,
|
|
193
|
+
async processMessage() {},
|
|
194
|
+
async processAction() {},
|
|
195
|
+
processReaction() {},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Feishu bridge.
|
|
201
|
+
*
|
|
202
|
+
* ACK gating: `@larksuite/channel` replies to Feishu only after awaited
|
|
203
|
+
* `onRawEvent` handlers, while the normalized `message` pipeline is not
|
|
204
|
+
* awaited. The gate is registered from `channelFactory`, i.e. before the
|
|
205
|
+
* adapter calls `connect()`, so there is no unguarded window.
|
|
206
|
+
*/
|
|
207
|
+
export function createFeishuSdkAdapter({
|
|
208
|
+
adapterFactory,
|
|
209
|
+
channelFactory,
|
|
210
|
+
normalizeEvent = normalize,
|
|
211
|
+
fetchImpl = fetch,
|
|
212
|
+
} = {}) {
|
|
213
|
+
const tokens = new Map();
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
async connect({ config, signal, onReady, onMessage, onError } = {}) {
|
|
217
|
+
const { clientID, clientSecret, ownerID } = requireConfig(config);
|
|
218
|
+
signal?.throwIfAborted();
|
|
219
|
+
let adapter;
|
|
220
|
+
let channel;
|
|
221
|
+
let offRaw;
|
|
222
|
+
let initPromise;
|
|
223
|
+
let stopped = false;
|
|
224
|
+
|
|
225
|
+
const stop = async () => {
|
|
226
|
+
if (stopped) return;
|
|
227
|
+
stopped = true;
|
|
228
|
+
const activeAdapter = adapter;
|
|
229
|
+
const activeChannel = channel;
|
|
230
|
+
adapter = undefined;
|
|
231
|
+
channel = undefined;
|
|
232
|
+
try {
|
|
233
|
+
offRaw?.();
|
|
234
|
+
} catch {}
|
|
235
|
+
offRaw = undefined;
|
|
236
|
+
await settleQuietly(activeAdapter?.disconnect?.());
|
|
237
|
+
// `channel.disconnect()` is a no-op while the handshake is still in
|
|
238
|
+
// flight. Once the aborted initialize settles, close the socket it may
|
|
239
|
+
// have opened, so an abort cannot leak a connection.
|
|
240
|
+
if (initPromise) {
|
|
241
|
+
void initPromise
|
|
242
|
+
.then(() => settleQuietly(activeChannel?.disconnect?.()))
|
|
243
|
+
.catch(() => {});
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const createChannel = channelFactory ?? ((options) => createLarkChannel({ ...options, retry: { maxAttempts: 1 } }));
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
const factory = adapterFactory ?? (await import('@larksuite/vercel-chat-adapter')).createLarkAdapter;
|
|
251
|
+
adapter = await factory({
|
|
252
|
+
appId: clientID,
|
|
253
|
+
appSecret: clientSecret,
|
|
254
|
+
logger: noopLogger(),
|
|
255
|
+
channelFactory: (options) => {
|
|
256
|
+
const created = createChannel(options);
|
|
257
|
+
if (!created || typeof created.onRawEvent !== 'function' || typeof created.getBotIdentity !== 'function') {
|
|
258
|
+
throw channelError('invalid_response');
|
|
259
|
+
}
|
|
260
|
+
channel = created;
|
|
261
|
+
offRaw = created.onRawEvent('im.message.receive_v1', async (raw) => {
|
|
262
|
+
if (stopped || !adapter) return;
|
|
263
|
+
const normalized = await normalizeEvent(raw, {
|
|
264
|
+
botIdentity: created.getBotIdentity(),
|
|
265
|
+
stripBotMentions: true,
|
|
266
|
+
});
|
|
267
|
+
const projected = projectFeishu(adapter.parseMessage(normalized));
|
|
268
|
+
if (!acceptsOwner(projected, ownerID)) return;
|
|
269
|
+
await onMessage?.(projected);
|
|
270
|
+
});
|
|
271
|
+
return created;
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
initPromise = adapter.initialize(createLarkHost());
|
|
275
|
+
await bounded(initPromise, signal);
|
|
276
|
+
if (signal?.aborted || stopped) {
|
|
277
|
+
await stop();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
// Re-arm readiness on the SDK's own reconnect events; otherwise a
|
|
281
|
+
// transient error would leave runtime.ready false forever.
|
|
282
|
+
channel?.on?.('reconnecting', () => onError?.(channelError('network_error')));
|
|
283
|
+
channel?.on?.('reconnected', () => onReady?.());
|
|
284
|
+
onReady?.();
|
|
285
|
+
await new Promise((resolve) => {
|
|
286
|
+
let settled = false;
|
|
287
|
+
let monitor;
|
|
288
|
+
const finish = () => {
|
|
289
|
+
if (settled) return;
|
|
290
|
+
settled = true;
|
|
291
|
+
clearInterval(monitor);
|
|
292
|
+
signal?.removeEventListener('abort', finish);
|
|
293
|
+
stop().finally(resolve);
|
|
294
|
+
};
|
|
295
|
+
// The vendor retries a bounded number of times and then sets a
|
|
296
|
+
// terminal `failed` state without emitting `reconnected`. Hand the
|
|
297
|
+
// lane back to Yuri's runtime so it can open a fresh adapter.
|
|
298
|
+
monitor = setInterval(() => {
|
|
299
|
+
if (settled) return;
|
|
300
|
+
let status;
|
|
301
|
+
try {
|
|
302
|
+
status = channel?.getConnectionStatus?.();
|
|
303
|
+
} catch {
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (status?.state !== 'failed') return;
|
|
307
|
+
onError?.(channelError('network_error'));
|
|
308
|
+
finish();
|
|
309
|
+
}, MONITOR_INTERVAL_MS);
|
|
310
|
+
monitor.unref?.();
|
|
311
|
+
signal?.addEventListener('abort', finish, { once: true });
|
|
312
|
+
if (signal?.aborted) finish();
|
|
313
|
+
});
|
|
314
|
+
} catch (error) {
|
|
315
|
+
await stop();
|
|
316
|
+
throw isYuriError(error) ? error : channelError(vendorCode(error));
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Feishu has no public idempotent send in either SDK: the Chat adapter
|
|
322
|
+
* always converts to markdown/post and retries internally, and
|
|
323
|
+
* `@larksuite/channel` exposes no `uuid`. The documented HTTP call keeps
|
|
324
|
+
* `msg_type: text` and a stable `uuid` for one attempt.
|
|
325
|
+
*/
|
|
326
|
+
async send({ config, target, text, image, id, signal, beforeSend } = {}) {
|
|
327
|
+
const { clientID, clientSecret, ownerID } = requireConfig(config);
|
|
328
|
+
const userID = ownerID || target?.userID;
|
|
329
|
+
if (!nonEmptyString(userID)) throw channelError('invalid_response');
|
|
330
|
+
const token = await feishuAccessToken(tokens, { clientID, clientSecret, signal, fetchImpl });
|
|
331
|
+
let imageKey;
|
|
332
|
+
if (image) {
|
|
333
|
+
const form = new FormData();
|
|
334
|
+
form.set('image_type', 'message');
|
|
335
|
+
form.set('image', new Blob([image.buffer], { type: image.mime }), image.filename);
|
|
336
|
+
const uploaded = await requestJSON(`${FEISHU_ORIGIN}/open-apis/im/v1/images`, {
|
|
337
|
+
body: form, headers: { Authorization: `Bearer ${token}` }, signal, fetchImpl,
|
|
338
|
+
});
|
|
339
|
+
if (uploaded?.code !== 0 || !nonEmptyString(uploaded?.data?.image_key)) throw channelError('delivery_failed');
|
|
340
|
+
imageKey = uploaded.data.image_key;
|
|
341
|
+
}
|
|
342
|
+
const uuid = outboundUuid(id);
|
|
343
|
+
beforeSend?.();
|
|
344
|
+
const json = await requestJSON(`${FEISHU_ORIGIN}${FEISHU_MESSAGE_PATH}?receive_id_type=open_id`, {
|
|
345
|
+
body: {
|
|
346
|
+
receive_id: userID,
|
|
347
|
+
msg_type: image ? 'image' : 'text',
|
|
348
|
+
content: JSON.stringify(image ? { image_key: imageKey } : { text: typeof text === 'string' ? text : '' }),
|
|
349
|
+
...(uuid ? { uuid } : {}),
|
|
350
|
+
},
|
|
351
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
352
|
+
signal,
|
|
353
|
+
fetchImpl,
|
|
354
|
+
sending: true,
|
|
355
|
+
});
|
|
356
|
+
if (json?.code !== 0) {
|
|
357
|
+
throw channelError([99991663, 99991664, 99991661, 10003, 10014].includes(json?.code) ? 'auth_expired' : 'invalid_response');
|
|
358
|
+
}
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Message de-duplication ID: stable per outbound row, <= 50 chars as required. */
|
|
364
|
+
export function outboundUuid(id) {
|
|
365
|
+
const value = typeof id === 'string' ? id : id == null ? '' : String(id);
|
|
366
|
+
if (!value) return undefined;
|
|
367
|
+
if (value.length <= 50) return value;
|
|
368
|
+
return createHash('sha256').update(value).digest('hex').slice(0, 40);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Provider registry consumed by the assistant channel routes.
|
|
373
|
+
*/
|
|
374
|
+
export function createAssistantChannelAdapters(options = {}) {
|
|
375
|
+
const { feishu = {}, wecom = {}, dingtalk = {} } = options;
|
|
376
|
+
return {
|
|
377
|
+
feishu: createFeishuSdkAdapter(feishu),
|
|
378
|
+
wecom: createOfficialWecomSdkAdapter(wecom),
|
|
379
|
+
dingtalk: createDingtalkSdkAdapter(dingtalk),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
export const createWecomSdkAdapter = createOfficialWecomSdkAdapter;
|