@botbotgo/better-call 0.1.40 → 0.1.42

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/scripts/demo.mjs +32 -28
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@botbotgo/better-call",
3
- "version": "0.1.40",
3
+ "version": "0.1.42",
4
4
  "description": "Small-model tool-call reliability layer for LangChain and custom agent runtimes.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
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 =
@@ -76,7 +88,7 @@ const stockQuote = {
76
88
  throw new Error("Invalid ticker: expected a non-empty string.");
77
89
  }
78
90
 
79
- if (!VALID_MARKETS.includes(market)) {
91
+ if (typeof market !== "string" || !VALID_MARKETS.includes(market)) {
80
92
  throw new Error("Invalid market: expected one of US, HK, CN.");
81
93
  }
82
94
 
@@ -86,7 +98,7 @@ 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 malformedRepairArgs = { symbol: "Apple", market: "NASDAQ" };
101
+ const intentionallyMalformedArgs = { symbol: "Apple", market: "NASDAQ" };
90
102
 
91
103
  const wrappedTools = betterTools([stockQuote], {
92
104
  userInput: "Get Apple stock in the US market.",
@@ -106,9 +118,9 @@ const wrappedStockQuote = wrappedTools[0];
106
118
 
107
119
  let wrappedOutput;
108
120
  try {
109
- wrappedOutput = await wrappedStockQuote.invoke(malformedRepairArgs);
121
+ wrappedOutput = await wrappedStockQuote.invoke(intentionallyMalformedArgs);
110
122
  } catch (error) {
111
- const context = `tool=stock_quote args=${JSON.stringify(malformedRepairArgs)}`;
123
+ const context = `tool=stock_quote args=${JSON.stringify(intentionallyMalformedArgs)}`;
112
124
  const message = `${errorMessage(error)} (${context})`;
113
125
  const contextualError =
114
126
  error instanceof Error ? new Error(message, { cause: error }) : new Error(message);
@@ -121,9 +133,9 @@ try {
121
133
  userInput: "Get Apple stock in the US market.",
122
134
  tools: [stockQuote],
123
135
  // Both the tool name and args are intentionally wrong to demonstrate repair:
124
- // "stock_price" should be "stock_quote"; malformedRepairArgs uses
136
+ // "stock_price" should be "stock_quote"; intentionallyMalformedArgs uses
125
137
  // "symbol"/"NASDAQ" instead of schema-valid stock_quote args.
126
- calls: [{ tool: "stock_price", args: malformedRepairArgs }],
138
+ calls: [{ tool: "stock_price", args: intentionallyMalformedArgs }],
127
139
  repair() {
128
140
  return [{ tool: "stock_quote", args: { ticker: "AAPL", market: "US" } }];
129
141
  },
@@ -157,39 +169,31 @@ try {
157
169
  });
158
170
  } catch (error) {
159
171
  const details = errorDetails(error);
160
- gatewayResult = {
161
- status: "error",
162
- originalToolName: "research",
163
- toolName: null,
164
- reason: `repairToolCall failed: ${details.message}`,
165
- errorType: details.type,
166
- errorMessage: details.message,
167
- errorCause: details.cause,
168
- };
172
+ gatewayResult = createGatewayErrorResult(
173
+ "research",
174
+ `repairToolCall failed: ${details.message}`,
175
+ details
176
+ );
169
177
  }
170
178
 
171
179
  const gatewayRepair =
172
180
  gatewayResult != null && typeof gatewayResult === "object"
173
181
  ? 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
- };
183
-
184
- const repairedCall = reliableResult?.calls?.[0] ?? null;
182
+ : createGatewayErrorResult("research", "repairToolCall returned no result.");
183
+
184
+ // Missing or empty calls are expected in some demo/error paths; normalize to null for reporting.
185
+ const repairedCall =
186
+ Array.isArray(reliableResult?.calls) && reliableResult.calls.length > 0
187
+ ? reliableResult.calls[0]
188
+ : null;
185
189
 
186
190
  const report = {
187
191
  wrappedTool: {
188
- before: malformedRepairArgs,
192
+ before: intentionallyMalformedArgs,
189
193
  after: wrappedOutput,
190
194
  },
191
195
  reliableToolCalls: {
192
- before: { tool: "stock_price", args: malformedRepairArgs },
196
+ before: { tool: "stock_price", args: intentionallyMalformedArgs },
193
197
  repaired: repairedCall,
194
198
  diagnostics: reliableResult?.diagnostics ?? null,
195
199
  },