@kafca/agentdock 0.1.46 → 0.1.48
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 +11 -0
- package/dist/renderer/assets/{Badge-BIesaDYG.js → Badge-C92lMj9Q.js} +1 -1
- package/dist/renderer/assets/{Chat-CpataoDh.js → Chat-DqoKCPwz.js} +1 -1
- package/dist/renderer/assets/{ChatMarkdown-_Mdmltgv.js → ChatMarkdown-C4psQvmt.js} +1 -1
- package/dist/renderer/assets/{Config-Pe5-zKZk.js → Config-1uk8FQL7.js} +2 -2
- package/dist/renderer/assets/{CronList-BnaOSPT8.js → CronList-DnKCiYWC.js} +1 -1
- package/dist/renderer/assets/{Dashboard-D05l8GKE.js → Dashboard-rOAQcJA0.js} +2 -2
- package/dist/renderer/assets/{EmptyState-BDBavDll.js → EmptyState-bF6Sc61O.js} +1 -1
- package/dist/renderer/assets/{KnowledgeDetail-DvR798l_.js → KnowledgeDetail-C5-Dp95R.js} +2 -2
- package/dist/renderer/assets/{KnowledgeHome-r2ZuvU8-.js → KnowledgeHome-DGa1EWXD.js} +2 -2
- package/dist/renderer/assets/{Logs-BaljtmAV.js → Logs-CKKVW2k8.js} +1 -1
- package/dist/renderer/assets/{Modal-CXhD_OWR.js → Modal-DIc9HxD5.js} +3 -3
- package/dist/renderer/assets/{MonitorList-D7tyipbx.js → MonitorList-D2YOJRIX.js} +1 -1
- package/dist/renderer/assets/{Page-KUuFYgjg.js → Page-vZ-_l6Kn.js} +1 -1
- package/dist/renderer/assets/ProjectDetail-CqTa6kAh.js +11 -0
- package/dist/renderer/assets/{ProjectList-CbdHd5Ev.js → ProjectList-LgDQVVyZ.js} +1 -1
- package/dist/renderer/assets/{Select-Djc8BXMC.js → Select-BN2HMOmY.js} +1 -1
- package/dist/renderer/assets/SessionChat-Beco1N1T.js +1 -0
- package/dist/renderer/assets/{SessionList-DHgl1s1D.js → SessionList-3NkskCJJ.js} +1 -1
- package/dist/renderer/assets/ThreadChat-Ji8pOm5p.js +28 -0
- package/dist/renderer/assets/{Workspace-DTocqY-F.js → Workspace-yarrGItB.js} +1 -1
- package/dist/renderer/assets/{arrow-left-CXyya3hP.js → arrow-left-DNovvfPp.js} +1 -1
- package/dist/renderer/assets/{book-open-9n4Kxc7E.js → book-open-CarZFsXv.js} +1 -1
- package/dist/renderer/assets/{bot-C9HBk5XH.js → bot-g16rGMp6.js} +1 -1
- package/dist/renderer/assets/{check-CutSg9a5.js → check-DjFX73Zi.js} +1 -1
- package/dist/renderer/assets/{chevron-down-DrydR4i3.js → chevron-down-B02YlQS_.js} +1 -1
- package/dist/renderer/assets/{circle-BEjtahw6.js → circle-DPfWdG1F.js} +1 -1
- package/dist/renderer/assets/{desktop-DATgk_ka.js → desktop-DMkue1T6.js} +1 -1
- package/dist/renderer/assets/{funnel-BhqZpS4f.js → funnel-Dy1VL-D9.js} +1 -1
- package/dist/renderer/assets/{heart-Dp8Vs-7_.js → heart-YjvGwq5-.js} +1 -1
- package/dist/renderer/assets/{index-BKpv0P0s.js → index-CZN1STMc.js} +2 -2
- package/dist/renderer/assets/index-CjCuR6DI.css +11 -0
- package/dist/renderer/assets/{index-cI3pWBnN.js → index-mvjDN9uV.js} +9 -9
- package/dist/renderer/assets/{pencil-ywcJzeOo.js → pencil-qpMjLIFd.js} +1 -1
- package/dist/renderer/assets/{play-AtN492Cx.js → play-CPCcNQcH.js} +1 -1
- package/dist/renderer/assets/{plus-BEj14BBV.js → plus-B76JWGKd.js} +1 -1
- package/dist/renderer/assets/projects-C1nFINjh.js +1 -0
- package/dist/renderer/assets/{save-D6ov4CNv.js → save-DtvrD3W1.js} +1 -1
- package/dist/renderer/assets/{search-C3bvw7AR.js → search-Ddth-YHH.js} +1 -1
- package/dist/renderer/assets/{server-C1Uyedyx.js → server-D0zmghb3.js} +1 -1
- package/dist/renderer/assets/{session-utils-DpRwJ6Nh.js → session-utils-C9oe-21F.js} +1 -1
- package/dist/renderer/assets/{sessions-BWmAppwK.js → sessions-BXOnLLah.js} +1 -1
- package/dist/renderer/assets/{shield-check-CVx1mEs2.js → shield-check-Cv5d_blH.js} +1 -1
- package/dist/renderer/assets/{trash-2-o-heXOgs.js → trash-2-C2mimX2b.js} +1 -1
- package/dist/renderer/assets/{user-dPOzUqIc.js → user-BrLJ4elW.js} +1 -1
- package/dist/renderer/assets/{wifi-off-Fz5c3_x_.js → wifi-off-DvRJYLbG.js} +1 -1
- package/dist/renderer/index.html +2 -2
- package/dist-electron/packages/core-sdk/src/index.js +4 -0
- package/dist-electron/services/local-ai-core/src/acp/local-core-acp-session-coordinator.js +53 -7
- package/dist-electron/services/local-ai-core/src/acp/store/local-core-acp-store.js +3 -0
- package/dist-electron/services/local-ai-core/src/acp/store/platform-store.js +13 -5
- package/dist-electron/services/local-ai-core/src/acp/store/schema.js +1 -0
- package/dist-electron/services/local-ai-core/src/acp/store/thread-store.js +3 -0
- package/dist-electron/services/local-ai-core/src/agents/pi/launch.js +1 -1
- package/dist-electron/services/local-ai-core/src/agents/shared/launch-utils.js +22 -0
- package/dist-electron/services/local-ai-core/src/channel/lark/local-core-lark-gateway.js +132 -348
- package/dist-electron/services/local-ai-core/src/channel/shared/base-channel-gateway.js +306 -0
- package/dist-electron/services/local-ai-core/src/channel/shared/session-command-runtime.js +14 -3
- package/dist-electron/services/local-ai-core/src/channel/weixin/local-core-weixin-gateway.js +131 -382
- package/dist-electron/services/local-ai-core/src/router/workspace-router.js +8 -1
- package/dist-electron/services/local-ai-core/src/runtime/agent-runtime-detector.js +2 -1
- package/dist-electron/services/local-ai-core/src/runtime/channel-service.js +150 -0
- package/dist-electron/services/local-ai-core/src/runtime/env-utils.js +12 -0
- package/dist-electron/services/local-ai-core/src/runtime/external-service.js +234 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/automation-handler.js +34 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/capabilities-handler.js +12 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/channel-handler.js +65 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/external-handler.js +17 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/knowledge-handler.js +65 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/openai-handler.js +496 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/provider-handler.js +24 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/runtime-handler.js +68 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/runtimes-handler.js +37 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/scheduler-handler.js +34 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/security-handler.js +49 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/task-handler.js +27 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/thread-handler.js +46 -0
- package/dist-electron/services/local-ai-core/src/runtime/handlers/workspace-handler.js +29 -0
- package/dist-electron/services/local-ai-core/src/runtime/local-core-controller.js +40 -592
- package/dist-electron/services/local-ai-core/src/runtime/server-helpers.js +96 -0
- package/dist-electron/services/local-ai-core/src/runtime/server-routes.js +3 -0
- package/dist-electron/services/local-ai-core/src/runtime/server.js +107 -1084
- package/dist-electron/services/local-ai-core/src/runtime/standalone.js +13 -1
- package/dist-electron/services/local-ai-core/src/thread/thread-command-service.js +23 -2
- package/dist-electron/services/local-ai-core/src/thread/thread-slash-command-dispatcher.js +1 -0
- package/dist-electron/src/pages/Projects/project-provider-config.js +75 -0
- package/dist-electron/src/pages/Threads/thread-chat-model.js +2 -0
- package/dist-electron/tests/contracts/architecture-docs.test.js +1 -0
- package/dist-electron/tests/electron/agent-runtime-detector.test.js +18 -0
- package/dist-electron/tests/electron/knowledge-skill-script.test.js +15 -5
- package/dist-electron/tests/electron/lac-cli.test.js +591 -688
- package/dist-electron/tests/electron/plugin-kernel.test.js +47 -31
- package/dist-electron/tests/electron/project-provider-config.test.js +71 -0
- package/dist-electron/tests/electron/workspace-task-store.test.js +40 -11
- package/dist-electron/tests/integration/local-core-cors.test.js +22 -0
- package/dist-electron/tests/integration/local-core-refactor.test.js +177 -0
- package/dist-electron/tests/integration/local-core-routes.test.js +45 -0
- package/package.json +2 -2
- package/dist/renderer/assets/ProjectDetail-Ddq94-yu.js +0 -11
- package/dist/renderer/assets/SessionChat-DOy0iKQR.js +0 -1
- package/dist/renderer/assets/ThreadChat-I7dYibDc.js +0 -23
- package/dist/renderer/assets/index-CO4fGqmv.css +0 -11
- package/dist/renderer/assets/projects-Clar8lgb.js +0 -1
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseChannelGateway = void 0;
|
|
4
|
+
const node_events_1 = require("node:events");
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const session_command_runtime_js_1 = require("../shared/session-command-runtime.js");
|
|
7
|
+
const thread_slash_command_dispatcher_js_1 = require("../../thread/thread-slash-command-dispatcher.js");
|
|
8
|
+
function resolveRuntimeKey(workspaceId, instanceId) {
|
|
9
|
+
return instanceId ? `${workspaceId}::${instanceId}` : workspaceId;
|
|
10
|
+
}
|
|
11
|
+
class BaseChannelGateway extends node_events_1.EventEmitter {
|
|
12
|
+
options;
|
|
13
|
+
routeType = 'channel.chat';
|
|
14
|
+
runtime = new Map();
|
|
15
|
+
threadRouting = new Map();
|
|
16
|
+
outboundEventChains = new Map();
|
|
17
|
+
outboundTurns = new Map();
|
|
18
|
+
mutedThreadBridgeCounts = new Map();
|
|
19
|
+
sessionCommandRuntime;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
super();
|
|
22
|
+
this.options = options;
|
|
23
|
+
const slashCommands = new thread_slash_command_dispatcher_js_1.ThreadSlashCommandDispatcher({
|
|
24
|
+
session: {
|
|
25
|
+
listThreads: (workspaceId) => options.getWorkspaceRouter().listThreads(workspaceId),
|
|
26
|
+
getThread: (threadId) => options.getWorkspaceRouter().getThread(threadId),
|
|
27
|
+
createThread: (workspaceId, title) => options.getWorkspaceRouter().createThread(workspaceId, title),
|
|
28
|
+
renameThread: (threadId, title) => options.getWorkspaceRouter().renameThread(threadId, title),
|
|
29
|
+
deleteThread: (threadId) => options.getWorkspaceRouter().deleteThread(threadId),
|
|
30
|
+
},
|
|
31
|
+
thread: {
|
|
32
|
+
getThreadRow: (threadId) => options.store.getThreadRow(threadId),
|
|
33
|
+
updateThreadAgentMode: (threadId, mode) => options.store.updateThreadAgentMode(threadId, mode),
|
|
34
|
+
updateThreadAgentType: (threadId, agentType) => options.store.updateThreadAgentType(threadId, agentType),
|
|
35
|
+
getLatestRunForThread: (threadId) => options.store.getLatestRunForThread(threadId),
|
|
36
|
+
createAuditEvent: (input) => options.store.createAuditEvent(input),
|
|
37
|
+
getAgentTypes: () => options.getWorkspaceRouter().getAgentTypes(),
|
|
38
|
+
setThreadMode: async (threadId, mode) => {
|
|
39
|
+
await options.getWorkspaceRouter().setThreadMode(threadId, mode);
|
|
40
|
+
},
|
|
41
|
+
closeThreadSession: (threadId) => options.getWorkspaceRouter().closeThreadSession(threadId),
|
|
42
|
+
interruptRun: (runId) => options.getWorkspaceRouter().interruptRun(runId),
|
|
43
|
+
setChannelPreferredAgent: (input) => options.store.updatePlatformThreadPreferredAgent(input.workspaceId, input.chatId, input.platformUserId, input.agentType, input.platform),
|
|
44
|
+
log: options.log,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
this.sessionCommandRuntime = new session_command_runtime_js_1.ChannelSessionCommandRuntime({
|
|
48
|
+
dispatcher: slashCommands,
|
|
49
|
+
store: options.store,
|
|
50
|
+
getThreadSessionKey: (threadId) => options.getWorkspaceRouter().getThreadSessionKey(threadId),
|
|
51
|
+
setThreadRoute: (sessionKey, route) => {
|
|
52
|
+
this.threadRouting.set(sessionKey, route);
|
|
53
|
+
},
|
|
54
|
+
createRoute: (input, threadId) => this.makeThreadRoute(input, threadId),
|
|
55
|
+
sendResult: (input, result) => this.sendSessionCommandResult(input, result),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
// ==================== Lifecycle ====================
|
|
59
|
+
async refreshBindings() {
|
|
60
|
+
const config = await this.options.readConfig();
|
|
61
|
+
const bindings = this.collectBindings(config);
|
|
62
|
+
const nextKeys = new Set(bindings.map((b) => resolveRuntimeKey(b.workspaceId, b.instanceId)));
|
|
63
|
+
for (const key of [...this.runtime.keys()]) {
|
|
64
|
+
if (!nextKeys.has(key)) {
|
|
65
|
+
await this.stopWorkspaceKey(key);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
for (const binding of bindings) {
|
|
69
|
+
const key = resolveRuntimeKey(binding.workspaceId, binding.instanceId);
|
|
70
|
+
const current = this.runtime.get(key);
|
|
71
|
+
if (!binding.enabled) {
|
|
72
|
+
if (current) {
|
|
73
|
+
await this.stopWorkspaceKey(key);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
this.runtime.set(key, this.createDisabledState(binding));
|
|
77
|
+
}
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (current?.status === 'running' && this.isSameIdentity(current, binding)) {
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
await this.startWorkspace(binding);
|
|
84
|
+
}
|
|
85
|
+
this.notifyRuntimeStateChanged();
|
|
86
|
+
}
|
|
87
|
+
/** Returns true if the current runtime state belongs to the same identity as the binding (skip restart). */
|
|
88
|
+
isSameIdentity(_state, _binding) {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
async enable(workspaceId, instanceId) {
|
|
92
|
+
const binding = await this.getBinding(workspaceId, instanceId);
|
|
93
|
+
await this.startWorkspace(binding);
|
|
94
|
+
return this.getStatus(workspaceId, binding.instanceId);
|
|
95
|
+
}
|
|
96
|
+
async disable(workspaceId, instanceId) {
|
|
97
|
+
await this.stopWorkspace(workspaceId, instanceId);
|
|
98
|
+
return this.getStatus(workspaceId, instanceId);
|
|
99
|
+
}
|
|
100
|
+
async start() {
|
|
101
|
+
await this.refreshBindings();
|
|
102
|
+
}
|
|
103
|
+
async stop() {
|
|
104
|
+
await this.close();
|
|
105
|
+
}
|
|
106
|
+
close() {
|
|
107
|
+
return Promise.all([...this.runtime.keys()].map((key) => this.stopWorkspaceKey(key))).then(() => undefined);
|
|
108
|
+
}
|
|
109
|
+
// ==================== Status ====================
|
|
110
|
+
getStatus(workspaceId, instanceId) {
|
|
111
|
+
this.options.store.expirePendingPairings();
|
|
112
|
+
const resolved = this.resolveRuntimeState(workspaceId, instanceId);
|
|
113
|
+
return this.buildStatusObject(resolved.state, resolved);
|
|
114
|
+
}
|
|
115
|
+
listStatuses() {
|
|
116
|
+
return [...this.runtime.values()]
|
|
117
|
+
.sort((a, b) => `${a.workspaceId}:${a.instanceId}`.localeCompare(`${b.workspaceId}:${b.instanceId}`))
|
|
118
|
+
.map((state) => this.getStatus(state.workspaceId, state.instanceId));
|
|
119
|
+
}
|
|
120
|
+
listPendingPairings(workspaceId) {
|
|
121
|
+
this.options.store.expirePendingPairings();
|
|
122
|
+
return this.options.store
|
|
123
|
+
.listPairingRequests(workspaceId)
|
|
124
|
+
.filter((item) => item.platform === this.platform || item.platform.startsWith(`${this.platform}:`))
|
|
125
|
+
.filter((item) => item.status === 'pending' && item.expiresAt >= new Date().toISOString());
|
|
126
|
+
}
|
|
127
|
+
listAuthorizedUsers(workspaceId) {
|
|
128
|
+
return this.options.store.listAuthorizedUsers(workspaceId)
|
|
129
|
+
.filter((item) => item.platform === this.platform || item.platform.startsWith(`${this.platform}:`));
|
|
130
|
+
}
|
|
131
|
+
// ==================== Pairing ====================
|
|
132
|
+
approvePairing(code) {
|
|
133
|
+
this.options.store.expirePendingPairings();
|
|
134
|
+
const pairing = this.options.store.getPairingRequest(code);
|
|
135
|
+
if (!pairing)
|
|
136
|
+
throw new Error(`Pairing code not found: ${code}`);
|
|
137
|
+
if (pairing.platform !== this.platform && !pairing.platform.startsWith(`${this.platform}:`))
|
|
138
|
+
throw new Error(`Pairing code ${code} is not a ${this.platform} pairing`);
|
|
139
|
+
if (pairing.status !== 'pending')
|
|
140
|
+
throw new Error(`Pairing code ${code} is already ${pairing.status}`);
|
|
141
|
+
if (pairing.expires_at < new Date().toISOString()) {
|
|
142
|
+
this.options.store.updatePairingStatus(code, 'expired');
|
|
143
|
+
throw new Error(`Pairing code ${code} has expired`);
|
|
144
|
+
}
|
|
145
|
+
const existing = this.options.store.getAuthorizedUser(pairing.workspace_id, pairing.platform_user_id, pairing.platform);
|
|
146
|
+
const userId = existing?.id || `${this.platform}-user-${(0, node_crypto_1.randomUUID)()}`;
|
|
147
|
+
const authorizedAt = new Date().toISOString();
|
|
148
|
+
this.options.store.createAuthorizedUser({
|
|
149
|
+
id: userId,
|
|
150
|
+
workspace_id: pairing.workspace_id,
|
|
151
|
+
platform: pairing.platform,
|
|
152
|
+
platform_user_id: pairing.platform_user_id,
|
|
153
|
+
chat_id: pairing.chat_id,
|
|
154
|
+
display_name: pairing.display_name,
|
|
155
|
+
thread_id: existing?.thread_id || null,
|
|
156
|
+
authorized_at: authorizedAt,
|
|
157
|
+
});
|
|
158
|
+
this.options.store.updatePairingStatus(code, 'approved');
|
|
159
|
+
this.notifyRuntimeStateChanged();
|
|
160
|
+
const user = this.options.store.listAuthorizedUsers(pairing.workspace_id, pairing.platform).find((e) => e.id === userId);
|
|
161
|
+
if (!user)
|
|
162
|
+
throw new Error('Authorized user lookup failed after approval');
|
|
163
|
+
return user;
|
|
164
|
+
}
|
|
165
|
+
rejectPairing(code) {
|
|
166
|
+
const pairing = this.options.store.getPairingRequest(code);
|
|
167
|
+
if (!pairing)
|
|
168
|
+
throw new Error(`Pairing code not found: ${code}`);
|
|
169
|
+
if (pairing.platform !== this.platform && !pairing.platform.startsWith(`${this.platform}:`))
|
|
170
|
+
throw new Error(`Pairing code ${code} is not a ${this.platform} pairing`);
|
|
171
|
+
this.options.store.updatePairingStatus(code, 'rejected');
|
|
172
|
+
this.notifyRuntimeStateChanged();
|
|
173
|
+
return { rejected: true };
|
|
174
|
+
}
|
|
175
|
+
// ==================== Bridge Event Throttling ====================
|
|
176
|
+
muteThreadBridge(threadId) {
|
|
177
|
+
const current = this.mutedThreadBridgeCounts.get(threadId) || 0;
|
|
178
|
+
this.mutedThreadBridgeCounts.set(threadId, current + 1);
|
|
179
|
+
}
|
|
180
|
+
unmuteThreadBridge(threadId) {
|
|
181
|
+
const current = this.mutedThreadBridgeCounts.get(threadId) || 0;
|
|
182
|
+
if (current <= 1) {
|
|
183
|
+
this.mutedThreadBridgeCounts.delete(threadId);
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
this.mutedThreadBridgeCounts.set(threadId, current - 1);
|
|
187
|
+
}
|
|
188
|
+
// ==================== Scheduling ====================
|
|
189
|
+
async sendScheduledCard(workspaceId, chatId, text) {
|
|
190
|
+
return this.sendScheduledMessage(workspaceId, { type: 'channel.chat', channelId: chatId }, text);
|
|
191
|
+
}
|
|
192
|
+
async sendScheduledMessage(workspaceId, route, text) {
|
|
193
|
+
const state = this.resolveRuntimeState(workspaceId, route.instanceId).state;
|
|
194
|
+
if (!state?.connected) {
|
|
195
|
+
this.options.log?.(`[${this.platform}] scheduled message skipped: workspace not connected: ${workspaceId}`);
|
|
196
|
+
return '';
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await this.sendOutboundMessage(workspaceId, {
|
|
200
|
+
route: { type: 'channel.chat', channelId: route.channelId, participantId: route.participantId },
|
|
201
|
+
parts: [{ type: 'text', text }],
|
|
202
|
+
});
|
|
203
|
+
return `${this.platform}_sched_${(0, node_crypto_1.randomUUID)()}`;
|
|
204
|
+
}
|
|
205
|
+
catch (error) {
|
|
206
|
+
this.options.log?.(`[${this.platform}] scheduled message failed for ${workspaceId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
207
|
+
return '';
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
registerScheduledThreadBridge(input) {
|
|
211
|
+
const instanceId = input.route.instanceId || 'default';
|
|
212
|
+
const route = {
|
|
213
|
+
workspaceId: input.workspaceId,
|
|
214
|
+
instanceId,
|
|
215
|
+
platformKey: `${this.platform}${instanceId !== 'default' ? `:${instanceId}` : ''}`,
|
|
216
|
+
platformUserId: input.route.participantId || '',
|
|
217
|
+
chatId: input.route.channelId,
|
|
218
|
+
threadId: input.threadId,
|
|
219
|
+
};
|
|
220
|
+
const previousRoute = this.threadRouting.get(input.sessionKey);
|
|
221
|
+
this.threadRouting.set(input.sessionKey, route);
|
|
222
|
+
return () => {
|
|
223
|
+
if (previousRoute) {
|
|
224
|
+
this.threadRouting.set(input.sessionKey, previousRoute);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.threadRouting.delete(input.sessionKey);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
// ==================== Session Commands ====================
|
|
232
|
+
async executeSessionCommand(input) {
|
|
233
|
+
return this.sessionCommandRuntime.execute(input);
|
|
234
|
+
}
|
|
235
|
+
// ==================== Runtime Management ====================
|
|
236
|
+
resolveRuntimeState(workspaceId, instanceId) {
|
|
237
|
+
const key = resolveRuntimeKey(workspaceId, instanceId);
|
|
238
|
+
if (instanceId) {
|
|
239
|
+
const state = this.runtime.get(key) || (instanceId === 'default' ? this.runtime.get(workspaceId) : undefined);
|
|
240
|
+
return { instanceId, state };
|
|
241
|
+
}
|
|
242
|
+
const states = [...this.runtime.values()].filter((entry) => entry.workspaceId === workspaceId);
|
|
243
|
+
const state = states.find((entry) => entry.instanceId === 'default') || states[0];
|
|
244
|
+
return { instanceId: state?.instanceId || 'default', state: state || this.runtime.get(workspaceId) };
|
|
245
|
+
}
|
|
246
|
+
async getBinding(workspaceId, instanceId) {
|
|
247
|
+
const config = await this.options.readConfig();
|
|
248
|
+
const bindings = this.collectBindings(config).filter((entry) => entry.workspaceId === workspaceId);
|
|
249
|
+
const binding = instanceId
|
|
250
|
+
? bindings.find((entry) => entry.instanceId === instanceId)
|
|
251
|
+
: bindings.find((entry) => entry.instanceId === 'default') || bindings[0];
|
|
252
|
+
if (!binding) {
|
|
253
|
+
throw new Error(`No ${this.platform} binding configured for workspace "${workspaceId}"${instanceId ? ` instance "${instanceId}"` : ''}`);
|
|
254
|
+
}
|
|
255
|
+
return binding;
|
|
256
|
+
}
|
|
257
|
+
async stopWorkspace(workspaceId, instanceId) {
|
|
258
|
+
const resolved = this.resolveRuntimeState(workspaceId, instanceId);
|
|
259
|
+
await this.stopWorkspaceKey(resolveRuntimeKey(workspaceId, resolved.instanceId));
|
|
260
|
+
}
|
|
261
|
+
async stopWorkspaceKey(key) {
|
|
262
|
+
const state = this.runtime.get(key);
|
|
263
|
+
if (!state)
|
|
264
|
+
return;
|
|
265
|
+
try {
|
|
266
|
+
await this.stopWorkspaceTransport(state);
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
this.options.log?.(`[${this.platform}] stop transport failed for ${state.workspaceId}/${state.instanceId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
270
|
+
}
|
|
271
|
+
this.resetStateToStopped(state, key);
|
|
272
|
+
this.notifyRuntimeStateChanged();
|
|
273
|
+
}
|
|
274
|
+
// ==================== Error Management ====================
|
|
275
|
+
notifyRuntimeStateChanged() {
|
|
276
|
+
this.options.eventBus.emit({
|
|
277
|
+
type: 'runtime.state.changed',
|
|
278
|
+
payload: {
|
|
279
|
+
reason: 'channel-bindings',
|
|
280
|
+
},
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
clearRuntimeError(state) {
|
|
284
|
+
state.lastError = undefined;
|
|
285
|
+
state.lastErrorInfo = undefined;
|
|
286
|
+
state.lastErrorAt = undefined;
|
|
287
|
+
}
|
|
288
|
+
setRuntimeError(state, errorInfo) {
|
|
289
|
+
state.lastError = errorInfo.message;
|
|
290
|
+
state.lastErrorInfo = errorInfo;
|
|
291
|
+
state.lastErrorAt = new Date().toISOString();
|
|
292
|
+
this.options.eventBus.emit({
|
|
293
|
+
type: 'localcore.error',
|
|
294
|
+
payload: {
|
|
295
|
+
scope: `channel.${this.platform}`,
|
|
296
|
+
errorInfo,
|
|
297
|
+
context: {
|
|
298
|
+
workspaceId: state.workspaceId,
|
|
299
|
+
instanceId: state.instanceId,
|
|
300
|
+
platform: this.platform,
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
exports.BaseChannelGateway = BaseChannelGateway;
|
|
@@ -13,6 +13,11 @@ class ChannelSessionCommandRuntime {
|
|
|
13
13
|
content: input.text,
|
|
14
14
|
defaultTitle: input.defaultTitle,
|
|
15
15
|
defaultAgentType: input.defaultAgentType,
|
|
16
|
+
channel: {
|
|
17
|
+
chatId: input.chatId,
|
|
18
|
+
platformUserId: input.platformUserId,
|
|
19
|
+
platform: input.platformKey,
|
|
20
|
+
},
|
|
16
21
|
});
|
|
17
22
|
if (!result.handled) {
|
|
18
23
|
return { handled: false, threadId: input.currentThreadId };
|
|
@@ -20,7 +25,7 @@ class ChannelSessionCommandRuntime {
|
|
|
20
25
|
let activeThreadId = input.currentThreadId;
|
|
21
26
|
for (const effect of result.effects || []) {
|
|
22
27
|
if (effect.type === 'created_thread') {
|
|
23
|
-
this.
|
|
28
|
+
this.inheritAgentState(input, input.currentThreadId, effect.threadId);
|
|
24
29
|
}
|
|
25
30
|
if (effect.type === 'activate_thread') {
|
|
26
31
|
activeThreadId = effect.threadId;
|
|
@@ -30,11 +35,17 @@ class ChannelSessionCommandRuntime {
|
|
|
30
35
|
await this.options.sendResult(input, result);
|
|
31
36
|
return { handled: true, threadId: activeThreadId, result };
|
|
32
37
|
}
|
|
33
|
-
|
|
34
|
-
const
|
|
38
|
+
inheritAgentState(input, sourceThreadId, targetThreadId) {
|
|
39
|
+
const sourceRow = this.options.store.getThreadRow?.(sourceThreadId);
|
|
40
|
+
const inheritedMode = sourceRow?.agent_mode || '';
|
|
35
41
|
if (inheritedMode && inheritedMode !== 'default') {
|
|
36
42
|
this.options.store.updateThreadAgentMode?.(targetThreadId, inheritedMode);
|
|
37
43
|
}
|
|
44
|
+
const binding = this.options.store.getPlatformThreadBinding?.(input.workspaceId, input.chatId, input.platformUserId, input.platformKey);
|
|
45
|
+
const preferredAgent = binding?.preferred_agent_type || sourceRow?.agent_type || '';
|
|
46
|
+
if (preferredAgent) {
|
|
47
|
+
this.options.store.updateThreadAgentType?.(targetThreadId, preferredAgent);
|
|
48
|
+
}
|
|
38
49
|
}
|
|
39
50
|
activateThread(input, threadId) {
|
|
40
51
|
const now = new Date().toISOString();
|