@columenlabs/lumen-ai 0.1.275 → 0.1.276

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -164,7 +164,7 @@ The panel's composer lists what can follow three keys as they are typed. The kit
164
164
 
165
165
  ### Bearings
166
166
 
167
- Each message the model gets starts with a few lines of bearings: the time now in the person's zone (the panel sends the browser's), UTC, how long since the chat's last message and the app's name. They go in the message, not the system prompt, so the prompt cache holds. A turn's lead (bearings, `plan.lead`, news) goes before the newest message only, so the chat never grows by it; a chat's OpenAI calls carry the chat's id as their cache key. `/compact` in the panel summarizes the chat's older part at once (as past `compactAt()`), without asking the model. An app adds its own lines with `bearings`; each call gets 300 ms, and when it is late or throws its lines are left out.
167
+ Each message the model gets starts with a few lines of bearings: the time now in the person's zone (the panel sends the browser's), UTC, how long since the chat's last message and the app's name. They go in the message, not the system prompt, so the prompt cache holds. A turn's lead (bearings, `plan.lead`, news) stays on the message it went with, so every earlier message is sent exactly as before and read from the prompt cache (a lead taken off the last message changed it, and that message and its tool steps were paid again: 0% read before, 97 to 99% after, on OpenAI). The newest lead says only what changed since the last (`stickyLeads`, `leadDelta`): one line when nothing did, the new and gone lines when some did, and the whole lead the first time, after compaction, or when the change is most of it. A chat's OpenAI calls carry the chat's id as their cache key. `/compact` in the panel summarizes the chat's older part at once (as past `compactAt()`), without asking the model. An app adds its own lines with `bearings`; each call gets 300 ms, and when it is late or throws its lines are left out.
168
168
 
169
169
  ```ts
170
170
  createAssistant({
@@ -132,6 +132,29 @@ export declare function cardNews(cards: AgentCard[]): string;
132
132
  export declare const COMPACT_COMMAND = "/compact";
133
133
  /** The messages with `lead` put before the newest message the person wrote. */
134
134
  export declare function withLead(messages: ModelMessage[], lead: string | undefined): ModelMessage[];
135
+ /** The leads a chat has sent, kept so each earlier message is sent again exactly as it was. */
136
+ export interface SentLeads {
137
+ /** Each person's message (`leadKey`) and the lead it went with. */
138
+ sent: Record<string, string>;
139
+ /** The last whole lead, which the next one is told against. */
140
+ last?: string;
141
+ }
142
+ /**
143
+ * The lead for the newest message, told against the last one: the whole lead the first time, a line
144
+ * when nothing changed, else only the lines that are new and the lines that went (the whole lead
145
+ * when that is not much shorter).
146
+ */
147
+ export declare function leadDelta(last: string | undefined, next: string): string;
148
+ /**
149
+ * The chat's messages with every lead where it was first sent, and the newest message's lead told
150
+ * against the last. A provider's prompt cache reuses only earlier messages sent exactly as before:
151
+ * a lead moved off the last message changed it, and that message and every step after it were
152
+ * paid again on the next turn. Unchanged context costs one line, changed context only what changed.
153
+ */
154
+ export declare function stickyLeads(messages: ModelMessage[], kept: SentLeads, lead: string): {
155
+ messages: ModelMessage[];
156
+ kept: SentLeads;
157
+ };
135
158
  /** One message the person sent, with the page, refs and tier the composer added. */
136
159
  export interface AskInput {
137
160
  message: string;
@@ -70,6 +70,68 @@ export function withLead(messages, lead) {
70
70
  i -= 1;
71
71
  return i < 0 ? messages : leadAt(messages, i, lead);
72
72
  }
73
+ /** Leads kept per chat; the oldest go first (a compacted chat no longer has their messages). */
74
+ const LEADS_KEPT = 60;
75
+ const LEAD_SAME = 'Context: as in my last message.\n\n';
76
+ const LEAD_CHANGED = 'Context: what changed since my last message (the rest is as it was).';
77
+ /** A person's message's place among theirs and a hash of its words: the same message turn after turn. */
78
+ function leadKey(n, m) {
79
+ const text = typeof m.content === 'string'
80
+ ? m.content
81
+ : m.content.map((p) => (p.type === 'text' ? p.text : p.type)).join('\n');
82
+ let h = 0x811c9dc5;
83
+ for (let i = 0; i < text.length; i++)
84
+ h = Math.imul(h ^ text.charCodeAt(i), 0x01000193);
85
+ return `${n}:${(h >>> 0).toString(36)}`;
86
+ }
87
+ /**
88
+ * The lead for the newest message, told against the last one: the whole lead the first time, a line
89
+ * when nothing changed, else only the lines that are new and the lines that went (the whole lead
90
+ * when that is not much shorter).
91
+ */
92
+ export function leadDelta(last, next) {
93
+ if (!next || last === undefined)
94
+ return next;
95
+ if (last === next)
96
+ return LEAD_SAME;
97
+ const was = new Set(last.split('\n'));
98
+ const is = new Set(next.split('\n'));
99
+ const added = next.split('\n').filter((l) => l.trim() && !was.has(l));
100
+ const gone = last.split('\n').filter((l) => l.trim() && !is.has(l));
101
+ const delta = `${LEAD_CHANGED}\n${added.length ? `Now:\n${added.join('\n')}\n` : ''}${gone.length ? `No longer:\n${gone.join('\n')}\n` : ''}\n`;
102
+ return delta.length < next.length * 0.6 ? delta : next;
103
+ }
104
+ /**
105
+ * The chat's messages with every lead where it was first sent, and the newest message's lead told
106
+ * against the last. A provider's prompt cache reuses only earlier messages sent exactly as before:
107
+ * a lead moved off the last message changed it, and that message and every step after it were
108
+ * paid again on the next turn. Unchanged context costs one line, changed context only what changed.
109
+ */
110
+ export function stickyLeads(messages, kept, lead) {
111
+ const users = messages.flatMap((m, i) => (m.role === 'user' ? [i] : []));
112
+ const newest = users.at(-1);
113
+ const sent = { ...kept.sent };
114
+ // Told against the last lead only when an earlier message still carries one (not after compaction).
115
+ let last = users.some((i, n) => i !== newest && sent[leadKey(n, messages[i])])
116
+ ? kept.last
117
+ : undefined;
118
+ let out = messages;
119
+ users.forEach((i, n) => {
120
+ const m = messages[i];
121
+ const key = leadKey(n, m);
122
+ if (i === newest && sent[key] === undefined && lead) {
123
+ sent[key] = leadDelta(last, lead);
124
+ last = lead;
125
+ }
126
+ const l = sent[key];
127
+ if (l)
128
+ out = leadAt(out, i, l);
129
+ });
130
+ const keys = Object.keys(sent);
131
+ for (const k of keys.slice(0, Math.max(0, keys.length - LEADS_KEPT)))
132
+ delete sent[k];
133
+ return { messages: out, kept: { sent, ...(last !== undefined && { last }) } };
134
+ }
73
135
  /** The message at `i` (one the person wrote) with `lead` put before what it says. */
74
136
  function leadAt(messages, i, lead) {
75
137
  const m = messages[i];
@@ -766,9 +828,11 @@ export class LumenAgent extends Think {
766
828
  this.#turn.lazy = lazy;
767
829
  }
768
830
  const turn = this.#turn;
769
- // The lead goes before the newest message only. Kept on every earlier message it grew the chat
770
- // by a whole lead (an app's page, often thousands of tokens) each turn.
771
- const cached = cachedTurn(system, withLead(ctx.messages, `${this.#news()}${this.#screenNews()}${plan.lead ?? ''}`));
831
+ // Each lead stays on the message it went with, so earlier messages are sent as before and read
832
+ // from the prompt cache; the newest says only what changed since the last (`stickyLeads`).
833
+ const led = stickyLeads(ctx.messages, this.#get('leads') ?? { sent: {} }, `${this.#news()}${this.#screenNews()}${plan.lead ?? ''}`);
834
+ this.#put('leads', led.kept);
835
+ const cached = cachedTurn(system, led.messages);
772
836
  return {
773
837
  model: this.#model(plan, () => {
774
838
  const fb = plan.fallback;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@columenlabs/lumen-ai",
3
- "version": "0.1.275",
3
+ "version": "0.1.276",
4
4
  "description": "Lumen's assistant and agent parts: a chat assistant's server (Claude tiers, tools over MCP, the web, hand off, spend caps), serving an MCP server from a Worker, read-only tool sets and metered tool calls.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -57,8 +57,8 @@
57
57
  "@anthropic-ai/sdk": "0.133.0",
58
58
  "@cloudflare/computer": "0.8.0",
59
59
  "@cloudflare/think": "0.23.0",
60
- "@columenlabs/lumen-ops": "0.1.275",
61
- "@columenlabs/lumen-ui-types": "0.1.275",
60
+ "@columenlabs/lumen-ops": "0.1.276",
61
+ "@columenlabs/lumen-ui-types": "0.1.276",
62
62
  "agents": "0.32.0",
63
63
  "ai": "7.0.162",
64
64
  "just-bash": "3.6.0",