Skip to content

Commit 1887544

Browse files
committed
support per-request requirements in tool call definitions
1 parent 8b2c14d commit 1887544

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

packages/ai/ai/src/AiTool.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type Pipeable, pipeArguments } from "effect/Pipeable"
88
import * as Predicate from "effect/Predicate"
99
import * as Schema from "effect/Schema"
1010
import * as AST from "effect/SchemaAST"
11+
import type * as Types from "effect/Types"
1112
import type { AiError } from "./AiError.js"
1213

1314
/**
@@ -34,9 +35,12 @@ export interface AiTool<
3435
out Name extends string,
3536
out Parameters extends AnyStructSchema = Schema.Struct<{}>,
3637
out Success extends Schema.Schema.Any = typeof Schema.Void,
37-
out Failure extends Schema.Schema.All = typeof Schema.Never
38+
out Failure extends Schema.Schema.All = typeof Schema.Never,
39+
out Requirements = never
3840
> extends Pipeable {
39-
readonly [TypeId]: TypeId
41+
readonly [TypeId]: {
42+
readonly _Requirements: Types.Covariant<Requirements>
43+
}
4044

4145
/**
4246
* The name of the tool.
@@ -71,6 +75,12 @@ export interface AiTool<
7175
*/
7276
readonly failureSchema: Failure
7377

78+
/**
79+
* Adds a requirement on a particular service for the tool call to be able to
80+
* be executed.
81+
*/
82+
addRequirement<Requirement>(): AiTool<Name, Parameters, Success, Failure, Requirements | Requirement>
83+
7484
/**
7585
* Set the schema to use for tool handler success.
7686
*/
@@ -109,14 +119,16 @@ export interface AiTool<
109119
* @since 1.0.0
110120
* @category Guards
111121
*/
112-
export const isAiTool = (u: unknown): u is AiTool<any, any, any, any> => Predicate.hasProperty(u, TypeId)
122+
export const isAiTool = (u: unknown): u is AiTool<any, any, any, any, any> => Predicate.hasProperty(u, TypeId)
113123

114124
/**
115125
* @since 1.0.0
116126
* @category Models
117127
*/
118128
export interface Any extends Pipeable {
119-
readonly [TypeId]: TypeId
129+
readonly [TypeId]: {
130+
readonly _Requirements: Types.Covariant<any>
131+
}
120132
readonly name: string
121133
readonly description?: string | undefined
122134
readonly key: string
@@ -157,12 +169,13 @@ export type HandlerEffect<Tool extends Any> = [Tool] extends [
157169
infer _Name,
158170
infer _Parameters,
159171
infer _Success,
160-
infer _Failure
172+
infer _Failure,
173+
infer _Requirements
161174
>
162175
] ? Effect.Effect<
163176
_Success["Type"],
164177
AiError | _Failure["Type"],
165-
_Parameters["Context"] | _Success["Context"] | _Failure["Context"]
178+
_Parameters["Context"] | _Success["Context"] | _Failure["Context"] | _Requirements
166179
>
167180
: never
168181

@@ -205,7 +218,8 @@ export type Name<Tool> = Tool extends AiTool<
205218
infer _Name,
206219
infer _Parameters,
207220
infer _Success,
208-
infer _Failure
221+
infer _Failure,
222+
infer _Requirements
209223
> ? _Name :
210224
never
211225

@@ -220,7 +234,8 @@ export type Parameters<Tool> = Tool extends AiTool<
220234
infer _Name,
221235
infer _Parameters,
222236
infer _Success,
223-
infer _Failure
237+
infer _Failure,
238+
infer _Requirements
224239
> ? _Parameters["Type"] :
225240
never
226241

@@ -235,7 +250,8 @@ export type ParametersSchema<Tool> = Tool extends AiTool<
235250
infer _Name,
236251
infer _Parameters,
237252
infer _Success,
238-
infer _Failure
253+
infer _Failure,
254+
infer _Requirements
239255
> ? _Parameters :
240256
never
241257

@@ -250,7 +266,8 @@ export type Success<Tool> = Tool extends AiTool<
250266
infer _Name,
251267
infer _Parameters,
252268
infer _Success,
253-
infer _Failure
269+
infer _Failure,
270+
infer _Requirements
254271
> ? _Success["Type"] :
255272
never
256273

@@ -265,7 +282,8 @@ export type SuccessSchema<Tool> = Tool extends AiTool<
265282
infer _Name,
266283
infer _Parameters,
267284
infer _Success,
268-
infer _Failure
285+
infer _Failure,
286+
infer _Requirements
269287
> ? _Success :
270288
never
271289

@@ -280,7 +298,8 @@ export type Failure<Tool> = Tool extends AiTool<
280298
infer _Name,
281299
infer _Parameters,
282300
infer _Success,
283-
infer _Failure
301+
infer _Failure,
302+
infer _Requirements
284303
> ? _Failure["Type"] :
285304
never
286305

@@ -295,7 +314,8 @@ export type FailureSchema<Tool> = Tool extends AiTool<
295314
infer _Name,
296315
infer _Parameters,
297316
infer _Success,
298-
infer _Failure
317+
infer _Failure,
318+
infer _Requirements
299319
> ? _Failure :
300320
never
301321

@@ -309,8 +329,9 @@ export type Context<Tool> = Tool extends AiTool<
309329
infer _Name,
310330
infer _Parameters,
311331
infer _Success,
312-
infer _Failure
313-
> ? _Parameters["Context"] | _Success["Context"] | _Failure["Context"] :
332+
infer _Failure,
333+
infer _Requirements
334+
> ? _Parameters["Context"] | _Success["Context"] | _Failure["Context"] | _Requirements :
314335
never
315336

316337
/**
@@ -357,7 +378,8 @@ export type ToHandler<Tool extends Any> = Tool extends AiTool<
357378
infer _Name,
358379
infer _Parameters,
359380
infer _Success,
360-
infer _Failure
381+
infer _Failure,
382+
infer _Requirements
361383
> ? Handler<_Name> :
362384
never
363385

@@ -366,6 +388,9 @@ const Proto = {
366388
pipe() {
367389
return pipeArguments(this, arguments)
368390
},
391+
addRequirement(this: AnyWithProtocol) {
392+
return makeProto({ ...this })
393+
},
369394
setSuccess(this: AnyWithProtocol, successSchema: Schema.Schema.Any) {
370395
return makeProto({
371396
...this,
@@ -439,12 +464,7 @@ export const make = <
439464
* it fails.
440465
*/
441466
readonly failure?: Failure
442-
}): AiTool<
443-
Name,
444-
Schema.Struct<Parameters>,
445-
Success,
446-
Failure
447-
> => {
467+
}): AiTool<Name, Schema.Struct<Parameters>, Success, Failure> => {
448468
const successSchema = options?.success ?? Schema.Void
449469
const failureSchema = options?.failure ?? Schema.Never
450470
return makeProto({

0 commit comments

Comments
 (0)