plugin-document-understanding 1.0.2 → 1.1.1

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 (34) hide show
  1. package/README.md +16 -366
  2. package/dist/client/components/EndpointsTab.d.ts +2 -0
  3. package/dist/client/components/JobsTab.d.ts +2 -0
  4. package/dist/client/components/PipelinesTab.d.ts +2 -0
  5. package/dist/client/components/PluginSettings.d.ts +2 -0
  6. package/dist/client/components/ServiceConfigTab.d.ts +2 -0
  7. package/dist/client/index.d.ts +1 -0
  8. package/dist/client/index.js +1 -1
  9. package/dist/client/locale.d.ts +10 -0
  10. package/dist/client/models/index.d.ts +11 -0
  11. package/dist/client/plugin.d.ts +6 -0
  12. package/dist/externalVersion.js +6 -6
  13. package/dist/index.d.ts +2 -0
  14. package/dist/locale/en-US.json +121 -121
  15. package/dist/locale/vi-VN.json +121 -121
  16. package/dist/server/actions.d.ts +17 -0
  17. package/dist/server/collections/config.d.ts +3 -0
  18. package/dist/server/collections/endpoints.d.ts +3 -0
  19. package/dist/server/collections/jobs.d.ts +3 -0
  20. package/dist/server/collections/pipeline-steps.d.ts +3 -0
  21. package/dist/server/collections/pipelines.d.ts +3 -0
  22. package/dist/server/index.d.ts +1 -0
  23. package/dist/server/plugin.d.ts +18 -0
  24. package/dist/server/plugin.js +3 -1
  25. package/dist/server/services/AsyncJobManager.d.ts +23 -0
  26. package/dist/server/services/DocumentUnderstandingService.d.ts +35 -0
  27. package/dist/server/services/DocumentUnderstandingService.js +37 -5
  28. package/dist/server/services/ExternalApiClient.d.ts +28 -0
  29. package/dist/server/services/ExternalApiClient.js +20 -3
  30. package/dist/server/services/PipelineExecutor.d.ts +23 -0
  31. package/dist/server/tools/document-understanding-tool.d.ts +8 -0
  32. package/dist/server/tools/document-understanding-tool.js +37 -3
  33. package/dist/server/types.d.ts +68 -0
  34. package/package.json +1 -1
package/README.md CHANGED
@@ -1,366 +1,16 @@
1
- # @nocobase/plugin-document-understanding
2
-
3
- Generic Document Processing Pipeline Orchestrator cho NocoBase 2.x.
4
- Plugin cho phep cau hinh va thuc thi cac pipeline xu ly tai lieu thong qua external API services (OCR, classification, extraction, ...).
5
-
6
- ---
7
-
8
- ## Yeu cau
9
-
10
- - NocoBase 2.x
11
- - (Tuy chon) `@nocobase/plugin-ai` neu muon su dung AI tool integration
12
-
13
- ---
14
-
15
- ## Cai dat
16
-
17
- ```bash
18
- yarn nocobase pm add @nocobase/plugin-document-understanding
19
- ```
20
-
21
- Sau do vao **Settings > Plugin Manager**, tim plugin **Document Understanding** va bat **Enable**.
22
-
23
- ---
24
-
25
- ## Huong dan su dung
26
-
27
- Sau khi enable plugin, vao **Settings > Document Understanding** se thay giao dien voi 4 tab.
28
-
29
- ### Buoc 1: Cau hinh Service (Tab "Service Config")
30
-
31
- Day la buoc bat buoc dau tien — plugin can biet external service cua ban o dau va xac thuc nhu the nao.
32
-
33
- 1. Mo tab **Service Config**
34
- 2. Dien cac truong:
35
-
36
- | Truong | Mo ta | Vi du |
37
- |--------|-------|-------|
38
- | **Base URL** | URL goc cua external document processing service | `http://my-ocr-service:8000` |
39
- | **Auth Type** | Phuong thuc xac thuc | `None`, `API Key`, `Bearer Token`, `Custom Header` |
40
- | **API Key / Token** | Key hoac token (hien thi khi Auth Type khac None) | `sk-abc123...` |
41
- | **Custom Header Name** | Ten header tuy chinh (khi chon Custom Header) | `X-Api-Key` |
42
- | **Default Request Timeout** | Timeout mac dinh cho moi request (ms) | `30000` |
43
- | **Default Retries** | So lan thu lai mac dinh khi loi | `2` |
44
- | **Default Poll Interval** | Khoang thoi gian giua cac lan poll (ms) | `5000` |
45
- | **Default Poll Timeout** | Thoi gian toi da cho poll (ms) | `300000` |
46
- | **Webhook Secret** | Secret de verify webhook callback tu external service | (tuy chon) |
47
-
48
- 3. Nhan **Save Configuration**
49
-
50
- ### Buoc 2: Dang ky Endpoints (Tab "Endpoints")
51
-
52
- Moi endpoint dai dien cho mot chuc nang cua external service (OCR, classify, extract, ...).
53
-
54
- 1. Mo tab **Endpoints**
55
- 2. Nhan **Add Endpoint**
56
- 3. Dien thong tin:
57
-
58
- | Truong | Mo ta | Vi du |
59
- |--------|-------|-------|
60
- | **Unique Name** | Ten dinh danh (khong trung) | `ocr`, `classify`, `extract_table` |
61
- | **Subpath** | Duong dan API tuong doi voi Base URL | `/api/v1/ocr` |
62
- | **HTTP Method** | `GET` hoac `POST` | `POST` |
63
- | **Execution Mode** | Cach thuc thi | Xem ben duoi |
64
- | **File Input Mode** | Cach gui file | `None`, `Multipart`, `Base64` |
65
- | **Enabled** | Bat/tat endpoint | `true` |
66
-
67
- **Execution Mode giai thich:**
68
-
69
- - **Synchronous**: Goi API, nhan ket qua ngay trong response. Phu hop cho cac task nhanh (< 30s).
70
- - **Polling**: Goi API nhan `task_id`, sau do plugin tu dong poll ket qua theo interval. Dien them:
71
- - `Poll Task ID Field`: truong trong response chua task_id (mac dinh: `task_id`)
72
- - `Poll Result Field`: truong chua ket qua khi poll thanh cong
73
- - `Poll Status Field` (tuy chon): truong chua trang thai, kiem tra xem task da xong chua
74
- - `Poll Completed Value` (tuy chon): gia tri cua status field khi hoan thanh (mac dinh: `completed`)
75
- - **Webhook**: Goi API nhan `task_id`, external service se goi lai webhook khi xong. Plugin tu dong xu ly callback.
76
-
77
- **Vi du: Dang ky endpoint OCR dong bo**
78
-
79
- ```
80
- Name: ocr
81
- Subpath: /api/v1/ocr
82
- Method: POST
83
- Execution Mode: Synchronous
84
- File Input: Multipart
85
- Enabled: true
86
- ```
87
-
88
- **Vi du: Dang ky endpoint classify bat dong bo (polling)**
89
-
90
- ```
91
- Name: classify
92
- Subpath: /api/v1/classify
93
- Method: POST
94
- Execution Mode: Polling
95
- Poll Task ID: task_id
96
- Poll Result Field: result
97
- File Input: None
98
- Enabled: true
99
- ```
100
-
101
- ### Buoc 3: Tao Pipeline (Tab "Pipelines")
102
-
103
- Pipeline la mot chuoi cac buoc (steps) goi lan luot cac endpoints da dang ky.
104
-
105
- 1. Mo tab **Pipelines**
106
- 2. Nhan **Add Pipeline**
107
- 3. Dien:
108
- - **Name**: ten pipeline (vi du: `full_document_processing`)
109
- - **Description**: mo ta
110
- - **Enabled**: bat
111
-
112
- **Cau hinh Steps (thong qua API hoac JSON):**
113
-
114
- Hien tai giao dien UI cua Pipelines Tab chi ho tro CRUD co ban (name, description, enabled).
115
- De cau hinh steps chi tiet, su dung REST API:
116
-
117
- ```bash
118
- # Tao pipeline voi steps
119
- curl -X POST http://localhost:13000/api/docUnderstanding:createPipeline \
120
- -H "Content-Type: application/json" \
121
- -H "Authorization: Bearer <your-nocobase-token>" \
122
- -d '{
123
- "name": "full_document_processing",
124
- "description": "OCR roi classify roi extract",
125
- "enabled": true,
126
- "steps": [
127
- {
128
- "stepOrder": 1,
129
- "name": "OCR",
130
- "endpointId": 1,
131
- "inputMapping": {},
132
- "outputAlias": "ocr_result",
133
- "onError": "fail"
134
- },
135
- {
136
- "stepOrder": 2,
137
- "name": "Classify",
138
- "endpointId": 2,
139
- "inputMapping": {
140
- "text": "$step[ocr_result].response.text"
141
- },
142
- "outputAlias": "classify_result",
143
- "onError": "fail"
144
- },
145
- {
146
- "stepOrder": 3,
147
- "name": "Extract Table",
148
- "endpointId": 3,
149
- "inputMapping": {
150
- "text": "$step[ocr_result].response.text",
151
- "doc_type": "$step[classify_result].response.category"
152
- },
153
- "outputAlias": "extract_result",
154
- "onError": "skip",
155
- "condition": {
156
- "field": "$step[classify_result].response.category",
157
- "op": "eq",
158
- "value": "invoice"
159
- }
160
- }
161
- ]
162
- }'
163
- ```
164
-
165
- **Input Mapping DSL:**
166
-
167
- | Cu phap | Mo ta | Vi du |
168
- |---------|-------|-------|
169
- | `$input.fieldName` | Lay tu input goc cua pipeline | `$input.document_url` |
170
- | `$step[alias].response.path` | Lay tu ket qua step truoc (theo alias) | `$step[ocr_result].response.text` |
171
- | `$step[1].response.path` | Lay tu ket qua step truoc (theo so thu tu) | `$step[1].response.text` |
172
- | `$files` | Toan bo files dau vao | `$files` |
173
- | Gia tri co dinh | String literal | `"invoice"` |
174
-
175
- **Condition operators ho tro:** `eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `in`, `contains`
176
-
177
- **onError options:** `fail` (dung pipeline), `skip` (bo qua step), `retry` (thu lai theo retryCount)
178
-
179
- ### Buoc 4: Chay Pipeline
180
-
181
- **Cach 1: Qua REST API**
182
-
183
- ```bash
184
- # Chay pipeline (tham so la pipelineId)
185
- curl -X POST http://localhost:13000/api/docUnderstanding:executePipeline \
186
- -H "Content-Type: application/json" \
187
- -H "Authorization: Bearer <your-nocobase-token>" \
188
- -d '{
189
- "pipelineId": 1,
190
- "input": {
191
- "document_url": "https://example.com/invoice.pdf"
192
- }
193
- }'
194
-
195
- # Response:
196
- # { "jobId": 42 }
197
- ```
198
-
199
- ```bash
200
- # Kiem tra trang thai job
201
- curl http://localhost:13000/api/docUnderstanding:getJobStatus?jobId=42 \
202
- -H "Authorization: Bearer <your-nocobase-token>"
203
-
204
- # Response:
205
- # {
206
- # "id": 42,
207
- # "status": "completed",
208
- # "stepResults": { "ocr_result": {...}, "classify_result": {...} },
209
- # "finalResult": {...}
210
- # }
211
- ```
212
-
213
- **Cach 2: Qua AI Employee (neu da cai plugin-ai)**
214
-
215
- Khi `@nocobase/plugin-ai` duoc bat, **moi pipeline enabled se tu dong tro thanh mot AI tool/skill rieng biet**.
216
- AI employee co the chon va goi truc tiep pipeline phu hop ma khong can biet pipeline name.
217
-
218
- **Cach hoat dong:**
219
-
220
- - Plugin dang ky mot **dynamic tools provider** voi AI tools manager.
221
- - Moi khi AI can danh sach tools, provider se query tat ca pipeline co `enabled = true` va tao 1 tool cho moi pipeline.
222
- - Tool name duoc sinh tu pipeline name: `doc_understanding.<sanitized_pipeline_name>`
223
- - Vi du: pipeline "Full Document Processing" → tool `doc_understanding.full_document_processing`
224
- - Moi tool co:
225
- - **Title**: `Document: <pipeline name>`
226
- - **Description**: mo ta pipeline + danh sach steps (vi du: "OCR → Classify → Extract")
227
- - **Schema**: lay tu `inputSchema` cua pipeline (neu co), hoac fallback la `{ input: object }`
228
-
229
- **Vi du: Ban co 3 pipelines enabled:**
230
-
231
- | Pipeline | Tool name | Mo ta AI nhin thay |
232
- |----------|-----------|-------------------|
233
- | OCR Only | `doc_understanding.ocr_only` | "Thuc hien OCR tren tai lieu. Steps: OCR" |
234
- | Invoice Processing | `doc_understanding.invoice_processing` | "Xu ly hoa don. Steps: OCR → Classify → Extract Table" |
235
- | Contract Analysis | `doc_understanding.contract_analysis` | "Phan tich hop dong. Steps: OCR → NER → Summarize" |
236
-
237
- **Khi nguoi dung hoi AI employee:**
238
-
239
- ```
240
- User: "Phan tich hoa don nay giup toi"
241
-
242
- AI tu dong chon tool: doc_understanding.invoice_processing
243
- - input: { document_url: "..." }
244
-
245
- → Tra ve ket qua JSON sau khi pipeline chay xong (poll toi da 30s)
246
- → Neu chua xong, tra ve jobId de theo doi
247
- ```
248
-
249
- **Luu y:**
250
- - Chi pipeline co `enabled = true` moi duoc dang ky lam tool
251
- - Khi ban tao/sua/xoa/bat/tat pipeline, danh sach tools tu dong cap nhat (dynamic, khong can restart)
252
- - Neu pipeline co `inputSchema` duoc dinh nghia, AI se biet chinh xac can truyen tham so gi
253
- - Moi tool hoat dong doc lap — AI employee co the chon dung tool phu hop theo ngu canh
254
-
255
- **Tip: Dinh nghia inputSchema cho pipeline de AI hieu ro input can thiet:**
256
-
257
- ```bash
258
- curl -X POST http://localhost:13000/api/docUnderstanding:updatePipeline/1 \
259
- -H "Content-Type: application/json" \
260
- -H "Authorization: Bearer <your-nocobase-token>" \
261
- -d '{
262
- "inputSchema": {
263
- "type": "object",
264
- "properties": {
265
- "document_url": {
266
- "type": "string",
267
- "description": "URL cua tai lieu can xu ly"
268
- },
269
- "language": {
270
- "type": "string",
271
- "enum": ["vi", "en", "ja"],
272
- "description": "Ngon ngu cua tai lieu"
273
- }
274
- },
275
- "required": ["document_url"]
276
- }
277
- }'
278
- ```
279
-
280
- Khi do AI se biet chinh xac can `document_url` (bat buoc) va `language` (tuy chon) khi goi tool.
281
-
282
- ### Buoc 5: Theo doi Jobs (Tab "Jobs history")
283
-
284
- 1. Mo tab **Jobs history**
285
- 2. Xem danh sach jobs voi trang thai:
286
- - **pending**: Dang cho
287
- - **running**: Dang chay
288
- - **polling**: Dang cho ket qua tu external service
289
- - **completed**: Hoan thanh
290
- - **failed**: That bai
291
- - **timeout**: Het thoi gian cho
292
- 3. Nhan **View Details** de xem chi tiet:
293
- - Input da gui
294
- - Ket qua tung step
295
- - Ket qua cuoi cung
296
- - Loi (neu co)
297
-
298
- Tab tu dong refresh moi 10 giay.
299
-
300
- ---
301
-
302
- ## Webhook Callback
303
-
304
- Neu endpoint su dung execution mode **Webhook**, external service can goi callback ve:
305
-
306
- ```
307
- POST http://localhost:13000/api/docUnderstanding:webhookCallback
308
- Content-Type: application/json
309
- X-Webhook-Signature: sha256=<hmac-hex> (neu co cau hinh Webhook Secret)
310
-
311
- {
312
- "task_id": "abc-123",
313
- "result": { ... }
314
- }
315
- ```
316
-
317
- Plugin se tu dong match `task_id` voi job dang cho va tiep tuc pipeline.
318
-
319
- ---
320
-
321
- ## REST API Reference
322
-
323
- | Action | Method | URL |
324
- |--------|--------|-----|
325
- | Lay config | GET | `/api/docUnderstanding:getConfig` |
326
- | Cap nhat config | POST | `/api/docUnderstanding:updateConfig` |
327
- | Danh sach endpoints | GET | `/api/docUnderstanding:listEndpoints` |
328
- | Tao endpoint | POST | `/api/docUnderstanding:createEndpoint` |
329
- | Sua endpoint | POST | `/api/docUnderstanding:updateEndpoint/{id}` |
330
- | Xoa endpoint | DELETE | `/api/docUnderstanding:deleteEndpoint/{id}` |
331
- | Danh sach pipelines | GET | `/api/docUnderstanding:listPipelines` |
332
- | Tao pipeline | POST | `/api/docUnderstanding:createPipeline` |
333
- | Sua pipeline | POST | `/api/docUnderstanding:updatePipeline/{id}` |
334
- | Xoa pipeline | DELETE | `/api/docUnderstanding:deletePipeline/{id}` |
335
- | Chay pipeline | POST | `/api/docUnderstanding:executePipeline` |
336
- | Trang thai job | GET | `/api/docUnderstanding:getJobStatus?jobId={id}` |
337
- | Danh sach jobs | GET | `/api/docUnderstanding:listJobs` |
338
- | Webhook callback | POST | `/api/docUnderstanding:webhookCallback` |
339
-
340
- ---
341
-
342
- ## Vi du: Pipeline OCR + Extract hoan chinh
343
-
344
- ```
345
- 1. Cau hinh Service Config:
346
- Base URL: http://docai-service:8000
347
- Auth Type: API Key
348
- API Key: sk-my-secret-key
349
-
350
- 2. Tao 2 endpoints:
351
- - "ocr": POST /ocr, Sync, Multipart
352
- - "extract": POST /extract, Sync, None
353
-
354
- 3. Tao pipeline "ocr_and_extract" voi 2 steps:
355
- Step 1: endpoint=ocr, outputAlias=ocr_out, onError=fail
356
- Step 2: endpoint=extract, inputMapping={ "text": "$step[ocr_out].response.text" },
357
- outputAlias=extract_out, onError=fail
358
-
359
- 4. Chay:
360
- POST /api/docUnderstanding:executePipeline
361
- { "pipelineId": 1, "input": {} }
362
- (gui file qua multipart neu can)
363
-
364
- 5. Kiem tra ket qua:
365
- GET /api/docUnderstanding:getJobStatus?jobId=1
366
- ```
1
+ # plugin-document-understanding
2
+
3
+ ## Overview
4
+ Provides advanced document understanding and structured data extraction capabilities.
5
+
6
+ ## Features
7
+ - **Schema-Based Extraction**: Define a NocoBase collection, and the AI will extract data from documents to fill the fields.
8
+ - **Batch Processing**: Process multiple documents in an automated workflow.
9
+ - **Human-in-the-loop**: UI to review and approve extracted fields before committing to the database.
10
+
11
+ ## Usage
12
+ 1. Enable the plugin.
13
+ 2. Create a collection (e.g., "Invoices" with fields: Total, Date, Vendor).
14
+ 3. Set up a Document Understanding task linked to this collection.
15
+ 4. Upload documents.
16
+ 5. The system will extract the data. Review the pending records in the UI and click "Approve" to save them.
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const EndpointsTab: () => React.JSX.Element;
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const JobsTab: () => React.JSX.Element;
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const PipelinesTab: () => React.JSX.Element;
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const PluginSettings: () => React.JSX.Element;
@@ -0,0 +1,2 @@
1
+ import React from 'react';
2
+ export declare const ServiceConfigTab: () => React.JSX.Element;
@@ -0,0 +1 @@
1
+ export { default } from './plugin';