@octopi-agent/engine 0.65.0 → 0.70.7
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/dist/config-schema/engine.d.ts +84 -0
- package/dist/config-schema/engine.js +4 -1
- package/dist/config-schema/substrate.d.ts +86 -0
- package/dist/config-schema/substrate.js +114 -0
- package/dist/config.d.ts +71 -0
- package/dist/harness/agent/builder.d.ts +1 -1
- package/dist/harness/agent/builder.js +27 -2
- package/dist/harness/agent/config-bridge.js +17 -4
- package/dist/harness/context/capabilities/summary/gate.js +2 -2
- package/dist/harness/context/capabilities/summary/policies.js +5 -5
- package/dist/harness/context/capabilities/summary/registry.js +29 -24
- package/dist/harness/context/layer-health.js +3 -2
- package/dist/harness/context/layers.d.ts +27 -13
- package/dist/harness/context/layers.js +76 -66
- package/dist/harness/context/system-prompt-assembler.d.ts +3 -0
- package/dist/harness/context/system-prompt-assembler.js +11 -1
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.js +3 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.d.ts +56 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.js +281 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +98 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +250 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.d.ts +124 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.js +372 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +109 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.js +8 -0
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/memory.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +14 -0
- package/dist/harness/index.d.ts +4 -3
- package/dist/harness/index.js +2 -1
- package/dist/harness/knowledge/query-service.js +5 -5
- package/dist/harness/knowledge/retriever.js +1 -1
- package/dist/harness/knowledge/source-store.d.ts +17 -2
- package/dist/harness/knowledge/source-store.js +6 -5
- package/dist/harness/memory/cognition-decay.d.ts +36 -0
- package/dist/harness/memory/cognition-decay.js +55 -0
- package/dist/harness/memory/cognition-gates.d.ts +60 -0
- package/dist/harness/memory/cognition-gates.js +191 -0
- package/dist/harness/memory/cognition-trigger.d.ts +30 -0
- package/dist/harness/memory/cognition-trigger.js +41 -0
- package/dist/harness/memory/cognition-types.d.ts +1 -1
- package/dist/harness/memory/cognition.d.ts +40 -8
- package/dist/harness/memory/cognition.js +432 -79
- package/dist/harness/memory/conceptualizer.d.ts +66 -0
- package/dist/harness/memory/conceptualizer.js +246 -0
- package/dist/harness/memory/index.d.ts +16 -2
- package/dist/harness/memory/index.js +11 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +9 -0
- package/dist/harness/memory/sqlite/agent-db.js +118 -15
- package/dist/harness/memory/sqlite/cognition-store.d.ts +55 -16
- package/dist/harness/memory/sqlite/cognition-store.js +591 -144
- package/dist/harness/memory/sqlite/wisdom-store.d.ts +33 -14
- package/dist/harness/memory/sqlite/wisdom-store.js +257 -44
- package/dist/harness/memory/types.d.ts +331 -16
- package/dist/harness/memory/types.js +49 -0
- package/dist/harness/memory/wisdom-formation.d.ts +101 -0
- package/dist/harness/memory/wisdom-formation.js +304 -0
- package/dist/harness/memory/wisdom-gates.d.ts +50 -0
- package/dist/harness/memory/wisdom-gates.js +204 -0
- package/dist/harness/memory/wisdom-policy.d.ts +107 -0
- package/dist/harness/memory/wisdom-policy.js +220 -0
- package/dist/harness/memory/wisdom-types.d.ts +3 -2
- package/dist/harness/memory/wisdom-types.js +2 -2
- package/dist/harness/memory/wisdom.d.ts +27 -0
- package/dist/harness/memory/wisdom.js +181 -0
- package/dist/harness/run/agent/agent.d.ts +9 -0
- package/dist/harness/run/agent/agent.js +19 -0
- package/dist/harness/run/reliability/run-agent.js +6 -2
- package/dist/harness/run/runner.js +2 -1
- package/dist/harness/session/index.d.ts +1 -0
- package/dist/harness/session/index.js +1 -0
- package/dist/harness/session/title.d.ts +86 -0
- package/dist/harness/session/title.js +268 -0
- package/dist/harness/shared/events/agent-event-map.d.ts +8 -0
- package/dist/harness/shared/events/agent-event-map.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.d.ts +9 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +125 -0
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -0
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +12 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.js +93 -0
- package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.js +91 -0
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -0
- package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -0
- package/dist/subsystems/wisdom-steward/formation/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/formation/handler.js +212 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -0
- package/dist/subsystems/wisdom-steward/govern/handler.d.ts +17 -0
- package/dist/subsystems/wisdom-steward/govern/handler.js +68 -0
- package/package.json +2 -2
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClientToolRegistry — 会话内客户端能力注册表
|
|
3
|
+
*
|
|
4
|
+
* - 多个 client 可注册同名 tool;默认路由「最近活跃」,成功后 session sticky
|
|
5
|
+
* - `clientFilter` 可钉 platform / instance
|
|
6
|
+
* - 敏感 device:候选必须唯一(或 filter 钉端),否则拒绝
|
|
7
|
+
* - 某名字最后一家下线时,由调用方 unregister 对应 RegisteredTool
|
|
8
|
+
*/
|
|
9
|
+
import type { ClientToolDescriptor, ClientToolName, ClientToolProvider } from './types.js';
|
|
10
|
+
export type ClientToolRouteDecision = {
|
|
11
|
+
ok: true;
|
|
12
|
+
clientInstanceId: string;
|
|
13
|
+
descriptor: ClientToolDescriptor;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
reason: 'unsupported' | 'ambiguous' | 'client_unavailable';
|
|
17
|
+
hint?: string;
|
|
18
|
+
};
|
|
19
|
+
export declare class ClientToolRegistry {
|
|
20
|
+
/** sessionId → clientInstanceId → provider */
|
|
21
|
+
private bySession;
|
|
22
|
+
/** session sticky:sessionId|name → 最近成功端 */
|
|
23
|
+
private preferred;
|
|
24
|
+
/** 在线性:超过该时长未心跳的 provider 视为离线(不进 tool 面、不可路由) */
|
|
25
|
+
private readonly liveTtlMs;
|
|
26
|
+
/** 可注入时钟(测试用);默认 Date.now */
|
|
27
|
+
private readonly clock;
|
|
28
|
+
constructor(options?: {
|
|
29
|
+
liveTtlMs?: number;
|
|
30
|
+
clock?: () => number;
|
|
31
|
+
});
|
|
32
|
+
private at;
|
|
33
|
+
private isLive;
|
|
34
|
+
private stickyKey;
|
|
35
|
+
/** 某 tool 在 session 内成功后钉偏好端(在线期间优先) */
|
|
36
|
+
notePreferred(sessionId: string, name: ClientToolName, clientInstanceId: string): void;
|
|
37
|
+
/** 清理失效 sticky(provider 不再 live 或已不声明该 tool) */
|
|
38
|
+
private dropStalePreferred;
|
|
39
|
+
/**
|
|
40
|
+
* 剔除超时未心跳的 provider(在线性打底)
|
|
41
|
+
*
|
|
42
|
+
* @returns 各 session 上消失的 tool 名(可能重复)
|
|
43
|
+
*/
|
|
44
|
+
expireStaleProviders(now?: number): Array<{
|
|
45
|
+
sessionId: string;
|
|
46
|
+
removedNames: ClientToolName[];
|
|
47
|
+
}>;
|
|
48
|
+
private collectNames;
|
|
49
|
+
/**
|
|
50
|
+
* 注册/更新某客户端在某会话上的能力(能力快照)
|
|
51
|
+
*
|
|
52
|
+
* @returns 本次新出现的 tool 名(调用方据此 register 进 ToolBus)
|
|
53
|
+
*/
|
|
54
|
+
registerClientProviders(input: {
|
|
55
|
+
sessionId: string;
|
|
56
|
+
clientInstanceId: string;
|
|
57
|
+
platform?: string;
|
|
58
|
+
principalId?: string;
|
|
59
|
+
descriptors: ClientToolDescriptor[];
|
|
60
|
+
now?: number;
|
|
61
|
+
}): {
|
|
62
|
+
addedNames: ClientToolName[];
|
|
63
|
+
allNames: ClientToolName[];
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* 客户端下线/撤回能力
|
|
67
|
+
*
|
|
68
|
+
* @returns 会话中已无任何 provider 的 tool 名(调用方 unregister)
|
|
69
|
+
*/
|
|
70
|
+
unregisterClientProviders(input: {
|
|
71
|
+
sessionId: string;
|
|
72
|
+
clientInstanceId: string;
|
|
73
|
+
}): {
|
|
74
|
+
removedNames: ClientToolName[];
|
|
75
|
+
};
|
|
76
|
+
/** 心跳 / 任意客户端活动:续期在线 */
|
|
77
|
+
touchClient(sessionId: string, clientInstanceId: string, now?: number): boolean;
|
|
78
|
+
/** 会话内仍在线且声明的 tool 名 */
|
|
79
|
+
sessionToolNames(sessionId: string, now?: number): ClientToolName[];
|
|
80
|
+
/** 会话内在线 provider */
|
|
81
|
+
listProviders(sessionId: string, now?: number): ClientToolProvider[];
|
|
82
|
+
/** 会话内是否有 live provider(tool 面过滤用) */
|
|
83
|
+
hasLiveProvider(sessionId: string, name: ClientToolName, now?: number): boolean;
|
|
84
|
+
/** 全局是否仍有任一 live provider(决定能否 uninstall) */
|
|
85
|
+
hasAnyProvider(name: ClientToolName, now?: number): boolean;
|
|
86
|
+
getDescriptor(sessionId: string, name: ClientToolName, now?: number): ClientToolDescriptor | undefined;
|
|
87
|
+
private matchesFilter;
|
|
88
|
+
/** 会话内某 tool 的 live 候选(已过 clientFilter) */
|
|
89
|
+
listCandidates(sessionId: string, name: ClientToolName, now?: number): Array<{
|
|
90
|
+
provider: ClientToolProvider;
|
|
91
|
+
descriptor: ClientToolDescriptor;
|
|
92
|
+
}>;
|
|
93
|
+
/**
|
|
94
|
+
* 解析执行端。仅 live provider;clientFilter → sticky → 最近活跃。
|
|
95
|
+
* 敏感 device:候选必须唯一,否则拒绝。
|
|
96
|
+
*/
|
|
97
|
+
resolveTarget(sessionId: string, name: ClientToolName, now?: number): ClientToolRouteDecision;
|
|
98
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClientToolRegistry — 会话内客户端能力注册表
|
|
3
|
+
*
|
|
4
|
+
* - 多个 client 可注册同名 tool;默认路由「最近活跃」,成功后 session sticky
|
|
5
|
+
* - `clientFilter` 可钉 platform / instance
|
|
6
|
+
* - 敏感 device:候选必须唯一(或 filter 钉端),否则拒绝
|
|
7
|
+
* - 某名字最后一家下线时,由调用方 unregister 对应 RegisteredTool
|
|
8
|
+
*/
|
|
9
|
+
export class ClientToolRegistry {
|
|
10
|
+
/** sessionId → clientInstanceId → provider */
|
|
11
|
+
bySession = new Map();
|
|
12
|
+
/** session sticky:sessionId|name → 最近成功端 */
|
|
13
|
+
preferred = new Map();
|
|
14
|
+
/** 在线性:超过该时长未心跳的 provider 视为离线(不进 tool 面、不可路由) */
|
|
15
|
+
liveTtlMs;
|
|
16
|
+
/** 可注入时钟(测试用);默认 Date.now */
|
|
17
|
+
clock;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.liveTtlMs = options?.liveTtlMs ?? 90_000;
|
|
20
|
+
this.clock = options?.clock ?? Date.now;
|
|
21
|
+
}
|
|
22
|
+
at(now) {
|
|
23
|
+
return now ?? this.clock();
|
|
24
|
+
}
|
|
25
|
+
isLive(p, now) {
|
|
26
|
+
return now - p.lastActiveAt <= this.liveTtlMs;
|
|
27
|
+
}
|
|
28
|
+
stickyKey(sessionId, name) {
|
|
29
|
+
return `${sessionId}|${name}`;
|
|
30
|
+
}
|
|
31
|
+
/** 某 tool 在 session 内成功后钉偏好端(在线期间优先) */
|
|
32
|
+
notePreferred(sessionId, name, clientInstanceId) {
|
|
33
|
+
this.preferred.set(this.stickyKey(sessionId, name), clientInstanceId);
|
|
34
|
+
}
|
|
35
|
+
/** 清理失效 sticky(provider 不再 live 或已不声明该 tool) */
|
|
36
|
+
dropStalePreferred() {
|
|
37
|
+
for (const [key, clientInstanceId] of [...this.preferred.entries()]) {
|
|
38
|
+
const sep = key.indexOf('|');
|
|
39
|
+
const sessionId = key.slice(0, sep);
|
|
40
|
+
const name = key.slice(sep + 1);
|
|
41
|
+
const p = this.bySession.get(sessionId)?.get(clientInstanceId);
|
|
42
|
+
const live = p !== undefined &&
|
|
43
|
+
this.isLive(p, this.clock()) &&
|
|
44
|
+
p.descriptors.some((d) => d.name === name);
|
|
45
|
+
if (!live)
|
|
46
|
+
this.preferred.delete(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 剔除超时未心跳的 provider(在线性打底)
|
|
51
|
+
*
|
|
52
|
+
* @returns 各 session 上消失的 tool 名(可能重复)
|
|
53
|
+
*/
|
|
54
|
+
expireStaleProviders(now) {
|
|
55
|
+
const ts = this.at(now);
|
|
56
|
+
const out = [];
|
|
57
|
+
for (const [sessionId, sessionMap] of [...this.bySession.entries()]) {
|
|
58
|
+
const before = this.collectNames(sessionMap);
|
|
59
|
+
for (const [clientId, p] of [...sessionMap.entries()]) {
|
|
60
|
+
if (!this.isLive(p, ts))
|
|
61
|
+
sessionMap.delete(clientId);
|
|
62
|
+
}
|
|
63
|
+
// 不删除空 sessionMap:注册过程中 expire 可能清空后 set,删 Map 会丢掉本次写入
|
|
64
|
+
const after = this.collectNames(sessionMap);
|
|
65
|
+
const removedNames = before.filter((n) => !after.includes(n));
|
|
66
|
+
if (removedNames.length > 0)
|
|
67
|
+
out.push({ sessionId, removedNames });
|
|
68
|
+
}
|
|
69
|
+
this.dropStalePreferred();
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
collectNames(sessionMap) {
|
|
73
|
+
const names = new Set();
|
|
74
|
+
for (const p of sessionMap.values()) {
|
|
75
|
+
for (const d of p.descriptors)
|
|
76
|
+
names.add(d.name);
|
|
77
|
+
}
|
|
78
|
+
return Array.from(names);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* 注册/更新某客户端在某会话上的能力(能力快照)
|
|
82
|
+
*
|
|
83
|
+
* @returns 本次新出现的 tool 名(调用方据此 register 进 ToolBus)
|
|
84
|
+
*/
|
|
85
|
+
registerClientProviders(input) {
|
|
86
|
+
const now = this.at(input.now);
|
|
87
|
+
this.expireStaleProviders(now);
|
|
88
|
+
const sessionMap = this.bySession.get(input.sessionId) ?? new Map();
|
|
89
|
+
this.bySession.set(input.sessionId, sessionMap);
|
|
90
|
+
const before = this.sessionToolNames(input.sessionId, now);
|
|
91
|
+
sessionMap.set(input.clientInstanceId, {
|
|
92
|
+
clientInstanceId: input.clientInstanceId,
|
|
93
|
+
sessionId: input.sessionId,
|
|
94
|
+
platform: input.platform,
|
|
95
|
+
principalId: input.principalId,
|
|
96
|
+
lastActiveAt: now,
|
|
97
|
+
descriptors: input.descriptors,
|
|
98
|
+
});
|
|
99
|
+
const after = this.sessionToolNames(input.sessionId, now);
|
|
100
|
+
const addedNames = after.filter((n) => !before.includes(n));
|
|
101
|
+
return { addedNames, allNames: after };
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 客户端下线/撤回能力
|
|
105
|
+
*
|
|
106
|
+
* @returns 会话中已无任何 provider 的 tool 名(调用方 unregister)
|
|
107
|
+
*/
|
|
108
|
+
unregisterClientProviders(input) {
|
|
109
|
+
const sessionMap = this.bySession.get(input.sessionId);
|
|
110
|
+
if (!sessionMap)
|
|
111
|
+
return { removedNames: [] };
|
|
112
|
+
const before = this.sessionToolNames(input.sessionId);
|
|
113
|
+
sessionMap.delete(input.clientInstanceId);
|
|
114
|
+
const after = this.sessionToolNames(input.sessionId);
|
|
115
|
+
this.dropStalePreferred();
|
|
116
|
+
return { removedNames: before.filter((n) => !after.includes(n)) };
|
|
117
|
+
}
|
|
118
|
+
/** 心跳 / 任意客户端活动:续期在线 */
|
|
119
|
+
touchClient(sessionId, clientInstanceId, now) {
|
|
120
|
+
const p = this.bySession.get(sessionId)?.get(clientInstanceId);
|
|
121
|
+
if (!p)
|
|
122
|
+
return false;
|
|
123
|
+
p.lastActiveAt = this.at(now);
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
/** 会话内仍在线且声明的 tool 名 */
|
|
127
|
+
sessionToolNames(sessionId, now) {
|
|
128
|
+
const ts = this.at(now);
|
|
129
|
+
this.expireStaleProviders(ts);
|
|
130
|
+
const names = new Set();
|
|
131
|
+
for (const p of this.bySession.get(sessionId)?.values() ?? []) {
|
|
132
|
+
if (!this.isLive(p, ts))
|
|
133
|
+
continue;
|
|
134
|
+
for (const d of p.descriptors)
|
|
135
|
+
names.add(d.name);
|
|
136
|
+
}
|
|
137
|
+
return Array.from(names).sort();
|
|
138
|
+
}
|
|
139
|
+
/** 会话内在线 provider */
|
|
140
|
+
listProviders(sessionId, now) {
|
|
141
|
+
const ts = this.at(now);
|
|
142
|
+
this.expireStaleProviders(ts);
|
|
143
|
+
return Array.from(this.bySession.get(sessionId)?.values() ?? []).filter((p) => this.isLive(p, ts));
|
|
144
|
+
}
|
|
145
|
+
/** 会话内是否有 live provider(tool 面过滤用) */
|
|
146
|
+
hasLiveProvider(sessionId, name, now) {
|
|
147
|
+
const ts = this.at(now);
|
|
148
|
+
this.expireStaleProviders(ts);
|
|
149
|
+
for (const p of this.bySession.get(sessionId)?.values() ?? []) {
|
|
150
|
+
if (!this.isLive(p, ts))
|
|
151
|
+
continue;
|
|
152
|
+
if (p.descriptors.some((d) => d.name === name))
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
/** 全局是否仍有任一 live provider(决定能否 uninstall) */
|
|
158
|
+
hasAnyProvider(name, now) {
|
|
159
|
+
const ts = this.at(now);
|
|
160
|
+
this.expireStaleProviders(ts);
|
|
161
|
+
for (const sessionMap of this.bySession.values()) {
|
|
162
|
+
for (const p of sessionMap.values()) {
|
|
163
|
+
if (!this.isLive(p, ts))
|
|
164
|
+
continue;
|
|
165
|
+
if (p.descriptors.some((d) => d.name === name))
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
getDescriptor(sessionId, name, now) {
|
|
172
|
+
for (const p of this.listProviders(sessionId, now)) {
|
|
173
|
+
const d = p.descriptors.find((x) => x.name === name);
|
|
174
|
+
if (d)
|
|
175
|
+
return d;
|
|
176
|
+
}
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
matchesFilter(provider, descriptor) {
|
|
180
|
+
const filter = descriptor.clientFilter;
|
|
181
|
+
if (!filter)
|
|
182
|
+
return true;
|
|
183
|
+
if (filter.instanceId && filter.instanceId !== provider.clientInstanceId)
|
|
184
|
+
return false;
|
|
185
|
+
if (filter.platforms && filter.platforms.length > 0) {
|
|
186
|
+
const platform = provider.platform;
|
|
187
|
+
if (!platform || !filter.platforms.includes(platform))
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
/** 会话内某 tool 的 live 候选(已过 clientFilter) */
|
|
193
|
+
listCandidates(sessionId, name, now) {
|
|
194
|
+
const ts = this.at(now);
|
|
195
|
+
const candidates = [];
|
|
196
|
+
for (const p of this.listProviders(sessionId, ts)) {
|
|
197
|
+
const d = p.descriptors.find((x) => x.name === name);
|
|
198
|
+
if (d && this.matchesFilter(p, d))
|
|
199
|
+
candidates.push({ provider: p, descriptor: d });
|
|
200
|
+
}
|
|
201
|
+
return candidates;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* 解析执行端。仅 live provider;clientFilter → sticky → 最近活跃。
|
|
205
|
+
* 敏感 device:候选必须唯一,否则拒绝。
|
|
206
|
+
*/
|
|
207
|
+
resolveTarget(sessionId, name, now) {
|
|
208
|
+
const ts = this.at(now);
|
|
209
|
+
this.dropStalePreferred();
|
|
210
|
+
const candidates = this.listCandidates(sessionId, name, ts);
|
|
211
|
+
if (candidates.length === 0) {
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
reason: 'unsupported',
|
|
215
|
+
hint: `no live client in session provides tool "${name}"`,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const sensitivity = candidates.some((c) => c.descriptor.device?.sensitivity === 'sensitive')
|
|
219
|
+
? 'sensitive'
|
|
220
|
+
: candidates.some((c) => c.descriptor.device?.sensitivity === 'personal')
|
|
221
|
+
? 'personal'
|
|
222
|
+
: 'public';
|
|
223
|
+
if (sensitivity === 'sensitive' && candidates.length > 1) {
|
|
224
|
+
return {
|
|
225
|
+
ok: false,
|
|
226
|
+
reason: 'ambiguous',
|
|
227
|
+
hint: `tool "${name}" is sensitivity=sensitive and ${candidates.length} clients provide it; ` +
|
|
228
|
+
'pin clientFilter.instanceId or disconnect extras',
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
const stickyId = this.preferred.get(this.stickyKey(sessionId, name));
|
|
232
|
+
if (stickyId) {
|
|
233
|
+
const sticky = candidates.find((c) => c.provider.clientInstanceId === stickyId);
|
|
234
|
+
if (sticky) {
|
|
235
|
+
return {
|
|
236
|
+
ok: true,
|
|
237
|
+
clientInstanceId: sticky.provider.clientInstanceId,
|
|
238
|
+
descriptor: sticky.descriptor,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
candidates.sort((a, b) => b.provider.lastActiveAt - a.provider.lastActiveAt);
|
|
243
|
+
const winner = candidates[0];
|
|
244
|
+
return {
|
|
245
|
+
ok: true,
|
|
246
|
+
clientInstanceId: winner.provider.clientInstanceId,
|
|
247
|
+
descriptor: winner.descriptor,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ClientStreamTransport — Host 侧 source/sink 采样通道
|
|
3
|
+
*
|
|
4
|
+
* LLM 不可见:不注册 stream_read/write/close。域工具封装生命周期,
|
|
5
|
+
* 样本经事件回调给 UI/插件;大字节仍走 asset。
|
|
6
|
+
*/
|
|
7
|
+
import type { RegisteredTool, ToolExecutionContext } from '@octopi-agent/core/types/tools.js';
|
|
8
|
+
import type { ClientToolCallId, ClientToolDescriptor, ClientToolName } from './types.js';
|
|
9
|
+
export type ClientStreamDirection = 'source' | 'sink';
|
|
10
|
+
export interface ClientStreamChannel {
|
|
11
|
+
streamId: string;
|
|
12
|
+
/** source: client→host 采集;sink: host→client 播放/呈现 */
|
|
13
|
+
direction: ClientStreamDirection;
|
|
14
|
+
/** 样本提示:'audio/16k' | 'geo/1hz' | … */
|
|
15
|
+
sampleHint?: string;
|
|
16
|
+
owner: {
|
|
17
|
+
sessionId: string;
|
|
18
|
+
clientInstanceId: string;
|
|
19
|
+
toolCallId?: ClientToolCallId;
|
|
20
|
+
toolName?: ClientToolName;
|
|
21
|
+
};
|
|
22
|
+
status: 'open' | 'closed';
|
|
23
|
+
openedAt: number;
|
|
24
|
+
closedAt?: number;
|
|
25
|
+
maxDurationMs?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface ClientStreamSampleInput {
|
|
28
|
+
data: unknown;
|
|
29
|
+
}
|
|
30
|
+
export type ClientStreamEventType = 'client_stream.opened' | 'client_stream.sample' | 'client_stream.closed';
|
|
31
|
+
export interface ClientStreamEvent {
|
|
32
|
+
type: ClientStreamEventType;
|
|
33
|
+
sessionId: string;
|
|
34
|
+
timestamp: number;
|
|
35
|
+
data: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
export interface ClientStreamTransportDeps {
|
|
38
|
+
emitEvent: (event: ClientStreamEvent) => void;
|
|
39
|
+
/** UI 样本最小间隔(ms);同窗多条合并为批 */
|
|
40
|
+
throttleMs?: number;
|
|
41
|
+
/** 每批最大样本数 */
|
|
42
|
+
maxBatch?: number;
|
|
43
|
+
/** 默认通道 TTL */
|
|
44
|
+
defaultMaxDurationMs?: number;
|
|
45
|
+
makeStreamId?: () => string;
|
|
46
|
+
now?: () => number;
|
|
47
|
+
}
|
|
48
|
+
export interface OpenStreamInput {
|
|
49
|
+
direction: ClientStreamDirection;
|
|
50
|
+
sessionId: string;
|
|
51
|
+
clientInstanceId: string;
|
|
52
|
+
toolCallId?: ClientToolCallId;
|
|
53
|
+
toolName?: ClientToolName;
|
|
54
|
+
sampleHint?: string;
|
|
55
|
+
maxDurationMs?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 会话流通道注册表 + 节流广播。
|
|
59
|
+
* 通道操作仅 Host/插件调用,不进入 LLM tool 面。
|
|
60
|
+
*/
|
|
61
|
+
export declare class ClientStreamTransport {
|
|
62
|
+
private readonly deps;
|
|
63
|
+
private readonly channels;
|
|
64
|
+
private readonly throttleMs;
|
|
65
|
+
private readonly maxBatch;
|
|
66
|
+
private readonly defaultMaxDurationMs;
|
|
67
|
+
private readonly makeStreamId;
|
|
68
|
+
private readonly now;
|
|
69
|
+
constructor(deps: ClientStreamTransportDeps);
|
|
70
|
+
open(input: OpenStreamInput): ClientStreamChannel;
|
|
71
|
+
/**
|
|
72
|
+
* 写入样本(source:client 上报;sink:host 下发前镜像给 UI)。
|
|
73
|
+
* 超过 maxBatch 的可丢样本在本窗丢弃。
|
|
74
|
+
*/
|
|
75
|
+
writeSamples(streamId: string, samples: ClientStreamSampleInput[]): boolean;
|
|
76
|
+
private scheduleFlush;
|
|
77
|
+
private flush;
|
|
78
|
+
close(streamId: string, reason?: string): ClientStreamChannel | null;
|
|
79
|
+
/** Run abort / session 结束 / client 下线时联动关闭 */
|
|
80
|
+
closeWhere(match: (channel: ClientStreamChannel) => boolean, reason?: string): number;
|
|
81
|
+
get(streamId: string): ClientStreamChannel | undefined;
|
|
82
|
+
list(sessionId?: string): ClientStreamChannel[];
|
|
83
|
+
/** 同 session 内某域工具仍打开的通道(复用,避免每次 invoke 新开) */
|
|
84
|
+
findOpenByTool(sessionId: string, toolName: string, clientInstanceId?: string): ClientStreamChannel | undefined;
|
|
85
|
+
}
|
|
86
|
+
export interface StreamDomainToolDeps {
|
|
87
|
+
transport: ClientStreamTransport;
|
|
88
|
+
/** 一次调用创建一条通道(可带业务参数与目标端) */
|
|
89
|
+
openChannel: (input: {
|
|
90
|
+
args: Record<string, unknown>;
|
|
91
|
+
context: ToolExecutionContext;
|
|
92
|
+
}) => Omit<OpenStreamInput, 'direction' | 'toolName'>;
|
|
93
|
+
/** 结果 value 句柄字段名:watchId / playId … */
|
|
94
|
+
handleKey?: string;
|
|
95
|
+
/** 通道打开后立即写入(如 sink 域工具的首批样本) */
|
|
96
|
+
onOpen?: (channel: ClientStreamChannel, args: Record<string, unknown>, transport: ClientStreamTransport) => void;
|
|
97
|
+
/**
|
|
98
|
+
* 复用策略(sink 续写 / source 去重):
|
|
99
|
+
* - `none`:每次新开(默认 source)
|
|
100
|
+
* - `append`:命中 playId 或同名 open 通道则续写样本,不新开
|
|
101
|
+
* - `sticky`:同名 open 通道则直接返回句柄,不新开
|
|
102
|
+
*/
|
|
103
|
+
reuse?: 'none' | 'append' | 'sticky';
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* source 域工具样板(location_watch 类):返回 value 域句柄,样本走 Host 通道。
|
|
107
|
+
* 不是通用 stream_read;生命周期由配对 stop 工具或 TTL 收口。
|
|
108
|
+
* 默认 sticky:同 session 已有 open 通道则复用,避免重复开流。
|
|
109
|
+
*/
|
|
110
|
+
export declare function createSourceStreamTool(descriptor: ClientToolDescriptor, deps: StreamDomainToolDeps): RegisteredTool;
|
|
111
|
+
/**
|
|
112
|
+
* sink 域工具样板(play 类):打开 host→client 下行通道,返回 value 域句柄。
|
|
113
|
+
* 默认 append:同 session / playId 续写样本,持续下发不新开通道。
|
|
114
|
+
*/
|
|
115
|
+
export declare function createSinkStreamTool(descriptor: ClientToolDescriptor, deps: StreamDomainToolDeps): RegisteredTool;
|
|
116
|
+
export interface StreamStopToolDeps {
|
|
117
|
+
transport: ClientStreamTransport;
|
|
118
|
+
/** 从 args 取句柄字段,默认 stop 用 watchId/playId */
|
|
119
|
+
handleKey?: string;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 域工具 stop 样板:按域句柄关通道。不是通用 stream_close。
|
|
123
|
+
*/
|
|
124
|
+
export declare function createStreamStopTool(descriptor: ClientToolDescriptor, deps: StreamStopToolDeps): RegisteredTool;
|