@anybio/govern 0.0.0 → 0.2.2
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 +34 -0
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +374 -0
- package/dist/cli.js +1891 -0
- package/dist/client-CU46a__g.d.cts +1928 -0
- package/dist/client-CU46a__g.d.ts +1928 -0
- package/dist/index.cjs +1541 -0
- package/dist/index.d.cts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.js +1503 -0
- package/dist/langchain.cjs +1700 -0
- package/dist/langchain.d.cts +148 -0
- package/dist/langchain.d.ts +148 -0
- package/dist/langchain.js +1695 -0
- package/dist/vercel-ai.cjs +1725 -0
- package/dist/vercel-ai.d.cts +94 -0
- package/dist/vercel-ai.d.ts +94 -0
- package/dist/vercel-ai.js +1721 -0
- package/package.json +127 -5
- package/spec/README.md +17 -0
- package/spec/openapi.json +3967 -0
- package/README +0 -3
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { LanguageModelMiddleware } from 'ai';
|
|
2
|
+
import { r as GuardResult, c as Channel, b as Audience, G as Govern, p as GovernOptions, K as InboundMessage } from './client-CU46a__g.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@anybio/govern/vercel-ai`: a language-model middleware for the Vercel AI
|
|
6
|
+
* SDK (`ai` v7, provider specification v4).
|
|
7
|
+
*
|
|
8
|
+
* const model = wrapLanguageModel({ model: openai("gpt-4o"), middleware: withGovern({ govern, channel: "web_chat", audience: "patient" }) });
|
|
9
|
+
*
|
|
10
|
+
* `wrapGenerate` evaluates the generated text before it is returned.
|
|
11
|
+
* `wrapStream` depends on the audience:
|
|
12
|
+
*
|
|
13
|
+
* - patient: buffer and release. Every part of the source stream is held,
|
|
14
|
+
* the full text is evaluated, and then either the original parts are
|
|
15
|
+
* replayed (allow) or the draft is replaced (hold, block, or the API
|
|
16
|
+
* unreachable in enforce). No token reaches the caller before the decision.
|
|
17
|
+
* - clinician: pass through with a trailing evaluation. Tokens stream as
|
|
18
|
+
* they arrive; after the stream ends the full text is evaluated and the
|
|
19
|
+
* outcome recorded with what actually went out.
|
|
20
|
+
*
|
|
21
|
+
* What replaces a held draft follows `reply.showHoldText`, the same rule as
|
|
22
|
+
* `guard`: on an in-session channel (`web_chat`, `app_message`) a single text
|
|
23
|
+
* block with the hold message; on an outbound channel (`sms`, `voice`,
|
|
24
|
+
* `email`, or one this version does not know) the hold message only when a
|
|
25
|
+
* holding line is owed, and otherwise no text at all, so a caller that sends
|
|
26
|
+
* whatever the model returns sends nothing.
|
|
27
|
+
*
|
|
28
|
+
* Per-call identifiers travel in `providerOptions.anybioGovern`:
|
|
29
|
+
*
|
|
30
|
+
* streamText({ model, messages, providerOptions: { anybioGovern: { conversationId: chatId, turnId: messageId } } });
|
|
31
|
+
*
|
|
32
|
+
* Every evaluation names the model: `model` is the wrapped model's `modelId`
|
|
33
|
+
* (what you asked for, for example `gpt-4o`), and `modelVersion` is the model
|
|
34
|
+
* id the provider reported on the response (the resolved snapshot, for
|
|
35
|
+
* example `gpt-4o-2024-08-06`) when it reports one. Either can be overridden
|
|
36
|
+
* in the options.
|
|
37
|
+
*
|
|
38
|
+
* The middleware imports only types from `ai`; there is no runtime coupling.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type WrapGenerate = NonNullable<LanguageModelMiddleware["wrapGenerate"]>;
|
|
42
|
+
type WrapArgs = Parameters<WrapGenerate>[0];
|
|
43
|
+
type LanguageModelCallOptions = WrapArgs["params"];
|
|
44
|
+
type StreamResult = Awaited<ReturnType<WrapArgs["doStream"]>>;
|
|
45
|
+
type StreamPart = StreamResult["stream"] extends ReadableStream<infer P> ? P : never;
|
|
46
|
+
/** The key under `providerOptions` the middleware reads per-call identifiers from. */
|
|
47
|
+
declare const PROVIDER_OPTIONS_KEY = "anybioGovern";
|
|
48
|
+
/** The key under `providerMetadata` the middleware writes the decision to. */
|
|
49
|
+
declare const PROVIDER_METADATA_KEY = "anybioGovern";
|
|
50
|
+
/** What a caller may pass as `providerOptions.anybioGovern` on one call. */
|
|
51
|
+
interface GovernCallOptions {
|
|
52
|
+
conversationId?: string;
|
|
53
|
+
turnId?: string;
|
|
54
|
+
patientRef?: string;
|
|
55
|
+
metadata?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
type Resolvable<T> = T | ((params: LanguageModelCallOptions) => T | undefined);
|
|
58
|
+
interface WithGovernOptions {
|
|
59
|
+
/** A `Govern` client, or the options to construct one. */
|
|
60
|
+
govern: Govern | GovernOptions;
|
|
61
|
+
channel: Channel;
|
|
62
|
+
audience: Audience;
|
|
63
|
+
/** Your thread id. A string, a function of the call params, or per call via `providerOptions.anybioGovern.conversationId`. Required by one of those routes. */
|
|
64
|
+
conversationId?: Resolvable<string>;
|
|
65
|
+
/** Your message id. Default: `providerOptions.anybioGovern.turnId`, else a random id per call. */
|
|
66
|
+
turnId?: Resolvable<string>;
|
|
67
|
+
patientRef?: Resolvable<string>;
|
|
68
|
+
metadata?: Resolvable<Record<string, unknown>>;
|
|
69
|
+
/** The model name recorded on each decision. Default: the wrapped model's `modelId`. */
|
|
70
|
+
model?: Resolvable<string>;
|
|
71
|
+
/** The model version recorded on each decision. Default: the model id the provider reported on the response, when it reports one. */
|
|
72
|
+
modelVersion?: Resolvable<string>;
|
|
73
|
+
/** The patient's inbound message, for the red-flag screen. */
|
|
74
|
+
inbound?: (params: LanguageModelCallOptions) => InboundMessage | undefined;
|
|
75
|
+
/** Overrides the client's hold message. */
|
|
76
|
+
holdMessage?: string;
|
|
77
|
+
/** Report the outcome as delivered with the text the middleware let through. Default true. */
|
|
78
|
+
reportOutcome?: boolean;
|
|
79
|
+
/** Called with every decision, after the reply is determined. */
|
|
80
|
+
onDecision?: (info: GovernDecisionInfo) => void;
|
|
81
|
+
/** Called when reporting an outcome fails. Delivery is unaffected. */
|
|
82
|
+
onError?: (error: unknown) => void;
|
|
83
|
+
}
|
|
84
|
+
interface GovernDecisionInfo {
|
|
85
|
+
reply: GuardResult;
|
|
86
|
+
conversationId: string;
|
|
87
|
+
turnId: string;
|
|
88
|
+
channel: Channel;
|
|
89
|
+
audience: Audience;
|
|
90
|
+
streamed: boolean;
|
|
91
|
+
}
|
|
92
|
+
declare function withGovern(options: WithGovernOptions): LanguageModelMiddleware;
|
|
93
|
+
|
|
94
|
+
export { type GovernCallOptions, type GovernDecisionInfo, type LanguageModelCallOptions, PROVIDER_METADATA_KEY, PROVIDER_OPTIONS_KEY, type StreamPart, type WithGovernOptions, withGovern };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { LanguageModelMiddleware } from 'ai';
|
|
2
|
+
import { r as GuardResult, c as Channel, b as Audience, G as Govern, p as GovernOptions, K as InboundMessage } from './client-CU46a__g.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@anybio/govern/vercel-ai`: a language-model middleware for the Vercel AI
|
|
6
|
+
* SDK (`ai` v7, provider specification v4).
|
|
7
|
+
*
|
|
8
|
+
* const model = wrapLanguageModel({ model: openai("gpt-4o"), middleware: withGovern({ govern, channel: "web_chat", audience: "patient" }) });
|
|
9
|
+
*
|
|
10
|
+
* `wrapGenerate` evaluates the generated text before it is returned.
|
|
11
|
+
* `wrapStream` depends on the audience:
|
|
12
|
+
*
|
|
13
|
+
* - patient: buffer and release. Every part of the source stream is held,
|
|
14
|
+
* the full text is evaluated, and then either the original parts are
|
|
15
|
+
* replayed (allow) or the draft is replaced (hold, block, or the API
|
|
16
|
+
* unreachable in enforce). No token reaches the caller before the decision.
|
|
17
|
+
* - clinician: pass through with a trailing evaluation. Tokens stream as
|
|
18
|
+
* they arrive; after the stream ends the full text is evaluated and the
|
|
19
|
+
* outcome recorded with what actually went out.
|
|
20
|
+
*
|
|
21
|
+
* What replaces a held draft follows `reply.showHoldText`, the same rule as
|
|
22
|
+
* `guard`: on an in-session channel (`web_chat`, `app_message`) a single text
|
|
23
|
+
* block with the hold message; on an outbound channel (`sms`, `voice`,
|
|
24
|
+
* `email`, or one this version does not know) the hold message only when a
|
|
25
|
+
* holding line is owed, and otherwise no text at all, so a caller that sends
|
|
26
|
+
* whatever the model returns sends nothing.
|
|
27
|
+
*
|
|
28
|
+
* Per-call identifiers travel in `providerOptions.anybioGovern`:
|
|
29
|
+
*
|
|
30
|
+
* streamText({ model, messages, providerOptions: { anybioGovern: { conversationId: chatId, turnId: messageId } } });
|
|
31
|
+
*
|
|
32
|
+
* Every evaluation names the model: `model` is the wrapped model's `modelId`
|
|
33
|
+
* (what you asked for, for example `gpt-4o`), and `modelVersion` is the model
|
|
34
|
+
* id the provider reported on the response (the resolved snapshot, for
|
|
35
|
+
* example `gpt-4o-2024-08-06`) when it reports one. Either can be overridden
|
|
36
|
+
* in the options.
|
|
37
|
+
*
|
|
38
|
+
* The middleware imports only types from `ai`; there is no runtime coupling.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
type WrapGenerate = NonNullable<LanguageModelMiddleware["wrapGenerate"]>;
|
|
42
|
+
type WrapArgs = Parameters<WrapGenerate>[0];
|
|
43
|
+
type LanguageModelCallOptions = WrapArgs["params"];
|
|
44
|
+
type StreamResult = Awaited<ReturnType<WrapArgs["doStream"]>>;
|
|
45
|
+
type StreamPart = StreamResult["stream"] extends ReadableStream<infer P> ? P : never;
|
|
46
|
+
/** The key under `providerOptions` the middleware reads per-call identifiers from. */
|
|
47
|
+
declare const PROVIDER_OPTIONS_KEY = "anybioGovern";
|
|
48
|
+
/** The key under `providerMetadata` the middleware writes the decision to. */
|
|
49
|
+
declare const PROVIDER_METADATA_KEY = "anybioGovern";
|
|
50
|
+
/** What a caller may pass as `providerOptions.anybioGovern` on one call. */
|
|
51
|
+
interface GovernCallOptions {
|
|
52
|
+
conversationId?: string;
|
|
53
|
+
turnId?: string;
|
|
54
|
+
patientRef?: string;
|
|
55
|
+
metadata?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
type Resolvable<T> = T | ((params: LanguageModelCallOptions) => T | undefined);
|
|
58
|
+
interface WithGovernOptions {
|
|
59
|
+
/** A `Govern` client, or the options to construct one. */
|
|
60
|
+
govern: Govern | GovernOptions;
|
|
61
|
+
channel: Channel;
|
|
62
|
+
audience: Audience;
|
|
63
|
+
/** Your thread id. A string, a function of the call params, or per call via `providerOptions.anybioGovern.conversationId`. Required by one of those routes. */
|
|
64
|
+
conversationId?: Resolvable<string>;
|
|
65
|
+
/** Your message id. Default: `providerOptions.anybioGovern.turnId`, else a random id per call. */
|
|
66
|
+
turnId?: Resolvable<string>;
|
|
67
|
+
patientRef?: Resolvable<string>;
|
|
68
|
+
metadata?: Resolvable<Record<string, unknown>>;
|
|
69
|
+
/** The model name recorded on each decision. Default: the wrapped model's `modelId`. */
|
|
70
|
+
model?: Resolvable<string>;
|
|
71
|
+
/** The model version recorded on each decision. Default: the model id the provider reported on the response, when it reports one. */
|
|
72
|
+
modelVersion?: Resolvable<string>;
|
|
73
|
+
/** The patient's inbound message, for the red-flag screen. */
|
|
74
|
+
inbound?: (params: LanguageModelCallOptions) => InboundMessage | undefined;
|
|
75
|
+
/** Overrides the client's hold message. */
|
|
76
|
+
holdMessage?: string;
|
|
77
|
+
/** Report the outcome as delivered with the text the middleware let through. Default true. */
|
|
78
|
+
reportOutcome?: boolean;
|
|
79
|
+
/** Called with every decision, after the reply is determined. */
|
|
80
|
+
onDecision?: (info: GovernDecisionInfo) => void;
|
|
81
|
+
/** Called when reporting an outcome fails. Delivery is unaffected. */
|
|
82
|
+
onError?: (error: unknown) => void;
|
|
83
|
+
}
|
|
84
|
+
interface GovernDecisionInfo {
|
|
85
|
+
reply: GuardResult;
|
|
86
|
+
conversationId: string;
|
|
87
|
+
turnId: string;
|
|
88
|
+
channel: Channel;
|
|
89
|
+
audience: Audience;
|
|
90
|
+
streamed: boolean;
|
|
91
|
+
}
|
|
92
|
+
declare function withGovern(options: WithGovernOptions): LanguageModelMiddleware;
|
|
93
|
+
|
|
94
|
+
export { type GovernCallOptions, type GovernDecisionInfo, type LanguageModelCallOptions, PROVIDER_METADATA_KEY, PROVIDER_OPTIONS_KEY, type StreamPart, type WithGovernOptions, withGovern };
|