plugin-document-understanding 1.1.14 → 1.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -29
- package/client-v2.d.ts +2 -2
- package/client-v2.js +1 -1
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -7
- package/dist/locale/en-US.json +3 -1
- package/dist/locale/zh-CN.json +3 -1
- package/dist/node_modules/form-data/lib/form_data.js +2 -2
- package/dist/node_modules/form-data/node_modules/es-define-property/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/package.json +81 -0
- package/dist/node_modules/form-data/node_modules/es-define-property/test/index.js +56 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/RequireObjectCoercible.js +11 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.d.ts +7 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/ToObject.js +10 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/index.js +4 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/isObject.js +6 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/package.json +80 -0
- package/dist/node_modules/form-data/node_modules/es-object-atoms/test/index.js +38 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.eslintrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.d.ts +10 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/index.js +35 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/package.json +78 -0
- package/dist/node_modules/form-data/node_modules/es-set-tostringtag/test/index.js +85 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.eslintrc +42 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/index.js +378 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.eslintrc +11 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/.nycrc +9 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/index.js +14 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/package.json +111 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/shams.js +45 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/index.js +22 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/core-js.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/shams/get-own-property-symbols.js +29 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/has-symbols/test/tests.js +58 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/node_modules/hasown/package.json +92 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/package.json +97 -0
- package/dist/node_modules/form-data/node_modules/get-intrinsic/test/GetIntrinsic.js +274 -0
- package/dist/node_modules/form-data/node_modules/gopd/.eslintrc +16 -0
- package/dist/node_modules/form-data/node_modules/gopd/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.d.ts +1 -0
- package/dist/node_modules/form-data/node_modules/gopd/gOPD.js +4 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.d.ts +5 -0
- package/dist/node_modules/form-data/node_modules/gopd/index.js +15 -0
- package/dist/node_modules/form-data/node_modules/gopd/package.json +77 -0
- package/dist/node_modules/form-data/node_modules/gopd/test/index.js +36 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.eslintrc +5 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/package.json +108 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/shams.js +8 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/index.js +21 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/core-js.js +31 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/shams/get-own-property-symbols.js +30 -0
- package/dist/node_modules/form-data/node_modules/has-tostringtag/test/tests.js +15 -0
- package/dist/node_modules/form-data/node_modules/hasown/.github/FUNDING.yml +12 -0
- package/dist/node_modules/form-data/node_modules/hasown/.nycrc +13 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.d.ts +3 -0
- package/dist/node_modules/form-data/node_modules/hasown/index.js +8 -0
- package/dist/node_modules/form-data/node_modules/hasown/package.json +91 -0
- package/dist/node_modules/form-data/package.json +1 -1
- package/dist/node_modules/mime-types/index.js +3 -3
- package/dist/node_modules/mime-types/package.json +1 -1
- package/dist/server/actions.js +45 -35
- package/package.json +57 -46
- package/src/client/index.tsx +15 -15
- package/src/client-v2/components/EndpointsTab.tsx +578 -578
- package/src/client-v2/components/JobsTab.tsx +294 -294
- package/src/client-v2/components/PipelinesTab.tsx +962 -962
- package/src/client-v2/components/PluginSettings.tsx +50 -50
- package/src/client-v2/components/ServiceConfigTab.tsx +139 -139
- package/src/client-v2/index.tsx +1 -1
- package/src/client-v2/locale.ts +21 -21
- package/src/client-v2/models/index.ts +12 -12
- package/src/client-v2/plugin.tsx +31 -21
- package/src/client-v2/types.ts +32 -32
- package/src/index.ts +2 -2
- package/src/locale/en-US.json +3 -1
- package/src/locale/zh-CN.json +3 -1
- package/src/server/__tests__/actions.test.ts +121 -98
- package/src/server/__tests__/cluster-safety.test.ts +259 -259
- package/src/server/__tests__/document-understanding-service.test.ts +82 -82
- package/src/server/__tests__/dugate-integration.test.ts +221 -221
- package/src/server/__tests__/smoke.test.ts +78 -55
- package/src/server/__tests__/upload-security.test.ts +128 -0
- package/src/server/actions.ts +53 -51
- package/src/server/collections/config.ts +18 -18
- package/src/server/collections/endpoints.ts +33 -33
- package/src/server/collections/jobs.ts +42 -42
- package/src/server/collections/pipeline-steps.ts +27 -27
- package/src/server/collections/pipelines.ts +19 -19
- package/src/server/dugate-seed.ts +491 -491
- package/src/server/index.ts +1 -1
- package/src/server/plugin.ts +143 -143
- package/src/server/services/AsyncJobManager.ts +238 -238
- package/src/server/services/DocumentUnderstandingService.ts +332 -332
- package/src/server/services/ExternalApiClient.ts +178 -178
- package/src/server/services/PipelineExecutor.ts +593 -593
- package/src/server/tools/document-understanding-tool.ts +181 -181
- package/src/server/types.ts +90 -90
|
@@ -1,181 +1,181 @@
|
|
|
1
|
-
import { DocumentUnderstandingService } from '../services/DocumentUnderstandingService';
|
|
2
|
-
import { PipelineDef, JobState } from '../types';
|
|
3
|
-
|
|
4
|
-
function cloneJson<T>(value: T): T {
|
|
5
|
-
if (value === undefined || value === null) {
|
|
6
|
-
return value;
|
|
7
|
-
}
|
|
8
|
-
return JSON.parse(JSON.stringify(value));
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Build tool name from pipeline name.
|
|
13
|
-
* Convention: doc_understanding.pipeline.<sanitized_name>
|
|
14
|
-
*/
|
|
15
|
-
function buildToolName(pipelineName: string): string {
|
|
16
|
-
const sanitized = pipelineName
|
|
17
|
-
.toLowerCase()
|
|
18
|
-
.replace(/[^a-z0-9_]/g, '_')
|
|
19
|
-
.replace(/_+/g, '_')
|
|
20
|
-
.replace(/^_|_$/g, '');
|
|
21
|
-
return `doc_understanding.${sanitized}`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function buildSchema(pipeline: PipelineDef) {
|
|
25
|
-
const baseSchema =
|
|
26
|
-
pipeline.inputSchema && Object.keys(pipeline.inputSchema).length > 0
|
|
27
|
-
? cloneJson(pipeline.inputSchema)
|
|
28
|
-
: {
|
|
29
|
-
type: 'object',
|
|
30
|
-
properties: {
|
|
31
|
-
input: {
|
|
32
|
-
type: 'object',
|
|
33
|
-
description: 'Dữ liệu đầu vào cho pipeline (JSON)',
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Add file_urls to allow AI to pass uploaded files
|
|
39
|
-
if (baseSchema.type === 'object' && baseSchema.properties) {
|
|
40
|
-
baseSchema.properties.file_urls = {
|
|
41
|
-
type: 'array',
|
|
42
|
-
items: { type: 'string' },
|
|
43
|
-
description: 'Danh sách các URL của file/ảnh đính kèm trong chat',
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return baseSchema;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function applySchemaDefaults(value: any, schema: any): any {
|
|
51
|
-
if (!schema || typeof schema !== 'object') {
|
|
52
|
-
return value;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (value === undefined && Object.prototype.hasOwnProperty.call(schema, 'default')) {
|
|
56
|
-
return cloneJson(schema.default);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const isObjectSchema = schema.type === 'object' || schema.properties;
|
|
60
|
-
if (isObjectSchema) {
|
|
61
|
-
const source = value && typeof value === 'object' && !Array.isArray(value) ? { ...value } : {};
|
|
62
|
-
|
|
63
|
-
for (const [key, propertySchema] of Object.entries(schema.properties || {})) {
|
|
64
|
-
const currentValue = source[key];
|
|
65
|
-
const nextValue = applySchemaDefaults(currentValue, propertySchema);
|
|
66
|
-
if (nextValue !== undefined) {
|
|
67
|
-
source[key] = nextValue;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return source;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (Array.isArray(value) && schema.items) {
|
|
74
|
-
return value.map((item) => applySchemaDefaults(item, schema.items));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return value;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Create a single tool definition for one pipeline.
|
|
82
|
-
*/
|
|
83
|
-
function createToolForPipeline(service: DocumentUnderstandingService, pipeline: PipelineDef) {
|
|
84
|
-
const stepNames = (pipeline.steps || [])
|
|
85
|
-
.sort((a, b) => a.stepOrder - b.stepOrder)
|
|
86
|
-
.map((s) => s.name)
|
|
87
|
-
.join(' → ');
|
|
88
|
-
|
|
89
|
-
const description = [
|
|
90
|
-
pipeline.description || `Thực thi pipeline "${pipeline.name}".`,
|
|
91
|
-
stepNames ? `Steps: ${stepNames}` : '',
|
|
92
|
-
'Trả về kết quả phân tích JSON hoặc Job ID nếu chạy async.',
|
|
93
|
-
]
|
|
94
|
-
.filter(Boolean)
|
|
95
|
-
.join(' ');
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
scope: 'CUSTOM' as const,
|
|
99
|
-
execution: 'backend' as const,
|
|
100
|
-
defaultPermission: 'ASK' as const,
|
|
101
|
-
|
|
102
|
-
introduction: {
|
|
103
|
-
title: `Document: ${pipeline.name}`,
|
|
104
|
-
about: pipeline.description || `Xử lý tài liệu qua pipeline "${pipeline.name}".`,
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
definition: {
|
|
108
|
-
name: buildToolName(pipeline.name),
|
|
109
|
-
description,
|
|
110
|
-
schema: buildSchema(pipeline),
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
invoke: async (ctx: any, args: any) => {
|
|
114
|
-
try {
|
|
115
|
-
const config = await service.getConfig();
|
|
116
|
-
const pollInterval = Math.max(config.pollInterval || 2000, 1000);
|
|
117
|
-
const maxWait = Math.min(config.pollTimeout || 30000, 30000);
|
|
118
|
-
|
|
119
|
-
// Extract file URLs
|
|
120
|
-
let files: any[] = [];
|
|
121
|
-
if (args.file_urls && Array.isArray(args.file_urls)) {
|
|
122
|
-
files = args.file_urls.map((url: string) => ({ url }));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// Clean up args if using default schema
|
|
126
|
-
const hasInputSchema = pipeline.inputSchema && Object.keys(pipeline.inputSchema).length > 0;
|
|
127
|
-
let input = hasInputSchema ? args : args.input || args;
|
|
128
|
-
if (input === args) {
|
|
129
|
-
input = { ...args };
|
|
130
|
-
delete input.file_urls;
|
|
131
|
-
}
|
|
132
|
-
if (hasInputSchema) {
|
|
133
|
-
input = applySchemaDefaults(input, pipeline.inputSchema);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const { jobId } = await service.executePipeline(pipeline.id, input, files);
|
|
137
|
-
const start = Date.now();
|
|
138
|
-
|
|
139
|
-
while (Date.now() - start < maxWait) {
|
|
140
|
-
const job: JobState = await service.getJobStatus(jobId);
|
|
141
|
-
if (job.status === 'completed') {
|
|
142
|
-
return { status: 'completed', result: job.finalResult };
|
|
143
|
-
} else if (job.status === 'failed' || job.status === 'timeout') {
|
|
144
|
-
return { status: job.status, error: job.error };
|
|
145
|
-
}
|
|
146
|
-
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return {
|
|
150
|
-
status: 'polling',
|
|
151
|
-
message: `Pipeline "${pipeline.name}" đang chạy, trả về job ID để theo dõi.`,
|
|
152
|
-
jobId,
|
|
153
|
-
};
|
|
154
|
-
} catch (err: any) {
|
|
155
|
-
return { error: err.message };
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Dynamic tools provider: queries all enabled pipelines and registers one tool per pipeline.
|
|
163
|
-
* Called on-demand by the AI tools manager each time tools are listed.
|
|
164
|
-
*/
|
|
165
|
-
export function createDynamicPipelineToolsProvider(service: DocumentUnderstandingService) {
|
|
166
|
-
return async (register: { registerTools: (options: any) => void }) => {
|
|
167
|
-
try {
|
|
168
|
-
if (!service.isReady()) return;
|
|
169
|
-
|
|
170
|
-
const pipelines = await service.listPipelines();
|
|
171
|
-
const enabledPipelines = pipelines.filter((p) => p.enabled);
|
|
172
|
-
|
|
173
|
-
if (enabledPipelines.length === 0) return;
|
|
174
|
-
|
|
175
|
-
const tools = enabledPipelines.map((pipeline) => createToolForPipeline(service, pipeline));
|
|
176
|
-
register.registerTools(tools);
|
|
177
|
-
} catch {
|
|
178
|
-
// Silently skip — service may not be initialized yet
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
}
|
|
1
|
+
import { DocumentUnderstandingService } from '../services/DocumentUnderstandingService';
|
|
2
|
+
import { PipelineDef, JobState } from '../types';
|
|
3
|
+
|
|
4
|
+
function cloneJson<T>(value: T): T {
|
|
5
|
+
if (value === undefined || value === null) {
|
|
6
|
+
return value;
|
|
7
|
+
}
|
|
8
|
+
return JSON.parse(JSON.stringify(value));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Build tool name from pipeline name.
|
|
13
|
+
* Convention: doc_understanding.pipeline.<sanitized_name>
|
|
14
|
+
*/
|
|
15
|
+
function buildToolName(pipelineName: string): string {
|
|
16
|
+
const sanitized = pipelineName
|
|
17
|
+
.toLowerCase()
|
|
18
|
+
.replace(/[^a-z0-9_]/g, '_')
|
|
19
|
+
.replace(/_+/g, '_')
|
|
20
|
+
.replace(/^_|_$/g, '');
|
|
21
|
+
return `doc_understanding.${sanitized}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function buildSchema(pipeline: PipelineDef) {
|
|
25
|
+
const baseSchema =
|
|
26
|
+
pipeline.inputSchema && Object.keys(pipeline.inputSchema).length > 0
|
|
27
|
+
? cloneJson(pipeline.inputSchema)
|
|
28
|
+
: {
|
|
29
|
+
type: 'object',
|
|
30
|
+
properties: {
|
|
31
|
+
input: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
description: 'Dữ liệu đầu vào cho pipeline (JSON)',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// Add file_urls to allow AI to pass uploaded files
|
|
39
|
+
if (baseSchema.type === 'object' && baseSchema.properties) {
|
|
40
|
+
baseSchema.properties.file_urls = {
|
|
41
|
+
type: 'array',
|
|
42
|
+
items: { type: 'string' },
|
|
43
|
+
description: 'Danh sách các URL của file/ảnh đính kèm trong chat',
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return baseSchema;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function applySchemaDefaults(value: any, schema: any): any {
|
|
51
|
+
if (!schema || typeof schema !== 'object') {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (value === undefined && Object.prototype.hasOwnProperty.call(schema, 'default')) {
|
|
56
|
+
return cloneJson(schema.default);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const isObjectSchema = schema.type === 'object' || schema.properties;
|
|
60
|
+
if (isObjectSchema) {
|
|
61
|
+
const source = value && typeof value === 'object' && !Array.isArray(value) ? { ...value } : {};
|
|
62
|
+
|
|
63
|
+
for (const [key, propertySchema] of Object.entries(schema.properties || {})) {
|
|
64
|
+
const currentValue = source[key];
|
|
65
|
+
const nextValue = applySchemaDefaults(currentValue, propertySchema);
|
|
66
|
+
if (nextValue !== undefined) {
|
|
67
|
+
source[key] = nextValue;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return source;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (Array.isArray(value) && schema.items) {
|
|
74
|
+
return value.map((item) => applySchemaDefaults(item, schema.items));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Create a single tool definition for one pipeline.
|
|
82
|
+
*/
|
|
83
|
+
function createToolForPipeline(service: DocumentUnderstandingService, pipeline: PipelineDef) {
|
|
84
|
+
const stepNames = (pipeline.steps || [])
|
|
85
|
+
.sort((a, b) => a.stepOrder - b.stepOrder)
|
|
86
|
+
.map((s) => s.name)
|
|
87
|
+
.join(' → ');
|
|
88
|
+
|
|
89
|
+
const description = [
|
|
90
|
+
pipeline.description || `Thực thi pipeline "${pipeline.name}".`,
|
|
91
|
+
stepNames ? `Steps: ${stepNames}` : '',
|
|
92
|
+
'Trả về kết quả phân tích JSON hoặc Job ID nếu chạy async.',
|
|
93
|
+
]
|
|
94
|
+
.filter(Boolean)
|
|
95
|
+
.join(' ');
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
scope: 'CUSTOM' as const,
|
|
99
|
+
execution: 'backend' as const,
|
|
100
|
+
defaultPermission: 'ASK' as const,
|
|
101
|
+
|
|
102
|
+
introduction: {
|
|
103
|
+
title: `Document: ${pipeline.name}`,
|
|
104
|
+
about: pipeline.description || `Xử lý tài liệu qua pipeline "${pipeline.name}".`,
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
definition: {
|
|
108
|
+
name: buildToolName(pipeline.name),
|
|
109
|
+
description,
|
|
110
|
+
schema: buildSchema(pipeline),
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
invoke: async (ctx: any, args: any) => {
|
|
114
|
+
try {
|
|
115
|
+
const config = await service.getConfig();
|
|
116
|
+
const pollInterval = Math.max(config.pollInterval || 2000, 1000);
|
|
117
|
+
const maxWait = Math.min(config.pollTimeout || 30000, 30000);
|
|
118
|
+
|
|
119
|
+
// Extract file URLs
|
|
120
|
+
let files: any[] = [];
|
|
121
|
+
if (args.file_urls && Array.isArray(args.file_urls)) {
|
|
122
|
+
files = args.file_urls.map((url: string) => ({ url }));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Clean up args if using default schema
|
|
126
|
+
const hasInputSchema = pipeline.inputSchema && Object.keys(pipeline.inputSchema).length > 0;
|
|
127
|
+
let input = hasInputSchema ? args : args.input || args;
|
|
128
|
+
if (input === args) {
|
|
129
|
+
input = { ...args };
|
|
130
|
+
delete input.file_urls;
|
|
131
|
+
}
|
|
132
|
+
if (hasInputSchema) {
|
|
133
|
+
input = applySchemaDefaults(input, pipeline.inputSchema);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const { jobId } = await service.executePipeline(pipeline.id, input, files);
|
|
137
|
+
const start = Date.now();
|
|
138
|
+
|
|
139
|
+
while (Date.now() - start < maxWait) {
|
|
140
|
+
const job: JobState = await service.getJobStatus(jobId);
|
|
141
|
+
if (job.status === 'completed') {
|
|
142
|
+
return { status: 'completed', result: job.finalResult };
|
|
143
|
+
} else if (job.status === 'failed' || job.status === 'timeout') {
|
|
144
|
+
return { status: job.status, error: job.error };
|
|
145
|
+
}
|
|
146
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
status: 'polling',
|
|
151
|
+
message: `Pipeline "${pipeline.name}" đang chạy, trả về job ID để theo dõi.`,
|
|
152
|
+
jobId,
|
|
153
|
+
};
|
|
154
|
+
} catch (err: any) {
|
|
155
|
+
return { error: err.message };
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Dynamic tools provider: queries all enabled pipelines and registers one tool per pipeline.
|
|
163
|
+
* Called on-demand by the AI tools manager each time tools are listed.
|
|
164
|
+
*/
|
|
165
|
+
export function createDynamicPipelineToolsProvider(service: DocumentUnderstandingService) {
|
|
166
|
+
return async (register: { registerTools: (options: any) => void }) => {
|
|
167
|
+
try {
|
|
168
|
+
if (!service.isReady()) return;
|
|
169
|
+
|
|
170
|
+
const pipelines = await service.listPipelines();
|
|
171
|
+
const enabledPipelines = pipelines.filter((p) => p.enabled);
|
|
172
|
+
|
|
173
|
+
if (enabledPipelines.length === 0) return;
|
|
174
|
+
|
|
175
|
+
const tools = enabledPipelines.map((pipeline) => createToolForPipeline(service, pipeline));
|
|
176
|
+
register.registerTools(tools);
|
|
177
|
+
} catch {
|
|
178
|
+
// Silently skip — service may not be initialized yet
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
package/src/server/types.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
export interface ServiceConfig {
|
|
2
|
-
id?: number;
|
|
3
|
-
baseUrl: string;
|
|
4
|
-
authType: 'api_key' | 'bearer' | 'custom_header' | 'none';
|
|
5
|
-
authKey?: string;
|
|
6
|
-
authHeaderName?: string;
|
|
7
|
-
defaultTimeout: number;
|
|
8
|
-
defaultRetries: number;
|
|
9
|
-
pollInterval: number;
|
|
10
|
-
pollTimeout: number;
|
|
11
|
-
webhookSecret?: string;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export type ClientServiceConfig = Omit<ServiceConfig, 'authKey' | 'webhookSecret'> & {
|
|
15
|
-
hasAuthKey: boolean;
|
|
16
|
-
hasWebhookSecret: boolean;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export interface EndpointDef {
|
|
20
|
-
id: number;
|
|
21
|
-
name: string;
|
|
22
|
-
subpath: string;
|
|
23
|
-
method: 'GET' | 'POST' | 'PUT';
|
|
24
|
-
description?: string;
|
|
25
|
-
requestBodySchema?: any;
|
|
26
|
-
responseSchema?: any;
|
|
27
|
-
fileInputMode: 'none' | 'multipart' | 'base64';
|
|
28
|
-
fileFieldName?: string;
|
|
29
|
-
maxFiles: number;
|
|
30
|
-
executionMode: 'sync' | 'polling' | 'webhook';
|
|
31
|
-
/** Discriminator field name sent in the request body/query (e.g. DUGate 'mode'/'type'/'task'/'action'/'process'). */
|
|
32
|
-
discriminatorField?: string;
|
|
33
|
-
/** Discriminator value for this endpoint (e.g. DUGate 'parse', 'invoice', 'disbursement'). */
|
|
34
|
-
discriminatorValue?: string;
|
|
35
|
-
/** When set, the request also appends '?sync=true' (DUGate synchronous mode). */
|
|
36
|
-
syncQueryParam?: string;
|
|
37
|
-
/** For 'sync' mode: DUGate returns 202 with a task ID. Extract the task ID with this path (e.g. 'name') or regex. */
|
|
38
|
-
taskIdExtractPath?: string;
|
|
39
|
-
/** For 'sync' mode: regex used to extract the task ID from a string field (e.g. 'operations/([^/]+)'). */
|
|
40
|
-
taskIdExtractRegex?: string;
|
|
41
|
-
pollResultSubpath?: string;
|
|
42
|
-
pollTaskIdField?: string;
|
|
43
|
-
pollResultField?: string;
|
|
44
|
-
pollInterval?: number;
|
|
45
|
-
pollTimeout?: number;
|
|
46
|
-
pollStatusField?: string;
|
|
47
|
-
pollCompletedValue?: string;
|
|
48
|
-
customHeaders?: Record<string, string>;
|
|
49
|
-
enabled: boolean;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface PipelineStepDef {
|
|
53
|
-
id: number;
|
|
54
|
-
pipelineId: number;
|
|
55
|
-
endpointId: number;
|
|
56
|
-
stepOrder: number;
|
|
57
|
-
name: string;
|
|
58
|
-
inputMapping?: Record<string, any>;
|
|
59
|
-
outputAlias?: string;
|
|
60
|
-
condition?: any;
|
|
61
|
-
onError: 'fail' | 'skip' | 'retry';
|
|
62
|
-
retryCount: number;
|
|
63
|
-
endpoint: EndpointDef; // Associated endpoint
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export interface PipelineDef {
|
|
67
|
-
id: number;
|
|
68
|
-
name: string;
|
|
69
|
-
description?: string;
|
|
70
|
-
inputSchema?: any;
|
|
71
|
-
outputMapping?: Record<string, any>;
|
|
72
|
-
enabled: boolean;
|
|
73
|
-
steps: PipelineStepDef[];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export interface JobState {
|
|
77
|
-
id: number;
|
|
78
|
-
pipelineId: number;
|
|
79
|
-
status: 'pending' | 'running' | 'polling' | 'completed' | 'failed' | 'timeout';
|
|
80
|
-
input: any;
|
|
81
|
-
currentStep: number;
|
|
82
|
-
stepResults: Record<string, any>;
|
|
83
|
-
finalResult?: any;
|
|
84
|
-
error?: string;
|
|
85
|
-
externalTaskIds: Record<string, string>;
|
|
86
|
-
startedAt?: Date;
|
|
87
|
-
completedAt?: Date;
|
|
88
|
-
ownedBy?: string;
|
|
89
|
-
leaseExpiresAt?: Date | null;
|
|
90
|
-
}
|
|
1
|
+
export interface ServiceConfig {
|
|
2
|
+
id?: number;
|
|
3
|
+
baseUrl: string;
|
|
4
|
+
authType: 'api_key' | 'bearer' | 'custom_header' | 'none';
|
|
5
|
+
authKey?: string;
|
|
6
|
+
authHeaderName?: string;
|
|
7
|
+
defaultTimeout: number;
|
|
8
|
+
defaultRetries: number;
|
|
9
|
+
pollInterval: number;
|
|
10
|
+
pollTimeout: number;
|
|
11
|
+
webhookSecret?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type ClientServiceConfig = Omit<ServiceConfig, 'authKey' | 'webhookSecret'> & {
|
|
15
|
+
hasAuthKey: boolean;
|
|
16
|
+
hasWebhookSecret: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export interface EndpointDef {
|
|
20
|
+
id: number;
|
|
21
|
+
name: string;
|
|
22
|
+
subpath: string;
|
|
23
|
+
method: 'GET' | 'POST' | 'PUT';
|
|
24
|
+
description?: string;
|
|
25
|
+
requestBodySchema?: any;
|
|
26
|
+
responseSchema?: any;
|
|
27
|
+
fileInputMode: 'none' | 'multipart' | 'base64';
|
|
28
|
+
fileFieldName?: string;
|
|
29
|
+
maxFiles: number;
|
|
30
|
+
executionMode: 'sync' | 'polling' | 'webhook';
|
|
31
|
+
/** Discriminator field name sent in the request body/query (e.g. DUGate 'mode'/'type'/'task'/'action'/'process'). */
|
|
32
|
+
discriminatorField?: string;
|
|
33
|
+
/** Discriminator value for this endpoint (e.g. DUGate 'parse', 'invoice', 'disbursement'). */
|
|
34
|
+
discriminatorValue?: string;
|
|
35
|
+
/** When set, the request also appends '?sync=true' (DUGate synchronous mode). */
|
|
36
|
+
syncQueryParam?: string;
|
|
37
|
+
/** For 'sync' mode: DUGate returns 202 with a task ID. Extract the task ID with this path (e.g. 'name') or regex. */
|
|
38
|
+
taskIdExtractPath?: string;
|
|
39
|
+
/** For 'sync' mode: regex used to extract the task ID from a string field (e.g. 'operations/([^/]+)'). */
|
|
40
|
+
taskIdExtractRegex?: string;
|
|
41
|
+
pollResultSubpath?: string;
|
|
42
|
+
pollTaskIdField?: string;
|
|
43
|
+
pollResultField?: string;
|
|
44
|
+
pollInterval?: number;
|
|
45
|
+
pollTimeout?: number;
|
|
46
|
+
pollStatusField?: string;
|
|
47
|
+
pollCompletedValue?: string;
|
|
48
|
+
customHeaders?: Record<string, string>;
|
|
49
|
+
enabled: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface PipelineStepDef {
|
|
53
|
+
id: number;
|
|
54
|
+
pipelineId: number;
|
|
55
|
+
endpointId: number;
|
|
56
|
+
stepOrder: number;
|
|
57
|
+
name: string;
|
|
58
|
+
inputMapping?: Record<string, any>;
|
|
59
|
+
outputAlias?: string;
|
|
60
|
+
condition?: any;
|
|
61
|
+
onError: 'fail' | 'skip' | 'retry';
|
|
62
|
+
retryCount: number;
|
|
63
|
+
endpoint: EndpointDef; // Associated endpoint
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface PipelineDef {
|
|
67
|
+
id: number;
|
|
68
|
+
name: string;
|
|
69
|
+
description?: string;
|
|
70
|
+
inputSchema?: any;
|
|
71
|
+
outputMapping?: Record<string, any>;
|
|
72
|
+
enabled: boolean;
|
|
73
|
+
steps: PipelineStepDef[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface JobState {
|
|
77
|
+
id: number;
|
|
78
|
+
pipelineId: number;
|
|
79
|
+
status: 'pending' | 'running' | 'polling' | 'completed' | 'failed' | 'timeout';
|
|
80
|
+
input: any;
|
|
81
|
+
currentStep: number;
|
|
82
|
+
stepResults: Record<string, any>;
|
|
83
|
+
finalResult?: any;
|
|
84
|
+
error?: string;
|
|
85
|
+
externalTaskIds: Record<string, string>;
|
|
86
|
+
startedAt?: Date;
|
|
87
|
+
completedAt?: Date;
|
|
88
|
+
ownedBy?: string;
|
|
89
|
+
leaseExpiresAt?: Date | null;
|
|
90
|
+
}
|