effect-assistant 0.1.0 → 0.3.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 (131) hide show
  1. package/README.md +1 -1
  2. package/dist/Assistant.d.ts +74 -28
  3. package/dist/Assistant.d.ts.map +1 -1
  4. package/dist/Assistant.js +11 -4
  5. package/dist/Assistant.js.map +1 -1
  6. package/dist/AssistantActivity.d.ts +10 -16
  7. package/dist/AssistantActivity.d.ts.map +1 -1
  8. package/dist/AssistantActivity.js +11 -4
  9. package/dist/AssistantActivity.js.map +1 -1
  10. package/dist/AssistantEngine.d.ts +39 -1
  11. package/dist/AssistantEngine.d.ts.map +1 -1
  12. package/dist/AssistantEngine.js +81 -28
  13. package/dist/AssistantEngine.js.map +1 -1
  14. package/dist/AssistantEvent.d.ts +52 -70
  15. package/dist/AssistantEvent.d.ts.map +1 -1
  16. package/dist/AssistantEvent.js +28 -57
  17. package/dist/AssistantEvent.js.map +1 -1
  18. package/dist/AssistantLoop.d.ts +7 -5
  19. package/dist/AssistantLoop.d.ts.map +1 -1
  20. package/dist/AssistantLoop.js +6 -3
  21. package/dist/AssistantLoop.js.map +1 -1
  22. package/dist/Block.js.map +1 -1
  23. package/dist/BlockKit.d.ts +14 -18
  24. package/dist/BlockKit.d.ts.map +1 -1
  25. package/dist/BlockKit.js +4 -7
  26. package/dist/BlockKit.js.map +1 -1
  27. package/dist/ChatLog.js +1 -1
  28. package/dist/Compaction.d.ts +8 -4
  29. package/dist/Compaction.d.ts.map +1 -1
  30. package/dist/Compaction.js +12 -15
  31. package/dist/Compaction.js.map +1 -1
  32. package/dist/CurrentChat.d.ts +2 -1
  33. package/dist/CurrentChat.d.ts.map +1 -1
  34. package/dist/CurrentChat.js.map +1 -1
  35. package/dist/Instructions.js +1 -1
  36. package/dist/Skill.d.ts +33 -0
  37. package/dist/Skill.d.ts.map +1 -1
  38. package/dist/Skill.js +27 -1
  39. package/dist/Skill.js.map +1 -1
  40. package/dist/Specialist.d.ts +4 -1
  41. package/dist/Specialist.d.ts.map +1 -1
  42. package/dist/Specialist.js.map +1 -1
  43. package/dist/Timeline.d.ts +482 -0
  44. package/dist/Timeline.d.ts.map +1 -0
  45. package/dist/Timeline.js +572 -0
  46. package/dist/Timeline.js.map +1 -0
  47. package/dist/ToolDiscovery.d.ts +4 -5
  48. package/dist/ToolDiscovery.d.ts.map +1 -1
  49. package/dist/ToolDiscovery.js +5 -4
  50. package/dist/ToolDiscovery.js.map +1 -1
  51. package/dist/ToolFailure.d.ts +1 -1
  52. package/dist/ToolFailure.js +1 -1
  53. package/dist/ToolFailure.js.map +1 -1
  54. package/dist/index.d.ts +2 -5
  55. package/dist/index.js +4 -7
  56. package/dist/internal/chat.js +11 -37
  57. package/dist/internal/chat.js.map +1 -1
  58. package/dist/internal/events.d.ts +15 -16
  59. package/dist/internal/events.d.ts.map +1 -1
  60. package/dist/internal/events.js +29 -28
  61. package/dist/internal/events.js.map +1 -1
  62. package/dist/internal/history.js +26 -58
  63. package/dist/internal/history.js.map +1 -1
  64. package/dist/internal/input.js +7 -0
  65. package/dist/internal/input.js.map +1 -0
  66. package/dist/internal/loop.d.ts +2 -2
  67. package/dist/internal/loop.d.ts.map +1 -1
  68. package/dist/internal/loop.js +21 -56
  69. package/dist/internal/loop.js.map +1 -1
  70. package/dist/internal/skills.js +6 -1
  71. package/dist/internal/skills.js.map +1 -1
  72. package/dist/internal/specialists.js +7 -7
  73. package/dist/internal/specialists.js.map +1 -1
  74. package/dist/internal/toolkit.d.ts +6 -20
  75. package/dist/internal/toolkit.d.ts.map +1 -1
  76. package/dist/internal/toolkit.js +10 -31
  77. package/dist/internal/toolkit.js.map +1 -1
  78. package/dist/internal/tools.js +2 -21
  79. package/dist/internal/tools.js.map +1 -1
  80. package/dist/internal/turns.d.ts.map +1 -1
  81. package/dist/internal/turns.js +24 -43
  82. package/dist/internal/turns.js.map +1 -1
  83. package/package.json +3 -2
  84. package/src/Assistant.ts +75 -29
  85. package/src/AssistantActivity.ts +11 -18
  86. package/src/AssistantEngine.ts +110 -43
  87. package/src/AssistantEvent.ts +46 -74
  88. package/src/AssistantLoop.ts +7 -5
  89. package/src/Block.ts +1 -1
  90. package/src/BlockKit.ts +18 -31
  91. package/src/Compaction.ts +8 -21
  92. package/src/CurrentChat.ts +2 -1
  93. package/src/Skill.ts +34 -1
  94. package/src/Specialist.ts +4 -1
  95. package/src/Timeline.ts +861 -0
  96. package/src/ToolDiscovery.ts +5 -13
  97. package/src/ToolFailure.ts +1 -1
  98. package/src/index.ts +5 -20
  99. package/src/internal/chat.ts +8 -57
  100. package/src/internal/events.ts +43 -43
  101. package/src/internal/history.ts +36 -101
  102. package/src/internal/input.ts +3 -0
  103. package/src/internal/loop.ts +31 -101
  104. package/src/internal/skills.ts +11 -0
  105. package/src/internal/specialists.ts +6 -9
  106. package/src/internal/toolkit.ts +17 -60
  107. package/src/internal/tools.ts +2 -53
  108. package/src/internal/turns.ts +37 -79
  109. package/dist/AppEvent.d.ts +0 -111
  110. package/dist/AppEvent.d.ts.map +0 -1
  111. package/dist/AppEvent.js +0 -71
  112. package/dist/AppEvent.js.map +0 -1
  113. package/dist/ChatView.d.ts +0 -459
  114. package/dist/ChatView.d.ts.map +0 -1
  115. package/dist/ChatView.js +0 -496
  116. package/dist/ChatView.js.map +0 -1
  117. package/dist/Decision.d.ts +0 -145
  118. package/dist/Decision.d.ts.map +0 -1
  119. package/dist/Decision.js +0 -178
  120. package/dist/Decision.js.map +0 -1
  121. package/dist/DecisionId.d.ts +0 -37
  122. package/dist/DecisionId.d.ts.map +0 -1
  123. package/dist/DecisionId.js +0 -44
  124. package/dist/DecisionId.js.map +0 -1
  125. package/dist/internal/decisions.js +0 -66
  126. package/dist/internal/decisions.js.map +0 -1
  127. package/src/AppEvent.ts +0 -134
  128. package/src/ChatView.ts +0 -658
  129. package/src/Decision.ts +0 -241
  130. package/src/DecisionId.ts +0 -46
  131. package/src/internal/decisions.ts +0 -131
@@ -4,10 +4,10 @@ import { Entry, Input, StoredAppEvent } from "./AssistantEvent.js";
4
4
  import { ChatLog } from "./ChatLog.js";
5
5
  import { needsTurn } from "./internal/history.js";
6
6
  import { make } from "./internal/chat.js";
7
- import * as Predicate from "effect/Predicate";
8
7
  import * as Context from "effect/Context";
9
8
  import * as Effect from "effect/Effect";
10
9
  import * as Layer from "effect/Layer";
10
+ import * as Predicate from "effect/Predicate";
11
11
  import * as Schema from "effect/Schema";
12
12
  import * as Stream from "effect/Stream";
13
13
  import * as ClusterSchema from "effect/cluster/ClusterSchema";
@@ -22,19 +22,21 @@ import * as Rpc from "effect/rpc/Rpc";
22
22
  import * as Scope from "effect/Scope";
23
23
  //#region src/AssistantEngine.ts
24
24
  /**
25
- * This module provides the `AssistantEngine` service, which runs assistants' chats, and its two
25
+ * This module provides the `AssistantEngine` service, which runs assistants' chats, and its
26
26
  * implementations.
27
27
  *
28
28
  * An assistant's `toLayer` registers the assistant with the engine, and the assistant's `submit`
29
29
  * and `subscribe` go through it. Pick one engine for the app, as with a workflow engine.
30
30
  * {@link layerMemory} runs chats in the process, for tests and single-process apps.
31
- * {@link layerCluster} runs each chat as a cluster entity, so a turn cut off by a crash resumes.
31
+ * {@link layerCluster} runs each chat as a cluster entity, so a turn cut off by a crash resumes, and
32
+ * {@link layerClusterClient} reaches those chats from a process that runs none.
32
33
  *
33
34
  * @since 0.1.0
34
35
  */
35
36
  var AssistantEngine_exports = /* @__PURE__ */ __exportAll({
36
37
  AssistantEngine: () => AssistantEngine,
37
38
  layerCluster: () => layerCluster,
39
+ layerClusterClient: () => layerClusterClient,
38
40
  layerMemory: () => layerMemory
39
41
  });
40
42
  /**
@@ -114,11 +116,11 @@ const layerMemory = Layer.effect(AssistantEngine, Effect.gen(function* () {
114
116
  })));
115
117
  }, Effect.scoped);
116
118
  const resumeAll = Effect.fnUntraced(function* (registration) {
117
- const { name, app } = registration.definition;
119
+ const { name } = registration.definition;
118
120
  for (const chatId of yield* log.unfinished) {
119
121
  const entries = yield* log.read(chatId);
120
122
  const lastTurn = entries.findLast(({ event }) => Predicate.isTagged(event, "TurnStarted"))?.event;
121
- if (Predicate.isTagged(lastTurn, "TurnStarted") && lastTurn.assistant === name && needsTurn(entries, app.wakes)) yield* withChat(name, chatId, (chat) => chat.resume);
123
+ if (Predicate.isTagged(lastTurn, "TurnStarted") && lastTurn.assistant === name && needsTurn(entries)) yield* withChat(name, chatId, (chat) => chat.resume);
122
124
  }
123
125
  });
124
126
  return AssistantEngine.of({
@@ -154,6 +156,33 @@ const chatEntity = (assistant) => Entity.make(`Assistant/${assistant}`, [
154
156
  Subscribe,
155
157
  Delete
156
158
  ]);
159
+ const makeClusterClient = Effect.gen(function* () {
160
+ const context = yield* Effect.context();
161
+ const clients = /* @__PURE__ */ new Map();
162
+ const clientOf = Effect.fnUntraced(function* (assistant) {
163
+ const known = clients.get(assistant);
164
+ if (known !== void 0) return known;
165
+ const makeClient = yield* chatEntity(assistant).client.pipe(Effect.provide(context));
166
+ const client = {
167
+ submit: (chatId, input) => makeClient(chatId).Submit({ input }, { discard: true }).pipe(Effect.orDie),
168
+ subscribe: (chatId, after) => makeClient(chatId).Subscribe({ after }).pipe(Stream.orDie),
169
+ delete: (chatId) => makeClient(chatId).Delete().pipe(Effect.orDie)
170
+ };
171
+ clients.set(assistant, client);
172
+ return client;
173
+ });
174
+ return {
175
+ submit: Effect.fn("AssistantEngine.submit")(function* (assistant, chatId, input) {
176
+ yield* (yield* clientOf(assistant)).submit(chatId, input);
177
+ }),
178
+ subscribe: (assistant, chatId, after) => Stream.unwrap(Effect.gen(function* () {
179
+ return (yield* clientOf(assistant)).subscribe(chatId, after);
180
+ })),
181
+ delete: Effect.fn("AssistantEngine.delete")(function* (assistant, chatId) {
182
+ yield* (yield* clientOf(assistant)).delete(chatId);
183
+ })
184
+ };
185
+ });
157
186
  /**
158
187
  * Layer that runs chats as cluster entities, one entity per chat, keyed by chat id.
159
188
  *
@@ -191,6 +220,7 @@ const chatEntity = (assistant) => Entity.make(`Assistant/${assistant}`, [
191
220
  * ```
192
221
  *
193
222
  * @see {@link layerMemory} for running chats in one process
223
+ * @see {@link layerClusterClient} for a process that only reaches chats the cluster runs
194
224
  *
195
225
  * @category layers
196
226
  * @since 0.1.0
@@ -198,7 +228,6 @@ const chatEntity = (assistant) => Entity.make(`Assistant/${assistant}`, [
198
228
  const layerCluster = Layer.effect(AssistantEngine, Effect.gen(function* () {
199
229
  const context = yield* Effect.context();
200
230
  const sharding = yield* Sharding.Sharding;
201
- const clients = /* @__PURE__ */ new Map();
202
231
  const register = Effect.fnUntraced(function* (registration) {
203
232
  const ChatEntity = chatEntity(registration.definition.name);
204
233
  const handlers = Effect.gen(function* () {
@@ -224,32 +253,56 @@ const layerCluster = Layer.effect(AssistantEngine, Effect.gen(function* () {
224
253
  concurrency: "unbounded"
225
254
  });
226
255
  });
227
- const clientOf = Effect.fnUntraced(function* (assistant) {
228
- const known = clients.get(assistant);
229
- if (known !== void 0) return known;
230
- const makeClient = yield* chatEntity(assistant).client.pipe(Effect.provide(context));
231
- const client = {
232
- submit: (chatId, input) => makeClient(chatId).Submit({ input }, { discard: true }).pipe(Effect.orDie),
233
- subscribe: (chatId, after) => makeClient(chatId).Subscribe({ after }).pipe(Stream.orDie),
234
- delete: (chatId) => makeClient(chatId).Delete().pipe(Effect.orDie)
235
- };
236
- clients.set(assistant, client);
237
- return client;
238
- });
256
+ const client = yield* makeClusterClient;
239
257
  return AssistantEngine.of({
240
258
  register: (registration) => register(registration).pipe(Effect.provide(context)),
241
- submit: Effect.fn("AssistantEngine.submit")(function* (assistant, chatId, input) {
242
- yield* (yield* clientOf(assistant)).submit(chatId, input);
243
- }),
244
- subscribe: (assistant, chatId, after) => Stream.unwrap(Effect.gen(function* () {
245
- return (yield* clientOf(assistant)).subscribe(chatId, after);
246
- })),
247
- delete: Effect.fn("AssistantEngine.delete")(function* (assistant, chatId) {
248
- yield* (yield* clientOf(assistant)).delete(chatId);
249
- })
259
+ ...client
260
+ });
261
+ }));
262
+ /**
263
+ * Layer that reaches chats running as cluster entities elsewhere, and runs none itself.
264
+ *
265
+ * **When to use**
266
+ *
267
+ * Use it in a process that only submits to, subscribes to, and deletes chats that the cluster's
268
+ * runners run with {@link layerCluster}, such as a web server that provides an assistant's
269
+ * `layerClient`. It needs no `ChatLog`, since the runners store the chats.
270
+ *
271
+ * **Details**
272
+ *
273
+ * The layer requires `Sharding`. An assistant's `toLayer` can't register with it: it dies, since
274
+ * no chats run in this process.
275
+ *
276
+ * **Example** (A web server in front of the cluster)
277
+ *
278
+ * ```ts
279
+ * import { Assistant, AssistantEngine } from "effect-assistant"
280
+ * import type * as Sharding from "effect/cluster/Sharding"
281
+ * import * as Layer from "effect/Layer"
282
+ *
283
+ * declare const ShardingLive: Layer.Layer<Sharding.Sharding>
284
+ *
285
+ * export const MyAssistant = Assistant.make("my-assistant")
286
+ *
287
+ * const MyAssistantClient = MyAssistant.layerClient.pipe(
288
+ * Layer.provide(AssistantEngine.layerClusterClient),
289
+ * Layer.provide(ShardingLive)
290
+ * )
291
+ * ```
292
+ *
293
+ * @see {@link layerCluster} for the runners that run the chats
294
+ *
295
+ * @category layers
296
+ * @since 0.3.0
297
+ */
298
+ const layerClusterClient = Layer.effect(AssistantEngine, Effect.gen(function* () {
299
+ const client = yield* makeClusterClient;
300
+ return AssistantEngine.of({
301
+ register: (registration) => Effect.die(`The ${registration.definition.name} assistant can't register with AssistantEngine.layerClusterClient, which runs no chats: provide AssistantEngine.layerCluster where its toLayer runs.`),
302
+ ...client
250
303
  });
251
304
  }));
252
305
  //#endregion
253
- export { AssistantEngine, AssistantEngine_exports, layerCluster, layerMemory };
306
+ export { AssistantEngine, AssistantEngine_exports, layerCluster, layerClusterClient, layerMemory };
254
307
 
255
308
  //# sourceMappingURL=AssistantEngine.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"AssistantEngine.js","names":["Chat.make","History.needsTurn","AssistantEvent.Input","AssistantEvent.StoredAppEvent","AssistantEvent.Entry"],"sources":["../src/AssistantEngine.ts"],"sourcesContent":["/**\n * This module provides the `AssistantEngine` service, which runs assistants' chats, and its two\n * implementations.\n *\n * An assistant's `toLayer` registers the assistant with the engine, and the assistant's `submit`\n * and `subscribe` go through it. Pick one engine for the app, as with a workflow engine.\n * {@link layerMemory} runs chats in the process, for tests and single-process apps.\n * {@link layerCluster} runs each chat as a cluster entity, so a turn cut off by a crash resumes.\n *\n * @since 0.1.0\n */\nimport * as ClusterSchema from \"effect/cluster/ClusterSchema\"\nimport * as Entity from \"effect/cluster/Entity\"\nimport * as Sharding from \"effect/cluster/Sharding\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Exit from \"effect/Exit\"\nimport * as Layer from \"effect/Layer\"\nimport * as MutableHashMap from \"effect/MutableHashMap\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as RcMap from \"effect/RcMap\"\nimport * as Rpc from \"effect/rpc/Rpc\"\nimport * as Schema from \"effect/Schema\"\nimport * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport { ChatLog } from \"./ChatLog.ts\"\nimport * as Chat from \"./internal/chat.ts\"\nimport * as History from \"./internal/history.ts\"\nimport type * as Turns from \"./internal/turns.ts\"\n\n/**\n * Service that runs assistants' chats. An assistant's `toLayer` registers the assistant with it,\n * and the assistant's `submit` and `subscribe` use it.\n *\n * @category services\n * @since 0.1.0\n */\nexport class AssistantEngine extends Context.Service<\n AssistantEngine,\n {\n /** Registers an assistant for as long as the scope is open. */\n readonly register: (registration: Turns.Registration) => Effect.Effect<void, never, Scope.Scope>\n /**\n * Stores an input in one of the assistant's chats. The assistant works on it in the\n * background.\n */\n readonly submit: (\n assistant: string,\n chatId: ChatId,\n input: AssistantEvent.Input | AssistantEvent.StoredAppEvent\n ) => Effect.Effect<void>\n /** Streams the chat's stored entries after the `after` seq, then live ones. */\n readonly subscribe: (assistant: string, chatId: ChatId, after: number) => Stream.Stream<AssistantEvent.Entry>\n /**\n * Deletes one of the assistant's chats. The chat's log records the deletion, and everything\n * that works in the chat stops.\n */\n readonly delete: (assistant: string, chatId: ChatId) => Effect.Effect<void>\n }\n>()(\"effect-assistant/AssistantEngine\") {}\n\nclass ChatKey extends Data.Class<{ readonly assistant: string; readonly chatId: ChatId }> {}\n\nconst notRegistered = (assistant: string) =>\n Effect.die(`The ${assistant} assistant isn't registered: provide its toLayer.`)\n\n/**\n * Layer that runs chats in the current process. Each chat's turns run as fibers, one at a time.\n *\n * **Details**\n *\n * With `ChatLog.layerSql`, the engine resumes chats at startup. When an assistant registers, the\n * engine finds the chats that assistant was working on when the process stopped (a turn cut off\n * partway, or an input that no turn read) and runs a turn for each. Run this engine in one process\n * only, because two processes would run the same chat and write its log at the same time.\n *\n * **Example** (Running chats in memory)\n *\n * ```ts\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const AppLive = MyAssistant.toLayer((turn) => turn.loop({ instructions: \"Be brief.\" })).pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n *\n * @see {@link layerCluster} for running each chat as a cluster entity\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerMemory: Layer.Layer<AssistantEngine, never, ChatLog> = Layer.effect(\n AssistantEngine,\n Effect.gen(function* () {\n const context = yield* Effect.context<ChatLog>()\n const log = yield* ChatLog\n const registered = new Map<string, Turns.Registration>()\n // A chat's keep-alive: a hold on its entry, so it stays open while runs or turns work in the background.\n const held = MutableHashMap.empty<ChatKey, Scope.Closeable>()\n\n // One entry per open chat, like a cluster entity: opened when used, closed five minutes after the last use.\n const chats: RcMap.RcMap<ChatKey, Chat.Chat> = yield* RcMap.make({\n lookup: Effect.fnUntraced(function* (key: ChatKey) {\n const registration = registered.get(key.assistant)\n\n if (registration === undefined) return yield* notRegistered(key.assistant)\n\n return yield* Chat.make({\n registration,\n chatId: key.chatId,\n keepAlive: (enabled) => keepAlive(key, enabled)\n }).pipe(Effect.provide(context))\n }),\n idleTimeToLive: \"5 minutes\"\n })\n\n // On: a hold on the chat's entry, in a scope of its own. Off: closing that scope lets the entry go idle.\n const keepAlive = Effect.fnUntraced(function* (key: ChatKey, enabled: boolean) {\n if (enabled) {\n const scope = yield* Scope.make()\n\n yield* Effect.provideService(RcMap.get(chats, key), Scope.Scope, scope)\n\n return MutableHashMap.set(held, key, scope)\n }\n\n const scope = MutableHashMap.get(held, key)\n\n MutableHashMap.remove(held, key)\n\n if (Option.isSome(scope)) yield* Scope.close(scope.value, Exit.void)\n })\n\n const withChat = Effect.fnUntraced(function* <A>(\n assistant: string,\n chatId: ChatId,\n f: (chat: Chat.Chat) => Effect.Effect<A>\n ) {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return yield* f(chat)\n }, Effect.scoped)\n\n // A chat owes a turn when its last turn was cut off, or an input came after.\n const resumeAll = Effect.fnUntraced(function* (registration: Turns.Registration) {\n const { name, app } = registration.definition\n\n for (const chatId of yield* log.unfinished) {\n const entries = yield* log.read(chatId)\n\n const lastTurn = entries.findLast(({ event }) => Predicate.isTagged(event, \"TurnStarted\"))?.event\n const ours = Predicate.isTagged(lastTurn, \"TurnStarted\") && lastTurn.assistant === name\n\n if (ours && History.needsTurn(entries, app.wakes)) yield* withChat(name, chatId, (chat) => chat.resume)\n }\n })\n\n return AssistantEngine.of({\n register: Effect.fn(\"AssistantEngine.register\")(function* (registration: Turns.Registration) {\n registered.set(registration.definition.name, registration)\n // Chats this assistant was working on when the process stopped: pick them up again.\n yield* Effect.forkScoped(resumeAll(registration))\n }),\n submit: (assistant, chatId, input) => withChat(assistant, chatId, (chat) => chat.submitInBackground(input)),\n // The chat deletes itself, so a chat that's open stops; the entry goes, and the next open finds it deleted.\n delete: Effect.fn(\"AssistantEngine.delete\")(function* (assistant: string, chatId: ChatId) {\n yield* withChat(assistant, chatId, (chat) => chat.delete)\n yield* RcMap.invalidate(chats, new ChatKey({ assistant, chatId }))\n }),\n // The subscriber holds the chat open while it streams.\n subscribe: (assistant, chatId, after) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return chat.stream(after)\n })\n )\n })\n })\n)\n\ninterface EntityClient {\n readonly submit: (chatId: ChatId, input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n readonly subscribe: (chatId: ChatId, after: number) => Stream.Stream<AssistantEvent.Entry>\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\nconst Submit = Rpc.make(\"Submit\", {\n payload: { input: Schema.Union([AssistantEvent.Input, AssistantEvent.StoredAppEvent]) }\n}).annotate(ClusterSchema.Persisted, true)\n\nconst Subscribe = Rpc.make(\"Subscribe\", {\n payload: { after: Schema.Int },\n success: AssistantEvent.Entry,\n stream: true\n})\n\nconst Delete = Rpc.make(\"Delete\")\n\nconst chatEntity = (assistant: string) => Entity.make(`Assistant/${assistant}`, [Submit, Subscribe, Delete])\n\n/**\n * Layer that runs chats as cluster entities, one entity per chat, keyed by chat id.\n *\n * **Details**\n *\n * The layer requires `Sharding`. Use a `SingleRunner` for one process with SQL storage, a runner\n * of the app's cluster, or `TestRunner` in tests.\n *\n * **Example** (Running chats on a single runner with Postgres)\n *\n * ```ts\n * import * as NodeCrypto from \"@effect/platform-node/NodeCrypto\"\n * import * as PgClient from \"@effect/sql-pg/PgClient\"\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as SingleRunner from \"effect/cluster/SingleRunner\"\n * import * as Config from \"effect/Config\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const AppLive = MyAssistant.toLayer((turn) => turn.loop({ instructions: \"Be brief.\" })).pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerCluster),\n * Layer.provideMerge(\n * Layer.merge(\n * ChatLog.layerSql,\n * SingleRunner.layer({ runnerStorage: \"sql\" }).pipe(Layer.provide(NodeCrypto.layer))\n * )\n * ),\n * Layer.provide(PgClient.layerConfig({ url: Config.Redacted(\"DATABASE_URL\") }))\n * )\n * ```\n *\n * @see {@link layerMemory} for running chats in one process\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerCluster: Layer.Layer<AssistantEngine, never, ChatLog | Sharding.Sharding> = Layer.effect(\n AssistantEngine,\n Effect.gen(function* () {\n const context = yield* Effect.context<ChatLog | Sharding.Sharding>()\n const sharding = yield* Sharding.Sharding\n const clients = new Map<string, EntityClient>()\n\n const register = Effect.fnUntraced(function* (registration: Turns.Registration) {\n const ChatEntity = chatEntity(registration.definition.name)\n\n const handlers = Effect.gen(function* () {\n const address = yield* Entity.CurrentAddress\n const chatId = yield* Schema.decodeUnknownEffect(ChatId)(address.entityId).pipe(Effect.orDie)\n // The entity's own: `Entity.keepAlive` needs its address and keep-alive latch, which is in there too.\n const entityContext = yield* Effect.context<Entity.CurrentAddress | Sharding.Sharding>()\n\n const keepAlive = (enabled: boolean) =>\n Entity.keepAlive(enabled).pipe(\n Effect.provide(entityContext),\n Effect.catchCause((cause) => Effect.logWarning(\"assistant chat keep-alive failed\", cause))\n )\n\n // A keep-alive left on from before a restart (by a run cut off) has done its job: it started the entity.\n yield* keepAlive(false)\n\n const chat = yield* Chat.make({ registration, chatId, keepAlive })\n yield* chat.resume\n\n return ChatEntity.of({\n // The turns run inside the request, so a crash leaves it unprocessed and the cluster redelivers it.\n Submit: (request) => chat.submit(request.payload.input),\n Subscribe: (request) => chat.stream(request.payload.after),\n Delete: () => chat.delete\n })\n })\n\n yield* sharding.registerEntity(ChatEntity, handlers.pipe(Effect.provide(context)), {\n maxIdleTime: \"5 minutes\",\n concurrency: \"unbounded\"\n })\n })\n\n // Made on first use, so a process that registers no assistant can still reach its chats.\n const clientOf = Effect.fnUntraced(function* (assistant: string) {\n const known = clients.get(assistant)\n\n if (known !== undefined) return known\n\n const makeClient = yield* chatEntity(assistant).client.pipe(Effect.provide(context))\n\n const client: EntityClient = {\n submit: (chatId, input) => makeClient(chatId).Submit({ input }, { discard: true }).pipe(Effect.orDie),\n subscribe: (chatId, after) => makeClient(chatId).Subscribe({ after }).pipe(Stream.orDie),\n delete: (chatId) => makeClient(chatId).Delete().pipe(Effect.orDie)\n }\n\n clients.set(assistant, client)\n\n return client\n })\n\n return AssistantEngine.of({\n register: (registration) => register(registration).pipe(Effect.provide(context)),\n submit: Effect.fn(\"AssistantEngine.submit\")(function* (\n assistant: string,\n chatId: ChatId,\n input: AssistantEvent.Input | AssistantEvent.StoredAppEvent\n ) {\n const client = yield* clientOf(assistant)\n\n yield* client.submit(chatId, input)\n }),\n subscribe: (assistant, chatId, after) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const client = yield* clientOf(assistant)\n\n return client.subscribe(chatId, after)\n })\n ),\n delete: Effect.fn(\"AssistantEngine.delete\")(function* (assistant: string, chatId: ChatId) {\n const client = yield* clientOf(assistant)\n\n yield* client.delete(chatId)\n })\n })\n })\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,IAAa,kBAAb,cAAqC,QAAQ,QAsB3C,CAAC,CAAC,kCAAkC,CAAC,CAAC,CAAC;AAEzC,IAAM,UAAN,cAAsB,KAAK,MAA+D,CAAC;AAE3F,MAAM,iBAAiB,cACrB,OAAO,IAAI,OAAO,UAAU,kDAAkD;;;;;;;;;;;;;;;;;;;;;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QAAQ,QAAQ,IAAI,SAAS;EAEnC,IAAI,UAAU,KAAA,GAAW,OAAO;EAEhC,MAAM,aAAa,OAAO,WAAW,SAAS,CAAC,CAAC,OAAO,KAAK,OAAO,QAAQ,OAAO,CAAC;EAEnF,MAAM,SAAuB;GAC3B,SAAS,QAAQ,UAAU,WAAW,MAAM,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG,EAAE,SAAS,KAAK,CAAC,CAAC,CAAC,KAAK,OAAO,KAAK;GACpG,YAAY,QAAQ,UAAU,WAAW,MAAM,CAAC,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,OAAO,KAAK;GACvF,SAAS,WAAW,WAAW,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,OAAO,KAAK;EACnE;EAEA,QAAQ,IAAI,WAAW,MAAM;EAE7B,OAAO;CACT,CAAC;CAED,OAAO,gBAAgB,GAAG;EACxB,WAAW,iBAAiB,SAAS,YAAY,CAAC,CAAC,KAAK,OAAO,QAAQ,OAAO,CAAC;EAC/E,QAAQ,OAAO,GAAG,wBAAwB,CAAC,CAAC,WAC1C,WACA,QACA,OACA;GAGA,QAAO,OAFe,SAAS,SAAS,EAAA,CAE1B,OAAO,QAAQ,KAAK;EACpC,CAAC;EACD,YAAY,WAAW,QAAQ,UAC7B,OAAO,OACL,OAAO,IAAI,aAAa;GAGtB,QAAO,OAFe,SAAS,SAAS,EAAA,CAE1B,UAAU,QAAQ,KAAK;EACvC,CAAC,CACH;EACF,QAAQ,OAAO,GAAG,wBAAwB,CAAC,CAAC,WAAW,WAAmB,QAAgB;GAGxF,QAAO,OAFe,SAAS,SAAS,EAAA,CAE1B,OAAO,MAAM;EAC7B,CAAC;CACH,CAAC;AACH,CAAC,CACH"}
1
+ {"version":3,"file":"AssistantEngine.js","names":["Chat.make","History.needsTurn","AssistantEvent.Input","AssistantEvent.StoredAppEvent","AssistantEvent.Entry"],"sources":["../src/AssistantEngine.ts"],"sourcesContent":["/**\n * This module provides the `AssistantEngine` service, which runs assistants' chats, and its\n * implementations.\n *\n * An assistant's `toLayer` registers the assistant with the engine, and the assistant's `submit`\n * and `subscribe` go through it. Pick one engine for the app, as with a workflow engine.\n * {@link layerMemory} runs chats in the process, for tests and single-process apps.\n * {@link layerCluster} runs each chat as a cluster entity, so a turn cut off by a crash resumes, and\n * {@link layerClusterClient} reaches those chats from a process that runs none.\n *\n * @since 0.1.0\n */\nimport * as ClusterSchema from \"effect/cluster/ClusterSchema\"\nimport * as Entity from \"effect/cluster/Entity\"\nimport * as Sharding from \"effect/cluster/Sharding\"\nimport * as Context from \"effect/Context\"\nimport * as Data from \"effect/Data\"\nimport * as Effect from \"effect/Effect\"\nimport * as Exit from \"effect/Exit\"\nimport * as Layer from \"effect/Layer\"\nimport * as MutableHashMap from \"effect/MutableHashMap\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as RcMap from \"effect/RcMap\"\nimport * as Rpc from \"effect/rpc/Rpc\"\nimport * as Schema from \"effect/Schema\"\nimport * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport { ChatLog } from \"./ChatLog.ts\"\nimport * as Chat from \"./internal/chat.ts\"\nimport * as History from \"./internal/history.ts\"\nimport type * as Turns from \"./internal/turns.ts\"\n\n/**\n * Service that runs assistants' chats. An assistant's `toLayer` registers the assistant with it,\n * and the assistant's `submit` and `subscribe` use it.\n *\n * @category services\n * @since 0.1.0\n */\nexport class AssistantEngine extends Context.Service<\n AssistantEngine,\n {\n /** Registers an assistant for as long as the scope is open. */\n readonly register: (registration: Turns.Registration) => Effect.Effect<void, never, Scope.Scope>\n /**\n * Stores an input in one of the assistant's chats. The assistant works on it in the\n * background.\n */\n readonly submit: (\n assistant: string,\n chatId: ChatId,\n input: AssistantEvent.Input | AssistantEvent.StoredAppEvent\n ) => Effect.Effect<void>\n /** Streams the chat's stored entries after the `after` seq, then live ones. */\n readonly subscribe: (assistant: string, chatId: ChatId, after: number) => Stream.Stream<AssistantEvent.Entry>\n /**\n * Deletes one of the assistant's chats. The chat's log records the deletion, and everything\n * that works in the chat stops.\n */\n readonly delete: (assistant: string, chatId: ChatId) => Effect.Effect<void>\n }\n>()(\"effect-assistant/AssistantEngine\") {}\n\nclass ChatKey extends Data.Class<{ readonly assistant: string; readonly chatId: ChatId }> {}\n\nconst notRegistered = (assistant: string) =>\n Effect.die(`The ${assistant} assistant isn't registered: provide its toLayer.`)\n\n/**\n * Layer that runs chats in the current process. Each chat's turns run as fibers, one at a time.\n *\n * **Details**\n *\n * With `ChatLog.layerSql`, the engine resumes chats at startup. When an assistant registers, the\n * engine finds the chats that assistant was working on when the process stopped (a turn cut off\n * partway, or an input that no turn read) and runs a turn for each. Run this engine in one process\n * only, because two processes would run the same chat and write its log at the same time.\n *\n * **Example** (Running chats in memory)\n *\n * ```ts\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const AppLive = MyAssistant.toLayer((turn) => turn.loop({ instructions: \"Be brief.\" })).pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n *\n * @see {@link layerCluster} for running each chat as a cluster entity\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerMemory: Layer.Layer<AssistantEngine, never, ChatLog> = Layer.effect(\n AssistantEngine,\n Effect.gen(function* () {\n const context = yield* Effect.context<ChatLog>()\n const log = yield* ChatLog\n const registered = new Map<string, Turns.Registration>()\n // A chat's keep-alive: a hold on its entry, so it stays open while runs or turns work in the background.\n const held = MutableHashMap.empty<ChatKey, Scope.Closeable>()\n\n // One entry per open chat, like a cluster entity: opened when used, closed five minutes after the last use.\n const chats: RcMap.RcMap<ChatKey, Chat.Chat> = yield* RcMap.make({\n lookup: Effect.fnUntraced(function* (key: ChatKey) {\n const registration = registered.get(key.assistant)\n\n if (registration === undefined) return yield* notRegistered(key.assistant)\n\n return yield* Chat.make({\n registration,\n chatId: key.chatId,\n keepAlive: (enabled) => keepAlive(key, enabled)\n }).pipe(Effect.provide(context))\n }),\n idleTimeToLive: \"5 minutes\"\n })\n\n // On: a hold on the chat's entry, in a scope of its own. Off: closing that scope lets the entry go idle.\n const keepAlive = Effect.fnUntraced(function* (key: ChatKey, enabled: boolean) {\n if (enabled) {\n const scope = yield* Scope.make()\n\n yield* Effect.provideService(RcMap.get(chats, key), Scope.Scope, scope)\n\n return MutableHashMap.set(held, key, scope)\n }\n\n const scope = MutableHashMap.get(held, key)\n\n MutableHashMap.remove(held, key)\n\n if (Option.isSome(scope)) yield* Scope.close(scope.value, Exit.void)\n })\n\n const withChat = Effect.fnUntraced(function* <A>(\n assistant: string,\n chatId: ChatId,\n f: (chat: Chat.Chat) => Effect.Effect<A>\n ) {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return yield* f(chat)\n }, Effect.scoped)\n\n // A chat owes a turn when its last turn was cut off, or an input came after.\n const resumeAll = Effect.fnUntraced(function* (registration: Turns.Registration) {\n const { name } = registration.definition\n\n for (const chatId of yield* log.unfinished) {\n const entries = yield* log.read(chatId)\n\n const lastTurn = entries.findLast(({ event }) => Predicate.isTagged(event, \"TurnStarted\"))?.event\n const ours = Predicate.isTagged(lastTurn, \"TurnStarted\") && lastTurn.assistant === name\n\n if (ours && History.needsTurn(entries)) yield* withChat(name, chatId, (chat) => chat.resume)\n }\n })\n\n return AssistantEngine.of({\n register: Effect.fn(\"AssistantEngine.register\")(function* (registration: Turns.Registration) {\n registered.set(registration.definition.name, registration)\n // Chats this assistant was working on when the process stopped: pick them up again.\n yield* Effect.forkScoped(resumeAll(registration))\n }),\n submit: (assistant, chatId, input) => withChat(assistant, chatId, (chat) => chat.submitInBackground(input)),\n // The chat deletes itself, so a chat that's open stops; the entry goes, and the next open finds it deleted.\n delete: Effect.fn(\"AssistantEngine.delete\")(function* (assistant: string, chatId: ChatId) {\n yield* withChat(assistant, chatId, (chat) => chat.delete)\n yield* RcMap.invalidate(chats, new ChatKey({ assistant, chatId }))\n }),\n // The subscriber holds the chat open while it streams.\n subscribe: (assistant, chatId, after) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return chat.stream(after)\n })\n )\n })\n })\n)\n\ninterface EntityClient {\n readonly submit: (chatId: ChatId, input: AssistantEvent.Input | AssistantEvent.StoredAppEvent) => Effect.Effect<void>\n readonly subscribe: (chatId: ChatId, after: number) => Stream.Stream<AssistantEvent.Entry>\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\nconst Submit = Rpc.make(\"Submit\", {\n payload: { input: Schema.Union([AssistantEvent.Input, AssistantEvent.StoredAppEvent]) }\n}).annotate(ClusterSchema.Persisted, true)\n\nconst Subscribe = Rpc.make(\"Subscribe\", {\n payload: { after: Schema.Int },\n success: AssistantEvent.Entry,\n stream: true\n})\n\nconst Delete = Rpc.make(\"Delete\")\n\nconst chatEntity = (assistant: string) => Entity.make(`Assistant/${assistant}`, [Submit, Subscribe, Delete])\n\ntype EngineClient = Omit<AssistantEngine[\"Service\"], \"register\">\n\n// Entity clients are made on first use, so a process that registers no assistant can still reach its chats.\nconst makeClusterClient = Effect.gen(function* () {\n const context = yield* Effect.context<Sharding.Sharding>()\n const clients = new Map<string, EntityClient>()\n\n const clientOf = Effect.fnUntraced(function* (assistant: string) {\n const known = clients.get(assistant)\n\n if (known !== undefined) return known\n\n const makeClient = yield* chatEntity(assistant).client.pipe(Effect.provide(context))\n\n const client: EntityClient = {\n submit: (chatId, input) => makeClient(chatId).Submit({ input }, { discard: true }).pipe(Effect.orDie),\n subscribe: (chatId, after) => makeClient(chatId).Subscribe({ after }).pipe(Stream.orDie),\n delete: (chatId) => makeClient(chatId).Delete().pipe(Effect.orDie)\n }\n\n clients.set(assistant, client)\n\n return client\n })\n\n const engine: EngineClient = {\n submit: Effect.fn(\"AssistantEngine.submit\")(function* (\n assistant: string,\n chatId: ChatId,\n input: AssistantEvent.Input | AssistantEvent.StoredAppEvent\n ) {\n const client = yield* clientOf(assistant)\n\n yield* client.submit(chatId, input)\n }),\n subscribe: (assistant, chatId, after) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const client = yield* clientOf(assistant)\n\n return client.subscribe(chatId, after)\n })\n ),\n delete: Effect.fn(\"AssistantEngine.delete\")(function* (assistant: string, chatId: ChatId) {\n const client = yield* clientOf(assistant)\n\n yield* client.delete(chatId)\n })\n }\n\n return engine\n})\n\n/**\n * Layer that runs chats as cluster entities, one entity per chat, keyed by chat id.\n *\n * **Details**\n *\n * The layer requires `Sharding`. Use a `SingleRunner` for one process with SQL storage, a runner\n * of the app's cluster, or `TestRunner` in tests.\n *\n * **Example** (Running chats on a single runner with Postgres)\n *\n * ```ts\n * import * as NodeCrypto from \"@effect/platform-node/NodeCrypto\"\n * import * as PgClient from \"@effect/sql-pg/PgClient\"\n * import { Assistant, AssistantEngine, ChatLog } from \"effect-assistant\"\n * import type * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as SingleRunner from \"effect/cluster/SingleRunner\"\n * import * as Config from \"effect/Config\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ModelLive: Layer.Layer<LanguageModel.LanguageModel>\n *\n * const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const AppLive = MyAssistant.toLayer((turn) => turn.loop({ instructions: \"Be brief.\" })).pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerCluster),\n * Layer.provideMerge(\n * Layer.merge(\n * ChatLog.layerSql,\n * SingleRunner.layer({ runnerStorage: \"sql\" }).pipe(Layer.provide(NodeCrypto.layer))\n * )\n * ),\n * Layer.provide(PgClient.layerConfig({ url: Config.Redacted(\"DATABASE_URL\") }))\n * )\n * ```\n *\n * @see {@link layerMemory} for running chats in one process\n * @see {@link layerClusterClient} for a process that only reaches chats the cluster runs\n *\n * @category layers\n * @since 0.1.0\n */\nexport const layerCluster: Layer.Layer<AssistantEngine, never, ChatLog | Sharding.Sharding> = Layer.effect(\n AssistantEngine,\n Effect.gen(function* () {\n const context = yield* Effect.context<ChatLog | Sharding.Sharding>()\n const sharding = yield* Sharding.Sharding\n\n const register = Effect.fnUntraced(function* (registration: Turns.Registration) {\n const ChatEntity = chatEntity(registration.definition.name)\n\n const handlers = Effect.gen(function* () {\n const address = yield* Entity.CurrentAddress\n const chatId = yield* Schema.decodeUnknownEffect(ChatId)(address.entityId).pipe(Effect.orDie)\n // The entity's own: `Entity.keepAlive` needs its address and keep-alive latch, which is in there too.\n const entityContext = yield* Effect.context<Entity.CurrentAddress | Sharding.Sharding>()\n\n const keepAlive = (enabled: boolean) =>\n Entity.keepAlive(enabled).pipe(\n Effect.provide(entityContext),\n Effect.catchCause((cause) => Effect.logWarning(\"assistant chat keep-alive failed\", cause))\n )\n\n // A keep-alive left on from before a restart (by a run cut off) has done its job: it started the entity.\n yield* keepAlive(false)\n\n const chat = yield* Chat.make({ registration, chatId, keepAlive })\n yield* chat.resume\n\n return ChatEntity.of({\n // The turns run inside the request, so a crash leaves it unprocessed and the cluster redelivers it.\n Submit: (request) => chat.submit(request.payload.input),\n Subscribe: (request) => chat.stream(request.payload.after),\n Delete: () => chat.delete\n })\n })\n\n yield* sharding.registerEntity(ChatEntity, handlers.pipe(Effect.provide(context)), {\n maxIdleTime: \"5 minutes\",\n concurrency: \"unbounded\"\n })\n })\n\n const client = yield* makeClusterClient\n\n return AssistantEngine.of({\n register: (registration) => register(registration).pipe(Effect.provide(context)),\n ...client\n })\n })\n)\n\n/**\n * Layer that reaches chats running as cluster entities elsewhere, and runs none itself.\n *\n * **When to use**\n *\n * Use it in a process that only submits to, subscribes to, and deletes chats that the cluster's\n * runners run with {@link layerCluster}, such as a web server that provides an assistant's\n * `layerClient`. It needs no `ChatLog`, since the runners store the chats.\n *\n * **Details**\n *\n * The layer requires `Sharding`. An assistant's `toLayer` can't register with it: it dies, since\n * no chats run in this process.\n *\n * **Example** (A web server in front of the cluster)\n *\n * ```ts\n * import { Assistant, AssistantEngine } from \"effect-assistant\"\n * import type * as Sharding from \"effect/cluster/Sharding\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const ShardingLive: Layer.Layer<Sharding.Sharding>\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const MyAssistantClient = MyAssistant.layerClient.pipe(\n * Layer.provide(AssistantEngine.layerClusterClient),\n * Layer.provide(ShardingLive)\n * )\n * ```\n *\n * @see {@link layerCluster} for the runners that run the chats\n *\n * @category layers\n * @since 0.3.0\n */\nexport const layerClusterClient: Layer.Layer<AssistantEngine, never, Sharding.Sharding> = Layer.effect(\n AssistantEngine,\n Effect.gen(function* () {\n const client = yield* makeClusterClient\n\n return AssistantEngine.of({\n register: (registration) =>\n Effect.die(\n `The ${registration.definition.name} assistant can't register with AssistantEngine.layerClusterClient, which runs no chats: provide AssistantEngine.layerCluster where its toLayer runs.`\n ),\n ...client\n })\n })\n)\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,kBAAb,cAAqC,QAAQ,QAsB3C,CAAC,CAAC,kCAAkC,CAAC,CAAC,CAAC;AAEzC,IAAM,UAAN,cAAsB,KAAK,MAA+D,CAAC;AAE3F,MAAM,iBAAiB,cACrB,OAAO,IAAI,OAAO,UAAU,kDAAkD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmChF,MAAa,cAA4D,MAAM,OAC7E,iBACA,OAAO,IAAI,aAAa;CACtB,MAAM,UAAU,OAAO,OAAO,QAAiB;CAC/C,MAAM,MAAM,OAAO;CACnB,MAAM,6BAAa,IAAI,IAAgC;CA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AO,aAAa,WAAW,KAAK,qJACtC;EACF,GAAG;CACL,CAAC;AACH,CAAC,CACH"}
@@ -1,8 +1,9 @@
1
1
  import { ChatId } from "./ChatId.js";
2
2
  import * as Schema from "effect/Schema";
3
3
  import * as Response from "effect/ai/Response";
4
+ import * as SchemaAST from "effect/SchemaAST";
4
5
  declare namespace AssistantEvent_d_exports {
5
- export { AnyEntry, AppTag, AssistantEvent, AssistantMessage, Attachment, BlockAnswer, BlocksClosed, CallOrigin, ChatDeleted, DecisionOutcome, DecisionRequested, DecisionResolved, Entry, Input, MessageBlock, ModelOutput, ModelPart, SpecialistFinished, SpecialistStarted, StoredAppEvent, ToolCallPart, ToolOutcome, ToolResult, ToolStarted, TurnCompleted, TurnFailed, TurnStarted, UserInput, UserMessage, assistantTexts, attachmentLine, formatSize, isInput, isLibraryEvent, isTurnEnd, isUserInput, isWaking, maxAttachments, textWithAttachments };
6
+ export { AnyEntry, AppEventSchema, AppTag, AssistantEvent, AssistantMessage, Attachment, BlockAnswer, BlocksClosed, ChatDeleted, Entry, Input, MessageBlock, ModelOutput, ModelPart, SpecialistFinished, SpecialistStarted, StoredAppEvent, ToolCallPart, ToolOutcome, ToolResult, ToolStarted, TurnCompleted, TurnFailed, TurnStarted, UserInput, UserMessage, assistantTexts, attachmentLine, formatSize, isAppInput, isInput, isLibraryEvent, isTurnEnd, isUserInput, isWaking, maxAttachments, textWithAttachments };
6
7
  }
7
8
  /**
8
9
  * Schema for a file the user attached to a message, kept by reference.
@@ -116,69 +117,6 @@ declare const SpecialistFinished_base: Schema.Class<SpecialistFinished, Schema.T
116
117
  * @since 0.1.0
117
118
  */
118
119
  export declare class SpecialistFinished extends SpecialistFinished_base {}
119
- /**
120
- * Schema for how the user decided, either `"approved"` or `"declined"`.
121
- *
122
- * @category schemas
123
- * @since 0.1.0
124
- */
125
- export declare const DecisionOutcome: Schema.Literals<readonly ["approved", "declined"]>;
126
- /**
127
- * Represents how the user decided.
128
- *
129
- * @category models
130
- * @since 0.1.0
131
- */
132
- export type DecisionOutcome = typeof DecisionOutcome.Type;
133
- /**
134
- * Schema for the origin of a decision that a tool call waits for.
135
- *
136
- * It holds the call as the model wrote it. Approving the decision runs exactly this call.
137
- *
138
- * @category schemas
139
- * @since 0.1.0
140
- */
141
- export declare const CallOrigin: Schema.TaggedStruct<"Call", {
142
- readonly callId: Schema.String;
143
- readonly tool: Schema.String;
144
- readonly params: Schema.Codec<Schema.Json, Schema.Json, never, never>;
145
- }>;
146
- /**
147
- * Represents the origin of a decision that a tool call waits for.
148
- *
149
- * @category models
150
- * @since 0.1.0
151
- */
152
- export type CallOrigin = typeof CallOrigin.Type;
153
- declare const DecisionRequested_base: Schema.Class<DecisionRequested, Schema.TaggedStruct<"DecisionRequested", {
154
- readonly id: Schema.brand<Schema.TemplateLiteral<readonly ["decision:", Schema.String]>, "~effect-assistant/DecisionId">;
155
- readonly origin: Schema.TaggedStruct<"Call", {
156
- readonly callId: Schema.String;
157
- readonly tool: Schema.String;
158
- readonly params: Schema.Codec<Schema.Json, Schema.Json, never, never>;
159
- }>;
160
- }>, {}>;
161
- /**
162
- * Represents a request for the user to decide something, by approving or declining it.
163
- *
164
- * @category schemas
165
- * @since 0.1.0
166
- */
167
- export declare class DecisionRequested extends DecisionRequested_base {}
168
- declare const DecisionResolved_base: Schema.Class<DecisionResolved, Schema.TaggedStruct<"DecisionResolved", {
169
- readonly id: Schema.brand<Schema.TemplateLiteral<readonly ["decision:", Schema.String]>, "~effect-assistant/DecisionId">;
170
- readonly outcome: Schema.Literals<readonly ["approved", "declined"]>;
171
- readonly answer: Schema.brand<Schema.String, "~effect-assistant/InputId">;
172
- }>, {}>;
173
- /**
174
- * Represents a decision the user made by answering a decision block for it.
175
- *
176
- * `answer` is the id of that `BlockAnswer`. An approved call runs at the turn's next step.
177
- *
178
- * @category schemas
179
- * @since 0.1.0
180
- */
181
- export declare class DecisionResolved extends DecisionResolved_base {}
182
120
  /**
183
121
  * Schema for every input `Assistant.submit` accepts.
184
122
  *
@@ -476,7 +414,7 @@ export declare class BlocksClosed extends BlocksClosed_base {}
476
414
  * @category schemas
477
415
  * @since 0.1.0
478
416
  */
479
- export declare const AssistantEvent: Schema.Union<readonly [typeof UserMessage, typeof BlockAnswer, typeof AssistantMessage, typeof ModelOutput, typeof ToolStarted, typeof ToolResult, typeof TurnStarted, typeof TurnCompleted, typeof TurnFailed, typeof SpecialistStarted, typeof SpecialistFinished, typeof BlocksClosed, typeof DecisionRequested, typeof DecisionResolved, typeof ChatDeleted]>;
417
+ export declare const AssistantEvent: Schema.Union<readonly [typeof UserMessage, typeof BlockAnswer, typeof AssistantMessage, typeof ModelOutput, typeof ToolStarted, typeof ToolResult, typeof TurnStarted, typeof TurnCompleted, typeof TurnFailed, typeof SpecialistStarted, typeof SpecialistFinished, typeof BlocksClosed, typeof ChatDeleted]>;
480
418
  /**
481
419
  * Represents an event the library defines.
482
420
  *
@@ -495,18 +433,49 @@ export type AssistantEvent = typeof AssistantEvent.Type;
495
433
  * @since 0.1.0
496
434
  */
497
435
  export declare const AppTag: Schema.brand<Schema.String, "~effect-assistant/AppTag">;
436
+ /**
437
+ * Represents the schema of one of the application's own events, as `Assistant.make`'s `events`
438
+ * takes it.
439
+ *
440
+ * **Details**
441
+ *
442
+ * It is a tagged struct or class (`Schema.TaggedStruct`, `Schema.TaggedClass`) whose `_tag` is a
443
+ * string literal other than the library's, and that needs no services. The log stores the event in
444
+ * the schema's JSON form.
445
+ *
446
+ * @category models
447
+ * @since 0.3.0
448
+ */
449
+ export type AppEventSchema = Schema.Codec<{
450
+ readonly _tag: string;
451
+ }, {
452
+ readonly _tag: string;
453
+ }> & {
454
+ readonly fields: {
455
+ readonly _tag: {
456
+ readonly ast: SchemaAST.AST;
457
+ };
458
+ };
459
+ };
498
460
  /**
499
461
  * Schema for one of the application's own events as it is stored, with its `_tag` and its fields
500
462
  * in JSON.
501
463
  *
502
- * An assistant made with `events: [AppEvent.make(Summary)]` decodes it to a `Summary` in
503
- * `CurrentChat`'s `entries` and in its `subscribe`.
464
+ * **Details**
465
+ *
466
+ * An assistant made with `events: [Summary]` decodes it to a `Summary` in `CurrentChat`'s
467
+ * `entries` and in its `subscribe`.
468
+ *
469
+ * An event that came in through the assistant's `submit`, from outside a turn, is an input, and the
470
+ * log marks it with a `"~effect-assistant/input": true` key (see {@link isAppInput}); one a turn
471
+ * appended has none. Decoding drops the key, so the app's schemas never see it.
504
472
  *
505
473
  * @category schemas
506
474
  * @since 0.1.0
507
475
  */
508
476
  export declare const StoredAppEvent: Schema.StructWithRest<Schema.Struct<{
509
477
  readonly _tag: Schema.brand<Schema.String, "~effect-assistant/AppTag">;
478
+ readonly "~effect-assistant/input": Schema.optionalKey<Schema.Literal<true>>;
510
479
  }>, readonly [Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>;
511
480
  /**
512
481
  * Represents one of the application's own events, as it is stored.
@@ -524,6 +493,18 @@ export type StoredAppEvent = typeof StoredAppEvent.Type;
524
493
  export declare const isLibraryEvent: (event: {
525
494
  readonly _tag: string;
526
495
  }) => event is AssistantEvent;
496
+ /**
497
+ * Returns whether one of the application's events, as the log stores it, is an input: it came in
498
+ * through the assistant's `submit`, from outside a turn, so storing it woke a turn. An event a turn
499
+ * appended (`CurrentChat`'s `append`) isn't, and neither is a decoded one, which no longer has the
500
+ * mark.
501
+ *
502
+ * @category guards
503
+ * @since 0.3.0
504
+ */
505
+ export declare const isAppInput: (event: {
506
+ readonly _tag: string;
507
+ }) => boolean;
527
508
  /**
528
509
  * Represents a chat's entry with any event in it, either the library's or the application's own
529
510
  * (decoded or not).
@@ -554,8 +535,9 @@ export declare const Entry: Schema.Struct<{
554
535
  readonly chatId: Schema.brand<Schema.TemplateLiteral<readonly ["chat:", Schema.String]>, "~effect-assistant/ChatId">;
555
536
  readonly seq: Schema.Int;
556
537
  readonly at: Schema.Finite;
557
- readonly event: Schema.Union<readonly [Schema.Union<readonly [typeof UserMessage, typeof BlockAnswer, typeof AssistantMessage, typeof ModelOutput, typeof ToolStarted, typeof ToolResult, typeof TurnStarted, typeof TurnCompleted, typeof TurnFailed, typeof SpecialistStarted, typeof SpecialistFinished, typeof BlocksClosed, typeof DecisionRequested, typeof DecisionResolved, typeof ChatDeleted]>, Schema.StructWithRest<Schema.Struct<{
538
+ readonly event: Schema.Union<readonly [Schema.Union<readonly [typeof UserMessage, typeof BlockAnswer, typeof AssistantMessage, typeof ModelOutput, typeof ToolStarted, typeof ToolResult, typeof TurnStarted, typeof TurnCompleted, typeof TurnFailed, typeof SpecialistStarted, typeof SpecialistFinished, typeof BlocksClosed, typeof ChatDeleted]>, Schema.StructWithRest<Schema.Struct<{
558
539
  readonly _tag: Schema.brand<Schema.String, "~effect-assistant/AppTag">;
540
+ readonly "~effect-assistant/input": Schema.optionalKey<Schema.Literal<true>>;
559
541
  }>, readonly [Schema.$Record<Schema.String, Schema.Codec<Schema.Json, Schema.Json, never, never>>]>]>;
560
542
  }>;
561
543
  /**
@@ -569,8 +551,8 @@ export type Entry = typeof Entry.Type;
569
551
  * Returns whether an event is one of the library's inputs, which are a user message, a block
570
552
  * answer, or a finished specialist run.
571
553
  *
572
- * The application's events that wake a turn are inputs too, but only their assistant knows which
573
- * (see `AppEvent`).
554
+ * The application's events that came in through the assistant's `submit` are inputs too (see
555
+ * {@link isAppInput}).
574
556
  *
575
557
  * @category guards
576
558
  * @since 0.1.0
@@ -1 +1 @@
1
- {"version":3,"file":"AssistantEvent.d.ts","names":[],"sources":["../src/AssistantEvent.ts"],"mappings":";;;;;;;;;;;;;;qBA0Ba,YAAU,OAAA;;;;;;;;;;;;;;;qBAgBV;;;;;;;YAQD,oBAAoB,WAAW;;;;;;;qBAQ9B,aAAU;;;;;;;;;qBAiBV,iBAAc,YAAgB;;;;;;;qBAS9B,sBAAmB,cAAgB,aAAe,cAAc;;;;;;;;;;;;;;;;;;;;;qBAchE,oBAAoB;;;;;;;;;;;;;;;;qBAepB,oBAAoB;;;;;;;;;;;;;;;;;;;;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1
1
  import { __exportAll } from "./_virtual/_rolldown/runtime.js";
2
2
  import { ChatId } from "./ChatId.js";
3
- import { DecisionId } from "./DecisionId.js";
4
3
  import { InputId, make } from "./InputId.js";
4
+ import { inputKey } from "./internal/input.js";
5
5
  import { RunId } from "./RunId.js";
6
6
  import * as Predicate from "effect/Predicate";
7
7
  import * as Schema from "effect/Schema";
@@ -11,7 +11,7 @@ import * as Response from "effect/ai/Response";
11
11
  * Defines the events that can happen in a chat.
12
12
  *
13
13
  * A chat is an append-only log of these events, and the log is the source of truth. A chat client
14
- * receives the part of the log its user sees (`ChatView`), and the model's history is rebuilt
14
+ * receives the part of the log its user sees (its `Timeline`), and the model's history is rebuilt
15
15
  * from the log before every model call. Besides the library's events ({@link AssistantEvent}), a
16
16
  * log holds the application's own events ({@link StoredAppEvent}).
17
17
  *
@@ -24,11 +24,7 @@ var AssistantEvent_exports = /* @__PURE__ */ __exportAll({
24
24
  Attachment: () => Attachment,
25
25
  BlockAnswer: () => BlockAnswer,
26
26
  BlocksClosed: () => BlocksClosed,
27
- CallOrigin: () => CallOrigin,
28
27
  ChatDeleted: () => ChatDeleted,
29
- DecisionOutcome: () => DecisionOutcome,
30
- DecisionRequested: () => DecisionRequested,
31
- DecisionResolved: () => DecisionResolved,
32
28
  Entry: () => Entry,
33
29
  Input: () => Input,
34
30
  MessageBlock: () => MessageBlock,
@@ -49,6 +45,7 @@ var AssistantEvent_exports = /* @__PURE__ */ __exportAll({
49
45
  assistantTexts: () => assistantTexts,
50
46
  attachmentLine: () => attachmentLine,
51
47
  formatSize: () => formatSize,
48
+ isAppInput: () => isAppInput,
52
49
  isInput: () => isInput,
53
50
  isLibraryEvent: () => isLibraryEvent,
54
51
  isTurnEnd: () => isTurnEnd,
@@ -159,49 +156,6 @@ var SpecialistFinished = class extends Schema.TaggedClass()("SpecialistFinished"
159
156
  error: Schema.optional(Schema.String)
160
157
  }) {};
161
158
  /**
162
- * Schema for how the user decided, either `"approved"` or `"declined"`.
163
- *
164
- * @category schemas
165
- * @since 0.1.0
166
- */
167
- const DecisionOutcome = Schema.Literals(["approved", "declined"]);
168
- /**
169
- * Schema for the origin of a decision that a tool call waits for.
170
- *
171
- * It holds the call as the model wrote it. Approving the decision runs exactly this call.
172
- *
173
- * @category schemas
174
- * @since 0.1.0
175
- */
176
- const CallOrigin = Schema.TaggedStruct("Call", {
177
- callId: Schema.String,
178
- tool: Schema.String,
179
- params: Schema.Json
180
- });
181
- /**
182
- * Represents a request for the user to decide something, by approving or declining it.
183
- *
184
- * @category schemas
185
- * @since 0.1.0
186
- */
187
- var DecisionRequested = class extends Schema.TaggedClass()("DecisionRequested", {
188
- id: DecisionId,
189
- origin: CallOrigin
190
- }) {};
191
- /**
192
- * Represents a decision the user made by answering a decision block for it.
193
- *
194
- * `answer` is the id of that `BlockAnswer`. An approved call runs at the turn's next step.
195
- *
196
- * @category schemas
197
- * @since 0.1.0
198
- */
199
- var DecisionResolved = class extends Schema.TaggedClass()("DecisionResolved", {
200
- id: DecisionId,
201
- outcome: DecisionOutcome,
202
- answer: InputId
203
- }) {};
204
- /**
205
159
  * Schema for every input `Assistant.submit` accepts.
206
160
  *
207
161
  * @category schemas
@@ -428,8 +382,6 @@ const AssistantEvent = Schema.Union([
428
382
  SpecialistStarted,
429
383
  SpecialistFinished,
430
384
  BlocksClosed,
431
- DecisionRequested,
432
- DecisionResolved,
433
385
  ChatDeleted
434
386
  ]);
435
387
  const libraryTags = new Set(AssistantEvent.members.map((member) => member.fields._tag.schema.literal));
@@ -448,13 +400,22 @@ const AppTag = Schema.String.pipe(Schema.brand("~effect-assistant/AppTag"));
448
400
  * Schema for one of the application's own events as it is stored, with its `_tag` and its fields
449
401
  * in JSON.
450
402
  *
451
- * An assistant made with `events: [AppEvent.make(Summary)]` decodes it to a `Summary` in
452
- * `CurrentChat`'s `entries` and in its `subscribe`.
403
+ * **Details**
404
+ *
405
+ * An assistant made with `events: [Summary]` decodes it to a `Summary` in `CurrentChat`'s
406
+ * `entries` and in its `subscribe`.
407
+ *
408
+ * An event that came in through the assistant's `submit`, from outside a turn, is an input, and the
409
+ * log marks it with a `"~effect-assistant/input": true` key (see {@link isAppInput}); one a turn
410
+ * appended has none. Decoding drops the key, so the app's schemas never see it.
453
411
  *
454
412
  * @category schemas
455
413
  * @since 0.1.0
456
414
  */
457
- const StoredAppEvent = Schema.StructWithRest(Schema.Struct({ _tag: AppTag }), [Schema.Record(Schema.String, Schema.Json)]).check(Schema.makeFilter((event) => !libraryTags.has(event._tag) || `${event._tag} is one of the library's events`));
415
+ const StoredAppEvent = Schema.StructWithRest(Schema.Struct({
416
+ _tag: AppTag,
417
+ [inputKey]: Schema.optionalKey(Schema.Literal(true))
418
+ }), [Schema.Record(Schema.String, Schema.Json)]).check(Schema.makeFilter((event) => !libraryTags.has(event._tag) || `${event._tag} is one of the library's events`));
458
419
  /**
459
420
  * Returns whether an event is one of the library's, not one of the app's own.
460
421
  *
@@ -463,6 +424,16 @@ const StoredAppEvent = Schema.StructWithRest(Schema.Struct({ _tag: AppTag }), [S
463
424
  */
464
425
  const isLibraryEvent = (event) => libraryTags.has(event._tag);
465
426
  /**
427
+ * Returns whether one of the application's events, as the log stores it, is an input: it came in
428
+ * through the assistant's `submit`, from outside a turn, so storing it woke a turn. An event a turn
429
+ * appended (`CurrentChat`'s `append`) isn't, and neither is a decoded one, which no longer has the
430
+ * mark.
431
+ *
432
+ * @category guards
433
+ * @since 0.3.0
434
+ */
435
+ const isAppInput = (event) => !isLibraryEvent(event) && Predicate.hasProperty(event, "~effect-assistant/input") && event["~effect-assistant/input"] === true;
436
+ /**
466
437
  * Schema for a stored event with its position in the chat.
467
438
  *
468
439
  * `seq` starts at 1 and has no gaps. `at` is the time the event was stored, in epoch
@@ -481,8 +452,8 @@ const Entry = Schema.Struct({
481
452
  * Returns whether an event is one of the library's inputs, which are a user message, a block
482
453
  * answer, or a finished specialist run.
483
454
  *
484
- * The application's events that wake a turn are inputs too, but only their assistant knows which
485
- * (see `AppEvent`).
455
+ * The application's events that came in through the assistant's `submit` are inputs too (see
456
+ * {@link isAppInput}).
486
457
  *
487
458
  * @category guards
488
459
  * @since 0.1.0
@@ -518,6 +489,6 @@ const isTurnEnd = (event) => Predicate.isTagged(event, "TurnCompleted") || Predi
518
489
  */
519
490
  const assistantTexts = (entries) => entries.flatMap(({ event }) => isLibraryEvent(event) && Predicate.isTagged(event, "AssistantMessage") ? [event.text] : []);
520
491
  //#endregion
521
- export { AppTag, AssistantEvent, AssistantEvent_exports, AssistantMessage, Attachment, BlockAnswer, BlocksClosed, CallOrigin, ChatDeleted, DecisionOutcome, DecisionRequested, DecisionResolved, Entry, Input, MessageBlock, ModelOutput, ModelPart, SpecialistFinished, SpecialistStarted, StoredAppEvent, ToolCallPart, ToolOutcome, ToolResult, ToolStarted, TurnCompleted, TurnFailed, TurnStarted, UserInput, UserMessage, assistantTexts, attachmentLine, formatSize, isInput, isLibraryEvent, isTurnEnd, isUserInput, isWaking, maxAttachments, textWithAttachments };
492
+ export { AppTag, AssistantEvent, AssistantEvent_exports, AssistantMessage, Attachment, BlockAnswer, BlocksClosed, ChatDeleted, Entry, Input, MessageBlock, ModelOutput, ModelPart, SpecialistFinished, SpecialistStarted, StoredAppEvent, ToolCallPart, ToolOutcome, ToolResult, ToolStarted, TurnCompleted, TurnFailed, TurnStarted, UserInput, UserMessage, assistantTexts, attachmentLine, formatSize, isAppInput, isInput, isLibraryEvent, isTurnEnd, isUserInput, isWaking, maxAttachments, textWithAttachments };
522
493
 
523
494
  //# sourceMappingURL=AssistantEvent.js.map