Skip to content

Commit c0523cf

Browse files
ryancormackclareliguori
authored andcommitted
fix: Format response text from server in example client implementation
- `String(result)` often ends up calling down to toString on the object. When there is a large JSON object, it ends up with `object Object` - This formats this, attempting to stringify objects, or eventually calling back down to String(text)
1 parent cb65e12 commit c0523cf

File tree

1 file changed

+13
-1
lines changed
  • examples/chatbots/typescript/src/server_clients

1 file changed

+13
-1
lines changed

examples/chatbots/typescript/src/server_clients/server.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export abstract class Server {
9999

100100
return {
101101
toolUseId: toolUseId,
102-
content: [{ text: String(result) }],
102+
content: [{ text: this.formatText(result) }],
103103
status: "success",
104104
};
105105
} catch (e) {
@@ -124,4 +124,16 @@ export abstract class Server {
124124
// This should never be reached due to the loop above
125125
throw new Error("Unexpected error in executeTool");
126126
}
127+
128+
formatText(text: unknown): string {
129+
if (typeof text === "string") {
130+
return text;
131+
} else if (Array.isArray(text)) {
132+
return text.map((item) => this.formatText(item)).join("\n");
133+
} else if (typeof text === "object" && text !== null) {
134+
return JSON.stringify(text);
135+
} else {
136+
return String(text);
137+
}
138+
}
127139
}

0 commit comments

Comments
 (0)