@octopi-agent/engine 0.56.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.
Files changed (175) hide show
  1. package/dist/config-schema/context.d.ts +1 -1
  2. package/dist/config-schema/context.js +1 -1
  3. package/dist/config-schema/engine.d.ts +43 -1
  4. package/dist/config-schema/substrate.d.ts +42 -0
  5. package/dist/config-schema/substrate.js +58 -0
  6. package/dist/config.d.ts +39 -1
  7. package/dist/harness/agent/builder.d.ts +14 -0
  8. package/dist/harness/agent/builder.js +14 -2
  9. package/dist/harness/agent/config-bridge.js +3 -0
  10. package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.d.ts +1 -1
  11. package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.js +1 -1
  12. package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.d.ts +1 -1
  13. package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.js +1 -1
  14. package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.d.ts +1 -1
  15. package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.js +13 -3
  16. package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.d.ts +1 -1
  17. package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.js +13 -4
  18. package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.d.ts +1 -1
  19. package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.js +25 -5
  20. package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.d.ts +1 -1
  21. package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.js +1 -1
  22. package/dist/harness/{context/capabilities → capabilities}/document/errors.d.ts +1 -1
  23. package/dist/harness/{context/capabilities → capabilities}/document/errors.js +1 -1
  24. package/dist/harness/capabilities/document/factory.d.ts +33 -0
  25. package/dist/harness/capabilities/document/factory.js +37 -0
  26. package/dist/harness/{context/capabilities → capabilities}/document/format.d.ts +1 -1
  27. package/dist/harness/{context/capabilities → capabilities}/document/format.js +1 -1
  28. package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.d.ts +1 -1
  29. package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.js +1 -1
  30. package/dist/harness/{context/capabilities → capabilities}/document/index.d.ts +3 -1
  31. package/dist/harness/{context/capabilities → capabilities}/document/index.js +2 -1
  32. package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.d.ts +3 -1
  33. package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.js +3 -1
  34. package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.d.ts +3 -1
  35. package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.js +44 -1
  36. package/dist/harness/{context/capabilities → capabilities}/document/port.d.ts +1 -1
  37. package/dist/harness/{context/capabilities → capabilities}/document/port.js +4 -4
  38. package/dist/harness/{context/capabilities → capabilities}/document/types.d.ts +11 -1
  39. package/dist/harness/{context/capabilities → capabilities}/document/types.js +1 -1
  40. package/dist/harness/capabilities/document/worker-port.d.ts +13 -0
  41. package/dist/harness/capabilities/document/worker-port.js +78 -0
  42. package/dist/harness/context/capabilities/index.d.ts +2 -2
  43. package/dist/harness/context/capabilities/index.js +2 -2
  44. package/dist/harness/context/constitution/default-agents.md +1 -0
  45. package/dist/harness/context/index.d.ts +2 -2
  46. package/dist/harness/context/index.js +1 -1
  47. package/dist/harness/context/layer-types.d.ts +26 -2
  48. package/dist/harness/context/layer-types.js +59 -11
  49. package/dist/harness/context/layers.d.ts +3 -0
  50. package/dist/harness/context/layers.js +38 -12
  51. package/dist/harness/context/system-prompt-assembler.d.ts +5 -0
  52. package/dist/harness/context/system-prompt-assembler.js +2 -0
  53. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.d.ts +10 -0
  54. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +115 -5
  55. package/dist/harness/extension/plugin-ecosystem/tools/memory.js +3 -0
  56. package/dist/harness/index.d.ts +5 -3
  57. package/dist/harness/index.js +3 -2
  58. package/dist/harness/knowledge/adapters.d.ts +8 -5
  59. package/dist/harness/knowledge/adapters.js +53 -13
  60. package/dist/harness/knowledge/catalog-types.d.ts +4 -0
  61. package/dist/harness/knowledge/client.d.ts +197 -0
  62. package/dist/harness/knowledge/client.js +237 -0
  63. package/dist/harness/knowledge/connector-fetcher.d.ts +2 -2
  64. package/dist/harness/knowledge/connectors.d.ts +3 -3
  65. package/dist/harness/knowledge/connectors.js +14 -8
  66. package/dist/harness/knowledge/db.d.ts +42 -3
  67. package/dist/harness/knowledge/db.js +466 -83
  68. package/dist/harness/knowledge/describe.d.ts +2 -0
  69. package/dist/harness/knowledge/describe.js +5 -1
  70. package/dist/harness/knowledge/document-extract-worker.d.ts +1 -0
  71. package/dist/harness/knowledge/document-extract-worker.js +68 -0
  72. package/dist/harness/knowledge/embed-runner.d.ts +53 -0
  73. package/dist/harness/knowledge/embed-runner.js +171 -0
  74. package/dist/harness/knowledge/engine-thread.d.ts +1 -0
  75. package/dist/harness/knowledge/engine-thread.js +302 -0
  76. package/dist/harness/knowledge/extract-document.d.ts +33 -0
  77. package/dist/harness/knowledge/extract-document.js +86 -0
  78. package/dist/harness/knowledge/fetchers.d.ts +14 -3
  79. package/dist/harness/knowledge/fetchers.js +52 -16
  80. package/dist/harness/knowledge/file-identity.d.ts +53 -0
  81. package/dist/harness/knowledge/file-identity.js +172 -0
  82. package/dist/harness/knowledge/file-index-store.d.ts +50 -0
  83. package/dist/harness/knowledge/file-index-store.js +132 -0
  84. package/dist/harness/knowledge/file-limits.d.ts +86 -0
  85. package/dist/harness/knowledge/file-limits.js +175 -0
  86. package/dist/harness/knowledge/fts.d.ts +60 -0
  87. package/dist/harness/knowledge/fts.js +218 -0
  88. package/dist/harness/knowledge/grounding.d.ts +3 -2
  89. package/dist/harness/knowledge/http-app.d.ts +50 -0
  90. package/dist/harness/knowledge/http-app.js +652 -0
  91. package/dist/harness/knowledge/http-bridge.d.ts +49 -0
  92. package/dist/harness/knowledge/http-bridge.js +17 -0
  93. package/dist/harness/knowledge/index-store.d.ts +166 -63
  94. package/dist/harness/knowledge/index-store.js +1025 -282
  95. package/dist/harness/knowledge/index.d.ts +47 -5
  96. package/dist/harness/knowledge/index.js +24 -2
  97. package/dist/harness/knowledge/ingest.d.ts +322 -49
  98. package/dist/harness/knowledge/ingest.js +1383 -267
  99. package/dist/harness/knowledge/job-control-state.d.ts +29 -0
  100. package/dist/harness/knowledge/job-control-state.js +44 -0
  101. package/dist/harness/knowledge/job-control.d.ts +35 -0
  102. package/dist/harness/knowledge/job-control.js +86 -0
  103. package/dist/harness/knowledge/job-queue.d.ts +62 -0
  104. package/dist/harness/knowledge/job-queue.js +173 -0
  105. package/dist/harness/knowledge/knowledge-serve-child.d.ts +1 -0
  106. package/dist/harness/knowledge/knowledge-serve-child.js +100 -0
  107. package/dist/harness/knowledge/local-stack.d.ts +27 -0
  108. package/dist/harness/knowledge/local-stack.js +63 -0
  109. package/dist/harness/knowledge/membership-store.d.ts +65 -0
  110. package/dist/harness/knowledge/membership-store.js +198 -0
  111. package/dist/harness/knowledge/network-guard.d.ts +11 -2
  112. package/dist/harness/knowledge/network-guard.js +130 -85
  113. package/dist/harness/knowledge/parse-text-in-worker.d.ts +32 -0
  114. package/dist/harness/knowledge/parse-text-in-worker.js +158 -0
  115. package/dist/harness/knowledge/query-service.d.ts +272 -0
  116. package/dist/harness/knowledge/query-service.js +326 -0
  117. package/dist/harness/knowledge/query-worker-client.d.ts +96 -0
  118. package/dist/harness/knowledge/query-worker-client.js +207 -0
  119. package/dist/harness/knowledge/query-worker.d.ts +1 -0
  120. package/dist/harness/knowledge/query-worker.js +186 -0
  121. package/dist/harness/knowledge/read-http.d.ts +35 -0
  122. package/dist/harness/knowledge/read-http.js +373 -0
  123. package/dist/harness/knowledge/retriever.d.ts +30 -5
  124. package/dist/harness/knowledge/retriever.js +98 -35
  125. package/dist/harness/knowledge/secret-scan.d.ts +13 -2
  126. package/dist/harness/knowledge/secret-scan.js +25 -2
  127. package/dist/harness/knowledge/serve.d.ts +48 -0
  128. package/dist/harness/knowledge/serve.js +336 -0
  129. package/dist/harness/knowledge/source-store.d.ts +83 -16
  130. package/dist/harness/knowledge/source-store.js +205 -78
  131. package/dist/harness/knowledge/start-service-process.d.ts +49 -0
  132. package/dist/harness/knowledge/start-service-process.js +158 -0
  133. package/dist/harness/knowledge/text-parse-worker.d.ts +1 -0
  134. package/dist/harness/knowledge/text-parse-worker.js +54 -0
  135. package/dist/harness/knowledge/topics.d.ts +13 -0
  136. package/dist/harness/knowledge/topics.js +48 -0
  137. package/dist/harness/knowledge/types.d.ts +1 -1
  138. package/dist/harness/knowledge/types.js +1 -1
  139. package/dist/harness/knowledge/vector-ann.d.ts +42 -0
  140. package/dist/harness/knowledge/vector-ann.js +68 -0
  141. package/dist/harness/knowledge/writer-lock.d.ts +13 -0
  142. package/dist/harness/knowledge/writer-lock.js +71 -0
  143. package/dist/harness/knowledge/writer-service.d.ts +194 -0
  144. package/dist/harness/knowledge/writer-service.js +355 -0
  145. package/dist/harness/knowledge/writer-worker-client.d.ts +102 -0
  146. package/dist/harness/knowledge/writer-worker-client.js +381 -0
  147. package/dist/harness/knowledge/writer-worker.d.ts +1 -0
  148. package/dist/harness/knowledge/writer-worker.js +189 -0
  149. package/dist/harness/memory/index.d.ts +3 -2
  150. package/dist/harness/memory/index.js +3 -1
  151. package/dist/harness/memory/retrieval-rank.d.ts +51 -0
  152. package/dist/harness/memory/retrieval-rank.js +60 -0
  153. package/dist/harness/memory/sqlite/embedding.d.ts +5 -2
  154. package/dist/harness/memory/sqlite/embedding.js +109 -16
  155. package/dist/harness/memory/sqlite/index.d.ts +2 -2
  156. package/dist/harness/memory/sqlite/index.js +1 -1
  157. package/dist/harness/memory/sqlite/keyword-search.d.ts +41 -2
  158. package/dist/harness/memory/sqlite/keyword-search.js +104 -17
  159. package/dist/harness/memory/sqlite/memory-store.d.ts +18 -4
  160. package/dist/harness/memory/sqlite/memory-store.js +89 -39
  161. package/dist/harness/memory/store.js +39 -21
  162. package/dist/harness/memory/types.d.ts +7 -0
  163. package/dist/harness/run/runner.d.ts +1 -0
  164. package/dist/harness/run/runner.js +4 -2
  165. package/dist/harness/session/attachments/service.d.ts +1 -1
  166. package/dist/harness/session/attachments/service.js +1 -1
  167. package/dist/harness/session/tasks/index.d.ts +1 -1
  168. package/dist/harness/session/tasks/index.js +1 -1
  169. package/dist/harness/session/tasks/render.d.ts +10 -0
  170. package/dist/harness/session/tasks/render.js +27 -0
  171. package/dist/index.d.ts +4 -2
  172. package/dist/index.js +2 -1
  173. package/dist/worker-path.d.ts +8 -0
  174. package/dist/worker-path.js +15 -0
  175. 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): void;
42
- unassignProject(projectKey: string, agentId: string): void;
43
- listProjectAgents(projectKey: string): string[];
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): void;
48
- unhideSource(agentId: string, sourceId: KnowledgeSourceId | string): void;
49
- listHidden(agentId: string): string[];
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): void;
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[]): void;
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): import('./types.js').KnowledgeSessionVisibilityItem[];
94
+ listSessionVisibility(sessionId: string, identity?: {
95
+ tenantId?: string;
96
+ gatewayId?: string;
97
+ }): import('./types.js').KnowledgeSessionVisibilityItem[];
63
98
  /** 项目登记视图(管理面:先建项目再挂源;**不**用孤儿源计数复活已删项目) */
64
- listProjects(): Array<{
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): void;
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): boolean;
116
+ removeProject(projectKey: string, opts?: {
117
+ tenantId?: string;
118
+ }): boolean;
76
119
  /**
77
- * 源是否对 (agent, session) 可见(effective = base ⊕ session overlay)
120
+ * 源是否对 (agent, session) 可见(gateway 漏斗 ⊕ base ⊕ session overlay)
78
121
  */
79
- isVisible(source: KnowledgeSource, agentId: string, sessionId?: string): boolean;
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): boolean;
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,6 +148,16 @@ export declare class KnowledgeSourceStore {
91
148
  sessionId?: string;
92
149
  maxEntries?: number;
93
150
  }): KnowledgeCatalogItem[];
151
+ /** 规模 + topics(短 TTL 缓存;禁止每轮全表拉 path) */
152
+ private catalogExtras;
153
+ /** 每源已入库文件数(catalog 规模粗标用) */
154
+ private fileCountsBySource;
155
+ /**
156
+ * 每源路径派生 topics(catalog 搜索线索)
157
+ *
158
+ * 只抽样前 N 条 path(按字典序),避免大库全表扫;topics 是粗线索不是精确索引。
159
+ */
160
+ private topicsBySource;
94
161
  /**
95
162
  * catalog 粗桶指纹(防每轮重渲)
96
163
  */
@@ -3,6 +3,7 @@
3
3
  */
4
4
  import { randomUUID, createHash } from 'node:crypto';
5
5
  import { KnowledgeDatabase } from './db.js';
6
+ import { deriveTopicsFromPaths } from './topics.js';
6
7
  import { asSourceId } from './types.js';
7
8
  const DEFAULT_SYNC = { strategy: 'watch', debounceMs: 2000, enabled: true };
8
9
  function statusBucket(status) {
@@ -16,11 +17,28 @@ function statusBucket(status) {
16
17
  return 'off';
17
18
  return 'pending';
18
19
  }
19
- function scaleLabel(source) {
20
+ /** 规模粗标:`~1.2k files` / `small`(arch/knowledge-layer.md §4.1);不输出精确 coverage */
21
+ function formatFileScale(fileCount) {
22
+ if (fileCount <= 0)
23
+ return 'empty';
24
+ if (fileCount < 30)
25
+ return 'small';
26
+ if (fileCount < 1000)
27
+ return `~${fileCount} files`;
28
+ if (fileCount < 1_000_000) {
29
+ const k = fileCount / 1000;
30
+ return `~${k >= 10 ? Math.round(k) : Math.round(k * 10) / 10}k files`;
31
+ }
32
+ const m = fileCount / 1_000_000;
33
+ return `~${m >= 10 ? Math.round(m) : Math.round(m * 10) / 10}m files`;
34
+ }
35
+ function scaleLabel(source, fileCount) {
36
+ if (fileCount != null && fileCount > 0)
37
+ return formatFileScale(fileCount);
20
38
  const cov = source.coverage;
21
39
  if (cov == null)
22
40
  return undefined;
23
- // 粗标,避免 coverage 数字抖动进 catalog fingerprint
41
+ // 尚无文件规模时用覆盖粗桶;避免 coverage 数字抖动进 catalog fingerprint
24
42
  if (cov >= 0.999)
25
43
  return 'ready';
26
44
  if (cov >= 0.5)
@@ -41,7 +59,7 @@ function deriveDisplayName(location) {
41
59
  const parts = cleaned.split(/[\\/]/).filter(Boolean);
42
60
  return parts[parts.length - 1] || location || 'source';
43
61
  }
44
- function toCatalogItem(source) {
62
+ function toCatalogItem(source, extras) {
45
63
  return {
46
64
  id: source.id,
47
65
  displayName: source.displayName,
@@ -49,7 +67,9 @@ function toCatalogItem(source) {
49
67
  status: statusBucket(source.status),
50
68
  description: source.description?.trim() || source.generatedDescription?.trim() || undefined,
51
69
  scopeLevel: source.scopeRef.level,
52
- scaleLabel: scaleLabel(source),
70
+ scaleLabel: scaleLabel(source, extras?.fileCount),
71
+ location: source.location,
72
+ topics: extras?.topics?.length ? extras.topics : undefined,
53
73
  };
54
74
  }
55
75
  function rowToSource(row) {
@@ -240,90 +260,107 @@ export class KnowledgeSourceStore {
240
260
  /**
241
261
  * 将 agent 挂到 Project(显式)
242
262
  */
243
- assignProject(projectKey, agentId) {
263
+ assignProject(projectKey, agentId, opts) {
244
264
  this.db.raw
245
- .prepare(`INSERT OR IGNORE INTO knowledge_project_agents (project_key, agent_id, created_at)
246
- VALUES (?, ?, ?)`)
247
- .run(projectKey, agentId, Date.now());
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());
248
269
  }
249
- unassignProject(projectKey, agentId) {
270
+ unassignProject(projectKey, agentId, opts) {
250
271
  this.db.raw
251
- .prepare('DELETE FROM knowledge_project_agents WHERE project_key = ? AND agent_id = ?')
252
- .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);
253
274
  }
254
- listProjectAgents(projectKey) {
275
+ listProjectAgents(projectKey, opts) {
255
276
  const rows = this.db.raw
256
- .prepare('SELECT agent_id FROM knowledge_project_agents WHERE project_key = ? ORDER BY agent_id')
257
- .all(projectKey);
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);
258
281
  return rows.map((r) => r.agent_id);
259
282
  }
260
283
  /**
261
284
  * 屏蔽 Global 源对某 agent 的可见(仅 global 源有意义)
262
285
  */
263
- hideSource(agentId, sourceId) {
286
+ hideSource(agentId, sourceId, opts) {
264
287
  const source = this.get(sourceId);
265
288
  if (source && source.scopeRef.level !== 'global') {
266
289
  throw new Error(`hide is only valid for global sources (got scope ${source.scopeRef.level})`);
267
290
  }
268
291
  this.db.raw
269
- .prepare(`INSERT OR IGNORE INTO knowledge_agent_hidden (agent_id, source_id, created_at)
270
- VALUES (?, ?, ?)`)
271
- .run(agentId, sourceId, Date.now());
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());
272
296
  }
273
- unhideSource(agentId, sourceId) {
297
+ unhideSource(agentId, sourceId, opts) {
274
298
  this.db.raw
275
- .prepare('DELETE FROM knowledge_agent_hidden WHERE agent_id = ? AND source_id = ?')
276
- .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);
277
301
  }
278
- listHidden(agentId) {
302
+ listHidden(agentId, opts) {
279
303
  const rows = this.db.raw
280
- .prepare('SELECT source_id FROM knowledge_agent_hidden WHERE agent_id = ? ORDER BY source_id')
281
- .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);
282
306
  return rows.map((r) => r.source_id);
283
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
+ }
284
315
  /**
285
316
  * 会话 overlay:单条 upsert(资产归属不变,只改本场视图)
286
317
  */
287
- setSessionVisibility(sessionId, item) {
318
+ setSessionVisibility(sessionId, item, identity) {
288
319
  if (!sessionId?.trim())
289
320
  throw new Error('sessionId is required');
290
321
  if (!item.targetId?.trim())
291
322
  throw new Error('targetId is required');
292
323
  this.db.raw
293
- .prepare(`INSERT INTO knowledge_session_visibility (session_id, target_type, target_id, op, created_at)
294
- VALUES (?, ?, ?, ?, ?)
295
- ON CONFLICT(session_id, target_type, target_id) DO UPDATE SET op = excluded.op`)
296
- .run(sessionId, item.targetType, item.targetId, item.op, Date.now());
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());
297
330
  }
298
331
  /**
299
- * 会话 overlay:全量替换
332
+ * 会话 overlay:全量替换(按 tenant/gateway 隔离,禁清他方行)
300
333
  */
301
- replaceSessionVisibility(sessionId, items) {
334
+ replaceSessionVisibility(sessionId, items, identity) {
302
335
  if (!sessionId?.trim())
303
336
  throw new Error('sessionId is required');
304
- this.db.raw
305
- .prepare('DELETE FROM knowledge_session_visibility WHERE session_id = ?')
306
- .run(sessionId);
337
+ this.clearSessionVisibility(sessionId, undefined, identity);
307
338
  for (const item of items)
308
- this.setSessionVisibility(sessionId, item);
339
+ this.setSessionVisibility(sessionId, item, identity);
309
340
  }
310
- clearSessionVisibility(sessionId, target) {
341
+ clearSessionVisibility(sessionId, target, identity) {
342
+ const tenant = identity?.tenantId ?? 'default';
343
+ const gateway = identity?.gatewayId ?? 'default';
311
344
  if (target) {
312
345
  this.db.raw
313
- .prepare('DELETE FROM knowledge_session_visibility WHERE session_id = ? AND target_type = ? AND target_id = ?')
314
- .run(sessionId, target.targetType, target.targetId);
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);
315
350
  return;
316
351
  }
317
352
  this.db.raw
318
- .prepare('DELETE FROM knowledge_session_visibility WHERE session_id = ?')
319
- .run(sessionId);
353
+ .prepare(`DELETE FROM knowledge_session_visibility
354
+ WHERE tenant_id = ? AND gateway_id = ? AND local_session_id = ?`)
355
+ .run(tenant, gateway, sessionId);
320
356
  }
321
- listSessionVisibility(sessionId) {
357
+ listSessionVisibility(sessionId, identity) {
322
358
  const rows = this.db.raw
323
- .prepare(`SELECT session_id, target_type, target_id, op, created_at
324
- FROM knowledge_session_visibility WHERE session_id = ?
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 = ?
325
362
  ORDER BY created_at`)
326
- .all(sessionId);
363
+ .all(identity?.tenantId ?? 'default', identity?.gatewayId ?? 'default', sessionId);
327
364
  return rows.map((r) => ({
328
365
  sessionId: r.session_id,
329
366
  targetType: r.target_type,
@@ -333,47 +370,65 @@ export class KnowledgeSourceStore {
333
370
  }));
334
371
  }
335
372
  /** 项目登记视图(管理面:先建项目再挂源;**不**用孤儿源计数复活已删项目) */
336
- listProjects() {
373
+ listProjects(opts) {
374
+ const tenant = opts?.tenantId ?? 'default';
375
+ const gateway = opts?.gatewayId ?? 'default';
337
376
  const registered = this.db.raw
338
- .prepare(`SELECT project_key, display_name FROM knowledge_projects ORDER BY project_key`)
339
- .all();
377
+ .prepare(`SELECT project_key, display_name FROM knowledge_projects
378
+ WHERE tenant_id = ? ORDER BY project_key`)
379
+ .all(tenant);
340
380
  const counts = this.db.raw
341
381
  .prepare(`SELECT scope_key AS project_key, COUNT(*) AS source_count
342
382
  FROM knowledge_sources
343
- WHERE scope_level = 'project' AND status != 'removed'
383
+ WHERE scope_level = 'project' AND status != 'removed' AND tenant_id = ?
344
384
  GROUP BY scope_key`)
345
- .all();
385
+ .all(tenant);
346
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
+ }
347
399
  // 只列已登记项目;孤儿源(项目已删)不再“复活”项目行
348
400
  return registered.map((r) => ({
349
401
  projectKey: r.project_key,
350
402
  displayName: r.display_name ?? undefined,
351
403
  sourceCount: countMap.get(r.project_key) ?? 0,
352
- assignedAgentIds: this.listProjectAgents(r.project_key),
404
+ assignedAgentIds: agentsByProject.get(r.project_key) ?? [],
353
405
  }));
354
406
  }
355
407
  /** 登记空项目(先建项目再挂源) */
356
- createProject(projectKey, displayName) {
408
+ createProject(projectKey, displayName, opts) {
357
409
  const key = projectKey.trim();
358
410
  if (!key)
359
411
  throw new Error('projectKey is required');
412
+ const tenant = opts?.tenantId ?? 'default';
360
413
  const now = Date.now();
361
414
  this.db.raw
362
- .prepare(`INSERT INTO knowledge_projects (project_key, display_name, created_at, updated_at)
363
- VALUES (?, ?, ?, ?)
364
- ON CONFLICT(project_key) DO UPDATE SET
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
365
419
  display_name = COALESCE(excluded.display_name, knowledge_projects.display_name),
366
420
  updated_at = excluded.updated_at`)
367
- .run(key, displayName ?? null, now, now);
421
+ .run(tenant, key, displayName ?? null, opts?.registeredBy ?? 'default', now, now);
368
422
  }
369
423
  /**
370
424
  * 删除项目登记。非空项目拒绝(先卸载/删除项目下源),避免孤儿源与静默收养。
371
425
  */
372
- removeProject(projectKey) {
426
+ removeProject(projectKey, opts) {
427
+ const tenant = opts?.tenantId ?? 'default';
373
428
  const sourceCount = this.db.raw
374
429
  .prepare(`SELECT COUNT(*) AS n FROM knowledge_sources
375
- WHERE scope_level = 'project' AND scope_key = ? AND status != 'removed'`)
376
- .get(projectKey).n;
430
+ WHERE scope_level = 'project' AND scope_key = ? AND status != 'removed' AND tenant_id = ?`)
431
+ .get(projectKey, tenant).n;
377
432
  if (sourceCount > 0) {
378
433
  throw new Error(`project "${projectKey}" still has ${sourceCount} source(s); remove or reassign them first`);
379
434
  }
@@ -389,36 +444,56 @@ export class KnowledgeSourceStore {
389
444
  return Number(res.changes ?? 0) > 0;
390
445
  }
391
446
  /**
392
- * 源是否对 (agent, session) 可见(effective = base ⊕ session overlay)
447
+ * 源是否对 (agent, session) 可见(gateway 漏斗 ⊕ base ⊕ session overlay)
393
448
  */
394
- isVisible(source, agentId, sessionId) {
449
+ isVisible(source, agentId, sessionId, identity) {
395
450
  if (source.status === 'removed' || source.status === 'disabled')
396
451
  return false;
397
- return this.applySessionOverlay(source, this.isBaseVisible(source, agentId, sessionId), sessionId);
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;
398
473
  }
399
474
  /** Agent 级 base 可见(不含会话 overlay;排查用) */
400
- isBaseVisible(source, agentId, sessionId) {
475
+ isBaseVisible(source, agentId, sessionId, identity) {
401
476
  if (source.status === 'removed' || source.status === 'disabled')
402
477
  return false;
403
478
  const { level, key } = source.scopeRef;
404
479
  if (level === 'global') {
405
- return !this.isHidden(agentId, source.id);
480
+ return !this.isHidden(agentId, source.id, identity);
406
481
  }
407
482
  if (level === 'project') {
408
- return this.isProjectAssigned(key, agentId);
483
+ return this.isProjectAssigned(key, agentId, identity);
409
484
  }
410
485
  return Boolean(sessionId) && key === sessionId;
411
486
  }
412
- applySessionOverlay(source, base, sessionId) {
487
+ applySessionOverlay(source, base, sessionId, identity) {
413
488
  if (!sessionId)
414
489
  return base;
415
- const srcOp = this.sessionOp(sessionId, 'source', source.id);
490
+ const srcOp = this.sessionOp(sessionId, 'source', source.id, identity);
416
491
  if (srcOp === 'exclude')
417
492
  return false;
418
493
  if (srcOp === 'include')
419
494
  return true;
420
495
  if (source.scopeRef.level === 'project') {
421
- const projOp = this.sessionOp(sessionId, 'project', source.scopeRef.key);
496
+ const projOp = this.sessionOp(sessionId, 'project', source.scopeRef.key, identity);
422
497
  if (projOp === 'exclude')
423
498
  return false;
424
499
  if (projOp === 'include')
@@ -426,25 +501,30 @@ export class KnowledgeSourceStore {
426
501
  }
427
502
  return base;
428
503
  }
429
- sessionOp(sessionId, targetType, targetId) {
504
+ sessionOp(sessionId, targetType, targetId, identity) {
430
505
  const row = this.db.raw
431
- .prepare('SELECT op FROM knowledge_session_visibility WHERE session_id = ? AND target_type = ? AND target_id = ?')
432
- .get(sessionId, targetType, targetId);
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);
433
510
  return row?.op ?? null;
434
511
  }
435
- isHidden(agentId, sourceId) {
512
+ isHidden(agentId, sourceId, identity) {
436
513
  const row = this.db.raw
437
- .prepare('SELECT 1 AS ok FROM knowledge_agent_hidden WHERE agent_id = ? AND source_id = ?')
438
- .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);
439
516
  return Boolean(row);
440
517
  }
441
- isProjectAssigned(projectKey, agentId) {
518
+ isProjectAssigned(projectKey, agentId, identity) {
442
519
  const row = this.db.raw
443
- .prepare('SELECT 1 AS ok FROM knowledge_project_agents WHERE project_key = ? AND agent_id = ?')
444
- .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);
445
522
  return Boolean(row);
446
523
  }
447
524
  // ── Catalog ──
525
+ /** catalog 规模/topics 派生缓存(Tier 0 每轮装配;短 TTL 足够) */
526
+ catalogExtrasCache = null;
527
+ static CATALOG_EXTRAS_TTL_MS = 2_000;
448
528
  /**
449
529
  * 生成 agent 可见 catalog(Tier 0)
450
530
  * 默认返回可见全集(display 截断在 KnowledgeLayer);maxEntries 仅作安全硬顶
@@ -459,10 +539,50 @@ export class KnowledgeSourceStore {
459
539
  return pb - pa;
460
540
  return a.displayName.localeCompare(b.displayName);
461
541
  });
542
+ const extras = this.catalogExtras();
462
543
  return sorted
463
544
  .filter((s) => !s.hiddenFromCatalog)
464
545
  .slice(0, max)
465
- .map((s) => toCatalogItem(s));
546
+ .map((s) => toCatalogItem(s, {
547
+ fileCount: extras.fileCounts.get(s.id),
548
+ topics: extras.topics.get(s.id),
549
+ }));
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
+ }
562
+ /** 每源已入库文件数(catalog 规模粗标用) */
563
+ fileCountsBySource() {
564
+ const rows = this.db.raw
565
+ .prepare('SELECT source_id AS id, COUNT(*) AS n FROM knowledge_memberships GROUP BY source_id')
566
+ .all();
567
+ return new Map(rows.map((r) => [r.id, r.n]));
568
+ }
569
+ /**
570
+ * 每源路径派生 topics(catalog 搜索线索)
571
+ *
572
+ * 只抽样前 N 条 path(按字典序),避免大库全表扫;topics 是粗线索不是精确索引。
573
+ */
574
+ topicsBySource() {
575
+ const sourceIds = this.db.raw
576
+ .prepare(`SELECT id FROM knowledge_sources WHERE status != 'removed'`)
577
+ .all().map((r) => r.id);
578
+ const out = new Map();
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));
584
+ }
585
+ return out;
466
586
  }
467
587
  /**
468
588
  * catalog 粗桶指纹(防每轮重渲)
@@ -470,7 +590,14 @@ export class KnowledgeSourceStore {
470
590
  catalogFingerprint(agentId, opts) {
471
591
  const items = this.catalogFor(agentId, { ...opts, maxEntries: 10_000 });
472
592
  const payload = items
473
- .map((i) => [i.id, i.displayName, i.status ?? '', i.description ?? '', i.scopeLevel ?? ''].join('|'))
593
+ .map((i) => [
594
+ i.id,
595
+ i.displayName,
596
+ i.status ?? '',
597
+ i.description ?? '',
598
+ i.scopeLevel ?? '',
599
+ (i.topics ?? []).join(','),
600
+ ].join('|'))
474
601
  .join('\n');
475
602
  return createHash('sha256').update(payload).digest('hex').slice(0, 16);
476
603
  }
@@ -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>;