|
| 1 | +import { afterAll, beforeAll, describe, expect, it, mock, spyOn } from 'bun:test'; |
| 2 | +import type { ExpressionContext } from '../expression/evaluator'; |
| 3 | +import type { LlmStep, Step } from '../parser/schema'; |
| 4 | +import { ConsoleLogger } from '../utils/logger'; |
| 5 | +import { OpenAIAdapter } from './llm-adapter'; |
| 6 | +import { executeLlmStep } from './llm-executor'; |
| 7 | + |
| 8 | +describe('Standard Tools Integration', () => { |
| 9 | + const originalOpenAIChat = OpenAIAdapter.prototype.chat; |
| 10 | + |
| 11 | + beforeAll(() => { |
| 12 | + // Mocking OpenAI Adapter |
| 13 | + }); |
| 14 | + |
| 15 | + afterAll(() => { |
| 16 | + OpenAIAdapter.prototype.chat = originalOpenAIChat; |
| 17 | + }); |
| 18 | + |
| 19 | + it('should inject standard tools when useStandardTools is true', async () => { |
| 20 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 21 | + let capturedTools: any[] = []; |
| 22 | + |
| 23 | + OpenAIAdapter.prototype.chat = mock(async (messages, options) => { |
| 24 | + capturedTools = options.tools || []; |
| 25 | + return { |
| 26 | + message: { |
| 27 | + role: 'assistant', |
| 28 | + content: 'I will read the file', |
| 29 | + tool_calls: [ |
| 30 | + { |
| 31 | + id: 'call_1', |
| 32 | + type: 'function', |
| 33 | + function: { |
| 34 | + name: 'read_file', |
| 35 | + arguments: JSON.stringify({ path: 'test.txt' }), |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + usage: { prompt_tokens: 10, completion_tokens: 10, total_tokens: 20 }, |
| 41 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 42 | + } as any; |
| 43 | + }); |
| 44 | + |
| 45 | + const step: LlmStep = { |
| 46 | + id: 'l1', |
| 47 | + type: 'llm', |
| 48 | + agent: 'test-agent', |
| 49 | + needs: [], |
| 50 | + prompt: 'read test.txt', |
| 51 | + useStandardTools: true, |
| 52 | + maxIterations: 1, |
| 53 | + }; |
| 54 | + |
| 55 | + const context: ExpressionContext = { inputs: {}, steps: {} }; |
| 56 | + const executeStepFn = mock(async (s: Step) => { |
| 57 | + return { status: 'success', output: 'file content' }; |
| 58 | + }); |
| 59 | + |
| 60 | + // We catch the "Max iterations reached" error because we set maxIterations to 1 |
| 61 | + // but we can still check if tools were injected and the tool call was made. |
| 62 | + try { |
| 63 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 64 | + await executeLlmStep(step, context, executeStepFn as any); |
| 65 | + } catch (e) { |
| 66 | + if ((e as Error).message !== 'Max ReAct iterations reached') throw e; |
| 67 | + } |
| 68 | + |
| 69 | + expect(capturedTools.some((t) => t.function.name === 'read_file')).toBe(true); |
| 70 | + expect(executeStepFn).toHaveBeenCalled(); |
| 71 | + const toolStep = executeStepFn.mock.calls[0][0] as Step; |
| 72 | + expect(toolStep.type).toBe('file'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should block risky standard tools without allowInsecure', async () => { |
| 76 | + OpenAIAdapter.prototype.chat = mock(async (messages, options) => { |
| 77 | + return { |
| 78 | + message: { |
| 79 | + role: 'assistant', |
| 80 | + content: 'I will run a command', |
| 81 | + tool_calls: [ |
| 82 | + { |
| 83 | + id: 'call_2', |
| 84 | + type: 'function', |
| 85 | + function: { |
| 86 | + name: 'run_command', |
| 87 | + arguments: JSON.stringify({ command: 'rm -rf /' }), |
| 88 | + }, |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + usage: { prompt_tokens: 10, completion_tokens: 10, total_tokens: 20 }, |
| 93 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 94 | + } as any; |
| 95 | + }); |
| 96 | + |
| 97 | + const step: LlmStep = { |
| 98 | + id: 'l1', |
| 99 | + type: 'llm', |
| 100 | + agent: 'test-agent', |
| 101 | + needs: [], |
| 102 | + prompt: 'run risky command', |
| 103 | + useStandardTools: true, |
| 104 | + allowInsecure: false, // Explicitly false |
| 105 | + maxIterations: 2, |
| 106 | + }; |
| 107 | + |
| 108 | + const context: ExpressionContext = { inputs: {}, steps: {} }; |
| 109 | + const executeStepFn = mock(async () => ({ status: 'success', output: '' })); |
| 110 | + |
| 111 | + // The execution should not throw, but it should return a tool error message to the LLM |
| 112 | + // However, in our mock, we want to see if executeStepFn was called. |
| 113 | + // Actually, in llm-executor.ts, it pushes a "Security Error" message if check fails and continues loop. |
| 114 | + |
| 115 | + let securityErrorMessage = ''; |
| 116 | + OpenAIAdapter.prototype.chat = mock(async (messages) => { |
| 117 | + const lastMessage = messages[messages.length - 1]; |
| 118 | + if (lastMessage.role === 'tool') { |
| 119 | + securityErrorMessage = lastMessage.content; |
| 120 | + return { |
| 121 | + message: { role: 'assistant', content: 'stop' }, |
| 122 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, |
| 123 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 124 | + } as any; |
| 125 | + } |
| 126 | + return { |
| 127 | + message: { |
| 128 | + role: 'assistant', |
| 129 | + tool_calls: [ |
| 130 | + { |
| 131 | + id: 'c2', |
| 132 | + type: 'function', |
| 133 | + function: { name: 'run_command', arguments: '{"command":"rm -rf /"}' }, |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 138 | + } as any; |
| 139 | + }); |
| 140 | + |
| 141 | + // biome-ignore lint/suspicious/noExplicitAny: mock |
| 142 | + await executeLlmStep(step, context, executeStepFn as any); |
| 143 | + |
| 144 | + expect(securityErrorMessage).toContain('Security Error'); |
| 145 | + expect(executeStepFn).not.toHaveBeenCalled(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments