@gakr-gakr/feishu 0.1.0
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/autobot.plugin.json +180 -0
- package/channel-entry.ts +20 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +16 -0
- package/index.ts +82 -0
- package/package.json +62 -0
- package/runtime-api.ts +55 -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.ts +13 -0
- package/skills/feishu-doc/SKILL.md +211 -0
- package/skills/feishu-doc/references/block-types.md +103 -0
- package/skills/feishu-drive/SKILL.md +97 -0
- package/skills/feishu-perm/SKILL.md +119 -0
- package/skills/feishu-wiki/SKILL.md +113 -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.ts +25 -0
- package/src/async.ts +104 -0
- package/src/audio-preflight.runtime.ts +9 -0
- package/src/bitable.ts +762 -0
- package/src/bot-content.ts +485 -0
- package/src/bot-runtime-api.ts +12 -0
- package/src/bot-sender-name.ts +125 -0
- package/src/bot.ts +1703 -0
- package/src/card-action.ts +447 -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.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.ts +1423 -0
- package/src/chat-schema.ts +25 -0
- package/src/chat.ts +188 -0
- package/src/client-timeout.ts +42 -0
- package/src/client.ts +262 -0
- package/src/comment-dispatcher-runtime-api.ts +6 -0
- package/src/comment-dispatcher.ts +107 -0
- package/src/comment-handler-runtime-api.ts +3 -0
- package/src/comment-handler.ts +303 -0
- package/src/comment-reaction.ts +259 -0
- package/src/comment-shared.ts +406 -0
- package/src/comment-target.ts +44 -0
- package/src/config-schema.ts +335 -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.ts +124 -0
- package/src/doc-schema.ts +182 -0
- package/src/docx-batch-insert.ts +223 -0
- package/src/docx-color-text.ts +154 -0
- package/src/docx-table-ops.ts +316 -0
- package/src/docx-types.ts +38 -0
- package/src/docx.ts +1596 -0
- package/src/drive-schema.ts +92 -0
- package/src/drive.ts +829 -0
- package/src/dynamic-agent.ts +143 -0
- package/src/event-types.ts +45 -0
- package/src/external-keys.ts +19 -0
- package/src/lifecycle.test-support.ts +220 -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.broadcast.reply-once.lifecycle.test-support.ts +264 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +421 -0
- package/src/monitor.comment-notice-handler.ts +105 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.message-handler.ts +350 -0
- package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
- package/src/monitor.startup.ts +74 -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/outbound-runtime-api.ts +1 -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.ts +321 -0
- package/src/post.ts +275 -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.ts +28 -0
- package/src/reply-dispatcher-runtime-api.ts +7 -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.ts +1 -0
- package/src/send-result.ts +80 -0
- package/src/send-target.ts +35 -0
- package/src/send.ts +861 -0
- package/src/sequential-key.ts +28 -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.ts +618 -0
- package/src/streaming-card.ts +571 -0
- package/src/subagent-hooks.ts +413 -0
- package/src/targets.ts +97 -0
- package/src/thread-bindings.ts +331 -0
- package/src/tool-account.ts +93 -0
- package/src/tool-factory-test-harness.ts +79 -0
- package/src/tool-result.ts +16 -0
- package/src/tools-config.ts +22 -0
- package/src/types.ts +106 -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
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
2
|
+
import {
|
|
3
|
+
resolveThreadBindingIdleTimeoutMsForChannel,
|
|
4
|
+
resolveThreadBindingMaxAgeMsForChannel,
|
|
5
|
+
registerSessionBindingAdapter,
|
|
6
|
+
resolveThreadBindingConversationIdFromBindingId,
|
|
7
|
+
unregisterSessionBindingAdapter,
|
|
8
|
+
type BindingTargetKind,
|
|
9
|
+
type SessionBindingAdapter,
|
|
10
|
+
type SessionBindingRecord,
|
|
11
|
+
} from "autobot/plugin-sdk/conversation-runtime";
|
|
12
|
+
import { normalizeAccountId, resolveAgentIdFromSessionKey } from "autobot/plugin-sdk/routing";
|
|
13
|
+
import { normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
14
|
+
|
|
15
|
+
type FeishuBindingTargetKind = "subagent" | "acp";
|
|
16
|
+
|
|
17
|
+
type FeishuThreadBindingRecord = {
|
|
18
|
+
accountId: string;
|
|
19
|
+
conversationId: string;
|
|
20
|
+
parentConversationId?: string;
|
|
21
|
+
deliveryTo?: string;
|
|
22
|
+
deliveryThreadId?: string;
|
|
23
|
+
targetKind: FeishuBindingTargetKind;
|
|
24
|
+
targetSessionKey: string;
|
|
25
|
+
agentId?: string;
|
|
26
|
+
label?: string;
|
|
27
|
+
boundBy?: string;
|
|
28
|
+
boundAt: number;
|
|
29
|
+
lastActivityAt: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type FeishuThreadBindingManager = {
|
|
33
|
+
accountId: string;
|
|
34
|
+
getByConversationId: (conversationId: string) => FeishuThreadBindingRecord | undefined;
|
|
35
|
+
listBySessionKey: (targetSessionKey: string) => FeishuThreadBindingRecord[];
|
|
36
|
+
bindConversation: (params: {
|
|
37
|
+
conversationId: string;
|
|
38
|
+
parentConversationId?: string;
|
|
39
|
+
targetKind: BindingTargetKind;
|
|
40
|
+
targetSessionKey: string;
|
|
41
|
+
metadata?: Record<string, unknown>;
|
|
42
|
+
}) => FeishuThreadBindingRecord | null;
|
|
43
|
+
touchConversation: (conversationId: string, at?: number) => FeishuThreadBindingRecord | null;
|
|
44
|
+
unbindConversation: (conversationId: string) => FeishuThreadBindingRecord | null;
|
|
45
|
+
unbindBySessionKey: (targetSessionKey: string) => FeishuThreadBindingRecord[];
|
|
46
|
+
stop: () => void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type FeishuThreadBindingsState = {
|
|
50
|
+
managersByAccountId: Map<string, FeishuThreadBindingManager>;
|
|
51
|
+
bindingsByAccountConversation: Map<string, FeishuThreadBindingRecord>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const FEISHU_THREAD_BINDINGS_STATE_KEY = Symbol.for("autobot.feishuThreadBindingsState");
|
|
55
|
+
let state: FeishuThreadBindingsState | undefined;
|
|
56
|
+
|
|
57
|
+
function getState(): FeishuThreadBindingsState {
|
|
58
|
+
if (!state) {
|
|
59
|
+
const globalStore = globalThis as Record<PropertyKey, unknown>;
|
|
60
|
+
state = (globalStore[FEISHU_THREAD_BINDINGS_STATE_KEY] as
|
|
61
|
+
| FeishuThreadBindingsState
|
|
62
|
+
| undefined) ?? {
|
|
63
|
+
managersByAccountId: new Map(),
|
|
64
|
+
bindingsByAccountConversation: new Map(),
|
|
65
|
+
};
|
|
66
|
+
globalStore[FEISHU_THREAD_BINDINGS_STATE_KEY] = state;
|
|
67
|
+
}
|
|
68
|
+
return state;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function resolveBindingKey(params: { accountId: string; conversationId: string }): string {
|
|
72
|
+
return `${params.accountId}:${params.conversationId}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toSessionBindingTargetKind(raw: FeishuBindingTargetKind): BindingTargetKind {
|
|
76
|
+
return raw === "subagent" ? "subagent" : "session";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function toFeishuTargetKind(raw: BindingTargetKind): FeishuBindingTargetKind {
|
|
80
|
+
return raw === "subagent" ? "subagent" : "acp";
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function toSessionBindingRecord(
|
|
84
|
+
record: FeishuThreadBindingRecord,
|
|
85
|
+
defaults: { idleTimeoutMs: number; maxAgeMs: number },
|
|
86
|
+
): SessionBindingRecord {
|
|
87
|
+
const idleExpiresAt =
|
|
88
|
+
defaults.idleTimeoutMs > 0 ? record.lastActivityAt + defaults.idleTimeoutMs : undefined;
|
|
89
|
+
const maxAgeExpiresAt = defaults.maxAgeMs > 0 ? record.boundAt + defaults.maxAgeMs : undefined;
|
|
90
|
+
const expiresAt =
|
|
91
|
+
idleExpiresAt != null && maxAgeExpiresAt != null
|
|
92
|
+
? Math.min(idleExpiresAt, maxAgeExpiresAt)
|
|
93
|
+
: (idleExpiresAt ?? maxAgeExpiresAt);
|
|
94
|
+
return {
|
|
95
|
+
bindingId: resolveBindingKey({
|
|
96
|
+
accountId: record.accountId,
|
|
97
|
+
conversationId: record.conversationId,
|
|
98
|
+
}),
|
|
99
|
+
targetSessionKey: record.targetSessionKey,
|
|
100
|
+
targetKind: toSessionBindingTargetKind(record.targetKind),
|
|
101
|
+
conversation: {
|
|
102
|
+
channel: "feishu",
|
|
103
|
+
accountId: record.accountId,
|
|
104
|
+
conversationId: record.conversationId,
|
|
105
|
+
parentConversationId: record.parentConversationId,
|
|
106
|
+
},
|
|
107
|
+
status: "active",
|
|
108
|
+
boundAt: record.boundAt,
|
|
109
|
+
expiresAt,
|
|
110
|
+
metadata: {
|
|
111
|
+
agentId: record.agentId,
|
|
112
|
+
label: record.label,
|
|
113
|
+
boundBy: record.boundBy,
|
|
114
|
+
deliveryTo: record.deliveryTo,
|
|
115
|
+
deliveryThreadId: record.deliveryThreadId,
|
|
116
|
+
lastActivityAt: record.lastActivityAt,
|
|
117
|
+
idleTimeoutMs: defaults.idleTimeoutMs,
|
|
118
|
+
maxAgeMs: defaults.maxAgeMs,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function createFeishuThreadBindingManager(params: {
|
|
124
|
+
accountId?: string;
|
|
125
|
+
cfg: AutoBotConfig;
|
|
126
|
+
}): FeishuThreadBindingManager {
|
|
127
|
+
const accountId = normalizeAccountId(params.accountId);
|
|
128
|
+
const existing = getState().managersByAccountId.get(accountId);
|
|
129
|
+
if (existing) {
|
|
130
|
+
return existing;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const idleTimeoutMs = resolveThreadBindingIdleTimeoutMsForChannel({
|
|
134
|
+
cfg: params.cfg,
|
|
135
|
+
channel: "feishu",
|
|
136
|
+
accountId,
|
|
137
|
+
});
|
|
138
|
+
const maxAgeMs = resolveThreadBindingMaxAgeMsForChannel({
|
|
139
|
+
cfg: params.cfg,
|
|
140
|
+
channel: "feishu",
|
|
141
|
+
accountId,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const manager: FeishuThreadBindingManager = {
|
|
145
|
+
accountId,
|
|
146
|
+
getByConversationId: (conversationId) =>
|
|
147
|
+
getState().bindingsByAccountConversation.get(
|
|
148
|
+
resolveBindingKey({ accountId, conversationId }),
|
|
149
|
+
),
|
|
150
|
+
listBySessionKey: (targetSessionKey) =>
|
|
151
|
+
[...getState().bindingsByAccountConversation.values()].filter(
|
|
152
|
+
(record) => record.accountId === accountId && record.targetSessionKey === targetSessionKey,
|
|
153
|
+
),
|
|
154
|
+
bindConversation: ({
|
|
155
|
+
conversationId,
|
|
156
|
+
parentConversationId,
|
|
157
|
+
targetKind,
|
|
158
|
+
targetSessionKey,
|
|
159
|
+
metadata,
|
|
160
|
+
}) => {
|
|
161
|
+
const normalizedConversationId = conversationId.trim();
|
|
162
|
+
const normalizedTargetSessionKey = targetSessionKey.trim();
|
|
163
|
+
if (!normalizedConversationId || !normalizedTargetSessionKey) {
|
|
164
|
+
return null;
|
|
165
|
+
}
|
|
166
|
+
const existing = getState().bindingsByAccountConversation.get(
|
|
167
|
+
resolveBindingKey({ accountId, conversationId: normalizedConversationId }),
|
|
168
|
+
);
|
|
169
|
+
const now = Date.now();
|
|
170
|
+
const record: FeishuThreadBindingRecord = {
|
|
171
|
+
accountId,
|
|
172
|
+
conversationId: normalizedConversationId,
|
|
173
|
+
parentConversationId:
|
|
174
|
+
normalizeOptionalString(parentConversationId) ?? existing?.parentConversationId,
|
|
175
|
+
deliveryTo:
|
|
176
|
+
typeof metadata?.deliveryTo === "string" && metadata.deliveryTo.trim()
|
|
177
|
+
? metadata.deliveryTo.trim()
|
|
178
|
+
: existing?.deliveryTo,
|
|
179
|
+
deliveryThreadId:
|
|
180
|
+
typeof metadata?.deliveryThreadId === "string" && metadata.deliveryThreadId.trim()
|
|
181
|
+
? metadata.deliveryThreadId.trim()
|
|
182
|
+
: existing?.deliveryThreadId,
|
|
183
|
+
targetKind: toFeishuTargetKind(targetKind),
|
|
184
|
+
targetSessionKey: normalizedTargetSessionKey,
|
|
185
|
+
agentId:
|
|
186
|
+
typeof metadata?.agentId === "string" && metadata.agentId.trim()
|
|
187
|
+
? metadata.agentId.trim()
|
|
188
|
+
: (existing?.agentId ?? resolveAgentIdFromSessionKey(normalizedTargetSessionKey)),
|
|
189
|
+
label:
|
|
190
|
+
typeof metadata?.label === "string" && metadata.label.trim()
|
|
191
|
+
? metadata.label.trim()
|
|
192
|
+
: existing?.label,
|
|
193
|
+
boundBy:
|
|
194
|
+
typeof metadata?.boundBy === "string" && metadata.boundBy.trim()
|
|
195
|
+
? metadata.boundBy.trim()
|
|
196
|
+
: existing?.boundBy,
|
|
197
|
+
boundAt: now,
|
|
198
|
+
lastActivityAt: now,
|
|
199
|
+
};
|
|
200
|
+
getState().bindingsByAccountConversation.set(
|
|
201
|
+
resolveBindingKey({ accountId, conversationId: normalizedConversationId }),
|
|
202
|
+
record,
|
|
203
|
+
);
|
|
204
|
+
return record;
|
|
205
|
+
},
|
|
206
|
+
touchConversation: (conversationId, at = Date.now()) => {
|
|
207
|
+
const key = resolveBindingKey({ accountId, conversationId });
|
|
208
|
+
const existingRecord = getState().bindingsByAccountConversation.get(key);
|
|
209
|
+
if (!existingRecord) {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
const updated = { ...existingRecord, lastActivityAt: at };
|
|
213
|
+
getState().bindingsByAccountConversation.set(key, updated);
|
|
214
|
+
return updated;
|
|
215
|
+
},
|
|
216
|
+
unbindConversation: (conversationId) => {
|
|
217
|
+
const key = resolveBindingKey({ accountId, conversationId });
|
|
218
|
+
const existingRecord = getState().bindingsByAccountConversation.get(key);
|
|
219
|
+
if (!existingRecord) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
getState().bindingsByAccountConversation.delete(key);
|
|
223
|
+
return existingRecord;
|
|
224
|
+
},
|
|
225
|
+
unbindBySessionKey: (targetSessionKey) => {
|
|
226
|
+
const removed: FeishuThreadBindingRecord[] = [];
|
|
227
|
+
for (const record of getState().bindingsByAccountConversation.values()) {
|
|
228
|
+
if (record.accountId !== accountId || record.targetSessionKey !== targetSessionKey) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
getState().bindingsByAccountConversation.delete(
|
|
232
|
+
resolveBindingKey({ accountId, conversationId: record.conversationId }),
|
|
233
|
+
);
|
|
234
|
+
removed.push(record);
|
|
235
|
+
}
|
|
236
|
+
return removed;
|
|
237
|
+
},
|
|
238
|
+
stop: () => {
|
|
239
|
+
for (const key of getState().bindingsByAccountConversation.keys()) {
|
|
240
|
+
if (key.startsWith(`${accountId}:`)) {
|
|
241
|
+
getState().bindingsByAccountConversation.delete(key);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
getState().managersByAccountId.delete(accountId);
|
|
245
|
+
unregisterSessionBindingAdapter({
|
|
246
|
+
channel: "feishu",
|
|
247
|
+
accountId,
|
|
248
|
+
adapter: sessionBindingAdapter,
|
|
249
|
+
});
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const sessionBindingAdapter: SessionBindingAdapter = {
|
|
254
|
+
channel: "feishu",
|
|
255
|
+
accountId,
|
|
256
|
+
capabilities: {
|
|
257
|
+
placements: ["current"],
|
|
258
|
+
},
|
|
259
|
+
bind: async (input) => {
|
|
260
|
+
if (input.conversation.channel !== "feishu" || input.placement === "child") {
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
const bound = manager.bindConversation({
|
|
264
|
+
conversationId: input.conversation.conversationId,
|
|
265
|
+
parentConversationId: input.conversation.parentConversationId,
|
|
266
|
+
targetKind: input.targetKind,
|
|
267
|
+
targetSessionKey: input.targetSessionKey,
|
|
268
|
+
metadata: input.metadata,
|
|
269
|
+
});
|
|
270
|
+
return bound ? toSessionBindingRecord(bound, { idleTimeoutMs, maxAgeMs }) : null;
|
|
271
|
+
},
|
|
272
|
+
listBySession: (targetSessionKey) =>
|
|
273
|
+
manager
|
|
274
|
+
.listBySessionKey(targetSessionKey)
|
|
275
|
+
.map((entry) => toSessionBindingRecord(entry, { idleTimeoutMs, maxAgeMs })),
|
|
276
|
+
resolveByConversation: (ref) => {
|
|
277
|
+
if (ref.channel !== "feishu") {
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
const found = manager.getByConversationId(ref.conversationId);
|
|
281
|
+
return found ? toSessionBindingRecord(found, { idleTimeoutMs, maxAgeMs }) : null;
|
|
282
|
+
},
|
|
283
|
+
touch: (bindingId, at) => {
|
|
284
|
+
const conversationId = resolveThreadBindingConversationIdFromBindingId({
|
|
285
|
+
accountId,
|
|
286
|
+
bindingId,
|
|
287
|
+
});
|
|
288
|
+
if (conversationId) {
|
|
289
|
+
manager.touchConversation(conversationId, at);
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
unbind: async (input) => {
|
|
293
|
+
if (input.targetSessionKey?.trim()) {
|
|
294
|
+
return manager
|
|
295
|
+
.unbindBySessionKey(input.targetSessionKey.trim())
|
|
296
|
+
.map((entry) => toSessionBindingRecord(entry, { idleTimeoutMs, maxAgeMs }));
|
|
297
|
+
}
|
|
298
|
+
const conversationId = resolveThreadBindingConversationIdFromBindingId({
|
|
299
|
+
accountId,
|
|
300
|
+
bindingId: input.bindingId,
|
|
301
|
+
});
|
|
302
|
+
if (!conversationId) {
|
|
303
|
+
return [];
|
|
304
|
+
}
|
|
305
|
+
const removed = manager.unbindConversation(conversationId);
|
|
306
|
+
return removed ? [toSessionBindingRecord(removed, { idleTimeoutMs, maxAgeMs })] : [];
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
registerSessionBindingAdapter(sessionBindingAdapter);
|
|
311
|
+
|
|
312
|
+
getState().managersByAccountId.set(accountId, manager);
|
|
313
|
+
return manager;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function getFeishuThreadBindingManager(
|
|
317
|
+
accountId?: string,
|
|
318
|
+
): FeishuThreadBindingManager | null {
|
|
319
|
+
return getState().managersByAccountId.get(normalizeAccountId(accountId)) ?? null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export const testing = {
|
|
323
|
+
resetFeishuThreadBindingsForTests() {
|
|
324
|
+
for (const manager of getState().managersByAccountId.values()) {
|
|
325
|
+
manager.stop();
|
|
326
|
+
}
|
|
327
|
+
getState().managersByAccountId.clear();
|
|
328
|
+
getState().bindingsByAccountConversation.clear();
|
|
329
|
+
},
|
|
330
|
+
};
|
|
331
|
+
export { testing as __testing };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
|
+
import { normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
3
|
+
import type { AutoBotPluginApi } from "../runtime-api.js";
|
|
4
|
+
import {
|
|
5
|
+
listFeishuAccountIds,
|
|
6
|
+
resolveFeishuAccount,
|
|
7
|
+
resolveFeishuRuntimeAccount,
|
|
8
|
+
} from "./accounts.js";
|
|
9
|
+
import { createFeishuClient } from "./client.js";
|
|
10
|
+
import { resolveToolsConfig } from "./tools-config.js";
|
|
11
|
+
import type { FeishuToolsConfig, ResolvedFeishuAccount } from "./types.js";
|
|
12
|
+
|
|
13
|
+
type AccountAwareParams = { accountId?: string };
|
|
14
|
+
|
|
15
|
+
function resolveImplicitToolAccountId(params: {
|
|
16
|
+
api: Pick<AutoBotPluginApi, "config">;
|
|
17
|
+
executeParams?: AccountAwareParams;
|
|
18
|
+
defaultAccountId?: string;
|
|
19
|
+
}): string | undefined {
|
|
20
|
+
const explicitAccountId = normalizeOptionalString(params.executeParams?.accountId);
|
|
21
|
+
if (explicitAccountId) {
|
|
22
|
+
return explicitAccountId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const contextualAccountId = normalizeOptionalString(params.defaultAccountId);
|
|
26
|
+
if (
|
|
27
|
+
contextualAccountId &&
|
|
28
|
+
listFeishuAccountIds(params.api.config).includes(contextualAccountId)
|
|
29
|
+
) {
|
|
30
|
+
const contextualAccount = resolveFeishuAccount({
|
|
31
|
+
cfg: params.api.config,
|
|
32
|
+
accountId: contextualAccountId,
|
|
33
|
+
});
|
|
34
|
+
if (contextualAccount.enabled) {
|
|
35
|
+
return contextualAccountId;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const configuredDefaultAccountId = normalizeOptionalString(
|
|
40
|
+
(params.api.config?.channels?.feishu as { defaultAccount?: unknown } | undefined)
|
|
41
|
+
?.defaultAccount,
|
|
42
|
+
);
|
|
43
|
+
if (configuredDefaultAccountId) {
|
|
44
|
+
return configuredDefaultAccountId;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function resolveFeishuToolAccount(params: {
|
|
51
|
+
api: Pick<AutoBotPluginApi, "config">;
|
|
52
|
+
executeParams?: AccountAwareParams;
|
|
53
|
+
defaultAccountId?: string;
|
|
54
|
+
}): ResolvedFeishuAccount {
|
|
55
|
+
if (!params.api.config) {
|
|
56
|
+
throw new Error("Feishu config unavailable");
|
|
57
|
+
}
|
|
58
|
+
return resolveFeishuRuntimeAccount({
|
|
59
|
+
cfg: params.api.config,
|
|
60
|
+
accountId: resolveImplicitToolAccountId(params),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function createFeishuToolClient(params: {
|
|
65
|
+
api: Pick<AutoBotPluginApi, "config">;
|
|
66
|
+
executeParams?: AccountAwareParams;
|
|
67
|
+
defaultAccountId?: string;
|
|
68
|
+
}): Lark.Client {
|
|
69
|
+
return createFeishuClient(resolveFeishuToolAccount(params));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function resolveAnyEnabledFeishuToolsConfig(
|
|
73
|
+
accounts: ResolvedFeishuAccount[],
|
|
74
|
+
): Required<FeishuToolsConfig> {
|
|
75
|
+
const merged: Required<FeishuToolsConfig> = {
|
|
76
|
+
doc: false,
|
|
77
|
+
chat: false,
|
|
78
|
+
wiki: false,
|
|
79
|
+
drive: false,
|
|
80
|
+
perm: false,
|
|
81
|
+
scopes: false,
|
|
82
|
+
};
|
|
83
|
+
for (const account of accounts) {
|
|
84
|
+
const cfg = resolveToolsConfig(account.config.tools);
|
|
85
|
+
merged.doc = merged.doc || cfg.doc;
|
|
86
|
+
merged.chat = merged.chat || cfg.chat;
|
|
87
|
+
merged.wiki = merged.wiki || cfg.wiki;
|
|
88
|
+
merged.drive = merged.drive || cfg.drive;
|
|
89
|
+
merged.perm = merged.perm || cfg.perm;
|
|
90
|
+
merged.scopes = merged.scopes || cfg.scopes;
|
|
91
|
+
}
|
|
92
|
+
return merged;
|
|
93
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { AutoBotPluginApi } from "../runtime-api.js";
|
|
2
|
+
|
|
3
|
+
type ToolContextLike = {
|
|
4
|
+
agentAccountId?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type ToolLike = {
|
|
8
|
+
name: string;
|
|
9
|
+
execute: (
|
|
10
|
+
toolCallId: string,
|
|
11
|
+
params: unknown,
|
|
12
|
+
) => Promise<{ details: Record<string, unknown> }> | { details: Record<string, unknown> };
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type RegisteredTool = {
|
|
16
|
+
tool: unknown;
|
|
17
|
+
opts?: { name?: string };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function toToolList(value: unknown): unknown[] {
|
|
21
|
+
if (!value) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
return Array.isArray(value) ? value : [value];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function asToolLike(tool: unknown, fallbackName?: string): ToolLike {
|
|
28
|
+
const candidate = tool as Partial<ToolLike>;
|
|
29
|
+
const name = candidate.name ?? fallbackName;
|
|
30
|
+
const execute = candidate.execute;
|
|
31
|
+
if (!name || typeof execute !== "function") {
|
|
32
|
+
throw new Error(`Resolved tool is missing required fields (name=${String(name)})`);
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
name,
|
|
36
|
+
execute: (toolCallId, params) => execute(toolCallId, params),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createToolFactoryHarness(cfg: AutoBotPluginApi["config"]) {
|
|
41
|
+
const registered: RegisteredTool[] = [];
|
|
42
|
+
|
|
43
|
+
const api: Pick<AutoBotPluginApi, "config" | "logger" | "registerTool"> = {
|
|
44
|
+
config: cfg,
|
|
45
|
+
logger: {
|
|
46
|
+
info: () => {},
|
|
47
|
+
warn: () => {},
|
|
48
|
+
error: () => {},
|
|
49
|
+
debug: () => {},
|
|
50
|
+
},
|
|
51
|
+
registerTool: (tool, opts) => {
|
|
52
|
+
registered.push({ tool, opts });
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const resolveTool = (name: string, ctx: ToolContextLike = {}): ToolLike => {
|
|
57
|
+
for (const entry of registered) {
|
|
58
|
+
if (entry.opts?.name === name && typeof entry.tool !== "function") {
|
|
59
|
+
return asToolLike(entry.tool, name);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (typeof entry.tool === "function") {
|
|
63
|
+
const builtTools = toToolList(entry.tool(ctx));
|
|
64
|
+
const hit = builtTools.find((tool) => (tool as { name?: string }).name === name);
|
|
65
|
+
if (hit) {
|
|
66
|
+
return asToolLike(hit, name);
|
|
67
|
+
}
|
|
68
|
+
} else if ((entry.tool as { name?: string }).name === name) {
|
|
69
|
+
return asToolLike(entry.tool, name);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw new Error(`Tool not registered: ${name}`);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
api: api as AutoBotPluginApi,
|
|
77
|
+
resolveTool,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { formatErrorMessage } from "autobot/plugin-sdk/error-runtime";
|
|
2
|
+
|
|
3
|
+
export function jsonToolResult(data: unknown) {
|
|
4
|
+
return {
|
|
5
|
+
content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
|
|
6
|
+
details: data,
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function unknownToolActionResult(action: unknown) {
|
|
11
|
+
return jsonToolResult({ error: `Unknown action: ${String(action)}` });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function toolExecutionErrorResult(error: unknown) {
|
|
15
|
+
return jsonToolResult({ error: formatErrorMessage(error) });
|
|
16
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { FeishuToolsConfig } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default tool configuration.
|
|
5
|
+
* - doc, chat, wiki, drive, scopes: enabled by default
|
|
6
|
+
* - perm: disabled by default (sensitive operation)
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_TOOLS_CONFIG: Required<FeishuToolsConfig> = {
|
|
9
|
+
doc: true,
|
|
10
|
+
chat: true,
|
|
11
|
+
wiki: true,
|
|
12
|
+
drive: true,
|
|
13
|
+
perm: false,
|
|
14
|
+
scopes: true,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Resolve tools config with defaults.
|
|
19
|
+
*/
|
|
20
|
+
export function resolveToolsConfig(cfg?: FeishuToolsConfig): Required<FeishuToolsConfig> {
|
|
21
|
+
return { ...DEFAULT_TOOLS_CONFIG, ...cfg };
|
|
22
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { MessageReceipt } from "autobot/plugin-sdk/channel-message";
|
|
2
|
+
import type { BaseProbeResult } from "autobot/plugin-sdk/core";
|
|
3
|
+
import type { FeishuConfigSchema, FeishuAccountConfigSchema, z } from "./config-schema.js";
|
|
4
|
+
import type { MentionTarget } from "./mention-target.types.js";
|
|
5
|
+
|
|
6
|
+
export type FeishuConfig = z.infer<typeof FeishuConfigSchema>;
|
|
7
|
+
export type FeishuAccountConfig = z.infer<typeof FeishuAccountConfigSchema>;
|
|
8
|
+
|
|
9
|
+
export type FeishuDomain = "feishu" | "lark" | (string & {});
|
|
10
|
+
|
|
11
|
+
export type FeishuDefaultAccountSelectionSource =
|
|
12
|
+
| "explicit-default"
|
|
13
|
+
| "mapped-default"
|
|
14
|
+
| "fallback";
|
|
15
|
+
type FeishuAccountSelectionSource = "explicit" | FeishuDefaultAccountSelectionSource;
|
|
16
|
+
|
|
17
|
+
export type ResolvedFeishuAccount = {
|
|
18
|
+
accountId: string;
|
|
19
|
+
selectionSource: FeishuAccountSelectionSource;
|
|
20
|
+
enabled: boolean;
|
|
21
|
+
configured: boolean;
|
|
22
|
+
name?: string;
|
|
23
|
+
appId?: string;
|
|
24
|
+
appSecret?: string;
|
|
25
|
+
encryptKey?: string;
|
|
26
|
+
verificationToken?: string;
|
|
27
|
+
domain: FeishuDomain;
|
|
28
|
+
/** Merged config (top-level defaults + account-specific overrides) */
|
|
29
|
+
config: FeishuConfig;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type FeishuIdType = "open_id" | "user_id" | "union_id" | "chat_id";
|
|
33
|
+
|
|
34
|
+
export type FeishuMessageContext = {
|
|
35
|
+
chatId: string;
|
|
36
|
+
messageId: string;
|
|
37
|
+
replyTargetMessageId?: string;
|
|
38
|
+
suppressReplyTarget?: boolean;
|
|
39
|
+
senderId: string;
|
|
40
|
+
senderOpenId: string;
|
|
41
|
+
senderName?: string;
|
|
42
|
+
chatType: FeishuChatType;
|
|
43
|
+
mentionedBot: boolean;
|
|
44
|
+
hasAnyMention?: boolean;
|
|
45
|
+
rootId?: string;
|
|
46
|
+
parentId?: string;
|
|
47
|
+
threadId?: string;
|
|
48
|
+
content: string;
|
|
49
|
+
contentType: string;
|
|
50
|
+
/** Mention forward targets (excluding the bot itself) */
|
|
51
|
+
mentionTargets?: MentionTarget[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type FeishuSendResult = {
|
|
55
|
+
messageId: string;
|
|
56
|
+
chatId: string;
|
|
57
|
+
receipt: MessageReceipt;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type FeishuChatType = "p2p" | "group" | "topic_group" | "private";
|
|
61
|
+
|
|
62
|
+
export function isFeishuGroupChatType(chatType: FeishuChatType | undefined): boolean {
|
|
63
|
+
return chatType === "group" || chatType === "topic_group";
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type FeishuMessageInfo = {
|
|
67
|
+
messageId: string;
|
|
68
|
+
chatId: string;
|
|
69
|
+
chatType?: FeishuChatType;
|
|
70
|
+
senderId?: string;
|
|
71
|
+
senderOpenId?: string;
|
|
72
|
+
senderType?: string;
|
|
73
|
+
content: string;
|
|
74
|
+
contentType: string;
|
|
75
|
+
createTime?: number;
|
|
76
|
+
/** Feishu thread ID (omt_xxx) — present when the message belongs to a topic thread. */
|
|
77
|
+
threadId?: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export interface FeishuProbeResult extends BaseProbeResult {
|
|
81
|
+
appId?: string;
|
|
82
|
+
botName?: string;
|
|
83
|
+
botOpenId?: string;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type FeishuMediaInfo = {
|
|
87
|
+
path: string;
|
|
88
|
+
contentType?: string;
|
|
89
|
+
placeholder: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type FeishuToolsConfig = {
|
|
93
|
+
doc?: boolean;
|
|
94
|
+
chat?: boolean;
|
|
95
|
+
wiki?: boolean;
|
|
96
|
+
drive?: boolean;
|
|
97
|
+
perm?: boolean;
|
|
98
|
+
scopes?: boolean;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export type DynamicAgentCreationConfig = {
|
|
102
|
+
enabled?: boolean;
|
|
103
|
+
workspaceTemplate?: string;
|
|
104
|
+
agentDirTemplate?: string;
|
|
105
|
+
maxAgents?: number;
|
|
106
|
+
};
|