@docmana/sdk 0.46.0 → 0.47.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 +9 -0
- package/LICENSE +21 -21
- package/README.md +23 -0
- package/dist/cli.mjs +413 -219
- package/dist/cli.mjs.map +1 -1
- package/dist/execution-events.d.ts +2 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +437 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +433 -235
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +11 -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", {
|
|
@@ -292,74 +661,6 @@ var Reviews = class {
|
|
|
292
661
|
|
|
293
662
|
// src/sdk/execution-events.ts
|
|
294
663
|
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
295
|
-
|
|
296
|
-
// src/sdk/review-events.ts
|
|
297
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
298
|
-
var REVIEW_EVENT_TYPES = [
|
|
299
|
-
"execution.awaiting_review",
|
|
300
|
-
"execution.review_done",
|
|
301
|
-
"execution.review_closed",
|
|
302
|
-
"execution.review_expired"
|
|
303
|
-
];
|
|
304
|
-
var ReviewEventVerificationError = class extends Error {
|
|
305
|
-
constructor(reason) {
|
|
306
|
-
super(`Invalid review event ${reason}`);
|
|
307
|
-
this.reason = reason;
|
|
308
|
-
this.name = "ReviewEventVerificationError";
|
|
309
|
-
}
|
|
310
|
-
reason;
|
|
311
|
-
};
|
|
312
|
-
function parseReviewEventCallback(value) {
|
|
313
|
-
const fail = () => {
|
|
314
|
-
throw new ReviewEventVerificationError("payload");
|
|
315
|
-
};
|
|
316
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
317
|
-
const raw = value;
|
|
318
|
-
const text = (key) => {
|
|
319
|
-
const v = raw[key];
|
|
320
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
321
|
-
return fail();
|
|
322
|
-
return v;
|
|
323
|
-
};
|
|
324
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
325
|
-
return fail();
|
|
326
|
-
const event = {
|
|
327
|
-
schemaVersion: 1,
|
|
328
|
-
eventId: text("eventId"),
|
|
329
|
-
type: raw.type,
|
|
330
|
-
organization: text("organization"),
|
|
331
|
-
executionResultId: text("executionResultId"),
|
|
332
|
-
reviewId: text("reviewId"),
|
|
333
|
-
publishedAt: text("publishedAt")
|
|
334
|
-
};
|
|
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
|
-
}
|
|
361
|
-
|
|
362
|
-
// src/sdk/execution-events.ts
|
|
363
664
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
364
665
|
"execution.node.started",
|
|
365
666
|
"execution.node.completed",
|
|
@@ -368,42 +669,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
368
669
|
];
|
|
369
670
|
function parseExecutionEventCallback(value) {
|
|
370
671
|
const raw = value;
|
|
672
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
371
673
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
372
674
|
return parseReviewEventCallback(value);
|
|
373
|
-
const
|
|
675
|
+
const fail2 = () => {
|
|
374
676
|
throw new ReviewEventVerificationError("payload");
|
|
375
677
|
};
|
|
376
|
-
const
|
|
678
|
+
const text2 = (key) => {
|
|
377
679
|
const v = raw[key];
|
|
378
|
-
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v :
|
|
680
|
+
return typeof v === "string" && v.length > 0 && v.length <= 512 && [...v].every((c) => c.charCodeAt(0) >= 32) ? v : fail2();
|
|
379
681
|
};
|
|
380
|
-
const
|
|
381
|
-
if (raw.schemaVersion !== 1) return
|
|
682
|
+
const integer2 = (key) => Number.isSafeInteger(raw[key]) && Number(raw[key]) >= 1 ? Number(raw[key]) : fail2();
|
|
683
|
+
if (raw.schemaVersion !== 1) return fail2();
|
|
382
684
|
const event = {
|
|
383
685
|
schemaVersion: 1,
|
|
384
|
-
eventId:
|
|
686
|
+
eventId: text2("eventId"),
|
|
385
687
|
type: raw.type,
|
|
386
|
-
organization:
|
|
387
|
-
executionResultId:
|
|
388
|
-
sequence:
|
|
389
|
-
occurredAt:
|
|
390
|
-
nodeId:
|
|
391
|
-
nodeType:
|
|
392
|
-
revision:
|
|
393
|
-
attemptId:
|
|
688
|
+
organization: text2("organization"),
|
|
689
|
+
executionResultId: text2("executionResultId"),
|
|
690
|
+
sequence: integer2("sequence"),
|
|
691
|
+
occurredAt: text2("occurredAt"),
|
|
692
|
+
nodeId: text2("nodeId"),
|
|
693
|
+
nodeType: text2("nodeType"),
|
|
694
|
+
revision: integer2("revision"),
|
|
695
|
+
attemptId: text2("attemptId"),
|
|
394
696
|
documentKeys: []
|
|
395
697
|
};
|
|
396
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
698
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
397
699
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
398
700
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
399
701
|
))
|
|
400
|
-
return
|
|
702
|
+
return fail2();
|
|
401
703
|
event.documentKeys = raw.documentKeys;
|
|
402
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
403
|
-
if (raw.flowVersion !== void 0) event.flowVersion =
|
|
704
|
+
if (raw.flowId !== void 0) event.flowId = text2("flowId");
|
|
705
|
+
if (raw.flowVersion !== void 0) event.flowVersion = integer2("flowVersion");
|
|
404
706
|
if (raw.publishedAt !== void 0) {
|
|
405
|
-
event.publishedAt =
|
|
406
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
707
|
+
event.publishedAt = text2("publishedAt");
|
|
708
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
407
709
|
}
|
|
408
710
|
return event;
|
|
409
711
|
}
|
|
@@ -438,114 +740,6 @@ var ExecutionEvents = class {
|
|
|
438
740
|
}
|
|
439
741
|
};
|
|
440
742
|
|
|
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
743
|
// src/sdk/http/http-client.ts
|
|
550
744
|
var HttpClient = class {
|
|
551
745
|
constructor(opts) {
|
|
@@ -554,10 +748,10 @@ var HttpClient = class {
|
|
|
554
748
|
opts;
|
|
555
749
|
async requestJson(method, path, options = {}) {
|
|
556
750
|
const res = await this.requestRaw(method, path, options);
|
|
557
|
-
const
|
|
558
|
-
if (!
|
|
751
|
+
const text2 = await res.text();
|
|
752
|
+
if (!text2) return {};
|
|
559
753
|
try {
|
|
560
|
-
return JSON.parse(
|
|
754
|
+
return JSON.parse(text2);
|
|
561
755
|
} catch {
|
|
562
756
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
563
757
|
}
|
|
@@ -573,10 +767,10 @@ var HttpClient = class {
|
|
|
573
767
|
}
|
|
574
768
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
575
769
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
576
|
-
const
|
|
770
|
+
const message2 = await this.extractMessage(res);
|
|
577
771
|
const error = errorFromResponse(
|
|
578
772
|
res.status,
|
|
579
|
-
|
|
773
|
+
message2,
|
|
580
774
|
requestId,
|
|
581
775
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
582
776
|
);
|
|
@@ -635,15 +829,15 @@ function isRecord(value) {
|
|
|
635
829
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
636
830
|
}
|
|
637
831
|
function reviewConflictDetails(raw) {
|
|
638
|
-
const
|
|
832
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
639
833
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
640
834
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
641
|
-
const id =
|
|
642
|
-
const name =
|
|
835
|
+
const id = text2(actor.id, 200);
|
|
836
|
+
const name = text2(actor.name, 200);
|
|
643
837
|
const profile = {
|
|
644
|
-
firstName:
|
|
645
|
-
lastName:
|
|
646
|
-
email:
|
|
838
|
+
firstName: text2(actor.firstName, 200),
|
|
839
|
+
lastName: text2(actor.lastName, 200),
|
|
840
|
+
email: text2(actor.email, 200)
|
|
647
841
|
};
|
|
648
842
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
649
843
|
const decidedBy = [
|
|
@@ -663,13 +857,13 @@ function reviewConflictDetails(raw) {
|
|
|
663
857
|
}
|
|
664
858
|
}
|
|
665
859
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
666
|
-
const decidedAt =
|
|
860
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
667
861
|
return {
|
|
668
862
|
currentVersion,
|
|
669
863
|
decidedBy,
|
|
670
864
|
decidedAt,
|
|
671
865
|
version: currentVersion,
|
|
672
|
-
submittedBy: id ?? name ??
|
|
866
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
673
867
|
submittedAt: decidedAt
|
|
674
868
|
};
|
|
675
869
|
}
|
|
@@ -871,8 +1065,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
871
1065
|
query: organization ? { organization } : void 0,
|
|
872
1066
|
signal
|
|
873
1067
|
});
|
|
874
|
-
const
|
|
875
|
-
const body =
|
|
1068
|
+
const text2 = await res.text();
|
|
1069
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
876
1070
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
877
1071
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
878
1072
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -940,8 +1134,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
940
1134
|
signal
|
|
941
1135
|
}
|
|
942
1136
|
);
|
|
943
|
-
const
|
|
944
|
-
const body =
|
|
1137
|
+
const text2 = await res.text();
|
|
1138
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
945
1139
|
const normalized = {
|
|
946
1140
|
...body,
|
|
947
1141
|
status: normalizeStatus(body.status)
|
|
@@ -1325,8 +1519,8 @@ function isTransientPollingError(error) {
|
|
|
1325
1519
|
|
|
1326
1520
|
// src/cli/errors.ts
|
|
1327
1521
|
var CliUsageError = class extends Error {
|
|
1328
|
-
constructor(
|
|
1329
|
-
super(
|
|
1522
|
+
constructor(message2) {
|
|
1523
|
+
super(message2);
|
|
1330
1524
|
this.name = "CliUsageError";
|
|
1331
1525
|
}
|
|
1332
1526
|
};
|
|
@@ -1347,8 +1541,8 @@ function formatFlowList(flows) {
|
|
|
1347
1541
|
for (const flow of flows) {
|
|
1348
1542
|
const id = flow.id || "(no id)";
|
|
1349
1543
|
const name = flow.name || "(no name)";
|
|
1350
|
-
const
|
|
1351
|
-
lines.push(`- ${id} - ${name}${
|
|
1544
|
+
const state2 = flow.state ? ` [${flow.state}]` : "";
|
|
1545
|
+
lines.push(`- ${id} - ${name}${state2}`);
|
|
1352
1546
|
}
|
|
1353
1547
|
return `${lines.join("\n")}
|
|
1354
1548
|
`;
|
|
@@ -2097,8 +2291,8 @@ function formatTokenExpiry(expiresAt) {
|
|
|
2097
2291
|
|
|
2098
2292
|
// src/cli/cli.ts
|
|
2099
2293
|
async function runCli(argv = process.argv.slice(2), env = process.env, io = {
|
|
2100
|
-
stdout: (
|
|
2101
|
-
stderr: (
|
|
2294
|
+
stdout: (text2) => process.stdout.write(text2),
|
|
2295
|
+
stderr: (text2) => process.stderr.write(text2)
|
|
2102
2296
|
}, clientFactory = (config) => new Docmana(config), deps = {}) {
|
|
2103
2297
|
const program = buildProgram(env, io, clientFactory, deps);
|
|
2104
2298
|
if (argv.length === 0) {
|