@xandreed/plugin-memory-summary 0.8.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Xand Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # @xandreed/plugin-memory-summary
2
+
3
+ Efferent composable agent framework, 0.8.0-next.0. Bun on Linux.
4
+
5
+ Documentation: https://github.com/xandreeddev/efferent
@@ -0,0 +1 @@
1
+ export { MemorySummaryConfig, memorySummaryDefaults, MemorySummaryLive, memorySummaryPlugin, SUMMARY_STRATEGY, summaryPolicy, default, } from "./plugin.adapter.js";
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ export {
2
+ MemorySummaryConfig,
3
+ memorySummaryDefaults,
4
+ MemorySummaryLive,
5
+ memorySummaryPlugin,
6
+ SUMMARY_STRATEGY,
7
+ summaryPolicy,
8
+ default
9
+ } from "./plugin.adapter.js";
@@ -0,0 +1,66 @@
1
+ import { Effect, Layer, Schema } from "effect";
2
+ import { ConversationMemory, HarnessError } from "@xandreed/core";
3
+ import type { MemoryPolicy } from "@xandreed/core";
4
+ export declare const MemorySummaryConfig: Schema.Struct<{
5
+ /** Summarize once the rendered messages pass this share of the budget. */
6
+ readonly triggerRatio: Schema.Number;
7
+ /** Keep at least this share of the budget verbatim (the newest turns). */
8
+ readonly keepRatio: Schema.Number;
9
+ /** Turns between two summaries (a summary is not revisited every step). */
10
+ readonly cooldownTurns: Schema.Int;
11
+ readonly turnContext: Schema.Literals<readonly ["current", "all"]>;
12
+ readonly replies: Schema.Boolean;
13
+ /** Show recorded tool digests in place of the results they digest. */
14
+ readonly digests: Schema.Boolean;
15
+ readonly media: Schema.Literals<readonly ["none", "inline"]>;
16
+ readonly maxImages: Schema.Int;
17
+ /** Instructions for the summarizer; the transcript and any earlier summary follow. */
18
+ readonly instructions: Schema.String;
19
+ }>;
20
+ export type MemorySummaryConfig = typeof MemorySummaryConfig.Type;
21
+ export declare const memorySummaryDefaults: MemorySummaryConfig;
22
+ export declare const SUMMARY_STRATEGY: {
23
+ readonly id: "summary";
24
+ readonly version: "1";
25
+ };
26
+ /**
27
+ * Summarizing conversation memory: the same full-fidelity log and render as
28
+ * the window strategy, but when context grows past the trigger the oldest
29
+ * turns are folded into one recorded summary (previous summary included).
30
+ */
31
+ export declare const summaryPolicy: (config: MemorySummaryConfig, summarize: (prompt: string) => Effect.Effect<string, HarnessError>) => MemoryPolicy;
32
+ /** The summarizer is the UtilityLlm of where each session is opened (the turn), so it runs under that turn's budget. */
33
+ export declare const memorySummaryPlugin: import("@xandreed/core").TypedPlugin<{
34
+ readonly triggerRatio: number;
35
+ readonly keepRatio: number;
36
+ readonly cooldownTurns: number;
37
+ readonly turnContext: "current" | "all";
38
+ readonly replies: boolean;
39
+ readonly digests: boolean;
40
+ readonly media: "none" | "inline";
41
+ readonly maxImages: number;
42
+ readonly instructions: string;
43
+ }, {
44
+ readonly triggerRatio: number;
45
+ readonly keepRatio: number;
46
+ readonly cooldownTurns: number;
47
+ readonly turnContext: "current" | "all";
48
+ readonly replies: boolean;
49
+ readonly digests: boolean;
50
+ readonly media: "none" | "inline";
51
+ readonly maxImages: number;
52
+ readonly instructions: string;
53
+ }, ConversationMemory, never, never>;
54
+ /** Summarizing memory as a typed layer: provides ConversationMemory (and needs a UtilityLlm per turn). */
55
+ export declare const MemorySummaryLive: (options?: Partial<{
56
+ readonly triggerRatio: number;
57
+ readonly keepRatio: number;
58
+ readonly cooldownTurns: number;
59
+ readonly turnContext: "current" | "all";
60
+ readonly replies: boolean;
61
+ readonly digests: boolean;
62
+ readonly media: "none" | "inline";
63
+ readonly maxImages: number;
64
+ readonly instructions: string;
65
+ }> | undefined) => Layer.Layer<ConversationMemory, HarnessError, never>;
66
+ export default memorySummaryPlugin;
@@ -0,0 +1,76 @@
1
+ import { Effect, Layer, Option, Schema } from "effect";
2
+ import {
3
+ ConversationMemory,
4
+ definePlugin,
5
+ digestTranscript,
6
+ estimateMessageTokens,
7
+ HarnessError,
8
+ openLogSession,
9
+ UtilityLlm
10
+ } from "@xandreed/core";
11
+ export const MemorySummaryConfig = Schema.Struct({
12
+ triggerRatio: Schema.Number.pipe(Schema.check(Schema.isBetween({ minimum: 0.1, maximum: 1 }))),
13
+ keepRatio: Schema.Number.pipe(Schema.check(Schema.isBetween({ minimum: 0.05, maximum: 0.9 }))),
14
+ cooldownTurns: Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0))),
15
+ turnContext: Schema.Literals(["current", "all"]),
16
+ replies: Schema.Boolean,
17
+ digests: Schema.Boolean,
18
+ media: Schema.Literals(["none", "inline"]),
19
+ maxImages: Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0))),
20
+ instructions: Schema.String
21
+ }), memorySummaryDefaults = {
22
+ triggerRatio: 0.8,
23
+ keepRatio: 0.16,
24
+ cooldownTurns: 3,
25
+ turnContext: "current",
26
+ replies: !0,
27
+ digests: !0,
28
+ media: "none",
29
+ maxImages: 8,
30
+ instructions: "Summarize the earlier part of this conversation for the same assistant, which will continue from your summary plus the newest turns. Preserve, in order: the user's goals and constraints; facts and records established (with their identifiers); answers already given; open questions and pending work. Dense prose and lists; never invent anything not in the transcript."
31
+ }, SUMMARY_STRATEGY = { id: "summary", version: "1" };
32
+ const latestSummary = (entries) => Option.fromNullishOr(entries.flatMap((entry) => entry.body._tag === "Compaction" && entry.body.strategy === SUMMARY_STRATEGY.id && entry.body.action._tag === "Summarize" ? [{ turn: entry.turn, keepFromTurn: entry.body.action.keepFromTurn, summary: entry.body.action.summary }] : []).at(-1)), keepFrom = (entries, turn, keepTokens, render) => Array.from({ length: turn }, (_, index) => turn - index).reduce((kept, candidate) => {
33
+ const tail = entries.filter((entry) => entry.turn >= candidate);
34
+ return estimateMessageTokens(render(tail)) <= keepTokens ? candidate : kept;
35
+ }, turn);
36
+ export const summaryPolicy = (config, summarize) => ({
37
+ strategy: SUMMARY_STRATEGY,
38
+ render: { turnContext: config.turnContext, replies: config.replies, digests: config.digests, media: { mode: config.media, maxImages: config.maxImages } },
39
+ digestOnWrite: Option.none(),
40
+ maintain: (input) => decide(config, summarize, input).pipe(Effect.map((actions) => ({ actions, digest: [] })))
41
+ });
42
+ const decide = (config, summarize, { entries, signal, turn, render }) => Effect.gen(function* () {
43
+ const tokens = estimateMessageTokens(render(entries)), previous = latestSummary(entries), cooling = Option.match(previous, { onNone: () => !1, onSome: (prior) => turn - prior.turn < config.cooldownTurns });
44
+ if (tokens <= signal.budgetTokens * config.triggerRatio || cooling)
45
+ return [];
46
+ const from = keepFrom(entries, turn, signal.budgetTokens * config.keepRatio, render), priorKeep = Option.match(previous, { onNone: () => 1, onSome: (prior) => prior.keepFromTurn });
47
+ if (from <= priorKeep)
48
+ return [];
49
+ const folded = entries.filter((entry) => entry.turn >= priorKeep && entry.turn < from), transcript = digestTranscript(folded, { result: 240, total: 120000 }), summary = (yield* summarize(`${config.instructions}${Option.match(previous, {
50
+ onNone: () => "",
51
+ onSome: (prior) => `
52
+
53
+ An earlier summary already covers the oldest turns \u2014 fold its facts in:
54
+ ${prior.summary}`
55
+ })}
56
+
57
+ TRANSCRIPT:
58
+ ${transcript}`)).trim(), action = { _tag: "Summarize", keepFromTurn: from, summary };
59
+ return summary.length === 0 ? [] : [action];
60
+ }), missingUtility = new HarnessError({ code: "memory.summary", message: "The summary strategy needs a UtilityLlm in the turn's services" });
61
+ export const memorySummaryPlugin = definePlugin({
62
+ id: "@xandreed/plugin-memory-summary",
63
+ version: "0.8.0-next.0",
64
+ scope: "runtime",
65
+ config: MemorySummaryConfig,
66
+ defaults: memorySummaryDefaults,
67
+ provides: [ConversationMemory],
68
+ layer: (config) => Layer.succeed(ConversationMemory, ConversationMemory.of({
69
+ strategy: SUMMARY_STRATEGY,
70
+ open: ({ runId, log }) => Effect.gen(function* () {
71
+ const utility = yield* Effect.serviceOption(UtilityLlm).pipe(Effect.flatMap(Option.match({ onNone: () => Effect.fail(missingUtility), onSome: Effect.succeed }))), policy = summaryPolicy(config, (prompt) => utility.complete(prompt).pipe(Effect.map((completion) => completion.text), Effect.mapError((error) => new HarnessError({ code: "memory.summary", message: error.message }))));
72
+ return yield* openLogSession(log, policy, { runId });
73
+ })
74
+ }))
75
+ }), MemorySummaryLive = memorySummaryPlugin.live;
76
+ export default memorySummaryPlugin;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@xandreed/plugin-memory-summary",
3
+ "version": "0.8.0-next.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "dependencies": {
13
+ "@xandreed/core": "0.8.0-next.0",
14
+ "effect": "4.0.0-rc.118"
15
+ },
16
+ "private": false,
17
+ "types": "./dist/index.d.ts",
18
+ "files": [
19
+ "dist",
20
+ "skills",
21
+ "profiles",
22
+ "README.md",
23
+ "LICENSE"
24
+ ],
25
+ "engines": {
26
+ "bun": ">=1.3.0"
27
+ },
28
+ "license": "MIT"
29
+ }