@anybio/govern 0.2.1 → 0.3.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 +45 -0
- package/README.md +115 -30
- package/dist/adapters-DBLr_Frn.d.cts +39 -0
- package/dist/adapters-DBLr_Frn.d.ts +39 -0
- package/dist/chunk-3JV2R5IW.js +64 -0
- package/dist/chunk-CPNPQMEU.js +1860 -0
- package/dist/chunk-I5XIXB5M.cjs +1910 -0
- package/dist/chunk-XZHZBIZA.cjs +72 -0
- package/dist/cli.js +465 -115
- package/dist/{client-CU46a__g.d.cts → client-BeyMec7E.d.cts} +870 -152
- package/dist/{client-CU46a__g.d.ts → client-BeyMec7E.d.ts} +870 -152
- package/dist/index.cjs +165 -1536
- package/dist/index.d.cts +49 -7
- package/dist/index.d.ts +49 -7
- package/dist/index.js +1 -1503
- package/dist/langchain.cjs +65 -1501
- package/dist/langchain.d.cts +154 -17
- package/dist/langchain.d.ts +154 -17
- package/dist/langchain.js +59 -1495
- package/dist/vercel-ai.cjs +106 -1526
- package/dist/vercel-ai.d.cts +129 -8
- package/dist/vercel-ai.d.ts +129 -8
- package/dist/vercel-ai.js +100 -1520
- package/package.json +22 -21
- package/spec/README.md +17 -0
- package/spec/openapi.json +752 -158
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import { existsSync, readFileSync, mkdirSync, writeFileSync, appendFileSync } fr
|
|
|
3
3
|
import { resolve, join } from 'path';
|
|
4
4
|
|
|
5
5
|
// src/version.ts
|
|
6
|
-
var SDK_VERSION = "0.
|
|
6
|
+
var SDK_VERSION = "0.3.0";
|
|
7
7
|
var SDK_NAME = "@anybio/govern";
|
|
8
8
|
|
|
9
9
|
// src/cli/detect.ts
|
|
@@ -196,8 +196,11 @@ const reply = await govern.guard(
|
|
|
196
196
|
{ conversationId, turnId, channel: "${options.channel}", audience: "${options.audience}" },
|
|
197
197
|
);
|
|
198
198
|
// Empty when nothing is owed: an outbound hold with no holding line.
|
|
199
|
-
if (reply.text !== "")
|
|
200
|
-
await reply.
|
|
199
|
+
if (reply.text !== "") {
|
|
200
|
+
await deliver(reply.text);
|
|
201
|
+
// The record: what was written, hashed locally. No decision id when the API was unreachable.
|
|
202
|
+
if (reply.decisionId) await govern.reportOutcome(reply.decisionId, { kind: "released", at: new Date(), content: reply.text });
|
|
203
|
+
}`
|
|
201
204
|
};
|
|
202
205
|
}
|
|
203
206
|
function indent(text) {
|
|
@@ -247,6 +250,30 @@ var GovernApiError = class extends GovernError {
|
|
|
247
250
|
var GovernConflictError = class extends GovernApiError {
|
|
248
251
|
name = "GovernConflictError";
|
|
249
252
|
};
|
|
253
|
+
var GovernReportError = class extends GovernError {
|
|
254
|
+
name = "GovernReportError";
|
|
255
|
+
decisionId;
|
|
256
|
+
/** The kind the event had: from an adapter, `released` or `discarded`. */
|
|
257
|
+
kind;
|
|
258
|
+
/** The event's `at`, as it was sent. */
|
|
259
|
+
at;
|
|
260
|
+
/** The SHA-256 hex the event carried, on `released` and `delivered`; null otherwise, and when nothing was sent. */
|
|
261
|
+
contentHash;
|
|
262
|
+
/** The reference as sent, or as given when nothing was sent; trimmed as the API trims it either way. Null when there was none. */
|
|
263
|
+
deliveryRef;
|
|
264
|
+
/** The reason code, as `deliveryRef` is. Null when there was none. */
|
|
265
|
+
reason;
|
|
266
|
+
constructor(input) {
|
|
267
|
+
const why = input.cause instanceof Error ? `${input.cause.name}: ${input.cause.message}` : String(input.cause);
|
|
268
|
+
super(`AnyBio Govern could not record the ${input.kind} outcome of decision ${input.decisionId}: ${why}`, { cause: input.cause });
|
|
269
|
+
this.decisionId = input.decisionId;
|
|
270
|
+
this.kind = input.kind;
|
|
271
|
+
this.at = input.at;
|
|
272
|
+
this.contentHash = input.contentHash;
|
|
273
|
+
this.deliveryRef = input.deliveryRef;
|
|
274
|
+
this.reason = input.reason;
|
|
275
|
+
}
|
|
276
|
+
};
|
|
250
277
|
var GovernNetworkError = class extends GovernError {
|
|
251
278
|
name = "GovernNetworkError";
|
|
252
279
|
constructor(message, options) {
|
|
@@ -530,12 +557,108 @@ function now() {
|
|
|
530
557
|
return typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
531
558
|
}
|
|
532
559
|
|
|
560
|
+
// src/signal.ts
|
|
561
|
+
function isAbortSignal(value) {
|
|
562
|
+
if (typeof value !== "object" || value === null) return false;
|
|
563
|
+
const candidate = value;
|
|
564
|
+
return typeof candidate.aborted === "boolean" && typeof candidate.addEventListener === "function" && typeof candidate.removeEventListener === "function";
|
|
565
|
+
}
|
|
566
|
+
function requireAbortSignal(value, where) {
|
|
567
|
+
if (value === void 0 || value === null) return void 0;
|
|
568
|
+
if (isAbortSignal(value)) return value;
|
|
569
|
+
const slip = typeof value === "object" && "signal" in value ? " (pass `controller.signal`, not the controller)" : "";
|
|
570
|
+
const kind = typeof value === "function" ? "a function" : typeof value === "object" ? "an object that is not a signal" : `a ${typeof value}`;
|
|
571
|
+
throw new GovernConfigError(`${where} must be an AbortSignal${slip}; it was ${kind}.`);
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// src/text.ts
|
|
575
|
+
var WHITE_SPACE = /* @__PURE__ */ new Set([
|
|
576
|
+
9,
|
|
577
|
+
10,
|
|
578
|
+
11,
|
|
579
|
+
12,
|
|
580
|
+
13,
|
|
581
|
+
32,
|
|
582
|
+
133,
|
|
583
|
+
160,
|
|
584
|
+
5760,
|
|
585
|
+
8192,
|
|
586
|
+
8193,
|
|
587
|
+
8194,
|
|
588
|
+
8195,
|
|
589
|
+
8196,
|
|
590
|
+
8197,
|
|
591
|
+
8198,
|
|
592
|
+
8199,
|
|
593
|
+
8200,
|
|
594
|
+
8201,
|
|
595
|
+
8202,
|
|
596
|
+
8232,
|
|
597
|
+
8233,
|
|
598
|
+
8239,
|
|
599
|
+
8287,
|
|
600
|
+
12288
|
|
601
|
+
]);
|
|
602
|
+
function isControl(unit) {
|
|
603
|
+
return unit <= 31 || unit >= 127 && unit <= 159;
|
|
604
|
+
}
|
|
605
|
+
var MAX_SHORT_TEXT_CHARS = 256;
|
|
606
|
+
function codePoints(value) {
|
|
607
|
+
return [...value].length;
|
|
608
|
+
}
|
|
609
|
+
function trimAsApi(value) {
|
|
610
|
+
let start = 0;
|
|
611
|
+
let end = value.length;
|
|
612
|
+
while (start < end && WHITE_SPACE.has(value.charCodeAt(start))) start++;
|
|
613
|
+
while (end > start && WHITE_SPACE.has(value.charCodeAt(end - 1))) end--;
|
|
614
|
+
return value.slice(start, end);
|
|
615
|
+
}
|
|
616
|
+
function shortTextFault(value) {
|
|
617
|
+
if (typeof value !== "string") return "is not a string";
|
|
618
|
+
const trimmed = trimAsApi(value);
|
|
619
|
+
if (trimmed === "") return "is blank";
|
|
620
|
+
if (codePoints(trimmed) > MAX_SHORT_TEXT_CHARS) return `is longer than ${MAX_SHORT_TEXT_CHARS} characters`;
|
|
621
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
622
|
+
if (isControl(trimmed.charCodeAt(i))) return "carries a control character";
|
|
623
|
+
}
|
|
624
|
+
return void 0;
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// src/time.ts
|
|
628
|
+
var RFC3339_DATE_TIME = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:[Zz]|[+-](\d{2}):(\d{2}))$/;
|
|
629
|
+
function daysIn(year, month) {
|
|
630
|
+
if (month === 2) return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
631
|
+
return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31;
|
|
632
|
+
}
|
|
633
|
+
function isRfc3339DateTime(value) {
|
|
634
|
+
const m = RFC3339_DATE_TIME.exec(value);
|
|
635
|
+
if (!m) return false;
|
|
636
|
+
const [year, month, day, hour, minute, second] = m.slice(1, 7).map(Number);
|
|
637
|
+
if (month < 1 || month > 12) return false;
|
|
638
|
+
if (day < 1 || day > daysIn(year, month)) return false;
|
|
639
|
+
if (hour > 23 || minute > 59 || second > 60) return false;
|
|
640
|
+
if (m[7] !== void 0 && (Number(m[7]) > 23 || Number(m[8]) > 59)) return false;
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
function timeValue(value) {
|
|
644
|
+
if (typeof value === "string") return isRfc3339DateTime(value) ? value : void 0;
|
|
645
|
+
if (Object.prototype.toString.call(value) === "[object Date]") {
|
|
646
|
+
const time = Date.prototype.getTime.call(value);
|
|
647
|
+
return Number.isNaN(time) ? void 0 : new Date(time).toISOString();
|
|
648
|
+
}
|
|
649
|
+
return void 0;
|
|
650
|
+
}
|
|
651
|
+
|
|
533
652
|
// src/types.ts
|
|
534
653
|
var CHANNELS = ["sms", "app_message", "web_chat", "voice", "email"];
|
|
535
|
-
var IN_SESSION_CHANNELS = ["web_chat", "app_message"];
|
|
654
|
+
var IN_SESSION_CHANNELS = ["web_chat", "app_message", "voice"];
|
|
536
655
|
function isInSessionChannel(channel) {
|
|
537
656
|
return typeof channel === "string" && IN_SESSION_CHANNELS.includes(channel);
|
|
538
657
|
}
|
|
658
|
+
var OUTBOUND_CHANNELS = CHANNELS.filter((channel) => !IN_SESSION_CHANNELS.includes(channel));
|
|
659
|
+
function isOutboundChannel(channel) {
|
|
660
|
+
return typeof channel === "string" && OUTBOUND_CHANNELS.includes(channel);
|
|
661
|
+
}
|
|
539
662
|
var AUDIENCES = ["patient", "clinician"];
|
|
540
663
|
var REASONS = {
|
|
541
664
|
gateUnavailable: "gate_unavailable",
|
|
@@ -566,6 +689,16 @@ var REASONS = {
|
|
|
566
689
|
* configuration, not the API's availability: this does not heal on its own.
|
|
567
690
|
*/
|
|
568
691
|
clientMisconfigured: "client_misconfigured",
|
|
692
|
+
/**
|
|
693
|
+
* SDK-only: the API refused the turn with 409 because the record moved
|
|
694
|
+
* under it: the decision a `retryOf` names was already superseded,
|
|
695
|
+
* retried or delivered, or a reviewer acted on it first, or one a
|
|
696
|
+
* `supersedes` names was an enforce decision still being decided, or was
|
|
697
|
+
* delivered, reviewed, superseded or retried. A race on shared state, not
|
|
698
|
+
* a misconfiguration.
|
|
699
|
+
* `guard` still holds in enforce; refresh the decision and decide again.
|
|
700
|
+
*/
|
|
701
|
+
turnConflict: "turn_conflict",
|
|
569
702
|
/** SDK-only: the server sent a decision value this version does not know. */
|
|
570
703
|
unknownDecision: "unknown_decision"
|
|
571
704
|
};
|
|
@@ -744,10 +877,12 @@ var DEFAULT_HOLD_MESSAGE = "Thanks for your message. A person from our team will
|
|
|
744
877
|
var RESERVED_TURN_ID_PREFIX = "govern:";
|
|
745
878
|
var MAX_CONVERSATION_ID_CHARS = 187;
|
|
746
879
|
var MAX_TURN_ID_CHARS = 206;
|
|
880
|
+
var MAX_TURN_REF_CHARS = 128;
|
|
881
|
+
var MAX_SUPERSEDES = 8;
|
|
747
882
|
var TRANSIENT_HTTP_STATUSES = /* @__PURE__ */ new Set([408, 429]);
|
|
748
883
|
function isClientMisconfiguration(error) {
|
|
749
884
|
if (!(error instanceof GovernApiError)) return false;
|
|
750
|
-
if (error.status < 400 || error.status >= 500) return false;
|
|
885
|
+
if (error.status < 400 || error.status >= 500 || error.status === 409) return false;
|
|
751
886
|
return !TRANSIENT_HTTP_STATUSES.has(error.status);
|
|
752
887
|
}
|
|
753
888
|
var GOVERN_PATHS = {
|
|
@@ -783,10 +918,14 @@ var Govern = class {
|
|
|
783
918
|
onEvent;
|
|
784
919
|
modes = /* @__PURE__ */ new Map();
|
|
785
920
|
disclosureRefs = new BoundedMap(1e4);
|
|
786
|
-
pendingAcks = new BoundedMap(1e4);
|
|
787
|
-
reportedDecisions = new BoundedMap(1e4);
|
|
788
921
|
/** Decision ids whose holding line this instance has already armed. */
|
|
789
922
|
holdingLines = new BoundedMap(1e4);
|
|
923
|
+
/**
|
|
924
|
+
* Outcome reports on the wire, each settling (never rejecting) when its
|
|
925
|
+
* write is done. A report joins before anything of it is awaited, its hash
|
|
926
|
+
* included, so `flushReports` waits for every report started before it.
|
|
927
|
+
*/
|
|
928
|
+
reportsInFlight = /* @__PURE__ */ new Set();
|
|
790
929
|
releaseWaiters = /* @__PURE__ */ new Map();
|
|
791
930
|
constructor(options) {
|
|
792
931
|
if (!options.dangerouslyAllowBrowser && runningInBrowser()) {
|
|
@@ -807,6 +946,7 @@ var Govern = class {
|
|
|
807
946
|
this.application = options.application.trim();
|
|
808
947
|
this.applicationId = UUID.test(this.application) ? this.application.toLowerCase() : null;
|
|
809
948
|
this.baseUrl = options.baseUrl ?? DEFAULT_BASE_URL;
|
|
949
|
+
if (options.holdMessage !== void 0) requireHoldMessage(options.holdMessage, "Govern: `holdMessage`");
|
|
810
950
|
this.holdMessage = options.holdMessage ?? DEFAULT_HOLD_MESSAGE;
|
|
811
951
|
this.assumeMode = options.assumeMode ?? "enforce";
|
|
812
952
|
this.onEvent = isolateEvents(options.onEvent);
|
|
@@ -833,6 +973,11 @@ var Govern = class {
|
|
|
833
973
|
requireConversationId(input.conversationId);
|
|
834
974
|
requireTurnId(input.turnId);
|
|
835
975
|
requireText(input.content, "content");
|
|
976
|
+
const retryOf = input.retryOf ?? void 0;
|
|
977
|
+
if (retryOf !== void 0) requireRetryOf(retryOf);
|
|
978
|
+
const turnRef = input.turnRef ?? void 0;
|
|
979
|
+
if (turnRef !== void 0) requireTurnRef(turnRef);
|
|
980
|
+
const supersedes = supersedesOf(input.supersedes);
|
|
836
981
|
const disclosure = input.disclosure ?? this.disclosureAttachment(input.conversationId);
|
|
837
982
|
const body = {
|
|
838
983
|
conversation_id: input.conversationId,
|
|
@@ -860,6 +1005,9 @@ var Govern = class {
|
|
|
860
1005
|
if (model) body.model = model;
|
|
861
1006
|
const modelVersion = input.modelVersion?.trim();
|
|
862
1007
|
if (modelVersion) body.model_version = modelVersion;
|
|
1008
|
+
if (retryOf !== void 0) body.retry_of = retryOf;
|
|
1009
|
+
if (turnRef !== void 0) body.turn_ref = turnRef;
|
|
1010
|
+
if (supersedes !== void 0) body.supersedes = [...supersedes];
|
|
863
1011
|
const response = await send(this.transport, {
|
|
864
1012
|
method: "POST",
|
|
865
1013
|
path: GOVERN_PATHS.evaluate,
|
|
@@ -867,7 +1015,7 @@ var Govern = class {
|
|
|
867
1015
|
idempotencyKey: input.turnId,
|
|
868
1016
|
signal: input.signal
|
|
869
1017
|
});
|
|
870
|
-
const decision = this.read(toDecision(response.body, response.status === 202));
|
|
1018
|
+
const decision = this.read(toDecision(response.body, response.status === 202), { arm: outcomeStateOf(response.body) === null });
|
|
871
1019
|
this.onEvent?.({
|
|
872
1020
|
type: "decision",
|
|
873
1021
|
decisionId: decision.id,
|
|
@@ -894,30 +1042,76 @@ var Govern = class {
|
|
|
894
1042
|
return this.read(toDecision(response.body, false));
|
|
895
1043
|
}
|
|
896
1044
|
/**
|
|
897
|
-
*
|
|
898
|
-
*
|
|
899
|
-
*
|
|
1045
|
+
* Record one outcome event for a decision: `released` when you wrote the
|
|
1046
|
+
* reply to its channel, `delivered` when the channel confirmed it reached
|
|
1047
|
+
* the person, `discarded` when it was dropped before it reached anyone,
|
|
1048
|
+
* `failed` when an attempt to deliver it failed. The text of `released`
|
|
1049
|
+
* and `delivered` is hashed here with SHA-256 and only the hash is sent.
|
|
1050
|
+
*
|
|
1051
|
+
* One event per kind per decision, `failed` excepted: the same event again
|
|
1052
|
+
* is accepted and resolves with the decision as recorded; a different
|
|
1053
|
+
* event of a kind already on record is refused with 409
|
|
1054
|
+
* (`GovernConflictError`). The fields are checked before anything is sent
|
|
1055
|
+
* (`GovernConfigError` naming the field). The report is tracked from the
|
|
1056
|
+
* moment this is called, so `flushReports` waits for it.
|
|
900
1057
|
*/
|
|
901
1058
|
async reportOutcome(decisionId, input) {
|
|
902
1059
|
requireText(decisionId, "decisionId");
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1060
|
+
checkOutcome(input);
|
|
1061
|
+
return this.track(this.writeOutcome(decisionId, input));
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Internal: the report the adapters make. Not part of the supported
|
|
1065
|
+
* surface; call `reportOutcome`.
|
|
1066
|
+
*
|
|
1067
|
+
* Records one outcome event as `reportOutcome` does, and never rejects:
|
|
1068
|
+
* when the event does not land, `onFailure` is called with a
|
|
1069
|
+
* `GovernReportError` built from the body as it was sent (the `at`, the
|
|
1070
|
+
* hash and the trimmed texts the request carried; nothing is hashed
|
|
1071
|
+
* again), or, when the fields were refused before anything was sent, from
|
|
1072
|
+
* the fields as given, trimmed as the API trims them. A `kind` outside the
|
|
1073
|
+
* four a caller reports has no event to name: `onFailure` gets the
|
|
1074
|
+
* `GovernConfigError` itself. The whole of it, the fields' check, the hash,
|
|
1075
|
+
* the request and the call to `onFailure`, is one promise in
|
|
1076
|
+
* `reportsInFlight` from the moment this is called, so `flushReports`
|
|
1077
|
+
* resolves only after `onFailure` has run. A field refused before sending
|
|
1078
|
+
* reaches `onFailure` at once, with nothing awaited.
|
|
1079
|
+
*
|
|
1080
|
+
* @internal
|
|
1081
|
+
*/
|
|
1082
|
+
reportForAdapter(decisionId, input, onFailure) {
|
|
1083
|
+
let body;
|
|
1084
|
+
const done = (async () => {
|
|
1085
|
+
try {
|
|
1086
|
+
requireText(decisionId, "decisionId");
|
|
1087
|
+
checkOutcome(input);
|
|
1088
|
+
body = await outcomeBody(input);
|
|
1089
|
+
await this.postOutcome(decisionId, body, input.signal);
|
|
1090
|
+
} catch (cause) {
|
|
1091
|
+
let error;
|
|
1092
|
+
const kind = input.kind;
|
|
1093
|
+
if (typeof kind === "string" && REPORTED_KINDS.has(kind)) {
|
|
1094
|
+
const given = (value) => typeof value === "string" ? trimAsApi(value) : null;
|
|
1095
|
+
error = new GovernReportError({
|
|
1096
|
+
decisionId: String(decisionId),
|
|
1097
|
+
kind,
|
|
1098
|
+
at: body?.at ?? timeValue(input.at) ?? String(input.at),
|
|
1099
|
+
contentHash: body ? body.content_hash ?? null : null,
|
|
1100
|
+
deliveryRef: body ? body.delivery_ref ?? null : given(input.deliveryRef),
|
|
1101
|
+
reason: body ? body.reason ?? null : given(input.reason),
|
|
1102
|
+
cause
|
|
1103
|
+
});
|
|
1104
|
+
} else {
|
|
1105
|
+
error = cause instanceof GovernConfigError ? cause : new GovernConfigError(String(cause));
|
|
1106
|
+
}
|
|
1107
|
+
try {
|
|
1108
|
+
onFailure(error);
|
|
1109
|
+
} catch {
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
})();
|
|
1113
|
+
this.track(done);
|
|
1114
|
+
return done;
|
|
921
1115
|
}
|
|
922
1116
|
/**
|
|
923
1117
|
* Record the AI-identity disclosure shown at the start of a conversation.
|
|
@@ -993,27 +1187,43 @@ var Govern = class {
|
|
|
993
1187
|
*
|
|
994
1188
|
* - allow: the draft (or, for a clinician audience, the gate's substitution when it returns one)
|
|
995
1189
|
* - hold or block: the hold message when `showHoldText`, else the empty string
|
|
996
|
-
* - API unreachable: a hold, as above, when the
|
|
997
|
-
* enforce (or unknown), the draft flagged
|
|
1190
|
+
* - API unreachable, or `ctx.gateSignal` fired: a hold, as above, when the
|
|
1191
|
+
* channel's last known mode is enforce (or unknown), the draft flagged
|
|
1192
|
+
* `degraded` when it is observe or off
|
|
1193
|
+
*
|
|
1194
|
+
* `ctx.signal` cancels the call instead: its abort is rethrown as
|
|
1195
|
+
* `GovernAbortError`, and it wins when both signals fired.
|
|
998
1196
|
*
|
|
999
1197
|
* On a hold, `sendHoldingLine` says whether a holding line is owed to the
|
|
1000
1198
|
* patient now: at most once per decision in this instance, and never where
|
|
1001
1199
|
* the contract says another message is the wrong thing. `showHoldText`
|
|
1002
1200
|
* applies the channel: true on every hold in an in-session channel
|
|
1003
|
-
* (`web_chat`, `app_message`), where the patient is waiting for
|
|
1004
|
-
* and
|
|
1201
|
+
* (`web_chat`, `app_message`, `voice`), where the patient is waiting for
|
|
1202
|
+
* the reply, and `sendHoldingLine` on an outbound or unknown channel. The
|
|
1203
|
+
* API's `show_hold_text` wins when the decision carries it (see
|
|
1204
|
+
* `GuardResult.showHoldText`).
|
|
1005
1205
|
*
|
|
1006
|
-
* Never returns rewritten text for a patient audience.
|
|
1206
|
+
* Never returns rewritten text for a patient audience. Reports no outcome:
|
|
1207
|
+
* once you write `text`, record it with `reportOutcome` (see
|
|
1208
|
+
* `GuardResult.record`).
|
|
1007
1209
|
*/
|
|
1008
1210
|
async guard(fn, ctx) {
|
|
1009
1211
|
requireConversationId(ctx.conversationId);
|
|
1010
1212
|
if (ctx.turnId !== void 0) requireTurnId(ctx.turnId);
|
|
1011
|
-
|
|
1213
|
+
const retryOf = ctx.retryOf ?? void 0;
|
|
1214
|
+
if (retryOf !== void 0) requireRetryOf(retryOf);
|
|
1215
|
+
const turnRef = ctx.turnRef ?? void 0;
|
|
1216
|
+
if (turnRef !== void 0) requireTurnRef(turnRef);
|
|
1217
|
+
const supersedes = supersedesOf(ctx.supersedes);
|
|
1218
|
+
if (ctx.holdMessage !== void 0) requireHoldMessage(ctx.holdMessage, "guard: `ctx.holdMessage`");
|
|
1219
|
+
const callSignal = requireAbortSignal(ctx.signal, "guard: `ctx.signal`");
|
|
1220
|
+
const gateSignal = requireAbortSignal(ctx.gateSignal, "guard: `ctx.gateSignal`");
|
|
1012
1221
|
const produced = await fn();
|
|
1013
|
-
const
|
|
1014
|
-
if (typeof
|
|
1222
|
+
const producedText = typeof produced === "string" ? produced : produced?.text;
|
|
1223
|
+
if (typeof producedText !== "string") {
|
|
1015
1224
|
throw new GovernConfigError("guard: the draft function must return a string or { text: string }.");
|
|
1016
1225
|
}
|
|
1226
|
+
const draft = producedText;
|
|
1017
1227
|
const turnId = ctx.turnId ?? randomId();
|
|
1018
1228
|
const holdMessage = ctx.holdMessage ?? this.holdMessage;
|
|
1019
1229
|
let record = null;
|
|
@@ -1023,6 +1233,7 @@ var Govern = class {
|
|
|
1023
1233
|
let routeToHuman = false;
|
|
1024
1234
|
let handoff = null;
|
|
1025
1235
|
let inboundFlags = [];
|
|
1236
|
+
const requestSignal = eitherSignal(callSignal, gateSignal);
|
|
1026
1237
|
try {
|
|
1027
1238
|
const input = {
|
|
1028
1239
|
conversationId: ctx.conversationId,
|
|
@@ -1037,7 +1248,10 @@ var Govern = class {
|
|
|
1037
1248
|
if (ctx.inbound !== void 0) input.inbound = ctx.inbound;
|
|
1038
1249
|
if (ctx.model !== void 0) input.model = ctx.model;
|
|
1039
1250
|
if (ctx.modelVersion !== void 0) input.modelVersion = ctx.modelVersion;
|
|
1040
|
-
if (
|
|
1251
|
+
if (retryOf !== void 0) input.retryOf = retryOf;
|
|
1252
|
+
if (turnRef !== void 0) input.turnRef = turnRef;
|
|
1253
|
+
if (supersedes !== void 0) input.supersedes = supersedes;
|
|
1254
|
+
if (requestSignal.signal !== void 0) input.signal = requestSignal.signal;
|
|
1041
1255
|
record = await this.evaluate(input);
|
|
1042
1256
|
mode = record.mode;
|
|
1043
1257
|
degraded = record.degraded;
|
|
@@ -1045,9 +1259,10 @@ var Govern = class {
|
|
|
1045
1259
|
handoff = record.handoff;
|
|
1046
1260
|
inboundFlags = record.inboundFlags;
|
|
1047
1261
|
} catch (error) {
|
|
1048
|
-
if (error instanceof
|
|
1262
|
+
if (error instanceof GovernConfigError) throw error;
|
|
1263
|
+
if (error instanceof GovernAbortError && !gateBoundFired(callSignal, gateSignal)) throw error;
|
|
1049
1264
|
degraded = true;
|
|
1050
|
-
degradeCause = isClientMisconfiguration(error) ? REASONS.clientMisconfigured : REASONS.gateUnavailable;
|
|
1265
|
+
degradeCause = error instanceof GovernConflictError ? REASONS.turnConflict : isClientMisconfiguration(error) ? REASONS.clientMisconfigured : REASONS.gateUnavailable;
|
|
1051
1266
|
const learned = this.modes.get(ctx.channel);
|
|
1052
1267
|
mode = learned ?? this.assumeMode;
|
|
1053
1268
|
this.onEvent?.({
|
|
@@ -1061,6 +1276,8 @@ var Govern = class {
|
|
|
1061
1276
|
status: error instanceof GovernApiError ? error.status : null,
|
|
1062
1277
|
error: describe(error)
|
|
1063
1278
|
});
|
|
1279
|
+
} finally {
|
|
1280
|
+
requestSignal.dispose();
|
|
1064
1281
|
}
|
|
1065
1282
|
let decision;
|
|
1066
1283
|
let text;
|
|
@@ -1084,39 +1301,18 @@ var Govern = class {
|
|
|
1084
1301
|
}
|
|
1085
1302
|
const held = decision !== "allow";
|
|
1086
1303
|
const sendHoldingLine = held && record !== null && record.sendHoldingLine;
|
|
1087
|
-
const showHoldText =
|
|
1304
|
+
const showHoldText = holdTextShown({
|
|
1305
|
+
held,
|
|
1306
|
+
audience: ctx.audience,
|
|
1307
|
+
channel: ctx.channel,
|
|
1308
|
+
sendHoldingLine,
|
|
1309
|
+
wire: record?.raw ?? null
|
|
1310
|
+
});
|
|
1088
1311
|
if (held && !showHoldText) text = "";
|
|
1089
1312
|
const stopClass = record?.stopClass ?? null;
|
|
1090
|
-
const owed = !held || showHoldText;
|
|
1091
|
-
const decisionId = record?.id ?? null;
|
|
1092
|
-
if (decisionId) {
|
|
1093
|
-
const delivered = owed;
|
|
1094
|
-
this.pendingAcks.set(ctx.conversationId, {
|
|
1095
|
-
decisionId,
|
|
1096
|
-
delivered,
|
|
1097
|
-
contentHash: delivered ? await sha256Hex(text) : void 0,
|
|
1098
|
-
at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1099
|
-
});
|
|
1100
|
-
}
|
|
1101
|
-
let acked = false;
|
|
1102
|
-
const ack = async (options = {}) => {
|
|
1103
|
-
if (acked) return;
|
|
1104
|
-
acked = true;
|
|
1105
|
-
if (!decisionId) return;
|
|
1106
|
-
const pending = this.pendingAcks.get(ctx.conversationId);
|
|
1107
|
-
if (pending?.decisionId === decisionId) this.pendingAcks.delete(ctx.conversationId);
|
|
1108
|
-
if (this.reportedDecisions.has(decisionId)) return;
|
|
1109
|
-
const delivered = options.delivered ?? owed;
|
|
1110
|
-
const outcome = { delivered };
|
|
1111
|
-
if (options.content !== void 0) outcome.content = options.content;
|
|
1112
|
-
else if (delivered) outcome.content = text;
|
|
1113
|
-
if (options.deliveredAt !== void 0) outcome.deliveredAt = options.deliveredAt;
|
|
1114
|
-
if (options.deliveryRef !== void 0) outcome.deliveryRef = options.deliveryRef;
|
|
1115
|
-
await this.reportOutcome(decisionId, outcome);
|
|
1116
|
-
};
|
|
1117
1313
|
return {
|
|
1118
1314
|
text,
|
|
1119
|
-
decisionId,
|
|
1315
|
+
decisionId: record?.id ?? null,
|
|
1120
1316
|
decision,
|
|
1121
1317
|
mode,
|
|
1122
1318
|
degraded,
|
|
@@ -1128,14 +1324,20 @@ var Govern = class {
|
|
|
1128
1324
|
routeToHuman,
|
|
1129
1325
|
handoff,
|
|
1130
1326
|
inboundFlags,
|
|
1131
|
-
record
|
|
1132
|
-
ack
|
|
1327
|
+
record
|
|
1133
1328
|
};
|
|
1134
1329
|
}
|
|
1135
|
-
/**
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1330
|
+
/**
|
|
1331
|
+
* Wait for every outcome report already on the wire: those `reportOutcome`
|
|
1332
|
+
* was called for and those an adapter made. Resolves once each has
|
|
1333
|
+
* settled, whether it was recorded or failed (a failure reaches the caller
|
|
1334
|
+
* of `reportOutcome`, or an adapter's `onError`). Call it on shutdown: a
|
|
1335
|
+
* report still in flight when the process exits is lost. A
|
|
1336
|
+
* `deliveredText` hook that has not answered yet has no report on the wire,
|
|
1337
|
+
* so it is not waited for.
|
|
1338
|
+
*/
|
|
1339
|
+
async flushReports() {
|
|
1340
|
+
while (this.reportsInFlight.size > 0) await Promise.all([...this.reportsInFlight]);
|
|
1139
1341
|
}
|
|
1140
1342
|
// -------------------------------------------------------------------------
|
|
1141
1343
|
// Memory the caller may read or seed
|
|
@@ -1267,40 +1469,35 @@ var Govern = class {
|
|
|
1267
1469
|
// -------------------------------------------------------------------------
|
|
1268
1470
|
// Internals
|
|
1269
1471
|
// -------------------------------------------------------------------------
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1472
|
+
/**
|
|
1473
|
+
* Hash the text (when no hash is given) and post the event. The caller has
|
|
1474
|
+
* checked the fields. An outcome is recorded after something happened to
|
|
1475
|
+
* the reply, so the answer never arms a holding line; it does count as
|
|
1476
|
+
* having seen the decision.
|
|
1477
|
+
*/
|
|
1478
|
+
async writeOutcome(decisionId, input) {
|
|
1479
|
+
return this.postOutcome(decisionId, await outcomeBody(input), input.signal);
|
|
1480
|
+
}
|
|
1481
|
+
/** Post a built outcome body. */
|
|
1482
|
+
async postOutcome(decisionId, body, signal) {
|
|
1277
1483
|
const response = await send(this.transport, {
|
|
1278
1484
|
method: "POST",
|
|
1279
1485
|
path: GOVERN_PATHS.outcome(decisionId),
|
|
1280
1486
|
body,
|
|
1281
|
-
signal
|
|
1487
|
+
signal
|
|
1282
1488
|
});
|
|
1489
|
+
this.onEvent?.({ type: "outcome", decisionId, kind: body.kind });
|
|
1283
1490
|
return this.read(toDecision(response.body, false), { arm: false });
|
|
1284
1491
|
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
contentHash: pending.contentHash,
|
|
1295
|
-
deliveryRef: void 0,
|
|
1296
|
-
signal: void 0
|
|
1297
|
-
});
|
|
1298
|
-
this.reportedDecisions.set(pending.decisionId, true);
|
|
1299
|
-
this.onEvent?.({ type: "outcome", decisionId: pending.decisionId, delivered: pending.delivered, auto: true });
|
|
1300
|
-
} catch (error) {
|
|
1301
|
-
if (!this.pendingAcks.has(conversationId)) this.pendingAcks.set(conversationId, pending);
|
|
1302
|
-
this.onEvent?.({ type: "error", operation: "auto_ack", error: describe(error) });
|
|
1303
|
-
}
|
|
1492
|
+
/** Keep `work` in `reportsInFlight` until it settles, and hand it back unchanged. */
|
|
1493
|
+
track(work) {
|
|
1494
|
+
const settled = work.then(
|
|
1495
|
+
() => void 0,
|
|
1496
|
+
() => void 0
|
|
1497
|
+
);
|
|
1498
|
+
this.reportsInFlight.add(settled);
|
|
1499
|
+
void settled.then(() => this.reportsInFlight.delete(settled));
|
|
1500
|
+
return work;
|
|
1304
1501
|
}
|
|
1305
1502
|
disclosureAttachment(conversationId) {
|
|
1306
1503
|
const ref = this.disclosureRefs.get(conversationId);
|
|
@@ -1326,9 +1523,11 @@ var Govern = class {
|
|
|
1326
1523
|
this.modes.set(decision.channel, decision.mode);
|
|
1327
1524
|
const id = decision.id;
|
|
1328
1525
|
if (!decision.sendHoldingLine || !id) return decision;
|
|
1329
|
-
const eligible = options.arm && holdingLineApplies(decision) && !this.holdingLines.has(id);
|
|
1330
|
-
this.holdingLines.set(id, true);
|
|
1331
|
-
|
|
1526
|
+
const eligible = options.arm && holdingLineApplies(decision) && !this.holdingLines.has(decisionKey(id));
|
|
1527
|
+
this.holdingLines.set(decisionKey(id), true);
|
|
1528
|
+
if (eligible) return decision;
|
|
1529
|
+
const narrowed = { ...decision, sendHoldingLine: false };
|
|
1530
|
+
return { ...narrowed, showHoldText: decisionShowsHoldText(narrowed) };
|
|
1332
1531
|
}
|
|
1333
1532
|
sleepOrWake(decisionId, ms, signal) {
|
|
1334
1533
|
return new Promise((resolve2, reject) => {
|
|
@@ -1368,7 +1567,7 @@ function toDecision(wire, accepted) {
|
|
|
1368
1567
|
const known = KNOWN_VERDICTS.has(rawVerdict);
|
|
1369
1568
|
const failOpen = wire.mode === "observe" || wire.mode === "off";
|
|
1370
1569
|
const decision = known ? rawVerdict : failOpen ? "allow" : "hold";
|
|
1371
|
-
|
|
1570
|
+
const mapped = {
|
|
1372
1571
|
id: wire.decision_id ?? null,
|
|
1373
1572
|
status: wire.status,
|
|
1374
1573
|
decision,
|
|
@@ -1392,12 +1591,17 @@ function toDecision(wire, accepted) {
|
|
|
1392
1591
|
// The API's value. `Govern` narrows it to at most once per decision id on
|
|
1393
1592
|
// the way out (see `Govern.read`); `raw.send_holding_line` keeps the original.
|
|
1394
1593
|
sendHoldingLine: wire.send_holding_line === true,
|
|
1594
|
+
// Set below, from the rest of the mapped decision.
|
|
1595
|
+
showHoldText: false,
|
|
1596
|
+
// Absent from a server that predates the fields: not retryable, not a retry.
|
|
1597
|
+
retryable: wire.retryable === true,
|
|
1598
|
+
retryOf: typeof wire.retry_of === "string" ? wire.retry_of : null,
|
|
1395
1599
|
model: typeof wire.model === "string" ? wire.model : null,
|
|
1396
1600
|
modelVersion: typeof wire.model_version === "string" ? wire.model_version : null,
|
|
1397
1601
|
createdAt: wire.created_at ?? null,
|
|
1398
1602
|
decidedAt: wire.decided_at ?? null,
|
|
1399
1603
|
reviewedAt: wire.reviewed_at ?? null,
|
|
1400
|
-
outcome:
|
|
1604
|
+
outcome: toOutcome(wire.outcome),
|
|
1401
1605
|
// Read from the typed wire. Absent means false.
|
|
1402
1606
|
routeToHuman: wire.route_to_human === true,
|
|
1403
1607
|
wouldHaveRouted: wire.would_have_routed === true,
|
|
@@ -1409,6 +1613,8 @@ function toDecision(wire, accepted) {
|
|
|
1409
1613
|
degraded: !known,
|
|
1410
1614
|
raw: wire
|
|
1411
1615
|
};
|
|
1616
|
+
mapped.showHoldText = decisionShowsHoldText(mapped);
|
|
1617
|
+
return mapped;
|
|
1412
1618
|
}
|
|
1413
1619
|
function toStopClass(value) {
|
|
1414
1620
|
if (value === null || value === void 0) return null;
|
|
@@ -1427,6 +1633,27 @@ function holdingLineApplies(decision) {
|
|
|
1427
1633
|
if (decision.reviewState === "released" || decision.reviewState === "rejected") return false;
|
|
1428
1634
|
return !decision.reasons.some((reason) => NO_HOLDING_LINE.has(reason));
|
|
1429
1635
|
}
|
|
1636
|
+
function holdTextShown(input) {
|
|
1637
|
+
const { held, audience, channel, sendHoldingLine, wire } = input;
|
|
1638
|
+
if (!held) return false;
|
|
1639
|
+
const inSession = isInSessionChannel(channel);
|
|
1640
|
+
let shown;
|
|
1641
|
+
if (typeof wire?.show_hold_text === "boolean") shown = wire.show_hold_text;
|
|
1642
|
+
else if (wire?.review_state === "released" || wire?.review_state === "rejected") shown = false;
|
|
1643
|
+
else shown = audience !== "patient" || inSession || sendHoldingLine;
|
|
1644
|
+
if (!shown || audience !== "patient" || inSession) return shown;
|
|
1645
|
+
if (isOutboundChannel(channel)) return sendHoldingLine;
|
|
1646
|
+
return sendHoldingLine || wire?.send_holding_line !== true;
|
|
1647
|
+
}
|
|
1648
|
+
function decisionShowsHoldText(decision) {
|
|
1649
|
+
return holdTextShown({
|
|
1650
|
+
held: decision.decision !== "allow",
|
|
1651
|
+
audience: decision.audience,
|
|
1652
|
+
channel: decision.channel,
|
|
1653
|
+
sendHoldingLine: decision.sendHoldingLine,
|
|
1654
|
+
wire: decision.raw
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1430
1657
|
function toInboundFlags(value) {
|
|
1431
1658
|
if (!Array.isArray(value)) return [];
|
|
1432
1659
|
const out = [];
|
|
@@ -1457,7 +1684,7 @@ function toHandoffRef(value) {
|
|
|
1457
1684
|
resolvedAt: str2(h.resolved_at),
|
|
1458
1685
|
automationResumedAt: str2(h.automation_resumed_at),
|
|
1459
1686
|
// Absent only from a server that predates the field, where every open
|
|
1460
|
-
// hand-off paused automation; read it that way
|
|
1687
|
+
// hand-off paused automation; read it that way, never as "not paused".
|
|
1461
1688
|
pausesAutomation: h.pauses_automation !== false
|
|
1462
1689
|
};
|
|
1463
1690
|
}
|
|
@@ -1544,16 +1771,31 @@ var GovernHandoffs = class {
|
|
|
1544
1771
|
}
|
|
1545
1772
|
};
|
|
1546
1773
|
function toOutcome(wire) {
|
|
1774
|
+
if (!wire || typeof wire !== "object" || typeof wire.state !== "string") return null;
|
|
1547
1775
|
return {
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1776
|
+
state: wire.state,
|
|
1777
|
+
events: Array.isArray(wire.events) ? wire.events.map(toOutcomeEvent) : [],
|
|
1778
|
+
raw: wire
|
|
1779
|
+
};
|
|
1780
|
+
}
|
|
1781
|
+
function toOutcomeEvent(wire) {
|
|
1782
|
+
return {
|
|
1783
|
+
kind: wire.kind,
|
|
1784
|
+
source: wire.source,
|
|
1785
|
+
at: wire.at,
|
|
1552
1786
|
recordedAt: wire.recorded_at,
|
|
1787
|
+
contentHash: wire.content_hash ?? null,
|
|
1788
|
+
deliveryRef: wire.delivery_ref ?? null,
|
|
1789
|
+
reason: wire.reason ?? null,
|
|
1553
1790
|
hashMatchesRelease: wire.hash_matches_release ?? null,
|
|
1791
|
+
supersededBy: wire.superseded_by ?? null,
|
|
1554
1792
|
raw: wire
|
|
1555
1793
|
};
|
|
1556
1794
|
}
|
|
1795
|
+
function outcomeStateOf(wire) {
|
|
1796
|
+
const state = wire.outcome?.state;
|
|
1797
|
+
return typeof state === "string" ? state : null;
|
|
1798
|
+
}
|
|
1557
1799
|
function toTranscriptJob(wire, created) {
|
|
1558
1800
|
return {
|
|
1559
1801
|
jobId: wire.job_id,
|
|
@@ -1588,9 +1830,6 @@ function requireText(value, name) {
|
|
|
1588
1830
|
throw new GovernConfigError(`${name} is required and must be a non-empty string.`);
|
|
1589
1831
|
}
|
|
1590
1832
|
}
|
|
1591
|
-
function codePoints(value) {
|
|
1592
|
-
return [...value].length;
|
|
1593
|
-
}
|
|
1594
1833
|
function requireConversationId(value, name = "conversationId") {
|
|
1595
1834
|
requireText(value, name);
|
|
1596
1835
|
if (codePoints(value.trim()) > MAX_CONVERSATION_ID_CHARS) {
|
|
@@ -1599,6 +1838,32 @@ function requireConversationId(value, name = "conversationId") {
|
|
|
1599
1838
|
);
|
|
1600
1839
|
}
|
|
1601
1840
|
}
|
|
1841
|
+
function requireRetryOf(value, name = "retryOf") {
|
|
1842
|
+
if (typeof value !== "string" || !UUID.test(value)) {
|
|
1843
|
+
throw new GovernConfigError(`${name} must be the id of the decision this turn retries (a UUID).`);
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
function requireTurnRef(value, name = "turnRef") {
|
|
1847
|
+
requireText(value, name);
|
|
1848
|
+
if (codePoints(value) > MAX_TURN_REF_CHARS) {
|
|
1849
|
+
throw new GovernConfigError(`${name} must be at most ${MAX_TURN_REF_CHARS} characters.`);
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
function supersedesOf(value, name = "supersedes") {
|
|
1853
|
+
if (value === void 0 || value === null) return void 0;
|
|
1854
|
+
if (!Array.isArray(value)) {
|
|
1855
|
+
throw new GovernConfigError(`${name} must be a list of the decision ids this turn replaces.`);
|
|
1856
|
+
}
|
|
1857
|
+
if (value.length > MAX_SUPERSEDES) {
|
|
1858
|
+
throw new GovernConfigError(`${name} may name at most ${MAX_SUPERSEDES} decisions.`);
|
|
1859
|
+
}
|
|
1860
|
+
for (const id of value) {
|
|
1861
|
+
if (typeof id !== "string" || !UUID.test(id)) {
|
|
1862
|
+
throw new GovernConfigError(`${name} must hold only decision ids (UUIDs).`);
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
return value.length === 0 ? void 0 : [...value];
|
|
1866
|
+
}
|
|
1602
1867
|
function requireTurnId(value, name = "turnId") {
|
|
1603
1868
|
requireText(value, name);
|
|
1604
1869
|
const trimmed = value.trim();
|
|
@@ -1616,6 +1881,91 @@ function requireTurnId(value, name = "turnId") {
|
|
|
1616
1881
|
function idempotencyKey(given) {
|
|
1617
1882
|
return given?.trim() || randomId();
|
|
1618
1883
|
}
|
|
1884
|
+
function eitherSignal(first, second) {
|
|
1885
|
+
const none = () => {
|
|
1886
|
+
};
|
|
1887
|
+
if (first === void 0 || second === void 0) return { signal: first ?? second, dispose: none };
|
|
1888
|
+
if (first.aborted) return { signal: first, dispose: none };
|
|
1889
|
+
if (second.aborted) return { signal: second, dispose: none };
|
|
1890
|
+
const controller = new AbortController();
|
|
1891
|
+
const onFirst = () => controller.abort(first.reason);
|
|
1892
|
+
const onSecond = () => controller.abort(second.reason);
|
|
1893
|
+
first.addEventListener("abort", onFirst, { once: true });
|
|
1894
|
+
second.addEventListener("abort", onSecond, { once: true });
|
|
1895
|
+
return {
|
|
1896
|
+
signal: controller.signal,
|
|
1897
|
+
dispose: () => {
|
|
1898
|
+
first.removeEventListener("abort", onFirst);
|
|
1899
|
+
second.removeEventListener("abort", onSecond);
|
|
1900
|
+
}
|
|
1901
|
+
};
|
|
1902
|
+
}
|
|
1903
|
+
function requireHoldMessage(value, where) {
|
|
1904
|
+
if (typeof value !== "string" || value.trim() === "") {
|
|
1905
|
+
throw new GovernConfigError(`${where} must be a non-empty string: it is the text a held patient sees in place of the reply.`);
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
function decisionKey(decisionId) {
|
|
1909
|
+
return decisionId.toLowerCase();
|
|
1910
|
+
}
|
|
1911
|
+
var REPORTED_KINDS = /* @__PURE__ */ new Set(["released", "delivered", "discarded", "failed"]);
|
|
1912
|
+
var HASHED_KINDS = /* @__PURE__ */ new Set(["released", "delivered"]);
|
|
1913
|
+
async function outcomeBody(input) {
|
|
1914
|
+
let hash;
|
|
1915
|
+
if (HASHED_KINDS.has(input.kind)) {
|
|
1916
|
+
const given = trimAsApi(input.contentHash ?? "").toLowerCase();
|
|
1917
|
+
hash = given === "" ? void 0 : given;
|
|
1918
|
+
if (hash === void 0 && typeof input.content === "string" && input.content !== "") hash = await sha256Hex(input.content);
|
|
1919
|
+
}
|
|
1920
|
+
const body = { kind: input.kind, at: timeValue(input.at) };
|
|
1921
|
+
if (hash !== void 0) body.content_hash = hash;
|
|
1922
|
+
if (typeof input.deliveryRef === "string") body.delivery_ref = trimAsApi(input.deliveryRef);
|
|
1923
|
+
if (typeof input.reason === "string") body.reason = trimAsApi(input.reason);
|
|
1924
|
+
return body;
|
|
1925
|
+
}
|
|
1926
|
+
var SHA256_HEX = /^[0-9a-f]{64}$/i;
|
|
1927
|
+
function checkShortText(value, name) {
|
|
1928
|
+
if (value === void 0 || value === null) return;
|
|
1929
|
+
const fault = shortTextFault(value);
|
|
1930
|
+
if (fault === "is not a string") throw new GovernConfigError(`reportOutcome: \`${name}\` must be a string.`);
|
|
1931
|
+
if (fault !== void 0) {
|
|
1932
|
+
throw new GovernConfigError(`reportOutcome: \`${name}\` must not be blank, must be at most ${MAX_SHORT_TEXT_CHARS} characters, and must carry no control characters; it ${fault}.`);
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
function checkOutcome(input) {
|
|
1936
|
+
const kind = input.kind;
|
|
1937
|
+
if (kind === "superseded") {
|
|
1938
|
+
throw new GovernConfigError("reportOutcome: `superseded` is recorded by the API itself, when it accepts a turn that replaces the decision; report released, delivered, discarded or failed.");
|
|
1939
|
+
}
|
|
1940
|
+
if (typeof kind !== "string" || !REPORTED_KINDS.has(kind)) {
|
|
1941
|
+
throw new GovernConfigError("reportOutcome: `kind` must be released, delivered, discarded or failed.");
|
|
1942
|
+
}
|
|
1943
|
+
if (timeValue(input.at) === void 0) {
|
|
1944
|
+
throw new GovernConfigError("reportOutcome: `at` is required and must be a valid Date or an RFC 3339 date-time string, such as 2026-09-16T10:00:00Z.");
|
|
1945
|
+
}
|
|
1946
|
+
if (input.content !== void 0 && input.content !== null && typeof input.content !== "string") {
|
|
1947
|
+
throw new GovernConfigError("reportOutcome: `content` must be a string.");
|
|
1948
|
+
}
|
|
1949
|
+
if (input.contentHash !== void 0 && input.contentHash !== null && typeof input.contentHash !== "string") {
|
|
1950
|
+
throw new GovernConfigError("reportOutcome: `contentHash` must be a string (the SHA-256 hex of the text).");
|
|
1951
|
+
}
|
|
1952
|
+
const givenHash = typeof input.contentHash === "string" ? trimAsApi(input.contentHash) : "";
|
|
1953
|
+
if (givenHash !== "" && !SHA256_HEX.test(givenHash)) {
|
|
1954
|
+
throw new GovernConfigError("reportOutcome: `contentHash` must be the SHA-256 of the UTF-8 text, as 64 hex characters.");
|
|
1955
|
+
}
|
|
1956
|
+
const hasText = typeof input.content === "string" && input.content !== "" || givenHash !== "";
|
|
1957
|
+
if (HASHED_KINDS.has(kind) && !hasText) {
|
|
1958
|
+
throw new GovernConfigError(`reportOutcome: \`${kind}\` carries the hash of the text: pass \`content\` (hashed locally) or \`contentHash\`.`);
|
|
1959
|
+
}
|
|
1960
|
+
if (!HASHED_KINDS.has(kind) && hasText) {
|
|
1961
|
+
throw new GovernConfigError(`reportOutcome: \`${kind}\` carries no text; pass neither \`content\` nor \`contentHash\`.`);
|
|
1962
|
+
}
|
|
1963
|
+
checkShortText(input.deliveryRef, "deliveryRef");
|
|
1964
|
+
checkShortText(input.reason, "reason");
|
|
1965
|
+
}
|
|
1966
|
+
function gateBoundFired(callSignal, gateSignal) {
|
|
1967
|
+
return gateSignal?.aborted === true && callSignal?.aborted !== true;
|
|
1968
|
+
}
|
|
1619
1969
|
function isoString(value) {
|
|
1620
1970
|
return value instanceof Date ? value.toISOString() : value;
|
|
1621
1971
|
}
|