@octopi-agent/engine 0.58.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.
Files changed (226) 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 +122 -1
  4. package/dist/config-schema/engine.js +4 -1
  5. package/dist/config-schema/substrate.d.ts +123 -0
  6. package/dist/config-schema/substrate.js +161 -0
  7. package/dist/config.d.ts +101 -1
  8. package/dist/harness/agent/builder.d.ts +15 -1
  9. package/dist/harness/agent/builder.js +41 -4
  10. package/dist/harness/agent/config-bridge.js +17 -4
  11. package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.d.ts +1 -1
  12. package/dist/harness/{context/capabilities → capabilities}/document/backends/docx.js +1 -1
  13. package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.d.ts +1 -1
  14. package/dist/harness/{context/capabilities → capabilities}/document/backends/office-parser.js +1 -1
  15. package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.d.ts +1 -1
  16. package/dist/harness/{context/capabilities → capabilities}/document/backends/pdf.js +13 -3
  17. package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.d.ts +1 -1
  18. package/dist/harness/{context/capabilities → capabilities}/document/backends/plain.js +13 -4
  19. package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.d.ts +1 -1
  20. package/dist/harness/{context/capabilities → capabilities}/document/backends/sheet.js +25 -5
  21. package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.d.ts +1 -1
  22. package/dist/harness/{context/capabilities → capabilities}/document/backends/xmind.js +1 -1
  23. package/dist/harness/{context/capabilities → capabilities}/document/errors.d.ts +1 -1
  24. package/dist/harness/{context/capabilities → capabilities}/document/errors.js +1 -1
  25. package/dist/harness/capabilities/document/factory.d.ts +33 -0
  26. package/dist/harness/capabilities/document/factory.js +37 -0
  27. package/dist/harness/{context/capabilities → capabilities}/document/format.d.ts +1 -1
  28. package/dist/harness/{context/capabilities → capabilities}/document/format.js +1 -1
  29. package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.d.ts +1 -1
  30. package/dist/harness/{context/capabilities → capabilities}/document/html-to-md.js +1 -1
  31. package/dist/harness/{context/capabilities → capabilities}/document/index.d.ts +3 -1
  32. package/dist/harness/{context/capabilities → capabilities}/document/index.js +2 -1
  33. package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.d.ts +3 -1
  34. package/dist/harness/{context/capabilities → capabilities}/document/legacy/factory.js +3 -1
  35. package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.d.ts +3 -1
  36. package/dist/harness/{context/capabilities → capabilities}/document/legacy/soffice.js +44 -1
  37. package/dist/harness/{context/capabilities → capabilities}/document/port.d.ts +1 -1
  38. package/dist/harness/{context/capabilities → capabilities}/document/port.js +4 -4
  39. package/dist/harness/{context/capabilities → capabilities}/document/types.d.ts +11 -1
  40. package/dist/harness/{context/capabilities → capabilities}/document/types.js +1 -1
  41. package/dist/harness/capabilities/document/worker-port.d.ts +13 -0
  42. package/dist/harness/capabilities/document/worker-port.js +78 -0
  43. package/dist/harness/context/capabilities/index.d.ts +2 -2
  44. package/dist/harness/context/capabilities/index.js +2 -2
  45. package/dist/harness/context/capabilities/summary/gate.js +2 -2
  46. package/dist/harness/context/capabilities/summary/policies.js +5 -5
  47. package/dist/harness/context/capabilities/summary/registry.js +29 -24
  48. package/dist/harness/context/layer-health.js +3 -2
  49. package/dist/harness/context/layers.d.ts +27 -13
  50. package/dist/harness/context/layers.js +76 -66
  51. package/dist/harness/context/system-prompt-assembler.d.ts +3 -0
  52. package/dist/harness/context/system-prompt-assembler.js +11 -1
  53. package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +4 -0
  54. package/dist/harness/extension/plugin-ecosystem/client-tools/index.js +3 -0
  55. package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.d.ts +56 -0
  56. package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.js +281 -0
  57. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +98 -0
  58. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +250 -0
  59. package/dist/harness/extension/plugin-ecosystem/client-tools/stream.d.ts +124 -0
  60. package/dist/harness/extension/plugin-ecosystem/client-tools/stream.js +372 -0
  61. package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +109 -0
  62. package/dist/harness/extension/plugin-ecosystem/client-tools/types.js +8 -0
  63. package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
  64. package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
  65. package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +1 -1
  66. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.d.ts +10 -0
  67. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +102 -5
  68. package/dist/harness/extension/plugin-ecosystem/tools/memory.d.ts +11 -1
  69. package/dist/harness/extension/plugin-ecosystem/tools/memory.js +14 -0
  70. package/dist/harness/index.d.ts +8 -5
  71. package/dist/harness/index.js +4 -2
  72. package/dist/harness/knowledge/adapters.d.ts +8 -5
  73. package/dist/harness/knowledge/adapters.js +53 -13
  74. package/dist/harness/knowledge/client.d.ts +197 -0
  75. package/dist/harness/knowledge/client.js +237 -0
  76. package/dist/harness/knowledge/connector-fetcher.d.ts +2 -2
  77. package/dist/harness/knowledge/connectors.d.ts +3 -3
  78. package/dist/harness/knowledge/connectors.js +14 -8
  79. package/dist/harness/knowledge/db.d.ts +40 -5
  80. package/dist/harness/knowledge/db.js +441 -113
  81. package/dist/harness/knowledge/document-extract-worker.js +55 -12
  82. package/dist/harness/knowledge/embed-runner.d.ts +53 -0
  83. package/dist/harness/knowledge/embed-runner.js +171 -0
  84. package/dist/harness/knowledge/engine-thread.d.ts +1 -0
  85. package/dist/harness/knowledge/engine-thread.js +302 -0
  86. package/dist/harness/knowledge/extract-document.d.ts +21 -5
  87. package/dist/harness/knowledge/extract-document.js +32 -8
  88. package/dist/harness/knowledge/fetchers.d.ts +14 -3
  89. package/dist/harness/knowledge/fetchers.js +52 -16
  90. package/dist/harness/knowledge/file-identity.d.ts +53 -0
  91. package/dist/harness/knowledge/file-identity.js +172 -0
  92. package/dist/harness/knowledge/file-index-store.d.ts +50 -0
  93. package/dist/harness/knowledge/file-index-store.js +132 -0
  94. package/dist/harness/knowledge/file-limits.d.ts +86 -0
  95. package/dist/harness/knowledge/file-limits.js +175 -0
  96. package/dist/harness/knowledge/fts.d.ts +60 -0
  97. package/dist/harness/knowledge/fts.js +218 -0
  98. package/dist/harness/knowledge/grounding.d.ts +3 -2
  99. package/dist/harness/knowledge/http-app.d.ts +50 -0
  100. package/dist/harness/knowledge/http-app.js +652 -0
  101. package/dist/harness/knowledge/http-bridge.d.ts +49 -0
  102. package/dist/harness/knowledge/http-bridge.js +17 -0
  103. package/dist/harness/knowledge/index-store.d.ts +158 -93
  104. package/dist/harness/knowledge/index-store.js +982 -441
  105. package/dist/harness/knowledge/index.d.ts +45 -5
  106. package/dist/harness/knowledge/index.js +23 -2
  107. package/dist/harness/knowledge/ingest.d.ts +262 -87
  108. package/dist/harness/knowledge/ingest.js +1072 -480
  109. package/dist/harness/knowledge/job-control-state.d.ts +29 -0
  110. package/dist/harness/knowledge/job-control-state.js +44 -0
  111. package/dist/harness/knowledge/job-control.d.ts +35 -0
  112. package/dist/harness/knowledge/job-control.js +86 -0
  113. package/dist/harness/knowledge/job-queue.d.ts +62 -0
  114. package/dist/harness/knowledge/job-queue.js +173 -0
  115. package/dist/harness/knowledge/knowledge-serve-child.d.ts +1 -0
  116. package/dist/harness/knowledge/knowledge-serve-child.js +100 -0
  117. package/dist/harness/knowledge/local-stack.d.ts +27 -0
  118. package/dist/harness/knowledge/local-stack.js +63 -0
  119. package/dist/harness/knowledge/membership-store.d.ts +65 -0
  120. package/dist/harness/knowledge/membership-store.js +198 -0
  121. package/dist/harness/knowledge/network-guard.d.ts +11 -2
  122. package/dist/harness/knowledge/network-guard.js +130 -85
  123. package/dist/harness/knowledge/parse-text-in-worker.d.ts +32 -0
  124. package/dist/harness/knowledge/parse-text-in-worker.js +158 -0
  125. package/dist/harness/knowledge/query-service.d.ts +272 -0
  126. package/dist/harness/knowledge/query-service.js +326 -0
  127. package/dist/harness/knowledge/query-worker-client.d.ts +96 -0
  128. package/dist/harness/knowledge/query-worker-client.js +207 -0
  129. package/dist/harness/knowledge/query-worker.d.ts +1 -0
  130. package/dist/harness/knowledge/query-worker.js +186 -0
  131. package/dist/harness/knowledge/read-http.d.ts +35 -0
  132. package/dist/harness/knowledge/read-http.js +373 -0
  133. package/dist/harness/knowledge/retriever.d.ts +22 -5
  134. package/dist/harness/knowledge/retriever.js +66 -35
  135. package/dist/harness/knowledge/secret-scan.d.ts +13 -2
  136. package/dist/harness/knowledge/secret-scan.js +25 -2
  137. package/dist/harness/knowledge/serve.d.ts +48 -0
  138. package/dist/harness/knowledge/serve.js +336 -0
  139. package/dist/harness/knowledge/source-store.d.ts +97 -19
  140. package/dist/harness/knowledge/source-store.js +166 -94
  141. package/dist/harness/knowledge/start-service-process.d.ts +49 -0
  142. package/dist/harness/knowledge/start-service-process.js +158 -0
  143. package/dist/harness/knowledge/text-parse-worker.d.ts +1 -0
  144. package/dist/harness/knowledge/text-parse-worker.js +54 -0
  145. package/dist/harness/knowledge/types.d.ts +1 -1
  146. package/dist/harness/knowledge/types.js +1 -1
  147. package/dist/harness/knowledge/vector-ann.d.ts +42 -0
  148. package/dist/harness/knowledge/vector-ann.js +68 -0
  149. package/dist/harness/knowledge/writer-lock.d.ts +13 -0
  150. package/dist/harness/knowledge/writer-lock.js +71 -0
  151. package/dist/harness/knowledge/writer-service.d.ts +194 -0
  152. package/dist/harness/knowledge/writer-service.js +355 -0
  153. package/dist/harness/knowledge/writer-worker-client.d.ts +102 -0
  154. package/dist/harness/knowledge/writer-worker-client.js +381 -0
  155. package/dist/harness/knowledge/writer-worker.d.ts +1 -0
  156. package/dist/harness/knowledge/writer-worker.js +189 -0
  157. package/dist/harness/memory/cognition-decay.d.ts +36 -0
  158. package/dist/harness/memory/cognition-decay.js +55 -0
  159. package/dist/harness/memory/cognition-gates.d.ts +60 -0
  160. package/dist/harness/memory/cognition-gates.js +191 -0
  161. package/dist/harness/memory/cognition-trigger.d.ts +30 -0
  162. package/dist/harness/memory/cognition-trigger.js +41 -0
  163. package/dist/harness/memory/cognition-types.d.ts +1 -1
  164. package/dist/harness/memory/cognition.d.ts +40 -8
  165. package/dist/harness/memory/cognition.js +432 -79
  166. package/dist/harness/memory/conceptualizer.d.ts +66 -0
  167. package/dist/harness/memory/conceptualizer.js +246 -0
  168. package/dist/harness/memory/index.d.ts +16 -2
  169. package/dist/harness/memory/index.js +11 -1
  170. package/dist/harness/memory/sqlite/agent-db.d.ts +9 -0
  171. package/dist/harness/memory/sqlite/agent-db.js +118 -15
  172. package/dist/harness/memory/sqlite/cognition-store.d.ts +55 -16
  173. package/dist/harness/memory/sqlite/cognition-store.js +591 -144
  174. package/dist/harness/memory/sqlite/embedding.d.ts +5 -2
  175. package/dist/harness/memory/sqlite/embedding.js +109 -16
  176. package/dist/harness/memory/sqlite/keyword-search.d.ts +21 -0
  177. package/dist/harness/memory/sqlite/keyword-search.js +64 -0
  178. package/dist/harness/memory/sqlite/wisdom-store.d.ts +33 -14
  179. package/dist/harness/memory/sqlite/wisdom-store.js +257 -44
  180. package/dist/harness/memory/types.d.ts +331 -16
  181. package/dist/harness/memory/types.js +49 -0
  182. package/dist/harness/memory/wisdom-formation.d.ts +101 -0
  183. package/dist/harness/memory/wisdom-formation.js +304 -0
  184. package/dist/harness/memory/wisdom-gates.d.ts +50 -0
  185. package/dist/harness/memory/wisdom-gates.js +204 -0
  186. package/dist/harness/memory/wisdom-policy.d.ts +107 -0
  187. package/dist/harness/memory/wisdom-policy.js +220 -0
  188. package/dist/harness/memory/wisdom-types.d.ts +3 -2
  189. package/dist/harness/memory/wisdom-types.js +2 -2
  190. package/dist/harness/memory/wisdom.d.ts +27 -0
  191. package/dist/harness/memory/wisdom.js +181 -0
  192. package/dist/harness/run/agent/agent.d.ts +9 -0
  193. package/dist/harness/run/agent/agent.js +19 -0
  194. package/dist/harness/run/reliability/run-agent.js +6 -2
  195. package/dist/harness/run/runner.js +2 -1
  196. package/dist/harness/session/attachments/service.d.ts +1 -1
  197. package/dist/harness/session/attachments/service.js +1 -1
  198. package/dist/harness/session/index.d.ts +1 -0
  199. package/dist/harness/session/index.js +1 -0
  200. package/dist/harness/session/title.d.ts +86 -0
  201. package/dist/harness/session/title.js +268 -0
  202. package/dist/harness/shared/events/agent-event-map.d.ts +8 -0
  203. package/dist/harness/shared/events/agent-event-map.js +1 -0
  204. package/dist/index.d.ts +7 -3
  205. package/dist/index.js +3 -1
  206. package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -0
  207. package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -0
  208. package/dist/subsystems/cognition-steward/conceptualize/handler.d.ts +9 -0
  209. package/dist/subsystems/cognition-steward/conceptualize/handler.js +125 -0
  210. package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -0
  211. package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -0
  212. package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +12 -0
  213. package/dist/subsystems/cognition-steward/consolidate/handler.js +93 -0
  214. package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -0
  215. package/dist/subsystems/wisdom-steward/evaluate/handler.d.ts +18 -0
  216. package/dist/subsystems/wisdom-steward/evaluate/handler.js +91 -0
  217. package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -0
  218. package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -0
  219. package/dist/subsystems/wisdom-steward/formation/handler.d.ts +18 -0
  220. package/dist/subsystems/wisdom-steward/formation/handler.js +212 -0
  221. package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -0
  222. package/dist/subsystems/wisdom-steward/govern/handler.d.ts +17 -0
  223. package/dist/subsystems/wisdom-steward/govern/handler.js +68 -0
  224. package/dist/worker-path.d.ts +8 -0
  225. package/dist/worker-path.js +15 -0
  226. package/package.json +2 -2
@@ -149,6 +149,8 @@ export declare class AgentBuilder {
149
149
  private _knowledgeIndexStore?;
150
150
  private _knowledgeSourceStore?;
151
151
  private _knowledgeHitLog?;
152
+ /** 独立 Service 路径:仅 grounding(工具走 client tools) */
153
+ private _knowledgeGroundingPort?;
152
154
  private _knowledgeGrounding?;
153
155
  /** 智慧(注入 system prompt WisdomLayer) */
154
156
  private _wisdomStore?;
@@ -287,9 +289,21 @@ export declare class AgentBuilder {
287
289
  includePriorUserTurns?: number;
288
290
  };
289
291
  }): this;
292
+ /**
293
+ * 仅注入 turn 级 grounding(Knowledge Service client 路径)。
294
+ * 工具面请另用 createKnowledgeClientTools;本地 store 路径请用 knowledgeRetriever。
295
+ */
296
+ knowledgeGrounding(port: import('../knowledge/retriever.js').AutoGroundPort, opts?: {
297
+ budgetTokens?: number;
298
+ budgetRatio?: number;
299
+ maxBudgetTokens?: number;
300
+ maxChunks?: number;
301
+ skipIfUserTokensBelow?: number;
302
+ includePriorUserTurns?: number;
303
+ }): this;
290
304
  /** 注入 WisdomStore(半静态思维范式,进 system prompt WisdomLayer) */
291
305
  wisdomStore(store: import('../memory/types.js').WisdomStore): this;
292
- /** 注入 ConceptGraphStore(按 query 召回概念边,进 system prompt CognitionLayer) */
306
+ /** 注入 ConceptGraphStore(spreadingActivate 召回概念子图,进 system prompt CognitionLayer) */
293
307
  cognitionStore(store: import('../memory/types.js').ConceptGraphStore): this;
294
308
  /**
295
309
  * system prompt 装配器调参
@@ -317,6 +317,8 @@ export class AgentBuilder {
317
317
  _knowledgeIndexStore;
318
318
  _knowledgeSourceStore;
319
319
  _knowledgeHitLog;
320
+ /** 独立 Service 路径:仅 grounding(工具走 client tools) */
321
+ _knowledgeGroundingPort;
320
322
  _knowledgeGrounding;
321
323
  /** 智慧(注入 system prompt WisdomLayer) */
322
324
  _wisdomStore;
@@ -522,12 +524,21 @@ export class AgentBuilder {
522
524
  this._knowledgeGrounding = deps.grounding;
523
525
  return this;
524
526
  }
527
+ /**
528
+ * 仅注入 turn 级 grounding(Knowledge Service client 路径)。
529
+ * 工具面请另用 createKnowledgeClientTools;本地 store 路径请用 knowledgeRetriever。
530
+ */
531
+ knowledgeGrounding(port, opts) {
532
+ this._knowledgeGroundingPort = port;
533
+ this._knowledgeGrounding = opts;
534
+ return this;
535
+ }
525
536
  /** 注入 WisdomStore(半静态思维范式,进 system prompt WisdomLayer) */
526
537
  wisdomStore(store) {
527
538
  this._wisdomStore = store;
528
539
  return this;
529
540
  }
530
- /** 注入 ConceptGraphStore(按 query 召回概念边,进 system prompt CognitionLayer) */
541
+ /** 注入 ConceptGraphStore(spreadingActivate 召回概念子图,进 system prompt CognitionLayer) */
531
542
  cognitionStore(store) {
532
543
  this._cognitionStore = store;
533
544
  return this;
@@ -774,6 +785,25 @@ export class AgentBuilder {
774
785
  gates: memCfg?.gates?.maxLength
775
786
  ? { maxLength: memCfg.gates.maxLength }
776
787
  : undefined,
788
+ onStored: this._cognitionStore && events
789
+ ? (info) => {
790
+ void import('../memory/cognition-trigger.js')
791
+ .then(({ emitConceptualizeRequest }) => {
792
+ emitConceptualizeRequest(events, {
793
+ memoryId: info.id,
794
+ proposition: info.proposition,
795
+ evidence: info.evidence,
796
+ memoryType: info.type,
797
+ memoryStatus: info.status,
798
+ channel: info.channel,
799
+ sessionId: info.sessionId,
800
+ });
801
+ })
802
+ .catch(() => {
803
+ // 动态加载失败不阻断 memory 写
804
+ });
805
+ }
806
+ : undefined,
777
807
  })) {
778
808
  this._toolBus.register(tool);
779
809
  }
@@ -873,10 +903,11 @@ export class AgentBuilder {
873
903
  });
874
904
  runner.setSystemPromptAssembler((input) => systemPromptAssembler.assemble(input), (sid) => systemPromptAssembler.clearSession(sid));
875
905
  // turn 级 Knowledge grounding(消息插槽 knowledgeGrounding;不进 system)
876
- if (this._knowledgeRetriever) {
906
+ const groundingPort = this._knowledgeGroundingPort ?? this._knowledgeRetriever;
907
+ if (groundingPort) {
877
908
  const { GroundingAssembler } = await import('../knowledge/grounding.js');
878
909
  const grounding = new GroundingAssembler({
879
- retriever: this._knowledgeRetriever,
910
+ retriever: groundingPort,
880
911
  hitLog: this._knowledgeHitLog,
881
912
  budgetTokens: this._knowledgeGrounding?.budgetTokens,
882
913
  budgetRatio: this._knowledgeGrounding?.budgetRatio,
@@ -962,10 +993,16 @@ export class AgentBuilder {
962
993
  tryRegister(spec);
963
994
  }
964
995
  }
965
- // 依赖注入:memoryStore / sessionStore / constitution / backfillCoverage
996
+ // 依赖注入:memoryStore / wisdomStore / conceptGraphStore / sessionStore / constitution / backfillCoverage
966
997
  if (this._memoryStore) {
967
998
  subsystemRuntime.registerDependency('memoryStore', this._memoryStore);
968
999
  }
1000
+ if (this._wisdomStore) {
1001
+ subsystemRuntime.registerDependency('wisdomStore', this._wisdomStore);
1002
+ }
1003
+ if (this._cognitionStore) {
1004
+ subsystemRuntime.registerDependency('conceptGraphStore', this._cognitionStore);
1005
+ }
969
1006
  {
970
1007
  const { InMemoryBackfillCoverageStore } = await import('../memory/backfill-coverage.js');
971
1008
  let coverage = this._backfillCoverage;
@@ -196,6 +196,8 @@ export async function buildFromConfig(config) {
196
196
  contextAssemblerConfig,
197
197
  constitutionConfig,
198
198
  memoryConfig: config.memory,
199
+ cognitionConfig: config.cognition,
200
+ wisdomConfig: config.wisdom,
199
201
  flatModels,
200
202
  levelMap: config.levelMap,
201
203
  modelsConfig: config.models,
@@ -294,10 +296,21 @@ async function buildAgent(agentConfig, shared) {
294
296
  similarityWeight: shared.memoryConfig?.retrieval?.similarityWeight,
295
297
  minKeywordScore: shared.memoryConfig?.retrieval?.minKeywordScore,
296
298
  }));
297
- builder.wisdomStore(new SqliteWisdomStore(db));
298
- builder.cognitionStore(new SqliteConceptGraph(db, {
299
- embeddingProvider: embRuntime?.provider ?? null,
300
- }));
299
+ const wis = shared.wisdomConfig;
300
+ if (wis?.enabled !== false) {
301
+ builder.wisdomStore(new SqliteWisdomStore(db, { hardCap: wis?.capacity?.hardCap }));
302
+ }
303
+ const cog = shared.cognitionConfig;
304
+ if (cog?.enabled !== false) {
305
+ builder.cognitionStore(new SqliteConceptGraph(db, {
306
+ embeddingProvider: embRuntime?.provider ?? null,
307
+ mergeThreshold: cog?.identity?.mergeThreshold,
308
+ candidateBandHi: cog?.identity?.candidateBandHi,
309
+ stopNodes: cog?.capacity?.stopNodes,
310
+ stopEdges: cog?.capacity?.stopEdges,
311
+ causesMinIndependentMemories: cog?.relations?.causesMinIndependentMemories,
312
+ }));
313
+ }
301
314
  // Knowledge catalog / 索引归独立 Knowledge 服务(arch/knowledge-layer.md);P1 前不挂
302
315
  }
303
316
  catch (err) {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * DOCX 后端 — mammoth(可选依赖,T0)→ HTML → Markdown
3
3
  *
4
- * @module harness/context/capabilities/document/backends/docx
4
+ * @module harness/capabilities/document/backends/docx
5
5
  */
6
6
  import type { DocumentExtractBackend } from '../types.js';
7
7
  export declare const docxMammothBackend: DocumentExtractBackend;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * DOCX 后端 — mammoth(可选依赖,T0)→ HTML → Markdown
3
3
  *
4
- * @module harness/context/capabilities/document/backends/docx
4
+ * @module harness/capabilities/document/backends/docx
5
5
  */
6
6
  import { DocumentExtractError } from '../errors.js';
7
7
  import { htmlFragmentToMarkdown } from '../html-to-md.js';
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PPTX / 长尾后端 — officeparser(可选依赖,T0 长尾)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/office-parser
4
+ * @module harness/capabilities/document/backends/office-parser
5
5
  */
6
6
  import type { DocumentExtractBackend } from '../types.js';
7
7
  export declare const officeParserBackend: DocumentExtractBackend;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PPTX / 长尾后端 — officeparser(可选依赖,T0 长尾)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/office-parser
4
+ * @module harness/capabilities/document/backends/office-parser
5
5
  */
6
6
  import { DocumentExtractError } from '../errors.js';
7
7
  let cached;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PDF 后端 — unpdf(可选依赖,T0)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/pdf
4
+ * @module harness/capabilities/document/backends/pdf
5
5
  */
6
6
  import type { DocumentExtractBackend } from '../types.js';
7
7
  export declare const pdfUnpdfBackend: DocumentExtractBackend;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * PDF 后端 — unpdf(可选依赖,T0)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/pdf
4
+ * @module harness/capabilities/document/backends/pdf
5
5
  */
6
6
  import { DocumentExtractError } from '../errors.js';
7
7
  let cached;
@@ -51,7 +51,8 @@ export const pdfUnpdfBackend = {
51
51
  const pdf = await unpdf.getDocumentProxy(source.data, {
52
52
  ...(source.password ? { password: source.password } : {}),
53
53
  });
54
- const { totalPages, text } = await unpdf.extractText(pdf, { mergePages: true });
54
+ // mergePages:false → 按页数组,便于 maxPages 截断
55
+ const { totalPages, text } = await unpdf.extractText(pdf, { mergePages: false });
55
56
  let title;
56
57
  let author;
57
58
  try {
@@ -62,7 +63,16 @@ export const pdfUnpdfBackend = {
62
63
  catch {
63
64
  // metadata is optional; extraction result remains usable
64
65
  }
65
- const markdown = Array.isArray(text) ? text.join('\n\n') : text;
66
+ const pages = Array.isArray(text) ? text : [text];
67
+ const maxPages = options.maxPages && options.maxPages > 0 ? options.maxPages : pages.length;
68
+ const usedPages = pages.slice(0, Math.min(maxPages, pages.length));
69
+ if (usedPages.length < totalPages) {
70
+ warnings.push({
71
+ code: 'PARTIAL_EXTRACT',
72
+ message: `pages truncated: ${usedPages.length}/${totalPages} (maxPages=${options.maxPages})`,
73
+ });
74
+ }
75
+ const markdown = usedPages.join('\n\n');
66
76
  return {
67
77
  markdown,
68
78
  meta: {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 纯文本 / Markdown / CSV 后端(无外部依赖)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/plain
4
+ * @module harness/capabilities/document/backends/plain
5
5
  */
6
6
  import type { DocumentExtractBackend } from '../types.js';
7
7
  export declare const plainTextBackend: DocumentExtractBackend;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 纯文本 / Markdown / CSV 后端(无外部依赖)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/plain
4
+ * @module harness/capabilities/document/backends/plain
5
5
  */
6
6
  function decodeUtf8(data) {
7
7
  return new TextDecoder('utf-8', { fatal: false }).decode(data);
@@ -24,18 +24,27 @@ export const plainTextBackend = {
24
24
  accepts({ format }) {
25
25
  return format === 'txt' || format === 'md' || format === 'csv' || format === 'html';
26
26
  },
27
- async extract(source, _options) {
27
+ async extract(source, options) {
28
28
  if (looksLikeBinary(source.data)) {
29
29
  const { DocumentExtractError } = await import('../errors.js');
30
30
  throw new DocumentExtractError('INVALID_SOURCE', 'plain-text backend received binary data');
31
31
  }
32
- const text = decodeUtf8(source.data);
32
+ let text = decodeUtf8(source.data);
33
+ const warnings = [];
34
+ const maxChars = options.maxTextChars && options.maxTextChars > 0 ? options.maxTextChars : 0;
35
+ if (maxChars > 0 && text.length > maxChars) {
36
+ text = `${text.slice(0, maxChars)}\n\n…[truncated: ${text.length - maxChars} chars omitted]`;
37
+ warnings.push({
38
+ code: 'PARTIAL_EXTRACT',
39
+ message: `text truncated to maxTextChars=${maxChars}`,
40
+ });
41
+ }
33
42
  const format = source.formatHint ?? 'txt';
34
43
  // html 原样保留标签交给 knowledge htmlAdapter;此处仅保证可读文本
35
44
  return {
36
45
  markdown: text,
37
46
  meta: { format },
38
- warnings: [],
47
+ warnings,
39
48
  backend: 'plain-text',
40
49
  };
41
50
  },
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 电子表格后端 — SheetJS `xlsx`(可选依赖,T0;覆盖 xlsx + xls)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/sheet
4
+ * @module harness/capabilities/document/backends/sheet
5
5
  */
6
6
  import type { DocumentExtractBackend } from '../types.js';
7
7
  export declare const sheetXlsxBackend: DocumentExtractBackend;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 电子表格后端 — SheetJS `xlsx`(可选依赖,T0;覆盖 xlsx + xls)
3
3
  *
4
- * @module harness/context/capabilities/document/backends/sheet
4
+ * @module harness/capabilities/document/backends/sheet
5
5
  */
6
6
  import { DocumentExtractError } from '../errors.js';
7
7
  let cached;
@@ -57,7 +57,7 @@ export const sheetXlsxBackend = {
57
57
  accepts({ format }) {
58
58
  return format === 'xlsx' || format === 'xls';
59
59
  },
60
- async extract(source, _options) {
60
+ async extract(source, options) {
61
61
  const xlsx = await loadXlsx();
62
62
  if (!xlsx) {
63
63
  throw new DocumentExtractError('BACKEND_UNAVAILABLE', 'xlsx (SheetJS) is not installed', [
@@ -72,18 +72,38 @@ export const sheetXlsxBackend = {
72
72
  const msg = err instanceof Error ? err.message : String(err);
73
73
  throw new DocumentExtractError('INVALID_SOURCE', `Spreadsheet extract failed: ${msg}`);
74
74
  }
75
+ const warnings = [];
76
+ const maxSheets = options.maxSheets && options.maxSheets > 0 ? options.maxSheets : Infinity;
77
+ const maxRows = options.maxRowsPerSheet && options.maxRowsPerSheet > 0
78
+ ? options.maxRowsPerSheet
79
+ : Infinity;
80
+ const names = wb.SheetNames;
81
+ const usedNames = names.slice(0, maxSheets === Infinity ? names.length : maxSheets);
82
+ if (usedNames.length < names.length) {
83
+ warnings.push({
84
+ code: 'PARTIAL_EXTRACT',
85
+ message: `sheets truncated: ${usedNames.length}/${names.length} (maxSheets=${options.maxSheets})`,
86
+ });
87
+ }
75
88
  const parts = [];
76
- for (const name of wb.SheetNames) {
89
+ for (const name of usedNames) {
77
90
  const sheet = wb.Sheets[name];
78
91
  const rows = xlsx.utils.sheet_to_json(sheet, { header: 1, raw: false, defval: '' });
79
- parts.push(`## ${name}\n\n${sheetToMarkdown(rows)}`);
92
+ const usedRows = maxRows === Infinity || rows.length <= maxRows ? rows : rows.slice(0, maxRows);
93
+ if (usedRows.length < rows.length) {
94
+ warnings.push({
95
+ code: 'PARTIAL_EXTRACT',
96
+ message: `sheet "${name}" rows truncated: ${usedRows.length}/${rows.length} (maxRowsPerSheet=${options.maxRowsPerSheet})`,
97
+ });
98
+ }
99
+ parts.push(`## ${name}\n\n${sheetToMarkdown(usedRows)}`);
80
100
  }
81
101
  return {
82
102
  markdown: parts.join('\n\n'),
83
103
  meta: {
84
104
  format: source.formatHint === 'xls' ? 'xls' : 'xlsx',
85
105
  },
86
- warnings: [],
106
+ warnings,
87
107
  backend: 'sheet-xlsx',
88
108
  };
89
109
  },
@@ -4,7 +4,7 @@
4
4
  * XMind 2020+:content.json 树(children.attached)
5
5
  * XMind 8:content.xml topic 树(尽力解析)
6
6
  *
7
- * @module harness/context/capabilities/document/backends/xmind
7
+ * @module harness/capabilities/document/backends/xmind
8
8
  */
9
9
  import type { DocumentExtractBackend } from '../types.js';
10
10
  /** content.json(XMind 2020+)→ Markdown 大纲 */
@@ -4,7 +4,7 @@
4
4
  * XMind 2020+:content.json 树(children.attached)
5
5
  * XMind 8:content.xml topic 树(尽力解析)
6
6
  *
7
- * @module harness/context/capabilities/document/backends/xmind
7
+ * @module harness/capabilities/document/backends/xmind
8
8
  */
9
9
  import { DocumentExtractError } from '../errors.js';
10
10
  let fflateCache;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Document 抽取结构化错误
3
3
  *
4
- * @module harness/context/capabilities/document/errors
4
+ * @module harness/capabilities/document/errors
5
5
  */
6
6
  import type { ExtractErrorCode } from './types.js';
7
7
  /**
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Document 抽取结构化错误
3
3
  *
4
- * @module harness/context/capabilities/document/errors
4
+ * @module harness/capabilities/document/errors
5
5
  */
6
6
  /**
7
7
  * DocumentExtractError — 调用方可按 code 分支,勿匹配 message 文本
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Document 能力统一装配 — Gateway / Knowledge 共用同一配置源
3
+ *
4
+ * 禁止调用方各自 `createDefaultDocumentPort` 再拼一份默认值:
5
+ * 那样 soffice/超时/限额会分叉(Knowledge worker 曾因此整批 no_adapter)。
6
+ *
7
+ * @module harness/capabilities/document/factory
8
+ */
9
+ import type { DocumentPort } from './types.js';
10
+ /** 根配置 `documents.*`(DocumentsConfigSchema) */
11
+ export interface DocumentCapabilityConfig {
12
+ extract?: {
13
+ enabled?: boolean;
14
+ timeoutMs?: number;
15
+ maxFileBytes?: number;
16
+ allowedRoots?: string[];
17
+ };
18
+ legacy?: {
19
+ converter?: 'none' | 'soffice' | 'remote' | null;
20
+ sofficePath?: string | null;
21
+ timeoutMs?: number;
22
+ cacheDir?: string | null;
23
+ cacheMaxBytes?: number;
24
+ maxInputBytes?: number;
25
+ };
26
+ }
27
+ /**
28
+ * 从 `documents.*` 配置创建 DocumentPort(唯一工厂)。
29
+ *
30
+ * @param cfg - 根配置 documents 段;undefined = 默认 T0 后端、无 legacy 转换
31
+ * @returns DocumentPort
32
+ */
33
+ export declare function createDocumentPortFromConfig(cfg?: DocumentCapabilityConfig | null): DocumentPort;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Document 能力统一装配 — Gateway / Knowledge 共用同一配置源
3
+ *
4
+ * 禁止调用方各自 `createDefaultDocumentPort` 再拼一份默认值:
5
+ * 那样 soffice/超时/限额会分叉(Knowledge worker 曾因此整批 no_adapter)。
6
+ *
7
+ * @module harness/capabilities/document/factory
8
+ */
9
+ import { createLegacyConverterFromConfig } from './legacy/factory.js';
10
+ import { createDefaultDocumentPort } from './port.js';
11
+ /**
12
+ * 从 `documents.*` 配置创建 DocumentPort(唯一工厂)。
13
+ *
14
+ * @param cfg - 根配置 documents 段;undefined = 默认 T0 后端、无 legacy 转换
15
+ * @returns DocumentPort
16
+ */
17
+ export function createDocumentPortFromConfig(cfg) {
18
+ const extract = cfg?.extract;
19
+ const config = {
20
+ enabled: extract?.enabled !== false,
21
+ ...(extract?.timeoutMs != null ? { timeoutMs: extract.timeoutMs } : {}),
22
+ ...(extract?.maxFileBytes != null ? { maxFileBytes: extract.maxFileBytes } : {}),
23
+ ...(extract?.allowedRoots?.length ? { allowedRoots: extract.allowedRoots } : {}),
24
+ };
25
+ const legacyConverter = createLegacyConverterFromConfig({
26
+ converter: cfg?.legacy?.converter ?? 'none',
27
+ sofficePath: cfg?.legacy?.sofficePath,
28
+ timeoutMs: cfg?.legacy?.timeoutMs,
29
+ cacheDir: cfg?.legacy?.cacheDir,
30
+ cacheMaxBytes: cfg?.legacy?.cacheMaxBytes,
31
+ maxInputBytes: cfg?.legacy?.maxInputBytes,
32
+ });
33
+ return createDefaultDocumentPort({
34
+ config,
35
+ ...(legacyConverter ? { legacyConverter } : {}),
36
+ });
37
+ }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 格式嗅探 — 扩展名 + magic bytes
3
3
  *
4
- * @module harness/context/capabilities/document/format
4
+ * @module harness/capabilities/document/format
5
5
  */
6
6
  import type { DocumentFormatHint, ExtractSource } from './types.js';
7
7
  /** 二进制老格式(需转换闸门) */
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * 格式嗅探 — 扩展名 + magic bytes
3
3
  *
4
- * @module harness/context/capabilities/document/format
4
+ * @module harness/capabilities/document/format
5
5
  */
6
6
  import { extname } from 'node:path';
7
7
  const EXT_MAP = {
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * 只覆盖 mammoth 常见语义标签,不引入 turndown 依赖。
5
5
  *
6
- * @module harness/context/capabilities/document/html-to-md
6
+ * @module harness/capabilities/document/html-to-md
7
7
  */
8
8
  /**
9
9
  * 将 mammoth 语义 HTML 片段转为 Markdown
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * 只覆盖 mammoth 常见语义标签,不引入 turndown 依赖。
5
5
  *
6
- * @module harness/context/capabilities/document/html-to-md
6
+ * @module harness/capabilities/document/html-to-md
7
7
  */
8
8
  /**
9
9
  * 将 mammoth 语义 HTML 片段转为 Markdown
@@ -3,13 +3,15 @@
3
3
  *
4
4
  * 读路径:二进制/文档 → Markdown;写路径不在此层。
5
5
  *
6
- * @module harness/context/capabilities/document
6
+ * @module harness/capabilities/document
7
7
  */
8
8
  export * from './types.js';
9
9
  export { DocumentExtractError, isDocumentExtractError } from './errors.js';
10
10
  export { DOCUMENT_EXTENSIONS, LEGACY_FORMATS, formatFromMagic, formatFromName, isDocumentPath, resolveFormat, } from './format.js';
11
11
  export { htmlFragmentToMarkdown } from './html-to-md.js';
12
12
  export { createDefaultBackends, createDefaultDocumentPort } from './port.js';
13
+ export { createDocumentPortFromConfig } from './factory.js';
14
+ export type { DocumentCapabilityConfig } from './factory.js';
13
15
  export { plainTextBackend } from './backends/plain.js';
14
16
  export { pdfUnpdfBackend } from './backends/pdf.js';
15
17
  export { docxMammothBackend } from './backends/docx.js';
@@ -3,13 +3,14 @@
3
3
  *
4
4
  * 读路径:二进制/文档 → Markdown;写路径不在此层。
5
5
  *
6
- * @module harness/context/capabilities/document
6
+ * @module harness/capabilities/document
7
7
  */
8
8
  export * from './types.js';
9
9
  export { DocumentExtractError, isDocumentExtractError } from './errors.js';
10
10
  export { DOCUMENT_EXTENSIONS, LEGACY_FORMATS, formatFromMagic, formatFromName, isDocumentPath, resolveFormat, } from './format.js';
11
11
  export { htmlFragmentToMarkdown } from './html-to-md.js';
12
12
  export { createDefaultBackends, createDefaultDocumentPort } from './port.js';
13
+ export { createDocumentPortFromConfig } from './factory.js';
13
14
  export { plainTextBackend } from './backends/plain.js';
14
15
  export { pdfUnpdfBackend } from './backends/pdf.js';
15
16
  export { docxMammothBackend } from './backends/docx.js';
@@ -2,7 +2,7 @@ import type { LegacyConverter } from '../types.js';
2
2
  /**
3
3
  * 按配置创建 legacy 转换器;converter=none 返回 null
4
4
  *
5
- * @param options - converter / sofficePath / timeoutMs
5
+ * @param options - converter / sofficePath / timeoutMs / cacheDir / cacheMaxBytes / maxInputBytes
6
6
  * @returns LegacyConverter | null
7
7
  */
8
8
  export declare function createLegacyConverterFromConfig(options: {
@@ -10,4 +10,6 @@ export declare function createLegacyConverterFromConfig(options: {
10
10
  sofficePath?: string | null;
11
11
  timeoutMs?: number;
12
12
  cacheDir?: string | null;
13
+ cacheMaxBytes?: number;
14
+ maxInputBytes?: number;
13
15
  }): LegacyConverter | null;
@@ -5,7 +5,7 @@ import { createSofficeLegacyConverter } from './soffice.js';
5
5
  /**
6
6
  * 按配置创建 legacy 转换器;converter=none 返回 null
7
7
  *
8
- * @param options - converter / sofficePath / timeoutMs
8
+ * @param options - converter / sofficePath / timeoutMs / cacheDir / cacheMaxBytes / maxInputBytes
9
9
  * @returns LegacyConverter | null
10
10
  */
11
11
  export function createLegacyConverterFromConfig(options) {
@@ -16,6 +16,8 @@ export function createLegacyConverterFromConfig(options) {
16
16
  sofficePath: options.sofficePath,
17
17
  timeoutMs: options.timeoutMs,
18
18
  cacheDir: options.cacheDir,
19
+ cacheMaxBytes: options.cacheMaxBytes,
20
+ maxInputBytes: options.maxInputBytes,
19
21
  });
20
22
  }
21
23
  // remote(MinerU/WPS/云转换)后续接入
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * 将 .doc/.xls/.ppt 转为 OOXML 后走 T0 抽取;不常驻、用完即退。
5
5
  *
6
- * @module harness/context/capabilities/document/legacy/soffice
6
+ * @module harness/capabilities/document/legacy/soffice
7
7
  */
8
8
  import type { LegacyConverter } from '../types.js';
9
9
  export interface SofficeLegacyOptions {
@@ -15,6 +15,8 @@ export interface SofficeLegacyOptions {
15
15
  maxInputBytes?: number;
16
16
  /** 转换件缓存目录;缺省不缓存 */
17
17
  cacheDir?: string | null;
18
+ /** 缓存目录总量上限;超出按 mtime 淘汰(默认 256MB) */
19
+ cacheMaxBytes?: number;
18
20
  }
19
21
  /**
20
22
  * 创建 soffice LegacyConverter