From 3376d08fd9660102986c0036b34f8c3178173b40 Mon Sep 17 00:00:00 2001 From: Ryan Cormack Date: Wed, 21 May 2025 17:34:22 +0100 Subject: [PATCH] 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) --- .../typescript/src/server_clients/server.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/chatbots/typescript/src/server_clients/server.ts b/examples/chatbots/typescript/src/server_clients/server.ts index 9146862..a2eeb0d 100644 --- a/examples/chatbots/typescript/src/server_clients/server.ts +++ b/examples/chatbots/typescript/src/server_clients/server.ts @@ -99,7 +99,7 @@ export abstract class Server { return { toolUseId: toolUseId, - content: [{ text: String(result) }], + content: [{ text: this.formatText(result) }], status: "success", }; } catch (e) { @@ -124,4 +124,16 @@ export abstract class Server { // This should never be reached due to the loop above throw new Error("Unexpected error in executeTool"); } + + formatText(text: unknown): string { + if (typeof text === "string") { + return text; + } else if (Array.isArray(text)) { + return text.map((item) => this.formatText(item)).join("\n"); + } else if (typeof text === "object" && text !== null) { + return JSON.stringify(text); + } else { + return String(text); + } + } }