@orpc/ai-sdk 1.14.11 → 1.14.13
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/README.md +48 -52
- package/dist/index.d.mts +59 -106
- package/dist/index.d.ts +59 -106
- package/dist/index.mjs +33 -132
- package/package.json +11 -12
package/README.md
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
<
|
|
1
|
+
<div align="center">
|
|
2
|
+
<image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<h1></h1>
|
|
2
6
|
|
|
3
7
|
<div align="center">
|
|
4
8
|
<a href="https://codecov.io/gh/middleapi/orpc">
|
|
5
|
-
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/
|
|
9
|
+
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/1.x/graph/badge.svg">
|
|
6
10
|
</a>
|
|
7
11
|
<a href="https://www.npmjs.com/package/@orpc/ai-sdk">
|
|
8
12
|
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fai-sdk?logo=npm" />
|
|
9
13
|
</a>
|
|
10
|
-
<a href="https://
|
|
11
|
-
<img alt="CodSpeed" src="https://img.shields.io/endpoint?url=https://codspeed.io/badge.json" />
|
|
12
|
-
</a>
|
|
13
|
-
<a href="https://github.com/middleapi/orpc/blob/main/LICENSE">
|
|
14
|
+
<a href="https://github.com/middleapi/orpc/blob/1.x/LICENSE">
|
|
14
15
|
<img alt="MIT License" src="https://img.shields.io/github/license/middleapi/orpc?logo=open-source-initiative" />
|
|
15
16
|
</a>
|
|
16
17
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
@@ -21,53 +22,55 @@
|
|
|
21
22
|
</a>
|
|
22
23
|
</div>
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
You can read the documentation [here](https://orpc.dev).
|
|
27
|
-
|
|
28
|
-
## Packages
|
|
29
|
-
|
|
30
|
-
**Core**
|
|
25
|
+
<h3 align="center">Typesafe APIs Made Simple 🪄</h3>
|
|
31
26
|
|
|
32
|
-
|
|
33
|
-
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build APIs or implement contracts.
|
|
34
|
-
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume APIs with end-to-end type safety.
|
|
35
|
-
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Add OpenAPI compatibility to APIs.
|
|
27
|
+
**oRPC is a powerful combination of RPC and OpenAPI**, makes it easy to build APIs that are end-to-end type-safe and adhere to OpenAPI standards
|
|
36
28
|
|
|
37
|
-
|
|
29
|
+
---
|
|
38
30
|
|
|
39
|
-
|
|
40
|
-
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): Integrate with [Valibot](https://valibot.dev/).
|
|
41
|
-
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): Integrate with [ArkType](https://arktype.io/).
|
|
31
|
+
## Highlights
|
|
42
32
|
|
|
43
|
-
|
|
33
|
+
- **🔗 End-to-End Type Safety**: Ensure type-safe inputs, outputs, and errors from client to server.
|
|
34
|
+
- **📘 First-Class OpenAPI**: Built-in support that fully adheres to the OpenAPI standard.
|
|
35
|
+
- **📝 Contract-First Development**: Optionally define your API contract before implementation.
|
|
36
|
+
- **🔍 First-Class OpenTelemetry**: Seamlessly integrate with OpenTelemetry for observability.
|
|
37
|
+
- **⚙️ Framework Integrations**: Seamlessly integrate with TanStack Query (React, Vue, Solid, Svelte, Angular), SWR, Pinia Colada, and more.
|
|
38
|
+
- **🚀 Server Actions**: Fully compatible with React Server Actions on Next.js, TanStack Start, and other platforms.
|
|
39
|
+
- **🔠 Standard Schema Support**: Works out of the box with Zod, Valibot, ArkType, and other schema validators.
|
|
40
|
+
- **🗃️ Native Types**: Supports native types like Date, File, Blob, BigInt, URL, and more.
|
|
41
|
+
- **⏱️ Lazy Router**: Enhance cold start times with our lazy routing feature.
|
|
42
|
+
- **📡 SSE & Streaming**: Enjoy full type-safe support for SSE and streaming.
|
|
43
|
+
- **🌍 Multi-Runtime Support**: Fast and lightweight on Cloudflare, Deno, Bun, Node.js, and beyond.
|
|
44
|
+
- **🔌 Extendability**: Easily extend functionality with plugins, middleware, and interceptors.
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
- [@orpc/ratelimit](https://www.npmjs.com/package/@orpc/ratelimit): Rate limiting with memory, Redis, and Upstash adapters.
|
|
47
|
-
- [@orpc/json-schema](https://www.npmjs.com/package/@orpc/json-schema): Smart coercion for OpenAPI requests.
|
|
48
|
-
|
|
49
|
-
**Framework & ecosystem integrations**
|
|
46
|
+
## Documentation
|
|
50
47
|
|
|
51
|
-
|
|
52
|
-
- [@orpc/ai-sdk](https://www.npmjs.com/package/@orpc/ai-sdk): Turn contracts and procedures into [AI SDK](https://ai-sdk.dev/) tools.
|
|
53
|
-
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): Integrate with [TanStack Query](https://tanstack.com/query/latest).
|
|
54
|
-
- [@orpc/pinia-colada](https://www.npmjs.com/package/@orpc/pinia-colada): Integrate with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
55
|
-
- [@orpc/swr](https://www.npmjs.com/package/@orpc/swr): Integrate with [SWR](https://swr.vercel.app/).
|
|
56
|
-
- [@orpc/experimental-effect](https://www.npmjs.com/package/@orpc/experimental-effect): Integrate with [Effect](https://effect.website/).
|
|
57
|
-
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Implement your contract with [NestJS](https://nestjs.com/).
|
|
58
|
-
- [@orpc/bun](https://www.npmjs.com/package/@orpc/bun): Adapters for [Bun's Redis](https://bun.sh/).
|
|
59
|
-
- [@orpc/cloudflare](https://www.npmjs.com/package/@orpc/cloudflare): Adapters for [Cloudflare's RateLimit and Durable Objects](https://developers.cloudflare.com/workers/).
|
|
60
|
-
- [@orpc/trpc](https://www.npmjs.com/package/@orpc/trpc): Reuse existing [tRPC](https://trpc.io/) routers within oRPC.
|
|
48
|
+
You can find the full documentation [here](https://orpc.dev).
|
|
61
49
|
|
|
62
|
-
|
|
50
|
+
## Packages
|
|
63
51
|
|
|
64
|
-
- [@orpc/
|
|
65
|
-
- [@orpc/
|
|
66
|
-
- [@orpc/
|
|
52
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Build your API contract.
|
|
53
|
+
- [@orpc/server](https://www.npmjs.com/package/@orpc/server): Build your API or implement API contract.
|
|
54
|
+
- [@orpc/client](https://www.npmjs.com/package/@orpc/client): Consume your API on the client with type-safety.
|
|
55
|
+
- [@orpc/openapi](https://www.npmjs.com/package/@orpc/openapi): Generate OpenAPI specs and handle OpenAPI requests.
|
|
56
|
+
- [@orpc/otel](https://www.npmjs.com/package/@orpc/otel): [OpenTelemetry](https://opentelemetry.io/) integration for observability.
|
|
57
|
+
- [@orpc/nest](https://www.npmjs.com/package/@orpc/nest): Deeply integrate oRPC with [NestJS](https://nestjs.com/).
|
|
58
|
+
- [@orpc/react](https://www.npmjs.com/package/@orpc/react): Utilities for integrating oRPC with React and React Server Actions.
|
|
59
|
+
- [@orpc/tanstack-query](https://www.npmjs.com/package/@orpc/tanstack-query): [TanStack Query](https://tanstack.com/query/latest) integration.
|
|
60
|
+
- [@orpc/experimental-react-swr](https://www.npmjs.com/package/@orpc/experimental-react-swr): [SWR](https://swr.vercel.app/) integration.
|
|
61
|
+
- [@orpc/vue-colada](https://www.npmjs.com/package/@orpc/vue-colada): Integration with [Pinia Colada](https://pinia-colada.esm.dev/).
|
|
62
|
+
- [@orpc/hey-api](https://www.npmjs.com/package/@orpc/hey-api): [Hey API](https://heyapi.dev/) integration.
|
|
63
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): More schemas that [Zod](https://zod.dev/) doesn't support yet.
|
|
64
|
+
- [@orpc/valibot](https://www.npmjs.com/package/@orpc/valibot): OpenAPI spec generation from [Valibot](https://valibot.dev/).
|
|
65
|
+
- [@orpc/arktype](https://www.npmjs.com/package/@orpc/arktype): OpenAPI spec generation from [ArkType](https://arktype.io/).
|
|
66
|
+
|
|
67
|
+
## `@orpc/ai-sdk`
|
|
68
|
+
|
|
69
|
+
The [AI SDK](https://ai-sdk.dev/) integration for oRPC.
|
|
67
70
|
|
|
68
71
|
## Sponsors
|
|
69
72
|
|
|
70
|
-
|
|
73
|
+
If you find oRPC valuable and would like to support its development, you can do so here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh).
|
|
71
74
|
|
|
72
75
|
### 🏆 Platinum Sponsor
|
|
73
76
|
|
|
@@ -109,10 +112,11 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
109
112
|
<td align="center"><a href="https://github.com/valerii15298?ref=orpc" target="_blank" rel="noopener" title="Valerii Petryniak"><img src="https://avatars.githubusercontent.com/u/44531564?u=88ac74d9bacd20401518441907acad21063cd397&v=4" width="139" alt="Valerii Petryniak"/><br />Valerii Petryniak</a></td>
|
|
110
113
|
<td align="center"><a href="https://github.com/letstri?ref=orpc" target="_blank" rel="noopener" title="Valerii Strilets"><img src="https://avatars.githubusercontent.com/u/13253748?u=c7b10399ccc8f8081e24db94ec32cd9858e86ac3&v=4" width="139" alt="Valerii Strilets"/><br />Valerii Strilets</a></td>
|
|
111
114
|
<td align="center"><a href="https://github.com/K-Mistele?ref=orpc" target="_blank" rel="noopener" title="Kyle Mistele"><img src="https://avatars.githubusercontent.com/u/18430555?u=3afebeb81de666e35aaac3ed46f14159d7603ffb&v=4" width="139" alt="Kyle Mistele"/><br />Kyle Mistele</a></td>
|
|
115
|
+
<td align="center"><a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&v=4" width="139" alt="Ryan Vogel"/><br />Ryan Vogel</a></td>
|
|
112
116
|
<td align="center"><a href="https://github.com/christ12938?ref=orpc" target="_blank" rel="noopener" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="139" alt="christ12938"/><br />christ12938</a></td>
|
|
113
|
-
<td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
114
117
|
</tr>
|
|
115
118
|
<tr>
|
|
119
|
+
<td align="center"><a href="https://github.com/Ryanjso?ref=orpc" target="_blank" rel="noopener" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="139" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
116
120
|
<td align="center"><a href="https://github.com/itigoore01?ref=orpc" target="_blank" rel="noopener" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="139" alt="shota"/><br />shota</a></td>
|
|
117
121
|
</tr>
|
|
118
122
|
</table>
|
|
@@ -172,7 +176,6 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
172
176
|
<a href="https://github.com/nguyenlc1993?ref=orpc" target="_blank" rel="noopener" title="Lê Cao Nguyên"><img src="https://avatars.githubusercontent.com/u/13871971?u=83c8b69d9e35b589c4e1f066cc113b1d9461386f&v=4" width="32" height="32" alt="Lê Cao Nguyên" /></a>
|
|
173
177
|
<a href="https://github.com/wobsoriano?ref=orpc" target="_blank" rel="noopener" title="Robert Soriano"><img src="https://avatars.githubusercontent.com/u/13049130?u=6d72104182e7c9ed25934815313fb69107332111&v=4" width="32" height="32" alt="Robert Soriano" /></a>
|
|
174
178
|
<a href="https://github.com/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="32" height="32" alt="Andrew Peters" /></a>
|
|
175
|
-
<a href="https://github.com/R44VC0RP?ref=orpc" target="_blank" rel="noopener" title="Ryan Vogel"><img src="https://avatars.githubusercontent.com/u/89211796?u=1857347b9787d8d8a7ea5bfc333f96be92d5a683&v=4" width="32" height="32" alt="Ryan Vogel" /></a>
|
|
176
179
|
<a href="https://github.com/SKostyukovich?ref=orpc" target="_blank" rel="noopener" title="SKostyukovich"><img src="https://avatars.githubusercontent.com/u/10700067?v=4" width="32" height="32" alt="SKostyukovich" /></a>
|
|
177
180
|
<a href="https://github.com/peter-adam-dy?ref=orpc" target="_blank" rel="noopener" title="Peter Adam"><img src="https://avatars.githubusercontent.com/u/132129459?u=4f3dbbb3b443990b56acb7d6a5d11ed2c555f6db&v=4" width="32" height="32" alt="Peter Adam" /></a>
|
|
178
181
|
<a href="https://github.com/FabworksHQ?ref=orpc" target="_blank" rel="noopener" title="Fabworks"><img src="https://avatars.githubusercontent.com/u/160179500?v=4" width="32" height="32" alt="Fabworks" /></a>
|
|
@@ -187,13 +190,6 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
187
190
|
<a href="https://github.com/plancraft?ref=orpc" target="_blank" rel="noopener" title="plancraft"><img src="https://avatars.githubusercontent.com/u/46482287?v=4" width="32" height="32" alt="plancraft" /></a>
|
|
188
191
|
</p>
|
|
189
192
|
|
|
190
|
-
## References
|
|
191
|
-
|
|
192
|
-
oRPC is inspired by existing solutions that prioritize type safety and developer experience. Special acknowledgments to:
|
|
193
|
-
|
|
194
|
-
- [tRPC](https://trpc.io): For pioneering the concept of end-to-end type-safe RPC and influencing the development of type-safe APIs.
|
|
195
|
-
- [ts-rest](https://ts-rest.com): For its emphasis on contract-first development and OpenAPI integration, which have greatly inspired oRPC's feature set.
|
|
196
|
-
|
|
197
193
|
## License
|
|
198
194
|
|
|
199
|
-
Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/
|
|
195
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/1.x/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,92 +1,45 @@
|
|
|
1
1
|
import { ClientOptions } from '@orpc/client';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
5
|
-
|
|
2
|
+
import { Meta, ContractProcedure, Schema, AnySchema, ErrorMap, InferSchemaOutput, InferSchemaInput } from '@orpc/contract';
|
|
3
|
+
import { Context, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
|
4
|
+
import { MaybeOptionalOptions, SetOptional } from '@orpc/shared';
|
|
5
|
+
export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
|
|
6
|
+
import { Tool } from 'ai';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
type FunctionTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = Extract<Tool<INPUT, OUTPUT>, {
|
|
12
|
-
type?: 'function' | undefined;
|
|
13
|
-
}>;
|
|
14
|
-
/**
|
|
15
|
-
* Base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options attached to a procedure/contract,
|
|
16
|
-
* used as defaults when creating tools from it.
|
|
17
|
-
*
|
|
18
|
-
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
21
|
-
*/
|
|
22
|
-
type AiSdkToolMeta<TInputSchema extends AnySchema = AnySchema, TOutputSchema extends AnySchema = AnySchema> = Partial<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema' | 'execute'>>;
|
|
23
|
-
interface AiSdkToolMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
|
|
24
|
-
name: '~ai-sdk/tool';
|
|
8
|
+
declare const AI_SDK_TOOL_META_SYMBOL: unique symbol;
|
|
9
|
+
interface AiSdkToolMeta extends Meta {
|
|
10
|
+
[AI_SDK_TOOL_META_SYMBOL]?: Partial<Tool<unknown, unknown>>;
|
|
25
11
|
}
|
|
26
|
-
|
|
27
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(meta: AiSdkToolMeta<TInputSchema, TOutputSchema>): AiSdkToolMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
|
|
12
|
+
declare class CreateToolError extends Error {
|
|
28
13
|
}
|
|
29
14
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
15
|
+
* Implements [procedure contract](https://orpc.dev/docs/contract-first/define-contract#procedure-contract)
|
|
16
|
+
* as an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing contract definitions.
|
|
34
17
|
*
|
|
35
|
-
* @
|
|
18
|
+
* @warning Requires a contract with an `input` schema defined.
|
|
19
|
+
* @info Standard [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract-first/define-contract).
|
|
36
20
|
*
|
|
37
21
|
* @example
|
|
38
22
|
* ```ts
|
|
39
|
-
* const getWeatherContract = oc
|
|
40
|
-
* .meta(aiSdkTool({
|
|
41
|
-
* description: 'Get the weather in a location',
|
|
42
|
-
* metadata: { source: 'weather-service' },
|
|
43
|
-
* }))
|
|
44
|
-
* .input(z.object({ location: z.string() }))
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
declare const aiSdkTool: AiSdkToolFunction;
|
|
48
|
-
/**
|
|
49
|
-
* Reads the base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options
|
|
50
|
-
* attached to a procedure/contract by {@link aiSdkTool}.
|
|
51
|
-
*
|
|
52
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
53
|
-
*/
|
|
54
|
-
declare function getAiSdkToolMeta(procedureOrLazy: AnyProcedureContract | Lazy<any>): AiSdkToolMeta | undefined;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Infers the value an AI SDK tool outputs for a given oRPC output type.
|
|
58
|
-
*
|
|
59
|
-
* For [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) outputs, the tool streams
|
|
60
|
-
* each yielded event as a [preliminary result](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#preliminary-tool-results)
|
|
61
|
-
* and the last event becomes the final result, so the tool output is the yield type.
|
|
62
|
-
*
|
|
63
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#streaming-tool-outputs AI SDK Streaming Tool Outputs Docs}
|
|
64
|
-
*/
|
|
65
|
-
type ToolOutput<T> = T extends AsyncIteratorObject<infer TYield, any, any> ? TYield : T;
|
|
66
|
-
interface ImplementToolFactoryOptions {
|
|
67
|
-
}
|
|
68
|
-
interface ToolImplementer {
|
|
69
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema>(contract: ProcedureContract<TInputSchema, TOutputSchema, any>, ...rest: MaybeOptionalOptions<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema'>>): Tool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates a builder that implements [procedure contracts](https://orpc.dev/docs/contract/procedure)
|
|
73
|
-
* as [AI SDK Tools](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing contract definitions.
|
|
74
|
-
*
|
|
75
|
-
* The factory accepts oRPC related options, and the resulting builder
|
|
76
|
-
* accepts a contract alongside AI SDK tool options.
|
|
77
|
-
*
|
|
78
|
-
* @info [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract/procedure).
|
|
79
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* import { aiSdkTool, implementToolFactory } from '@orpc/ai-sdk'
|
|
84
23
|
* import { oc } from '@orpc/contract'
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
24
|
+
* import {
|
|
25
|
+
* AI_SDK_TOOL_META_SYMBOL,
|
|
26
|
+
* AiSdkToolMeta,
|
|
27
|
+
* implementTool,
|
|
28
|
+
* } from '@orpc/ai-sdk'
|
|
29
|
+
* import { z } from 'zod'
|
|
30
|
+
*
|
|
31
|
+
* interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
|
|
32
|
+
* const base = oc.$meta<ORPCMeta>({})
|
|
33
|
+
*
|
|
34
|
+
* const getWeatherContract = base
|
|
35
|
+
* .meta({
|
|
36
|
+
* [AI_SDK_TOOL_META_SYMBOL]: {
|
|
37
|
+
* title: 'Get Weather', // AI SDK tool title
|
|
38
|
+
* },
|
|
39
|
+
* })
|
|
40
|
+
* .route({
|
|
41
|
+
* summary: 'Get the weather in a location', // AI SDK tool description
|
|
42
|
+
* })
|
|
90
43
|
* .input(
|
|
91
44
|
* z.object({
|
|
92
45
|
* location: z.string().describe('The location to get the weather for'),
|
|
@@ -99,8 +52,6 @@ interface ToolImplementer {
|
|
|
99
52
|
* }),
|
|
100
53
|
* )
|
|
101
54
|
*
|
|
102
|
-
* const implementTool = implementToolFactory()
|
|
103
|
-
*
|
|
104
55
|
* const getWeatherTool = implementTool(getWeatherContract, {
|
|
105
56
|
* execute: async ({ location }) => ({
|
|
106
57
|
* location,
|
|
@@ -109,29 +60,35 @@ interface ToolImplementer {
|
|
|
109
60
|
* })
|
|
110
61
|
* ```
|
|
111
62
|
*/
|
|
112
|
-
declare function
|
|
113
|
-
type CreateToolFactoryOptions<TInitialContext extends Context> = ImplementToolFactoryOptions & ProcedureClientOptions<TInitialContext, Schema<unknown>, object, never, object> & Omit<ClientOptions<object>, 'context'>;
|
|
114
|
-
interface ToolFactory<TInitialContext extends Context> {
|
|
115
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema>(procedure: Procedure<TInitialContext, any, TInputSchema, TOutputSchema, any, any>, ...rest: MaybeOptionalOptions<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema' | 'execute'>>): Tool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>;
|
|
116
|
-
}
|
|
63
|
+
declare function implementTool<TOutInput, TInOutput>(contract: ContractProcedure<Schema<any, TOutInput>, Schema<TInOutput, any>, any, AiSdkToolMeta>, ...rest: MaybeOptionalOptions<SetOptional<Tool<TOutInput, TInOutput>, 'inputSchema' | 'outputSchema'>>): Tool<TOutInput, TInOutput>;
|
|
117
64
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* The factory accepts oRPC related options, and the resulting builder
|
|
122
|
-
* accepts a procedure alongside AI SDK tool options.
|
|
65
|
+
* Converts a [procedure](https://orpc.dev/docs/procedure) into an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools)
|
|
66
|
+
* by leveraging existing procedure definitions.
|
|
123
67
|
*
|
|
124
|
-
* @
|
|
68
|
+
* @warning Requires a contract with an `input` schema defined.
|
|
125
69
|
*
|
|
126
70
|
* @example
|
|
127
71
|
* ```ts
|
|
128
|
-
* import { aiSdkTool, createToolFactory } from '@orpc/ai-sdk'
|
|
129
72
|
* import { os } from '@orpc/server'
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
73
|
+
* import {
|
|
74
|
+
* AI_SDK_TOOL_META_SYMBOL,
|
|
75
|
+
* AiSdkToolMeta,
|
|
76
|
+
* createTool
|
|
77
|
+
* } from '@orpc/ai-sdk'
|
|
78
|
+
* import { z } from 'zod'
|
|
79
|
+
*
|
|
80
|
+
* interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
|
|
81
|
+
* const base = os.$meta<ORPCMeta>({})
|
|
82
|
+
*
|
|
83
|
+
* const getWeatherProcedure = base
|
|
84
|
+
* .meta({
|
|
85
|
+
* [AI_SDK_TOOL_META_SYMBOL]: {
|
|
86
|
+
* title: 'Get Weather', // AI SDK tool title
|
|
87
|
+
* },
|
|
88
|
+
* })
|
|
89
|
+
* .route({
|
|
90
|
+
* summary: 'Get the weather in a location',
|
|
91
|
+
* })
|
|
135
92
|
* .input(z.object({
|
|
136
93
|
* location: z.string().describe('The location to get the weather for'),
|
|
137
94
|
* }))
|
|
@@ -144,16 +101,12 @@ interface ToolFactory<TInitialContext extends Context> {
|
|
|
144
101
|
* temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
145
102
|
* }))
|
|
146
103
|
*
|
|
147
|
-
* const createTool = createToolFactory({
|
|
148
|
-
* context: {}, // provide initial context if needed
|
|
149
|
-
* })
|
|
150
|
-
*
|
|
151
104
|
* const getWeatherTool = createTool(getWeatherProcedure, {
|
|
152
|
-
*
|
|
105
|
+
* context: {}, // provide initial context if needed
|
|
153
106
|
* })
|
|
154
107
|
* ```
|
|
155
108
|
*/
|
|
156
|
-
declare function
|
|
109
|
+
declare function createTool<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends AiSdkToolMeta>(procedure: Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>, ...rest: MaybeOptionalOptions<SetOptional<Tool<InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>>, 'inputSchema' | 'outputSchema' | 'execute'> & CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, Record<never, never>> & Omit<ClientOptions<Record<never, never>>, 'context'>>): Tool<InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>>;
|
|
157
110
|
|
|
158
|
-
export {
|
|
159
|
-
export type {
|
|
111
|
+
export { AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool, implementTool };
|
|
112
|
+
export type { AiSdkToolMeta };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,92 +1,45 @@
|
|
|
1
1
|
import { ClientOptions } from '@orpc/client';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
5
|
-
|
|
2
|
+
import { Meta, ContractProcedure, Schema, AnySchema, ErrorMap, InferSchemaOutput, InferSchemaInput } from '@orpc/contract';
|
|
3
|
+
import { Context, Procedure, CreateProcedureClientOptions } from '@orpc/server';
|
|
4
|
+
import { MaybeOptionalOptions, SetOptional } from '@orpc/shared';
|
|
5
|
+
export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
|
|
6
|
+
import { Tool } from 'ai';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
type FunctionTool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONValue | unknown | never = any> = Extract<Tool<INPUT, OUTPUT>, {
|
|
12
|
-
type?: 'function' | undefined;
|
|
13
|
-
}>;
|
|
14
|
-
/**
|
|
15
|
-
* Base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options attached to a procedure/contract,
|
|
16
|
-
* used as defaults when creating tools from it.
|
|
17
|
-
*
|
|
18
|
-
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
19
|
-
*
|
|
20
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
21
|
-
*/
|
|
22
|
-
type AiSdkToolMeta<TInputSchema extends AnySchema = AnySchema, TOutputSchema extends AnySchema = AnySchema> = Partial<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema' | 'execute'>>;
|
|
23
|
-
interface AiSdkToolMetaPlugin<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends MetaPlugin<TInputSchema, TOutputSchema, TErrorMap> {
|
|
24
|
-
name: '~ai-sdk/tool';
|
|
8
|
+
declare const AI_SDK_TOOL_META_SYMBOL: unique symbol;
|
|
9
|
+
interface AiSdkToolMeta extends Meta {
|
|
10
|
+
[AI_SDK_TOOL_META_SYMBOL]?: Partial<Tool<unknown, unknown>>;
|
|
25
11
|
}
|
|
26
|
-
|
|
27
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(meta: AiSdkToolMeta<TInputSchema, TOutputSchema>): AiSdkToolMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
|
|
12
|
+
declare class CreateToolError extends Error {
|
|
28
13
|
}
|
|
29
14
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
15
|
+
* Implements [procedure contract](https://orpc.dev/docs/contract-first/define-contract#procedure-contract)
|
|
16
|
+
* as an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing contract definitions.
|
|
34
17
|
*
|
|
35
|
-
* @
|
|
18
|
+
* @warning Requires a contract with an `input` schema defined.
|
|
19
|
+
* @info Standard [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract-first/define-contract).
|
|
36
20
|
*
|
|
37
21
|
* @example
|
|
38
22
|
* ```ts
|
|
39
|
-
* const getWeatherContract = oc
|
|
40
|
-
* .meta(aiSdkTool({
|
|
41
|
-
* description: 'Get the weather in a location',
|
|
42
|
-
* metadata: { source: 'weather-service' },
|
|
43
|
-
* }))
|
|
44
|
-
* .input(z.object({ location: z.string() }))
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
declare const aiSdkTool: AiSdkToolFunction;
|
|
48
|
-
/**
|
|
49
|
-
* Reads the base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options
|
|
50
|
-
* attached to a procedure/contract by {@link aiSdkTool}.
|
|
51
|
-
*
|
|
52
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
53
|
-
*/
|
|
54
|
-
declare function getAiSdkToolMeta(procedureOrLazy: AnyProcedureContract | Lazy<any>): AiSdkToolMeta | undefined;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Infers the value an AI SDK tool outputs for a given oRPC output type.
|
|
58
|
-
*
|
|
59
|
-
* For [AsyncIteratorObject](https://orpc.dev/docs/async-iterator-object) outputs, the tool streams
|
|
60
|
-
* each yielded event as a [preliminary result](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#preliminary-tool-results)
|
|
61
|
-
* and the last event becomes the final result, so the tool output is the yield type.
|
|
62
|
-
*
|
|
63
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#streaming-tool-outputs AI SDK Streaming Tool Outputs Docs}
|
|
64
|
-
*/
|
|
65
|
-
type ToolOutput<T> = T extends AsyncIteratorObject<infer TYield, any, any> ? TYield : T;
|
|
66
|
-
interface ImplementToolFactoryOptions {
|
|
67
|
-
}
|
|
68
|
-
interface ToolImplementer {
|
|
69
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema>(contract: ProcedureContract<TInputSchema, TOutputSchema, any>, ...rest: MaybeOptionalOptions<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema'>>): Tool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Creates a builder that implements [procedure contracts](https://orpc.dev/docs/contract/procedure)
|
|
73
|
-
* as [AI SDK Tools](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing contract definitions.
|
|
74
|
-
*
|
|
75
|
-
* The factory accepts oRPC related options, and the resulting builder
|
|
76
|
-
* accepts a contract alongside AI SDK tool options.
|
|
77
|
-
*
|
|
78
|
-
* @info [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract/procedure).
|
|
79
|
-
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
80
|
-
*
|
|
81
|
-
* @example
|
|
82
|
-
* ```ts
|
|
83
|
-
* import { aiSdkTool, implementToolFactory } from '@orpc/ai-sdk'
|
|
84
23
|
* import { oc } from '@orpc/contract'
|
|
85
|
-
*
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
24
|
+
* import {
|
|
25
|
+
* AI_SDK_TOOL_META_SYMBOL,
|
|
26
|
+
* AiSdkToolMeta,
|
|
27
|
+
* implementTool,
|
|
28
|
+
* } from '@orpc/ai-sdk'
|
|
29
|
+
* import { z } from 'zod'
|
|
30
|
+
*
|
|
31
|
+
* interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
|
|
32
|
+
* const base = oc.$meta<ORPCMeta>({})
|
|
33
|
+
*
|
|
34
|
+
* const getWeatherContract = base
|
|
35
|
+
* .meta({
|
|
36
|
+
* [AI_SDK_TOOL_META_SYMBOL]: {
|
|
37
|
+
* title: 'Get Weather', // AI SDK tool title
|
|
38
|
+
* },
|
|
39
|
+
* })
|
|
40
|
+
* .route({
|
|
41
|
+
* summary: 'Get the weather in a location', // AI SDK tool description
|
|
42
|
+
* })
|
|
90
43
|
* .input(
|
|
91
44
|
* z.object({
|
|
92
45
|
* location: z.string().describe('The location to get the weather for'),
|
|
@@ -99,8 +52,6 @@ interface ToolImplementer {
|
|
|
99
52
|
* }),
|
|
100
53
|
* )
|
|
101
54
|
*
|
|
102
|
-
* const implementTool = implementToolFactory()
|
|
103
|
-
*
|
|
104
55
|
* const getWeatherTool = implementTool(getWeatherContract, {
|
|
105
56
|
* execute: async ({ location }) => ({
|
|
106
57
|
* location,
|
|
@@ -109,29 +60,35 @@ interface ToolImplementer {
|
|
|
109
60
|
* })
|
|
110
61
|
* ```
|
|
111
62
|
*/
|
|
112
|
-
declare function
|
|
113
|
-
type CreateToolFactoryOptions<TInitialContext extends Context> = ImplementToolFactoryOptions & ProcedureClientOptions<TInitialContext, Schema<unknown>, object, never, object> & Omit<ClientOptions<object>, 'context'>;
|
|
114
|
-
interface ToolFactory<TInitialContext extends Context> {
|
|
115
|
-
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema>(procedure: Procedure<TInitialContext, any, TInputSchema, TOutputSchema, any, any>, ...rest: MaybeOptionalOptions<Omit<FunctionTool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>, 'inputSchema' | 'outputSchema' | 'execute'>>): Tool<InferSchemaOutput<TInputSchema>, ToolOutput<InferSchemaInput<TOutputSchema>>>;
|
|
116
|
-
}
|
|
63
|
+
declare function implementTool<TOutInput, TInOutput>(contract: ContractProcedure<Schema<any, TOutInput>, Schema<TInOutput, any>, any, AiSdkToolMeta>, ...rest: MaybeOptionalOptions<SetOptional<Tool<TOutInput, TInOutput>, 'inputSchema' | 'outputSchema'>>): Tool<TOutInput, TInOutput>;
|
|
117
64
|
/**
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* The factory accepts oRPC related options, and the resulting builder
|
|
122
|
-
* accepts a procedure alongside AI SDK tool options.
|
|
65
|
+
* Converts a [procedure](https://orpc.dev/docs/procedure) into an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools)
|
|
66
|
+
* by leveraging existing procedure definitions.
|
|
123
67
|
*
|
|
124
|
-
* @
|
|
68
|
+
* @warning Requires a contract with an `input` schema defined.
|
|
125
69
|
*
|
|
126
70
|
* @example
|
|
127
71
|
* ```ts
|
|
128
|
-
* import { aiSdkTool, createToolFactory } from '@orpc/ai-sdk'
|
|
129
72
|
* import { os } from '@orpc/server'
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
73
|
+
* import {
|
|
74
|
+
* AI_SDK_TOOL_META_SYMBOL,
|
|
75
|
+
* AiSdkToolMeta,
|
|
76
|
+
* createTool
|
|
77
|
+
* } from '@orpc/ai-sdk'
|
|
78
|
+
* import { z } from 'zod'
|
|
79
|
+
*
|
|
80
|
+
* interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
|
|
81
|
+
* const base = os.$meta<ORPCMeta>({})
|
|
82
|
+
*
|
|
83
|
+
* const getWeatherProcedure = base
|
|
84
|
+
* .meta({
|
|
85
|
+
* [AI_SDK_TOOL_META_SYMBOL]: {
|
|
86
|
+
* title: 'Get Weather', // AI SDK tool title
|
|
87
|
+
* },
|
|
88
|
+
* })
|
|
89
|
+
* .route({
|
|
90
|
+
* summary: 'Get the weather in a location',
|
|
91
|
+
* })
|
|
135
92
|
* .input(z.object({
|
|
136
93
|
* location: z.string().describe('The location to get the weather for'),
|
|
137
94
|
* }))
|
|
@@ -144,16 +101,12 @@ interface ToolFactory<TInitialContext extends Context> {
|
|
|
144
101
|
* temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
145
102
|
* }))
|
|
146
103
|
*
|
|
147
|
-
* const createTool = createToolFactory({
|
|
148
|
-
* context: {}, // provide initial context if needed
|
|
149
|
-
* })
|
|
150
|
-
*
|
|
151
104
|
* const getWeatherTool = createTool(getWeatherProcedure, {
|
|
152
|
-
*
|
|
105
|
+
* context: {}, // provide initial context if needed
|
|
153
106
|
* })
|
|
154
107
|
* ```
|
|
155
108
|
*/
|
|
156
|
-
declare function
|
|
109
|
+
declare function createTool<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends AiSdkToolMeta>(procedure: Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>, ...rest: MaybeOptionalOptions<SetOptional<Tool<InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>>, 'inputSchema' | 'outputSchema' | 'execute'> & CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, Record<never, never>> & Omit<ClientOptions<Record<never, never>>, 'context'>>): Tool<InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>>;
|
|
157
110
|
|
|
158
|
-
export {
|
|
159
|
-
export type {
|
|
111
|
+
export { AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool, implementTool };
|
|
112
|
+
export type { AiSdkToolMeta };
|
package/dist/index.mjs
CHANGED
|
@@ -1,142 +1,43 @@
|
|
|
1
|
-
import { getAsyncIteratorObjectSchemaDetails } from '@orpc/contract';
|
|
2
|
-
import { combineJsonSchemasWithComposition } from '@orpc/json-schema';
|
|
3
1
|
import { Procedure, call } from '@orpc/server';
|
|
4
|
-
import { resolveMaybeOptionalOptions
|
|
2
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
+
export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
|
|
5
4
|
import { tool } from 'ai';
|
|
6
5
|
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
init(meta) {
|
|
10
|
-
const existing = meta["~ai-sdk/tool"];
|
|
11
|
-
return {
|
|
12
|
-
...meta,
|
|
13
|
-
"~ai-sdk/tool": { ...existing, ...incoming }
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
function getAiSdkToolMeta(procedureOrLazy) {
|
|
18
|
-
return procedureOrLazy["~orpc"].meta["~ai-sdk/tool"];
|
|
6
|
+
const AI_SDK_TOOL_META_SYMBOL = Symbol("ORPC_AI_SDK_TOOL_META");
|
|
7
|
+
class CreateToolError extends Error {
|
|
19
8
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
vendor: ORPC_NAME,
|
|
24
|
-
version: 1,
|
|
25
|
-
validate: (value) => ({ value }),
|
|
26
|
-
jsonSchema: {
|
|
27
|
-
input: () => ({}),
|
|
28
|
-
output: () => ({})
|
|
29
|
-
}
|
|
9
|
+
function implementTool(contract, ...rest) {
|
|
10
|
+
if (contract["~orpc"].inputSchema === void 0) {
|
|
11
|
+
throw new CreateToolError("Cannot implement tool from a contract procedure without input schema.");
|
|
30
12
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return rest;
|
|
39
|
-
}
|
|
40
|
-
return jsonSchema;
|
|
13
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
14
|
+
return tool({
|
|
15
|
+
inputSchema: contract["~orpc"].inputSchema,
|
|
16
|
+
outputSchema: contract["~orpc"].outputSchema,
|
|
17
|
+
description: contract["~orpc"].route.summary ?? contract["~orpc"].route.description,
|
|
18
|
+
...contract["~orpc"].meta[AI_SDK_TOOL_META_SYMBOL],
|
|
19
|
+
...options
|
|
41
20
|
});
|
|
42
|
-
const combined = combineJsonSchemasWithComposition("allOf", branches);
|
|
43
|
-
return { $schema, ...combined };
|
|
44
|
-
}
|
|
45
|
-
function combineSchemas(schemas) {
|
|
46
|
-
if (schemas.length === 0) {
|
|
47
|
-
return void 0;
|
|
48
|
-
}
|
|
49
|
-
if (schemas.length === 1) {
|
|
50
|
-
return schemas[0];
|
|
51
|
-
}
|
|
52
|
-
const standardJsonSchemas = schemas.filter((schema) => schema["~standard"].jsonSchema);
|
|
53
|
-
const jsonSchema = standardJsonSchemas.length ? {
|
|
54
|
-
input: (options) => combineJsonSchemas(standardJsonSchemas.map((schema) => schema["~standard"].jsonSchema.input(options))),
|
|
55
|
-
output: (options) => combineJsonSchemas(standardJsonSchemas.map((schema) => schema["~standard"].jsonSchema.output(options)))
|
|
56
|
-
} : void 0;
|
|
57
|
-
return {
|
|
58
|
-
"~standard": {
|
|
59
|
-
vendor: ORPC_NAME,
|
|
60
|
-
version: 1,
|
|
61
|
-
async validate(value) {
|
|
62
|
-
let current = value;
|
|
63
|
-
for (const schema of schemas) {
|
|
64
|
-
const result = await schema["~standard"].validate(current);
|
|
65
|
-
if (result.issues) {
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
current = result.value;
|
|
69
|
-
}
|
|
70
|
-
return { value: current };
|
|
71
|
-
},
|
|
72
|
-
jsonSchema
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
21
|
}
|
|
76
|
-
function
|
|
77
|
-
if (schemas.length === 0) {
|
|
78
|
-
return void 0;
|
|
79
|
-
}
|
|
80
|
-
const yieldSchemas = [];
|
|
81
|
-
for (const schema of schemas) {
|
|
82
|
-
const details = getAsyncIteratorObjectSchemaDetails(schema);
|
|
83
|
-
if (!details) {
|
|
84
|
-
return void 0;
|
|
85
|
-
}
|
|
86
|
-
yieldSchemas.push(details.yieldSchema);
|
|
87
|
-
}
|
|
88
|
-
return yieldSchemas;
|
|
89
|
-
}
|
|
90
|
-
function combineOutputSchemas(outputSchemas) {
|
|
91
|
-
const yieldSchemas = getIteratorYieldSchemas(outputSchemas);
|
|
92
|
-
return combineSchemas([...yieldSchemas ?? outputSchemas].reverse());
|
|
93
|
-
}
|
|
94
|
-
function implementToolFactory(_options = {}) {
|
|
95
|
-
const factory = (contract, ...rest) => {
|
|
96
|
-
const toolOptions = resolveMaybeOptionalOptions(rest);
|
|
97
|
-
const inputSchemas = toArray(contract["~orpc"].inputSchemas);
|
|
98
|
-
const outputSchemas = toArray(contract["~orpc"].outputSchemas);
|
|
99
|
-
const defaults = getAiSdkToolMeta(contract);
|
|
100
|
-
return tool({
|
|
101
|
-
...defaults,
|
|
102
|
-
...toolOptions,
|
|
103
|
-
inputSchema: combineSchemas(inputSchemas) ?? ANY_SCHEMA,
|
|
104
|
-
outputSchema: combineOutputSchemas(outputSchemas)
|
|
105
|
-
});
|
|
106
|
-
};
|
|
107
|
-
return factory;
|
|
108
|
-
}
|
|
109
|
-
function createToolFactory(...rest) {
|
|
22
|
+
function createTool(procedure, ...rest) {
|
|
110
23
|
const options = resolveMaybeOptionalOptions(rest);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const output = await call(disabledValidation, input, { signal: callingOptions.abortSignal, ...options });
|
|
129
|
-
if (!isAsyncIteratorObject(output)) {
|
|
130
|
-
yield output;
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
yield* output;
|
|
134
|
-
} : (input, callingOptions) => {
|
|
135
|
-
return call(disabledValidation, input, { signal: callingOptions.abortSignal, ...options });
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
};
|
|
139
|
-
return factory;
|
|
24
|
+
return implementTool(procedure, {
|
|
25
|
+
execute: ((input, callingOptions) => {
|
|
26
|
+
const disabledValidation = new Procedure({
|
|
27
|
+
...procedure["~orpc"],
|
|
28
|
+
inputValidationIndex: Number.NaN,
|
|
29
|
+
// disable input validation
|
|
30
|
+
outputValidationIndex: Number.NaN
|
|
31
|
+
// disable output validation
|
|
32
|
+
});
|
|
33
|
+
return call(
|
|
34
|
+
disabledValidation,
|
|
35
|
+
input,
|
|
36
|
+
{ signal: callingOptions.abortSignal, ...options }
|
|
37
|
+
);
|
|
38
|
+
}),
|
|
39
|
+
...options
|
|
40
|
+
});
|
|
140
41
|
}
|
|
141
42
|
|
|
142
|
-
export {
|
|
43
|
+
export { AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool, implementTool };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/ai-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.14.
|
|
4
|
+
"version": "1.14.13",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -10,12 +10,11 @@
|
|
|
10
10
|
"directory": "packages/ai-sdk"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
|
-
"
|
|
14
|
-
"
|
|
13
|
+
"ai-sdk",
|
|
14
|
+
"orpc"
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"exports": {
|
|
18
|
-
"./package.json": "./package.json",
|
|
19
18
|
".": {
|
|
20
19
|
"types": "./dist/index.d.mts",
|
|
21
20
|
"import": "./dist/index.mjs",
|
|
@@ -26,21 +25,21 @@
|
|
|
26
25
|
"dist"
|
|
27
26
|
],
|
|
28
27
|
"peerDependencies": {
|
|
29
|
-
"ai": ">=
|
|
28
|
+
"ai": ">=5.0.76"
|
|
30
29
|
},
|
|
31
30
|
"dependencies": {
|
|
32
|
-
"@orpc/client": "1.14.
|
|
33
|
-
"@orpc/
|
|
34
|
-
"@orpc/shared": "1.14.
|
|
35
|
-
"@orpc/server": "1.14.
|
|
36
|
-
"@orpc/contract": "1.14.11"
|
|
31
|
+
"@orpc/client": "1.14.13",
|
|
32
|
+
"@orpc/contract": "1.14.13",
|
|
33
|
+
"@orpc/shared": "1.14.13",
|
|
34
|
+
"@orpc/server": "1.14.13"
|
|
37
35
|
},
|
|
38
36
|
"devDependencies": {
|
|
39
|
-
"ai": "^
|
|
40
|
-
"zod": "^4.
|
|
37
|
+
"ai": "^6.0.116",
|
|
38
|
+
"zod": "^4.3.6"
|
|
41
39
|
},
|
|
42
40
|
"scripts": {
|
|
43
41
|
"build": "unbuild",
|
|
42
|
+
"build:watch": "pnpm run build --watch",
|
|
44
43
|
"type:check": "tsc -b"
|
|
45
44
|
}
|
|
46
45
|
}
|