-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathverifier.ts
More file actions
133 lines (119 loc) · 4.15 KB
/
Copy pathverifier.ts
File metadata and controls
133 lines (119 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* QWED Open Responses - Response Verifier
*/
import { BaseGuard } from './guards';
import { VerificationResult, GuardResult, ParsedResponse } from './types';
// Re-export types
export { VerificationResult, GuardResult };
/**
* Main verifier for AI responses.
*/
export class ResponseVerifier {
private defaultGuards: BaseGuard[];
private strictMode: boolean;
constructor(guards: BaseGuard[] = [], options: { strictMode?: boolean } = {}) {
this.defaultGuards = guards;
this.strictMode = options.strictMode ?? true;
}
/**
* Verify a response against guards.
*/
verify(
response: any,
guards?: BaseGuard[],
context?: Record<string, any>
): VerificationResult {
const guardsToUse = guards ?? this.defaultGuards;
const parsedResponse = this.parseResponse(response);
// Fail-closed: zero guards must never produce verified=true (#27).
if (guardsToUse.length === 0) {
return {
verified: false,
response: parsedResponse,
guardsPassed: 0,
guardsFailed: 0,
guardResults: [{
guardName: 'ResponseVerifier',
passed: false,
message: 'No guards configured — verification cannot be performed. Pass at least one guard or set defaultGuards.',
severity: 'error',
}],
blocked: this.strictMode,
blockReason: 'No guards configured — fail-closed (zero-guard verify).',
timestamp: new Date().toISOString(),
};
}
const guardResults: GuardResult[] = [];
let guardsPassed = 0;
let guardsFailed = 0;
let blocked = false;
let blockReason: string | undefined;
for (const guard of guardsToUse) {
try {
const result = guard.check(parsedResponse, context);
guardResults.push(result);
if (result.passed) {
guardsPassed++;
} else {
guardsFailed++;
if (result.severity === 'error' && this.strictMode) {
blocked = true;
blockReason = result.message;
}
}
} catch (error) {
guardResults.push({
guardName: guard.name,
passed: false,
message: `Guard error: ${error instanceof Error ? error.message : String(error)}`,
severity: 'error',
});
guardsFailed++;
}
}
return {
verified: guardsFailed === 0,
response: parsedResponse,
guardsPassed,
guardsFailed,
guardResults,
blocked,
blockReason,
timestamp: new Date().toISOString(),
};
}
/**
* Verify a tool call.
*/
verifyToolCall(
toolName: string,
args: Record<string, any>,
guards?: BaseGuard[]
): VerificationResult {
const toolCall = {
type: 'tool_call',
toolName,
arguments: args,
};
return this.verify(toolCall, guards);
}
private parseResponse(response: any): ParsedResponse {
// Parse strictness mirrors Python _parse_response (#30): Python
// raises ValueError for non-dict/scalar inputs — npm must reject
// them too, not wrap them as {type:'unknown'} and verify them.
if (response !== null && typeof response === 'object' && !Array.isArray(response)) {
return response;
}
if (typeof response === 'string') {
try {
return JSON.parse(response);
} catch {
return { type: 'text', content: response };
}
}
const typeName = response === null ? 'null' : Array.isArray(response) ? 'list' : typeof response;
throw new Error(
`Cannot parse response of type ${typeName}. Expected object, string, or JSON.`
);
}
}