effect-assistant 0.13.2 → 0.15.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/dist/Assistant.d.ts +5 -4
- package/dist/Assistant.d.ts.map +1 -1
- package/dist/Assistant.js +2 -2
- package/dist/Assistant.js.map +1 -1
- package/dist/AssistantActivity.d.ts +5 -4
- package/dist/AssistantActivity.d.ts.map +1 -1
- package/dist/AssistantActivity.js +0 -11
- package/dist/AssistantActivity.js.map +1 -1
- package/dist/AssistantEngine.d.ts +2 -2
- package/dist/AssistantEngine.d.ts.map +1 -1
- package/dist/AssistantEngine.js +8 -8
- package/dist/AssistantEngine.js.map +1 -1
- package/dist/AssistantEvent.d.ts +5 -4
- package/dist/AssistantEvent.d.ts.map +1 -1
- package/dist/AssistantEvent.js +3 -3
- package/dist/AssistantEvent.js.map +1 -1
- package/dist/ChatLog.js +4 -4
- package/dist/ChatLog.js.map +1 -1
- package/dist/ChatSignal.js +1 -1
- package/dist/ChatSignal.js.map +1 -1
- package/dist/Memory.d.ts +1 -1
- package/dist/Memory.js +2 -2
- package/dist/StatusSummary.d.ts +1 -1
- package/dist/StatusSummary.js +1 -1
- package/dist/Timeline.d.ts +21 -18
- package/dist/Timeline.d.ts.map +1 -1
- package/dist/Timeline.js +17 -15
- package/dist/Timeline.js.map +1 -1
- package/dist/ToolApproval.d.ts +3 -2
- package/dist/ToolApproval.d.ts.map +1 -1
- package/dist/ToolApproval.js +0 -13
- package/dist/ToolApproval.js.map +1 -1
- package/dist/internal/history.js +5 -3
- package/dist/internal/history.js.map +1 -1
- package/dist/internal/live.js +1 -1
- package/dist/internal/loop.js +1 -1
- package/dist/internal/summary.js +1 -1
- package/package.json +1 -1
- package/src/Assistant.ts +13 -9
- package/src/AssistantActivity.ts +5 -4
- package/src/AssistantEngine.ts +10 -10
- package/src/AssistantEvent.ts +5 -4
- package/src/ChatLog.ts +4 -4
- package/src/ChatSignal.ts +1 -1
- package/src/Timeline.ts +29 -21
- package/src/ToolApproval.ts +3 -2
- package/src/internal/history.ts +5 -3
package/dist/Assistant.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import * as Option from "effect/Option";
|
|
|
15
15
|
import * as Schema from "effect/Schema";
|
|
16
16
|
import * as Scope from "effect/Scope";
|
|
17
17
|
import * as Stream from "effect/Stream";
|
|
18
|
+
import * as DateTime from "effect/DateTime";
|
|
18
19
|
import * as Prompt from "effect/ai/Prompt";
|
|
19
20
|
import * as Tracer from "effect/Tracer";
|
|
20
21
|
import * as Tool from "effect/ai/Tool";
|
|
@@ -117,7 +118,7 @@ export type HistoryView<Events = never> = (entries: ReadonlyArray<Entry<Events>>
|
|
|
117
118
|
export interface Entry<Events = never> {
|
|
118
119
|
readonly chatId: ChatId;
|
|
119
120
|
readonly sequence: number;
|
|
120
|
-
readonly at:
|
|
121
|
+
readonly at: DateTime.Utc;
|
|
121
122
|
readonly event: AssistantEvent | Events;
|
|
122
123
|
}
|
|
123
124
|
/**
|
|
@@ -305,15 +306,15 @@ export interface Service<Events = never> {
|
|
|
305
306
|
*/
|
|
306
307
|
readonly subscribe: {
|
|
307
308
|
(chatId: ChatId, options?: SubscribeOptions & {
|
|
308
|
-
readonly
|
|
309
|
+
readonly signals?: false | undefined;
|
|
309
310
|
}): Stream.Stream<Entry<Events>>;
|
|
310
311
|
/**
|
|
311
|
-
* With `
|
|
312
|
+
* With `signals: true`, the stream also has the chat's signals (see `ChatSignal`): the status of
|
|
312
313
|
* what a turn is doing, and whether a message to the user is on its way. They come in order
|
|
313
314
|
* with the entries, only while the stream runs, and are never stored.
|
|
314
315
|
*/
|
|
315
316
|
(chatId: ChatId, options: SubscribeOptions & {
|
|
316
|
-
readonly
|
|
317
|
+
readonly signals: true;
|
|
317
318
|
}): Stream.Stream<Entry<Events> | ChatSignal>;
|
|
318
319
|
};
|
|
319
320
|
/**
|
package/dist/Assistant.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":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ACL,2BACA,cAAc,cAAc,4BAC5B,eAAe,cAAc,+BAA4C,MAEzE,MAAI;;WAGC,SAAS;;;;;WAKT,cAAc;;;;;;;WAOd,SAAS;MAEnB,UAAU,MAAM,eAAe"}
|
package/dist/Assistant.js
CHANGED
|
@@ -57,7 +57,7 @@ function entrySchema(events) {
|
|
|
57
57
|
return Schema.toCodecJson(Schema.Struct({
|
|
58
58
|
chatId: ChatId,
|
|
59
59
|
sequence: Schema.Int,
|
|
60
|
-
at: Schema.
|
|
60
|
+
at: Schema.DateTimeUtcFromMillis,
|
|
61
61
|
event: Schema.Union([AssistantEvent, ...events])
|
|
62
62
|
}));
|
|
63
63
|
}
|
|
@@ -115,7 +115,7 @@ const make = (name, options) => {
|
|
|
115
115
|
const chats = yield* Chats;
|
|
116
116
|
const decode = (item) => isChatSignal(item) ? Option.some(item) : decodeEntry(definition.app, item);
|
|
117
117
|
function subscribe(chatId, options) {
|
|
118
|
-
return engine.subscribe(name, chatId, options?.after ?? 0, options?.
|
|
118
|
+
return engine.subscribe(name, chatId, options?.after ?? 0, options?.signals === true).pipe(Stream.filterMap(Filter.fromPredicateOption(decode)));
|
|
119
119
|
}
|
|
120
120
|
return Key.of({
|
|
121
121
|
submit: Effect.fn("Assistant.submit")(function* (chatId, input) {
|
package/dist/Assistant.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Assistant.js","names":["Telemetry.TurnTracer","AssistantEvent.AssistantEvent","AppEvents.make","AssistantEvent.isInput","AppEvents.encode","Chats.Chats","ChatSignal.isChatSignal","AppEvents.decodeEntry","Telemetry.withoutTracing","Turns.register"],"sources":["../src/Assistant.ts"],"sourcesContent":["/**\n * This module provides `Assistant.make`, which defines an assistant that works in a chat the way a\n * colleague does: the user messages it, it works in the background with the app's tools, and it\n * messages the user back.\n *\n * A definition is what the rest of an app imports. Yielding it (`yield* MyAssistant`) returns the\n * assistant's {@link Service}, which submits inputs to chats and streams them back. Its `toLayer`\n * registers the handler that runs each {@link Turn} with the `AssistantEngine`, and its\n * `layerClient` provides the service without a handler. Code that runs inside a chat reads and\n * extends that chat through the definition's {@link Chat} key.\n *\n * @since 0.1.0\n */\nimport type * as AiError from \"effect/ai/AiError\"\nimport type * as LanguageModel from \"effect/ai/LanguageModel\"\nimport type * as Prompt from \"effect/ai/Prompt\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Filter from \"effect/Filter\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport type * as Tracer from \"effect/Tracer\"\nimport { AssistantEngine } from \"./AssistantEngine.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type * as AssistantLoop from \"./AssistantLoop.ts\"\nimport type * as BlockKit from \"./BlockKit.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport * as ChatSignal from \"./ChatSignal.ts\"\nimport * as Chats from \"./Chats.ts\"\nimport type { CurrentChat } from \"./CurrentChat.ts\"\nimport type * as Instructions from \"./Instructions.ts\"\nimport * as AppEvents from \"./internal/events.ts\"\nimport * as Telemetry from \"./internal/telemetry.ts\"\nimport * as Turns from \"./internal/turns.ts\"\nimport type * as Specialist from \"./Specialist.ts\"\n\nconst TypeId = \"~effect-assistant/Assistant\" as const\n\n/**\n * Represents the services that a turn provides to its tools, its loop, and its specialists. These\n * are the turn's chat (`CurrentChat`) and the chat's specialist runner (`Specialist.Runner`).\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type TurnServices = CurrentChat | Specialist.Runner\n\n/**\n * Represents the options accepted by `turn.run`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface RunOptions<T extends Record<string, Tool.Any> = Record<never, never>, Events = never> {\n /** The assistant's role and rules. The loop places `Instructions.protocol` before them. */\n readonly instructions: string | Instructions.Resolved\n /**\n * Tools the model can use in addition to the assistant's own. This is an `effect/ai` toolkit,\n * and its handlers come from the layer. Combine several toolkits with `Toolkit.merge`, for\n * example `Memory.Tools`, a `Skill.toolkit(...)`, or a `ToolDiscovery.make(...)` toolkit together\n * with your own.\n */\n readonly toolkit?: Toolkit.Toolkit<T> | undefined\n /** The maximum number of model calls per turn. Defaults to 16. */\n readonly maxSteps?: number | undefined\n /** The message the reply pass gives the model when the turn would end without a reply. */\n readonly replyNudge?: string | undefined\n /**\n * Selects which of the chat's entries the model reads, for example only those since the last\n * summary, and maps the app's own events it should read into prompt messages. Defaults to all\n * entries, without the app's events.\n */\n readonly history?: HistoryView<Events> | undefined\n}\n\n/**\n * Represents a function that selects which of a chat's entries the model reads, and what else it\n * reads with them.\n *\n * **Details**\n *\n * The function receives the chat's entries, the app's own events among them, and returns, in\n * order, a mix of entries and `Prompt.Message`s. The model reads the returned entries the same way\n * it reads the full history, and reads each prompt message unchanged, where the view put it. The\n * model reads none of the app's events by itself: map those it should read to a message, and an\n * app event's entry left in the result is dropped.\n *\n * A message stands right after the entry before it: one that came after the model's last read, or\n * between a tool call and its result, moves after them, as an input does.\n *\n * **Example** (Telling the model about an order the user placed)\n *\n * ```ts\n * import { Assistant, InputId } from \"effect-assistant\"\n * import * as Prompt from \"effect/ai/Prompt\"\n * import * as Schema from \"effect/Schema\"\n *\n * class OrderPlaced extends Schema.TaggedClass<OrderPlaced>()(\"OrderPlaced\", {\n * id: InputId.InputId,\n * reference: Schema.String\n * }) {}\n *\n * const MyAssistant = Assistant.make(\"my-assistant\", { events: [OrderPlaced] })\n *\n * const withOrders: Assistant.HistoryView<OrderPlaced> = (entries) =>\n * entries.map((entry) =>\n * entry.event instanceof OrderPlaced\n * ? Prompt.userMessage({\n * content: [Prompt.textPart({ text: `The user placed order ${entry.event.reference}.` })]\n * })\n * : entry\n * )\n *\n * // An `OrderPlaced` that the API submits wakes a turn, which reads it as this note.\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.run({ instructions: \"Be brief.\", history: withOrders })\n * )\n * ```\n *\n * @category models\n * @since 0.1.0\n */\nexport type HistoryView<Events = never> = (\n entries: ReadonlyArray<Entry<Events>>\n) => ReadonlyArray<Entry<Events> | Prompt.Message>\n\n/**\n * Represents an entry in a chat's log, with the assistant's own events decoded to their types.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Entry<Events = never> {\n readonly chatId: ChatId\n readonly sequence: number\n readonly at: number\n readonly event: AssistantEvent.AssistantEvent | Events\n}\n\n/**\n * Represents the options accepted by `turn.replyIfOwed`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ReplyOptions {\n /** The assistant's instructions, as passed to `turn.run`. `Instructions.protocol` is added. */\n readonly instructions: string | Instructions.Resolved\n /**\n * The message given to the model after the history. Defaults to a message saying that it\n * hasn't replied yet and should reply now.\n */\n readonly nudge?: string | undefined\n}\n\n/**\n * Represents one turn, as the assistant's handler receives it.\n *\n * `Events` is the type of the app's own events that the assistant registered with its `events`\n * option.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Turn<Events = never> {\n readonly chatId: ChatId\n /**\n * Returns the model's history, built from the chat's entries (all of them, or those a view\n * keeps), together with how far into the chat it reaches.\n *\n * Read it before every model call, as the loop does. Each step records how far its model call\n * read, and the turn ends once it has handled what the model saw. An input that arrived after\n * that is handled by the next turn.\n */\n readonly history: (view?: HistoryView<Events>) => Effect.Effect<AssistantLoop.ChatHistory>\n /**\n * Returns the full system prompt for the given instructions. It contains\n * `Instructions.protocol`, the instructions, and the specialist runs that are working now.\n */\n readonly instructions: (instructions: string | Instructions.Resolved) => Instructions.Resolved\n /**\n * Runs the agent loop over the chat. The loop uses `Instructions.protocol`, the assistant's own\n * tools, and the instructions and tools in `options`. Every step is a tool call, and\n * the loop ends with `replyIfOwed`.\n */\n readonly run: <T extends Record<string, Tool.Any> = Record<never, never>>(\n options: RunOptions<T, Events>\n ) => Stream.Stream<\n AssistantLoop.Event,\n AiError.AiError,\n LanguageModel.LanguageModel | TurnServices | AssistantLoop.ToolkitServices<T>\n >\n /**\n * Runs one more pass when the turn would otherwise end without a reply to the user's latest\n * message. In this pass the model can only reply.\n *\n * The stream is empty when no reply is owed, or when an input arrived that the next turn will\n * read. `run` runs this pass with its own prompt and tools, so the pass reuses the cached\n * prompt.\n */\n readonly replyIfOwed: (\n options: ReplyOptions\n ) => Stream.Stream<AssistantLoop.Event, AiError.AiError, LanguageModel.LanguageModel | TurnServices>\n}\n\n/**\n * Represents an assistant's handler, which runs a turn and returns it as a stream of the loop's\n * events.\n *\n * The handler may return the stream directly or from an Effect. `R` is the services the stream\n * requires, and `RE` is the services the Effect requires.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Handler<R = never, RE = R, Events = never> = (\n turn: Turn<Events>\n) =>\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n\n/**\n * Computes the services an assistant's handler needs beyond those its turns provide (the turn\n * services and `LanguageModel`) and its tools' handlers (which its layer requires). These are the\n * services that its build Effect must require.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type HandlerServices<R> = Exclude<R, TurnServices | LanguageModel.LanguageModel | Tool.Handler<string>>\n\nconst toStream = <R, RE>(\n result:\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n): Stream.Stream<AssistantLoop.Event, unknown, R | RE> => (Effect.isEffect(result) ? Stream.unwrap(result) : result)\n\n/**\n * Sets the tracer that an assistant's turns and specialist runs record their spans with. Each\n * turn's `invoke_agent` span is then the root of a trace on that tracer, with the turn's model\n * calls and tool calls below it. By default they use the tracer of the fiber that runs them.\n *\n * Provide it to the assistant's layer to send the agent's work to its own backend, such as an LLM\n * observability tool, while the rest of the app traces elsewhere.\n *\n * @example\n * ```ts\n * const AgentTracer = Layer.effect(\n * Assistant.TurnTracer,\n * OtlpTracer.make({ url: \"https://llm-observability.example.com/v1/traces\" }).pipe(Effect.map(Option.some))\n * )\n *\n * const MyAssistantLive = MyAssistant.toLayer(handler).pipe(Layer.provide(AgentTracer))\n * ```\n *\n * @category references\n * @since 0.12.0\n */\nexport const TurnTracer: Context.Reference<Option.Option<Tracer.Tracer>> = Telemetry.TurnTracer\n\n/**\n * Identifies an assistant's service in a context. Each assistant has its own identifier, keyed by\n * its name.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Identifier<Name extends string> {\n readonly \"~effect-assistant/Assistant\": Name\n}\n\n/**\n * Identifies, in a context, the chat that an assistant's turns run in.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ChatIdentifier<Name extends string> {\n readonly \"~effect-assistant/Assistant/Chat\": Name\n}\n\n/**\n * Represents the chat that a turn of this assistant runs in, typed with the assistant's events.\n *\n * Use `yield* MyAssistant.Chat` in the handler, in the handler's build, or in a tool that its\n * turns run. Code that doesn't know the assistant uses `CurrentChat` instead.\n *\n * @see {@link CurrentChat} for the same chat without the assistant's event types\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Chat<Events = never> {\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, typed.\n */\n readonly entries: Effect.Effect<ReadonlyArray<Entry<Events>>>\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, such as a summary. None of them wakes a\n * turn: only what comes in through the assistant's `submit` does.\n */\n append(...events: ReadonlyArray<AssistantEvent.AssistantEvent | Events>): Effect.Effect<void>\n}\n\n/**\n * Represents the options accepted by an assistant's `subscribe`.\n *\n * @category models\n * @since 0.13.0\n */\nexport interface SubscribeOptions {\n /** The `sequence` after which the stream starts. Defaults to `0`, the whole log. */\n readonly after?: number | undefined\n}\n\n/**\n * Represents the service that `yield* MyAssistant` returns. It gives code outside a chat, such as\n * a server or a workflow, access to the assistant's chats. Code that runs inside a chat uses\n * `CurrentChat` instead.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service<Events = never> {\n /**\n * Stores an input or one of the assistant's own events, then returns. Examples are a user's\n * message or block answer, a workflow's progress, or the order the user placed. Each one is an\n * input: storing it wakes a turn, which the assistant runs in the background. The handler decides\n * what the turn does with it, and an app event the model should read needs a history view that\n * maps it (see {@link HistoryView}). To store an app event without waking a turn, append it from\n * within the chat instead (`yield* MyAssistant.Chat`).\n *\n * Fails with `GetChatError` when the app's `Chats` service reports that the chat can't be used,\n * because the app doesn't know the chat or has deleted it.\n */\n // Method syntax: a service for these events is one for any (`Service<any>`), for helpers over assistants.\n submit(chatId: ChatId, input: AssistantEvent.Input | Events): Effect.Effect<void, Chats.GetChatError>\n /**\n * Streams the chat's stored entries, then live ones, with the assistant's own events decoded.\n * A reader that already has the earlier entries passes `after` and receives only the entries\n * after that `sequence`.\n *\n * For a chat client, pipe the whole log into `Timeline.changes(MyAssistant, { after })`, which\n * streams the chat's timeline, as its user sees it. Its `after` goes there, not here: a message\n * on the timeline depends on earlier entries. The timeline shows only the app events named in its\n * `show`, none by default (see `Timeline.changes`).\n */\n readonly subscribe: {\n (chatId: ChatId, options?: SubscribeOptions & { readonly live?: false | undefined }): Stream.Stream<Entry<Events>>\n /**\n * With `live: true`, the stream also has the chat's signals (see `ChatSignal`): the status of\n * what a turn is doing, and whether a message to the user is on its way. They come in order\n * with the entries, only while the stream runs, and are never stored.\n */\n (\n chatId: ChatId,\n options: SubscribeOptions & { readonly live: true }\n ): Stream.Stream<Entry<Events> | ChatSignal.ChatSignal>\n }\n /**\n * Deletes a chat by storing a `ChatDeleted` event and stopping everything that works in it.\n *\n * **Details**\n *\n * A turn or specialist run working in the chat stops, and subscribers' streams end with the\n * `ChatDeleted` event. After that the chat stores nothing more: a later input, a run's report,\n * or a workflow's event for it is dropped without an error. The log is kept, and removing it is\n * up to the app. Deleting a chat a second time does nothing.\n *\n * When the app's `Chats` service reports a chat as deleted, the assistant deletes it in the same\n * way the next time it checks. It checks before an input is stored and before each model call.\n */\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\n/**\n * Represents an assistant's definition.\n *\n * The definition holds what callers and the UI need to know about the assistant, builds its\n * handler's layer, and is the key of its service (`yield* MyAssistant`). `Events` is the type of\n * the app's own events that the assistant registered.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Assistant<\n Name extends string = string,\n Specs extends Specialist.Any = never,\n Events = never\n> extends Context.Service<Identifier<Name>, Service<Events>> {\n readonly [TypeId]: typeof TypeId\n readonly name: Name\n readonly blocks: BlockKit.BlockKit | undefined\n /** The specialists the assistant can hand work to. */\n readonly specialists: ReadonlyArray<Specs>\n /** The schemas of the app's own events the assistant stores. */\n readonly events: ReadonlyArray<AssistantEvent.AppEventSchema>\n /**\n * Creates a layer that registers the assistant's handler with the engine and provides the\n * assistant's service.\n *\n * **Details**\n *\n * `build` is the handler, or an Effect that returns it. The handler may use the turn services,\n * `LanguageModel`, its tools' handlers (which the layer then requires), and services that its\n * build Effect required (see `Specialist.CheckServices`). The layer requires the\n * `AssistantEngine`, the `LanguageModel`, and each specialist's handler\n * (`Research.toLayer(...)`).\n *\n * **Example** (Providing an assistant's handler)\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 * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.run({ instructions: \"You help a small team. Be brief.\" })\n * )\n *\n * const AppLive = MyAssistantLive.pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n */\n readonly toLayer: <R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, Events> | Effect.Effect<Handler<R, RE, Events>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ) => Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n /**\n * A layer that provides the assistant's service without its handler.\n *\n * **When to use**\n *\n * Use it in a process that submits to chats that another process runs, such as a web server in\n * front of a cluster.\n *\n * **Example** (Serving clients in front of a cluster)\n *\n * ```ts\n * import { Assistant, AssistantEngine } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * // Requires `Sharding`. The cluster's runners run the chats, with `AssistantEngine.layerCluster`.\n * const MyAssistantClient = MyAssistant.layerClient.pipe(\n * Layer.provide(AssistantEngine.layerClusterClient)\n * )\n * ```\n */\n readonly layerClient: Layer.Layer<Identifier<Name>, never, AssistantEngine>\n /**\n * The key of the chat that the assistant's turns run in, typed with its events. See\n * {@link Chat}.\n */\n readonly Chat: Context.Service<ChatIdentifier<Name>, Chat<Events>>\n /** A schema that encodes the chat's entries to JSON, including the assistant's own events. */\n readonly Entry: Schema.Codec<Entry<Events>, Schema.Json>\n}\n\n/**\n * Represents any assistant, for use in generic constraints.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type Any = Assistant<any, any, any>\n\n/**\n * Returns `true` when the value is an assistant's definition, created with {@link make}.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isAssistant = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)\n\nfunction entrySchema<E>(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Codec<Entry<E>, Schema.Json>\n// The overload states the decoded type: the library's events, or one of these schemas' types.\nfunction entrySchema(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Top {\n return Schema.toCodecJson(\n Schema.Struct({\n chatId: ChatId,\n sequence: Schema.Int,\n at: Schema.Finite,\n event: Schema.Union([AssistantEvent.AssistantEvent, ...events])\n })\n )\n}\n\n/**\n * Creates an assistant definition from its name, its UI blocks, its specialists, and the schemas of\n * the app's own events it stores.\n *\n * **Details**\n *\n * The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield\n * the definition to get the assistant's {@link Service}.\n *\n * The definition only says which of the app's events a chat stores. What happens with one is up\n * to the places that use it. One that comes in through the service's `submit`, from outside a\n * turn, is an input and wakes a turn, like a user's message; one a turn appends to its chat never\n * does. The handler decides what a turn does, a history view says how the model reads an event\n * ({@link HistoryView}), and the chat's timeline says which its user sees\n * (`Timeline.changes`).\n *\n * **Example** (Defining an assistant and submitting a message)\n *\n * ```ts\n * import { Assistant, AssistantEvent, ChatId } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const program = Effect.gen(function* () {\n * const assistant = yield* MyAssistant\n * const chatId = yield* ChatId.make()\n *\n * yield* assistant.submit(chatId, AssistantEvent.UserMessage.make({ text: \"Weather in Oslo?\" }))\n * })\n * ```\n *\n * @see {@link Assistant} for the definition's members\n * @see {@link Service} for what `yield* MyAssistant` returns\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const make = <\n const Name extends string,\n const Specs extends ReadonlyArray<Specialist.Any> = readonly [],\n const Events extends ReadonlyArray<AssistantEvent.AppEventSchema> = readonly []\n>(\n name: Name,\n options?: {\n /** UI blocks the assistant can add to its messages. */\n readonly blocks?: BlockKit.BlockKit | undefined\n /**\n * The specialists the assistant can hand work to. Each specialist's `toLayer` must be\n * provided to the assistant's layer.\n */\n readonly specialists?: Specs | undefined\n /**\n * The schemas of the app's own events, each a tagged struct or class. `submit` and the chat's\n * `append` store them, in their JSON form, and the chat's `entries` and `subscribe` read them\n * back, decoded. Each needs a tag of its own that isn't one of the library's: `make` throws\n * otherwise.\n */\n readonly events?: Events | undefined\n }\n): Assistant<Name, Specs[number], Events[number][\"Type\"]> => {\n const events = options?.events ?? []\n\n const definition: Turns.Definition = {\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n app: AppEvents.make(events)\n }\n\n type OwnEvents = Events[number][\"Type\"]\n\n const Key = Context.Service<Identifier<Name>, Service<OwnEvents>>(`effect-assistant/Assistant/${name}`)\n\n // The app's own events go to the engine encoded, as they're stored; the library's go unchanged.\n const stored = (\n input: AssistantEvent.Input | OwnEvents\n ): Effect.Effect<AssistantEvent.Input | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isInput(input) ? Effect.succeed(input) : AppEvents.encode(definition.app, input)\n\n const ChatKey = Context.Service<ChatIdentifier<Name>, Chat<OwnEvents>>(`effect-assistant/Assistant/${name}/Chat`)\n\n // On the engine and the app's `Chats` the layer was given, so every caller sees the same ones.\n const makeService = Effect.gen(function* () {\n const engine = yield* AssistantEngine\n const chats = yield* Chats.Chats\n\n const decode = (\n item: AssistantEvent.Entry | ChatSignal.ChatSignal\n ): Option.Option<Entry<OwnEvents> | ChatSignal.ChatSignal> =>\n ChatSignal.isChatSignal(item) ? Option.some(item) : AppEvents.decodeEntry(definition.app, item)\n\n function subscribe(\n chatId: ChatId,\n options?: SubscribeOptions & { readonly live?: false | undefined }\n ): Stream.Stream<Entry<OwnEvents>>\n function subscribe(\n chatId: ChatId,\n options: SubscribeOptions & { readonly live: true }\n ): Stream.Stream<Entry<OwnEvents> | ChatSignal.ChatSignal>\n function subscribe(\n chatId: ChatId,\n options?: SubscribeOptions & { readonly live?: boolean | undefined }\n ): Stream.Stream<Entry<OwnEvents> | ChatSignal.ChatSignal> {\n return engine\n .subscribe(name, chatId, options?.after ?? 0, options?.live === true)\n .pipe(Stream.filterMap(Filter.fromPredicateOption(decode)))\n }\n\n return Key.of({\n submit: Effect.fn(\"Assistant.submit\")(function* (chatId: ChatId, input: AssistantEvent.Input | OwnEvents) {\n // Checked here, where the caller can handle it: the chat checks again when it stores the input.\n yield* chats.get(chatId)\n yield* engine.submit(name, chatId, yield* stored(input))\n }),\n subscribe,\n delete: (chatId) => engine.delete(name, chatId)\n })\n })\n\n // The signature checks what the handler uses (`CheckServices`); the layer's services, which the build\n // required, are provided to it and to every turn's stream, so what's left is the turn services.\n function toLayer<R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, OwnEvents> | Effect.Effect<Handler<R, RE, OwnEvents>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ): Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs[number]>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n function toLayer(\n build: Handler<any, any, any> | Effect.Effect<Handler<any, any, any>, unknown, any>\n ): Layer.Layer<Identifier<Name>, never, any> {\n return Layer.effect(\n Key,\n Effect.gen(function* () {\n // Without the layer's scope: what a build or a turn sets up lives as long as the chat or the turn.\n const context = yield* Effect.context().pipe(\n Effect.map(Context.omit(Scope.Scope)),\n Effect.map(Telemetry.withoutTracing)\n )\n\n const registration = yield* Turns.register(definition, {\n // The build runs per chat activation, in the chat; here it only keeps the layer's services for it.\n open: (typed) =>\n Effect.gen(function* () {\n // The chat's entries are already decoded with this assistant's events.\n const chat = ChatKey.of({ chatId: typed.chatId, entries: typed.entries, append: typed.append })\n const handler = Effect.isEffect(build) ? yield* Effect.provideService(build, ChatKey, chat) : build\n\n return (turn: Turn<any>) =>\n Stream.provide(toStream(handler(turn)), context).pipe(Stream.provideService(ChatKey, chat))\n }).pipe(Effect.provide(context), Effect.orDie)\n })\n\n yield* (yield* AssistantEngine).register(registration)\n\n return yield* makeService\n })\n )\n }\n\n // The key is a function, whose own `name` is read-only: the assistant's name replaces it.\n const named = Object.defineProperty(Key, \"name\", { value: name, writable: true })\n\n return Object.assign(named, {\n [TypeId]: TypeId,\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n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eAAe,CAAC;EACtC;EACA;EACA,aAAa,MAAM,OAAO,KAAK,WAAW;EAC1C,MAAM;EACN,OAAO,YAAuB,MAAM;CACtC,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"Assistant.js","names":["Telemetry.TurnTracer","AssistantEvent.AssistantEvent","AppEvents.make","AssistantEvent.isInput","AppEvents.encode","Chats.Chats","ChatSignal.isChatSignal","AppEvents.decodeEntry","Telemetry.withoutTracing","Turns.register"],"sources":["../src/Assistant.ts"],"sourcesContent":["/**\n * This module provides `Assistant.make`, which defines an assistant that works in a chat the way a\n * colleague does: the user messages it, it works in the background with the app's tools, and it\n * messages the user back.\n *\n * A definition is what the rest of an app imports. Yielding it (`yield* MyAssistant`) returns the\n * assistant's {@link Service}, which submits inputs to chats and streams them back. Its `toLayer`\n * registers the handler that runs each {@link Turn} with the `AssistantEngine`, and its\n * `layerClient` provides the service without a handler. Code that runs inside a chat reads and\n * extends that chat through the definition's {@link Chat} key.\n *\n * @since 0.1.0\n */\nimport type * as AiError from \"effect/ai/AiError\"\nimport type * as LanguageModel from \"effect/ai/LanguageModel\"\nimport type * as Prompt from \"effect/ai/Prompt\"\nimport type * as Tool from \"effect/ai/Tool\"\nimport type * as Toolkit from \"effect/ai/Toolkit\"\nimport * as Context from \"effect/Context\"\nimport type * as DateTime from \"effect/DateTime\"\nimport * as Effect from \"effect/Effect\"\nimport * as Filter from \"effect/Filter\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Schema from \"effect/Schema\"\nimport * as Scope from \"effect/Scope\"\nimport * as Stream from \"effect/Stream\"\nimport type * as Tracer from \"effect/Tracer\"\nimport { AssistantEngine } from \"./AssistantEngine.ts\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type * as AssistantLoop from \"./AssistantLoop.ts\"\nimport type * as BlockKit from \"./BlockKit.ts\"\nimport { ChatId } from \"./ChatId.ts\"\nimport * as ChatSignal from \"./ChatSignal.ts\"\nimport * as Chats from \"./Chats.ts\"\nimport type { CurrentChat } from \"./CurrentChat.ts\"\nimport type * as Instructions from \"./Instructions.ts\"\nimport * as AppEvents from \"./internal/events.ts\"\nimport * as Telemetry from \"./internal/telemetry.ts\"\nimport * as Turns from \"./internal/turns.ts\"\nimport type * as Specialist from \"./Specialist.ts\"\n\nconst TypeId = \"~effect-assistant/Assistant\" as const\n\n/**\n * Represents the services that a turn provides to its tools, its loop, and its specialists. These\n * are the turn's chat (`CurrentChat`) and the chat's specialist runner (`Specialist.Runner`).\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type TurnServices = CurrentChat | Specialist.Runner\n\n/**\n * Represents the options accepted by `turn.run`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface RunOptions<T extends Record<string, Tool.Any> = Record<never, never>, Events = never> {\n /** The assistant's role and rules. The loop places `Instructions.protocol` before them. */\n readonly instructions: string | Instructions.Resolved\n /**\n * Tools the model can use in addition to the assistant's own. This is an `effect/ai` toolkit,\n * and its handlers come from the layer. Combine several toolkits with `Toolkit.merge`, for\n * example `Memory.Tools`, a `Skill.toolkit(...)`, or a `ToolDiscovery.make(...)` toolkit together\n * with your own.\n */\n readonly toolkit?: Toolkit.Toolkit<T> | undefined\n /** The maximum number of model calls per turn. Defaults to 16. */\n readonly maxSteps?: number | undefined\n /** The message the reply pass gives the model when the turn would end without a reply. */\n readonly replyNudge?: string | undefined\n /**\n * Selects which of the chat's entries the model reads, for example only those since the last\n * summary, and maps the app's own events it should read into prompt messages. Defaults to all\n * entries, without the app's events.\n */\n readonly history?: HistoryView<Events> | undefined\n}\n\n/**\n * Represents a function that selects which of a chat's entries the model reads, and what else it\n * reads with them.\n *\n * **Details**\n *\n * The function receives the chat's entries, the app's own events among them, and returns, in\n * order, a mix of entries and `Prompt.Message`s. The model reads the returned entries the same way\n * it reads the full history, and reads each prompt message unchanged, where the view put it. The\n * model reads none of the app's events by itself: map those it should read to a message, and an\n * app event's entry left in the result is dropped.\n *\n * A message stands right after the entry before it: one that came after the model's last read, or\n * between a tool call and its result, moves after them, as an input does.\n *\n * **Example** (Telling the model about an order the user placed)\n *\n * ```ts\n * import { Assistant, InputId } from \"effect-assistant\"\n * import * as Prompt from \"effect/ai/Prompt\"\n * import * as Schema from \"effect/Schema\"\n *\n * class OrderPlaced extends Schema.TaggedClass<OrderPlaced>()(\"OrderPlaced\", {\n * id: InputId.InputId,\n * reference: Schema.String\n * }) {}\n *\n * const MyAssistant = Assistant.make(\"my-assistant\", { events: [OrderPlaced] })\n *\n * const withOrders: Assistant.HistoryView<OrderPlaced> = (entries) =>\n * entries.map((entry) =>\n * entry.event instanceof OrderPlaced\n * ? Prompt.userMessage({\n * content: [Prompt.textPart({ text: `The user placed order ${entry.event.reference}.` })]\n * })\n * : entry\n * )\n *\n * // An `OrderPlaced` that the API submits wakes a turn, which reads it as this note.\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.run({ instructions: \"Be brief.\", history: withOrders })\n * )\n * ```\n *\n * @category models\n * @since 0.1.0\n */\nexport type HistoryView<Events = never> = (\n entries: ReadonlyArray<Entry<Events>>\n) => ReadonlyArray<Entry<Events> | Prompt.Message>\n\n/**\n * Represents an entry in a chat's log, with the assistant's own events decoded to their types.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Entry<Events = never> {\n readonly chatId: ChatId\n readonly sequence: number\n readonly at: DateTime.Utc\n readonly event: AssistantEvent.AssistantEvent | Events\n}\n\n/**\n * Represents the options accepted by `turn.replyIfOwed`.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ReplyOptions {\n /** The assistant's instructions, as passed to `turn.run`. `Instructions.protocol` is added. */\n readonly instructions: string | Instructions.Resolved\n /**\n * The message given to the model after the history. Defaults to a message saying that it\n * hasn't replied yet and should reply now.\n */\n readonly nudge?: string | undefined\n}\n\n/**\n * Represents one turn, as the assistant's handler receives it.\n *\n * `Events` is the type of the app's own events that the assistant registered with its `events`\n * option.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Turn<Events = never> {\n readonly chatId: ChatId\n /**\n * Returns the model's history, built from the chat's entries (all of them, or those a view\n * keeps), together with how far into the chat it reaches.\n *\n * Read it before every model call, as the loop does. Each step records how far its model call\n * read, and the turn ends once it has handled what the model saw. An input that arrived after\n * that is handled by the next turn.\n */\n readonly history: (view?: HistoryView<Events>) => Effect.Effect<AssistantLoop.ChatHistory>\n /**\n * Returns the full system prompt for the given instructions. It contains\n * `Instructions.protocol`, the instructions, and the specialist runs that are working now.\n */\n readonly instructions: (instructions: string | Instructions.Resolved) => Instructions.Resolved\n /**\n * Runs the agent loop over the chat. The loop uses `Instructions.protocol`, the assistant's own\n * tools, and the instructions and tools in `options`. Every step is a tool call, and\n * the loop ends with `replyIfOwed`.\n */\n readonly run: <T extends Record<string, Tool.Any> = Record<never, never>>(\n options: RunOptions<T, Events>\n ) => Stream.Stream<\n AssistantLoop.Event,\n AiError.AiError,\n LanguageModel.LanguageModel | TurnServices | AssistantLoop.ToolkitServices<T>\n >\n /**\n * Runs one more pass when the turn would otherwise end without a reply to the user's latest\n * message. In this pass the model can only reply.\n *\n * The stream is empty when no reply is owed, or when an input arrived that the next turn will\n * read. `run` runs this pass with its own prompt and tools, so the pass reuses the cached\n * prompt.\n */\n readonly replyIfOwed: (\n options: ReplyOptions\n ) => Stream.Stream<AssistantLoop.Event, AiError.AiError, LanguageModel.LanguageModel | TurnServices>\n}\n\n/**\n * Represents an assistant's handler, which runs a turn and returns it as a stream of the loop's\n * events.\n *\n * The handler may return the stream directly or from an Effect. `R` is the services the stream\n * requires, and `RE` is the services the Effect requires.\n *\n * @category models\n * @since 0.1.0\n */\nexport type Handler<R = never, RE = R, Events = never> = (\n turn: Turn<Events>\n) =>\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n\n/**\n * Computes the services an assistant's handler needs beyond those its turns provide (the turn\n * services and `LanguageModel`) and its tools' handlers (which its layer requires). These are the\n * services that its build Effect must require.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type HandlerServices<R> = Exclude<R, TurnServices | LanguageModel.LanguageModel | Tool.Handler<string>>\n\nconst toStream = <R, RE>(\n result:\n | Stream.Stream<AssistantLoop.Event, unknown, R>\n | Effect.Effect<Stream.Stream<AssistantLoop.Event, unknown, R>, unknown, RE>\n): Stream.Stream<AssistantLoop.Event, unknown, R | RE> => (Effect.isEffect(result) ? Stream.unwrap(result) : result)\n\n/**\n * Sets the tracer that an assistant's turns and specialist runs record their spans with. Each\n * turn's `invoke_agent` span is then the root of a trace on that tracer, with the turn's model\n * calls and tool calls below it. By default they use the tracer of the fiber that runs them.\n *\n * Provide it to the assistant's layer to send the agent's work to its own backend, such as an LLM\n * observability tool, while the rest of the app traces elsewhere.\n *\n * @example\n * ```ts\n * const AgentTracer = Layer.effect(\n * Assistant.TurnTracer,\n * OtlpTracer.make({ url: \"https://llm-observability.example.com/v1/traces\" }).pipe(Effect.map(Option.some))\n * )\n *\n * const MyAssistantLive = MyAssistant.toLayer(handler).pipe(Layer.provide(AgentTracer))\n * ```\n *\n * @category references\n * @since 0.12.0\n */\nexport const TurnTracer: Context.Reference<Option.Option<Tracer.Tracer>> = Telemetry.TurnTracer\n\n/**\n * Identifies an assistant's service in a context. Each assistant has its own identifier, keyed by\n * its name.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Identifier<Name extends string> {\n readonly \"~effect-assistant/Assistant\": Name\n}\n\n/**\n * Identifies, in a context, the chat that an assistant's turns run in.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface ChatIdentifier<Name extends string> {\n readonly \"~effect-assistant/Assistant/Chat\": Name\n}\n\n/**\n * Represents the chat that a turn of this assistant runs in, typed with the assistant's events.\n *\n * Use `yield* MyAssistant.Chat` in the handler, in the handler's build, or in a tool that its\n * turns run. Code that doesn't know the assistant uses `CurrentChat` instead.\n *\n * @see {@link CurrentChat} for the same chat without the assistant's event types\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Chat<Events = never> {\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, typed.\n */\n readonly entries: Effect.Effect<ReadonlyArray<Entry<Events>>>\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, such as a summary. None of them wakes a\n * turn: only what comes in through the assistant's `submit` does.\n */\n append(...events: ReadonlyArray<AssistantEvent.AssistantEvent | Events>): Effect.Effect<void>\n}\n\n/**\n * Represents the options accepted by an assistant's `subscribe`.\n *\n * @category models\n * @since 0.13.0\n */\nexport interface SubscribeOptions {\n /** The `sequence` after which the stream starts. Defaults to `0`, the whole log. */\n readonly after?: number | undefined\n}\n\n/**\n * Represents the service that `yield* MyAssistant` returns. It gives code outside a chat, such as\n * a server or a workflow, access to the assistant's chats. Code that runs inside a chat uses\n * `CurrentChat` instead.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Service<Events = never> {\n /**\n * Stores an input or one of the assistant's own events, then returns. Examples are a user's\n * message or block answer, a workflow's progress, or the order the user placed. Each one is an\n * input: storing it wakes a turn, which the assistant runs in the background. The handler decides\n * what the turn does with it, and an app event the model should read needs a history view that\n * maps it (see {@link HistoryView}). To store an app event without waking a turn, append it from\n * within the chat instead (`yield* MyAssistant.Chat`).\n *\n * Fails with `GetChatError` when the app's `Chats` service reports that the chat can't be used,\n * because the app doesn't know the chat or has deleted it.\n */\n // Method syntax: a service for these events is one for any (`Service<any>`), for helpers over assistants.\n submit(chatId: ChatId, input: AssistantEvent.Input | Events): Effect.Effect<void, Chats.GetChatError>\n /**\n * Streams the chat's stored entries, then live ones, with the assistant's own events decoded.\n * A reader that already has the earlier entries passes `after` and receives only the entries\n * after that `sequence`.\n *\n * For a chat client, pipe the whole log into `Timeline.changes(MyAssistant, { after })`, which\n * streams the chat's timeline, as its user sees it. Its `after` goes there, not here: a message\n * on the timeline depends on earlier entries. The timeline shows only the app events named in its\n * `show`, none by default (see `Timeline.changes`).\n */\n readonly subscribe: {\n (\n chatId: ChatId,\n options?: SubscribeOptions & { readonly signals?: false | undefined }\n ): Stream.Stream<Entry<Events>>\n /**\n * With `signals: true`, the stream also has the chat's signals (see `ChatSignal`): the status of\n * what a turn is doing, and whether a message to the user is on its way. They come in order\n * with the entries, only while the stream runs, and are never stored.\n */\n (\n chatId: ChatId,\n options: SubscribeOptions & { readonly signals: true }\n ): Stream.Stream<Entry<Events> | ChatSignal.ChatSignal>\n }\n /**\n * Deletes a chat by storing a `ChatDeleted` event and stopping everything that works in it.\n *\n * **Details**\n *\n * A turn or specialist run working in the chat stops, and subscribers' streams end with the\n * `ChatDeleted` event. After that the chat stores nothing more: a later input, a run's report,\n * or a workflow's event for it is dropped without an error. The log is kept, and removing it is\n * up to the app. Deleting a chat a second time does nothing.\n *\n * When the app's `Chats` service reports a chat as deleted, the assistant deletes it in the same\n * way the next time it checks. It checks before an input is stored and before each model call.\n */\n readonly delete: (chatId: ChatId) => Effect.Effect<void>\n}\n\n/**\n * Represents an assistant's definition.\n *\n * The definition holds what callers and the UI need to know about the assistant, builds its\n * handler's layer, and is the key of its service (`yield* MyAssistant`). `Events` is the type of\n * the app's own events that the assistant registered.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface Assistant<\n Name extends string = string,\n Specs extends Specialist.Any = never,\n Events = never\n> extends Context.Service<Identifier<Name>, Service<Events>> {\n readonly [TypeId]: typeof TypeId\n readonly name: Name\n readonly blocks: BlockKit.BlockKit | undefined\n /** The specialists the assistant can hand work to. */\n readonly specialists: ReadonlyArray<Specs>\n /** The schemas of the app's own events the assistant stores. */\n readonly events: ReadonlyArray<AssistantEvent.AppEventSchema>\n /**\n * Creates a layer that registers the assistant's handler with the engine and provides the\n * assistant's service.\n *\n * **Details**\n *\n * `build` is the handler, or an Effect that returns it. The handler may use the turn services,\n * `LanguageModel`, its tools' handlers (which the layer then requires), and services that its\n * build Effect required (see `Specialist.CheckServices`). The layer requires the\n * `AssistantEngine`, the `LanguageModel`, and each specialist's handler\n * (`Research.toLayer(...)`).\n *\n * **Example** (Providing an assistant's handler)\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 * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const MyAssistantLive = MyAssistant.toLayer((turn) =>\n * turn.run({ instructions: \"You help a small team. Be brief.\" })\n * )\n *\n * const AppLive = MyAssistantLive.pipe(\n * Layer.provide(ModelLive),\n * Layer.provideMerge(AssistantEngine.layerMemory),\n * Layer.provideMerge(ChatLog.layerMemory)\n * )\n * ```\n */\n readonly toLayer: <R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, Events> | Effect.Effect<Handler<R, RE, Events>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ) => Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n /**\n * A layer that provides the assistant's service without its handler.\n *\n * **When to use**\n *\n * Use it in a process that submits to chats that another process runs, such as a web server in\n * front of a cluster.\n *\n * **Example** (Serving clients in front of a cluster)\n *\n * ```ts\n * import { Assistant, AssistantEngine } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * // Requires `Sharding`. The cluster's runners run the chats, with `AssistantEngine.layerCluster`.\n * const MyAssistantClient = MyAssistant.layerClient.pipe(\n * Layer.provide(AssistantEngine.layerClusterClient)\n * )\n * ```\n */\n readonly layerClient: Layer.Layer<Identifier<Name>, never, AssistantEngine>\n /**\n * The key of the chat that the assistant's turns run in, typed with its events. See\n * {@link Chat}.\n */\n readonly Chat: Context.Service<ChatIdentifier<Name>, Chat<Events>>\n /** A schema that encodes the chat's entries to JSON, including the assistant's own events. */\n readonly Entry: Schema.Codec<Entry<Events>, Schema.Json>\n}\n\n/**\n * Represents any assistant, for use in generic constraints.\n *\n * @category utility types\n * @since 0.1.0\n */\nexport type Any = Assistant<any, any, any>\n\n/**\n * Returns `true` when the value is an assistant's definition, created with {@link make}.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isAssistant = (u: unknown): u is Any => Predicate.hasProperty(u, TypeId)\n\nfunction entrySchema<E>(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Codec<Entry<E>, Schema.Json>\n// The overload states the decoded type: the library's events, or one of these schemas' types.\nfunction entrySchema(events: ReadonlyArray<AssistantEvent.AppEventSchema>): Schema.Top {\n return Schema.toCodecJson(\n Schema.Struct({\n chatId: ChatId,\n sequence: Schema.Int,\n at: Schema.DateTimeUtcFromMillis,\n event: Schema.Union([AssistantEvent.AssistantEvent, ...events])\n })\n )\n}\n\n/**\n * Creates an assistant definition from its name, its UI blocks, its specialists, and the schemas of\n * the app's own events it stores.\n *\n * **Details**\n *\n * The definition holds no code that runs turns. Provide a handler with its `toLayer`, then yield\n * the definition to get the assistant's {@link Service}.\n *\n * The definition only says which of the app's events a chat stores. What happens with one is up\n * to the places that use it. One that comes in through the service's `submit`, from outside a\n * turn, is an input and wakes a turn, like a user's message; one a turn appends to its chat never\n * does. The handler decides what a turn does, a history view says how the model reads an event\n * ({@link HistoryView}), and the chat's timeline says which its user sees\n * (`Timeline.changes`).\n *\n * **Example** (Defining an assistant and submitting a message)\n *\n * ```ts\n * import { Assistant, AssistantEvent, ChatId } from \"effect-assistant\"\n * import * as Effect from \"effect/Effect\"\n *\n * export const MyAssistant = Assistant.make(\"my-assistant\")\n *\n * const program = Effect.gen(function* () {\n * const assistant = yield* MyAssistant\n * const chatId = yield* ChatId.make()\n *\n * yield* assistant.submit(chatId, AssistantEvent.UserMessage.make({ text: \"Weather in Oslo?\" }))\n * })\n * ```\n *\n * @see {@link Assistant} for the definition's members\n * @see {@link Service} for what `yield* MyAssistant` returns\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const make = <\n const Name extends string,\n const Specs extends ReadonlyArray<Specialist.Any> = readonly [],\n const Events extends ReadonlyArray<AssistantEvent.AppEventSchema> = readonly []\n>(\n name: Name,\n options?: {\n /** UI blocks the assistant can add to its messages. */\n readonly blocks?: BlockKit.BlockKit | undefined\n /**\n * The specialists the assistant can hand work to. Each specialist's `toLayer` must be\n * provided to the assistant's layer.\n */\n readonly specialists?: Specs | undefined\n /**\n * The schemas of the app's own events, each a tagged struct or class. `submit` and the chat's\n * `append` store them, in their JSON form, and the chat's `entries` and `subscribe` read them\n * back, decoded. Each needs a tag of its own that isn't one of the library's: `make` throws\n * otherwise.\n */\n readonly events?: Events | undefined\n }\n): Assistant<Name, Specs[number], Events[number][\"Type\"]> => {\n const events = options?.events ?? []\n\n const definition: Turns.Definition = {\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n app: AppEvents.make(events)\n }\n\n type OwnEvents = Events[number][\"Type\"]\n\n const Key = Context.Service<Identifier<Name>, Service<OwnEvents>>(`effect-assistant/Assistant/${name}`)\n\n // The app's own events go to the engine encoded, as they're stored; the library's go unchanged.\n const stored = (\n input: AssistantEvent.Input | OwnEvents\n ): Effect.Effect<AssistantEvent.Input | AssistantEvent.StoredAppEvent> =>\n AssistantEvent.isInput(input) ? Effect.succeed(input) : AppEvents.encode(definition.app, input)\n\n const ChatKey = Context.Service<ChatIdentifier<Name>, Chat<OwnEvents>>(`effect-assistant/Assistant/${name}/Chat`)\n\n // On the engine and the app's `Chats` the layer was given, so every caller sees the same ones.\n const makeService = Effect.gen(function* () {\n const engine = yield* AssistantEngine\n const chats = yield* Chats.Chats\n\n const decode = (\n item: AssistantEvent.Entry | ChatSignal.ChatSignal\n ): Option.Option<Entry<OwnEvents> | ChatSignal.ChatSignal> =>\n ChatSignal.isChatSignal(item) ? Option.some(item) : AppEvents.decodeEntry(definition.app, item)\n\n function subscribe(\n chatId: ChatId,\n options?: SubscribeOptions & { readonly signals?: false | undefined }\n ): Stream.Stream<Entry<OwnEvents>>\n function subscribe(\n chatId: ChatId,\n options: SubscribeOptions & { readonly signals: true }\n ): Stream.Stream<Entry<OwnEvents> | ChatSignal.ChatSignal>\n function subscribe(\n chatId: ChatId,\n options?: SubscribeOptions & { readonly signals?: boolean | undefined }\n ): Stream.Stream<Entry<OwnEvents> | ChatSignal.ChatSignal> {\n return engine\n .subscribe(name, chatId, options?.after ?? 0, options?.signals === true)\n .pipe(Stream.filterMap(Filter.fromPredicateOption(decode)))\n }\n\n return Key.of({\n submit: Effect.fn(\"Assistant.submit\")(function* (chatId: ChatId, input: AssistantEvent.Input | OwnEvents) {\n // Checked here, where the caller can handle it: the chat checks again when it stores the input.\n yield* chats.get(chatId)\n yield* engine.submit(name, chatId, yield* stored(input))\n }),\n subscribe,\n delete: (chatId) => engine.delete(name, chatId)\n })\n })\n\n // The signature checks what the handler uses (`CheckServices`); the layer's services, which the build\n // required, are provided to it and to every turn's stream, so what's left is the turn services.\n function toLayer<R = never, RE = never, E = never, RX = never>(\n build: (Handler<R, RE, OwnEvents> | Effect.Effect<Handler<R, RE, OwnEvents>, E, RX>) &\n Specialist.CheckServices<Exclude<HandlerServices<R | RE>, RX | ChatIdentifier<Name>>>\n ): Layer.Layer<\n Identifier<Name>,\n never,\n | AssistantEngine\n | LanguageModel.LanguageModel\n | Specialist.HandlerOf<Specs[number]>\n | Exclude<RX, Scope.Scope | CurrentChat | ChatIdentifier<Name>>\n | Specialist.ToolHandlers<R | RE>\n >\n function toLayer(\n build: Handler<any, any, any> | Effect.Effect<Handler<any, any, any>, unknown, any>\n ): Layer.Layer<Identifier<Name>, never, any> {\n return Layer.effect(\n Key,\n Effect.gen(function* () {\n // Without the layer's scope: what a build or a turn sets up lives as long as the chat or the turn.\n const context = yield* Effect.context().pipe(\n Effect.map(Context.omit(Scope.Scope)),\n Effect.map(Telemetry.withoutTracing)\n )\n\n const registration = yield* Turns.register(definition, {\n // The build runs per chat activation, in the chat; here it only keeps the layer's services for it.\n open: (typed) =>\n Effect.gen(function* () {\n // The chat's entries are already decoded with this assistant's events.\n const chat = ChatKey.of({ chatId: typed.chatId, entries: typed.entries, append: typed.append })\n const handler = Effect.isEffect(build) ? yield* Effect.provideService(build, ChatKey, chat) : build\n\n return (turn: Turn<any>) =>\n Stream.provide(toStream(handler(turn)), context).pipe(Stream.provideService(ChatKey, chat))\n }).pipe(Effect.provide(context), Effect.orDie)\n })\n\n yield* (yield* AssistantEngine).register(registration)\n\n return yield* makeService\n })\n )\n }\n\n // The key is a function, whose own `name` is read-only: the assistant's name replaces it.\n const named = Object.defineProperty(Key, \"name\", { value: name, writable: true })\n\n return Object.assign(named, {\n [TypeId]: TypeId,\n name,\n blocks: options?.blocks,\n specialists: options?.specialists ?? [],\n events,\n toLayer,\n layerClient: Layer.effect(Key, makeService),\n Chat: ChatKey,\n Entry: entrySchema<OwnEvents>(events)\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAM,SAAS;AAmMf,MAAM,YACJ,WAGyD,OAAO,SAAS,MAAM,IAAI,OAAO,OAAO,MAAM,IAAI;;;;;;;;;;;;;;;;;;;;;;AAuB7G,MAAa,aAA8DA;;;;;;;AA6O3E,MAAa,eAAe,MAAyB,UAAU,YAAY,GAAG,MAAM;AAIpF,SAAS,YAAY,QAAkE;CACrF,OAAO,OAAO,YACZ,OAAO,OAAO;EACZ,QAAQ;EACR,UAAU,OAAO;EACjB,IAAI,OAAO;EACX,OAAO,OAAO,MAAM,CAACC,gBAA+B,GAAG,MAAM,CAAC;CAChE,CAAC,CACH;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwCA,MAAa,QAKX,MACA,YAgB2D;CAC3D,MAAM,SAAS,SAAS,UAAU,CAAC;CAEnC,MAAM,aAA+B;EACnC;EACA,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S,MAAM;KAAS,QAAQ,MAAM;IAAO,CAAC;IAC9F,MAAM,UAAU,OAAO,SAAS,KAAK,IAAI,OAAO,OAAO,eAAe,OAAO,SAAS,IAAI,IAAI;IAE9F,QAAQ,SACN,OAAO,QAAQ,SAAS,QAAQ,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,KAAK,OAAO,eAAe,SAAS,IAAI,CAAC;GAC9F,CAAC,CAAC,CAAC,KAAK,OAAO,QAAQ,OAAO,GAAG,OAAO,KAAK,EACjD,CAAC;GAED,QAAQ,OAAO,gBAAA,CAAiB,SAAS,YAAY;GAErD,OAAO,OAAO;EAChB,CAAC,CACH;CACF;CAGA,MAAM,QAAQ,OAAO,eAAe,KAAK,QAAQ;EAAE,OAAO;EAAM,UAAU;CAAK,CAAC;CAEhF,OAAO,OAAO,OAAO,OAAO;GACzB,SAAS;EACV;EACA,QAAQ,SAAS;EACjB,aAAa,SAAS,eAAe,CAAC;EACtC;EACA;EACA,aAAa,MAAM,OAAO,KAAK,WAAW;EAC1C,MAAM;EACN,OAAO,YAAuB,MAAM;CACtC,CAAC;AACH"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AnyEntry, AssistantEvent_d_exports } from "./AssistantEvent.js";
|
|
2
2
|
import { RunId } from "./RunId.js";
|
|
3
|
+
import * as DateTime from "effect/DateTime";
|
|
3
4
|
declare namespace AssistantActivity_d_exports {
|
|
4
5
|
export { SpecialistRun, State, apply, empty, fromEntries, isWorking };
|
|
5
6
|
}
|
|
@@ -14,8 +15,8 @@ export interface SpecialistRun {
|
|
|
14
15
|
readonly specialist: string;
|
|
15
16
|
/** The goal the run is working on. */
|
|
16
17
|
readonly goal: string;
|
|
17
|
-
/** When the run started
|
|
18
|
-
readonly since:
|
|
18
|
+
/** When the run started. */
|
|
19
|
+
readonly since: DateTime.Utc;
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* Represents what is working in a chat.
|
|
@@ -24,8 +25,8 @@ export interface SpecialistRun {
|
|
|
24
25
|
* @since 0.1.0
|
|
25
26
|
*/
|
|
26
27
|
export interface State {
|
|
27
|
-
/** When the running turn started,
|
|
28
|
-
readonly turnSince:
|
|
28
|
+
/** When the running turn started, or `undefined` when no turn runs. */
|
|
29
|
+
readonly turnSince: DateTime.Utc | undefined;
|
|
29
30
|
/**
|
|
30
31
|
* Whether an input arrived that no finished turn has read. When this is true, a turn is about
|
|
31
32
|
* to start.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantActivity.d.ts","names":[],"sources":["../src/AssistantActivity.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"AssistantActivity.d.ts","names":[],"sources":["../src/AssistantActivity.ts"],"mappings":";;;;;;;;;;;;iBAuBiB;WACN,OAAO;WACP;;WAEA;;WAEA,OAAO,SAAS;;;;;;;;iBASV;;WAEN,WAAW,SAAS;;;;;WAKpB;;WAEA;;WAEA,aAAa,YAAY,OAAO;;;;;;;;qBAS9B,OAAO;;;;;;;;;;;;;;;qBA8BP,QAAK,UAAc,OAAK,OAAS,aAA0B;;;;;;;;qBA8C3D,cAAW,SAAa,cAAc,cAA2B;;;;;;;;qBAUjE,YAAS,UAAc"}
|
|
@@ -3,17 +3,6 @@ import { isAppInput, isInput, isLibraryEvent, isWaking } from "./AssistantEvent.
|
|
|
3
3
|
import * as Predicate from "effect/Predicate";
|
|
4
4
|
import * as Match from "effect/Match";
|
|
5
5
|
//#region src/AssistantActivity.ts
|
|
6
|
-
/**
|
|
7
|
-
* This module provides functions that determine what is working in a chat right now, read from
|
|
8
|
-
* the chat's log.
|
|
9
|
-
*
|
|
10
|
-
* A {@link State} records whether the assistant is running a turn or is about to start one,
|
|
11
|
-
* and which specialist runs are working. Fold a chat's entries into it with {@link apply}, or
|
|
12
|
-
* build it from a list of entries with {@link fromEntries}, then check it with
|
|
13
|
-
* {@link isWorking}.
|
|
14
|
-
*
|
|
15
|
-
* @since 0.1.0
|
|
16
|
-
*/
|
|
17
6
|
var AssistantActivity_exports = /* @__PURE__ */ __exportAll({
|
|
18
7
|
apply: () => apply,
|
|
19
8
|
empty: () => empty,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantActivity.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isAppInput","AssistantEvent.isInput","AssistantEvent.isWaking"],"sources":["../src/AssistantActivity.ts"],"sourcesContent":["/**\n * This module provides functions that determine what is working in a chat right now, read from\n * the chat's log.\n *\n * A {@link State} records whether the assistant is running a turn or is about to start one,\n * and which specialist runs are working. Fold a chat's entries into it with {@link apply}, or\n * build it from a list of entries with {@link fromEntries}, then check it with\n * {@link isWorking}.\n *\n * @since 0.1.0\n */\nimport * as Match from \"effect/Match\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type { RunId } from \"./RunId.ts\"\n\n/**\n * Represents a specialist run that is working.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface SpecialistRun {\n readonly runId: RunId\n readonly specialist: string\n /** The goal the run is working on. */\n readonly goal: string\n /** When the run started
|
|
1
|
+
{"version":3,"file":"AssistantActivity.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isAppInput","AssistantEvent.isInput","AssistantEvent.isWaking"],"sources":["../src/AssistantActivity.ts"],"sourcesContent":["/**\n * This module provides functions that determine what is working in a chat right now, read from\n * the chat's log.\n *\n * A {@link State} records whether the assistant is running a turn or is about to start one,\n * and which specialist runs are working. Fold a chat's entries into it with {@link apply}, or\n * build it from a list of entries with {@link fromEntries}, then check it with\n * {@link isWorking}.\n *\n * @since 0.1.0\n */\nimport type * as DateTime from \"effect/DateTime\"\nimport * as Match from \"effect/Match\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as AssistantEvent from \"./AssistantEvent.ts\"\nimport type { RunId } from \"./RunId.ts\"\n\n/**\n * Represents a specialist run that is working.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface SpecialistRun {\n readonly runId: RunId\n readonly specialist: string\n /** The goal the run is working on. */\n readonly goal: string\n /** When the run started. */\n readonly since: DateTime.Utc\n}\n\n/**\n * Represents what is working in a chat.\n *\n * @category models\n * @since 0.1.0\n */\nexport interface State {\n /** When the running turn started, or `undefined` when no turn runs. */\n readonly turnSince: DateTime.Utc | undefined\n /**\n * Whether an input arrived that no finished turn has read. When this is true, a turn is about\n * to start.\n */\n readonly queued: boolean\n /** The `sequence` of the newest input. */\n readonly lastInputSequence: number\n /** Specialist runs that are working, by run id. */\n readonly specialists: ReadonlyMap<RunId, SpecialistRun>\n}\n\n/**\n * The activity of a chat with an empty log, where nothing is working.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const empty: State = {\n turnSince: undefined,\n queued: false,\n lastInputSequence: 0,\n specialists: new Map()\n}\n\nconst withoutRun = (activity: State, runId: RunId): State => {\n if (!activity.specialists.has(runId)) return activity\n\n const specialists = new Map(activity.specialists)\n specialists.delete(runId)\n\n return { ...activity, specialists }\n}\n\n/**\n * Returns the activity after one more entry. Fold a chat's entries in order, or use\n * {@link fromEntries}.\n *\n * **Details**\n *\n * One of the app's events counts as an input, and a turn as about to start, when it came in\n * through the assistant's `submit`. The log marks it so (`AssistantEvent.isAppInput`), as\n * `ChatLog.read` returns it; `subscribe` streams it decoded, without the mark, so there the turn\n * shows from its `TurnStarted`.\n *\n * @category combinators\n * @since 0.1.0\n */\nexport const apply = (activity: State, entry: AssistantEvent.AnyEntry): State => {\n const event = entry.event\n\n if (!AssistantEvent.isLibraryEvent(event)) {\n return AssistantEvent.isAppInput(event)\n ? { ...activity, queued: true, lastInputSequence: entry.sequence }\n : activity\n }\n\n if (AssistantEvent.isInput(event)) {\n // A finished run's report is an input, and ends the run.\n const current = Predicate.isTagged(event, \"SpecialistFinished\") ? withoutRun(activity, event.runId) : activity\n\n return AssistantEvent.isWaking(event) ? { ...current, queued: true, lastInputSequence: entry.sequence } : current\n }\n\n return Match.value(event).pipe(\n Match.tag(\"TurnStarted\", (): State => ({ ...activity, turnSince: entry.at })),\n // Turns run one at a time, so an end closes whichever turn is open, also one a restart cut off.\n Match.tag(\"TurnCompleted\", \"TurnFailed\", (ended): State => ({\n ...activity,\n turnSince: undefined,\n queued: activity.lastInputSequence > ended.throughSequence\n })),\n Match.tag(\"SpecialistStarted\", (started): State => ({\n ...activity,\n specialists: new Map(activity.specialists).set(started.runId, {\n runId: started.runId,\n specialist: started.specialist,\n goal: started.goal,\n since: entry.at\n })\n })),\n // Deleting a chat stops everything in it, and nothing starts again.\n Match.tag(\"ChatDeleted\", (): State => ({ ...empty, lastInputSequence: activity.lastInputSequence })),\n Match.orElse(() => activity)\n )\n}\n\n/**\n * Returns what is working in a chat after the given entries, folded in order from\n * {@link empty}.\n *\n * @category constructors\n * @since 0.1.0\n */\nexport const fromEntries = (entries: ReadonlyArray<AssistantEvent.AnyEntry>): State =>\n entries.reduce((activity, entry) => apply(activity, entry), empty)\n\n/**\n * Returns whether anything is working: a turn that is running or about to start, or a specialist\n * run.\n *\n * @category guards\n * @since 0.1.0\n */\nexport const isWorking = (activity: State): boolean =>\n activity.turnSince !== undefined || activity.queued || activity.specialists.size > 0\n"],"mappings":";;;;;;;;;;;;;;;;;AA0DA,MAAa,QAAe;CAC1B,WAAW,KAAA;CACX,QAAQ;CACR,mBAAmB;CACnB,6BAAa,IAAI,IAAI;AACvB;AAEA,MAAM,cAAc,UAAiB,UAAwB;CAC3D,IAAI,CAAC,SAAS,YAAY,IAAI,KAAK,GAAG,OAAO;CAE7C,MAAM,cAAc,IAAI,IAAI,SAAS,WAAW;CAChD,YAAY,OAAO,KAAK;CAExB,OAAO;EAAE,GAAG;EAAU;CAAY;AACpC;;;;;;;;;;;;;;;AAgBA,MAAa,SAAS,UAAiB,UAA0C;CAC/E,MAAM,QAAQ,MAAM;CAEpB,IAAI,CAACA,eAA8B,KAAK,GACtC,OAAOC,WAA0B,KAAK,IAClC;EAAE,GAAG;EAAU,QAAQ;EAAM,mBAAmB,MAAM;CAAS,IAC/D;CAGN,IAAIC,QAAuB,KAAK,GAAG;EAEjC,MAAM,UAAU,UAAU,SAAS,OAAO,oBAAoB,IAAI,WAAW,UAAU,MAAM,KAAK,IAAI;EAEtG,OAAOC,SAAwB,KAAK,IAAI;GAAE,GAAG;GAAS,QAAQ;GAAM,mBAAmB,MAAM;EAAS,IAAI;CAC5G;CAEA,OAAO,MAAM,MAAM,KAAK,CAAC,CAAC,KACxB,MAAM,IAAI,sBAA6B;EAAE,GAAG;EAAU,WAAW,MAAM;CAAG,EAAE,GAE5E,MAAM,IAAI,iBAAiB,eAAe,WAAkB;EAC1D,GAAG;EACH,WAAW,KAAA;EACX,QAAQ,SAAS,oBAAoB,MAAM;CAC7C,EAAE,GACF,MAAM,IAAI,sBAAsB,aAAoB;EAClD,GAAG;EACH,aAAa,IAAI,IAAI,SAAS,WAAW,CAAC,CAAC,IAAI,QAAQ,OAAO;GAC5D,OAAO,QAAQ;GACf,YAAY,QAAQ;GACpB,MAAM,QAAQ;GACd,OAAO,MAAM;EACf,CAAC;CACH,EAAE,GAEF,MAAM,IAAI,sBAA6B;EAAE,GAAG;EAAO,mBAAmB,SAAS;CAAkB,EAAE,GACnG,MAAM,aAAa,QAAQ,CAC7B;AACF;;;;;;;;AASA,MAAa,eAAe,YAC1B,QAAQ,QAAQ,UAAU,UAAU,MAAM,UAAU,KAAK,GAAG,KAAK;;;;;;;;AASnE,MAAa,aAAa,aACxB,SAAS,cAAc,KAAA,KAAa,SAAS,UAAU,SAAS,YAAY,OAAO"}
|
|
@@ -21,10 +21,10 @@ declare const AssistantEngine_base: Context.ServiceClass<AssistantEngine, "effec
|
|
|
21
21
|
*/
|
|
22
22
|
readonly submit: (assistant: string, chatId: ChatId, input: Input | StoredAppEvent) => Effect.Effect<void>;
|
|
23
23
|
/**
|
|
24
|
-
* Streams the chat's stored entries after the `after` sequence, then live ones. With `
|
|
24
|
+
* Streams the chat's stored entries after the `after` sequence, then live ones. With `signals`,
|
|
25
25
|
* the chat's signals too, in order with the entries.
|
|
26
26
|
*/
|
|
27
|
-
readonly subscribe: (assistant: string, chatId: ChatId, after: number,
|
|
27
|
+
readonly subscribe: (assistant: string, chatId: ChatId, after: number, signals: boolean) => Stream.Stream<Entry | ChatSignal>;
|
|
28
28
|
/**
|
|
29
29
|
* Deletes one of the assistant's chats. The chat's log records the deletion, and everything
|
|
30
30
|
* that works in the chat stops.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantEngine.d.ts","names":[],"sources":["../src/AssistantEngine.ts"],"mappings":";;;;;;;;;;;;;;;;WA+CuB,WAAC,cAAc,iBAAuB,OAAO,oBAAoB,MAAM;;;;;WAKzE,SACf,mBACA,QAAQ,QACR,OAAO,QAAuB,mBAC3B,OAAO;;;;;WAKQ,YAClB,mBACA,QAAQ,QACR,eACA,
|
|
1
|
+
{"version":3,"file":"AssistantEngine.d.ts","names":[],"sources":["../src/AssistantEngine.ts"],"mappings":";;;;;;;;;;;;;;;;WA+CuB,WAAC,cAAc,iBAAuB,OAAO,oBAAoB,MAAM;;;;;WAKzE,SACf,mBACA,QAAQ,QACR,OAAO,QAAuB,mBAC3B,OAAO;;;;;WAKQ,YAClB,mBACA,QAAQ,QACR,eACA,qBACG,OAAO,OAAO,QAAuB;;;;;WAKzB,SAAC,mBAAmB,QAAQ,WAAW,OAAO;;;;;;;;;qBA5BtD,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAsExB,aAAa,MAAM,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAmNjD,cAAc,MAAM,MAAM,wBAAwB,UAAU,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAwFrE,oBAAoB,MAAM,MAAM,wBAAwB,SAAS"}
|
package/dist/AssistantEngine.js
CHANGED
|
@@ -137,12 +137,12 @@ const layerMemory = Layer.effect(AssistantEngine, Effect.gen(function* () {
|
|
|
137
137
|
chatId
|
|
138
138
|
}));
|
|
139
139
|
}),
|
|
140
|
-
subscribe: (assistant, chatId, after,
|
|
140
|
+
subscribe: (assistant, chatId, after, signals) => Stream.unwrap(Effect.gen(function* () {
|
|
141
141
|
const chat = yield* RcMap.get(chats, new ChatKey({
|
|
142
142
|
assistant,
|
|
143
143
|
chatId
|
|
144
144
|
}));
|
|
145
|
-
return
|
|
145
|
+
return signals ? chat.live(after) : chat.stream(after);
|
|
146
146
|
}))
|
|
147
147
|
});
|
|
148
148
|
}));
|
|
@@ -150,7 +150,7 @@ const Submit = Rpc.make("Submit", { payload: { input: Schema.Union([Input, Store
|
|
|
150
150
|
const Subscribe = Rpc.make("Subscribe", {
|
|
151
151
|
payload: {
|
|
152
152
|
after: Schema.Int,
|
|
153
|
-
|
|
153
|
+
signals: Schema.optionalKey(Schema.Boolean)
|
|
154
154
|
},
|
|
155
155
|
success: Schema.Union([Entry, ChatSignal]),
|
|
156
156
|
stream: true
|
|
@@ -170,9 +170,9 @@ const makeClusterClient = Effect.gen(function* () {
|
|
|
170
170
|
const makeClient = yield* chatEntity(assistant).client.pipe(Effect.provide(context));
|
|
171
171
|
const client = {
|
|
172
172
|
submit: (chatId, input) => makeClient(chatId).Submit({ input }, { discard: true }).pipe(Effect.orDie),
|
|
173
|
-
subscribe: (chatId, after,
|
|
173
|
+
subscribe: (chatId, after, signals) => makeClient(chatId).Subscribe({
|
|
174
174
|
after,
|
|
175
|
-
|
|
175
|
+
signals
|
|
176
176
|
}).pipe(Stream.orDie),
|
|
177
177
|
delete: (chatId) => makeClient(chatId).Delete().pipe(Effect.orDie)
|
|
178
178
|
};
|
|
@@ -183,8 +183,8 @@ const makeClusterClient = Effect.gen(function* () {
|
|
|
183
183
|
submit: Effect.fn("AssistantEngine.submit")(function* (assistant, chatId, input) {
|
|
184
184
|
yield* (yield* clientOf(assistant)).submit(chatId, input);
|
|
185
185
|
}),
|
|
186
|
-
subscribe: (assistant, chatId, after,
|
|
187
|
-
return (yield* clientOf(assistant)).subscribe(chatId, after,
|
|
186
|
+
subscribe: (assistant, chatId, after, signals) => Stream.unwrap(Effect.gen(function* () {
|
|
187
|
+
return (yield* clientOf(assistant)).subscribe(chatId, after, signals);
|
|
188
188
|
})),
|
|
189
189
|
delete: Effect.fn("AssistantEngine.delete")(function* (assistant, chatId) {
|
|
190
190
|
yield* (yield* clientOf(assistant)).delete(chatId);
|
|
@@ -252,7 +252,7 @@ const layerCluster = Layer.effect(AssistantEngine, Effect.gen(function* () {
|
|
|
252
252
|
yield* chat.resume;
|
|
253
253
|
return ChatEntity.of({
|
|
254
254
|
Submit: (request) => chat.submit(request.payload.input),
|
|
255
|
-
Subscribe: (request) => request.payload.
|
|
255
|
+
Subscribe: (request) => request.payload.signals === true ? chat.live(request.payload.after) : chat.stream(request.payload.after),
|
|
256
256
|
Delete: () => chat.delete
|
|
257
257
|
});
|
|
258
258
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantEngine.js","names":["Chat.make","History.needsTurn","AssistantEvent.Input","AssistantEvent.StoredAppEvent","AssistantEvent.Entry","ChatSignal.ChatSignal"],"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 ChatSignal from \"./ChatSignal.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 /**\n * Streams the chat's stored entries after the `after` sequence, then live ones. With `live`,\n * the chat's signals too, in order with the entries.\n */\n readonly subscribe: (\n assistant: string,\n chatId: ChatId,\n after: number,\n live: boolean\n ) => Stream.Stream<AssistantEvent.Entry | ChatSignal.ChatSignal>\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.run({ 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 // Without the layer's scope, so a chat's resources close with its entry.\n const context = yield* Effect.context<ChatLog>().pipe(Effect.map(Context.omit(Scope.Scope)))\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, live) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return live ? chat.live(after) : 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: (\n chatId: ChatId,\n after: number,\n live: boolean\n ) => Stream.Stream<AssistantEvent.Entry | ChatSignal.ChatSignal>\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, live: Schema.optionalKey(Schema.Boolean) },\n success: Schema.Union([AssistantEvent.Entry, ChatSignal.ChatSignal]),\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, live) => makeClient(chatId).Subscribe({ after, live }).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, live) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const client = yield* clientOf(assistant)\n\n return client.subscribe(chatId, after, live)\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.run({ 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 // Without the layer's scope, so a chat's resources close with its entity.\n const context = yield* Effect.context<ChatLog | Sharding.Sharding>().pipe(Effect.map(Context.omit(Scope.Scope)))\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) =>\n request.payload.live === true ? chat.live(request.payload.after) : 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAAqC,QAAQ,QA8B3C,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;CAEtB,MAAM,UAAU,OAAO,OAAO,QAAiB,CAAC,CAAC,KAAK,OAAO,IAAI,QAAQ,KAAK,MAAM,KAAK,CAAC,CAAC;CAC3F,MAAM,MAAM,OAAO;CACnB,MAAM,6BAAa,IAAI,IAAgC;CAEvD,MAAM,OAAO,eAAe,MAAgC;CAG5D,MAAM,QAAyC,OAAO,MAAM,KAAK;EAC/D,Q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ACH,CAAC,CACH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,MAAa,qBAA6E,MAAM,OAC9F,iBACA,OAAO,IAAI,aAAa;CACtB,MAAM,SAAS,OAAO;CAEtB,OAAO,gBAAgB,GAAG;EACxB,WAAW,iBACT,OAAO,IACL,OAAO,aAAa,WAAW,KAAK,qJACtC;EACF,GAAG;CACL,CAAC;AACH,CAAC,CACH"}
|
|
1
|
+
{"version":3,"file":"AssistantEngine.js","names":["Chat.make","History.needsTurn","AssistantEvent.Input","AssistantEvent.StoredAppEvent","AssistantEvent.Entry","ChatSignal.ChatSignal"],"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 ChatSignal from \"./ChatSignal.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 /**\n * Streams the chat's stored entries after the `after` sequence, then live ones. With `signals`,\n * the chat's signals too, in order with the entries.\n */\n readonly subscribe: (\n assistant: string,\n chatId: ChatId,\n after: number,\n signals: boolean\n ) => Stream.Stream<AssistantEvent.Entry | ChatSignal.ChatSignal>\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.run({ 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 // Without the layer's scope, so a chat's resources close with its entry.\n const context = yield* Effect.context<ChatLog>().pipe(Effect.map(Context.omit(Scope.Scope)))\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, signals) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const chat = yield* RcMap.get(chats, new ChatKey({ assistant, chatId }))\n\n return signals ? chat.live(after) : 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: (\n chatId: ChatId,\n after: number,\n signals: boolean\n ) => Stream.Stream<AssistantEvent.Entry | ChatSignal.ChatSignal>\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, signals: Schema.optionalKey(Schema.Boolean) },\n success: Schema.Union([AssistantEvent.Entry, ChatSignal.ChatSignal]),\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, signals) => makeClient(chatId).Subscribe({ after, signals }).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, signals) =>\n Stream.unwrap(\n Effect.gen(function* () {\n const client = yield* clientOf(assistant)\n\n return client.subscribe(chatId, after, signals)\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.run({ 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 // Without the layer's scope, so a chat's resources close with its entity.\n const context = yield* Effect.context<ChatLog | Sharding.Sharding>().pipe(Effect.map(Context.omit(Scope.Scope)))\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) =>\n request.payload.signals === true ? chat.live(request.payload.after) : 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,IAAa,kBAAb,cAAqC,QAAQ,QA8B3C,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;CAEtB,MAAM,UAAU,OAAO,OAAO,QAAiB,CAAC,CAAC,KAAK,OAAO,IAAI,QAAQ,KAAK,MAAM,KAAK,CAAC,CAAC;CAC3F,MAAM,MAAM,OAAO;CACnB,MAAM,6BAAa,IAAI,IAAgC;CAEvD,MAAM,OAAO,eAAe,MAAgC;CAG5D,MAAM,QAAyC,OAAO,MAAM,KAAK;EAC/D,QAAQ,OAAO,WAAW,WAAW,KAAc;GACjD,MAAM,eAAe,WAAW,IAAI,IAAI,SAAS;GAEjD,IAAI,iBAAiB,KAAA,GAAW,OAAO,OAAO,cAAc,IAAI,SAAS;GAEzE,OAAO,OAAOA,KAAU;IACtB;IACA,QAAQ,IAAI;IACZ,YAAY,YAAY,UAAU,KAAK,OAAO;GAChD,CAAC,CAAC,CAAC,KAAK,OAAO,QAAQ,OAA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|
package/dist/AssistantEvent.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
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 DateTime from "effect/DateTime";
|
|
4
5
|
import * as SchemaAST from "effect/SchemaAST";
|
|
5
6
|
declare namespace AssistantEvent_d_exports {
|
|
6
7
|
export { AnyEntry, AppEventSchema, AppTag, ApprovalDecision, ApprovalRequested, ApprovalsExpired, 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 };
|
|
@@ -576,7 +577,7 @@ export declare const isAppInput: (event: {
|
|
|
576
577
|
export interface AnyEntry {
|
|
577
578
|
readonly chatId: ChatId;
|
|
578
579
|
readonly sequence: number;
|
|
579
|
-
readonly at:
|
|
580
|
+
readonly at: DateTime.Utc;
|
|
580
581
|
readonly event: {
|
|
581
582
|
readonly _tag: string;
|
|
582
583
|
};
|
|
@@ -584,8 +585,8 @@ export interface AnyEntry {
|
|
|
584
585
|
/**
|
|
585
586
|
* Schema for a stored event with its position in the chat.
|
|
586
587
|
*
|
|
587
|
-
* `sequence` starts at 1 and has no gaps. `at` is the time the event was stored,
|
|
588
|
-
* milliseconds.
|
|
588
|
+
* `sequence` starts at 1 and has no gaps. `at` is the time the event was stored, a UTC
|
|
589
|
+
* `DateTime` that encodes as epoch milliseconds.
|
|
589
590
|
*
|
|
590
591
|
* @category schemas
|
|
591
592
|
* @since 0.1.0
|
|
@@ -593,7 +594,7 @@ export interface AnyEntry {
|
|
|
593
594
|
export declare const Entry: Schema.Struct<{
|
|
594
595
|
readonly chatId: Schema.brand<Schema.TemplateLiteral<readonly ["chat:", Schema.String]>, "~effect-assistant/ChatId">;
|
|
595
596
|
readonly sequence: Schema.Int;
|
|
596
|
-
readonly at: Schema.
|
|
597
|
+
readonly at: Schema.DateTimeUtcFromMillis;
|
|
597
598
|
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 ApprovalRequested, typeof ApprovalDecision, typeof ApprovalsExpired, typeof ChatDeleted]>, Schema.StructWithRest<Schema.Struct<{
|
|
598
599
|
readonly _tag: Schema.brand<Schema.String, "~effect-assistant/AppTag">;
|
|
599
600
|
readonly "~effect-assistant/input": Schema.optionalKey<Schema.Literal<true>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AssistantEvent.d.ts","names":[],"sources":["../src/AssistantEvent.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"AssistantEvent.d.ts","names":[],"sources":["../src/AssistantEvent.ts"],"mappings":";;;;;;;;;;;;;;;;qBA4Ba,YAAU,OAAA;;;;;;;;;;;;;;;qBAgBV;;;;;;;YAQD,oBAAoB,WAAW;;;;;;;qBAQ9B,aAAU;;;;;;;;;qBAiBV,iBAAc,YAAgB;;;;;;;qBAS9B,sBAAmB,cAAgB,aAAe,cAAc;;;;;;;;;;;;;;;;;;;;;qBAchE,oBAAoB;;;;;;;;;;;;;;;;qBAepB,oBAAoB;;;;;;;;;;;;;;;;;;;;;qBAgBpB,2BAA2B;;;;;;;;;;;;;;;;;qBAsB3B,yBAAyB;;;;;;;qBAazB,OAAK,OAAA,uBAAA,oBAAA,oBAAA,2BAAA;;;;;;;YAQN,eAAe,MAAM;;;;;;;;;;;qBAYpB,WAAS,OAAA,uBAAA,oBAAA,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;;;;;;;;;;qBAW9D,aAAU;WAAsB;;;;;;;;;;;iBAY5B;WACN,QAAQ;WACR;WACA,IAAI,SAAS;WACb;aAAkB;;;;;;;;;;;;qBAYhB,OAAK,OAAA;;;;;;;;;;;;;;;YAaN,eAAe,MAAM;;;;;;;;;;;qBAYpB,UAAO;WAAsB;MAAiB,SAAS;;;;;;;;qBAavD,WAAQ,OAAW;;;;;;;qBASnB,cAAW,OAAW,UAAQ,SAAS;;;;;;;qBAWvC,YAAS;WAAsB;MAAiB,SAAS,gBAAgB;;;;;;;qBASzE,iBAAc,SAAa,cAAc,cAAY"}
|
package/dist/AssistantEvent.js
CHANGED
|
@@ -501,8 +501,8 @@ const isAppInput = (event) => !isLibraryEvent(event) && Predicate.hasProperty(ev
|
|
|
501
501
|
/**
|
|
502
502
|
* Schema for a stored event with its position in the chat.
|
|
503
503
|
*
|
|
504
|
-
* `sequence` starts at 1 and has no gaps. `at` is the time the event was stored,
|
|
505
|
-
* milliseconds.
|
|
504
|
+
* `sequence` starts at 1 and has no gaps. `at` is the time the event was stored, a UTC
|
|
505
|
+
* `DateTime` that encodes as epoch milliseconds.
|
|
506
506
|
*
|
|
507
507
|
* @category schemas
|
|
508
508
|
* @since 0.1.0
|
|
@@ -510,7 +510,7 @@ const isAppInput = (event) => !isLibraryEvent(event) && Predicate.hasProperty(ev
|
|
|
510
510
|
const Entry = Schema.Struct({
|
|
511
511
|
chatId: ChatId,
|
|
512
512
|
sequence: Schema.Int,
|
|
513
|
-
at: Schema.
|
|
513
|
+
at: Schema.DateTimeUtcFromMillis,
|
|
514
514
|
event: Schema.Union([AssistantEvent, StoredAppEvent])
|
|
515
515
|
});
|
|
516
516
|
/**
|