@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/src/Response.ts CHANGED
@@ -26,11 +26,11 @@
26
26
  *
27
27
  * @since 1.0.0
28
28
  */
29
- import { ParseResult } from "effect"
30
29
  import type * as DateTime from "effect/DateTime"
31
30
  import * as Effect from "effect/Effect"
32
31
  import { constFalse } from "effect/Function"
33
32
  import type * as Option from "effect/Option"
33
+ import * as ParseResult from "effect/ParseResult"
34
34
  import * as Predicate from "effect/Predicate"
35
35
  import * as Schema from "effect/Schema"
36
36
  import type * as Tool from "./Tool.js"
@@ -85,7 +85,7 @@ export type AnyPart =
85
85
  | ToolParamsDeltaPart
86
86
  | ToolParamsEndPart
87
87
  | ToolCallPart<any, any>
88
- | ToolResultPart<any, any>
88
+ | ToolResultPart<any, any, any>
89
89
  | FilePart
90
90
  | DocumentSourcePart
91
91
  | UrlSourcePart
@@ -202,10 +202,10 @@ export const AllParts = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
202
202
  toolkit: T
203
203
  ): Schema.Schema<AllParts<Toolkit.Tools<T>>, AllPartsEncoded> => {
204
204
  const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
205
- const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
205
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
206
206
  for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
207
207
  toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
208
- toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
208
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema, tool.failureSchema))
209
209
  }
210
210
  return Schema.Union(
211
211
  TextPart,
@@ -283,10 +283,10 @@ export const Part = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
283
283
  toolkit: T
284
284
  ): Schema.Schema<Part<Toolkit.Tools<T>>, PartEncoded> => {
285
285
  const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
286
- const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
286
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
287
287
  for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
288
288
  toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
289
- toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
289
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema, tool.failureSchema))
290
290
  }
291
291
  return Schema.Union(
292
292
  TextPart,
@@ -367,10 +367,10 @@ export const StreamPart = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
367
367
  toolkit: T
368
368
  ): Schema.Schema<StreamPart<Toolkit.Tools<T>>, StreamPartEncoded> => {
369
369
  const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
370
- const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
370
+ const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any, any>, ToolResultPartEncoded>> = []
371
371
  for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
372
372
  toolCalls.push(ToolCallPart(tool.name, tool.parametersSchema as any))
373
- toolCallResults.push(ToolResultPart(tool.name, tool.successSchema))
373
+ toolCallResults.push(ToolResultPart(tool.name, tool.successSchema, tool.failureSchema))
374
374
  }
375
375
  return Schema.Union(
376
376
  TextStartPart,
@@ -406,7 +406,8 @@ export const StreamPart = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
406
406
  * @category Utility Types
407
407
  */
408
408
  export type ToolCallParts<Tools extends Record<string, Tool.Any>> = {
409
- [Name in keyof Tools]: Name extends string ? ToolCallPart<Name, Tool.ParametersSchema<Tools[Name]>["fields"]>
409
+ [Name in keyof Tools]: Name extends string ?
410
+ ToolCallPart<Name, Schema.Struct.Type<Tool.ParametersSchema<Tools[Name]>["fields"]>>
410
411
  : never
411
412
  }[keyof Tools]
412
413
 
@@ -419,7 +420,11 @@ export type ToolCallParts<Tools extends Record<string, Tool.Any>> = {
419
420
  * @category Utility Types
420
421
  */
421
422
  export type ToolResultParts<Tools extends Record<string, Tool.Any>> = {
422
- [Name in keyof Tools]: Name extends string ? ToolResultPart<Name, Tool.Success<Tools[Name]>>
423
+ [Name in keyof Tools]: Name extends string ? ToolResultPart<
424
+ Name,
425
+ Tool.Success<Tools[Name]>,
426
+ Tool.Failure<Tools[Name]>
427
+ >
423
428
  : never
424
429
  }[keyof Tools]
425
430
 
@@ -542,6 +547,20 @@ export const makePart = <const Type extends AnyPart["type"]>(
542
547
  metadata: params.metadata ?? {}
543
548
  }) as any)
544
549
 
550
+ /**
551
+ * A utility type for specifying the parameters required to construct a
552
+ * specific response part.
553
+ *
554
+ * @since 1.0.0
555
+ * @category Utility Types
556
+ */
557
+ export type ConstructorParams<Part extends AnyPart> = Omit<Part, PartTypeId | "type" | "sourceType" | "metadata"> & {
558
+ /**
559
+ * Optional provider-specific metadata for this part.
560
+ */
561
+ readonly metadata?: Part["metadata"] | undefined
562
+ }
563
+
545
564
  // =============================================================================
546
565
  // Text Part
547
566
  // =============================================================================
@@ -605,6 +624,14 @@ export const TextPart: Schema.Schema<TextPart, TextPartEncoded> = Schema.Struct(
605
624
  Schema.annotations({ identifier: "TextPart" })
606
625
  )
607
626
 
627
+ /**
628
+ * Constructs a new text part.
629
+ *
630
+ * @since 1.0.0
631
+ * @category Constructors
632
+ */
633
+ export const textPart = (params: ConstructorParams<TextPart>): TextPart => makePart("text", params)
634
+
608
635
  // =============================================================================
609
636
  // Text Start Part
610
637
  // =============================================================================
@@ -661,6 +688,14 @@ export const TextStartPart: Schema.Schema<TextStartPart, TextStartPartEncoded> =
661
688
  Schema.annotations({ identifier: "TextStartPart" })
662
689
  )
663
690
 
691
+ /**
692
+ * Constructs a new text start part.
693
+ *
694
+ * @since 1.0.0
695
+ * @category Constructors
696
+ */
697
+ export const textStartPart = (params: ConstructorParams<TextStartPart>): TextStartPart => makePart("text-start", params)
698
+
664
699
  // =============================================================================
665
700
  // Text Delta Part
666
701
  // =============================================================================
@@ -725,6 +760,14 @@ export const TextDeltaPart: Schema.Schema<TextDeltaPart, TextDeltaPartEncoded> =
725
760
  Schema.annotations({ identifier: "TextDeltaPart" })
726
761
  )
727
762
 
763
+ /**
764
+ * Constructs a new text delta part.
765
+ *
766
+ * @since 1.0.0
767
+ * @category Constructors
768
+ */
769
+ export const textDeltaPart = (params: ConstructorParams<TextDeltaPart>): TextDeltaPart => makePart("text-delta", params)
770
+
728
771
  // =============================================================================
729
772
  // Text End Part
730
773
  // =============================================================================
@@ -781,6 +824,14 @@ export const TextEndPart: Schema.Schema<TextEndPart, TextEndPartEncoded> = Schem
781
824
  Schema.annotations({ identifier: "TextEndPart" })
782
825
  )
783
826
 
827
+ /**
828
+ * Constructs a new text end part.
829
+ *
830
+ * @since 1.0.0
831
+ * @category Constructors
832
+ */
833
+ export const textEndPart = (params: ConstructorParams<TextEndPart>): TextEndPart => makePart("text-end", params)
834
+
784
835
  // =============================================================================
785
836
  // Reasoning Part
786
837
  // =============================================================================
@@ -847,6 +898,14 @@ export const ReasoningPart: Schema.Schema<ReasoningPart, ReasoningPartEncoded> =
847
898
  Schema.annotations({ identifier: "ReasoningPart" })
848
899
  )
849
900
 
901
+ /**
902
+ * Constructs a new reasoning part.
903
+ *
904
+ * @since 1.0.0
905
+ * @category Constructors
906
+ */
907
+ export const reasoningPart = (params: ConstructorParams<ReasoningPart>): ReasoningPart => makePart("reasoning", params)
908
+
850
909
  // =============================================================================
851
910
  // Reasoning Start Part
852
911
  // =============================================================================
@@ -903,6 +962,15 @@ export const ReasoningStartPart: Schema.Schema<ReasoningStartPart, ReasoningStar
903
962
  Schema.annotations({ identifier: "ReasoningStartPart" })
904
963
  )
905
964
 
965
+ /**
966
+ * Constructs a new reasoning start part.
967
+ *
968
+ * @since 1.0.0
969
+ * @category Constructors
970
+ */
971
+ export const reasoningStartPart = (params: ConstructorParams<ReasoningStartPart>): ReasoningStartPart =>
972
+ makePart("reasoning-start", params)
973
+
906
974
  // =============================================================================
907
975
  // Reasoning Delta Part
908
976
  // =============================================================================
@@ -967,6 +1035,15 @@ export const ReasoningDeltaPart: Schema.Schema<ReasoningDeltaPart, ReasoningDelt
967
1035
  Schema.annotations({ identifier: "ReasoningDeltaPart" })
968
1036
  )
969
1037
 
1038
+ /**
1039
+ * Constructs a new reasoning delta part.
1040
+ *
1041
+ * @since 1.0.0
1042
+ * @category Constructors
1043
+ */
1044
+ export const reasoningDeltaPart = (params: ConstructorParams<ReasoningDeltaPart>): ReasoningDeltaPart =>
1045
+ makePart("reasoning-delta", params)
1046
+
970
1047
  // =============================================================================
971
1048
  // Reasoning End Part
972
1049
  // =============================================================================
@@ -1023,6 +1100,15 @@ export const ReasoningEndPart: Schema.Schema<ReasoningEndPart, ReasoningEndPartE
1023
1100
  Schema.annotations({ identifier: "ReasoningEndPart" })
1024
1101
  )
1025
1102
 
1103
+ /**
1104
+ * Constructs a new reasoning end part.
1105
+ *
1106
+ * @since 1.0.0
1107
+ * @category Constructors
1108
+ */
1109
+ export const reasoningEndPart = (params: ConstructorParams<ReasoningEndPart>): ReasoningEndPart =>
1110
+ makePart("reasoning-end", params)
1111
+
1026
1112
  // =============================================================================
1027
1113
  // Tool Params Start Part
1028
1114
  // =============================================================================
@@ -1119,6 +1205,15 @@ export const ToolParamsStartPart: Schema.Schema<ToolParamsStartPart, ToolParamsS
1119
1205
  Schema.annotations({ identifier: "ToolParamsStartPart" })
1120
1206
  )
1121
1207
 
1208
+ /**
1209
+ * Constructs a new tool params start part.
1210
+ *
1211
+ * @since 1.0.0
1212
+ * @category Constructors
1213
+ */
1214
+ export const toolParamsStartPart = (params: ConstructorParams<ToolParamsStartPart>): ToolParamsStartPart =>
1215
+ makePart("tool-params-start", params)
1216
+
1122
1217
  // =============================================================================
1123
1218
  // Tool Params Delta Part
1124
1219
  // =============================================================================
@@ -1185,6 +1280,15 @@ export const ToolParamsDeltaPart: Schema.Schema<ToolParamsDeltaPart, ToolParamsD
1185
1280
  Schema.annotations({ identifier: "ToolParamsDeltaPart" })
1186
1281
  )
1187
1282
 
1283
+ /**
1284
+ * Constructs a new tool params delta part.
1285
+ *
1286
+ * @since 1.0.0
1287
+ * @category Constructors
1288
+ */
1289
+ export const toolParamsDeltaPart = (params: ConstructorParams<ToolParamsDeltaPart>): ToolParamsDeltaPart =>
1290
+ makePart("tool-params-delta", params)
1291
+
1188
1292
  // =============================================================================
1189
1293
  // Tool Params End Part
1190
1294
  // =============================================================================
@@ -1242,6 +1346,15 @@ export const ToolParamsEndPart: Schema.Schema<ToolParamsEndPart, ToolParamsEndPa
1242
1346
  Schema.annotations({ identifier: "ToolParamsEndPart" })
1243
1347
  )
1244
1348
 
1349
+ /**
1350
+ * Constructs a new tool params end part.
1351
+ *
1352
+ * @since 1.0.0
1353
+ * @category Constructors
1354
+ */
1355
+ export const toolParamsEndPart = (params: ConstructorParams<ToolParamsEndPart>): ToolParamsEndPart =>
1356
+ makePart("tool-params-end", params)
1357
+
1245
1358
  // =============================================================================
1246
1359
  // Tool Call Part
1247
1360
  // =============================================================================
@@ -1276,9 +1389,7 @@ export const ToolParamsEndPart: Schema.Schema<ToolParamsEndPart, ToolParamsEndPa
1276
1389
  * @since 1.0.0
1277
1390
  * @category Models
1278
1391
  */
1279
- export interface ToolCallPart<Name extends string, Params extends Schema.Struct.Fields>
1280
- extends BasePart<"tool-call", ToolCallPartMetadata>
1281
- {
1392
+ export interface ToolCallPart<Name extends string, Params> extends BasePart<"tool-call", ToolCallPartMetadata> {
1282
1393
  /**
1283
1394
  * Unique identifier for this tool call.
1284
1395
  */
@@ -1291,7 +1402,7 @@ export interface ToolCallPart<Name extends string, Params extends Schema.Struct.
1291
1402
  /**
1292
1403
  * Parameters to pass to the tool.
1293
1404
  */
1294
- readonly params: Schema.Struct.Type<Params>
1405
+ readonly params: Params
1295
1406
  /**
1296
1407
  * Optional provider-specific name for the tool, which can be useful when the
1297
1408
  * name of the tool in the `Toolkit` and the name of the tool used by the
@@ -1380,15 +1491,95 @@ export const ToolCallPart = <const Name extends string, Params extends Schema.St
1380
1491
  Schema.annotations({ identifier: "ToolCallPart" })
1381
1492
  ) as any
1382
1493
 
1494
+ /**
1495
+ * Constructs a new tool call part.
1496
+ *
1497
+ * @since 1.0.0
1498
+ * @category Constructors
1499
+ */
1500
+ export const toolCallPart = <const Name extends string, Params>(
1501
+ params: ConstructorParams<ToolCallPart<Name, Params>>
1502
+ ): ToolCallPart<Name, Params> => makePart("tool-call", params)
1503
+
1383
1504
  // =============================================================================
1384
1505
  // Tool Call Result Part
1385
1506
  // =============================================================================
1386
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
+
1387
1577
  /**
1388
1578
  * Response part representing the result of a tool call.
1389
1579
  *
1390
1580
  * @example
1391
1581
  * ```ts
1582
+ * import { Either } from "effect"
1392
1583
  * import { Response } from "@effect/ai"
1393
1584
  *
1394
1585
  * interface WeatherData {
@@ -1399,10 +1590,12 @@ export const ToolCallPart = <const Name extends string, Params extends Schema.St
1399
1590
  *
1400
1591
  * const toolResultPart: Response.ToolResultPart<
1401
1592
  * "get_weather",
1402
- * WeatherData
1403
- * > = Response.makePart("tool-result", {
1593
+ * WeatherData,
1594
+ * never
1595
+ * > = Response.toolResultPart({
1404
1596
  * id: "call_123",
1405
1597
  * name: "get_weather",
1598
+ * isFailure: false,
1406
1599
  * result: {
1407
1600
  * temperature: 22,
1408
1601
  * condition: "sunny",
@@ -1417,44 +1610,12 @@ export const ToolCallPart = <const Name extends string, Params extends Schema.St
1417
1610
  * })
1418
1611
  * ```
1419
1612
  *
1420
- * @template Name - String literal type for the tool name
1421
- * @template Result - Type of the tool result
1422
- *
1423
1613
  * @since 1.0.0
1424
1614
  * @category Models
1425
1615
  */
1426
- export interface ToolResultPart<Name extends string, Result> extends BasePart<"tool-result", ToolResultPartMetadata> {
1427
- /**
1428
- * Unique identifier matching the original tool call.
1429
- */
1430
- readonly id: string
1431
- /**
1432
- * Name of the tool being called, which corresponds to the name of the tool
1433
- * in the `Toolkit` included with the request.
1434
- */
1435
- readonly name: Name
1436
- /**
1437
- * The decoded result returned by the tool execution.
1438
- */
1439
- readonly result: Result
1440
- /**
1441
- * The encoded result for serialization purposes.
1442
- */
1443
- readonly encodedResult: unknown
1444
- /**
1445
- * Optional provider-specific name for the tool, which can be useful when the
1446
- * name of the tool in the `Toolkit` and the name of the tool used by the
1447
- * model are different.
1448
- *
1449
- * This is usually happens only with provider-defined tools which require a
1450
- * user-space handler.
1451
- */
1452
- readonly providerName?: string | undefined
1453
- /**
1454
- * Whether the tool was executed by the provider (true) or framework (false).
1455
- */
1456
- readonly providerExecuted: boolean
1457
- }
1616
+ export type ToolResultPart<Name extends string, Success, Failure> =
1617
+ | ToolResultSuccess<Name, Success>
1618
+ | ToolResultFailure<Name, Failure>
1458
1619
 
1459
1620
  /**
1460
1621
  * Encoded representation of tool result parts for serialization.
@@ -1476,6 +1637,10 @@ export interface ToolResultPartEncoded extends BasePartEncoded<"tool-result", To
1476
1637
  * The result returned by the tool execution.
1477
1638
  */
1478
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
1479
1644
  /**
1480
1645
  * Optional provider-specific name for the tool, which can be useful when the
1481
1646
  * name of the tool in the `Toolkit` and the name of the tool used by the
@@ -1506,28 +1671,29 @@ export interface ToolResultPartMetadata extends ProviderMetadata {}
1506
1671
  * @since 1.0.0
1507
1672
  * @category Schemas
1508
1673
  */
1509
- export const ToolResultPart = <const Name extends string, Result extends Schema.Schema.Any>(
1510
- /**
1511
- * Name of the tool.
1512
- */
1674
+ export const ToolResultPart = <
1675
+ const Name extends string,
1676
+ Success extends Schema.Schema.Any,
1677
+ Failure extends Schema.Schema.All
1678
+ >(
1513
1679
  name: Name,
1514
- /**
1515
- * Schema for the tool result.
1516
- */
1517
- result: Result
1680
+ success: Success,
1681
+ failure: Failure
1518
1682
  ): Schema.Schema<
1519
- ToolResultPart<Name, Schema.Schema.Type<Result>>,
1683
+ ToolResultPart<Name, Schema.Schema.Type<Success>, Schema.Schema.Type<Failure>>,
1520
1684
  ToolResultPartEncoded
1521
1685
  > => {
1522
1686
  const Base = Schema.Struct({
1523
1687
  id: Schema.String,
1524
1688
  type: Schema.Literal("tool-result"),
1525
- providerName: Schema.optional(Schema.String)
1689
+ providerName: Schema.optional(Schema.String),
1690
+ isFailure: Schema.Boolean
1526
1691
  })
1692
+ const ResultSchema = Schema.Union(success, failure)
1527
1693
  const Encoded = Schema.Struct({
1528
1694
  ...Base.fields,
1529
1695
  name: Schema.String,
1530
- result: Schema.encodedSchema(result),
1696
+ result: Schema.encodedSchema(ResultSchema),
1531
1697
  providerExecuted: Schema.optional(Schema.Boolean),
1532
1698
  metadata: Schema.optional(ProviderMetadata)
1533
1699
  })
@@ -1535,47 +1701,67 @@ export const ToolResultPart = <const Name extends string, Result extends Schema.
1535
1701
  ...Base.fields,
1536
1702
  [PartTypeId]: Schema.Literal(PartTypeId),
1537
1703
  name: Schema.Literal(name),
1538
- result: Schema.typeSchema(result),
1539
- encodedResult: Schema.encodedSchema(result),
1704
+ result: Schema.typeSchema(ResultSchema),
1705
+ encodedResult: Schema.encodedSchema(ResultSchema),
1540
1706
  providerExecuted: Schema.Boolean,
1541
1707
  metadata: ProviderMetadata
1542
1708
  })
1543
- const decodeParams = ParseResult.decode<any, any, never>(result as any)
1544
- const encodeParams = ParseResult.encode<any, any, never>(result as any)
1709
+ const decodeResult = ParseResult.decode<any, any, never>(ResultSchema as any)
1710
+ const encodeResult = ParseResult.encode<any, any, never>(ResultSchema as any)
1545
1711
  return Schema.transformOrFail(
1546
1712
  Encoded,
1547
1713
  Decoded,
1548
1714
  {
1549
1715
  strict: true,
1550
1716
  decode: Effect.fnUntraced(function*(encoded) {
1551
- const decoded = yield* decodeParams(encoded.result)
1717
+ const decoded = yield* decodeResult(encoded.result)
1552
1718
  const providerExecuted = encoded.providerExecuted ?? false
1553
1719
  return {
1554
1720
  ...encoded,
1555
1721
  [PartTypeId]: PartTypeId,
1556
1722
  name: encoded.name as Name,
1557
1723
  result: decoded,
1558
- encodedResult: encoded.result,
1724
+ encodedResult: encoded.result as any,
1559
1725
  metadata: encoded.metadata ?? {},
1560
1726
  providerExecuted
1561
1727
  } as const
1562
1728
  }),
1563
1729
  encode: Effect.fnUntraced(function*(decoded) {
1564
- const encoded = yield* encodeParams(decoded.result)
1730
+ const encoded = yield* encodeResult(decoded.result)
1565
1731
  return {
1566
- id: decoded.id,
1567
- type: decoded.type,
1568
- name: decoded.name,
1732
+ ...decoded,
1569
1733
  result: encoded,
1570
- ...(decoded.metadata ? { metadata: decoded.metadata } : {}),
1734
+ ...(decoded.metadata ?? {}),
1571
1735
  ...(decoded.providerName ? { providerName: decoded.providerName } : {}),
1572
1736
  ...(decoded.providerExecuted ? { providerExecuted: true } : {})
1573
1737
  }
1574
1738
  })
1575
1739
  }
1576
- ).annotations({ identifier: `ToolResultPart(${name})` })
1740
+ ).annotations({ identifier: `ToolResultPart(${name})` }) as any
1577
1741
  }
1578
1742
 
1743
+ /**
1744
+ * Constructs a new tool result part.
1745
+ *
1746
+ * @since 1.0.0
1747
+ * @category Constructors
1748
+ */
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
1764
+
1579
1765
  // =============================================================================
1580
1766
  // File Part
1581
1767
  // =============================================================================
@@ -1651,6 +1837,14 @@ export const FilePart: Schema.Schema<FilePart, FilePartEncoded> = Schema.Struct(
1651
1837
  Schema.annotations({ identifier: "FilePart" })
1652
1838
  )
1653
1839
 
1840
+ /**
1841
+ * Constructs a new file part.
1842
+ *
1843
+ * @since 1.0.0
1844
+ * @category Constructors
1845
+ */
1846
+ export const filePart = (params: ConstructorParams<FilePart>): FilePart => makePart("file", params)
1847
+
1654
1848
  // =============================================================================
1655
1849
  // Document Source Part
1656
1850
  // =============================================================================
@@ -1743,6 +1937,15 @@ export const DocumentSourcePart: Schema.Schema<DocumentSourcePart, DocumentSourc
1743
1937
  Schema.annotations({ identifier: "DocumentSourcePart" })
1744
1938
  )
1745
1939
 
1940
+ /**
1941
+ * Constructs a new document source part.
1942
+ *
1943
+ * @since 1.0.0
1944
+ * @category Constructors
1945
+ */
1946
+ export const documentSourcePart = (params: ConstructorParams<DocumentSourcePart>): DocumentSourcePart =>
1947
+ makePart("source", { ...params, sourceType: "document" }) as any
1948
+
1746
1949
  // =============================================================================
1747
1950
  // Url Source Part
1748
1951
  // =============================================================================
@@ -1826,6 +2029,15 @@ export const UrlSourcePart: Schema.Schema<UrlSourcePart, UrlSourcePartEncoded> =
1826
2029
  Schema.annotations({ identifier: "UrlSourcePart" })
1827
2030
  )
1828
2031
 
2032
+ /**
2033
+ * Constructs a new URL source part.
2034
+ *
2035
+ * @since 1.0.0
2036
+ * @category Constructors
2037
+ */
2038
+ export const urlSourcePart = (params: ConstructorParams<UrlSourcePart>): UrlSourcePart =>
2039
+ makePart("source", { ...params, sourceType: "url" }) as any
2040
+
1829
2041
  // =============================================================================
1830
2042
  // Response Metadata Part
1831
2043
  // =============================================================================
@@ -1912,6 +2124,15 @@ export const ResponseMetadataPart: Schema.Schema<ResponseMetadataPart, ResponseM
1912
2124
  Schema.annotations({ identifier: "ResponseMetadataPart" })
1913
2125
  )
1914
2126
 
2127
+ /**
2128
+ * Constructs a new response metadata part.
2129
+ *
2130
+ * @since 1.0.0
2131
+ * @category Constructors
2132
+ */
2133
+ export const responseMetadataPart = (params: ConstructorParams<ResponseMetadataPart>): ResponseMetadataPart =>
2134
+ makePart("response-metadata", params)
2135
+
1915
2136
  // =============================================================================
1916
2137
  // Finish Part
1917
2138
  // =============================================================================
@@ -2070,6 +2291,14 @@ export const FinishPart: Schema.Schema<FinishPart, FinishPartEncoded> = Schema.S
2070
2291
  Schema.annotations({ identifier: "FinishPart" })
2071
2292
  )
2072
2293
 
2294
+ /**
2295
+ * Constructs a new finish part.
2296
+ *
2297
+ * @since 1.0.0
2298
+ * @category Constructors
2299
+ */
2300
+ export const finishPart = (params: ConstructorParams<FinishPart>): FinishPart => makePart("finish", params)
2301
+
2073
2302
  // =============================================================================
2074
2303
  // Error Part
2075
2304
  // =============================================================================
@@ -2126,3 +2355,11 @@ export const ErrorPart: Schema.Schema<ErrorPart, ErrorPartEncoded> = Schema.Stru
2126
2355
  Schema.attachPropertySignature(PartTypeId, PartTypeId),
2127
2356
  Schema.annotations({ identifier: "ErrorPart" })
2128
2357
  )
2358
+
2359
+ /**
2360
+ * Constructs a new error part.
2361
+ *
2362
+ * @since 1.0.0
2363
+ * @category Constructors
2364
+ */
2365
+ export const errorPart = (params: ConstructorParams<ErrorPart>): ErrorPart => makePart("error", params)