@octopi-agent/engine 0.58.0 → 0.65.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/dist/config-schema/context.d.ts +1 -1
- package/dist/config-schema/context.js +1 -1
- package/dist/config-schema/engine.d.ts +38 -1
- package/dist/config-schema/substrate.d.ts +37 -0
- package/dist/config-schema/substrate.js +47 -0
- package/dist/config.d.ts +30 -1
- package/dist/harness/agent/builder.d.ts +14 -0
- package/dist/harness/agent/builder.js +14 -2
- package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.js +13 -3
- package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.js +13 -4
- package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.js +25 -5
- package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/errors.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/errors.js +1 -1
- package/dist/harness/capabilities/document/factory.d.ts +33 -0
- package/dist/harness/capabilities/document/factory.js +37 -0
- package/dist/harness/{context/capabilities → capabilities}/document/format.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/format.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.js +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/index.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/index.js +2 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.js +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.d.ts +3 -1
- package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.js +44 -1
- package/dist/harness/{context/capabilities → capabilities}/document/port.d.ts +1 -1
- package/dist/harness/{context/capabilities → capabilities}/document/port.js +4 -4
- package/dist/harness/{context/capabilities → capabilities}/document/types.d.ts +11 -1
- package/dist/harness/{context/capabilities → capabilities}/document/types.js +1 -1
- package/dist/harness/capabilities/document/worker-port.d.ts +13 -0
- package/dist/harness/capabilities/document/worker-port.js +78 -0
- package/dist/harness/context/capabilities/index.d.ts +2 -2
- package/dist/harness/context/capabilities/index.js +2 -2
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.d.ts +10 -0
- package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +102 -5
- package/dist/harness/index.d.ts +4 -2
- package/dist/harness/index.js +2 -1
- package/dist/harness/knowledge/adapters.d.ts +8 -5
- package/dist/harness/knowledge/adapters.js +53 -13
- package/dist/harness/knowledge/client.d.ts +197 -0
- package/dist/harness/knowledge/client.js +237 -0
- package/dist/harness/knowledge/connector-fetcher.d.ts +2 -2
- package/dist/harness/knowledge/connectors.d.ts +3 -3
- package/dist/harness/knowledge/connectors.js +14 -8
- package/dist/harness/knowledge/db.d.ts +40 -5
- package/dist/harness/knowledge/db.js +441 -113
- package/dist/harness/knowledge/document-extract-worker.js +55 -12
- package/dist/harness/knowledge/embed-runner.d.ts +53 -0
- package/dist/harness/knowledge/embed-runner.js +171 -0
- package/dist/harness/knowledge/engine-thread.d.ts +1 -0
- package/dist/harness/knowledge/engine-thread.js +302 -0
- package/dist/harness/knowledge/extract-document.d.ts +21 -5
- package/dist/harness/knowledge/extract-document.js +32 -8
- package/dist/harness/knowledge/fetchers.d.ts +14 -3
- package/dist/harness/knowledge/fetchers.js +52 -16
- package/dist/harness/knowledge/file-identity.d.ts +53 -0
- package/dist/harness/knowledge/file-identity.js +172 -0
- package/dist/harness/knowledge/file-index-store.d.ts +50 -0
- package/dist/harness/knowledge/file-index-store.js +132 -0
- package/dist/harness/knowledge/file-limits.d.ts +86 -0
- package/dist/harness/knowledge/file-limits.js +175 -0
- package/dist/harness/knowledge/fts.d.ts +60 -0
- package/dist/harness/knowledge/fts.js +218 -0
- package/dist/harness/knowledge/grounding.d.ts +3 -2
- package/dist/harness/knowledge/http-app.d.ts +50 -0
- package/dist/harness/knowledge/http-app.js +652 -0
- package/dist/harness/knowledge/http-bridge.d.ts +49 -0
- package/dist/harness/knowledge/http-bridge.js +17 -0
- package/dist/harness/knowledge/index-store.d.ts +158 -93
- package/dist/harness/knowledge/index-store.js +982 -441
- package/dist/harness/knowledge/index.d.ts +45 -5
- package/dist/harness/knowledge/index.js +23 -2
- package/dist/harness/knowledge/ingest.d.ts +262 -87
- package/dist/harness/knowledge/ingest.js +1072 -480
- package/dist/harness/knowledge/job-control-state.d.ts +29 -0
- package/dist/harness/knowledge/job-control-state.js +44 -0
- package/dist/harness/knowledge/job-control.d.ts +35 -0
- package/dist/harness/knowledge/job-control.js +86 -0
- package/dist/harness/knowledge/job-queue.d.ts +62 -0
- package/dist/harness/knowledge/job-queue.js +173 -0
- package/dist/harness/knowledge/knowledge-serve-child.d.ts +1 -0
- package/dist/harness/knowledge/knowledge-serve-child.js +100 -0
- package/dist/harness/knowledge/local-stack.d.ts +27 -0
- package/dist/harness/knowledge/local-stack.js +63 -0
- package/dist/harness/knowledge/membership-store.d.ts +65 -0
- package/dist/harness/knowledge/membership-store.js +198 -0
- package/dist/harness/knowledge/network-guard.d.ts +11 -2
- package/dist/harness/knowledge/network-guard.js +130 -85
- package/dist/harness/knowledge/parse-text-in-worker.d.ts +32 -0
- package/dist/harness/knowledge/parse-text-in-worker.js +158 -0
- package/dist/harness/knowledge/query-service.d.ts +272 -0
- package/dist/harness/knowledge/query-service.js +326 -0
- package/dist/harness/knowledge/query-worker-client.d.ts +96 -0
- package/dist/harness/knowledge/query-worker-client.js +207 -0
- package/dist/harness/knowledge/query-worker.d.ts +1 -0
- package/dist/harness/knowledge/query-worker.js +186 -0
- package/dist/harness/knowledge/read-http.d.ts +35 -0
- package/dist/harness/knowledge/read-http.js +373 -0
- package/dist/harness/knowledge/retriever.d.ts +22 -5
- package/dist/harness/knowledge/retriever.js +66 -35
- package/dist/harness/knowledge/secret-scan.d.ts +13 -2
- package/dist/harness/knowledge/secret-scan.js +25 -2
- package/dist/harness/knowledge/serve.d.ts +48 -0
- package/dist/harness/knowledge/serve.js +336 -0
- package/dist/harness/knowledge/source-store.d.ts +80 -17
- package/dist/harness/knowledge/source-store.js +160 -89
- package/dist/harness/knowledge/start-service-process.d.ts +49 -0
- package/dist/harness/knowledge/start-service-process.js +158 -0
- package/dist/harness/knowledge/text-parse-worker.d.ts +1 -0
- package/dist/harness/knowledge/text-parse-worker.js +54 -0
- package/dist/harness/knowledge/types.d.ts +1 -1
- package/dist/harness/knowledge/types.js +1 -1
- package/dist/harness/knowledge/vector-ann.d.ts +42 -0
- package/dist/harness/knowledge/vector-ann.js +68 -0
- package/dist/harness/knowledge/writer-lock.d.ts +13 -0
- package/dist/harness/knowledge/writer-lock.js +71 -0
- package/dist/harness/knowledge/writer-service.d.ts +194 -0
- package/dist/harness/knowledge/writer-service.js +355 -0
- package/dist/harness/knowledge/writer-worker-client.d.ts +102 -0
- package/dist/harness/knowledge/writer-worker-client.js +381 -0
- package/dist/harness/knowledge/writer-worker.d.ts +1 -0
- package/dist/harness/knowledge/writer-worker.js +189 -0
- package/dist/harness/memory/sqlite/embedding.d.ts +5 -2
- package/dist/harness/memory/sqlite/embedding.js +109 -16
- package/dist/harness/memory/sqlite/keyword-search.d.ts +21 -0
- package/dist/harness/memory/sqlite/keyword-search.js +64 -0
- package/dist/harness/session/attachments/service.d.ts +1 -1
- package/dist/harness/session/attachments/service.js +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +2 -1
- package/dist/worker-path.d.ts +8 -0
- package/dist/worker-path.js +15 -0
- package/package.json +2 -2
|
@@ -38,51 +38,108 @@ export declare class KnowledgeSourceStore {
|
|
|
38
38
|
/**
|
|
39
39
|
* 将 agent 挂到 Project(显式)
|
|
40
40
|
*/
|
|
41
|
-
assignProject(projectKey: string, agentId: string
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
assignProject(projectKey: string, agentId: string, opts?: {
|
|
42
|
+
tenantId?: string;
|
|
43
|
+
gatewayId?: string;
|
|
44
|
+
}): void;
|
|
45
|
+
unassignProject(projectKey: string, agentId: string, opts?: {
|
|
46
|
+
tenantId?: string;
|
|
47
|
+
gatewayId?: string;
|
|
48
|
+
}): void;
|
|
49
|
+
listProjectAgents(projectKey: string, opts?: {
|
|
50
|
+
tenantId?: string;
|
|
51
|
+
gatewayId?: string;
|
|
52
|
+
}): string[];
|
|
44
53
|
/**
|
|
45
54
|
* 屏蔽 Global 源对某 agent 的可见(仅 global 源有意义)
|
|
46
55
|
*/
|
|
47
|
-
hideSource(agentId: string, sourceId: KnowledgeSourceId | string
|
|
48
|
-
|
|
49
|
-
|
|
56
|
+
hideSource(agentId: string, sourceId: KnowledgeSourceId | string, opts?: {
|
|
57
|
+
tenantId?: string;
|
|
58
|
+
gatewayId?: string;
|
|
59
|
+
}): void;
|
|
60
|
+
unhideSource(agentId: string, sourceId: KnowledgeSourceId | string, opts?: {
|
|
61
|
+
tenantId?: string;
|
|
62
|
+
gatewayId?: string;
|
|
63
|
+
}): void;
|
|
64
|
+
listHidden(agentId: string, opts?: {
|
|
65
|
+
tenantId?: string;
|
|
66
|
+
gatewayId?: string;
|
|
67
|
+
}): string[];
|
|
68
|
+
/** 哪些 agent 对该 global 源做了隐藏 */
|
|
69
|
+
listAgentsHidingSource(sourceId: string, opts?: {
|
|
70
|
+
tenantId?: string;
|
|
71
|
+
gatewayId?: string;
|
|
72
|
+
}): string[];
|
|
50
73
|
/**
|
|
51
74
|
* 会话 overlay:单条 upsert(资产归属不变,只改本场视图)
|
|
52
75
|
*/
|
|
53
|
-
setSessionVisibility(sessionId: string, item: import('./types.js').KnowledgeSessionVisibilityInput
|
|
76
|
+
setSessionVisibility(sessionId: string, item: import('./types.js').KnowledgeSessionVisibilityInput, identity?: {
|
|
77
|
+
tenantId?: string;
|
|
78
|
+
gatewayId?: string;
|
|
79
|
+
}): void;
|
|
54
80
|
/**
|
|
55
|
-
* 会话 overlay
|
|
81
|
+
* 会话 overlay:全量替换(按 tenant/gateway 隔离,禁清他方行)
|
|
56
82
|
*/
|
|
57
|
-
replaceSessionVisibility(sessionId: string, items: readonly import('./types.js').KnowledgeSessionVisibilityInput[]
|
|
83
|
+
replaceSessionVisibility(sessionId: string, items: readonly import('./types.js').KnowledgeSessionVisibilityInput[], identity?: {
|
|
84
|
+
tenantId?: string;
|
|
85
|
+
gatewayId?: string;
|
|
86
|
+
}): void;
|
|
58
87
|
clearSessionVisibility(sessionId: string, target?: {
|
|
59
88
|
targetType: import('./types.js').KnowledgeVisibilityTargetType;
|
|
60
89
|
targetId: string;
|
|
90
|
+
}, identity?: {
|
|
91
|
+
tenantId?: string;
|
|
92
|
+
gatewayId?: string;
|
|
61
93
|
}): void;
|
|
62
|
-
listSessionVisibility(sessionId: string
|
|
94
|
+
listSessionVisibility(sessionId: string, identity?: {
|
|
95
|
+
tenantId?: string;
|
|
96
|
+
gatewayId?: string;
|
|
97
|
+
}): import('./types.js').KnowledgeSessionVisibilityItem[];
|
|
63
98
|
/** 项目登记视图(管理面:先建项目再挂源;**不**用孤儿源计数复活已删项目) */
|
|
64
|
-
listProjects(
|
|
99
|
+
listProjects(opts?: {
|
|
100
|
+
tenantId?: string;
|
|
101
|
+
gatewayId?: string;
|
|
102
|
+
}): Array<{
|
|
65
103
|
projectKey: string;
|
|
66
104
|
displayName?: string;
|
|
67
105
|
sourceCount: number;
|
|
68
106
|
assignedAgentIds: string[];
|
|
69
107
|
}>;
|
|
70
108
|
/** 登记空项目(先建项目再挂源) */
|
|
71
|
-
createProject(projectKey: string, displayName?: string
|
|
109
|
+
createProject(projectKey: string, displayName?: string, opts?: {
|
|
110
|
+
tenantId?: string;
|
|
111
|
+
registeredBy?: string;
|
|
112
|
+
}): void;
|
|
72
113
|
/**
|
|
73
114
|
* 删除项目登记。非空项目拒绝(先卸载/删除项目下源),避免孤儿源与静默收养。
|
|
74
115
|
*/
|
|
75
|
-
removeProject(projectKey: string
|
|
116
|
+
removeProject(projectKey: string, opts?: {
|
|
117
|
+
tenantId?: string;
|
|
118
|
+
}): boolean;
|
|
76
119
|
/**
|
|
77
|
-
* 源是否对 (agent, session) 可见(
|
|
120
|
+
* 源是否对 (agent, session) 可见(gateway 漏斗 ⊕ base ⊕ session overlay)
|
|
78
121
|
*/
|
|
79
|
-
isVisible(source: KnowledgeSource, agentId: string, sessionId?: string
|
|
122
|
+
isVisible(source: KnowledgeSource, agentId: string, sessionId?: string, identity?: {
|
|
123
|
+
tenantId?: string;
|
|
124
|
+
gatewayId?: string;
|
|
125
|
+
}): boolean;
|
|
126
|
+
/** 网关漏斗:registered_by=己方 ∪ public */
|
|
127
|
+
isGatewayVisible(source: KnowledgeSource, identity?: {
|
|
128
|
+
tenantId?: string;
|
|
129
|
+
gatewayId?: string;
|
|
130
|
+
}): boolean;
|
|
80
131
|
/** Agent 级 base 可见(不含会话 overlay;排查用) */
|
|
81
|
-
isBaseVisible(source: KnowledgeSource, agentId: string, sessionId?: string
|
|
132
|
+
isBaseVisible(source: KnowledgeSource, agentId: string, sessionId?: string, identity?: {
|
|
133
|
+
tenantId?: string;
|
|
134
|
+
gatewayId?: string;
|
|
135
|
+
}): boolean;
|
|
82
136
|
private applySessionOverlay;
|
|
83
137
|
private sessionOp;
|
|
84
138
|
private isHidden;
|
|
85
139
|
private isProjectAssigned;
|
|
140
|
+
/** catalog 规模/topics 派生缓存(Tier 0 每轮装配;短 TTL 足够) */
|
|
141
|
+
private catalogExtrasCache;
|
|
142
|
+
private static readonly CATALOG_EXTRAS_TTL_MS;
|
|
86
143
|
/**
|
|
87
144
|
* 生成 agent 可见 catalog(Tier 0)
|
|
88
145
|
* 默认返回可见全集(display 截断在 KnowledgeLayer);maxEntries 仅作安全硬顶
|
|
@@ -91,9 +148,15 @@ export declare class KnowledgeSourceStore {
|
|
|
91
148
|
sessionId?: string;
|
|
92
149
|
maxEntries?: number;
|
|
93
150
|
}): KnowledgeCatalogItem[];
|
|
151
|
+
/** 规模 + topics(短 TTL 缓存;禁止每轮全表拉 path) */
|
|
152
|
+
private catalogExtras;
|
|
94
153
|
/** 每源已入库文件数(catalog 规模粗标用) */
|
|
95
154
|
private fileCountsBySource;
|
|
96
|
-
/**
|
|
155
|
+
/**
|
|
156
|
+
* 每源路径派生 topics(catalog 搜索线索)
|
|
157
|
+
*
|
|
158
|
+
* 只抽样前 N 条 path(按字典序),避免大库全表扫;topics 是粗线索不是精确索引。
|
|
159
|
+
*/
|
|
97
160
|
private topicsBySource;
|
|
98
161
|
/**
|
|
99
162
|
* catalog 粗桶指纹(防每轮重渲)
|
|
@@ -260,90 +260,107 @@ export class KnowledgeSourceStore {
|
|
|
260
260
|
/**
|
|
261
261
|
* 将 agent 挂到 Project(显式)
|
|
262
262
|
*/
|
|
263
|
-
assignProject(projectKey, agentId) {
|
|
263
|
+
assignProject(projectKey, agentId, opts) {
|
|
264
264
|
this.db.raw
|
|
265
|
-
.prepare(`INSERT OR IGNORE INTO knowledge_project_agents
|
|
266
|
-
|
|
267
|
-
|
|
265
|
+
.prepare(`INSERT OR IGNORE INTO knowledge_project_agents
|
|
266
|
+
(tenant_id, gateway_id, local_agent_id, project_key, created_at)
|
|
267
|
+
VALUES (?, ?, ?, ?, ?)`)
|
|
268
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, projectKey, Date.now());
|
|
268
269
|
}
|
|
269
|
-
unassignProject(projectKey, agentId) {
|
|
270
|
+
unassignProject(projectKey, agentId, opts) {
|
|
270
271
|
this.db.raw
|
|
271
|
-
.prepare('DELETE FROM knowledge_project_agents WHERE project_key = ? AND
|
|
272
|
-
.run(projectKey, agentId);
|
|
272
|
+
.prepare('DELETE FROM knowledge_project_agents WHERE tenant_id = ? AND gateway_id = ? AND project_key = ? AND local_agent_id = ?')
|
|
273
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', projectKey, agentId);
|
|
273
274
|
}
|
|
274
|
-
listProjectAgents(projectKey) {
|
|
275
|
+
listProjectAgents(projectKey, opts) {
|
|
275
276
|
const rows = this.db.raw
|
|
276
|
-
.prepare(
|
|
277
|
-
|
|
277
|
+
.prepare(`SELECT local_agent_id AS agent_id FROM knowledge_project_agents
|
|
278
|
+
WHERE tenant_id = ? AND gateway_id = ? AND project_key = ?
|
|
279
|
+
ORDER BY local_agent_id`)
|
|
280
|
+
.all(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', projectKey);
|
|
278
281
|
return rows.map((r) => r.agent_id);
|
|
279
282
|
}
|
|
280
283
|
/**
|
|
281
284
|
* 屏蔽 Global 源对某 agent 的可见(仅 global 源有意义)
|
|
282
285
|
*/
|
|
283
|
-
hideSource(agentId, sourceId) {
|
|
286
|
+
hideSource(agentId, sourceId, opts) {
|
|
284
287
|
const source = this.get(sourceId);
|
|
285
288
|
if (source && source.scopeRef.level !== 'global') {
|
|
286
289
|
throw new Error(`hide is only valid for global sources (got scope ${source.scopeRef.level})`);
|
|
287
290
|
}
|
|
288
291
|
this.db.raw
|
|
289
|
-
.prepare(`INSERT OR IGNORE INTO knowledge_agent_hidden
|
|
290
|
-
|
|
291
|
-
|
|
292
|
+
.prepare(`INSERT OR IGNORE INTO knowledge_agent_hidden
|
|
293
|
+
(tenant_id, gateway_id, local_agent_id, source_id, created_at)
|
|
294
|
+
VALUES (?, ?, ?, ?, ?)`)
|
|
295
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId, Date.now());
|
|
292
296
|
}
|
|
293
|
-
unhideSource(agentId, sourceId) {
|
|
297
|
+
unhideSource(agentId, sourceId, opts) {
|
|
294
298
|
this.db.raw
|
|
295
|
-
.prepare('DELETE FROM knowledge_agent_hidden WHERE
|
|
296
|
-
.run(agentId, sourceId);
|
|
299
|
+
.prepare('DELETE FROM knowledge_agent_hidden WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ? AND source_id = ?')
|
|
300
|
+
.run(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId, sourceId);
|
|
297
301
|
}
|
|
298
|
-
listHidden(agentId) {
|
|
302
|
+
listHidden(agentId, opts) {
|
|
299
303
|
const rows = this.db.raw
|
|
300
|
-
.prepare('SELECT source_id FROM knowledge_agent_hidden WHERE
|
|
301
|
-
.all(agentId);
|
|
304
|
+
.prepare('SELECT source_id FROM knowledge_agent_hidden WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ? ORDER BY source_id')
|
|
305
|
+
.all(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', agentId);
|
|
302
306
|
return rows.map((r) => r.source_id);
|
|
303
307
|
}
|
|
308
|
+
/** 哪些 agent 对该 global 源做了隐藏 */
|
|
309
|
+
listAgentsHidingSource(sourceId, opts) {
|
|
310
|
+
const rows = this.db.raw
|
|
311
|
+
.prepare('SELECT local_agent_id FROM knowledge_agent_hidden WHERE tenant_id = ? AND gateway_id = ? AND source_id = ? ORDER BY local_agent_id')
|
|
312
|
+
.all(opts?.tenantId ?? 'default', opts?.gatewayId ?? 'default', sourceId);
|
|
313
|
+
return rows.map((r) => r.local_agent_id);
|
|
314
|
+
}
|
|
304
315
|
/**
|
|
305
316
|
* 会话 overlay:单条 upsert(资产归属不变,只改本场视图)
|
|
306
317
|
*/
|
|
307
|
-
setSessionVisibility(sessionId, item) {
|
|
318
|
+
setSessionVisibility(sessionId, item, identity) {
|
|
308
319
|
if (!sessionId?.trim())
|
|
309
320
|
throw new Error('sessionId is required');
|
|
310
321
|
if (!item.targetId?.trim())
|
|
311
322
|
throw new Error('targetId is required');
|
|
312
323
|
this.db.raw
|
|
313
|
-
.prepare(`INSERT INTO knowledge_session_visibility
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
324
|
+
.prepare(`INSERT INTO knowledge_session_visibility
|
|
325
|
+
(tenant_id, gateway_id, local_session_id, target_type, target_id, op, created_at)
|
|
326
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
327
|
+
ON CONFLICT(tenant_id, gateway_id, local_session_id, target_type, target_id)
|
|
328
|
+
DO UPDATE SET op = excluded.op`)
|
|
329
|
+
.run(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', sessionId, item.targetType, item.targetId, item.op, Date.now());
|
|
317
330
|
}
|
|
318
331
|
/**
|
|
319
|
-
* 会话 overlay
|
|
332
|
+
* 会话 overlay:全量替换(按 tenant/gateway 隔离,禁清他方行)
|
|
320
333
|
*/
|
|
321
|
-
replaceSessionVisibility(sessionId, items) {
|
|
334
|
+
replaceSessionVisibility(sessionId, items, identity) {
|
|
322
335
|
if (!sessionId?.trim())
|
|
323
336
|
throw new Error('sessionId is required');
|
|
324
|
-
this.
|
|
325
|
-
.prepare('DELETE FROM knowledge_session_visibility WHERE session_id = ?')
|
|
326
|
-
.run(sessionId);
|
|
337
|
+
this.clearSessionVisibility(sessionId, undefined, identity);
|
|
327
338
|
for (const item of items)
|
|
328
|
-
this.setSessionVisibility(sessionId, item);
|
|
339
|
+
this.setSessionVisibility(sessionId, item, identity);
|
|
329
340
|
}
|
|
330
|
-
clearSessionVisibility(sessionId, target) {
|
|
341
|
+
clearSessionVisibility(sessionId, target, identity) {
|
|
342
|
+
const tenant = identity?.tenantId ?? 'default';
|
|
343
|
+
const gateway = identity?.gatewayId ?? 'default';
|
|
331
344
|
if (target) {
|
|
332
345
|
this.db.raw
|
|
333
|
-
.prepare(
|
|
334
|
-
|
|
346
|
+
.prepare(`DELETE FROM knowledge_session_visibility
|
|
347
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?
|
|
348
|
+
AND target_type = ? AND target_id = ?`)
|
|
349
|
+
.run(tenant, gateway, sessionId, target.targetType, target.targetId);
|
|
335
350
|
return;
|
|
336
351
|
}
|
|
337
352
|
this.db.raw
|
|
338
|
-
.prepare(
|
|
339
|
-
|
|
353
|
+
.prepare(`DELETE FROM knowledge_session_visibility
|
|
354
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?`)
|
|
355
|
+
.run(tenant, gateway, sessionId);
|
|
340
356
|
}
|
|
341
|
-
listSessionVisibility(sessionId) {
|
|
357
|
+
listSessionVisibility(sessionId, identity) {
|
|
342
358
|
const rows = this.db.raw
|
|
343
|
-
.prepare(`SELECT session_id, target_type, target_id, op, created_at
|
|
344
|
-
FROM knowledge_session_visibility
|
|
359
|
+
.prepare(`SELECT local_session_id AS session_id, target_type, target_id, op, created_at
|
|
360
|
+
FROM knowledge_session_visibility
|
|
361
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?
|
|
345
362
|
ORDER BY created_at`)
|
|
346
|
-
.all(sessionId);
|
|
363
|
+
.all(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', sessionId);
|
|
347
364
|
return rows.map((r) => ({
|
|
348
365
|
sessionId: r.session_id,
|
|
349
366
|
targetType: r.target_type,
|
|
@@ -353,47 +370,65 @@ export class KnowledgeSourceStore {
|
|
|
353
370
|
}));
|
|
354
371
|
}
|
|
355
372
|
/** 项目登记视图(管理面:先建项目再挂源;**不**用孤儿源计数复活已删项目) */
|
|
356
|
-
listProjects() {
|
|
373
|
+
listProjects(opts) {
|
|
374
|
+
const tenant = opts?.tenantId ?? 'default';
|
|
375
|
+
const gateway = opts?.gatewayId ?? 'default';
|
|
357
376
|
const registered = this.db.raw
|
|
358
|
-
.prepare(`SELECT project_key, display_name FROM knowledge_projects
|
|
359
|
-
|
|
377
|
+
.prepare(`SELECT project_key, display_name FROM knowledge_projects
|
|
378
|
+
WHERE tenant_id = ? ORDER BY project_key`)
|
|
379
|
+
.all(tenant);
|
|
360
380
|
const counts = this.db.raw
|
|
361
381
|
.prepare(`SELECT scope_key AS project_key, COUNT(*) AS source_count
|
|
362
382
|
FROM knowledge_sources
|
|
363
|
-
WHERE scope_level = 'project' AND status != 'removed'
|
|
383
|
+
WHERE scope_level = 'project' AND status != 'removed' AND tenant_id = ?
|
|
364
384
|
GROUP BY scope_key`)
|
|
365
|
-
.all();
|
|
385
|
+
.all(tenant);
|
|
366
386
|
const countMap = new Map(counts.map((c) => [c.project_key, c.source_count]));
|
|
387
|
+
// 一次拉齐 project→agents,禁止每项目一条 N+1
|
|
388
|
+
const agentRows = this.db.raw
|
|
389
|
+
.prepare(`SELECT project_key, local_agent_id FROM knowledge_project_agents
|
|
390
|
+
WHERE tenant_id = ? AND gateway_id = ?
|
|
391
|
+
ORDER BY project_key, local_agent_id`)
|
|
392
|
+
.all(tenant, gateway);
|
|
393
|
+
const agentsByProject = new Map();
|
|
394
|
+
for (const row of agentRows) {
|
|
395
|
+
const list = agentsByProject.get(row.project_key) ?? [];
|
|
396
|
+
list.push(row.local_agent_id);
|
|
397
|
+
agentsByProject.set(row.project_key, list);
|
|
398
|
+
}
|
|
367
399
|
// 只列已登记项目;孤儿源(项目已删)不再“复活”项目行
|
|
368
400
|
return registered.map((r) => ({
|
|
369
401
|
projectKey: r.project_key,
|
|
370
402
|
displayName: r.display_name ?? undefined,
|
|
371
403
|
sourceCount: countMap.get(r.project_key) ?? 0,
|
|
372
|
-
assignedAgentIds:
|
|
404
|
+
assignedAgentIds: agentsByProject.get(r.project_key) ?? [],
|
|
373
405
|
}));
|
|
374
406
|
}
|
|
375
407
|
/** 登记空项目(先建项目再挂源) */
|
|
376
|
-
createProject(projectKey, displayName) {
|
|
408
|
+
createProject(projectKey, displayName, opts) {
|
|
377
409
|
const key = projectKey.trim();
|
|
378
410
|
if (!key)
|
|
379
411
|
throw new Error('projectKey is required');
|
|
412
|
+
const tenant = opts?.tenantId ?? 'default';
|
|
380
413
|
const now = Date.now();
|
|
381
414
|
this.db.raw
|
|
382
|
-
.prepare(`INSERT INTO knowledge_projects
|
|
383
|
-
|
|
384
|
-
|
|
415
|
+
.prepare(`INSERT INTO knowledge_projects
|
|
416
|
+
(tenant_id, project_key, display_name, registered_by, visibility, created_at, updated_at)
|
|
417
|
+
VALUES (?, ?, ?, ?, 'private', ?, ?)
|
|
418
|
+
ON CONFLICT(tenant_id, project_key) DO UPDATE SET
|
|
385
419
|
display_name = COALESCE(excluded.display_name, knowledge_projects.display_name),
|
|
386
420
|
updated_at = excluded.updated_at`)
|
|
387
|
-
.run(key, displayName ?? null, now, now);
|
|
421
|
+
.run(tenant, key, displayName ?? null, opts?.registeredBy ?? 'default', now, now);
|
|
388
422
|
}
|
|
389
423
|
/**
|
|
390
424
|
* 删除项目登记。非空项目拒绝(先卸载/删除项目下源),避免孤儿源与静默收养。
|
|
391
425
|
*/
|
|
392
|
-
removeProject(projectKey) {
|
|
426
|
+
removeProject(projectKey, opts) {
|
|
427
|
+
const tenant = opts?.tenantId ?? 'default';
|
|
393
428
|
const sourceCount = this.db.raw
|
|
394
429
|
.prepare(`SELECT COUNT(*) AS n FROM knowledge_sources
|
|
395
|
-
WHERE scope_level = 'project' AND scope_key = ? AND status != 'removed'
|
|
396
|
-
.get(projectKey).n;
|
|
430
|
+
WHERE scope_level = 'project' AND scope_key = ? AND status != 'removed' AND tenant_id = ?`)
|
|
431
|
+
.get(projectKey, tenant).n;
|
|
397
432
|
if (sourceCount > 0) {
|
|
398
433
|
throw new Error(`project "${projectKey}" still has ${sourceCount} source(s); remove or reassign them first`);
|
|
399
434
|
}
|
|
@@ -409,36 +444,56 @@ export class KnowledgeSourceStore {
|
|
|
409
444
|
return Number(res.changes ?? 0) > 0;
|
|
410
445
|
}
|
|
411
446
|
/**
|
|
412
|
-
* 源是否对 (agent, session) 可见(
|
|
447
|
+
* 源是否对 (agent, session) 可见(gateway 漏斗 ⊕ base ⊕ session overlay)
|
|
413
448
|
*/
|
|
414
|
-
isVisible(source, agentId, sessionId) {
|
|
449
|
+
isVisible(source, agentId, sessionId, identity) {
|
|
415
450
|
if (source.status === 'removed' || source.status === 'disabled')
|
|
416
451
|
return false;
|
|
417
|
-
|
|
452
|
+
if (!this.isGatewayVisible(source, identity))
|
|
453
|
+
return false;
|
|
454
|
+
return this.applySessionOverlay(source, this.isBaseVisible(source, agentId, sessionId, identity), sessionId, identity);
|
|
455
|
+
}
|
|
456
|
+
/** 网关漏斗:registered_by=己方 ∪ public */
|
|
457
|
+
isGatewayVisible(source, identity) {
|
|
458
|
+
const gw = identity?.gatewayId;
|
|
459
|
+
if (!gw)
|
|
460
|
+
return true;
|
|
461
|
+
const row = this.db.raw
|
|
462
|
+
.prepare('SELECT registered_by, visibility, tenant_id FROM knowledge_sources WHERE id = ?')
|
|
463
|
+
.get(source.id);
|
|
464
|
+
if (!row)
|
|
465
|
+
return true;
|
|
466
|
+
const tenant = identity?.tenantId ?? 'default';
|
|
467
|
+
if (row.tenant_id && row.tenant_id !== tenant)
|
|
468
|
+
return false;
|
|
469
|
+
if (row.registered_by && row.registered_by !== gw && row.visibility !== 'public') {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
return true;
|
|
418
473
|
}
|
|
419
474
|
/** Agent 级 base 可见(不含会话 overlay;排查用) */
|
|
420
|
-
isBaseVisible(source, agentId, sessionId) {
|
|
475
|
+
isBaseVisible(source, agentId, sessionId, identity) {
|
|
421
476
|
if (source.status === 'removed' || source.status === 'disabled')
|
|
422
477
|
return false;
|
|
423
478
|
const { level, key } = source.scopeRef;
|
|
424
479
|
if (level === 'global') {
|
|
425
|
-
return !this.isHidden(agentId, source.id);
|
|
480
|
+
return !this.isHidden(agentId, source.id, identity);
|
|
426
481
|
}
|
|
427
482
|
if (level === 'project') {
|
|
428
|
-
return this.isProjectAssigned(key, agentId);
|
|
483
|
+
return this.isProjectAssigned(key, agentId, identity);
|
|
429
484
|
}
|
|
430
485
|
return Boolean(sessionId) && key === sessionId;
|
|
431
486
|
}
|
|
432
|
-
applySessionOverlay(source, base, sessionId) {
|
|
487
|
+
applySessionOverlay(source, base, sessionId, identity) {
|
|
433
488
|
if (!sessionId)
|
|
434
489
|
return base;
|
|
435
|
-
const srcOp = this.sessionOp(sessionId, 'source', source.id);
|
|
490
|
+
const srcOp = this.sessionOp(sessionId, 'source', source.id, identity);
|
|
436
491
|
if (srcOp === 'exclude')
|
|
437
492
|
return false;
|
|
438
493
|
if (srcOp === 'include')
|
|
439
494
|
return true;
|
|
440
495
|
if (source.scopeRef.level === 'project') {
|
|
441
|
-
const projOp = this.sessionOp(sessionId, 'project', source.scopeRef.key);
|
|
496
|
+
const projOp = this.sessionOp(sessionId, 'project', source.scopeRef.key, identity);
|
|
442
497
|
if (projOp === 'exclude')
|
|
443
498
|
return false;
|
|
444
499
|
if (projOp === 'include')
|
|
@@ -446,25 +501,30 @@ export class KnowledgeSourceStore {
|
|
|
446
501
|
}
|
|
447
502
|
return base;
|
|
448
503
|
}
|
|
449
|
-
sessionOp(sessionId, targetType, targetId) {
|
|
504
|
+
sessionOp(sessionId, targetType, targetId, identity) {
|
|
450
505
|
const row = this.db.raw
|
|
451
|
-
.prepare(
|
|
452
|
-
|
|
506
|
+
.prepare(`SELECT op FROM knowledge_session_visibility
|
|
507
|
+
WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?
|
|
508
|
+
AND target_type = ? AND target_id = ?`)
|
|
509
|
+
.get(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', sessionId, targetType, targetId);
|
|
453
510
|
return row?.op ?? null;
|
|
454
511
|
}
|
|
455
|
-
isHidden(agentId, sourceId) {
|
|
512
|
+
isHidden(agentId, sourceId, identity) {
|
|
456
513
|
const row = this.db.raw
|
|
457
|
-
.prepare('SELECT 1 AS ok FROM knowledge_agent_hidden WHERE
|
|
458
|
-
.get(agentId, sourceId);
|
|
514
|
+
.prepare('SELECT 1 AS ok FROM knowledge_agent_hidden WHERE tenant_id = ? AND gateway_id = ? AND local_agent_id = ? AND source_id = ?')
|
|
515
|
+
.get(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', agentId, sourceId);
|
|
459
516
|
return Boolean(row);
|
|
460
517
|
}
|
|
461
|
-
isProjectAssigned(projectKey, agentId) {
|
|
518
|
+
isProjectAssigned(projectKey, agentId, identity) {
|
|
462
519
|
const row = this.db.raw
|
|
463
|
-
.prepare('SELECT 1 AS ok FROM knowledge_project_agents WHERE project_key = ? AND
|
|
464
|
-
.get(projectKey, agentId);
|
|
520
|
+
.prepare('SELECT 1 AS ok FROM knowledge_project_agents WHERE tenant_id = ? AND gateway_id = ? AND project_key = ? AND local_agent_id = ?')
|
|
521
|
+
.get(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', projectKey, agentId);
|
|
465
522
|
return Boolean(row);
|
|
466
523
|
}
|
|
467
524
|
// ── Catalog ──
|
|
525
|
+
/** catalog 规模/topics 派生缓存(Tier 0 每轮装配;短 TTL 足够) */
|
|
526
|
+
catalogExtrasCache = null;
|
|
527
|
+
static CATALOG_EXTRAS_TTL_MS = 2_000;
|
|
468
528
|
/**
|
|
469
529
|
* 生成 agent 可见 catalog(Tier 0)
|
|
470
530
|
* 默认返回可见全集(display 截断在 KnowledgeLayer);maxEntries 仅作安全硬顶
|
|
@@ -479,37 +539,48 @@ export class KnowledgeSourceStore {
|
|
|
479
539
|
return pb - pa;
|
|
480
540
|
return a.displayName.localeCompare(b.displayName);
|
|
481
541
|
});
|
|
482
|
-
const
|
|
483
|
-
const topicsBySource = this.topicsBySource();
|
|
542
|
+
const extras = this.catalogExtras();
|
|
484
543
|
return sorted
|
|
485
544
|
.filter((s) => !s.hiddenFromCatalog)
|
|
486
545
|
.slice(0, max)
|
|
487
546
|
.map((s) => toCatalogItem(s, {
|
|
488
|
-
fileCount: fileCounts.get(s.id),
|
|
489
|
-
topics:
|
|
547
|
+
fileCount: extras.fileCounts.get(s.id),
|
|
548
|
+
topics: extras.topics.get(s.id),
|
|
490
549
|
}));
|
|
491
550
|
}
|
|
551
|
+
/** 规模 + topics(短 TTL 缓存;禁止每轮全表拉 path) */
|
|
552
|
+
catalogExtras() {
|
|
553
|
+
const now = Date.now();
|
|
554
|
+
if (this.catalogExtrasCache && now - this.catalogExtrasCache.at < KnowledgeSourceStore.CATALOG_EXTRAS_TTL_MS) {
|
|
555
|
+
return this.catalogExtrasCache;
|
|
556
|
+
}
|
|
557
|
+
const fileCounts = this.fileCountsBySource();
|
|
558
|
+
const topics = this.topicsBySource();
|
|
559
|
+
this.catalogExtrasCache = { at: now, fileCounts, topics };
|
|
560
|
+
return this.catalogExtrasCache;
|
|
561
|
+
}
|
|
492
562
|
/** 每源已入库文件数(catalog 规模粗标用) */
|
|
493
563
|
fileCountsBySource() {
|
|
494
564
|
const rows = this.db.raw
|
|
495
|
-
.prepare('SELECT source_id AS id, COUNT(*) AS n FROM
|
|
565
|
+
.prepare('SELECT source_id AS id, COUNT(*) AS n FROM knowledge_memberships GROUP BY source_id')
|
|
496
566
|
.all();
|
|
497
567
|
return new Map(rows.map((r) => [r.id, r.n]));
|
|
498
568
|
}
|
|
499
|
-
/**
|
|
569
|
+
/**
|
|
570
|
+
* 每源路径派生 topics(catalog 搜索线索)
|
|
571
|
+
*
|
|
572
|
+
* 只抽样前 N 条 path(按字典序),避免大库全表扫;topics 是粗线索不是精确索引。
|
|
573
|
+
*/
|
|
500
574
|
topicsBySource() {
|
|
501
|
-
const
|
|
502
|
-
.prepare(
|
|
503
|
-
.all();
|
|
504
|
-
const bySource = new Map();
|
|
505
|
-
for (const r of rows) {
|
|
506
|
-
const list = bySource.get(r.id) ?? [];
|
|
507
|
-
list.push(r.path);
|
|
508
|
-
bySource.set(r.id, list);
|
|
509
|
-
}
|
|
575
|
+
const sourceIds = this.db.raw
|
|
576
|
+
.prepare(`SELECT id FROM knowledge_sources WHERE status != 'removed'`)
|
|
577
|
+
.all().map((r) => r.id);
|
|
510
578
|
const out = new Map();
|
|
511
|
-
|
|
512
|
-
|
|
579
|
+
const stmt = this.db.raw.prepare(`SELECT logical_path AS path FROM knowledge_memberships WHERE source_id = ? ORDER BY logical_path LIMIT 128`);
|
|
580
|
+
for (const id of sourceIds) {
|
|
581
|
+
const paths = stmt.all(id).map((r) => r.path);
|
|
582
|
+
if (paths.length > 0)
|
|
583
|
+
out.set(id, deriveTopicsFromPaths(paths));
|
|
513
584
|
}
|
|
514
585
|
return out;
|
|
515
586
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* startKnowledgeServiceProcess — fork 独立子进程托管 Knowledge Service
|
|
3
|
+
*
|
|
4
|
+
* 为什么是进程而不是 Worker:知识索引重且稳定不可控,不得与 Gateway 共命运。
|
|
5
|
+
* Worker 仍共享堆/OOM 与进程生命周期,只是把 event loop 挪开,不是故障隔离。
|
|
6
|
+
*
|
|
7
|
+
* fork 根因约束(与 `octopi serve` daemon 同款):
|
|
8
|
+
* - `execArgv: []`:禁止继承父进程 loader/inspect/NODE_OPTIONS 钩子
|
|
9
|
+
* - 剥离 `NODE_CHANNEL_*`:父进程若是 IPC 子进程(vitest / daemon),继承会错绑通道
|
|
10
|
+
* - `stdio` 不用 `inherit`:Windows 管道句柄继承会干扰 IPC
|
|
11
|
+
* - Electron 宿主:`ELECTRON_RUN_AS_NODE=1` + **保持** `process.execPath`。
|
|
12
|
+
* 换成捆绑 node.exe 当 execPath 会切断 Windows 上的 fork IPC。
|
|
13
|
+
*/
|
|
14
|
+
import { type ChildProcess } from 'node:child_process';
|
|
15
|
+
import type { KnowledgeServiceToken } from './http-app.js';
|
|
16
|
+
import type { DocumentCapabilityConfig } from '../capabilities/document/factory.js';
|
|
17
|
+
export interface KnowledgeServiceProcessOptions {
|
|
18
|
+
dbPath: string;
|
|
19
|
+
port?: number;
|
|
20
|
+
host?: string;
|
|
21
|
+
tokens: KnowledgeServiceToken[];
|
|
22
|
+
documentConfig?: DocumentCapabilityConfig | null;
|
|
23
|
+
embed?: {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
embedBatch?: number;
|
|
26
|
+
embedMinIntervalMs?: number;
|
|
27
|
+
embedConcurrency?: number;
|
|
28
|
+
embedSecretPolicy?: 'allow' | 'redact' | 'skip';
|
|
29
|
+
};
|
|
30
|
+
sqliteVecExtensionPath?: string;
|
|
31
|
+
embeddingModels?: {
|
|
32
|
+
providers?: Record<string, unknown>;
|
|
33
|
+
embedding?: unknown;
|
|
34
|
+
} | null;
|
|
35
|
+
startTimeoutMs?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface KnowledgeServiceProcessHandle {
|
|
38
|
+
child: ChildProcess;
|
|
39
|
+
port: number;
|
|
40
|
+
/** 发 close 并等待退出;超时 SIGTERM */
|
|
41
|
+
stop(): Promise<void>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 在独立子进程启动 Knowledge HTTP Service。
|
|
45
|
+
*
|
|
46
|
+
* @param opts - 与 startKnowledgeService 同构 + embeddingModels(子进程内重建 provider)
|
|
47
|
+
* @returns child + 端口 + stop
|
|
48
|
+
*/
|
|
49
|
+
export declare function startKnowledgeServiceProcess(opts: KnowledgeServiceProcessOptions): Promise<KnowledgeServiceProcessHandle>;
|