Skip to content

Commit 58ca2e9

Browse files
committed
chore: update dependencies and refactor example responses to return structured objects
1 parent 46d938d commit 58ca2e9

File tree

4 files changed

+81
-29
lines changed

4 files changed

+81
-29
lines changed

bun.lock

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/17-terminal.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ async function main() {
6060
}
6161
}
6262

63-
return `Search Results for "${
64-
input.query || "your search"
65-
}":\n\n${result}`;
63+
return {
64+
type: "text" as const,
65+
value: `Search Results for "${
66+
input.query || "your search"
67+
}":\n\n${result}`,
68+
};
6669
},
6770
}),
6871

@@ -87,14 +90,20 @@ async function main() {
8790

8891
// Mock summarization by taking key sentences and reducing length
8992
if (words.length <= 20) {
90-
return `Summary: ${text}`;
93+
return {
94+
type: "text" as const,
95+
value: `Summary: ${text}`,
96+
};
9197
}
9298

9399
const targetLength =
94100
input.length === "short" ? 30 : input.length === "medium" ? 50 : 80;
95101
const summary = words.slice(0, targetLength).join(" ");
96102

97-
return `Summary (${input.length || "medium"} length): ${summary}...`;
103+
return {
104+
type: "text" as const,
105+
value: `Summary (${input.length || "medium"} length): ${summary}...`,
106+
};
98107
},
99108
}),
100109

@@ -119,7 +128,10 @@ async function main() {
119128
.slice(0, Math.floor(words.length * 0.7))
120129
.join(" ");
121130

122-
return `Concise version: ${conciseText}`;
131+
return {
132+
type: "text" as const,
133+
value: `Concise version: ${conciseText}`,
134+
};
123135
},
124136
}),
125137

@@ -149,7 +161,10 @@ async function main() {
149161
.map((point) => `• ${point.trim()}`)
150162
.join("\n");
151163

152-
return `Bullet Points:\n${bulletList}`;
164+
return {
165+
type: "text" as const,
166+
value: `Bullet Points:\n${bulletList}`,
167+
};
153168
},
154169
}),
155170

@@ -162,7 +177,9 @@ async function main() {
162177
async execute(input) {
163178
console.log("[TOOL] Compare Topics", input);
164179

165-
return `Comparison between "${input.topic1}" and "${input.topic2}":
180+
return {
181+
type: "text" as const,
182+
value: `Comparison between "${input.topic1}" and "${input.topic2}":
166183
167184
Similarities:
168185
• Both are cutting-edge technology fields
@@ -172,7 +189,8 @@ Similarities:
172189
Differences:
173190
${input.topic1}: Focuses on specific technological approach and applications
174191
${input.topic2}: Has different implementation methods and use cases
175-
• Timeline and maturity levels may vary between the two fields`;
192+
• Timeline and maturity levels may vary between the two fields`,
193+
};
176194
},
177195
}),
178196
};

examples/18-weather.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ async function main() {
239239
async execute(_input) {
240240
spinner.stop();
241241
console.log(formatToolCall("time", ""));
242-
return new Date().toLocaleTimeString();
242+
return {
243+
type: "text" as const,
244+
value: new Date().toLocaleTimeString(),
245+
};
243246
},
244247
}),
245248
convert_to_celsius: tool({
@@ -267,7 +270,7 @@ async function main() {
267270
chalk.green(`${input.temperature}°F = ${celsius}°C`)
268271
);
269272

270-
return `${celsius}°C`;
273+
return { type: "text" as const, value: `${celsius}°C` };
271274
},
272275
}),
273276

@@ -302,7 +305,11 @@ async function main() {
302305
locationSpinner.fail(
303306
chalk.red("Could not detect location")
304307
);
305-
return "Unable to detect your location. Please specify a location for weather.";
308+
return {
309+
type: "text" as const,
310+
value:
311+
"Unable to detect your location. Please specify a location for weather.",
312+
};
306313
}
307314

308315
locationSpinner.succeed(
@@ -311,7 +318,11 @@ async function main() {
311318
locationToUse = `${autoLocation.city}, ${autoLocation.region}`;
312319
} catch (_error) {
313320
locationSpinner.fail(chalk.red("Location detection failed"));
314-
return "Failed to detect your location. Please specify a location for weather.";
321+
return {
322+
type: "text" as const,
323+
value:
324+
"Failed to detect your location. Please specify a location for weather.",
325+
};
315326
}
316327
}
317328

@@ -331,7 +342,10 @@ async function main() {
331342
toolSpinner.fail(
332343
chalk.red(`Location "${locationToUse}" not found`)
333344
);
334-
return `Location "${locationToUse}" not found`;
345+
return {
346+
type: "text" as const,
347+
value: `Location "${locationToUse}" not found`,
348+
};
335349
}
336350

337351
// Get weather data
@@ -364,10 +378,16 @@ async function main() {
364378
chalk.green(`${coordinates.name}, ${coordinates.country}`)
365379
);
366380

367-
return `${coordinates.name}: ${current.temperature_2m}°F, ${weatherDescription}, ${current.relative_humidity_2m}% humidity, ${current.wind_speed_10m}mph wind`;
381+
return {
382+
type: "text" as const,
383+
value: `${coordinates.name}: ${current.temperature_2m}°F, ${weatherDescription}, ${current.relative_humidity_2m}% humidity, ${current.wind_speed_10m}mph wind`,
384+
};
368385
} catch (_error) {
369386
toolSpinner.fail(chalk.red("Error getting weather"));
370-
return `Error getting weather for "${locationToUse}"`;
387+
return {
388+
type: "text" as const,
389+
value: `Error getting weather for "${locationToUse}"`,
390+
};
371391
}
372392
},
373393
}),
@@ -408,7 +428,8 @@ async function main() {
408428
// Count tokens (simple whitespace split)
409429
tokenCount += chunk.text.split(/\s+/).filter(Boolean).length;
410430
} else if (chunk.type === "tool-result") {
411-
toolResponses.push(chunk as any);
431+
toolResponses.push(chunk);
432+
// Don't display tool results - let the AI incorporate them into its response
412433
} else if (chunk.type === "tool-call") {
413434
toolCalls.push(chunk satisfies ToolCallPart);
414435
}
@@ -427,16 +448,21 @@ async function main() {
427448
console.log(speedMsg);
428449
}
429450

430-
if (toolResponses.length > 0) {
431-
messages.push({ role: "tool", content: toolResponses });
432-
}
433-
434-
if (assistantResponse.trim()) {
451+
if (assistantResponse.trim() || toolCalls.length > 0) {
435452
messages.push({
436453
role: "assistant",
437-
content: [{ type: "text", text: assistantResponse }, ...toolCalls],
454+
content: [
455+
...(assistantResponse.trim()
456+
? [{ type: "text" as const, text: assistantResponse }]
457+
: []),
458+
...toolCalls,
459+
],
438460
});
439461
}
462+
463+
if (toolResponses.length > 0) {
464+
messages.push({ role: "tool", content: toolResponses });
465+
}
440466
} catch (_error) {
441467
spinner.fail(chalk.red("Error occurred"));
442468
console.log("");

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
],
8181
"dependencies": {
8282
"@ai-sdk/provider": "2.0.0-beta.1",
83-
"@ai-sdk/provider-utils": "3.0.0-beta.1",
83+
"@ai-sdk/provider-utils": "3.0.0-beta.2",
8484
"@types/json-schema": "^7.0.15",
8585
"h3": "^1.15.3",
8686
"json-schema": "^0.4.0",
8787
"zod-to-json-schema": "^3.24.5",
88-
"ai": "5.0.0-beta.1"
88+
"ai": "5.0.0-beta.9"
8989
},
9090
"peerDependencies": {
9191
"ai": "",

0 commit comments

Comments
 (0)