@botbotgo/better-call 0.1.41 → 0.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/demo.mjs +56 -31
package/package.json
CHANGED
package/scripts/demo.mjs
CHANGED
|
@@ -8,6 +8,18 @@ function errorMessage(error) {
|
|
|
8
8
|
return error instanceof Error ? error.message : String(error);
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
function createGatewayErrorResult(originalToolName, reason, details = {}) {
|
|
12
|
+
return {
|
|
13
|
+
status: "error",
|
|
14
|
+
originalToolName,
|
|
15
|
+
toolName: null,
|
|
16
|
+
reason,
|
|
17
|
+
errorType: details.type ?? null,
|
|
18
|
+
errorMessage: details.message ?? null,
|
|
19
|
+
errorCause: details.cause ?? null,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
11
23
|
function errorDetails(error) {
|
|
12
24
|
if (error instanceof Error) {
|
|
13
25
|
const causeMessage =
|
|
@@ -86,13 +98,38 @@ const stockQuote = {
|
|
|
86
98
|
|
|
87
99
|
// Intentionally malformed repair input: "symbol" should be "ticker", and
|
|
88
100
|
// "NASDAQ" is not one of the schema-valid markets ("US", "HK", or "CN").
|
|
89
|
-
const
|
|
101
|
+
const intentionallyMalformedArgs = { symbol: "Apple", market: "NASDAQ" };
|
|
102
|
+
|
|
103
|
+
function repairDemoStockCall(repairInput = {}) {
|
|
104
|
+
const attemptedArgs =
|
|
105
|
+
Array.isArray(repairInput?.calls) && repairInput.calls[0]?.args != null
|
|
106
|
+
? repairInput.calls[0].args
|
|
107
|
+
: intentionallyMalformedArgs;
|
|
108
|
+
|
|
109
|
+
const attemptedTicker =
|
|
110
|
+
typeof attemptedArgs.ticker === "string"
|
|
111
|
+
? attemptedArgs.ticker
|
|
112
|
+
: typeof attemptedArgs.symbol === "string"
|
|
113
|
+
? attemptedArgs.symbol
|
|
114
|
+
: "AAPL";
|
|
115
|
+
const normalizedTicker = attemptedTicker.trim().toLowerCase();
|
|
116
|
+
const ticker =
|
|
117
|
+
normalizedTicker === "apple" || normalizedTicker === ""
|
|
118
|
+
? "AAPL"
|
|
119
|
+
: normalizedTicker.toUpperCase();
|
|
120
|
+
|
|
121
|
+
const attemptedMarket = typeof attemptedArgs.market === "string" ? attemptedArgs.market : null;
|
|
122
|
+
const market =
|
|
123
|
+
attemptedMarket != null && VALID_MARKETS.includes(attemptedMarket)
|
|
124
|
+
? attemptedMarket
|
|
125
|
+
: "US";
|
|
126
|
+
|
|
127
|
+
return [{ tool: "stock_quote", args: { ticker, market } }];
|
|
128
|
+
}
|
|
90
129
|
|
|
91
130
|
const wrappedTools = betterTools([stockQuote], {
|
|
92
131
|
userInput: "Get Apple stock in the US market.",
|
|
93
|
-
repair
|
|
94
|
-
return [{ tool: "stock_quote", args: { ticker: "AAPL", market: "US" } }];
|
|
95
|
-
},
|
|
132
|
+
repair: repairDemoStockCall,
|
|
96
133
|
});
|
|
97
134
|
|
|
98
135
|
if (!Array.isArray(wrappedTools) || wrappedTools.length === 0) {
|
|
@@ -106,9 +143,9 @@ const wrappedStockQuote = wrappedTools[0];
|
|
|
106
143
|
|
|
107
144
|
let wrappedOutput;
|
|
108
145
|
try {
|
|
109
|
-
wrappedOutput = await wrappedStockQuote.invoke(
|
|
146
|
+
wrappedOutput = await wrappedStockQuote.invoke(intentionallyMalformedArgs);
|
|
110
147
|
} catch (error) {
|
|
111
|
-
const context = `tool=stock_quote args=${JSON.stringify(
|
|
148
|
+
const context = `tool=stock_quote args=${JSON.stringify(intentionallyMalformedArgs)}`;
|
|
112
149
|
const message = `${errorMessage(error)} (${context})`;
|
|
113
150
|
const contextualError =
|
|
114
151
|
error instanceof Error ? new Error(message, { cause: error }) : new Error(message);
|
|
@@ -121,12 +158,10 @@ try {
|
|
|
121
158
|
userInput: "Get Apple stock in the US market.",
|
|
122
159
|
tools: [stockQuote],
|
|
123
160
|
// Both the tool name and args are intentionally wrong to demonstrate repair:
|
|
124
|
-
// "stock_price" should be "stock_quote";
|
|
161
|
+
// "stock_price" should be "stock_quote"; intentionallyMalformedArgs uses
|
|
125
162
|
// "symbol"/"NASDAQ" instead of schema-valid stock_quote args.
|
|
126
|
-
calls: [{ tool: "stock_price", args:
|
|
127
|
-
repair
|
|
128
|
-
return [{ tool: "stock_quote", args: { ticker: "AAPL", market: "US" } }];
|
|
129
|
-
},
|
|
163
|
+
calls: [{ tool: "stock_price", args: intentionallyMalformedArgs }],
|
|
164
|
+
repair: repairDemoStockCall,
|
|
130
165
|
});
|
|
131
166
|
} catch (error) {
|
|
132
167
|
exitWithError("tool-call repair", error);
|
|
@@ -157,31 +192,21 @@ try {
|
|
|
157
192
|
});
|
|
158
193
|
} catch (error) {
|
|
159
194
|
const details = errorDetails(error);
|
|
160
|
-
gatewayResult =
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
errorType: details.type,
|
|
166
|
-
errorMessage: details.message,
|
|
167
|
-
errorCause: details.cause,
|
|
168
|
-
};
|
|
195
|
+
gatewayResult = createGatewayErrorResult(
|
|
196
|
+
"research",
|
|
197
|
+
`repairToolCall failed: ${details.message}`,
|
|
198
|
+
details
|
|
199
|
+
);
|
|
169
200
|
}
|
|
170
201
|
|
|
171
202
|
const gatewayRepair =
|
|
172
203
|
gatewayResult != null && typeof gatewayResult === "object"
|
|
173
204
|
? gatewayResult
|
|
174
|
-
:
|
|
175
|
-
status: "error",
|
|
176
|
-
originalToolName: "research",
|
|
177
|
-
toolName: null,
|
|
178
|
-
reason: "repairToolCall returned no result.",
|
|
179
|
-
errorType: null,
|
|
180
|
-
errorMessage: null,
|
|
181
|
-
errorCause: null,
|
|
182
|
-
};
|
|
205
|
+
: createGatewayErrorResult("research", "repairToolCall returned no result.");
|
|
183
206
|
|
|
184
207
|
// Missing or empty calls are expected in some demo/error paths; normalize to null for reporting.
|
|
208
|
+
// This demo reports only the first repaired call so the summary stays compact and stable.
|
|
209
|
+
// If multiple calls are present, subsequent calls are intentionally left out of this view.
|
|
185
210
|
const repairedCall =
|
|
186
211
|
Array.isArray(reliableResult?.calls) && reliableResult.calls.length > 0
|
|
187
212
|
? reliableResult.calls[0]
|
|
@@ -189,11 +214,11 @@ const repairedCall =
|
|
|
189
214
|
|
|
190
215
|
const report = {
|
|
191
216
|
wrappedTool: {
|
|
192
|
-
before:
|
|
217
|
+
before: intentionallyMalformedArgs,
|
|
193
218
|
after: wrappedOutput,
|
|
194
219
|
},
|
|
195
220
|
reliableToolCalls: {
|
|
196
|
-
before: { tool: "stock_price", args:
|
|
221
|
+
before: { tool: "stock_price", args: intentionallyMalformedArgs },
|
|
197
222
|
repaired: repairedCall,
|
|
198
223
|
diagnostics: reliableResult?.diagnostics ?? null,
|
|
199
224
|
},
|