ai 6.0.95 → 6.0.97
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/CHANGELOG.md +12 -0
- package/dist/index.d.mts +446 -420
- package/dist/index.d.ts +446 -420
- package/dist/index.js +205 -71
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +205 -71
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/03-agents/02-building-agents.mdx +47 -5
- package/docs/03-ai-sdk-core/05-generating-text.mdx +56 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +407 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +25 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +478 -3
- package/package.json +1 -1
- package/src/agent/agent.ts +33 -1
- package/src/agent/create-agent-ui-stream-response.ts +1 -1
- package/src/agent/create-agent-ui-stream.ts +1 -1
- package/src/agent/index.ts +6 -2
- package/src/agent/pipe-agent-ui-stream-to-response.ts +1 -1
- package/src/agent/tool-loop-agent-settings.ts +61 -2
- package/src/agent/tool-loop-agent.ts +80 -20
- package/src/generate-text/callback-events.ts +332 -0
- package/src/generate-text/execute-tool-call.ts +14 -28
- package/src/generate-text/generate-text.ts +39 -346
- package/src/generate-text/index.ts +4 -0
- package/src/generate-text/run-tools-transformation.ts +16 -0
- package/src/generate-text/stream-text.ts +205 -33
- package/src/agent/tool-loop-agent-on-finish-callback.ts +0 -31
- package/src/agent/tool-loop-agent-on-step-finish-callback.ts +0 -11
package/dist/internal/index.js
CHANGED
|
@@ -153,7 +153,7 @@ var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
|
153
153
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
154
154
|
|
|
155
155
|
// src/version.ts
|
|
156
|
-
var VERSION = true ? "6.0.
|
|
156
|
+
var VERSION = true ? "6.0.97" : "0.0.0-test";
|
|
157
157
|
|
|
158
158
|
// src/util/download/download.ts
|
|
159
159
|
var download = async ({
|
package/dist/internal/index.mjs
CHANGED
|
@@ -329,15 +329,39 @@ export async function POST(request: Request) {
|
|
|
329
329
|
}
|
|
330
330
|
```
|
|
331
331
|
|
|
332
|
-
###
|
|
332
|
+
### Lifecycle Callbacks
|
|
333
333
|
|
|
334
|
-
|
|
335
|
-
|
|
334
|
+
<Note type="warning">
|
|
335
|
+
Experimental callbacks are subject to breaking changes in incremental package
|
|
336
|
+
releases.
|
|
337
|
+
</Note>
|
|
338
|
+
|
|
339
|
+
Agents provide lifecycle callbacks that let you hook into different phases of the agent execution.
|
|
340
|
+
These are useful for logging, observability, debugging, and custom telemetry.
|
|
336
341
|
|
|
337
342
|
```ts
|
|
338
343
|
const result = await myAgent.generate({
|
|
339
344
|
prompt: 'Research and summarize the latest AI trends',
|
|
340
|
-
|
|
345
|
+
|
|
346
|
+
experimental_onStart({ model, functionId }) {
|
|
347
|
+
console.log('Agent started', { model: model.modelId, functionId });
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
experimental_onStepStart({ stepNumber, model }) {
|
|
351
|
+
console.log(`Step ${stepNumber} starting`, { model: model.modelId });
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
experimental_onToolCallStart({ toolCall }) {
|
|
355
|
+
console.log(`Tool call starting: ${toolCall.toolName}`);
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
experimental_onToolCallFinish({ toolCall, durationMs, success }) {
|
|
359
|
+
console.log(`Tool call finished: ${toolCall.toolName} (${durationMs}ms)`, {
|
|
360
|
+
success,
|
|
361
|
+
});
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
onStepFinish({ stepNumber, usage, finishReason, toolCalls }) {
|
|
341
365
|
console.log(`Step ${stepNumber} completed:`, {
|
|
342
366
|
inputTokens: usage.inputTokens,
|
|
343
367
|
outputTokens: usage.outputTokens,
|
|
@@ -345,10 +369,28 @@ const result = await myAgent.generate({
|
|
|
345
369
|
toolsUsed: toolCalls?.map(tc => tc.toolName),
|
|
346
370
|
});
|
|
347
371
|
},
|
|
372
|
+
|
|
373
|
+
onFinish({ totalUsage, steps }) {
|
|
374
|
+
console.log('Agent finished:', {
|
|
375
|
+
totalSteps: steps.length,
|
|
376
|
+
totalTokens: totalUsage.totalTokens,
|
|
377
|
+
});
|
|
378
|
+
},
|
|
348
379
|
});
|
|
349
380
|
```
|
|
350
381
|
|
|
351
|
-
|
|
382
|
+
The available lifecycle callbacks are:
|
|
383
|
+
|
|
384
|
+
- **`experimental_onStart`**: Called once when the agent operation begins, before any LLM calls. Receives model info, prompt, settings, and telemetry metadata.
|
|
385
|
+
- **`experimental_onStepStart`**: Called before each step (LLM call). Receives the step number, model, messages being sent, tools, and prior steps.
|
|
386
|
+
- **`experimental_onToolCallStart`**: Called right before a tool's `execute` function runs. Receives the tool call object with tool name, call ID, and input.
|
|
387
|
+
- **`experimental_onToolCallFinish`**: Called right after a tool's `execute` function completes or errors. Receives the tool call, `durationMs`, and a `success` discriminator (`output` when successful, `error` when failed).
|
|
388
|
+
- **`onStepFinish`**: Called after each step finishes. Receives step results including usage, finish reason, and tool calls.
|
|
389
|
+
- **`onFinish`**: Called when all steps are finished and the response is complete. Receives all step results, total usage, and telemetry metadata.
|
|
390
|
+
|
|
391
|
+
#### Constructor vs. Method Callbacks
|
|
392
|
+
|
|
393
|
+
All lifecycle callbacks can be defined in the constructor for agent-wide tracking, in the `generate()`/`stream()` call for per-call tracking, or both. When both are provided, both are called (constructor first, then the method callback):
|
|
352
394
|
|
|
353
395
|
```ts
|
|
354
396
|
const agent = new ToolLoopAgent({
|
|
@@ -297,6 +297,62 @@ const result = streamText({
|
|
|
297
297
|
});
|
|
298
298
|
```
|
|
299
299
|
|
|
300
|
+
### Lifecycle callbacks (experimental)
|
|
301
|
+
|
|
302
|
+
<Note type="warning">
|
|
303
|
+
Experimental callbacks are subject to breaking changes in incremental package
|
|
304
|
+
releases.
|
|
305
|
+
</Note>
|
|
306
|
+
|
|
307
|
+
`streamText` provides several experimental lifecycle callbacks that let you hook into different phases of the streaming process.
|
|
308
|
+
These are useful for logging, observability, debugging, and custom telemetry.
|
|
309
|
+
Errors thrown inside these callbacks are silently caught and do not break the streaming flow.
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
import { streamText } from 'ai';
|
|
313
|
+
__PROVIDER_IMPORT__;
|
|
314
|
+
|
|
315
|
+
const result = streamText({
|
|
316
|
+
model: __MODEL__,
|
|
317
|
+
prompt: 'What is the weather in San Francisco?',
|
|
318
|
+
tools: {
|
|
319
|
+
// ... your tools
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
experimental_onStart({ model, system, prompt, messages }) {
|
|
323
|
+
console.log('Streaming started', { model, prompt });
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
experimental_onStepStart({ stepNumber, model, messages }) {
|
|
327
|
+
console.log(`Step ${stepNumber} starting`, { model: model.modelId });
|
|
328
|
+
},
|
|
329
|
+
|
|
330
|
+
experimental_onToolCallStart({ toolCall }) {
|
|
331
|
+
console.log(`Tool call starting: ${toolCall.toolName}`, {
|
|
332
|
+
toolCallId: toolCall.toolCallId,
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
experimental_onToolCallFinish({ toolCall, durationMs, success, error }) {
|
|
337
|
+
console.log(`Tool call finished: ${toolCall.toolName} (${durationMs}ms)`, {
|
|
338
|
+
success,
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
|
|
342
|
+
onStepFinish({ finishReason, usage }) {
|
|
343
|
+
console.log('Step finished', { finishReason, usage });
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
The available lifecycle callbacks are:
|
|
349
|
+
|
|
350
|
+
- **`experimental_onStart`**: Called once when the `streamText` operation begins, before any LLM calls. Receives model info, prompt, settings, and telemetry metadata.
|
|
351
|
+
- **`experimental_onStepStart`**: Called before each step (LLM call). Receives the step number, model, messages being sent, tools, and prior steps.
|
|
352
|
+
- **`experimental_onToolCallStart`**: Called right before a tool's `execute` function runs. Receives the tool call object, messages, and context.
|
|
353
|
+
- **`experimental_onToolCallFinish`**: Called right after a tool's `execute` function completes or errors. Receives the tool call object, `durationMs`, and a discriminated union with `success`/`output` or `success`/`error`.
|
|
354
|
+
- **`onStepFinish`**: Called after each step finishes. Receives the finish reason, usage, and other step details.
|
|
355
|
+
|
|
300
356
|
### `fullStream` property
|
|
301
357
|
|
|
302
358
|
You can read a stream with all events using the `fullStream` property.
|
|
@@ -1743,6 +1743,413 @@ To see `streamText` in action, check out [these examples](#examples).
|
|
|
1743
1743
|
},
|
|
1744
1744
|
],
|
|
1745
1745
|
},
|
|
1746
|
+
{
|
|
1747
|
+
name: 'experimental_onStart',
|
|
1748
|
+
type: '(event: OnStartEvent) => PromiseLike<void> | void',
|
|
1749
|
+
isOptional: true,
|
|
1750
|
+
description:
|
|
1751
|
+
'Callback that is called when the streamText operation begins, before any LLM calls are made. Errors thrown in this callback are silently caught and do not break the generation flow. Experimental (can break in patch releases).',
|
|
1752
|
+
properties: [
|
|
1753
|
+
{
|
|
1754
|
+
type: 'OnStartEvent',
|
|
1755
|
+
parameters: [
|
|
1756
|
+
{
|
|
1757
|
+
name: 'model',
|
|
1758
|
+
type: '{ provider: string; modelId: string }',
|
|
1759
|
+
description: 'The model being used for the generation.',
|
|
1760
|
+
},
|
|
1761
|
+
{
|
|
1762
|
+
name: 'system',
|
|
1763
|
+
type: 'string | SystemModelMessage | Array<SystemModelMessage> | undefined',
|
|
1764
|
+
description: 'The system message(s) provided to the model.',
|
|
1765
|
+
},
|
|
1766
|
+
{
|
|
1767
|
+
name: 'prompt',
|
|
1768
|
+
type: 'string | Array<ModelMessage> | undefined',
|
|
1769
|
+
description:
|
|
1770
|
+
'The prompt string or array of messages if using the prompt option.',
|
|
1771
|
+
},
|
|
1772
|
+
{
|
|
1773
|
+
name: 'messages',
|
|
1774
|
+
type: 'Array<ModelMessage> | undefined',
|
|
1775
|
+
description: 'The messages array if using the messages option.',
|
|
1776
|
+
},
|
|
1777
|
+
{
|
|
1778
|
+
name: 'tools',
|
|
1779
|
+
type: 'TOOLS | undefined',
|
|
1780
|
+
description: 'The tools available for this generation.',
|
|
1781
|
+
},
|
|
1782
|
+
{
|
|
1783
|
+
name: 'toolChoice',
|
|
1784
|
+
type: 'ToolChoice<TOOLS> | undefined',
|
|
1785
|
+
description: 'The tool choice strategy for this generation.',
|
|
1786
|
+
},
|
|
1787
|
+
{
|
|
1788
|
+
name: 'activeTools',
|
|
1789
|
+
type: 'Array<keyof TOOLS> | undefined',
|
|
1790
|
+
description:
|
|
1791
|
+
'Limits which tools are available for the model to call.',
|
|
1792
|
+
},
|
|
1793
|
+
{
|
|
1794
|
+
name: 'maxOutputTokens',
|
|
1795
|
+
type: 'number | undefined',
|
|
1796
|
+
description: 'Maximum number of tokens to generate.',
|
|
1797
|
+
},
|
|
1798
|
+
{
|
|
1799
|
+
name: 'temperature',
|
|
1800
|
+
type: 'number | undefined',
|
|
1801
|
+
description: 'Sampling temperature for generation.',
|
|
1802
|
+
},
|
|
1803
|
+
{
|
|
1804
|
+
name: 'topP',
|
|
1805
|
+
type: 'number | undefined',
|
|
1806
|
+
description: 'Top-p (nucleus) sampling parameter.',
|
|
1807
|
+
},
|
|
1808
|
+
{
|
|
1809
|
+
name: 'topK',
|
|
1810
|
+
type: 'number | undefined',
|
|
1811
|
+
description: 'Top-k sampling parameter.',
|
|
1812
|
+
},
|
|
1813
|
+
{
|
|
1814
|
+
name: 'presencePenalty',
|
|
1815
|
+
type: 'number | undefined',
|
|
1816
|
+
description: 'Presence penalty for generation.',
|
|
1817
|
+
},
|
|
1818
|
+
{
|
|
1819
|
+
name: 'frequencyPenalty',
|
|
1820
|
+
type: 'number | undefined',
|
|
1821
|
+
description: 'Frequency penalty for generation.',
|
|
1822
|
+
},
|
|
1823
|
+
{
|
|
1824
|
+
name: 'stopSequences',
|
|
1825
|
+
type: 'string[] | undefined',
|
|
1826
|
+
description: 'Sequences that will stop generation.',
|
|
1827
|
+
},
|
|
1828
|
+
{
|
|
1829
|
+
name: 'seed',
|
|
1830
|
+
type: 'number | undefined',
|
|
1831
|
+
description: 'Random seed for reproducible generation.',
|
|
1832
|
+
},
|
|
1833
|
+
{
|
|
1834
|
+
name: 'maxRetries',
|
|
1835
|
+
type: 'number',
|
|
1836
|
+
description: 'Maximum number of retries for failed requests.',
|
|
1837
|
+
},
|
|
1838
|
+
{
|
|
1839
|
+
name: 'timeout',
|
|
1840
|
+
type: 'number | { totalMs?: number; stepMs?: number; chunkMs?: number } | undefined',
|
|
1841
|
+
description:
|
|
1842
|
+
'Timeout configuration for the generation. Can be a number (milliseconds) or an object with totalMs, stepMs, chunkMs.',
|
|
1843
|
+
},
|
|
1844
|
+
{
|
|
1845
|
+
name: 'headers',
|
|
1846
|
+
type: 'Record<string, string | undefined> | undefined',
|
|
1847
|
+
description: 'Additional HTTP headers sent with the request.',
|
|
1848
|
+
},
|
|
1849
|
+
{
|
|
1850
|
+
name: 'providerOptions',
|
|
1851
|
+
type: 'ProviderOptions | undefined',
|
|
1852
|
+
description: 'Additional provider-specific options.',
|
|
1853
|
+
},
|
|
1854
|
+
{
|
|
1855
|
+
name: 'stopWhen',
|
|
1856
|
+
type: 'StopCondition<TOOLS> | Array<StopCondition<TOOLS>> | undefined',
|
|
1857
|
+
description:
|
|
1858
|
+
'Condition(s) for stopping the generation. When the condition is an array, any of the conditions can be met to stop.',
|
|
1859
|
+
},
|
|
1860
|
+
{
|
|
1861
|
+
name: 'output',
|
|
1862
|
+
type: 'OUTPUT | undefined',
|
|
1863
|
+
description:
|
|
1864
|
+
'The output specification for structured outputs, if configured.',
|
|
1865
|
+
},
|
|
1866
|
+
{
|
|
1867
|
+
name: 'abortSignal',
|
|
1868
|
+
type: 'AbortSignal | undefined',
|
|
1869
|
+
description: 'Abort signal for cancelling the operation.',
|
|
1870
|
+
},
|
|
1871
|
+
{
|
|
1872
|
+
name: 'include',
|
|
1873
|
+
type: '{ requestBody?: boolean } | undefined',
|
|
1874
|
+
description:
|
|
1875
|
+
'Settings for controlling what data is included in step results.',
|
|
1876
|
+
},
|
|
1877
|
+
{
|
|
1878
|
+
name: 'functionId',
|
|
1879
|
+
type: 'string | undefined',
|
|
1880
|
+
description:
|
|
1881
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
1882
|
+
},
|
|
1883
|
+
{
|
|
1884
|
+
name: 'metadata',
|
|
1885
|
+
type: 'Record<string, unknown> | undefined',
|
|
1886
|
+
description: 'Additional metadata passed to the generation.',
|
|
1887
|
+
},
|
|
1888
|
+
{
|
|
1889
|
+
name: 'experimental_context',
|
|
1890
|
+
type: 'unknown',
|
|
1891
|
+
description:
|
|
1892
|
+
'User-defined context object that flows through the entire generation lifecycle.',
|
|
1893
|
+
},
|
|
1894
|
+
],
|
|
1895
|
+
},
|
|
1896
|
+
],
|
|
1897
|
+
},
|
|
1898
|
+
{
|
|
1899
|
+
name: 'experimental_onStepStart',
|
|
1900
|
+
type: '(event: OnStepStartEvent) => PromiseLike<void> | void',
|
|
1901
|
+
isOptional: true,
|
|
1902
|
+
description:
|
|
1903
|
+
'Callback that is called when a step (LLM call) begins, before the provider is called. Errors thrown in this callback are silently caught and do not break the generation flow. Experimental (can break in patch releases).',
|
|
1904
|
+
properties: [
|
|
1905
|
+
{
|
|
1906
|
+
type: 'OnStepStartEvent',
|
|
1907
|
+
parameters: [
|
|
1908
|
+
{
|
|
1909
|
+
name: 'stepNumber',
|
|
1910
|
+
type: 'number',
|
|
1911
|
+
description: 'Zero-based index of the current step.',
|
|
1912
|
+
},
|
|
1913
|
+
{
|
|
1914
|
+
name: 'model',
|
|
1915
|
+
type: '{ provider: string; modelId: string }',
|
|
1916
|
+
description: 'The model being used for this step.',
|
|
1917
|
+
},
|
|
1918
|
+
{
|
|
1919
|
+
name: 'system',
|
|
1920
|
+
type: 'string | SystemModelMessage | Array<SystemModelMessage> | undefined',
|
|
1921
|
+
description: 'The system message for this step.',
|
|
1922
|
+
},
|
|
1923
|
+
{
|
|
1924
|
+
name: 'messages',
|
|
1925
|
+
type: 'Array<ModelMessage>',
|
|
1926
|
+
description:
|
|
1927
|
+
'The messages that will be sent to the model for this step. Uses the user-facing ModelMessage format. May be overridden by prepareStep.',
|
|
1928
|
+
},
|
|
1929
|
+
{
|
|
1930
|
+
name: 'tools',
|
|
1931
|
+
type: 'TOOLS | undefined',
|
|
1932
|
+
description: 'The tools available for this generation.',
|
|
1933
|
+
},
|
|
1934
|
+
{
|
|
1935
|
+
name: 'toolChoice',
|
|
1936
|
+
type: 'LanguageModelV3ToolChoice | undefined',
|
|
1937
|
+
description: 'The tool choice configuration for this step.',
|
|
1938
|
+
},
|
|
1939
|
+
{
|
|
1940
|
+
name: 'activeTools',
|
|
1941
|
+
type: 'Array<keyof TOOLS> | undefined',
|
|
1942
|
+
description: 'Limits which tools are available for this step.',
|
|
1943
|
+
},
|
|
1944
|
+
{
|
|
1945
|
+
name: 'steps',
|
|
1946
|
+
type: 'ReadonlyArray<StepResult<TOOLS>>',
|
|
1947
|
+
description:
|
|
1948
|
+
'Array of results from previous steps (empty for the first step).',
|
|
1949
|
+
},
|
|
1950
|
+
{
|
|
1951
|
+
name: 'providerOptions',
|
|
1952
|
+
type: 'ProviderOptions | undefined',
|
|
1953
|
+
description:
|
|
1954
|
+
'Additional provider-specific options for this step.',
|
|
1955
|
+
},
|
|
1956
|
+
{
|
|
1957
|
+
name: 'timeout',
|
|
1958
|
+
type: 'number | { totalMs?: number; stepMs?: number; chunkMs?: number } | undefined',
|
|
1959
|
+
description: 'Timeout configuration for the generation.',
|
|
1960
|
+
},
|
|
1961
|
+
{
|
|
1962
|
+
name: 'headers',
|
|
1963
|
+
type: 'Record<string, string | undefined> | undefined',
|
|
1964
|
+
description: 'Additional HTTP headers sent with the request.',
|
|
1965
|
+
},
|
|
1966
|
+
{
|
|
1967
|
+
name: 'stopWhen',
|
|
1968
|
+
type: 'StopCondition<TOOLS> | Array<StopCondition<TOOLS>> | undefined',
|
|
1969
|
+
description: 'Condition(s) for stopping the generation.',
|
|
1970
|
+
},
|
|
1971
|
+
{
|
|
1972
|
+
name: 'output',
|
|
1973
|
+
type: 'OUTPUT | undefined',
|
|
1974
|
+
description:
|
|
1975
|
+
'The output specification for structured outputs, if configured.',
|
|
1976
|
+
},
|
|
1977
|
+
{
|
|
1978
|
+
name: 'abortSignal',
|
|
1979
|
+
type: 'AbortSignal | undefined',
|
|
1980
|
+
description: 'Abort signal for cancelling the operation.',
|
|
1981
|
+
},
|
|
1982
|
+
{
|
|
1983
|
+
name: 'include',
|
|
1984
|
+
type: '{ requestBody?: boolean } | undefined',
|
|
1985
|
+
description:
|
|
1986
|
+
'Settings for controlling what data is included in step results.',
|
|
1987
|
+
},
|
|
1988
|
+
{
|
|
1989
|
+
name: 'functionId',
|
|
1990
|
+
type: 'string | undefined',
|
|
1991
|
+
description:
|
|
1992
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
1993
|
+
},
|
|
1994
|
+
{
|
|
1995
|
+
name: 'metadata',
|
|
1996
|
+
type: 'Record<string, unknown> | undefined',
|
|
1997
|
+
description: 'Additional metadata from telemetry settings.',
|
|
1998
|
+
},
|
|
1999
|
+
{
|
|
2000
|
+
name: 'experimental_context',
|
|
2001
|
+
type: 'unknown',
|
|
2002
|
+
description:
|
|
2003
|
+
'User-defined context object. May be updated from prepareStep between steps.',
|
|
2004
|
+
},
|
|
2005
|
+
],
|
|
2006
|
+
},
|
|
2007
|
+
],
|
|
2008
|
+
},
|
|
2009
|
+
{
|
|
2010
|
+
name: 'experimental_onToolCallStart',
|
|
2011
|
+
type: '(event: OnToolCallStartEvent) => PromiseLike<void> | void',
|
|
2012
|
+
isOptional: true,
|
|
2013
|
+
description:
|
|
2014
|
+
"Callback that is called right before a tool's execute function runs. Errors thrown in this callback are silently caught and do not break the generation flow. Experimental (can break in patch releases).",
|
|
2015
|
+
properties: [
|
|
2016
|
+
{
|
|
2017
|
+
type: 'OnToolCallStartEvent',
|
|
2018
|
+
parameters: [
|
|
2019
|
+
{
|
|
2020
|
+
name: 'stepNumber',
|
|
2021
|
+
type: 'number | undefined',
|
|
2022
|
+
description:
|
|
2023
|
+
'The zero-based index of the current step where this tool call occurs. May be undefined in streaming contexts.',
|
|
2024
|
+
},
|
|
2025
|
+
{
|
|
2026
|
+
name: 'model',
|
|
2027
|
+
type: '{ provider: string; modelId: string } | undefined',
|
|
2028
|
+
description:
|
|
2029
|
+
'Information about the model being used. May be undefined in streaming contexts.',
|
|
2030
|
+
},
|
|
2031
|
+
{
|
|
2032
|
+
name: 'toolCall',
|
|
2033
|
+
type: 'TypedToolCall<TOOLS>',
|
|
2034
|
+
description:
|
|
2035
|
+
'The full tool call object containing toolName, toolCallId, input, and metadata.',
|
|
2036
|
+
},
|
|
2037
|
+
{
|
|
2038
|
+
name: 'messages',
|
|
2039
|
+
type: 'Array<ModelMessage>',
|
|
2040
|
+
description:
|
|
2041
|
+
'The conversation messages available at tool execution time.',
|
|
2042
|
+
},
|
|
2043
|
+
{
|
|
2044
|
+
name: 'abortSignal',
|
|
2045
|
+
type: 'AbortSignal | undefined',
|
|
2046
|
+
description: 'Signal for cancelling the operation.',
|
|
2047
|
+
},
|
|
2048
|
+
{
|
|
2049
|
+
name: 'functionId',
|
|
2050
|
+
type: 'string | undefined',
|
|
2051
|
+
description:
|
|
2052
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
2053
|
+
},
|
|
2054
|
+
{
|
|
2055
|
+
name: 'metadata',
|
|
2056
|
+
type: 'Record<string, unknown> | undefined',
|
|
2057
|
+
description: 'Additional metadata from telemetry settings.',
|
|
2058
|
+
},
|
|
2059
|
+
{
|
|
2060
|
+
name: 'experimental_context',
|
|
2061
|
+
type: 'unknown',
|
|
2062
|
+
description:
|
|
2063
|
+
'User-defined context object flowing through the generation.',
|
|
2064
|
+
},
|
|
2065
|
+
],
|
|
2066
|
+
},
|
|
2067
|
+
],
|
|
2068
|
+
},
|
|
2069
|
+
{
|
|
2070
|
+
name: 'experimental_onToolCallFinish',
|
|
2071
|
+
type: '(event: OnToolCallFinishEvent) => PromiseLike<void> | void',
|
|
2072
|
+
isOptional: true,
|
|
2073
|
+
description:
|
|
2074
|
+
"Callback that is called right after a tool's execute function completes (or errors). Uses a discriminated union on the `success` field: when `success: true`, `output` contains the tool result; when `success: false`, `error` contains the error. Errors thrown in this callback are silently caught and do not break the generation flow. Experimental (can break in patch releases).",
|
|
2075
|
+
properties: [
|
|
2076
|
+
{
|
|
2077
|
+
type: 'OnToolCallFinishEvent',
|
|
2078
|
+
parameters: [
|
|
2079
|
+
{
|
|
2080
|
+
name: 'stepNumber',
|
|
2081
|
+
type: 'number | undefined',
|
|
2082
|
+
description:
|
|
2083
|
+
'The zero-based index of the current step where this tool call occurred. May be undefined in streaming contexts.',
|
|
2084
|
+
},
|
|
2085
|
+
{
|
|
2086
|
+
name: 'model',
|
|
2087
|
+
type: '{ provider: string; modelId: string } | undefined',
|
|
2088
|
+
description:
|
|
2089
|
+
'Information about the model being used. May be undefined in streaming contexts.',
|
|
2090
|
+
},
|
|
2091
|
+
{
|
|
2092
|
+
name: 'toolCall',
|
|
2093
|
+
type: 'TypedToolCall<TOOLS>',
|
|
2094
|
+
description:
|
|
2095
|
+
'The full tool call object containing toolName, toolCallId, input, and metadata.',
|
|
2096
|
+
},
|
|
2097
|
+
{
|
|
2098
|
+
name: 'messages',
|
|
2099
|
+
type: 'Array<ModelMessage>',
|
|
2100
|
+
description:
|
|
2101
|
+
'The conversation messages available at tool execution time.',
|
|
2102
|
+
},
|
|
2103
|
+
{
|
|
2104
|
+
name: 'abortSignal',
|
|
2105
|
+
type: 'AbortSignal | undefined',
|
|
2106
|
+
description: 'Signal for cancelling the operation.',
|
|
2107
|
+
},
|
|
2108
|
+
{
|
|
2109
|
+
name: 'durationMs',
|
|
2110
|
+
type: 'number',
|
|
2111
|
+
description:
|
|
2112
|
+
'The wall-clock duration of the tool execution in milliseconds.',
|
|
2113
|
+
},
|
|
2114
|
+
{
|
|
2115
|
+
name: 'functionId',
|
|
2116
|
+
type: 'string | undefined',
|
|
2117
|
+
description:
|
|
2118
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
2119
|
+
},
|
|
2120
|
+
{
|
|
2121
|
+
name: 'metadata',
|
|
2122
|
+
type: 'Record<string, unknown> | undefined',
|
|
2123
|
+
description: 'Additional metadata from telemetry settings.',
|
|
2124
|
+
},
|
|
2125
|
+
{
|
|
2126
|
+
name: 'experimental_context',
|
|
2127
|
+
type: 'unknown',
|
|
2128
|
+
description:
|
|
2129
|
+
'User-defined context object flowing through the generation.',
|
|
2130
|
+
},
|
|
2131
|
+
{
|
|
2132
|
+
name: 'success',
|
|
2133
|
+
type: 'boolean',
|
|
2134
|
+
description:
|
|
2135
|
+
'Discriminator indicating whether the tool call succeeded. When true, output is available. When false, error is available.',
|
|
2136
|
+
},
|
|
2137
|
+
{
|
|
2138
|
+
name: 'output',
|
|
2139
|
+
type: 'unknown',
|
|
2140
|
+
description:
|
|
2141
|
+
"The tool's return value (only present when `success: true`).",
|
|
2142
|
+
},
|
|
2143
|
+
{
|
|
2144
|
+
name: 'error',
|
|
2145
|
+
type: 'unknown',
|
|
2146
|
+
description:
|
|
2147
|
+
'The error that occurred during tool execution (only present when `success: false`).',
|
|
2148
|
+
},
|
|
2149
|
+
],
|
|
2150
|
+
},
|
|
2151
|
+
],
|
|
2152
|
+
},
|
|
1746
2153
|
]}
|
|
1747
2154
|
/>
|
|
1748
2155
|
|
|
@@ -65,10 +65,30 @@ export type AgentCallParameters<CALL_OPTIONS, TOOLS extends ToolSet = {}> = ([
|
|
|
65
65
|
* Can be used alongside abortSignal.
|
|
66
66
|
*/
|
|
67
67
|
timeout?: number | { totalMs?: number };
|
|
68
|
+
/**
|
|
69
|
+
* Callback that is called when the agent operation begins, before any LLM calls.
|
|
70
|
+
*/
|
|
71
|
+
experimental_onStart?: ToolLoopAgentOnStartCallback<TOOLS>;
|
|
72
|
+
/**
|
|
73
|
+
* Callback that is called when a step (LLM call) begins, before the provider is called.
|
|
74
|
+
*/
|
|
75
|
+
experimental_onStepStart?: ToolLoopAgentOnStepStartCallback<TOOLS>;
|
|
76
|
+
/**
|
|
77
|
+
* Callback that is called before each tool execution begins.
|
|
78
|
+
*/
|
|
79
|
+
experimental_onToolCallStart?: ToolLoopAgentOnToolCallStartCallback<TOOLS>;
|
|
80
|
+
/**
|
|
81
|
+
* Callback that is called after each tool execution completes.
|
|
82
|
+
*/
|
|
83
|
+
experimental_onToolCallFinish?: ToolLoopAgentOnToolCallFinishCallback<TOOLS>;
|
|
68
84
|
/**
|
|
69
85
|
* Callback that is called when each step (LLM call) is finished, including intermediate steps.
|
|
70
86
|
*/
|
|
71
87
|
onStepFinish?: ToolLoopAgentOnStepFinishCallback<TOOLS>;
|
|
88
|
+
/**
|
|
89
|
+
* Callback that is called when all steps are finished and the response is complete.
|
|
90
|
+
*/
|
|
91
|
+
onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
|
|
72
92
|
};
|
|
73
93
|
|
|
74
94
|
/**
|
|
@@ -142,7 +162,12 @@ Both `generate()` and `stream()` accept an `AgentCallParameters<CALL_OPTIONS, TO
|
|
|
142
162
|
- `options` (optional): Additional call options when `CALL_OPTIONS` is not `never`
|
|
143
163
|
- `abortSignal` (optional): An `AbortSignal` to cancel the operation
|
|
144
164
|
- `timeout` (optional): A timeout in milliseconds. Can be specified as a number or as an object with a `totalMs` property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside `abortSignal`.
|
|
165
|
+
- `experimental_onStart` (optional): Callback invoked when the agent operation begins, before any LLM calls. Experimental.
|
|
166
|
+
- `experimental_onStepStart` (optional): Callback invoked when a step (LLM call) begins, before the provider is called. Experimental.
|
|
167
|
+
- `experimental_onToolCallStart` (optional): Callback invoked right before a tool's execute function runs. Experimental.
|
|
168
|
+
- `experimental_onToolCallFinish` (optional): Callback invoked right after a tool's execute function completes or errors. Experimental.
|
|
145
169
|
- `onStepFinish` (optional): A callback invoked after each agent step (LLM/tool call) completes. Useful for tracking token usage or logging.
|
|
170
|
+
- `onFinish` (optional): A callback invoked when all steps are finished and the response is complete.
|
|
146
171
|
|
|
147
172
|
## Example: Custom Agent Implementation
|
|
148
173
|
|