@effect/ai 0.30.0 → 0.31.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.
@@ -40,7 +40,6 @@
40
40
  import * as Context from "effect/Context";
41
41
  import * as Effect from "effect/Effect";
42
42
  import { CommitPrototype } from "effect/Effectable";
43
- import * as Either from "effect/Either";
44
43
  import { identity } from "effect/Function";
45
44
  import { BaseProto as InspectableProto } from "effect/Inspectable";
46
45
  import * as Layer from "effect/Layer";
@@ -89,8 +88,9 @@ const Proto = {
89
88
  if (Predicate.isUndefined(schemas)) {
90
89
  const handler = context.unsafeMap.get(tool.id);
91
90
  const decodeParameters = Schema.decodeUnknown(tool.parametersSchema);
92
- const validateResult = Schema.validate(tool.resultSchema);
93
- const encodeResult = Schema.encodeUnknown(tool.resultSchema);
91
+ const resultSchema = Schema.Union(tool.successSchema, tool.failureSchema);
92
+ const validateResult = Schema.validate(resultSchema);
93
+ const encodeResult = Schema.encodeUnknown(resultSchema);
94
94
  schemas = {
95
95
  context: handler.context,
96
96
  handler: handler.handler,
@@ -125,10 +125,21 @@ const Proto = {
125
125
  description: `Failed to decode tool call parameters for tool '${name}' from:\n'${JSON.stringify(params, undefined, 2)}'`,
126
126
  cause
127
127
  }));
128
- const result = yield* schemas.handler(decodedParams).pipe(Effect.matchEffect({
129
- onFailure: error => tool.failureMode === "error" ? Effect.fail(error) : Effect.succeed(Either.left(error)),
130
- onSuccess: value => Effect.succeed(Either.right(value))
131
- }), Effect.flatMap(either => schemas.validateResult(either)), Effect.mapInputContext(input => Context.merge(schemas.context, input)), Effect.mapError(cause => ParseResult.isParseError(cause) ? new AiError.MalformedInput({
128
+ const {
129
+ isFailure,
130
+ result
131
+ } = yield* schemas.handler(decodedParams).pipe(Effect.map(result => ({
132
+ result,
133
+ isFailure: false
134
+ })), Effect.catchAll(error =>
135
+ // If the tool handler failed, check the tool's failure mode to
136
+ // determine how the result should be returned to the end user
137
+ tool.failureMode === "error" ? Effect.fail(error) : Effect.succeed({
138
+ result: error,
139
+ isFailure: true
140
+ })), Effect.tap(({
141
+ result
142
+ }) => schemas.validateResult(result)), Effect.mapInputContext(input => Context.merge(schemas.context, input)), Effect.mapError(cause => ParseResult.isParseError(cause) ? new AiError.MalformedInput({
132
143
  module: "Toolkit",
133
144
  method: `${name}.handle`,
134
145
  description: `Failed to validate tool call result for tool '${name}'`,
@@ -141,13 +152,14 @@ const Proto = {
141
152
  cause
142
153
  }));
143
154
  return {
155
+ isFailure,
144
156
  result,
145
157
  encodedResult
146
158
  };
147
159
  });
148
160
  return {
149
161
  tools,
150
- handle: handle
162
+ handle
151
163
  };
152
164
  });
153
165
  },
@@ -1 +1 @@
1
- {"version":3,"file":"Toolkit.js","names":["Context","Effect","CommitPrototype","Either","identity","BaseProto","InspectableProto","Layer","ParseResult","pipeArguments","Predicate","Schema","AiError","Tool","TypeId","Proto","of","toContext","build","gen","context","handlers","isEffect","contextMap","Map","name","handler","Object","entries","tool","tools","set","id","unsafeMake","toLayer","scopedContext","commit","schemasCache","WeakMap","getSchemas","schemas","get","isUndefined","unsafeMap","decodeParameters","decodeUnknown","parametersSchema","validateResult","validate","resultSchema","encodeResult","encodeUnknown","handle","fn","captureStackTrace","params","annotateCurrentSpan","parameters","toolNames","keys","join","MalformedOutput","module","method","description","decodedParams","mapError","cause","JSON","stringify","undefined","result","pipe","matchEffect","onFailure","error","failureMode","fail","succeed","left","onSuccess","value","right","flatMap","either","mapInputContext","input","merge","isParseError","MalformedInput","encodedResult","toJSON","_id","Array","from","values","map","arguments","makeProto","assign","resolveInput","output","isSchema","fromTaggedRequest","empty","make","toolkits","toolkit"],"sources":["../../src/Toolkit.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,OAAO,KAAKA,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC,eAAe,QAAQ,mBAAmB;AACn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"version":3,"file":"Toolkit.js","names":["Context","Effect","CommitPrototype","identity","BaseProto","InspectableProto","Layer","ParseResult","pipeArguments","Predicate","Schema","AiError","Tool","TypeId","Proto","of","toContext","build","gen","context","handlers","isEffect","contextMap","Map","name","handler","Object","entries","tool","tools","set","id","unsafeMake","toLayer","scopedContext","commit","schemasCache","WeakMap","getSchemas","schemas","get","isUndefined","unsafeMap","decodeParameters","decodeUnknown","parametersSchema","resultSchema","Union","successSchema","failureSchema","validateResult","validate","encodeResult","encodeUnknown","handle","fn","captureStackTrace","params","annotateCurrentSpan","parameters","toolNames","keys","join","MalformedOutput","module","method","description","decodedParams","mapError","cause","JSON","stringify","undefined","isFailure","result","pipe","map","catchAll","error","failureMode","fail","succeed","tap","mapInputContext","input","merge","isParseError","MalformedInput","encodedResult","toJSON","_id","Array","from","values","arguments","makeProto","assign","resolveInput","output","value","isSchema","fromTaggedRequest","empty","make","toolkits","toolkit"],"sources":["../../src/Toolkit.ts"],"sourcesContent":[null],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCA,OAAO,KAAKA,OAAO,MAAM,gBAAgB;AACzC,OAAO,KAAKC,MAAM,MAAM,eAAe;AACvC,SAASC,eAAe,QAAQ,mBAAmB;AACnD,SAASC,QAAQ,QAAQ,iBAAiB;AAE1C,SAASC,SAAS,IAAIC,gBAAgB,QAAQ,oBAAoB;AAClE,OAAO,KAAKC,KAAK,MAAM,cAAc;AAErC,OAAO,KAAKC,WAAW,MAAM,oBAAoB;AACjD,SAAwBC,aAAa,QAAQ,iBAAiB;AAC9D,OAAO,KAAKC,SAAS,MAAM,kBAAkB;AAC7C,OAAO,KAAKC,MAAM,MAAM,eAAe;AAEvC,OAAO,KAAKC,OAAO,MAAM,cAAc;AACvC,OAAO,KAAKC,IAAI,MAAM,WAAW;AAEjC;;;;;;AAMA,OAAO,MAAMC,MAAM,GAAG,qBAAqB;AA8K3C,MAAMC,KAAK,GAAG;EACZ,GAAGZ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M,CAAC0F,QAAQ,CAACxE,IAAI,CAAC,GAAGhB,IAAI,CAACyF,iBAAiB,CAACzE,IAAW,CAAC,GAAGA,IAAY;IACzFsE,MAAM,CAACtE,IAAI,CAACJ,IAAI,CAAC,GAAG2E,KAAK;EAC3B;EACA,OAAOD,MAAM;AACf,CAAC;AAED;;;;;;;;;AASA,OAAO,MAAMI,KAAK,gBAAgBP,SAAS,CAAC,EAAE,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCA,OAAO,MAAMQ,IAAI,GAAGA,CAClB,GAAG1E,KAAY,KACiBkE,SAAS,CAACE,YAAY,CAAC,GAAGpE,KAAK,CAAC,CAAQ;AAkC1E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,OAAO,MAAMwD,KAAK,GAAGA;AACnB;;;AAGA,GAAGmB,QAAkB,KACa;EAClC,MAAM3E,KAAK,GAAG,EAAyB;EACvC,KAAK,MAAM4E,OAAO,IAAID,QAAQ,EAAE;IAC9B,KAAK,MAAM,CAAChF,IAAI,EAAEI,IAAI,CAAC,IAAIF,MAAM,CAACC,OAAO,CAAC8E,OAAO,CAAC5E,KAAK,CAAC,EAAE;MACxDA,KAAK,CAACL,IAAI,CAAC,GAAGI,IAAI;IACpB;EACF;EACA,OAAOmE,SAAS,CAAClE,KAAK,CAAQ;AAChC,CAAC","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect/ai",
3
- "version": "0.30.0",
3
+ "version": "0.31.1",
4
4
  "description": "Effect modules for working with AI apis",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -15,9 +15,9 @@
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@effect/experimental": "^0.56.0",
18
- "@effect/platform": "^0.92.1",
19
18
  "@effect/rpc": "^0.71.0",
20
- "effect": "^3.18.4"
19
+ "effect": "^3.18.4",
20
+ "@effect/platform": "^0.92.1"
21
21
  },
22
22
  "publishConfig": {
23
23
  "provenance": true
@@ -51,10 +51,12 @@
51
51
  import * as Chunk from "effect/Chunk"
52
52
  import * as Context from "effect/Context"
53
53
  import * as Effect from "effect/Effect"
54
+ import * as Mailbox from "effect/Mailbox"
54
55
  import * as Option from "effect/Option"
55
56
  import * as ParseResult from "effect/ParseResult"
56
57
  import * as Predicate from "effect/Predicate"
57
58
  import * as Schema from "effect/Schema"
59
+ import type * as Scope from "effect/Scope"
58
60
  import * as Stream from "effect/Stream"
59
61
  import type { Span } from "effect/Tracer"
60
62
  import type { Concurrency, Mutable, NoExcessProperties } from "effect/Types"
@@ -797,7 +799,9 @@ export const make: (params: ConstructorParams) => Effect.Effect<Service> = Effec
797
799
  >(options: Options & GenerateTextOptions<Tools>, providerOptions: Mutable<ProviderOptions>) => Effect.Effect<
798
800
  Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator>,
799
801
  Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _E : never,
800
- Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _R : never
802
+ | (Options extends { readonly toolkit: Effect.Effect<Toolkit.WithHandler<Tools>, infer _E, infer _R> } ? _R
803
+ : never)
804
+ | Scope.Scope
801
805
  > = Effect.fnUntraced(
802
806
  function*<
803
807
  Tools extends Record<string, Tool.Any>,
@@ -842,16 +846,21 @@ export const make: (params: ConstructorParams) => Effect.Effect<Service> = Effec
842
846
  ) as Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator>
843
847
  }
844
848
 
845
- const ResponseSchema = Schema.Chunk(Response.StreamPart(toolkit))
849
+ const mailbox = yield* Mailbox.make<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError>()
850
+ const ResponseSchema = Schema.Array(Response.StreamPart(toolkit))
846
851
  const decode = Schema.decode(ResponseSchema)
847
- return params.streamText(providerOptions).pipe(
848
- Stream.mapChunksEffect(Effect.fnUntraced(function*(chunk) {
852
+ yield* params.streamText(providerOptions).pipe(
853
+ Stream.runForEachChunk(Effect.fnUntraced(function*(chunk) {
849
854
  const rawContent = Chunk.toArray(chunk)
850
- const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency)
851
855
  const content = yield* decode(rawContent)
852
- return Chunk.unsafeFromArray([...content, ...toolResults])
853
- }))
854
- ) as Stream.Stream<Response.StreamPart<Tools>, AiError.AiError | ParseResult.ParseError, IdGenerator>
856
+ yield* mailbox.offerAll(content)
857
+ const toolResults = yield* resolveToolCalls(rawContent, toolkit, options.concurrency)
858
+ yield* mailbox.offerAll(toolResults as any)
859
+ })),
860
+ Mailbox.into(mailbox),
861
+ Effect.forkScoped
862
+ )
863
+ return Mailbox.toStream(mailbox)
855
864
  }
856
865
  )
857
866
 
@@ -1010,12 +1019,13 @@ const resolveToolCalls = <Tools extends Record<string, Tool.Any>>(
1010
1019
 
1011
1020
  return Effect.forEach(toolCalls, (toolCall) => {
1012
1021
  return toolkit.handle(toolCall.name, toolCall.params as any).pipe(
1013
- Effect.map(({ encodedResult, result }) =>
1022
+ Effect.map(({ encodedResult, isFailure, result }) =>
1014
1023
  Response.makePart("tool-result", {
1015
1024
  id: toolCall.id,
1016
1025
  name: toolCall.name,
1017
1026
  result,
1018
1027
  encodedResult,
1028
+ isFailure,
1019
1029
  providerExecuted: false,
1020
1030
  ...(toolCall.providerName !== undefined
1021
1031
  ? { providerName: toolCall.providerName }
package/src/Prompt.ts CHANGED
@@ -606,13 +606,11 @@ export const toolCallPart = (params: PartConstructorParams<ToolCallPart>): ToolC
606
606
  * const toolResultPart: Prompt.ToolResultPart = Prompt.makePart("tool-result", {
607
607
  * id: "call_123",
608
608
  * name: "get_weather",
609
+ * isFailure: false,
609
610
  * result: {
610
- * _tag: "Right",
611
- * right: {
612
- * temperature: 22,
613
- * condition: "sunny",
614
- * humidity: 65
615
- * }
611
+ * temperature: 22,
612
+ * condition: "sunny",
613
+ * humidity: 65
616
614
  * }
617
615
  * })
618
616
  * ```
@@ -629,10 +627,14 @@ export interface ToolResultPart extends BasePart<"tool-result", ToolResultPartOp
629
627
  * Name of the tool that was executed.
630
628
  */
631
629
  readonly name: string
630
+ /**
631
+ * Whether or not the result of executing the tool call handler was an error.
632
+ */
633
+ readonly isFailure: boolean
632
634
  /**
633
635
  * The result returned by the tool execution.
634
636
  */
635
- readonly result: Schema.EitherEncoded<unknown, unknown>
637
+ readonly result: unknown
636
638
  }
637
639
 
638
640
  /**
@@ -650,10 +652,14 @@ export interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", To
650
652
  * Name of the tool that was executed.
651
653
  */
652
654
  readonly name: string
655
+ /**
656
+ * Whether or not the result of executing the tool call handler was an error.
657
+ */
658
+ readonly isFailure: boolean
653
659
  /**
654
660
  * The result returned by the tool execution.
655
661
  */
656
- readonly result: Schema.EitherEncoded<unknown, unknown>
662
+ readonly result: unknown
657
663
  }
658
664
 
659
665
  /**
@@ -675,10 +681,8 @@ export const ToolResultPart: Schema.Schema<ToolResultPart, ToolResultPartEncoded
675
681
  type: Schema.Literal("tool-result"),
676
682
  id: Schema.String,
677
683
  name: Schema.String,
678
- result: Schema.encodedSchema(Schema.Either({
679
- left: Schema.Unknown,
680
- right: Schema.Unknown
681
- })),
684
+ isFailure: Schema.Boolean,
685
+ result: Schema.Unknown,
682
686
  options: Schema.optionalWith(ProviderOptions, { default: constEmptyObject })
683
687
  }).pipe(
684
688
  Schema.attachPropertySignature(PartTypeId, PartTypeId),
@@ -1028,9 +1032,10 @@ export const userMessage = (params: MessageConstructorParams<UserMessage>): User
1028
1032
  * Prompt.makePart("tool-result", {
1029
1033
  * id: "call_123",
1030
1034
  * name: "get_weather",
1035
+ * isFailure: false,
1031
1036
  * result: {
1032
- * _tag: "Right",
1033
- * right: { temperature: 72, condition: "sunny" }
1037
+ * temperature: 72,
1038
+ * condition: "sunny"
1034
1039
  * }
1035
1040
  * }),
1036
1041
  * Prompt.makePart("text", {
@@ -1138,15 +1143,13 @@ export const assistantMessage = (params: MessageConstructorParams<AssistantMessa
1138
1143
  * Prompt.makePart("tool-result", {
1139
1144
  * id: "call_123",
1140
1145
  * name: "search_web",
1146
+ * isFailure: false,
1141
1147
  * result: {
1142
- * _tag: "Right",
1143
- * right: {
1144
- * query: "TypeScript best practices",
1145
- * results: [
1146
- * { title: "TypeScript Handbook", url: "https://..." },
1147
- * { title: "Effective TypeScript", url: "https://..." }
1148
- * ]
1149
- * }
1148
+ * query: "TypeScript best practices",
1149
+ * results: [
1150
+ * { title: "TypeScript Handbook", url: "https://..." },
1151
+ * { title: "Effective TypeScript", url: "https://..." }
1152
+ * ]
1150
1153
  * }
1151
1154
  * })
1152
1155
  * ]
@@ -1564,8 +1567,9 @@ const isValidPart = (part: Response.AnyPart): part is ValidResponsePart => {
1564
1567
  * Response.makePart("tool-result", {
1565
1568
  * id: "call_1",
1566
1569
  * name: "get_time",
1567
- * result: Either.right("10:30 AM"),
1568
- * encodedResult: { _tag: "Right", right: "10:30 AM" },
1570
+ * isFailure: false,
1571
+ * result: "10:30 AM",
1572
+ * encodedResult: "10:30 AM",
1569
1573
  * providerExecuted: false
1570
1574
  * })
1571
1575
  * ]
@@ -1650,6 +1654,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1650
1654
  toolParts.push(makePart("tool-result", {
1651
1655
  id: part.id,
1652
1656
  name: part.providerName ?? part.name,
1657
+ isFailure: part.isFailure,
1653
1658
  result: part.encodedResult
1654
1659
  }))
1655
1660
  break
package/src/Response.ts CHANGED
@@ -28,7 +28,6 @@
28
28
  */
29
29
  import type * as DateTime from "effect/DateTime"
30
30
  import * as Effect from "effect/Effect"
31
- import type * as Either from "effect/Either"
32
31
  import { constFalse } from "effect/Function"
33
32
  import type * as Option from "effect/Option"
34
33
  import * as ParseResult from "effect/ParseResult"
@@ -1506,6 +1505,75 @@ export const toolCallPart = <const Name extends string, Params>(
1506
1505
  // Tool Call Result Part
1507
1506
  // =============================================================================
1508
1507
 
1508
+ /**
1509
+ * The base fields of a tool result part.
1510
+ *
1511
+ * @since 1.0.0
1512
+ * @category Models
1513
+ */
1514
+ export interface BaseToolResult<Name extends string> extends BasePart<"tool-result", ToolResultPartMetadata> {
1515
+ /**
1516
+ * Unique identifier matching the original tool call.
1517
+ */
1518
+ readonly id: string
1519
+ /**
1520
+ * Name of the tool being called, which corresponds to the name of the tool
1521
+ * in the `Toolkit` included with the request.
1522
+ */
1523
+ readonly name: Name
1524
+ /**
1525
+ * The encoded result for serialization purposes.
1526
+ */
1527
+ readonly encodedResult: unknown
1528
+ /**
1529
+ * Optional provider-specific name for the tool, which can be useful when the
1530
+ * name of the tool in the `Toolkit` and the name of the tool used by the
1531
+ * model are different.
1532
+ *
1533
+ * This is usually happens only with provider-defined tools which require a
1534
+ * user-space handler.
1535
+ */
1536
+ readonly providerName?: string | undefined
1537
+ /**
1538
+ * Whether the tool was executed by the provider (true) or framework (false).
1539
+ */
1540
+ readonly providerExecuted: boolean
1541
+ }
1542
+
1543
+ /**
1544
+ * Represents a successful tool call result.
1545
+ *
1546
+ * @since 1.0.0
1547
+ * @category Models
1548
+ */
1549
+ export interface ToolResultSuccess<Name extends string, Success> extends BaseToolResult<Name> {
1550
+ /**
1551
+ * The decoded success returned by the tool execution.
1552
+ */
1553
+ readonly result: Success
1554
+ /**
1555
+ * Whether or not the result of executing the tool call handler was an error.
1556
+ */
1557
+ readonly isFailure: false
1558
+ }
1559
+
1560
+ /**
1561
+ * Represents a failed tool call result.
1562
+ *
1563
+ * @since 1.0.0
1564
+ * @category Models
1565
+ */
1566
+ export interface ToolResultFailure<Name extends string, Failure> extends BaseToolResult<Name> {
1567
+ /**
1568
+ * The decoded failure returned by the tool execution.
1569
+ */
1570
+ readonly result: Failure
1571
+ /**
1572
+ * Whether or not the result of executing the tool call handler was an error.
1573
+ */
1574
+ readonly isFailure: true
1575
+ }
1576
+
1509
1577
  /**
1510
1578
  * Response part representing the result of a tool call.
1511
1579
  *
@@ -1527,18 +1595,16 @@ export const toolCallPart = <const Name extends string, Params>(
1527
1595
  * > = Response.toolResultPart({
1528
1596
  * id: "call_123",
1529
1597
  * name: "get_weather",
1530
- * result: Either.right({
1598
+ * isFailure: false,
1599
+ * result: {
1531
1600
  * temperature: 22,
1532
1601
  * condition: "sunny",
1533
1602
  * humidity: 65
1534
- * }),
1603
+ * },
1535
1604
  * encodedResult: {
1536
- * _tag: "Right",
1537
- * right: {
1538
- * temperature: 22,
1539
- * condition: "sunny",
1540
- * humidity: 65
1541
- * }
1605
+ * temperature: 22,
1606
+ * condition: "sunny",
1607
+ * humidity: 65
1542
1608
  * },
1543
1609
  * providerExecuted: false
1544
1610
  * })
@@ -1547,40 +1613,9 @@ export const toolCallPart = <const Name extends string, Params>(
1547
1613
  * @since 1.0.0
1548
1614
  * @category Models
1549
1615
  */
1550
- export interface ToolResultPart<Name extends string, Success, Failure>
1551
- extends BasePart<"tool-result", ToolResultPartMetadata>
1552
- {
1553
- /**
1554
- * Unique identifier matching the original tool call.
1555
- */
1556
- readonly id: string
1557
- /**
1558
- * Name of the tool being called, which corresponds to the name of the tool
1559
- * in the `Toolkit` included with the request.
1560
- */
1561
- readonly name: Name
1562
- /**
1563
- * The decoded result returned by the tool execution.
1564
- */
1565
- readonly result: Either.Either<Success, Failure>
1566
- /**
1567
- * The encoded result for serialization purposes.
1568
- */
1569
- readonly encodedResult: Schema.EitherEncoded<unknown, unknown>
1570
- /**
1571
- * Optional provider-specific name for the tool, which can be useful when the
1572
- * name of the tool in the `Toolkit` and the name of the tool used by the
1573
- * model are different.
1574
- *
1575
- * This is usually happens only with provider-defined tools which require a
1576
- * user-space handler.
1577
- */
1578
- readonly providerName?: string | undefined
1579
- /**
1580
- * Whether the tool was executed by the provider (true) or framework (false).
1581
- */
1582
- readonly providerExecuted: boolean
1583
- }
1616
+ export type ToolResultPart<Name extends string, Success, Failure> =
1617
+ | ToolResultSuccess<Name, Success>
1618
+ | ToolResultFailure<Name, Failure>
1584
1619
 
1585
1620
  /**
1586
1621
  * Encoded representation of tool result parts for serialization.
@@ -1601,7 +1636,11 @@ export interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", To
1601
1636
  /**
1602
1637
  * The result returned by the tool execution.
1603
1638
  */
1604
- readonly result: Schema.EitherEncoded<unknown, unknown>
1639
+ readonly result: unknown
1640
+ /**
1641
+ * Whether or not the result of executing the tool call handler was an error.
1642
+ */
1643
+ readonly isFailure: boolean
1605
1644
  /**
1606
1645
  * Optional provider-specific name for the tool, which can be useful when the
1607
1646
  * name of the tool in the `Toolkit` and the name of the tool used by the
@@ -1644,15 +1683,13 @@ export const ToolResultPart = <
1644
1683
  ToolResultPart<Name, Schema.Schema.Type<Success>, Schema.Schema.Type<Failure>>,
1645
1684
  ToolResultPartEncoded
1646
1685
  > => {
1647
- const ResultSchema = Schema.Either({
1648
- left: failure,
1649
- right: success
1650
- })
1651
1686
  const Base = Schema.Struct({
1652
1687
  id: Schema.String,
1653
1688
  type: Schema.Literal("tool-result"),
1654
- providerName: Schema.optional(Schema.String)
1689
+ providerName: Schema.optional(Schema.String),
1690
+ isFailure: Schema.Boolean
1655
1691
  })
1692
+ const ResultSchema = Schema.Union(success, failure)
1656
1693
  const Encoded = Schema.Struct({
1657
1694
  ...Base.fields,
1658
1695
  name: Schema.String,
@@ -1692,11 +1729,9 @@ export const ToolResultPart = <
1692
1729
  encode: Effect.fnUntraced(function*(decoded) {
1693
1730
  const encoded = yield* encodeResult(decoded.result)
1694
1731
  return {
1695
- id: decoded.id,
1696
- type: decoded.type,
1697
- name: decoded.name,
1732
+ ...decoded,
1698
1733
  result: encoded,
1699
- ...(decoded.metadata ? { metadata: decoded.metadata } : {}),
1734
+ ...(decoded.metadata ?? {}),
1700
1735
  ...(decoded.providerName ? { providerName: decoded.providerName } : {}),
1701
1736
  ...(decoded.providerExecuted ? { providerExecuted: true } : {})
1702
1737
  }
@@ -1711,9 +1746,21 @@ export const ToolResultPart = <
1711
1746
  * @since 1.0.0
1712
1747
  * @category Constructors
1713
1748
  */
1714
- export const toolResultPart = <const Name extends string, Success, Failure>(
1715
- params: ConstructorParams<ToolResultPart<Name, Success, Failure>>
1716
- ): ToolResultPart<Name, Success, Failure> => makePart("tool-result", params)
1749
+ export const toolResultPart = <
1750
+ const Params extends ConstructorParams<ToolResultPart<string, any, any>>
1751
+ >(
1752
+ params: Params
1753
+ ): Params extends {
1754
+ readonly name: infer Name extends string
1755
+ readonly isFailure: false
1756
+ readonly result: infer Success
1757
+ } ? ToolResultPart<Name, Success, never>
1758
+ : Params extends {
1759
+ readonly name: infer Name extends string
1760
+ readonly isFailure: true
1761
+ readonly result: infer Failure
1762
+ } ? ToolResultPart<Name, never, Failure>
1763
+ : never => makePart("tool-result", params) as any
1717
1764
 
1718
1765
  // =============================================================================
1719
1766
  // File Part