effect-assistant 0.8.1 → 0.8.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,11 +13,8 @@ effect-assistant is a TypeScript library for chat assistants that work like a co
|
|
|
13
13
|
in the background with your tools, and it messages you back. It is built on [Effect](https://effect.website) and
|
|
14
14
|
`effect/ai`.
|
|
15
15
|
|
|
16
|
-
Each chat is one append-only log of events.
|
|
17
|
-
|
|
18
|
-
```text
|
|
19
|
-
{ model's history, user's timeline, what to resume } = f(chat log)
|
|
20
|
-
```
|
|
16
|
+
Each chat is one append-only log of events. What the model reads, what the user sees, and where a turn cut off by a
|
|
17
|
+
crash picks up are all read from that log.
|
|
21
18
|
|
|
22
19
|
## Quickstart
|
|
23
20
|
|
|
@@ -183,6 +180,90 @@ reads the last summary plus what came after; the log keeps everything, so the us
|
|
|
183
180
|
turn.run({ instructions, history: Compaction.since(Schema.is(Summary)) })
|
|
184
181
|
```
|
|
185
182
|
|
|
183
|
+
### Putting it together
|
|
184
|
+
|
|
185
|
+
One assistant with all of it: tools with an approval, a skill, memory, blocks that show a tool's result, a specialist,
|
|
186
|
+
a daily budget, and a summary after the reply once the chat is long. The budget and the summarizing are services of
|
|
187
|
+
your app; the handler decides what they mean for the chat. `MyTools` holds a `search_documents` tool and a
|
|
188
|
+
`send_email` tool with `needsApproval`; `ChooseOne` is a block, `WeeklyReport` a [skill](https://github.com/dbrxnds/effect-assistant/tree/main/apps/docs/guide/skills.md) and
|
|
189
|
+
`Research` a [specialist](https://github.com/dbrxnds/effect-assistant/tree/main/apps/docs/guide/specialists.md).
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
class Summary extends Schema.TaggedClass<Summary>()("Summary", { text: Schema.String, throughSequence: Schema.Int }) {}
|
|
193
|
+
class BudgetReached extends Schema.TaggedClass<BudgetReached>()("BudgetReached", {}) {}
|
|
194
|
+
|
|
195
|
+
type MyEvent = Summary | BudgetReached
|
|
196
|
+
|
|
197
|
+
export const MyAssistant = Assistant.make("my-assistant", {
|
|
198
|
+
blocks: BlockKit.make([ChooseOne, Block.fromToolResult(SearchDocuments)]),
|
|
199
|
+
events: [Summary, BudgetReached],
|
|
200
|
+
specialists: [Research]
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
class AiBudget extends Context.Service<
|
|
204
|
+
AiBudget,
|
|
205
|
+
{ readonly exceeded: (chatId: ChatId.ChatId) => Effect.Effect<boolean> }
|
|
206
|
+
>()("AiBudget") {}
|
|
207
|
+
|
|
208
|
+
class ChatCompaction extends Context.Service<
|
|
209
|
+
ChatCompaction,
|
|
210
|
+
{
|
|
211
|
+
readonly summarize: (
|
|
212
|
+
chat: Assistant.Chat<MyEvent>,
|
|
213
|
+
turn: Assistant.Turn<MyEvent>
|
|
214
|
+
) => Effect.Effect<Option.Option<Summary>, AiError.AiError>
|
|
215
|
+
}
|
|
216
|
+
>()("ChatCompaction") {}
|
|
217
|
+
|
|
218
|
+
const Skills = Skill.toolkit([WeeklyReport])
|
|
219
|
+
|
|
220
|
+
export const MyAssistantLive = MyAssistant.toLayer(
|
|
221
|
+
Effect.gen(function* () {
|
|
222
|
+
const budget = yield* AiBudget
|
|
223
|
+
const compaction = yield* ChatCompaction
|
|
224
|
+
const memory = yield* Memory.MemoryStore
|
|
225
|
+
|
|
226
|
+
return Effect.fn("MyAssistant.turn")(function* (turn: Assistant.Turn<MyEvent>) {
|
|
227
|
+
const chat = yield* MyAssistant.Chat
|
|
228
|
+
|
|
229
|
+
if (yield* budget.exceeded(turn.chatId)) {
|
|
230
|
+
yield* chat.append(BudgetReached.make({}))
|
|
231
|
+
|
|
232
|
+
return Stream.empty
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// After the reply, so the user doesn't wait; a failed summary leaves the chat as it is.
|
|
236
|
+
const summarized = compaction.summarize(chat, turn).pipe(
|
|
237
|
+
Effect.flatMap(Option.match({ onNone: () => Effect.void, onSome: (summary) => chat.append(summary) })),
|
|
238
|
+
Effect.catchCause((cause) => Effect.logWarning("compaction failed", cause))
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
return turn
|
|
242
|
+
.run({
|
|
243
|
+
instructions: `${instructions}\n\n${Memory.render(yield* memory.facts(turn.chatId))}`,
|
|
244
|
+
toolkit: Toolkit.merge(MyTools, Memory.Tools, Skills),
|
|
245
|
+
history: Compaction.since(Schema.is(Summary)),
|
|
246
|
+
maxSteps: 20
|
|
247
|
+
})
|
|
248
|
+
.pipe(
|
|
249
|
+
Stream.concat(Stream.drain(Stream.fromEffect(summarized))),
|
|
250
|
+
Stream.provideService(Memory.MemoryStore, memory)
|
|
251
|
+
)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
).pipe(Layer.provide([MyToolsLive, Memory.layerTools, Skills.layer, ResearchLive]))
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
```text
|
|
258
|
+
AiBudget.exceeded ─► BudgetReached ─► chat log (and no turn)
|
|
259
|
+
chat log ─► Compaction.since(Summary) ─► history ──┐
|
|
260
|
+
memory facts ─► instructions ──────────────────────┼─► turn.run ─► events ─► chat log
|
|
261
|
+
MyTools + Memory.Tools + Skills ─► toolkit ────────┘
|
|
262
|
+
ChatCompaction.summarize, after the reply ─► Summary ─► chat log
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
The app provides `AiBudget`, `ChatCompaction` and a `Memory.MemoryStore`.
|
|
266
|
+
|
|
186
267
|
### And more
|
|
187
268
|
|
|
188
269
|
- [Specialists](https://github.com/dbrxnds/effect-assistant/tree/main/apps/docs/guide/specialists.md): focused agents that do slow work in the background.
|
package/package.json
CHANGED