effect-assistant 0.11.1 → 0.13.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.
Files changed (80) hide show
  1. package/dist/Assistant.d.ts +49 -4
  2. package/dist/Assistant.d.ts.map +1 -1
  3. package/dist/Assistant.js +33 -3
  4. package/dist/Assistant.js.map +1 -1
  5. package/dist/AssistantEngine.d.ts +6 -2
  6. package/dist/AssistantEngine.d.ts.map +1 -1
  7. package/dist/AssistantEngine.js +18 -10
  8. package/dist/AssistantEngine.js.map +1 -1
  9. package/dist/AssistantLoop.js +1 -1
  10. package/dist/AssistantTest.js +1 -1
  11. package/dist/BlockKit.js +1 -1
  12. package/dist/ChatSignal.d.ts +58 -0
  13. package/dist/ChatSignal.d.ts.map +1 -0
  14. package/dist/ChatSignal.js +73 -0
  15. package/dist/ChatSignal.js.map +1 -0
  16. package/dist/Compaction.js +2 -2
  17. package/dist/Memory.js +1 -1
  18. package/dist/Specialist.d.ts.map +1 -1
  19. package/dist/Specialist.js +2 -1
  20. package/dist/Specialist.js.map +1 -1
  21. package/dist/StatusSummary.d.ts +70 -0
  22. package/dist/StatusSummary.d.ts.map +1 -0
  23. package/dist/StatusSummary.js +84 -0
  24. package/dist/StatusSummary.js.map +1 -0
  25. package/dist/Timeline.d.ts +10 -2
  26. package/dist/Timeline.d.ts.map +1 -1
  27. package/dist/Timeline.js +74 -20
  28. package/dist/Timeline.js.map +1 -1
  29. package/dist/ToolStatus.d.ts +72 -0
  30. package/dist/ToolStatus.d.ts.map +1 -0
  31. package/dist/ToolStatus.js +71 -0
  32. package/dist/ToolStatus.js.map +1 -0
  33. package/dist/index.d.ts +4 -1
  34. package/dist/index.js +4 -1
  35. package/dist/internal/chat.js +19 -10
  36. package/dist/internal/chat.js.map +1 -1
  37. package/dist/internal/events.d.ts +1 -1
  38. package/dist/internal/events.js +1 -1
  39. package/dist/internal/history.js +1 -1
  40. package/dist/internal/live.js +87 -0
  41. package/dist/internal/live.js.map +1 -0
  42. package/dist/internal/loop.d.ts +1 -1
  43. package/dist/internal/loop.d.ts.map +1 -1
  44. package/dist/internal/loop.js +44 -13
  45. package/dist/internal/loop.js.map +1 -1
  46. package/dist/internal/schema.js +1 -1
  47. package/dist/internal/specialists.js +4 -3
  48. package/dist/internal/specialists.js.map +1 -1
  49. package/dist/internal/stream.js +72 -0
  50. package/dist/internal/stream.js.map +1 -0
  51. package/dist/internal/summary.js +65 -0
  52. package/dist/internal/summary.js.map +1 -0
  53. package/dist/internal/telemetry.js +16 -5
  54. package/dist/internal/telemetry.js.map +1 -1
  55. package/dist/internal/toolkit.js +1 -1
  56. package/dist/internal/tools.js +1 -1
  57. package/dist/internal/turns.d.ts +5 -0
  58. package/dist/internal/turns.d.ts.map +1 -1
  59. package/dist/internal/turns.js +18 -7
  60. package/dist/internal/turns.js.map +1 -1
  61. package/dist/internal/writer.d.ts +2 -0
  62. package/dist/internal/writer.d.ts.map +1 -1
  63. package/package.json +1 -1
  64. package/src/Assistant.ts +77 -13
  65. package/src/AssistantEngine.ts +25 -11
  66. package/src/ChatSignal.ts +71 -0
  67. package/src/Specialist.ts +5 -1
  68. package/src/StatusSummary.ts +126 -0
  69. package/src/Timeline.ts +82 -20
  70. package/src/ToolStatus.ts +90 -0
  71. package/src/index.ts +15 -0
  72. package/src/internal/chat.ts +41 -21
  73. package/src/internal/live.ts +133 -0
  74. package/src/internal/loop.ts +104 -25
  75. package/src/internal/specialists.ts +6 -0
  76. package/src/internal/stream.ts +98 -0
  77. package/src/internal/summary.ts +123 -0
  78. package/src/internal/telemetry.ts +25 -2
  79. package/src/internal/turns.ts +27 -3
  80. package/src/internal/writer.ts +3 -0
package/src/Timeline.ts CHANGED
@@ -29,6 +29,7 @@ import type * as Assistant from "./Assistant.ts"
29
29
  import * as AssistantActivity from "./AssistantActivity.ts"
30
30
  import * as AssistantEvent from "./AssistantEvent.ts"
31
31
  import * as BlockKit from "./BlockKit.ts"
32
+ import * as ChatSignal from "./ChatSignal.ts"
32
33
  import { InputId } from "./InputId.ts"
33
34
  import * as AppEvents from "./internal/events.ts"
34
35
  import * as ToolApproval from "./ToolApproval.ts"
@@ -227,12 +228,19 @@ export type SpecialistWorking = typeof SpecialistWorking.Type
227
228
  * something that arrived. `specialists` lists the specialists that are working. When the chat is
228
229
  * idle, nothing is working, even if a workflow is waiting on a timer or on the user.
229
230
  *
231
+ * On a live subscription (`subscribe(chatId, { live: true })`), `status` is a short line about what
232
+ * the running turn is doing, and `typing` is true while the model writes a message to the user. Both
233
+ * come from the chat's signals (see `ChatSignal`): they go when the turn ends, and a timeline
234
+ * folded from the log alone has neither.
235
+ *
230
236
  * @category schemas
231
237
  * @since 0.1.0
232
238
  */
233
239
  export const Activity = Schema.Struct({
234
240
  working: Schema.Boolean,
235
- specialists: Schema.Array(SpecialistWorking)
241
+ specialists: Schema.Array(SpecialistWorking),
242
+ status: Schema.optional(Schema.String),
243
+ typing: Schema.Boolean
236
244
  })
237
245
 
238
246
  /**
@@ -363,7 +371,7 @@ export const Change: <const Events extends ReadonlyArray<AssistantEvent.AppEvent
363
371
  export const empty: Timeline = {
364
372
  sequence: 0,
365
373
  items: [],
366
- activity: { working: false, specialists: [] },
374
+ activity: { working: false, specialists: [], typing: false },
367
375
  failed: false,
368
376
  deleted: false
369
377
  }
@@ -599,19 +607,45 @@ interface Schemas {
599
607
 
600
608
  interface Folding {
601
609
  readonly step: (entry: AssistantEvent.AnyEntry) => AnyChange | undefined
610
+ readonly signal: (signal: ChatSignal.ChatSignal) => AnyChange | undefined
602
611
  readonly snapshot: AnyChange
603
612
  }
604
613
 
605
- const activityOf = (activity: AssistantActivity.State): Activity => ({
606
- working: activity.turnSince !== undefined || activity.queued,
607
- specialists: [...activity.specialists.values()].map((run) => ({
608
- specialist: run.specialist,
609
- since: run.since
610
- }))
611
- })
614
+ interface LiveState {
615
+ readonly status: string | undefined
616
+ readonly typing: boolean
617
+ }
618
+
619
+ const noLive: LiveState = { status: undefined, typing: false }
620
+
621
+ const applySignal = (live: LiveState, signal: ChatSignal.ChatSignal): LiveState =>
622
+ Match.value(signal).pipe(
623
+ Match.tagsExhaustive({
624
+ Status: ({ text }): LiveState => ({ ...live, status: text }),
625
+ TypingStarted: (): LiveState => ({ status: undefined, typing: true }),
626
+ TypingStopped: (): LiveState => ({ ...live, typing: false })
627
+ })
628
+ )
629
+
630
+ const activityOf = (activity: AssistantActivity.State, live: LiveState): Activity => {
631
+ const { status } = live
632
+
633
+ const shown: Activity = {
634
+ working: activity.turnSince !== undefined || activity.queued,
635
+ specialists: [...activity.specialists.values()].map((run) => ({
636
+ specialist: run.specialist,
637
+ since: run.since
638
+ })),
639
+ typing: live.typing
640
+ }
641
+
642
+ return status === undefined ? shown : { ...shown, status }
643
+ }
612
644
 
613
645
  const sameActivity = (a: Activity, b: Activity) =>
614
646
  a.working === b.working &&
647
+ a.status === b.status &&
648
+ a.typing === b.typing &&
615
649
  a.specialists.length === b.specialists.length &&
616
650
  a.specialists.every((run, i) => {
617
651
  const other = b.specialists[i]
@@ -626,6 +660,7 @@ const folding = (show: ReadonlyArray<AssistantEvent.AppEventSchema>): Folding =>
626
660
  let failed = false
627
661
  let deleted = false
628
662
  let activity = AssistantActivity.empty
663
+ let live = noLive
629
664
  let blocks: ReadonlyMap<string, BlockKit.State> = new Map()
630
665
  // The items in order, and the assistant's messages as stored: their blocks change as the chat goes on.
631
666
  const items = new Map<string, AnyItem>()
@@ -765,15 +800,37 @@ const folding = (show: ReadonlyArray<AssistantEvent.AppEventSchema>): Folding =>
765
800
  return [item]
766
801
  })
767
802
 
803
+ // A turn's end clears what it showed, also when a signal didn't arrive.
804
+ const settle = (event: { readonly _tag: string }) => {
805
+ if (
806
+ Predicate.isTagged(event, "TurnCompleted") ||
807
+ Predicate.isTagged(event, "TurnFailed") ||
808
+ Predicate.isTagged(event, "ChatDeleted")
809
+ ) {
810
+ live = noLive
811
+ }
812
+ }
813
+
814
+ const signal = (received: ChatSignal.ChatSignal): AnyChange | undefined => {
815
+ const previous = activityOf(activity, live)
816
+
817
+ live = applySignal(live, received)
818
+
819
+ const now = activityOf(activity, live)
820
+
821
+ return sameActivity(previous, now) ? undefined : { sequence, items: [], activity: now, failed, deleted }
822
+ }
823
+
768
824
  const step = (entry: AssistantEvent.AnyEntry): AnyChange | undefined => {
769
- const previous = { activity: activityOf(activity), failed, deleted }
825
+ const previous = { activity: activityOf(activity, live), failed, deleted }
770
826
  const { event } = entry
771
827
 
772
828
  sequence = entry.sequence
773
829
  activity = AssistantActivity.apply(activity, entry)
830
+ settle(event)
774
831
 
775
832
  if (!AssistantEvent.isLibraryEvent(event)) {
776
- const now = activityOf(activity)
833
+ const now = activityOf(activity, live)
777
834
 
778
835
  return Option.match(eventItemOf(entry), {
779
836
  onNone: () =>
@@ -804,7 +861,7 @@ const folding = (show: ReadonlyArray<AssistantEvent.AppEventSchema>): Folding =>
804
861
  })
805
862
 
806
863
  const all = [...changed, ...approvalItems(event, decidedBefore)]
807
- const now = activityOf(activity)
864
+ const now = activityOf(activity, live)
808
865
 
809
866
  return all.length === 0 &&
810
867
  failed === previous.failed &&
@@ -816,8 +873,9 @@ const folding = (show: ReadonlyArray<AssistantEvent.AppEventSchema>): Folding =>
816
873
 
817
874
  return {
818
875
  step,
876
+ signal,
819
877
  get snapshot() {
820
- return { sequence, items: [...items.values()], activity: activityOf(activity), failed, deleted }
878
+ return { sequence, items: [...items.values()], activity: activityOf(activity, live), failed, deleted }
821
879
  }
822
880
  }
823
881
  }
@@ -871,12 +929,12 @@ export interface ChangesOptions<
871
929
  }
872
930
 
873
931
  function changesWith<E, R, Events, const Show extends Shown<Events> = readonly []>(
874
- entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>,
932
+ entries: Stream.Stream<AssistantEvent.AnyEntry | ChatSignal.ChatSignal, E, R>,
875
933
  definition: Definition<Events>,
876
934
  options?: ChangesOptions<Show>
877
935
  ): Stream.Stream<Change<Show[number]["Type"]>, E, R>
878
936
  function changesWith<E, R>(
879
- entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>,
937
+ entries: Stream.Stream<AssistantEvent.AnyEntry | ChatSignal.ChatSignal, E, R>,
880
938
  _definition: Schemas,
881
939
  options?: ChangesOptions<ReadonlyArray<AssistantEvent.AppEventSchema>>
882
940
  ): Stream.Stream<AnyChange, E, R> {
@@ -886,10 +944,12 @@ function changesWith<E, R>(
886
944
  Stream.mapAccumArray(
887
945
  () => folding(options?.show ?? []),
888
946
  (timeline, chunk) => {
889
- const changed = chunk.flatMap((entry) => {
890
- const change = timeline.step(entry)
947
+ const changed = chunk.flatMap((item) => {
948
+ if (ChatSignal.isChatSignal(item)) return Option.toArray(Option.fromUndefinedOr(timeline.signal(item)))
949
+
950
+ const change = timeline.step(item)
891
951
 
892
- return change === undefined || entry.sequence <= after ? [] : [change]
952
+ return change === undefined || item.sequence <= after ? [] : [change]
893
953
  })
894
954
 
895
955
  const merged = merge(changed)
@@ -951,9 +1011,11 @@ export const changes: {
951
1011
  <Events, const Show extends Shown<Events> = readonly []>(
952
1012
  definition: Definition<Events>,
953
1013
  options?: ChangesOptions<Show>
954
- ): <E, R>(entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>) => Stream.Stream<Change<Show[number]["Type"]>, E, R>
1014
+ ): <E, R>(
1015
+ entries: Stream.Stream<AssistantEvent.AnyEntry | ChatSignal.ChatSignal, E, R>
1016
+ ) => Stream.Stream<Change<Show[number]["Type"]>, E, R>
955
1017
  <E, R, Events, const Show extends Shown<Events> = readonly []>(
956
- entries: Stream.Stream<AssistantEvent.AnyEntry, E, R>,
1018
+ entries: Stream.Stream<AssistantEvent.AnyEntry | ChatSignal.ChatSignal, E, R>,
957
1019
  definition: Definition<Events>,
958
1020
  options?: ChangesOptions<Show>
959
1021
  ): Stream.Stream<Change<Show[number]["Type"]>, E, R>
@@ -0,0 +1,90 @@
1
+ /**
2
+ * This module provides status labels for tool calls: while a call runs, the chat shows the user a
3
+ * short line about what the assistant is doing, like "Checking last summer's campaign budget".
4
+ *
5
+ * Mark the tools that take a while or act on something with {@link described}: workflows, reports
6
+ * from other systems, orders, specialists. The model then sees an optional `description` parameter
7
+ * on them and fills it in the chat's language. When the call starts, the chat sends the
8
+ * description as a `ChatSignal.Status`; it shows until the next status, so it stays after the call
9
+ * ends. The tool's handler never receives the parameter. Quick reads need no label: the chat shows its generic working state for
10
+ * them.
11
+ *
12
+ * @since 0.13.0
13
+ */
14
+ import type * as Tool from "effect/ai/Tool"
15
+ import * as Context from "effect/Context"
16
+ import type * as Schema from "effect/Schema"
17
+
18
+ type Schemas = {
19
+ readonly parameters: Schema.Constraint
20
+ readonly success: Schema.Constraint
21
+ readonly failure: Schema.Constraint
22
+ readonly failureMode: Tool.FailureMode
23
+ }
24
+
25
+ /**
26
+ * The name of the parameter the model fills with a call's status.
27
+ *
28
+ * @category constants
29
+ * @since 0.13.0
30
+ */
31
+ export const parameterName = "description"
32
+
33
+ /**
34
+ * What the model reads about the parameter.
35
+ *
36
+ * @category constants
37
+ * @since 0.13.0
38
+ */
39
+ export const parameterDescription =
40
+ "Shown to the user while this runs: 3 to 8 plain words in the user's language about what you're doing, like \"Checking last summer's campaign budget\". No tool names, ids or code."
41
+
42
+ /**
43
+ * Annotation for whether a tool's calls show a status, set with {@link described}.
44
+ *
45
+ * @category annotations
46
+ * @since 0.13.0
47
+ */
48
+ export const Described = Context.Reference<boolean>("effect-assistant/ToolStatus/Described", {
49
+ defaultValue: () => false
50
+ })
51
+
52
+ /**
53
+ * Marks a tool whose calls show a status while they run.
54
+ *
55
+ * **Details**
56
+ *
57
+ * The model sees an optional `description` parameter on the tool, and the chat shows what it
58
+ * writes there while the call runs. The handler gets its parameters without it. A tool that has a
59
+ * parameter of that name already can't be marked: the loop stops with a defect when it offers one.
60
+ *
61
+ * **Example** (A slow report with a status)
62
+ *
63
+ * ```ts
64
+ * import { ToolStatus } from "effect-assistant"
65
+ * import * as Tool from "effect/ai/Tool"
66
+ * import * as Schema from "effect/Schema"
67
+ *
68
+ * export const CampaignReport = ToolStatus.described(
69
+ * Tool.make("campaign_report", {
70
+ * description: "The spend and results of a past campaign.",
71
+ * parameters: Schema.Struct({ campaignId: Schema.String }),
72
+ * success: Schema.Struct({ spent: Schema.Finite })
73
+ * })
74
+ * )
75
+ * ```
76
+ *
77
+ * @category annotations
78
+ * @since 0.13.0
79
+ */
80
+ export const described = <Name extends string, Config extends Schemas, R>(
81
+ tool: Tool.Tool<Name, Config, R>
82
+ ): Tool.Tool<Name, Config, R> => tool.annotate(Described, true)
83
+
84
+ /**
85
+ * Returns whether a tool's calls show a status.
86
+ *
87
+ * @category guards
88
+ * @since 0.13.0
89
+ */
90
+ export const isDescribed = (tool: Tool.Any): boolean => Context.get(tool.annotations, Described)
package/src/index.ts CHANGED
@@ -59,6 +59,11 @@ export * as ChatId from "./ChatId.ts"
59
59
  */
60
60
  export * as ChatLog from "./ChatLog.ts"
61
61
 
62
+ /**
63
+ * @since 0.13.0
64
+ */
65
+ export * as ChatSignal from "./ChatSignal.ts"
66
+
62
67
  /**
63
68
  * @since 0.1.0
64
69
  */
@@ -104,6 +109,11 @@ export * as Skill from "./Skill.ts"
104
109
  */
105
110
  export * as Specialist from "./Specialist.ts"
106
111
 
112
+ /**
113
+ * @since 0.13.0
114
+ */
115
+ export * as StatusSummary from "./StatusSummary.ts"
116
+
107
117
  /**
108
118
  * @since 0.3.0
109
119
  */
@@ -128,3 +138,8 @@ export * as ToolFailure from "./ToolFailure.ts"
128
138
  * @since 0.5.0
129
139
  */
130
140
  export * as ToolResult from "./ToolResult.ts"
141
+
142
+ /**
143
+ * @since 0.13.0
144
+ */
145
+ export * as ToolStatus from "./ToolStatus.ts"
@@ -15,6 +15,7 @@ import * as SynchronizedRef from "effect/SynchronizedRef"
15
15
  import * as AssistantEvent from "../AssistantEvent.ts"
16
16
  import type { ChatId } from "../ChatId.ts"
17
17
  import { ChatLog } from "../ChatLog.ts"
18
+ import * as ChatSignal from "../ChatSignal.ts"
18
19
  import { CurrentChat } from "../CurrentChat.ts"
19
20
  import * as Chats from "../Chats.ts"
20
21
  import * as SpecialistService from "../Specialist.ts"
@@ -34,7 +35,11 @@ const idOf = (event: { readonly _tag: string }): string | undefined =>
34
35
  ? event.id
35
36
  : undefined
36
37
 
37
- const makeChannelPubSub = Effect.acquireRelease(PubSub.unbounded<AssistantEvent.Entry>(), PubSub.shutdown)
38
+ /** @internal */
39
+ export type Item = AssistantEvent.Entry | ChatSignal.ChatSignal
40
+
41
+ // Entries and signals share it, so a live subscriber gets them in the order they happened.
42
+ const makeChannelPubSub = Effect.acquireRelease(PubSub.unbounded<Item>(), PubSub.shutdown)
38
43
 
39
44
  const makeHolding = Effect.fnUntraced(function* (keepAlive: (enabled: boolean) => Effect.Effect<void>) {
40
45
  const holds = yield* SynchronizedRef.make(0)
@@ -77,17 +82,22 @@ export interface Chat {
77
82
  // For inputs no turn has read, like a report stored during a shutdown.
78
83
  readonly resume: Effect.Effect<void>
79
84
  readonly stream: (after: number) => Stream.Stream<AssistantEvent.Entry>
85
+ // With the signals sent while it streams; a signal sent before isn't sent again.
86
+ readonly live: (after: number) => Stream.Stream<Item>
80
87
  // Stops what works in the chat and ends its subscribers' streams; its log stays.
81
88
  readonly delete: Effect.Effect<void>
82
89
  }
83
90
 
84
- const isChatDeleted = (entry: AssistantEvent.Entry) => Predicate.isTagged(entry.event, "ChatDeleted")
91
+ const isChatDeleted = (item: Item) => !ChatSignal.isChatSignal(item) && Predicate.isTagged(item.event, "ChatDeleted")
92
+
93
+ const isEntry = (item: Item): item is AssistantEvent.Entry => !ChatSignal.isChatSignal(item)
85
94
 
86
95
  const deletedChat = (entries: ReadonlyArray<AssistantEvent.Entry>): Chat => ({
87
96
  submit: () => Effect.void,
88
97
  submitInBackground: () => Effect.void,
89
98
  resume: Effect.void,
90
99
  stream: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),
100
+ live: (after) => Stream.fromIterable(entries.filter((entry) => entry.sequence > after)),
91
101
  delete: Effect.void
92
102
  })
93
103
 
@@ -155,7 +165,11 @@ export const make = Effect.fnUntraced(function* (options: {
155
165
  const chat: Writer = {
156
166
  chatId,
157
167
  entries: Ref.get(entries),
158
- append: (events) => Semaphore.withPermit(writes)(write(events))
168
+ append: (events) => Semaphore.withPermit(writes)(write(events)),
169
+ signal: (signal) =>
170
+ Effect.gen(function* () {
171
+ if (!(yield* isDeleted)) yield* PubSub.publish(channel, signal)
172
+ })
159
173
  }
160
174
 
161
175
  const toStored = (event: {
@@ -254,6 +268,7 @@ export const make = Effect.fnUntraced(function* (options: {
254
268
  handle,
255
269
  specialists,
256
270
  model: registration.model,
271
+ tracer: registration.tracer,
257
272
  holding,
258
273
  report: Effect.fnUntraced(function* (finished) {
259
274
  yield* store(finished)
@@ -296,6 +311,27 @@ export const make = Effect.fnUntraced(function* (options: {
296
311
  // A chat the app deleted while it was closed is deleted now, so its subscribers hear it.
297
312
  yield* checkAvailable
298
313
 
314
+ const subscribe = (after: number): Stream.Stream<Item> =>
315
+ Stream.unwrap(
316
+ Effect.gen(function* () {
317
+ // Subscribes before it reads, so nothing appended in between is missed.
318
+ const subscription = yield* PubSub.subscribe(channel)
319
+ const all = yield* chat.entries
320
+ const stored = all.filter((entry) => entry.sequence > after)
321
+ const last = stored.at(-1)?.sequence ?? after
322
+
323
+ // Deleted: what the reader hasn't seen, then the end. Otherwise the stream ends with the `ChatDeleted`.
324
+ if (all.some(isChatDeleted)) return Stream.fromIterable(stored)
325
+
326
+ return Stream.concat(
327
+ Stream.fromIterable<Item>(stored),
328
+ Stream.fromSubscription(subscription).pipe(
329
+ Stream.filter((item) => ChatSignal.isChatSignal(item) || item.sequence > last)
330
+ )
331
+ ).pipe(Stream.takeUntil(isChatDeleted))
332
+ })
333
+ )
334
+
299
335
  const chatApi: Chat = {
300
336
  submit: Effect.fnUntraced(function* (input) {
301
337
  yield* store(input)
@@ -308,24 +344,8 @@ export const make = Effect.fnUntraced(function* (options: {
308
344
  resume: Effect.gen(function* () {
309
345
  if (History.needsTurn(yield* chat.entries)) yield* inBackground(drain)
310
346
  }),
311
- stream: (after) =>
312
- Stream.unwrap(
313
- Effect.gen(function* () {
314
- // Subscribes before it reads, so nothing appended in between is missed.
315
- const subscription = yield* PubSub.subscribe(channel)
316
- const all = yield* chat.entries
317
- const stored = all.filter((entry) => entry.sequence > after)
318
- const last = stored.at(-1)?.sequence ?? after
319
-
320
- // Deleted: what the reader hasn't seen, then the end. Otherwise the stream ends with the `ChatDeleted`.
321
- if (all.some(isChatDeleted)) return Stream.fromIterable(stored)
322
-
323
- return Stream.concat(
324
- Stream.fromIterable(stored),
325
- Stream.fromSubscription(subscription).pipe(Stream.filter((entry) => entry.sequence > last))
326
- ).pipe(Stream.takeUntil(isChatDeleted))
327
- })
328
- ),
347
+ stream: (after) => subscribe(after).pipe(Stream.filter(isEntry)),
348
+ live: subscribe,
329
349
  delete: remove
330
350
  }
331
351
 
@@ -0,0 +1,133 @@
1
+ import * as Clock from "effect/Clock"
2
+ import * as Context from "effect/Context"
3
+ import * as Effect from "effect/Effect"
4
+ import * as Option from "effect/Option"
5
+ import * as SynchronizedRef from "effect/SynchronizedRef"
6
+ import * as ChatSignal from "../ChatSignal.ts"
7
+
8
+ interface Labelled {
9
+ readonly callId: string
10
+ readonly text: string
11
+ }
12
+
13
+ /** @internal */
14
+ export interface State {
15
+ /** The described calls still running, oldest first. */
16
+ readonly running: ReadonlyArray<Labelled>
17
+ /** The line the user sees; none before the first status and after typing started. */
18
+ readonly shown: string | undefined
19
+ /** When the line last changed, or the turn started, in epoch milliseconds. */
20
+ readonly shownAt: number
21
+ readonly typing: boolean
22
+ }
23
+
24
+ /** @internal */
25
+ export interface Live {
26
+ readonly callStarted: (callId: string, text: string) => Effect.Effect<void>
27
+ readonly callEnded: (callId: string) => Effect.Effect<void>
28
+ // A summary's line: shown unless the model started a message, or the line says it already.
29
+ readonly summarized: (text: string) => Effect.Effect<void>
30
+ readonly typing: (typing: boolean) => Effect.Effect<void>
31
+ readonly state: Effect.Effect<State>
32
+ // The turn ends: typing stops. Its end, in the log, clears the line.
33
+ readonly reset: Effect.Effect<void>
34
+ }
35
+
36
+ // A turn's own: a specialist's run, or a loop outside a turn, shows nothing.
37
+ /** @internal */
38
+ export const TurnLive = Context.Reference<Option.Option<Live>>("effect-assistant/TurnLive", {
39
+ defaultValue: Option.none
40
+ })
41
+
42
+ /** @internal */
43
+ export const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {
44
+ const live = yield* TurnLive
45
+
46
+ if (Option.isSome(live)) yield* f(live.value)
47
+ })
48
+
49
+ type Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined
50
+
51
+ /** @internal */
52
+ export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {
53
+ const state = yield* SynchronizedRef.make<State>({
54
+ running: [],
55
+ shown: undefined,
56
+ shownAt: yield* Clock.currentTimeMillis,
57
+ typing: false
58
+ })
59
+
60
+ // Under the ref's lock, so signals go out in the order the state changed.
61
+ const change = (f: (current: State, now: number) => Change) =>
62
+ SynchronizedRef.updateEffect(
63
+ state,
64
+ Effect.fnUntraced(function* (current) {
65
+ const changed = f(current, yield* Clock.currentTimeMillis)
66
+
67
+ if (changed === undefined) return current
68
+
69
+ const [signals, next] = changed
70
+
71
+ yield* Effect.forEach(signals, send, { discard: true })
72
+
73
+ return next
74
+ })
75
+ )
76
+
77
+ const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>
78
+ current.shown === text
79
+ ? [[], current]
80
+ : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]
81
+
82
+ const live: Live = {
83
+ callStarted: (callId, text) =>
84
+ change((current, now) => {
85
+ const running = { ...current, running: [...current.running, { callId, text }] }
86
+
87
+ return current.typing ? [[], running] : show(running, text, now)
88
+ }),
89
+ // The line stays when the last labelled call ends; while another runs, the line says that one.
90
+ callEnded: (callId) =>
91
+ change((current, now) => {
92
+ const ended = current.running.find((call) => call.callId === callId)
93
+
94
+ if (ended === undefined) return undefined
95
+
96
+ const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }
97
+ const still = next.running.at(-1)
98
+
99
+ return still === undefined || current.typing || current.shown !== ended.text
100
+ ? [[], next]
101
+ : show(next, still.text, now)
102
+ }),
103
+ summarized: (text) =>
104
+ change((current, now) =>
105
+ current.typing
106
+ ? undefined
107
+ : current.shown === text
108
+ ? [[], { ...current, shownAt: now }]
109
+ : show(current, text, now)
110
+ ),
111
+ typing: (typing) =>
112
+ change((current, now) => {
113
+ if (current.typing === typing) return undefined
114
+
115
+ if (typing) {
116
+ return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]
117
+ }
118
+
119
+ // A call that runs on after the message still says what it does.
120
+ const stopped = { ...current, typing, shownAt: now }
121
+ const still = current.running.at(-1)
122
+ const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)
123
+
124
+ return [[ChatSignal.TypingStopped.make({}), ...signals], next]
125
+ }),
126
+ state: SynchronizedRef.get(state),
127
+ reset: change((current) =>
128
+ current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined
129
+ )
130
+ }
131
+
132
+ return live
133
+ })