@xmanrui/dsh-im 5.8.0 → 5.9.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/README.en.md +42 -7
- package/README.md +53 -7
- package/docs/preset-workspaces.md +37 -0
- package/lib/client.js +34 -2
- package/lib/index.js +329 -326
- package/package.json +3 -1
- package/plugin-src/client/connection-error.js +1 -1
- package/plugin-src/host/channels/dingtalk/production.mjs +2 -1
- package/plugin-src/host/channels/feishu/production.mjs +2 -1
- package/plugin-src/host/channels/matrix/production.mjs +2 -1
- package/plugin-src/host/channels/qq/production.mjs +2 -1
- package/plugin-src/host/channels/shared/binding-events.mjs +130 -0
- package/plugin-src/host/channels/shared/production.mjs +32 -3
- package/plugin-src/host/channels/slack/production.mjs +2 -1
- package/plugin-src/host/channels/wecom/production.mjs +2 -1
- package/plugin-src/host/channels/wecom-app/production.mjs +2 -1
- package/plugin-src/host/channels/weixin/production.mjs +2 -1
- package/plugin-src/host/channels/whatsapp/production.mjs +2 -1
- package/plugin-src/host/delivery-adapter.mjs +122 -1
- package/plugin-src/host/delivery-service.mjs +40 -0
- package/plugin-src/host/ds-bot-client.mjs +28 -2
- package/plugin-src/host/index.mjs +24 -1
- package/plugin-src/host/modern-harness-api.mjs +3 -0
- package/scripts/telegram-menu-scopes.mjs +57 -0
- package/scripts/verify-interface-language.mjs +60 -15
- package/src/channels/dingtalk/dingtalk-bridge.mjs +2 -3
- package/src/channels/dingtalk/dingtalk-menu.mjs +4 -5
- package/src/channels/email/transports/imap-smtp.mjs +21 -11
- package/src/channels/feishu/bridge.mjs +46 -10
- package/src/channels/feishu/feishu-cards.mjs +96 -25
- package/src/channels/qq/qq-menu.mjs +3 -4
- package/src/channels/shared/action-card.mjs +331 -0
- package/src/channels/shared/bot-workspace-store.mjs +123 -8
- package/src/channels/shared/command-catalog.mjs +20 -5
- package/src/channels/shared/conversation/describe.mjs +116 -0
- package/src/channels/shared/conversation-state-store.mjs +62 -0
- package/src/channels/shared/diagnostic-details.mjs +38 -0
- package/src/channels/shared/harness-approval.mjs +58 -13
- package/src/channels/shared/harness-client.mjs +13 -0
- package/src/channels/shared/harness-session-binding.mjs +13 -1
- package/src/channels/shared/i18n-en/feishu.mjs +6 -0
- package/src/channels/shared/i18n-en/shared-a.mjs +9 -0
- package/src/channels/shared/i18n-en/telegram.mjs +14 -0
- package/src/channels/shared/i18n-ru/feishu.mjs +6 -0
- package/src/channels/shared/i18n-ru/shared-a.mjs +9 -0
- package/src/channels/shared/i18n-ru/telegram.mjs +14 -0
- package/src/channels/shared/model-command.mjs +5 -0
- package/src/channels/shared/preset-workspaces.mjs +48 -0
- package/src/channels/shared/qr-encoding-error.mjs +8 -2
- package/src/channels/shared/status-command.mjs +159 -0
- package/src/channels/shared/text-harness-bridge.mjs +86 -4
- package/src/channels/shared/workspace-command.mjs +20 -12
- package/src/channels/shared/workspace-session.mjs +30 -5
- package/src/channels/telegram/approval-card.mjs +379 -0
- package/src/channels/telegram/telegram-api.mjs +10 -1
- package/src/channels/telegram/telegram-bridge.mjs +7 -0
- package/src/channels/telegram/telegram-runtime.mjs +125 -13
- package/src/channels/wecom/wecom-bridge.mjs +5 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmanrui/dsh-im",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.9.0",
|
|
4
4
|
"icon": "./assets/logo-plugin-message-link-gradient.webp",
|
|
5
5
|
"description": "把微信、飞书、企微、钉钉、QQ、Telegram、WhatsApp、Slack、Discord、Matrix接入DSH。 Connect Telegram, WhatsApp, Slack, Discord, Matrix, WeChat, Feishu, WeCom, DingTalk, and QQ to DSH.",
|
|
6
6
|
"keywords": [
|
|
@@ -56,6 +56,7 @@
|
|
|
56
56
|
"lib",
|
|
57
57
|
"locale/*.json",
|
|
58
58
|
"plugin-src",
|
|
59
|
+
"scripts/telegram-menu-scopes.mjs",
|
|
59
60
|
"scripts/verify-interface-language.mjs",
|
|
60
61
|
"scripts/verify-injected-context.mjs",
|
|
61
62
|
"scripts/verify-lan-management.mjs",
|
|
@@ -67,6 +68,7 @@
|
|
|
67
68
|
"PROACTIVE_DELIVERY.md",
|
|
68
69
|
"PROACTIVE_DELIVERY.en.md",
|
|
69
70
|
"docs/client-integration.md",
|
|
71
|
+
"docs/preset-workspaces.md",
|
|
70
72
|
"LICENSE",
|
|
71
73
|
"README.md",
|
|
72
74
|
"README.en.md",
|
|
@@ -37,7 +37,7 @@ export function formatConnectionDiagnostic(value) {
|
|
|
37
37
|
return [
|
|
38
38
|
localizeText(error.message), details.hint ? localizeText(details.hint) : null,
|
|
39
39
|
localizeText('错误码') + ': ' + error.code,
|
|
40
|
-
...['channel', 'operation', 'stage', 'method', 'transport', 'submissionState', 'attempts', 'lastFailureReason', 'reason', 'reasons', 'errorTypes', 'qrEncoder', 'qrRenderer', 'host', 'durationMs', 'timeoutMs', 'nodeVersion', 'dshVersion', 'platform', 'dependencies', 'proxyConfigured', 'truncated', 'httpStatus', 'providerCode', 'resource', 'file', 'field', 'issue', 'referenceId', 'occurredAt', 'rollback', 'pluginVersion']
|
|
40
|
+
...['channel', 'operation', 'stage', 'method', 'transport', 'submissionState', 'attempts', 'lastFailureReason', 'reason', 'reasons', 'errorTypes', 'causeMessage', 'qrEncoder', 'qrRenderer', 'host', 'durationMs', 'timeoutMs', 'nodeVersion', 'dshVersion', 'platform', 'dependencies', 'proxyConfigured', 'truncated', 'httpStatus', 'providerCode', 'resource', 'file', 'field', 'issue', 'referenceId', 'occurredAt', 'rollback', 'pluginVersion']
|
|
41
41
|
.filter(field => details[field] !== undefined)
|
|
42
42
|
.map(field => `${field}: ${typeof details[field] === 'object' ? JSON.stringify(details[field]) : details[field]}`),
|
|
43
43
|
].filter(Boolean).join('\n');
|
|
@@ -63,7 +63,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
63
63
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
64
64
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
65
65
|
const workspaces = internals.workspaces
|
|
66
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
66
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
67
67
|
const canListConfiguredBots = typeof configStore.list === 'function';
|
|
68
68
|
const configuredBots = canListConfiguredBots ? configStore.list() : [];
|
|
69
69
|
if (canListConfiguredBots) {
|
|
@@ -167,6 +167,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
167
167
|
},
|
|
168
168
|
});
|
|
169
169
|
const controller = createWorkspaceAwareController(coreController, {
|
|
170
|
+
harness,
|
|
170
171
|
workspaces,
|
|
171
172
|
stateFor,
|
|
172
173
|
agentPresetCatalog,
|
|
@@ -125,7 +125,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
125
125
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
126
126
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
127
127
|
const workspaces = internals.workspaces
|
|
128
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
128
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
129
129
|
const canListConfiguredBots = typeof configStore.list === 'function';
|
|
130
130
|
const listConfiguredBots = () => canListConfiguredBots ? configStore.list() : [];
|
|
131
131
|
const configuredBots = listConfiguredBots();
|
|
@@ -302,6 +302,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
302
302
|
},
|
|
303
303
|
});
|
|
304
304
|
const controller = createWorkspaceAwareController(coreController, {
|
|
305
|
+
harness,
|
|
305
306
|
workspaces,
|
|
306
307
|
stateFor: stateForBotId,
|
|
307
308
|
agentPresetCatalog,
|
|
@@ -51,7 +51,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
51
51
|
const defaultWorkspace = resolve(config.workspace ?? process.cwd());
|
|
52
52
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
53
53
|
const workspaces = internals.workspaces
|
|
54
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
|
|
54
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
55
55
|
const configuredBots = configStore.list();
|
|
56
56
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
57
57
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -213,6 +213,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
213
213
|
},
|
|
214
214
|
});
|
|
215
215
|
const controller = createWorkspaceAwareController(coreController, {
|
|
216
|
+
harness,
|
|
216
217
|
workspaces,
|
|
217
218
|
stateFor,
|
|
218
219
|
agentPresetCatalog,
|
|
@@ -62,7 +62,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
62
62
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
63
63
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
64
64
|
const workspaces = internals.workspaces
|
|
65
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
65
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
66
66
|
const configuredBots = configStore.list();
|
|
67
67
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
68
68
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -160,6 +160,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
160
160
|
},
|
|
161
161
|
});
|
|
162
162
|
const controller = createWorkspaceAwareController(coreController, {
|
|
163
|
+
harness,
|
|
163
164
|
workspaces,
|
|
164
165
|
stateFor,
|
|
165
166
|
agentPresetCatalog,
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { parseConversationKey } from '../../../../src/channels/shared/conversation/describe.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Identity events for conversation bindings.
|
|
5
|
+
*
|
|
6
|
+
* `dsh-im/session-pre-bind` is announced the moment a conversation points at a
|
|
7
|
+
* session. It runs synchronously and before the Session exists, so a subscriber
|
|
8
|
+
* holds the identity while the first system prompt is assembled.
|
|
9
|
+
*
|
|
10
|
+
* `dsh-im/session-unbind` is announced when a conversation stops pointing at a
|
|
11
|
+
* session: it was switched to another one, or the binding was cleared. Without
|
|
12
|
+
* it a subscriber can only notice that *some* session was bound later, and its
|
|
13
|
+
* own session->identity state keeps claiming a conversation it no longer owns.
|
|
14
|
+
*
|
|
15
|
+
* Both events carry the same conversation fields, so a subscriber handles them
|
|
16
|
+
* symmetrically.
|
|
17
|
+
*/
|
|
18
|
+
export function createBindingAnnouncer({
|
|
19
|
+
channel,
|
|
20
|
+
emit,
|
|
21
|
+
authorsOf = () => null,
|
|
22
|
+
parse = parseConversationKey,
|
|
23
|
+
} = {}) {
|
|
24
|
+
if (typeof emit !== 'function') throw new TypeError('emit is required');
|
|
25
|
+
|
|
26
|
+
const conversationFields = (conversationKey) => {
|
|
27
|
+
const parsed = parse(conversationKey, { channel });
|
|
28
|
+
return {
|
|
29
|
+
channel,
|
|
30
|
+
conversationKey,
|
|
31
|
+
chatType: parsed?.chatType ?? null,
|
|
32
|
+
chatId: parsed?.chatId ?? null,
|
|
33
|
+
threadId: parsed?.threadId ?? null,
|
|
34
|
+
senderId: parsed?.senderId ?? null,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// A subscriber that throws must not break the message being handled, so every
|
|
39
|
+
// announce swallows its own failure.
|
|
40
|
+
const announce = (event, payload) => {
|
|
41
|
+
try {
|
|
42
|
+
emit(event, Object.freeze(payload));
|
|
43
|
+
} catch {
|
|
44
|
+
// ignore a subscriber's failure
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const bind = (botId, conversationKey, sessionId) => {
|
|
49
|
+
try {
|
|
50
|
+
const authors = authorsOf(botId);
|
|
51
|
+
const author = authors?.[conversationKey] ?? null;
|
|
52
|
+
const fields = conversationFields(conversationKey);
|
|
53
|
+
announce('dsh-im/session-pre-bind', {
|
|
54
|
+
...fields,
|
|
55
|
+
botId,
|
|
56
|
+
sessionId,
|
|
57
|
+
senderId: author?.senderId ?? fields.senderId,
|
|
58
|
+
senderName: author?.senderName ?? null,
|
|
59
|
+
});
|
|
60
|
+
} catch {
|
|
61
|
+
// ignore a malformed conversation key or author snapshot
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const unbind = (botId, conversationKey, sessionId) => {
|
|
66
|
+
if (typeof sessionId !== 'string' || sessionId === '') return;
|
|
67
|
+
try {
|
|
68
|
+
announce('dsh-im/session-unbind', {
|
|
69
|
+
...conversationFields(conversationKey),
|
|
70
|
+
botId,
|
|
71
|
+
sessionId,
|
|
72
|
+
});
|
|
73
|
+
} catch {
|
|
74
|
+
// ignore a malformed conversation key
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const previousSession = (state, key) => (typeof state.sessionFor === 'function'
|
|
79
|
+
? state.sessionFor(key)
|
|
80
|
+
: null);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Wrap one bot's state store so every committed binding change is announced.
|
|
84
|
+
* A refused write (`false`) announces nothing, and a write that repeats the
|
|
85
|
+
* current session is not an unbind: the conversation never left it.
|
|
86
|
+
*/
|
|
87
|
+
const wrap = (botId, state) => {
|
|
88
|
+
if (state === null || typeof state !== 'object' || state.__announces === true) return state;
|
|
89
|
+
const setSession = state.setSession;
|
|
90
|
+
if (typeof setSession !== 'function') return state;
|
|
91
|
+
|
|
92
|
+
state.setSession = async (key, sessionId) => {
|
|
93
|
+
const previous = previousSession(state, key);
|
|
94
|
+
const result = await setSession.call(state, key, sessionId);
|
|
95
|
+
if (result === false) return result;
|
|
96
|
+
if (previous !== null && previous !== sessionId) unbind(botId, key, previous);
|
|
97
|
+
bind(botId, key, sessionId);
|
|
98
|
+
return result;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
if (typeof state.clearSession === 'function') {
|
|
102
|
+
const clearSession = state.clearSession;
|
|
103
|
+
state.clearSession = async (key) => {
|
|
104
|
+
const previous = previousSession(state, key);
|
|
105
|
+
const result = await clearSession.call(state, key);
|
|
106
|
+
unbind(botId, key, previous);
|
|
107
|
+
return result;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (typeof state.clearSessions === 'function') {
|
|
112
|
+
const clearSessions = state.clearSessions;
|
|
113
|
+
state.clearSessions = async () => {
|
|
114
|
+
const bindings = typeof state.snapshot === 'function'
|
|
115
|
+
? state.snapshot()?.sessions ?? null
|
|
116
|
+
: null;
|
|
117
|
+
const result = await clearSessions.call(state);
|
|
118
|
+
for (const [key, sessionId] of Object.entries(bindings ?? {})) {
|
|
119
|
+
unbind(botId, key, sessionId);
|
|
120
|
+
}
|
|
121
|
+
return result;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
state.__announces = true;
|
|
126
|
+
return state;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
return { bind, unbind, wrap };
|
|
130
|
+
}
|
|
@@ -4,6 +4,7 @@ import { unlink } from 'node:fs/promises';
|
|
|
4
4
|
import { homedir } from 'node:os';
|
|
5
5
|
import { join, resolve } from 'node:path';
|
|
6
6
|
|
|
7
|
+
import { createBindingAnnouncer } from './binding-events.mjs';
|
|
7
8
|
import { createTokenConnectionSupervisor } from './connection-supervisor.mjs';
|
|
8
9
|
import { createHarnessCommandExecutor } from '../../harness-command-executor.mjs';
|
|
9
10
|
import { harnessConnection } from '../../harness-connection.mjs';
|
|
@@ -71,7 +72,7 @@ export async function createTokenProductionController(ctx, config, internals, de
|
|
|
71
72
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
72
73
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
73
74
|
const workspaces = internals.workspaces
|
|
74
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
75
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
75
76
|
const configuredBots = configStore.list();
|
|
76
77
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
77
78
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -93,14 +94,33 @@ export async function createTokenProductionController(ctx, config, internals, de
|
|
|
93
94
|
});
|
|
94
95
|
const stateStores = new Map();
|
|
95
96
|
const statePath = (botId) => resolve(paths.bots, botId, 'state.json');
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Every binding change is announced the moment it is written: a bind before the
|
|
100
|
+
* Session exists, an unbind when the conversation moves to another session or
|
|
101
|
+
* its binding is cleared. Listeners run synchronously.
|
|
102
|
+
*/
|
|
103
|
+
const announcer = createBindingAnnouncer({
|
|
104
|
+
channel,
|
|
105
|
+
emit: (event, payload) => ctx.emit(event, payload),
|
|
106
|
+
authorsOf: (botId) => (typeof stateStores.get(botId)?.snapshot === 'function'
|
|
107
|
+
? stateStores.get(botId).snapshot()?.authors ?? null
|
|
108
|
+
: null),
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
/** Every loaded store reports its own bindings, once. */
|
|
96
112
|
const stateFor = async (botId) => {
|
|
97
113
|
let state = stateStores.get(botId);
|
|
98
114
|
if (!state) {
|
|
99
|
-
state = await new ResolvedStateStore(statePath(botId)).load();
|
|
115
|
+
state = announcer.wrap(botId, await new ResolvedStateStore(statePath(botId)).load());
|
|
100
116
|
stateStores.set(botId, state);
|
|
101
117
|
}
|
|
102
118
|
return state;
|
|
103
119
|
};
|
|
120
|
+
// Warm the per-bot state memo now: the synchronous identity lookup can only
|
|
121
|
+
// answer from state that is already loaded in this process, and the first prompt
|
|
122
|
+
// of a session arrives long before any delivery call would load it.
|
|
123
|
+
await Promise.all(configuredBots.map((bot) => stateFor(bot.botId).catch(() => null)));
|
|
104
124
|
const commandExecutor = createHarnessCommandExecutor(ctx, internals.commandExecutor);
|
|
105
125
|
const inboundTtl = internals.inboundTtl ?? getInboundTtlRuntime(ctx, config);
|
|
106
126
|
const inboundTtlService = inboundTtl?.service ?? inboundTtl;
|
|
@@ -222,6 +242,7 @@ export async function createTokenProductionController(ctx, config, internals, de
|
|
|
222
242
|
});
|
|
223
243
|
controllerRef = coreController;
|
|
224
244
|
const controller = createWorkspaceAwareController(coreController, {
|
|
245
|
+
harness,
|
|
225
246
|
workspaces,
|
|
226
247
|
stateFor,
|
|
227
248
|
agentPresetCatalog,
|
|
@@ -238,7 +259,15 @@ export async function createTokenProductionController(ctx, config, internals, de
|
|
|
238
259
|
return {
|
|
239
260
|
controller,
|
|
240
261
|
...(supportsDeliveryChannel(channel) ? {
|
|
241
|
-
deliveryAdapter: createDeliveryAdapter({
|
|
262
|
+
deliveryAdapter: createDeliveryAdapter({
|
|
263
|
+
dsBot: config.deliveryService?.dsBot,
|
|
264
|
+
channel,
|
|
265
|
+
workspaces,
|
|
266
|
+
coreController,
|
|
267
|
+
stateFor,
|
|
268
|
+
// The same store, without the await: it is a memo of already loaded bots.
|
|
269
|
+
stateForSync: (botId) => stateStores.get(botId) ?? null,
|
|
270
|
+
}),
|
|
242
271
|
} : {}),
|
|
243
272
|
ready: supervisor.ready,
|
|
244
273
|
async close() {
|
|
@@ -50,7 +50,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
50
50
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
51
51
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
52
52
|
const workspaces = internals.workspaces
|
|
53
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
53
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
54
54
|
const configuredBots = configStore.list();
|
|
55
55
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
56
56
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -148,6 +148,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
148
148
|
},
|
|
149
149
|
});
|
|
150
150
|
const controller = createWorkspaceAwareController(coreController, {
|
|
151
|
+
harness,
|
|
151
152
|
workspaces,
|
|
152
153
|
stateFor,
|
|
153
154
|
agentPresetCatalog,
|
|
@@ -60,7 +60,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
60
60
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
61
61
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
62
62
|
const workspaces = internals.workspaces
|
|
63
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
63
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
64
64
|
const configuredBots = configStore.list();
|
|
65
65
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
66
66
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -163,6 +163,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
163
163
|
},
|
|
164
164
|
});
|
|
165
165
|
const controller = createWorkspaceAwareController(coreController, {
|
|
166
|
+
harness,
|
|
166
167
|
workspaces,
|
|
167
168
|
stateFor,
|
|
168
169
|
agentPresetCatalog,
|
|
@@ -74,7 +74,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
74
74
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
75
75
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
76
76
|
const workspaces = internals.workspaces
|
|
77
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
77
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
78
78
|
const configuredBots = configStore.list();
|
|
79
79
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
80
80
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -194,6 +194,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
194
194
|
},
|
|
195
195
|
});
|
|
196
196
|
const controller = createWorkspaceAwareController(coreController, {
|
|
197
|
+
harness,
|
|
197
198
|
workspaces,
|
|
198
199
|
stateFor,
|
|
199
200
|
agentPresetCatalog,
|
|
@@ -103,7 +103,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
103
103
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
104
104
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
105
105
|
const workspaces = diagnosticWorkspaces(internals.workspaces
|
|
106
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load());
|
|
106
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load());
|
|
107
107
|
const configuredBots = configStore.list();
|
|
108
108
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
109
109
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -203,6 +203,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
203
203
|
},
|
|
204
204
|
});
|
|
205
205
|
const workspaceController = createWorkspaceAwareController(coreController, {
|
|
206
|
+
harness,
|
|
206
207
|
workspaces,
|
|
207
208
|
stateFor,
|
|
208
209
|
agentPresetCatalog,
|
|
@@ -71,7 +71,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
71
71
|
const { defaultWorkspace, ungroupedWorkspace } = await prepareBotWorkspace(config);
|
|
72
72
|
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
|
73
73
|
const workspaces = internals.workspaces
|
|
74
|
-
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace }).load();
|
|
74
|
+
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace, ungroupedWorkspace, groupByAgentPreset: config.groupByAgentPreset }).load();
|
|
75
75
|
const configuredBots = configStore.list();
|
|
76
76
|
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
|
77
77
|
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
|
|
@@ -173,6 +173,7 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|
|
173
173
|
},
|
|
174
174
|
});
|
|
175
175
|
const controller = createWorkspaceAwareController(coreController, {
|
|
176
|
+
harness,
|
|
176
177
|
workspaces,
|
|
177
178
|
stateFor,
|
|
178
179
|
agentPresetCatalog,
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
privateDeliverySuggestionFromConversationKey,
|
|
6
6
|
resolvePrivateConversationKey,
|
|
7
7
|
} from './delivery-suggestions.mjs';
|
|
8
|
+
import { parseConversationKey } from '../../src/channels/shared/conversation/describe.mjs';
|
|
8
9
|
import { withSessionBindingLock } from '../../src/channels/shared/session-binding-lock.mjs';
|
|
9
10
|
import { dsBotError } from './ds-bot-client.mjs';
|
|
10
11
|
|
|
@@ -217,6 +218,85 @@ export function sessionSyncTarget(channel, workspaces, configured) {
|
|
|
217
218
|
return target ? { ...target, targetId: configured.targetId } : null;
|
|
218
219
|
}
|
|
219
220
|
|
|
221
|
+
/** Describe the conversation of one channel's state that owns `sessionId`. */
|
|
222
|
+
function describeFromState(state, sessionId, channel) {
|
|
223
|
+
const sessions = stateSessions(state);
|
|
224
|
+
if (!sessions) return null;
|
|
225
|
+
const conversationKey = Object.keys(sessions).find((key) => sessions[key] === sessionId);
|
|
226
|
+
if (!conversationKey) return null;
|
|
227
|
+
const snapshot = typeof state.snapshot === 'function' ? state.snapshot() : null;
|
|
228
|
+
const authors = snapshot?.authors && typeof snapshot.authors === 'object'
|
|
229
|
+
&& !Array.isArray(snapshot.authors) ? snapshot.authors : null;
|
|
230
|
+
const parsed = parseConversationKey(conversationKey, { channel });
|
|
231
|
+
const author = authors?.[conversationKey] ?? null;
|
|
232
|
+
return {
|
|
233
|
+
conversationKey,
|
|
234
|
+
chatType: parsed?.chatType ?? null,
|
|
235
|
+
chatId: parsed?.chatId ?? null,
|
|
236
|
+
threadId: parsed?.threadId ?? null,
|
|
237
|
+
chatTitle: null,
|
|
238
|
+
senderId: author?.senderId ?? parsed?.senderId ?? null,
|
|
239
|
+
senderName: author?.senderName ?? null,
|
|
240
|
+
senderUsername: author?.senderUsername ?? null,
|
|
241
|
+
senderIsBot: author ? author.isBot === true : null,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function botIdsOfChannel(workspaces) {
|
|
246
|
+
try {
|
|
247
|
+
return workspaces.listBotIds();
|
|
248
|
+
} catch {
|
|
249
|
+
return [];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The bot and the conversation binding that own `sessionId`, awaiting each state. */
|
|
254
|
+
async function findBySession(workspaces, stateOf, sessionId, channel) {
|
|
255
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null;
|
|
256
|
+
for (const botId of botIdsOfChannel(workspaces)) {
|
|
257
|
+
let state;
|
|
258
|
+
try {
|
|
259
|
+
state = await stateOf(botId);
|
|
260
|
+
} catch {
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
let binding = null;
|
|
264
|
+
try {
|
|
265
|
+
binding = describeFromState(state, sessionId, channel);
|
|
266
|
+
} catch {
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (binding !== null) return { botId, binding };
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* The same lookup over states that are already in memory. Never awaits: prompt
|
|
276
|
+
* assembly is synchronous, and a lookup that cannot answer right now has to say
|
|
277
|
+
* `null` instead of starting work the caller will never see.
|
|
278
|
+
*/
|
|
279
|
+
function findBySessionSync(workspaces, stateOf, sessionId, channel) {
|
|
280
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null;
|
|
281
|
+
for (const botId of botIdsOfChannel(workspaces)) {
|
|
282
|
+
let state;
|
|
283
|
+
try {
|
|
284
|
+
state = stateOf(botId);
|
|
285
|
+
} catch {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
if (!state) continue;
|
|
289
|
+
let binding = null;
|
|
290
|
+
try {
|
|
291
|
+
binding = describeFromState(state, sessionId, channel);
|
|
292
|
+
} catch {
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (binding !== null) return { botId, binding };
|
|
296
|
+
}
|
|
297
|
+
return null;
|
|
298
|
+
}
|
|
299
|
+
|
|
220
300
|
function stateSessions(state) {
|
|
221
301
|
if (!state || typeof state.snapshot !== 'function') return null;
|
|
222
302
|
const sessions = state.snapshot()?.sessions;
|
|
@@ -246,7 +326,14 @@ function sessionSyncView(channel, target, sessions, configured) {
|
|
|
246
326
|
}
|
|
247
327
|
|
|
248
328
|
/** Bind one channel's existing workspace store and unwrapped controller to DeliveryService. */
|
|
249
|
-
export function createDeliveryAdapter({
|
|
329
|
+
export function createDeliveryAdapter({
|
|
330
|
+
channel,
|
|
331
|
+
workspaces,
|
|
332
|
+
coreController,
|
|
333
|
+
stateFor,
|
|
334
|
+
stateForSync = null,
|
|
335
|
+
dsBot,
|
|
336
|
+
}) {
|
|
250
337
|
if (!supportsDeliveryChannel(channel)) throw new TypeError(`Unsupported delivery channel: ${channel}`);
|
|
251
338
|
if (!workspaces || typeof workspaces !== 'object') {
|
|
252
339
|
throw new TypeError('delivery adapter requires a workspace store');
|
|
@@ -335,6 +422,40 @@ export function createDeliveryAdapter({ channel, workspaces, coreController, sta
|
|
|
335
422
|
listDsBotBindings: () => workspaces.listDsBotBindings().filter(({ conversationKey }) => (
|
|
336
423
|
privateDeliverySuggestionFromConversationKey(channel, conversationKey)
|
|
337
424
|
)),
|
|
425
|
+
/**
|
|
426
|
+
* Reverse lookup for a session: which conversation of this channel owns it.
|
|
427
|
+
* Reads only what the channel already keeps (its state file), never the
|
|
428
|
+
* network, and never throws — an unknown session is simply `null`.
|
|
429
|
+
*/
|
|
430
|
+
async describeConversationBySession(sessionId) {
|
|
431
|
+
const found = await findBySession(workspaces, stateFor, sessionId, channel);
|
|
432
|
+
if (found === null) return null;
|
|
433
|
+
const { botId, binding } = found;
|
|
434
|
+
let botName = null;
|
|
435
|
+
try {
|
|
436
|
+
if (typeof coreController.describeDeliveryAccount === 'function') {
|
|
437
|
+
botName = (await coreController.describeDeliveryAccount(botId))?.name ?? null;
|
|
438
|
+
}
|
|
439
|
+
} catch {
|
|
440
|
+
botName = null;
|
|
441
|
+
}
|
|
442
|
+
return { channel, botId, botName, ...binding };
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* The same lookup without a single `await`: prompt assembly is synchronous and
|
|
447
|
+
* cannot wait for a promise. Reads only state that is already loaded in this
|
|
448
|
+
* process, so a session of a bot that was never touched answers `null` — the
|
|
449
|
+
* caller decides what an unknown session means. `botName` is left out on
|
|
450
|
+
* purpose: naming a bot needs the network, and a never-awaited lookup must not
|
|
451
|
+
* start one.
|
|
452
|
+
*/
|
|
453
|
+
describeConversationBySessionSync(sessionId) {
|
|
454
|
+
if (typeof stateForSync !== 'function') return null;
|
|
455
|
+
const found = findBySessionSync(workspaces, stateForSync, sessionId, channel);
|
|
456
|
+
if (found === null) return null;
|
|
457
|
+
return { channel, botId: found.botId, botName: null, ...found.binding };
|
|
458
|
+
},
|
|
338
459
|
async listTargets(botId) {
|
|
339
460
|
const targets = workspaces.listDeliveryTargets(botId);
|
|
340
461
|
const configured = new Map(
|
|
@@ -482,6 +482,46 @@ export class DeliveryService {
|
|
|
482
482
|
}
|
|
483
483
|
}
|
|
484
484
|
|
|
485
|
+
/**
|
|
486
|
+
* Describe the conversation a session belongs to, or null when no channel
|
|
487
|
+
* owns it. Pure lookup: no network, no session creation, no throwing.
|
|
488
|
+
* @param {string} sessionId harness session id
|
|
489
|
+
*/
|
|
490
|
+
async describeConversation(sessionId) {
|
|
491
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null;
|
|
492
|
+
for (const { adapter } of this.#adapters.values()) {
|
|
493
|
+
if (typeof adapter.describeConversationBySession !== 'function') continue;
|
|
494
|
+
try {
|
|
495
|
+
const described = await adapter.describeConversationBySession(sessionId);
|
|
496
|
+
if (described) return described;
|
|
497
|
+
} catch {
|
|
498
|
+
// A channel that cannot answer must not hide the others.
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return null;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The same lookup, answered from memory only: no await, no network. A session
|
|
507
|
+
* whose channel has not loaded its state yet answers null — which is exactly the
|
|
508
|
+
* truth a synchronous prompt assembly can act on.
|
|
509
|
+
* @param {string} sessionId harness session id
|
|
510
|
+
*/
|
|
511
|
+
describeConversationSync(sessionId) {
|
|
512
|
+
if (typeof sessionId !== 'string' || sessionId === '') return null;
|
|
513
|
+
for (const { adapter } of this.#adapters.values()) {
|
|
514
|
+
if (typeof adapter.describeConversationBySessionSync !== 'function') continue;
|
|
515
|
+
try {
|
|
516
|
+
const described = adapter.describeConversationBySessionSync(sessionId);
|
|
517
|
+
if (described) return described;
|
|
518
|
+
} catch {
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return null;
|
|
523
|
+
}
|
|
524
|
+
|
|
485
525
|
async describeBot(botId) {
|
|
486
526
|
const id = botIdOf(botId);
|
|
487
527
|
const registration = await this.#checkedRegistrationFor(id);
|
|
@@ -20,13 +20,16 @@ function appearance(bot) {
|
|
|
20
20
|
/** Read DS Bot through the current Host's registered route, without a second
|
|
21
21
|
* runtime, credentials, or assumptions about its configured storage directory. */
|
|
22
22
|
export function createDsBotClient(ctx) {
|
|
23
|
-
async function call(endpoint, payload = {}) {
|
|
23
|
+
async function call(endpoint, payload = {}, options = {}) {
|
|
24
|
+
options.signal?.throwIfAborted();
|
|
24
25
|
const connection = ctx.connection;
|
|
25
26
|
if (typeof connection?.createSharedFetchHandler !== 'function') throw dsBotError();
|
|
27
|
+
const timeout = AbortSignal.timeout(15_000);
|
|
28
|
+
const signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
|
|
26
29
|
const response = await connection.createSharedFetchHandler('/api').fetch(new Request(
|
|
27
30
|
'http://localhost/api/bot', {
|
|
28
31
|
method: 'POST', headers: { 'content-type': 'application/json' },
|
|
29
|
-
body: JSON.stringify({ endpoint, payload }), signal
|
|
32
|
+
body: JSON.stringify({ endpoint, payload }), signal,
|
|
30
33
|
},
|
|
31
34
|
), harnessPeer(ctx));
|
|
32
35
|
if (!response.ok) throw dsBotError();
|
|
@@ -76,6 +79,29 @@ export function createDsBotClient(ctx) {
|
|
|
76
79
|
answerQuestion(sessionId, questionId, answer) {
|
|
77
80
|
return call('answer-question', { sessionId, questionId, answer });
|
|
78
81
|
},
|
|
82
|
+
async updateBotModel(id, selection, options = {}) {
|
|
83
|
+
if (typeof id !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(id)
|
|
84
|
+
|| typeof selection?.provider !== 'string' || !selection.provider
|
|
85
|
+
|| typeof selection?.model !== 'string' || !selection.model
|
|
86
|
+
|| (selection.reasoningEffort !== undefined
|
|
87
|
+
&& (typeof selection.reasoningEffort !== 'string' || !selection.reasoningEffort))) {
|
|
88
|
+
throw dsBotError('model-unavailable');
|
|
89
|
+
}
|
|
90
|
+
// DS Bot owns both its persisted model and all of its sessions. A model
|
|
91
|
+
// change without an explicit effort resets to that model's default.
|
|
92
|
+
const ref = `${selection.provider}/${selection.model}`;
|
|
93
|
+
const updated = await call('update-bot', {
|
|
94
|
+
id, model: ref, effort: selection.reasoningEffort ?? null,
|
|
95
|
+
}, options);
|
|
96
|
+
if (updated?.id !== id || updated?.model !== ref || updated?.appliedModel !== ref) {
|
|
97
|
+
throw dsBotError('model-selection-mismatch');
|
|
98
|
+
}
|
|
99
|
+
return { selected: {
|
|
100
|
+
provider: selection.provider,
|
|
101
|
+
model: selection.model,
|
|
102
|
+
...(updated.appliedEffort ? { reasoningEffort: updated.appliedEffort } : {}),
|
|
103
|
+
} };
|
|
104
|
+
},
|
|
79
105
|
async resolve(id) {
|
|
80
106
|
const result = await status();
|
|
81
107
|
if (result.state !== 'ready') throw dsBotError();
|