pi-openai-toolkit 0.15.1 → 0.16.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/README.md +24 -2
- package/README.zh.md +24 -2
- package/docs/configuration.md +9 -3
- package/docs/internals.md +25 -2
- package/package.json +3 -1
- package/src/auto-mode/classifier.ts +161 -165
- package/src/auto-mode/extension.ts +691 -661
- package/src/auto-mode/model-selection-guard.ts +52 -0
- package/src/auto-mode/reviewer.ts +325 -335
- package/src/auto-mode/transcript.ts +135 -133
- package/src/context-management/manual-compact.ts +433 -0
- package/src/context-management/tools.ts +11 -2
- package/src/context-management/window-manager.ts +35 -9
- package/src/extension-runtime.ts +49 -11
- package/src/remote-v2-client.ts +108 -33
|
@@ -1,335 +1,325 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
|
|
17
|
-
type
|
|
18
|
-
type
|
|
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
|
-
const
|
|
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
|
-
candidates.push(
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
)
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
): Message {
|
|
327
|
-
return {
|
|
328
|
-
role: "toolResult",
|
|
329
|
-
toolCallId: call.id,
|
|
330
|
-
toolName: call.name,
|
|
331
|
-
content: [{ type: "text", text }],
|
|
332
|
-
isError: true,
|
|
333
|
-
timestamp: Date.now(),
|
|
334
|
-
};
|
|
335
|
-
}
|
|
1
|
+
import type {
|
|
2
|
+
AssistantMessage,
|
|
3
|
+
Context,
|
|
4
|
+
Message,
|
|
5
|
+
Tool,
|
|
6
|
+
ToolCall,
|
|
7
|
+
} from "@earendil-works/pi-ai";
|
|
8
|
+
import type { ModelRegistry } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { parseModelSpec } from "../runtime";
|
|
10
|
+
import { backfillVerdictFields, buildReviewPrompt, reviewerSystemPrompt } from "./prompt";
|
|
11
|
+
import {
|
|
12
|
+
boundReviewText,
|
|
13
|
+
type GuardianVerdict,
|
|
14
|
+
MAX_REVIEW_MODEL_KEY_CHARS,
|
|
15
|
+
type ReviewFailureCause,
|
|
16
|
+
type ReviewOutcome,
|
|
17
|
+
type RiskLevel,
|
|
18
|
+
type UserAuthorization,
|
|
19
|
+
} from "./types";
|
|
20
|
+
|
|
21
|
+
/** The reviewer needs model lookup plus whatever read-only tools we choose to give it. */
|
|
22
|
+
export type ReviewerRegistry = Pick<ModelRegistry, "find" | "complete">;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One read-only investigation tool the reviewer may call. `parameters` is passed to
|
|
26
|
+
* the provider verbatim; `execute` is ours, so the reviewer can never reach a tool
|
|
27
|
+
* that is not in this list.
|
|
28
|
+
*/
|
|
29
|
+
export type EvidenceTool = {
|
|
30
|
+
name: string;
|
|
31
|
+
description: string;
|
|
32
|
+
parameters: Tool["parameters"];
|
|
33
|
+
execute: (
|
|
34
|
+
args: Record<string, unknown>,
|
|
35
|
+
signal: AbortSignal | undefined,
|
|
36
|
+
) => Promise<string>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const RISK_LEVELS: readonly string[] = ["low", "medium", "high", "critical"];
|
|
40
|
+
const AUTHORIZATIONS: readonly string[] = ["unknown", "low", "medium", "high"];
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parse a reviewer answer. Only the outcome is required; risk, authorization, and
|
|
44
|
+
* rationale are back-filled. Both the Codex-style field names and our earlier
|
|
45
|
+
* `decision`/`reason` spelling are accepted, so an older reviewer prompt cached by
|
|
46
|
+
* a provider still yields a usable verdict.
|
|
47
|
+
*
|
|
48
|
+
* A verdict we cannot read is reported as a failure, never guessed into approval.
|
|
49
|
+
*/
|
|
50
|
+
export function parseReviewVerdict(text: string): GuardianVerdict | undefined {
|
|
51
|
+
for (const candidate of candidateJsonObjects(text)) {
|
|
52
|
+
let parsed: unknown;
|
|
53
|
+
try {
|
|
54
|
+
parsed = JSON.parse(candidate);
|
|
55
|
+
} catch {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
|
|
59
|
+
const record = parsed as Record<string, unknown>;
|
|
60
|
+
const rawOutcome = record.outcome ?? record.decision;
|
|
61
|
+
if (rawOutcome !== "allow" && rawOutcome !== "deny") continue;
|
|
62
|
+
const risk = String(record.risk_level ?? record.riskLevel ?? "").toLowerCase();
|
|
63
|
+
const authorization = String(
|
|
64
|
+
record.user_authorization ?? record.userAuthorization ?? "",
|
|
65
|
+
).toLowerCase();
|
|
66
|
+
const rationale =
|
|
67
|
+
typeof record.rationale === "string"
|
|
68
|
+
? record.rationale
|
|
69
|
+
: typeof record.reason === "string"
|
|
70
|
+
? record.reason
|
|
71
|
+
: undefined;
|
|
72
|
+
return backfillVerdictFields({
|
|
73
|
+
outcome: rawOutcome,
|
|
74
|
+
riskLevel: (RISK_LEVELS.includes(risk) ? risk : undefined) as RiskLevel | undefined,
|
|
75
|
+
userAuthorization: (AUTHORIZATIONS.includes(authorization) ? authorization : undefined) as
|
|
76
|
+
| UserAuthorization
|
|
77
|
+
| undefined,
|
|
78
|
+
rationale,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function candidateJsonObjects(text: string): string[] {
|
|
85
|
+
const candidates: string[] = [];
|
|
86
|
+
const fenced = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
87
|
+
if (fenced?.[1]) candidates.push(fenced[1].trim());
|
|
88
|
+
candidates.push(text.trim());
|
|
89
|
+
const brace = text.match(/\{[\s\S]*\}/);
|
|
90
|
+
if (brace?.[0]) candidates.push(brace[0]);
|
|
91
|
+
return candidates;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function firstTextBlock(message: AssistantMessage): string {
|
|
95
|
+
return message.content
|
|
96
|
+
.filter((block): block is { type: "text"; text: string } => block.type === "text")
|
|
97
|
+
.map((block) => block.text)
|
|
98
|
+
.join("")
|
|
99
|
+
.trim();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Pi 0.85 typed tool-call arguments as `Record<string, any>` and Pi 0.86 tightened
|
|
104
|
+
* them to the public `JsonObject`. Narrow with Pi's exported `ToolCall` instead of a
|
|
105
|
+
* handwritten shape so the guard tracks whichever contract the installed Pi declares.
|
|
106
|
+
*/
|
|
107
|
+
function toolCallBlocks(message: AssistantMessage): ToolCall[] {
|
|
108
|
+
return message.content.filter((block): block is ToolCall => block.type === "toolCall");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type ReviewAttempt<T> = {
|
|
112
|
+
value?: T;
|
|
113
|
+
errorMessage?: string;
|
|
114
|
+
timedOut: boolean;
|
|
115
|
+
cancelled: boolean;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/** One deadline owns every model and evidence operation in this review. */
|
|
119
|
+
function createReviewDeadline(timeoutMs: number, callerSignal?: AbortSignal) {
|
|
120
|
+
const deadlineAt = Date.now() + timeoutMs;
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
let timedOut = false;
|
|
123
|
+
let cancelled = false;
|
|
124
|
+
const abortFromCaller = () => {
|
|
125
|
+
if (controller.signal.aborted) return;
|
|
126
|
+
cancelled = true;
|
|
127
|
+
controller.abort();
|
|
128
|
+
};
|
|
129
|
+
const expire = () => {
|
|
130
|
+
if (controller.signal.aborted) return;
|
|
131
|
+
timedOut = true;
|
|
132
|
+
controller.abort();
|
|
133
|
+
};
|
|
134
|
+
if (callerSignal?.aborted) abortFromCaller();
|
|
135
|
+
else callerSignal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
136
|
+
const timer = setTimeout(expire, Math.max(0, timeoutMs));
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
async run<T>(operation: (signal: AbortSignal) => Promise<T>): Promise<ReviewAttempt<T>> {
|
|
140
|
+
if (Date.now() >= deadlineAt) expire();
|
|
141
|
+
if (controller.signal.aborted) return { timedOut, cancelled };
|
|
142
|
+
let onAbort!: () => void;
|
|
143
|
+
const aborted = new Promise<ReviewAttempt<T>>((resolve) => {
|
|
144
|
+
onAbort = () => resolve({ timedOut, cancelled });
|
|
145
|
+
controller.signal.addEventListener("abort", onAbort, { once: true });
|
|
146
|
+
});
|
|
147
|
+
try {
|
|
148
|
+
// Both settlement handlers stay attached after cancellation, so an
|
|
149
|
+
// uncooperative dependency cannot leak an unhandled rejection.
|
|
150
|
+
const pending = Promise.resolve().then(() => {
|
|
151
|
+
controller.signal.throwIfAborted();
|
|
152
|
+
return operation(controller.signal);
|
|
153
|
+
}).then(
|
|
154
|
+
(value): ReviewAttempt<T> => ({ value, timedOut, cancelled }),
|
|
155
|
+
(error): ReviewAttempt<T> => ({ errorMessage: error instanceof Error ? error.message : String(error), timedOut, cancelled }),
|
|
156
|
+
);
|
|
157
|
+
const result = await Promise.race([pending, aborted]);
|
|
158
|
+
if (Date.now() >= deadlineAt) expire();
|
|
159
|
+
return { ...result, timedOut, cancelled };
|
|
160
|
+
} finally {
|
|
161
|
+
controller.signal.removeEventListener("abort", onAbort);
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
dispose() {
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
callerSignal?.removeEventListener("abort", abortFromCaller);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function failure(cause: ReviewFailureCause, reason: string): ReviewOutcome {
|
|
172
|
+
return { kind: "unavailable", reason: boundReviewText(reason, 240), cause };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function outcomeFromVerdict(
|
|
176
|
+
verdict: GuardianVerdict,
|
|
177
|
+
reviewerModel: string,
|
|
178
|
+
evidenceRounds: number,
|
|
179
|
+
): ReviewOutcome {
|
|
180
|
+
return verdict.outcome === "allow"
|
|
181
|
+
? { kind: "allow", verdict, reviewerModel, evidenceRounds }
|
|
182
|
+
: { kind: "deny", verdict, reviewerModel, evidenceRounds };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Ask the reviewer model about one pending tool call.
|
|
187
|
+
*
|
|
188
|
+
* When `evidenceTools` are supplied the reviewer runs as a short bounded agent
|
|
189
|
+
* loop: it may call read-only tools to establish facts, we execute those calls
|
|
190
|
+
* ourselves, and we feed the results back. The whole exchange shares one deadline,
|
|
191
|
+
* and the final round is forced to answer without tools, so a reviewer that keeps
|
|
192
|
+
* investigating still terminates as a verdict or as an explicit failure.
|
|
193
|
+
*/
|
|
194
|
+
export async function requestToolReview(params: {
|
|
195
|
+
registry: ReviewerRegistry;
|
|
196
|
+
reviewerModelSpec: string;
|
|
197
|
+
toolName: string;
|
|
198
|
+
toolInput: unknown;
|
|
199
|
+
transcript?: string;
|
|
200
|
+
cwd?: string;
|
|
201
|
+
timeoutMs: number;
|
|
202
|
+
signal?: AbortSignal;
|
|
203
|
+
evidenceTools?: EvidenceTool[];
|
|
204
|
+
maxEvidenceRounds?: number;
|
|
205
|
+
}): Promise<ReviewOutcome> {
|
|
206
|
+
const parsed = parseModelSpec(params.reviewerModelSpec);
|
|
207
|
+
if (!parsed) {
|
|
208
|
+
return failure(
|
|
209
|
+
"not-configured",
|
|
210
|
+
`autoMode.reviewerModel "${params.reviewerModelSpec}" is not provider/model-id.`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const model = params.registry.find(parsed.provider, parsed.modelId);
|
|
215
|
+
if (!model) {
|
|
216
|
+
return failure("not-configured", `Reviewer model ${params.reviewerModelSpec} is not available.`);
|
|
217
|
+
}
|
|
218
|
+
const reviewerModel = boundReviewText(`${model.provider}/${model.id}`, MAX_REVIEW_MODEL_KEY_CHARS);
|
|
219
|
+
|
|
220
|
+
const tools = params.evidenceTools ?? [];
|
|
221
|
+
const maxRounds = Math.max(0, params.maxEvidenceRounds ?? 0);
|
|
222
|
+
const useTools = tools.length > 0 && maxRounds > 0;
|
|
223
|
+
const toolByName = new Map(tools.map((tool) => [tool.name, tool]));
|
|
224
|
+
const messages: Message[] = [
|
|
225
|
+
{
|
|
226
|
+
role: "user",
|
|
227
|
+
content: [
|
|
228
|
+
{
|
|
229
|
+
type: "text",
|
|
230
|
+
text: buildReviewPrompt({
|
|
231
|
+
toolName: params.toolName,
|
|
232
|
+
toolInput: params.toolInput,
|
|
233
|
+
transcript: params.transcript,
|
|
234
|
+
cwd: params.cwd,
|
|
235
|
+
}),
|
|
236
|
+
},
|
|
237
|
+
],
|
|
238
|
+
timestamp: Date.now(),
|
|
239
|
+
},
|
|
240
|
+
];
|
|
241
|
+
|
|
242
|
+
const deadline = createReviewDeadline(params.timeoutMs, params.signal);
|
|
243
|
+
try {
|
|
244
|
+
let evidenceRounds = 0;
|
|
245
|
+
for (let round = 0; ; round += 1) {
|
|
246
|
+
const forceAnswer = !useTools || round >= maxRounds;
|
|
247
|
+
const context: Context = {
|
|
248
|
+
systemPrompt: reviewerSystemPrompt(useTools),
|
|
249
|
+
messages,
|
|
250
|
+
...(forceAnswer ? {} : { tools: tools.map(toProviderTool) }),
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const attempt = await deadline.run((signal) => params.registry.complete(model, context, { signal, cacheRetention: "none" }));
|
|
254
|
+
if (attempt.cancelled) {
|
|
255
|
+
return failure("cancelled", "Auto-mode review was cancelled before it finished.");
|
|
256
|
+
}
|
|
257
|
+
if (attempt.timedOut) {
|
|
258
|
+
return failure("timeout", `Auto-mode review timed out after ${params.timeoutMs}ms.`);
|
|
259
|
+
}
|
|
260
|
+
if (attempt.errorMessage) {
|
|
261
|
+
return failure("provider-error", attempt.errorMessage);
|
|
262
|
+
}
|
|
263
|
+
const response = attempt.value;
|
|
264
|
+
if (!response) {
|
|
265
|
+
return failure("provider-error", "Reviewer returned no message.");
|
|
266
|
+
}
|
|
267
|
+
if (response.stopReason === "error" || response.stopReason === "aborted") {
|
|
268
|
+
return failure(
|
|
269
|
+
"provider-error",
|
|
270
|
+
response.errorMessage ?? `Reviewer stopped with "${response.stopReason}".`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const calls = toolCallBlocks(response);
|
|
275
|
+
if (forceAnswer || calls.length === 0) {
|
|
276
|
+
const verdict = parseReviewVerdict(firstTextBlock(response));
|
|
277
|
+
if (!verdict) {
|
|
278
|
+
return failure("invalid-output", "Reviewer did not return a readable allow/deny verdict.");
|
|
279
|
+
}
|
|
280
|
+
return outcomeFromVerdict(verdict, reviewerModel, evidenceRounds);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
messages.push(response);
|
|
284
|
+
evidenceRounds += 1;
|
|
285
|
+
for (const call of calls) {
|
|
286
|
+
const tool = toolByName.get(call.name);
|
|
287
|
+
if (!tool) {
|
|
288
|
+
messages.push(refusedToolResult(call, `Tool "${call.name}" is not available to the reviewer.`));
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const evidence = await deadline.run((signal) => tool.execute(call.arguments, signal));
|
|
292
|
+
if (evidence.cancelled) return failure("cancelled", "Auto-mode review was cancelled during investigation.");
|
|
293
|
+
if (evidence.timedOut) return failure("timeout", `Auto-mode review timed out after ${params.timeoutMs}ms.`);
|
|
294
|
+
messages.push({
|
|
295
|
+
role: "toolResult",
|
|
296
|
+
toolCallId: call.id,
|
|
297
|
+
toolName: call.name,
|
|
298
|
+
content: [{ type: "text", text: boundReviewText(evidence.errorMessage ?? evidence.value ?? "", evidence.errorMessage ? 1_000 : 4_000) }],
|
|
299
|
+
isError: evidence.errorMessage !== undefined,
|
|
300
|
+
timestamp: Date.now(),
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
} finally {
|
|
305
|
+
deadline.dispose();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function toProviderTool(tool: EvidenceTool): Tool {
|
|
310
|
+
return { name: tool.name, description: tool.description, parameters: tool.parameters };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function refusedToolResult(
|
|
314
|
+
call: { id: string; name: string },
|
|
315
|
+
text: string,
|
|
316
|
+
): Message {
|
|
317
|
+
return {
|
|
318
|
+
role: "toolResult",
|
|
319
|
+
toolCallId: call.id,
|
|
320
|
+
toolName: call.name,
|
|
321
|
+
content: [{ type: "text", text }],
|
|
322
|
+
isError: true,
|
|
323
|
+
timestamp: Date.now(),
|
|
324
|
+
};
|
|
325
|
+
}
|