@botbotgo/better-call 0.1.45 → 0.1.47
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 +30 -13
package/package.json
CHANGED
package/scripts/demo.mjs
CHANGED
|
@@ -65,6 +65,8 @@ function exitWithError(step, error) {
|
|
|
65
65
|
process.exit(1);
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
// Demo tool definition: validates ticker and market arguments before returning
|
|
69
|
+
// a normalized quote payload. Accepted markets are US, HK, and CN.
|
|
68
70
|
const stockQuote = {
|
|
69
71
|
name: "stock_quote",
|
|
70
72
|
description: "Get a stock quote.",
|
|
@@ -98,8 +100,21 @@ const stockQuote = {
|
|
|
98
100
|
|
|
99
101
|
// Intentionally malformed repair input: "symbol" should be "ticker", and
|
|
100
102
|
// "NASDAQ" is not one of the schema-valid markets ("US", "HK", or "CN").
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
+
const DEMO_MALFORMED_INPUT = { symbol: "Apple", market: "NASDAQ" };
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Repairs malformed stock quote tool-call input into one schema-valid call.
|
|
107
|
+
*
|
|
108
|
+
* Diagnostics, when present, come from the surrounding reliableToolCalls run
|
|
109
|
+
* and explain validation/repair decisions such as fallback ticker or market values.
|
|
110
|
+
*
|
|
111
|
+
* @param {object} [repairInput={}] Optional repair payload that may include
|
|
112
|
+
* `calls[0].args`.
|
|
113
|
+
* @returns {{ tool: string, args: { ticker: string, market: "US" | "HK" | "CN" } }[]}
|
|
114
|
+
* A repaired `stock_quote` call with normalized arguments. Ticker input may
|
|
115
|
+
* be provided as `ticker` or legacy `symbol`; missing values fall back to
|
|
116
|
+
* AAPL, and unsupported markets fall back to US.
|
|
117
|
+
*/
|
|
103
118
|
function repairDemoStockCall(repairInput = {}) {
|
|
104
119
|
const hasUsableArgsShape = (value) =>
|
|
105
120
|
typeof value === "object" &&
|
|
@@ -108,9 +123,11 @@ function repairDemoStockCall(repairInput = {}) {
|
|
|
108
123
|
("ticker" in value || "symbol" in value || "market" in value);
|
|
109
124
|
|
|
110
125
|
const attemptedArgs =
|
|
111
|
-
Array.isArray(repairInput?.calls) &&
|
|
126
|
+
Array.isArray(repairInput?.calls) &&
|
|
127
|
+
repairInput.calls.length > 0 &&
|
|
128
|
+
hasUsableArgsShape(repairInput.calls[0]?.args)
|
|
112
129
|
? repairInput.calls[0].args
|
|
113
|
-
:
|
|
130
|
+
: DEMO_MALFORMED_INPUT;
|
|
114
131
|
|
|
115
132
|
const attemptedTicker =
|
|
116
133
|
typeof attemptedArgs.ticker === "string"
|
|
@@ -118,9 +135,8 @@ function repairDemoStockCall(repairInput = {}) {
|
|
|
118
135
|
: typeof attemptedArgs.symbol === "string"
|
|
119
136
|
? attemptedArgs.symbol
|
|
120
137
|
: "AAPL";
|
|
121
|
-
const normalizedTicker =
|
|
122
|
-
|
|
123
|
-
// This demo falls back to AAPL when no ticker-like value is available.
|
|
138
|
+
const normalizedTicker = attemptedTicker.trim().toLowerCase();
|
|
139
|
+
// This demo maps empty input and the "apple" alias to AAPL.
|
|
124
140
|
const ticker =
|
|
125
141
|
normalizedTicker === ""
|
|
126
142
|
? "AAPL"
|
|
@@ -153,9 +169,9 @@ const wrappedStockQuote = wrappedTools[0];
|
|
|
153
169
|
|
|
154
170
|
let wrappedOutput;
|
|
155
171
|
try {
|
|
156
|
-
wrappedOutput = await wrappedStockQuote.invoke(
|
|
172
|
+
wrappedOutput = await wrappedStockQuote.invoke(DEMO_MALFORMED_INPUT);
|
|
157
173
|
} catch (error) {
|
|
158
|
-
const context = `tool=stock_quote args=${JSON.stringify(
|
|
174
|
+
const context = `tool=stock_quote args=${JSON.stringify(DEMO_MALFORMED_INPUT)}`;
|
|
159
175
|
const message = `${errorMessage(error)} (${context})`;
|
|
160
176
|
const contextualError =
|
|
161
177
|
error instanceof Error ? new Error(message, { cause: error }) : new Error(message);
|
|
@@ -168,9 +184,9 @@ try {
|
|
|
168
184
|
userInput: "Get Apple stock in the US market.",
|
|
169
185
|
tools: [stockQuote],
|
|
170
186
|
// Both the tool name and args are intentionally wrong to demonstrate repair:
|
|
171
|
-
// "stock_price" should be "stock_quote";
|
|
187
|
+
// "stock_price" should be "stock_quote"; DEMO_MALFORMED_INPUT uses
|
|
172
188
|
// "symbol"/"NASDAQ" instead of schema-valid stock_quote args.
|
|
173
|
-
calls: [{ tool: "stock_price", args:
|
|
189
|
+
calls: [{ tool: "stock_price", args: DEMO_MALFORMED_INPUT }],
|
|
174
190
|
repair: repairDemoStockCall,
|
|
175
191
|
});
|
|
176
192
|
} catch (error) {
|
|
@@ -227,12 +243,13 @@ const repairedCall =
|
|
|
227
243
|
|
|
228
244
|
const report = {
|
|
229
245
|
wrappedTool: {
|
|
230
|
-
before:
|
|
246
|
+
before: DEMO_MALFORMED_INPUT,
|
|
231
247
|
after: wrappedOutput,
|
|
232
248
|
},
|
|
233
249
|
reliableToolCalls: {
|
|
234
|
-
before: { tool: "stock_price", args:
|
|
250
|
+
before: { tool: "stock_price", args: DEMO_MALFORMED_INPUT },
|
|
235
251
|
repaired: repairedCall,
|
|
252
|
+
// Optional validation/repair metadata; null means this run returned none.
|
|
236
253
|
diagnostics: reliableResult?.diagnostics ?? null,
|
|
237
254
|
},
|
|
238
255
|
gatewayRepair: gatewayRepairResult,
|