effect-assistant 0.16.0 → 0.17.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/StatusSummary.d.ts +4 -3
- package/dist/StatusSummary.d.ts.map +1 -1
- package/dist/StatusSummary.js +6 -5
- package/dist/StatusSummary.js.map +1 -1
- package/dist/internal/live.js +3 -1
- package/dist/internal/live.js.map +1 -1
- package/dist/internal/summary.js +24 -5
- package/dist/internal/summary.js.map +1 -1
- package/package.json +1 -1
- package/src/StatusSummary.ts +8 -6
- package/src/internal/live.ts +4 -1
- package/src/internal/summary.ts +27 -7
package/dist/StatusSummary.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ declare namespace StatusSummary_d_exports {
|
|
|
13
13
|
* @category constants
|
|
14
14
|
* @since 0.13.0
|
|
15
15
|
*/
|
|
16
|
-
export declare const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read
|
|
16
|
+
export declare const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
17
17
|
/**
|
|
18
18
|
* Represents the options accepted by {@link layer}.
|
|
19
19
|
*
|
|
@@ -32,8 +32,9 @@ declare const StatusSummary_base: Context.ServiceClass<StatusSummary, "effect-as
|
|
|
32
32
|
readonly after: Duration.Duration;
|
|
33
33
|
readonly staleAfter: Duration.Duration;
|
|
34
34
|
/**
|
|
35
|
-
* Returns the status for a turn, from its transcript:
|
|
36
|
-
*
|
|
35
|
+
* Returns the status for a turn, from its transcript: this turn after a few earlier messages,
|
|
36
|
+
* what still runs, and the lines shown so far. `None` when the model has nothing new, wrote
|
|
37
|
+
* nothing usable, or its call failed.
|
|
37
38
|
*/
|
|
38
39
|
readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>;
|
|
39
40
|
}>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StatusSummary.d.ts","names":[],"sources":["../src/StatusSummary.ts"],"mappings":";;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"StatusSummary.d.ts","names":[],"sources":["../src/StatusSummary.ts"],"mappings":";;;;;;;;;;;;;;;qBA6Ba;;;;;;;iBASI;;WAEN,QAAQ,SAAS;;WAEjB,aAAa,SAAS;;WAEtB;;;WAYS,OAAA,SAAS;WACJ,YAAA,SAAS;;;;;;WAMV,YAAC,uBAAuB,OAAO,OAAO,OAAO;;;;;;;;qBAVxD,sBAAsB;;;;;;;;;;;;;;;;;;;;;qBA6CtB,QAAK,UAAc,YAAU,MAAM,MAAM,sBAAsB,cAAc"}
|
package/dist/StatusSummary.js
CHANGED
|
@@ -16,8 +16,9 @@ import * as LanguageModel from "effect/ai/LanguageModel";
|
|
|
16
16
|
* Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary
|
|
17
17
|
* is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.
|
|
18
18
|
* It never replaces the status of a described call that still runs, never shows while the model
|
|
19
|
-
* writes to the user, and never after the turn's message unless the model starts new work.
|
|
20
|
-
*
|
|
19
|
+
* writes to the user, and never after the turn's message unless the model starts new work. The
|
|
20
|
+
* model reads this turn, after a few earlier messages as context, and the lines the turn showed so
|
|
21
|
+
* far, and answers `-` when nothing new happened since: then the line stays as it is.
|
|
21
22
|
*
|
|
22
23
|
* @since 0.13.0
|
|
23
24
|
*/
|
|
@@ -32,7 +33,7 @@ var StatusSummary_exports = /* @__PURE__ */ __exportAll({
|
|
|
32
33
|
* @category constants
|
|
33
34
|
* @since 0.13.0
|
|
34
35
|
*/
|
|
35
|
-
const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read
|
|
36
|
+
const defaultInstructions = "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
36
37
|
/**
|
|
37
38
|
* Service that writes a turn's status from what the turn did so far.
|
|
38
39
|
*
|
|
@@ -43,7 +44,7 @@ var StatusSummary = class extends Context.Service()("effect-assistant/StatusSumm
|
|
|
43
44
|
const MAX_LENGTH = 60;
|
|
44
45
|
const clean = (text) => {
|
|
45
46
|
const line = (text.trim().split("\n")[0] ?? "").replace(/^["'“”‘’]+|["'“”‘’]+$/g, "").replace(/[.…]+$/, "").trim();
|
|
46
|
-
return line === "" || line.length > MAX_LENGTH ? Option.none() : Option.some(line);
|
|
47
|
+
return line === "" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line);
|
|
47
48
|
};
|
|
48
49
|
/**
|
|
49
50
|
* Creates the summary on the model it is given: provide a small, fast one.
|
|
@@ -67,7 +68,7 @@ const clean = (text) => {
|
|
|
67
68
|
*/
|
|
68
69
|
const layer = (options) => Layer.effect(StatusSummary, Effect.gen(function* () {
|
|
69
70
|
const model = yield* LanguageModel.LanguageModel;
|
|
70
|
-
const instructions = options?.instructions ?? "You write the status line a chat shows while an assistant works for its user. Read
|
|
71
|
+
const instructions = options?.instructions ?? "You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year's budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.";
|
|
71
72
|
const summarize = Effect.fn("StatusSummary.summarize")(function* (transcript) {
|
|
72
73
|
const response = yield* model.generateText({ prompt: Prompt.fromMessages([Prompt.systemMessage({ content: instructions }), Prompt.userMessage({ content: [Prompt.textPart({ text: transcript })] })]) });
|
|
73
74
|
return clean(response.text);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StatusSummary.js","names":[],"sources":["../src/StatusSummary.ts"],"sourcesContent":["/**\n * This module provides the status a turn shows when nothing else says what it is doing: when the\n * line stays empty for a while, or a call's status still shows after that call ended, a model reads\n * the turn so far and writes a few words about it, like \"Comparing last year's budgets\".\n *\n * Provide {@link layer} next to the assistant's `toLayer`, with a small, fast model of its own.\n * Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary\n * is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.\n * It never replaces the status of a described call that still runs, never shows while the model\n * writes to the user, and never after the turn's message unless the model starts new work.
|
|
1
|
+
{"version":3,"file":"StatusSummary.js","names":[],"sources":["../src/StatusSummary.ts"],"sourcesContent":["/**\n * This module provides the status a turn shows when nothing else says what it is doing: when the\n * line stays empty for a while, or a call's status still shows after that call ended, a model reads\n * the turn so far and writes a few words about it, like \"Comparing last year's budgets\".\n *\n * Provide {@link layer} next to the assistant's `toLayer`, with a small, fast model of its own.\n * Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary\n * is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.\n * It never replaces the status of a described call that still runs, never shows while the model\n * writes to the user, and never after the turn's message unless the model starts new work. The\n * model reads this turn, after a few earlier messages as context, and the lines the turn showed so\n * far, and answers `-` when nothing new happened since: then the line stays as it is.\n *\n * @since 0.13.0\n */\nimport * as LanguageModel from \"effect/ai/LanguageModel\"\nimport * as Prompt from \"effect/ai/Prompt\"\nimport * as Context from \"effect/Context\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Layer from \"effect/Layer\"\nimport * as Option from \"effect/Option\"\n\n/**\n * The instructions the summary's model gets by default.\n *\n * @category constants\n * @since 0.13.0\n */\nexport const defaultInstructions =\n 'You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like \"Comparing last year\\'s budgets\". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.'\n\n/**\n * Represents the options accepted by {@link layer}.\n *\n * @category models\n * @since 0.13.0\n */\nexport interface Options {\n /** How long the line may stay empty before a summary fills it. Defaults to 2 seconds. */\n readonly after?: Duration.Input | undefined\n /** How long a line may stay unchanged before a summary refreshes it. Defaults to 5 seconds. */\n readonly staleAfter?: Duration.Input | undefined\n /** The summary model's instructions. Defaults to {@link defaultInstructions}. */\n readonly instructions?: string | undefined\n}\n\n/**\n * Service that writes a turn's status from what the turn did so far.\n *\n * @category services\n * @since 0.13.0\n */\nexport class StatusSummary extends Context.Service<\n StatusSummary,\n {\n readonly after: Duration.Duration\n readonly staleAfter: Duration.Duration\n /**\n * Returns the status for a turn, from its transcript: this turn after a few earlier messages,\n * what still runs, and the lines shown so far. `None` when the model has nothing new, wrote\n * nothing usable, or its call failed.\n */\n readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>\n }\n>()(\"effect-assistant/StatusSummary\") {}\n\nconst MAX_LENGTH = 60\n\nconst clean = (text: string): Option.Option<string> => {\n const line = (text.trim().split(\"\\n\")[0] ?? \"\")\n .replace(/^[\"'“”‘’]+|[\"'“”‘’]+$/g, \"\")\n .replace(/[.…]+$/, \"\")\n .trim()\n\n return line === \"\" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line)\n}\n\n/**\n * Creates the summary on the model it is given: provide a small, fast one.\n *\n * **Example** (A cheap model for statuses)\n *\n * ```ts\n * import { StatusSummary } from \"effect-assistant\"\n * import * as Layer from \"effect/Layer\"\n *\n * declare const MyAssistantLive: Layer.Layer<never>\n * declare const FastModel: Layer.Layer<import(\"effect/ai/LanguageModel\").LanguageModel>\n *\n * export const Live = MyAssistantLive.pipe(\n * Layer.provide(StatusSummary.layer({ after: \"2 seconds\" }).pipe(Layer.provide(FastModel)))\n * )\n * ```\n *\n * @category layers\n * @since 0.13.0\n */\nexport const layer = (options?: Options): Layer.Layer<StatusSummary, never, LanguageModel.LanguageModel> =>\n Layer.effect(\n StatusSummary,\n Effect.gen(function* () {\n const model = yield* LanguageModel.LanguageModel\n const instructions = options?.instructions ?? defaultInstructions\n\n const summarize = Effect.fn(\"StatusSummary.summarize\")(function* (transcript: string) {\n const response = yield* model.generateText({\n prompt: Prompt.fromMessages([\n Prompt.systemMessage({ content: instructions }),\n Prompt.userMessage({ content: [Prompt.textPart({ text: transcript })] })\n ])\n })\n\n return clean(response.text)\n })\n\n return StatusSummary.of({\n after: Duration.fromInputUnsafe(options?.after ?? \"2 seconds\"),\n staleAfter: Duration.fromInputUnsafe(options?.staleAfter ?? \"5 seconds\"),\n summarize: (transcript) =>\n summarize(transcript).pipe(\n Effect.catchCause((cause) =>\n Effect.as(Effect.logDebug(\"assistant status summary failed\", cause), Option.none())\n )\n )\n })\n })\n )\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAa,sBACX;;;;;;;AAuBF,IAAa,gBAAb,cAAmC,QAAQ,QAYzC,CAAC,CAAC,gCAAgC,CAAC,CAAC,CAAC;AAEvC,MAAM,aAAa;AAEnB,MAAM,SAAS,SAAwC;CACrD,MAAM,QAAQ,KAAK,KAAK,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,GAAA,CACzC,QAAQ,0BAA0B,EAAE,CAAC,CACrC,QAAQ,UAAU,EAAE,CAAC,CACrB,KAAK;CAER,OAAO,SAAS,MAAM,WAAW,KAAK,IAAI,KAAK,KAAK,SAAS,aAAa,OAAO,KAAK,IAAI,OAAO,KAAK,IAAI;AAC5G;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAa,SAAS,YACpB,MAAM,OACJ,eACA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,OAAO,cAAc;CACnC,MAAM,eAAe,SAAS,gBAAA;CAE9B,MAAM,YAAY,OAAO,GAAG,yBAAyB,CAAC,CAAC,WAAW,YAAoB;EACpF,MAAM,WAAW,OAAO,MAAM,aAAa,EACzC,QAAQ,OAAO,aAAa,CAC1B,OAAO,cAAc,EAAE,SAAS,aAAa,CAAC,GAC9C,OAAO,YAAY,EAAE,SAAS,CAAC,OAAO,SAAS,EAAE,MAAM,WAAW,CAAC,CAAC,EAAE,CAAC,CACzE,CAAC,EACH,CAAC;EAED,OAAO,MAAM,SAAS,IAAI;CAC5B,CAAC;CAED,OAAO,cAAc,GAAG;EACtB,OAAO,SAAS,gBAAgB,SAAS,SAAS,WAAW;EAC7D,YAAY,SAAS,gBAAgB,SAAS,cAAc,WAAW;EACvE,YAAY,eACV,UAAU,UAAU,CAAC,CAAC,KACpB,OAAO,YAAY,UACjB,OAAO,GAAG,OAAO,SAAS,mCAAmC,KAAK,GAAG,OAAO,KAAK,CAAC,CACpF,CACF;CACJ,CAAC;AACH,CAAC,CACH"}
|
package/dist/internal/live.js
CHANGED
|
@@ -18,6 +18,7 @@ const make = Effect.fnUntraced(function* (send) {
|
|
|
18
18
|
running: [],
|
|
19
19
|
shown: void 0,
|
|
20
20
|
shownAt: yield* Clock.currentTimeMillis,
|
|
21
|
+
lines: [],
|
|
21
22
|
typing: false
|
|
22
23
|
});
|
|
23
24
|
const change = (f) => SynchronizedRef.updateEffect(state, Effect.fnUntraced(function* (current) {
|
|
@@ -30,7 +31,8 @@ const make = Effect.fnUntraced(function* (send) {
|
|
|
30
31
|
const show = (current, text, now) => current.shown === text ? [[], current] : [[Status.make({ text })], {
|
|
31
32
|
...current,
|
|
32
33
|
shown: text,
|
|
33
|
-
shownAt: now
|
|
34
|
+
shownAt: now,
|
|
35
|
+
lines: [...current.lines, text]
|
|
34
36
|
}];
|
|
35
37
|
return {
|
|
36
38
|
callStarted: (callId, text) => change((current, now) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"live.js","names":[],"sources":["../../src/internal/live.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\n\ninterface Labelled {\n readonly callId: string\n readonly text: string\n}\n\n/** @internal */\nexport interface State {\n /** The described calls still running, oldest first. */\n readonly running: ReadonlyArray<Labelled>\n /** The line the user sees; none before the first status and after typing started. */\n readonly shown: string | undefined\n /** When the line last changed, or the turn started, in epoch milliseconds. */\n readonly shownAt: number\n readonly typing: boolean\n}\n\n/** @internal */\nexport interface Live {\n readonly callStarted: (callId: string, text: string) => Effect.Effect<void>\n readonly callEnded: (callId: string) => Effect.Effect<void>\n // A summary's line: shown unless the model started a message, or the line says it already.\n readonly summarized: (text: string) => Effect.Effect<void>\n readonly typing: (typing: boolean) => Effect.Effect<void>\n readonly state: Effect.Effect<State>\n // The turn ends: typing stops. Its end, in the log, clears the line.\n readonly reset: Effect.Effect<void>\n}\n\n// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.\n/** @internal */\nexport const TurnLive = Context.Reference<Option.Option<Live>>(\"effect-assistant/TurnLive\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {\n const live = yield* TurnLive\n\n if (Option.isSome(live)) yield* f(live.value)\n})\n\ntype Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {\n const state = yield* SynchronizedRef.make<State>({\n running: [],\n shown: undefined,\n shownAt: yield* Clock.currentTimeMillis,\n typing: false\n })\n\n // Under the ref's lock, so signals go out in the order the state changed.\n const change = (f: (current: State, now: number) => Change) =>\n SynchronizedRef.updateEffect(\n state,\n Effect.fnUntraced(function* (current) {\n const changed = f(current, yield* Clock.currentTimeMillis)\n\n if (changed === undefined) return current\n\n const [signals, next] = changed\n\n yield* Effect.forEach(signals, send, { discard: true })\n\n return next\n })\n )\n\n const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>\n current.shown === text\n ? [[], current]\n : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]\n\n const live: Live = {\n callStarted: (callId, text) =>\n change((current, now) => {\n const running = { ...current, running: [...current.running, { callId, text }] }\n\n return current.typing ? [[], running] : show(running, text, now)\n }),\n // The line stays when the last labelled call ends; while another runs, the line says that one.\n callEnded: (callId) =>\n change((current, now) => {\n const ended = current.running.find((call) => call.callId === callId)\n\n if (ended === undefined) return undefined\n\n const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }\n const still = next.running.at(-1)\n\n return still === undefined || current.typing || current.shown !== ended.text\n ? [[], next]\n : show(next, still.text, now)\n }),\n summarized: (text) =>\n change((current, now) =>\n current.typing\n ? undefined\n : current.shown === text\n ? [[], { ...current, shownAt: now }]\n : show(current, text, now)\n ),\n typing: (typing) =>\n change((current, now) => {\n if (current.typing === typing) return undefined\n\n if (typing) {\n return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]\n }\n\n // A call that runs on after the message still says what it does.\n const stopped = { ...current, typing, shownAt: now }\n const still = current.running.at(-1)\n const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)\n\n return [[ChatSignal.TypingStopped.make({}), ...signals], next]\n }),\n state: SynchronizedRef.get(state),\n reset: change((current) =>\n current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined\n )\n }\n\n return live\n})\n"],"mappings":";;;;;;;;
|
|
1
|
+
{"version":3,"file":"live.js","names":[],"sources":["../../src/internal/live.ts"],"sourcesContent":["import * as Clock from \"effect/Clock\"\nimport * as Context from \"effect/Context\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as SynchronizedRef from \"effect/SynchronizedRef\"\nimport * as ChatSignal from \"../ChatSignal.ts\"\n\ninterface Labelled {\n readonly callId: string\n readonly text: string\n}\n\n/** @internal */\nexport interface State {\n /** The described calls still running, oldest first. */\n readonly running: ReadonlyArray<Labelled>\n /** The line the user sees; none before the first status and after typing started. */\n readonly shown: string | undefined\n /** When the line last changed, or the turn started, in epoch milliseconds. */\n readonly shownAt: number\n /** Every line the turn showed, oldest first. */\n readonly lines: ReadonlyArray<string>\n readonly typing: boolean\n}\n\n/** @internal */\nexport interface Live {\n readonly callStarted: (callId: string, text: string) => Effect.Effect<void>\n readonly callEnded: (callId: string) => Effect.Effect<void>\n // A summary's line: shown unless the model started a message, or the line says it already.\n readonly summarized: (text: string) => Effect.Effect<void>\n readonly typing: (typing: boolean) => Effect.Effect<void>\n readonly state: Effect.Effect<State>\n // The turn ends: typing stops. Its end, in the log, clears the line.\n readonly reset: Effect.Effect<void>\n}\n\n// A turn's own: a specialist's run, or a loop outside a turn, shows nothing.\n/** @internal */\nexport const TurnLive = Context.Reference<Option.Option<Live>>(\"effect-assistant/TurnLive\", {\n defaultValue: Option.none\n})\n\n/** @internal */\nexport const use = Effect.fnUntraced(function* (f: (live: Live) => Effect.Effect<void>) {\n const live = yield* TurnLive\n\n if (Option.isSome(live)) yield* f(live.value)\n})\n\ntype Change = readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] | undefined\n\n/** @internal */\nexport const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatSignal) => Effect.Effect<void>) {\n const state = yield* SynchronizedRef.make<State>({\n running: [],\n shown: undefined,\n shownAt: yield* Clock.currentTimeMillis,\n lines: [],\n typing: false\n })\n\n // Under the ref's lock, so signals go out in the order the state changed.\n const change = (f: (current: State, now: number) => Change) =>\n SynchronizedRef.updateEffect(\n state,\n Effect.fnUntraced(function* (current) {\n const changed = f(current, yield* Clock.currentTimeMillis)\n\n if (changed === undefined) return current\n\n const [signals, next] = changed\n\n yield* Effect.forEach(signals, send, { discard: true })\n\n return next\n })\n )\n\n const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>\n current.shown === text\n ? [[], current]\n : [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now, lines: [...current.lines, text] }]\n\n const live: Live = {\n callStarted: (callId, text) =>\n change((current, now) => {\n const running = { ...current, running: [...current.running, { callId, text }] }\n\n return current.typing ? [[], running] : show(running, text, now)\n }),\n // The line stays when the last labelled call ends; while another runs, the line says that one.\n callEnded: (callId) =>\n change((current, now) => {\n const ended = current.running.find((call) => call.callId === callId)\n\n if (ended === undefined) return undefined\n\n const next = { ...current, running: current.running.filter((call) => call.callId !== callId) }\n const still = next.running.at(-1)\n\n return still === undefined || current.typing || current.shown !== ended.text\n ? [[], next]\n : show(next, still.text, now)\n }),\n summarized: (text) =>\n change((current, now) =>\n current.typing\n ? undefined\n : current.shown === text\n ? [[], { ...current, shownAt: now }]\n : show(current, text, now)\n ),\n typing: (typing) =>\n change((current, now) => {\n if (current.typing === typing) return undefined\n\n if (typing) {\n return [[ChatSignal.TypingStarted.make({})], { ...current, typing, shown: undefined, shownAt: now }]\n }\n\n // A call that runs on after the message still says what it does.\n const stopped = { ...current, typing, shownAt: now }\n const still = current.running.at(-1)\n const [signals, next] = still === undefined ? [[], stopped] : show(stopped, still.text, now)\n\n return [[ChatSignal.TypingStopped.make({}), ...signals], next]\n }),\n state: SynchronizedRef.get(state),\n reset: change((current) =>\n current.typing ? [[ChatSignal.TypingStopped.make({})], { ...current, typing: false }] : undefined\n )\n }\n\n return live\n})\n"],"mappings":";;;;;;;;AAuCA,MAAa,WAAW,QAAQ,UAA+B,6BAA6B,EAC1F,cAAc,OAAO,KACvB,CAAC;;AAGD,MAAa,MAAM,OAAO,WAAW,WAAW,GAAwC;CACtF,MAAM,OAAO,OAAO;CAEpB,IAAI,OAAO,OAAO,IAAI,GAAG,OAAO,EAAE,KAAK,KAAK;AAC9C,CAAC;;AAKD,MAAa,OAAO,OAAO,WAAW,WAAW,MAA8D;CAC7G,MAAM,QAAQ,OAAO,gBAAgB,KAAY;EAC/C,SAAS,CAAC;EACV,OAAO,KAAA;EACP,SAAS,OAAO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AC;EACH,aAAa,SACX,QAAQ,SAAS,QACf,QAAQ,SACJ,KAAA,IACA,QAAQ,UAAU,OAChB,CAAC,CAAC,GAAG;GAAE,GAAG;GAAS,SAAS;EAAI,CAAC,IACjC,KAAK,SAAS,MAAM,GAAG,CAC/B;EACF,SAAS,WACP,QAAQ,SAAS,QAAQ;GACvB,IAAI,QAAQ,WAAW,QAAQ,OAAO,KAAA;GAEtC,IAAI,QACF,OAAO,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,CAAC,GAAG;IAAE,GAAG;IAAS;IAAQ,OAAO,KAAA;IAAW,SAAS;GAAI,CAAC;GAIrG,MAAM,UAAU;IAAE,GAAG;IAAS;IAAQ,SAAS;GAAI;GACnD,MAAM,QAAQ,QAAQ,QAAQ,GAAG,EAAE;GACnC,MAAM,CAAC,SAAS,QAAQ,UAAU,KAAA,IAAY,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,SAAS,MAAM,MAAM,GAAG;GAE3F,OAAO,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,GAAG,GAAG,OAAO,GAAG,IAAI;EAC/D,CAAC;EACH,OAAO,gBAAgB,IAAI,KAAK;EAChC,OAAO,QAAQ,YACb,QAAQ,SAAS,CAAC,CAAA,cAA0B,KAAK,CAAC,CAAC,CAAC,GAAG;GAAE,GAAG;GAAS,QAAQ;EAAM,CAAC,IAAI,KAAA,CAC1F;CAGQ;AACZ,CAAC"}
|
package/dist/internal/summary.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isLibraryEvent } from "../AssistantEvent.js";
|
|
1
|
+
import { isLibraryEvent, isTurnEnd } from "../AssistantEvent.js";
|
|
2
2
|
import { toJsonText } from "./schema.js";
|
|
3
3
|
import { fromEntries } from "./history.js";
|
|
4
4
|
import * as Effect from "effect/Effect";
|
|
@@ -31,10 +31,29 @@ const runningCalls = (entries) => {
|
|
|
31
31
|
const answered = new Set(events.flatMap((event) => Predicate.isTagged(event, "ToolResult") ? [event.callId] : []));
|
|
32
32
|
return events.flatMap((event) => Predicate.isTagged(event, "ToolStarted") && !answered.has(event.callId) ? [event.tool] : []);
|
|
33
33
|
};
|
|
34
|
+
const turnStart = (entries) => {
|
|
35
|
+
const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, "TurnStarted"));
|
|
36
|
+
return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => isTurnEnd(event)) + 1;
|
|
37
|
+
};
|
|
34
38
|
/** @internal */
|
|
35
|
-
const transcript = (entries) => {
|
|
36
|
-
const
|
|
37
|
-
|
|
39
|
+
const transcript = (entries, shown) => {
|
|
40
|
+
const start = turnStart(entries);
|
|
41
|
+
const all = fromEntries(entries);
|
|
42
|
+
const earlierCount = Math.min(fromEntries(entries.slice(0, start)).length, all.length);
|
|
43
|
+
const earlier = all.slice(0, earlierCount).slice(-4).flatMap(lines);
|
|
44
|
+
const now = all.slice(earlierCount).slice(-12).flatMap(lines);
|
|
45
|
+
const running = runningCalls(entries.slice(start));
|
|
46
|
+
return [
|
|
47
|
+
...earlier.length === 0 ? [] : [
|
|
48
|
+
"Earlier in the chat:",
|
|
49
|
+
...earlier,
|
|
50
|
+
""
|
|
51
|
+
],
|
|
52
|
+
"This turn:",
|
|
53
|
+
...now,
|
|
54
|
+
...running.length === 0 ? [] : [`Running now: ${running.join(", ")}`],
|
|
55
|
+
...shown.length === 0 ? [] : ["", `Shown so far: ${shown.map((line) => `"${line}"`).join(", ")}`]
|
|
56
|
+
].join("\n");
|
|
38
57
|
};
|
|
39
58
|
const lastSequence = (entries) => entries.at(-1)?.sequence ?? 0;
|
|
40
59
|
const isWork = (event) => Predicate.isTagged(event, "TurnStarted") || Predicate.isTagged(event, "ModelOutput") || Predicate.isTagged(event, "ToolStarted");
|
|
@@ -55,7 +74,7 @@ const run = Effect.fnUntraced(function* (summary, live, chat) {
|
|
|
55
74
|
const through = lastSequence(entries);
|
|
56
75
|
const waited = (yield* Clock.currentTimeMillis) - current.shownAt;
|
|
57
76
|
if (current.typing || current.running.length > 0 || waited < (current.shown === void 0 ? after : staleAfter) || through === (yield* Ref.get(summarized)) || !workingSinceMessage(entries)) return;
|
|
58
|
-
const written = yield* summary.summarize(transcript(entries));
|
|
77
|
+
const written = yield* summary.summarize(transcript(entries, current.lines));
|
|
59
78
|
yield* Ref.set(summarized, through);
|
|
60
79
|
if (Option.isSome(written)) yield* live.summarized(written.value);
|
|
61
80
|
}).pipe(Effect.repeat(Schedule.spaced(Math.max(MIN_CHECK_MILLIS, Math.min(MAX_CHECK_MILLIS, after / 2, staleAfter / 2)))));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"summary.js","names":["AssistantEvent.isLibraryEvent","History.fromEntries"],"sources":["../../src/internal/summary.ts"],"sourcesContent":["import type * as Prompt from \"effect/ai/Prompt\"\nimport * as Clock from \"effect/Clock\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Ref from \"effect/Ref\"\nimport * as Schedule from \"effect/Schedule\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as StatusSummary from \"../StatusSummary.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport { toJsonText } from \"./schema.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst MAX_LINE = 300\n\nconst MAX_MESSAGES = 12\n\nconst clip = (text: string) => (text.length > MAX_LINE ? `${text.slice(0, MAX_LINE)}…` : text)\n\nconst assistantLines = (part: Prompt.AssistantMessagePart): ReadonlyArray<string> => {\n switch (part.type) {\n case \"text\":\n return [`Assistant: ${clip(part.text)}`]\n case \"tool-call\":\n return [`Assistant runs ${part.name} ${clip(toJsonText(part.params))}`]\n default:\n return []\n }\n}\n\nconst lines = (message: Prompt.Message): ReadonlyArray<string> => {\n switch (message.role) {\n case \"system\":\n return []\n case \"user\":\n return message.content.flatMap((part) => (part.type === \"text\" ? [`User: ${clip(part.text)}`] : []))\n case \"assistant\":\n return message.content.flatMap(assistantLines)\n case \"tool\":\n return message.content.flatMap((part) =>\n part.type === \"tool-result\"\n ? [`Result of ${part.name}: ${clip(Predicate.isString(part.result) ? part.result : toJsonText(part.result))}`]\n : []\n )\n }\n}\n\n// The calls started and not answered yet: what the assistant is doing right now.\nconst runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const events = entries.flatMap(({ event }) => (AssistantEvent.isLibraryEvent(event) ? [event] : []))\n const answered = new Set(events.flatMap((event) => (Predicate.isTagged(event, \"ToolResult\") ? [event.callId] : [])))\n\n return events.flatMap((event) =>\n Predicate.isTagged(event, \"ToolStarted\") && !answered.has(event.callId) ? [event.tool] : []\n )\n}\n\n// The
|
|
1
|
+
{"version":3,"file":"summary.js","names":["AssistantEvent.isLibraryEvent","AssistantEvent.isTurnEnd","History.fromEntries"],"sources":["../../src/internal/summary.ts"],"sourcesContent":["import type * as Prompt from \"effect/ai/Prompt\"\nimport * as Clock from \"effect/Clock\"\nimport * as Duration from \"effect/Duration\"\nimport * as Effect from \"effect/Effect\"\nimport * as Option from \"effect/Option\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Ref from \"effect/Ref\"\nimport * as Schedule from \"effect/Schedule\"\nimport * as AssistantEvent from \"../AssistantEvent.ts\"\nimport type * as StatusSummary from \"../StatusSummary.ts\"\nimport * as History from \"./history.ts\"\nimport * as Live from \"./live.ts\"\nimport { toJsonText } from \"./schema.ts\"\nimport type { Writer } from \"./writer.ts\"\n\nconst MAX_LINE = 300\n\nconst MAX_MESSAGES = 12\n\nconst clip = (text: string) => (text.length > MAX_LINE ? `${text.slice(0, MAX_LINE)}…` : text)\n\nconst assistantLines = (part: Prompt.AssistantMessagePart): ReadonlyArray<string> => {\n switch (part.type) {\n case \"text\":\n return [`Assistant: ${clip(part.text)}`]\n case \"tool-call\":\n return [`Assistant runs ${part.name} ${clip(toJsonText(part.params))}`]\n default:\n return []\n }\n}\n\nconst lines = (message: Prompt.Message): ReadonlyArray<string> => {\n switch (message.role) {\n case \"system\":\n return []\n case \"user\":\n return message.content.flatMap((part) => (part.type === \"text\" ? [`User: ${clip(part.text)}`] : []))\n case \"assistant\":\n return message.content.flatMap(assistantLines)\n case \"tool\":\n return message.content.flatMap((part) =>\n part.type === \"tool-result\"\n ? [`Result of ${part.name}: ${clip(Predicate.isString(part.result) ? part.result : toJsonText(part.result))}`]\n : []\n )\n }\n}\n\n// The calls started and not answered yet: what the assistant is doing right now.\nconst runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const events = entries.flatMap(({ event }) => (AssistantEvent.isLibraryEvent(event) ? [event] : []))\n const answered = new Set(events.flatMap((event) => (Predicate.isTagged(event, \"ToolResult\") ? [event.callId] : [])))\n\n return events.flatMap((event) =>\n Predicate.isTagged(event, \"ToolStarted\") && !answered.has(event.callId) ? [event.tool] : []\n )\n}\n\nconst MAX_EARLIER = 4\n\n// The turn's input follows the end of the turn before it.\nconst turnStart = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, \"TurnStarted\"))\n\n return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => AssistantEvent.isTurnEnd(event)) + 1\n}\n\n// This turn's messages as plain lines, after a few earlier ones that are only context, with what still runs and the\n// lines the user saw: the summary's model gets no tools, so no tool call parts.\n/** @internal */\nexport const transcript = (entries: ReadonlyArray<AssistantEvent.AnyEntry>, shown: ReadonlyArray<string>): string => {\n const start = turnStart(entries)\n const all = History.fromEntries(entries)\n const earlierCount = Math.min(History.fromEntries(entries.slice(0, start)).length, all.length)\n const earlier = all.slice(0, earlierCount).slice(-MAX_EARLIER).flatMap(lines)\n const now = all.slice(earlierCount).slice(-MAX_MESSAGES).flatMap(lines)\n const running = runningCalls(entries.slice(start))\n\n return [\n ...(earlier.length === 0 ? [] : [\"Earlier in the chat:\", ...earlier, \"\"]),\n \"This turn:\",\n ...now,\n ...(running.length === 0 ? [] : [`Running now: ${running.join(\", \")}`]),\n ...(shown.length === 0 ? [] : [\"\", `Shown so far: ${shown.map((line) => `\"${line}\"`).join(\", \")}`])\n ].join(\"\\n\")\n}\n\nconst lastSequence = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => entries.at(-1)?.sequence ?? 0\n\nconst isWork = (event: { readonly _tag: string }) =>\n Predicate.isTagged(event, \"TurnStarted\") ||\n Predicate.isTagged(event, \"ModelOutput\") ||\n Predicate.isTagged(event, \"ToolStarted\")\n\n// After a message the user is up to date: a summary shows again only for work the model started since, not for what a\n// handler does after its reply (a compaction, say).\nconst workingSinceMessage = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {\n const message = entries.findLastIndex(({ event }) => Predicate.isTagged(event, \"AssistantMessage\"))\n\n return message === -1 || entries.slice(message + 1).some(({ event }) => isWork(event))\n}\n\nconst MAX_CHECK_MILLIS = 500\n\nconst MIN_CHECK_MILLIS = 100\n\n// A line for a turn that shows nothing for a while, or whose line hasn't changed for longer: never while a described\n// call says what runs, or the model writes to the user, and only after something new happened.\n/** @internal */\nexport const run = Effect.fnUntraced(function* (\n summary: StatusSummary.StatusSummary[\"Service\"],\n live: Live.Live,\n chat: Writer\n) {\n const summarized = yield* Ref.make(-1)\n const after = Duration.toMillis(summary.after)\n const staleAfter = Duration.toMillis(summary.staleAfter)\n\n const tick = Effect.gen(function* () {\n const current = yield* live.state\n const entries = yield* chat.entries\n const through = lastSequence(entries)\n const waited = (yield* Clock.currentTimeMillis) - current.shownAt\n\n if (\n current.typing ||\n current.running.length > 0 ||\n waited < (current.shown === undefined ? after : staleAfter) ||\n through === (yield* Ref.get(summarized)) ||\n !workingSinceMessage(entries)\n ) {\n return\n }\n\n const written = yield* summary.summarize(transcript(entries, current.lines))\n\n yield* Ref.set(summarized, through)\n\n if (Option.isSome(written)) yield* live.summarized(written.value)\n })\n\n // Often enough that a line is filled or refreshed close to when it's due.\n yield* tick.pipe(\n Effect.repeat(Sched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AIzC,MAAM,uBAAuB,YAAoD;CAC/E,MAAM,UAAU,QAAQ,eAAe,EAAE,YAAY,UAAU,SAAS,OAAO,kBAAkB,CAAC;CAElG,OAAO,YAAY,MAAM,QAAQ,MAAM,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,YAAY,OAAO,KAAK,CAAC;AACvF;AAEA,MAAM,mBAAmB;AAEzB,MAAM,mBAAmB;;AAKzB,MAAa,MAAM,OAAO,WAAW,WACnC,SACA,MACA,MACA;CACA,MAAM,aAAa,OAAO,IAAI,KAAK,EAAE;CACrC,MAAM,QAAQ,SAAS,SAAS,QAAQ,KAAK;CAC7C,MAAM,aAAa,SAAS,SAAS,QAAQ,UAAU;CA0BvD,OAxBa,OAAO,IAAI,aAAa;EACnC,MAAM,UAAU,OAAO,KAAK;EAC5B,MAAM,UAAU,OAAO,KAAK;EAC5B,MAAM,UAAU,aAAa,OAAO;EACpC,MAAM,UAAU,OAAO,MAAM,qBAAqB,QAAQ;EAE1D,IACE,QAAQ,UACR,QAAQ,QAAQ,SAAS,KACzB,UAAU,QAAQ,UAAU,KAAA,IAAY,QAAQ,eAChD,aAAa,OAAO,IAAI,IAAI,UAAU,MACtC,CAAC,oBAAoB,OAAO,GAE5B;EAGF,MAAM,UAAU,OAAO,QAAQ,UAAU,WAAW,SAAS,QAAQ,KAAK,CAAC;EAE3E,OAAO,IAAI,IAAI,YAAY,OAAO;EAElC,IAAI,OAAO,OAAO,OAAO,GAAG,OAAO,KAAK,WAAW,QAAQ,KAAK;CAClE,CAGU,CAAC,CAAC,KACV,OAAO,OAAO,SAAS,OAAO,KAAK,IAAI,kBAAkB,KAAK,IAAI,kBAAkB,QAAQ,GAAG,aAAa,CAAC,CAAC,CAAC,CAAC,CAClH;AACF,CAAC"}
|
package/package.json
CHANGED
package/src/StatusSummary.ts
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
* Without it, a turn shows only the statuses its tool calls write (see `ToolStatus`). The summary
|
|
8
8
|
* is a `ChatSignal.Status` like any other: it is never stored and the turn's model never reads it.
|
|
9
9
|
* It never replaces the status of a described call that still runs, never shows while the model
|
|
10
|
-
* writes to the user, and never after the turn's message unless the model starts new work.
|
|
11
|
-
*
|
|
10
|
+
* writes to the user, and never after the turn's message unless the model starts new work. The
|
|
11
|
+
* model reads this turn, after a few earlier messages as context, and the lines the turn showed so
|
|
12
|
+
* far, and answers `-` when nothing new happened since: then the line stays as it is.
|
|
12
13
|
*
|
|
13
14
|
* @since 0.13.0
|
|
14
15
|
*/
|
|
@@ -27,7 +28,7 @@ import * as Option from "effect/Option"
|
|
|
27
28
|
* @since 0.13.0
|
|
28
29
|
*/
|
|
29
30
|
export const defaultInstructions =
|
|
30
|
-
'You write the status line a chat shows while an assistant works for its user. Read
|
|
31
|
+
'You write the status line a chat shows while an assistant works for its user. Read this turn and say what the assistant is doing now, in 3 to 5 plain words, in the language the user writes in, like "Comparing last year\'s budgets". Earlier in the chat is only context: never describe it. Never say again, in any words, what a line shown so far says; when nothing new happened since, answer with -. No tool names, ids, code, quotes or final period. Answer with the status only.'
|
|
31
32
|
|
|
32
33
|
/**
|
|
33
34
|
* Represents the options accepted by {@link layer}.
|
|
@@ -56,8 +57,9 @@ export class StatusSummary extends Context.Service<
|
|
|
56
57
|
readonly after: Duration.Duration
|
|
57
58
|
readonly staleAfter: Duration.Duration
|
|
58
59
|
/**
|
|
59
|
-
* Returns the status for a turn, from its transcript:
|
|
60
|
-
*
|
|
60
|
+
* Returns the status for a turn, from its transcript: this turn after a few earlier messages,
|
|
61
|
+
* what still runs, and the lines shown so far. `None` when the model has nothing new, wrote
|
|
62
|
+
* nothing usable, or its call failed.
|
|
61
63
|
*/
|
|
62
64
|
readonly summarize: (transcript: string) => Effect.Effect<Option.Option<string>>
|
|
63
65
|
}
|
|
@@ -71,7 +73,7 @@ const clean = (text: string): Option.Option<string> => {
|
|
|
71
73
|
.replace(/[.…]+$/, "")
|
|
72
74
|
.trim()
|
|
73
75
|
|
|
74
|
-
return line === "" || line.length > MAX_LENGTH ? Option.none() : Option.some(line)
|
|
76
|
+
return line === "" || /^[-–—]+$/.test(line) || line.length > MAX_LENGTH ? Option.none() : Option.some(line)
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
/**
|
package/src/internal/live.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface State {
|
|
|
18
18
|
readonly shown: string | undefined
|
|
19
19
|
/** When the line last changed, or the turn started, in epoch milliseconds. */
|
|
20
20
|
readonly shownAt: number
|
|
21
|
+
/** Every line the turn showed, oldest first. */
|
|
22
|
+
readonly lines: ReadonlyArray<string>
|
|
21
23
|
readonly typing: boolean
|
|
22
24
|
}
|
|
23
25
|
|
|
@@ -54,6 +56,7 @@ export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatS
|
|
|
54
56
|
running: [],
|
|
55
57
|
shown: undefined,
|
|
56
58
|
shownAt: yield* Clock.currentTimeMillis,
|
|
59
|
+
lines: [],
|
|
57
60
|
typing: false
|
|
58
61
|
})
|
|
59
62
|
|
|
@@ -77,7 +80,7 @@ export const make = Effect.fnUntraced(function* (send: (signal: ChatSignal.ChatS
|
|
|
77
80
|
const show = (current: State, text: string, now: number): readonly [ReadonlyArray<ChatSignal.ChatSignal>, State] =>
|
|
78
81
|
current.shown === text
|
|
79
82
|
? [[], current]
|
|
80
|
-
: [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now }]
|
|
83
|
+
: [[ChatSignal.Status.make({ text })], { ...current, shown: text, shownAt: now, lines: [...current.lines, text] }]
|
|
81
84
|
|
|
82
85
|
const live: Live = {
|
|
83
86
|
callStarted: (callId, text) =>
|
package/src/internal/summary.ts
CHANGED
|
@@ -57,13 +57,33 @@ const runningCalls = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
|
57
57
|
)
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
60
|
+
const MAX_EARLIER = 4
|
|
61
|
+
|
|
62
|
+
// The turn's input follows the end of the turn before it.
|
|
63
|
+
const turnStart = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => {
|
|
64
|
+
const started = entries.findLastIndex(({ event }) => Predicate.isTagged(event, "TurnStarted"))
|
|
65
65
|
|
|
66
|
-
return
|
|
66
|
+
return entries.slice(0, Math.max(started, 0)).findLastIndex(({ event }) => AssistantEvent.isTurnEnd(event)) + 1
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// This turn's messages as plain lines, after a few earlier ones that are only context, with what still runs and the
|
|
70
|
+
// lines the user saw: the summary's model gets no tools, so no tool call parts.
|
|
71
|
+
/** @internal */
|
|
72
|
+
export const transcript = (entries: ReadonlyArray<AssistantEvent.AnyEntry>, shown: ReadonlyArray<string>): string => {
|
|
73
|
+
const start = turnStart(entries)
|
|
74
|
+
const all = History.fromEntries(entries)
|
|
75
|
+
const earlierCount = Math.min(History.fromEntries(entries.slice(0, start)).length, all.length)
|
|
76
|
+
const earlier = all.slice(0, earlierCount).slice(-MAX_EARLIER).flatMap(lines)
|
|
77
|
+
const now = all.slice(earlierCount).slice(-MAX_MESSAGES).flatMap(lines)
|
|
78
|
+
const running = runningCalls(entries.slice(start))
|
|
79
|
+
|
|
80
|
+
return [
|
|
81
|
+
...(earlier.length === 0 ? [] : ["Earlier in the chat:", ...earlier, ""]),
|
|
82
|
+
"This turn:",
|
|
83
|
+
...now,
|
|
84
|
+
...(running.length === 0 ? [] : [`Running now: ${running.join(", ")}`]),
|
|
85
|
+
...(shown.length === 0 ? [] : ["", `Shown so far: ${shown.map((line) => `"${line}"`).join(", ")}`])
|
|
86
|
+
].join("\n")
|
|
67
87
|
}
|
|
68
88
|
|
|
69
89
|
const lastSequence = (entries: ReadonlyArray<AssistantEvent.AnyEntry>) => entries.at(-1)?.sequence ?? 0
|
|
@@ -113,7 +133,7 @@ export const run = Effect.fnUntraced(function* (
|
|
|
113
133
|
return
|
|
114
134
|
}
|
|
115
135
|
|
|
116
|
-
const written = yield* summary.summarize(transcript(entries))
|
|
136
|
+
const written = yield* summary.summarize(transcript(entries, current.lines))
|
|
117
137
|
|
|
118
138
|
yield* Ref.set(summarized, through)
|
|
119
139
|
|