@docmana/sdk 0.46.0 → 0.48.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.
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -21
- package/README.md +55 -0
- package/dist/cli.mjs +466 -218
- package/dist/cli.mjs.map +1 -1
- package/dist/execution-events.d.ts +2 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +490 -234
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +486 -234
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +28 -2
- package/package.json +1 -1
package/dist/cli.mjs
CHANGED
|
@@ -190,6 +190,343 @@ var FormulaAuthoring = class {
|
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
192
|
|
|
193
|
+
// src/sdk/errors.ts
|
|
194
|
+
var DocmanaError = class extends Error {
|
|
195
|
+
status;
|
|
196
|
+
requestId;
|
|
197
|
+
code;
|
|
198
|
+
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
199
|
+
apiCode;
|
|
200
|
+
reviewConflict;
|
|
201
|
+
constructor(message2, opts = {}) {
|
|
202
|
+
super(message2);
|
|
203
|
+
this.name = new.target.name;
|
|
204
|
+
this.status = opts.status;
|
|
205
|
+
this.requestId = opts.requestId;
|
|
206
|
+
this.code = opts.code ?? "docmana_error";
|
|
207
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var DocmanaAuthError = class extends DocmanaError {
|
|
211
|
+
constructor(message2, opts = {}) {
|
|
212
|
+
super(message2, { ...opts, code: "auth_error" });
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
var DocmanaPermissionError = class extends DocmanaError {
|
|
216
|
+
constructor(message2, opts = {}) {
|
|
217
|
+
super(message2, { ...opts, code: "permission_error" });
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
var DocmanaNotFoundError = class extends DocmanaError {
|
|
221
|
+
constructor(message2, opts = {}) {
|
|
222
|
+
super(message2, { ...opts, code: "not_found" });
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
var DocmanaRequestError = class extends DocmanaError {
|
|
226
|
+
constructor(message2, opts = {}) {
|
|
227
|
+
super(message2, { ...opts, code: "bad_request" });
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
231
|
+
constructor(message2, opts = {}) {
|
|
232
|
+
super(message2, { ...opts, code: "payload_too_large" });
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var DocmanaExecutionError = class extends DocmanaError {
|
|
236
|
+
errors;
|
|
237
|
+
result;
|
|
238
|
+
constructor(message2, errors = [], result) {
|
|
239
|
+
super(message2, { code: "execution_failed" });
|
|
240
|
+
this.errors = errors;
|
|
241
|
+
this.result = result;
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
245
|
+
jobId;
|
|
246
|
+
error;
|
|
247
|
+
result;
|
|
248
|
+
constructor(message2, result) {
|
|
249
|
+
super(message2, { code: "fraud_detection_failed" });
|
|
250
|
+
this.jobId = result.jobId;
|
|
251
|
+
this.error = result.error;
|
|
252
|
+
this.result = result;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
var DocmanaTimeoutError = class extends DocmanaError {
|
|
256
|
+
constructor(message2) {
|
|
257
|
+
super(message2, { code: "timeout" });
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
var DocmanaAbortError = class extends DocmanaError {
|
|
261
|
+
constructor(message2) {
|
|
262
|
+
super(message2, { code: "aborted" });
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
var DocmanaRateLimitError = class extends DocmanaError {
|
|
266
|
+
retryAfterMs;
|
|
267
|
+
constructor(message2, opts = {}) {
|
|
268
|
+
super(message2, { ...opts, code: "rate_limited" });
|
|
269
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
273
|
+
retryAfterMs;
|
|
274
|
+
constructor(message2, opts = {}) {
|
|
275
|
+
super(message2, { ...opts, code: "service_unavailable" });
|
|
276
|
+
this.retryAfterMs = opts.retryAfterMs;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
function errorFromResponse(status, message2, requestId, retryAfterMs) {
|
|
280
|
+
const opts = { status, requestId };
|
|
281
|
+
switch (status) {
|
|
282
|
+
case 400:
|
|
283
|
+
return new DocmanaRequestError(message2, opts);
|
|
284
|
+
case 401:
|
|
285
|
+
return new DocmanaAuthError(message2, opts);
|
|
286
|
+
case 403:
|
|
287
|
+
return new DocmanaPermissionError(message2, opts);
|
|
288
|
+
case 404:
|
|
289
|
+
return new DocmanaNotFoundError(message2, opts);
|
|
290
|
+
case 413:
|
|
291
|
+
return new DocmanaPayloadTooLargeError(message2, opts);
|
|
292
|
+
case 429:
|
|
293
|
+
return new DocmanaRateLimitError(message2, { ...opts, retryAfterMs });
|
|
294
|
+
case 503:
|
|
295
|
+
return new DocmanaServiceUnavailableError(message2, { ...opts, retryAfterMs });
|
|
296
|
+
default:
|
|
297
|
+
return new DocmanaError(message2, opts);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// src/sdk/review-events.ts
|
|
302
|
+
import { createHmac, timingSafeEqual } from "crypto";
|
|
303
|
+
var REVIEW_EVENT_TYPES = [
|
|
304
|
+
"execution.awaiting_review",
|
|
305
|
+
"execution.review_done",
|
|
306
|
+
"execution.review_closed",
|
|
307
|
+
"execution.review_expired"
|
|
308
|
+
];
|
|
309
|
+
var ReviewEventVerificationError = class extends Error {
|
|
310
|
+
constructor(reason) {
|
|
311
|
+
super(`Invalid review event ${reason}`);
|
|
312
|
+
this.reason = reason;
|
|
313
|
+
this.name = "ReviewEventVerificationError";
|
|
314
|
+
}
|
|
315
|
+
reason;
|
|
316
|
+
};
|
|
317
|
+
function parseReviewEventCallback(value) {
|
|
318
|
+
const fail2 = () => {
|
|
319
|
+
throw new ReviewEventVerificationError("payload");
|
|
320
|
+
};
|
|
321
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return fail2();
|
|
322
|
+
const raw = value;
|
|
323
|
+
const text2 = (key) => {
|
|
324
|
+
const v = raw[key];
|
|
325
|
+
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
326
|
+
return fail2();
|
|
327
|
+
return v;
|
|
328
|
+
};
|
|
329
|
+
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
330
|
+
return fail2();
|
|
331
|
+
const event = {
|
|
332
|
+
schemaVersion: 1,
|
|
333
|
+
eventId: text2("eventId"),
|
|
334
|
+
type: raw.type,
|
|
335
|
+
organization: text2("organization"),
|
|
336
|
+
executionResultId: text2("executionResultId"),
|
|
337
|
+
reviewId: text2("reviewId"),
|
|
338
|
+
publishedAt: text2("publishedAt")
|
|
339
|
+
};
|
|
340
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
341
|
+
for (const key of ["flowId", "nodeId"])
|
|
342
|
+
if (raw[key] !== void 0) event[key] = text2(key);
|
|
343
|
+
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
344
|
+
if (raw[key] === void 0) continue;
|
|
345
|
+
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail2();
|
|
346
|
+
event[key] = Number(raw[key]);
|
|
347
|
+
}
|
|
348
|
+
if (raw.documentKeys !== void 0) {
|
|
349
|
+
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
350
|
+
return fail2();
|
|
351
|
+
event.documentKeys = raw.documentKeys;
|
|
352
|
+
}
|
|
353
|
+
if (raw.decision !== void 0) {
|
|
354
|
+
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail2();
|
|
355
|
+
event.decision = raw.decision;
|
|
356
|
+
}
|
|
357
|
+
if (raw.reviewStatus !== void 0) {
|
|
358
|
+
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail2();
|
|
359
|
+
event.reviewStatus = raw.reviewStatus;
|
|
360
|
+
}
|
|
361
|
+
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
362
|
+
return fail2();
|
|
363
|
+
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail2();
|
|
364
|
+
return event;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// src/sdk/resume-progress.ts
|
|
368
|
+
var EXECUTION_PROGRESS_TYPE = "execution.progress";
|
|
369
|
+
var PROGRESS_MESSAGE_KEYS = [
|
|
370
|
+
"resume.running",
|
|
371
|
+
"resume.awaitingReview",
|
|
372
|
+
"resume.completed",
|
|
373
|
+
"resume.rejected",
|
|
374
|
+
"resume.failed",
|
|
375
|
+
"node.pending",
|
|
376
|
+
"node.running",
|
|
377
|
+
"node.done",
|
|
378
|
+
"node.failed",
|
|
379
|
+
"node.reused",
|
|
380
|
+
"node.skipped",
|
|
381
|
+
"item.pending",
|
|
382
|
+
"item.running",
|
|
383
|
+
"item.llmCall",
|
|
384
|
+
"item.llmCallRetry",
|
|
385
|
+
"item.subagent",
|
|
386
|
+
"item.subagentBatch",
|
|
387
|
+
"item.ocrCall",
|
|
388
|
+
"item.done",
|
|
389
|
+
"item.failed",
|
|
390
|
+
"item.reused",
|
|
391
|
+
"item.skippedAncestorFailed",
|
|
392
|
+
"item.skippedBranchNotTaken"
|
|
393
|
+
];
|
|
394
|
+
var fail = () => {
|
|
395
|
+
throw new ReviewEventVerificationError("payload");
|
|
396
|
+
};
|
|
397
|
+
function record(value) {
|
|
398
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : fail();
|
|
399
|
+
}
|
|
400
|
+
function text(value) {
|
|
401
|
+
return typeof value === "string" && value.length > 0 && value.length <= 512 && [...value].every((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127) ? value : fail();
|
|
402
|
+
}
|
|
403
|
+
function instant(value) {
|
|
404
|
+
const v = text(value);
|
|
405
|
+
return /^\d{4}-\d{2}-\d{2}T.+(?:Z|[+-]\d{2}:\d{2})$/.test(v) && Number.isFinite(Date.parse(v)) ? v : fail();
|
|
406
|
+
}
|
|
407
|
+
var integer = (v, min = 0) => Number.isSafeInteger(v) && Number(v) >= min ? Number(v) : fail();
|
|
408
|
+
var bool = (v) => typeof v === "boolean" ? v : fail();
|
|
409
|
+
function choice(v, values) {
|
|
410
|
+
return values.includes(v) ? v : fail();
|
|
411
|
+
}
|
|
412
|
+
function array(v, max) {
|
|
413
|
+
return Array.isArray(v) && v.length <= max ? v : fail();
|
|
414
|
+
}
|
|
415
|
+
var state = (v) => choice(v, ["pending", "running", "done", "failed", "skipped", "reused"]);
|
|
416
|
+
function message(value, prefix) {
|
|
417
|
+
const raw = record(value), p = record(raw.params), params = {};
|
|
418
|
+
const key = choice(raw.key, PROGRESS_MESSAGE_KEYS);
|
|
419
|
+
if (!key.startsWith(prefix + ".")) return fail();
|
|
420
|
+
if (prefix !== "resume") params.nodeId = text(p.nodeId);
|
|
421
|
+
if (prefix === "node") {
|
|
422
|
+
params.done = integer(p.done);
|
|
423
|
+
params.total = integer(p.total);
|
|
424
|
+
}
|
|
425
|
+
if (prefix === "item") {
|
|
426
|
+
params.docKey = text(p.docKey);
|
|
427
|
+
params.branchId = p.branchId === null ? null : text(p.branchId);
|
|
428
|
+
for (const k of ["attempt", "batch", "batches"])
|
|
429
|
+
if (p[k] !== void 0) params[k] = integer(p[k], 1);
|
|
430
|
+
}
|
|
431
|
+
return { key, params };
|
|
432
|
+
}
|
|
433
|
+
function parseExecutionProgress(value) {
|
|
434
|
+
const raw = record(value);
|
|
435
|
+
if (raw.schemaVersion !== 1 || raw.type !== EXECUTION_PROGRESS_TYPE) return fail();
|
|
436
|
+
const result = {
|
|
437
|
+
schemaVersion: 1,
|
|
438
|
+
type: EXECUTION_PROGRESS_TYPE,
|
|
439
|
+
eventId: text(raw.eventId),
|
|
440
|
+
organization: text(raw.organization),
|
|
441
|
+
executionResultId: text(raw.executionResultId),
|
|
442
|
+
flowId: text(raw.flowId),
|
|
443
|
+
flowVersion: raw.flowVersion === null ? null : integer(raw.flowVersion, 1),
|
|
444
|
+
reviewId: text(raw.reviewId),
|
|
445
|
+
revision: instant(raw.revision),
|
|
446
|
+
stateVersion: integer(raw.stateVersion, 1),
|
|
447
|
+
phase: choice(raw.phase, [
|
|
448
|
+
"running",
|
|
449
|
+
"awaiting_review",
|
|
450
|
+
"completed",
|
|
451
|
+
"rejected",
|
|
452
|
+
"failed"
|
|
453
|
+
]),
|
|
454
|
+
message: message(raw.message, "resume"),
|
|
455
|
+
updatedAt: instant(raw.updatedAt),
|
|
456
|
+
itemsTruncated: bool(raw.itemsTruncated),
|
|
457
|
+
nodesTruncated: bool(raw.nodesTruncated),
|
|
458
|
+
nodes: array(raw.nodes, 5e3).map((v) => {
|
|
459
|
+
const n = record(v), nodeId = text(n.nodeId), documents = record(n.documents), m = message(n.message, "node");
|
|
460
|
+
if (m.params.nodeId !== nodeId) return fail();
|
|
461
|
+
return {
|
|
462
|
+
nodeId,
|
|
463
|
+
nodeType: text(n.nodeType),
|
|
464
|
+
status: state(n.status),
|
|
465
|
+
message: m,
|
|
466
|
+
documents: {
|
|
467
|
+
total: integer(documents.total),
|
|
468
|
+
pending: integer(documents.pending),
|
|
469
|
+
running: integer(documents.running),
|
|
470
|
+
done: integer(documents.done),
|
|
471
|
+
failed: integer(documents.failed),
|
|
472
|
+
skipped: integer(documents.skipped),
|
|
473
|
+
reused: integer(documents.reused)
|
|
474
|
+
},
|
|
475
|
+
items: array(n.items, 1e4).map((v2) => {
|
|
476
|
+
const i = record(v2), a = i.activity === null ? null : record(i.activity), m2 = message(i.message, "item");
|
|
477
|
+
const documentKey = text(i.documentKey), branchId = i.branchId === null ? null : text(i.branchId);
|
|
478
|
+
if (m2.params.nodeId !== nodeId || m2.params.docKey !== documentKey || m2.params.branchId !== branchId)
|
|
479
|
+
return fail();
|
|
480
|
+
return {
|
|
481
|
+
documentKey,
|
|
482
|
+
branchId,
|
|
483
|
+
status: state(i.status),
|
|
484
|
+
message: m2,
|
|
485
|
+
startedAt: i.startedAt === null ? null : instant(i.startedAt),
|
|
486
|
+
endedAt: i.endedAt === null ? null : instant(i.endedAt),
|
|
487
|
+
activity: a === null ? null : {
|
|
488
|
+
kind: choice(a.kind, ["llm_call", "subagent", "ocr_call"]),
|
|
489
|
+
attempt: a.attempt === null ? null : integer(a.attempt, 1),
|
|
490
|
+
batch: a.batch === null ? null : integer(a.batch, 1),
|
|
491
|
+
batches: a.batches === null ? null : integer(a.batches, 1)
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
})
|
|
495
|
+
};
|
|
496
|
+
}),
|
|
497
|
+
...raw.publishedAt === void 0 ? {} : { publishedAt: instant(raw.publishedAt) }
|
|
498
|
+
};
|
|
499
|
+
const phaseKey = result.phase === "awaiting_review" ? "awaitingReview" : result.phase;
|
|
500
|
+
if (result.message.key !== `resume.${phaseKey}` || result.eventId !== `${result.executionResultId}:${result.revision}:${result.stateVersion}`)
|
|
501
|
+
return fail();
|
|
502
|
+
return result;
|
|
503
|
+
}
|
|
504
|
+
function parseResumeProgressResponse(value) {
|
|
505
|
+
const event = parseExecutionProgress(value), labels = record(record(value).labels);
|
|
506
|
+
const label = (v) => {
|
|
507
|
+
if (v === null) return null;
|
|
508
|
+
if (typeof v !== "string") return fail();
|
|
509
|
+
return [...v.replace(/\s+/g, " ")].filter((c) => c.charCodeAt(0) >= 32 && c.charCodeAt(0) !== 127).join("").trim().slice(0, 512) || null;
|
|
510
|
+
};
|
|
511
|
+
return {
|
|
512
|
+
...event,
|
|
513
|
+
labels: {
|
|
514
|
+
nodes: Object.fromEntries(
|
|
515
|
+
Object.entries(record(labels.nodes)).map(([k, v]) => {
|
|
516
|
+
const n = record(v);
|
|
517
|
+
return [text(k), { name: label(n.name), type: text(n.type) }];
|
|
518
|
+
})
|
|
519
|
+
),
|
|
520
|
+
documents: Object.fromEntries(
|
|
521
|
+
Object.entries(record(labels.documents)).map(([k, v]) => [text(k), label(v)])
|
|
522
|
+
),
|
|
523
|
+
branches: Object.fromEntries(
|
|
524
|
+
Object.entries(record(labels.branches)).map(([k, v]) => [text(k), label(v)])
|
|
525
|
+
)
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
193
530
|
// src/sdk/reviews.ts
|
|
194
531
|
function segment(value) {
|
|
195
532
|
if (!value || value === "." || value === ".." || /[\\/]/.test(value) || [...value].some((char) => char.charCodeAt(0) < 32))
|
|
@@ -201,6 +538,38 @@ var Reviews = class {
|
|
|
201
538
|
this.http = http;
|
|
202
539
|
}
|
|
203
540
|
http;
|
|
541
|
+
/** One non-consuming snapshot read for opening/reconnecting a view. No polling. */
|
|
542
|
+
async progress(executionId, options) {
|
|
543
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
544
|
+
const response = await this.http.requestRaw(
|
|
545
|
+
"GET",
|
|
546
|
+
`/executions/${segment(executionId)}/review/progress`,
|
|
547
|
+
{
|
|
548
|
+
signal: options.signal,
|
|
549
|
+
query: { organization: options.organization }
|
|
550
|
+
}
|
|
551
|
+
);
|
|
552
|
+
let body;
|
|
553
|
+
try {
|
|
554
|
+
body = await response.text();
|
|
555
|
+
} catch (error) {
|
|
556
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
557
|
+
throw new DocmanaAbortError("Request aborted");
|
|
558
|
+
throw error;
|
|
559
|
+
}
|
|
560
|
+
try {
|
|
561
|
+
const result = parseResumeProgressResponse(JSON.parse(body));
|
|
562
|
+
if (result.executionResultId !== executionId || result.organization !== options.organization)
|
|
563
|
+
throw new Error("Invalid progress scope");
|
|
564
|
+
return result;
|
|
565
|
+
} catch {
|
|
566
|
+
throw new DocmanaError("Invalid execution progress response", {
|
|
567
|
+
status: response.status,
|
|
568
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
569
|
+
code: "invalid_progress_response"
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
}
|
|
204
573
|
/** Read a page of HITL execution metadata without consuming any result. */
|
|
205
574
|
listExecutions(options) {
|
|
206
575
|
return this.http.requestJson("GET", "/reviews/executions", {
|
|
@@ -283,83 +652,69 @@ var Reviews = class {
|
|
|
283
652
|
query: {
|
|
284
653
|
organization: options.organization,
|
|
285
654
|
...options.version ? { v: options.version } : {},
|
|
286
|
-
...options.size ? { size: options.size } : {}
|
|
655
|
+
...options.size ? { size: options.size } : {},
|
|
656
|
+
...options.language ? { language: options.language } : {}
|
|
287
657
|
}
|
|
288
658
|
}
|
|
289
659
|
);
|
|
290
660
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
661
|
+
async requestDocumentTranslation(executionId, docKey, language, options) {
|
|
662
|
+
if (!options.organization?.trim()) throw new Error("Organization is required");
|
|
663
|
+
if (!language?.trim()) throw new Error("Language is required");
|
|
664
|
+
const response = await this.http.requestRaw(
|
|
665
|
+
"POST",
|
|
666
|
+
`/executions/${segment(executionId)}/review/documents/${segment(docKey)}/translations`,
|
|
667
|
+
{
|
|
668
|
+
signal: options.signal,
|
|
669
|
+
query: { organization: options.organization },
|
|
670
|
+
headers: { "content-type": "application/json" },
|
|
671
|
+
body: JSON.stringify({ language })
|
|
672
|
+
}
|
|
673
|
+
);
|
|
674
|
+
let body;
|
|
675
|
+
try {
|
|
676
|
+
body = await response.json();
|
|
677
|
+
} catch (error) {
|
|
678
|
+
if (options.signal?.aborted || error instanceof Error && error.name === "AbortError")
|
|
679
|
+
throw new DocmanaAbortError("Request aborted");
|
|
680
|
+
throw new DocmanaError("Invalid document translation response", {
|
|
681
|
+
status: response.status,
|
|
682
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
683
|
+
code: "invalid_translation_response"
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
return parseDocumentTranslation(body, response.status === 202 ? 202 : 200, response);
|
|
309
687
|
}
|
|
310
|
-
reason;
|
|
311
688
|
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
689
|
+
var VARIANTS = ["original", "translation"];
|
|
690
|
+
var TRANSLATION_STATES = [
|
|
691
|
+
"absent",
|
|
692
|
+
"preparing",
|
|
693
|
+
"ready",
|
|
694
|
+
"failed",
|
|
695
|
+
"unsupported"
|
|
696
|
+
];
|
|
697
|
+
function parseDocumentTranslation(body, status, response) {
|
|
698
|
+
const data = typeof body === "object" && body !== null ? body : {};
|
|
699
|
+
const servedLanguage = data.servedLanguage;
|
|
700
|
+
if (typeof data.requestedLanguage !== "string" || !(servedLanguage === null || typeof servedLanguage === "string") || !VARIANTS.includes(data.variant) || typeof data.fallback !== "boolean" || !TRANSLATION_STATES.includes(data.translationState))
|
|
701
|
+
throw new DocmanaError("Invalid document translation response", {
|
|
702
|
+
status: response.status,
|
|
703
|
+
requestId: response.headers.get("x-request-id") ?? void 0,
|
|
704
|
+
code: "invalid_translation_response"
|
|
705
|
+
});
|
|
706
|
+
return {
|
|
707
|
+
status,
|
|
708
|
+
requestedLanguage: data.requestedLanguage,
|
|
709
|
+
servedLanguage,
|
|
710
|
+
variant: data.variant,
|
|
711
|
+
fallback: data.fallback,
|
|
712
|
+
translationState: data.translationState
|
|
334
713
|
};
|
|
335
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
336
|
-
for (const key of ["flowId", "nodeId"])
|
|
337
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
338
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
339
|
-
if (raw[key] === void 0) continue;
|
|
340
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
341
|
-
event[key] = Number(raw[key]);
|
|
342
|
-
}
|
|
343
|
-
if (raw.documentKeys !== void 0) {
|
|
344
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
345
|
-
return fail();
|
|
346
|
-
event.documentKeys = raw.documentKeys;
|
|
347
|
-
}
|
|
348
|
-
if (raw.decision !== void 0) {
|
|
349
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
350
|
-
event.decision = raw.decision;
|
|
351
|
-
}
|
|
352
|
-
if (raw.reviewStatus !== void 0) {
|
|
353
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
354
|
-
event.reviewStatus = raw.reviewStatus;
|
|
355
|
-
}
|
|
356
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
357
|
-
return fail();
|
|
358
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
359
|
-
return event;
|
|
360
714
|
}
|
|
361
715
|
|
|
362
716
|
// src/sdk/execution-events.ts
|
|
717
|
+
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
363
718
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
364
719
|
"execution.node.started",
|
|
365
720
|
"execution.node.completed",
|
|
@@ -368,42 +723,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
368
723
|
];
|
|
369
724
|
function parseExecutionEventCallback(value) {
|
|
370
725
|
const raw = value;
|
|
726
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
371
727
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
372
728
|
return parseReviewEventCallback(value);
|
|
373
|
-
const
|
|
729
|
+
const fail2 = () => {
|
|
374
730
|
throw new ReviewEventVerificationError("payload");
|
|
375
731
|
};
|
|
376
|
-
const
|
|
732
|
+
const text2 = (key) => {
|
|
377
733
|
const v = raw[key];
|
|
378
|
-
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v :
|
|
734
|
+
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v : fail2();
|
|
379
735
|
};
|
|
380
|
-
const
|
|
381
|
-
if (raw.schemaVersion !== 1) return
|
|
736
|
+
const integer2 = (key) => Number.isSafeInteger(raw[key]) && Number(raw[key]) >= 1 ? Number(raw[key]) : fail2();
|
|
737
|
+
if (raw.schemaVersion !== 1) return fail2();
|
|
382
738
|
const event = {
|
|
383
739
|
schemaVersion: 1,
|
|
384
|
-
eventId:
|
|
740
|
+
eventId: text2("eventId"),
|
|
385
741
|
type: raw.type,
|
|
386
|
-
organization:
|
|
387
|
-
executionResultId:
|
|
388
|
-
sequence:
|
|
389
|
-
occurredAt:
|
|
390
|
-
nodeId:
|
|
391
|
-
nodeType:
|
|
392
|
-
revision:
|
|
393
|
-
attemptId:
|
|
742
|
+
organization: text2("organization"),
|
|
743
|
+
executionResultId: text2("executionResultId"),
|
|
744
|
+
sequence: integer2("sequence"),
|
|
745
|
+
occurredAt: text2("occurredAt"),
|
|
746
|
+
nodeId: text2("nodeId"),
|
|
747
|
+
nodeType: text2("nodeType"),
|
|
748
|
+
revision: integer2("revision"),
|
|
749
|
+
attemptId: text2("attemptId"),
|
|
394
750
|
documentKeys: []
|
|
395
751
|
};
|
|
396
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
752
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
397
753
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
398
754
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
399
755
|
))
|
|
400
|
-
return
|
|
756
|
+
return fail2();
|
|
401
757
|
event.documentKeys = raw.documentKeys;
|
|
402
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
403
|
-
if (raw.flowVersion !== void 0) event.flowVersion =
|
|
758
|
+
if (raw.flowId !== void 0) event.flowId = text2("flowId");
|
|
759
|
+
if (raw.flowVersion !== void 0) event.flowVersion = integer2("flowVersion");
|
|
404
760
|
if (raw.publishedAt !== void 0) {
|
|
405
|
-
event.publishedAt =
|
|
406
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
761
|
+
event.publishedAt = text2("publishedAt");
|
|
762
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
407
763
|
}
|
|
408
764
|
return event;
|
|
409
765
|
}
|
|
@@ -438,114 +794,6 @@ var ExecutionEvents = class {
|
|
|
438
794
|
}
|
|
439
795
|
};
|
|
440
796
|
|
|
441
|
-
// src/sdk/errors.ts
|
|
442
|
-
var DocmanaError = class extends Error {
|
|
443
|
-
status;
|
|
444
|
-
requestId;
|
|
445
|
-
code;
|
|
446
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
447
|
-
apiCode;
|
|
448
|
-
reviewConflict;
|
|
449
|
-
constructor(message, opts = {}) {
|
|
450
|
-
super(message);
|
|
451
|
-
this.name = new.target.name;
|
|
452
|
-
this.status = opts.status;
|
|
453
|
-
this.requestId = opts.requestId;
|
|
454
|
-
this.code = opts.code ?? "docmana_error";
|
|
455
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
459
|
-
constructor(message, opts = {}) {
|
|
460
|
-
super(message, { ...opts, code: "auth_error" });
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
464
|
-
constructor(message, opts = {}) {
|
|
465
|
-
super(message, { ...opts, code: "permission_error" });
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
469
|
-
constructor(message, opts = {}) {
|
|
470
|
-
super(message, { ...opts, code: "not_found" });
|
|
471
|
-
}
|
|
472
|
-
};
|
|
473
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
474
|
-
constructor(message, opts = {}) {
|
|
475
|
-
super(message, { ...opts, code: "bad_request" });
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
479
|
-
constructor(message, opts = {}) {
|
|
480
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
481
|
-
}
|
|
482
|
-
};
|
|
483
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
484
|
-
errors;
|
|
485
|
-
result;
|
|
486
|
-
constructor(message, errors = [], result) {
|
|
487
|
-
super(message, { code: "execution_failed" });
|
|
488
|
-
this.errors = errors;
|
|
489
|
-
this.result = result;
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
493
|
-
jobId;
|
|
494
|
-
error;
|
|
495
|
-
result;
|
|
496
|
-
constructor(message, result) {
|
|
497
|
-
super(message, { code: "fraud_detection_failed" });
|
|
498
|
-
this.jobId = result.jobId;
|
|
499
|
-
this.error = result.error;
|
|
500
|
-
this.result = result;
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
504
|
-
constructor(message) {
|
|
505
|
-
super(message, { code: "timeout" });
|
|
506
|
-
}
|
|
507
|
-
};
|
|
508
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
509
|
-
constructor(message) {
|
|
510
|
-
super(message, { code: "aborted" });
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
514
|
-
retryAfterMs;
|
|
515
|
-
constructor(message, opts = {}) {
|
|
516
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
517
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
521
|
-
retryAfterMs;
|
|
522
|
-
constructor(message, opts = {}) {
|
|
523
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
524
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
525
|
-
}
|
|
526
|
-
};
|
|
527
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
528
|
-
const opts = { status, requestId };
|
|
529
|
-
switch (status) {
|
|
530
|
-
case 400:
|
|
531
|
-
return new DocmanaRequestError(message, opts);
|
|
532
|
-
case 401:
|
|
533
|
-
return new DocmanaAuthError(message, opts);
|
|
534
|
-
case 403:
|
|
535
|
-
return new DocmanaPermissionError(message, opts);
|
|
536
|
-
case 404:
|
|
537
|
-
return new DocmanaNotFoundError(message, opts);
|
|
538
|
-
case 413:
|
|
539
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
540
|
-
case 429:
|
|
541
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
542
|
-
case 503:
|
|
543
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
544
|
-
default:
|
|
545
|
-
return new DocmanaError(message, opts);
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
|
|
549
797
|
// src/sdk/http/http-client.ts
|
|
550
798
|
var HttpClient = class {
|
|
551
799
|
constructor(opts) {
|
|
@@ -554,10 +802,10 @@ var HttpClient = class {
|
|
|
554
802
|
opts;
|
|
555
803
|
async requestJson(method, path, options = {}) {
|
|
556
804
|
const res = await this.requestRaw(method, path, options);
|
|
557
|
-
const
|
|
558
|
-
if (!
|
|
805
|
+
const text2 = await res.text();
|
|
806
|
+
if (!text2) return {};
|
|
559
807
|
try {
|
|
560
|
-
return JSON.parse(
|
|
808
|
+
return JSON.parse(text2);
|
|
561
809
|
} catch {
|
|
562
810
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
563
811
|
}
|
|
@@ -573,10 +821,10 @@ var HttpClient = class {
|
|
|
573
821
|
}
|
|
574
822
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
575
823
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
576
|
-
const
|
|
824
|
+
const message2 = await this.extractMessage(res);
|
|
577
825
|
const error = errorFromResponse(
|
|
578
826
|
res.status,
|
|
579
|
-
|
|
827
|
+
message2,
|
|
580
828
|
requestId,
|
|
581
829
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
582
830
|
);
|
|
@@ -635,15 +883,15 @@ function isRecord(value) {
|
|
|
635
883
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
636
884
|
}
|
|
637
885
|
function reviewConflictDetails(raw) {
|
|
638
|
-
const
|
|
886
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
639
887
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
640
888
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
641
|
-
const id =
|
|
642
|
-
const name =
|
|
889
|
+
const id = text2(actor.id, 200);
|
|
890
|
+
const name = text2(actor.name, 200);
|
|
643
891
|
const profile = {
|
|
644
|
-
firstName:
|
|
645
|
-
lastName:
|
|
646
|
-
email:
|
|
892
|
+
firstName: text2(actor.firstName, 200),
|
|
893
|
+
lastName: text2(actor.lastName, 200),
|
|
894
|
+
email: text2(actor.email, 200)
|
|
647
895
|
};
|
|
648
896
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
649
897
|
const decidedBy = [
|
|
@@ -663,13 +911,13 @@ function reviewConflictDetails(raw) {
|
|
|
663
911
|
}
|
|
664
912
|
}
|
|
665
913
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
666
|
-
const decidedAt =
|
|
914
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
667
915
|
return {
|
|
668
916
|
currentVersion,
|
|
669
917
|
decidedBy,
|
|
670
918
|
decidedAt,
|
|
671
919
|
version: currentVersion,
|
|
672
|
-
submittedBy: id ?? name ??
|
|
920
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
673
921
|
submittedAt: decidedAt
|
|
674
922
|
};
|
|
675
923
|
}
|
|
@@ -871,8 +1119,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
871
1119
|
query: organization ? { organization } : void 0,
|
|
872
1120
|
signal
|
|
873
1121
|
});
|
|
874
|
-
const
|
|
875
|
-
const body =
|
|
1122
|
+
const text2 = await res.text();
|
|
1123
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
876
1124
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
877
1125
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
878
1126
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -940,8 +1188,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
940
1188
|
signal
|
|
941
1189
|
}
|
|
942
1190
|
);
|
|
943
|
-
const
|
|
944
|
-
const body =
|
|
1191
|
+
const text2 = await res.text();
|
|
1192
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
945
1193
|
const normalized = {
|
|
946
1194
|
...body,
|
|
947
1195
|
status: normalizeStatus(body.status)
|
|
@@ -1325,8 +1573,8 @@ function isTransientPollingError(error) {
|
|
|
1325
1573
|
|
|
1326
1574
|
// src/cli/errors.ts
|
|
1327
1575
|
var CliUsageError = class extends Error {
|
|
1328
|
-
constructor(
|
|
1329
|
-
super(
|
|
1576
|
+
constructor(message2) {
|
|
1577
|
+
super(message2);
|
|
1330
1578
|
this.name = "CliUsageError";
|
|
1331
1579
|
}
|
|
1332
1580
|
};
|
|
@@ -1347,8 +1595,8 @@ function formatFlowList(flows) {
|
|
|
1347
1595
|
for (const flow of flows) {
|
|
1348
1596
|
const id = flow.id || "(no id)";
|
|
1349
1597
|
const name = flow.name || "(no name)";
|
|
1350
|
-
const
|
|
1351
|
-
lines.push(`- ${id} - ${name}${
|
|
1598
|
+
const state2 = flow.state ? ` [${flow.state}]` : "";
|
|
1599
|
+
lines.push(`- ${id} - ${name}${state2}`);
|
|
1352
1600
|
}
|
|
1353
1601
|
return `${lines.join("\n")}
|
|
1354
1602
|
`;
|
|
@@ -2097,8 +2345,8 @@ function formatTokenExpiry(expiresAt) {
|
|
|
2097
2345
|
|
|
2098
2346
|
// src/cli/cli.ts
|
|
2099
2347
|
async function runCli(argv = process.argv.slice(2), env = process.env, io = {
|
|
2100
|
-
stdout: (
|
|
2101
|
-
stderr: (
|
|
2348
|
+
stdout: (text2) => process.stdout.write(text2),
|
|
2349
|
+
stderr: (text2) => process.stderr.write(text2)
|
|
2102
2350
|
}, clientFactory = (config) => new Docmana(config), deps = {}) {
|
|
2103
2351
|
const program = buildProgram(env, io, clientFactory, deps);
|
|
2104
2352
|
if (argv.length === 0) {
|