@kodelyth/feishu 2026.5.39 → 2026.5.42
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/api.ts +32 -0
- package/channel-entry.ts +20 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +16 -0
- package/dist/accounts-D0ow-lRb.js +429 -0
- package/dist/api.js +2308 -0
- package/dist/app-registration-DBSnysKJ.js +184 -0
- package/dist/audio-preflight.runtime-Dpjbn-7r.js +7 -0
- package/dist/channel-13WQvQ0u.js +2115 -0
- package/dist/channel-entry.js +22 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-JMJonrJ4.js +729 -0
- package/dist/client-D1pzbBGo.js +157 -0
- package/dist/contract-api.js +9 -0
- package/dist/conversation-id-_58ecqlx.js +139 -0
- package/dist/drive-CgHOluXx.js +883 -0
- package/dist/index.js +68 -0
- package/dist/monitor-oWptK0zL.js +60 -0
- package/dist/monitor.account-DHaWlslg.js +5207 -0
- package/dist/monitor.state-C211a4tX.js +100 -0
- package/dist/probe-CF4duEpK.js +149 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/runtime-DSh5rL_d.js +8 -0
- package/dist/runtime-api.js +14 -0
- package/dist/secret-contract-NSee-WzN.js +119 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/security-audit-DWVC0vSK.js +11 -0
- package/dist/security-audit-shared-Dpcwxeft.js +38 -0
- package/dist/security-contract-api.js +2 -0
- package/dist/send-DfZuV4Fi.js +1212 -0
- package/dist/session-conversation-Duaukbnl.js +27 -0
- package/dist/session-key-api.js +2 -0
- package/dist/setup-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/subagent-hooks-Dtegs0kh.js +235 -0
- package/dist/subagent-hooks-api.js +23 -0
- package/dist/targets-DFskxX4p.js +48 -0
- package/dist/thread-bindings-DI7lVSOE.js +222 -0
- package/index.ts +82 -0
- package/klaw.plugin.json +47 -1712
- package/package.json +4 -4
- package/runtime-api.ts +52 -0
- package/secret-contract-api.ts +5 -0
- package/security-contract-api.ts +1 -0
- package/session-key-api.ts +1 -0
- package/setup-api.ts +3 -0
- package/setup-entry.test.ts +19 -0
- package/setup-entry.ts +13 -0
- package/src/accounts.test.ts +480 -0
- package/src/accounts.ts +333 -0
- package/src/agent-config.ts +21 -0
- package/src/app-registration.ts +331 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +25 -0
- package/src/async.test.ts +35 -0
- package/src/async.ts +104 -0
- package/src/audio-preflight.runtime.ts +9 -0
- package/src/bitable.test.ts +136 -0
- package/src/bitable.ts +762 -0
- package/src/bot-content.ts +485 -0
- package/src/bot-group-name.test.ts +116 -0
- package/src/bot-runtime-api.ts +12 -0
- package/src/bot-sender-name.ts +125 -0
- package/src/bot.broadcast.test.ts +523 -0
- package/src/bot.card-action.test.ts +552 -0
- package/src/bot.checkBotMentioned.test.ts +265 -0
- package/src/bot.helpers.test.ts +135 -0
- package/src/bot.stripBotMention.test.ts +126 -0
- package/src/bot.test.ts +3671 -0
- package/src/bot.ts +1703 -0
- package/src/card-action.ts +447 -0
- package/src/card-interaction.test.ts +131 -0
- package/src/card-interaction.ts +159 -0
- package/src/card-test-helpers.ts +54 -0
- package/src/card-ux-approval.ts +65 -0
- package/src/card-ux-launcher.test.ts +106 -0
- package/src/card-ux-launcher.ts +121 -0
- package/src/card-ux-shared.ts +33 -0
- package/src/channel-runtime-api.ts +16 -0
- package/src/channel.runtime.ts +47 -0
- package/src/channel.test.ts +1151 -0
- package/src/channel.ts +1423 -0
- package/src/chat-schema.ts +25 -0
- package/src/chat.test.ts +240 -0
- package/src/chat.ts +188 -0
- package/src/client-timeout.ts +42 -0
- package/src/client.test.ts +447 -0
- package/src/client.ts +262 -0
- package/src/comment-dispatcher-runtime-api.ts +6 -0
- package/src/comment-dispatcher.test.ts +185 -0
- package/src/comment-dispatcher.ts +107 -0
- package/src/comment-handler-runtime-api.ts +3 -0
- package/src/comment-handler.test.ts +592 -0
- package/src/comment-handler.ts +303 -0
- package/src/comment-reaction.test.ts +138 -0
- package/src/comment-reaction.ts +259 -0
- package/src/comment-shared.test.ts +183 -0
- package/src/comment-shared.ts +406 -0
- package/src/comment-target.ts +44 -0
- package/src/config-schema.test.ts +326 -0
- package/src/config-schema.ts +335 -0
- package/src/conversation-id.test.ts +18 -0
- package/src/conversation-id.ts +199 -0
- package/src/dedup-runtime-api.ts +1 -0
- package/src/dedup.ts +141 -0
- package/src/dedupe-key.ts +72 -0
- package/src/directory.static.ts +61 -0
- package/src/directory.test.ts +141 -0
- package/src/directory.ts +124 -0
- package/src/doc-schema.ts +182 -0
- package/src/docx-batch-insert.test.ts +116 -0
- package/src/docx-batch-insert.ts +223 -0
- package/src/docx-color-text.ts +154 -0
- package/src/docx-table-ops.test.ts +53 -0
- package/src/docx-table-ops.ts +316 -0
- package/src/docx-types.ts +38 -0
- package/src/docx.account-selection.test.ts +95 -0
- package/src/docx.test.ts +701 -0
- package/src/docx.ts +1596 -0
- package/src/drive-schema.ts +92 -0
- package/src/drive.test.ts +1237 -0
- package/src/drive.ts +829 -0
- package/src/dynamic-agent.test.ts +155 -0
- package/src/dynamic-agent.ts +143 -0
- package/src/event-types.ts +45 -0
- package/src/external-keys.test.ts +20 -0
- package/src/external-keys.ts +19 -0
- package/src/lifecycle.test-support.ts +220 -0
- package/src/media.test.ts +955 -0
- package/src/media.ts +1105 -0
- package/src/mention-target.types.ts +5 -0
- package/src/mention.ts +114 -0
- package/src/message-action-contract.ts +13 -0
- package/src/monitor-state-runtime-api.ts +7 -0
- package/src/monitor-transport-runtime-api.ts +10 -0
- package/src/monitor.account.ts +492 -0
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +219 -0
- package/src/monitor.bot-identity.ts +86 -0
- package/src/monitor.bot-menu-handler.ts +165 -0
- package/src/monitor.bot-menu.lifecycle.test-support.ts +224 -0
- package/src/monitor.bot-menu.test.ts +188 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +421 -0
- package/src/monitor.cleanup.test.ts +383 -0
- package/src/monitor.comment-notice-handler.ts +105 -0
- package/src/monitor.comment.test.ts +967 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +4 -0
- package/src/monitor.message-handler.ts +350 -0
- package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
- package/src/monitor.reaction.test.ts +739 -0
- package/src/monitor.startup.test.ts +213 -0
- package/src/monitor.startup.ts +74 -0
- package/src/monitor.state.defaults.test.ts +46 -0
- package/src/monitor.state.ts +170 -0
- package/src/monitor.synthetic-error.ts +18 -0
- package/src/monitor.test-mocks.ts +46 -0
- package/src/monitor.transport.ts +451 -0
- package/src/monitor.ts +100 -0
- package/src/monitor.webhook-e2e.test.ts +279 -0
- package/src/monitor.webhook-security.test.ts +389 -0
- package/src/monitor.webhook.test-helpers.ts +116 -0
- package/src/outbound-runtime-api.ts +1 -0
- package/src/outbound.test.ts +1118 -0
- package/src/outbound.ts +785 -0
- package/src/perm-schema.ts +52 -0
- package/src/perm.ts +170 -0
- package/src/pins.ts +108 -0
- package/src/policy.test.ts +223 -0
- package/src/policy.ts +318 -0
- package/src/post.test.ts +105 -0
- package/src/post.ts +275 -0
- package/src/probe.test.ts +283 -0
- package/src/probe.ts +166 -0
- package/src/processing-claims.ts +59 -0
- package/src/qr-terminal.ts +1 -0
- package/src/reactions.ts +123 -0
- package/src/reasoning-preview.test.ts +113 -0
- package/src/reasoning-preview.ts +28 -0
- package/src/reply-dispatcher-runtime-api.ts +7 -0
- package/src/reply-dispatcher.test.ts +1513 -0
- package/src/reply-dispatcher.ts +748 -0
- package/src/runtime.ts +9 -0
- package/src/secret-contract.ts +145 -0
- package/src/secret-input.ts +1 -0
- package/src/security-audit-shared.ts +69 -0
- package/src/security-audit.test.ts +59 -0
- package/src/security-audit.ts +1 -0
- package/src/send-result.ts +80 -0
- package/src/send-target.test.ts +86 -0
- package/src/send-target.ts +35 -0
- package/src/send.reply-fallback.test.ts +417 -0
- package/src/send.test.ts +621 -0
- package/src/send.ts +861 -0
- package/src/sequential-key.test.ts +72 -0
- package/src/sequential-key.ts +25 -0
- package/src/sequential-queue.test.ts +165 -0
- package/src/sequential-queue.ts +86 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +48 -0
- package/src/setup-core.ts +51 -0
- package/src/setup-surface.test.ts +484 -0
- package/src/setup-surface.ts +618 -0
- package/src/streaming-card.test.ts +397 -0
- package/src/streaming-card.ts +571 -0
- package/src/subagent-hooks.test.ts +627 -0
- package/src/subagent-hooks.ts +413 -0
- package/src/targets.ts +97 -0
- package/src/test-support/lifecycle-test-support.ts +454 -0
- package/src/thread-bindings.test.ts +180 -0
- package/src/thread-bindings.ts +331 -0
- package/src/tool-account-routing.test.ts +250 -0
- package/src/tool-account.test.ts +44 -0
- package/src/tool-account.ts +93 -0
- package/src/tool-factory-test-harness.ts +79 -0
- package/src/tool-result.test.ts +32 -0
- package/src/tool-result.ts +16 -0
- package/src/tools-config.test.ts +21 -0
- package/src/tools-config.ts +22 -0
- package/src/types.ts +106 -0
- package/src/typing.test.ts +144 -0
- package/src/typing.ts +214 -0
- package/src/wiki-schema.ts +69 -0
- package/src/wiki.ts +270 -0
- package/subagent-hooks-api.ts +31 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-entry.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/security-contract-api.js +0 -7
- package/session-key-api.js +0 -7
- package/setup-api.js +0 -7
- package/setup-entry.js +0 -7
- package/subagent-hooks-api.js +0 -7
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeOptionalLowercaseString,
|
|
3
|
+
normalizeOptionalString,
|
|
4
|
+
} from "klaw/plugin-sdk/string-coerce-runtime";
|
|
5
|
+
import { buildFeishuConversationId, parseFeishuConversationId } from "./conversation-id.js";
|
|
6
|
+
import { normalizeFeishuTarget } from "./targets.js";
|
|
7
|
+
import { getFeishuThreadBindingManager } from "./thread-bindings.js";
|
|
8
|
+
|
|
9
|
+
function summarizeError(err: unknown): string {
|
|
10
|
+
if (err instanceof Error) {
|
|
11
|
+
return err.message;
|
|
12
|
+
}
|
|
13
|
+
if (typeof err === "string") {
|
|
14
|
+
return err;
|
|
15
|
+
}
|
|
16
|
+
return "error";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function stripProviderPrefix(raw: string): string {
|
|
20
|
+
return raw.replace(/^(feishu|lark):/i, "").trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveFeishuRequesterConversation(params: {
|
|
24
|
+
accountId?: string;
|
|
25
|
+
to?: string;
|
|
26
|
+
threadId?: string | number;
|
|
27
|
+
requesterSessionKey?: string;
|
|
28
|
+
}): {
|
|
29
|
+
accountId: string;
|
|
30
|
+
conversationId: string;
|
|
31
|
+
parentConversationId?: string;
|
|
32
|
+
} | null {
|
|
33
|
+
const manager = getFeishuThreadBindingManager(params.accountId);
|
|
34
|
+
if (!manager) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const rawTo = params.to?.trim();
|
|
38
|
+
const withoutProviderPrefix = rawTo ? stripProviderPrefix(rawTo) : "";
|
|
39
|
+
const normalizedTarget = rawTo ? normalizeFeishuTarget(rawTo) : null;
|
|
40
|
+
const threadId =
|
|
41
|
+
params.threadId != null && params.threadId !== "" ? String(params.threadId).trim() : "";
|
|
42
|
+
const isChatTarget = /^(chat|group|channel):/i.test(withoutProviderPrefix);
|
|
43
|
+
const parsedRequesterTopic =
|
|
44
|
+
normalizedTarget && threadId && isChatTarget
|
|
45
|
+
? parseFeishuConversationId({
|
|
46
|
+
conversationId: buildFeishuConversationId({
|
|
47
|
+
chatId: normalizedTarget,
|
|
48
|
+
scope: "group_topic",
|
|
49
|
+
topicId: threadId,
|
|
50
|
+
}),
|
|
51
|
+
parentConversationId: normalizedTarget,
|
|
52
|
+
})
|
|
53
|
+
: null;
|
|
54
|
+
const requesterSessionKey = params.requesterSessionKey?.trim();
|
|
55
|
+
if (requesterSessionKey) {
|
|
56
|
+
const existingBindings = manager.listBySessionKey(requesterSessionKey);
|
|
57
|
+
if (existingBindings.length === 1) {
|
|
58
|
+
const existing = existingBindings[0];
|
|
59
|
+
return {
|
|
60
|
+
accountId: existing.accountId,
|
|
61
|
+
conversationId: existing.conversationId,
|
|
62
|
+
parentConversationId: existing.parentConversationId,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (existingBindings.length > 1) {
|
|
66
|
+
if (rawTo && normalizedTarget && !threadId && !isChatTarget) {
|
|
67
|
+
const directMatches = existingBindings.filter(
|
|
68
|
+
(entry) =>
|
|
69
|
+
entry.accountId === manager.accountId &&
|
|
70
|
+
entry.conversationId === normalizedTarget &&
|
|
71
|
+
!entry.parentConversationId,
|
|
72
|
+
);
|
|
73
|
+
if (directMatches.length === 1) {
|
|
74
|
+
const existing = directMatches[0];
|
|
75
|
+
return {
|
|
76
|
+
accountId: existing.accountId,
|
|
77
|
+
conversationId: existing.conversationId,
|
|
78
|
+
parentConversationId: existing.parentConversationId,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
if (parsedRequesterTopic) {
|
|
84
|
+
const matchingTopicBindings = existingBindings.filter((entry) => {
|
|
85
|
+
const parsed = parseFeishuConversationId({
|
|
86
|
+
conversationId: entry.conversationId,
|
|
87
|
+
parentConversationId: entry.parentConversationId,
|
|
88
|
+
});
|
|
89
|
+
return (
|
|
90
|
+
parsed?.chatId === parsedRequesterTopic.chatId &&
|
|
91
|
+
parsed?.topicId === parsedRequesterTopic.topicId
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
if (matchingTopicBindings.length === 1) {
|
|
95
|
+
const existing = matchingTopicBindings[0];
|
|
96
|
+
return {
|
|
97
|
+
accountId: existing.accountId,
|
|
98
|
+
conversationId: existing.conversationId,
|
|
99
|
+
parentConversationId: existing.parentConversationId,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const senderScopedTopicBindings = matchingTopicBindings.filter((entry) => {
|
|
103
|
+
const parsed = parseFeishuConversationId({
|
|
104
|
+
conversationId: entry.conversationId,
|
|
105
|
+
parentConversationId: entry.parentConversationId,
|
|
106
|
+
});
|
|
107
|
+
return parsed?.scope === "group_topic_sender";
|
|
108
|
+
});
|
|
109
|
+
if (
|
|
110
|
+
senderScopedTopicBindings.length === 1 &&
|
|
111
|
+
matchingTopicBindings.length === senderScopedTopicBindings.length
|
|
112
|
+
) {
|
|
113
|
+
const existing = senderScopedTopicBindings[0];
|
|
114
|
+
return {
|
|
115
|
+
accountId: existing.accountId,
|
|
116
|
+
conversationId: existing.conversationId,
|
|
117
|
+
parentConversationId: existing.parentConversationId,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!rawTo) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
if (!normalizedTarget) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (threadId) {
|
|
133
|
+
if (!isChatTarget) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
accountId: manager.accountId,
|
|
138
|
+
conversationId: buildFeishuConversationId({
|
|
139
|
+
chatId: normalizedTarget,
|
|
140
|
+
scope: "group_topic",
|
|
141
|
+
topicId: threadId,
|
|
142
|
+
}),
|
|
143
|
+
parentConversationId: normalizedTarget,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (isChatTarget) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
accountId: manager.accountId,
|
|
153
|
+
conversationId: normalizedTarget,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function resolveFeishuDeliveryOrigin(params: {
|
|
158
|
+
conversationId: string;
|
|
159
|
+
parentConversationId?: string;
|
|
160
|
+
accountId: string;
|
|
161
|
+
deliveryTo?: string;
|
|
162
|
+
deliveryThreadId?: string;
|
|
163
|
+
}): {
|
|
164
|
+
channel: "feishu";
|
|
165
|
+
accountId: string;
|
|
166
|
+
to: string;
|
|
167
|
+
threadId?: string;
|
|
168
|
+
} {
|
|
169
|
+
const deliveryTo = params.deliveryTo?.trim();
|
|
170
|
+
const deliveryThreadId = params.deliveryThreadId?.trim();
|
|
171
|
+
if (deliveryTo) {
|
|
172
|
+
return {
|
|
173
|
+
channel: "feishu",
|
|
174
|
+
accountId: params.accountId,
|
|
175
|
+
to: deliveryTo,
|
|
176
|
+
...(deliveryThreadId ? { threadId: deliveryThreadId } : {}),
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
const parsed = parseFeishuConversationId({
|
|
180
|
+
conversationId: params.conversationId,
|
|
181
|
+
parentConversationId: params.parentConversationId,
|
|
182
|
+
});
|
|
183
|
+
if (parsed?.topicId) {
|
|
184
|
+
return {
|
|
185
|
+
channel: "feishu",
|
|
186
|
+
accountId: params.accountId,
|
|
187
|
+
to: `chat:${params.parentConversationId?.trim() || parsed.chatId}`,
|
|
188
|
+
threadId: parsed.topicId,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
channel: "feishu",
|
|
193
|
+
accountId: params.accountId,
|
|
194
|
+
to: `user:${params.conversationId}`,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function resolveMatchingChildBinding(params: {
|
|
199
|
+
accountId?: string;
|
|
200
|
+
childSessionKey: string;
|
|
201
|
+
requesterSessionKey?: string;
|
|
202
|
+
requesterOrigin?: {
|
|
203
|
+
to?: string;
|
|
204
|
+
threadId?: string | number;
|
|
205
|
+
};
|
|
206
|
+
}) {
|
|
207
|
+
const manager = getFeishuThreadBindingManager(params.accountId);
|
|
208
|
+
if (!manager) {
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
const childBindings = manager.listBySessionKey(params.childSessionKey.trim());
|
|
212
|
+
if (childBindings.length === 0) {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const requesterConversation = resolveFeishuRequesterConversation({
|
|
217
|
+
accountId: manager.accountId,
|
|
218
|
+
to: params.requesterOrigin?.to,
|
|
219
|
+
threadId: params.requesterOrigin?.threadId,
|
|
220
|
+
requesterSessionKey: params.requesterSessionKey,
|
|
221
|
+
});
|
|
222
|
+
if (requesterConversation) {
|
|
223
|
+
const matched = childBindings.find(
|
|
224
|
+
(entry) =>
|
|
225
|
+
entry.accountId === requesterConversation.accountId &&
|
|
226
|
+
entry.conversationId === requesterConversation.conversationId &&
|
|
227
|
+
normalizeOptionalString(entry.parentConversationId) ===
|
|
228
|
+
normalizeOptionalString(requesterConversation.parentConversationId),
|
|
229
|
+
);
|
|
230
|
+
if (matched) {
|
|
231
|
+
return matched;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return childBindings.length === 1 ? childBindings[0] : null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
type FeishuSubagentContext = {
|
|
239
|
+
requesterSessionKey?: string;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
type FeishuSubagentSpawningEvent = {
|
|
243
|
+
threadRequested?: boolean;
|
|
244
|
+
requester?: {
|
|
245
|
+
channel?: string;
|
|
246
|
+
accountId?: string;
|
|
247
|
+
to?: string;
|
|
248
|
+
threadId?: string | number;
|
|
249
|
+
};
|
|
250
|
+
childSessionKey: string;
|
|
251
|
+
agentId?: string;
|
|
252
|
+
label?: string;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
type FeishuSubagentDeliveryTargetEvent = {
|
|
256
|
+
expectsCompletionMessage?: boolean;
|
|
257
|
+
requesterOrigin?: {
|
|
258
|
+
channel?: string;
|
|
259
|
+
accountId?: string;
|
|
260
|
+
to?: string;
|
|
261
|
+
threadId?: string | number;
|
|
262
|
+
};
|
|
263
|
+
childSessionKey: string;
|
|
264
|
+
requesterSessionKey?: string;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
type FeishuSubagentEndedEvent = {
|
|
268
|
+
accountId?: string;
|
|
269
|
+
targetSessionKey: string;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
type FeishuSubagentSpawningResult =
|
|
273
|
+
| {
|
|
274
|
+
status: "ok";
|
|
275
|
+
threadBindingReady?: boolean;
|
|
276
|
+
deliveryOrigin?: {
|
|
277
|
+
channel: "feishu";
|
|
278
|
+
accountId?: string;
|
|
279
|
+
to?: string;
|
|
280
|
+
threadId?: string | number;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
| { status: "error"; error: string }
|
|
284
|
+
| undefined;
|
|
285
|
+
|
|
286
|
+
type FeishuSubagentDeliveryTargetResult =
|
|
287
|
+
| {
|
|
288
|
+
origin: {
|
|
289
|
+
channel: "feishu";
|
|
290
|
+
accountId?: string;
|
|
291
|
+
to?: string;
|
|
292
|
+
threadId?: string | number;
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
| undefined;
|
|
296
|
+
|
|
297
|
+
export async function handleFeishuSubagentSpawning(
|
|
298
|
+
event: FeishuSubagentSpawningEvent,
|
|
299
|
+
ctx: FeishuSubagentContext,
|
|
300
|
+
): Promise<FeishuSubagentSpawningResult> {
|
|
301
|
+
if (!event.threadRequested) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
const requesterChannel = normalizeOptionalLowercaseString(event.requester?.channel);
|
|
305
|
+
if (requesterChannel !== "feishu") {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const manager = getFeishuThreadBindingManager(event.requester?.accountId);
|
|
310
|
+
if (!manager) {
|
|
311
|
+
return {
|
|
312
|
+
status: "error" as const,
|
|
313
|
+
error:
|
|
314
|
+
"Feishu current-conversation binding is unavailable because the Feishu account monitor is not active.",
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const conversation = resolveFeishuRequesterConversation({
|
|
319
|
+
accountId: event.requester?.accountId,
|
|
320
|
+
to: event.requester?.to,
|
|
321
|
+
threadId: event.requester?.threadId,
|
|
322
|
+
requesterSessionKey: ctx.requesterSessionKey,
|
|
323
|
+
});
|
|
324
|
+
if (!conversation) {
|
|
325
|
+
return {
|
|
326
|
+
status: "error" as const,
|
|
327
|
+
error:
|
|
328
|
+
"Feishu current-conversation binding is only available in direct messages or topic conversations.",
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
try {
|
|
333
|
+
const binding = manager.bindConversation({
|
|
334
|
+
conversationId: conversation.conversationId,
|
|
335
|
+
parentConversationId: conversation.parentConversationId,
|
|
336
|
+
targetKind: "subagent",
|
|
337
|
+
targetSessionKey: event.childSessionKey,
|
|
338
|
+
metadata: {
|
|
339
|
+
agentId: event.agentId,
|
|
340
|
+
label: event.label,
|
|
341
|
+
boundBy: "system",
|
|
342
|
+
deliveryTo: event.requester?.to,
|
|
343
|
+
deliveryThreadId:
|
|
344
|
+
event.requester?.threadId != null && event.requester.threadId !== ""
|
|
345
|
+
? String(event.requester.threadId)
|
|
346
|
+
: undefined,
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
if (!binding) {
|
|
350
|
+
return {
|
|
351
|
+
status: "error" as const,
|
|
352
|
+
error:
|
|
353
|
+
"Unable to bind this Feishu conversation to the spawned subagent session. Session mode is unavailable for this target.",
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
return {
|
|
357
|
+
status: "ok" as const,
|
|
358
|
+
threadBindingReady: true,
|
|
359
|
+
deliveryOrigin: resolveFeishuDeliveryOrigin({
|
|
360
|
+
conversationId: binding.conversationId,
|
|
361
|
+
parentConversationId: binding.parentConversationId,
|
|
362
|
+
accountId: binding.accountId,
|
|
363
|
+
deliveryTo: binding.deliveryTo,
|
|
364
|
+
deliveryThreadId: binding.deliveryThreadId,
|
|
365
|
+
}),
|
|
366
|
+
};
|
|
367
|
+
} catch (err) {
|
|
368
|
+
return {
|
|
369
|
+
status: "error" as const,
|
|
370
|
+
error: `Feishu conversation bind failed: ${summarizeError(err)}`,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function handleFeishuSubagentDeliveryTarget(
|
|
376
|
+
event: FeishuSubagentDeliveryTargetEvent,
|
|
377
|
+
): FeishuSubagentDeliveryTargetResult {
|
|
378
|
+
if (!event.expectsCompletionMessage) {
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
const requesterChannel = normalizeOptionalLowercaseString(event.requesterOrigin?.channel);
|
|
382
|
+
if (requesterChannel !== "feishu") {
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const binding = resolveMatchingChildBinding({
|
|
387
|
+
accountId: event.requesterOrigin?.accountId,
|
|
388
|
+
childSessionKey: event.childSessionKey,
|
|
389
|
+
requesterSessionKey: event.requesterSessionKey,
|
|
390
|
+
requesterOrigin: {
|
|
391
|
+
to: event.requesterOrigin?.to,
|
|
392
|
+
threadId: event.requesterOrigin?.threadId,
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
if (!binding) {
|
|
396
|
+
return undefined;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
origin: resolveFeishuDeliveryOrigin({
|
|
401
|
+
conversationId: binding.conversationId,
|
|
402
|
+
parentConversationId: binding.parentConversationId,
|
|
403
|
+
accountId: binding.accountId,
|
|
404
|
+
deliveryTo: binding.deliveryTo,
|
|
405
|
+
deliveryThreadId: binding.deliveryThreadId,
|
|
406
|
+
}),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
export function handleFeishuSubagentEnded(event: FeishuSubagentEndedEvent) {
|
|
411
|
+
const manager = getFeishuThreadBindingManager(event.accountId);
|
|
412
|
+
manager?.unbindBySessionKey(event.targetSessionKey);
|
|
413
|
+
}
|
package/src/targets.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import type { FeishuIdType } from "./types.js";
|
|
3
|
+
|
|
4
|
+
const CHAT_ID_PREFIX = "oc_";
|
|
5
|
+
const OPEN_ID_PREFIX = "ou_";
|
|
6
|
+
const USER_ID_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
7
|
+
|
|
8
|
+
function stripProviderPrefix(raw: string): string {
|
|
9
|
+
return raw.replace(/^(feishu|lark):/i, "").trim();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function detectIdType(id: string): FeishuIdType | null {
|
|
13
|
+
const trimmed = id.trim();
|
|
14
|
+
if (trimmed.startsWith(CHAT_ID_PREFIX)) {
|
|
15
|
+
return "chat_id";
|
|
16
|
+
}
|
|
17
|
+
if (trimmed.startsWith(OPEN_ID_PREFIX)) {
|
|
18
|
+
return "open_id";
|
|
19
|
+
}
|
|
20
|
+
if (USER_ID_REGEX.test(trimmed)) {
|
|
21
|
+
return "user_id";
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function normalizeFeishuTarget(raw: string): string | null {
|
|
27
|
+
const trimmed = raw.trim();
|
|
28
|
+
if (!trimmed) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const withoutProvider = stripProviderPrefix(trimmed);
|
|
33
|
+
const lowered = normalizeLowercaseStringOrEmpty(withoutProvider);
|
|
34
|
+
if (lowered.startsWith("chat:")) {
|
|
35
|
+
return withoutProvider.slice("chat:".length).trim() || null;
|
|
36
|
+
}
|
|
37
|
+
if (lowered.startsWith("group:")) {
|
|
38
|
+
return withoutProvider.slice("group:".length).trim() || null;
|
|
39
|
+
}
|
|
40
|
+
if (lowered.startsWith("channel:")) {
|
|
41
|
+
return withoutProvider.slice("channel:".length).trim() || null;
|
|
42
|
+
}
|
|
43
|
+
if (lowered.startsWith("user:")) {
|
|
44
|
+
return withoutProvider.slice("user:".length).trim() || null;
|
|
45
|
+
}
|
|
46
|
+
if (lowered.startsWith("dm:")) {
|
|
47
|
+
return withoutProvider.slice("dm:".length).trim() || null;
|
|
48
|
+
}
|
|
49
|
+
if (lowered.startsWith("open_id:")) {
|
|
50
|
+
return withoutProvider.slice("open_id:".length).trim() || null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return withoutProvider;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function resolveReceiveIdType(id: string): "chat_id" | "open_id" | "user_id" {
|
|
57
|
+
const trimmed = id.trim();
|
|
58
|
+
const lowered = normalizeLowercaseStringOrEmpty(trimmed);
|
|
59
|
+
if (
|
|
60
|
+
lowered.startsWith("chat:") ||
|
|
61
|
+
lowered.startsWith("group:") ||
|
|
62
|
+
lowered.startsWith("channel:")
|
|
63
|
+
) {
|
|
64
|
+
return "chat_id";
|
|
65
|
+
}
|
|
66
|
+
if (lowered.startsWith("open_id:")) {
|
|
67
|
+
return "open_id";
|
|
68
|
+
}
|
|
69
|
+
if (lowered.startsWith("user:") || lowered.startsWith("dm:")) {
|
|
70
|
+
const normalized = trimmed.replace(/^(user|dm):/i, "").trim();
|
|
71
|
+
return normalized.startsWith(OPEN_ID_PREFIX) ? "open_id" : "user_id";
|
|
72
|
+
}
|
|
73
|
+
if (trimmed.startsWith(CHAT_ID_PREFIX)) {
|
|
74
|
+
return "chat_id";
|
|
75
|
+
}
|
|
76
|
+
if (trimmed.startsWith(OPEN_ID_PREFIX)) {
|
|
77
|
+
return "open_id";
|
|
78
|
+
}
|
|
79
|
+
return "user_id";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function looksLikeFeishuId(raw: string): boolean {
|
|
83
|
+
const trimmed = stripProviderPrefix(raw.trim());
|
|
84
|
+
if (!trimmed) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
if (/^(chat|group|channel|user|dm|open_id):/i.test(trimmed)) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
if (trimmed.startsWith(CHAT_ID_PREFIX)) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
if (trimmed.startsWith(OPEN_ID_PREFIX)) {
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|