Skip to content

Commit 0409509

Browse files
fixes: 2229 - completePrompt doesn't work for bedrock. (#2230)
* fixes: 2229 - completePrompt doesn't work for bedrock. * fix failing test
1 parent 1b25190 commit 0409509

File tree

3 files changed

+79
-38
lines changed

3 files changed

+79
-38
lines changed

src/api/providers/__tests__/bedrock-custom-arn.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,20 @@ import { ApiHandlerOptions } from "../../../shared/api"
33

44
// Mock the AWS SDK
55
jest.mock("@aws-sdk/client-bedrock-runtime", () => {
6+
const mockResponse = {
7+
output: {
8+
message: {
9+
content: [
10+
{
11+
text: "Test response",
12+
},
13+
],
14+
},
15+
},
16+
}
17+
618
const mockSend = jest.fn().mockImplementation(() => {
7-
return Promise.resolve({
8-
output: new TextEncoder().encode(JSON.stringify({ content: "Test response" })),
9-
})
19+
return Promise.resolve(mockResponse)
1020
})
1121

1222
return {

src/api/providers/__tests__/bedrock.test.ts

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,20 @@ describe("AwsBedrockHandler", () => {
399399
})
400400
})
401401

402+
//response.output.message.content[0].text
403+
402404
describe("completePrompt", () => {
403405
it("should complete prompt successfully", async () => {
404406
const mockResponse = {
405-
output: new TextEncoder().encode(
406-
JSON.stringify({
407-
content: "Test response",
408-
}),
409-
),
407+
output: {
408+
message: {
409+
content: [
410+
{
411+
text: "Test response",
412+
},
413+
],
414+
},
415+
},
410416
}
411417

412418
const mockSend = jest.fn().mockResolvedValue(mockResponse)
@@ -450,7 +456,9 @@ describe("AwsBedrockHandler", () => {
450456

451457
it("should handle invalid response format", async () => {
452458
const mockResponse = {
453-
output: new TextEncoder().encode("invalid json"),
459+
output: {
460+
message: {},
461+
},
454462
}
455463

456464
const mockSend = jest.fn().mockResolvedValue(mockResponse)
@@ -464,9 +472,16 @@ describe("AwsBedrockHandler", () => {
464472

465473
it("should handle empty response", async () => {
466474
const mockResponse = {
467-
output: new TextEncoder().encode(JSON.stringify({})),
475+
output: {
476+
message: {
477+
content: [
478+
{
479+
text: "",
480+
},
481+
],
482+
},
483+
},
468484
}
469-
470485
const mockSend = jest.fn().mockResolvedValue(mockResponse)
471486
handler["client"] = {
472487
send: mockSend,
@@ -486,11 +501,15 @@ describe("AwsBedrockHandler", () => {
486501
})
487502

488503
const mockResponse = {
489-
output: new TextEncoder().encode(
490-
JSON.stringify({
491-
content: "Test response",
492-
}),
493-
),
504+
output: {
505+
message: {
506+
content: [
507+
{
508+
text: "Test response",
509+
},
510+
],
511+
},
512+
},
494513
}
495514

496515
const mockSend = jest.fn().mockResolvedValue(mockResponse)
@@ -519,11 +538,15 @@ describe("AwsBedrockHandler", () => {
519538
})
520539

521540
const mockResponse = {
522-
output: new TextEncoder().encode(
523-
JSON.stringify({
524-
content: "Test response",
525-
}),
526-
),
541+
output: {
542+
message: {
543+
content: [
544+
{
545+
text: "Test response",
546+
},
547+
],
548+
},
549+
},
527550
}
528551

529552
const mockSend = jest.fn().mockResolvedValue(mockResponse)
@@ -552,13 +575,16 @@ describe("AwsBedrockHandler", () => {
552575
})
553576

554577
const mockResponse = {
555-
output: new TextEncoder().encode(
556-
JSON.stringify({
557-
content: "Test response",
558-
}),
559-
),
578+
output: {
579+
message: {
580+
content: [
581+
{
582+
text: "Test response",
583+
},
584+
],
585+
},
586+
},
560587
}
561-
562588
const mockSend = jest.fn().mockResolvedValue(mockResponse)
563589
handler["client"] = {
564590
send: mockSend,
@@ -585,11 +611,15 @@ describe("AwsBedrockHandler", () => {
585611
})
586612

587613
const mockResponse = {
588-
output: new TextEncoder().encode(
589-
JSON.stringify({
590-
content: "Test response",
591-
}),
592-
),
614+
output: {
615+
message: {
616+
content: [
617+
{
618+
text: "Test response",
619+
},
620+
],
621+
},
622+
},
593623
}
594624

595625
const mockSend = jest.fn().mockResolvedValue(mockResponse)

src/api/providers/bedrock.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,14 @@ Please check:
665665
const command = new ConverseCommand(payload)
666666
const response = await this.client.send(command)
667667

668-
if (response.output && response.output instanceof Uint8Array) {
668+
if (
669+
response?.output?.message?.content &&
670+
response.output.message.content.length > 0 &&
671+
response.output.message.content[0].text &&
672+
response.output.message.content[0].text.trim().length > 0
673+
) {
669674
try {
670-
const outputStr = new TextDecoder().decode(response.output)
671-
const output = JSON.parse(outputStr)
672-
if (output.content) {
673-
return output.content
674-
}
675+
return response.output.message.content[0].text
675676
} catch (parseError) {
676677
logger.error("Failed to parse Bedrock response", {
677678
ctx: "bedrock",

0 commit comments

Comments
 (0)