|
| 1 | +import { TEST_USER_ID } from '@codebuff/common/constants' |
| 2 | +import { getInitialSessionState } from '@codebuff/common/types/session-state' |
| 3 | +import { |
| 4 | + afterAll, |
| 5 | + beforeAll, |
| 6 | + beforeEach, |
| 7 | + describe, |
| 8 | + expect, |
| 9 | + it, |
| 10 | + Mock, |
| 11 | + mock, |
| 12 | + spyOn, |
| 13 | +} from 'bun:test' |
| 14 | +import { WebSocket } from 'ws' |
| 15 | + |
| 16 | +import * as runAgentStep from '../run-agent-step' |
| 17 | +import * as agentRegistryModule from '../templates/agent-registry' |
| 18 | +import { AgentTemplate } from '../templates/types' |
| 19 | +import { |
| 20 | + handleSpawnAgents, |
| 21 | + SendSubagentChunk, |
| 22 | +} from '../tools/handlers/spawn-agents' |
| 23 | +import * as loggerModule from '../util/logger' |
| 24 | +import { mockFileContext, MockWebSocket } from './test-utils' |
| 25 | + |
| 26 | +describe('Subagent Streaming', () => { |
| 27 | + let mockSendSubagentChunk: Mock<SendSubagentChunk> |
| 28 | + let mockLoopAgentSteps: Mock<(typeof runAgentStep)['loopAgentSteps']> |
| 29 | + |
| 30 | + beforeAll(() => { |
| 31 | + // Mock dependencies |
| 32 | + spyOn(loggerModule.logger, 'debug').mockImplementation(() => {}) |
| 33 | + spyOn(loggerModule.logger, 'error').mockImplementation(() => {}) |
| 34 | + spyOn(loggerModule.logger, 'info').mockImplementation(() => {}) |
| 35 | + spyOn(loggerModule.logger, 'warn').mockImplementation(() => {}) |
| 36 | + spyOn(loggerModule, 'withLoggerContext').mockImplementation( |
| 37 | + async (context: any, fn: () => Promise<any>) => fn() |
| 38 | + ) |
| 39 | + |
| 40 | + // Mock sendSubagentChunk function to capture streaming messages |
| 41 | + mockSendSubagentChunk = mock( |
| 42 | + (data: { |
| 43 | + userInputId: string |
| 44 | + agentId: string |
| 45 | + agentType: string |
| 46 | + chunk: string |
| 47 | + prompt?: string |
| 48 | + }) => {} |
| 49 | + ) |
| 50 | + |
| 51 | + // Mock loopAgentSteps to simulate subagent execution with streaming |
| 52 | + mockLoopAgentSteps = spyOn( |
| 53 | + runAgentStep, |
| 54 | + 'loopAgentSteps' |
| 55 | + ).mockImplementation(async (ws, options) => { |
| 56 | + // Simulate streaming chunks by calling the callback |
| 57 | + if (options.onResponseChunk) { |
| 58 | + options.onResponseChunk('Thinking about the problem...') |
| 59 | + options.onResponseChunk('Found a solution!') |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + agentState: { |
| 64 | + ...options.agentState, |
| 65 | + messageHistory: [ |
| 66 | + { role: 'assistant', content: 'Test response from subagent' }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + // Mock agent registry |
| 73 | + spyOn(agentRegistryModule.agentRegistry, 'initialize').mockImplementation( |
| 74 | + async () => {} |
| 75 | + ) |
| 76 | + spyOn( |
| 77 | + agentRegistryModule.agentRegistry, |
| 78 | + 'getAllTemplates' |
| 79 | + ).mockImplementation(() => ({ |
| 80 | + thinker: { |
| 81 | + id: 'thinker', |
| 82 | + name: 'Thinker', |
| 83 | + outputMode: 'last_message', |
| 84 | + promptSchema: { |
| 85 | + prompt: { |
| 86 | + safeParse: () => ({ success: true }), |
| 87 | + } as any, |
| 88 | + }, |
| 89 | + purpose: '', |
| 90 | + model: '', |
| 91 | + includeMessageHistory: true, |
| 92 | + toolNames: [], |
| 93 | + spawnableAgents: [], |
| 94 | + initialAssistantMessage: '', |
| 95 | + initialAssistantPrefix: '', |
| 96 | + stepAssistantMessage: '', |
| 97 | + stepAssistantPrefix: '', |
| 98 | + systemPrompt: '', |
| 99 | + userInputPrompt: '', |
| 100 | + agentStepPrompt: '', |
| 101 | + }, |
| 102 | + })) |
| 103 | + spyOn(agentRegistryModule.agentRegistry, 'getAgentName').mockImplementation( |
| 104 | + () => 'Thinker' |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + beforeEach(() => { |
| 109 | + mockSendSubagentChunk.mockClear() |
| 110 | + mockLoopAgentSteps.mockClear() |
| 111 | + }) |
| 112 | + |
| 113 | + afterAll(() => { |
| 114 | + mock.restore() |
| 115 | + }) |
| 116 | + |
| 117 | + it('should send subagent-response-chunk messages during agent execution', async () => { |
| 118 | + const ws = new MockWebSocket() as unknown as WebSocket |
| 119 | + const sessionState = getInitialSessionState(mockFileContext) |
| 120 | + const agentState = sessionState.mainAgentState |
| 121 | + |
| 122 | + // Mock parent agent template that can spawn thinker |
| 123 | + const parentTemplate = { |
| 124 | + id: 'base', |
| 125 | + spawnableAgents: ['thinker'], |
| 126 | + } as unknown as AgentTemplate |
| 127 | + |
| 128 | + const toolCall = { |
| 129 | + toolName: 'spawn_agents' as const, |
| 130 | + toolCallId: 'test-tool-call-id', |
| 131 | + args: { |
| 132 | + agents: [ |
| 133 | + { |
| 134 | + agent_type: 'thinker', |
| 135 | + prompt: 'Think about this problem', |
| 136 | + }, |
| 137 | + ], |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + const { result } = handleSpawnAgents({ |
| 142 | + previousToolCallFinished: Promise.resolve(), |
| 143 | + toolCall, |
| 144 | + fileContext: mockFileContext, |
| 145 | + clientSessionId: 'test-session', |
| 146 | + userInputId: 'test-input', |
| 147 | + getLatestState: () => ({ messages: [] }), |
| 148 | + state: { |
| 149 | + ws, |
| 150 | + fingerprintId: 'test-fingerprint', |
| 151 | + userId: TEST_USER_ID, |
| 152 | + agentTemplate: parentTemplate, |
| 153 | + sendSubagentChunk: mockSendSubagentChunk, |
| 154 | + messages: [], |
| 155 | + agentState, |
| 156 | + }, |
| 157 | + }) |
| 158 | + |
| 159 | + await result |
| 160 | + |
| 161 | + // Verify that subagent streaming messages were sent |
| 162 | + expect(mockSendSubagentChunk).toHaveBeenCalledTimes(2) |
| 163 | + |
| 164 | + // Check first streaming chunk |
| 165 | + expect(mockSendSubagentChunk).toHaveBeenNthCalledWith(1, { |
| 166 | + userInputId: 'test-input', |
| 167 | + agentId: expect.any(String), |
| 168 | + agentType: 'thinker', |
| 169 | + chunk: 'Thinking about the problem...', |
| 170 | + prompt: 'Think about this problem', |
| 171 | + }) |
| 172 | + |
| 173 | + // Check second streaming chunk |
| 174 | + expect(mockSendSubagentChunk).toHaveBeenNthCalledWith(2, { |
| 175 | + userInputId: 'test-input', |
| 176 | + agentId: expect.any(String), |
| 177 | + agentType: 'thinker', |
| 178 | + chunk: 'Found a solution!', |
| 179 | + prompt: 'Think about this problem', |
| 180 | + }) |
| 181 | + }) |
| 182 | + |
| 183 | + it('should include correct agentId and agentType in streaming messages', async () => { |
| 184 | + const ws = new MockWebSocket() as unknown as WebSocket |
| 185 | + const sessionState = getInitialSessionState(mockFileContext) |
| 186 | + const agentState = sessionState.mainAgentState |
| 187 | + |
| 188 | + const parentTemplate = { |
| 189 | + id: 'base', |
| 190 | + spawnableAgents: ['thinker'], |
| 191 | + } as unknown as AgentTemplate |
| 192 | + |
| 193 | + const toolCall = { |
| 194 | + toolName: 'spawn_agents' as const, |
| 195 | + toolCallId: 'test-tool-call-id-2', |
| 196 | + args: { |
| 197 | + agents: [ |
| 198 | + { |
| 199 | + agent_type: 'thinker', |
| 200 | + prompt: 'Test prompt', |
| 201 | + }, |
| 202 | + ], |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + const { result } = handleSpawnAgents({ |
| 207 | + previousToolCallFinished: Promise.resolve(), |
| 208 | + toolCall, |
| 209 | + fileContext: mockFileContext, |
| 210 | + clientSessionId: 'test-session', |
| 211 | + userInputId: 'test-input-123', |
| 212 | + getLatestState: () => ({ messages: [] }), |
| 213 | + state: { |
| 214 | + ws, |
| 215 | + fingerprintId: 'test-fingerprint', |
| 216 | + userId: TEST_USER_ID, |
| 217 | + agentTemplate: parentTemplate, |
| 218 | + sendSubagentChunk: mockSendSubagentChunk, |
| 219 | + messages: [], |
| 220 | + agentState, |
| 221 | + }, |
| 222 | + }) |
| 223 | + await result |
| 224 | + |
| 225 | + // Verify the streaming messages have consistent agentId and correct agentType |
| 226 | + expect(mockSendSubagentChunk.mock.calls.length).toBeGreaterThanOrEqual(2) |
| 227 | + const calls = mockSendSubagentChunk.mock.calls as Array< |
| 228 | + [ |
| 229 | + { |
| 230 | + userInputId: string |
| 231 | + agentId: string |
| 232 | + agentType: string |
| 233 | + chunk: string |
| 234 | + prompt?: string |
| 235 | + }, |
| 236 | + ] |
| 237 | + > |
| 238 | + const firstCall = calls[0][0] |
| 239 | + const secondCall = calls[1][0] |
| 240 | + |
| 241 | + expect(firstCall.agentId).toBe(secondCall.agentId) // Same agent ID |
| 242 | + expect(firstCall.agentType).toBe('thinker') |
| 243 | + expect(secondCall.agentType).toBe('thinker') |
| 244 | + expect(firstCall.userInputId).toBe('test-input-123') |
| 245 | + expect(secondCall.userInputId).toBe('test-input-123') |
| 246 | + }) |
| 247 | +}) |
0 commit comments