Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/api/providers/__tests__/anthropic-vertex.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,41 @@ describe("VertexHandler", () => {
const result = await handler.completePrompt("Test prompt")
expect(result).toBe("")
})

it("should handle empty content array for Claude", async () => {
handler = new AnthropicVertexHandler({
apiModelId: "claude-3-5-sonnet-v2@20241022",
vertexProjectId: "test-project",
vertexRegion: "us-central1",
})

const mockCreate = vitest.fn().mockResolvedValue({
content: [],
})
;(handler["client"].messages as any).create = mockCreate

const result = await handler.completePrompt("Test prompt")
expect(result).toBe("")
})

it("should return text from first text block when mixed content for Claude", async () => {
handler = new AnthropicVertexHandler({
apiModelId: "claude-3-5-sonnet-v2@20241022",
vertexProjectId: "test-project",
vertexRegion: "us-central1",
})

const mockCreate = vitest.fn().mockResolvedValue({
content: [
{ type: "thinking", thinking: "internal reasoning" },
{ type: "text", text: "visible response" },
],
})
;(handler["client"].messages as any).create = mockCreate

const result = await handler.completePrompt("Test prompt")
expect(result).toBe("visible response")
})
})

describe("getModel", () => {
Expand Down Expand Up @@ -1342,7 +1377,9 @@ describe("VertexHandler", () => {
}))
;(sonnetHandler["client"].messages as any).create = mockCreate

await sonnetHandler.createMessage("You are a helpful assistant", [{ role: "user", content: "Hello" }]).next()
await sonnetHandler
.createMessage("You are a helpful assistant", [{ role: "user", content: "Hello" }])
.next()

expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
8 changes: 2 additions & 6 deletions src/api/providers/anthropic-vertex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,9 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
} as Anthropic.Messages.MessageCreateParamsNonStreaming

const response = await this.client.messages.create(params)
const content = response.content[0]
const content = response.content.find(({ type }) => type === "text")

if (content.type === "text") {
return content.text
}

return ""
return content?.type === "text" ? content.text : ""
} catch (error) {
if (error instanceof Error) {
throw new Error(`Vertex completion error: ${error.message}`)
Expand Down
Loading