@@ -8,6 +8,7 @@ import { type Pipeable, pipeArguments } from "effect/Pipeable"
8
8
import * as Predicate from "effect/Predicate"
9
9
import * as Schema from "effect/Schema"
10
10
import * as AST from "effect/SchemaAST"
11
+ import type * as Types from "effect/Types"
11
12
import type { AiError } from "./AiError.js"
12
13
13
14
/**
@@ -34,9 +35,12 @@ export interface AiTool<
34
35
out Name extends string ,
35
36
out Parameters extends AnyStructSchema = Schema . Struct < { } > ,
36
37
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
38
40
> extends Pipeable {
39
- readonly [ TypeId ] : TypeId
41
+ readonly [ TypeId ] : {
42
+ readonly _Requirements : Types . Covariant < Requirements >
43
+ }
40
44
41
45
/**
42
46
* The name of the tool.
@@ -71,6 +75,12 @@ export interface AiTool<
71
75
*/
72
76
readonly failureSchema : Failure
73
77
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
+
74
84
/**
75
85
* Set the schema to use for tool handler success.
76
86
*/
@@ -109,14 +119,16 @@ export interface AiTool<
109
119
* @since 1.0.0
110
120
* @category Guards
111
121
*/
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 )
113
123
114
124
/**
115
125
* @since 1.0.0
116
126
* @category Models
117
127
*/
118
128
export interface Any extends Pipeable {
119
- readonly [ TypeId ] : TypeId
129
+ readonly [ TypeId ] : {
130
+ readonly _Requirements : Types . Covariant < any >
131
+ }
120
132
readonly name : string
121
133
readonly description ?: string | undefined
122
134
readonly key : string
@@ -157,12 +169,13 @@ export type HandlerEffect<Tool extends Any> = [Tool] extends [
157
169
infer _Name ,
158
170
infer _Parameters ,
159
171
infer _Success ,
160
- infer _Failure
172
+ infer _Failure ,
173
+ infer _Requirements
161
174
>
162
175
] ? Effect . Effect <
163
176
_Success [ "Type" ] ,
164
177
AiError | _Failure [ "Type" ] ,
165
- _Parameters [ "Context" ] | _Success [ "Context" ] | _Failure [ "Context" ]
178
+ _Parameters [ "Context" ] | _Success [ "Context" ] | _Failure [ "Context" ] | _Requirements
166
179
>
167
180
: never
168
181
@@ -205,7 +218,8 @@ export type Name<Tool> = Tool extends AiTool<
205
218
infer _Name ,
206
219
infer _Parameters ,
207
220
infer _Success ,
208
- infer _Failure
221
+ infer _Failure ,
222
+ infer _Requirements
209
223
> ? _Name :
210
224
never
211
225
@@ -220,7 +234,8 @@ export type Parameters<Tool> = Tool extends AiTool<
220
234
infer _Name ,
221
235
infer _Parameters ,
222
236
infer _Success ,
223
- infer _Failure
237
+ infer _Failure ,
238
+ infer _Requirements
224
239
> ? _Parameters [ "Type" ] :
225
240
never
226
241
@@ -235,7 +250,8 @@ export type ParametersSchema<Tool> = Tool extends AiTool<
235
250
infer _Name ,
236
251
infer _Parameters ,
237
252
infer _Success ,
238
- infer _Failure
253
+ infer _Failure ,
254
+ infer _Requirements
239
255
> ? _Parameters :
240
256
never
241
257
@@ -250,7 +266,8 @@ export type Success<Tool> = Tool extends AiTool<
250
266
infer _Name ,
251
267
infer _Parameters ,
252
268
infer _Success ,
253
- infer _Failure
269
+ infer _Failure ,
270
+ infer _Requirements
254
271
> ? _Success [ "Type" ] :
255
272
never
256
273
@@ -265,7 +282,8 @@ export type SuccessSchema<Tool> = Tool extends AiTool<
265
282
infer _Name ,
266
283
infer _Parameters ,
267
284
infer _Success ,
268
- infer _Failure
285
+ infer _Failure ,
286
+ infer _Requirements
269
287
> ? _Success :
270
288
never
271
289
@@ -280,7 +298,8 @@ export type Failure<Tool> = Tool extends AiTool<
280
298
infer _Name ,
281
299
infer _Parameters ,
282
300
infer _Success ,
283
- infer _Failure
301
+ infer _Failure ,
302
+ infer _Requirements
284
303
> ? _Failure [ "Type" ] :
285
304
never
286
305
@@ -295,7 +314,8 @@ export type FailureSchema<Tool> = Tool extends AiTool<
295
314
infer _Name ,
296
315
infer _Parameters ,
297
316
infer _Success ,
298
- infer _Failure
317
+ infer _Failure ,
318
+ infer _Requirements
299
319
> ? _Failure :
300
320
never
301
321
@@ -309,8 +329,9 @@ export type Context<Tool> = Tool extends AiTool<
309
329
infer _Name ,
310
330
infer _Parameters ,
311
331
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 :
314
335
never
315
336
316
337
/**
@@ -357,7 +378,8 @@ export type ToHandler<Tool extends Any> = Tool extends AiTool<
357
378
infer _Name ,
358
379
infer _Parameters ,
359
380
infer _Success ,
360
- infer _Failure
381
+ infer _Failure ,
382
+ infer _Requirements
361
383
> ? Handler < _Name > :
362
384
never
363
385
@@ -366,6 +388,9 @@ const Proto = {
366
388
pipe ( ) {
367
389
return pipeArguments ( this , arguments )
368
390
} ,
391
+ addRequirement ( this : AnyWithProtocol ) {
392
+ return makeProto ( { ...this } )
393
+ } ,
369
394
setSuccess ( this : AnyWithProtocol , successSchema : Schema . Schema . Any ) {
370
395
return makeProto ( {
371
396
...this ,
@@ -439,12 +464,7 @@ export const make = <
439
464
* it fails.
440
465
*/
441
466
readonly failure ?: Failure
442
- } ) : AiTool <
443
- Name ,
444
- Schema . Struct < Parameters > ,
445
- Success ,
446
- Failure
447
- > => {
467
+ } ) : AiTool < Name , Schema . Struct < Parameters > , Success , Failure > => {
448
468
const successSchema = options ?. success ?? Schema . Void
449
469
const failureSchema = options ?. failure ?? Schema . Never
450
470
return makeProto ( {
0 commit comments