@anybio/govern 0.2.2 → 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 +41 -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 +1 -1
- package/spec/README.md +1 -1
- package/spec/openapi.json +752 -158
package/dist/langchain.d.cts
CHANGED
|
@@ -3,7 +3,9 @@ import { BaseMessage, AIMessage } from '@langchain/core/messages';
|
|
|
3
3
|
import { Serialized } from '@langchain/core/load/serializable';
|
|
4
4
|
import { LLMResult } from '@langchain/core/outputs';
|
|
5
5
|
import { RunnableConfig, Runnable } from '@langchain/core/runnables';
|
|
6
|
-
import {
|
|
6
|
+
import { D as DeliveredText } from './adapters-DBLr_Frn.cjs';
|
|
7
|
+
export { a as DeliveredMessage } from './adapters-DBLr_Frn.cjs';
|
|
8
|
+
import { f as Decision, G as Govern, o as GovernOptions, b as Channel, a as Audience, J as InboundMessage, q as GuardResult } from './client-BeyMec7E.cjs';
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* `@anybio/govern/langchain`: a callback handler that records, and a runnable
|
|
@@ -25,17 +27,63 @@ import { G as Govern, p as GovernOptions, c as Channel, b as Audience, K as Inbo
|
|
|
25
27
|
* `modelVersion` is the `modelVersion` option, else the model the provider
|
|
26
28
|
* reported on the response (`response_metadata.model_name`, or `model`), when
|
|
27
29
|
* it reports one.
|
|
30
|
+
*
|
|
31
|
+
* Two signals, kept apart on purpose. The run's own `config.signal` is
|
|
32
|
+
* LangChain's cancel: it is never passed to the gate, so it never becomes a
|
|
33
|
+
* hold. A run cancelled before `governed`'s gate call is not gated, and one
|
|
34
|
+
* cancelled while the gate decides is recorded as `discarded`, since the
|
|
35
|
+
* caller's `invoke` has ended and nothing reached the patient.
|
|
36
|
+
* `configurable.anybioGovern.signal` bounds only `governed`'s gate call, and
|
|
37
|
+
* its abort is an answer: the rule for an unreachable API decides what is
|
|
38
|
+
* returned.
|
|
28
39
|
*/
|
|
29
40
|
|
|
30
41
|
/** The key under `config.configurable` the adapter reads per-call identifiers from. */
|
|
31
42
|
declare const CONFIGURABLE_KEY = "anybioGovern";
|
|
32
43
|
/** The key under `AIMessage.response_metadata` the wrapper writes the decision to. */
|
|
33
44
|
declare const RESPONSE_METADATA_KEY = "anybio_govern";
|
|
45
|
+
/** What a caller may pass as `config.configurable.anybioGovern` on one call. */
|
|
34
46
|
interface GovernConfigurable {
|
|
35
47
|
conversationId?: string;
|
|
36
48
|
turnId?: string;
|
|
49
|
+
/** The decision this call's turn retries. See `EvaluateInput.retryOf`. `null` is no retry. */
|
|
50
|
+
retryOf?: string | null;
|
|
51
|
+
/** Your stable reference for the patient turn. See `EvaluateInput.turnRef`. `null` is none. */
|
|
52
|
+
turnRef?: string | null;
|
|
53
|
+
/** The decisions this call's turn replaces. See `EvaluateInput.supersedes`. `null` is none. */
|
|
54
|
+
supersedes?: readonly string[] | null;
|
|
37
55
|
patientRef?: string;
|
|
38
56
|
metadata?: Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Bounds `governed`'s gate call for this call.
|
|
59
|
+
*
|
|
60
|
+
* When it aborts, before or during the gate call, the call ends (no retry
|
|
61
|
+
* follows) and the rule for an unreachable API applies: in enforce the
|
|
62
|
+
* output is held, with the hold message where `showHoldText` is true and
|
|
63
|
+
* empty text where it is false; in observe the output comes back flagged
|
|
64
|
+
* `degraded`. The reply carries `reasons: ["gate_unavailable"]` and no
|
|
65
|
+
* decision id, and the client emits its `degraded` event. It travels to
|
|
66
|
+
* `guard` as `gateSignal`, which answers this way; the call's own `signal`
|
|
67
|
+
* would be rethrown instead, and the wrapper owns the reply.
|
|
68
|
+
*
|
|
69
|
+
* Not the run's `config.signal`, which is LangChain's cancel and is never
|
|
70
|
+
* passed to the gate: wiring it in would turn a cancel into a hold.
|
|
71
|
+
* `GovernCallbackHandler` sees no per-call config and makes no gate call,
|
|
72
|
+
* so it takes no signal.
|
|
73
|
+
*
|
|
74
|
+
* A signal or a function that returns one. A signal's clock starts when
|
|
75
|
+
* you create it, so one made before `invoke` bounds the wrapped runnable
|
|
76
|
+
* and the gate together, and it belongs to one call: bound with
|
|
77
|
+
* `withConfig`, the same signal is shared by every call after it, and once
|
|
78
|
+
* it fires every one of them holds. The function is called when the gate
|
|
79
|
+
* call starts, once per call, so `() => AbortSignal.timeout(ms)` is a
|
|
80
|
+
* fresh bound measured from the gate call, safe to bind once. `null`, or
|
|
81
|
+
* a function returning `null` or `undefined`, is no bound. A value that is
|
|
82
|
+
* neither a signal nor a function is refused with `GovernConfigError`
|
|
83
|
+
* before the wrapped runnable runs; a function that returns something
|
|
84
|
+
* else is refused when the gate call starts.
|
|
85
|
+
*/
|
|
86
|
+
signal?: AbortSignal | null | ((config: RunnableConfig | undefined) => AbortSignal | null | undefined);
|
|
39
87
|
}
|
|
40
88
|
type Resolvable<T> = T | ((config: RunnableConfig | undefined) => T | undefined);
|
|
41
89
|
interface GovernLangChainOptions {
|
|
@@ -57,14 +105,88 @@ interface GovernLangChainOptions {
|
|
|
57
105
|
model?: Resolvable<string>;
|
|
58
106
|
/** The model version recorded on each decision. Default: the model the provider reported on the response, when it reports one. */
|
|
59
107
|
modelVersion?: Resolvable<string>;
|
|
108
|
+
/**
|
|
109
|
+
* The patient's inbound message, for the red-flag screen. Requires
|
|
110
|
+
* `patientRef` (the option, or `configurable.anybioGovern.patientRef`).
|
|
111
|
+
*/
|
|
60
112
|
inbound?: (config: RunnableConfig | undefined) => InboundMessage | undefined;
|
|
61
113
|
holdMessage?: string;
|
|
62
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Called when recording an outcome fails, and with a failing
|
|
116
|
+
* `deliveredText`. Nothing retries a failed report after the transport's
|
|
117
|
+
* own retries. A report that did not land arrives as a
|
|
118
|
+
* `GovernReportError`: the decision id and the event exactly as sent
|
|
119
|
+
* (`kind`, `at`, `contentHash`, `deliveryRef`, `reason`), to send again
|
|
120
|
+
* with `govern.reportOutcome`, and the failure on `cause` (a
|
|
121
|
+
* `GovernConflictError` when a different event of that kind is already on
|
|
122
|
+
* record). A hook that throws or answers something else arrives as itself.
|
|
123
|
+
*/
|
|
63
124
|
onError?: (error: unknown) => void;
|
|
64
125
|
}
|
|
126
|
+
/** What `governed` hands `onDecision` and `deliveredText`. */
|
|
127
|
+
interface GovernedDecisionInfo {
|
|
128
|
+
reply: GuardResult;
|
|
129
|
+
conversationId: string;
|
|
130
|
+
turnId: string;
|
|
131
|
+
}
|
|
132
|
+
/** What `GovernCallbackHandler` hands `onDecision` and `deliveredText`. */
|
|
133
|
+
interface GovernCallbackDecisionInfo {
|
|
134
|
+
decision: Decision;
|
|
135
|
+
runId: string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The call's context (conversation id, turn id, `retryOf`, patient
|
|
139
|
+
* reference, metadata, inbound and the per-call bound) is resolved before
|
|
140
|
+
* the wrapped runnable runs, as `withGovern` resolves it before the model is
|
|
141
|
+
* called. A run that could not be gated fails with `GovernConfigError`
|
|
142
|
+
* before the model runs, even one that would never reach the gate (an empty
|
|
143
|
+
* output, or `ungatedOutput: "pass"`), and a resolver function you pass sees
|
|
144
|
+
* the state before the chain. Only the model version the provider reports is
|
|
145
|
+
* read from the output, afterwards.
|
|
146
|
+
*/
|
|
65
147
|
interface GovernedOptions extends GovernLangChainOptions {
|
|
66
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* Record the write-time outcome once per decision, as the wrapper returns:
|
|
150
|
+
* `released` with the hash of the text it returned, or of what
|
|
151
|
+
* `deliveredText` says went out; `discarded` when nothing went out.
|
|
152
|
+
* Default true.
|
|
153
|
+
*
|
|
154
|
+
* With false the wrapper reports nothing, and the outcome is yours alone
|
|
155
|
+
* to record with `govern.reportOutcome`. A decision you never report stays
|
|
156
|
+
* without an outcome.
|
|
157
|
+
*/
|
|
67
158
|
reportOutcome?: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* What actually went out, for the outcome report. Without it the wrapper
|
|
161
|
+
* records `released` with the text it returned, or `discarded`
|
|
162
|
+
* (`nothing_to_send`) when it returned empty text. With it, the wrapper
|
|
163
|
+
* asks: return the text you wrote to the channel (it is hashed locally;
|
|
164
|
+
* only the hash is sent), `{ text, deliveredAt?, deliveryRef? }` to say
|
|
165
|
+
* when it went out and under which reference, or `null` when nothing went
|
|
166
|
+
* out. Text is recorded as `released`, a shown hold message included;
|
|
167
|
+
* `null` and empty text as `discarded` (`nothing_delivered`).
|
|
168
|
+
*
|
|
169
|
+
* Called once per decision with the same info `onDecision` receives, as the
|
|
170
|
+
* wrapper returns. It does not wait for it before returning. Return a
|
|
171
|
+
* promise to answer once you know; its answer is the decision's one
|
|
172
|
+
* report, whenever it settles. A decision the hook fails to answer stays
|
|
173
|
+
* without an outcome. A decision a retry or a later turn's `supersedes`
|
|
174
|
+
* replaced is recorded like any other, from the hook's answer (on a
|
|
175
|
+
* retried hold nothing was shown, so `null`, which is `discarded`); the API
|
|
176
|
+
* adds its own `superseded` event when it applies the retry or the list.
|
|
177
|
+
* On a turn you retry, settle the hook after the retry's gate call: the
|
|
178
|
+
* stopped turn's answer (nothing, or the hold text after a refused retry)
|
|
179
|
+
* is known only once the retry is accepted or refused, and the SDK cannot
|
|
180
|
+
* order it for you.
|
|
181
|
+
*
|
|
182
|
+
* Never called with `reportOutcome: false`, for a reply with no decision
|
|
183
|
+
* id (the API unreachable, or the channel off), for a replayed decision
|
|
184
|
+
* whose outcome already has a state on record, or for a run whose own
|
|
185
|
+
* `config.signal` fired while the gate was deciding (recorded as
|
|
186
|
+
* `discarded`, `cancelled`). A throw, a rejection or any other value goes
|
|
187
|
+
* to `onError`, and nothing is recorded for that decision.
|
|
188
|
+
*/
|
|
189
|
+
deliveredText?: (info: GovernedDecisionInfo) => DeliveredText | Promise<DeliveredText>;
|
|
68
190
|
/**
|
|
69
191
|
* What to do when the wrapped runnable returns something this adapter cannot
|
|
70
192
|
* read as text: a structured output, a dict, a list.
|
|
@@ -80,23 +202,37 @@ interface GovernedOptions extends GovernLangChainOptions {
|
|
|
80
202
|
* error.
|
|
81
203
|
*/
|
|
82
204
|
ungatedOutput?: "error" | "pass";
|
|
83
|
-
onDecision?: (info:
|
|
84
|
-
reply: GuardResult;
|
|
85
|
-
conversationId: string;
|
|
86
|
-
turnId: string;
|
|
87
|
-
}) => void;
|
|
205
|
+
onDecision?: (info: GovernedDecisionInfo) => void;
|
|
88
206
|
}
|
|
89
207
|
interface GovernCallbackHandlerOptions extends GovernLangChainOptions {
|
|
90
208
|
/**
|
|
91
|
-
*
|
|
92
|
-
* cannot know what the chain delivered, so leave outcome
|
|
93
|
-
* code that sends the message
|
|
209
|
+
* Record every recorded decision's generation as `released`. Default
|
|
210
|
+
* false: a callback cannot know what the chain delivered, so leave outcome
|
|
211
|
+
* reporting to the code that sends the message, or set `deliveredText`.
|
|
94
212
|
*/
|
|
95
213
|
reportOutcome?: boolean;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
214
|
+
/**
|
|
215
|
+
* What actually went out, for the outcome report when `reportOutcome` is
|
|
216
|
+
* true. Without it the handler records the generation's text as
|
|
217
|
+
* `released`. With it, the handler asks: return the text you wrote to the
|
|
218
|
+
* channel (hashed locally; only the hash is sent), `{ text, deliveredAt?,
|
|
219
|
+
* deliveryRef? }` to say when it went out and under which reference, or
|
|
220
|
+
* `null` when nothing went out. Text is recorded as `released`; `null` and
|
|
221
|
+
* empty text as `discarded` (`nothing_delivered`).
|
|
222
|
+
*
|
|
223
|
+
* Called once per recorded decision with the same info `onDecision`
|
|
224
|
+
* receives, after the decision is recorded. The handler does not wait for
|
|
225
|
+
* it: LangChain runs callbacks one at a time, and a hook that waits for the
|
|
226
|
+
* chain's own delivery would hold every callback behind it. Return a
|
|
227
|
+
* promise to answer once you know.
|
|
228
|
+
*
|
|
229
|
+
* Never called with `reportOutcome` off, for a decision with no id, or for
|
|
230
|
+
* a replayed decision whose outcome already has a state on record. A
|
|
231
|
+
* throw, a rejection or any other value goes to `onError`, and nothing is
|
|
232
|
+
* recorded for that decision.
|
|
233
|
+
*/
|
|
234
|
+
deliveredText?: (info: GovernCallbackDecisionInfo) => DeliveredText | Promise<DeliveredText>;
|
|
235
|
+
onDecision?: (info: GovernCallbackDecisionInfo) => void;
|
|
100
236
|
}
|
|
101
237
|
/**
|
|
102
238
|
* What `governed` returns for a runnable that returns `T`.
|
|
@@ -107,7 +243,7 @@ interface GovernCallbackHandlerOptions extends GovernLangChainOptions {
|
|
|
107
243
|
* plus the decision under
|
|
108
244
|
* `response_metadata.anybio_govern`. The gate has one message class it can
|
|
109
245
|
* construct, so wrapping a runnable whose output is some other `BaseMessage`
|
|
110
|
-
* subclass widens the type by exactly that much
|
|
246
|
+
* subclass widens the type by exactly that much, and does not pretend the
|
|
111
247
|
* subclass survives the substitution.
|
|
112
248
|
*/
|
|
113
249
|
type GovernedOutput<T> = T extends string ? string : T | AIMessage;
|
|
@@ -135,6 +271,7 @@ declare class GovernCallbackHandler extends BaseCallbackHandler {
|
|
|
135
271
|
name: string;
|
|
136
272
|
private readonly options;
|
|
137
273
|
private readonly govern;
|
|
274
|
+
private readonly reportFailure;
|
|
138
275
|
/** The model each run named at its start, until the run ends. Bounded, for runs that never end. */
|
|
139
276
|
private readonly runModels;
|
|
140
277
|
constructor(options: GovernCallbackHandlerOptions);
|
|
@@ -145,4 +282,4 @@ declare class GovernCallbackHandler extends BaseCallbackHandler {
|
|
|
145
282
|
private rememberModel;
|
|
146
283
|
}
|
|
147
284
|
|
|
148
|
-
export { CONFIGURABLE_KEY, GovernCallbackHandler, type GovernCallbackHandlerOptions, type GovernConfigurable, type GovernLangChainOptions, type GovernedOptions, type GovernedOutput, RESPONSE_METADATA_KEY, governed };
|
|
285
|
+
export { CONFIGURABLE_KEY, DeliveredText, type GovernCallbackDecisionInfo, GovernCallbackHandler, type GovernCallbackHandlerOptions, type GovernConfigurable, type GovernLangChainOptions, type GovernedDecisionInfo, type GovernedOptions, type GovernedOutput, RESPONSE_METADATA_KEY, governed };
|
package/dist/langchain.d.ts
CHANGED
|
@@ -3,7 +3,9 @@ import { BaseMessage, AIMessage } from '@langchain/core/messages';
|
|
|
3
3
|
import { Serialized } from '@langchain/core/load/serializable';
|
|
4
4
|
import { LLMResult } from '@langchain/core/outputs';
|
|
5
5
|
import { RunnableConfig, Runnable } from '@langchain/core/runnables';
|
|
6
|
-
import {
|
|
6
|
+
import { D as DeliveredText } from './adapters-DBLr_Frn.js';
|
|
7
|
+
export { a as DeliveredMessage } from './adapters-DBLr_Frn.js';
|
|
8
|
+
import { f as Decision, G as Govern, o as GovernOptions, b as Channel, a as Audience, J as InboundMessage, q as GuardResult } from './client-BeyMec7E.js';
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
11
|
* `@anybio/govern/langchain`: a callback handler that records, and a runnable
|
|
@@ -25,17 +27,63 @@ import { G as Govern, p as GovernOptions, c as Channel, b as Audience, K as Inbo
|
|
|
25
27
|
* `modelVersion` is the `modelVersion` option, else the model the provider
|
|
26
28
|
* reported on the response (`response_metadata.model_name`, or `model`), when
|
|
27
29
|
* it reports one.
|
|
30
|
+
*
|
|
31
|
+
* Two signals, kept apart on purpose. The run's own `config.signal` is
|
|
32
|
+
* LangChain's cancel: it is never passed to the gate, so it never becomes a
|
|
33
|
+
* hold. A run cancelled before `governed`'s gate call is not gated, and one
|
|
34
|
+
* cancelled while the gate decides is recorded as `discarded`, since the
|
|
35
|
+
* caller's `invoke` has ended and nothing reached the patient.
|
|
36
|
+
* `configurable.anybioGovern.signal` bounds only `governed`'s gate call, and
|
|
37
|
+
* its abort is an answer: the rule for an unreachable API decides what is
|
|
38
|
+
* returned.
|
|
28
39
|
*/
|
|
29
40
|
|
|
30
41
|
/** The key under `config.configurable` the adapter reads per-call identifiers from. */
|
|
31
42
|
declare const CONFIGURABLE_KEY = "anybioGovern";
|
|
32
43
|
/** The key under `AIMessage.response_metadata` the wrapper writes the decision to. */
|
|
33
44
|
declare const RESPONSE_METADATA_KEY = "anybio_govern";
|
|
45
|
+
/** What a caller may pass as `config.configurable.anybioGovern` on one call. */
|
|
34
46
|
interface GovernConfigurable {
|
|
35
47
|
conversationId?: string;
|
|
36
48
|
turnId?: string;
|
|
49
|
+
/** The decision this call's turn retries. See `EvaluateInput.retryOf`. `null` is no retry. */
|
|
50
|
+
retryOf?: string | null;
|
|
51
|
+
/** Your stable reference for the patient turn. See `EvaluateInput.turnRef`. `null` is none. */
|
|
52
|
+
turnRef?: string | null;
|
|
53
|
+
/** The decisions this call's turn replaces. See `EvaluateInput.supersedes`. `null` is none. */
|
|
54
|
+
supersedes?: readonly string[] | null;
|
|
37
55
|
patientRef?: string;
|
|
38
56
|
metadata?: Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* Bounds `governed`'s gate call for this call.
|
|
59
|
+
*
|
|
60
|
+
* When it aborts, before or during the gate call, the call ends (no retry
|
|
61
|
+
* follows) and the rule for an unreachable API applies: in enforce the
|
|
62
|
+
* output is held, with the hold message where `showHoldText` is true and
|
|
63
|
+
* empty text where it is false; in observe the output comes back flagged
|
|
64
|
+
* `degraded`. The reply carries `reasons: ["gate_unavailable"]` and no
|
|
65
|
+
* decision id, and the client emits its `degraded` event. It travels to
|
|
66
|
+
* `guard` as `gateSignal`, which answers this way; the call's own `signal`
|
|
67
|
+
* would be rethrown instead, and the wrapper owns the reply.
|
|
68
|
+
*
|
|
69
|
+
* Not the run's `config.signal`, which is LangChain's cancel and is never
|
|
70
|
+
* passed to the gate: wiring it in would turn a cancel into a hold.
|
|
71
|
+
* `GovernCallbackHandler` sees no per-call config and makes no gate call,
|
|
72
|
+
* so it takes no signal.
|
|
73
|
+
*
|
|
74
|
+
* A signal or a function that returns one. A signal's clock starts when
|
|
75
|
+
* you create it, so one made before `invoke` bounds the wrapped runnable
|
|
76
|
+
* and the gate together, and it belongs to one call: bound with
|
|
77
|
+
* `withConfig`, the same signal is shared by every call after it, and once
|
|
78
|
+
* it fires every one of them holds. The function is called when the gate
|
|
79
|
+
* call starts, once per call, so `() => AbortSignal.timeout(ms)` is a
|
|
80
|
+
* fresh bound measured from the gate call, safe to bind once. `null`, or
|
|
81
|
+
* a function returning `null` or `undefined`, is no bound. A value that is
|
|
82
|
+
* neither a signal nor a function is refused with `GovernConfigError`
|
|
83
|
+
* before the wrapped runnable runs; a function that returns something
|
|
84
|
+
* else is refused when the gate call starts.
|
|
85
|
+
*/
|
|
86
|
+
signal?: AbortSignal | null | ((config: RunnableConfig | undefined) => AbortSignal | null | undefined);
|
|
39
87
|
}
|
|
40
88
|
type Resolvable<T> = T | ((config: RunnableConfig | undefined) => T | undefined);
|
|
41
89
|
interface GovernLangChainOptions {
|
|
@@ -57,14 +105,88 @@ interface GovernLangChainOptions {
|
|
|
57
105
|
model?: Resolvable<string>;
|
|
58
106
|
/** The model version recorded on each decision. Default: the model the provider reported on the response, when it reports one. */
|
|
59
107
|
modelVersion?: Resolvable<string>;
|
|
108
|
+
/**
|
|
109
|
+
* The patient's inbound message, for the red-flag screen. Requires
|
|
110
|
+
* `patientRef` (the option, or `configurable.anybioGovern.patientRef`).
|
|
111
|
+
*/
|
|
60
112
|
inbound?: (config: RunnableConfig | undefined) => InboundMessage | undefined;
|
|
61
113
|
holdMessage?: string;
|
|
62
|
-
/**
|
|
114
|
+
/**
|
|
115
|
+
* Called when recording an outcome fails, and with a failing
|
|
116
|
+
* `deliveredText`. Nothing retries a failed report after the transport's
|
|
117
|
+
* own retries. A report that did not land arrives as a
|
|
118
|
+
* `GovernReportError`: the decision id and the event exactly as sent
|
|
119
|
+
* (`kind`, `at`, `contentHash`, `deliveryRef`, `reason`), to send again
|
|
120
|
+
* with `govern.reportOutcome`, and the failure on `cause` (a
|
|
121
|
+
* `GovernConflictError` when a different event of that kind is already on
|
|
122
|
+
* record). A hook that throws or answers something else arrives as itself.
|
|
123
|
+
*/
|
|
63
124
|
onError?: (error: unknown) => void;
|
|
64
125
|
}
|
|
126
|
+
/** What `governed` hands `onDecision` and `deliveredText`. */
|
|
127
|
+
interface GovernedDecisionInfo {
|
|
128
|
+
reply: GuardResult;
|
|
129
|
+
conversationId: string;
|
|
130
|
+
turnId: string;
|
|
131
|
+
}
|
|
132
|
+
/** What `GovernCallbackHandler` hands `onDecision` and `deliveredText`. */
|
|
133
|
+
interface GovernCallbackDecisionInfo {
|
|
134
|
+
decision: Decision;
|
|
135
|
+
runId: string;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The call's context (conversation id, turn id, `retryOf`, patient
|
|
139
|
+
* reference, metadata, inbound and the per-call bound) is resolved before
|
|
140
|
+
* the wrapped runnable runs, as `withGovern` resolves it before the model is
|
|
141
|
+
* called. A run that could not be gated fails with `GovernConfigError`
|
|
142
|
+
* before the model runs, even one that would never reach the gate (an empty
|
|
143
|
+
* output, or `ungatedOutput: "pass"`), and a resolver function you pass sees
|
|
144
|
+
* the state before the chain. Only the model version the provider reports is
|
|
145
|
+
* read from the output, afterwards.
|
|
146
|
+
*/
|
|
65
147
|
interface GovernedOptions extends GovernLangChainOptions {
|
|
66
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* Record the write-time outcome once per decision, as the wrapper returns:
|
|
150
|
+
* `released` with the hash of the text it returned, or of what
|
|
151
|
+
* `deliveredText` says went out; `discarded` when nothing went out.
|
|
152
|
+
* Default true.
|
|
153
|
+
*
|
|
154
|
+
* With false the wrapper reports nothing, and the outcome is yours alone
|
|
155
|
+
* to record with `govern.reportOutcome`. A decision you never report stays
|
|
156
|
+
* without an outcome.
|
|
157
|
+
*/
|
|
67
158
|
reportOutcome?: boolean;
|
|
159
|
+
/**
|
|
160
|
+
* What actually went out, for the outcome report. Without it the wrapper
|
|
161
|
+
* records `released` with the text it returned, or `discarded`
|
|
162
|
+
* (`nothing_to_send`) when it returned empty text. With it, the wrapper
|
|
163
|
+
* asks: return the text you wrote to the channel (it is hashed locally;
|
|
164
|
+
* only the hash is sent), `{ text, deliveredAt?, deliveryRef? }` to say
|
|
165
|
+
* when it went out and under which reference, or `null` when nothing went
|
|
166
|
+
* out. Text is recorded as `released`, a shown hold message included;
|
|
167
|
+
* `null` and empty text as `discarded` (`nothing_delivered`).
|
|
168
|
+
*
|
|
169
|
+
* Called once per decision with the same info `onDecision` receives, as the
|
|
170
|
+
* wrapper returns. It does not wait for it before returning. Return a
|
|
171
|
+
* promise to answer once you know; its answer is the decision's one
|
|
172
|
+
* report, whenever it settles. A decision the hook fails to answer stays
|
|
173
|
+
* without an outcome. A decision a retry or a later turn's `supersedes`
|
|
174
|
+
* replaced is recorded like any other, from the hook's answer (on a
|
|
175
|
+
* retried hold nothing was shown, so `null`, which is `discarded`); the API
|
|
176
|
+
* adds its own `superseded` event when it applies the retry or the list.
|
|
177
|
+
* On a turn you retry, settle the hook after the retry's gate call: the
|
|
178
|
+
* stopped turn's answer (nothing, or the hold text after a refused retry)
|
|
179
|
+
* is known only once the retry is accepted or refused, and the SDK cannot
|
|
180
|
+
* order it for you.
|
|
181
|
+
*
|
|
182
|
+
* Never called with `reportOutcome: false`, for a reply with no decision
|
|
183
|
+
* id (the API unreachable, or the channel off), for a replayed decision
|
|
184
|
+
* whose outcome already has a state on record, or for a run whose own
|
|
185
|
+
* `config.signal` fired while the gate was deciding (recorded as
|
|
186
|
+
* `discarded`, `cancelled`). A throw, a rejection or any other value goes
|
|
187
|
+
* to `onError`, and nothing is recorded for that decision.
|
|
188
|
+
*/
|
|
189
|
+
deliveredText?: (info: GovernedDecisionInfo) => DeliveredText | Promise<DeliveredText>;
|
|
68
190
|
/**
|
|
69
191
|
* What to do when the wrapped runnable returns something this adapter cannot
|
|
70
192
|
* read as text: a structured output, a dict, a list.
|
|
@@ -80,23 +202,37 @@ interface GovernedOptions extends GovernLangChainOptions {
|
|
|
80
202
|
* error.
|
|
81
203
|
*/
|
|
82
204
|
ungatedOutput?: "error" | "pass";
|
|
83
|
-
onDecision?: (info:
|
|
84
|
-
reply: GuardResult;
|
|
85
|
-
conversationId: string;
|
|
86
|
-
turnId: string;
|
|
87
|
-
}) => void;
|
|
205
|
+
onDecision?: (info: GovernedDecisionInfo) => void;
|
|
88
206
|
}
|
|
89
207
|
interface GovernCallbackHandlerOptions extends GovernLangChainOptions {
|
|
90
208
|
/**
|
|
91
|
-
*
|
|
92
|
-
* cannot know what the chain delivered, so leave outcome
|
|
93
|
-
* code that sends the message
|
|
209
|
+
* Record every recorded decision's generation as `released`. Default
|
|
210
|
+
* false: a callback cannot know what the chain delivered, so leave outcome
|
|
211
|
+
* reporting to the code that sends the message, or set `deliveredText`.
|
|
94
212
|
*/
|
|
95
213
|
reportOutcome?: boolean;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
214
|
+
/**
|
|
215
|
+
* What actually went out, for the outcome report when `reportOutcome` is
|
|
216
|
+
* true. Without it the handler records the generation's text as
|
|
217
|
+
* `released`. With it, the handler asks: return the text you wrote to the
|
|
218
|
+
* channel (hashed locally; only the hash is sent), `{ text, deliveredAt?,
|
|
219
|
+
* deliveryRef? }` to say when it went out and under which reference, or
|
|
220
|
+
* `null` when nothing went out. Text is recorded as `released`; `null` and
|
|
221
|
+
* empty text as `discarded` (`nothing_delivered`).
|
|
222
|
+
*
|
|
223
|
+
* Called once per recorded decision with the same info `onDecision`
|
|
224
|
+
* receives, after the decision is recorded. The handler does not wait for
|
|
225
|
+
* it: LangChain runs callbacks one at a time, and a hook that waits for the
|
|
226
|
+
* chain's own delivery would hold every callback behind it. Return a
|
|
227
|
+
* promise to answer once you know.
|
|
228
|
+
*
|
|
229
|
+
* Never called with `reportOutcome` off, for a decision with no id, or for
|
|
230
|
+
* a replayed decision whose outcome already has a state on record. A
|
|
231
|
+
* throw, a rejection or any other value goes to `onError`, and nothing is
|
|
232
|
+
* recorded for that decision.
|
|
233
|
+
*/
|
|
234
|
+
deliveredText?: (info: GovernCallbackDecisionInfo) => DeliveredText | Promise<DeliveredText>;
|
|
235
|
+
onDecision?: (info: GovernCallbackDecisionInfo) => void;
|
|
100
236
|
}
|
|
101
237
|
/**
|
|
102
238
|
* What `governed` returns for a runnable that returns `T`.
|
|
@@ -107,7 +243,7 @@ interface GovernCallbackHandlerOptions extends GovernLangChainOptions {
|
|
|
107
243
|
* plus the decision under
|
|
108
244
|
* `response_metadata.anybio_govern`. The gate has one message class it can
|
|
109
245
|
* construct, so wrapping a runnable whose output is some other `BaseMessage`
|
|
110
|
-
* subclass widens the type by exactly that much
|
|
246
|
+
* subclass widens the type by exactly that much, and does not pretend the
|
|
111
247
|
* subclass survives the substitution.
|
|
112
248
|
*/
|
|
113
249
|
type GovernedOutput<T> = T extends string ? string : T | AIMessage;
|
|
@@ -135,6 +271,7 @@ declare class GovernCallbackHandler extends BaseCallbackHandler {
|
|
|
135
271
|
name: string;
|
|
136
272
|
private readonly options;
|
|
137
273
|
private readonly govern;
|
|
274
|
+
private readonly reportFailure;
|
|
138
275
|
/** The model each run named at its start, until the run ends. Bounded, for runs that never end. */
|
|
139
276
|
private readonly runModels;
|
|
140
277
|
constructor(options: GovernCallbackHandlerOptions);
|
|
@@ -145,4 +282,4 @@ declare class GovernCallbackHandler extends BaseCallbackHandler {
|
|
|
145
282
|
private rememberModel;
|
|
146
283
|
}
|
|
147
284
|
|
|
148
|
-
export { CONFIGURABLE_KEY, GovernCallbackHandler, type GovernCallbackHandlerOptions, type GovernConfigurable, type GovernLangChainOptions, type GovernedOptions, type GovernedOutput, RESPONSE_METADATA_KEY, governed };
|
|
285
|
+
export { CONFIGURABLE_KEY, DeliveredText, type GovernCallbackDecisionInfo, GovernCallbackHandler, type GovernCallbackHandlerOptions, type GovernConfigurable, type GovernLangChainOptions, type GovernedDecisionInfo, type GovernedOptions, type GovernedOutput, RESPONSE_METADATA_KEY, governed };
|