effect-assistant 0.0.0-stage → 0.2.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 (172) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -2
  3. package/dist/AppEvent.d.ts +111 -0
  4. package/dist/AppEvent.d.ts.map +1 -0
  5. package/dist/AppEvent.js +71 -0
  6. package/dist/AppEvent.js.map +1 -0
  7. package/dist/Assistant.d.ts +403 -0
  8. package/dist/Assistant.d.ts.map +1 -0
  9. package/dist/Assistant.js +126 -0
  10. package/dist/Assistant.js.map +1 -0
  11. package/dist/AssistantActivity.d.ts +85 -0
  12. package/dist/AssistantActivity.d.ts.map +1 -0
  13. package/dist/AssistantActivity.js +105 -0
  14. package/dist/AssistantActivity.js.map +1 -0
  15. package/dist/AssistantEngine.d.ts +116 -0
  16. package/dist/AssistantEngine.d.ts.map +1 -0
  17. package/dist/AssistantEngine.js +255 -0
  18. package/dist/AssistantEngine.js.map +1 -0
  19. package/dist/AssistantEvent.d.ts +551 -0
  20. package/dist/AssistantEvent.d.ts.map +1 -0
  21. package/dist/AssistantEvent.js +473 -0
  22. package/dist/AssistantEvent.js.map +1 -0
  23. package/dist/AssistantLoop.d.ts +173 -0
  24. package/dist/AssistantLoop.d.ts.map +1 -0
  25. package/dist/AssistantLoop.js +111 -0
  26. package/dist/AssistantLoop.js.map +1 -0
  27. package/dist/AssistantToolCall.d.ts +32 -0
  28. package/dist/AssistantToolCall.d.ts.map +1 -0
  29. package/dist/AssistantToolCall.js +37 -0
  30. package/dist/AssistantToolCall.js.map +1 -0
  31. package/dist/Block.d.ts +69 -0
  32. package/dist/Block.d.ts.map +1 -0
  33. package/dist/Block.js +39 -0
  34. package/dist/Block.js.map +1 -0
  35. package/dist/BlockKit.d.ts +180 -0
  36. package/dist/BlockKit.d.ts.map +1 -0
  37. package/dist/BlockKit.js +148 -0
  38. package/dist/BlockKit.js.map +1 -0
  39. package/dist/ChatId.d.ts +31 -0
  40. package/dist/ChatId.d.ts.map +1 -0
  41. package/dist/ChatId.js +36 -0
  42. package/dist/ChatId.js.map +1 -0
  43. package/dist/ChatLog.d.ts +109 -0
  44. package/dist/ChatLog.d.ts.map +1 -0
  45. package/dist/ChatLog.js +177 -0
  46. package/dist/ChatLog.js.map +1 -0
  47. package/dist/ChatView.d.ts +408 -0
  48. package/dist/ChatView.d.ts.map +1 -0
  49. package/dist/ChatView.js +443 -0
  50. package/dist/ChatView.js.map +1 -0
  51. package/dist/Chats.d.ts +110 -0
  52. package/dist/Chats.d.ts.map +1 -0
  53. package/dist/Chats.js +98 -0
  54. package/dist/Chats.js.map +1 -0
  55. package/dist/Compaction.d.ts +105 -0
  56. package/dist/Compaction.d.ts.map +1 -0
  57. package/dist/Compaction.js +126 -0
  58. package/dist/Compaction.js.map +1 -0
  59. package/dist/CurrentChat.d.ts +40 -0
  60. package/dist/CurrentChat.d.ts.map +1 -0
  61. package/dist/CurrentChat.js +33 -0
  62. package/dist/CurrentChat.js.map +1 -0
  63. package/dist/InputId.d.ts +38 -0
  64. package/dist/InputId.d.ts.map +1 -0
  65. package/dist/InputId.js +43 -0
  66. package/dist/InputId.js.map +1 -0
  67. package/dist/Instructions.d.ts +56 -0
  68. package/dist/Instructions.d.ts.map +1 -0
  69. package/dist/Instructions.js +50 -0
  70. package/dist/Instructions.js.map +1 -0
  71. package/dist/Memory.d.ts +190 -0
  72. package/dist/Memory.d.ts.map +1 -0
  73. package/dist/Memory.js +216 -0
  74. package/dist/Memory.js.map +1 -0
  75. package/dist/RunId.d.ts +41 -0
  76. package/dist/RunId.d.ts.map +1 -0
  77. package/dist/RunId.js +46 -0
  78. package/dist/RunId.js.map +1 -0
  79. package/dist/Skill.d.ts +72 -0
  80. package/dist/Skill.d.ts.map +1 -0
  81. package/dist/Skill.js +58 -0
  82. package/dist/Skill.js.map +1 -0
  83. package/dist/Specialist.d.ts +267 -0
  84. package/dist/Specialist.d.ts.map +1 -0
  85. package/dist/Specialist.js +119 -0
  86. package/dist/Specialist.js.map +1 -0
  87. package/dist/ToolDiscovery.d.ts +107 -0
  88. package/dist/ToolDiscovery.d.ts.map +1 -0
  89. package/dist/ToolDiscovery.js +140 -0
  90. package/dist/ToolDiscovery.js.map +1 -0
  91. package/dist/ToolFailure.d.ts +66 -0
  92. package/dist/ToolFailure.d.ts.map +1 -0
  93. package/dist/ToolFailure.js +79 -0
  94. package/dist/ToolFailure.js.map +1 -0
  95. package/dist/_virtual/_rolldown/runtime.js +13 -0
  96. package/dist/index.d.ts +24 -0
  97. package/dist/index.js +24 -0
  98. package/dist/internal/chat.js +177 -0
  99. package/dist/internal/chat.js.map +1 -0
  100. package/dist/internal/events.d.ts +46 -0
  101. package/dist/internal/events.d.ts.map +1 -0
  102. package/dist/internal/events.js +65 -0
  103. package/dist/internal/events.js.map +1 -0
  104. package/dist/internal/history.js +147 -0
  105. package/dist/internal/history.js.map +1 -0
  106. package/dist/internal/instructions.js +11 -0
  107. package/dist/internal/instructions.js.map +1 -0
  108. package/dist/internal/loop.d.ts +73 -0
  109. package/dist/internal/loop.d.ts.map +1 -0
  110. package/dist/internal/loop.js +131 -0
  111. package/dist/internal/loop.js.map +1 -0
  112. package/dist/internal/schema.js +14 -0
  113. package/dist/internal/schema.js.map +1 -0
  114. package/dist/internal/skills.js +28 -0
  115. package/dist/internal/skills.js.map +1 -0
  116. package/dist/internal/specialists.js +272 -0
  117. package/dist/internal/specialists.js.map +1 -0
  118. package/dist/internal/telemetry.js +85 -0
  119. package/dist/internal/telemetry.js.map +1 -0
  120. package/dist/internal/toolkit.d.ts +62 -0
  121. package/dist/internal/toolkit.d.ts.map +1 -0
  122. package/dist/internal/toolkit.js +108 -0
  123. package/dist/internal/toolkit.js.map +1 -0
  124. package/dist/internal/tools.js +113 -0
  125. package/dist/internal/tools.js.map +1 -0
  126. package/dist/internal/turns.d.ts +64 -0
  127. package/dist/internal/turns.d.ts.map +1 -0
  128. package/dist/internal/turns.js +214 -0
  129. package/dist/internal/turns.js.map +1 -0
  130. package/dist/internal/uuid.js +45 -0
  131. package/dist/internal/uuid.js.map +1 -0
  132. package/dist/internal/writer.d.ts +12 -0
  133. package/dist/internal/writer.d.ts.map +1 -0
  134. package/package.json +52 -3
  135. package/src/AppEvent.ts +134 -0
  136. package/src/Assistant.ts +589 -0
  137. package/src/AssistantActivity.ts +150 -0
  138. package/src/AssistantEngine.ts +343 -0
  139. package/src/AssistantEvent.ts +569 -0
  140. package/src/AssistantLoop.ts +209 -0
  141. package/src/AssistantToolCall.ts +40 -0
  142. package/src/Block.ts +84 -0
  143. package/src/BlockKit.ts +298 -0
  144. package/src/ChatId.ts +36 -0
  145. package/src/ChatLog.ts +247 -0
  146. package/src/ChatView.ts +587 -0
  147. package/src/Chats.ts +122 -0
  148. package/src/Compaction.ts +207 -0
  149. package/src/CurrentChat.ts +47 -0
  150. package/src/InputId.ts +43 -0
  151. package/src/Instructions.ts +72 -0
  152. package/src/Memory.ts +276 -0
  153. package/src/RunId.ts +46 -0
  154. package/src/Skill.ts +91 -0
  155. package/src/Specialist.ts +355 -0
  156. package/src/ToolDiscovery.ts +179 -0
  157. package/src/ToolFailure.ts +74 -0
  158. package/src/index.ts +120 -0
  159. package/src/internal/chat.ts +323 -0
  160. package/src/internal/events.ts +100 -0
  161. package/src/internal/history.ts +294 -0
  162. package/src/internal/instructions.ts +9 -0
  163. package/src/internal/loop.ts +274 -0
  164. package/src/internal/schema.ts +25 -0
  165. package/src/internal/skills.ts +37 -0
  166. package/src/internal/specialists.ts +410 -0
  167. package/src/internal/telemetry.ts +125 -0
  168. package/src/internal/toolkit.ts +188 -0
  169. package/src/internal/tools.ts +159 -0
  170. package/src/internal/turns.ts +410 -0
  171. package/src/internal/uuid.ts +50 -0
  172. package/src/internal/writer.ts +12 -0
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Runs the agent loop, which calls the model, runs its tool calls, and repeats.
3
+ *
4
+ * Most handlers use `turn.loop` or `run.loop`. This module is for a loop of your own: {@link run}
5
+ * runs the loop until the model is done, and {@link step} is one step, as a body for
6
+ * {@link loop}.
7
+ *
8
+ * Tools are a native toolkit (`effect/ai/Toolkit`) whose handlers come from the handler's layer.
9
+ * In a turn, the assistant's own tools (`send_user_message`, `wait`, and the specialist tools)
10
+ * are added to them.
11
+ *
12
+ * @since 0.1.0
13
+ */
14
+ import type * as AiError from "effect/ai/AiError"
15
+ import type * as LanguageModel from "effect/ai/LanguageModel"
16
+ import type * as Prompt from "effect/ai/Prompt"
17
+ import type * as Tool from "effect/ai/Tool"
18
+ import type * as Toolkit from "effect/ai/Toolkit"
19
+ import * as Effect from "effect/Effect"
20
+ import * as Option from "effect/Option"
21
+ import * as Stream from "effect/Stream"
22
+ import type * as Instructions from "./Instructions.ts"
23
+ import * as Loop from "./internal/loop.ts"
24
+ import * as Toolkits from "./internal/toolkit.ts"
25
+
26
+ /**
27
+ * Represents what the loop emits.
28
+ *
29
+ * For each model call, the loop emits the call's output. Then, for each of its tool calls, it
30
+ * emits a `ToolStarted` before the call runs and the call's result.
31
+ *
32
+ * @category models
33
+ * @since 0.1.0
34
+ */
35
+ export type Event = Loop.Event
36
+
37
+ /**
38
+ * Represents a chat's history as the model reads it.
39
+ *
40
+ * It holds the messages, and the last `seq` of the chat they cover.
41
+ *
42
+ * @category models
43
+ * @since 0.1.0
44
+ */
45
+ export type ChatHistory = Loop.ChatHistory
46
+
47
+ /**
48
+ * Represents how a step went, for deciding what comes next.
49
+ *
50
+ * @category models
51
+ * @since 0.1.0
52
+ */
53
+ export type StepResult = Loop.StepResult
54
+
55
+ /**
56
+ * Represents what a loop body emits, which is either a value or what to do next.
57
+ *
58
+ * @category models
59
+ * @since 0.1.0
60
+ */
61
+ export type Step<A, S> = Loop.Step<A, S>
62
+
63
+ /**
64
+ * Emits a value from a loop body.
65
+ *
66
+ * @category constructors
67
+ * @since 0.1.0
68
+ */
69
+ export const value = Loop.value
70
+
71
+ /**
72
+ * Ends a loop body's step and continues the loop with the given state.
73
+ *
74
+ * @category constructors
75
+ * @since 0.1.0
76
+ */
77
+ export const next = Loop.next
78
+
79
+ /**
80
+ * Ends the loop.
81
+ *
82
+ * @category constructors
83
+ * @since 0.1.0
84
+ */
85
+ export const stop = Loop.stop
86
+
87
+ /**
88
+ * Runs a loop body from an initial state.
89
+ *
90
+ * It emits the body's values, and runs the body again with the state it passes to `next`, until
91
+ * the body calls `stop`.
92
+ *
93
+ * @category constructors
94
+ * @since 0.1.0
95
+ */
96
+ export const loop = Loop.loop
97
+
98
+ type NoTools = Record<never, never>
99
+
100
+ /**
101
+ * Extracts the services a toolkit's tools need when they run, which are their handlers and the
102
+ * services those handlers use.
103
+ *
104
+ * @category utility types
105
+ * @since 0.1.0
106
+ */
107
+ export type ToolkitServices<T extends Record<string, Tool.Any>> = Tool.HandlersFor<T> | Toolkits.ToolServices<T>
108
+
109
+ /**
110
+ * Represents the options for {@link step}.
111
+ *
112
+ * @category models
113
+ * @since 0.1.0
114
+ */
115
+ export interface StepOptions<T extends Record<string, Tool.Any> = NoTools> {
116
+ /** The full system prompt, like `turn.instructions(text)`, optionally with span attributes. */
117
+ readonly instructions: string | Instructions.Resolved
118
+ /**
119
+ * The tools the model can call, in addition to the assistant's own tools in a turn. Their
120
+ * handlers come from the layer.
121
+ */
122
+ readonly toolkit?: Toolkit.Toolkit<T> | undefined
123
+ /**
124
+ * The current history, read at the start of the step. It is either messages, or a chat's
125
+ * history (`turn.history()`), whose `throughSeq` the step's `ModelOutput` records as how far
126
+ * the model had read.
127
+ */
128
+ readonly history: Effect.Effect<ReadonlyArray<Prompt.Message> | ChatHistory>
129
+ /**
130
+ * Defaults to `"auto"`, where a step without a tool call ends the loop. Forcing a call
131
+ * (`"required"`, or a tool by name) is refused by newer models, and with extended thinking.
132
+ */
133
+ readonly toolChoice?: LanguageModel.ToolChoice<string> | undefined
134
+ }
135
+
136
+ /**
137
+ * Represents the options for {@link run}.
138
+ *
139
+ * @category models
140
+ * @since 0.1.0
141
+ */
142
+ export interface Options<T extends Record<string, Tool.Any> = NoTools> extends StepOptions<T> {
143
+ /** The model calls allowed. Defaults to 16. */
144
+ readonly maxSteps?: number | undefined
145
+ }
146
+
147
+ const toolsOf = <T extends Record<string, Tool.Any>>(toolkit: Toolkit.Toolkit<T> | undefined) =>
148
+ Effect.map(Loop.TurnTools, (turn) => {
149
+ const own = toolkit === undefined ? Toolkits.empty : Toolkits.fromToolkit(toolkit)
150
+
151
+ return Option.match(turn, { onNone: () => own, onSome: (builtins) => Toolkits.merge(builtins, own) })
152
+ })
153
+
154
+ /**
155
+ * Runs one step of the agent loop, as a body for {@link loop}.
156
+ *
157
+ * **Details**
158
+ *
159
+ * The step calls the model with the current history and emits its `ModelOutput`. It then runs
160
+ * the output's tool calls and emits a `ToolStarted` and a `ToolResult` for each. `then` receives
161
+ * the step's result and decides what comes next, with `AssistantLoop.next(state)` or
162
+ * `AssistantLoop.stop()`.
163
+ *
164
+ * A tool with Effect's `needsApproval` set fails the loop before the model is called: the
165
+ * library doesn't run calls that wait for approval (see the guide on involving the user).
166
+ *
167
+ * @category constructors
168
+ * @since 0.1.0
169
+ */
170
+ export const step = <T extends Record<string, Tool.Any> = NoTools, S = never, E2 = never, R2 = never>(
171
+ options: StepOptions<T>,
172
+ then: (result: StepResult) => Stream.Stream<Step<never, S>, E2, R2>
173
+ ): Stream.Stream<Step<Event, S>, AiError.AiError | E2, LanguageModel.LanguageModel | ToolkitServices<T> | R2> =>
174
+ Stream.unwrap(Effect.map(toolsOf(options.toolkit), (toolkit) => Loop.step({ ...options, toolkit }, then)))
175
+
176
+ /**
177
+ * Runs the agent loop as a stream.
178
+ *
179
+ * **Details**
180
+ *
181
+ * It runs {@link step} after step until the model is done and nothing new arrived while it
182
+ * worked, or until it has made `maxSteps` model calls.
183
+ *
184
+ * **Example** (A turn's loop with a step limit)
185
+ *
186
+ * ```ts
187
+ * import { AssistantLoop } from "effect-assistant"
188
+ * import * as Layer from "effect/Layer"
189
+ * import * as Stream from "effect/Stream"
190
+ *
191
+ * const instructions = "You help a small marketing team."
192
+ *
193
+ * const MyAssistantLive = MyAssistant.toLayer((turn) =>
194
+ * AssistantLoop.run({
195
+ * instructions: turn.instructions(instructions),
196
+ * toolkit: MyTools, // the assistant's own tools are added to them
197
+ * history: turn.history(),
198
+ * maxSteps: 12
199
+ * }).pipe(Stream.concat(turn.replyIfOwed({ instructions })))
200
+ * ).pipe(Layer.provide(MyToolsLive))
201
+ * ```
202
+ *
203
+ * @category constructors
204
+ * @since 0.1.0
205
+ */
206
+ export const run = <T extends Record<string, Tool.Any> = NoTools>(
207
+ options: Options<T>
208
+ ): Stream.Stream<Event, AiError.AiError, LanguageModel.LanguageModel | ToolkitServices<T>> =>
209
+ Stream.unwrap(Effect.map(toolsOf(options.toolkit), (toolkit) => Loop.run({ ...options, toolkit })))
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Gives a tool the call it runs for.
3
+ *
4
+ * The {@link AssistantToolCall} service holds the call's id and the tool's name, and
5
+ * {@link idempotencyKey} derives a key for anything the call does outside the chat.
6
+ *
7
+ * @since 0.1.0
8
+ */
9
+ import * as Context from "effect/Context"
10
+ import type * as Effect from "effect/Effect"
11
+
12
+ /**
13
+ * Service that describes the tool call a tool runs for.
14
+ *
15
+ * The agent loop provides it to every call.
16
+ *
17
+ * @category services
18
+ * @since 0.1.0
19
+ */
20
+ export class AssistantToolCall extends Context.Service<
21
+ AssistantToolCall,
22
+ {
23
+ /** The call's id, as the model wrote it. It stays the same when a crash runs the call again. */
24
+ readonly callId: string
25
+ /** The tool's name. */
26
+ readonly tool: string
27
+ }
28
+ >()("effect-assistant/AssistantToolCall") {}
29
+
30
+ /**
31
+ * Gets an idempotency key for what the current tool call does outside the chat.
32
+ *
33
+ * The key is the same every time the call runs, and different for every other call.
34
+ *
35
+ * @category accessors
36
+ * @since 0.1.0
37
+ */
38
+ export const idempotencyKey: Effect.Effect<string, never, AssistantToolCall> = AssistantToolCall.useSync(
39
+ (call) => `tool-call:${call.callId}`
40
+ )
package/src/Block.ts ADDED
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Defines UI blocks that an assistant can put in a message.
3
+ *
4
+ * A block is a bit of UI for when a structured answer is easier than typing, such as options to
5
+ * pick from, a yes or no, or a small input. The assistant fills in its `props`, and
6
+ * using the block sends back a value typed by its `answer` schema.
7
+ *
8
+ * @since 0.1.0
9
+ */
10
+ import type * as Schema from "effect/Schema"
11
+
12
+ /**
13
+ * Schema that a block's props or answer can use.
14
+ *
15
+ * It is any codec that needs no services, since the client decodes and encodes with it too.
16
+ *
17
+ * @category utility types
18
+ * @since 0.1.0
19
+ */
20
+ export type Codec = Schema.Codec<any, any>
21
+
22
+ /**
23
+ * Represents a kind of UI block.
24
+ *
25
+ * @category models
26
+ * @since 0.1.0
27
+ */
28
+ export interface Block<Kind extends string = string, Props extends Codec = Codec, Answer extends Codec = Codec> {
29
+ readonly kind: Kind
30
+ /** When to use the block. The assistant sees this. */
31
+ readonly description: string
32
+ /** The props the assistant fills in. */
33
+ readonly props: Props
34
+ /** What using the block sends back, or `undefined` for a display-only card. */
35
+ readonly answer: Answer | undefined
36
+ }
37
+
38
+ /**
39
+ * Represents any block, for generic constraints.
40
+ *
41
+ * @category utility types
42
+ * @since 0.1.0
43
+ */
44
+ export type Any = Block
45
+
46
+ /**
47
+ * Creates a block.
48
+ *
49
+ * **Details**
50
+ *
51
+ * A block without an `answer` schema is a display-only card. Put the block in the assistant's
52
+ * `BlockKit` so the assistant can use it.
53
+ *
54
+ * **Example** (Defining a block with options to pick from)
55
+ *
56
+ * ```ts
57
+ * import { Assistant, Block, BlockKit } from "effect-assistant"
58
+ * import * as Schema from "effect/Schema"
59
+ *
60
+ * const PickOne = Block.make("pick_one", {
61
+ * description: "Options to pick from, when the user chooses between a few.",
62
+ * props: Schema.Struct({ options: Schema.Array(Schema.String) }),
63
+ * answer: Schema.Struct({ picked: Schema.String })
64
+ * })
65
+ *
66
+ * export const MyAssistant = Assistant.make("my-assistant", { blocks: BlockKit.make([PickOne]) })
67
+ * ```
68
+ *
69
+ * @category constructors
70
+ * @since 0.1.0
71
+ */
72
+ export const make = <const Kind extends string, Props extends Codec, Answer extends Codec = Schema.Never>(
73
+ kind: Kind,
74
+ options: {
75
+ readonly description: string
76
+ readonly props: Props
77
+ readonly answer?: Answer | undefined
78
+ }
79
+ ): Block<Kind, Props, Answer> => ({
80
+ kind,
81
+ description: options.description,
82
+ props: options.props,
83
+ answer: options.answer
84
+ })
@@ -0,0 +1,298 @@
1
+ /**
2
+ * Groups the blocks an assistant may put in its messages, and renders them on the client.
3
+ *
4
+ * A kit is created with {@link make} and passed to `Assistant.make`. {@link fromEntries} computes
5
+ * the state of every message's block from a chat's log, {@link closeStale} closes the blocks the
6
+ * user moved on from, and {@link toClient} creates a client-side renderer with one function per
7
+ * block kind.
8
+ *
9
+ * @since 0.1.0
10
+ */
11
+ import * as Effect from "effect/Effect"
12
+ import * as Option from "effect/Option"
13
+ import * as Predicate from "effect/Predicate"
14
+ import * as Schema from "effect/Schema"
15
+ import * as AssistantEvent from "./AssistantEvent.ts"
16
+ import type * as Block from "./Block.ts"
17
+ import type * as ChatView from "./ChatView.ts"
18
+ import { CurrentChat } from "./CurrentChat.ts"
19
+ import * as InputId from "./InputId.ts"
20
+
21
+ /**
22
+ * Represents a catalog of blocks, with a lookup by kind.
23
+ *
24
+ * @category models
25
+ * @since 0.1.0
26
+ */
27
+ export interface BlockKit<Blocks extends Block.Any = Block.Any> {
28
+ readonly blocks: ReadonlyArray<Blocks>
29
+ readonly byKind: ReadonlyMap<string, Blocks>
30
+ }
31
+
32
+ /**
33
+ * Creates a kit from blocks.
34
+ *
35
+ * @category constructors
36
+ * @since 0.1.0
37
+ */
38
+ export const make = <const Blocks extends ReadonlyArray<Block.Any>>(blocks: Blocks): BlockKit<Blocks[number]> => ({
39
+ blocks,
40
+ byKind: new Map(blocks.map((block) => [block.kind, block]))
41
+ })
42
+
43
+ type KindOf<B> = B extends { readonly kind: infer K extends string } ? K : never
44
+
45
+ type BlockOf<Kit extends BlockKit, K> = Extract<Kit["blocks"][number], { readonly kind: K }>
46
+
47
+ type PropsOf<B> = B extends { readonly props: Schema.Top } ? B["props"]["Type"] : never
48
+
49
+ type AnswerOf<B> = B extends { readonly answer: infer A } ? (A extends Schema.Top ? A["Type"] : never) : never
50
+
51
+ /**
52
+ * Represents the state of a message's block.
53
+ *
54
+ * @category models
55
+ * @since 0.1.0
56
+ */
57
+ export interface State {
58
+ readonly open: boolean
59
+ /** The user's answer, if they used the block. */
60
+ readonly answer: AssistantEvent.BlockAnswer | undefined
61
+ }
62
+
63
+ /**
64
+ * Returns the block states after one more event.
65
+ *
66
+ * @category utils
67
+ * @since 0.1.0
68
+ */
69
+ export const apply = (
70
+ states: ReadonlyMap<string, State>,
71
+ event: { readonly _tag: string }
72
+ ): ReadonlyMap<string, State> => {
73
+ if (!AssistantEvent.isLibraryEvent(event)) return states
74
+
75
+ const close = (ids: ReadonlyArray<string>) => {
76
+ const open = ids.filter((id) => states.get(id)?.open === true)
77
+
78
+ if (open.length === 0) return states
79
+
80
+ const next = new Map(states)
81
+
82
+ for (const id of open) next.set(id, { open: false, answer: undefined })
83
+
84
+ return next
85
+ }
86
+
87
+ if (Predicate.isTagged(event, "BlocksClosed")) return close(event.messageIds)
88
+
89
+ if (Predicate.isTagged(event, "AssistantMessage")) {
90
+ const closed = close(event.closes ?? [])
91
+
92
+ return event.block === undefined ? closed : new Map(closed).set(event.id, { open: true, answer: undefined })
93
+ }
94
+
95
+ if (!Predicate.isTagged(event, "BlockAnswer") || states.get(event.messageId)?.open !== true) return states
96
+
97
+ return new Map(states).set(event.messageId, { open: false, answer: event })
98
+ }
99
+
100
+ /**
101
+ * Returns the state of every message's block, by message id.
102
+ *
103
+ * @category utils
104
+ * @since 0.1.0
105
+ */
106
+ export const fromEntries = (entries: ReadonlyArray<AssistantEvent.AnyEntry>): ReadonlyMap<string, State> => {
107
+ let states: ReadonlyMap<string, State> = new Map()
108
+
109
+ for (const entry of entries) states = apply(states, entry.event)
110
+
111
+ return states
112
+ }
113
+
114
+ /**
115
+ * Returns the ids of the messages whose blocks are still open after the user typed the given
116
+ * number of messages without using them.
117
+ *
118
+ * **Details**
119
+ *
120
+ * These are the blocks {@link closeStale} closes.
121
+ *
122
+ * @category utils
123
+ * @since 0.1.0
124
+ */
125
+ export const staleBlocks = (
126
+ entries: ReadonlyArray<AssistantEvent.AnyEntry>,
127
+ options: { readonly messages: number }
128
+ ): ReadonlyArray<string> => {
129
+ const open = [...fromEntries(entries)].flatMap(([id, state]) => (state.open ? [id] : []))
130
+
131
+ return open.filter((id) => {
132
+ const shown = entries.findIndex(
133
+ ({ event }) =>
134
+ AssistantEvent.isLibraryEvent(event) && Predicate.isTagged(event, "AssistantMessage") && event.id === id
135
+ )
136
+
137
+ if (shown === -1) return false
138
+
139
+ const typed = entries.slice(shown + 1).filter(({ event }) => Predicate.isTagged(event, "UserMessage")).length
140
+
141
+ return typed >= options.messages
142
+ })
143
+ }
144
+
145
+ /**
146
+ * Closes the blocks the user moved on from, which are still open after the given number of typed
147
+ * messages.
148
+ *
149
+ * **Details**
150
+ *
151
+ * Run it from a turn, before or after its loop. It records a `BlocksClosed` event, so every
152
+ * client folds the blocks closed.
153
+ *
154
+ * @see {@link staleBlocks} for the blocks it closes
155
+ *
156
+ * @category utils
157
+ * @since 0.1.0
158
+ */
159
+ export const closeStale = Effect.fn(function* (options: { readonly messages: number }) {
160
+ const chat = yield* CurrentChat
161
+ const stale = staleBlocks(yield* chat.entries, options)
162
+
163
+ if (stale.length > 0) yield* chat.append(AssistantEvent.BlocksClosed.make({ messageIds: stale }))
164
+ })
165
+
166
+ /**
167
+ * Represents what a renderer receives for one block.
168
+ *
169
+ * @category models
170
+ * @since 0.1.0
171
+ */
172
+ export interface RenderArgs<B> {
173
+ /**
174
+ * The id of the message the block is on. A block whose button calls your API can key the work
175
+ * on it, with the chat's id, so the work runs once per block.
176
+ */
177
+ readonly messageId: string
178
+ readonly text: string
179
+ readonly props: PropsOf<B>
180
+ /**
181
+ * Whether the block is open. It is `false` once the block is answered, with the block or in
182
+ * words; show it read-only then.
183
+ */
184
+ readonly open: boolean
185
+ /** The user's answer, if they used the block. */
186
+ readonly answer: AnswerOf<B> | undefined
187
+ /** Sends an answer. Only call it while `open`. */
188
+ readonly respond: (answer: AnswerOf<B>) => void
189
+ }
190
+
191
+ /**
192
+ * Represents a renderer for every kind in a kit.
193
+ *
194
+ * @category models
195
+ * @since 0.1.0
196
+ */
197
+ export type Renderers<Kit extends BlockKit, Out> = {
198
+ readonly [K in KindOf<Kit["blocks"][number]>]: (args: RenderArgs<BlockOf<Kit, K>>) => Out
199
+ }
200
+
201
+ /**
202
+ * Represents the options for rendering one message's block.
203
+ *
204
+ * @category models
205
+ * @since 0.1.0
206
+ */
207
+ export interface RenderOptions {
208
+ /** Submits an answer, for example over RPC. */
209
+ readonly respond: (answer: AssistantEvent.BlockAnswer) => void
210
+ }
211
+
212
+ /**
213
+ * Creates a client-side renderer for the blocks on a chat view's messages
214
+ * (`ChatView.AssistantMessage`).
215
+ *
216
+ * **Details**
217
+ *
218
+ * The renderers are type-checked, so every kind in the kit needs one, and each receives typed
219
+ * `props` and `answer`. A renderer's `respond` builds a `BlockAnswer` input, with the answer
220
+ * encoded by the block's answer schema, and passes it to the `respond` option to submit.
221
+ *
222
+ * The returned function returns `undefined` for a message without a block, or with a block it
223
+ * can't decode or has no renderer for.
224
+ *
225
+ * **Example** (Rendering blocks with React)
226
+ *
227
+ * ```tsx
228
+ * import { BlockKit } from "effect-assistant"
229
+ * import type { ReactNode } from "react"
230
+ *
231
+ * const Blocks = BlockKit.make([PickOne, OrderDraft])
232
+ *
233
+ * const renderBlock = BlockKit.toClient<typeof Blocks, ReactNode>(Blocks, {
234
+ * pick_one: ({ props, open, respond }) =>
235
+ * props.options.map((option) => (
236
+ * <button key={option} disabled={!open} onClick={() => respond({ picked: option })}>
237
+ * {option}
238
+ * </button>
239
+ * )),
240
+ * // No answer schema: the button calls the app's own API with the props the user sees.
241
+ * order_draft: ({ props }) => <button onClick={() => placeOrder(props)}>Place order</button>
242
+ * })
243
+ *
244
+ * // For each AssistantMessage in the chat's view:
245
+ * renderBlock(message, { respond: (answer) => submit(answer) })
246
+ * ```
247
+ *
248
+ * @category constructors
249
+ * @since 0.1.0
250
+ */
251
+ export function toClient<Kit extends BlockKit, Out>(
252
+ kit: Kit,
253
+ renderers: Renderers<Kit, Out>
254
+ ): (message: ChatView.AssistantMessage, options: RenderOptions) => Out | undefined
255
+ // The overload above checks that every kind has a renderer; here each one takes any block's
256
+ // arguments.
257
+ export function toClient<Out>(
258
+ kit: BlockKit,
259
+ renderers: Readonly<Record<string, (args: RenderArgs<Block.Any>) => Out>>
260
+ ): (message: ChatView.AssistantMessage, options: RenderOptions) => Out | undefined {
261
+ return (message, options) => {
262
+ if (message.block === undefined) return undefined
263
+
264
+ const { kind } = message.block
265
+ const block = kit.byKind.get(kind)
266
+ const render = renderers[kind]
267
+
268
+ if (block === undefined || render === undefined) return undefined
269
+
270
+ const props = Schema.decodeUnknownOption(block.props)(message.block.props)
271
+
272
+ if (Option.isNone(props)) return undefined
273
+
274
+ const given = message.block.answer
275
+ const answerCodec = block.answer === undefined ? undefined : Schema.toCodecJson(block.answer)
276
+
277
+ const answer =
278
+ given === undefined || answerCodec === undefined ? Option.none() : Schema.decodeUnknownOption(answerCodec)(given)
279
+
280
+ return render({
281
+ messageId: message.id,
282
+ text: message.text,
283
+ props: props.value,
284
+ open: message.block.status === "open",
285
+ answer: Option.getOrUndefined(answer),
286
+ respond: (value) =>
287
+ options.respond(
288
+ AssistantEvent.BlockAnswer.make({
289
+ // A block takes one answer: the same id every time, so a second click is stored once.
290
+ id: InputId.fromKey(`${message.id}:answer`),
291
+ messageId: message.id,
292
+ kind,
293
+ value: answerCodec === undefined ? value : Schema.encodeUnknownSync(answerCodec)(value)
294
+ })
295
+ )
296
+ })
297
+ }
298
+ }
package/src/ChatId.ts ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * This module provides the id of a chat, which is `chat:` followed by a UUID.
3
+ *
4
+ * A chat id keys everything about a chat: its log, its cluster entity, and its trace session.
5
+ *
6
+ * @since 0.1.0
7
+ */
8
+ import * as Effect from "effect/Effect"
9
+ import * as Schema from "effect/Schema"
10
+ import * as Uuid from "./internal/uuid.ts"
11
+
12
+ /**
13
+ * Schema for chat ids of the form `` `chat:${uuid}` ``. The schema is branded, so a run id or a
14
+ * plain string can't be passed where a chat id is required.
15
+ *
16
+ * @category schemas
17
+ * @since 0.1.0
18
+ */
19
+ export const ChatId = Schema.TemplateLiteral(["chat:", Uuid.Uuid]).pipe(Schema.brand("~effect-assistant/ChatId"))
20
+
21
+ /**
22
+ * Represents a chat id.
23
+ *
24
+ * @category models
25
+ * @since 0.1.0
26
+ */
27
+ export type ChatId = typeof ChatId.Type
28
+
29
+ /**
30
+ * Creates a new random chat id using Effect's `Random`. Seed `Random` in tests to get the same ids
31
+ * on every run.
32
+ *
33
+ * @category constructors
34
+ * @since 0.1.0
35
+ */
36
+ export const make = (): Effect.Effect<ChatId> => Effect.map(Uuid.make, (uuid) => ChatId.make(`chat:${uuid}`))