@effect/ai 0.29.1 → 0.31.0
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/cjs/LanguageModel.js +6 -2
- package/dist/cjs/LanguageModel.js.map +1 -1
- package/dist/cjs/Prompt.js +77 -7
- package/dist/cjs/Prompt.js.map +1 -1
- package/dist/cjs/Response.js +177 -58
- package/dist/cjs/Response.js.map +1 -1
- package/dist/cjs/Tool.js +7 -17
- package/dist/cjs/Tool.js.map +1 -1
- package/dist/cjs/Toolkit.js +26 -11
- package/dist/cjs/Toolkit.js.map +1 -1
- package/dist/dts/LanguageModel.d.ts +6 -4
- package/dist/dts/LanguageModel.d.ts.map +1 -1
- package/dist/dts/Prompt.d.ts +106 -1
- package/dist/dts/Prompt.d.ts.map +1 -1
- package/dist/dts/Response.d.ts +262 -49
- package/dist/dts/Response.d.ts.map +1 -1
- package/dist/dts/Tool.d.ts +99 -16
- package/dist/dts/Tool.d.ts.map +1 -1
- package/dist/dts/Toolkit.d.ts +1 -4
- package/dist/dts/Toolkit.d.ts.map +1 -1
- package/dist/esm/LanguageModel.js +6 -2
- package/dist/esm/LanguageModel.js.map +1 -1
- package/dist/esm/Prompt.js +67 -6
- package/dist/esm/Prompt.js.map +1 -1
- package/dist/esm/Response.js +156 -55
- package/dist/esm/Response.js.map +1 -1
- package/dist/esm/Tool.js +7 -17
- package/dist/esm/Tool.js.map +1 -1
- package/dist/esm/Toolkit.js +26 -11
- package/dist/esm/Toolkit.js.map +1 -1
- package/package.json +3 -3
- package/src/LanguageModel.ts +22 -11
- package/src/Prompt.ts +131 -16
- package/src/Response.ts +311 -74
- package/src/Tool.ts +279 -123
- package/src/Toolkit.ts +31 -28
package/src/Tool.ts
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
* @since 1.0.0
|
|
28
28
|
*/
|
|
29
29
|
import * as Context from "effect/Context"
|
|
30
|
-
import * as Effect from "effect/Effect"
|
|
30
|
+
import type * as Effect from "effect/Effect"
|
|
31
31
|
import { constFalse, constTrue, identity } from "effect/Function"
|
|
32
32
|
import * as JsonSchema from "effect/JSONSchema"
|
|
33
33
|
import * as Option from "effect/Option"
|
|
@@ -37,7 +37,7 @@ import * as Predicate from "effect/Predicate"
|
|
|
37
37
|
import * as Schema from "effect/Schema"
|
|
38
38
|
import * as AST from "effect/SchemaAST"
|
|
39
39
|
import type { Covariant } from "effect/Types"
|
|
40
|
-
import * as AiError from "./AiError.js"
|
|
40
|
+
import type * as AiError from "./AiError.js"
|
|
41
41
|
|
|
42
42
|
// =============================================================================
|
|
43
43
|
// Type Ids
|
|
@@ -115,15 +115,13 @@ export interface Tool<
|
|
|
115
115
|
readonly parameters: AnyStructSchema
|
|
116
116
|
readonly success: Schema.Schema.Any
|
|
117
117
|
readonly failure: Schema.Schema.All
|
|
118
|
-
|
|
119
|
-
readonly parameters: Schema.Struct<{}>
|
|
120
|
-
readonly success: typeof Schema.Void
|
|
121
|
-
readonly failure: typeof Schema.Never
|
|
118
|
+
readonly failureMode: FailureMode
|
|
122
119
|
},
|
|
123
120
|
Requirements = never
|
|
124
121
|
> extends Tool.Variance<Requirements> {
|
|
125
122
|
/**
|
|
126
|
-
* The tool identifier which is used to uniquely identify the tool.
|
|
123
|
+
* The tool identifier which is used to uniquely identify the tool.
|
|
124
|
+
*/
|
|
127
125
|
readonly id: string
|
|
128
126
|
|
|
129
127
|
/**
|
|
@@ -136,6 +134,19 @@ export interface Tool<
|
|
|
136
134
|
*/
|
|
137
135
|
readonly description?: string | undefined
|
|
138
136
|
|
|
137
|
+
/**
|
|
138
|
+
* The strategy used for handling errors returned from tool call handler
|
|
139
|
+
* execution.
|
|
140
|
+
*
|
|
141
|
+
* If set to `"error"` (the default), errors that occur during tool call
|
|
142
|
+
* handler execution will be returned in the error channel of the calling
|
|
143
|
+
* effect.
|
|
144
|
+
*
|
|
145
|
+
* If set to `"return"`, errors that occur during tool call handler execution
|
|
146
|
+
* will be captured and returned as part of the tool call result.
|
|
147
|
+
*/
|
|
148
|
+
readonly failureMode: FailureMode
|
|
149
|
+
|
|
139
150
|
/**
|
|
140
151
|
* A `Schema` representing the parameters that a tool must be called with.
|
|
141
152
|
*/
|
|
@@ -167,60 +178,76 @@ export interface Tool<
|
|
|
167
178
|
* **MUST** be provided to each request to the large language model provider
|
|
168
179
|
* instead of being provided when creating the tool call handler layer.
|
|
169
180
|
*/
|
|
170
|
-
addDependency<Identifier, Service>(
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
Identifier | Requirements
|
|
174
|
-
>
|
|
181
|
+
addDependency<Identifier, Service>(
|
|
182
|
+
tag: Context.Tag<Identifier, Service>
|
|
183
|
+
): Tool<Name, Config, Identifier | Requirements>
|
|
175
184
|
|
|
176
185
|
/**
|
|
177
186
|
* Set the schema to use to validate the result of a tool call when successful.
|
|
178
187
|
*/
|
|
179
|
-
setParameters<
|
|
188
|
+
setParameters<
|
|
189
|
+
ParametersSchema extends Schema.Struct<any> | Schema.Struct.Fields
|
|
190
|
+
>(
|
|
180
191
|
schema: ParametersSchema
|
|
181
|
-
): Tool<
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
:
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
192
|
+
): Tool<
|
|
193
|
+
Name,
|
|
194
|
+
{
|
|
195
|
+
readonly parameters: ParametersSchema extends Schema.Struct<infer _> ? ParametersSchema
|
|
196
|
+
: ParametersSchema extends Schema.Struct.Fields ? Schema.Struct<ParametersSchema>
|
|
197
|
+
: never
|
|
198
|
+
readonly success: Config["success"]
|
|
199
|
+
readonly failure: Config["failure"]
|
|
200
|
+
readonly failureMode: Config["failureMode"]
|
|
201
|
+
},
|
|
202
|
+
Requirements
|
|
203
|
+
>
|
|
188
204
|
|
|
189
205
|
/**
|
|
190
206
|
* Set the schema to use to validate the result of a tool call when successful.
|
|
191
207
|
*/
|
|
192
|
-
setSuccess<SuccessSchema extends Schema.Schema.Any>(
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
208
|
+
setSuccess<SuccessSchema extends Schema.Schema.Any>(
|
|
209
|
+
schema: SuccessSchema
|
|
210
|
+
): Tool<
|
|
211
|
+
Name,
|
|
212
|
+
{
|
|
213
|
+
readonly parameters: Config["parameters"]
|
|
214
|
+
readonly success: SuccessSchema
|
|
215
|
+
readonly failure: Config["failure"]
|
|
216
|
+
readonly failureMode: Config["failureMode"]
|
|
217
|
+
},
|
|
218
|
+
Requirements
|
|
219
|
+
>
|
|
197
220
|
|
|
198
221
|
/**
|
|
199
222
|
* Set the schema to use to validate the result of a tool call when it fails.
|
|
200
223
|
*/
|
|
201
|
-
setFailure<FailureSchema extends Schema.Schema.Any>(
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
224
|
+
setFailure<FailureSchema extends Schema.Schema.Any>(
|
|
225
|
+
schema: FailureSchema
|
|
226
|
+
): Tool<
|
|
227
|
+
Name,
|
|
228
|
+
{
|
|
229
|
+
readonly parameters: Config["parameters"]
|
|
230
|
+
readonly success: Config["success"]
|
|
231
|
+
readonly failure: FailureSchema
|
|
232
|
+
readonly failureMode: Config["failureMode"]
|
|
233
|
+
},
|
|
234
|
+
Requirements
|
|
235
|
+
>
|
|
206
236
|
|
|
207
237
|
/**
|
|
208
238
|
* Add an annotation to the tool.
|
|
209
239
|
*/
|
|
210
|
-
annotate<I, S>(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
>
|
|
240
|
+
annotate<I, S>(
|
|
241
|
+
tag: Context.Tag<I, S>,
|
|
242
|
+
value: S
|
|
243
|
+
): Tool<Name, Config, Requirements>
|
|
215
244
|
|
|
216
245
|
/**
|
|
217
246
|
* Add many annotations to the tool.
|
|
218
247
|
*/
|
|
219
|
-
annotateContext<I>(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
Requirements
|
|
223
|
-
>
|
|
248
|
+
annotateContext<I>(
|
|
249
|
+
context: Context.Context<I>
|
|
250
|
+
): Tool<Name, Config, Requirements>
|
|
224
251
|
}
|
|
225
252
|
|
|
226
253
|
/**
|
|
@@ -264,21 +291,25 @@ export interface ProviderDefined<
|
|
|
264
291
|
readonly parameters: AnyStructSchema
|
|
265
292
|
readonly success: Schema.Schema.Any
|
|
266
293
|
readonly failure: Schema.Schema.All
|
|
294
|
+
readonly failureMode: FailureMode
|
|
267
295
|
} = {
|
|
268
296
|
readonly args: Schema.Struct<{}>
|
|
269
297
|
readonly parameters: Schema.Struct<{}>
|
|
270
298
|
readonly success: typeof Schema.Void
|
|
271
299
|
readonly failure: typeof Schema.Never
|
|
300
|
+
readonly failureMode: "error"
|
|
272
301
|
},
|
|
273
302
|
RequiresHandler extends boolean = false
|
|
274
303
|
> extends
|
|
275
|
-
Tool<
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
:
|
|
279
|
-
|
|
280
|
-
:
|
|
281
|
-
|
|
304
|
+
Tool<
|
|
305
|
+
Name,
|
|
306
|
+
{
|
|
307
|
+
readonly parameters: Config["parameters"]
|
|
308
|
+
readonly success: Config["success"]
|
|
309
|
+
readonly failure: Config["failure"]
|
|
310
|
+
readonly failureMode: Config["failureMode"]
|
|
311
|
+
}
|
|
312
|
+
>,
|
|
282
313
|
Tool.ProviderDefinedProto
|
|
283
314
|
{
|
|
284
315
|
/**
|
|
@@ -303,13 +334,23 @@ export interface ProviderDefined<
|
|
|
303
334
|
* this tool into a `Layer`.
|
|
304
335
|
*/
|
|
305
336
|
readonly requiresHandler: RequiresHandler
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Decodes the result received after the provider-defined tool is called.
|
|
309
|
-
*/
|
|
310
|
-
decodeResult(args: unknown): Effect.Effect<Config["success"]["Type"], AiError.AiError>
|
|
311
337
|
}
|
|
312
338
|
|
|
339
|
+
/**
|
|
340
|
+
* The strategy used for handling errors returned from tool call handler
|
|
341
|
+
* execution.
|
|
342
|
+
*
|
|
343
|
+
* If set to `"error"` (the default), errors that occur during tool call handler
|
|
344
|
+
* execution will be returned in the error channel of the calling effect.
|
|
345
|
+
*
|
|
346
|
+
* If set to `"return"`, errors that occur during tool call handler execution
|
|
347
|
+
* will be captured and returned as part of the tool call result.
|
|
348
|
+
*
|
|
349
|
+
* @since 1.0.0
|
|
350
|
+
* @category Models
|
|
351
|
+
*/
|
|
352
|
+
export type FailureMode = "error" | "return"
|
|
353
|
+
|
|
313
354
|
/**
|
|
314
355
|
* @since 1.0.0
|
|
315
356
|
*/
|
|
@@ -431,8 +472,9 @@ export const isUserDefined = (u: unknown): u is Tool<string, any, any> =>
|
|
|
431
472
|
* @since 1.0.0
|
|
432
473
|
* @category Guards
|
|
433
474
|
*/
|
|
434
|
-
export const isProviderDefined = (
|
|
435
|
-
|
|
475
|
+
export const isProviderDefined = (
|
|
476
|
+
u: unknown
|
|
477
|
+
): u is ProviderDefined<string, any> => Predicate.hasProperty(u, ProviderDefinedTypeId)
|
|
436
478
|
|
|
437
479
|
// =============================================================================
|
|
438
480
|
// Utility Types
|
|
@@ -454,6 +496,7 @@ export interface Any extends Pipeable {
|
|
|
454
496
|
readonly parametersSchema: AnyStructSchema
|
|
455
497
|
readonly successSchema: Schema.Schema.Any
|
|
456
498
|
readonly failureSchema: Schema.Schema.All
|
|
499
|
+
readonly failureMode: FailureMode
|
|
457
500
|
readonly annotations: Context.Context<never>
|
|
458
501
|
}
|
|
459
502
|
|
|
@@ -468,7 +511,9 @@ export interface AnyProviderDefined extends Any {
|
|
|
468
511
|
readonly argsSchema: AnyStructSchema
|
|
469
512
|
readonly requiresHandler: boolean
|
|
470
513
|
readonly providerName: string
|
|
471
|
-
readonly decodeResult: (
|
|
514
|
+
readonly decodeResult: (
|
|
515
|
+
result: unknown
|
|
516
|
+
) => Effect.Effect<any, AiError.AiError>
|
|
472
517
|
}
|
|
473
518
|
|
|
474
519
|
/**
|
|
@@ -509,6 +554,7 @@ export interface FromTaggedRequest<S extends AnyTaggedRequestSchema> extends
|
|
|
509
554
|
readonly parameters: S
|
|
510
555
|
readonly success: S["success"]
|
|
511
556
|
readonly failure: S["failure"]
|
|
557
|
+
readonly failureMode: "error"
|
|
512
558
|
}
|
|
513
559
|
>
|
|
514
560
|
{}
|
|
@@ -523,8 +569,8 @@ export type Name<T> = T extends Tool<
|
|
|
523
569
|
infer _Name,
|
|
524
570
|
infer _Config,
|
|
525
571
|
infer _Requirements
|
|
526
|
-
> ? _Name
|
|
527
|
-
never
|
|
572
|
+
> ? _Name
|
|
573
|
+
: never
|
|
528
574
|
|
|
529
575
|
/**
|
|
530
576
|
* A utility type to extract the type of the tool call parameters.
|
|
@@ -536,8 +582,8 @@ export type Parameters<T> = T extends Tool<
|
|
|
536
582
|
infer _Name,
|
|
537
583
|
infer _Config,
|
|
538
584
|
infer _Requirements
|
|
539
|
-
> ? Schema.Struct.Type<_Config["parameters"]["fields"]>
|
|
540
|
-
never
|
|
585
|
+
> ? Schema.Struct.Type<_Config["parameters"]["fields"]>
|
|
586
|
+
: never
|
|
541
587
|
|
|
542
588
|
/**
|
|
543
589
|
* A utility type to extract the encoded type of the tool call parameters.
|
|
@@ -549,8 +595,8 @@ export type ParametersEncoded<T> = T extends Tool<
|
|
|
549
595
|
infer _Name,
|
|
550
596
|
infer _Config,
|
|
551
597
|
infer _Requirements
|
|
552
|
-
> ? Schema.Schema.Encoded<_Config["parameters"]>
|
|
553
|
-
never
|
|
598
|
+
> ? Schema.Schema.Encoded<_Config["parameters"]>
|
|
599
|
+
: never
|
|
554
600
|
|
|
555
601
|
/**
|
|
556
602
|
* A utility type to extract the schema for the parameters which an `Tool`
|
|
@@ -563,8 +609,8 @@ export type ParametersSchema<T> = T extends Tool<
|
|
|
563
609
|
infer _Name,
|
|
564
610
|
infer _Config,
|
|
565
611
|
infer _Requirements
|
|
566
|
-
> ? _Config["parameters"]
|
|
567
|
-
never
|
|
612
|
+
> ? _Config["parameters"]
|
|
613
|
+
: never
|
|
568
614
|
|
|
569
615
|
/**
|
|
570
616
|
* A utility type to extract the type of the tool call result when it succeeds.
|
|
@@ -576,8 +622,8 @@ export type Success<T> = T extends Tool<
|
|
|
576
622
|
infer _Name,
|
|
577
623
|
infer _Config,
|
|
578
624
|
infer _Requirements
|
|
579
|
-
> ? Schema.Schema.Type<_Config["success"]>
|
|
580
|
-
never
|
|
625
|
+
> ? Schema.Schema.Type<_Config["success"]>
|
|
626
|
+
: never
|
|
581
627
|
|
|
582
628
|
/**
|
|
583
629
|
* A utility type to extract the encoded type of the tool call result when
|
|
@@ -590,8 +636,8 @@ export type SuccessEncoded<T> = T extends Tool<
|
|
|
590
636
|
infer _Name,
|
|
591
637
|
infer _Config,
|
|
592
638
|
infer _Requirements
|
|
593
|
-
> ? Schema.Schema.Encoded<_Config["success"]>
|
|
594
|
-
never
|
|
639
|
+
> ? Schema.Schema.Encoded<_Config["success"]>
|
|
640
|
+
: never
|
|
595
641
|
|
|
596
642
|
/**
|
|
597
643
|
* A utility type to extract the schema for the return type of a tool call when
|
|
@@ -604,8 +650,8 @@ export type SuccessSchema<T> = T extends Tool<
|
|
|
604
650
|
infer _Name,
|
|
605
651
|
infer _Config,
|
|
606
652
|
infer _Requirements
|
|
607
|
-
> ? _Config["success"]
|
|
608
|
-
never
|
|
653
|
+
> ? _Config["success"]
|
|
654
|
+
: never
|
|
609
655
|
|
|
610
656
|
/**
|
|
611
657
|
* A utility type to extract the type of the tool call result when it fails.
|
|
@@ -617,8 +663,8 @@ export type Failure<T> = T extends Tool<
|
|
|
617
663
|
infer _Name,
|
|
618
664
|
infer _Config,
|
|
619
665
|
infer _Requirements
|
|
620
|
-
> ? Schema.Schema.Type<_Config["failure"]>
|
|
621
|
-
never
|
|
666
|
+
> ? Schema.Schema.Type<_Config["failure"]>
|
|
667
|
+
: never
|
|
622
668
|
|
|
623
669
|
/**
|
|
624
670
|
* A utility type to extract the encoded type of the tool call result when
|
|
@@ -631,8 +677,36 @@ export type FailureEncoded<T> = T extends Tool<
|
|
|
631
677
|
infer _Name,
|
|
632
678
|
infer _Config,
|
|
633
679
|
infer _Requirements
|
|
634
|
-
> ? Schema.Schema.Encoded<_Config["failure"]>
|
|
635
|
-
never
|
|
680
|
+
> ? Schema.Schema.Encoded<_Config["failure"]>
|
|
681
|
+
: never
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* A utility type to extract the type of the tool call result whether it
|
|
685
|
+
* succeeds or fails.
|
|
686
|
+
*
|
|
687
|
+
* @since 1.0.0
|
|
688
|
+
* @category Utility Types
|
|
689
|
+
*/
|
|
690
|
+
export type Result<T> = T extends Tool<
|
|
691
|
+
infer _Name,
|
|
692
|
+
infer _Config,
|
|
693
|
+
infer _Requirements
|
|
694
|
+
> ? Success<T> | Failure<T>
|
|
695
|
+
: never
|
|
696
|
+
|
|
697
|
+
/**
|
|
698
|
+
* A utility type to extract the encoded type of the tool call result whether
|
|
699
|
+
* it succeeds or fails.
|
|
700
|
+
*
|
|
701
|
+
* @since 1.0.0
|
|
702
|
+
* @category Utility Types
|
|
703
|
+
*/
|
|
704
|
+
export type ResultEncoded<T> = T extends Tool<
|
|
705
|
+
infer _Name,
|
|
706
|
+
infer _Config,
|
|
707
|
+
infer _Requirements
|
|
708
|
+
> ? SuccessEncoded<T> | FailureEncoded<T>
|
|
709
|
+
: never
|
|
636
710
|
|
|
637
711
|
/**
|
|
638
712
|
* A utility type to extract the requirements of an `Tool`.
|
|
@@ -644,8 +718,12 @@ export type Requirements<T> = T extends Tool<
|
|
|
644
718
|
infer _Name,
|
|
645
719
|
infer _Config,
|
|
646
720
|
infer _Requirements
|
|
647
|
-
> ?
|
|
648
|
-
|
|
721
|
+
> ?
|
|
722
|
+
| _Config["parameters"]["Context"]
|
|
723
|
+
| _Config["success"]["Context"]
|
|
724
|
+
| _Config["failure"]["Context"]
|
|
725
|
+
| _Requirements
|
|
726
|
+
: never
|
|
649
727
|
|
|
650
728
|
/**
|
|
651
729
|
* Represents an `Tool` that has been implemented within the application.
|
|
@@ -667,10 +745,14 @@ export interface Handler<Name extends string> {
|
|
|
667
745
|
* @category Models
|
|
668
746
|
*/
|
|
669
747
|
export interface HandlerResult<Tool extends Any> {
|
|
748
|
+
/**
|
|
749
|
+
* Whether the result of executing the tool call handler was an error or not.
|
|
750
|
+
*/
|
|
751
|
+
readonly isFailure: boolean
|
|
670
752
|
/**
|
|
671
753
|
* The result of executing the handler for a particular tool.
|
|
672
754
|
*/
|
|
673
|
-
readonly result:
|
|
755
|
+
readonly result: Result<Tool>
|
|
674
756
|
/**
|
|
675
757
|
* The pre-encoded tool call result of executing the handler for a particular
|
|
676
758
|
* tool as a JSON-serializable value. The encoded result can be incorporated
|
|
@@ -679,6 +761,21 @@ export interface HandlerResult<Tool extends Any> {
|
|
|
679
761
|
readonly encodedResult: unknown
|
|
680
762
|
}
|
|
681
763
|
|
|
764
|
+
/**
|
|
765
|
+
* A utility type which represents the possible errors that can be raised by
|
|
766
|
+
* a tool call's handler.
|
|
767
|
+
*
|
|
768
|
+
* @since 1.0.0
|
|
769
|
+
* @category Utility Types
|
|
770
|
+
*/
|
|
771
|
+
export type HandlerError<T> = T extends Tool<
|
|
772
|
+
infer _Name,
|
|
773
|
+
infer _Config,
|
|
774
|
+
infer _Requirements
|
|
775
|
+
> ? _Config["failureMode"] extends "error" ? _Config["failure"]["Type"]
|
|
776
|
+
: never
|
|
777
|
+
: never
|
|
778
|
+
|
|
682
779
|
/**
|
|
683
780
|
* A utility type to create a union of `Handler` types for all tools in a
|
|
684
781
|
* record.
|
|
@@ -698,8 +795,12 @@ export type HandlersFor<Tools extends Record<string, Any>> = {
|
|
|
698
795
|
* @since 1.0.0
|
|
699
796
|
* @category Utility Types
|
|
700
797
|
*/
|
|
701
|
-
export type RequiresHandler<Tool extends Any> = Tool extends
|
|
702
|
-
|
|
798
|
+
export type RequiresHandler<Tool extends Any> = Tool extends ProviderDefined<
|
|
799
|
+
infer _Name,
|
|
800
|
+
infer _Config,
|
|
801
|
+
infer _RequiresHandler
|
|
802
|
+
> ? _RequiresHandler
|
|
803
|
+
: true
|
|
703
804
|
|
|
704
805
|
// =============================================================================
|
|
705
806
|
// Constructors
|
|
@@ -713,11 +814,14 @@ const Proto = {
|
|
|
713
814
|
addDependency(this: Any) {
|
|
714
815
|
return userDefinedProto({ ...this })
|
|
715
816
|
},
|
|
716
|
-
setParameters(
|
|
817
|
+
setParameters(
|
|
818
|
+
this: Any,
|
|
819
|
+
parametersSchema: Schema.Struct<any> | Schema.Struct.Fields
|
|
820
|
+
) {
|
|
717
821
|
return userDefinedProto({
|
|
718
822
|
...this,
|
|
719
823
|
parametersSchema: Schema.isSchema(parametersSchema)
|
|
720
|
-
? parametersSchema as any
|
|
824
|
+
? (parametersSchema as any)
|
|
721
825
|
: Schema.Struct(parametersSchema as any)
|
|
722
826
|
})
|
|
723
827
|
},
|
|
@@ -749,27 +853,15 @@ const Proto = {
|
|
|
749
853
|
|
|
750
854
|
const ProviderDefinedProto = {
|
|
751
855
|
...Proto,
|
|
752
|
-
[ProviderDefinedTypeId]: ProviderDefinedTypeId
|
|
753
|
-
decodeResult(this: AnyProviderDefined, result: unknown) {
|
|
754
|
-
return Schema.decodeUnknown(this.successSchema)(result).pipe(
|
|
755
|
-
Effect.orElse(() => Schema.decodeUnknown(this.failureSchema as any)(result)),
|
|
756
|
-
Effect.mapError((cause) =>
|
|
757
|
-
new AiError.MalformedOutput({
|
|
758
|
-
module: "Tool",
|
|
759
|
-
method: "ProviderDefined.decodeResult",
|
|
760
|
-
description: `Failed to decode the result of provider-defined tool '${this.name}'`,
|
|
761
|
-
cause
|
|
762
|
-
})
|
|
763
|
-
)
|
|
764
|
-
)
|
|
765
|
-
}
|
|
856
|
+
[ProviderDefinedTypeId]: ProviderDefinedTypeId
|
|
766
857
|
}
|
|
767
858
|
|
|
768
859
|
const userDefinedProto = <
|
|
769
860
|
const Name extends string,
|
|
770
861
|
Parameters extends AnyStructSchema,
|
|
771
862
|
Success extends Schema.Schema.Any,
|
|
772
|
-
Failure extends Schema.Schema.All
|
|
863
|
+
Failure extends Schema.Schema.All,
|
|
864
|
+
Mode extends FailureMode
|
|
773
865
|
>(options: {
|
|
774
866
|
readonly name: Name
|
|
775
867
|
readonly description?: string | undefined
|
|
@@ -777,12 +869,14 @@ const userDefinedProto = <
|
|
|
777
869
|
readonly successSchema: Success
|
|
778
870
|
readonly failureSchema: Failure
|
|
779
871
|
readonly annotations: Context.Context<never>
|
|
872
|
+
readonly failureMode: Mode
|
|
780
873
|
}): Tool<
|
|
781
874
|
Name,
|
|
782
875
|
{
|
|
783
876
|
readonly parameters: Parameters
|
|
784
877
|
readonly success: Success
|
|
785
878
|
readonly failure: Failure
|
|
879
|
+
readonly failureMode: Mode
|
|
786
880
|
}
|
|
787
881
|
> => {
|
|
788
882
|
const self = Object.assign(Object.create(Proto), options)
|
|
@@ -796,7 +890,8 @@ const providerDefinedProto = <
|
|
|
796
890
|
Parameters extends AnyStructSchema,
|
|
797
891
|
Success extends Schema.Schema.Any,
|
|
798
892
|
Failure extends Schema.Schema.All,
|
|
799
|
-
RequiresHandler extends boolean
|
|
893
|
+
RequiresHandler extends boolean,
|
|
894
|
+
Mode extends FailureMode
|
|
800
895
|
>(options: {
|
|
801
896
|
readonly id: string
|
|
802
897
|
readonly name: Name
|
|
@@ -807,6 +902,7 @@ const providerDefinedProto = <
|
|
|
807
902
|
readonly parametersSchema: Parameters
|
|
808
903
|
readonly successSchema: Success
|
|
809
904
|
readonly failureSchema: Failure
|
|
905
|
+
readonly failureMode: FailureMode
|
|
810
906
|
}): ProviderDefined<
|
|
811
907
|
Name,
|
|
812
908
|
{
|
|
@@ -814,6 +910,7 @@ const providerDefinedProto = <
|
|
|
814
910
|
readonly parameters: Parameters
|
|
815
911
|
readonly success: Success
|
|
816
912
|
readonly failure: Failure
|
|
913
|
+
readonly failureMode: Mode
|
|
817
914
|
},
|
|
818
915
|
RequiresHandler
|
|
819
916
|
> => Object.assign(Object.create(ProviderDefinedProto), options)
|
|
@@ -847,6 +944,7 @@ export const make = <
|
|
|
847
944
|
Parameters extends Schema.Struct.Fields = {},
|
|
848
945
|
Success extends Schema.Schema.Any = typeof Schema.Void,
|
|
849
946
|
Failure extends Schema.Schema.All = typeof Schema.Never,
|
|
947
|
+
Mode extends FailureMode | undefined = undefined,
|
|
850
948
|
Dependencies extends Array<Context.Tag<any, any>> = []
|
|
851
949
|
>(
|
|
852
950
|
/**
|
|
@@ -870,6 +968,17 @@ export const make = <
|
|
|
870
968
|
* Schema for tool execution failures.
|
|
871
969
|
*/
|
|
872
970
|
readonly failure?: Failure | undefined
|
|
971
|
+
/**
|
|
972
|
+
* The strategy used for handling errors returned from tool call handler
|
|
973
|
+
* execution.
|
|
974
|
+
*
|
|
975
|
+
* If set to `"error"` (the default), errors that occur during tool call handler
|
|
976
|
+
* execution will be returned in the error channel of the calling effect.
|
|
977
|
+
*
|
|
978
|
+
* If set to `"return"`, errors that occur during tool call handler execution
|
|
979
|
+
* will be captured and returned as part of the tool call result.
|
|
980
|
+
*/
|
|
981
|
+
readonly failureMode?: Mode
|
|
873
982
|
/**
|
|
874
983
|
* Service dependencies required by the tool handler.
|
|
875
984
|
*/
|
|
@@ -881,6 +990,7 @@ export const make = <
|
|
|
881
990
|
readonly parameters: Schema.Struct<Parameters>
|
|
882
991
|
readonly success: Success
|
|
883
992
|
readonly failure: Failure
|
|
993
|
+
readonly failureMode: Mode extends undefined ? "error" : Mode
|
|
884
994
|
},
|
|
885
995
|
Context.Tag.Identifier<Dependencies[number]>
|
|
886
996
|
> => {
|
|
@@ -894,6 +1004,7 @@ export const make = <
|
|
|
894
1004
|
: constEmptyStruct,
|
|
895
1005
|
successSchema,
|
|
896
1006
|
failureSchema,
|
|
1007
|
+
failureMode: options?.failureMode ?? "error",
|
|
897
1008
|
annotations: Context.empty()
|
|
898
1009
|
}) as any
|
|
899
1010
|
}
|
|
@@ -973,19 +1084,37 @@ export const providerDefined = <
|
|
|
973
1084
|
*/
|
|
974
1085
|
readonly failure?: Failure | undefined
|
|
975
1086
|
}) =>
|
|
976
|
-
|
|
1087
|
+
<Mode extends FailureMode | undefined = undefined>(
|
|
1088
|
+
args: RequiresHandler extends true ? Schema.Simplify<
|
|
1089
|
+
Schema.Struct.Encoded<Args> & {
|
|
1090
|
+
/**
|
|
1091
|
+
* The strategy used for handling errors returned from tool call handler
|
|
1092
|
+
* execution.
|
|
1093
|
+
*
|
|
1094
|
+
* If set to `"error"` (the default), errors that occur during tool call handler
|
|
1095
|
+
* execution will be returned in the error channel of the calling effect.
|
|
1096
|
+
*
|
|
1097
|
+
* If set to `"return"`, errors that occur during tool call handler execution
|
|
1098
|
+
* will be captured and returned as part of the tool call result.
|
|
1099
|
+
*/
|
|
1100
|
+
readonly failureMode?: Mode
|
|
1101
|
+
}
|
|
1102
|
+
>
|
|
1103
|
+
: Schema.Simplify<Schema.Struct.Encoded<Args>>
|
|
1104
|
+
): ProviderDefined<
|
|
977
1105
|
Name,
|
|
978
1106
|
{
|
|
979
1107
|
readonly args: Schema.Struct<Args>
|
|
980
1108
|
readonly parameters: Schema.Struct<Parameters>
|
|
981
1109
|
readonly success: Success
|
|
982
1110
|
readonly failure: Failure
|
|
1111
|
+
readonly failureMode: Mode extends undefined ? "error" : Mode
|
|
983
1112
|
},
|
|
984
1113
|
RequiresHandler
|
|
985
1114
|
> => {
|
|
1115
|
+
const failureMode = "failureMode" in args ? args.failureMode : undefined
|
|
986
1116
|
const successSchema = options?.success ?? Schema.Void
|
|
987
1117
|
const failureSchema = options?.failure ?? Schema.Never
|
|
988
|
-
const resultSchema = Schema.EitherFromUnion({ right: successSchema, left: failureSchema })
|
|
989
1118
|
return providerDefinedProto({
|
|
990
1119
|
id: options.id,
|
|
991
1120
|
name: options.toolkitName,
|
|
@@ -996,8 +1125,9 @@ export const providerDefined = <
|
|
|
996
1125
|
parametersSchema: options?.parameters
|
|
997
1126
|
? Schema.Struct(options?.parameters as any)
|
|
998
1127
|
: constEmptyStruct,
|
|
999
|
-
successSchema
|
|
1000
|
-
failureSchema
|
|
1128
|
+
successSchema,
|
|
1129
|
+
failureSchema,
|
|
1130
|
+
failureMode: failureMode ?? "error"
|
|
1001
1131
|
}) as any
|
|
1002
1132
|
}
|
|
1003
1133
|
|
|
@@ -1041,10 +1171,13 @@ export const fromTaggedRequest = <S extends AnyTaggedRequestSchema>(
|
|
|
1041
1171
|
): FromTaggedRequest<S> =>
|
|
1042
1172
|
userDefinedProto({
|
|
1043
1173
|
name: schema._tag,
|
|
1044
|
-
description: Option.getOrUndefined(
|
|
1174
|
+
description: Option.getOrUndefined(
|
|
1175
|
+
AST.getDescriptionAnnotation((schema.ast as any).to)
|
|
1176
|
+
),
|
|
1045
1177
|
parametersSchema: schema,
|
|
1046
1178
|
successSchema: schema.success,
|
|
1047
1179
|
failureSchema: schema.failure,
|
|
1180
|
+
failureMode: "error",
|
|
1048
1181
|
annotations: Context.empty()
|
|
1049
1182
|
}) as any
|
|
1050
1183
|
|
|
@@ -1079,6 +1212,7 @@ export const getDescription = <
|
|
|
1079
1212
|
readonly parameters: AnyStructSchema
|
|
1080
1213
|
readonly success: Schema.Schema.Any
|
|
1081
1214
|
readonly failure: Schema.Schema.All
|
|
1215
|
+
readonly failureMode: FailureMode
|
|
1082
1216
|
}
|
|
1083
1217
|
>(
|
|
1084
1218
|
/**
|
|
@@ -1096,16 +1230,18 @@ export const getDescription = <
|
|
|
1096
1230
|
* @since 1.0.0
|
|
1097
1231
|
* @category Utilities
|
|
1098
1232
|
*/
|
|
1099
|
-
export const getDescriptionFromSchemaAst = (
|
|
1100
|
-
|
|
1101
|
-
|
|
1233
|
+
export const getDescriptionFromSchemaAst = (
|
|
1234
|
+
ast: AST.AST
|
|
1235
|
+
): string | undefined => {
|
|
1236
|
+
const annotations = ast._tag === "Transformation"
|
|
1237
|
+
? {
|
|
1102
1238
|
...ast.to.annotations,
|
|
1103
1239
|
...ast.annotations
|
|
1104
|
-
}
|
|
1105
|
-
ast.annotations
|
|
1240
|
+
}
|
|
1241
|
+
: ast.annotations
|
|
1106
1242
|
return AST.DescriptionAnnotationId in annotations
|
|
1107
|
-
? annotations[AST.DescriptionAnnotationId] as string
|
|
1108
|
-
undefined
|
|
1243
|
+
? (annotations[AST.DescriptionAnnotationId] as string)
|
|
1244
|
+
: undefined
|
|
1109
1245
|
}
|
|
1110
1246
|
|
|
1111
1247
|
/**
|
|
@@ -1148,14 +1284,19 @@ export const getJsonSchema = <
|
|
|
1148
1284
|
readonly parameters: AnyStructSchema
|
|
1149
1285
|
readonly success: Schema.Schema.Any
|
|
1150
1286
|
readonly failure: Schema.Schema.All
|
|
1287
|
+
readonly failureMode: FailureMode
|
|
1151
1288
|
}
|
|
1152
|
-
>(
|
|
1289
|
+
>(
|
|
1290
|
+
tool: Tool<Name, Config>
|
|
1291
|
+
): JsonSchema.JsonSchema7 => getJsonSchemaFromSchemaAst(tool.parametersSchema.ast)
|
|
1153
1292
|
|
|
1154
1293
|
/**
|
|
1155
1294
|
* @since 1.0.0
|
|
1156
1295
|
* @category Utilities
|
|
1157
1296
|
*/
|
|
1158
|
-
export const getJsonSchemaFromSchemaAst = (
|
|
1297
|
+
export const getJsonSchemaFromSchemaAst = (
|
|
1298
|
+
ast: AST.AST
|
|
1299
|
+
): JsonSchema.JsonSchema7 => {
|
|
1159
1300
|
const props = AST.getPropertySignatures(ast)
|
|
1160
1301
|
if (props.length === 0) {
|
|
1161
1302
|
return {
|
|
@@ -1193,7 +1334,10 @@ export const getJsonSchemaFromSchemaAst = (ast: AST.AST): JsonSchema.JsonSchema7
|
|
|
1193
1334
|
* @since 1.0.0
|
|
1194
1335
|
* @category Annotations
|
|
1195
1336
|
*/
|
|
1196
|
-
export class Title extends Context.Tag("@effect/ai/Tool/Title")<
|
|
1337
|
+
export class Title extends Context.Tag("@effect/ai/Tool/Title")<
|
|
1338
|
+
Title,
|
|
1339
|
+
string
|
|
1340
|
+
>() {}
|
|
1197
1341
|
|
|
1198
1342
|
/**
|
|
1199
1343
|
* Annotation indicating whether a tool only reads data without making changes.
|
|
@@ -1209,9 +1353,12 @@ export class Title extends Context.Tag("@effect/ai/Tool/Title")<Title, string>()
|
|
|
1209
1353
|
* @since 1.0.0
|
|
1210
1354
|
* @category Annotations
|
|
1211
1355
|
*/
|
|
1212
|
-
export class Readonly extends Context.Reference<Readonly>()(
|
|
1213
|
-
|
|
1214
|
-
|
|
1356
|
+
export class Readonly extends Context.Reference<Readonly>()(
|
|
1357
|
+
"@effect/ai/Tool/Readonly",
|
|
1358
|
+
{
|
|
1359
|
+
defaultValue: constFalse
|
|
1360
|
+
}
|
|
1361
|
+
) {}
|
|
1215
1362
|
|
|
1216
1363
|
/**
|
|
1217
1364
|
* Annotation indicating whether a tool performs destructive operations.
|
|
@@ -1227,9 +1374,12 @@ export class Readonly extends Context.Reference<Readonly>()("@effect/ai/Tool/Rea
|
|
|
1227
1374
|
* @since 1.0.0
|
|
1228
1375
|
* @category Annotations
|
|
1229
1376
|
*/
|
|
1230
|
-
export class Destructive extends Context.Reference<Destructive>()(
|
|
1231
|
-
|
|
1232
|
-
|
|
1377
|
+
export class Destructive extends Context.Reference<Destructive>()(
|
|
1378
|
+
"@effect/ai/Tool/Destructive",
|
|
1379
|
+
{
|
|
1380
|
+
defaultValue: constTrue
|
|
1381
|
+
}
|
|
1382
|
+
) {}
|
|
1233
1383
|
|
|
1234
1384
|
/**
|
|
1235
1385
|
* Annotation indicating whether a tool can be called multiple times safely.
|
|
@@ -1245,9 +1395,12 @@ export class Destructive extends Context.Reference<Destructive>()("@effect/ai/To
|
|
|
1245
1395
|
* @since 1.0.0
|
|
1246
1396
|
* @category Annotations
|
|
1247
1397
|
*/
|
|
1248
|
-
export class Idempotent extends Context.Reference<Idempotent>()(
|
|
1249
|
-
|
|
1250
|
-
|
|
1398
|
+
export class Idempotent extends Context.Reference<Idempotent>()(
|
|
1399
|
+
"@effect/ai/Tool/Idempotent",
|
|
1400
|
+
{
|
|
1401
|
+
defaultValue: constFalse
|
|
1402
|
+
}
|
|
1403
|
+
) {}
|
|
1251
1404
|
|
|
1252
1405
|
/**
|
|
1253
1406
|
* Annotation indicating whether a tool can handle arbitrary external data.
|
|
@@ -1263,9 +1416,12 @@ export class Idempotent extends Context.Reference<Idempotent>()("@effect/ai/Tool
|
|
|
1263
1416
|
* @since 1.0.0
|
|
1264
1417
|
* @category Annotations
|
|
1265
1418
|
*/
|
|
1266
|
-
export class OpenWorld extends Context.Reference<OpenWorld>()(
|
|
1267
|
-
|
|
1268
|
-
|
|
1419
|
+
export class OpenWorld extends Context.Reference<OpenWorld>()(
|
|
1420
|
+
"@effect/ai/Tool/OpenWorld",
|
|
1421
|
+
{
|
|
1422
|
+
defaultValue: constTrue
|
|
1423
|
+
}
|
|
1424
|
+
) {}
|
|
1269
1425
|
|
|
1270
1426
|
// Licensed under BSD-3-Clause (below code only)
|
|
1271
1427
|
// Code adapted from https://github.com/fastify/secure-json-parse/blob/783fcb1b5434709466759847cec974381939673a/index.js
|