@effect/ai 0.29.1 → 0.30.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/src/Tool.ts CHANGED
@@ -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
  */
@@ -153,6 +164,12 @@ export interface Tool<
153
164
  */
154
165
  readonly failureSchema: Config["failure"]
155
166
 
167
+ /**
168
+ * A `Schema` representing the result of a tool call, whether it succeeds or
169
+ * fails.
170
+ */
171
+ readonly resultSchema: Schema.Either<Config["success"], Config["failure"]>
172
+
156
173
  /**
157
174
  * A `Context` object containing tool annotations which can store metadata
158
175
  * about the tool.
@@ -167,60 +184,76 @@ export interface Tool<
167
184
  * **MUST** be provided to each request to the large language model provider
168
185
  * instead of being provided when creating the tool call handler layer.
169
186
  */
170
- addDependency<Identifier, Service>(tag: Context.Tag<Identifier, Service>): Tool<
171
- Name,
172
- Config,
173
- Identifier | Requirements
174
- >
187
+ addDependency<Identifier, Service>(
188
+ tag: Context.Tag<Identifier, Service>
189
+ ): Tool<Name, Config, Identifier | Requirements>
175
190
 
176
191
  /**
177
192
  * Set the schema to use to validate the result of a tool call when successful.
178
193
  */
179
- setParameters<ParametersSchema extends Schema.Struct<any> | Schema.Struct.Fields>(
194
+ setParameters<
195
+ ParametersSchema extends Schema.Struct<any> | Schema.Struct.Fields
196
+ >(
180
197
  schema: ParametersSchema
181
- ): Tool<Name, {
182
- readonly parameters: ParametersSchema extends Schema.Struct<infer _> ? ParametersSchema
183
- : ParametersSchema extends Schema.Struct.Fields ? Schema.Struct<ParametersSchema>
184
- : never
185
- readonly success: Config["success"]
186
- readonly failure: Config["failure"]
187
- }, Requirements>
198
+ ): Tool<
199
+ Name,
200
+ {
201
+ readonly parameters: ParametersSchema extends Schema.Struct<infer _> ? ParametersSchema
202
+ : ParametersSchema extends Schema.Struct.Fields ? Schema.Struct<ParametersSchema>
203
+ : never
204
+ readonly success: Config["success"]
205
+ readonly failure: Config["failure"]
206
+ readonly failureMode: Config["failureMode"]
207
+ },
208
+ Requirements
209
+ >
188
210
 
189
211
  /**
190
212
  * Set the schema to use to validate the result of a tool call when successful.
191
213
  */
192
- setSuccess<SuccessSchema extends Schema.Schema.Any>(schema: SuccessSchema): Tool<Name, {
193
- readonly parameters: Config["parameters"]
194
- readonly success: SuccessSchema
195
- readonly failure: Config["failure"]
196
- }, Requirements>
214
+ setSuccess<SuccessSchema extends Schema.Schema.Any>(
215
+ schema: SuccessSchema
216
+ ): Tool<
217
+ Name,
218
+ {
219
+ readonly parameters: Config["parameters"]
220
+ readonly success: SuccessSchema
221
+ readonly failure: Config["failure"]
222
+ readonly failureMode: Config["failureMode"]
223
+ },
224
+ Requirements
225
+ >
197
226
 
198
227
  /**
199
228
  * Set the schema to use to validate the result of a tool call when it fails.
200
229
  */
201
- setFailure<FailureSchema extends Schema.Schema.Any>(schema: FailureSchema): Tool<Name, {
202
- readonly parameters: Config["parameters"]
203
- readonly success: Config["success"]
204
- readonly failure: FailureSchema
205
- }, Requirements>
230
+ setFailure<FailureSchema extends Schema.Schema.Any>(
231
+ schema: FailureSchema
232
+ ): Tool<
233
+ Name,
234
+ {
235
+ readonly parameters: Config["parameters"]
236
+ readonly success: Config["success"]
237
+ readonly failure: FailureSchema
238
+ readonly failureMode: Config["failureMode"]
239
+ },
240
+ Requirements
241
+ >
206
242
 
207
243
  /**
208
244
  * Add an annotation to the tool.
209
245
  */
210
- annotate<I, S>(tag: Context.Tag<I, S>, value: S): Tool<
211
- Name,
212
- Config,
213
- Requirements
214
- >
246
+ annotate<I, S>(
247
+ tag: Context.Tag<I, S>,
248
+ value: S
249
+ ): Tool<Name, Config, Requirements>
215
250
 
216
251
  /**
217
252
  * Add many annotations to the tool.
218
253
  */
219
- annotateContext<I>(context: Context.Context<I>): Tool<
220
- Name,
221
- Config,
222
- Requirements
223
- >
254
+ annotateContext<I>(
255
+ context: Context.Context<I>
256
+ ): Tool<Name, Config, Requirements>
224
257
  }
225
258
 
226
259
  /**
@@ -264,21 +297,25 @@ export interface ProviderDefined<
264
297
  readonly parameters: AnyStructSchema
265
298
  readonly success: Schema.Schema.Any
266
299
  readonly failure: Schema.Schema.All
300
+ readonly failureMode: FailureMode
267
301
  } = {
268
302
  readonly args: Schema.Struct<{}>
269
303
  readonly parameters: Schema.Struct<{}>
270
304
  readonly success: typeof Schema.Void
271
305
  readonly failure: typeof Schema.Never
306
+ readonly failureMode: "error"
272
307
  },
273
308
  RequiresHandler extends boolean = false
274
309
  > extends
275
- Tool<Name, {
276
- readonly parameters: Config["parameters"]
277
- success: RequiresHandler extends true ? Config["success"]
278
- : Schema.Either<Config["success"], Config["failure"]>
279
- failure: RequiresHandler extends true ? Config["failure"]
280
- : typeof Schema.Never
281
- }>,
310
+ Tool<
311
+ Name,
312
+ {
313
+ readonly parameters: Config["parameters"]
314
+ readonly success: Config["success"]
315
+ readonly failure: Config["failure"]
316
+ readonly failureMode: Config["failureMode"]
317
+ }
318
+ >,
282
319
  Tool.ProviderDefinedProto
283
320
  {
284
321
  /**
@@ -307,9 +344,26 @@ export interface ProviderDefined<
307
344
  /**
308
345
  * Decodes the result received after the provider-defined tool is called.
309
346
  */
310
- decodeResult(args: unknown): Effect.Effect<Config["success"]["Type"], AiError.AiError>
347
+ decodeResult(
348
+ args: unknown
349
+ ): Effect.Effect<Config["success"]["Type"], AiError.AiError>
311
350
  }
312
351
 
352
+ /**
353
+ * The strategy used for handling errors returned from tool call handler
354
+ * execution.
355
+ *
356
+ * If set to `"error"` (the default), errors that occur during tool call handler
357
+ * execution will be returned in the error channel of the calling effect.
358
+ *
359
+ * If set to `"return"`, errors that occur during tool call handler execution
360
+ * will be captured and returned as part of the tool call result.
361
+ *
362
+ * @since 1.0.0
363
+ * @category Models
364
+ */
365
+ export type FailureMode = "error" | "return"
366
+
313
367
  /**
314
368
  * @since 1.0.0
315
369
  */
@@ -431,8 +485,9 @@ export const isUserDefined = (u: unknown): u is Tool<string, any, any> =>
431
485
  * @since 1.0.0
432
486
  * @category Guards
433
487
  */
434
- export const isProviderDefined = (u: unknown): u is ProviderDefined<string, any> =>
435
- Predicate.hasProperty(u, ProviderDefinedTypeId)
488
+ export const isProviderDefined = (
489
+ u: unknown
490
+ ): u is ProviderDefined<string, any> => Predicate.hasProperty(u, ProviderDefinedTypeId)
436
491
 
437
492
  // =============================================================================
438
493
  // Utility Types
@@ -454,6 +509,8 @@ export interface Any extends Pipeable {
454
509
  readonly parametersSchema: AnyStructSchema
455
510
  readonly successSchema: Schema.Schema.Any
456
511
  readonly failureSchema: Schema.Schema.All
512
+ readonly resultSchema: Schema.Either<any, any>
513
+ readonly failureMode: FailureMode
457
514
  readonly annotations: Context.Context<never>
458
515
  }
459
516
 
@@ -468,7 +525,9 @@ export interface AnyProviderDefined extends Any {
468
525
  readonly argsSchema: AnyStructSchema
469
526
  readonly requiresHandler: boolean
470
527
  readonly providerName: string
471
- readonly decodeResult: (result: unknown) => Effect.Effect<any, AiError.AiError>
528
+ readonly decodeResult: (
529
+ result: unknown
530
+ ) => Effect.Effect<any, AiError.AiError>
472
531
  }
473
532
 
474
533
  /**
@@ -509,6 +568,7 @@ export interface FromTaggedRequest<S extends AnyTaggedRequestSchema> extends
509
568
  readonly parameters: S
510
569
  readonly success: S["success"]
511
570
  readonly failure: S["failure"]
571
+ readonly failureMode: "error"
512
572
  }
513
573
  >
514
574
  {}
@@ -523,8 +583,8 @@ export type Name<T> = T extends Tool<
523
583
  infer _Name,
524
584
  infer _Config,
525
585
  infer _Requirements
526
- > ? _Name :
527
- never
586
+ > ? _Name
587
+ : never
528
588
 
529
589
  /**
530
590
  * A utility type to extract the type of the tool call parameters.
@@ -536,8 +596,8 @@ export type Parameters<T> = T extends Tool<
536
596
  infer _Name,
537
597
  infer _Config,
538
598
  infer _Requirements
539
- > ? Schema.Struct.Type<_Config["parameters"]["fields"]> :
540
- never
599
+ > ? Schema.Struct.Type<_Config["parameters"]["fields"]>
600
+ : never
541
601
 
542
602
  /**
543
603
  * A utility type to extract the encoded type of the tool call parameters.
@@ -549,8 +609,8 @@ export type ParametersEncoded<T> = T extends Tool<
549
609
  infer _Name,
550
610
  infer _Config,
551
611
  infer _Requirements
552
- > ? Schema.Schema.Encoded<_Config["parameters"]> :
553
- never
612
+ > ? Schema.Schema.Encoded<_Config["parameters"]>
613
+ : never
554
614
 
555
615
  /**
556
616
  * A utility type to extract the schema for the parameters which an `Tool`
@@ -563,8 +623,8 @@ export type ParametersSchema<T> = T extends Tool<
563
623
  infer _Name,
564
624
  infer _Config,
565
625
  infer _Requirements
566
- > ? _Config["parameters"] :
567
- never
626
+ > ? _Config["parameters"]
627
+ : never
568
628
 
569
629
  /**
570
630
  * A utility type to extract the type of the tool call result when it succeeds.
@@ -576,8 +636,8 @@ export type Success<T> = T extends Tool<
576
636
  infer _Name,
577
637
  infer _Config,
578
638
  infer _Requirements
579
- > ? Schema.Schema.Type<_Config["success"]> :
580
- never
639
+ > ? Schema.Schema.Type<_Config["success"]>
640
+ : never
581
641
 
582
642
  /**
583
643
  * A utility type to extract the encoded type of the tool call result when
@@ -590,8 +650,8 @@ export type SuccessEncoded<T> = T extends Tool<
590
650
  infer _Name,
591
651
  infer _Config,
592
652
  infer _Requirements
593
- > ? Schema.Schema.Encoded<_Config["success"]> :
594
- never
653
+ > ? Schema.Schema.Encoded<_Config["success"]>
654
+ : never
595
655
 
596
656
  /**
597
657
  * A utility type to extract the schema for the return type of a tool call when
@@ -604,8 +664,8 @@ export type SuccessSchema<T> = T extends Tool<
604
664
  infer _Name,
605
665
  infer _Config,
606
666
  infer _Requirements
607
- > ? _Config["success"] :
608
- never
667
+ > ? _Config["success"]
668
+ : never
609
669
 
610
670
  /**
611
671
  * A utility type to extract the type of the tool call result when it fails.
@@ -617,8 +677,8 @@ export type Failure<T> = T extends Tool<
617
677
  infer _Name,
618
678
  infer _Config,
619
679
  infer _Requirements
620
- > ? Schema.Schema.Type<_Config["failure"]> :
621
- never
680
+ > ? Schema.Schema.Type<_Config["failure"]>
681
+ : never
622
682
 
623
683
  /**
624
684
  * A utility type to extract the encoded type of the tool call result when
@@ -631,7 +691,35 @@ export type FailureEncoded<T> = T extends Tool<
631
691
  infer _Name,
632
692
  infer _Config,
633
693
  infer _Requirements
634
- > ? Schema.Schema.Encoded<_Config["failure"]> :
694
+ > ? Schema.Schema.Encoded<_Config["failure"]>
695
+ : never
696
+
697
+ /**
698
+ * A utility type to extract the type of the tool call result whether it
699
+ * succeeds or fails.
700
+ *
701
+ * @since 1.0.0
702
+ * @category Utility Types
703
+ */
704
+ export type Result<T> = T extends Tool<
705
+ infer _Name,
706
+ infer _Config,
707
+ infer _Requirements
708
+ > ? Schema.Either<_Config["success"], _Config["failure"]>["Type"] :
709
+ never
710
+
711
+ /**
712
+ * A utility type to extract the encoded type of the tool call result whether
713
+ * it succeeds or fails.
714
+ *
715
+ * @since 1.0.0
716
+ * @category Utility Types
717
+ */
718
+ export type ResultEncoded<T> = T extends Tool<
719
+ infer _Name,
720
+ infer _Config,
721
+ infer _Requirements
722
+ > ? Schema.Either<_Config["success"], _Config["failure"]>["Encoded"] :
635
723
  never
636
724
 
637
725
  /**
@@ -644,8 +732,12 @@ export type Requirements<T> = T extends Tool<
644
732
  infer _Name,
645
733
  infer _Config,
646
734
  infer _Requirements
647
- > ? _Config["parameters"]["Context"] | _Config["success"]["Context"] | _Config["failure"]["Context"] | _Requirements :
648
- never
735
+ > ?
736
+ | _Config["parameters"]["Context"]
737
+ | _Config["success"]["Context"]
738
+ | _Config["failure"]["Context"]
739
+ | _Requirements
740
+ : never
649
741
 
650
742
  /**
651
743
  * Represents an `Tool` that has been implemented within the application.
@@ -670,15 +762,30 @@ export interface HandlerResult<Tool extends Any> {
670
762
  /**
671
763
  * The result of executing the handler for a particular tool.
672
764
  */
673
- readonly result: Success<Tool>
765
+ readonly result: Result<Tool>
674
766
  /**
675
767
  * The pre-encoded tool call result of executing the handler for a particular
676
768
  * tool as a JSON-serializable value. The encoded result can be incorporated
677
769
  * into subsequent requests to the large language model.
678
770
  */
679
- readonly encodedResult: unknown
771
+ readonly encodedResult: Schema.EitherEncoded<unknown, unknown>
680
772
  }
681
773
 
774
+ /**
775
+ * A utility type which represents the possible errors that can be raised by
776
+ * a tool call's handler.
777
+ *
778
+ * @since 1.0.0
779
+ * @category Utility Types
780
+ */
781
+ export type HandlerError<T> = T extends Tool<
782
+ infer _Name,
783
+ infer _Config,
784
+ infer _Requirements
785
+ > ? _Config["failureMode"] extends "error" ? Schema.Schema.Type<_Config["failure"]>
786
+ : never
787
+ : never
788
+
682
789
  /**
683
790
  * A utility type to create a union of `Handler` types for all tools in a
684
791
  * record.
@@ -698,8 +805,12 @@ export type HandlersFor<Tools extends Record<string, Any>> = {
698
805
  * @since 1.0.0
699
806
  * @category Utility Types
700
807
  */
701
- export type RequiresHandler<Tool extends Any> = Tool extends
702
- ProviderDefined<infer _Name, infer _Config, infer _RequiresHandler> ? _RequiresHandler : true
808
+ export type RequiresHandler<Tool extends Any> = Tool extends ProviderDefined<
809
+ infer _Name,
810
+ infer _Config,
811
+ infer _RequiresHandler
812
+ > ? _RequiresHandler
813
+ : true
703
814
 
704
815
  // =============================================================================
705
816
  // Constructors
@@ -713,11 +824,14 @@ const Proto = {
713
824
  addDependency(this: Any) {
714
825
  return userDefinedProto({ ...this })
715
826
  },
716
- setParameters(this: Any, parametersSchema: Schema.Struct<any> | Schema.Struct.Fields) {
827
+ setParameters(
828
+ this: Any,
829
+ parametersSchema: Schema.Struct<any> | Schema.Struct.Fields
830
+ ) {
717
831
  return userDefinedProto({
718
832
  ...this,
719
833
  parametersSchema: Schema.isSchema(parametersSchema)
720
- ? parametersSchema as any
834
+ ? (parametersSchema as any)
721
835
  : Schema.Struct(parametersSchema as any)
722
836
  })
723
837
  },
@@ -753,13 +867,14 @@ const ProviderDefinedProto = {
753
867
  decodeResult(this: AnyProviderDefined, result: unknown) {
754
868
  return Schema.decodeUnknown(this.successSchema)(result).pipe(
755
869
  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
- })
870
+ Effect.mapError(
871
+ (cause) =>
872
+ new AiError.MalformedOutput({
873
+ module: "Tool",
874
+ method: "ProviderDefined.decodeResult",
875
+ description: `Failed to decode the result of provider-defined tool '${this.name}'`,
876
+ cause
877
+ })
763
878
  )
764
879
  )
765
880
  }
@@ -769,20 +884,24 @@ const userDefinedProto = <
769
884
  const Name extends string,
770
885
  Parameters extends AnyStructSchema,
771
886
  Success extends Schema.Schema.Any,
772
- Failure extends Schema.Schema.All
887
+ Failure extends Schema.Schema.All,
888
+ Mode extends FailureMode
773
889
  >(options: {
774
890
  readonly name: Name
775
891
  readonly description?: string | undefined
776
892
  readonly parametersSchema: Parameters
777
893
  readonly successSchema: Success
778
894
  readonly failureSchema: Failure
895
+ readonly resultSchema: Schema.Either<Success, Failure>
779
896
  readonly annotations: Context.Context<never>
897
+ readonly failureMode: Mode
780
898
  }): Tool<
781
899
  Name,
782
900
  {
783
901
  readonly parameters: Parameters
784
902
  readonly success: Success
785
903
  readonly failure: Failure
904
+ readonly failureMode: Mode
786
905
  }
787
906
  > => {
788
907
  const self = Object.assign(Object.create(Proto), options)
@@ -796,7 +915,8 @@ const providerDefinedProto = <
796
915
  Parameters extends AnyStructSchema,
797
916
  Success extends Schema.Schema.Any,
798
917
  Failure extends Schema.Schema.All,
799
- RequiresHandler extends boolean
918
+ RequiresHandler extends boolean,
919
+ Mode extends FailureMode
800
920
  >(options: {
801
921
  readonly id: string
802
922
  readonly name: Name
@@ -807,6 +927,8 @@ const providerDefinedProto = <
807
927
  readonly parametersSchema: Parameters
808
928
  readonly successSchema: Success
809
929
  readonly failureSchema: Failure
930
+ readonly resultSchema: Schema.Either<Success, Failure>
931
+ readonly failureMode: FailureMode
810
932
  }): ProviderDefined<
811
933
  Name,
812
934
  {
@@ -814,6 +936,7 @@ const providerDefinedProto = <
814
936
  readonly parameters: Parameters
815
937
  readonly success: Success
816
938
  readonly failure: Failure
939
+ readonly failureMode: Mode
817
940
  },
818
941
  RequiresHandler
819
942
  > => Object.assign(Object.create(ProviderDefinedProto), options)
@@ -847,6 +970,7 @@ export const make = <
847
970
  Parameters extends Schema.Struct.Fields = {},
848
971
  Success extends Schema.Schema.Any = typeof Schema.Void,
849
972
  Failure extends Schema.Schema.All = typeof Schema.Never,
973
+ Mode extends FailureMode | undefined = undefined,
850
974
  Dependencies extends Array<Context.Tag<any, any>> = []
851
975
  >(
852
976
  /**
@@ -870,6 +994,17 @@ export const make = <
870
994
  * Schema for tool execution failures.
871
995
  */
872
996
  readonly failure?: Failure | undefined
997
+ /**
998
+ * The strategy used for handling errors returned from tool call handler
999
+ * execution.
1000
+ *
1001
+ * If set to `"error"` (the default), errors that occur during tool call handler
1002
+ * execution will be returned in the error channel of the calling effect.
1003
+ *
1004
+ * If set to `"return"`, errors that occur during tool call handler execution
1005
+ * will be captured and returned as part of the tool call result.
1006
+ */
1007
+ readonly failureMode?: Mode
873
1008
  /**
874
1009
  * Service dependencies required by the tool handler.
875
1010
  */
@@ -881,11 +1016,16 @@ export const make = <
881
1016
  readonly parameters: Schema.Struct<Parameters>
882
1017
  readonly success: Success
883
1018
  readonly failure: Failure
1019
+ readonly failureMode: Mode extends undefined ? "error" : Mode
884
1020
  },
885
1021
  Context.Tag.Identifier<Dependencies[number]>
886
1022
  > => {
887
1023
  const successSchema = options?.success ?? Schema.Void
888
1024
  const failureSchema = options?.failure ?? Schema.Never
1025
+ const resultSchema = Schema.Either({
1026
+ left: failureSchema,
1027
+ right: successSchema
1028
+ })
889
1029
  return userDefinedProto({
890
1030
  name,
891
1031
  description: options?.description,
@@ -894,6 +1034,8 @@ export const make = <
894
1034
  : constEmptyStruct,
895
1035
  successSchema,
896
1036
  failureSchema,
1037
+ resultSchema,
1038
+ failureMode: options?.failureMode ?? "error",
897
1039
  annotations: Context.empty()
898
1040
  }) as any
899
1041
  }
@@ -973,19 +1115,41 @@ export const providerDefined = <
973
1115
  */
974
1116
  readonly failure?: Failure | undefined
975
1117
  }) =>
976
- (args: Schema.Simplify<Schema.Struct.Encoded<Args>>): ProviderDefined<
1118
+ <Mode extends FailureMode | undefined = undefined>(
1119
+ args: RequiresHandler extends true ? Schema.Simplify<
1120
+ Schema.Struct.Encoded<Args> & {
1121
+ /**
1122
+ * The strategy used for handling errors returned from tool call handler
1123
+ * execution.
1124
+ *
1125
+ * If set to `"error"` (the default), errors that occur during tool call handler
1126
+ * execution will be returned in the error channel of the calling effect.
1127
+ *
1128
+ * If set to `"return"`, errors that occur during tool call handler execution
1129
+ * will be captured and returned as part of the tool call result.
1130
+ */
1131
+ readonly failureMode?: Mode
1132
+ }
1133
+ >
1134
+ : Schema.Simplify<Schema.Struct.Encoded<Args>>
1135
+ ): ProviderDefined<
977
1136
  Name,
978
1137
  {
979
1138
  readonly args: Schema.Struct<Args>
980
1139
  readonly parameters: Schema.Struct<Parameters>
981
1140
  readonly success: Success
982
1141
  readonly failure: Failure
1142
+ readonly failureMode: Mode extends undefined ? "error" : Mode
983
1143
  },
984
1144
  RequiresHandler
985
1145
  > => {
1146
+ const failureMode = "failureMode" in args ? args.failureMode : undefined
986
1147
  const successSchema = options?.success ?? Schema.Void
987
1148
  const failureSchema = options?.failure ?? Schema.Never
988
- const resultSchema = Schema.EitherFromUnion({ right: successSchema, left: failureSchema })
1149
+ const resultSchema = Schema.Either({
1150
+ right: successSchema,
1151
+ left: failureSchema
1152
+ })
989
1153
  return providerDefinedProto({
990
1154
  id: options.id,
991
1155
  name: options.toolkitName,
@@ -996,8 +1160,10 @@ export const providerDefined = <
996
1160
  parametersSchema: options?.parameters
997
1161
  ? Schema.Struct(options?.parameters as any)
998
1162
  : constEmptyStruct,
999
- successSchema: options.requiresHandler === true ? successSchema : resultSchema,
1000
- failureSchema: options.requiresHandler === true ? failureSchema : Schema.Never
1163
+ successSchema,
1164
+ failureSchema,
1165
+ resultSchema,
1166
+ failureMode: failureMode ?? "error"
1001
1167
  }) as any
1002
1168
  }
1003
1169
 
@@ -1041,10 +1207,17 @@ export const fromTaggedRequest = <S extends AnyTaggedRequestSchema>(
1041
1207
  ): FromTaggedRequest<S> =>
1042
1208
  userDefinedProto({
1043
1209
  name: schema._tag,
1044
- description: Option.getOrUndefined(AST.getDescriptionAnnotation((schema.ast as any).to)),
1210
+ description: Option.getOrUndefined(
1211
+ AST.getDescriptionAnnotation((schema.ast as any).to)
1212
+ ),
1045
1213
  parametersSchema: schema,
1046
1214
  successSchema: schema.success,
1047
1215
  failureSchema: schema.failure,
1216
+ resultSchema: Schema.Either({
1217
+ left: schema.failure,
1218
+ right: schema.success
1219
+ }),
1220
+ failureMode: "error",
1048
1221
  annotations: Context.empty()
1049
1222
  }) as any
1050
1223
 
@@ -1079,6 +1252,7 @@ export const getDescription = <
1079
1252
  readonly parameters: AnyStructSchema
1080
1253
  readonly success: Schema.Schema.Any
1081
1254
  readonly failure: Schema.Schema.All
1255
+ readonly failureMode: FailureMode
1082
1256
  }
1083
1257
  >(
1084
1258
  /**
@@ -1096,16 +1270,18 @@ export const getDescription = <
1096
1270
  * @since 1.0.0
1097
1271
  * @category Utilities
1098
1272
  */
1099
- export const getDescriptionFromSchemaAst = (ast: AST.AST): string | undefined => {
1100
- const annotations = ast._tag === "Transformation" ?
1101
- {
1273
+ export const getDescriptionFromSchemaAst = (
1274
+ ast: AST.AST
1275
+ ): string | undefined => {
1276
+ const annotations = ast._tag === "Transformation"
1277
+ ? {
1102
1278
  ...ast.to.annotations,
1103
1279
  ...ast.annotations
1104
- } :
1105
- ast.annotations
1280
+ }
1281
+ : ast.annotations
1106
1282
  return AST.DescriptionAnnotationId in annotations
1107
- ? annotations[AST.DescriptionAnnotationId] as string :
1108
- undefined
1283
+ ? (annotations[AST.DescriptionAnnotationId] as string)
1284
+ : undefined
1109
1285
  }
1110
1286
 
1111
1287
  /**
@@ -1148,14 +1324,19 @@ export const getJsonSchema = <
1148
1324
  readonly parameters: AnyStructSchema
1149
1325
  readonly success: Schema.Schema.Any
1150
1326
  readonly failure: Schema.Schema.All
1327
+ readonly failureMode: FailureMode
1151
1328
  }
1152
- >(tool: Tool<Name, Config>): JsonSchema.JsonSchema7 => getJsonSchemaFromSchemaAst(tool.parametersSchema.ast)
1329
+ >(
1330
+ tool: Tool<Name, Config>
1331
+ ): JsonSchema.JsonSchema7 => getJsonSchemaFromSchemaAst(tool.parametersSchema.ast)
1153
1332
 
1154
1333
  /**
1155
1334
  * @since 1.0.0
1156
1335
  * @category Utilities
1157
1336
  */
1158
- export const getJsonSchemaFromSchemaAst = (ast: AST.AST): JsonSchema.JsonSchema7 => {
1337
+ export const getJsonSchemaFromSchemaAst = (
1338
+ ast: AST.AST
1339
+ ): JsonSchema.JsonSchema7 => {
1159
1340
  const props = AST.getPropertySignatures(ast)
1160
1341
  if (props.length === 0) {
1161
1342
  return {
@@ -1193,7 +1374,10 @@ export const getJsonSchemaFromSchemaAst = (ast: AST.AST): JsonSchema.JsonSchema7
1193
1374
  * @since 1.0.0
1194
1375
  * @category Annotations
1195
1376
  */
1196
- export class Title extends Context.Tag("@effect/ai/Tool/Title")<Title, string>() {}
1377
+ export class Title extends Context.Tag("@effect/ai/Tool/Title")<
1378
+ Title,
1379
+ string
1380
+ >() {}
1197
1381
 
1198
1382
  /**
1199
1383
  * Annotation indicating whether a tool only reads data without making changes.
@@ -1209,9 +1393,12 @@ export class Title extends Context.Tag("@effect/ai/Tool/Title")<Title, string>()
1209
1393
  * @since 1.0.0
1210
1394
  * @category Annotations
1211
1395
  */
1212
- export class Readonly extends Context.Reference<Readonly>()("@effect/ai/Tool/Readonly", {
1213
- defaultValue: constFalse
1214
- }) {}
1396
+ export class Readonly extends Context.Reference<Readonly>()(
1397
+ "@effect/ai/Tool/Readonly",
1398
+ {
1399
+ defaultValue: constFalse
1400
+ }
1401
+ ) {}
1215
1402
 
1216
1403
  /**
1217
1404
  * Annotation indicating whether a tool performs destructive operations.
@@ -1227,9 +1414,12 @@ export class Readonly extends Context.Reference<Readonly>()("@effect/ai/Tool/Rea
1227
1414
  * @since 1.0.0
1228
1415
  * @category Annotations
1229
1416
  */
1230
- export class Destructive extends Context.Reference<Destructive>()("@effect/ai/Tool/Destructive", {
1231
- defaultValue: constTrue
1232
- }) {}
1417
+ export class Destructive extends Context.Reference<Destructive>()(
1418
+ "@effect/ai/Tool/Destructive",
1419
+ {
1420
+ defaultValue: constTrue
1421
+ }
1422
+ ) {}
1233
1423
 
1234
1424
  /**
1235
1425
  * Annotation indicating whether a tool can be called multiple times safely.
@@ -1245,9 +1435,12 @@ export class Destructive extends Context.Reference<Destructive>()("@effect/ai/To
1245
1435
  * @since 1.0.0
1246
1436
  * @category Annotations
1247
1437
  */
1248
- export class Idempotent extends Context.Reference<Idempotent>()("@effect/ai/Tool/Idempotent", {
1249
- defaultValue: constFalse
1250
- }) {}
1438
+ export class Idempotent extends Context.Reference<Idempotent>()(
1439
+ "@effect/ai/Tool/Idempotent",
1440
+ {
1441
+ defaultValue: constFalse
1442
+ }
1443
+ ) {}
1251
1444
 
1252
1445
  /**
1253
1446
  * Annotation indicating whether a tool can handle arbitrary external data.
@@ -1263,9 +1456,12 @@ export class Idempotent extends Context.Reference<Idempotent>()("@effect/ai/Tool
1263
1456
  * @since 1.0.0
1264
1457
  * @category Annotations
1265
1458
  */
1266
- export class OpenWorld extends Context.Reference<OpenWorld>()("@effect/ai/Tool/OpenWorld", {
1267
- defaultValue: constTrue
1268
- }) {}
1459
+ export class OpenWorld extends Context.Reference<OpenWorld>()(
1460
+ "@effect/ai/Tool/OpenWorld",
1461
+ {
1462
+ defaultValue: constTrue
1463
+ }
1464
+ ) {}
1269
1465
 
1270
1466
  // Licensed under BSD-3-Clause (below code only)
1271
1467
  // Code adapted from https://github.com/fastify/secure-json-parse/blob/783fcb1b5434709466759847cec974381939673a/index.js