@framers/agentos 0.1.1 → 0.1.3
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/README.md +273 -223
- package/dist/cognitive_substrate/personas/definitions/atlas_systems_architect.json +2 -2
- package/dist/core/guardrails/ICrossAgentGuardrailService.d.ts +180 -0
- package/dist/core/guardrails/ICrossAgentGuardrailService.d.ts.map +1 -0
- package/dist/core/guardrails/ICrossAgentGuardrailService.js +26 -0
- package/dist/core/guardrails/ICrossAgentGuardrailService.js.map +1 -0
- package/dist/core/guardrails/IGuardrailService.d.ts +233 -42
- package/dist/core/guardrails/IGuardrailService.d.ts.map +1 -1
- package/dist/core/guardrails/IGuardrailService.js +34 -4
- package/dist/core/guardrails/IGuardrailService.js.map +1 -1
- package/dist/core/guardrails/crossAgentGuardrailDispatcher.d.ts +119 -0
- package/dist/core/guardrails/crossAgentGuardrailDispatcher.d.ts.map +1 -0
- package/dist/core/guardrails/crossAgentGuardrailDispatcher.js +221 -0
- package/dist/core/guardrails/crossAgentGuardrailDispatcher.js.map +1 -0
- package/dist/core/guardrails/guardrailDispatcher.d.ts +97 -9
- package/dist/core/guardrails/guardrailDispatcher.d.ts.map +1 -1
- package/dist/core/guardrails/guardrailDispatcher.js +119 -9
- package/dist/core/guardrails/guardrailDispatcher.js.map +1 -1
- package/dist/core/guardrails/index.d.ts +52 -0
- package/dist/core/guardrails/index.d.ts.map +1 -0
- package/dist/core/guardrails/index.js +56 -0
- package/dist/core/guardrails/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +84 -84
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module crossAgentGuardrailDispatcher
|
|
3
|
+
*
|
|
4
|
+
* Dispatcher for cross-agent guardrail evaluations.
|
|
5
|
+
*
|
|
6
|
+
* Enables supervisor/observer guardrails to monitor and intervene in
|
|
7
|
+
* other agents' output streams within an agency.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // Wrap an agent's output with cross-agent guardrail supervision
|
|
12
|
+
* const supervisedStream = wrapWithCrossAgentGuardrails(
|
|
13
|
+
* crossAgentGuardrails,
|
|
14
|
+
* { sourceAgentId: 'worker-1', observerAgentId: 'supervisor', agencyId: 'agency-1' },
|
|
15
|
+
* guardrailContext,
|
|
16
|
+
* workerOutputStream,
|
|
17
|
+
* { streamId: 'stream-123' }
|
|
18
|
+
* );
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { AgentOSResponseChunkType } from '../../api/types/AgentOSResponse.js';
|
|
22
|
+
import { GuardrailAction, } from './IGuardrailService.js';
|
|
23
|
+
import { isCrossAgentGuardrail, shouldObserveAgent, } from './ICrossAgentGuardrailService.js';
|
|
24
|
+
import { createGuardrailBlockedStream } from './guardrailDispatcher.js';
|
|
25
|
+
/**
|
|
26
|
+
* Evaluate a chunk through all applicable cross-agent guardrails.
|
|
27
|
+
*
|
|
28
|
+
* Filters guardrails to only those observing the source agent, then
|
|
29
|
+
* evaluates the chunk through each. Respects `canInterruptOthers` flag.
|
|
30
|
+
*
|
|
31
|
+
* @param guardrails - Cross-agent guardrails to evaluate
|
|
32
|
+
* @param crossAgentContext - Source/observer agent context
|
|
33
|
+
* @param guardrailContext - Standard guardrail context
|
|
34
|
+
* @param chunk - The output chunk to evaluate
|
|
35
|
+
* @returns Evaluation result with blocked status and any modifications
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```typescript
|
|
39
|
+
* const result = await evaluateCrossAgentGuardrails(
|
|
40
|
+
* crossAgentGuardrails,
|
|
41
|
+
* { sourceAgentId: 'worker-1', observerAgentId: 'supervisor' },
|
|
42
|
+
* guardrailContext,
|
|
43
|
+
* textDeltaChunk
|
|
44
|
+
* );
|
|
45
|
+
*
|
|
46
|
+
* if (result.blocked) {
|
|
47
|
+
* // Terminate the source agent's stream
|
|
48
|
+
* } else if (result.modifiedChunk) {
|
|
49
|
+
* // Use the sanitized chunk
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export async function evaluateCrossAgentGuardrails(guardrails, crossAgentContext, guardrailContext, chunk) {
|
|
54
|
+
const evaluations = [];
|
|
55
|
+
let currentChunk = chunk;
|
|
56
|
+
let blocked = false;
|
|
57
|
+
// Filter to guardrails that should observe this agent
|
|
58
|
+
const applicableGuardrails = guardrails.filter((g) => shouldObserveAgent(g, crossAgentContext.sourceAgentId));
|
|
59
|
+
for (const guardrail of applicableGuardrails) {
|
|
60
|
+
// Skip if no cross-agent evaluation method
|
|
61
|
+
if (typeof guardrail.evaluateCrossAgentOutput !== 'function') {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
// Check if this guardrail evaluates streaming chunks
|
|
65
|
+
const isStreamingChunk = currentChunk.type === AgentOSResponseChunkType.TEXT_DELTA && !currentChunk.isFinal;
|
|
66
|
+
if (isStreamingChunk && !guardrail.config?.evaluateStreamingChunks) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
// Build payload
|
|
70
|
+
const payload = {
|
|
71
|
+
sourceAgentId: crossAgentContext.sourceAgentId,
|
|
72
|
+
observerAgentId: crossAgentContext.observerAgentId,
|
|
73
|
+
agencyId: crossAgentContext.agencyId,
|
|
74
|
+
chunk: currentChunk,
|
|
75
|
+
context: guardrailContext,
|
|
76
|
+
};
|
|
77
|
+
let evaluation = null;
|
|
78
|
+
try {
|
|
79
|
+
evaluation = await guardrail.evaluateCrossAgentOutput(payload);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.warn('[AgentOS][CrossAgentGuardrails] evaluateCrossAgentOutput failed.', error);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (!evaluation) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
// Downgrade BLOCK/SANITIZE to FLAG if canInterruptOthers is false
|
|
89
|
+
if (!guardrail.canInterruptOthers) {
|
|
90
|
+
if (evaluation.action === GuardrailAction.BLOCK ||
|
|
91
|
+
evaluation.action === GuardrailAction.SANITIZE) {
|
|
92
|
+
evaluation = {
|
|
93
|
+
...evaluation,
|
|
94
|
+
action: GuardrailAction.FLAG,
|
|
95
|
+
metadata: {
|
|
96
|
+
...evaluation.metadata,
|
|
97
|
+
originalAction: evaluation.action,
|
|
98
|
+
downgraded: true,
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
evaluations.push(evaluation);
|
|
104
|
+
// Handle actions
|
|
105
|
+
if (evaluation.action === GuardrailAction.BLOCK) {
|
|
106
|
+
blocked = true;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
if (evaluation.action === GuardrailAction.SANITIZE &&
|
|
110
|
+
evaluation.modifiedText !== undefined) {
|
|
111
|
+
// Modify the chunk based on type
|
|
112
|
+
if (currentChunk.type === AgentOSResponseChunkType.TEXT_DELTA) {
|
|
113
|
+
currentChunk = {
|
|
114
|
+
...currentChunk,
|
|
115
|
+
textDelta: evaluation.modifiedText,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
else if (currentChunk.type === AgentOSResponseChunkType.FINAL_RESPONSE) {
|
|
119
|
+
currentChunk = {
|
|
120
|
+
...currentChunk,
|
|
121
|
+
finalResponseText: evaluation.modifiedText,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
blocked,
|
|
128
|
+
modifiedChunk: currentChunk !== chunk ? currentChunk : undefined,
|
|
129
|
+
evaluations,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Wrap an agent's output stream with cross-agent guardrail supervision.
|
|
134
|
+
*
|
|
135
|
+
* Creates an async generator that evaluates each chunk through applicable
|
|
136
|
+
* cross-agent guardrails before yielding. If any guardrail returns BLOCK
|
|
137
|
+
* (and has `canInterruptOthers: true`), the stream is terminated.
|
|
138
|
+
*
|
|
139
|
+
* @param guardrails - Cross-agent guardrails to apply
|
|
140
|
+
* @param crossAgentContext - Source/observer agent context
|
|
141
|
+
* @param guardrailContext - Standard guardrail context
|
|
142
|
+
* @param stream - Source agent's output stream
|
|
143
|
+
* @param options - Stream options
|
|
144
|
+
* @returns Supervised stream with cross-agent guardrail filtering
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* // Supervisor monitors worker agent
|
|
149
|
+
* const supervisedStream = wrapWithCrossAgentGuardrails(
|
|
150
|
+
* [qualityGate, policyEnforcer],
|
|
151
|
+
* {
|
|
152
|
+
* sourceAgentId: 'worker-analyst',
|
|
153
|
+
* observerAgentId: 'supervisor',
|
|
154
|
+
* agencyId: 'research-agency'
|
|
155
|
+
* },
|
|
156
|
+
* guardrailContext,
|
|
157
|
+
* workerStream,
|
|
158
|
+
* { streamId: 'stream-xyz' }
|
|
159
|
+
* );
|
|
160
|
+
*
|
|
161
|
+
* for await (const chunk of supervisedStream) {
|
|
162
|
+
* // Chunk has been approved/modified by cross-agent guardrails
|
|
163
|
+
* yield chunk;
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export async function* wrapWithCrossAgentGuardrails(guardrails, crossAgentContext, guardrailContext, stream, options) {
|
|
168
|
+
// Filter to applicable guardrails once
|
|
169
|
+
const applicableGuardrails = guardrails.filter((g) => shouldObserveAgent(g, crossAgentContext.sourceAgentId));
|
|
170
|
+
// If no applicable guardrails, pass through unchanged
|
|
171
|
+
if (applicableGuardrails.length === 0) {
|
|
172
|
+
yield* stream;
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
for await (const chunk of stream) {
|
|
176
|
+
const result = await evaluateCrossAgentGuardrails(applicableGuardrails, crossAgentContext, guardrailContext, chunk);
|
|
177
|
+
if (result.blocked) {
|
|
178
|
+
// Find the blocking evaluation for the error message
|
|
179
|
+
const blockingEval = result.evaluations.find((e) => e.action === GuardrailAction.BLOCK);
|
|
180
|
+
if (blockingEval) {
|
|
181
|
+
yield* createGuardrailBlockedStream(guardrailContext, blockingEval, options);
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
// Yield the (potentially modified) chunk
|
|
186
|
+
const outputChunk = result.modifiedChunk ?? chunk;
|
|
187
|
+
// Attach cross-agent evaluation metadata
|
|
188
|
+
if (result.evaluations.length > 0) {
|
|
189
|
+
const metadata = outputChunk.metadata ?? {};
|
|
190
|
+
yield {
|
|
191
|
+
...outputChunk,
|
|
192
|
+
metadata: {
|
|
193
|
+
...metadata,
|
|
194
|
+
crossAgentGuardrail: {
|
|
195
|
+
evaluations: result.evaluations.map((e) => ({
|
|
196
|
+
action: e.action,
|
|
197
|
+
reason: e.reason,
|
|
198
|
+
reasonCode: e.reasonCode,
|
|
199
|
+
metadata: e.metadata,
|
|
200
|
+
})),
|
|
201
|
+
sourceAgentId: crossAgentContext.sourceAgentId,
|
|
202
|
+
observerAgentId: crossAgentContext.observerAgentId,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
yield outputChunk;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Extract cross-agent guardrails from a mixed array of guardrail services.
|
|
214
|
+
*
|
|
215
|
+
* @param services - Array of guardrail services (may include non-cross-agent)
|
|
216
|
+
* @returns Only the cross-agent guardrail services
|
|
217
|
+
*/
|
|
218
|
+
export function filterCrossAgentGuardrails(services) {
|
|
219
|
+
return services.filter((s) => s != null && typeof s === 'object' && isCrossAgentGuardrail(s));
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=crossAgentGuardrailDispatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crossAgentGuardrailDispatcher.js","sourceRoot":"","sources":["../../../src/core/guardrails/crossAgentGuardrailDispatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAmB,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC5F,OAAO,EACL,eAAe,GAGhB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAGL,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,4BAA4B,EAA+B,MAAM,uBAAuB,CAAC;AA8BlG;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,UAAyC,EACzC,iBAA6C,EAC7C,gBAAkC,EAClC,KAAsB;IAEtB,MAAM,WAAW,GAAgC,EAAE,CAAC;IACpD,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,sDAAsD;IACtD,MAAM,oBAAoB,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CACnD,kBAAkB,CAAC,CAAC,EAAE,iBAAiB,CAAC,aAAa,CAAC,CACvD,CAAC;IAEF,KAAK,MAAM,SAAS,IAAI,oBAAoB,EAAE,CAAC;QAC7C,2CAA2C;QAC3C,IAAI,OAAO,SAAS,CAAC,wBAAwB,KAAK,UAAU,EAAE,CAAC;YAC7D,SAAS;QACX,CAAC;QAED,qDAAqD;QACrD,MAAM,gBAAgB,GACpB,YAAY,CAAC,IAAI,KAAK,wBAAwB,CAAC,UAAU,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC;QACrF,IAAI,gBAAgB,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,uBAAuB,EAAE,CAAC;YACnE,SAAS;QACX,CAAC;QAED,gBAAgB;QAChB,MAAM,OAAO,GAA4B;YACvC,aAAa,EAAE,iBAAiB,CAAC,aAAa;YAC9C,eAAe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ponse } from '../../api/types/AgentOSResponse';
|
|
3
3
|
import { type GuardrailContext, type GuardrailEvaluationResult, type IGuardrailService } from './IGuardrailService';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Result of running input guardrails.
|
|
6
|
+
*
|
|
7
|
+
* Contains the potentially modified input and all evaluation results.
|
|
8
|
+
* Check `evaluation.action` to determine if processing should continue.
|
|
6
9
|
*/
|
|
7
10
|
export interface GuardrailInputOutcome {
|
|
11
|
+
/** Input after all sanitization (may be modified from original) */
|
|
8
12
|
sanitizedInput: AgentOSInput;
|
|
13
|
+
/** The last evaluation result (for backwards compatibility) */
|
|
9
14
|
evaluation?: GuardrailEvaluationResult | null;
|
|
15
|
+
/** All evaluation results from all guardrails */
|
|
10
16
|
evaluations?: GuardrailEvaluationResult[];
|
|
11
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Options for output guardrail wrapping.
|
|
20
|
+
*/
|
|
12
21
|
export interface GuardrailOutputOptions {
|
|
22
|
+
/** Stream identifier for error chunks */
|
|
13
23
|
streamId: string;
|
|
24
|
+
/** Persona ID for error chunks */
|
|
14
25
|
personaId?: string;
|
|
26
|
+
/** Input evaluations to attach to first output chunk */
|
|
15
27
|
inputEvaluations?: GuardrailEvaluationResult[] | null;
|
|
16
28
|
}
|
|
17
29
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
30
|
+
* Evaluate user input through all registered guardrails.
|
|
31
|
+
*
|
|
32
|
+
* Runs guardrails in sequence, allowing each to modify or block the input.
|
|
33
|
+
* If any guardrail returns {@link GuardrailAction.BLOCK}, evaluation stops
|
|
34
|
+
* immediately and the blocked result is returned.
|
|
35
|
+
*
|
|
36
|
+
* @param service - Single guardrail or array of guardrails to evaluate
|
|
37
|
+
* @param input - User input to evaluate
|
|
38
|
+
* @param context - Conversation context for policy decisions
|
|
39
|
+
* @returns Outcome containing sanitized input and all evaluations
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* const outcome = await evaluateInputGuardrails(
|
|
44
|
+
* [contentFilter, piiRedactor],
|
|
45
|
+
* userInput,
|
|
46
|
+
* { userId: 'user-123', sessionId: 'session-abc' }
|
|
47
|
+
* );
|
|
48
|
+
*
|
|
49
|
+
* if (outcome.evaluation?.action === GuardrailAction.BLOCK) {
|
|
50
|
+
* // Input was blocked - return error stream
|
|
51
|
+
* yield* createGuardrailBlockedStream(context, outcome.evaluation);
|
|
52
|
+
* return;
|
|
53
|
+
* }
|
|
54
|
+
*
|
|
55
|
+
* // Use sanitized input for orchestration
|
|
56
|
+
* const cleanInput = outcome.sanitizedInput;
|
|
57
|
+
* ```
|
|
20
58
|
*/
|
|
21
59
|
export declare function evaluateInputGuardrails(service: IGuardrailService | IGuardrailService[] | undefined, input: AgentOSInput, context: GuardrailContext): Promise<GuardrailInputOutcome>;
|
|
22
60
|
/**
|
|
23
|
-
*
|
|
61
|
+
* Create a stream that emits a single error chunk for blocked content.
|
|
62
|
+
*
|
|
63
|
+
* Use this when input evaluation returns {@link GuardrailAction.BLOCK}
|
|
64
|
+
* to generate an appropriate error response without invoking orchestration.
|
|
65
|
+
*
|
|
66
|
+
* @param context - Guardrail context for the error details
|
|
67
|
+
* @param evaluation - The blocking evaluation result
|
|
68
|
+
* @param options - Stream options (streamId, personaId)
|
|
69
|
+
* @returns Async generator yielding a single ERROR chunk
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* if (outcome.evaluation?.action === GuardrailAction.BLOCK) {
|
|
74
|
+
* yield* createGuardrailBlockedStream(
|
|
75
|
+
* guardrailContext,
|
|
76
|
+
* outcome.evaluation,
|
|
77
|
+
* { streamId: 'stream-123', personaId: 'support-agent' }
|
|
78
|
+
* );
|
|
79
|
+
* return;
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
24
82
|
*/
|
|
25
83
|
export declare function createGuardrailBlockedStream(context: GuardrailContext, evaluation: GuardrailEvaluationResult, options?: GuardrailOutputOptions): AsyncGenerator<AgentOSResponse, void, undefined>;
|
|
26
84
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
85
|
+
* Wrap a response stream with guardrail filtering.
|
|
86
|
+
*
|
|
87
|
+
* Creates an async generator that evaluates each chunk through registered
|
|
88
|
+
* guardrails before yielding to the client. Supports both real-time streaming
|
|
89
|
+
* evaluation and final-only evaluation based on guardrail configuration.
|
|
29
90
|
*
|
|
30
91
|
* **Evaluation Strategy:**
|
|
31
|
-
* - Guardrails with `config.evaluateStreamingChunks === true` evaluate TEXT_DELTA chunks
|
|
32
|
-
* - All guardrails evaluate FINAL_RESPONSE chunks (final check)
|
|
33
|
-
* -
|
|
92
|
+
* - Guardrails with `config.evaluateStreamingChunks === true` evaluate TEXT_DELTA chunks
|
|
93
|
+
* - All guardrails evaluate FINAL_RESPONSE chunks (final safety check)
|
|
94
|
+
* - Rate limiting via `config.maxStreamingEvaluations` per guardrail
|
|
95
|
+
*
|
|
96
|
+
* **Actions:**
|
|
97
|
+
* - {@link GuardrailAction.BLOCK} - Terminates stream immediately with error chunk
|
|
98
|
+
* - {@link GuardrailAction.SANITIZE} - Replaces chunk content with `modifiedText`
|
|
99
|
+
* - {@link GuardrailAction.FLAG} / {@link GuardrailAction.ALLOW} - Passes through
|
|
100
|
+
*
|
|
101
|
+
* @param service - Single guardrail or array of guardrails
|
|
102
|
+
* @param context - Conversation context for policy decisions
|
|
103
|
+
* @param stream - Source response stream to wrap
|
|
104
|
+
* @param options - Stream options and input evaluations to attach
|
|
105
|
+
* @returns Wrapped stream with guardrail filtering applied
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```typescript
|
|
109
|
+
* // Wrap output stream with PII redaction
|
|
110
|
+
* const safeStream = wrapOutputGuardrails(
|
|
111
|
+
* [piiRedactor, contentFilter],
|
|
112
|
+
* guardrailContext,
|
|
113
|
+
* orchestratorStream,
|
|
114
|
+
* { streamId: 'stream-123', inputEvaluations }
|
|
115
|
+
* );
|
|
116
|
+
*
|
|
117
|
+
* for await (const chunk of safeStream) {
|
|
118
|
+
* // Chunks are filtered/sanitized before reaching here
|
|
119
|
+
* yield chunk;
|
|
120
|
+
* }
|
|
121
|
+
* ```
|
|
34
122
|
*/
|
|
35
123
|
export declare function wrapOutputGuardrails(service: IGuardrailService | IGuardrailService[] | undefined, context: GuardrailContext, stream: AsyncGenerator<AgentOSResponse, void, undefined>, options: GuardrailOutputOptions): AsyncGenerator<AgentOSResponse, void, undefined>;
|
|
36
124
|
//# sourceMappingURL=guardrailDispatcher.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guardrailDispatcher.d.ts","sourceRoot":"","sources":["../../../src/core/guardrails/guardrailDispatcher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"guardrailDispatcher.d.ts","sourceRoot":"","sources":["../../../src/core/guardrails/guardrailDispatcher.ts"],"names":[],"mappings":"AAgCA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EACL,eAAe,EAIhB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,yBAAyB,EAC9B,KAAK,iBAAiB,EACvB,MAAM,qBAAqB,CAAC;AAc7B;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC,mEAAmE;IACnE,cAAc,EAAE,YAAY,CAAC;IAE7B,+DAA+D;IAC/D,UAAU,CAAC,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAE9C,iDAAiD;IACjD,WAAW,CAAC,EAAE,yBAAyB,EAAE,CAAC;CAC3C;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IAEjB,kCAAkC;IAClC,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB,wDAAwD;IACxD,gBAAgB,CAAC,EAAE,yBAAyB,EAAE,GAAG,IAAI,CAAC;CACvD;AAaD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,uBAAuB,CAC3C,OAAO,EAAE,iBAAiB,GAAG,iBAAiB,EAAE,GAAG,SAAS,EAC5D,KAAK,EAAE,YAAY,EACnB,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,qBAAqB,CAAC,CAmDhC;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAuB,4BAA4B,CACjD,OAAO,EAAE,gBAAgB,EACzB,UAAU,EAAE,yBAAyB,EACrC,OAAO,CAAC,EAAE,sBAAsB,GAC/B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,CAkBlD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAuB,oBAAoB,CACzC,OAAO,EAAE,iBAAiB,GAAG,iBAAiB,EAAE,GAAG,SAAS,EAC5D,OAAO,EAAE,gBAAgB,EACzB,MAAM,EAAE,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,EACxD,OAAO,EAAE,sBAAsB,GAC9B,cAAc,CAAC,eAAe,EAAE,IAAI,EAAE,SAAS,CAAC,CA8IlD"}
|
|
@@ -1,13 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module guardrailDispatcher
|
|
3
|
+
*
|
|
4
|
+
* Dispatches guardrail evaluations for input and output processing.
|
|
5
|
+
*
|
|
6
|
+
* This module provides two main functions:
|
|
7
|
+
* - {@link evaluateInputGuardrails} - Evaluate user input before orchestration
|
|
8
|
+
* - {@link wrapOutputGuardrails} - Wrap output stream with guardrail filtering
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Input evaluation
|
|
13
|
+
* const outcome = await evaluateInputGuardrails(
|
|
14
|
+
* guardrailServices,
|
|
15
|
+
* userInput,
|
|
16
|
+
* guardrailContext
|
|
17
|
+
* );
|
|
18
|
+
*
|
|
19
|
+
* if (outcome.evaluation?.action === GuardrailAction.BLOCK) {
|
|
20
|
+
* return createGuardrailBlockedStream(context, outcome.evaluation);
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* // Output wrapping
|
|
24
|
+
* const safeStream = wrapOutputGuardrails(
|
|
25
|
+
* guardrailServices,
|
|
26
|
+
* guardrailContext,
|
|
27
|
+
* outputStream,
|
|
28
|
+
* { streamId, personaId }
|
|
29
|
+
* );
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
1
32
|
import { uuidv4 } from '../../utils/uuid.js';
|
|
2
33
|
import { AgentOSResponseChunkType, } from '../../api/types/AgentOSResponse.js';
|
|
3
34
|
import { GuardrailAction, } from './IGuardrailService.js';
|
|
4
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Type guard to check if a guardrail service implements evaluateOutput.
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
5
39
|
function hasEvaluateOutput(svc) {
|
|
6
40
|
return typeof svc.evaluateOutput === 'function';
|
|
7
41
|
}
|
|
8
42
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
43
|
+
* Evaluate user input through all registered guardrails.
|
|
44
|
+
*
|
|
45
|
+
* Runs guardrails in sequence, allowing each to modify or block the input.
|
|
46
|
+
* If any guardrail returns {@link GuardrailAction.BLOCK}, evaluation stops
|
|
47
|
+
* immediately and the blocked result is returned.
|
|
48
|
+
*
|
|
49
|
+
* @param service - Single guardrail or array of guardrails to evaluate
|
|
50
|
+
* @param input - User input to evaluate
|
|
51
|
+
* @param context - Conversation context for policy decisions
|
|
52
|
+
* @returns Outcome containing sanitized input and all evaluations
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const outcome = await evaluateInputGuardrails(
|
|
57
|
+
* [contentFilter, piiRedactor],
|
|
58
|
+
* userInput,
|
|
59
|
+
* { userId: 'user-123', sessionId: 'session-abc' }
|
|
60
|
+
* );
|
|
61
|
+
*
|
|
62
|
+
* if (outcome.evaluation?.action === GuardrailAction.BLOCK) {
|
|
63
|
+
* // Input was blocked - return error stream
|
|
64
|
+
* yield* createGuardrailBlockedStream(context, outcome.evaluation);
|
|
65
|
+
* return;
|
|
66
|
+
* }
|
|
67
|
+
*
|
|
68
|
+
* // Use sanitized input for orchestration
|
|
69
|
+
* const cleanInput = outcome.sanitizedInput;
|
|
70
|
+
* ```
|
|
11
71
|
*/
|
|
12
72
|
export async function evaluateInputGuardrails(service, input, context) {
|
|
13
73
|
const services = Array.isArray(service)
|
|
@@ -54,7 +114,27 @@ export async function evaluateInputGuardrails(service, input, context) {
|
|
|
54
114
|
};
|
|
55
115
|
}
|
|
56
116
|
/**
|
|
57
|
-
*
|
|
117
|
+
* Create a stream that emits a single error chunk for blocked content.
|
|
118
|
+
*
|
|
119
|
+
* Use this when input evaluation returns {@link GuardrailAction.BLOCK}
|
|
120
|
+
* to generate an appropriate error response without invoking orchestration.
|
|
121
|
+
*
|
|
122
|
+
* @param context - Guardrail context for the error details
|
|
123
|
+
* @param evaluation - The blocking evaluation result
|
|
124
|
+
* @param options - Stream options (streamId, personaId)
|
|
125
|
+
* @returns Async generator yielding a single ERROR chunk
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```typescript
|
|
129
|
+
* if (outcome.evaluation?.action === GuardrailAction.BLOCK) {
|
|
130
|
+
* yield* createGuardrailBlockedStream(
|
|
131
|
+
* guardrailContext,
|
|
132
|
+
* outcome.evaluation,
|
|
133
|
+
* { streamId: 'stream-123', personaId: 'support-agent' }
|
|
134
|
+
* );
|
|
135
|
+
* return;
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
58
138
|
*/
|
|
59
139
|
export async function* createGuardrailBlockedStream(context, evaluation, options) {
|
|
60
140
|
const streamId = options?.streamId ?? uuidv4();
|
|
@@ -76,13 +156,43 @@ export async function* createGuardrailBlockedStream(context, evaluation, options
|
|
|
76
156
|
yield errorChunk;
|
|
77
157
|
}
|
|
78
158
|
/**
|
|
79
|
-
*
|
|
80
|
-
*
|
|
159
|
+
* Wrap a response stream with guardrail filtering.
|
|
160
|
+
*
|
|
161
|
+
* Creates an async generator that evaluates each chunk through registered
|
|
162
|
+
* guardrails before yielding to the client. Supports both real-time streaming
|
|
163
|
+
* evaluation and final-only evaluation based on guardrail configuration.
|
|
81
164
|
*
|
|
82
165
|
* **Evaluation Strategy:**
|
|
83
|
-
* - Guardrails with `config.evaluateStreamingChunks === true` evaluate TEXT_DELTA chunks
|
|
84
|
-
* - All guardrails evaluate FINAL_RESPONSE chunks (final check)
|
|
85
|
-
* -
|
|
166
|
+
* - Guardrails with `config.evaluateStreamingChunks === true` evaluate TEXT_DELTA chunks
|
|
167
|
+
* - All guardrails evaluate FINAL_RESPONSE chunks (final safety check)
|
|
168
|
+
* - Rate limiting via `config.maxStreamingEvaluations` per guardrail
|
|
169
|
+
*
|
|
170
|
+
* **Actions:**
|
|
171
|
+
* - {@link GuardrailAction.BLOCK} - Terminates stream immediately with error chunk
|
|
172
|
+
* - {@link GuardrailAction.SANITIZE} - Replaces chunk content with `modifiedText`
|
|
173
|
+
* - {@link GuardrailAction.FLAG} / {@link GuardrailAction.ALLOW} - Passes through
|
|
174
|
+
*
|
|
175
|
+
* @param service - Single guardrail or array of guardrails
|
|
176
|
+
* @param context - Conversation context for policy decisions
|
|
177
|
+
* @param stream - Source response stream to wrap
|
|
178
|
+
* @param options - Stream options and input evaluations to attach
|
|
179
|
+
* @returns Wrapped stream with guardrail filtering applied
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* // Wrap output stream with PII redaction
|
|
184
|
+
* const safeStream = wrapOutputGuardrails(
|
|
185
|
+
* [piiRedactor, contentFilter],
|
|
186
|
+
* guardrailContext,
|
|
187
|
+
* orchestratorStream,
|
|
188
|
+
* { streamId: 'stream-123', inputEvaluations }
|
|
189
|
+
* );
|
|
190
|
+
*
|
|
191
|
+
* for await (const chunk of safeStream) {
|
|
192
|
+
* // Chunks are filtered/sanitized before reaching here
|
|
193
|
+
* yield chunk;
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
86
196
|
*/
|
|
87
197
|
export async function* wrapOutputGuardrails(service, context, stream, options) {
|
|
88
198
|
const services = Array.isArray(service)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guardrailDispatcher.js","sourceRoot":"","sources":["../../../src/core/guardrails/guardrailDispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AAErD,OAAO,EAEL,wBAAwB,GAGzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,eAAe,GAIhB,MAAM,qBAAqB,CAAC;AAE7B
|
|
1
|
+
{"version":3,"file":"guardrailDispatcher.js","sourceRoot":"","sources":["../../../src/core/guardrails/guardrailDispatcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AAErD,OAAO,EAEL,wBAAwB,GAGzB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,eAAe,GAIhB,MAAM,qBAAqB,CAAC;AAE7B;;;GAGG;AACH,SAAS,iBAAiB,CACxB,GAAsB;IAItB,OAAO,OAAQ,GAAyB,CAAC,cAAc,KAAK,UAAU,CAAC;AACzE,CAAC;AA4CD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,OAA4D,EAC5D,KAAmB,EACnB,OAAyB;IAEzB,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QACrC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC;QACzB,CAAC,CAAC,OAAO;YACT,CAAC,CAAC,CAAC,OAAO,CAAC;YACX,CAAC,CAAC,EAAE,CAAC;IAEP,IAAI,QAAQ,CAAC,MAAM,KAAK,C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iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACrD,MAAM,cAAc,GAAG,iBAAiB,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAEvD,MAAM,WAAW,GAAG,aAAa,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IACxD,MAAM,YAAY,GAAG,cAAc,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;IAE3D,MAAM,SAAS,GAA4B;QACzC,GAAG,iBAAiB;QACpB,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrD,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KACzD,CAAC;IAEF,OAAO;QACL,GAAG,KAAK;QACR,QAAQ,EAAE;YACR,GAAG,gBAAgB;YACnB,SAAS;SACV;KACF,CAAC;AACJ,CAAC;AAED,SAAS,iBAAiB,CACxB,KAAyD;IAEzD,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module guardrails
|
|
3
|
+
*
|
|
4
|
+
* Guardrails system for content safety and policy enforcement.
|
|
5
|
+
*
|
|
6
|
+
* Guardrails intercept content at two points:
|
|
7
|
+
* 1. **Input** - Before user messages enter the orchestration pipeline
|
|
8
|
+
* 2. **Output** - Before agent responses are streamed to the client
|
|
9
|
+
*
|
|
10
|
+
* @example Basic guardrail
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import {
|
|
13
|
+
* IGuardrailService,
|
|
14
|
+
* GuardrailAction,
|
|
15
|
+
* type GuardrailInputPayload
|
|
16
|
+
* } from '../../core/guardrails';
|
|
17
|
+
*
|
|
18
|
+
* class ContentFilter implements IGuardrailService {
|
|
19
|
+
* async evaluateInput({ input }: GuardrailInputPayload) {
|
|
20
|
+
* if (containsProhibitedContent(input.textInput)) {
|
|
21
|
+
* return {
|
|
22
|
+
* action: GuardrailAction.BLOCK,
|
|
23
|
+
* reason: 'Content policy violation'
|
|
24
|
+
* };
|
|
25
|
+
* }
|
|
26
|
+
* return null;
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @example Cross-agent supervision
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import {
|
|
34
|
+
* ICrossAgentGuardrailService,
|
|
35
|
+
* GuardrailAction
|
|
36
|
+
* } from '../../core/guardrails';
|
|
37
|
+
*
|
|
38
|
+
* class SupervisorGuardrail implements ICrossAgentGuardrailService {
|
|
39
|
+
* observeAgentIds = ['worker-1', 'worker-2'];
|
|
40
|
+
* canInterruptOthers = true;
|
|
41
|
+
*
|
|
42
|
+
* async evaluateCrossAgentOutput({ sourceAgentId, chunk }) {
|
|
43
|
+
* // Supervise worker agents' outputs
|
|
44
|
+
* }
|
|
45
|
+
* }
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export { GuardrailAction, type GuardrailConfig, type GuardrailContext, type GuardrailEvaluationResult, type GuardrailInputPayload, type GuardrailOutputPayload, type IGuardrailService, } from './IGuardrailService';
|
|
49
|
+
export { createGuardrailBlockedStream, evaluateInputGuardrails, type GuardrailInputOutcome, type GuardrailOutputOptions, wrapOutputGuardrails, } from './guardrailDispatcher';
|
|
50
|
+
export { type CrossAgentOutputPayload, type ICrossAgentGuardrailService, isCrossAgentGuardrail, shouldObserveAgent, } from './ICrossAgentGuardrailService';
|
|
51
|
+
export { type CrossAgentEvaluationResult, type CrossAgentGuardrailContext, evaluateCrossAgentGuardrails, filterCrossAgentGuardrails, wrapWithCrossAgentGuardrails, } from './crossAgentGuardrailDispatcher';
|
|
52
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/guardrails/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAGH,OAAO,EACL,eAAe,EACf,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAC3B,KAAK,iBAAiB,GACvB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,4BAA4B,EAC5B,uBAAuB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAC3B,oBAAoB,GACrB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EACL,KAAK,uBAAuB,EAC5B,KAAK,2BAA2B,EAChC,qBAAqB,EACrB,kBAAkB,GACnB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EACL,KAAK,0BAA0B,EAC/B,KAAK,0BAA0B,EAC/B,4BAA4B,EAC5B,0BAA0B,EAC1B,4BAA4B,GAC7B,MAAM,iCAAiC,CAAC"}
|