@octopi-agent/engine 0.55.0 → 0.58.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 (105) hide show
  1. package/dist/config-schema/context.d.ts +38 -0
  2. package/dist/config-schema/context.js +39 -0
  3. package/dist/config-schema/engine.d.ts +42 -0
  4. package/dist/config-schema/engine.js +3 -1
  5. package/dist/config-schema/substrate.d.ts +5 -0
  6. package/dist/config-schema/substrate.js +11 -0
  7. package/dist/config.d.ts +44 -0
  8. package/dist/harness/agent/config-bridge.js +3 -0
  9. package/dist/harness/context/capabilities/document/backends/docx.d.ts +7 -0
  10. package/dist/harness/context/capabilities/document/backends/docx.js +67 -0
  11. package/dist/harness/context/capabilities/document/backends/office-parser.d.ts +7 -0
  12. package/dist/harness/context/capabilities/document/backends/office-parser.js +76 -0
  13. package/dist/harness/context/capabilities/document/backends/pdf.d.ts +7 -0
  14. package/dist/harness/context/capabilities/document/backends/pdf.js +86 -0
  15. package/dist/harness/context/capabilities/document/backends/plain.d.ts +7 -0
  16. package/dist/harness/context/capabilities/document/backends/plain.js +42 -0
  17. package/dist/harness/context/capabilities/document/backends/sheet.d.ts +7 -0
  18. package/dist/harness/context/capabilities/document/backends/sheet.js +90 -0
  19. package/dist/harness/context/capabilities/document/backends/xmind.d.ts +20 -0
  20. package/dist/harness/context/capabilities/document/backends/xmind.js +198 -0
  21. package/dist/harness/context/capabilities/document/errors.d.ts +27 -0
  22. package/dist/harness/context/capabilities/document/errors.js +33 -0
  23. package/dist/harness/context/capabilities/document/format.d.ts +38 -0
  24. package/dist/harness/context/capabilities/document/format.js +113 -0
  25. package/dist/harness/context/capabilities/document/html-to-md.d.ts +14 -0
  26. package/dist/harness/context/capabilities/document/html-to-md.js +97 -0
  27. package/dist/harness/context/capabilities/document/index.d.ts +21 -0
  28. package/dist/harness/context/capabilities/document/index.js +20 -0
  29. package/dist/harness/context/capabilities/document/legacy/factory.d.ts +13 -0
  30. package/dist/harness/context/capabilities/document/legacy/factory.js +23 -0
  31. package/dist/harness/context/capabilities/document/legacy/soffice.d.ts +32 -0
  32. package/dist/harness/context/capabilities/document/legacy/soffice.js +186 -0
  33. package/dist/harness/context/capabilities/document/port.d.ts +19 -0
  34. package/dist/harness/context/capabilities/document/port.js +400 -0
  35. package/dist/harness/context/capabilities/document/types.d.ts +149 -0
  36. package/dist/harness/context/capabilities/document/types.js +8 -0
  37. package/dist/harness/context/capabilities/index.d.ts +1 -0
  38. package/dist/harness/context/capabilities/index.js +1 -0
  39. package/dist/harness/context/constitution/default-agents.md +1 -0
  40. package/dist/harness/context/index.d.ts +2 -2
  41. package/dist/harness/context/index.js +1 -1
  42. package/dist/harness/context/layer-types.d.ts +26 -2
  43. package/dist/harness/context/layer-types.js +59 -11
  44. package/dist/harness/context/layers.d.ts +3 -0
  45. package/dist/harness/context/layers.js +38 -12
  46. package/dist/harness/context/system-prompt-assembler.d.ts +5 -0
  47. package/dist/harness/context/system-prompt-assembler.js +2 -0
  48. package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +1 -0
  49. package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +10 -0
  50. package/dist/harness/extension/plugin-ecosystem/tools/document-tools.d.ts +64 -0
  51. package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +155 -0
  52. package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +1 -0
  53. package/dist/harness/extension/plugin-ecosystem/tools/index.js +1 -0
  54. package/dist/harness/extension/plugin-ecosystem/tools/knowledge.js +13 -0
  55. package/dist/harness/extension/plugin-ecosystem/tools/memory.js +3 -0
  56. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +2 -0
  57. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -1
  58. package/dist/harness/index.d.ts +3 -3
  59. package/dist/harness/index.js +2 -2
  60. package/dist/harness/knowledge/catalog-types.d.ts +4 -0
  61. package/dist/harness/knowledge/db.d.ts +4 -0
  62. package/dist/harness/knowledge/db.js +58 -3
  63. package/dist/harness/knowledge/describe.d.ts +2 -0
  64. package/dist/harness/knowledge/describe.js +5 -1
  65. package/dist/harness/knowledge/document-extract-worker.d.ts +1 -0
  66. package/dist/harness/knowledge/document-extract-worker.js +25 -0
  67. package/dist/harness/knowledge/extract-document.d.ts +17 -0
  68. package/dist/harness/knowledge/extract-document.js +62 -0
  69. package/dist/harness/knowledge/index-store.d.ts +49 -5
  70. package/dist/harness/knowledge/index-store.js +240 -32
  71. package/dist/harness/knowledge/index.d.ts +2 -0
  72. package/dist/harness/knowledge/index.js +1 -0
  73. package/dist/harness/knowledge/ingest.d.ts +123 -2
  74. package/dist/harness/knowledge/ingest.js +661 -59
  75. package/dist/harness/knowledge/retriever.d.ts +8 -0
  76. package/dist/harness/knowledge/retriever.js +33 -1
  77. package/dist/harness/knowledge/source-store.d.ts +4 -0
  78. package/dist/harness/knowledge/source-store.js +62 -6
  79. package/dist/harness/knowledge/topics.d.ts +13 -0
  80. package/dist/harness/knowledge/topics.js +48 -0
  81. package/dist/harness/memory/index.d.ts +3 -2
  82. package/dist/harness/memory/index.js +3 -1
  83. package/dist/harness/memory/retrieval-rank.d.ts +51 -0
  84. package/dist/harness/memory/retrieval-rank.js +60 -0
  85. package/dist/harness/memory/sqlite/index.d.ts +2 -2
  86. package/dist/harness/memory/sqlite/index.js +1 -1
  87. package/dist/harness/memory/sqlite/keyword-search.d.ts +20 -2
  88. package/dist/harness/memory/sqlite/keyword-search.js +40 -17
  89. package/dist/harness/memory/sqlite/memory-store.d.ts +18 -4
  90. package/dist/harness/memory/sqlite/memory-store.js +89 -39
  91. package/dist/harness/memory/store.js +39 -21
  92. package/dist/harness/memory/types.d.ts +7 -0
  93. package/dist/harness/run/runner.d.ts +1 -0
  94. package/dist/harness/run/runner.js +4 -2
  95. package/dist/harness/session/attachments/extract.d.ts +3 -2
  96. package/dist/harness/session/attachments/extract.js +19 -6
  97. package/dist/harness/session/attachments/service.d.ts +5 -1
  98. package/dist/harness/session/attachments/service.js +44 -2
  99. package/dist/harness/session/tasks/index.d.ts +1 -1
  100. package/dist/harness/session/tasks/index.js +1 -1
  101. package/dist/harness/session/tasks/render.d.ts +10 -0
  102. package/dist/harness/session/tasks/render.js +27 -0
  103. package/dist/index.d.ts +2 -2
  104. package/dist/index.js +1 -1
  105. package/package.json +8 -3
@@ -111,6 +111,44 @@ export declare const CompactCapabilityConfigSchema: z.ZodObject<{
111
111
  auto: "auto";
112
112
  }>>;
113
113
  }, z.core.$strip>;
114
+ /** 公用能力:documents(DocumentPort 读抽取) */
115
+ export declare const DocumentsConfigSchema: z.ZodObject<{
116
+ extract: z.ZodOptional<z.ZodObject<{
117
+ enabled: z.ZodOptional<z.ZodBoolean>;
118
+ backend: z.ZodOptional<z.ZodEnum<{
119
+ auto: "auto";
120
+ "t0-only": "t0-only";
121
+ }>>;
122
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
123
+ maxFileBytes: z.ZodOptional<z.ZodNumber>;
124
+ allowedRoots: z.ZodOptional<z.ZodArray<z.ZodString>>;
125
+ }, z.core.$strip>>;
126
+ legacy: z.ZodOptional<z.ZodObject<{
127
+ converter: z.ZodOptional<z.ZodEnum<{
128
+ none: "none";
129
+ soffice: "soffice";
130
+ remote: "remote";
131
+ }>>;
132
+ sofficePath: z.ZodOptional<z.ZodNullable<z.ZodString>>;
133
+ cacheDir: z.ZodOptional<z.ZodNullable<z.ZodString>>;
134
+ cacheMaxBytes: z.ZodOptional<z.ZodNumber>;
135
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
136
+ allowTextScrape: z.ZodOptional<z.ZodBoolean>;
137
+ }, z.core.$strip>>;
138
+ markitdown: z.ZodOptional<z.ZodObject<{
139
+ enabled: z.ZodOptional<z.ZodBoolean>;
140
+ command: z.ZodOptional<z.ZodString>;
141
+ }, z.core.$strip>>;
142
+ enhanced: z.ZodOptional<z.ZodObject<{
143
+ enabled: z.ZodOptional<z.ZodBoolean>;
144
+ provider: z.ZodOptional<z.ZodString>;
145
+ endpoint: z.ZodOptional<z.ZodNullable<z.ZodString>>;
146
+ command: z.ZodOptional<z.ZodNullable<z.ZodString>>;
147
+ authRef: z.ZodOptional<z.ZodNullable<z.ZodString>>;
148
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
149
+ preferOn: z.ZodOptional<z.ZodArray<z.ZodString>>;
150
+ }, z.core.$strip>>;
151
+ }, z.core.$strip>;
114
152
  /** 全局运行宪法(产品资产 / 集成商替换 / 关闭) */
115
153
  export declare const ConstitutionConfigSchema: z.ZodObject<{
116
154
  mode: z.ZodEnum<{
@@ -89,6 +89,45 @@ export const CompactCapabilityConfigSchema = z.object({
89
89
  defaultTargetTokens: z.number().int().positive().optional(),
90
90
  defaultMode: z.enum(['structure_only', 'summary_only', 'head_tail_only', 'auto']).optional(),
91
91
  });
92
+ /** 公用能力:documents(DocumentPort 读抽取) */
93
+ export const DocumentsConfigSchema = z.object({
94
+ extract: z
95
+ .object({
96
+ enabled: z.boolean().optional(),
97
+ backend: z.enum(['auto', 't0-only']).optional(),
98
+ timeoutMs: z.number().int().positive().optional(),
99
+ maxFileBytes: z.number().int().positive().optional(),
100
+ allowedRoots: z.array(z.string()).optional(),
101
+ })
102
+ .optional(),
103
+ legacy: z
104
+ .object({
105
+ converter: z.enum(['none', 'soffice', 'remote']).optional(),
106
+ sofficePath: z.string().nullable().optional(),
107
+ cacheDir: z.string().nullable().optional(),
108
+ cacheMaxBytes: z.number().int().positive().optional(),
109
+ timeoutMs: z.number().int().positive().optional(),
110
+ allowTextScrape: z.boolean().optional(),
111
+ })
112
+ .optional(),
113
+ markitdown: z
114
+ .object({
115
+ enabled: z.boolean().optional(),
116
+ command: z.string().optional(),
117
+ })
118
+ .optional(),
119
+ enhanced: z
120
+ .object({
121
+ enabled: z.boolean().optional(),
122
+ provider: z.string().optional(),
123
+ endpoint: z.string().nullable().optional(),
124
+ command: z.string().nullable().optional(),
125
+ authRef: z.string().nullable().optional(),
126
+ timeoutMs: z.number().int().positive().optional(),
127
+ preferOn: z.array(z.string()).optional(),
128
+ })
129
+ .optional(),
130
+ });
92
131
  /** 全局运行宪法(产品资产 / 集成商替换 / 关闭) */
93
132
  export const ConstitutionConfigSchema = z
94
133
  .object({
@@ -303,6 +303,43 @@ export declare const engineConfigSchema: z.ZodObject<{
303
303
  auto: "auto";
304
304
  }>>;
305
305
  }, z.core.$strip>>;
306
+ documents: z.ZodOptional<z.ZodObject<{
307
+ extract: z.ZodOptional<z.ZodObject<{
308
+ enabled: z.ZodOptional<z.ZodBoolean>;
309
+ backend: z.ZodOptional<z.ZodEnum<{
310
+ auto: "auto";
311
+ "t0-only": "t0-only";
312
+ }>>;
313
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
314
+ maxFileBytes: z.ZodOptional<z.ZodNumber>;
315
+ allowedRoots: z.ZodOptional<z.ZodArray<z.ZodString>>;
316
+ }, z.core.$strip>>;
317
+ legacy: z.ZodOptional<z.ZodObject<{
318
+ converter: z.ZodOptional<z.ZodEnum<{
319
+ none: "none";
320
+ soffice: "soffice";
321
+ remote: "remote";
322
+ }>>;
323
+ sofficePath: z.ZodOptional<z.ZodNullable<z.ZodString>>;
324
+ cacheDir: z.ZodOptional<z.ZodNullable<z.ZodString>>;
325
+ cacheMaxBytes: z.ZodOptional<z.ZodNumber>;
326
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
327
+ allowTextScrape: z.ZodOptional<z.ZodBoolean>;
328
+ }, z.core.$strip>>;
329
+ markitdown: z.ZodOptional<z.ZodObject<{
330
+ enabled: z.ZodOptional<z.ZodBoolean>;
331
+ command: z.ZodOptional<z.ZodString>;
332
+ }, z.core.$strip>>;
333
+ enhanced: z.ZodOptional<z.ZodObject<{
334
+ enabled: z.ZodOptional<z.ZodBoolean>;
335
+ provider: z.ZodOptional<z.ZodString>;
336
+ endpoint: z.ZodOptional<z.ZodNullable<z.ZodString>>;
337
+ command: z.ZodOptional<z.ZodNullable<z.ZodString>>;
338
+ authRef: z.ZodOptional<z.ZodNullable<z.ZodString>>;
339
+ timeoutMs: z.ZodOptional<z.ZodNumber>;
340
+ preferOn: z.ZodOptional<z.ZodArray<z.ZodString>>;
341
+ }, z.core.$strip>>;
342
+ }, z.core.$strip>>;
306
343
  knowledge: z.ZodOptional<z.ZodObject<{
307
344
  recall: z.ZodOptional<z.ZodEnum<{
308
345
  off: "off";
@@ -479,6 +516,11 @@ export declare const engineConfigSchema: z.ZodObject<{
479
516
  admin: z.ZodOptional<z.ZodNumber>;
480
517
  }, z.core.$strip>>;
481
518
  }, z.core.$strip>>;
519
+ retrieval: z.ZodOptional<z.ZodObject<{
520
+ minSimilarity: z.ZodOptional<z.ZodNumber>;
521
+ similarityWeight: z.ZodOptional<z.ZodNumber>;
522
+ minKeywordScore: z.ZodOptional<z.ZodNumber>;
523
+ }, z.core.$strip>>;
482
524
  gates: z.ZodOptional<z.ZodObject<{
483
525
  maxLength: z.ZodOptional<z.ZodObject<{
484
526
  fact: z.ZodOptional<z.ZodNumber>;
@@ -7,7 +7,7 @@ import { z } from 'zod';
7
7
  import { AgentConfigSchema, ChannelConfigSchema, PluginConfigSchema } from './agent.js';
8
8
  import { ModelsConfigSchema } from './models.js';
9
9
  import { AgentRuntimeJsonConfigSchema, BudgetPolicyJsonConfigSchema, ConcurrencyConfigSchema, RunGuardJsonConfigSchema, } from './run.js';
10
- import { CompactCapabilityConfigSchema, ConstitutionConfigSchema, ContextAssemblerConfigSchema, ContextEngineConfigSchema, SummaryConfigSchema, } from './context.js';
10
+ import { CompactCapabilityConfigSchema, ConstitutionConfigSchema, ContextAssemblerConfigSchema, ContextEngineConfigSchema, DocumentsConfigSchema, SummaryConfigSchema, } from './context.js';
11
11
  import { KnowledgeConfigSchema, MemoryConfigSchema } from './substrate.js';
12
12
  import { SecurityConfigSchema, SessionAclConfigSchema, SessionConfigSchema, ToolIsolationConfigSchema, } from './governance.js';
13
13
  import { ObservabilityConfigSchema, ObserverConfigSchema } from './observability.js';
@@ -31,6 +31,8 @@ export const engineConfigSchema = z.object({
31
31
  summary: SummaryConfigSchema.optional(),
32
32
  /** 公用能力 compact 缺省 */
33
33
  compact: CompactCapabilityConfigSchema.optional(),
34
+ /** 公用能力 documents(DocumentPort 读抽取) */
35
+ documents: DocumentsConfigSchema.optional(),
34
36
  /** Knowledge 全局缺省(agents[].knowledge.recall 覆盖) */
35
37
  knowledge: KnowledgeConfigSchema.optional(),
36
38
  context: z
@@ -146,6 +146,11 @@ export declare const MemoryConfigSchema: z.ZodObject<{
146
146
  admin: z.ZodOptional<z.ZodNumber>;
147
147
  }, z.core.$strip>>;
148
148
  }, z.core.$strip>>;
149
+ retrieval: z.ZodOptional<z.ZodObject<{
150
+ minSimilarity: z.ZodOptional<z.ZodNumber>;
151
+ similarityWeight: z.ZodOptional<z.ZodNumber>;
152
+ minKeywordScore: z.ZodOptional<z.ZodNumber>;
153
+ }, z.core.$strip>>;
149
154
  gates: z.ZodOptional<z.ZodObject<{
150
155
  maxLength: z.ZodOptional<z.ZodObject<{
151
156
  fact: z.ZodOptional<z.ZodNumber>;
@@ -177,6 +177,17 @@ export const MemoryConfigSchema = z.object({
177
177
  .optional(),
178
178
  })
179
179
  .optional(),
180
+ /** 检索相关性地板与混合排序(自动注入宁缺毋滥) */
181
+ retrieval: z
182
+ .object({
183
+ /** 向量路径最低余弦相似度(默认 0.35);低于则丢弃 */
184
+ minSimilarity: z.number().min(0).max(1).optional(),
185
+ /** 混合排序中相似度权重(默认 0.65),其余为 importance×confidence×decay */
186
+ similarityWeight: z.number().min(0).max(1).optional(),
187
+ /** 关键词路径最低命中分(默认 2) */
188
+ minKeywordScore: z.number().min(0).optional(),
189
+ })
190
+ .optional(),
180
191
  gates: z
181
192
  .object({
182
193
  maxLength: z
package/dist/config.d.ts CHANGED
@@ -441,6 +441,39 @@ export interface CompactCapabilityConfig {
441
441
  defaultTargetTokens?: number;
442
442
  defaultMode?: 'structure_only' | 'summary_only' | 'head_tail_only' | 'auto';
443
443
  }
444
+ /**
445
+ * 公用能力 documents(DocumentPort 读抽取)
446
+ */
447
+ export interface DocumentsCapabilityConfig {
448
+ extract?: {
449
+ enabled?: boolean;
450
+ backend?: 'auto' | 't0-only';
451
+ timeoutMs?: number;
452
+ maxFileBytes?: number;
453
+ allowedRoots?: string[];
454
+ };
455
+ legacy?: {
456
+ converter?: 'none' | 'soffice' | 'remote';
457
+ sofficePath?: string | null;
458
+ cacheDir?: string | null;
459
+ cacheMaxBytes?: number;
460
+ timeoutMs?: number;
461
+ allowTextScrape?: boolean;
462
+ };
463
+ markitdown?: {
464
+ enabled?: boolean;
465
+ command?: string;
466
+ };
467
+ enhanced?: {
468
+ enabled?: boolean;
469
+ provider?: 'mineru' | 'azure-di' | 'azure-cu' | string;
470
+ endpoint?: string | null;
471
+ command?: string | null;
472
+ authRef?: string | null;
473
+ timeoutMs?: number;
474
+ preferOn?: string[];
475
+ };
476
+ }
444
477
  /**
445
478
  * system prompt 层装配器配置(七层 ContextAssembler)
446
479
  *
@@ -654,6 +687,8 @@ export interface HarnessConfig {
654
687
  summary?: SummaryCapabilityConfig;
655
688
  /** 公用能力 compact 缺省 */
656
689
  compact?: CompactCapabilityConfig;
690
+ /** 公用能力 documents(DocumentPort 读抽取) */
691
+ documents?: DocumentsCapabilityConfig;
657
692
  /** system prompt 七层装配器配置 */
658
693
  contextAssembler?: ContextAssemblerConfig;
659
694
  /** context 域扩展:全局宪法等 */
@@ -696,6 +731,15 @@ export interface HarnessConfig {
696
731
  injectMinScore?: number;
697
732
  channelPriors?: Partial<Record<'user_directive' | 'decision' | 'fail_fix' | 'model_inference' | 'admin', number>>;
698
733
  };
734
+ /** 检索相关性地板与混合排序(自动注入宁缺毋滥) */
735
+ retrieval?: {
736
+ /** 向量路径最低余弦相似度(默认 0.35) */
737
+ minSimilarity?: number;
738
+ /** 混合排序相似度权重(默认 0.65) */
739
+ similarityWeight?: number;
740
+ /** 关键词路径最低命中分(默认 2) */
741
+ minKeywordScore?: number;
742
+ };
699
743
  gates?: {
700
744
  maxLength?: Partial<Record<'fact' | 'method' | 'norm', number>>;
701
745
  };
@@ -290,6 +290,9 @@ async function buildAgent(agentConfig, shared) {
290
290
  builder.memoryStore(new SqliteMemoryStore(db, {
291
291
  embeddingProvider: embRuntime?.provider ?? null,
292
292
  vectorEngine: embRuntime?.vectorEngine ?? 'auto',
293
+ minSimilarity: shared.memoryConfig?.retrieval?.minSimilarity,
294
+ similarityWeight: shared.memoryConfig?.retrieval?.similarityWeight,
295
+ minKeywordScore: shared.memoryConfig?.retrieval?.minKeywordScore,
293
296
  }));
294
297
  builder.wisdomStore(new SqliteWisdomStore(db));
295
298
  builder.cognitionStore(new SqliteConceptGraph(db, {
@@ -0,0 +1,7 @@
1
+ /**
2
+ * DOCX 后端 — mammoth(可选依赖,T0)→ HTML → Markdown
3
+ *
4
+ * @module harness/context/capabilities/document/backends/docx
5
+ */
6
+ import type { DocumentExtractBackend } from '../types.js';
7
+ export declare const docxMammothBackend: DocumentExtractBackend;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * DOCX 后端 — mammoth(可选依赖,T0)→ HTML → Markdown
3
+ *
4
+ * @module harness/context/capabilities/document/backends/docx
5
+ */
6
+ import { DocumentExtractError } from '../errors.js';
7
+ import { htmlFragmentToMarkdown } from '../html-to-md.js';
8
+ let cached;
9
+ async function loadMammoth() {
10
+ if (cached !== undefined)
11
+ return cached;
12
+ try {
13
+ const mod = (await import('mammoth'));
14
+ if (typeof mod.convertToHtml !== 'function') {
15
+ cached = null;
16
+ return null;
17
+ }
18
+ cached = mod;
19
+ return mod;
20
+ }
21
+ catch {
22
+ // 可选依赖未安装 —— 缓存 null,调用方走 BACKEND_UNAVAILABLE
23
+ cached = null;
24
+ return null;
25
+ }
26
+ }
27
+ export const docxMammothBackend = {
28
+ id: 'docx-mammoth',
29
+ tier: 't0',
30
+ formats: ['docx'],
31
+ async isAvailable() {
32
+ return (await loadMammoth()) !== null;
33
+ },
34
+ accepts({ format }) {
35
+ return format === 'docx';
36
+ },
37
+ async extract(source, _options) {
38
+ const mammoth = await loadMammoth();
39
+ if (!mammoth) {
40
+ throw new DocumentExtractError('BACKEND_UNAVAILABLE', 'mammoth is not installed', [
41
+ 'optional:mammoth',
42
+ ]);
43
+ }
44
+ const buffer = Buffer.from(source.data);
45
+ try {
46
+ const result = await mammoth.convertToHtml({ buffer });
47
+ const markdown = htmlFragmentToMarkdown(result.value);
48
+ const warnings = (result.messages ?? []).map((m) => ({
49
+ code: 'PARTIAL_EXTRACT',
50
+ message: m.message,
51
+ }));
52
+ return {
53
+ markdown,
54
+ meta: { format: 'docx' },
55
+ warnings,
56
+ backend: 'docx-mammoth',
57
+ };
58
+ }
59
+ catch (err) {
60
+ const msg = err instanceof Error ? err.message : String(err);
61
+ if (/password|encrypt/i.test(msg)) {
62
+ throw new DocumentExtractError('PASSWORD_REQUIRED', 'DOCX requires a password');
63
+ }
64
+ throw new DocumentExtractError('INVALID_SOURCE', `DOCX extract failed: ${msg}`);
65
+ }
66
+ },
67
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * PPTX / 长尾后端 — officeparser(可选依赖,T0 长尾)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/office-parser
5
+ */
6
+ import type { DocumentExtractBackend } from '../types.js';
7
+ export declare const officeParserBackend: DocumentExtractBackend;
@@ -0,0 +1,76 @@
1
+ /**
2
+ * PPTX / 长尾后端 — officeparser(可选依赖,T0 长尾)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/office-parser
5
+ */
6
+ import { DocumentExtractError } from '../errors.js';
7
+ let cached;
8
+ async function loadOfficeParser() {
9
+ if (cached !== undefined)
10
+ return cached;
11
+ try {
12
+ const mod = (await import('officeparser'));
13
+ const parse = mod.OfficeParser?.parseOffice ?? mod.parseOffice;
14
+ if (typeof parse !== 'function') {
15
+ cached = null;
16
+ return null;
17
+ }
18
+ cached = mod;
19
+ return mod;
20
+ }
21
+ catch {
22
+ // 可选依赖未安装 —— 缓存 null,调用方走 BACKEND_UNAVAILABLE
23
+ cached = null;
24
+ return null;
25
+ }
26
+ }
27
+ const FORMATS = ['pptx', 'odt', 'ods', 'odp', 'odg', 'epub', 'tex', 'rtf'];
28
+ export const officeParserBackend = {
29
+ id: 'office-parser',
30
+ tier: 't0',
31
+ formats: [...FORMATS],
32
+ async isAvailable() {
33
+ return (await loadOfficeParser()) !== null;
34
+ },
35
+ accepts({ format }) {
36
+ return FORMATS.includes(format);
37
+ },
38
+ async extract(source, options) {
39
+ const mod = await loadOfficeParser();
40
+ const parse = mod?.OfficeParser?.parseOffice ?? mod?.parseOffice;
41
+ if (!mod || typeof parse !== 'function') {
42
+ throw new DocumentExtractError('BACKEND_UNAVAILABLE', 'officeparser is not installed', [
43
+ 'optional:officeparser',
44
+ ]);
45
+ }
46
+ const warnings = [];
47
+ if (options.ocr) {
48
+ warnings.push({
49
+ code: 'OCR_UNAVAILABLE',
50
+ message: 'officeparser OCR requires ocr+extractAttachments; not enabled in P0',
51
+ });
52
+ }
53
+ try {
54
+ const ast = await parse(source.data, { fileType: source.formatHint });
55
+ const { value } = await ast.to('md');
56
+ return {
57
+ markdown: value,
58
+ meta: {
59
+ format: source.formatHint ?? 'unknown',
60
+ title: ast.metadata?.title != null && typeof ast.metadata.title === 'string'
61
+ ? ast.metadata.title
62
+ : undefined,
63
+ },
64
+ warnings,
65
+ backend: 'office-parser',
66
+ };
67
+ }
68
+ catch (err) {
69
+ const msg = err instanceof Error ? err.message : String(err);
70
+ if (/password/i.test(msg)) {
71
+ throw new DocumentExtractError('PASSWORD_REQUIRED', 'document requires a password');
72
+ }
73
+ throw new DocumentExtractError('INVALID_SOURCE', `officeparser extract failed: ${msg}`);
74
+ }
75
+ },
76
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * PDF 后端 — unpdf(可选依赖,T0)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/pdf
5
+ */
6
+ import type { DocumentExtractBackend } from '../types.js';
7
+ export declare const pdfUnpdfBackend: DocumentExtractBackend;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * PDF 后端 — unpdf(可选依赖,T0)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/pdf
5
+ */
6
+ import { DocumentExtractError } from '../errors.js';
7
+ let cached;
8
+ async function loadUnpdf() {
9
+ if (cached !== undefined)
10
+ return cached;
11
+ try {
12
+ const mod = (await import('unpdf'));
13
+ if (typeof mod.getDocumentProxy !== 'function' || typeof mod.extractText !== 'function') {
14
+ cached = null;
15
+ return null;
16
+ }
17
+ cached = mod;
18
+ return mod;
19
+ }
20
+ catch {
21
+ // 可选依赖未安装 —— 缓存 null,调用方走 BACKEND_UNAVAILABLE
22
+ cached = null;
23
+ return null;
24
+ }
25
+ }
26
+ export const pdfUnpdfBackend = {
27
+ id: 'pdf-unpdf',
28
+ tier: 't0',
29
+ formats: ['pdf'],
30
+ async isAvailable() {
31
+ return (await loadUnpdf()) !== null;
32
+ },
33
+ accepts({ format }) {
34
+ return format === 'pdf';
35
+ },
36
+ async extract(source, options) {
37
+ const unpdf = await loadUnpdf();
38
+ if (!unpdf) {
39
+ throw new DocumentExtractError('BACKEND_UNAVAILABLE', 'unpdf is not installed', [
40
+ 'optional:unpdf',
41
+ ]);
42
+ }
43
+ const warnings = [];
44
+ if (options.ocr) {
45
+ warnings.push({
46
+ code: 'OCR_UNAVAILABLE',
47
+ message: 'pdf-unpdf extracts text layer only; enable enhanced OCR backend if needed',
48
+ });
49
+ }
50
+ try {
51
+ const pdf = await unpdf.getDocumentProxy(source.data, {
52
+ ...(source.password ? { password: source.password } : {}),
53
+ });
54
+ const { totalPages, text } = await unpdf.extractText(pdf, { mergePages: true });
55
+ let title;
56
+ let author;
57
+ try {
58
+ const meta = await unpdf.getMeta(pdf);
59
+ title = meta.info?.Title != null ? String(meta.info.Title) : undefined;
60
+ author = meta.info?.Author != null ? String(meta.info.Author) : undefined;
61
+ }
62
+ catch {
63
+ // metadata is optional; extraction result remains usable
64
+ }
65
+ const markdown = Array.isArray(text) ? text.join('\n\n') : text;
66
+ return {
67
+ markdown,
68
+ meta: {
69
+ format: 'pdf',
70
+ pages: totalPages,
71
+ title,
72
+ author,
73
+ },
74
+ warnings,
75
+ backend: 'pdf-unpdf',
76
+ };
77
+ }
78
+ catch (err) {
79
+ const msg = err instanceof Error ? err.message : String(err);
80
+ if (/password/i.test(msg)) {
81
+ throw new DocumentExtractError('PASSWORD_REQUIRED', 'PDF requires a password');
82
+ }
83
+ throw new DocumentExtractError('INVALID_SOURCE', `PDF extract failed: ${msg}`);
84
+ }
85
+ },
86
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 纯文本 / Markdown / CSV 后端(无外部依赖)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/plain
5
+ */
6
+ import type { DocumentExtractBackend } from '../types.js';
7
+ export declare const plainTextBackend: DocumentExtractBackend;
@@ -0,0 +1,42 @@
1
+ /**
2
+ * 纯文本 / Markdown / CSV 后端(无外部依赖)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/plain
5
+ */
6
+ function decodeUtf8(data) {
7
+ return new TextDecoder('utf-8', { fatal: false }).decode(data);
8
+ }
9
+ function looksLikeBinary(data) {
10
+ const n = Math.min(data.length, 8000);
11
+ for (let i = 0; i < n; i++) {
12
+ if (data[i] === 0)
13
+ return true;
14
+ }
15
+ return false;
16
+ }
17
+ export const plainTextBackend = {
18
+ id: 'plain-text',
19
+ tier: 't0',
20
+ formats: ['txt', 'md', 'csv', 'html'],
21
+ async isAvailable() {
22
+ return true;
23
+ },
24
+ accepts({ format }) {
25
+ return format === 'txt' || format === 'md' || format === 'csv' || format === 'html';
26
+ },
27
+ async extract(source, _options) {
28
+ if (looksLikeBinary(source.data)) {
29
+ const { DocumentExtractError } = await import('../errors.js');
30
+ throw new DocumentExtractError('INVALID_SOURCE', 'plain-text backend received binary data');
31
+ }
32
+ const text = decodeUtf8(source.data);
33
+ const format = source.formatHint ?? 'txt';
34
+ // html 原样保留标签交给 knowledge htmlAdapter;此处仅保证可读文本
35
+ return {
36
+ markdown: text,
37
+ meta: { format },
38
+ warnings: [],
39
+ backend: 'plain-text',
40
+ };
41
+ },
42
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 电子表格后端 — SheetJS `xlsx`(可选依赖,T0;覆盖 xlsx + xls)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/sheet
5
+ */
6
+ import type { DocumentExtractBackend } from '../types.js';
7
+ export declare const sheetXlsxBackend: DocumentExtractBackend;