@orpc/ai-sdk 1.14.8 → 2.0.0-beta.20
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 +58 -53
- package/dist/index.d.mts +106 -59
- package/dist/index.d.ts +106 -59
- package/dist/index.mjs +132 -33
- package/package.json +12 -11
package/README.md
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
<
|
|
2
|
-
<image align="center" src="https://orpc.dev/logo.webp" width=280 alt="oRPC logo" />
|
|
3
|
-
</div>
|
|
4
|
-
|
|
5
|
-
<h1></h1>
|
|
1
|
+
<h1 align="center">oRPC - Typesafe APIs Made Simple 🪄</h1>
|
|
6
2
|
|
|
7
3
|
<div align="center">
|
|
8
4
|
<a href="https://codecov.io/gh/middleapi/orpc">
|
|
9
|
-
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/
|
|
5
|
+
<img alt="codecov" src="https://codecov.io/gh/middleapi/orpc/branch/main/graph/badge.svg">
|
|
10
6
|
</a>
|
|
11
7
|
<a href="https://www.npmjs.com/package/@orpc/ai-sdk">
|
|
12
8
|
<img alt="weekly downloads" src="https://img.shields.io/npm/dw/%40orpc%2Fai-sdk?logo=npm" />
|
|
13
9
|
</a>
|
|
14
|
-
<a href="https://
|
|
10
|
+
<a href="https://app.codspeed.io/middleapi/orpc?utm_source=badge">
|
|
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">
|
|
15
14
|
<img alt="MIT License" src="https://img.shields.io/github/license/middleapi/orpc?logo=open-source-initiative" />
|
|
16
15
|
</a>
|
|
17
16
|
<a href="https://discord.gg/TXEbwRBvQn">
|
|
@@ -22,55 +21,53 @@
|
|
|
22
21
|
</a>
|
|
23
22
|
</div>
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
## Documentation
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
You can read the documentation [here](https://orpc.dev).
|
|
28
27
|
|
|
29
|
-
|
|
28
|
+
## Packages
|
|
30
29
|
|
|
31
|
-
|
|
30
|
+
**Core**
|
|
32
31
|
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
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.
|
|
32
|
+
- [@orpc/contract](https://www.npmjs.com/package/@orpc/contract): Define API contract as the single source of truth.
|
|
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.
|
|
45
36
|
|
|
46
|
-
|
|
37
|
+
**Schema validation**
|
|
47
38
|
|
|
48
|
-
|
|
39
|
+
- [@orpc/zod](https://www.npmjs.com/package/@orpc/zod): Integrate with [Zod](https://zod.dev/).
|
|
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/).
|
|
49
42
|
|
|
50
|
-
|
|
43
|
+
**Built-in features**
|
|
44
|
+
|
|
45
|
+
- [@orpc/publisher](https://www.npmjs.com/package/@orpc/publisher): Pub/Sub with memory, Redis, and Upstash adapters.
|
|
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**
|
|
50
|
+
|
|
51
|
+
- [@orpc/next](https://www.npmjs.com/package/@orpc/next): Integrate with [Next.js Server Functions](https://nextjs.org/docs/app/getting-started/mutating-data).
|
|
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.
|
|
51
61
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
- [@orpc/
|
|
55
|
-
- [@orpc/
|
|
56
|
-
- [@orpc/
|
|
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.
|
|
62
|
+
**Observability**
|
|
63
|
+
|
|
64
|
+
- [@orpc/opentelemetry](https://www.npmjs.com/package/@orpc/opentelemetry): Integrate with [OpenTelemetry](https://opentelemetry.io/) for distributed tracing.
|
|
65
|
+
- [@orpc/pino](https://www.npmjs.com/package/@orpc/pino): Integrate with [Pino](https://getpino.io/) for logging.
|
|
66
|
+
- [@orpc/evlog](https://www.npmjs.com/package/@orpc/evlog): Integrate with [Evlog](https://evlog.dev/) for logging.
|
|
70
67
|
|
|
71
68
|
## Sponsors
|
|
72
69
|
|
|
73
|
-
|
|
70
|
+
Like what we build over at [middleapi](https://github.com/middleapi)? You can help keep it going here: [GitHub Sponsors](https://github.com/sponsors/dinwwwh). Every bit helps! 🚀
|
|
74
71
|
|
|
75
72
|
### 🏆 Platinum Sponsor
|
|
76
73
|
|
|
@@ -112,12 +109,10 @@ If you find oRPC valuable and would like to support its development, you can do
|
|
|
112
109
|
<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>
|
|
113
110
|
<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>
|
|
114
111
|
<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/andrewpeters9?ref=orpc" target="_blank" rel="noopener" title="Andrew Peters"><img src="https://avatars.githubusercontent.com/u/36251325?v=4" width="139" alt="Andrew Peters"/><br />Andrew Peters</a></td>
|
|
116
112
|
<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>
|
|
113
|
+
<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>
|
|
117
114
|
</tr>
|
|
118
115
|
<tr>
|
|
119
|
-
<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>
|
|
120
|
-
<td align="center"><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="139" alt="Peter Adam"/><br />Peter Adam</a></td>
|
|
121
116
|
<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>
|
|
122
117
|
<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>
|
|
123
118
|
</tr>
|
|
@@ -128,25 +123,25 @@ If you find oRPC valuable and would like to support its development, you can do
|
|
|
128
123
|
<table>
|
|
129
124
|
<tr>
|
|
130
125
|
<td align="center"><a href="https://github.com/rhinodavid?ref=orpc" target="_blank" rel="noopener" title="David Walsh"><img src="https://avatars.githubusercontent.com/u/5778036?u=b5521f07d2f88c3db2a0dae62b5f2f8357214af0&v=4" width="119" alt="David Walsh"/><br />David Walsh</a></td>
|
|
126
|
+
<td align="center"><a href="https://github.com/Nic13Gamer?ref=orpc" target="_blank" rel="noopener" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="119" alt="Nicholas"/><br />Nicholas</a></td>
|
|
131
127
|
<td align="center"><a href="https://github.com/Robbe95?ref=orpc" target="_blank" rel="noopener" title="Robbe Vaes"><img src="https://avatars.githubusercontent.com/u/44748019?u=e0232402c045ad4eac7cbd217f1f47e083103b89&v=4" width="119" alt="Robbe Vaes"/><br />Robbe Vaes</a></td>
|
|
132
128
|
<td align="center"><a href="https://github.com/aidansunbury?ref=orpc" target="_blank" rel="noopener" title="Aidan Sunbury"><img src="https://avatars.githubusercontent.com/u/64103161?v=4" width="119" alt="Aidan Sunbury"/><br />Aidan Sunbury</a></td>
|
|
133
129
|
<td align="center"><a href="https://github.com/soonoo?ref=orpc" target="_blank" rel="noopener" title="soonoo"><img src="https://avatars.githubusercontent.com/u/5436405?u=5d0b4aa955c87e30e6bda7f0cccae5402da99528&v=4" width="119" alt="soonoo"/><br />soonoo</a></td>
|
|
134
130
|
<td align="center"><a href="https://github.com/kporten?ref=orpc" target="_blank" rel="noopener" title="Kevin Porten"><img src="https://avatars.githubusercontent.com/u/1839345?u=dc2263d5cfe0d927ce1a0be04a1d55dd6b55405c&v=4" width="119" alt="Kevin Porten"/><br />Kevin Porten</a></td>
|
|
135
131
|
<td align="center"><a href="https://github.com/pumpkinlink?ref=orpc" target="_blank" rel="noopener" title="Denis"><img src="https://avatars.githubusercontent.com/u/11864620?u=5f47bbe6c65d0f6f5cf011021490238e4b0593d0&v=4" width="119" alt="Denis"/><br />Denis</a></td>
|
|
136
|
-
<td align="center"><a href="https://github.com/christopher-kapic?ref=orpc" target="_blank" rel="noopener" title="Christopher Kapic"><img src="https://avatars.githubusercontent.com/u/59740769?u=e7ad4b72b5bf6c9eb1644c26dbf3332a8f987377&v=4" width="119" alt="Christopher Kapic"/><br />Christopher Kapic</a></td>
|
|
137
132
|
</tr>
|
|
138
133
|
<tr>
|
|
134
|
+
<td align="center"><a href="https://github.com/christopher-kapic?ref=orpc" target="_blank" rel="noopener" title="Christopher Kapic"><img src="https://avatars.githubusercontent.com/u/59740769?u=e7ad4b72b5bf6c9eb1644c26dbf3332a8f987377&v=4" width="119" alt="Christopher Kapic"/><br />Christopher Kapic</a></td>
|
|
139
135
|
<td align="center"><a href="https://github.com/thomasballinger?ref=orpc" target="_blank" rel="noopener" title="Tom Ballinger"><img src="https://avatars.githubusercontent.com/u/458879?u=4b045ac75d721b6ac2b42a74d7d37f61f0414031&v=4" width="119" alt="Tom Ballinger"/><br />Tom Ballinger</a></td>
|
|
140
136
|
<td align="center"><a href="https://github.com/SSam0419?ref=orpc" target="_blank" rel="noopener" title="Sam"><img src="https://avatars.githubusercontent.com/u/102863520?u=3c89611f549d5070be232eb4532f690c8f2e7a65&v=4" width="119" alt="Sam"/><br />Sam</a></td>
|
|
141
137
|
<td align="center"><a href="https://github.com/Titoine?ref=orpc" target="_blank" rel="noopener" title="Titoine"><img src="https://avatars.githubusercontent.com/u/3514286?u=1bb1e86b0c99c8a1121372e56d51a177eea12191&v=4" width="119" alt="Titoine"/><br />Titoine</a></td>
|
|
142
138
|
<td align="center"><a href="https://github.com/Mnigos?ref=orpc" target="_blank" rel="noopener" title="Igor Makowski"><img src="https://avatars.githubusercontent.com/u/56691628?u=ee8c879478f7c151b9156aef6c74243fa3e247a8&v=4" width="119" alt="Igor Makowski"/><br />Igor Makowski</a></td>
|
|
143
139
|
<td align="center"><a href="https://github.com/hanayashiki?ref=orpc" target="_blank" rel="noopener" title="hanayashiki"><img src="https://avatars.githubusercontent.com/u/26056783?u=06c3b9205a16fd41a871e82da1cc2a09306d53f5&v=4" width="119" alt="hanayashiki"/><br />hanayashiki</a></td>
|
|
144
140
|
<td align="center"><a href="https://github.com/ldub?ref=orpc" target="_blank" rel="noopener" title="Lev Dubinets"><img src="https://avatars.githubusercontent.com/u/3114081?u=f547f5d5012cab54851f1b1ad72d10e537f78fc2&v=4" width="119" alt="Lev Dubinets"/><br />Lev Dubinets</a></td>
|
|
145
|
-
<td align="center"><a href="https://github.com/mr-kelly?ref=orpc" target="_blank" rel="noopener" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
146
141
|
</tr>
|
|
147
142
|
<tr>
|
|
143
|
+
<td align="center"><a href="https://github.com/mr-kelly?ref=orpc" target="_blank" rel="noopener" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="119" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
148
144
|
<td align="center"><a href="https://github.com/piscis?ref=orpc" target="_blank" rel="noopener" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="119" alt="Alex"/><br />Alex</a></td>
|
|
149
|
-
<td align="center"><a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="nattstack"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="119" alt="nattstack"/><br />nattstack</a></td>
|
|
150
145
|
<td align="center"><a href="https://github.com/finom?ref=orpc" target="_blank" rel="noopener" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=29e91400dbd4a9c217048a8f59562c4f740498e6&v=4" width="119" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
151
146
|
</tr>
|
|
152
147
|
</table>
|
|
@@ -163,7 +158,7 @@ If you find oRPC valuable and would like to support its development, you can do
|
|
|
163
158
|
<a href="https://github.com/theoludwig?ref=orpc" target="_blank" rel="noopener" title="Théo LUDWIG"><img src="https://avatars.githubusercontent.com/u/25207499?u=a6a9653725a2f574c07893748806668e0598cdbe&v=4" width="32" height="32" alt="Théo LUDWIG" /></a>
|
|
164
159
|
<a href="https://github.com/abhay-ramesh?ref=orpc" target="_blank" rel="noopener" title="Abhay Ramesh"><img src="https://avatars.githubusercontent.com/u/66196314?u=c5c2b0327b26606c2efcfaf17046ab18c3d25c57&v=4" width="32" height="32" alt="Abhay Ramesh" /></a>
|
|
165
160
|
<a href="https://github.com/shr-ink?ref=orpc" target="_blank" rel="noopener" title="shr.ink oü"><img src="https://avatars.githubusercontent.com/u/139700438?v=4" width="32" height="32" alt="shr.ink oü" /></a>
|
|
166
|
-
<a href="https://github.com/johngerome?ref=orpc" target="_blank" rel="noopener" title="0x4e32"><img src="https://avatars.githubusercontent.com/u/2002000?u=
|
|
161
|
+
<a href="https://github.com/johngerome?ref=orpc" target="_blank" rel="noopener" title="0x4e32"><img src="https://avatars.githubusercontent.com/u/2002000?u=505e54608466ab53754f702973687b04c6424c1f&v=4" width="32" height="32" alt="0x4e32" /></a>
|
|
167
162
|
<a href="https://github.com/yzuyr?ref=orpc" target="_blank" rel="noopener" title="Ryuz"><img src="https://avatars.githubusercontent.com/u/196539378?u=d38374588d219b6748b16406982f6559411466d4&v=4" width="32" height="32" alt="Ryuz" /></a>
|
|
168
163
|
<a href="https://github.com/happyboy2022?ref=orpc" target="_blank" rel="noopener" title="happyboy"><img src="https://avatars.githubusercontent.com/u/103669586?u=65b49c4b893ed3703909fbb3a7a22313f3f9c121&v=4" width="32" height="32" alt="happyboy" /></a>
|
|
169
164
|
<a href="https://github.com/YiCChi?ref=orpc" target="_blank" rel="noopener" title="yicchi"><img src="https://avatars.githubusercontent.com/u/86967274?u=6c2756f09fe15dd94d572f560e979cd157982852&v=4" width="32" height="32" alt="yicchi" /></a>
|
|
@@ -177,7 +172,9 @@ If you find oRPC valuable and would like to support its development, you can do
|
|
|
177
172
|
<a href="https://github.com/nelsonlaidev?ref=orpc" target="_blank" rel="noopener" title="Nelson Lai"><img src="https://avatars.githubusercontent.com/u/75498339?u=2fc0e0b95dd184c5ffb744df977cb15a18b60672&v=4" width="32" height="32" alt="Nelson Lai" /></a>
|
|
178
173
|
<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>
|
|
179
174
|
<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>
|
|
175
|
+
<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>
|
|
180
176
|
<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
|
+
<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>
|
|
181
178
|
<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>
|
|
182
179
|
<a href="https://github.com/NovakAnton?ref=orpc" target="_blank" rel="noopener" title="Novak Antonijevic"><img src="https://avatars.githubusercontent.com/u/157126729?u=ae49fa22292d55c0434ff0ca008206155b18663b&v=4" width="32" height="32" alt="Novak Antonijevic" /></a>
|
|
183
180
|
<a href="https://github.com/laduniestu?ref=orpc" target="_blank" rel="noopener" title="Laduni Estu Syalwa"><img src="https://avatars.githubusercontent.com/u/44757637?u=a2fc1ea8f7d827a96721176f79d30592d1c48059&v=4" width="32" height="32" alt="Laduni Estu Syalwa" /></a>
|
|
@@ -185,10 +182,18 @@ If you find oRPC valuable and would like to support its development, you can do
|
|
|
185
182
|
<a href="https://github.com/illarionvk?ref=orpc" target="_blank" rel="noopener" title="Illarion Koperski"><img src="https://avatars.githubusercontent.com/u/5012724?u=7cfa13652f7ac5fb3c56d880e3eb3fbe40c3ea34&v=4" width="32" height="32" alt="Illarion Koperski" /></a>
|
|
186
183
|
<a href="https://github.com/steelbrain?ref=orpc" target="_blank" rel="noopener" title="Anees Iqbal"><img src="https://avatars.githubusercontent.com/u/4278113?u=22b80b5399eed68ac76cd58b02961b0481f1db11&v=4" width="32" height="32" alt="Anees Iqbal" /></a>
|
|
187
184
|
<a href="https://github.com/Scrumplex?ref=orpc" target="_blank" rel="noopener" title="Sefa Eyeoglu"><img src="https://avatars.githubusercontent.com/u/11587657?u=ab503582165c0bbff0cca47ce31c9450bb1553c9&v=4" width="32" height="32" alt="Sefa Eyeoglu" /></a>
|
|
185
|
+
<a href="https://github.com/nattstack?ref=orpc" target="_blank" rel="noopener" title="natt"><img src="https://avatars.githubusercontent.com/u/31426677?u=fa9dbb8b3e66eb0ea3c88db5dc07f31c8c5418fe&v=4" width="32" height="32" alt="natt" /></a>
|
|
188
186
|
<a href="https://github.com/ChromeGG?ref=orpc" target="_blank" rel="noopener" title="Adam Tkaczyk"><img src="https://avatars.githubusercontent.com/u/39050595?u=a58ca6042a6950e94e6e92442db76ef584279bc0&v=4" width="32" height="32" alt="Adam Tkaczyk" /></a>
|
|
189
187
|
<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>
|
|
190
188
|
</p>
|
|
191
189
|
|
|
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
|
+
|
|
192
197
|
## License
|
|
193
198
|
|
|
194
|
-
Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/
|
|
199
|
+
Distributed under the MIT License. See [LICENSE](https://github.com/middleapi/orpc/blob/main/LICENSE) for more information.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,45 +1,92 @@
|
|
|
1
1
|
import { ClientOptions } from '@orpc/client';
|
|
2
|
-
import {
|
|
3
|
-
import { Context,
|
|
4
|
-
import { MaybeOptionalOptions
|
|
5
|
-
|
|
6
|
-
import { Tool } from 'ai';
|
|
2
|
+
import { AnySchema, ErrorMap, InferSchemaOutput, InferSchemaInput, MetaPlugin, AnyProcedureContract, Schema, ProcedureContract } from '@orpc/contract';
|
|
3
|
+
import { Lazy, Context, ProcedureClientOptions, Procedure } from '@orpc/server';
|
|
4
|
+
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
5
|
+
import { JSONValue, Tool } from 'ai';
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The [function tool](https://ai-sdk.dev/docs/foundations/tools) member of the AI SDK `Tool` union,
|
|
9
|
+
* which is the kind of tool this package produces.
|
|
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';
|
|
11
25
|
}
|
|
12
|
-
|
|
26
|
+
interface AiSdkToolFunction {
|
|
27
|
+
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(meta: AiSdkToolMeta<TInputSchema, TOutputSchema>): AiSdkToolMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
|
|
13
28
|
}
|
|
14
29
|
/**
|
|
15
|
-
*
|
|
16
|
-
* as
|
|
30
|
+
* Attaches base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options to a procedure/contract,
|
|
31
|
+
* used as defaults when creating tools from it.
|
|
32
|
+
*
|
|
33
|
+
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
17
34
|
*
|
|
18
|
-
* @
|
|
19
|
-
* @info Standard [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract-first/define-contract).
|
|
35
|
+
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
20
36
|
*
|
|
21
37
|
* @example
|
|
22
38
|
* ```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'
|
|
23
84
|
* import { oc } from '@orpc/contract'
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
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
|
-
* })
|
|
85
|
+
*
|
|
86
|
+
* const getWeatherContract = oc
|
|
87
|
+
* .meta(
|
|
88
|
+
* aiSdkTool({ description: 'Get the weather in a location' }), // default AI SDK tool options
|
|
89
|
+
* )
|
|
43
90
|
* .input(
|
|
44
91
|
* z.object({
|
|
45
92
|
* location: z.string().describe('The location to get the weather for'),
|
|
@@ -52,6 +99,8 @@ declare class CreateToolError extends Error {
|
|
|
52
99
|
* }),
|
|
53
100
|
* )
|
|
54
101
|
*
|
|
102
|
+
* const implementTool = implementToolFactory()
|
|
103
|
+
*
|
|
55
104
|
* const getWeatherTool = implementTool(getWeatherContract, {
|
|
56
105
|
* execute: async ({ location }) => ({
|
|
57
106
|
* location,
|
|
@@ -60,35 +109,29 @@ declare class CreateToolError extends Error {
|
|
|
60
109
|
* })
|
|
61
110
|
* ```
|
|
62
111
|
*/
|
|
63
|
-
declare function
|
|
112
|
+
declare function implementToolFactory(_options?: ImplementToolFactoryOptions): ToolImplementer;
|
|
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
|
+
}
|
|
64
117
|
/**
|
|
65
|
-
*
|
|
66
|
-
* by leveraging existing procedure definitions.
|
|
118
|
+
* Creates a builder that converts [procedures](https://orpc.dev/docs/procedure)
|
|
119
|
+
* into [AI SDK Tools](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing procedure definitions.
|
|
120
|
+
*
|
|
121
|
+
* The factory accepts oRPC related options, and the resulting builder
|
|
122
|
+
* accepts a procedure alongside AI SDK tool options.
|
|
67
123
|
*
|
|
68
|
-
* @
|
|
124
|
+
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-factory AI SDK Tool Factory Docs}
|
|
69
125
|
*
|
|
70
126
|
* @example
|
|
71
127
|
* ```ts
|
|
128
|
+
* import { aiSdkTool, createToolFactory } from '@orpc/ai-sdk'
|
|
72
129
|
* import { os } from '@orpc/server'
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
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
|
-
* })
|
|
130
|
+
*
|
|
131
|
+
* const getWeatherProcedure = os
|
|
132
|
+
* .meta(
|
|
133
|
+
* aiSdkTool({ description: 'Get the weather in a location' }), // default AI SDK tool options
|
|
134
|
+
* )
|
|
92
135
|
* .input(z.object({
|
|
93
136
|
* location: z.string().describe('The location to get the weather for'),
|
|
94
137
|
* }))
|
|
@@ -101,12 +144,16 @@ declare function implementTool<TOutInput, TInOutput>(contract: ContractProcedure
|
|
|
101
144
|
* temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
102
145
|
* }))
|
|
103
146
|
*
|
|
104
|
-
* const
|
|
147
|
+
* const createTool = createToolFactory({
|
|
105
148
|
* context: {}, // provide initial context if needed
|
|
106
149
|
* })
|
|
150
|
+
*
|
|
151
|
+
* const getWeatherTool = createTool(getWeatherProcedure, {
|
|
152
|
+
* // ...AI SDK tool options/overrides here if needed
|
|
153
|
+
* })
|
|
107
154
|
* ```
|
|
108
155
|
*/
|
|
109
|
-
declare function
|
|
156
|
+
declare function createToolFactory<TInitialContext extends Context = object>(...rest: MaybeOptionalOptions<CreateToolFactoryOptions<TInitialContext>>): ToolFactory<TInitialContext>;
|
|
110
157
|
|
|
111
|
-
export {
|
|
112
|
-
export type { AiSdkToolMeta };
|
|
158
|
+
export { aiSdkTool, createToolFactory, getAiSdkToolMeta, implementToolFactory };
|
|
159
|
+
export type { AiSdkToolFunction, AiSdkToolMeta, AiSdkToolMetaPlugin, CreateToolFactoryOptions, FunctionTool, ImplementToolFactoryOptions, ToolFactory, ToolImplementer, ToolOutput };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,92 @@
|
|
|
1
1
|
import { ClientOptions } from '@orpc/client';
|
|
2
|
-
import {
|
|
3
|
-
import { Context,
|
|
4
|
-
import { MaybeOptionalOptions
|
|
5
|
-
|
|
6
|
-
import { Tool } from 'ai';
|
|
2
|
+
import { AnySchema, ErrorMap, InferSchemaOutput, InferSchemaInput, MetaPlugin, AnyProcedureContract, Schema, ProcedureContract } from '@orpc/contract';
|
|
3
|
+
import { Lazy, Context, ProcedureClientOptions, Procedure } from '@orpc/server';
|
|
4
|
+
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
5
|
+
import { JSONValue, Tool } from 'ai';
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The [function tool](https://ai-sdk.dev/docs/foundations/tools) member of the AI SDK `Tool` union,
|
|
9
|
+
* which is the kind of tool this package produces.
|
|
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';
|
|
11
25
|
}
|
|
12
|
-
|
|
26
|
+
interface AiSdkToolFunction {
|
|
27
|
+
<TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(meta: AiSdkToolMeta<TInputSchema, TOutputSchema>): AiSdkToolMetaPlugin<TInputSchema, TOutputSchema, TErrorMap>;
|
|
13
28
|
}
|
|
14
29
|
/**
|
|
15
|
-
*
|
|
16
|
-
* as
|
|
30
|
+
* Attaches base [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools) options to a procedure/contract,
|
|
31
|
+
* used as defaults when creating tools from it.
|
|
32
|
+
*
|
|
33
|
+
* **Note**: When defined multiple times, options are overridden by the most recent call.
|
|
17
34
|
*
|
|
18
|
-
* @
|
|
19
|
-
* @info Standard [procedures](https://orpc.dev/docs/procedure) are also compatible with [procedure contracts](https://orpc.dev/docs/contract-first/define-contract).
|
|
35
|
+
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-implementer AI SDK Tool Implementer Docs}
|
|
20
36
|
*
|
|
21
37
|
* @example
|
|
22
38
|
* ```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'
|
|
23
84
|
* import { oc } from '@orpc/contract'
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
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
|
-
* })
|
|
85
|
+
*
|
|
86
|
+
* const getWeatherContract = oc
|
|
87
|
+
* .meta(
|
|
88
|
+
* aiSdkTool({ description: 'Get the weather in a location' }), // default AI SDK tool options
|
|
89
|
+
* )
|
|
43
90
|
* .input(
|
|
44
91
|
* z.object({
|
|
45
92
|
* location: z.string().describe('The location to get the weather for'),
|
|
@@ -52,6 +99,8 @@ declare class CreateToolError extends Error {
|
|
|
52
99
|
* }),
|
|
53
100
|
* )
|
|
54
101
|
*
|
|
102
|
+
* const implementTool = implementToolFactory()
|
|
103
|
+
*
|
|
55
104
|
* const getWeatherTool = implementTool(getWeatherContract, {
|
|
56
105
|
* execute: async ({ location }) => ({
|
|
57
106
|
* location,
|
|
@@ -60,35 +109,29 @@ declare class CreateToolError extends Error {
|
|
|
60
109
|
* })
|
|
61
110
|
* ```
|
|
62
111
|
*/
|
|
63
|
-
declare function
|
|
112
|
+
declare function implementToolFactory(_options?: ImplementToolFactoryOptions): ToolImplementer;
|
|
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
|
+
}
|
|
64
117
|
/**
|
|
65
|
-
*
|
|
66
|
-
* by leveraging existing procedure definitions.
|
|
118
|
+
* Creates a builder that converts [procedures](https://orpc.dev/docs/procedure)
|
|
119
|
+
* into [AI SDK Tools](https://ai-sdk.dev/docs/foundations/tools) by leveraging existing procedure definitions.
|
|
120
|
+
*
|
|
121
|
+
* The factory accepts oRPC related options, and the resulting builder
|
|
122
|
+
* accepts a procedure alongside AI SDK tool options.
|
|
67
123
|
*
|
|
68
|
-
* @
|
|
124
|
+
* @see {@link https://orpc.dev/docs/integrations/ai-sdk#tool-factory AI SDK Tool Factory Docs}
|
|
69
125
|
*
|
|
70
126
|
* @example
|
|
71
127
|
* ```ts
|
|
128
|
+
* import { aiSdkTool, createToolFactory } from '@orpc/ai-sdk'
|
|
72
129
|
* import { os } from '@orpc/server'
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
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
|
-
* })
|
|
130
|
+
*
|
|
131
|
+
* const getWeatherProcedure = os
|
|
132
|
+
* .meta(
|
|
133
|
+
* aiSdkTool({ description: 'Get the weather in a location' }), // default AI SDK tool options
|
|
134
|
+
* )
|
|
92
135
|
* .input(z.object({
|
|
93
136
|
* location: z.string().describe('The location to get the weather for'),
|
|
94
137
|
* }))
|
|
@@ -101,12 +144,16 @@ declare function implementTool<TOutInput, TInOutput>(contract: ContractProcedure
|
|
|
101
144
|
* temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
102
145
|
* }))
|
|
103
146
|
*
|
|
104
|
-
* const
|
|
147
|
+
* const createTool = createToolFactory({
|
|
105
148
|
* context: {}, // provide initial context if needed
|
|
106
149
|
* })
|
|
150
|
+
*
|
|
151
|
+
* const getWeatherTool = createTool(getWeatherProcedure, {
|
|
152
|
+
* // ...AI SDK tool options/overrides here if needed
|
|
153
|
+
* })
|
|
107
154
|
* ```
|
|
108
155
|
*/
|
|
109
|
-
declare function
|
|
156
|
+
declare function createToolFactory<TInitialContext extends Context = object>(...rest: MaybeOptionalOptions<CreateToolFactoryOptions<TInitialContext>>): ToolFactory<TInitialContext>;
|
|
110
157
|
|
|
111
|
-
export {
|
|
112
|
-
export type { AiSdkToolMeta };
|
|
158
|
+
export { aiSdkTool, createToolFactory, getAiSdkToolMeta, implementToolFactory };
|
|
159
|
+
export type { AiSdkToolFunction, AiSdkToolMeta, AiSdkToolMetaPlugin, CreateToolFactoryOptions, FunctionTool, ImplementToolFactoryOptions, ToolFactory, ToolImplementer, ToolOutput };
|
package/dist/index.mjs
CHANGED
|
@@ -1,43 +1,142 @@
|
|
|
1
|
+
import { getAsyncIteratorObjectSchemaDetails } from '@orpc/contract';
|
|
2
|
+
import { combineJsonSchemasWithComposition } from '@orpc/json-schema';
|
|
1
3
|
import { Procedure, call } from '@orpc/server';
|
|
2
|
-
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
3
|
-
export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
|
|
4
|
+
import { resolveMaybeOptionalOptions, toArray, isAsyncIteratorObject, ORPC_NAME } from '@orpc/shared';
|
|
4
5
|
import { tool } from 'ai';
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
-
|
|
7
|
+
const aiSdkTool = (incoming) => ({
|
|
8
|
+
name: "~ai-sdk/tool",
|
|
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"];
|
|
8
19
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
20
|
+
|
|
21
|
+
const ANY_SCHEMA = {
|
|
22
|
+
"~standard": {
|
|
23
|
+
vendor: ORPC_NAME,
|
|
24
|
+
version: 1,
|
|
25
|
+
validate: (value) => ({ value }),
|
|
26
|
+
jsonSchema: {
|
|
27
|
+
input: () => ({}),
|
|
28
|
+
output: () => ({})
|
|
29
|
+
}
|
|
12
30
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
31
|
+
};
|
|
32
|
+
function combineJsonSchemas(jsonSchemas) {
|
|
33
|
+
let $schema;
|
|
34
|
+
const branches = jsonSchemas.map((jsonSchema) => {
|
|
35
|
+
if ("$schema" in jsonSchema) {
|
|
36
|
+
const { $schema: branch$schema, ...rest } = jsonSchema;
|
|
37
|
+
$schema ??= branch$schema;
|
|
38
|
+
return rest;
|
|
39
|
+
}
|
|
40
|
+
return jsonSchema;
|
|
20
41
|
});
|
|
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
|
+
};
|
|
21
75
|
}
|
|
22
|
-
function
|
|
76
|
+
function getIteratorYieldSchemas(schemas) {
|
|
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) {
|
|
23
110
|
const options = resolveMaybeOptionalOptions(rest);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
111
|
+
const implementTool = implementToolFactory(options);
|
|
112
|
+
const factory = (procedure, ...rest2) => {
|
|
113
|
+
const toolOptions = resolveMaybeOptionalOptions(rest2);
|
|
114
|
+
const disabledValidation = new Procedure({
|
|
115
|
+
...procedure["~orpc"],
|
|
116
|
+
disableInputValidation: true,
|
|
117
|
+
disableOutputValidation: true
|
|
118
|
+
});
|
|
119
|
+
const isIteratorOutput = getIteratorYieldSchemas(toArray(procedure["~orpc"].outputSchemas)) !== void 0;
|
|
120
|
+
return implementTool(procedure, {
|
|
121
|
+
...toolOptions,
|
|
122
|
+
/**
|
|
123
|
+
* For `asyncIteratorObject` outputs, the tool streams each event as a
|
|
124
|
+
* [preliminary result](https://ai-sdk.dev/docs/ai-sdk-core/tools-and-tool-calling#preliminary-tool-results),
|
|
125
|
+
* and the last event becomes the final result.
|
|
126
|
+
*/
|
|
127
|
+
execute: isIteratorOutput ? async function* (input, callingOptions) {
|
|
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;
|
|
41
140
|
}
|
|
42
141
|
|
|
43
|
-
export {
|
|
142
|
+
export { aiSdkTool, createToolFactory, getAiSdkToolMeta, implementToolFactory };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orpc/ai-sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0-beta.20",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://orpc.dev",
|
|
7
7
|
"repository": {
|
|
@@ -10,11 +10,12 @@
|
|
|
10
10
|
"directory": "packages/ai-sdk"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
|
-
"
|
|
14
|
-
"
|
|
13
|
+
"orpc",
|
|
14
|
+
"ai-sdk"
|
|
15
15
|
],
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"exports": {
|
|
18
|
+
"./package.json": "./package.json",
|
|
18
19
|
".": {
|
|
19
20
|
"types": "./dist/index.d.mts",
|
|
20
21
|
"import": "./dist/index.mjs",
|
|
@@ -25,21 +26,21 @@
|
|
|
25
26
|
"dist"
|
|
26
27
|
],
|
|
27
28
|
"peerDependencies": {
|
|
28
|
-
"ai": ">=
|
|
29
|
+
"ai": ">=7.0.31"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@orpc/client": "
|
|
32
|
-
"@orpc/contract": "
|
|
33
|
-
"@orpc/
|
|
34
|
-
"@orpc/
|
|
32
|
+
"@orpc/client": "2.0.0-beta.20",
|
|
33
|
+
"@orpc/contract": "2.0.0-beta.20",
|
|
34
|
+
"@orpc/json-schema": "2.0.0-beta.20",
|
|
35
|
+
"@orpc/server": "2.0.0-beta.20",
|
|
36
|
+
"@orpc/shared": "2.0.0-beta.20"
|
|
35
37
|
},
|
|
36
38
|
"devDependencies": {
|
|
37
|
-
"ai": "^
|
|
38
|
-
"zod": "^4.3
|
|
39
|
+
"ai": "^7.0.31",
|
|
40
|
+
"zod": "^4.4.3"
|
|
39
41
|
},
|
|
40
42
|
"scripts": {
|
|
41
43
|
"build": "unbuild",
|
|
42
|
-
"build:watch": "pnpm run build --watch",
|
|
43
44
|
"type:check": "tsc -b"
|
|
44
45
|
}
|
|
45
46
|
}
|