|
1 | | -import { defineTool } from "@microsoft/agent-framework-core"; |
| 1 | +import { AgentInvalidResponseError, defineTool } from "@microsoft/agent-framework-core"; |
2 | 2 | import OpenAI from "openai"; |
3 | 3 | import type { |
4 | 4 | ChatCompletion, |
@@ -154,6 +154,55 @@ describe("OpenAIChatCompletionClient", () => { |
154 | 154 | expect(response.text).toBe("Rainy"); |
155 | 155 | }); |
156 | 156 |
|
| 157 | + it.each([ |
| 158 | + { name: "empty stream", chunks: [] }, |
| 159 | + { name: "choice-less chunks", chunks: [{ ...chunk("response-1", {}), choices: [] }] }, |
| 160 | + { |
| 161 | + name: "usage-only stream", |
| 162 | + chunks: [ |
| 163 | + { |
| 164 | + ...chunk("response-1", {}), |
| 165 | + choices: [], |
| 166 | + usage: { prompt_tokens: 2, completion_tokens: 3, total_tokens: 5 }, |
| 167 | + }, |
| 168 | + ], |
| 169 | + }, |
| 170 | + ])("rejects a malformed $name without any completion choice", async ({ chunks }) => { |
| 171 | + const { client, create } = mockOpenAI([chunks]); |
| 172 | + const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" }); |
| 173 | + const stream = chatClient.getResponse("request", { stream: true }); |
| 174 | + const consume = async () => { |
| 175 | + for await (const _update of stream) { |
| 176 | + // Even if usage metadata is emitted, the response must not finalize successfully. |
| 177 | + } |
| 178 | + }; |
| 179 | + |
| 180 | + await expect(consume()).rejects.toThrow(AgentInvalidResponseError); |
| 181 | + await expect(stream.getFinalResponse()).rejects.toThrow("OpenAI returned no completion choices."); |
| 182 | + expect(create).toHaveBeenCalledTimes(1); |
| 183 | + }); |
| 184 | + |
| 185 | + it("preserves a valid response followed by a usage-only chunk", async () => { |
| 186 | + const { client } = mockOpenAI([ |
| 187 | + [ |
| 188 | + chunk("response-1", { role: "assistant", content: "hello" }, "stop"), |
| 189 | + { |
| 190 | + ...chunk("response-1", {}), |
| 191 | + choices: [], |
| 192 | + usage: { prompt_tokens: 2, completion_tokens: 3, total_tokens: 5 }, |
| 193 | + }, |
| 194 | + ], |
| 195 | + ]); |
| 196 | + const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" }); |
| 197 | + const response = await chatClient.getResponse("request", { stream: true }).getFinalResponse(); |
| 198 | + |
| 199 | + expect(response.text).toBe("hello"); |
| 200 | + expect(response.messages).toHaveLength(1); |
| 201 | + expect(response.responseId).toBe("response-1"); |
| 202 | + expect(response.finishReason).toBe("stop"); |
| 203 | + expect(response.usageDetails).toEqual({ inputTokenCount: 2, outputTokenCount: 3, totalTokenCount: 5 }); |
| 204 | + }); |
| 205 | + |
157 | 206 | it("preserves non-streaming and streaming refusals", async () => { |
158 | 207 | const nonStreamingRefusal = completion("response-1", null); |
159 | 208 | nonStreamingRefusal.choices[0]!.message.refusal = "I cannot help with that."; |
@@ -232,4 +281,50 @@ describe("OpenAIChatCompletionClient", () => { |
232 | 281 | expect(calls[0]?.id).toBe("openai:response-replayed:choice:0:tool:0"); |
233 | 282 | expect(response.text).toBe("done"); |
234 | 283 | }); |
| 284 | + |
| 285 | + it("does not add an empty OpenAI assistant message when a streaming tool turn is replayed", async () => { |
| 286 | + const replayed = [ |
| 287 | + chunk("response-replayed", { |
| 288 | + role: "assistant", |
| 289 | + tool_calls: [{ index: 0, id: "call-1", type: "function", function: { name: "side_", arguments: "{" } }], |
| 290 | + }), |
| 291 | + chunk( |
| 292 | + "response-replayed", |
| 293 | + { tool_calls: [{ index: 0, function: { name: "effect", arguments: "}" } }] }, |
| 294 | + "tool_calls", |
| 295 | + ), |
| 296 | + ]; |
| 297 | + const { client, create } = mockOpenAI([ |
| 298 | + replayed, |
| 299 | + replayed, |
| 300 | + [chunk("response-final", { role: "assistant", content: "done" }, "stop")], |
| 301 | + ]); |
| 302 | + const execute = vi.fn(() => "complete"); |
| 303 | + const sideEffect = defineTool({ |
| 304 | + name: "side_effect", |
| 305 | + parameters: { type: "object", additionalProperties: false }, |
| 306 | + execute, |
| 307 | + }); |
| 308 | + const chatClient = new OpenAIChatCompletionClient({ client, model: "gpt-test" }); |
| 309 | + const response = await chatClient |
| 310 | + .getResponse("request", { stream: true, options: { tools: [sideEffect] } }) |
| 311 | + .getFinalResponse(); |
| 312 | + const finalRequest = create.mock.calls[2]?.[0] as ChatCompletionCreateParams; |
| 313 | + |
| 314 | + expect(execute).toHaveBeenCalledTimes(1); |
| 315 | + expect(create).toHaveBeenCalledTimes(3); |
| 316 | + expect(finalRequest.tool_choice).toBe("none"); |
| 317 | + expect(finalRequest.messages).toEqual(create.mock.calls[1]?.[0].messages); |
| 318 | + expect(finalRequest.messages).toEqual([ |
| 319 | + { role: "user", content: "request" }, |
| 320 | + { |
| 321 | + role: "assistant", |
| 322 | + content: null, |
| 323 | + tool_calls: [{ id: "call-1", type: "function", function: { name: "side_effect", arguments: "{}" } }], |
| 324 | + }, |
| 325 | + { role: "tool", tool_call_id: "call-1", content: "complete" }, |
| 326 | + ]); |
| 327 | + expect(response.messages.map((message) => message.role)).toEqual(["assistant", "tool", "assistant"]); |
| 328 | + expect(response.text).toBe("done"); |
| 329 | + }); |
235 | 330 | }); |
0 commit comments