|
| 1 | +import { LLMock } from "@copilotkit/aimock" |
| 2 | +import type { ChatCompletionRequest, ChatMessage } from "@copilotkit/aimock" |
| 3 | + |
| 4 | +type ToolResultExpectation = { toolCallId: string; expected: string[] } |
| 5 | + |
| 6 | +type ReadFileResultFixture = { |
| 7 | + toolCallId: string |
| 8 | + expected: string[] | ToolResultExpectation[] |
| 9 | + result: string |
| 10 | + id: string |
| 11 | +} |
| 12 | + |
| 13 | +function isToolResultExpectation(value: unknown): value is ToolResultExpectation { |
| 14 | + return typeof value === "object" && value !== null && "toolCallId" in value && "expected" in value |
| 15 | +} |
| 16 | + |
| 17 | +function toolResultContains(req: ChatCompletionRequest, toolCallId: string, expected: string[]) { |
| 18 | + const messages = Array.isArray(req?.messages) ? req.messages : [] |
| 19 | + const toolMessage = messages.find( |
| 20 | + (message: ChatMessage) => message?.role === "tool" && message.tool_call_id === toolCallId, |
| 21 | + ) |
| 22 | + |
| 23 | + const content = toolMessage?.content |
| 24 | + if (typeof content !== "string") { |
| 25 | + return false |
| 26 | + } |
| 27 | + |
| 28 | + return expected.every((text) => content.includes(text)) |
| 29 | +} |
| 30 | + |
| 31 | +function toolResultsContain(req: ChatCompletionRequest, expectations: ToolResultExpectation[]) { |
| 32 | + return expectations.every(({ toolCallId, expected }) => toolResultContains(req, toolCallId, expected)) |
| 33 | +} |
| 34 | + |
| 35 | +export function addReadFileResultFixtures(mock: InstanceType<typeof LLMock>) { |
| 36 | + const fixtures: ReadFileResultFixture[] = [ |
| 37 | + { |
| 38 | + toolCallId: "call_read_file_simple_001", |
| 39 | + expected: ["File: simple-read-file-smoke.txt", "1 | Hello, World!"], |
| 40 | + result: 'The file [`simple-read-file-smoke.txt`](simple-read-file-smoke.txt) contains the text: "Hello, World!"', |
| 41 | + id: "call_read_file_simple_002", |
| 42 | + }, |
| 43 | + { |
| 44 | + toolCallId: "call_read_file_multiline_001", |
| 45 | + expected: ["File: multiline-read-file.txt", "1 | Line 1", "5 | Line 5"], |
| 46 | + result: "The file [`multiline-read-file.txt`](multiline-read-file.txt) contains 5 lines: Line 1, Line 2, Line 3, Line 4, and Line 5.", |
| 47 | + id: "call_read_file_multiline_002", |
| 48 | + }, |
| 49 | + { |
| 50 | + toolCallId: "call_read_file_slice_001", |
| 51 | + expected: ["File: multiline-read-file.txt", "2 | Line 2", "3 | Line 3", "4 | Line 4"], |
| 52 | + result: "The three lines read from [`multiline-read-file.txt`](multiline-read-file.txt) starting at offset 2 are:\n\n- Line 2\n- Line 3\n- Line 4", |
| 53 | + id: "call_read_file_slice_002", |
| 54 | + }, |
| 55 | + { |
| 56 | + toolCallId: "call_read_file_missing_001", |
| 57 | + expected: ["non-existent-read-file.txt", "ENOENT", "no such file or directory"], |
| 58 | + result: "Attempting to read [`non-existent-read-file.txt`](non-existent-read-file.txt) resulted in an error: the file does not exist. This error was handled appropriately and no file contents were returned.", |
| 59 | + id: "call_read_file_missing_002", |
| 60 | + }, |
| 61 | + { |
| 62 | + toolCallId: "call_read_file_xml_001", |
| 63 | + expected: ["File: xml-content-read-file.xml", "<child>Test content</child>", "<data>Some data</data>"], |
| 64 | + result: "The XML file [`xml-content-read-file.xml`](xml-content-read-file.xml) contains the following elements:\n- `<root>` (root element)\n- `<child>` (child of root)\n- `<data>` (child of root)\n\nThe structure is:\n```xml\n<root>\n <child>Test content</child>\n <data>Some data</data>\n</root>\n```", |
| 65 | + id: "call_read_file_xml_002", |
| 66 | + }, |
| 67 | + { |
| 68 | + toolCallId: "call_read_file_multiple_multiline_001", |
| 69 | + expected: [ |
| 70 | + { |
| 71 | + toolCallId: "call_read_file_multiple_simple_001", |
| 72 | + expected: ["File: simple-read-file-smoke.txt", "1 | Hello, World!"], |
| 73 | + }, |
| 74 | + { |
| 75 | + toolCallId: "call_read_file_multiple_multiline_001", |
| 76 | + expected: ["File: multiline-read-file.txt", "1 | Line 1", "5 | Line 5"], |
| 77 | + }, |
| 78 | + ], |
| 79 | + result: "Contents of [`simple-read-file-smoke.txt`](simple-read-file-smoke.txt):\n\n```\nHello, World!\n```\n\nContents of [`multiline-read-file.txt`](multiline-read-file.txt):\n\n```\nLine 1\nLine 2\nLine 3\nLine 4\nLine 5\n```", |
| 80 | + id: "call_read_file_multiple_002", |
| 81 | + }, |
| 82 | + { |
| 83 | + toolCallId: "call_read_file_large_001", |
| 84 | + expected: [ |
| 85 | + "File: large-read-file.txt", |
| 86 | + "1 | Line 1: This is a test line with some content", |
| 87 | + "100 | Line 100: This is a test line with some content", |
| 88 | + ], |
| 89 | + result: "The file [`large-read-file.txt`](large-read-file.txt) contains 100 lines, each following the pattern: `Line N: This is a test line with some content`, where `N` is the line number (from 1 to 100). The structure is consistent throughout the file, with only the line number changing on each line.", |
| 90 | + id: "call_read_file_large_002", |
| 91 | + }, |
| 92 | + ] |
| 93 | + |
| 94 | + for (const fixture of fixtures) { |
| 95 | + mock.addFixture({ |
| 96 | + match: { |
| 97 | + toolCallId: fixture.toolCallId, |
| 98 | + predicate: (req) => |
| 99 | + isToolResultExpectation(fixture.expected[0]) |
| 100 | + ? toolResultsContain(req, fixture.expected as ToolResultExpectation[]) |
| 101 | + : toolResultContains(req, fixture.toolCallId, fixture.expected as string[]), |
| 102 | + }, |
| 103 | + response: { |
| 104 | + toolCalls: [ |
| 105 | + { |
| 106 | + name: "attempt_completion", |
| 107 | + arguments: JSON.stringify({ result: fixture.result }), |
| 108 | + id: fixture.id, |
| 109 | + }, |
| 110 | + ], |
| 111 | + }, |
| 112 | + }) |
| 113 | + } |
| 114 | +} |
0 commit comments