@effect/ai 0.28.2 → 0.28.4

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/Prompt.ts CHANGED
@@ -51,6 +51,7 @@
51
51
  *
52
52
  * @since 1.0.0
53
53
  */
54
+ import * as Arbitrary from "effect/Arbitrary"
54
55
  import * as Arr from "effect/Array"
55
56
  import { constFalse, dual } from "effect/Function"
56
57
  import * as ParseResult from "effect/ParseResult"
@@ -225,10 +226,10 @@ export const makePart = <const Type extends Part["type"]>(
225
226
  }
226
227
  ): Extract<Part, { type: Type }> =>
227
228
  (({
228
- ...params,
229
- [PartTypeId]: PartTypeId,
230
- type,
231
- options: params.options ?? {}
229
+ ...params,
230
+ [PartTypeId]: PartTypeId,
231
+ type,
232
+ options: params.options ?? {}
232
233
  }) as any)
233
234
 
234
235
  // =============================================================================
@@ -724,10 +725,10 @@ export const makeMessage = <const Role extends Message["role"]>(
724
725
  }
725
726
  ): Extract<Message, { role: Role }> =>
726
727
  (({
727
- ...params,
728
- [MessageTypeId]: MessageTypeId,
729
- role,
730
- options: params.options ?? {}
728
+ ...params,
729
+ [MessageTypeId]: MessageTypeId,
730
+ role,
731
+ options: params.options ?? {}
731
732
  }) as any)
732
733
 
733
734
  /**
@@ -1214,7 +1215,11 @@ export class PromptFromSelf extends Schema.declare(
1214
1215
  (u) => isPrompt(u),
1215
1216
  {
1216
1217
  identifier: "PromptFromSelf",
1217
- description: "a Prompt instance"
1218
+ description: "a Prompt instance",
1219
+ arbitrary: (): Arbitrary.LazyArbitrary<Prompt> => (fc) =>
1220
+ fc.array(
1221
+ Arbitrary.makeLazy(Message)(fc)
1222
+ ).map(makePrompt)
1218
1223
  }
1219
1224
  ) {}
1220
1225
 
@@ -1467,14 +1472,15 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1467
1472
  return empty
1468
1473
  }
1469
1474
 
1470
- const content: Array<AssistantMessagePart> = []
1475
+ const assistantParts: Array<AssistantMessagePart> = []
1476
+ const toolParts: Array<ToolMessagePart> = []
1471
1477
 
1472
1478
  const textDeltas: Array<string> = []
1473
1479
  function flushTextDeltas() {
1474
1480
  if (textDeltas.length > 0) {
1475
1481
  const text = textDeltas.join("")
1476
1482
  if (text.length > 0) {
1477
- content.push(makePart("text", { text }))
1483
+ assistantParts.push(makePart("text", { text }))
1478
1484
  }
1479
1485
  textDeltas.length = 0
1480
1486
  }
@@ -1485,7 +1491,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1485
1491
  if (reasoningDeltas.length > 0) {
1486
1492
  const text = reasoningDeltas.join("")
1487
1493
  if (text.length > 0) {
1488
- content.push(makePart("reasoning", { text }))
1494
+ assistantParts.push(makePart("reasoning", { text }))
1489
1495
  }
1490
1496
  reasoningDeltas.length = 0
1491
1497
  }
@@ -1501,7 +1507,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1501
1507
  switch (part.type) {
1502
1508
  case "text": {
1503
1509
  flushDeltas()
1504
- content.push(makePart("text", { text: part.text }))
1510
+ assistantParts.push(makePart("text", { text: part.text }))
1505
1511
  break
1506
1512
  }
1507
1513
  case "text-delta": {
@@ -1511,7 +1517,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1511
1517
  }
1512
1518
  case "reasoning": {
1513
1519
  flushDeltas()
1514
- content.push(makePart("reasoning", { text: part.text }))
1520
+ assistantParts.push(makePart("reasoning", { text: part.text }))
1515
1521
  break
1516
1522
  }
1517
1523
  case "reasoning-delta": {
@@ -1521,7 +1527,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1521
1527
  }
1522
1528
  case "tool-call": {
1523
1529
  flushDeltas()
1524
- content.push(makePart("tool-call", {
1530
+ assistantParts.push(makePart("tool-call", {
1525
1531
  id: part.id,
1526
1532
  name: part.providerName ?? part.name,
1527
1533
  params: part.params,
@@ -1531,7 +1537,7 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1531
1537
  }
1532
1538
  case "tool-result": {
1533
1539
  flushDeltas()
1534
- content.push(makePart("tool-result", {
1540
+ toolParts.push(makePart("tool-result", {
1535
1541
  id: part.id,
1536
1542
  name: part.providerName ?? part.name,
1537
1543
  result: part.encodedResult
@@ -1544,9 +1550,18 @@ export const fromResponseParts = (parts: ReadonlyArray<Response.AnyPart>): Promp
1544
1550
 
1545
1551
  flushDeltas()
1546
1552
 
1547
- const message = makeMessage("assistant", { content })
1553
+ if (assistantParts.length === 0 && toolParts.length === 0) {
1554
+ return empty
1555
+ }
1548
1556
 
1549
- return makePrompt([message])
1557
+ const messages: Array<Message> = []
1558
+ if (assistantParts.length > 0) {
1559
+ messages.push(makeMessage("assistant", { content: assistantParts }))
1560
+ }
1561
+ if (toolParts.length > 0) {
1562
+ messages.push(makeMessage("tool", { content: toolParts }))
1563
+ }
1564
+ return makePrompt(messages)
1550
1565
  }
1551
1566
 
1552
1567
  // =============================================================================
@@ -1600,7 +1615,7 @@ export const merge: {
1600
1615
  * @since 1.0.0
1601
1616
  * @category Combinators
1602
1617
  */
1603
- (other: RawInput): (self: Prompt) => Prompt
1618
+ (input: RawInput): (self: Prompt) => Prompt
1604
1619
  // =============================================================================
1605
1620
  // Merging Prompts
1606
1621
  // =============================================================================
@@ -1626,12 +1641,17 @@ export const merge: {
1626
1641
  * @since 1.0.0
1627
1642
  * @category Combinators
1628
1643
  */
1629
- (self: Prompt, other: RawInput): Prompt
1630
- } = dual(2, (self: Prompt, other: RawInput): Prompt =>
1631
- fromMessages([
1632
- ...self.content,
1633
- ...make(other).content
1634
- ]))
1644
+ (self: Prompt, input: RawInput): Prompt
1645
+ } = dual(2, (self: Prompt, input: RawInput): Prompt => {
1646
+ const other = make(input)
1647
+ if (self.content.length === 0) {
1648
+ return other
1649
+ }
1650
+ if (other.content.length === 0) {
1651
+ return self
1652
+ }
1653
+ return fromMessages([...self.content, ...other.content])
1654
+ })
1635
1655
 
1636
1656
  // =============================================================================
1637
1657
  // Manipulating Prompts
package/src/Response.ts CHANGED
@@ -200,10 +200,7 @@ export type AllPartsEncoded =
200
200
  */
201
201
  export const AllParts = <T extends Toolkit.Any | Toolkit.WithHandler<any>>(
202
202
  toolkit: T
203
- ): Schema.Schema<
204
- AllParts<Toolkit.Tools<T>>,
205
- AllParts<Toolkit.Tools<T>>
206
- > => {
203
+ ): Schema.Schema<AllParts<Toolkit.Tools<T>>, AllPartsEncoded> => {
207
204
  const toolCalls: Array<Schema.Schema<ToolCallPart<string, any>, ToolCallPartEncoded>> = []
208
205
  const toolCallResults: Array<Schema.Schema<ToolResultPart<string, any>, ToolResultPartEncoded>> = []
209
206
  for (const tool of Object.values(toolkit.tools as Record<string, Tool.Any>)) {
@@ -542,7 +539,7 @@ export const makePart = <const Type extends AnyPart["type"]>(
542
539
  ...params,
543
540
  [PartTypeId]: PartTypeId,
544
541
  type,
545
- metadata: params.metadata
542
+ metadata: params.metadata ?? {}
546
543
  }) as any)
547
544
 
548
545
  // =============================================================================
package/src/index.ts CHANGED
@@ -54,7 +54,7 @@
54
54
  * url: "https://api.openai.com/v1/completions",
55
55
  * urlParams: [],
56
56
  * hash: Option.none(),
57
- * headers: { "Authorization": "Bearer ***" }
57
+ * headers: { "Content-Type": "application/json" }
58
58
  * }
59
59
  * })
60
60
  * })