@docmana/sdk 0.45.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 +13 -0
- package/README.md +36 -0
- package/dist/cli.mjs +428 -219
- 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 +452 -235
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +448 -235
- package/dist/index.mjs.map +1 -1
- package/dist/resume-progress.d.ts +81 -0
- package/dist/reviews.d.ts +43 -2
- package/package.json +13 -9
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,53 @@ 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
|
+
}
|
|
573
|
+
/** Read a page of HITL execution metadata without consuming any result. */
|
|
574
|
+
listExecutions(options) {
|
|
575
|
+
return this.http.requestJson("GET", "/reviews/executions", {
|
|
576
|
+
signal: options.signal,
|
|
577
|
+
query: {
|
|
578
|
+
organization: options.organization,
|
|
579
|
+
...options.flowId !== void 0 ? { flowId: options.flowId } : {},
|
|
580
|
+
...options.executionId !== void 0 ? { executionId: options.executionId } : {},
|
|
581
|
+
...options.limit !== void 0 ? { limit: String(options.limit) } : {},
|
|
582
|
+
...options.offset !== void 0 ? { offset: String(options.offset) } : {},
|
|
583
|
+
...options.sortBy !== void 0 ? { sortBy: options.sortBy } : {},
|
|
584
|
+
...options.sortDirection !== void 0 ? { sortDirection: options.sortDirection } : {}
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}
|
|
204
588
|
/** Repeatable metadata read: never consumes the final execution result. */
|
|
205
589
|
calculation(executionId, options) {
|
|
206
590
|
return this.http.requestJson("GET", `/executions/${segment(executionId)}/review/calculation`, {
|
|
@@ -277,74 +661,6 @@ var Reviews = class {
|
|
|
277
661
|
|
|
278
662
|
// src/sdk/execution-events.ts
|
|
279
663
|
import { createHmac as createHmac2, timingSafeEqual as timingSafeEqual2 } from "crypto";
|
|
280
|
-
|
|
281
|
-
// src/sdk/review-events.ts
|
|
282
|
-
import { createHmac, timingSafeEqual } from "crypto";
|
|
283
|
-
var REVIEW_EVENT_TYPES = [
|
|
284
|
-
"execution.awaiting_review",
|
|
285
|
-
"execution.review_done",
|
|
286
|
-
"execution.review_closed",
|
|
287
|
-
"execution.review_expired"
|
|
288
|
-
];
|
|
289
|
-
var ReviewEventVerificationError = class extends Error {
|
|
290
|
-
constructor(reason) {
|
|
291
|
-
super(`Invalid review event ${reason}`);
|
|
292
|
-
this.reason = reason;
|
|
293
|
-
this.name = "ReviewEventVerificationError";
|
|
294
|
-
}
|
|
295
|
-
reason;
|
|
296
|
-
};
|
|
297
|
-
function parseReviewEventCallback(value) {
|
|
298
|
-
const fail = () => {
|
|
299
|
-
throw new ReviewEventVerificationError("payload");
|
|
300
|
-
};
|
|
301
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return fail();
|
|
302
|
-
const raw = value;
|
|
303
|
-
const text = (key) => {
|
|
304
|
-
const v = raw[key];
|
|
305
|
-
if (typeof v !== "string" || !v.trim() || v.length > 512 || [...v].some((char) => char.charCodeAt(0) < 32))
|
|
306
|
-
return fail();
|
|
307
|
-
return v;
|
|
308
|
-
};
|
|
309
|
-
if (raw.schemaVersion !== 1 || !REVIEW_EVENT_TYPES.includes(raw.type))
|
|
310
|
-
return fail();
|
|
311
|
-
const event = {
|
|
312
|
-
schemaVersion: 1,
|
|
313
|
-
eventId: text("eventId"),
|
|
314
|
-
type: raw.type,
|
|
315
|
-
organization: text("organization"),
|
|
316
|
-
executionResultId: text("executionResultId"),
|
|
317
|
-
reviewId: text("reviewId"),
|
|
318
|
-
publishedAt: text("publishedAt")
|
|
319
|
-
};
|
|
320
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail();
|
|
321
|
-
for (const key of ["flowId", "nodeId"])
|
|
322
|
-
if (raw[key] !== void 0) event[key] = text(key);
|
|
323
|
-
for (const key of ["flowVersion", "reviewVersion"]) {
|
|
324
|
-
if (raw[key] === void 0) continue;
|
|
325
|
-
if (!Number.isSafeInteger(raw[key]) || Number(raw[key]) < 1) return fail();
|
|
326
|
-
event[key] = Number(raw[key]);
|
|
327
|
-
}
|
|
328
|
-
if (raw.documentKeys !== void 0) {
|
|
329
|
-
if (!Array.isArray(raw.documentKeys) || raw.documentKeys.length > 1e3 || raw.documentKeys.some((v) => typeof v !== "string" || !v || v.length > 512))
|
|
330
|
-
return fail();
|
|
331
|
-
event.documentKeys = raw.documentKeys;
|
|
332
|
-
}
|
|
333
|
-
if (raw.decision !== void 0) {
|
|
334
|
-
if (!["continue", "exclude", "rerun", "reject"].includes(String(raw.decision))) return fail();
|
|
335
|
-
event.decision = raw.decision;
|
|
336
|
-
}
|
|
337
|
-
if (raw.reviewStatus !== void 0) {
|
|
338
|
-
if (!["completed", "rejected", "failed"].includes(String(raw.reviewStatus))) return fail();
|
|
339
|
-
event.reviewStatus = raw.reviewStatus;
|
|
340
|
-
}
|
|
341
|
-
if (event.type === "execution.review_done" && (!event.decision || !event.reviewVersion))
|
|
342
|
-
return fail();
|
|
343
|
-
if (event.type === "execution.review_closed" && !event.reviewStatus) return fail();
|
|
344
|
-
return event;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// src/sdk/execution-events.ts
|
|
348
664
|
var EXECUTION_NODE_EVENT_TYPES = [
|
|
349
665
|
"execution.node.started",
|
|
350
666
|
"execution.node.completed",
|
|
@@ -353,42 +669,43 @@ var EXECUTION_NODE_EVENT_TYPES = [
|
|
|
353
669
|
];
|
|
354
670
|
function parseExecutionEventCallback(value) {
|
|
355
671
|
const raw = value;
|
|
672
|
+
if (raw?.type === "execution.progress") return parseExecutionProgress(value);
|
|
356
673
|
if (!raw || !EXECUTION_NODE_EVENT_TYPES.includes(raw.type))
|
|
357
674
|
return parseReviewEventCallback(value);
|
|
358
|
-
const
|
|
675
|
+
const fail2 = () => {
|
|
359
676
|
throw new ReviewEventVerificationError("payload");
|
|
360
677
|
};
|
|
361
|
-
const
|
|
678
|
+
const text2 = (key) => {
|
|
362
679
|
const v = raw[key];
|
|
363
|
-
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();
|
|
364
681
|
};
|
|
365
|
-
const
|
|
366
|
-
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();
|
|
367
684
|
const event = {
|
|
368
685
|
schemaVersion: 1,
|
|
369
|
-
eventId:
|
|
686
|
+
eventId: text2("eventId"),
|
|
370
687
|
type: raw.type,
|
|
371
|
-
organization:
|
|
372
|
-
executionResultId:
|
|
373
|
-
sequence:
|
|
374
|
-
occurredAt:
|
|
375
|
-
nodeId:
|
|
376
|
-
nodeType:
|
|
377
|
-
revision:
|
|
378
|
-
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"),
|
|
379
696
|
documentKeys: []
|
|
380
697
|
};
|
|
381
|
-
if (!Number.isFinite(Date.parse(event.occurredAt))) return
|
|
698
|
+
if (!Number.isFinite(Date.parse(event.occurredAt))) return fail2();
|
|
382
699
|
if (!Array.isArray(raw.documentKeys) || !raw.documentKeys.length || raw.documentKeys.length > 1e3 || raw.documentKeys.some(
|
|
383
700
|
(v) => typeof v !== "string" || !v || v.length > 512 || [...v].some((c) => c.charCodeAt(0) < 32)
|
|
384
701
|
))
|
|
385
|
-
return
|
|
702
|
+
return fail2();
|
|
386
703
|
event.documentKeys = raw.documentKeys;
|
|
387
|
-
if (raw.flowId !== void 0) event.flowId =
|
|
388
|
-
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");
|
|
389
706
|
if (raw.publishedAt !== void 0) {
|
|
390
|
-
event.publishedAt =
|
|
391
|
-
if (!Number.isFinite(Date.parse(event.publishedAt))) return
|
|
707
|
+
event.publishedAt = text2("publishedAt");
|
|
708
|
+
if (!Number.isFinite(Date.parse(event.publishedAt))) return fail2();
|
|
392
709
|
}
|
|
393
710
|
return event;
|
|
394
711
|
}
|
|
@@ -423,114 +740,6 @@ var ExecutionEvents = class {
|
|
|
423
740
|
}
|
|
424
741
|
};
|
|
425
742
|
|
|
426
|
-
// src/sdk/errors.ts
|
|
427
|
-
var DocmanaError = class extends Error {
|
|
428
|
-
status;
|
|
429
|
-
requestId;
|
|
430
|
-
code;
|
|
431
|
-
/** Public API business code; `code` retains the SDK's existing error category. */
|
|
432
|
-
apiCode;
|
|
433
|
-
reviewConflict;
|
|
434
|
-
constructor(message, opts = {}) {
|
|
435
|
-
super(message);
|
|
436
|
-
this.name = new.target.name;
|
|
437
|
-
this.status = opts.status;
|
|
438
|
-
this.requestId = opts.requestId;
|
|
439
|
-
this.code = opts.code ?? "docmana_error";
|
|
440
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
var DocmanaAuthError = class extends DocmanaError {
|
|
444
|
-
constructor(message, opts = {}) {
|
|
445
|
-
super(message, { ...opts, code: "auth_error" });
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
var DocmanaPermissionError = class extends DocmanaError {
|
|
449
|
-
constructor(message, opts = {}) {
|
|
450
|
-
super(message, { ...opts, code: "permission_error" });
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
var DocmanaNotFoundError = class extends DocmanaError {
|
|
454
|
-
constructor(message, opts = {}) {
|
|
455
|
-
super(message, { ...opts, code: "not_found" });
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
var DocmanaRequestError = class extends DocmanaError {
|
|
459
|
-
constructor(message, opts = {}) {
|
|
460
|
-
super(message, { ...opts, code: "bad_request" });
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
var DocmanaPayloadTooLargeError = class extends DocmanaError {
|
|
464
|
-
constructor(message, opts = {}) {
|
|
465
|
-
super(message, { ...opts, code: "payload_too_large" });
|
|
466
|
-
}
|
|
467
|
-
};
|
|
468
|
-
var DocmanaExecutionError = class extends DocmanaError {
|
|
469
|
-
errors;
|
|
470
|
-
result;
|
|
471
|
-
constructor(message, errors = [], result) {
|
|
472
|
-
super(message, { code: "execution_failed" });
|
|
473
|
-
this.errors = errors;
|
|
474
|
-
this.result = result;
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
var DocmanaFraudDetectionError = class extends DocmanaError {
|
|
478
|
-
jobId;
|
|
479
|
-
error;
|
|
480
|
-
result;
|
|
481
|
-
constructor(message, result) {
|
|
482
|
-
super(message, { code: "fraud_detection_failed" });
|
|
483
|
-
this.jobId = result.jobId;
|
|
484
|
-
this.error = result.error;
|
|
485
|
-
this.result = result;
|
|
486
|
-
}
|
|
487
|
-
};
|
|
488
|
-
var DocmanaTimeoutError = class extends DocmanaError {
|
|
489
|
-
constructor(message) {
|
|
490
|
-
super(message, { code: "timeout" });
|
|
491
|
-
}
|
|
492
|
-
};
|
|
493
|
-
var DocmanaAbortError = class extends DocmanaError {
|
|
494
|
-
constructor(message) {
|
|
495
|
-
super(message, { code: "aborted" });
|
|
496
|
-
}
|
|
497
|
-
};
|
|
498
|
-
var DocmanaRateLimitError = class extends DocmanaError {
|
|
499
|
-
retryAfterMs;
|
|
500
|
-
constructor(message, opts = {}) {
|
|
501
|
-
super(message, { ...opts, code: "rate_limited" });
|
|
502
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
|
-
var DocmanaServiceUnavailableError = class extends DocmanaError {
|
|
506
|
-
retryAfterMs;
|
|
507
|
-
constructor(message, opts = {}) {
|
|
508
|
-
super(message, { ...opts, code: "service_unavailable" });
|
|
509
|
-
this.retryAfterMs = opts.retryAfterMs;
|
|
510
|
-
}
|
|
511
|
-
};
|
|
512
|
-
function errorFromResponse(status, message, requestId, retryAfterMs) {
|
|
513
|
-
const opts = { status, requestId };
|
|
514
|
-
switch (status) {
|
|
515
|
-
case 400:
|
|
516
|
-
return new DocmanaRequestError(message, opts);
|
|
517
|
-
case 401:
|
|
518
|
-
return new DocmanaAuthError(message, opts);
|
|
519
|
-
case 403:
|
|
520
|
-
return new DocmanaPermissionError(message, opts);
|
|
521
|
-
case 404:
|
|
522
|
-
return new DocmanaNotFoundError(message, opts);
|
|
523
|
-
case 413:
|
|
524
|
-
return new DocmanaPayloadTooLargeError(message, opts);
|
|
525
|
-
case 429:
|
|
526
|
-
return new DocmanaRateLimitError(message, { ...opts, retryAfterMs });
|
|
527
|
-
case 503:
|
|
528
|
-
return new DocmanaServiceUnavailableError(message, { ...opts, retryAfterMs });
|
|
529
|
-
default:
|
|
530
|
-
return new DocmanaError(message, opts);
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
743
|
// src/sdk/http/http-client.ts
|
|
535
744
|
var HttpClient = class {
|
|
536
745
|
constructor(opts) {
|
|
@@ -539,10 +748,10 @@ var HttpClient = class {
|
|
|
539
748
|
opts;
|
|
540
749
|
async requestJson(method, path, options = {}) {
|
|
541
750
|
const res = await this.requestRaw(method, path, options);
|
|
542
|
-
const
|
|
543
|
-
if (!
|
|
751
|
+
const text2 = await res.text();
|
|
752
|
+
if (!text2) return {};
|
|
544
753
|
try {
|
|
545
|
-
return JSON.parse(
|
|
754
|
+
return JSON.parse(text2);
|
|
546
755
|
} catch {
|
|
547
756
|
throw new DocmanaError("Invalid JSON response from Docmana", { status: res.status });
|
|
548
757
|
}
|
|
@@ -558,10 +767,10 @@ var HttpClient = class {
|
|
|
558
767
|
}
|
|
559
768
|
if (!res.ok && !options.acceptStatuses?.includes(res.status)) {
|
|
560
769
|
const requestId = res.headers.get("x-request-id") ?? void 0;
|
|
561
|
-
const
|
|
770
|
+
const message2 = await this.extractMessage(res);
|
|
562
771
|
const error = errorFromResponse(
|
|
563
772
|
res.status,
|
|
564
|
-
|
|
773
|
+
message2,
|
|
565
774
|
requestId,
|
|
566
775
|
parseRetryAfterMs(res.headers.get("retry-after"))
|
|
567
776
|
);
|
|
@@ -620,15 +829,15 @@ function isRecord(value) {
|
|
|
620
829
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
621
830
|
}
|
|
622
831
|
function reviewConflictDetails(raw) {
|
|
623
|
-
const
|
|
832
|
+
const text2 = (value, limit) => typeof value === "string" ? value.slice(0, limit) : null;
|
|
624
833
|
const version = (value) => Number.isSafeInteger(value) && Number(value) >= 1 ? value : null;
|
|
625
834
|
const actor = isRecord(raw.decidedBy) ? raw.decidedBy : {};
|
|
626
|
-
const id =
|
|
627
|
-
const name =
|
|
835
|
+
const id = text2(actor.id, 200);
|
|
836
|
+
const name = text2(actor.name, 200);
|
|
628
837
|
const profile = {
|
|
629
|
-
firstName:
|
|
630
|
-
lastName:
|
|
631
|
-
email:
|
|
838
|
+
firstName: text2(actor.firstName, 200),
|
|
839
|
+
lastName: text2(actor.lastName, 200),
|
|
840
|
+
email: text2(actor.email, 200)
|
|
632
841
|
};
|
|
633
842
|
const source = actor.source === "integrator" || actor.source === "token" ? actor.source : null;
|
|
634
843
|
const decidedBy = [
|
|
@@ -648,13 +857,13 @@ function reviewConflictDetails(raw) {
|
|
|
648
857
|
}
|
|
649
858
|
}
|
|
650
859
|
const currentVersion = version(raw.currentVersion) ?? version(raw.version);
|
|
651
|
-
const decidedAt =
|
|
860
|
+
const decidedAt = text2(raw.decidedAt, 40) ?? text2(raw.submittedAt, 40);
|
|
652
861
|
return {
|
|
653
862
|
currentVersion,
|
|
654
863
|
decidedBy,
|
|
655
864
|
decidedAt,
|
|
656
865
|
version: currentVersion,
|
|
657
|
-
submittedBy: id ?? name ??
|
|
866
|
+
submittedBy: id ?? name ?? text2(raw.submittedBy, 200),
|
|
658
867
|
submittedAt: decidedAt
|
|
659
868
|
};
|
|
660
869
|
}
|
|
@@ -856,8 +1065,8 @@ async function getStatus(http, executionResultId, signal, organization) {
|
|
|
856
1065
|
query: organization ? { organization } : void 0,
|
|
857
1066
|
signal
|
|
858
1067
|
});
|
|
859
|
-
const
|
|
860
|
-
const body =
|
|
1068
|
+
const text2 = await res.text();
|
|
1069
|
+
const body = text2 ? JSON.parse(text2) : { status: "" };
|
|
861
1070
|
const normalizedBody = { ...body, status: normalizeStatus(body.status) };
|
|
862
1071
|
const pollAfterMs = parsePositiveInt(res.headers.get("x-poll-after"));
|
|
863
1072
|
if (typeof process !== "undefined" && process.env?.DOCMANA_SDK_DEBUG) {
|
|
@@ -925,8 +1134,8 @@ async function getFraudDetectionStatus(http, jobId, signal, organization) {
|
|
|
925
1134
|
signal
|
|
926
1135
|
}
|
|
927
1136
|
);
|
|
928
|
-
const
|
|
929
|
-
const body =
|
|
1137
|
+
const text2 = await res.text();
|
|
1138
|
+
const body = text2 ? JSON.parse(text2) : { jobId, status: "" };
|
|
930
1139
|
const normalized = {
|
|
931
1140
|
...body,
|
|
932
1141
|
status: normalizeStatus(body.status)
|
|
@@ -1310,8 +1519,8 @@ function isTransientPollingError(error) {
|
|
|
1310
1519
|
|
|
1311
1520
|
// src/cli/errors.ts
|
|
1312
1521
|
var CliUsageError = class extends Error {
|
|
1313
|
-
constructor(
|
|
1314
|
-
super(
|
|
1522
|
+
constructor(message2) {
|
|
1523
|
+
super(message2);
|
|
1315
1524
|
this.name = "CliUsageError";
|
|
1316
1525
|
}
|
|
1317
1526
|
};
|
|
@@ -1332,8 +1541,8 @@ function formatFlowList(flows) {
|
|
|
1332
1541
|
for (const flow of flows) {
|
|
1333
1542
|
const id = flow.id || "(no id)";
|
|
1334
1543
|
const name = flow.name || "(no name)";
|
|
1335
|
-
const
|
|
1336
|
-
lines.push(`- ${id} - ${name}${
|
|
1544
|
+
const state2 = flow.state ? ` [${flow.state}]` : "";
|
|
1545
|
+
lines.push(`- ${id} - ${name}${state2}`);
|
|
1337
1546
|
}
|
|
1338
1547
|
return `${lines.join("\n")}
|
|
1339
1548
|
`;
|
|
@@ -2082,8 +2291,8 @@ function formatTokenExpiry(expiresAt) {
|
|
|
2082
2291
|
|
|
2083
2292
|
// src/cli/cli.ts
|
|
2084
2293
|
async function runCli(argv = process.argv.slice(2), env = process.env, io = {
|
|
2085
|
-
stdout: (
|
|
2086
|
-
stderr: (
|
|
2294
|
+
stdout: (text2) => process.stdout.write(text2),
|
|
2295
|
+
stderr: (text2) => process.stderr.write(text2)
|
|
2087
2296
|
}, clientFactory = (config) => new Docmana(config), deps = {}) {
|
|
2088
2297
|
const program = buildProgram(env, io, clientFactory, deps);
|
|
2089
2298
|
if (argv.length === 0) {
|