Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 90 lines 3.13 KB
Raw
1 /**
2 * A hand-authored path played with no model and no aimock.
3 *
4 * A hand-authored fixture is the path the skill teaches, written as one
5 * fixture per model turn. Played in order, its `response` blocks are that
6 * path; its `match` blocks only gate a replay on what the CLI printed, and are
7 * not read here. `mutate.ts` plays a path with one command left out, which
8 * aimock cannot serve: the next turn's `match` names the call that never ran.
9 */
10
11 import { readFileSync } from 'node:fs';
12 import { z } from 'zod';
13 import type { ModelAdapter, ModelTurn } from './agent';
14 import { loopDriver, type Driver } from './run-journey';
15
16 const HandAuthoredFile = z.object({
17 fixtures: z.array(
18 z.object({
19 response: z.object({
20 content: z.string().optional(),
21 toolCalls: z
22 .array(
23 z.object({
24 id: z.string(),
25 name: z.string(),
26 arguments: z.looseObject({ command: z.string() }),
27 })
28 )
29 .optional(),
30 }),
31 })
32 ),
33 });
34
35 /** The model turns of a hand-authored file, in order. */
36 export const readScriptedPath = (path: string): ReadonlyArray<ModelTurn> =>
37 HandAuthoredFile.parse(JSON.parse(readFileSync(path, 'utf8'))).fixtures.map(({ response }) => ({
38 text: response.content ?? '',
39 calls: (response.toolCalls ?? [])
40 .filter(call => call.name === 'shell')
41 .map(call => ({ id: call.id, command: call.arguments.command })),
42 }));
43
44 /** The commands of a path, in the order they run. */
45 export const scriptedCommands = (turns: ReadonlyArray<ModelTurn>): string[] =>
46 turns.flatMap(turn => turn.calls.map(call => call.command));
47
48 export const scriptedAdapter = (
49 turns: ReadonlyArray<ModelTurn>,
50 options: {
51 /** The index, in `scriptedCommands(turns)`, of the one command not to run. */
52 drop?: number;
53 } = {}
54 ): ModelAdapter => {
55 let turn = 0;
56 let command = 0;
57 return {
58 // Nothing speaks this path's wire format; the field only says which
59 // provider an adapter was built for, and this one was built for none.
60 provider: 'openai',
61 model: 'scripted',
62 next: async () => {
63 const scripted = turns[turn];
64 turn += 1;
65 if (!scripted) return { text: '', calls: [], stopped: 'script-ended' };
66 const calls = scripted.calls.flatMap(call => {
67 const index = command;
68 command += 1;
69 return index === options.drop ? [] : [call];
70 });
71 return { text: scripted.text, calls };
72 },
73 // The path is fixed, so what the commands printed and what the person said
74 // change nothing about the next turn.
75 record: () => {},
76 say: () => {},
77 // A turn whose only command was dropped asks for nothing, which would end
78 // the loop; the rest of the path still has to be played.
79 hasMore: () => turn < turns.length,
80 };
81 };
82
83 /**
84 * The minimal loop driving a scripted path. Output is scrubbed as in a
85 * replay, so the URLs the path's text names are the ones the person expects.
86 */
87 export const scriptedDriver = (path: string, options: { drop?: number } = {}): Driver => {
88 const turns = readScriptedPath(path);
89 return loopDriver(() => scriptedAdapter(turns, options));
90 };