|
| 1 | +import type { ChatCompletionRequest, ChatMessage } from "@copilotkit/aimock" |
| 2 | + |
| 3 | +import { LLMock } from "@copilotkit/aimock" |
| 4 | + |
| 5 | +function anyToolResultContains(req: ChatCompletionRequest, ...terms: string[]): boolean { |
| 6 | + const messages: ChatMessage[] = Array.isArray(req?.messages) ? req.messages : [] |
| 7 | + return messages.some( |
| 8 | + (msg) => |
| 9 | + msg?.role === "tool" && |
| 10 | + typeof msg.content === "string" && |
| 11 | + terms.every((t) => (msg.content as string).includes(t)), |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +export function addColdShellInitFixtures(mock: InstanceType<typeof LLMock>) { |
| 16 | + // On cold zsh terminals the first execution may produce 0 chunks (VSCode |
| 17 | + // execution.read() limitation on basic shell integration — see issue #242897). |
| 18 | + // When the first result is empty, the mock retries the same command so the |
| 19 | + // second attempt (on the now-warm terminal) captures real output. |
| 20 | + mock.addFixture({ |
| 21 | + match: { |
| 22 | + toolCallId: "call_cold_shell_init_001", |
| 23 | + predicate: (req: ChatCompletionRequest) => |
| 24 | + // First attempt returned empty — retry the command |
| 25 | + !anyToolResultContains(req, "cold-init-ok"), |
| 26 | + }, |
| 27 | + response: { |
| 28 | + toolCalls: [ |
| 29 | + { |
| 30 | + name: "execute_command", |
| 31 | + arguments: JSON.stringify({ |
| 32 | + command: "python3 -c \"\nimport sys\nprint('cold-init-ok', file=sys.stdout)\nsys.exit(0)\n\"", |
| 33 | + }), |
| 34 | + id: "call_cold_shell_init_003", |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + }) |
| 39 | + |
| 40 | + // Match whichever attempt (first or retry) delivers real output — prove |
| 41 | + // the guard kept the process alive long enough for the output to arrive. |
| 42 | + mock.addFixture({ |
| 43 | + match: { |
| 44 | + predicate: (req: ChatCompletionRequest) => anyToolResultContains(req, "cold-init-ok", "Exit code: 0"), |
| 45 | + }, |
| 46 | + response: { |
| 47 | + toolCalls: [ |
| 48 | + { |
| 49 | + name: "attempt_completion", |
| 50 | + arguments: JSON.stringify({ result: "Cold shell init command completed with real output." }), |
| 51 | + id: "call_cold_shell_init_002", |
| 52 | + }, |
| 53 | + ], |
| 54 | + }, |
| 55 | + }) |
| 56 | +} |
0 commit comments