@tanstack/ai 0.0.1
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/LICENSE +21 -0
- package/README.md +131 -0
- package/dist/esm/base-adapter.d.ts +35 -0
- package/dist/esm/base-adapter.js +12 -0
- package/dist/esm/base-adapter.js.map +1 -0
- package/dist/esm/core/chat-common-options.d.ts +52 -0
- package/dist/esm/core/chat.d.ts +30 -0
- package/dist/esm/core/chat.js +533 -0
- package/dist/esm/core/chat.js.map +1 -0
- package/dist/esm/core/embedding.d.ts +8 -0
- package/dist/esm/core/embedding.js +33 -0
- package/dist/esm/core/embedding.js.map +1 -0
- package/dist/esm/core/summarize.d.ts +9 -0
- package/dist/esm/core/summarize.js +36 -0
- package/dist/esm/core/summarize.js.map +1 -0
- package/dist/esm/event-client.d.ts +311 -0
- package/dist/esm/event-client.js +62 -0
- package/dist/esm/event-client.js.map +1 -0
- package/dist/esm/index.d.ts +16 -0
- package/dist/esm/index.js +50 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/message-converters.d.ts +52 -0
- package/dist/esm/message-converters.js +162 -0
- package/dist/esm/message-converters.js.map +1 -0
- package/dist/esm/stream/index.d.ts +11 -0
- package/dist/esm/stream/json-parser.d.ts +38 -0
- package/dist/esm/stream/json-parser.js +28 -0
- package/dist/esm/stream/json-parser.js.map +1 -0
- package/dist/esm/stream/message-updaters.d.ts +44 -0
- package/dist/esm/stream/message-updaters.js +141 -0
- package/dist/esm/stream/message-updaters.js.map +1 -0
- package/dist/esm/stream/processor.d.ts +242 -0
- package/dist/esm/stream/processor.js +693 -0
- package/dist/esm/stream/processor.js.map +1 -0
- package/dist/esm/stream/strategies.d.ts +43 -0
- package/dist/esm/stream/strategies.js +54 -0
- package/dist/esm/stream/strategies.js.map +1 -0
- package/dist/esm/stream/types.d.ts +71 -0
- package/dist/esm/tools/tool-calls.d.ts +112 -0
- package/dist/esm/tools/tool-calls.js +302 -0
- package/dist/esm/tools/tool-calls.js.map +1 -0
- package/dist/esm/tools/tool-definition.d.ts +125 -0
- package/dist/esm/tools/tool-definition.js +25 -0
- package/dist/esm/tools/tool-definition.js.map +1 -0
- package/dist/esm/tools/zod-converter.d.ts +30 -0
- package/dist/esm/tools/zod-converter.js +36 -0
- package/dist/esm/tools/zod-converter.js.map +1 -0
- package/dist/esm/types.d.ts +619 -0
- package/dist/esm/utilities/agent-loop-strategies.d.ts +59 -0
- package/dist/esm/utilities/agent-loop-strategies.js +23 -0
- package/dist/esm/utilities/agent-loop-strategies.js.map +1 -0
- package/dist/esm/utilities/chat-options.d.ts +6 -0
- package/dist/esm/utilities/chat-options.js +7 -0
- package/dist/esm/utilities/chat-options.js.map +1 -0
- package/dist/esm/utilities/messages.d.ts +30 -0
- package/dist/esm/utilities/messages.js +7 -0
- package/dist/esm/utilities/messages.js.map +1 -0
- package/dist/esm/utilities/stream-to-response.d.ts +48 -0
- package/dist/esm/utilities/stream-to-response.js +62 -0
- package/dist/esm/utilities/stream-to-response.js.map +1 -0
- package/package.json +65 -0
- package/src/base-adapter.ts +85 -0
- package/src/core/chat-common-options.ts +55 -0
- package/src/core/chat.ts +771 -0
- package/src/core/embedding.ts +54 -0
- package/src/core/summarize.ts +56 -0
- package/src/event-client.ts +389 -0
- package/src/index.ts +68 -0
- package/src/message-converters.ts +285 -0
- package/src/stream/index.ts +41 -0
- package/src/stream/json-parser.ts +58 -0
- package/src/stream/message-updaters.ts +275 -0
- package/src/stream/processor.ts +1092 -0
- package/src/stream/strategies.ts +78 -0
- package/src/stream/types.ts +94 -0
- package/src/tools/tool-calls.ts +471 -0
- package/src/tools/tool-definition.ts +206 -0
- package/src/tools/zod-converter.ts +85 -0
- package/src/types.ts +872 -0
- package/src/utilities/agent-loop-strategies.ts +85 -0
- package/src/utilities/chat-options.ts +35 -0
- package/src/utilities/messages.ts +63 -0
- package/src/utilities/stream-to-response.ts +116 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tanner Linsley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="./media/header_ai.png" >
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<br />
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<a href="https://npmjs.com/package/@tanstack/ai" target="\_parent">
|
|
9
|
+
<img alt="" src="https://img.shields.io/npm/dm/@tanstack/ai.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/TanStack/ai" target="\_parent">
|
|
12
|
+
<img alt="" src="https://img.shields.io/github/stars/TanStack/ai.svg?style=social&label=Star" alt="GitHub stars" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://bundlephobia.com/result?p=@tanstack/ai@latest" target="\_parent">
|
|
15
|
+
<img alt="" src="https://badgen.net/bundlephobia/minzip/@tanstack/ai@latest" />
|
|
16
|
+
</a>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<div align="center">
|
|
20
|
+
<a href="#badge">
|
|
21
|
+
<img alt="semantic-release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg">
|
|
22
|
+
</a>
|
|
23
|
+
<a href="#badge">
|
|
24
|
+
<img src="https://img.shields.io/github/v/release/tanstack/ai" alt="Release"/>
|
|
25
|
+
</a>
|
|
26
|
+
<a href="https://twitter.com/tan_stack">
|
|
27
|
+
<img src="https://img.shields.io/twitter/follow/tan_stack.svg?style=social" alt="Follow @TanStack"/>
|
|
28
|
+
</a>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<div align="center">
|
|
32
|
+
|
|
33
|
+
### [Become a Sponsor!](https://github.com/sponsors/tannerlinsley/)
|
|
34
|
+
</div>
|
|
35
|
+
|
|
36
|
+
# TanStack AI
|
|
37
|
+
|
|
38
|
+
A powerful, type-safe AI SDK for building AI-powered applications.
|
|
39
|
+
|
|
40
|
+
- Provider-agnostic adapters (OpenAI, Anthropic, Gemini, Ollama, etc.)
|
|
41
|
+
- **Multimodal content support** - Send images, audio, video, and documents
|
|
42
|
+
- Chat completion, streaming, and agent loop strategies
|
|
43
|
+
- Headless chat state management with adapters (SSE, HTTP stream, custom)
|
|
44
|
+
- Isomorphic type-safe tools with server/client execution
|
|
45
|
+
- **Enhanced integration with TanStack Start** - Share implementations between AI tools and server functions
|
|
46
|
+
|
|
47
|
+
### <a href="https://tanstack.com/ai">Read the docs →</b></a>
|
|
48
|
+
|
|
49
|
+
## Bonus: TanStack Start Integration
|
|
50
|
+
|
|
51
|
+
TanStack AI works with **any** framework (Next.js, Express, Remix, etc.).
|
|
52
|
+
|
|
53
|
+
**With TanStack Start**, you get a bonus: share implementations between AI tools and server functions with `createServerFnTool`:
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
import { createServerFnTool } from '@tanstack/ai-react'
|
|
57
|
+
|
|
58
|
+
// Define once, get AI tool AND server function (TanStack Start only)
|
|
59
|
+
const getProducts = createServerFnTool({
|
|
60
|
+
name: 'getProducts',
|
|
61
|
+
inputSchema: z.object({ query: z.string() }),
|
|
62
|
+
execute: async ({ query }) => db.products.search(query),
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Use in AI chat
|
|
66
|
+
chat({ tools: [getProducts.server] })
|
|
67
|
+
|
|
68
|
+
// Call directly from components (no API endpoint needed!)
|
|
69
|
+
const products = await getProducts.serverFn({ query: 'laptop' })
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
No duplicate logic, full type safety, automatic validation. The `serverFn` feature requires TanStack Start. See [docs](https://tanstack.com/ai) for details.
|
|
73
|
+
|
|
74
|
+
## Get Involved
|
|
75
|
+
|
|
76
|
+
- We welcome issues and pull requests!
|
|
77
|
+
- Participate in [GitHub discussions](https://github.com/TanStack/ai/discussions)
|
|
78
|
+
- Chat with the community on [Discord](https://discord.com/invite/WrRKjPJ)
|
|
79
|
+
- See [CONTRIBUTING.md](./CONTRIBUTING.md) for setup instructions
|
|
80
|
+
|
|
81
|
+
## Partners
|
|
82
|
+
|
|
83
|
+
<table align="center">
|
|
84
|
+
<tr>
|
|
85
|
+
<td>
|
|
86
|
+
<a href="https://www.coderabbit.ai/?via=tanstack&dub_id=aCcEEdAOqqutX6OS" >
|
|
87
|
+
<picture>
|
|
88
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/coderabbit-dark-CMcuvjEy.svg" height="40" />
|
|
89
|
+
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/coderabbit-light-DVMJ2jHi.svg" height="40" />
|
|
90
|
+
<img src="https://tanstack.com/assets/coderabbit-light-DVMJ2jHi.svg" height="40" alt="CodeRabbit" />
|
|
91
|
+
</picture>
|
|
92
|
+
</a>
|
|
93
|
+
</td>
|
|
94
|
+
<td>
|
|
95
|
+
<a href="https://www.cloudflare.com?utm_source=tanstack">
|
|
96
|
+
<picture>
|
|
97
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://tanstack.com/assets/cloudflare-white-DQDB7UaL.svg" height="60" />
|
|
98
|
+
<source media="(prefers-color-scheme: light)" srcset="https://tanstack.com/assets/cloudflare-black-CPufaW0B.svg" height="60" />
|
|
99
|
+
<img src="https://tanstack.com/assets/cloudflare-black-CPufaW0B.svg" height="60" alt="Cloudflare" />
|
|
100
|
+
</picture>
|
|
101
|
+
</a>
|
|
102
|
+
</td>
|
|
103
|
+
</tr>
|
|
104
|
+
</table>
|
|
105
|
+
|
|
106
|
+
<div align="center">
|
|
107
|
+
<img src="./media/partner_logo.svg" alt="AI & you?" height="65">
|
|
108
|
+
<p>
|
|
109
|
+
We're looking for TanStack AI Partners to join our mission! Partner with us to push the boundaries of TanStack AI and build amazing things together.
|
|
110
|
+
</p>
|
|
111
|
+
<a href="mailto:partners@tanstack.com?subject=TanStack AI Partnership"><b>LET'S CHAT</b></a>
|
|
112
|
+
</div>
|
|
113
|
+
|
|
114
|
+
## Explore the TanStack Ecosystem
|
|
115
|
+
|
|
116
|
+
- <a href="https://github.com/tanstack/config"><b>TanStack Config</b></a> – Tooling for JS/TS packages
|
|
117
|
+
- <a href="https://github.com/tanstack/db"><b>TanStack DB</b></a> – Reactive sync client store
|
|
118
|
+
- <a href="https://github.com/tanstack/devtools">TanStack Devtools</a> – Unified devtools panel
|
|
119
|
+
- <a href="https://github.com/tanstack/form"><b>TanStack Form</b></a> – Type‑safe form state
|
|
120
|
+
- <a href="https://github.com/tanstack/pacer"><b>TanStack Pacer</b></a> – Debouncing, throttling, batching
|
|
121
|
+
- <a href="https://github.com/tanstack/query"><b>TanStack Query</b></a> – Async state & caching
|
|
122
|
+
- <a href="https://github.com/tanstack/ranger"><b>TanStack Ranger</b></a> – Range & slider primitives
|
|
123
|
+
- <a href="https://github.com/tanstack/router"><b>TanStack Router</b></a> – Type‑safe routing, caching & URL state
|
|
124
|
+
- <a href="https://github.com/tanstack/router"><b>TanStack Start</b></a> – Full‑stack SSR & streaming
|
|
125
|
+
- <a href="https://github.com/tanstack/store"><b>TanStack Store</b></a> – Reactive data store
|
|
126
|
+
- <a href="https://github.com/tanstack/table"><b>TanStack Table</b></a> – Headless datagrids
|
|
127
|
+
- <a href="https://github.com/tanstack/virtual"><b>TanStack Virtual</b></a> – Virtualized rendering
|
|
128
|
+
|
|
129
|
+
… and more at <a href="https://tanstack.com"><b>TanStack.com »</b></a>
|
|
130
|
+
|
|
131
|
+
<!-- USE THE FORCE LUKE -->
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { AIAdapter, AIAdapterConfig, ChatOptions, DefaultMessageMetadataByModality, EmbeddingOptions, EmbeddingResult, Modality, StreamChunk, SummarizationOptions, SummarizationResult } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base adapter class with support for endpoint-specific models and provider options.
|
|
4
|
+
*
|
|
5
|
+
* Generic parameters:
|
|
6
|
+
* - TChatModels: Models that support chat/text completion
|
|
7
|
+
* - TEmbeddingModels: Models that support embeddings
|
|
8
|
+
* - TChatProviderOptions: Provider-specific options for chat endpoint
|
|
9
|
+
* - TEmbeddingProviderOptions: Provider-specific options for embedding endpoint
|
|
10
|
+
* - TModelProviderOptionsByName: Provider-specific options for model by name
|
|
11
|
+
* - TModelInputModalitiesByName: Map from model name to its supported input modalities
|
|
12
|
+
* - TMessageMetadataByModality: Map from modality type to adapter-specific metadata types
|
|
13
|
+
*/
|
|
14
|
+
export declare abstract class BaseAdapter<TChatModels extends ReadonlyArray<string> = ReadonlyArray<string>, TEmbeddingModels extends ReadonlyArray<string> = ReadonlyArray<string>, TChatProviderOptions extends Record<string, any> = Record<string, any>, TEmbeddingProviderOptions extends Record<string, any> = Record<string, any>, TModelProviderOptionsByName extends Record<string, any> = Record<string, any>, TModelInputModalitiesByName extends Record<string, ReadonlyArray<Modality>> = Record<string, ReadonlyArray<Modality>>, TMessageMetadataByModality extends {
|
|
15
|
+
image: unknown;
|
|
16
|
+
audio: unknown;
|
|
17
|
+
video: unknown;
|
|
18
|
+
document: unknown;
|
|
19
|
+
} = DefaultMessageMetadataByModality> implements AIAdapter<TChatModels, TEmbeddingModels, TChatProviderOptions, TEmbeddingProviderOptions, TModelProviderOptionsByName, TModelInputModalitiesByName, TMessageMetadataByModality> {
|
|
20
|
+
abstract name: string;
|
|
21
|
+
abstract models: TChatModels;
|
|
22
|
+
embeddingModels?: TEmbeddingModels;
|
|
23
|
+
protected config: AIAdapterConfig;
|
|
24
|
+
_providerOptions?: TChatProviderOptions;
|
|
25
|
+
_chatProviderOptions?: TChatProviderOptions;
|
|
26
|
+
_embeddingProviderOptions?: TEmbeddingProviderOptions;
|
|
27
|
+
_modelProviderOptionsByName: TModelProviderOptionsByName;
|
|
28
|
+
_modelInputModalitiesByName?: TModelInputModalitiesByName;
|
|
29
|
+
_messageMetadataByModality?: TMessageMetadataByModality;
|
|
30
|
+
constructor(config?: AIAdapterConfig);
|
|
31
|
+
abstract chatStream(options: ChatOptions): AsyncIterable<StreamChunk>;
|
|
32
|
+
abstract summarize(options: SummarizationOptions): Promise<SummarizationResult>;
|
|
33
|
+
abstract createEmbeddings(options: EmbeddingOptions): Promise<EmbeddingResult>;
|
|
34
|
+
protected generateId(): string;
|
|
35
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
class BaseAdapter {
|
|
2
|
+
constructor(config = {}) {
|
|
3
|
+
this.config = config;
|
|
4
|
+
}
|
|
5
|
+
generateId() {
|
|
6
|
+
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export {
|
|
10
|
+
BaseAdapter
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=base-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-adapter.js","sources":["../../src/base-adapter.ts"],"sourcesContent":["import type {\n AIAdapter,\n AIAdapterConfig,\n ChatOptions,\n DefaultMessageMetadataByModality,\n EmbeddingOptions,\n EmbeddingResult,\n Modality,\n StreamChunk,\n SummarizationOptions,\n SummarizationResult,\n} from './types'\n\n/**\n * Base adapter class with support for endpoint-specific models and provider options.\n *\n * Generic parameters:\n * - TChatModels: Models that support chat/text completion\n * - TEmbeddingModels: Models that support embeddings\n * - TChatProviderOptions: Provider-specific options for chat endpoint\n * - TEmbeddingProviderOptions: Provider-specific options for embedding endpoint\n * - TModelProviderOptionsByName: Provider-specific options for model by name\n * - TModelInputModalitiesByName: Map from model name to its supported input modalities\n * - TMessageMetadataByModality: Map from modality type to adapter-specific metadata types\n */\nexport abstract class BaseAdapter<\n TChatModels extends ReadonlyArray<string> = ReadonlyArray<string>,\n TEmbeddingModels extends ReadonlyArray<string> = ReadonlyArray<string>,\n TChatProviderOptions extends Record<string, any> = Record<string, any>,\n TEmbeddingProviderOptions extends Record<string, any> = Record<string, any>,\n TModelProviderOptionsByName extends Record<string, any> = Record<string, any>,\n TModelInputModalitiesByName extends Record<\n string,\n ReadonlyArray<Modality>\n > = Record<string, ReadonlyArray<Modality>>,\n TMessageMetadataByModality extends {\n image: unknown\n audio: unknown\n video: unknown\n document: unknown\n } = DefaultMessageMetadataByModality,\n> implements\n AIAdapter<\n TChatModels,\n TEmbeddingModels,\n TChatProviderOptions,\n TEmbeddingProviderOptions,\n TModelProviderOptionsByName,\n TModelInputModalitiesByName,\n TMessageMetadataByModality\n >\n{\n abstract name: string\n abstract models: TChatModels\n embeddingModels?: TEmbeddingModels\n protected config: AIAdapterConfig\n\n // These properties are used for type inference only, never assigned at runtime\n _providerOptions?: TChatProviderOptions\n _chatProviderOptions?: TChatProviderOptions\n _embeddingProviderOptions?: TEmbeddingProviderOptions\n // Type-only map; concrete adapters should override this with a precise type\n _modelProviderOptionsByName!: TModelProviderOptionsByName\n // Type-only map for model input modalities; concrete adapters should override this\n _modelInputModalitiesByName?: TModelInputModalitiesByName\n // Type-only map for message metadata types; concrete adapters should override this\n _messageMetadataByModality?: TMessageMetadataByModality\n\n constructor(config: AIAdapterConfig = {}) {\n this.config = config\n }\n\n abstract chatStream(options: ChatOptions): AsyncIterable<StreamChunk>\n\n abstract summarize(\n options: SummarizationOptions,\n ): Promise<SummarizationResult>\n abstract createEmbeddings(options: EmbeddingOptions): Promise<EmbeddingResult>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random()\n .toString(36)\n .substring(7)}`\n }\n}\n"],"names":[],"mappings":"AAyBO,MAAe,YA0BtB;AAAA,EAiBE,YAAY,SAA0B,IAAI;AACxC,SAAK,SAAS;AAAA,EAChB;AAAA,EASU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EACvC,SAAS,EAAE,EACX,UAAU,CAAC,CAAC;AAAA,EACjB;AACF;"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Common options shared across different AI provider implementations.
|
|
3
|
+
* These options represent the standard parameters that work across OpenAI, Anthropic, and Gemini.
|
|
4
|
+
*/
|
|
5
|
+
export interface CommonOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Controls the randomness of the output.
|
|
8
|
+
* Higher values (e.g., 0.8) make output more random, lower values (e.g., 0.2) make it more focused and deterministic.
|
|
9
|
+
* Range: [0.0, 2.0]
|
|
10
|
+
*
|
|
11
|
+
* Note: Generally recommended to use either temperature or topP, but not both.
|
|
12
|
+
*
|
|
13
|
+
* Provider usage:
|
|
14
|
+
* - OpenAI: `temperature` (number) - in text.top_p field
|
|
15
|
+
* - Anthropic: `temperature` (number) - ranges from 0.0 to 1.0, default 1.0
|
|
16
|
+
* - Gemini: `generationConfig.temperature` (number) - ranges from 0.0 to 2.0
|
|
17
|
+
*/
|
|
18
|
+
temperature?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Nucleus sampling parameter. An alternative to temperature sampling.
|
|
21
|
+
* The model considers the results of tokens with topP probability mass.
|
|
22
|
+
* For example, 0.1 means only tokens comprising the top 10% probability mass are considered.
|
|
23
|
+
*
|
|
24
|
+
* Note: Generally recommended to use either temperature or topP, but not both.
|
|
25
|
+
*
|
|
26
|
+
* Provider usage:
|
|
27
|
+
* - OpenAI: `text.top_p` (number)
|
|
28
|
+
* - Anthropic: `top_p` (number | null)
|
|
29
|
+
* - Gemini: `generationConfig.topP` (number)
|
|
30
|
+
*/
|
|
31
|
+
topP?: number;
|
|
32
|
+
/**
|
|
33
|
+
* The maximum number of tokens to generate in the response.
|
|
34
|
+
*
|
|
35
|
+
* Provider usage:
|
|
36
|
+
* - OpenAI: `max_output_tokens` (number) - includes visible output and reasoning tokens
|
|
37
|
+
* - Anthropic: `max_tokens` (number, required) - range x >= 1
|
|
38
|
+
* - Gemini: `generationConfig.maxOutputTokens` (number)
|
|
39
|
+
*/
|
|
40
|
+
maxTokens?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Additional metadata to attach to the request.
|
|
43
|
+
* Can be used for tracking, debugging, or passing custom information.
|
|
44
|
+
* Structure and constraints vary by provider.
|
|
45
|
+
*
|
|
46
|
+
* Provider usage:
|
|
47
|
+
* - OpenAI: `metadata` (Record<string, string>) - max 16 key-value pairs, keys max 64 chars, values max 512 chars
|
|
48
|
+
* - Anthropic: `metadata` (Record<string, any>) - includes optional user_id (max 256 chars)
|
|
49
|
+
* - Gemini: Not directly available in TextProviderOptions
|
|
50
|
+
*/
|
|
51
|
+
metadata?: Record<string, any>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AIAdapter, ChatStreamOptionsForModel, StreamChunk } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Standalone chat streaming function with type inference from adapter
|
|
4
|
+
* Returns an async iterable of StreamChunks for streaming responses
|
|
5
|
+
* Includes automatic tool execution loop
|
|
6
|
+
*
|
|
7
|
+
* @param options Chat options
|
|
8
|
+
* @param options.adapter - AI adapter instance to use
|
|
9
|
+
* @param options.model - Model name (autocompletes based on adapter)
|
|
10
|
+
* @param options.messages - Conversation messages
|
|
11
|
+
* @param options.tools - Optional tools for function calling (auto-executed)
|
|
12
|
+
* @param options.agentLoopStrategy - Optional strategy for controlling tool execution loop
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const stream = chat({
|
|
17
|
+
* adapter: openai(),
|
|
18
|
+
* model: 'gpt-4o',
|
|
19
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
20
|
+
* tools: [weatherTool], // Optional: auto-executed when called
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* for await (const chunk of stream) {
|
|
24
|
+
* if (chunk.type === 'content') {
|
|
25
|
+
* console.log(chunk.delta);
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function chat<TAdapter extends AIAdapter<any, any, any, any, any, any>, const TModel extends TAdapter extends AIAdapter<infer Models, any, any, any, any, any> ? Models[number] : string>(options: ChatStreamOptionsForModel<TAdapter, TModel>): AsyncIterable<StreamChunk>;
|