effect-assistant 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +22 -2
- package/dist/AppEvent.d.ts +111 -0
- package/dist/AppEvent.d.ts.map +1 -0
- package/dist/AppEvent.js +71 -0
- package/dist/AppEvent.js.map +1 -0
- package/dist/Assistant.d.ts +403 -0
- package/dist/Assistant.d.ts.map +1 -0
- package/dist/Assistant.js +126 -0
- package/dist/Assistant.js.map +1 -0
- package/dist/AssistantActivity.d.ts +85 -0
- package/dist/AssistantActivity.d.ts.map +1 -0
- package/dist/AssistantActivity.js +105 -0
- package/dist/AssistantActivity.js.map +1 -0
- package/dist/AssistantEngine.d.ts +116 -0
- package/dist/AssistantEngine.d.ts.map +1 -0
- package/dist/AssistantEngine.js +255 -0
- package/dist/AssistantEngine.js.map +1 -0
- package/dist/AssistantEvent.d.ts +614 -0
- package/dist/AssistantEvent.d.ts.map +1 -0
- package/dist/AssistantEvent.js +523 -0
- package/dist/AssistantEvent.js.map +1 -0
- package/dist/AssistantLoop.d.ts +171 -0
- package/dist/AssistantLoop.d.ts.map +1 -0
- package/dist/AssistantLoop.js +108 -0
- package/dist/AssistantLoop.js.map +1 -0
- package/dist/AssistantToolCall.d.ts +32 -0
- package/dist/AssistantToolCall.d.ts.map +1 -0
- package/dist/AssistantToolCall.js +37 -0
- package/dist/AssistantToolCall.js.map +1 -0
- package/dist/Block.d.ts +69 -0
- package/dist/Block.d.ts.map +1 -0
- package/dist/Block.js +39 -0
- package/dist/Block.js.map +1 -0
- package/dist/BlockKit.d.ts +184 -0
- package/dist/BlockKit.d.ts.map +1 -0
- package/dist/BlockKit.js +151 -0
- package/dist/BlockKit.js.map +1 -0
- package/dist/ChatId.d.ts +31 -0
- package/dist/ChatId.d.ts.map +1 -0
- package/dist/ChatId.js +36 -0
- package/dist/ChatId.js.map +1 -0
- package/dist/ChatLog.d.ts +109 -0
- package/dist/ChatLog.d.ts.map +1 -0
- package/dist/ChatLog.js +177 -0
- package/dist/ChatLog.js.map +1 -0
- package/dist/ChatView.d.ts +459 -0
- package/dist/ChatView.d.ts.map +1 -0
- package/dist/ChatView.js +496 -0
- package/dist/ChatView.js.map +1 -0
- package/dist/Chats.d.ts +110 -0
- package/dist/Chats.d.ts.map +1 -0
- package/dist/Chats.js +98 -0
- package/dist/Chats.js.map +1 -0
- package/dist/Compaction.d.ts +105 -0
- package/dist/Compaction.d.ts.map +1 -0
- package/dist/Compaction.js +133 -0
- package/dist/Compaction.js.map +1 -0
- package/dist/CurrentChat.d.ts +40 -0
- package/dist/CurrentChat.d.ts.map +1 -0
- package/dist/CurrentChat.js +33 -0
- package/dist/CurrentChat.js.map +1 -0
- package/dist/Decision.d.ts +145 -0
- package/dist/Decision.d.ts.map +1 -0
- package/dist/Decision.js +178 -0
- package/dist/Decision.js.map +1 -0
- package/dist/DecisionId.d.ts +37 -0
- package/dist/DecisionId.d.ts.map +1 -0
- package/dist/DecisionId.js +44 -0
- package/dist/DecisionId.js.map +1 -0
- package/dist/InputId.d.ts +38 -0
- package/dist/InputId.d.ts.map +1 -0
- package/dist/InputId.js +43 -0
- package/dist/InputId.js.map +1 -0
- package/dist/Instructions.d.ts +56 -0
- package/dist/Instructions.d.ts.map +1 -0
- package/dist/Instructions.js +50 -0
- package/dist/Instructions.js.map +1 -0
- package/dist/Memory.d.ts +190 -0
- package/dist/Memory.d.ts.map +1 -0
- package/dist/Memory.js +216 -0
- package/dist/Memory.js.map +1 -0
- package/dist/RunId.d.ts +41 -0
- package/dist/RunId.d.ts.map +1 -0
- package/dist/RunId.js +46 -0
- package/dist/RunId.js.map +1 -0
- package/dist/Skill.d.ts +72 -0
- package/dist/Skill.d.ts.map +1 -0
- package/dist/Skill.js +58 -0
- package/dist/Skill.js.map +1 -0
- package/dist/Specialist.d.ts +267 -0
- package/dist/Specialist.d.ts.map +1 -0
- package/dist/Specialist.js +119 -0
- package/dist/Specialist.js.map +1 -0
- package/dist/ToolDiscovery.d.ts +108 -0
- package/dist/ToolDiscovery.d.ts.map +1 -0
- package/dist/ToolDiscovery.js +139 -0
- package/dist/ToolDiscovery.js.map +1 -0
- package/dist/ToolFailure.d.ts +66 -0
- package/dist/ToolFailure.d.ts.map +1 -0
- package/dist/ToolFailure.js +79 -0
- package/dist/ToolFailure.js.map +1 -0
- package/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/internal/chat.js +202 -0
- package/dist/internal/chat.js.map +1 -0
- package/dist/internal/decisions.js +66 -0
- package/dist/internal/decisions.js.map +1 -0
- package/dist/internal/events.d.ts +46 -0
- package/dist/internal/events.d.ts.map +1 -0
- package/dist/internal/events.js +65 -0
- package/dist/internal/events.js.map +1 -0
- package/dist/internal/history.js +185 -0
- package/dist/internal/history.js.map +1 -0
- package/dist/internal/instructions.js +11 -0
- package/dist/internal/instructions.js.map +1 -0
- package/dist/internal/loop.d.ts +73 -0
- package/dist/internal/loop.d.ts.map +1 -0
- package/dist/internal/loop.js +166 -0
- package/dist/internal/loop.js.map +1 -0
- package/dist/internal/schema.js +14 -0
- package/dist/internal/schema.js.map +1 -0
- package/dist/internal/skills.js +28 -0
- package/dist/internal/skills.js.map +1 -0
- package/dist/internal/specialists.js +273 -0
- package/dist/internal/specialists.js.map +1 -0
- package/dist/internal/telemetry.js +85 -0
- package/dist/internal/telemetry.js.map +1 -0
- package/dist/internal/toolkit.d.ts +76 -0
- package/dist/internal/toolkit.d.ts.map +1 -0
- package/dist/internal/toolkit.js +129 -0
- package/dist/internal/toolkit.js.map +1 -0
- package/dist/internal/tools.js +132 -0
- package/dist/internal/tools.js.map +1 -0
- package/dist/internal/turns.d.ts +64 -0
- package/dist/internal/turns.d.ts.map +1 -0
- package/dist/internal/turns.js +221 -0
- package/dist/internal/turns.js.map +1 -0
- package/dist/internal/uuid.js +45 -0
- package/dist/internal/uuid.js.map +1 -0
- package/dist/internal/writer.d.ts +12 -0
- package/dist/internal/writer.d.ts.map +1 -0
- package/package.json +51 -3
- package/src/AppEvent.ts +134 -0
- package/src/Assistant.ts +589 -0
- package/src/AssistantActivity.ts +150 -0
- package/src/AssistantEngine.ts +343 -0
- package/src/AssistantEvent.ts +635 -0
- package/src/AssistantLoop.ts +207 -0
- package/src/AssistantToolCall.ts +40 -0
- package/src/Block.ts +84 -0
- package/src/BlockKit.ts +311 -0
- package/src/ChatId.ts +36 -0
- package/src/ChatLog.ts +247 -0
- package/src/ChatView.ts +658 -0
- package/src/Chats.ts +122 -0
- package/src/Compaction.ts +224 -0
- package/src/CurrentChat.ts +47 -0
- package/src/Decision.ts +241 -0
- package/src/DecisionId.ts +46 -0
- package/src/InputId.ts +43 -0
- package/src/Instructions.ts +72 -0
- package/src/Memory.ts +276 -0
- package/src/RunId.ts +46 -0
- package/src/Skill.ts +91 -0
- package/src/Specialist.ts +355 -0
- package/src/ToolDiscovery.ts +187 -0
- package/src/ToolFailure.ts +74 -0
- package/src/index.ts +130 -0
- package/src/internal/chat.ts +372 -0
- package/src/internal/decisions.ts +131 -0
- package/src/internal/events.ts +100 -0
- package/src/internal/history.ts +366 -0
- package/src/internal/instructions.ts +9 -0
- package/src/internal/loop.ts +344 -0
- package/src/internal/schema.ts +25 -0
- package/src/internal/skills.ts +37 -0
- package/src/internal/specialists.ts +414 -0
- package/src/internal/telemetry.ts +125 -0
- package/src/internal/toolkit.ts +231 -0
- package/src/internal/tools.ts +210 -0
- package/src/internal/turns.ts +429 -0
- package/src/internal/uuid.ts +50 -0
- package/src/internal/writer.ts +12 -0
package/dist/Chats.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { ChatId } from "./ChatId.js";
|
|
3
|
+
import * as Context from "effect/Context";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
import * as Schema from "effect/Schema";
|
|
6
|
+
//#region src/Chats.ts
|
|
7
|
+
/**
|
|
8
|
+
* This module provides the `Chats` service, through which the app tells the library which chats
|
|
9
|
+
* exist.
|
|
10
|
+
*
|
|
11
|
+
* The app keeps its own record of each chat (its owner, its title, whether it was deleted) and
|
|
12
|
+
* decides when a chat is created: up front, or on its first message. The library asks `Chats`
|
|
13
|
+
* before it stores an input and before each model call. When the app reports a chat as deleted,
|
|
14
|
+
* the library deletes it as well (see `Assistant.delete`).
|
|
15
|
+
*
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
*/
|
|
18
|
+
var Chats_exports = /* @__PURE__ */ __exportAll({
|
|
19
|
+
Chats: () => Chats,
|
|
20
|
+
GetChatError: () => GetChatError,
|
|
21
|
+
Reason: () => Reason
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Schema for the reason a chat can't be used. `"NotFound"` means the app doesn't know the chat,
|
|
25
|
+
* and `"Deleted"` means the app deleted it.
|
|
26
|
+
*
|
|
27
|
+
* @category schemas
|
|
28
|
+
* @since 0.1.0
|
|
29
|
+
*/
|
|
30
|
+
const Reason = Schema.Literals(["NotFound", "Deleted"]);
|
|
31
|
+
/**
|
|
32
|
+
* Represents the error that `get` fails with when a chat can't be used. It carries the chat's id
|
|
33
|
+
* and the {@link Reason}. An input for such a chat isn't stored.
|
|
34
|
+
*
|
|
35
|
+
* **Example** (Failing for an unknown chat)
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* if (row === undefined) return yield* new GetChatError({ chatId, reason: "NotFound" })
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @category errors
|
|
42
|
+
* @since 0.1.0
|
|
43
|
+
*/
|
|
44
|
+
var GetChatError = class extends Schema.Error("effect-assistant/Chats/GetChatError")({
|
|
45
|
+
_tag: Schema.tag("GetChatError"),
|
|
46
|
+
chatId: ChatId,
|
|
47
|
+
reason: Reason
|
|
48
|
+
}) {};
|
|
49
|
+
/**
|
|
50
|
+
* Service that tells the library which of the app's chats exist.
|
|
51
|
+
*
|
|
52
|
+
* **Details**
|
|
53
|
+
*
|
|
54
|
+
* By default every chat exists from its first input, and a chat is deleted only when its log
|
|
55
|
+
* says so (`ChatDeleted`). Provide your own implementation to have the app decide. Provide it
|
|
56
|
+
* where you provide the `ChatLog`, so the engine and your server see the same one.
|
|
57
|
+
*
|
|
58
|
+
* **Example** (Reading chats from the app's own table)
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* import { type ChatId, Chats } from "effect-assistant"
|
|
62
|
+
* import * as Effect from "effect/Effect"
|
|
63
|
+
* import * as Layer from "effect/Layer"
|
|
64
|
+
* import * as SqlClient from "effect/sql/SqlClient"
|
|
65
|
+
*
|
|
66
|
+
* const ChatsLive = Layer.effect(
|
|
67
|
+
* Chats.Chats,
|
|
68
|
+
* Effect.gen(function* () {
|
|
69
|
+
* const sql = yield* SqlClient.SqlClient
|
|
70
|
+
*
|
|
71
|
+
* return {
|
|
72
|
+
* get: Effect.fn(function* (chatId: ChatId.ChatId) {
|
|
73
|
+
* const [row] = yield* sql<{ readonly deleted_at: number | null }>`
|
|
74
|
+
* SELECT deleted_at FROM chats WHERE chat_id = ${chatId}
|
|
75
|
+
* `.pipe(Effect.orDie)
|
|
76
|
+
*
|
|
77
|
+
* if (row === undefined) {
|
|
78
|
+
* return yield* new Chats.GetChatError({ chatId, reason: "NotFound" })
|
|
79
|
+
* }
|
|
80
|
+
* if (row.deleted_at !== null) {
|
|
81
|
+
* return yield* new Chats.GetChatError({ chatId, reason: "Deleted" })
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* return { chatId }
|
|
85
|
+
* })
|
|
86
|
+
* }
|
|
87
|
+
* })
|
|
88
|
+
* )
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @category services
|
|
92
|
+
* @since 0.1.0
|
|
93
|
+
*/
|
|
94
|
+
const Chats = Context.Reference("effect-assistant/Chats", { defaultValue: () => ({ get: (chatId) => Effect.succeed({ chatId }) }) });
|
|
95
|
+
//#endregion
|
|
96
|
+
export { Chats, Chats_exports, GetChatError, Reason };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=Chats.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Chats.js","names":[],"sources":["../src/Chats.ts"],"sourcesContent":["/**\n * This module provides the `Chats` service, through which the app tells the library which chats\n * exist.\n *\n * The app keeps its own record of each chat (its owner, its title, whether it was deleted) and\n * decides when a chat is created: up front, or on its first message. The library asks `Chats`\n * before it stores an input and before each model call. When the app reports a chat as deleted,\n * the library deletes it as well (see `Assistant.delete`).\n *\n * @since 0.1.0\n */\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Schema from \"effect/Schema\"\nimport { ChatId } from \"./ChatId.ts\"\n\n/**\n * Schema for the reason a chat can't be used. `\"NotFound\"` means the app doesn't know the chat,\n * and `\"Deleted\"` means the app deleted it.\n *\n * @category schemas\n * @since 0.1.0\n */\nexport const Reason = Schema.Literals([\"NotFound\", \"Deleted\"])\n\n/**\n * The reason a chat can't be used.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Reason = typeof Reason.Type\n\n/**\n * Represents the error that `get` fails with when a chat can't be used. It carries the chat's id\n * and the {@link Reason}. An input for such a chat isn't stored.\n *\n * **Example** (Failing for an unknown chat)\n *\n * ```ts\n * if (row === undefined) return yield* new GetChatError({ chatId, reason: \"NotFound\" })\n * ```\n *\n * @category errors\n * @since 0.1.0\n */\nexport class GetChatError extends Schema.Error<GetChatError>(\"effect-assistant/Chats/GetChatError\")({\n _tag: Schema.tag(\"GetChatError\"),\n chatId: ChatId,\n reason: Reason\n}) {}\n\n/**\n * Represents what the library needs to know about a chat, which is its id. The app's own record\n * can carry more.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ChatRecord {\n readonly chatId: ChatId\n}\n\n/**\n * Represents the app's chats, as the `Chats` service exposes them.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service {\n /** Returns the chat, or fails with a `GetChatError` that says why it can't be used. */\n readonly get: (chatId: ChatId) => Effect.Effect<ChatRecord, GetChatError>\n}\n\n/**\n * Service that tells the library which of the app's chats exist.\n *\n * **Details**\n *\n * By default every chat exists from its first input, and a chat is deleted only when its log\n * says so (`ChatDeleted`). Provide your own implementation to have the app decide. Provide it\n * where you provide the `ChatLog`, so the engine and your server see the same one.\n *\n * **Example** (Reading chats from the app's own table)\n *\n * ```ts\n * import { type ChatId, Chats } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n * import * as Layer from \"effect/Layer\"\n * import * as SqlClient from \"effect/sql/SqlClient\"\n *\n * const ChatsLive = Layer.effect(\n * Chats.Chats,\n * Effect.gen(function* () {\n * const sql = yield* SqlClient.SqlClient\n *\n * return {\n * get: Effect.fn(function* (chatId: ChatId.ChatId) {\n * const [row] = yield* sql<{ readonly deleted_at: number | null }>`\n * SELECT deleted_at FROM chats WHERE chat_id = ${chatId}\n * `.pipe(Effect.orDie)\n *\n * if (row === undefined) {\n * return yield* new Chats.GetChatError({ chatId, reason: \"NotFound\" })\n * }\n * if (row.deleted_at !== null) {\n * return yield* new Chats.GetChatError({ chatId, reason: \"Deleted\" })\n * }\n *\n * return { chatId }\n * })\n * }\n * })\n * )\n * ```\n *\n * @category services\n * @since 0.1.0\n */\nexport const Chats: Context.Reference<Service> = Context.Reference<Service>(\"effect-assistant/Chats\", {\n defaultValue: () => ({ get: (chatId) => Effect.succeed({ chatId }) })\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuBA,MAAa,SAAS,OAAO,SAAS,CAAC,YAAY,SAAS,CAAC;;;;;;;;;;;;;;AAuB7D,IAAa,eAAb,cAAkC,OAAO,MAAoB,qCAAqC,CAAC,CAAC;CAClG,MAAM,OAAO,IAAI,cAAc;CAC/B,QAAQ;CACR,QAAQ;AACV,CAAC,CAAC,CAAC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqEJ,MAAa,QAAoC,QAAQ,UAAmB,0BAA0B,EACpG,qBAAqB,EAAE,MAAM,WAAW,OAAO,QAAQ,EAAE,OAAO,CAAC,EAAE,GACrE,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { AnyEntry, AssistantEvent_d_exports } from "./AssistantEvent.js";
|
|
2
|
+
import { Assistant_d_exports, Entry } from "./Assistant.js";
|
|
3
|
+
import * as Option from "effect/Option";
|
|
4
|
+
import * as Prompt from "effect/ai/Prompt";
|
|
5
|
+
declare namespace Compaction_d_exports {
|
|
6
|
+
export { Summary, attachedFiles, inputTokens, openItems, since };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Represents the fields a summary event must have: its text and the last `seq` it covers.
|
|
10
|
+
*
|
|
11
|
+
* Entries after `throughSeq` are kept as they are, so an input that arrived while the summary was
|
|
12
|
+
* being written isn't lost.
|
|
13
|
+
*
|
|
14
|
+
* @category models
|
|
15
|
+
* @since 0.1.0
|
|
16
|
+
*/
|
|
17
|
+
export interface Summary {
|
|
18
|
+
readonly _tag: string;
|
|
19
|
+
readonly text: string;
|
|
20
|
+
readonly throughSeq: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the number of input tokens used by the chat's last model call, which shows how large
|
|
24
|
+
* the prompt has grown. Returns 0 before the first call.
|
|
25
|
+
*
|
|
26
|
+
* @category utils
|
|
27
|
+
* @since 0.1.0
|
|
28
|
+
*/
|
|
29
|
+
export declare const inputTokens: (entries: ReadonlyArray<AnyEntry>) => number;
|
|
30
|
+
/**
|
|
31
|
+
* Returns the items that were open at `throughSeq` and are still open: decisions waiting for the
|
|
32
|
+
* user, blocks the user hasn't answered, and specialists that are working. The result is text for
|
|
33
|
+
* the model, or `None` when nothing is open.
|
|
34
|
+
*
|
|
35
|
+
* @category utils
|
|
36
|
+
* @since 0.1.0
|
|
37
|
+
*/
|
|
38
|
+
export declare const openItems: (entries: ReadonlyArray<AnyEntry>, throughSeq: number) => Option.Option<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the files the user attached up to `throughSeq`, as lines of text for the model, or
|
|
41
|
+
* `None` when there are none. A summary may leave out a file's id, which tools need.
|
|
42
|
+
*
|
|
43
|
+
* @category utils
|
|
44
|
+
* @since 0.1.0
|
|
45
|
+
*/
|
|
46
|
+
export declare const attachedFiles: (entries: ReadonlyArray<AnyEntry>, throughSeq: number) => Option.Option<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a history view that starts at the last summary.
|
|
49
|
+
*
|
|
50
|
+
* **Details**
|
|
51
|
+
*
|
|
52
|
+
* `isSummary` identifies the app's summary events. The model reads the last summary, the files
|
|
53
|
+
* attached earlier ({@link attachedFiles}), and the items that are still open
|
|
54
|
+
* ({@link openItems}), followed by the entries after the summary. When the chat has no summary,
|
|
55
|
+
* the view returns every entry.
|
|
56
|
+
*
|
|
57
|
+
* **Example** (Compacting a long chat after each turn)
|
|
58
|
+
*
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { AppEvent, Assistant, type AssistantEvent, Compaction } from "effect-assistant"
|
|
61
|
+
* import * as LanguageModel from "effect/ai/LanguageModel"
|
|
62
|
+
* import * as Effect from "effect/Effect"
|
|
63
|
+
* import * as Schema from "effect/Schema"
|
|
64
|
+
* import * as Stream from "effect/Stream"
|
|
65
|
+
*
|
|
66
|
+
* class Summary extends Schema.TaggedClass<Summary>()("Summary", {
|
|
67
|
+
* text: Schema.String,
|
|
68
|
+
* throughSeq: Schema.Int
|
|
69
|
+
* }) {}
|
|
70
|
+
*
|
|
71
|
+
* declare const transcript: (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => string
|
|
72
|
+
*
|
|
73
|
+
* export const MyAssistant = Assistant.make("my-assistant", { events: [AppEvent.make(Summary)] })
|
|
74
|
+
*
|
|
75
|
+
* const MyAssistantLive = MyAssistant.toLayer((turn) => {
|
|
76
|
+
* const compact = Effect.gen(function* () {
|
|
77
|
+
* const chat = yield* MyAssistant.Chat
|
|
78
|
+
* const entries = yield* chat.entries
|
|
79
|
+
*
|
|
80
|
+
* if (Compaction.inputTokens(entries) < 100_000) return
|
|
81
|
+
*
|
|
82
|
+
* const { text } = yield* LanguageModel.generateText({
|
|
83
|
+
* prompt: `Summarize this chat:\n\n${transcript(entries)}`
|
|
84
|
+
* })
|
|
85
|
+
*
|
|
86
|
+
* yield* chat.append(Summary.make({ text, throughSeq: entries.at(-1)?.seq ?? 0 }))
|
|
87
|
+
* })
|
|
88
|
+
*
|
|
89
|
+
* // After the reply, so the user doesn't wait for the summary.
|
|
90
|
+
* return Stream.concat(
|
|
91
|
+
* turn.loop({ instructions: "Be brief.", history: Compaction.since(Schema.is(Summary)) }),
|
|
92
|
+
* Stream.drain(Stream.fromEffect(compact))
|
|
93
|
+
* )
|
|
94
|
+
* })
|
|
95
|
+
* ```
|
|
96
|
+
*
|
|
97
|
+
* @category utils
|
|
98
|
+
* @since 0.1.0
|
|
99
|
+
*/
|
|
100
|
+
export declare const since: (isSummary: (event: unknown) => event is Summary) => <Events extends {
|
|
101
|
+
readonly _tag: string;
|
|
102
|
+
}>(entries: ReadonlyArray<Entry<Events>>) => ReadonlyArray<Entry<Events> | Prompt.Message>;
|
|
103
|
+
//#endregion
|
|
104
|
+
export { Compaction_d_exports };
|
|
105
|
+
//# sourceMappingURL=Compaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Compaction.d.ts","names":[],"sources":["../src/Compaction.ts"],"mappings":";;;;;;;;;;;;;;;;iBAgCiB;WACN;WACA;WACA;;;;;;;;;qBAUE,cAAW,SAAa,cAAc;;;;;;;;;qBAmBtC,YAAS,SACX,cAAc,WAAwB,uBAE9C,OAAO;;;;;;;;qBAmEG,gBAAa,SACf,cAAc,WAAwB,uBAE9C,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA+DG,QAAK,YACH,mBAAmB,SAAS,aACxC;WAA0B;GAAc,SAC9B,cAAc,MAAgB,aACtC,cAAc,MAAgB,UAAU,OAAO"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { attachmentLine, isLibraryEvent } from "./AssistantEvent.js";
|
|
3
|
+
import { fromEntries } from "./Decision.js";
|
|
4
|
+
import { fromEntries as fromEntries$1 } from "./BlockKit.js";
|
|
5
|
+
import { toJsonText } from "./internal/schema.js";
|
|
6
|
+
import { userText } from "./internal/history.js";
|
|
7
|
+
import { fromEntries as fromEntries$2 } from "./AssistantActivity.js";
|
|
8
|
+
import * as Predicate from "effect/Predicate";
|
|
9
|
+
import * as Option from "effect/Option";
|
|
10
|
+
//#region src/Compaction.ts
|
|
11
|
+
var Compaction_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
attachedFiles: () => attachedFiles,
|
|
13
|
+
inputTokens: () => inputTokens,
|
|
14
|
+
openItems: () => openItems,
|
|
15
|
+
since: () => since
|
|
16
|
+
});
|
|
17
|
+
/**
|
|
18
|
+
* Returns the number of input tokens used by the chat's last model call, which shows how large
|
|
19
|
+
* the prompt has grown. Returns 0 before the first call.
|
|
20
|
+
*
|
|
21
|
+
* @category utils
|
|
22
|
+
* @since 0.1.0
|
|
23
|
+
*/
|
|
24
|
+
const inputTokens = (entries) => {
|
|
25
|
+
const last = entries.findLast(({ event }) => isLibraryEvent(event) && Predicate.isTagged(event, "ModelOutput") && event.usage !== void 0)?.event;
|
|
26
|
+
return last !== void 0 && isLibraryEvent(last) && Predicate.isTagged(last, "ModelOutput") ? last.usage?.inputTokens.total ?? 0 : 0;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Returns the items that were open at `throughSeq` and are still open: decisions waiting for the
|
|
30
|
+
* user, blocks the user hasn't answered, and specialists that are working. The result is text for
|
|
31
|
+
* the model, or `None` when nothing is open.
|
|
32
|
+
*
|
|
33
|
+
* @category utils
|
|
34
|
+
* @since 0.1.0
|
|
35
|
+
*/
|
|
36
|
+
const openItems = (entries, throughSeq) => {
|
|
37
|
+
const seqOf = (found) => entries.find(({ event }) => found(event))?.seq ?? Number.POSITIVE_INFINITY;
|
|
38
|
+
const opened = (seq) => seq <= throughSeq;
|
|
39
|
+
const decisions = [...fromEntries(entries).values()].flatMap((state) => state.outcome === void 0 && opened(seqOf((event) => isLibraryEvent(event) && Predicate.isTagged(event, "DecisionRequested") && event.id === state.request.id)) ? [`- decision ${state.request.id}, for ${state.request.origin.tool}: ${toJsonText(state.request.origin.params)}`] : []);
|
|
40
|
+
const blocks = [...fromEntries$1(entries)].flatMap(([messageId, state]) => {
|
|
41
|
+
const shown = entries.find(({ event }) => isLibraryEvent(event) && Predicate.isTagged(event, "AssistantMessage") && event.id === messageId);
|
|
42
|
+
const message = shown?.event;
|
|
43
|
+
return state.open && shown !== void 0 && opened(shown.seq) && message !== void 0 && isLibraryEvent(message) && Predicate.isTagged(message, "AssistantMessage") ? [`- message ${messageId}, a ${message.block?.kind} block: ${message.text}`] : [];
|
|
44
|
+
});
|
|
45
|
+
const specialists = [...fromEntries$2(entries).specialists.values()].flatMap((run) => opened(seqOf((event) => isLibraryEvent(event) && Predicate.isTagged(event, "SpecialistStarted") && event.runId === run.runId)) ? [`- your ${run.specialist} specialist (${run.runId}), working on: ${run.goal}`] : []);
|
|
46
|
+
const sections = [
|
|
47
|
+
decisions.length > 0 ? `Decisions waiting for the user:\n${decisions.join("\n")}` : "",
|
|
48
|
+
blocks.length > 0 ? `Blocks the user hasn't answered:\n${blocks.join("\n")}` : "",
|
|
49
|
+
specialists.length > 0 ? `Specialists still working:\n${specialists.join("\n")}` : ""
|
|
50
|
+
].filter((section) => section.length > 0);
|
|
51
|
+
return sections.length === 0 ? Option.none() : Option.some(sections.join("\n\n"));
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Returns the files the user attached up to `throughSeq`, as lines of text for the model, or
|
|
55
|
+
* `None` when there are none. A summary may leave out a file's id, which tools need.
|
|
56
|
+
*
|
|
57
|
+
* @category utils
|
|
58
|
+
* @since 0.1.0
|
|
59
|
+
*/
|
|
60
|
+
const attachedFiles = (entries, throughSeq) => {
|
|
61
|
+
const lines = entries.flatMap(({ seq, event }) => seq <= throughSeq && isLibraryEvent(event) && Predicate.isTagged(event, "UserMessage") ? (event.attachments ?? []).map(attachmentLine) : []);
|
|
62
|
+
return lines.length === 0 ? Option.none() : Option.some(lines.join("\n"));
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Creates a history view that starts at the last summary.
|
|
66
|
+
*
|
|
67
|
+
* **Details**
|
|
68
|
+
*
|
|
69
|
+
* `isSummary` identifies the app's summary events. The model reads the last summary, the files
|
|
70
|
+
* attached earlier ({@link attachedFiles}), and the items that are still open
|
|
71
|
+
* ({@link openItems}), followed by the entries after the summary. When the chat has no summary,
|
|
72
|
+
* the view returns every entry.
|
|
73
|
+
*
|
|
74
|
+
* **Example** (Compacting a long chat after each turn)
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { AppEvent, Assistant, type AssistantEvent, Compaction } from "effect-assistant"
|
|
78
|
+
* import * as LanguageModel from "effect/ai/LanguageModel"
|
|
79
|
+
* import * as Effect from "effect/Effect"
|
|
80
|
+
* import * as Schema from "effect/Schema"
|
|
81
|
+
* import * as Stream from "effect/Stream"
|
|
82
|
+
*
|
|
83
|
+
* class Summary extends Schema.TaggedClass<Summary>()("Summary", {
|
|
84
|
+
* text: Schema.String,
|
|
85
|
+
* throughSeq: Schema.Int
|
|
86
|
+
* }) {}
|
|
87
|
+
*
|
|
88
|
+
* declare const transcript: (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => string
|
|
89
|
+
*
|
|
90
|
+
* export const MyAssistant = Assistant.make("my-assistant", { events: [AppEvent.make(Summary)] })
|
|
91
|
+
*
|
|
92
|
+
* const MyAssistantLive = MyAssistant.toLayer((turn) => {
|
|
93
|
+
* const compact = Effect.gen(function* () {
|
|
94
|
+
* const chat = yield* MyAssistant.Chat
|
|
95
|
+
* const entries = yield* chat.entries
|
|
96
|
+
*
|
|
97
|
+
* if (Compaction.inputTokens(entries) < 100_000) return
|
|
98
|
+
*
|
|
99
|
+
* const { text } = yield* LanguageModel.generateText({
|
|
100
|
+
* prompt: `Summarize this chat:\n\n${transcript(entries)}`
|
|
101
|
+
* })
|
|
102
|
+
*
|
|
103
|
+
* yield* chat.append(Summary.make({ text, throughSeq: entries.at(-1)?.seq ?? 0 }))
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* // After the reply, so the user doesn't wait for the summary.
|
|
107
|
+
* return Stream.concat(
|
|
108
|
+
* turn.loop({ instructions: "Be brief.", history: Compaction.since(Schema.is(Summary)) }),
|
|
109
|
+
* Stream.drain(Stream.fromEffect(compact))
|
|
110
|
+
* )
|
|
111
|
+
* })
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @category utils
|
|
115
|
+
* @since 0.1.0
|
|
116
|
+
*/
|
|
117
|
+
const since = (isSummary) => (entries) => {
|
|
118
|
+
const last = entries.findLast(({ event }) => isSummary(event))?.event;
|
|
119
|
+
if (last === void 0 || !isSummary(last)) return entries;
|
|
120
|
+
const open = Option.match(openItems(entries, last.throughSeq), {
|
|
121
|
+
onNone: () => "",
|
|
122
|
+
onSome: (items) => `\n\n<still_open>\n${items}\n</still_open>`
|
|
123
|
+
});
|
|
124
|
+
const files = Option.match(attachedFiles(entries, last.throughSeq), {
|
|
125
|
+
onNone: () => "",
|
|
126
|
+
onSome: (lines) => `\n\n<files_attached_earlier>\n${lines}\n</files_attached_earlier>`
|
|
127
|
+
});
|
|
128
|
+
return [userText(`<earlier_in_this_chat>\n${last.text}\n</earlier_in_this_chat>${files}${open}`), ...entries.filter(({ seq, event }) => seq > last.throughSeq && !isSummary(event))];
|
|
129
|
+
};
|
|
130
|
+
//#endregion
|
|
131
|
+
export { Compaction_exports, attachedFiles, inputTokens, openItems, since };
|
|
132
|
+
|
|
133
|
+
//# sourceMappingURL=Compaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Compaction.js","names":["AssistantEvent.isLibraryEvent","Decision.fromEntries","BlockKit.fromEntries","AssistantActivity.fromEntries","AssistantEvent.attachmentLine"],"sources":["../src/Compaction.ts"],"sourcesContent":["/**\n * This module provides what a long chat needs so that the model can read a summary in place of\n * everything before it.\n *\n * The app decides when to compact, which model writes the summary, and what the summary says.\n * Its handler reads the chat's entries, has a model write a summary, and stores the summary as one\n * of the assistant's own events (see {@link Summary}). Its loop then reads the chat through\n * {@link since}. {@link inputTokens} helps decide when the prompt has grown large enough to\n * compact. The log keeps every entry, so the user still sees the whole chat.\n *\n * @since 0.1.0\n */\nimport type * as Prompt from \"effect/ai/Prompt\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport type * as Assistant from \"./Assistant.ts\"\nimport * as AssistantActivity from \"./AssistantActivity.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport * as BlockKit from \"./BlockKit.ts\"\nimport * as Decision from \"./Decision.ts\"\nimport { userText } from \"./internal/history.ts\"\nimport { toJsonText } from \"./internal/schema.ts\"\n\n/**\n * Represents the fields a summary event must have: its text and the last `seq` it covers.\n *\n * Entries after `throughSeq` are kept as they are, so an input that arrived while the summary was\n * being written isn't lost.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Summary {\n readonly _tag: string\n readonly text: string\n readonly throughSeq: number\n}\n\n/**\n * Returns the number of input tokens used by the chat's last model call, which shows how large\n * the prompt has grown. Returns 0 before the first call.\n *\n * @category utils\n * @since 0.1.0\n */\nexport const inputTokens = (entries: ReadonlyArray<AssistantEvent.AnyEntry>): number => {\n const last = entries.findLast(\n ({ event }) =>\n AssistantEvent.isLibraryEvent(event) && Predicate.isTagged(event, \"ModelOutput\") && event.usage !== undefined\n )?.event\n\n return last !== undefined && AssistantEvent.isLibraryEvent(last) && Predicate.isTagged(last, \"ModelOutput\")\n ? (last.usage?.inputTokens.total ?? 0)\n : 0\n}\n\n/**\n * Returns the items that were open at `throughSeq` and are still open: decisions waiting for the\n * user, blocks the user hasn't answered, and specialists that are working. The result is text for\n * the model, or `None` when nothing is open.\n *\n * @category utils\n * @since 0.1.0\n */\nexport const openItems = (\n entries: ReadonlyArray<AssistantEvent.AnyEntry>,\n throughSeq: number\n): Option.Option<string> => {\n const seqOf = (found: (event: AssistantEvent.AnyEntry[\"event\"]) => boolean) =>\n entries.find(({ event }) => found(event))?.seq ?? Number.POSITIVE_INFINITY\n\n const opened = (seq: number) => seq <= throughSeq\n\n const decisions = [...Decision.fromEntries(entries).values()].flatMap((state) =>\n state.outcome === undefined &&\n opened(\n seqOf(\n (event) =>\n AssistantEvent.isLibraryEvent(event) &&\n Predicate.isTagged(event, \"DecisionRequested\") &&\n event.id === state.request.id\n )\n )\n ? [`- decision ${state.request.id}, for ${state.request.origin.tool}: ${toJsonText(state.request.origin.params)}`]\n : []\n )\n\n const blocks = [...BlockKit.fromEntries(entries)].flatMap(([messageId, state]) => {\n const shown = entries.find(\n ({ event }) =>\n AssistantEvent.isLibraryEvent(event) && Predicate.isTagged(event, \"AssistantMessage\") && event.id === messageId\n )\n\n const message = shown?.event\n\n return state.open &&\n shown !== undefined &&\n opened(shown.seq) &&\n message !== undefined &&\n AssistantEvent.isLibraryEvent(message) &&\n Predicate.isTagged(message, \"AssistantMessage\")\n ? [`- message ${messageId}, a ${message.block?.kind} block: ${message.text}`]\n : []\n })\n\n const specialists = [...AssistantActivity.fromEntries(entries).specialists.values()].flatMap((run) =>\n opened(\n seqOf(\n (event) =>\n AssistantEvent.isLibraryEvent(event) &&\n Predicate.isTagged(event, \"SpecialistStarted\") &&\n event.runId === run.runId\n )\n )\n ? [`- your ${run.specialist} specialist (${run.runId}), working on: ${run.goal}`]\n : []\n )\n\n const sections = [\n decisions.length > 0 ? `Decisions waiting for the user:\\n${decisions.join(\"\\n\")}` : \"\",\n blocks.length > 0 ? `Blocks the user hasn't answered:\\n${blocks.join(\"\\n\")}` : \"\",\n specialists.length > 0 ? `Specialists still working:\\n${specialists.join(\"\\n\")}` : \"\"\n ].filter((section) => section.length > 0)\n\n return sections.length === 0 ? Option.none() : Option.some(sections.join(\"\\n\\n\"))\n}\n\n/**\n * Returns the files the user attached up to `throughSeq`, as lines of text for the model, or\n * `None` when there are none. A summary may leave out a file's id, which tools need.\n *\n * @category utils\n * @since 0.1.0\n */\nexport const attachedFiles = (\n entries: ReadonlyArray<AssistantEvent.AnyEntry>,\n throughSeq: number\n): Option.Option<string> => {\n const lines = entries.flatMap(({ seq, event }) =>\n seq <= throughSeq && AssistantEvent.isLibraryEvent(event) && Predicate.isTagged(event, \"UserMessage\")\n ? (event.attachments ?? []).map(AssistantEvent.attachmentLine)\n : []\n )\n\n return lines.length === 0 ? Option.none() : Option.some(lines.join(\"\\n\"))\n}\n\n/**\n * Creates a history view that starts at the last summary.\n *\n * **Details**\n *\n * `isSummary` identifies the app's summary events. The model reads the last summary, the files\n * attached earlier ({@link attachedFiles}), and the items that are still open\n * ({@link openItems}), followed by the entries after the summary. When the chat has no summary,\n * the view returns every entry.\n *\n * **Example** (Compacting a long chat after each turn)\n *\n * ```ts\n * import { AppEvent, Assistant, type AssistantEvent, Compaction } from \"effect-assistant\"\n * import * as LanguageModel from \"effect/ai/LanguageModel\"\n * import * as Effect from \"effect/Effect\"\n * import * as Schema from \"effect/Schema\"\n * import * as Stream from \"effect/Stream\"\n *\n * class Summary extends Schema.TaggedClass<Summary>()(\"Summary\", {\n * text: Schema.String,\n * throughSeq: Schema.Int\n * }) {}\n *\n * declare const transcript: (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => string\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\", { events: [AppEvent.make(Summary)] })\n *\n * const MyAssistantLive = MyAssistant.toLayer((turn) => {\n * const compact = Effect.gen(function* () {\n * const chat = yield* MyAssistant.Chat\n * const entries = yield* chat.entries\n *\n * if (Compaction.inputTokens(entries) < 100_000) return\n *\n * const { text } = yield* LanguageModel.generateText({\n * prompt: `Summarize this chat:\\n\\n${transcript(entries)}`\n * })\n *\n * yield* chat.append(Summary.make({ text, throughSeq: entries.at(-1)?.seq ?? 0 }))\n * })\n *\n * // After the reply, so the user doesn't wait for the summary.\n * return Stream.concat(\n * turn.loop({ instructions: \"Be brief.\", history: Compaction.since(Schema.is(Summary)) }),\n * Stream.drain(Stream.fromEffect(compact))\n * )\n * })\n * ```\n *\n * @category utils\n * @since 0.1.0\n */\nexport const since =\n (isSummary: (event: unknown) => event is Summary) =>\n <Events extends { readonly _tag: string }>(\n entries: ReadonlyArray<Assistant.Entry<Events>>\n ): ReadonlyArray<Assistant.Entry<Events> | Prompt.Message> => {\n const last = entries.findLast(({ event }) => isSummary(event))?.event\n\n if (last === undefined || !isSummary(last)) return entries\n\n const open = Option.match(openItems(entries, last.throughSeq), {\n onNone: () => \"\",\n onSome: (items) => `\\n\\n<still_open>\\n${items}\\n</still_open>`\n })\n\n const files = Option.match(attachedFiles(entries, last.throughSeq), {\n onNone: () => \"\",\n onSome: (lines) => `\\n\\n<files_attached_earlier>\\n${lines}\\n</files_attached_earlier>`\n })\n\n return [\n userText(`<earlier_in_this_chat>\\n${last.text}\\n</earlier_in_this_chat>${files}${open}`),\n ...entries.filter(({ seq, event }) => seq > last.throughSeq && !isSummary(event))\n ]\n }\n"],"mappings":"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"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ChatId } from "./ChatId.js";
|
|
2
|
+
import { AnyEntry, AssistantEvent_d_exports } from "./AssistantEvent.js";
|
|
3
|
+
import * as Context from "effect/Context";
|
|
4
|
+
import * as Effect from "effect/Effect";
|
|
5
|
+
declare namespace CurrentChat_d_exports {
|
|
6
|
+
export { CurrentChat };
|
|
7
|
+
}
|
|
8
|
+
declare const CurrentChat_base: Context.ServiceClass<CurrentChat, "effect-assistant/CurrentChat", {
|
|
9
|
+
readonly chatId: ChatId;
|
|
10
|
+
/**
|
|
11
|
+
* The chat's entries so far, in order. They include the library's events and the assistant's
|
|
12
|
+
* own events, decoded; check for these with `Schema.is(MyEvent)` or `instanceof`. An app event
|
|
13
|
+
* that no longer decodes is left out.
|
|
14
|
+
*/
|
|
15
|
+
readonly entries: Effect.Effect<ReadonlyArray<AnyEntry>>;
|
|
16
|
+
/**
|
|
17
|
+
* Appends events after the chat's last entry and sends them to the chat's subscribers. The
|
|
18
|
+
* events can be the library's or the assistant's own (registered with its `events` option),
|
|
19
|
+
* such as a summary. An event that wakes a turn is handled by the next turn.
|
|
20
|
+
*/
|
|
21
|
+
readonly append: (...events: ReadonlyArray<{
|
|
22
|
+
readonly _tag: string;
|
|
23
|
+
}>) => Effect.Effect<void>;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Service that represents the chat the current code runs in.
|
|
27
|
+
*
|
|
28
|
+
* **Details**
|
|
29
|
+
*
|
|
30
|
+
* In a turn, it is the chat the turn runs in. In a specialist's run, and in its handler's build,
|
|
31
|
+
* it is the chat the run belongs to: the chat of the turn that spawned the run. A run's tools
|
|
32
|
+
* read and append to that chat.
|
|
33
|
+
*
|
|
34
|
+
* @category services
|
|
35
|
+
* @since 0.1.0
|
|
36
|
+
*/
|
|
37
|
+
export declare class CurrentChat extends CurrentChat_base {}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { CurrentChat_d_exports };
|
|
40
|
+
//# sourceMappingURL=CurrentChat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrentChat.d.ts","names":[],"sources":["../src/CurrentChat.ts"],"mappings":";;;;;;;;WAgCqB,QAAA;;;;;;WAMC,SAAA,OAAO,OAAO,cAAc;;;;;;WAM7B,YAAI,QAAQ;aAAyB;SAAoB,OAAO;;;;;;;;;;;;;;qBAfxE,oBAAoB"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
//#region src/CurrentChat.ts
|
|
4
|
+
/**
|
|
5
|
+
* This module provides the `CurrentChat` service, which gives code that runs inside a chat
|
|
6
|
+
* access to that chat: its id, its entries, and a way to append to it.
|
|
7
|
+
*
|
|
8
|
+
* A turn's handler, its tools, and its specialists' runs get the chat with `yield* CurrentChat`.
|
|
9
|
+
* Inside a specialist's run, it is the chat the run belongs to, which is the chat of the turn
|
|
10
|
+
* that spawned the run. The service doesn't carry an assistant's event types, so code that
|
|
11
|
+
* doesn't know the assistant, such as a shared tool, can use it. Code that knows the assistant
|
|
12
|
+
* can use `MyAssistant.Chat`, which is the same chat typed with the assistant's events.
|
|
13
|
+
*
|
|
14
|
+
* @since 0.1.0
|
|
15
|
+
*/
|
|
16
|
+
var CurrentChat_exports = /* @__PURE__ */ __exportAll({ CurrentChat: () => CurrentChat });
|
|
17
|
+
/**
|
|
18
|
+
* Service that represents the chat the current code runs in.
|
|
19
|
+
*
|
|
20
|
+
* **Details**
|
|
21
|
+
*
|
|
22
|
+
* In a turn, it is the chat the turn runs in. In a specialist's run, and in its handler's build,
|
|
23
|
+
* it is the chat the run belongs to: the chat of the turn that spawned the run. A run's tools
|
|
24
|
+
* read and append to that chat.
|
|
25
|
+
*
|
|
26
|
+
* @category services
|
|
27
|
+
* @since 0.1.0
|
|
28
|
+
*/
|
|
29
|
+
var CurrentChat = class extends Context.Service()("effect-assistant/CurrentChat") {};
|
|
30
|
+
//#endregion
|
|
31
|
+
export { CurrentChat, CurrentChat_exports };
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=CurrentChat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CurrentChat.js","names":[],"sources":["../src/CurrentChat.ts"],"sourcesContent":["/**\n * This module provides the `CurrentChat` service, which gives code that runs inside a chat\n * access to that chat: its id, its entries, and a way to append to it.\n *\n * A turn's handler, its tools, and its specialists' runs get the chat with `yield* CurrentChat`.\n * Inside a specialist's run, it is the chat the run belongs to, which is the chat of the turn\n * that spawned the run. The service doesn't carry an assistant's event types, so code that\n * doesn't know the assistant, such as a shared tool, can use it. Code that knows the assistant\n * can use `MyAssistant.Chat`, which is the same chat typed with the assistant's events.\n *\n * @since 0.1.0\n */\nimport * as Context from \"effect/Context\"\nimport type * as Effect from \"effect/Effect\"\nimport type * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type { ChatId } from \"./ChatId.ts\"\n\n/**\n * Service that represents the chat the current code runs in.\n *\n * **Details**\n *\n * In a turn, it is the chat the turn runs in. In a specialist's run, and in its handler's build,\n * it is the chat the run belongs to: the chat of the turn that spawned the run. A run's tools\n * read and append to that chat.\n *\n * @category services\n * @since 0.1.0\n */\nexport class CurrentChat extends Context.Service<\n CurrentChat,\n {\n readonly chatId: ChatId\n /**\n * The chat's entries so far, in order. They include the library's events and the assistant's\n * own events, decoded; check for these with `Schema.is(MyEvent)` or `instanceof`. An app event\n * that no longer decodes is left out.\n */\n readonly entries: Effect.Effect<ReadonlyArray<AssistantEvent.AnyEntry>>\n /**\n * Appends events after the chat's last entry and sends them to the chat's subscribers. The\n * events can be the library's or the assistant's own (registered with its `events` option),\n * such as a summary. An event that wakes a turn is handled by the next turn.\n */\n readonly append: (...events: ReadonlyArray<{ readonly _tag: string }>) => Effect.Effect<void>\n }\n>()(\"effect-assistant/CurrentChat\") {}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,IAAa,cAAb,cAAiC,QAAQ,QAiBvC,CAAC,CAAC,8BAA8B,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { AnyEntry, AssistantEvent_d_exports, DecisionOutcome, DecisionRequested, ToolResult } from "./AssistantEvent.js";
|
|
2
|
+
import { Block, Block_d_exports } from "./Block.js";
|
|
3
|
+
import { InputId } from "./InputId.js";
|
|
4
|
+
import * as Schema from "effect/Schema";
|
|
5
|
+
declare namespace Decision_d_exports {
|
|
6
|
+
export { Answer, Props, State, answerAll, apply, block, fromEntries, kind };
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* The kind of the decision block.
|
|
10
|
+
*
|
|
11
|
+
* @category utils
|
|
12
|
+
* @since 0.1.0
|
|
13
|
+
*/
|
|
14
|
+
export declare const kind = "decision";
|
|
15
|
+
/**
|
|
16
|
+
* Schema for the decision block's props, which list the decisions it asks for and, optionally,
|
|
17
|
+
* its buttons' labels.
|
|
18
|
+
*
|
|
19
|
+
* @category schemas
|
|
20
|
+
* @since 0.1.0
|
|
21
|
+
*/
|
|
22
|
+
export declare const Props: Schema.Struct<{
|
|
23
|
+
readonly decisionIds: Schema.$Array<Schema.String>;
|
|
24
|
+
readonly approveLabel: Schema.optional<Schema.String>;
|
|
25
|
+
readonly declineLabel: Schema.optional<Schema.String>;
|
|
26
|
+
}>;
|
|
27
|
+
/**
|
|
28
|
+
* Represents the decision block's props.
|
|
29
|
+
*
|
|
30
|
+
* @category models
|
|
31
|
+
* @since 0.1.0
|
|
32
|
+
*/
|
|
33
|
+
export type Props = typeof Props.Type;
|
|
34
|
+
/**
|
|
35
|
+
* Schema for the decision block's answer, which holds an outcome for every decision the block
|
|
36
|
+
* asks for, by id.
|
|
37
|
+
*
|
|
38
|
+
* @category schemas
|
|
39
|
+
* @since 0.1.0
|
|
40
|
+
*/
|
|
41
|
+
export declare const Answer: Schema.Struct<{
|
|
42
|
+
readonly decisions: Schema.$Record<Schema.String, Schema.Literals<readonly ["approved", "declined"]>>;
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Represents the decision block's answer.
|
|
46
|
+
*
|
|
47
|
+
* @category models
|
|
48
|
+
* @since 0.1.0
|
|
49
|
+
*/
|
|
50
|
+
export type Answer = typeof Answer.Type;
|
|
51
|
+
/**
|
|
52
|
+
* Block with approve and decline buttons for the decisions waiting for the user.
|
|
53
|
+
*
|
|
54
|
+
* **Details**
|
|
55
|
+
*
|
|
56
|
+
* Put it in the assistant's `BlockKit`. An assistant with a tool that needs a decision must have
|
|
57
|
+
* it. The client renders it like any other block, with the calls from {@link fromEntries} (their
|
|
58
|
+
* tool and params).
|
|
59
|
+
*
|
|
60
|
+
* **Example** (Asking the user before placing an order)
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { Assistant, BlockKit, Decision } from "effect-assistant"
|
|
64
|
+
* import * as Tool from "effect/ai/Tool"
|
|
65
|
+
* import * as Toolkit from "effect/ai/Toolkit"
|
|
66
|
+
* import * as Effect from "effect/Effect"
|
|
67
|
+
* import * as Layer from "effect/Layer"
|
|
68
|
+
* import * as Schema from "effect/Schema"
|
|
69
|
+
*
|
|
70
|
+
* export const Shop = Assistant.make("shop", { blocks: BlockKit.make([Decision.block]) })
|
|
71
|
+
*
|
|
72
|
+
* const OrderTools = Toolkit.make(
|
|
73
|
+
* Tool.make("place_order", {
|
|
74
|
+
* description: "Places an order.",
|
|
75
|
+
* parameters: Schema.Struct({ sku: Schema.String, quantity: Schema.Int }),
|
|
76
|
+
* success: Schema.Struct({ reference: Schema.String }),
|
|
77
|
+
* needsApproval: true
|
|
78
|
+
* })
|
|
79
|
+
* )
|
|
80
|
+
*
|
|
81
|
+
* const OrderToolsLive = OrderTools.toLayer({
|
|
82
|
+
* place_order: () => Effect.succeed({ reference: "R-1042" })
|
|
83
|
+
* })
|
|
84
|
+
*
|
|
85
|
+
* // The call waits until the user answers the decision block.
|
|
86
|
+
* const ShopLive = Shop.toLayer((turn) =>
|
|
87
|
+
* turn.loop({ instructions: "You place orders for the team.", toolkit: OrderTools })
|
|
88
|
+
* ).pipe(Layer.provide(OrderToolsLive))
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @category constructors
|
|
92
|
+
* @since 0.1.0
|
|
93
|
+
*/
|
|
94
|
+
export declare const block: Block<"decision", Schema.Struct<{
|
|
95
|
+
readonly decisionIds: Schema.$Array<Schema.String>;
|
|
96
|
+
readonly approveLabel: Schema.optional<Schema.String>;
|
|
97
|
+
readonly declineLabel: Schema.optional<Schema.String>;
|
|
98
|
+
}>, Schema.Struct<{
|
|
99
|
+
readonly decisions: Schema.$Record<Schema.String, Schema.Literals<readonly ["approved", "declined"]>>;
|
|
100
|
+
}>>;
|
|
101
|
+
/**
|
|
102
|
+
* Creates an answer that gives every decision the block asks for the same outcome.
|
|
103
|
+
*
|
|
104
|
+
* This is what an "Approve" or "Decline" button sends.
|
|
105
|
+
*
|
|
106
|
+
* @category constructors
|
|
107
|
+
* @since 0.1.0
|
|
108
|
+
*/
|
|
109
|
+
export declare const answerAll: (props: Props, outcome: DecisionOutcome) => Answer;
|
|
110
|
+
/**
|
|
111
|
+
* Represents the state of a decision.
|
|
112
|
+
*
|
|
113
|
+
* @category models
|
|
114
|
+
* @since 0.1.0
|
|
115
|
+
*/
|
|
116
|
+
export interface State {
|
|
117
|
+
readonly request: DecisionRequested;
|
|
118
|
+
/** How the user decided, or `undefined` while the decision waits. */
|
|
119
|
+
readonly outcome: DecisionOutcome | undefined;
|
|
120
|
+
/** The id of the answer that decided it. */
|
|
121
|
+
readonly answer: InputId | undefined;
|
|
122
|
+
/** Whether its `DecisionResolved` is stored. A workflow's decision has been passed on by then. */
|
|
123
|
+
readonly resolved: boolean;
|
|
124
|
+
/** The approved call's result, once the call has run. */
|
|
125
|
+
readonly result: ToolResult | undefined;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Returns the decision states after one more event.
|
|
129
|
+
*
|
|
130
|
+
* @category utils
|
|
131
|
+
* @since 0.1.0
|
|
132
|
+
*/
|
|
133
|
+
export declare const apply: (states: ReadonlyMap<string, State>, event: {
|
|
134
|
+
readonly _tag: string;
|
|
135
|
+
}) => ReadonlyMap<string, State>;
|
|
136
|
+
/**
|
|
137
|
+
* Returns every decision in the log and where it stands, by id.
|
|
138
|
+
*
|
|
139
|
+
* @category utils
|
|
140
|
+
* @since 0.1.0
|
|
141
|
+
*/
|
|
142
|
+
export declare const fromEntries: (entries: ReadonlyArray<AnyEntry>) => ReadonlyMap<string, State>;
|
|
143
|
+
//#endregion
|
|
144
|
+
export { Decision_d_exports };
|
|
145
|
+
//# sourceMappingURL=Decision.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Decision.d.ts","names":[],"sources":["../src/Decision.ts"],"mappings":";;;;;;;;;;;;;qBAyBa;;;;;;;;qBASA,OAAK,OAAA;;;;;;;;;;;YAqBN,eAAe,MAAM;;;;;;;;qBASpB,QAAM,OAAA;;;;;;;;;YAUP,gBAAgB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA6CtB,OAAK,kBAAA,OAAA;;;;;;;;;;;;;;;qBAeL,YAAS,OAAW,OAAK,SAAW,oBAAiC;;;;;;;iBAUjE;WACN,SAAS;;WAET,SAAS;;WAET,QAAQ;;WAER;;WAEA,QAAQ;;;;;;;;qBAWN,QAAK,QACR,oBAAoB,QAAM;WAChB;MACjB,oBAAoB;;;;;;;qBAmEV,cAAW,SAAa,cAAc,cAA2B,oBAAoB"}
|