|
| 1 | +@testable import AgentRunKit |
| 2 | +import Foundation |
| 3 | +import Testing |
| 4 | + |
| 5 | +func assertSmokeAnthropicContinuityReplay(client: some LLMClient) async throws { |
| 6 | + let addTool = try makeSmokeAddTool() |
| 7 | + let configuration = AgentConfiguration( |
| 8 | + maxIterations: 5, |
| 9 | + systemPrompt: """ |
| 10 | + You are a calculator assistant. When asked to add numbers, call the add tool exactly once. |
| 11 | + After receiving the tool result, immediately call finish with the numeric answer. |
| 12 | + Do not answer in plain text and do not call add again. |
| 13 | + """ |
| 14 | + ) |
| 15 | + let agent = Agent<EmptyContext>(client: client, tools: [addTool], configuration: configuration) |
| 16 | + |
| 17 | + var firstHistory: [ChatMessage]? |
| 18 | + for try await event in agent.stream(userMessage: "What is 17 + 25?", context: EmptyContext()) { |
| 19 | + guard case let .finished(_, _, _, history) = event.kind else { continue } |
| 20 | + firstHistory = history |
| 21 | + } |
| 22 | + let history = try #require(firstHistory) |
| 23 | + |
| 24 | + let replayableAssistant = history.compactMap { message -> AssistantMessage? in |
| 25 | + guard case let .assistant(assistant) = message else { return nil } |
| 26 | + guard assistant.continuity?.substrate == .anthropicMessages else { return nil } |
| 27 | + return assistant |
| 28 | + }.first |
| 29 | + let continuity = try #require(replayableAssistant?.continuity) |
| 30 | + |
| 31 | + #expect(continuity.substrate == .anthropicMessages) |
| 32 | + guard case let .object(payload) = continuity.payload, |
| 33 | + case let .array(blocks) = payload["content"] |
| 34 | + else { |
| 35 | + Issue.record("Expected Anthropic continuity payload with content array") |
| 36 | + return |
| 37 | + } |
| 38 | + #expect(!blocks.isEmpty) |
| 39 | + |
| 40 | + let secondResult = try await agent.run( |
| 41 | + userMessage: "What was the previous sum? Reply with just the number.", |
| 42 | + history: history, |
| 43 | + context: EmptyContext() |
| 44 | + ) |
| 45 | + #expect(try requireContent(secondResult).contains("42")) |
| 46 | +} |
0 commit comments