Skip to content

Commit 215d410

Browse files
committed
feat: add an opt-in raw API dump for Anthropic, Vertex Claude and Bedrock
Records the unmodified provider request and every stream event before Zoo normalises them, so empty thinking blocks, signatures and stop reasons that the chat view drops can be inspected. Two independent settings under Settings > About: - rawApiDumpToTaskFile: append JSONL to raw_api_dump.jsonl in the task dir - rawApiDumpToOutputChannel: print to the "Zoo Code Raw API" output panel See also Zoo-Code-Org#1820.
1 parent def27e1 commit 215d410

37 files changed

Lines changed: 708 additions & 35 deletions

‎packages/types/src/global-settings.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,16 @@ export const globalSettingsSchema = z.object({
246246
execaShellPath: z.string().optional(),
247247

248248
diagnosticsEnabled: z.boolean().optional(),
249+
/**
250+
* Append each raw provider request and stream event to `raw_api_dump.jsonl` in the task directory.
251+
* @default false
252+
*/
253+
rawApiDumpToTaskFile: z.boolean().optional(),
254+
/**
255+
* Print each raw provider request and stream event to the "Zoo Code Raw API" output channel.
256+
* @default false
257+
*/
258+
rawApiDumpToOutputChannel: z.boolean().optional(),
249259
autoCloseZooOpenedFiles: z.boolean().optional(),
250260
autoCloseZooOpenedFilesAfterUserEdited: z.boolean().optional(),
251261
autoCloseZooOpenedNewFiles: z.boolean().optional(),

‎packages/types/src/vscode-extension-host.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ export type ExtensionState = Pick<
308308
| "terminalProfile"
309309
| "execaShellPath"
310310
| "diagnosticsEnabled"
311+
| "rawApiDumpToTaskFile"
312+
| "rawApiDumpToOutputChannel"
311313
| "autoCloseZooOpenedFiles"
312314
| "autoCloseZooOpenedFilesAfterUserEdited"
313315
| "autoCloseZooOpenedNewFiles"

‎src/api/index.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@roo-code/types"
1111

1212
import { getRouterRemovalMessage } from "../core/config/routerRemoval"
13+
import type { RawApiDump } from "../utils/rawApiDump"
1314
import { ApiStream } from "./transform/stream"
1415

1516
import {
@@ -115,6 +116,11 @@ export interface ApiHandlerCreateMessageMetadata {
115116
* when the user clicks stop, preventing wasted API tokens/compute on the provider side.
116117
*/
117118
abortSignal?: AbortSignal
119+
/**
120+
* Debug sink for the unmodified provider request and stream events.
121+
* Only set when the user enabled raw API dumping.
122+
*/
123+
rawApiDump?: RawApiDump
118124
}
119125

120126
export interface ApiHandler {

‎src/api/providers/__tests__/anthropic.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { AnthropicHandler } from "../anthropic"
44
import { ApiHandlerOptions } from "../../../shared/api"
55
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
6+
import { makeRecordingRawApiDump } from "../../../test-utils/api"
67
import { clearAllMocks } from "../../../test-utils/reset"
78

89
// Mock TelemetryService
@@ -1243,6 +1244,28 @@ describe("AnthropicHandler", () => {
12431244
)
12441245
})
12451246

1247+
it("hands the request and every raw stream event, including empty thinking blocks, to the raw API dump", async () => {
1248+
const events = [
1249+
{ type: "message_start", message: { usage: { input_tokens: 1, output_tokens: 0 } } },
1250+
{ type: "content_block_start", index: 0, content_block: { type: "thinking", thinking: "" } },
1251+
{ type: "content_block_delta", index: 0, delta: { type: "signature_delta", signature: "sig" } },
1252+
{ type: "content_block_stop", index: 0 },
1253+
]
1254+
mockCreate.mockImplementationOnce(async () => asyncStreamFrom(events))
1255+
const rawApiDump = makeRecordingRawApiDump()
1256+
1257+
await collectStream(handler.createMessage(systemPrompt, messages, { taskId: "test-task", rawApiDump }))
1258+
1259+
expect(rawApiDump.records[0]).toMatchObject({
1260+
provider: "Anthropic",
1261+
kind: "request",
1262+
data: { params: expect.objectContaining({ model: mockOptions.apiModelId, stream: true }) },
1263+
})
1264+
expect(rawApiDump.records.slice(1)).toEqual(
1265+
events.map((data) => ({ provider: "Anthropic", kind: "event", data })),
1266+
)
1267+
})
1268+
12461269
it("should handle tool_use blocks in stream and emit tool_call_partial", async () => {
12471270
mockCreate.mockImplementationOnce(async () =>
12481271
asyncStreamFrom([

‎src/api/providers/__tests__/bedrock.stream-diagnostics.spec.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import type { Anthropic } from "@anthropic-ai/sdk"
4141
import { AwsBedrockHandler, BEDROCK_STREAM_EXCEPTION_KEYS } from "../bedrock"
4242
import { OutputTokenLimitError } from "../utils/output-token-limit-error"
4343
import type { ApiStreamChunk } from "../../transform/stream"
44-
import { makeCreateMessageMetadata } from "../../../test-utils/api"
44+
import { makeCreateMessageMetadata, makeRecordingRawApiDump } from "../../../test-utils/api"
4545
import { clearAllMocks } from "../../../test-utils/reset"
4646

4747
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello" }]
@@ -160,4 +160,59 @@ describe("AwsBedrockHandler stream diagnostics", () => {
160160
])
161161
})
162162
})
163+
164+
describe("raw API dump", () => {
165+
it("records the request payload and every stream event before normalisation", async () => {
166+
// An empty reasoning block and a bare signature delta both yield no chunk.
167+
const events = [
168+
{ contentBlockStart: { contentBlockIndex: 0, start: { reasoningContent: {} } } },
169+
{ contentBlockDelta: { contentBlockIndex: 0, delta: { reasoningContent: { signature: "sig" } } } },
170+
{ contentBlockDelta: { contentBlockIndex: 1, delta: { text: "hi" } } },
171+
{ messageStop: { stopReason: "end_turn" } },
172+
]
173+
const rawApiDump = makeRecordingRawApiDump()
174+
const handler = createHandler(events)
175+
176+
const chunks: ApiStreamChunk[] = []
177+
for await (const chunk of handler.createMessage(
178+
"system",
179+
messages,
180+
makeCreateMessageMetadata({ rawApiDump }),
181+
)) {
182+
chunks.push(chunk)
183+
}
184+
185+
expect(chunks.filter((c) => c.type === "text")).toEqual([{ type: "text", text: "hi" }])
186+
expect(rawApiDump.records[0]).toMatchObject({
187+
provider: "Bedrock",
188+
kind: "request",
189+
data: expect.objectContaining({ modelId: expect.any(String), messages: expect.any(Array) }),
190+
})
191+
expect(rawApiDump.records.slice(1)).toEqual(
192+
events.map((data) => ({ provider: "Bedrock", kind: "event", data })),
193+
)
194+
})
195+
196+
it("records stream errors", async () => {
197+
const rawApiDump = makeRecordingRawApiDump()
198+
const handler = createHandler([{ validationException: { message: "bad" } }])
199+
200+
await expect(
201+
(async () => {
202+
for await (const _ of handler.createMessage(
203+
"system",
204+
messages,
205+
makeCreateMessageMetadata({ rawApiDump }),
206+
)) {
207+
// drain
208+
}
209+
})(),
210+
).rejects.toThrow()
211+
212+
expect(rawApiDump.records.at(-1)).toMatchObject({
213+
kind: "error",
214+
data: expect.objectContaining({ message: expect.stringContaining("bad") }),
215+
})
216+
})
217+
})
163218
})

‎src/api/providers/anthropic-vertex.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
116116
// and prompt caching
117117
const requestOptions = betas?.length ? { headers: { "anthropic-beta": betas.join(",") } } : undefined
118118

119+
metadata?.rawApiDump?.request("AnthropicVertex", { params, requestOptions })
119120
const stream = await this.client.messages.create(params, requestOptions)
120121

121122
for await (const chunk of stream) {
123+
metadata?.rawApiDump?.event("AnthropicVertex", chunk)
122124
switch (chunk.type) {
123125
case "message_start": {
124126
const usage = chunk.message!.usage

‎src/api/providers/anthropic.ts‎

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -179,43 +179,46 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
179179
stream: true,
180180
...nativeToolParams,
181181
}
182+
const requestOptions = (() => {
183+
// prompt caching: https://x.com/alexalbert__/status/1823751995901272068
184+
// https://github.com/anthropics/anthropic-sdk-typescript?tab=readme-ov-file#default-headers
185+
// https://github.com/anthropics/anthropic-sdk-typescript/commit/c920b77fc67bd839bfeb6716ceab9d7c9bbe7393
186+
187+
// Then check for models that support prompt caching
188+
switch (modelId) {
189+
case "claude-sonnet-5":
190+
case "claude-sonnet-4-6":
191+
case "claude-sonnet-4-5":
192+
case "claude-sonnet-4-20250514":
193+
case "claude-opus-4-6":
194+
case "claude-opus-4-7":
195+
case "claude-opus-4-8":
196+
case "claude-opus-5":
197+
case "claude-opus-5-5":
198+
case "claude-fable-5-1":
199+
case "claude-fable-5":
200+
case "claude-opus-4-5-20251101":
201+
case "claude-opus-4-1-20250805":
202+
case "claude-opus-4-20250514":
203+
case "claude-3-7-sonnet-20250219":
204+
case "claude-3-5-sonnet-20241022":
205+
case "claude-3-5-haiku-20241022":
206+
case "claude-3-opus-20240229":
207+
case "claude-haiku-4-5-20251001":
208+
case "claude-3-haiku-20240307":
209+
betas.push("prompt-caching-2024-07-31")
210+
return { headers: { "anthropic-beta": betas.join(",") } }
211+
default:
212+
return undefined
213+
}
214+
})()
215+
metadata?.rawApiDump?.request(this.providerName, { params: requestParams, requestOptions })
182216
stream = await this.client.messages.create(
183217
requestParams as Anthropic.Messages.MessageCreateParamsStreaming,
184-
(() => {
185-
// prompt caching: https://x.com/alexalbert__/status/1823751995901272068
186-
// https://github.com/anthropics/anthropic-sdk-typescript?tab=readme-ov-file#default-headers
187-
// https://github.com/anthropics/anthropic-sdk-typescript/commit/c920b77fc67bd839bfeb6716ceab9d7c9bbe7393
188-
189-
// Then check for models that support prompt caching
190-
switch (modelId) {
191-
case "claude-sonnet-5":
192-
case "claude-sonnet-4-6":
193-
case "claude-sonnet-4-5":
194-
case "claude-sonnet-4-20250514":
195-
case "claude-opus-4-6":
196-
case "claude-opus-4-7":
197-
case "claude-opus-4-8":
198-
case "claude-opus-5":
199-
case "claude-opus-5-5":
200-
case "claude-fable-5-1":
201-
case "claude-fable-5":
202-
case "claude-opus-4-5-20251101":
203-
case "claude-opus-4-1-20250805":
204-
case "claude-opus-4-20250514":
205-
case "claude-3-7-sonnet-20250219":
206-
case "claude-3-5-sonnet-20241022":
207-
case "claude-3-5-haiku-20241022":
208-
case "claude-3-opus-20240229":
209-
case "claude-haiku-4-5-20251001":
210-
case "claude-3-haiku-20240307":
211-
betas.push("prompt-caching-2024-07-31")
212-
return { headers: { "anthropic-beta": betas.join(",") } }
213-
default:
214-
return undefined
215-
}
216-
})(),
218+
requestOptions,
217219
)
218220
} catch (error) {
221+
metadata?.rawApiDump?.error(this.providerName, error)
219222
TelemetryService.instance.captureException(
220223
new ApiProviderError(
221224
error instanceof Error ? error.message : String(error),
@@ -240,10 +243,12 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
240243
stream: true,
241244
...nativeToolParams,
242245
}
246+
metadata?.rawApiDump?.request(this.providerName, { params: requestParams })
243247
stream = (await this.client.messages.create(
244248
requestParams as Anthropic.Messages.MessageCreateParamsStreaming,
245249
)) as any
246250
} catch (error) {
251+
metadata?.rawApiDump?.error(this.providerName, error)
247252
TelemetryService.instance.captureException(
248253
new ApiProviderError(
249254
error instanceof Error ? error.message : String(error),
@@ -264,6 +269,7 @@ export class AnthropicHandler extends BaseProvider implements SingleCompletionHa
264269
let cacheReadTokens = 0
265270

266271
for await (const chunk of stream) {
272+
metadata?.rawApiDump?.event(this.providerName, chunk)
267273
switch (chunk.type) {
268274
case "message_start": {
269275
// Tells us cache reads/writes/input/output.

‎src/api/providers/bedrock.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
621621
10 * 60 * 1000,
622622
)
623623

624+
metadata?.rawApiDump?.request(this.providerName, payload)
624625
const command = new ConverseStreamCommand(payload)
625626
const response = await this.client.send(command, {
626627
abortSignal: requestController.signal,
@@ -633,6 +634,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
633634

634635
let outputLimitReached = false
635636
for await (const chunk of response.stream) {
637+
metadata?.rawApiDump?.event(this.providerName, chunk)
636638
// Parse the chunk as JSON if it's a string (for tests)
637639
let streamEvent: StreamEvent
638640
try {
@@ -848,6 +850,7 @@ export class AwsBedrockHandler extends BaseProvider implements SingleCompletionH
848850
} catch (error: unknown) {
849851
// Clear timeout on error
850852
clearTimeout(timeoutId)
853+
metadata?.rawApiDump?.error(this.providerName, error)
851854

852855
// Capture error in telemetry before processing
853856
const errorMessage = error instanceof Error ? error.message : String(error)

‎src/core/task/Task.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import { calculateApiCostAnthropic, calculateApiCostOpenAI } from "../../shared/
101101
import { getWorkspacePath } from "../../utils/path"
102102
import { sanitizeToolUseId } from "../../utils/tool-id"
103103
import { getTaskDirectoryPath } from "../../utils/storage"
104+
import { createRawApiDump } from "../../utils/rawApiDump"
104105
import { logger } from "../../utils/logging"
105106

106107
// prompts
@@ -5729,11 +5730,19 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
57295730
this.currentRequestAbortController = new AbortController()
57305731
const abortSignal = this.currentRequestAbortController.signal
57315732

5733+
const rawApiDump = createRawApiDump({
5734+
taskId: this.taskId,
5735+
globalStoragePath: this.globalStoragePath,
5736+
toTaskFile: state?.rawApiDumpToTaskFile ?? false,
5737+
toOutputChannel: state?.rawApiDumpToOutputChannel ?? false,
5738+
})
5739+
57325740
const metadata: ApiHandlerCreateMessageMetadata = {
57335741
mode: mode,
57345742
taskId: this.taskId,
57355743
suppressPreviousResponseId: this.skipPrevResponseIdOnce,
57365744
abortSignal,
5745+
...(rawApiDump ? { rawApiDump } : {}),
57375746
// Include tools whenever they are present.
57385747
...(shouldIncludeTools
57395748
? {

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,56 @@ describe("Cline", () => {
20312031
expect(systemPromptCall[16]).toEqual(["execute_command"])
20322032
})
20332033

2034+
it.each([
2035+
[{ rawApiDumpToTaskFile: false, rawApiDumpToOutputChannel: false }, false],
2036+
[{ rawApiDumpToTaskFile: true, rawApiDumpToOutputChannel: false }, true],
2037+
[{ rawApiDumpToTaskFile: false, rawApiDumpToOutputChannel: true }, true],
2038+
])("passes a raw API dump to the provider only when enabled (%o)", async (dumpSettings, expectDump) => {
2039+
const task = new Task({
2040+
provider: mockProvider,
2041+
apiConfiguration: mockApiConfig,
2042+
task: "test task",
2043+
startTask: false,
2044+
})
2045+
await task.getTaskMode()
2046+
2047+
const realState = await mockProvider.getState()
2048+
vi.spyOn(mockProvider, "getState").mockResolvedValue({
2049+
...realState,
2050+
mcpEnabled: false,
2051+
autoApprovalEnabled: false,
2052+
...dumpSettings,
2053+
})
2054+
vi.spyOn(task.diffViewProvider, "reset").mockResolvedValue(undefined)
2055+
const createMessageSpy = vi.spyOn(task.api, "createMessage").mockReturnValue({
2056+
async *[Symbol.asyncIterator]() {
2057+
yield { type: "text", text: "ok" }
2058+
},
2059+
async next() {
2060+
return { done: true, value: undefined }
2061+
},
2062+
async return() {
2063+
return { done: true, value: undefined }
2064+
},
2065+
async throw(error: unknown) {
2066+
throw error
2067+
},
2068+
async [Symbol.asyncDispose]() {},
2069+
} as AsyncGenerator<ApiStreamChunk>)
2070+
vi.mocked(SYSTEM_PROMPT).mockResolvedValueOnce("mock system prompt")
2071+
2072+
await task.attemptApiRequest(0).next()
2073+
2074+
const metadata = requireDefined(createMessageSpy.mock.calls.at(-1))[2]
2075+
if (expectDump) {
2076+
expect(metadata?.rawApiDump).toEqual(
2077+
expect.objectContaining({ request: expect.any(Function), event: expect.any(Function) }),
2078+
)
2079+
} else {
2080+
expect(metadata).not.toHaveProperty("rawApiDump")
2081+
}
2082+
})
2083+
20342084
it("threads the captured state snapshot into the system prompt when manually condensing", async () => {
20352085
// condenseContext captures provider state once and threads it into
20362086
// getSystemPrompt so the prompt and the condensing tool array resolve

0 commit comments

Comments
 (0)