-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-agent-snippet.ts
More file actions
35 lines (28 loc) · 1.04 KB
/
test-agent-snippet.ts
File metadata and controls
35 lines (28 loc) · 1.04 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
type Schema<T> = { safeParse: (data: unknown) => { success: boolean; data?: T; error?: { issues: Array<{ path: (string | number)[]; message: string }> } } };
class Agent {
async structured<T>(
prompt: string,
schema: Schema<T>,
options?: { abortSignal?: AbortSignal; maxRetries?: number },
): Promise<T> {
const maxRetries = options?.maxRetries ?? 3;
for (let attempt = 0; attempt < maxRetries; attempt++) {
const currentPrompt = attempt === 0
? prompt
: `${prompt}\n\nPrevious attempt returned invalid JSON. Please fix and respond with valid JSON only.`;
}
throw new Error(`Failed after ${3} attempts`);
}
async structuredViaTool<T>(
prompt: string,
schema: Schema<T>,
options?: { abortSignal?: AbortSignal; maxRetries?: number },
): Promise<T> {
const maxRetries = options?.maxRetries ?? 3;
let captured: T | undefined;
for (let attempt = 0; attempt < maxRetries; attempt++) {
captured = undefined;
}
throw new Error(`Failed after ${maxRetries} attempts`);
}
}