@octopi-agent/engine 0.55.0 → 0.56.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 (56) 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 +37 -0
  4. package/dist/config-schema/engine.js +3 -1
  5. package/dist/config.d.ts +35 -0
  6. package/dist/harness/context/capabilities/document/backends/docx.d.ts +7 -0
  7. package/dist/harness/context/capabilities/document/backends/docx.js +67 -0
  8. package/dist/harness/context/capabilities/document/backends/office-parser.d.ts +7 -0
  9. package/dist/harness/context/capabilities/document/backends/office-parser.js +76 -0
  10. package/dist/harness/context/capabilities/document/backends/pdf.d.ts +7 -0
  11. package/dist/harness/context/capabilities/document/backends/pdf.js +86 -0
  12. package/dist/harness/context/capabilities/document/backends/plain.d.ts +7 -0
  13. package/dist/harness/context/capabilities/document/backends/plain.js +42 -0
  14. package/dist/harness/context/capabilities/document/backends/sheet.d.ts +7 -0
  15. package/dist/harness/context/capabilities/document/backends/sheet.js +90 -0
  16. package/dist/harness/context/capabilities/document/backends/xmind.d.ts +20 -0
  17. package/dist/harness/context/capabilities/document/backends/xmind.js +198 -0
  18. package/dist/harness/context/capabilities/document/errors.d.ts +27 -0
  19. package/dist/harness/context/capabilities/document/errors.js +33 -0
  20. package/dist/harness/context/capabilities/document/format.d.ts +38 -0
  21. package/dist/harness/context/capabilities/document/format.js +113 -0
  22. package/dist/harness/context/capabilities/document/html-to-md.d.ts +14 -0
  23. package/dist/harness/context/capabilities/document/html-to-md.js +97 -0
  24. package/dist/harness/context/capabilities/document/index.d.ts +21 -0
  25. package/dist/harness/context/capabilities/document/index.js +20 -0
  26. package/dist/harness/context/capabilities/document/legacy/factory.d.ts +13 -0
  27. package/dist/harness/context/capabilities/document/legacy/factory.js +23 -0
  28. package/dist/harness/context/capabilities/document/legacy/soffice.d.ts +32 -0
  29. package/dist/harness/context/capabilities/document/legacy/soffice.js +186 -0
  30. package/dist/harness/context/capabilities/document/port.d.ts +19 -0
  31. package/dist/harness/context/capabilities/document/port.js +400 -0
  32. package/dist/harness/context/capabilities/document/types.d.ts +149 -0
  33. package/dist/harness/context/capabilities/document/types.js +8 -0
  34. package/dist/harness/context/capabilities/index.d.ts +1 -0
  35. package/dist/harness/context/capabilities/index.js +1 -0
  36. package/dist/harness/extension/plugin-ecosystem/tools/builtin.d.ts +1 -0
  37. package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +10 -0
  38. package/dist/harness/extension/plugin-ecosystem/tools/document-tools.d.ts +64 -0
  39. package/dist/harness/extension/plugin-ecosystem/tools/document-tools.js +155 -0
  40. package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +1 -0
  41. package/dist/harness/extension/plugin-ecosystem/tools/index.js +1 -0
  42. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.d.ts +2 -0
  43. package/dist/harness/extension/plugin-ecosystem/tools/tool-set.js +4 -1
  44. package/dist/harness/index.d.ts +2 -2
  45. package/dist/harness/index.js +1 -1
  46. package/dist/harness/knowledge/index-store.d.ts +7 -1
  47. package/dist/harness/knowledge/index-store.js +6 -0
  48. package/dist/harness/knowledge/ingest.d.ts +23 -0
  49. package/dist/harness/knowledge/ingest.js +81 -3
  50. package/dist/harness/session/attachments/extract.d.ts +3 -2
  51. package/dist/harness/session/attachments/extract.js +19 -6
  52. package/dist/harness/session/attachments/service.d.ts +5 -1
  53. package/dist/harness/session/attachments/service.js +44 -2
  54. package/dist/index.d.ts +2 -2
  55. package/dist/index.js +1 -1
  56. 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";
@@ -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
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 域扩展:全局宪法等 */
@@ -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;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 电子表格后端 — SheetJS `xlsx`(可选依赖,T0;覆盖 xlsx + xls)
3
+ *
4
+ * @module harness/context/capabilities/document/backends/sheet
5
+ */
6
+ import { DocumentExtractError } from '../errors.js';
7
+ let cached;
8
+ async function loadXlsx() {
9
+ if (cached !== undefined)
10
+ return cached;
11
+ try {
12
+ const mod = (await import('xlsx'));
13
+ if (typeof mod.read !== 'function' || !mod.utils?.sheet_to_json) {
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
+ function cellToText(v) {
27
+ if (v == null)
28
+ return '';
29
+ if (typeof v === 'string')
30
+ return v.replace(/\|/g, '\\|').replace(/\n+/g, ' ');
31
+ return String(v).replace(/\|/g, '\\|');
32
+ }
33
+ function sheetToMarkdown(rows) {
34
+ if (!rows.length)
35
+ return '';
36
+ const width = Math.max(...rows.map((r) => r.length));
37
+ const norm = rows.map((r) => {
38
+ const copy = [];
39
+ for (let i = 0; i < width; i++)
40
+ copy.push(cellToText(r[i]));
41
+ return copy;
42
+ });
43
+ const lines = [
44
+ `| ${norm[0].join(' | ')} |`,
45
+ `| ${norm[0].map(() => '---').join(' | ')} |`,
46
+ ...norm.slice(1).map((r) => `| ${r.join(' | ')} |`),
47
+ ];
48
+ return lines.join('\n');
49
+ }
50
+ export const sheetXlsxBackend = {
51
+ id: 'sheet-xlsx',
52
+ tier: 't0',
53
+ formats: ['xlsx', 'xls'],
54
+ async isAvailable() {
55
+ return (await loadXlsx()) !== null;
56
+ },
57
+ accepts({ format }) {
58
+ return format === 'xlsx' || format === 'xls';
59
+ },
60
+ async extract(source, _options) {
61
+ const xlsx = await loadXlsx();
62
+ if (!xlsx) {
63
+ throw new DocumentExtractError('BACKEND_UNAVAILABLE', 'xlsx (SheetJS) is not installed', [
64
+ 'optional:xlsx',
65
+ ]);
66
+ }
67
+ let wb;
68
+ try {
69
+ wb = xlsx.read(source.data, { type: 'array', cellDates: true });
70
+ }
71
+ catch (err) {
72
+ const msg = err instanceof Error ? err.message : String(err);
73
+ throw new DocumentExtractError('INVALID_SOURCE', `Spreadsheet extract failed: ${msg}`);
74
+ }
75
+ const parts = [];
76
+ for (const name of wb.SheetNames) {
77
+ const sheet = wb.Sheets[name];
78
+ const rows = xlsx.utils.sheet_to_json(sheet, { header: 1, raw: false, defval: '' });
79
+ parts.push(`## ${name}\n\n${sheetToMarkdown(rows)}`);
80
+ }
81
+ return {
82
+ markdown: parts.join('\n\n'),
83
+ meta: {
84
+ format: source.formatHint === 'xls' ? 'xls' : 'xlsx',
85
+ },
86
+ warnings: [],
87
+ backend: 'sheet-xlsx',
88
+ };
89
+ },
90
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * XMind 后端 — ZIP + content.json / content.xml → Markdown 大纲
3
+ *
4
+ * XMind 2020+:content.json 树(children.attached)
5
+ * XMind 8:content.xml topic 树(尽力解析)
6
+ *
7
+ * @module harness/context/capabilities/document/backends/xmind
8
+ */
9
+ import type { DocumentExtractBackend } from '../types.js';
10
+ /** content.json(XMind 2020+)→ Markdown 大纲 */
11
+ export declare function xmindJsonToMarkdown(jsonText: string): string;
12
+ /**
13
+ * content.xml(XMind 8)→ Markdown 大纲
14
+ * 仅取 title 树,不解析样式/关系边。
15
+ *
16
+ * @param xmlText - content.xml 文本
17
+ * @returns Markdown 大纲
18
+ */
19
+ export declare function xmindXmlToMarkdown(xmlText: string): string;
20
+ export declare const xmindBackend: DocumentExtractBackend;