|
1 | 1 | import { Anthropic } from "@anthropic-ai/sdk" |
2 | | -import { appendEnvironmentDetails, removeEnvironmentDetailsBlocks, UserContentBlock } from "../appendEnvironmentDetails" |
| 2 | +import { |
| 3 | + appendEnvironmentDetails, |
| 4 | + removeEnvironmentDetailsBlocks, |
| 5 | + stripAppendedEnvironmentDetails, |
| 6 | + UserContentBlock, |
| 7 | +} from "../appendEnvironmentDetails" |
3 | 8 |
|
4 | 9 | describe("appendEnvironmentDetails", () => { |
5 | 10 | const envDetails = "<environment_details>\n# Test\nSome details\n</environment_details>" |
@@ -314,3 +319,96 @@ describe("removeEnvironmentDetailsBlocks", () => { |
314 | 319 | expect(result).toHaveLength(0) |
315 | 320 | }) |
316 | 321 | }) |
| 322 | + |
| 323 | +describe("stripAppendedEnvironmentDetails", () => { |
| 324 | + const envDetails = "<environment_details>\n# Test\nSome details\n</environment_details>" |
| 325 | + |
| 326 | + it("should strip environment details from the end of a text block", () => { |
| 327 | + const content: UserContentBlock[] = [{ type: "text", text: "User message\n\n" + envDetails }] |
| 328 | + |
| 329 | + const result = stripAppendedEnvironmentDetails(content) |
| 330 | + |
| 331 | + expect(result).toHaveLength(1) |
| 332 | + expect((result[0] as Anthropic.Messages.TextBlockParam).text).toBe("User message") |
| 333 | + }) |
| 334 | + |
| 335 | + it("should strip environment details from tool_result string content", () => { |
| 336 | + const content: UserContentBlock[] = [ |
| 337 | + { |
| 338 | + type: "tool_result", |
| 339 | + tool_use_id: "tool-123", |
| 340 | + content: "Tool result\n\n" + envDetails, |
| 341 | + }, |
| 342 | + ] |
| 343 | + |
| 344 | + const result = stripAppendedEnvironmentDetails(content) |
| 345 | + |
| 346 | + expect(result).toHaveLength(1) |
| 347 | + expect((result[0] as Anthropic.Messages.ToolResultBlockParam).content).toBe("Tool result") |
| 348 | + }) |
| 349 | + |
| 350 | + it("should strip environment details from tool_result array content", () => { |
| 351 | + const content: UserContentBlock[] = [ |
| 352 | + { |
| 353 | + type: "tool_result", |
| 354 | + tool_use_id: "tool-123", |
| 355 | + content: [{ type: "text", text: "Result text\n\n" + envDetails }], |
| 356 | + }, |
| 357 | + ] |
| 358 | + |
| 359 | + const result = stripAppendedEnvironmentDetails(content) |
| 360 | + |
| 361 | + const toolResult = result[0] as Anthropic.Messages.ToolResultBlockParam |
| 362 | + const contentArray = toolResult.content as Anthropic.Messages.TextBlockParam[] |
| 363 | + expect(contentArray[0].text).toBe("Result text") |
| 364 | + }) |
| 365 | + |
| 366 | + it("should also remove standalone environment_details blocks", () => { |
| 367 | + const content: UserContentBlock[] = [ |
| 368 | + { type: "text", text: "User message" }, |
| 369 | + { type: "text", text: envDetails }, |
| 370 | + ] |
| 371 | + |
| 372 | + const result = stripAppendedEnvironmentDetails(content) |
| 373 | + |
| 374 | + expect(result).toHaveLength(1) |
| 375 | + expect((result[0] as Anthropic.Messages.TextBlockParam).text).toBe("User message") |
| 376 | + }) |
| 377 | + |
| 378 | + it("should handle content without environment details", () => { |
| 379 | + const content: UserContentBlock[] = [ |
| 380 | + { type: "text", text: "User message" }, |
| 381 | + { |
| 382 | + type: "tool_result", |
| 383 | + tool_use_id: "tool-123", |
| 384 | + content: "Tool result", |
| 385 | + }, |
| 386 | + ] |
| 387 | + |
| 388 | + const result = stripAppendedEnvironmentDetails(content) |
| 389 | + |
| 390 | + expect(result).toEqual(content) |
| 391 | + }) |
| 392 | + |
| 393 | + it("should handle empty content", () => { |
| 394 | + const result = stripAppendedEnvironmentDetails([]) |
| 395 | + expect(result).toHaveLength(0) |
| 396 | + }) |
| 397 | + |
| 398 | + it("should preserve is_error flag when stripping from tool_result", () => { |
| 399 | + const content: UserContentBlock[] = [ |
| 400 | + { |
| 401 | + type: "tool_result", |
| 402 | + tool_use_id: "tool-123", |
| 403 | + content: "Error\n\n" + envDetails, |
| 404 | + is_error: true, |
| 405 | + }, |
| 406 | + ] |
| 407 | + |
| 408 | + const result = stripAppendedEnvironmentDetails(content) |
| 409 | + |
| 410 | + const toolResult = result[0] as Anthropic.Messages.ToolResultBlockParam |
| 411 | + expect(toolResult.is_error).toBe(true) |
| 412 | + expect(toolResult.content).toBe("Error") |
| 413 | + }) |
| 414 | +}) |
0 commit comments