@anvia/core 0.11.2 → 0.12.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/dist/agent/index.d.ts +14 -11
- package/dist/agent/index.js +11 -10
- package/dist/{agent-Bq2KtSaw.d.ts → agent-BWF8_O8k.d.ts} +17 -8
- package/dist/audio-generation/index.d.ts +3 -3
- package/dist/{chunk-N6REB3DH.js → chunk-4AJJIIAQ.js} +9 -7
- package/dist/chunk-4AJJIIAQ.js.map +1 -0
- package/dist/{chunk-WJO6NVE2.js → chunk-5LFGEMIW.js} +1 -1
- package/dist/{chunk-WJO6NVE2.js.map → chunk-5LFGEMIW.js.map} +1 -1
- package/dist/{chunk-DOS77JZC.js → chunk-JNO4AR7Q.js} +2 -2
- package/dist/{chunk-SISUMRUX.js → chunk-KTTIPWE7.js} +467 -111
- package/dist/chunk-KTTIPWE7.js.map +1 -0
- package/dist/{chunk-R75SGHCI.js → chunk-MIJUAWHU.js} +6 -6
- package/dist/{chunk-5EDHXYH5.js → chunk-PGFYN5ZX.js} +12 -7
- package/dist/chunk-PGFYN5ZX.js.map +1 -0
- package/dist/{chunk-X3SWV6W6.js → chunk-R7F2JF7J.js} +7 -7
- package/dist/chunk-R7F2JF7J.js.map +1 -0
- package/dist/{chunk-5G3VME2D.js → chunk-SRBW3LD7.js} +2 -2
- package/dist/chunk-SRBW3LD7.js.map +1 -0
- package/dist/{chunk-62RUYGMM.js → chunk-TIH4QM55.js} +12 -3
- package/dist/chunk-TIH4QM55.js.map +1 -0
- package/dist/chunk-YRERPIA5.js +451 -0
- package/dist/chunk-YRERPIA5.js.map +1 -0
- package/dist/{chunk-V3WTT7TE.js → chunk-ZGIE73BH.js} +2 -2
- package/dist/completion/index.d.ts +8 -6
- package/dist/completion/index.js +2 -2
- package/dist/{create-completion-BEbtU5pX.d.ts → create-completion-Y6YsYbQB.d.ts} +2 -2
- package/dist/evals/index.d.ts +9 -8
- package/dist/evals/index.js +14 -13
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.d.ts +9 -8
- package/dist/extractor/index.js +12 -11
- package/dist/guardrails/index.d.ts +211 -0
- package/dist/guardrails/index.js +35 -0
- package/dist/guardrails/index.js.map +1 -0
- package/dist/hooks/index.d.ts +3 -3
- package/dist/image-generation/index.d.ts +3 -3
- package/dist/{index-CTNzKidE.d.ts → index-DsWWqP0Q.d.ts} +9 -2
- package/dist/index.d.ts +13 -12
- package/dist/index.js +37 -19
- package/dist/internal/agent.d.ts +9 -8
- package/dist/internal/agent.js +10 -9
- package/dist/loaders/index.d.ts +1 -1
- package/dist/mcp/index.d.ts +5 -4
- package/dist/memory/index.d.ts +3 -3
- package/dist/{middleware-CG7l0rR3.d.ts → middleware-BTR0eRtx.d.ts} +3 -3
- package/dist/model-listing/index.d.ts +2 -1
- package/dist/observability/index.d.ts +4 -3
- package/dist/pipeline/index.d.ts +9 -8
- package/dist/pipeline/index.js +3 -3
- package/dist/request/index.d.ts +9 -8
- package/dist/request/index.js +9 -8
- package/dist/skills/index.d.ts +5 -4
- package/dist/skills/index.js +6 -6
- package/dist/{think-tool-B8Rqw2i_.d.ts → think-tool-Bn3HHu_z.d.ts} +4 -1
- package/dist/tool/index.d.ts +6 -5
- package/dist/tool/index.js +6 -6
- package/dist/{tool-BLypI31e.d.ts → tool-DGELXD2z.d.ts} +4 -1
- package/dist/transcription/index.d.ts +3 -3
- package/dist/{types-KKjICcrA.d.ts → types-9VPKg7ih.d.ts} +1 -1
- package/dist/{types-D-YTJdLj.d.ts → types-Bx_ZtjPi.d.ts} +1 -1
- package/dist/{types-CCxnnJjU.d.ts → types-CGML74sE.d.ts} +1 -1
- package/dist/{types-CNfjQ5rB.d.ts → types-CqG6d7Ce.d.ts} +1 -1
- package/dist/{types-CIwm9p4w.d.ts → types-Dc83Qzch.d.ts} +2 -2
- package/dist/{types-C8loR4il.d.ts → types-q9G7Bvwl.d.ts} +8 -8
- package/dist/ui/index.d.ts +1 -1
- package/dist/ui/index.js +2 -2
- package/dist/vector-store/index.d.ts +5 -4
- package/dist/vector-store/index.js +3 -3
- package/package.json +6 -2
- package/dist/chunk-5EDHXYH5.js.map +0 -1
- package/dist/chunk-5G3VME2D.js.map +0 -1
- package/dist/chunk-62RUYGMM.js.map +0 -1
- package/dist/chunk-N6REB3DH.js.map +0 -1
- package/dist/chunk-SISUMRUX.js.map +0 -1
- package/dist/chunk-X3SWV6W6.js.map +0 -1
- /package/dist/{chunk-DOS77JZC.js.map → chunk-JNO4AR7Q.js.map} +0 -0
- /package/dist/{chunk-R75SGHCI.js.map → chunk-MIJUAWHU.js.map} +0 -0
- /package/dist/{chunk-V3WTT7TE.js.map → chunk-ZGIE73BH.js.map} +0 -0
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
3
|
normalizeAgentId
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-4AJJIIAQ.js";
|
|
5
5
|
import {
|
|
6
6
|
ToolSet
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-R7F2JF7J.js";
|
|
8
8
|
import {
|
|
9
9
|
resolveMemoryOptions
|
|
10
10
|
} from "./chunk-M6RPFHG7.js";
|
|
11
11
|
import {
|
|
12
12
|
toProviderJsonSchema
|
|
13
13
|
} from "./chunk-WQKHFADH.js";
|
|
14
|
+
import {
|
|
15
|
+
appendGuardrailPolicies
|
|
16
|
+
} from "./chunk-YRERPIA5.js";
|
|
14
17
|
|
|
15
18
|
// src/agent/builder.ts
|
|
16
19
|
var AgentBuilder = class {
|
|
@@ -32,6 +35,7 @@ var AgentBuilder = class {
|
|
|
32
35
|
requestHook;
|
|
33
36
|
schema;
|
|
34
37
|
approvalOptions;
|
|
38
|
+
guardrailPolicies = [];
|
|
35
39
|
skillInstructionBlocks = [];
|
|
36
40
|
observerRegistrations = [];
|
|
37
41
|
dynamicContextRegistrations = [];
|
|
@@ -147,6 +151,10 @@ var AgentBuilder = class {
|
|
|
147
151
|
this.approvalOptions = options;
|
|
148
152
|
return this;
|
|
149
153
|
}
|
|
154
|
+
guardrails(policies) {
|
|
155
|
+
this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
150
158
|
memory(store, options = {}) {
|
|
151
159
|
this.memoryRegistration = {
|
|
152
160
|
store,
|
|
@@ -185,6 +193,7 @@ var AgentBuilder = class {
|
|
|
185
193
|
outputSchema: this.schema,
|
|
186
194
|
observers: this.observerRegistrations,
|
|
187
195
|
approvals: this.approvalOptions,
|
|
196
|
+
guardrails: this.guardrailPolicies,
|
|
188
197
|
dynamicContexts: this.dynamicContextRegistrations,
|
|
189
198
|
dynamicTools: this.dynamicToolRegistrations,
|
|
190
199
|
middlewares: this.middlewareRegistrations,
|
|
@@ -203,4 +212,4 @@ var AgentBuilder = class {
|
|
|
203
212
|
export {
|
|
204
213
|
AgentBuilder
|
|
205
214
|
};
|
|
206
|
-
//# sourceMappingURL=chunk-
|
|
215
|
+
//# sourceMappingURL=chunk-TIH4QM55.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent/builder.ts"],"sourcesContent":["import type { CompletionModel, Document, JsonObject, JsonValue, ToolChoice } from \"../completion\";\nimport { appendGuardrailPolicies } from \"../guardrails\";\nimport type { GuardrailPolicy, GuardrailPolicyInput } from \"../guardrails\";\nimport type { PromptHook } from \"../hooks\";\nimport type { McpServer } from \"../mcp\";\nimport { resolveMemoryOptions } from \"../memory/options\";\nimport type { MemoryOptions, MemoryRegistration, MemoryStore } from \"../memory/types\";\nimport type { AgentObserver, AgentObserverRegistration, ObserveOptions } from \"../observability\";\nimport { toProviderJsonSchema, type ZodSchema } from \"../schema/zod-schema\";\nimport type { SkillSet } from \"../skills\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { AgentMiddleware, ToolMiddleware } from \"../tool/middleware\";\nimport type { AnyTool, ToolApprovalsOptions } from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport { Agent } from \"./agent\";\nimport { normalizeAgentId } from \"./ids\";\nimport type {\n AgentEventStore,\n AgentEventStoreOptions,\n AgentEventStoreRegistration,\n DynamicContextOptions,\n DynamicContextRegistration,\n DynamicToolOptions,\n DynamicToolRegistration,\n} from \"./types\";\n\nexport class AgentBuilder<M extends CompletionModel = CompletionModel> {\n private readonly agentId: string;\n private agentName: string | undefined;\n private agentDescription: string | undefined;\n private instructionBlocks: string[] = [];\n private contextDocs: Document[] = [];\n private temp: number | undefined;\n private maxTokenCount: number | undefined;\n private params: JsonValue | undefined;\n private choice: ToolChoice | undefined;\n private turns: number | undefined;\n private requestHook: PromptHook | undefined;\n private schema: JsonObject | undefined;\n private approvalOptions: ToolApprovalsOptions | undefined;\n private guardrailPolicies: GuardrailPolicy[] = [];\n private skillInstructionBlocks: string[] = [];\n private observerRegistrations: AgentObserverRegistration[] = [];\n private dynamicContextRegistrations: DynamicContextRegistration[] = [];\n private dynamicToolRegistrations: DynamicToolRegistration[] = [];\n private middlewareRegistrations: AgentMiddleware[] = [];\n private memoryRegistration: MemoryRegistration | undefined;\n private eventStoreRegistration: AgentEventStoreRegistration | undefined;\n private activeToolSet = new ToolSet();\n\n constructor(\n agentId: string,\n private readonly completionModel: M,\n ) {\n this.agentId = normalizeAgentId(agentId);\n }\n\n name(name: string): this {\n this.agentName = name;\n return this;\n }\n\n description(description: string): this {\n this.agentDescription = description;\n return this;\n }\n\n instructions(instructions: string): this {\n if (instructions.length > 0) {\n this.instructionBlocks.push(instructions);\n }\n return this;\n }\n\n context(text: string, id = `static_doc_${this.contextDocs.length}`): this {\n this.contextDocs.push({ id, text });\n return this;\n }\n\n dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this {\n this.dynamicContextRegistrations.push({ index, options } as DynamicContextRegistration);\n return this;\n }\n\n dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this {\n this.dynamicToolRegistrations.push({ index, options });\n return this;\n }\n\n tool(tool: AnyTool): this {\n this.activeToolSet.addTool(tool);\n return this;\n }\n\n tools(tools: AnyTool[]): this {\n this.activeToolSet.addTools(tools);\n return this;\n }\n\n mcp(servers: McpServer[]): this {\n for (const server of servers) {\n this.activeToolSet.addTools(server.tools);\n }\n return this;\n }\n\n skills(skillSet: SkillSet): this {\n if (skillSet.instructions.length > 0) {\n this.skillInstructionBlocks.push(skillSet.instructions);\n }\n this.activeToolSet.addTools(skillSet.tools);\n return this;\n }\n\n useToolSet(toolSet: ToolSet): this {\n toolSet.addTools(this.activeToolSet);\n this.activeToolSet = toolSet;\n return this;\n }\n\n temperature(temperature: number): this {\n this.temp = temperature;\n return this;\n }\n\n maxTokens(maxTokens: number): this {\n this.maxTokenCount = maxTokens;\n return this;\n }\n\n additionalParams(params: JsonValue): this {\n this.params = params;\n return this;\n }\n\n toolChoice(toolChoice: ToolChoice): this {\n this.choice = toolChoice;\n return this;\n }\n\n defaultMaxTurns(defaultMaxTurns: number): this {\n this.turns = defaultMaxTurns;\n return this;\n }\n\n hook(hook: PromptHook): this {\n this.requestHook = hook;\n return this;\n }\n\n middleware(middleware: AgentMiddleware): this {\n this.middlewareRegistrations.push(middleware);\n return this;\n }\n\n middlewares(middlewares: AgentMiddleware[]): this {\n this.middlewareRegistrations.push(...middlewares);\n return this;\n }\n\n /**\n * @deprecated Use `middleware` instead.\n */\n toolMiddleware(middleware: ToolMiddleware): this {\n return this.middleware(middleware);\n }\n\n /**\n * @deprecated Use `middlewares` instead.\n */\n toolMiddlewares(middlewares: ToolMiddleware[]): this {\n return this.middlewares(middlewares);\n }\n\n observe(observer: AgentObserver, options: ObserveOptions = {}): this {\n this.observerRegistrations.push({\n observer,\n failOnObserverError: options.failOnObserverError,\n });\n return this;\n }\n\n approvals(options: ToolApprovalsOptions): this {\n this.approvalOptions = options;\n return this;\n }\n\n guardrails(policies: GuardrailPolicyInput): this {\n this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);\n return this;\n }\n\n memory(store: MemoryStore, options: MemoryOptions = {}): this {\n this.memoryRegistration = {\n store,\n options: resolveMemoryOptions(options),\n };\n return this;\n }\n\n eventStore(store: AgentEventStore, options: AgentEventStoreOptions = {}): this {\n this.eventStoreRegistration = {\n store,\n options: {\n include: options.include ?? \"all\",\n },\n };\n return this;\n }\n\n outputSchema(schema: ZodSchema): this {\n this.schema = toProviderJsonSchema(schema);\n return this;\n }\n\n build(): Agent<M> {\n return new Agent({\n id: this.agentId,\n name: this.agentName,\n description: this.agentDescription,\n model: this.completionModel,\n instructions: this.buildInstructions(),\n staticContext: this.contextDocs,\n temperature: this.temp,\n maxTokens: this.maxTokenCount,\n additionalParams: this.params,\n toolSet: this.activeToolSet,\n toolChoice: this.choice,\n defaultMaxTurns: this.turns,\n hook: this.requestHook,\n outputSchema: this.schema,\n observers: this.observerRegistrations,\n approvals: this.approvalOptions,\n guardrails: this.guardrailPolicies,\n dynamicContexts: this.dynamicContextRegistrations,\n dynamicTools: this.dynamicToolRegistrations,\n middlewares: this.middlewareRegistrations,\n memory: this.memoryRegistration,\n eventStore: this.eventStoreRegistration,\n });\n }\n\n private buildInstructions(): string | undefined {\n const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(\n (part): part is string => part !== undefined && part.length > 0,\n );\n return parts.length === 0 ? undefined : parts.join(\"\\n\\n\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BO,IAAM,eAAN,MAAgE;AAAA,EAwBrE,YACE,SACiB,iBACjB;AADiB;AAEjB,SAAK,UAAU,iBAAiB,OAAO;AAAA,EACzC;AAAA,EAHmB;AAAA,EAzBF;AAAA,EACT;AAAA,EACA;AAAA,EACA,oBAA8B,CAAC;AAAA,EAC/B,cAA0B,CAAC;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA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]}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// src/guardrails/index.ts
|
|
2
|
+
function defineGuardrailPolicy(options) {
|
|
3
|
+
return {
|
|
4
|
+
id: options.id,
|
|
5
|
+
mode: options.mode ?? "enforce",
|
|
6
|
+
input: options.input ?? [],
|
|
7
|
+
tools: options.tools ?? [],
|
|
8
|
+
toolResults: options.toolResults ?? [],
|
|
9
|
+
output: options.output ?? []
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function defineInputGuardrail(guardrail) {
|
|
13
|
+
return guardrail;
|
|
14
|
+
}
|
|
15
|
+
function defineToolGuardrail(guardrail) {
|
|
16
|
+
return guardrail;
|
|
17
|
+
}
|
|
18
|
+
function defineToolResultGuardrail(guardrail) {
|
|
19
|
+
return guardrail;
|
|
20
|
+
}
|
|
21
|
+
function defineOutputGuardrail(guardrail) {
|
|
22
|
+
return guardrail;
|
|
23
|
+
}
|
|
24
|
+
function allow(options = {}) {
|
|
25
|
+
return { action: "allow", ...options };
|
|
26
|
+
}
|
|
27
|
+
function block(options) {
|
|
28
|
+
return { action: "block", ...options };
|
|
29
|
+
}
|
|
30
|
+
var guardrails = {
|
|
31
|
+
blockText,
|
|
32
|
+
redactText
|
|
33
|
+
};
|
|
34
|
+
function normalizeGuardrailPolicies(policies) {
|
|
35
|
+
if (policies === void 0) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
return Array.isArray(policies) ? policies : [policies];
|
|
39
|
+
}
|
|
40
|
+
function appendGuardrailPolicies(current, next) {
|
|
41
|
+
return [...current, ...normalizeGuardrailPolicies(next)];
|
|
42
|
+
}
|
|
43
|
+
function hasEnforcedOutputGuardrails(policies) {
|
|
44
|
+
return policies.some((policy) => policy.mode === "enforce" && policy.output.length > 0);
|
|
45
|
+
}
|
|
46
|
+
async function runInputGuardrails(policies, context) {
|
|
47
|
+
let prompt = context.prompt;
|
|
48
|
+
let inputText = context.inputText;
|
|
49
|
+
const decisions = [];
|
|
50
|
+
for (const policy of policies) {
|
|
51
|
+
for (const guardrail of policy.input) {
|
|
52
|
+
const currentContext = { ...context, prompt, inputText };
|
|
53
|
+
const result = await runGuardrail(
|
|
54
|
+
policy,
|
|
55
|
+
guardrail.id,
|
|
56
|
+
"input",
|
|
57
|
+
() => guardrail.check(currentContext, inputActions)
|
|
58
|
+
);
|
|
59
|
+
decisions.push(result.decision);
|
|
60
|
+
if (policy.mode === "observe") {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (result.action.action === "block") {
|
|
64
|
+
return {
|
|
65
|
+
prompt,
|
|
66
|
+
inputText,
|
|
67
|
+
blocked: true,
|
|
68
|
+
message: result.action.message,
|
|
69
|
+
decisions
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (result.action.action === "rewrite") {
|
|
73
|
+
if (result.action.prompt !== void 0) {
|
|
74
|
+
prompt = result.action.prompt;
|
|
75
|
+
inputText = textFromMessage(prompt);
|
|
76
|
+
} else if (result.action.inputText !== void 0) {
|
|
77
|
+
inputText = result.action.inputText;
|
|
78
|
+
prompt = rewriteMessageText(prompt, inputText);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { prompt, inputText, blocked: false, decisions };
|
|
84
|
+
}
|
|
85
|
+
async function runToolGuardrails(policies, toolGuardrails, context) {
|
|
86
|
+
let rawArgs = context.rawArgs;
|
|
87
|
+
let args = context.args;
|
|
88
|
+
const decisions = [];
|
|
89
|
+
for (const entry of toolGuardrailEntries(policies, toolGuardrails, context.toolName)) {
|
|
90
|
+
const currentContext = { ...context, rawArgs, args };
|
|
91
|
+
const result = await runGuardrail(
|
|
92
|
+
entry.policy,
|
|
93
|
+
entry.guardrail.id,
|
|
94
|
+
"tool",
|
|
95
|
+
() => entry.guardrail.check(currentContext, toolActions)
|
|
96
|
+
);
|
|
97
|
+
decisions.push(result.decision);
|
|
98
|
+
if (entry.policy.mode === "observe") {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (result.action.action === "block") {
|
|
102
|
+
return {
|
|
103
|
+
rawArgs,
|
|
104
|
+
blocked: true,
|
|
105
|
+
message: result.action.message,
|
|
106
|
+
decisions
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (result.action.action === "request_approval") {
|
|
110
|
+
return {
|
|
111
|
+
rawArgs,
|
|
112
|
+
blocked: false,
|
|
113
|
+
approval: {
|
|
114
|
+
reason: result.action.reason,
|
|
115
|
+
rejectMessage: result.action.rejectMessage
|
|
116
|
+
},
|
|
117
|
+
decisions
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
if (result.action.action === "rewrite") {
|
|
121
|
+
rawArgs = typeof result.action.args === "string" ? result.action.args : JSON.stringify(result.action.args);
|
|
122
|
+
try {
|
|
123
|
+
args = JSON.parse(rawArgs);
|
|
124
|
+
} catch {
|
|
125
|
+
args = rawArgs;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return { rawArgs, blocked: false, decisions };
|
|
130
|
+
}
|
|
131
|
+
async function runToolResultGuardrails(policies, toolGuardrails, context) {
|
|
132
|
+
let result = context.result;
|
|
133
|
+
let structuredResult = context.structuredResult;
|
|
134
|
+
const decisions = [];
|
|
135
|
+
for (const entry of toolResultGuardrailEntries(policies, toolGuardrails, context.toolName)) {
|
|
136
|
+
const currentContext = { ...context, result, structuredResult };
|
|
137
|
+
const guardrailResult = await runGuardrail(
|
|
138
|
+
entry.policy,
|
|
139
|
+
entry.guardrail.id,
|
|
140
|
+
"tool_result",
|
|
141
|
+
() => entry.guardrail.check(currentContext, toolResultActions)
|
|
142
|
+
);
|
|
143
|
+
decisions.push(guardrailResult.decision);
|
|
144
|
+
if (entry.policy.mode === "observe") {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (guardrailResult.action.action === "block") {
|
|
148
|
+
return {
|
|
149
|
+
result,
|
|
150
|
+
structuredResult,
|
|
151
|
+
blocked: true,
|
|
152
|
+
message: guardrailResult.action.message,
|
|
153
|
+
decisions
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (guardrailResult.action.action === "rewrite") {
|
|
157
|
+
if (guardrailResult.action.structuredResult !== void 0) {
|
|
158
|
+
structuredResult = guardrailResult.action.structuredResult;
|
|
159
|
+
result = textFromToolResultContent(structuredResult);
|
|
160
|
+
} else if (guardrailResult.action.result !== void 0) {
|
|
161
|
+
result = guardrailResult.action.result;
|
|
162
|
+
structuredResult = void 0;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { result, structuredResult, blocked: false, decisions };
|
|
167
|
+
}
|
|
168
|
+
async function runOutputGuardrails(policies, context) {
|
|
169
|
+
let outputText = context.outputText;
|
|
170
|
+
const decisions = [];
|
|
171
|
+
for (const policy of policies) {
|
|
172
|
+
for (const guardrail of policy.output) {
|
|
173
|
+
const currentContext = { ...context, outputText };
|
|
174
|
+
const result = await runGuardrail(
|
|
175
|
+
policy,
|
|
176
|
+
guardrail.id,
|
|
177
|
+
"output",
|
|
178
|
+
() => guardrail.check(currentContext, outputActions)
|
|
179
|
+
);
|
|
180
|
+
decisions.push(result.decision);
|
|
181
|
+
if (policy.mode === "observe") {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (result.action.action === "block") {
|
|
185
|
+
return {
|
|
186
|
+
outputText,
|
|
187
|
+
blocked: true,
|
|
188
|
+
message: result.action.message,
|
|
189
|
+
decisions
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (result.action.action === "rewrite") {
|
|
193
|
+
outputText = result.action.outputText;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return { outputText, blocked: false, decisions };
|
|
198
|
+
}
|
|
199
|
+
async function runGuardrail(policy, guardrailId, boundary, run) {
|
|
200
|
+
const startedAt = Date.now();
|
|
201
|
+
try {
|
|
202
|
+
const action = await run() ?? allow();
|
|
203
|
+
return {
|
|
204
|
+
action,
|
|
205
|
+
decision: decisionRecord({
|
|
206
|
+
policy,
|
|
207
|
+
guardrailId,
|
|
208
|
+
boundary,
|
|
209
|
+
action,
|
|
210
|
+
latencyMs: Date.now() - startedAt
|
|
211
|
+
})
|
|
212
|
+
};
|
|
213
|
+
} catch (error) {
|
|
214
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
215
|
+
if (policy.mode === "observe") {
|
|
216
|
+
return {
|
|
217
|
+
action: allow(),
|
|
218
|
+
decision: {
|
|
219
|
+
policyId: policy.id,
|
|
220
|
+
guardrailId,
|
|
221
|
+
boundary,
|
|
222
|
+
mode: policy.mode,
|
|
223
|
+
action: "error",
|
|
224
|
+
applied: false,
|
|
225
|
+
reason: message,
|
|
226
|
+
latencyMs: Date.now() - startedAt
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
throw error;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function decisionRecord(args) {
|
|
234
|
+
const applied = args.policy.mode === "enforce" && args.action.action !== "allow";
|
|
235
|
+
const record = {
|
|
236
|
+
policyId: args.policy.id,
|
|
237
|
+
guardrailId: args.guardrailId,
|
|
238
|
+
boundary: args.boundary,
|
|
239
|
+
mode: args.policy.mode,
|
|
240
|
+
action: args.action.action,
|
|
241
|
+
applied,
|
|
242
|
+
latencyMs: args.latencyMs
|
|
243
|
+
};
|
|
244
|
+
if (args.action.reason !== void 0) record.reason = args.action.reason;
|
|
245
|
+
if ("message" in args.action && typeof args.action.message === "string") {
|
|
246
|
+
record.message = args.action.message;
|
|
247
|
+
}
|
|
248
|
+
if (args.action.metadata !== void 0) record.metadata = args.action.metadata;
|
|
249
|
+
return record;
|
|
250
|
+
}
|
|
251
|
+
var commonActions = {
|
|
252
|
+
allow,
|
|
253
|
+
block
|
|
254
|
+
};
|
|
255
|
+
var inputActions = {
|
|
256
|
+
...commonActions,
|
|
257
|
+
rewrite(options) {
|
|
258
|
+
return { action: "rewrite", ...options };
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
var toolActions = {
|
|
262
|
+
...commonActions,
|
|
263
|
+
rewrite(options) {
|
|
264
|
+
return { action: "rewrite", ...options };
|
|
265
|
+
},
|
|
266
|
+
requestApproval(options = {}) {
|
|
267
|
+
return { action: "request_approval", ...options };
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
var toolResultActions = {
|
|
271
|
+
...commonActions,
|
|
272
|
+
rewrite(options) {
|
|
273
|
+
return { action: "rewrite", ...options };
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
var outputActions = {
|
|
277
|
+
...commonActions,
|
|
278
|
+
rewrite(options) {
|
|
279
|
+
return { action: "rewrite", ...options };
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
function toolGuardrailEntries(policies, toolGuardrails, toolName) {
|
|
283
|
+
return [
|
|
284
|
+
...policies.flatMap(
|
|
285
|
+
(policy) => policy.tools.filter((guardrail) => matchesTool(guardrail.tool, toolName)).map((guardrail) => ({ policy, guardrail }))
|
|
286
|
+
),
|
|
287
|
+
...toolGuardrails.map((guardrail) => ({
|
|
288
|
+
policy: defineGuardrailPolicy({
|
|
289
|
+
id: `tool:${toolName}:${guardrail.id}`,
|
|
290
|
+
tools: [guardrail]
|
|
291
|
+
}),
|
|
292
|
+
guardrail
|
|
293
|
+
}))
|
|
294
|
+
];
|
|
295
|
+
}
|
|
296
|
+
function toolResultGuardrailEntries(policies, toolGuardrails, toolName) {
|
|
297
|
+
return [
|
|
298
|
+
...policies.flatMap(
|
|
299
|
+
(policy) => policy.toolResults.filter((guardrail) => matchesTool(guardrail.tool, toolName)).map((guardrail) => ({ policy, guardrail }))
|
|
300
|
+
),
|
|
301
|
+
...toolGuardrails.map((guardrail) => ({
|
|
302
|
+
policy: defineGuardrailPolicy({
|
|
303
|
+
id: `tool:${toolName}:${guardrail.id}`,
|
|
304
|
+
toolResults: [guardrail]
|
|
305
|
+
}),
|
|
306
|
+
guardrail
|
|
307
|
+
}))
|
|
308
|
+
];
|
|
309
|
+
}
|
|
310
|
+
function matchesTool(tool, toolName) {
|
|
311
|
+
if (tool === void 0) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
return Array.isArray(tool) ? tool.includes(toolName) : tool === toolName;
|
|
315
|
+
}
|
|
316
|
+
function blockText(options) {
|
|
317
|
+
return textPatternGuardrail(options, "block");
|
|
318
|
+
}
|
|
319
|
+
function redactText(options) {
|
|
320
|
+
return textPatternGuardrail(options, "rewrite");
|
|
321
|
+
}
|
|
322
|
+
function textPatternGuardrail(options, action) {
|
|
323
|
+
if (options.boundary === "input") {
|
|
324
|
+
return defineInputGuardrail({
|
|
325
|
+
id: options.id,
|
|
326
|
+
check(ctx, actions) {
|
|
327
|
+
return textPatternAction(
|
|
328
|
+
ctx.inputText,
|
|
329
|
+
options,
|
|
330
|
+
action,
|
|
331
|
+
(value) => actions.rewrite({ inputText: value, reason: options.reason })
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
if (options.boundary === "output") {
|
|
337
|
+
return defineOutputGuardrail({
|
|
338
|
+
id: options.id,
|
|
339
|
+
check(ctx, actions) {
|
|
340
|
+
return textPatternAction(
|
|
341
|
+
ctx.outputText,
|
|
342
|
+
options,
|
|
343
|
+
action,
|
|
344
|
+
(value) => actions.rewrite({ outputText: value, reason: options.reason })
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return defineToolResultGuardrail({
|
|
350
|
+
id: options.id,
|
|
351
|
+
check(ctx, actions) {
|
|
352
|
+
return textPatternAction(
|
|
353
|
+
ctx.result,
|
|
354
|
+
options,
|
|
355
|
+
action,
|
|
356
|
+
(value) => actions.rewrite({ result: value, reason: options.reason })
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
function textPatternAction(text, options, action, rewrite) {
|
|
362
|
+
const matched = options.patterns.some((pattern) => textPatternMatches(text, pattern));
|
|
363
|
+
if (!matched) {
|
|
364
|
+
return allow();
|
|
365
|
+
}
|
|
366
|
+
if (action === "block") {
|
|
367
|
+
return block({
|
|
368
|
+
reason: options.reason,
|
|
369
|
+
message: options.message
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
let current = text;
|
|
373
|
+
for (const pattern of options.patterns) {
|
|
374
|
+
current = replaceTextPattern(current, pattern, options.replacement ?? "[redacted]");
|
|
375
|
+
}
|
|
376
|
+
return rewrite(current);
|
|
377
|
+
}
|
|
378
|
+
function textPatternMatches(text, pattern) {
|
|
379
|
+
if (typeof pattern === "string") {
|
|
380
|
+
return text.includes(pattern);
|
|
381
|
+
}
|
|
382
|
+
pattern.lastIndex = 0;
|
|
383
|
+
const matched = pattern.test(text);
|
|
384
|
+
pattern.lastIndex = 0;
|
|
385
|
+
return matched;
|
|
386
|
+
}
|
|
387
|
+
function replaceTextPattern(text, pattern, replacement) {
|
|
388
|
+
if (typeof pattern === "string") {
|
|
389
|
+
return text.split(pattern).join(replacement);
|
|
390
|
+
}
|
|
391
|
+
pattern.lastIndex = 0;
|
|
392
|
+
const flags = pattern.flags.replace("y", "");
|
|
393
|
+
const globalFlags = flags.includes("g") ? flags : `${flags}g`;
|
|
394
|
+
return text.replace(new RegExp(pattern.source, globalFlags), replacement);
|
|
395
|
+
}
|
|
396
|
+
function textFromMessage(message) {
|
|
397
|
+
if (message.role === "system") {
|
|
398
|
+
return message.content;
|
|
399
|
+
}
|
|
400
|
+
return message.content.flatMap((content) => {
|
|
401
|
+
if (content.type === "text") {
|
|
402
|
+
return [content.text];
|
|
403
|
+
}
|
|
404
|
+
if (content.type === "document" && content.source.type === "text") {
|
|
405
|
+
return [content.source.text];
|
|
406
|
+
}
|
|
407
|
+
return [];
|
|
408
|
+
}).join("\n");
|
|
409
|
+
}
|
|
410
|
+
function rewriteMessageText(message, text) {
|
|
411
|
+
if (message.role === "system") {
|
|
412
|
+
return { ...message, content: text };
|
|
413
|
+
}
|
|
414
|
+
if (message.role === "user") {
|
|
415
|
+
return {
|
|
416
|
+
...message,
|
|
417
|
+
content: [
|
|
418
|
+
{ type: "text", text },
|
|
419
|
+
...message.content.filter(
|
|
420
|
+
(item) => item.type !== "text" && !(item.type === "document" && item.source.type === "text")
|
|
421
|
+
)
|
|
422
|
+
]
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
if (message.role === "assistant") {
|
|
426
|
+
return { ...message, content: [{ type: "text", text }] };
|
|
427
|
+
}
|
|
428
|
+
return message;
|
|
429
|
+
}
|
|
430
|
+
function textFromToolResultContent(content) {
|
|
431
|
+
return content.map((item) => item.type === "text" ? item.text : "[image]").join("\n");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
export {
|
|
435
|
+
defineGuardrailPolicy,
|
|
436
|
+
defineInputGuardrail,
|
|
437
|
+
defineToolGuardrail,
|
|
438
|
+
defineToolResultGuardrail,
|
|
439
|
+
defineOutputGuardrail,
|
|
440
|
+
allow,
|
|
441
|
+
block,
|
|
442
|
+
guardrails,
|
|
443
|
+
normalizeGuardrailPolicies,
|
|
444
|
+
appendGuardrailPolicies,
|
|
445
|
+
hasEnforcedOutputGuardrails,
|
|
446
|
+
runInputGuardrails,
|
|
447
|
+
runToolGuardrails,
|
|
448
|
+
runToolResultGuardrails,
|
|
449
|
+
runOutputGuardrails
|
|
450
|
+
};
|
|
451
|
+
//# sourceMappingURL=chunk-YRERPIA5.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/guardrails/index.ts"],"sourcesContent":["import type { JsonObject, JsonValue, Message, ToolResultContent, Usage } from \"../completion\";\n\ntype MaybePromise<T> = T | Promise<T>;\n\nexport type GuardrailMode = \"enforce\" | \"observe\";\n\nexport type GuardrailBoundary = \"input\" | \"tool\" | \"tool_result\" | \"output\";\n\nexport type GuardrailRunContext = {\n agentId: string;\n runId: string;\n sessionId?: string | undefined;\n metadata?: JsonObject | undefined;\n};\n\nexport type GuardrailDecisionRecord = {\n policyId: string;\n guardrailId: string;\n boundary: GuardrailBoundary;\n mode: GuardrailMode;\n action: GuardrailActionName;\n applied: boolean;\n reason?: string | undefined;\n message?: string | undefined;\n metadata?: JsonObject | undefined;\n latencyMs: number;\n};\n\nexport type GuardrailActionName = \"allow\" | \"block\" | \"rewrite\" | \"request_approval\" | \"error\";\n\nexport type GuardrailActionBase = {\n reason?: string | undefined;\n metadata?: JsonObject | undefined;\n};\n\nexport type GuardrailAllow = GuardrailActionBase & {\n action: \"allow\";\n};\n\nexport type GuardrailBlock = GuardrailActionBase & {\n action: \"block\";\n reason: string;\n message?: string | undefined;\n};\n\nexport type InputGuardrailRewrite = GuardrailActionBase & {\n action: \"rewrite\";\n prompt?: Message | undefined;\n inputText?: string | undefined;\n};\n\nexport type ToolGuardrailRewrite = GuardrailActionBase & {\n action: \"rewrite\";\n args: JsonValue | string;\n};\n\nexport type ToolResultGuardrailRewrite = GuardrailActionBase & {\n action: \"rewrite\";\n result?: string | undefined;\n structuredResult?: ToolResultContent[] | undefined;\n};\n\nexport type OutputGuardrailRewrite = GuardrailActionBase & {\n action: \"rewrite\";\n outputText: string;\n};\n\nexport type ToolGuardrailApprovalRequest = GuardrailActionBase & {\n action: \"request_approval\";\n reason?: string | undefined;\n rejectMessage?: string | undefined;\n};\n\nexport type InputGuardrailResult = GuardrailAllow | GuardrailBlock | InputGuardrailRewrite;\nexport type ToolGuardrailResult =\n | GuardrailAllow\n | GuardrailBlock\n | ToolGuardrailRewrite\n | ToolGuardrailApprovalRequest;\nexport type ToolResultGuardrailResult =\n | GuardrailAllow\n | GuardrailBlock\n | ToolResultGuardrailRewrite;\nexport type OutputGuardrailResult = GuardrailAllow | GuardrailBlock | OutputGuardrailRewrite;\n\nexport type GuardrailCommonActions = {\n allow(options?: GuardrailActionBase): GuardrailAllow;\n block(options: Omit<GuardrailBlock, \"action\">): GuardrailBlock;\n};\n\nexport type InputGuardrailActions = GuardrailCommonActions & {\n rewrite(options: Omit<InputGuardrailRewrite, \"action\">): InputGuardrailRewrite;\n};\n\nexport type ToolGuardrailActions = GuardrailCommonActions & {\n rewrite(options: Omit<ToolGuardrailRewrite, \"action\">): ToolGuardrailRewrite;\n requestApproval(\n options?: Omit<ToolGuardrailApprovalRequest, \"action\">,\n ): ToolGuardrailApprovalRequest;\n};\n\nexport type ToolResultGuardrailActions = GuardrailCommonActions & {\n rewrite(options: Omit<ToolResultGuardrailRewrite, \"action\">): ToolResultGuardrailRewrite;\n};\n\nexport type OutputGuardrailActions = GuardrailCommonActions & {\n rewrite(options: Omit<OutputGuardrailRewrite, \"action\">): OutputGuardrailRewrite;\n};\n\nexport type InputGuardrailContext = {\n prompt: Message;\n history: Message[];\n inputText: string;\n run: GuardrailRunContext;\n};\n\nexport type ToolGuardrailContext<Args = unknown> = {\n toolName: string;\n args: Args;\n rawArgs: string;\n toolCallId?: string | undefined;\n internalCallId: string;\n turn: number;\n run: GuardrailRunContext;\n};\n\nexport type ToolResultGuardrailContext<Args = unknown> = {\n toolName: string;\n args: Args;\n rawArgs: string;\n result: string;\n structuredResult?: ToolResultContent[] | undefined;\n toolCallId?: string | undefined;\n internalCallId: string;\n turn: number;\n run: GuardrailRunContext;\n};\n\nexport type OutputGuardrailContext = {\n outputText: string;\n messages: Message[];\n usage: Usage;\n run: GuardrailRunContext;\n};\n\nexport type InputGuardrail = {\n id: string;\n check(\n context: InputGuardrailContext,\n actions: InputGuardrailActions,\n ): MaybePromise<InputGuardrailResult | undefined>;\n};\n\nexport type ToolGuardrail<Args = unknown> = {\n id: string;\n tool?: string | string[] | undefined;\n check(\n context: ToolGuardrailContext<Args>,\n actions: ToolGuardrailActions,\n ): MaybePromise<ToolGuardrailResult | undefined>;\n};\n\nexport type ToolResultGuardrail<Args = unknown> = {\n id: string;\n tool?: string | string[] | undefined;\n check(\n context: ToolResultGuardrailContext<Args>,\n actions: ToolResultGuardrailActions,\n ): MaybePromise<ToolResultGuardrailResult | undefined>;\n};\n\nexport type OutputGuardrail = {\n id: string;\n check(\n context: OutputGuardrailContext,\n actions: OutputGuardrailActions,\n ): MaybePromise<OutputGuardrailResult | undefined>;\n};\n\nexport type GuardrailPolicy = {\n id: string;\n mode: GuardrailMode;\n input: InputGuardrail[];\n tools: ToolGuardrail[];\n toolResults: ToolResultGuardrail[];\n output: OutputGuardrail[];\n};\n\nexport type GuardrailPolicyOptions = {\n id: string;\n mode?: GuardrailMode | undefined;\n input?: InputGuardrail[] | undefined;\n tools?: ToolGuardrail[] | undefined;\n toolResults?: ToolResultGuardrail[] | undefined;\n output?: OutputGuardrail[] | undefined;\n};\n\nexport type GuardrailPolicyInput = GuardrailPolicy | GuardrailPolicy[];\n\nexport type InputGuardrailRunResult = {\n prompt: Message;\n inputText: string;\n blocked: boolean;\n message?: string | undefined;\n decisions: GuardrailDecisionRecord[];\n};\n\nexport type ToolGuardrailRunResult = {\n rawArgs: string;\n blocked: boolean;\n message?: string | undefined;\n approval?: { reason?: string | undefined; rejectMessage?: string | undefined } | undefined;\n decisions: GuardrailDecisionRecord[];\n};\n\nexport type ToolResultGuardrailRunResult = {\n result: string;\n structuredResult?: ToolResultContent[] | undefined;\n blocked: boolean;\n message?: string | undefined;\n decisions: GuardrailDecisionRecord[];\n};\n\nexport type OutputGuardrailRunResult = {\n outputText: string;\n blocked: boolean;\n message?: string | undefined;\n decisions: GuardrailDecisionRecord[];\n};\n\nexport function defineGuardrailPolicy(options: GuardrailPolicyOptions): GuardrailPolicy {\n return {\n id: options.id,\n mode: options.mode ?? \"enforce\",\n input: options.input ?? [],\n tools: options.tools ?? [],\n toolResults: options.toolResults ?? [],\n output: options.output ?? [],\n };\n}\n\nexport function defineInputGuardrail(guardrail: InputGuardrail): InputGuardrail {\n return guardrail;\n}\n\nexport function defineToolGuardrail<Args = unknown>(\n guardrail: ToolGuardrail<Args>,\n): ToolGuardrail<Args> {\n return guardrail;\n}\n\nexport function defineToolResultGuardrail<Args = unknown>(\n guardrail: ToolResultGuardrail<Args>,\n): ToolResultGuardrail<Args> {\n return guardrail;\n}\n\nexport function defineOutputGuardrail(guardrail: OutputGuardrail): OutputGuardrail {\n return guardrail;\n}\n\nexport function allow(options: GuardrailActionBase = {}): GuardrailAllow {\n return { action: \"allow\", ...options };\n}\n\nexport function block(options: Omit<GuardrailBlock, \"action\">): GuardrailBlock {\n return { action: \"block\", ...options };\n}\n\nexport const guardrails = {\n blockText,\n redactText,\n};\n\nexport function normalizeGuardrailPolicies(\n policies: GuardrailPolicyInput | undefined,\n): GuardrailPolicy[] {\n if (policies === undefined) {\n return [];\n }\n return Array.isArray(policies) ? policies : [policies];\n}\n\nexport function appendGuardrailPolicies(\n current: GuardrailPolicy[],\n next: GuardrailPolicyInput,\n): GuardrailPolicy[] {\n return [...current, ...normalizeGuardrailPolicies(next)];\n}\n\nexport function hasEnforcedOutputGuardrails(policies: GuardrailPolicy[]): boolean {\n return policies.some((policy) => policy.mode === \"enforce\" && policy.output.length > 0);\n}\n\nexport async function runInputGuardrails(\n policies: GuardrailPolicy[],\n context: InputGuardrailContext,\n): Promise<InputGuardrailRunResult> {\n let prompt = context.prompt;\n let inputText = context.inputText;\n const decisions: GuardrailDecisionRecord[] = [];\n\n for (const policy of policies) {\n for (const guardrail of policy.input) {\n const currentContext = { ...context, prompt, inputText };\n const result = await runGuardrail(policy, guardrail.id, \"input\", () =>\n guardrail.check(currentContext, inputActions),\n );\n decisions.push(result.decision);\n\n if (policy.mode === \"observe\") {\n continue;\n }\n if (result.action.action === \"block\") {\n return {\n prompt,\n inputText,\n blocked: true,\n message: result.action.message,\n decisions,\n };\n }\n if (result.action.action === \"rewrite\") {\n if (result.action.prompt !== undefined) {\n prompt = result.action.prompt;\n inputText = textFromMessage(prompt);\n } else if (result.action.inputText !== undefined) {\n inputText = result.action.inputText;\n prompt = rewriteMessageText(prompt, inputText);\n }\n }\n }\n }\n\n return { prompt, inputText, blocked: false, decisions };\n}\n\nexport async function runToolGuardrails<Args>(\n policies: GuardrailPolicy[],\n toolGuardrails: ToolGuardrail<Args>[],\n context: ToolGuardrailContext<Args>,\n): Promise<ToolGuardrailRunResult> {\n let rawArgs = context.rawArgs;\n let args = context.args;\n const decisions: GuardrailDecisionRecord[] = [];\n\n for (const entry of toolGuardrailEntries(policies, toolGuardrails, context.toolName)) {\n const currentContext = { ...context, rawArgs, args };\n const result = await runGuardrail(entry.policy, entry.guardrail.id, \"tool\", () =>\n entry.guardrail.check(currentContext, toolActions),\n );\n decisions.push(result.decision);\n\n if (entry.policy.mode === \"observe\") {\n continue;\n }\n if (result.action.action === \"block\") {\n return {\n rawArgs,\n blocked: true,\n message: result.action.message,\n decisions,\n };\n }\n if (result.action.action === \"request_approval\") {\n return {\n rawArgs,\n blocked: false,\n approval: {\n reason: result.action.reason,\n rejectMessage: result.action.rejectMessage,\n },\n decisions,\n };\n }\n if (result.action.action === \"rewrite\") {\n rawArgs =\n typeof result.action.args === \"string\"\n ? result.action.args\n : JSON.stringify(result.action.args);\n try {\n args = JSON.parse(rawArgs) as Args;\n } catch {\n args = rawArgs as Args;\n }\n }\n }\n\n return { rawArgs, blocked: false, decisions };\n}\n\nexport async function runToolResultGuardrails<Args>(\n policies: GuardrailPolicy[],\n toolGuardrails: ToolResultGuardrail<Args>[],\n context: ToolResultGuardrailContext<Args>,\n): Promise<ToolResultGuardrailRunResult> {\n let result = context.result;\n let structuredResult = context.structuredResult;\n const decisions: GuardrailDecisionRecord[] = [];\n\n for (const entry of toolResultGuardrailEntries(policies, toolGuardrails, context.toolName)) {\n const currentContext = { ...context, result, structuredResult };\n const guardrailResult = await runGuardrail(\n entry.policy,\n entry.guardrail.id,\n \"tool_result\",\n () => entry.guardrail.check(currentContext, toolResultActions),\n );\n decisions.push(guardrailResult.decision);\n\n if (entry.policy.mode === \"observe\") {\n continue;\n }\n if (guardrailResult.action.action === \"block\") {\n return {\n result,\n structuredResult,\n blocked: true,\n message: guardrailResult.action.message,\n decisions,\n };\n }\n if (guardrailResult.action.action === \"rewrite\") {\n if (guardrailResult.action.structuredResult !== undefined) {\n structuredResult = guardrailResult.action.structuredResult;\n result = textFromToolResultContent(structuredResult);\n } else if (guardrailResult.action.result !== undefined) {\n result = guardrailResult.action.result;\n structuredResult = undefined;\n }\n }\n }\n\n return { result, structuredResult, blocked: false, decisions };\n}\n\nexport async function runOutputGuardrails(\n policies: GuardrailPolicy[],\n context: OutputGuardrailContext,\n): Promise<OutputGuardrailRunResult> {\n let outputText = context.outputText;\n const decisions: GuardrailDecisionRecord[] = [];\n\n for (const policy of policies) {\n for (const guardrail of policy.output) {\n const currentContext = { ...context, outputText };\n const result = await runGuardrail(policy, guardrail.id, \"output\", () =>\n guardrail.check(currentContext, outputActions),\n );\n decisions.push(result.decision);\n\n if (policy.mode === \"observe\") {\n continue;\n }\n if (result.action.action === \"block\") {\n return {\n outputText,\n blocked: true,\n message: result.action.message,\n decisions,\n };\n }\n if (result.action.action === \"rewrite\") {\n outputText = result.action.outputText;\n }\n }\n }\n\n return { outputText, blocked: false, decisions };\n}\n\ntype RunGuardrailResult<TAction extends { action: GuardrailActionName }> = {\n action: TAction;\n decision: GuardrailDecisionRecord;\n};\n\nasync function runGuardrail<TAction extends { action: GuardrailActionName } & GuardrailActionBase>(\n policy: GuardrailPolicy,\n guardrailId: string,\n boundary: GuardrailBoundary,\n run: () => MaybePromise<TAction | undefined>,\n): Promise<RunGuardrailResult<TAction | GuardrailAllow>> {\n const startedAt = Date.now();\n try {\n const action = (await run()) ?? allow();\n return {\n action,\n decision: decisionRecord({\n policy,\n guardrailId,\n boundary,\n action,\n latencyMs: Date.now() - startedAt,\n }),\n };\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n if (policy.mode === \"observe\") {\n return {\n action: allow(),\n decision: {\n policyId: policy.id,\n guardrailId,\n boundary,\n mode: policy.mode,\n action: \"error\",\n applied: false,\n reason: message,\n latencyMs: Date.now() - startedAt,\n },\n };\n }\n throw error;\n }\n}\n\nfunction decisionRecord(args: {\n policy: GuardrailPolicy;\n guardrailId: string;\n boundary: GuardrailBoundary;\n action: GuardrailAllow | GuardrailBlock | ({ action: GuardrailActionName } & GuardrailActionBase);\n latencyMs: number;\n}): GuardrailDecisionRecord {\n const applied = args.policy.mode === \"enforce\" && args.action.action !== \"allow\";\n const record: GuardrailDecisionRecord = {\n policyId: args.policy.id,\n guardrailId: args.guardrailId,\n boundary: args.boundary,\n mode: args.policy.mode,\n action: args.action.action,\n applied,\n latencyMs: args.latencyMs,\n };\n if (args.action.reason !== undefined) record.reason = args.action.reason;\n if (\"message\" in args.action && typeof args.action.message === \"string\") {\n record.message = args.action.message;\n }\n if (args.action.metadata !== undefined) record.metadata = args.action.metadata;\n return record;\n}\n\nconst commonActions: GuardrailCommonActions = {\n allow,\n block,\n};\n\nconst inputActions: InputGuardrailActions = {\n ...commonActions,\n rewrite(options) {\n return { action: \"rewrite\", ...options };\n },\n};\n\nconst toolActions: ToolGuardrailActions = {\n ...commonActions,\n rewrite(options) {\n return { action: \"rewrite\", ...options };\n },\n requestApproval(options = {}) {\n return { action: \"request_approval\", ...options };\n },\n};\n\nconst toolResultActions: ToolResultGuardrailActions = {\n ...commonActions,\n rewrite(options) {\n return { action: \"rewrite\", ...options };\n },\n};\n\nconst outputActions: OutputGuardrailActions = {\n ...commonActions,\n rewrite(options) {\n return { action: \"rewrite\", ...options };\n },\n};\n\nfunction toolGuardrailEntries<Args>(\n policies: GuardrailPolicy[],\n toolGuardrails: ToolGuardrail<Args>[],\n toolName: string,\n): Array<{ policy: GuardrailPolicy; guardrail: ToolGuardrail<Args> }> {\n return [\n ...policies.flatMap((policy) =>\n policy.tools\n .filter((guardrail) => matchesTool(guardrail.tool, toolName))\n .map((guardrail) => ({ policy, guardrail: guardrail as ToolGuardrail<Args> })),\n ),\n ...toolGuardrails.map((guardrail) => ({\n policy: defineGuardrailPolicy({\n id: `tool:${toolName}:${guardrail.id}`,\n tools: [guardrail],\n }),\n guardrail,\n })),\n ];\n}\n\nfunction toolResultGuardrailEntries<Args>(\n policies: GuardrailPolicy[],\n toolGuardrails: ToolResultGuardrail<Args>[],\n toolName: string,\n): Array<{ policy: GuardrailPolicy; guardrail: ToolResultGuardrail<Args> }> {\n return [\n ...policies.flatMap((policy) =>\n policy.toolResults\n .filter((guardrail) => matchesTool(guardrail.tool, toolName))\n .map((guardrail) => ({ policy, guardrail: guardrail as ToolResultGuardrail<Args> })),\n ),\n ...toolGuardrails.map((guardrail) => ({\n policy: defineGuardrailPolicy({\n id: `tool:${toolName}:${guardrail.id}`,\n toolResults: [guardrail],\n }),\n guardrail,\n })),\n ];\n}\n\nfunction matchesTool(tool: string | string[] | undefined, toolName: string): boolean {\n if (tool === undefined) {\n return true;\n }\n return Array.isArray(tool) ? tool.includes(toolName) : tool === toolName;\n}\n\ntype TextPatternBoundary = \"input\" | \"output\" | \"tool_result\";\n\ntype TextPatternGuardrailFor<Boundary extends TextPatternBoundary> = Boundary extends \"input\"\n ? InputGuardrail\n : Boundary extends \"output\"\n ? OutputGuardrail\n : ToolResultGuardrail;\n\ntype TextPatternGuardrailOptions<Boundary extends TextPatternBoundary = TextPatternBoundary> = {\n id: string;\n boundary: Boundary;\n patterns: Array<string | RegExp>;\n reason: string;\n message?: string | undefined;\n};\n\ntype TextPatternRedactOptions<Boundary extends TextPatternBoundary = TextPatternBoundary> =\n TextPatternGuardrailOptions<Boundary> & { replacement?: string | undefined };\n\nfunction blockText<Boundary extends TextPatternBoundary>(\n options: TextPatternGuardrailOptions<Boundary>,\n): TextPatternGuardrailFor<Boundary> {\n return textPatternGuardrail(options, \"block\");\n}\n\nfunction redactText<Boundary extends TextPatternBoundary>(\n options: TextPatternRedactOptions<Boundary>,\n): TextPatternGuardrailFor<Boundary> {\n return textPatternGuardrail(options, \"rewrite\");\n}\n\nfunction textPatternGuardrail<Boundary extends TextPatternBoundary>(\n options: TextPatternRedactOptions<Boundary>,\n action: \"block\" | \"rewrite\",\n): TextPatternGuardrailFor<Boundary> {\n if (options.boundary === \"input\") {\n return defineInputGuardrail({\n id: options.id,\n check(ctx, actions) {\n return textPatternAction(ctx.inputText, options, action, (value) =>\n actions.rewrite({ inputText: value, reason: options.reason }),\n );\n },\n }) as TextPatternGuardrailFor<Boundary>;\n }\n if (options.boundary === \"output\") {\n return defineOutputGuardrail({\n id: options.id,\n check(ctx, actions) {\n return textPatternAction(ctx.outputText, options, action, (value) =>\n actions.rewrite({ outputText: value, reason: options.reason }),\n );\n },\n }) as TextPatternGuardrailFor<Boundary>;\n }\n return defineToolResultGuardrail({\n id: options.id,\n check(ctx, actions) {\n return textPatternAction(ctx.result, options, action, (value) =>\n actions.rewrite({ result: value, reason: options.reason }),\n );\n },\n }) as TextPatternGuardrailFor<Boundary>;\n}\n\nfunction textPatternAction<TRewrite>(\n text: string,\n options: TextPatternGuardrailOptions & { replacement?: string | undefined },\n action: \"block\" | \"rewrite\",\n rewrite: (value: string) => TRewrite,\n): GuardrailAllow | GuardrailBlock | TRewrite {\n const matched = options.patterns.some((pattern) => textPatternMatches(text, pattern));\n if (!matched) {\n return allow();\n }\n if (action === \"block\") {\n return block({\n reason: options.reason,\n message: options.message,\n });\n }\n let current = text;\n for (const pattern of options.patterns) {\n current = replaceTextPattern(current, pattern, options.replacement ?? \"[redacted]\");\n }\n return rewrite(current);\n}\n\nfunction textPatternMatches(text: string, pattern: string | RegExp): boolean {\n if (typeof pattern === \"string\") {\n return text.includes(pattern);\n }\n pattern.lastIndex = 0;\n const matched = pattern.test(text);\n pattern.lastIndex = 0;\n return matched;\n}\n\nfunction replaceTextPattern(text: string, pattern: string | RegExp, replacement: string): string {\n if (typeof pattern === \"string\") {\n return text.split(pattern).join(replacement);\n }\n pattern.lastIndex = 0;\n const flags = pattern.flags.replace(\"y\", \"\");\n const globalFlags = flags.includes(\"g\") ? flags : `${flags}g`;\n return text.replace(new RegExp(pattern.source, globalFlags), replacement);\n}\n\nfunction textFromMessage(message: Message): string {\n if (message.role === \"system\") {\n return message.content;\n }\n return message.content\n .flatMap((content) => {\n if (content.type === \"text\") {\n return [content.text];\n }\n if (content.type === \"document\" && content.source.type === \"text\") {\n return [content.source.text];\n }\n return [];\n })\n .join(\"\\n\");\n}\n\nfunction rewriteMessageText(message: Message, text: string): Message {\n if (message.role === \"system\") {\n return { ...message, content: text };\n }\n if (message.role === \"user\") {\n return {\n ...message,\n content: [\n { type: \"text\", text },\n ...message.content.filter(\n (item) =>\n item.type !== \"text\" && !(item.type === \"document\" && item.source.type === \"text\"),\n ),\n ],\n };\n }\n if (message.role === \"assistant\") {\n return { ...message, content: [{ type: \"text\", text }] };\n }\n return message;\n}\n\nfunction textFromToolResultContent(content: ToolResultContent[]): string {\n return content.map((item) => (item.type === \"text\" ? item.text : \"[image]\")).join(\"\\n\");\n}\n"],"mappings":";AAsOO,SAAS,sBAAsB,SAAkD;AACtF,SAAO;AAAA,IACL,IAAI,QAAQ;AAAA,IACZ,MAAM,QAAQ,QAAQ;AAAA,IACtB,OAAO,QAAQ,SAAS,CAAC;AAAA,IACzB,OAAO,QAAQ,SAAS,CAAC;AAAA,IACzB,aAAa,QAAQ,eAAe,CAAC;AAAA,IACrC,QAAQ,QAAQ,UAAU,CAAC;AAAA,EAC7B;AACF;AAEO,SAAS,qBAAqB,WAA2C;AAC9E,SAAO;AACT;AAEO,SAAS,oBACd,WACqB;AACrB,SAAO;AACT;AAEO,SAAS,0BACd,WAC2B;AAC3B,SAAO;AACT;AAEO,SAAS,sBAAsB,WAA6C;AACjF,SAAO;AACT;AAEO,SAAS,MAAM,UAA+B,CAAC,GAAmB;AACvE,SAAO,EAAE,QAAQ,SAAS,GAAG,QAAQ;AACvC;AAEO,SAAS,MAAM,SAAyD;AAC7E,SAAO,EAAE,QAAQ,SAAS,GAAG,QAAQ;AACvC;AAEO,IAAM,aAAa;AAAA,EACxB;AAAA,EACA;AACF;AAEO,SAAS,2BACd,UACmB;AACnB,MAAI,aAAa,QAAW;AAC1B,WAAO,CAAC;AAAA,EACV;AACA,SAAO,MAAM,QAAQ,QAAQ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|
|
7
7
|
import {
|
|
8
8
|
isRecord
|
|
9
9
|
} from "./chunk-MMHG7WAM.js";
|
|
@@ -395,4 +395,4 @@ export {
|
|
|
395
395
|
SkillValidationError,
|
|
396
396
|
skill
|
|
397
397
|
};
|
|
398
|
-
//# sourceMappingURL=chunk-
|
|
398
|
+
//# sourceMappingURL=chunk-ZGIE73BH.js.map
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
export { C as CreateCompletionBaseOptions, a as CreateCompletionInput, b as CreateCompletionOptions, c as CreateCompletionResult, d as CreateCompletionStreamOptions, e as CreateParsedCompletionOptions, f as CreateParsedCompletionResult, g as createCompletion, h as createCompletionStream, i as createParsedCompletion, j as isStreamingCompletionModel } from '../create-completion-
|
|
2
|
-
import { D as Document, M as Message, C as CompletionModel, h as ToolDefinition, n as ToolChoice, e as JsonValue, J as JsonObject, b as CompletionRequest, c as CompletionResponse } from '../types-
|
|
3
|
-
export { A as AssistantContent, a as AssistantMessage, r as CompletionCapabilityError, t as CompletionModelCapabilities, p as CompletionStreamEvent, u as DocumentContent, I as ImageContent, v as ImageDetail, d as JsonPrimitive, R as Reasoning, w as ReasoningContent, x as ReasoningContentType, o as StreamingCompletionModel, S as SystemMessage, T as Text, f as ToolCall, g as ToolContent, y as ToolFunction, i as ToolMessage, j as ToolResult, k as ToolResultContent, U as Usage, l as UserContent, m as UserMessage, z as assertCompletionRequestSupported, q as isToolResultContentArray, B as reasoningDisplayText, s as serializeToolResultOutput, E as textFromAssistantContent } from '../types-
|
|
1
|
+
export { C as CreateCompletionBaseOptions, a as CreateCompletionInput, b as CreateCompletionOptions, c as CreateCompletionResult, d as CreateCompletionStreamOptions, e as CreateParsedCompletionOptions, f as CreateParsedCompletionResult, g as createCompletion, h as createCompletionStream, i as createParsedCompletion, j as isStreamingCompletionModel } from '../create-completion-Y6YsYbQB.js';
|
|
2
|
+
import { D as Document, M as Message, C as CompletionModel, h as ToolDefinition, n as ToolChoice, e as JsonValue, J as JsonObject, b as CompletionRequest, c as CompletionResponse } from '../types-q9G7Bvwl.js';
|
|
3
|
+
export { A as AssistantContent, a as AssistantMessage, r as CompletionCapabilityError, t as CompletionModelCapabilities, p as CompletionStreamEvent, u as DocumentContent, I as ImageContent, v as ImageDetail, d as JsonPrimitive, R as Reasoning, w as ReasoningContent, x as ReasoningContentType, o as StreamingCompletionModel, S as SystemMessage, T as Text, f as ToolCall, g as ToolContent, y as ToolFunction, i as ToolMessage, j as ToolResult, k as ToolResultContent, U as Usage, l as UserContent, m as UserMessage, z as assertCompletionRequestSupported, q as isToolResultContentArray, B as reasoningDisplayText, s as serializeToolResultOutput, E as textFromAssistantContent } from '../types-q9G7Bvwl.js';
|
|
4
4
|
import '../zod-schema-C7F4clpm.js';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
7
7
|
declare function normalizeDocuments(documents: Document[]): Message | undefined;
|
|
8
8
|
declare function formatDocument(document: Document): string;
|
|
9
9
|
|
|
10
|
+
type ModelNameOf<M extends CompletionModel> = M extends CompletionModel<unknown, infer ModelName> ? ModelName : string;
|
|
11
|
+
type RawResponseOf<M extends CompletionModel> = M extends CompletionModel<infer RawResponse, infer _ModelName> ? RawResponse : unknown;
|
|
10
12
|
declare class CompletionRequestBuilder<M extends CompletionModel = CompletionModel> {
|
|
11
13
|
private readonly model;
|
|
12
14
|
private readonly promptMessage;
|
|
@@ -21,7 +23,7 @@ declare class CompletionRequestBuilder<M extends CompletionModel = CompletionMod
|
|
|
21
23
|
private params;
|
|
22
24
|
private schema;
|
|
23
25
|
constructor(model: M, promptMessage: Message);
|
|
24
|
-
modelOverride(model:
|
|
26
|
+
modelOverride(model: ModelNameOf<M> | undefined): this;
|
|
25
27
|
instructions(instructions: string | undefined): this;
|
|
26
28
|
messages(messages: Message[]): this;
|
|
27
29
|
documents(documents: Document[]): this;
|
|
@@ -31,8 +33,8 @@ declare class CompletionRequestBuilder<M extends CompletionModel = CompletionMod
|
|
|
31
33
|
toolChoice(toolChoice: ToolChoice | undefined): this;
|
|
32
34
|
additionalParams(additionalParams: JsonValue | undefined): this;
|
|
33
35
|
outputSchema(outputSchema: JsonObject | undefined): this;
|
|
34
|
-
build(): CompletionRequest
|
|
35
|
-
send(): Promise<CompletionResponse
|
|
36
|
+
build(): CompletionRequest<ModelNameOf<M>>;
|
|
37
|
+
send(): Promise<CompletionResponse<RawResponseOf<M>>>;
|
|
36
38
|
private buildInstructions;
|
|
37
39
|
}
|
|
38
40
|
|
package/dist/completion/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
formatDocument,
|
|
7
7
|
isStreamingCompletionModel,
|
|
8
8
|
normalizeDocuments
|
|
9
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-SRBW3LD7.js";
|
|
10
10
|
import {
|
|
11
11
|
AssistantContent,
|
|
12
12
|
CompletionCapabilityError,
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
reasoningDisplayText,
|
|
20
20
|
serializeToolResultOutput,
|
|
21
21
|
textFromAssistantContent
|
|
22
|
-
} from "../chunk-
|
|
22
|
+
} from "../chunk-5LFGEMIW.js";
|
|
23
23
|
import "../chunk-WQKHFADH.js";
|
|
24
24
|
export {
|
|
25
25
|
AssistantContent,
|