|
| 1 | +import { LLMock } from "@copilotkit/aimock" |
| 2 | + |
| 3 | +import { toolResultContains } from "./tool-result" |
| 4 | + |
| 5 | +type ExecuteCommandToolCall = { |
| 6 | + name: "execute_command" | "attempt_completion" |
| 7 | + params: Record<string, unknown> |
| 8 | + id: string |
| 9 | +} |
| 10 | + |
| 11 | +type ExecuteCommandFixture = { |
| 12 | + toolCallId: string |
| 13 | + expected: string[] |
| 14 | + toolCalls: ExecuteCommandToolCall[] |
| 15 | +} |
| 16 | + |
| 17 | +export function addExecuteCommandResultFixtures(mock: InstanceType<typeof LLMock>) { |
| 18 | + const fixtures: ExecuteCommandFixture[] = [ |
| 19 | + { |
| 20 | + toolCallId: "call_execute_command_simple_001", |
| 21 | + expected: ["Command executed in terminal within working directory '", "Exit code: 0\nOutput:\n"], |
| 22 | + toolCalls: [ |
| 23 | + { |
| 24 | + name: "attempt_completion", |
| 25 | + params: { |
| 26 | + result: "Ran the echo command and created `execute-command-tool-fixture/simple-echo.txt`.", |
| 27 | + }, |
| 28 | + id: "call_execute_command_simple_002", |
| 29 | + }, |
| 30 | + ], |
| 31 | + }, |
| 32 | + { |
| 33 | + toolCallId: "call_execute_command_cwd_001", |
| 34 | + expected: ["execute-command-tool-fixture/custom-cwd'. Exit code: 0", "Output:\n"], |
| 35 | + toolCalls: [ |
| 36 | + { |
| 37 | + name: "attempt_completion", |
| 38 | + params: { |
| 39 | + result: "Ran the command inside `execute-command-tool-fixture/custom-cwd` and created `output.txt`.", |
| 40 | + }, |
| 41 | + id: "call_execute_command_cwd_002", |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + toolCallId: "call_execute_command_multi_001", |
| 47 | + expected: ["Command executed in terminal within working directory '", "Exit code: 0\nOutput:\n"], |
| 48 | + toolCalls: [ |
| 49 | + { |
| 50 | + name: "execute_command", |
| 51 | + params: { |
| 52 | + command: "printf 'Line 2\\n' >> execute-command-tool-fixture/multi-command.txt", |
| 53 | + }, |
| 54 | + id: "call_execute_command_multi_002", |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + toolCallId: "call_execute_command_multi_002", |
| 60 | + expected: ["Command executed in terminal within working directory '", "Exit code: 0\nOutput:\n"], |
| 61 | + toolCalls: [ |
| 62 | + { |
| 63 | + name: "attempt_completion", |
| 64 | + params: { |
| 65 | + result: "Ran both commands and populated `execute-command-tool-fixture/multi-command.txt` with two lines.", |
| 66 | + }, |
| 67 | + id: "call_execute_command_multi_003", |
| 68 | + }, |
| 69 | + ], |
| 70 | + }, |
| 71 | + { |
| 72 | + toolCallId: "call_execute_command_long_running_001", |
| 73 | + expected: ["Exit code: 0", "Command completed after delay"], |
| 74 | + toolCalls: [ |
| 75 | + { |
| 76 | + name: "attempt_completion", |
| 77 | + params: { |
| 78 | + result: "The delayed command completed and printed `Command completed after delay`.", |
| 79 | + }, |
| 80 | + id: "call_execute_command_long_running_002", |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | + ] |
| 85 | + |
| 86 | + for (const fixture of fixtures) { |
| 87 | + mock.addFixture({ |
| 88 | + match: { |
| 89 | + toolCallId: fixture.toolCallId, |
| 90 | + predicate: (req) => toolResultContains(req, fixture.toolCallId, fixture.expected), |
| 91 | + }, |
| 92 | + response: { |
| 93 | + toolCalls: fixture.toolCalls.map((toolCall) => ({ |
| 94 | + name: toolCall.name, |
| 95 | + arguments: JSON.stringify(toolCall.params), |
| 96 | + id: toolCall.id, |
| 97 | + })), |
| 98 | + }, |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments