@tanstack/ai 0.35.0 → 0.36.0
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 +2 -2
- package/skills/ai-core/tool-calling/SKILL.md +26 -1
- package/src/activities/chat/index.ts +8 -0
- package/src/activities/chat/tools/lazy-tool-manager.ts +12 -4
- package/src/activities/chat/tools/lazy-tools.ts +33 -0
- package/src/index.ts +5 -0
- package/src/types.ts +23 -1
- package/dist/esm/activities/chat/adapter.d.ts +0 -163
- package/dist/esm/activities/chat/adapter.js +0 -17
- package/dist/esm/activities/chat/adapter.js.map +0 -1
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +0 -59
- package/dist/esm/activities/chat/agent-loop-strategies.js +0 -23
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +0 -1
- package/dist/esm/activities/chat/index.d.ts +0 -264
- package/dist/esm/activities/chat/index.js +0 -1723
- package/dist/esm/activities/chat/index.js.map +0 -1
- package/dist/esm/activities/chat/mcp/manager.d.ts +0 -25
- package/dist/esm/activities/chat/mcp/manager.js +0 -71
- package/dist/esm/activities/chat/mcp/manager.js.map +0 -1
- package/dist/esm/activities/chat/mcp/types.d.ts +0 -56
- package/dist/esm/activities/chat/messages.d.ts +0 -78
- package/dist/esm/activities/chat/messages.js +0 -374
- package/dist/esm/activities/chat/messages.js.map +0 -1
- package/dist/esm/activities/chat/middleware/builder.d.ts +0 -46
- package/dist/esm/activities/chat/middleware/builder.js +0 -17
- package/dist/esm/activities/chat/middleware/builder.js.map +0 -1
- package/dist/esm/activities/chat/middleware/capabilities.d.ts +0 -93
- package/dist/esm/activities/chat/middleware/capabilities.js +0 -45
- package/dist/esm/activities/chat/middleware/capabilities.js.map +0 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +0 -87
- package/dist/esm/activities/chat/middleware/compose.js +0 -510
- package/dist/esm/activities/chat/middleware/compose.js.map +0 -1
- package/dist/esm/activities/chat/middleware/define.d.ts +0 -20
- package/dist/esm/activities/chat/middleware/define.js +0 -7
- package/dist/esm/activities/chat/middleware/define.js.map +0 -1
- package/dist/esm/activities/chat/middleware/index.d.ts +0 -10
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.d.ts +0 -89
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +0 -76
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +0 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +0 -405
- package/dist/esm/activities/chat/middleware/validate.d.ts +0 -19
- package/dist/esm/activities/chat/middleware/validate.js +0 -30
- package/dist/esm/activities/chat/middleware/validate.js.map +0 -1
- package/dist/esm/activities/chat/runtime-context-types.d.ts +0 -43
- package/dist/esm/activities/chat/stream/index.d.ts +0 -11
- package/dist/esm/activities/chat/stream/json-parser.d.ts +0 -38
- package/dist/esm/activities/chat/stream/json-parser.js +0 -28
- package/dist/esm/activities/chat/stream/json-parser.js.map +0 -1
- package/dist/esm/activities/chat/stream/message-updaters.d.ts +0 -81
- package/dist/esm/activities/chat/stream/message-updaters.js +0 -253
- package/dist/esm/activities/chat/stream/message-updaters.js.map +0 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +0 -439
- package/dist/esm/activities/chat/stream/processor.js +0 -1426
- package/dist/esm/activities/chat/stream/processor.js.map +0 -1
- package/dist/esm/activities/chat/stream/strategies.d.ts +0 -43
- package/dist/esm/activities/chat/stream/strategies.js +0 -54
- package/dist/esm/activities/chat/stream/strategies.js.map +0 -1
- package/dist/esm/activities/chat/stream/types.d.ts +0 -90
- package/dist/esm/activities/chat/tools/lazy-tool-manager.d.ts +0 -81
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +0 -188
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +0 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +0 -140
- package/dist/esm/activities/chat/tools/schema-converter.js +0 -167
- package/dist/esm/activities/chat/tools/schema-converter.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +0 -140
- package/dist/esm/activities/chat/tools/tool-calls.js +0 -512
- package/dist/esm/activities/chat/tools/tool-calls.js.map +0 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +0 -135
- package/dist/esm/activities/chat/tools/tool-definition.js +0 -25
- package/dist/esm/activities/chat/tools/tool-definition.js.map +0 -1
- package/dist/esm/activities/error-payload.d.ts +0 -30
- package/dist/esm/activities/error-payload.js +0 -54
- package/dist/esm/activities/error-payload.js.map +0 -1
- package/dist/esm/activities/generateAudio/adapter.d.ts +0 -62
- package/dist/esm/activities/generateAudio/adapter.js +0 -16
- package/dist/esm/activities/generateAudio/adapter.js.map +0 -1
- package/dist/esm/activities/generateAudio/index.d.ts +0 -85
- package/dist/esm/activities/generateAudio/index.js +0 -114
- package/dist/esm/activities/generateAudio/index.js.map +0 -1
- package/dist/esm/activities/generateImage/adapter.d.ts +0 -78
- package/dist/esm/activities/generateImage/adapter.js +0 -16
- package/dist/esm/activities/generateImage/adapter.js.map +0 -1
- package/dist/esm/activities/generateImage/index.d.ts +0 -134
- package/dist/esm/activities/generateImage/index.js +0 -121
- package/dist/esm/activities/generateImage/index.js.map +0 -1
- package/dist/esm/activities/generateSpeech/adapter.d.ts +0 -62
- package/dist/esm/activities/generateSpeech/adapter.js +0 -16
- package/dist/esm/activities/generateSpeech/adapter.js.map +0 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +0 -96
- package/dist/esm/activities/generateSpeech/index.js +0 -119
- package/dist/esm/activities/generateSpeech/index.js.map +0 -1
- package/dist/esm/activities/generateTranscription/adapter.d.ts +0 -62
- package/dist/esm/activities/generateTranscription/adapter.js +0 -16
- package/dist/esm/activities/generateTranscription/adapter.js.map +0 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +0 -109
- package/dist/esm/activities/generateTranscription/index.js +0 -109
- package/dist/esm/activities/generateTranscription/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/adapter.d.ts +0 -145
- package/dist/esm/activities/generateVideo/adapter.js +0 -30
- package/dist/esm/activities/generateVideo/adapter.js.map +0 -1
- package/dist/esm/activities/generateVideo/index.d.ts +0 -223
- package/dist/esm/activities/generateVideo/index.js +0 -295
- package/dist/esm/activities/generateVideo/index.js.map +0 -1
- package/dist/esm/activities/generateVideo/snap.d.ts +0 -14
- package/dist/esm/activities/generateVideo/snap.js +0 -54
- package/dist/esm/activities/generateVideo/snap.js.map +0 -1
- package/dist/esm/activities/index.d.ts +0 -27
- package/dist/esm/activities/index.js +0 -43
- package/dist/esm/activities/index.js.map +0 -1
- package/dist/esm/activities/middleware/index.d.ts +0 -2
- package/dist/esm/activities/middleware/run.d.ts +0 -20
- package/dist/esm/activities/middleware/run.js +0 -42
- package/dist/esm/activities/middleware/run.js.map +0 -1
- package/dist/esm/activities/middleware/types.d.ts +0 -118
- package/dist/esm/activities/stream-generation-result.d.ts +0 -15
- package/dist/esm/activities/stream-generation-result.js +0 -49
- package/dist/esm/activities/stream-generation-result.js.map +0 -1
- package/dist/esm/activities/summarize/adapter.d.ts +0 -74
- package/dist/esm/activities/summarize/adapter.js +0 -16
- package/dist/esm/activities/summarize/adapter.js.map +0 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +0 -45
- package/dist/esm/activities/summarize/chat-stream-summarize.js +0 -212
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +0 -1
- package/dist/esm/activities/summarize/index.d.ts +0 -108
- package/dist/esm/activities/summarize/index.js +0 -113
- package/dist/esm/activities/summarize/index.js.map +0 -1
- package/dist/esm/adapter-internals.d.ts +0 -5
- package/dist/esm/adapter-internals.js +0 -10
- package/dist/esm/adapter-internals.js.map +0 -1
- package/dist/esm/client.d.ts +0 -44
- package/dist/esm/client.js +0 -67
- package/dist/esm/client.js.map +0 -1
- package/dist/esm/extend-adapter.d.ts +0 -152
- package/dist/esm/extend-adapter.js +0 -21
- package/dist/esm/extend-adapter.js.map +0 -1
- package/dist/esm/index.d.ts +0 -44
- package/dist/esm/index.js +0 -106
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/logger/console-logger.d.ts +0 -29
- package/dist/esm/logger/console-logger.js +0 -83
- package/dist/esm/logger/console-logger.js.map +0 -1
- package/dist/esm/logger/internal-logger.d.ts +0 -41
- package/dist/esm/logger/internal-logger.js +0 -86
- package/dist/esm/logger/internal-logger.js.map +0 -1
- package/dist/esm/logger/resolve.d.ts +0 -14
- package/dist/esm/logger/resolve.js +0 -54
- package/dist/esm/logger/resolve.js.map +0 -1
- package/dist/esm/logger/types.d.ts +0 -75
- package/dist/esm/middlewares/content-guard.d.ts +0 -77
- package/dist/esm/middlewares/content-guard.js +0 -156
- package/dist/esm/middlewares/content-guard.js.map +0 -1
- package/dist/esm/middlewares/index.d.ts +0 -2
- package/dist/esm/middlewares/index.js +0 -7
- package/dist/esm/middlewares/index.js.map +0 -1
- package/dist/esm/middlewares/otel.d.ts +0 -81
- package/dist/esm/middlewares/otel.js +0 -745
- package/dist/esm/middlewares/otel.js.map +0 -1
- package/dist/esm/middlewares/tool-cache.d.ts +0 -1
- package/dist/esm/middlewares/usage-attributes.d.ts +0 -24
- package/dist/esm/middlewares/usage-attributes.js +0 -43
- package/dist/esm/middlewares/usage-attributes.js.map +0 -1
- package/dist/esm/realtime/index.d.ts +0 -28
- package/dist/esm/realtime/index.js +0 -8
- package/dist/esm/realtime/index.js.map +0 -1
- package/dist/esm/realtime/types.d.ts +0 -282
- package/dist/esm/stream-to-response.d.ts +0 -102
- package/dist/esm/stream-to-response.js +0 -121
- package/dist/esm/stream-to-response.js.map +0 -1
- package/dist/esm/strip-to-spec-middleware.d.ts +0 -18
- package/dist/esm/strip-to-spec-middleware.js +0 -20
- package/dist/esm/strip-to-spec-middleware.js.map +0 -1
- package/dist/esm/system-prompts.d.ts +0 -66
- package/dist/esm/system-prompts.js +0 -23
- package/dist/esm/system-prompts.js.map +0 -1
- package/dist/esm/tool-registry.d.ts +0 -81
- package/dist/esm/tool-registry.js +0 -49
- package/dist/esm/tool-registry.js.map +0 -1
- package/dist/esm/tools/provider-tool.d.ts +0 -30
- package/dist/esm/tools/provider-tool.js +0 -7
- package/dist/esm/tools/provider-tool.js.map +0 -1
- package/dist/esm/types.d.ts +0 -1573
- package/dist/esm/utilities/ag-ui-wire.d.ts +0 -44
- package/dist/esm/utilities/ag-ui-wire.js +0 -107
- package/dist/esm/utilities/ag-ui-wire.js.map +0 -1
- package/dist/esm/utilities/chat-params.d.ts +0 -85
- package/dist/esm/utilities/chat-params.js +0 -100
- package/dist/esm/utilities/chat-params.js.map +0 -1
- package/dist/esm/utilities/errors.d.ts +0 -13
- package/dist/esm/utilities/errors.js +0 -22
- package/dist/esm/utilities/errors.js.map +0 -1
- package/dist/esm/utilities/media-prompt.d.ts +0 -35
- package/dist/esm/utilities/media-prompt.js +0 -43
- package/dist/esm/utilities/media-prompt.js.map +0 -1
- package/dist/esm/utilities/numbers.d.ts +0 -8
- package/dist/esm/utilities/numbers.js +0 -12
- package/dist/esm/utilities/numbers.js.map +0 -1
- package/dist/esm/utilities/sampling-keys.d.ts +0 -20
- package/dist/esm/utilities/sampling-keys.js +0 -20
- package/dist/esm/utilities/sampling-keys.js.map +0 -1
- package/dist/esm/utilities/tool-result.d.ts +0 -21
- package/dist/esm/utilities/tool-result.js +0 -37
- package/dist/esm/utilities/tool-result.js.map +0 -1
- package/dist/esm/utilities/usage.d.ts +0 -31
- package/dist/esm/utilities/usage.js +0 -11
- package/dist/esm/utilities/usage.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -17
- package/dist/esm/utils.js +0 -20
- package/dist/esm/utils.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"description": "Type-safe TypeScript AI SDK for streaming chat, tool calling, agents, structured outputs, and multimodal generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"@ag-ui/core": "^0.0.52",
|
|
77
77
|
"@standard-schema/spec": "^1.1.0",
|
|
78
78
|
"partial-json": "^0.1.7",
|
|
79
|
-
"@tanstack/ai-event-client": "0.6.
|
|
79
|
+
"@tanstack/ai-event-client": "0.6.7",
|
|
80
80
|
"@tanstack/ai-utils": "0.3.0"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
@@ -360,7 +360,7 @@ const compareProducts = compareProductsDef.server(async ({ productIds }) => {
|
|
|
360
360
|
export async function POST(request: Request) {
|
|
361
361
|
const { messages } = await request.json()
|
|
362
362
|
const stream = chat({
|
|
363
|
-
adapter: openaiText('gpt-
|
|
363
|
+
adapter: openaiText('gpt-5.5'),
|
|
364
364
|
messages,
|
|
365
365
|
tools: [getProducts, compareProducts],
|
|
366
366
|
agentLoopStrategy: maxIterations(20),
|
|
@@ -375,6 +375,31 @@ gets the full schema, then calls `compareProducts` directly.
|
|
|
375
375
|
Once discovered, a tool stays available for the conversation.
|
|
376
376
|
When all lazy tools are discovered, the discovery tool is removed automatically.
|
|
377
377
|
|
|
378
|
+
### Tuning the lazy catalog with `lazyToolsConfig`
|
|
379
|
+
|
|
380
|
+
By default the discovery-tool catalog lists only bare names (`'none'`). Pass
|
|
381
|
+
`lazyToolsConfig` to `chat()` to include more context:
|
|
382
|
+
|
|
383
|
+
```typescript
|
|
384
|
+
const stream = chat({
|
|
385
|
+
adapter: openaiText('gpt-5.5'),
|
|
386
|
+
messages,
|
|
387
|
+
tools: [getProducts, compareProducts],
|
|
388
|
+
agentLoopStrategy: maxIterations(20),
|
|
389
|
+
lazyToolsConfig: { includeDescription: 'first-sentence' },
|
|
390
|
+
})
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
`includeDescription` values:
|
|
394
|
+
|
|
395
|
+
| Value | Catalog entry | When to use |
|
|
396
|
+
| ------------------ | ---------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
|
|
397
|
+
| `'none'` (default) | `compareProducts` | Smallest prompt; model discovers by name |
|
|
398
|
+
| `'first-sentence'` | `compareProducts — Compare two or more products side by side.` | Helps the model decide whether to discover without extra tokens |
|
|
399
|
+
| `'full'` | `compareProducts — Compare two or more products side by side. Accepts productIds array.` | Use when descriptions are short or the model needs full context to route correctly |
|
|
400
|
+
|
|
401
|
+
The post-discovery payload always returns the full description and schema regardless of this setting.
|
|
402
|
+
|
|
378
403
|
## MCP Tools
|
|
379
404
|
|
|
380
405
|
`@tanstack/ai-mcp` lets a server-side `chat()` call discover and invoke tools
|
|
@@ -47,6 +47,7 @@ import type {
|
|
|
47
47
|
CustomEvent,
|
|
48
48
|
InferSchemaType,
|
|
49
49
|
JSONSchema,
|
|
50
|
+
LazyToolsConfig,
|
|
50
51
|
ModelMessage,
|
|
51
52
|
RunFinishedEvent,
|
|
52
53
|
SchemaInput,
|
|
@@ -235,6 +236,12 @@ export interface TextActivityOptions<
|
|
|
235
236
|
abortController?: TextOptions['abortController']
|
|
236
237
|
/** Strategy for controlling the agent loop */
|
|
237
238
|
agentLoopStrategy?: TextOptions['agentLoopStrategy']
|
|
239
|
+
/**
|
|
240
|
+
* Optional configuration for lazy-tool discovery (tools marked `lazy: true`).
|
|
241
|
+
* Tunes how much of each lazy tool's description appears in the discovery
|
|
242
|
+
* catalog. Optional — defaults to `{ includeDescription: 'none' }`.
|
|
243
|
+
*/
|
|
244
|
+
lazyToolsConfig?: LazyToolsConfig
|
|
238
245
|
/** Unique conversation identifier for tracking */
|
|
239
246
|
conversationId?: TextOptions['conversationId']
|
|
240
247
|
/** Thread/conversation ID for AG-UI protocol. Auto-generated if not provided. */
|
|
@@ -606,6 +613,7 @@ class TextEngine<
|
|
|
606
613
|
this.lazyToolManager = new LazyToolManager(
|
|
607
614
|
config.params.tools || [],
|
|
608
615
|
this.messages,
|
|
616
|
+
config.params.lazyToolsConfig,
|
|
609
617
|
)
|
|
610
618
|
this.tools = this.lazyToolManager.getActiveTools()
|
|
611
619
|
this.toolCallManager = new ToolCallManager<
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { convertSchemaToJsonSchema } from './schema-converter'
|
|
2
|
-
import
|
|
2
|
+
import { renderLazyCatalogEntry } from './lazy-tools'
|
|
3
|
+
import type { AnyTool, LazyToolsConfig, Tool } from '../../../types'
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Name of the synthetic tool the LLM calls to discover lazy tools.
|
|
@@ -23,6 +24,7 @@ export class LazyToolManager {
|
|
|
23
24
|
private readonly discoveredTools: Set<string>
|
|
24
25
|
private hasNewDiscoveries: boolean
|
|
25
26
|
private readonly discoveryTool: Tool | null
|
|
27
|
+
private readonly lazyToolsConfig: LazyToolsConfig
|
|
26
28
|
|
|
27
29
|
constructor(
|
|
28
30
|
tools: ReadonlyArray<Tool>,
|
|
@@ -36,7 +38,9 @@ export class LazyToolManager {
|
|
|
36
38
|
}>
|
|
37
39
|
toolCallId?: string
|
|
38
40
|
}>,
|
|
41
|
+
lazyToolsConfig: LazyToolsConfig = {},
|
|
39
42
|
) {
|
|
43
|
+
this.lazyToolsConfig = lazyToolsConfig
|
|
40
44
|
const eager: Array<Tool> = []
|
|
41
45
|
this.lazyToolMap = new Map()
|
|
42
46
|
this.discoveredTools = new Set()
|
|
@@ -224,9 +228,13 @@ export class LazyToolManager {
|
|
|
224
228
|
|
|
225
229
|
const lazyToolMap = this.lazyToolMap
|
|
226
230
|
|
|
227
|
-
// Build the static description
|
|
228
|
-
|
|
229
|
-
const
|
|
231
|
+
// Build the static description, rendering each entry per includeDescription.
|
|
232
|
+
// With the default 'none' this is byte-identical to the legacy output.
|
|
233
|
+
const include = this.lazyToolsConfig.includeDescription ?? 'none'
|
|
234
|
+
const allLazyEntries = Array.from(this.lazyToolMap.values()).map((t) =>
|
|
235
|
+
renderLazyCatalogEntry(t.name, t.description, include),
|
|
236
|
+
)
|
|
237
|
+
const description = `You have access to additional tools that can be discovered. Available tools: [${allLazyEntries.join(', ')}]. Call this tool with a list of tool names to discover their full descriptions and argument schemas before using them.`
|
|
230
238
|
|
|
231
239
|
// Use the arrow function to capture `this` context
|
|
232
240
|
const manager = this
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { LazyToolsConfig } from '../../../types'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Extract the first sentence of a description (up to the first ., !, or ?
|
|
5
|
+
* followed by whitespace or end-of-string). Falls back to the whole trimmed
|
|
6
|
+
* string when there is no sentence terminator.
|
|
7
|
+
*/
|
|
8
|
+
export function firstSentence(text: string): string {
|
|
9
|
+
const trimmed = text.trim()
|
|
10
|
+
if (!trimmed) return ''
|
|
11
|
+
const match = trimmed.match(/^.*?[.!?](?=\s|$)/)
|
|
12
|
+
return (match ? match[0] : trimmed).trim()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Render one entry in a lazy-tool catalog according to `includeDescription`.
|
|
17
|
+
* - 'none' (default) → bare name (preserves legacy chat behavior)
|
|
18
|
+
* - 'first-sentence' → `name — <first sentence>`
|
|
19
|
+
* - 'full' → `name — <full description>`
|
|
20
|
+
* Falls back to the bare name when there is no description.
|
|
21
|
+
*/
|
|
22
|
+
export function renderLazyCatalogEntry(
|
|
23
|
+
name: string,
|
|
24
|
+
description: string,
|
|
25
|
+
includeDescription: LazyToolsConfig['includeDescription'] = 'none',
|
|
26
|
+
): string {
|
|
27
|
+
if (includeDescription === 'none' || !description.trim()) return name
|
|
28
|
+
const desc =
|
|
29
|
+
includeDescription === 'first-sentence'
|
|
30
|
+
? firstSentence(description)
|
|
31
|
+
: description.trim()
|
|
32
|
+
return desc ? `${name} — ${desc}` : name
|
|
33
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -154,6 +154,11 @@ export type {
|
|
|
154
154
|
// All types
|
|
155
155
|
export * from './types'
|
|
156
156
|
|
|
157
|
+
export {
|
|
158
|
+
firstSentence,
|
|
159
|
+
renderLazyCatalogEntry,
|
|
160
|
+
} from './activities/chat/tools/lazy-tools'
|
|
161
|
+
|
|
157
162
|
// Usage utilities
|
|
158
163
|
export { buildBaseUsage, type BaseUsageInput } from './utilities/usage'
|
|
159
164
|
|
package/src/types.ts
CHANGED
|
@@ -654,13 +654,29 @@ export interface Tool<
|
|
|
654
654
|
/** If true, tool execution requires user approval before running. Works with both server and client tools. */
|
|
655
655
|
needsApproval?: boolean
|
|
656
656
|
|
|
657
|
-
/** If true, this tool is lazy and will only be sent to the LLM after being discovered via the lazy tool discovery mechanism.
|
|
657
|
+
/** If true, this tool is lazy and will only be sent to the LLM after being discovered via the lazy tool discovery mechanism. Works with both chat() (the synthetic discovery tool) and Code Mode (kept out of the system prompt and revealed via discover_tools). */
|
|
658
658
|
lazy?: boolean
|
|
659
659
|
|
|
660
660
|
/** Additional metadata for adapters or custom extensions */
|
|
661
661
|
metadata?: Record<string, any> | undefined
|
|
662
662
|
}
|
|
663
663
|
|
|
664
|
+
/**
|
|
665
|
+
* Configuration for the lazy-tool discovery catalog, shared by chat() and
|
|
666
|
+
* Code Mode. Optional in both — lazy behavior is triggered purely by tools
|
|
667
|
+
* marked `lazy: true`; this only tunes how much of each lazy tool's
|
|
668
|
+
* description appears in the pre-discovery catalog. The post-discovery payload
|
|
669
|
+
* always returns the full description + schema.
|
|
670
|
+
*/
|
|
671
|
+
export interface LazyToolsConfig {
|
|
672
|
+
/**
|
|
673
|
+
* How much of each lazy tool's description appears in the pre-discovery
|
|
674
|
+
* catalog (the names list shown before the model discovers the tool).
|
|
675
|
+
* @default 'none'
|
|
676
|
+
*/
|
|
677
|
+
includeDescription?: 'full' | 'first-sentence' | 'none'
|
|
678
|
+
}
|
|
679
|
+
|
|
664
680
|
export type AnyTool = Omit<Tool<any, any, any, any>, 'execute'> & {
|
|
665
681
|
execute?: ((args: any, context?: any) => any) | undefined
|
|
666
682
|
}
|
|
@@ -819,6 +835,12 @@ export interface TextOptions<
|
|
|
819
835
|
*/
|
|
820
836
|
systemPrompts?: Array<SystemPrompt>
|
|
821
837
|
agentLoopStrategy?: AgentLoopStrategy
|
|
838
|
+
/**
|
|
839
|
+
* Optional configuration for lazy-tool discovery (tools marked `lazy: true`).
|
|
840
|
+
* Tunes how much of each lazy tool's description appears in the discovery
|
|
841
|
+
* catalog. Optional — defaults to `{ includeDescription: 'none' }`.
|
|
842
|
+
*/
|
|
843
|
+
lazyToolsConfig?: LazyToolsConfig
|
|
822
844
|
/**
|
|
823
845
|
* Observability metadata attached to this call. Surfaced to middleware,
|
|
824
846
|
* devtools, and the event client; values may be arbitrarily structured
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { DefaultMessageMetadataByModality, JSONSchema, Modality, StreamChunk, TextOptions, TokenUsage } from '../../types.js';
|
|
2
|
-
import { CapabilityHandle } from './middleware/capabilities.js';
|
|
3
|
-
/**
|
|
4
|
-
* Configuration for adapter instances
|
|
5
|
-
*/
|
|
6
|
-
export interface TextAdapterConfig {
|
|
7
|
-
apiKey?: string;
|
|
8
|
-
baseUrl?: string;
|
|
9
|
-
timeout?: number;
|
|
10
|
-
maxRetries?: number;
|
|
11
|
-
headers?: Record<string, string>;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Options for structured output generation.
|
|
15
|
-
*
|
|
16
|
-
* The internal logger is threaded through `chatOptions.logger` (inherited from
|
|
17
|
-
* `TextOptions`). Adapter implementations must call `logger.request()` before
|
|
18
|
-
* SDK calls, `logger.provider()` for each chunk received, and `logger.errors()`
|
|
19
|
-
* in catch blocks.
|
|
20
|
-
*/
|
|
21
|
-
export interface StructuredOutputOptions<TProviderOptions extends object> {
|
|
22
|
-
/** Text options for the request */
|
|
23
|
-
chatOptions: TextOptions<TProviderOptions>;
|
|
24
|
-
/** JSON Schema for structured output - already converted from Zod in the ai layer */
|
|
25
|
-
outputSchema: JSONSchema;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Result from structured output generation
|
|
29
|
-
*/
|
|
30
|
-
export interface StructuredOutputResult<T = unknown> {
|
|
31
|
-
/** The parsed data conforming to the schema */
|
|
32
|
-
data: T;
|
|
33
|
-
/** The raw text response from the model before parsing */
|
|
34
|
-
rawText: string;
|
|
35
|
-
/** Token usage information (if provided by the adapter) */
|
|
36
|
-
usage?: TokenUsage;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Text adapter interface with pre-resolved generics.
|
|
40
|
-
*
|
|
41
|
-
* An adapter is created by a provider function: `provider('model')` → `adapter`
|
|
42
|
-
* All type resolution happens at the provider call site, not in this interface.
|
|
43
|
-
*
|
|
44
|
-
* Generic parameters:
|
|
45
|
-
* - TModel: The specific model name (e.g., 'gpt-4o')
|
|
46
|
-
* - TProviderOptions: Provider-specific options for this model (already resolved)
|
|
47
|
-
* - TInputModalities: Supported input modalities for this model (already resolved)
|
|
48
|
-
* - TMessageMetadata: Metadata types for content parts (already resolved)
|
|
49
|
-
* - TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`
|
|
50
|
-
* - TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)
|
|
51
|
-
* - TSystemPromptMetadata: Provider-typed metadata accepted on each
|
|
52
|
-
* `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to
|
|
53
|
-
* `never` — adapters without per-prompt metadata reject the `metadata`
|
|
54
|
-
* field at the call site.
|
|
55
|
-
*/
|
|
56
|
-
export interface TextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown, TSystemPromptMetadata = never> {
|
|
57
|
-
/** Discriminator for adapter kind */
|
|
58
|
-
readonly kind: 'text';
|
|
59
|
-
/** Provider name identifier (e.g., 'openai', 'anthropic') */
|
|
60
|
-
readonly name: string;
|
|
61
|
-
/** The model this adapter is configured for */
|
|
62
|
-
readonly model: TModel;
|
|
63
|
-
/**
|
|
64
|
-
* Capabilities this adapter requires at runtime. `chat()` validates that the
|
|
65
|
-
* configured middleware provides each one. Model adapters omit this; harness
|
|
66
|
-
* adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.
|
|
67
|
-
* Runtime access to capabilities from inside the adapter is not yet wired —
|
|
68
|
-
* this is the declaration/validation surface only.
|
|
69
|
-
*/
|
|
70
|
-
readonly requires?: ReadonlyArray<CapabilityHandle>;
|
|
71
|
-
/**
|
|
72
|
-
* @internal Type-only properties for inference. Not assigned at runtime.
|
|
73
|
-
*/
|
|
74
|
-
'~types': {
|
|
75
|
-
providerOptions: TProviderOptions;
|
|
76
|
-
inputModalities: TInputModalities;
|
|
77
|
-
messageMetadataByModality: TMessageMetadataByModality;
|
|
78
|
-
toolCapabilities: TToolCapabilities;
|
|
79
|
-
toolCallMetadata: TToolCallMetadata;
|
|
80
|
-
systemPromptMetadata: TSystemPromptMetadata;
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* Stream text completions from the model
|
|
84
|
-
*/
|
|
85
|
-
chatStream: (options: TextOptions<TProviderOptions>) => AsyncIterable<StreamChunk>;
|
|
86
|
-
/**
|
|
87
|
-
* Generate structured output using the provider's native structured output API.
|
|
88
|
-
* This method uses stream: false and sends the JSON schema to the provider
|
|
89
|
-
* to ensure the response conforms to the expected structure.
|
|
90
|
-
*
|
|
91
|
-
* @param options - Structured output options containing chat options and JSON schema
|
|
92
|
-
* @returns Promise with the raw data (validation is done in the chat function)
|
|
93
|
-
*/
|
|
94
|
-
structuredOutput: (options: StructuredOutputOptions<TProviderOptions>) => Promise<StructuredOutputResult<unknown>>;
|
|
95
|
-
/**
|
|
96
|
-
* Stream structured output using the provider's native streaming structured
|
|
97
|
-
* output API (stream + response_format json_schema in a single request).
|
|
98
|
-
*
|
|
99
|
-
* Optional — adapters without native streaming JSON omit this method and the
|
|
100
|
-
* activity layer synthesizes a stream around the non-streaming
|
|
101
|
-
* `structuredOutput` call.
|
|
102
|
-
*
|
|
103
|
-
* Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,
|
|
104
|
-
* TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final
|
|
105
|
-
* `CUSTOM` event named `structured-output.complete` whose `value` is
|
|
106
|
-
* `{ object, raw, reasoning? }`.
|
|
107
|
-
*/
|
|
108
|
-
structuredOutputStream?: (options: StructuredOutputOptions<TProviderOptions>) => AsyncIterable<StreamChunk>;
|
|
109
|
-
/**
|
|
110
|
-
* Declares whether the adapter supports combining `tools` and a
|
|
111
|
-
* schema-constrained final answer in a single streaming request.
|
|
112
|
-
*
|
|
113
|
-
* When `true`, the engine wires `outputSchema` into the regular
|
|
114
|
-
* `chatStream()` call and skips the separate `runStructuredFinalization`
|
|
115
|
-
* round-trip. The model's natural final turn carries the
|
|
116
|
-
* schema-constrained JSON text and the engine harvests it from the agent
|
|
117
|
-
* loop's accumulated content.
|
|
118
|
-
*
|
|
119
|
-
* When `false`, `undefined`, or the method is omitted, the engine runs
|
|
120
|
-
* the agent loop without `outputSchema` and then issues a separate
|
|
121
|
-
* `structuredOutput` / `structuredOutputStream` call against the JSON
|
|
122
|
-
* schema for finalization (the legacy path).
|
|
123
|
-
*
|
|
124
|
-
* The method receives the per-call `modelOptions` so providers whose
|
|
125
|
-
* support depends on the resolved upstream model (e.g. OpenRouter) can
|
|
126
|
-
* answer per-request. Most adapters can return a constant.
|
|
127
|
-
*/
|
|
128
|
-
supportsCombinedToolsAndSchema?: (modelOptions?: TProviderOptions | undefined) => boolean;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* A TextAdapter with any/unknown type parameters.
|
|
132
|
-
* Useful as a constraint in generic functions and interfaces.
|
|
133
|
-
*/
|
|
134
|
-
export type AnyTextAdapter = TextAdapter<any, any, any, any, any, any, any>;
|
|
135
|
-
/**
|
|
136
|
-
* Abstract base class for text adapters.
|
|
137
|
-
* Extend this class to implement a text adapter for a specific provider.
|
|
138
|
-
*
|
|
139
|
-
* Generic parameters match TextAdapter - all pre-resolved by the provider function.
|
|
140
|
-
*/
|
|
141
|
-
export declare abstract class BaseTextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown, TSystemPromptMetadata = never> implements TextAdapter<TModel, TProviderOptions, TInputModalities, TMessageMetadataByModality, TToolCapabilities, TToolCallMetadata, TSystemPromptMetadata> {
|
|
142
|
-
readonly kind: "text";
|
|
143
|
-
abstract readonly name: string;
|
|
144
|
-
readonly model: TModel;
|
|
145
|
-
readonly requires?: ReadonlyArray<CapabilityHandle>;
|
|
146
|
-
'~types': {
|
|
147
|
-
providerOptions: TProviderOptions;
|
|
148
|
-
inputModalities: TInputModalities;
|
|
149
|
-
messageMetadataByModality: TMessageMetadataByModality;
|
|
150
|
-
toolCapabilities: TToolCapabilities;
|
|
151
|
-
toolCallMetadata: TToolCallMetadata;
|
|
152
|
-
systemPromptMetadata: TSystemPromptMetadata;
|
|
153
|
-
};
|
|
154
|
-
protected config: TextAdapterConfig;
|
|
155
|
-
constructor(config: TextAdapterConfig | undefined, model: TModel);
|
|
156
|
-
abstract chatStream(options: TextOptions<TProviderOptions>): AsyncIterable<StreamChunk>;
|
|
157
|
-
/**
|
|
158
|
-
* Generate structured output using the provider's native structured output API.
|
|
159
|
-
* Concrete implementations should override this to use provider-specific structured output.
|
|
160
|
-
*/
|
|
161
|
-
abstract structuredOutput(options: StructuredOutputOptions<TProviderOptions>): Promise<StructuredOutputResult<unknown>>;
|
|
162
|
-
protected generateId(): string;
|
|
163
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
class BaseTextAdapter {
|
|
2
|
-
kind = "text";
|
|
3
|
-
model;
|
|
4
|
-
requires = void 0;
|
|
5
|
-
config;
|
|
6
|
-
constructor(config = {}, model) {
|
|
7
|
-
this.config = config;
|
|
8
|
-
this.model = model;
|
|
9
|
-
}
|
|
10
|
-
generateId() {
|
|
11
|
-
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export {
|
|
15
|
-
BaseTextAdapter
|
|
16
|
-
};
|
|
17
|
-
//# sourceMappingURL=adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sources":["../../../../src/activities/chat/adapter.ts"],"sourcesContent":["import type {\n DefaultMessageMetadataByModality,\n JSONSchema,\n Modality,\n StreamChunk,\n TextOptions,\n TokenUsage,\n} from '../../types'\nimport type { CapabilityHandle } from './middleware/capabilities'\n\n/**\n * Configuration for adapter instances\n */\nexport interface TextAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Options for structured output generation.\n *\n * The internal logger is threaded through `chatOptions.logger` (inherited from\n * `TextOptions`). Adapter implementations must call `logger.request()` before\n * SDK calls, `logger.provider()` for each chunk received, and `logger.errors()`\n * in catch blocks.\n */\nexport interface StructuredOutputOptions<TProviderOptions extends object> {\n /** Text options for the request */\n chatOptions: TextOptions<TProviderOptions>\n /** JSON Schema for structured output - already converted from Zod in the ai layer */\n outputSchema: JSONSchema\n}\n\n/**\n * Result from structured output generation\n */\nexport interface StructuredOutputResult<T = unknown> {\n /** The parsed data conforming to the schema */\n data: T\n /** The raw text response from the model before parsing */\n rawText: string\n /** Token usage information (if provided by the adapter) */\n usage?: TokenUsage\n}\n\n/**\n * Text adapter interface with pre-resolved generics.\n *\n * An adapter is created by a provider function: `provider('model')` → `adapter`\n * All type resolution happens at the provider call site, not in this interface.\n *\n * Generic parameters:\n * - TModel: The specific model name (e.g., 'gpt-4o')\n * - TProviderOptions: Provider-specific options for this model (already resolved)\n * - TInputModalities: Supported input modalities for this model (already resolved)\n * - TMessageMetadata: Metadata types for content parts (already resolved)\n * - TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`\n * - TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)\n * - TSystemPromptMetadata: Provider-typed metadata accepted on each\n * `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to\n * `never` — adapters without per-prompt metadata reject the `metadata`\n * field at the call site.\n */\nexport interface TextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> {\n /** Discriminator for adapter kind */\n readonly kind: 'text'\n /** Provider name identifier (e.g., 'openai', 'anthropic') */\n readonly name: string\n /** The model this adapter is configured for */\n readonly model: TModel\n\n /**\n * Capabilities this adapter requires at runtime. `chat()` validates that the\n * configured middleware provides each one. Model adapters omit this; harness\n * adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.\n * Runtime access to capabilities from inside the adapter is not yet wired —\n * this is the declaration/validation surface only.\n */\n readonly requires?: ReadonlyArray<CapabilityHandle>\n\n /**\n * @internal Type-only properties for inference. Not assigned at runtime.\n */\n '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n /**\n * Stream text completions from the model\n */\n chatStream: (\n options: TextOptions<TProviderOptions>,\n ) => AsyncIterable<StreamChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * This method uses stream: false and sends the JSON schema to the provider\n * to ensure the response conforms to the expected structure.\n *\n * @param options - Structured output options containing chat options and JSON schema\n * @returns Promise with the raw data (validation is done in the chat function)\n */\n structuredOutput: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => Promise<StructuredOutputResult<unknown>>\n\n /**\n * Stream structured output using the provider's native streaming structured\n * output API (stream + response_format json_schema in a single request).\n *\n * Optional — adapters without native streaming JSON omit this method and the\n * activity layer synthesizes a stream around the non-streaming\n * `structuredOutput` call.\n *\n * Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,\n * TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final\n * `CUSTOM` event named `structured-output.complete` whose `value` is\n * `{ object, raw, reasoning? }`.\n */\n structuredOutputStream?: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => AsyncIterable<StreamChunk>\n\n /**\n * Declares whether the adapter supports combining `tools` and a\n * schema-constrained final answer in a single streaming request.\n *\n * When `true`, the engine wires `outputSchema` into the regular\n * `chatStream()` call and skips the separate `runStructuredFinalization`\n * round-trip. The model's natural final turn carries the\n * schema-constrained JSON text and the engine harvests it from the agent\n * loop's accumulated content.\n *\n * When `false`, `undefined`, or the method is omitted, the engine runs\n * the agent loop without `outputSchema` and then issues a separate\n * `structuredOutput` / `structuredOutputStream` call against the JSON\n * schema for finalization (the legacy path).\n *\n * The method receives the per-call `modelOptions` so providers whose\n * support depends on the resolved upstream model (e.g. OpenRouter) can\n * answer per-request. Most adapters can return a constant.\n */\n supportsCombinedToolsAndSchema?: (\n modelOptions?: TProviderOptions | undefined,\n ) => boolean\n}\n\n/**\n * A TextAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyTextAdapter = TextAdapter<any, any, any, any, any, any, any>\n\n/**\n * Abstract base class for text adapters.\n * Extend this class to implement a text adapter for a specific provider.\n *\n * Generic parameters match TextAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> implements TextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadataByModality,\n TToolCapabilities,\n TToolCallMetadata,\n TSystemPromptMetadata\n> {\n readonly kind = 'text' as const\n abstract readonly name: string\n readonly model: TModel\n readonly requires?: ReadonlyArray<CapabilityHandle> = undefined\n\n // Type-only property - never assigned at runtime\n declare '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n protected config: TextAdapterConfig\n\n constructor(config: TextAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<StreamChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * Concrete implementations should override this to use provider-specific structured output.\n */\n abstract structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"names":[],"mappings":"AA+KO,MAAe,gBAgBpB;AAAA,EACS,OAAO;AAAA,EAEP;AAAA,EACA,WAA6C;AAAA,EAY5C;AAAA,EAEV,YAAY,SAA4B,CAAA,GAAI,OAAe;AACzD,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAcU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { AgentLoopStrategy } from '../../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Creates a strategy that continues for a maximum number of iterations
|
|
4
|
-
*
|
|
5
|
-
* @param max - Maximum number of iterations to allow
|
|
6
|
-
* @returns AgentLoopStrategy that stops after max iterations
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```typescript
|
|
10
|
-
* const stream = chat({
|
|
11
|
-
* adapter: openaiText(),
|
|
12
|
-
* model: "gpt-4o",
|
|
13
|
-
* messages: [...],
|
|
14
|
-
* tools: [weatherTool],
|
|
15
|
-
* agentLoopStrategy: maxIterations(3), // Max 3 iterations
|
|
16
|
-
* });
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export declare function maxIterations(max: number): AgentLoopStrategy;
|
|
20
|
-
/**
|
|
21
|
-
* Creates a strategy that continues until a specific finish reason is encountered
|
|
22
|
-
*
|
|
23
|
-
* @param stopReasons - Finish reasons that should stop the loop
|
|
24
|
-
* @returns AgentLoopStrategy that stops on specific finish reasons
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```typescript
|
|
28
|
-
* const stream = chat({
|
|
29
|
-
* adapter: openaiText(),
|
|
30
|
-
* model: "gpt-4o",
|
|
31
|
-
* messages: [...],
|
|
32
|
-
* tools: [weatherTool],
|
|
33
|
-
* agentLoopStrategy: untilFinishReason(["stop", "length"]),
|
|
34
|
-
* });
|
|
35
|
-
* ```
|
|
36
|
-
*/
|
|
37
|
-
export declare function untilFinishReason(stopReasons: Array<string>): AgentLoopStrategy;
|
|
38
|
-
/**
|
|
39
|
-
* Creates a strategy that combines multiple strategies with AND logic
|
|
40
|
-
* All strategies must return true to continue
|
|
41
|
-
*
|
|
42
|
-
* @param strategies - Array of strategies to combine
|
|
43
|
-
* @returns AgentLoopStrategy that continues only if all strategies return true
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* ```typescript
|
|
47
|
-
* const stream = chat({
|
|
48
|
-
* adapter: openaiText(),
|
|
49
|
-
* model: "gpt-4o",
|
|
50
|
-
* messages: [...],
|
|
51
|
-
* tools: [weatherTool],
|
|
52
|
-
* agentLoopStrategy: combineStrategies([
|
|
53
|
-
* maxIterations(10),
|
|
54
|
-
* ({ messages }) => messages.length < 100,
|
|
55
|
-
* ]),
|
|
56
|
-
* });
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
export declare function combineStrategies(strategies: Array<AgentLoopStrategy>): AgentLoopStrategy;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
function maxIterations(max) {
|
|
2
|
-
return ({ iterationCount }) => iterationCount < max;
|
|
3
|
-
}
|
|
4
|
-
function untilFinishReason(stopReasons) {
|
|
5
|
-
return ({ finishReason, iterationCount }) => {
|
|
6
|
-
if (iterationCount === 0) return true;
|
|
7
|
-
if (finishReason && stopReasons.includes(finishReason)) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
return true;
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
function combineStrategies(strategies) {
|
|
14
|
-
return (state) => {
|
|
15
|
-
return strategies.every((strategy) => strategy(state));
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
export {
|
|
19
|
-
combineStrategies,
|
|
20
|
-
maxIterations,
|
|
21
|
-
untilFinishReason
|
|
22
|
-
};
|
|
23
|
-
//# sourceMappingURL=agent-loop-strategies.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent-loop-strategies.js","sources":["../../../../src/activities/chat/agent-loop-strategies.ts"],"sourcesContent":["import type { AgentLoopStrategy } from '../../types'\n\n/**\n * Creates a strategy that continues for a maximum number of iterations\n *\n * @param max - Maximum number of iterations to allow\n * @returns AgentLoopStrategy that stops after max iterations\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: maxIterations(3), // Max 3 iterations\n * });\n * ```\n */\nexport function maxIterations(max: number): AgentLoopStrategy {\n return ({ iterationCount }) => iterationCount < max\n}\n\n/**\n * Creates a strategy that continues until a specific finish reason is encountered\n *\n * @param stopReasons - Finish reasons that should stop the loop\n * @returns AgentLoopStrategy that stops on specific finish reasons\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: untilFinishReason([\"stop\", \"length\"]),\n * });\n * ```\n */\nexport function untilFinishReason(\n stopReasons: Array<string>,\n): AgentLoopStrategy {\n return ({ finishReason, iterationCount }) => {\n // Always allow at least one iteration\n if (iterationCount === 0) return true\n\n // Stop if we hit a stop reason\n if (finishReason && stopReasons.includes(finishReason)) {\n return false\n }\n\n // Otherwise continue\n return true\n }\n}\n\n/**\n * Creates a strategy that combines multiple strategies with AND logic\n * All strategies must return true to continue\n *\n * @param strategies - Array of strategies to combine\n * @returns AgentLoopStrategy that continues only if all strategies return true\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: combineStrategies([\n * maxIterations(10),\n * ({ messages }) => messages.length < 100,\n * ]),\n * });\n * ```\n */\nexport function combineStrategies(\n strategies: Array<AgentLoopStrategy>,\n): AgentLoopStrategy {\n return (state) => {\n return strategies.every((strategy) => strategy(state))\n }\n}\n"],"names":[],"mappings":"AAmBO,SAAS,cAAc,KAAgC;AAC5D,SAAO,CAAC,EAAE,qBAAqB,iBAAiB;AAClD;AAmBO,SAAS,kBACd,aACmB;AACnB,SAAO,CAAC,EAAE,cAAc,qBAAqB;AAE3C,QAAI,mBAAmB,EAAG,QAAO;AAGjC,QAAI,gBAAgB,YAAY,SAAS,YAAY,GAAG;AACtD,aAAO;AAAA,IACT;AAGA,WAAO;AAAA,EACT;AACF;AAuBO,SAAS,kBACd,YACmB;AACnB,SAAO,CAAC,UAAU;AAChB,WAAO,WAAW,MAAM,CAAC,aAAa,SAAS,KAAK,CAAC;AAAA,EACvD;AACF;"}
|