@orpc/ai-sdk 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,15 +1,114 @@
1
- import { Meta, ContractProcedure, Schema } from '@orpc/contract';
2
- import { SetOptional } from '@orpc/shared';
1
+ import { ClientOptions } from '@orpc/client';
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';
3
5
  export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
4
6
  import { Tool } from 'ai';
5
7
 
6
- declare const CREATE_AI_SDK_TOOL_META_SYMBOL: unique symbol;
7
- interface CreateAiSdkToolMeta extends Meta {
8
- [CREATE_AI_SDK_TOOL_META_SYMBOL]?: Partial<Tool<unknown, unknown>>;
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>>;
9
11
  }
10
12
  declare class CreateToolError extends Error {
11
13
  }
12
- declare function createTool<TInput, TOutput>(contract: ContractProcedure<Schema<any, TInput>, Schema<any, TOutput>, any, CreateAiSdkToolMeta>, options: SetOptional<Tool<TInput, TOutput>, 'inputSchema' | 'outputSchema'>): Tool<TInput, TOutput>;
14
+ /**
15
+ * Implements [procedure contract](https://orpc.unnoq.com/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.
17
+ *
18
+ * @warning Requires a contract with an `input` schema defined.
19
+ * @info Standard [procedures](https://orpc.unnoq.com/docs/procedure) are also compatible with [procedure contracts](https://orpc.unnoq.com/docs/contract-first/define-contract).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { oc } from '@orpc/contract'
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
+ * name: 'custom-tool-name', // AI SDK tool name
38
+ * },
39
+ * })
40
+ * .route({
41
+ * summary: 'Get the weather in a location', // AI SDK tool description
42
+ * })
43
+ * .input(
44
+ * z.object({
45
+ * location: z.string().describe('The location to get the weather for'),
46
+ * }),
47
+ * )
48
+ * .output(
49
+ * z.object({
50
+ * location: z.string().describe('The location the weather is for'),
51
+ * temperature: z.number().describe('The temperature in Celsius'),
52
+ * }),
53
+ * )
54
+ *
55
+ * const getWeatherTool = implementTool(getWeatherContract, {
56
+ * execute: async ({ location }) => ({
57
+ * location,
58
+ * temperature: 72 + Math.floor(Math.random() * 21) - 10,
59
+ * }),
60
+ * })
61
+ * ```
62
+ */
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>;
64
+ /**
65
+ * Converts a [procedure](https://orpc.unnoq.com/docs/procedure) into an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools)
66
+ * by leveraging existing procedure definitions.
67
+ *
68
+ * @warning Requires a contract with an `input` schema defined.
69
+ * @warning Validation occurs twice (once for the tool, once for the procedure call).
70
+ * So validation may fail if inputSchema or outputSchema transform the data into different shapes.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { os } from '@orpc/server'
75
+ * import {
76
+ * AI_SDK_TOOL_META_SYMBOL,
77
+ * AiSdkToolMeta,
78
+ * createTool
79
+ * } from '@orpc/ai-sdk'
80
+ * import { z } from 'zod'
81
+ *
82
+ * interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
83
+ * const base = os.$meta<ORPCMeta>({})
84
+ *
85
+ * const getWeatherProcedure = base
86
+ * .meta({
87
+ * [AI_SDK_TOOL_META_SYMBOL]: {
88
+ * name: 'custom-tool-name', // AI SDK tool name
89
+ * },
90
+ * })
91
+ * .route({
92
+ * summary: 'Get the weather in a location',
93
+ * })
94
+ * .input(z.object({
95
+ * location: z.string().describe('The location to get the weather for'),
96
+ * }))
97
+ * .output(z.object({
98
+ * location: z.string().describe('The location the weather is for'),
99
+ * temperature: z.number().describe('The temperature in Celsius'),
100
+ * }))
101
+ * .handler(async ({ input }) => ({
102
+ * location: input.location,
103
+ * temperature: 72 + Math.floor(Math.random() * 21) - 10,
104
+ * }))
105
+ *
106
+ * const getWeatherTool = createTool(getWeatherProcedure, {
107
+ * context: {}, // provide initial context if needed
108
+ * })
109
+ * ```
110
+ */
111
+ 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>>;
13
112
 
14
- export { CREATE_AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool };
15
- export type { CreateAiSdkToolMeta };
113
+ export { AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool, implementTool };
114
+ export type { AiSdkToolMeta };
package/dist/index.d.ts CHANGED
@@ -1,15 +1,114 @@
1
- import { Meta, ContractProcedure, Schema } from '@orpc/contract';
2
- import { SetOptional } from '@orpc/shared';
1
+ import { ClientOptions } from '@orpc/client';
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';
3
5
  export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
4
6
  import { Tool } from 'ai';
5
7
 
6
- declare const CREATE_AI_SDK_TOOL_META_SYMBOL: unique symbol;
7
- interface CreateAiSdkToolMeta extends Meta {
8
- [CREATE_AI_SDK_TOOL_META_SYMBOL]?: Partial<Tool<unknown, unknown>>;
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>>;
9
11
  }
10
12
  declare class CreateToolError extends Error {
11
13
  }
12
- declare function createTool<TInput, TOutput>(contract: ContractProcedure<Schema<any, TInput>, Schema<any, TOutput>, any, CreateAiSdkToolMeta>, options: SetOptional<Tool<TInput, TOutput>, 'inputSchema' | 'outputSchema'>): Tool<TInput, TOutput>;
14
+ /**
15
+ * Implements [procedure contract](https://orpc.unnoq.com/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.
17
+ *
18
+ * @warning Requires a contract with an `input` schema defined.
19
+ * @info Standard [procedures](https://orpc.unnoq.com/docs/procedure) are also compatible with [procedure contracts](https://orpc.unnoq.com/docs/contract-first/define-contract).
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * import { oc } from '@orpc/contract'
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
+ * name: 'custom-tool-name', // AI SDK tool name
38
+ * },
39
+ * })
40
+ * .route({
41
+ * summary: 'Get the weather in a location', // AI SDK tool description
42
+ * })
43
+ * .input(
44
+ * z.object({
45
+ * location: z.string().describe('The location to get the weather for'),
46
+ * }),
47
+ * )
48
+ * .output(
49
+ * z.object({
50
+ * location: z.string().describe('The location the weather is for'),
51
+ * temperature: z.number().describe('The temperature in Celsius'),
52
+ * }),
53
+ * )
54
+ *
55
+ * const getWeatherTool = implementTool(getWeatherContract, {
56
+ * execute: async ({ location }) => ({
57
+ * location,
58
+ * temperature: 72 + Math.floor(Math.random() * 21) - 10,
59
+ * }),
60
+ * })
61
+ * ```
62
+ */
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>;
64
+ /**
65
+ * Converts a [procedure](https://orpc.unnoq.com/docs/procedure) into an [AI SDK Tool](https://ai-sdk.dev/docs/foundations/tools)
66
+ * by leveraging existing procedure definitions.
67
+ *
68
+ * @warning Requires a contract with an `input` schema defined.
69
+ * @warning Validation occurs twice (once for the tool, once for the procedure call).
70
+ * So validation may fail if inputSchema or outputSchema transform the data into different shapes.
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * import { os } from '@orpc/server'
75
+ * import {
76
+ * AI_SDK_TOOL_META_SYMBOL,
77
+ * AiSdkToolMeta,
78
+ * createTool
79
+ * } from '@orpc/ai-sdk'
80
+ * import { z } from 'zod'
81
+ *
82
+ * interface ORPCMeta extends AiSdkToolMeta {} // optional extend meta
83
+ * const base = os.$meta<ORPCMeta>({})
84
+ *
85
+ * const getWeatherProcedure = base
86
+ * .meta({
87
+ * [AI_SDK_TOOL_META_SYMBOL]: {
88
+ * name: 'custom-tool-name', // AI SDK tool name
89
+ * },
90
+ * })
91
+ * .route({
92
+ * summary: 'Get the weather in a location',
93
+ * })
94
+ * .input(z.object({
95
+ * location: z.string().describe('The location to get the weather for'),
96
+ * }))
97
+ * .output(z.object({
98
+ * location: z.string().describe('The location the weather is for'),
99
+ * temperature: z.number().describe('The temperature in Celsius'),
100
+ * }))
101
+ * .handler(async ({ input }) => ({
102
+ * location: input.location,
103
+ * temperature: 72 + Math.floor(Math.random() * 21) - 10,
104
+ * }))
105
+ *
106
+ * const getWeatherTool = createTool(getWeatherProcedure, {
107
+ * context: {}, // provide initial context if needed
108
+ * })
109
+ * ```
110
+ */
111
+ 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>>;
13
112
 
14
- export { CREATE_AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool };
15
- export type { CreateAiSdkToolMeta };
113
+ export { AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool, implementTool };
114
+ export type { AiSdkToolMeta };
package/dist/index.mjs CHANGED
@@ -1,20 +1,32 @@
1
- import { tool } from 'ai';
1
+ import { call } from '@orpc/server';
2
+ import { resolveMaybeOptionalOptions } from '@orpc/shared';
2
3
  export { AsyncIteratorClass, asyncIteratorToStream as eventIteratorToStream, asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, streamToAsyncIteratorClass as streamToEventIterator } from '@orpc/shared';
4
+ import { tool } from 'ai';
3
5
 
4
- const CREATE_AI_SDK_TOOL_META_SYMBOL = Symbol("ORPC_CREATE_AI_SDK_TOOL_META");
6
+ const AI_SDK_TOOL_META_SYMBOL = Symbol("ORPC_AI_SDK_TOOL_META");
5
7
  class CreateToolError extends Error {
6
8
  }
7
- function createTool(contract, options) {
9
+ function implementTool(contract, ...rest) {
8
10
  if (contract["~orpc"].inputSchema === void 0) {
9
- throw new CreateToolError("Cannot create tool from a contract procedure without input schema.");
11
+ throw new CreateToolError("Cannot implement tool from a contract procedure without input schema.");
10
12
  }
13
+ const options = resolveMaybeOptionalOptions(rest);
11
14
  return tool({
12
15
  inputSchema: contract["~orpc"].inputSchema,
13
16
  outputSchema: contract["~orpc"].outputSchema,
14
17
  description: contract["~orpc"].route.summary ?? contract["~orpc"].route.description,
15
- ...contract["~orpc"].meta[CREATE_AI_SDK_TOOL_META_SYMBOL],
18
+ ...contract["~orpc"].meta[AI_SDK_TOOL_META_SYMBOL],
19
+ ...options
20
+ });
21
+ }
22
+ function createTool(procedure, ...rest) {
23
+ const options = resolveMaybeOptionalOptions(rest);
24
+ return implementTool(procedure, {
25
+ execute: (input) => {
26
+ return call(procedure, input, options);
27
+ },
16
28
  ...options
17
29
  });
18
30
  }
19
31
 
20
- export { CREATE_AI_SDK_TOOL_META_SYMBOL, CreateToolError, createTool };
32
+ 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": "0.0.0",
4
+ "version": "0.0.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -27,7 +27,9 @@
27
27
  "ai": ">=5.0.76"
28
28
  },
29
29
  "dependencies": {
30
+ "@orpc/server": "1.10.2",
30
31
  "@orpc/contract": "1.10.2",
32
+ "@orpc/client": "1.10.2",
31
33
  "@orpc/shared": "1.10.2"
32
34
  },
33
35
  "devDependencies": {