@picassohq/cli 1.0.3 → 1.0.4
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/build-info.json +1 -1
- package/dist/index.js +1591 -1251
- package/package.json +1 -1
- package/runtime/engine-manifest.json +22 -11
- package/runtime/packages/picasso-compaction/cordis.patch.yml +4 -0
- package/runtime/packages/picasso-compaction/index.js +623 -0
- package/runtime/packages/picasso-compaction/package.json +27 -0
- package/runtime/packages/picasso-lean-context/argument-repair.js +422 -0
- package/runtime/packages/picasso-lean-context/index.js +23 -2
- package/runtime/packages/picasso-model-router/connections.js +49 -3
- package/runtime/packages/picasso-model-router/index.js +32 -1
- package/runtime/packages/picasso-model-router/package.json +3 -1
- package/runtime/packages/picasso-model-router/thinking-default.js +212 -0
- package/runtime/packages/picasso-web-search/backends.js +245 -195
- package/runtime/packages/picasso-web-search/config.js +75 -27
- package/runtime/packages/picasso-web-search/cordis.patch.yml +2 -1
- package/runtime/packages/picasso-web-search/failure.js +7 -1
- package/runtime/packages/picasso-web-search/index.js +7 -5
- package/runtime/packages/picasso-web-search/package.json +6 -0
- package/runtime/packages/picasso-web-search/requests.js +339 -0
- package/runtime/packages/picasso-web-search/router.js +17 -12
- package/runtime/plugins/picasso-compaction/index.js +623 -0
- package/runtime/plugins/picasso-compaction/package.json +27 -0
- package/runtime/plugins/picasso-lean-context/argument-repair.js +422 -0
- package/runtime/plugins/picasso-lean-context/index.js +23 -2
- package/runtime/plugins/picasso-model-router/connections.js +49 -3
- package/runtime/plugins/picasso-model-router/index.js +32 -1
- package/runtime/plugins/picasso-model-router/package.json +3 -1
- package/runtime/plugins/picasso-model-router/thinking-default.js +212 -0
- package/runtime/plugins/picasso-provider-models/index.js +236 -4
- package/runtime/plugins/picasso-web-search/backends.js +245 -195
- package/runtime/plugins/picasso-web-search/config.js +75 -27
- package/runtime/plugins/picasso-web-search/failure.js +7 -1
- package/runtime/plugins/picasso-web-search/index.js +7 -5
- package/runtime/plugins/picasso-web-search/package.json +6 -0
- package/runtime/plugins/picasso-web-search/requests.js +339 -0
- package/runtime/plugins/picasso-web-search/router.js +17 -12
- package/runtime/relay/main.mjs +2776 -1494
- package/runtime/relay/model-listing.mjs +337 -26
- package/runtime/relay/versions-routes.mjs +65 -3
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
// @picasso/compaction — conversation compaction that always summarizes on a model that works now.
|
|
2
|
+
//
|
|
3
|
+
// The engine's compaction (dsh-compaction-basic) summarizes on the model of the conversation's LAST request. When that
|
|
4
|
+
// model's connection is gone (a provider the person removed) every compaction fails — the switch to another model that
|
|
5
|
+
// needs one, and `/compact` too ("no adapter registered for provider …"); and a history longer than the summarizing
|
|
6
|
+
// model's window could never be summarized in its single call. This plugin replaces only the engine's documented
|
|
7
|
+
// summarizer hook (`summarize(input, agent, signal)`, "the sole subclass customization hook"); range selection,
|
|
8
|
+
// pricing, the checkpoint framing and the durable replacement stay the engine's.
|
|
9
|
+
//
|
|
10
|
+
// Which model summarizes, in order, each one only if it resolves now (its connection is registered):
|
|
11
|
+
// 1. the model the conversation is switching to / has selected (the pending selection);
|
|
12
|
+
// 2. the model of the last request;
|
|
13
|
+
// 3. the agent's configured route;
|
|
14
|
+
// 4. other connected models, Picasso's managed route first, then the largest context window (at most
|
|
15
|
+
// MAX_FALLBACK_ATTEMPTS of them are tried).
|
|
16
|
+
// A model whose call fails is followed by the next; when every one fails the error says so in plain words.
|
|
17
|
+
//
|
|
18
|
+
// When the history fits the model in one call, the call is the engine's own shape (system, tools, history, then the
|
|
19
|
+
// instruction), so a model that served the last request reuses its warm prompt cache; it IS the engine's call when the
|
|
20
|
+
// engine would have picked that model anyway. When it does not fit, the history is rendered as plain text, split into
|
|
21
|
+
// parts that fit, each part is summarized, and the part summaries are merged (in rounds, if even they do not fit).
|
|
22
|
+
//
|
|
23
|
+
// C1 zero-fallback: the summary is always the model's own text. Nothing is synthesized when no model can produce
|
|
24
|
+
// one; the compaction fails and the engine records the attempt. Tier 1 is untouched: the summary lands in history
|
|
25
|
+
// (Tier 2) through the engine's checkpoint message, exactly as before.
|
|
26
|
+
|
|
27
|
+
export const name = 'picasso-compaction';
|
|
28
|
+
export const dshTarget = '0.2.2-rc.3';
|
|
29
|
+
|
|
30
|
+
/** The managed route (`packages/core/src/dsh/managed-transport.ts` MANAGED_DSH_PROVIDER). */
|
|
31
|
+
export const MANAGED_ROUTE = 'picasso-managed';
|
|
32
|
+
/** Window assumed for a model whose connection states none (conservative: it only makes parts smaller). */
|
|
33
|
+
export const FALLBACK_CONTEXT_WINDOW = 65_536;
|
|
34
|
+
/** Output cap of one summary call; a checkpoint is a few thousand tokens. */
|
|
35
|
+
export const SUMMARY_MAX_TOKENS = 16_384;
|
|
36
|
+
/** The engine's own summary cap when its config names none (`headroomTokens` default). */
|
|
37
|
+
export const ENGINE_DEFAULT_MAX_TOKENS = 65_536;
|
|
38
|
+
/** Other connected models tried after the selected, last-used and configured ones. */
|
|
39
|
+
export const MAX_FALLBACK_ATTEMPTS = 2;
|
|
40
|
+
/** Merge rounds before giving up on a history whose part summaries still do not fit. */
|
|
41
|
+
export const MAX_MERGE_ROUNDS = 5;
|
|
42
|
+
/** Characters per token for sizing; lower than the engine's 4 so code and JSON never overflow a part. */
|
|
43
|
+
const CHARS_PER_TOKEN = 3;
|
|
44
|
+
/** Share of the input room actually filled (tokenizers differ from any estimate). */
|
|
45
|
+
const FILL_RATIO = 0.9;
|
|
46
|
+
/** Tokens kept for the instruction and framing around a part. */
|
|
47
|
+
const PROMPT_OVERHEAD_TOKENS = 2_000;
|
|
48
|
+
/** Halvings of the part size after a route refuses a request as too large. */
|
|
49
|
+
const MAX_SHRINKS = 4;
|
|
50
|
+
const MIN_PART_TOKENS = 2_000;
|
|
51
|
+
const MAX_PROVIDERS_SCANNED = 8;
|
|
52
|
+
const MAX_MODELS_PER_PROVIDER = 6;
|
|
53
|
+
const ORIGINAL = Symbol.for('cordis.original');
|
|
54
|
+
|
|
55
|
+
const SECTIONS = [
|
|
56
|
+
'## Primary Request and Intent',
|
|
57
|
+
"- [the user's original and evolving goals; quote verbatim where the exact wording matters]",
|
|
58
|
+
'',
|
|
59
|
+
'## Key Technical Concepts',
|
|
60
|
+
'- [technologies, frameworks, patterns, and conventions in play]',
|
|
61
|
+
'',
|
|
62
|
+
'## Files and Code',
|
|
63
|
+
'- [exact path: why it matters, key changes or snippets]',
|
|
64
|
+
'',
|
|
65
|
+
'## Errors and Fixes',
|
|
66
|
+
'- [error: how it was resolved, plus any related user feedback]',
|
|
67
|
+
'',
|
|
68
|
+
'## Pending Jobs',
|
|
69
|
+
'- [explicitly requested work not yet completed]',
|
|
70
|
+
'',
|
|
71
|
+
'## Current Work',
|
|
72
|
+
'- [precisely what was in progress at this checkpoint]',
|
|
73
|
+
'',
|
|
74
|
+
'## Next Step',
|
|
75
|
+
'- [the single next action, directly in line with the most recent request, or "(none)"]',
|
|
76
|
+
'',
|
|
77
|
+
'## Critical Context',
|
|
78
|
+
'- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]',
|
|
79
|
+
].join('\n');
|
|
80
|
+
|
|
81
|
+
const RULES = [
|
|
82
|
+
'Rules:',
|
|
83
|
+
'- Output EXACTLY the Markdown structure above: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section.',
|
|
84
|
+
'- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.',
|
|
85
|
+
'- Capture user feedback and explicit instructions faithfully, especially corrections.',
|
|
86
|
+
'- Never copy a secret value (API key, token, password, private key, connection string with credentials) into the checkpoint: name it and where it lives instead (for example "the OpenAI key in .env").',
|
|
87
|
+
'- Do NOT mention this summarization request or that the context was compacted.',
|
|
88
|
+
'- Output only the checkpoint text: do not call any tool or take any other action.',
|
|
89
|
+
'- A <compacted-summary> block in the material is an earlier checkpoint: keep its still-true facts, drop stale ones, and merge newer information into one consolidated summary.',
|
|
90
|
+
].join('\n');
|
|
91
|
+
|
|
92
|
+
/** The instruction after the replayed history (single call: the engine's request shape). */
|
|
93
|
+
export const SUMMARY_INSTRUCTION = [
|
|
94
|
+
'You are now acting as a compaction engine for this AI coding assistant. Condense the conversation ABOVE into a structured checkpoint that lets another model resume the work with no loss of essential context.',
|
|
95
|
+
'',
|
|
96
|
+
SECTIONS,
|
|
97
|
+
'',
|
|
98
|
+
RULES,
|
|
99
|
+
].join('\n');
|
|
100
|
+
|
|
101
|
+
/** One part of a history too long for one call. */
|
|
102
|
+
export function partPrompt(text, index, count) {
|
|
103
|
+
return [
|
|
104
|
+
`<transcript part="${index + 1} of ${count}">`,
|
|
105
|
+
text,
|
|
106
|
+
'</transcript>',
|
|
107
|
+
'',
|
|
108
|
+
`You are now acting as a compaction engine for an AI coding assistant. The transcript above is part ${index + 1} of ${count} of one earlier conversation between a person and the assistant (oldest part first). Condense THIS PART into a structured checkpoint; later steps merge the parts, so record what this part establishes even when it continues work from an earlier part.`,
|
|
109
|
+
'',
|
|
110
|
+
SECTIONS,
|
|
111
|
+
'',
|
|
112
|
+
RULES,
|
|
113
|
+
].join('\n');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Merge the checkpoints of consecutive parts into one. */
|
|
117
|
+
export function mergePrompt(summaries) {
|
|
118
|
+
return [
|
|
119
|
+
...summaries.flatMap((summary, index) => [`<checkpoint part="${index + 1} of ${summaries.length}">`, summary, '</checkpoint>', '']),
|
|
120
|
+
`You are now acting as a compaction engine for an AI coding assistant. The ${summaries.length} checkpoints above summarize consecutive parts of ONE conversation, oldest first. Merge them into a single checkpoint: later parts override earlier ones where they disagree, "Current Work" and "Next Step" come from the latest part, and nothing still true is lost.`,
|
|
121
|
+
'',
|
|
122
|
+
SECTIONS,
|
|
123
|
+
'',
|
|
124
|
+
RULES,
|
|
125
|
+
].join('\n');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A compaction failure in plain words (the engine records `message` on the compaction and shows it). */
|
|
129
|
+
export class CompactionSummaryError extends Error {
|
|
130
|
+
constructor(message, attempts = []) {
|
|
131
|
+
super(message);
|
|
132
|
+
this.name = 'CompactionSummaryError';
|
|
133
|
+
this.code = 'PICASSO_COMPACTION_SUMMARY';
|
|
134
|
+
this.attempts = attempts;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const text = (value) => (typeof value === 'string' ? value : '');
|
|
139
|
+
const firstLine = (value) => {
|
|
140
|
+
const raw = value instanceof Error ? value.message : String(value ?? '');
|
|
141
|
+
const line = raw.split('\n').find((part) => part.trim() !== '') ?? raw;
|
|
142
|
+
return line.length > 200 ? `${line.slice(0, 199)}…` : line;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export function estimateTextTokens(value) {
|
|
146
|
+
return Math.ceil(text(value).length / CHARS_PER_TOKEN) + 4;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function estimateBlocks(blocks) {
|
|
150
|
+
let tokens = 4;
|
|
151
|
+
for (const block of Array.isArray(blocks) ? blocks : []) {
|
|
152
|
+
if (block?.type === 'text' || block?.type === 'reasoning') tokens += estimateTextTokens(block.text);
|
|
153
|
+
else if (block?.type === 'tool-call') tokens += estimateTextTokens(`${text(block.name)} ${text(block.arguments)}`);
|
|
154
|
+
else tokens += estimateTextTokens(safeJson(block));
|
|
155
|
+
}
|
|
156
|
+
return tokens;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function safeJson(value) {
|
|
160
|
+
try {
|
|
161
|
+
return JSON.stringify(value) ?? '';
|
|
162
|
+
} catch {
|
|
163
|
+
return '';
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Tokens the engine's request shape costs: every message plus the tool schemas. */
|
|
168
|
+
export function estimateInputTokens(input) {
|
|
169
|
+
let tokens = 0;
|
|
170
|
+
for (const message of input?.messages ?? []) tokens += estimateBlocks(message?.content);
|
|
171
|
+
if (Array.isArray(input?.tools) && input.tools.length > 0) tokens += estimateTextTokens(safeJson(input.tools));
|
|
172
|
+
return tokens;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Output cap and input room of one call on a model with `window` tokens. */
|
|
176
|
+
export function callBudget(window, defaultMaxTokens) {
|
|
177
|
+
const capped = Math.min(SUMMARY_MAX_TOKENS, Math.floor(window / 4), defaultMaxTokens ?? Number.POSITIVE_INFINITY);
|
|
178
|
+
const maxTokens = Math.max(1_024, capped);
|
|
179
|
+
const inputTokens = Math.max(1_024, Math.floor((window - maxTokens) * FILL_RATIO) - PROMPT_OVERHEAD_TOKENS);
|
|
180
|
+
return { maxTokens, inputTokens };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function inputRoom(window, maxTokens) {
|
|
184
|
+
return Math.floor((window - maxTokens) * FILL_RATIO) - PROMPT_OVERHEAD_TOKENS;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const ROLE_LABEL = { user: 'User', assistant: 'Assistant', tool: 'Tool result' };
|
|
188
|
+
|
|
189
|
+
/** One history message as transcript text; '' for messages a summary does not need (system prompt, tool changes). */
|
|
190
|
+
export function renderMessage(message) {
|
|
191
|
+
const label = ROLE_LABEL[message?.role];
|
|
192
|
+
if (label === undefined) return '';
|
|
193
|
+
const parts = [];
|
|
194
|
+
for (const block of Array.isArray(message.content) ? message.content : []) {
|
|
195
|
+
switch (block?.type) {
|
|
196
|
+
case 'text':
|
|
197
|
+
if (text(block.text).length > 0) parts.push(block.text);
|
|
198
|
+
break;
|
|
199
|
+
case 'reasoning':
|
|
200
|
+
case 'tool-addition':
|
|
201
|
+
case 'tool-removal':
|
|
202
|
+
break;
|
|
203
|
+
case 'tool-call':
|
|
204
|
+
parts.push(`[tool call ${text(block.name)} ${text(block.arguments)}]`);
|
|
205
|
+
break;
|
|
206
|
+
case 'image':
|
|
207
|
+
parts.push('[image]');
|
|
208
|
+
break;
|
|
209
|
+
case 'file':
|
|
210
|
+
parts.push(`[file ${text(block.attachment?.name) || text(block.attachment?.path)}]`.trim());
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
parts.push(safeJson(block));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (parts.length === 0) return '';
|
|
217
|
+
const heading = message.role === 'tool' && message.isError === true ? `${label} (error)` : label;
|
|
218
|
+
return `### ${heading}\n${parts.join('\n')}`;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* The history as transcript parts of at most `partTokens` estimated tokens each, in order. A message larger than a
|
|
223
|
+
* part is split across parts (marked as continued); nothing is dropped.
|
|
224
|
+
*/
|
|
225
|
+
export function transcriptParts(messages, partTokens) {
|
|
226
|
+
const maxChars = Math.max(1_000, (partTokens - 4) * CHARS_PER_TOKEN);
|
|
227
|
+
const parts = [];
|
|
228
|
+
let current = '';
|
|
229
|
+
const flush = () => {
|
|
230
|
+
if (current.length > 0) parts.push(current);
|
|
231
|
+
current = '';
|
|
232
|
+
};
|
|
233
|
+
for (const message of messages ?? []) {
|
|
234
|
+
const rendered = renderMessage(message);
|
|
235
|
+
if (rendered.length === 0) continue;
|
|
236
|
+
const joined = current.length === 0 ? rendered : `${current}\n\n${rendered}`;
|
|
237
|
+
if (joined.length <= maxChars) {
|
|
238
|
+
current = joined;
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
flush();
|
|
242
|
+
if (rendered.length <= maxChars) {
|
|
243
|
+
current = rendered;
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
for (let start = 0; start < rendered.length; start += maxChars) {
|
|
247
|
+
const piece = rendered.slice(start, start + maxChars);
|
|
248
|
+
parts.push(start === 0 ? piece : `(continued)\n${piece}`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
flush();
|
|
252
|
+
return parts;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Group checkpoint texts so each group's merge prompt fits `inputTokens`. */
|
|
256
|
+
export function packSummaries(summaries, inputTokens) {
|
|
257
|
+
const groups = [];
|
|
258
|
+
let current = [];
|
|
259
|
+
let size = 0;
|
|
260
|
+
for (const summary of summaries) {
|
|
261
|
+
const cost = estimateTextTokens(summary) + 16;
|
|
262
|
+
if (current.length > 0 && size + cost > inputTokens) {
|
|
263
|
+
groups.push(current);
|
|
264
|
+
current = [];
|
|
265
|
+
size = 0;
|
|
266
|
+
}
|
|
267
|
+
current.push(summary);
|
|
268
|
+
size += cost;
|
|
269
|
+
}
|
|
270
|
+
if (current.length > 0) groups.push(current);
|
|
271
|
+
return groups;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function addUsage(total, usage) {
|
|
275
|
+
if (usage === undefined || usage === null || typeof usage !== 'object') return total;
|
|
276
|
+
const sum = { ...(total ?? { inputTokens: 0, outputTokens: 0 }) };
|
|
277
|
+
for (const key of ['inputTokens', 'outputTokens', 'totalTokens', 'cacheReadTokens', 'cacheWriteTokens', 'reasoningTokens']) {
|
|
278
|
+
const value = usage[key];
|
|
279
|
+
if (typeof value === 'number' && Number.isFinite(value)) sum[key] = (sum[key] ?? 0) + value;
|
|
280
|
+
}
|
|
281
|
+
return sum;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** One model call's output: content blocks in index order, the finish reason and the usage. */
|
|
285
|
+
async function collect(stream) {
|
|
286
|
+
const ended = new Map();
|
|
287
|
+
const deltas = new Map();
|
|
288
|
+
let finish;
|
|
289
|
+
let usage;
|
|
290
|
+
for await (const chunk of stream) {
|
|
291
|
+
switch (chunk?.type) {
|
|
292
|
+
case 'text-delta':
|
|
293
|
+
deltas.set(chunk.index, (deltas.get(chunk.index) ?? '') + text(chunk.text));
|
|
294
|
+
break;
|
|
295
|
+
case 'block-end':
|
|
296
|
+
ended.set(chunk.index, chunk.block);
|
|
297
|
+
break;
|
|
298
|
+
case 'usage':
|
|
299
|
+
usage = chunk.usage;
|
|
300
|
+
break;
|
|
301
|
+
case 'finish':
|
|
302
|
+
finish = chunk.reason;
|
|
303
|
+
break;
|
|
304
|
+
default:
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
const indices = [...new Set([...ended.keys(), ...deltas.keys()])].sort((a, b) => a - b);
|
|
309
|
+
const blocks = indices.map((index) => ended.get(index) ?? { type: 'text', text: deltas.get(index) ?? '' });
|
|
310
|
+
return { blocks, finish, usage };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function finishFailure(finish) {
|
|
314
|
+
if (finish === undefined) return undefined;
|
|
315
|
+
if (finish.kind === 'error' || finish.kind === 'aborted') {
|
|
316
|
+
const failure = finish.failure ?? {};
|
|
317
|
+
const error = new Error(text(failure.message) || `the model stopped (${finish.kind})`);
|
|
318
|
+
if (typeof failure.code === 'string') error.code = failure.code;
|
|
319
|
+
return error;
|
|
320
|
+
}
|
|
321
|
+
if (finish.kind === 'max-tokens') {
|
|
322
|
+
const error = new Error('the summary was cut off at its length limit');
|
|
323
|
+
error.code = 'MAX_TOKENS';
|
|
324
|
+
return error;
|
|
325
|
+
}
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** A route refused the request for its size (HTTP 413, "payload too large", a context overflow). */
|
|
330
|
+
export function isTooLarge(error) {
|
|
331
|
+
const status = error?.details?.status ?? error?.status ?? error?.cause?.details?.status;
|
|
332
|
+
if (status === 413) return true;
|
|
333
|
+
// The model said the request overflowed its window: the size estimate was off, so smaller parts are the answer too.
|
|
334
|
+
if (error?.code === 'CONTEXT_WINDOW_EXCEEDED') return true;
|
|
335
|
+
const message = error instanceof Error ? error.message : String(error ?? '');
|
|
336
|
+
return /\b413\b|payload too large|request(?:_| )too(?:_| )large|body is too large|entity too large/iu.test(message);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const sameTarget = (a, b) => a !== undefined && b !== undefined && a.provider === b.provider && a.model === b.model;
|
|
340
|
+
|
|
341
|
+
function target(provider, model) {
|
|
342
|
+
return typeof provider === 'string' && provider.length > 0 && typeof model === 'string' && model.length > 0 ? { provider, model } : undefined;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** The pending model selection (what the conversation is switching to), else the last used one. */
|
|
346
|
+
function selectedTarget(agent) {
|
|
347
|
+
try {
|
|
348
|
+
const current = agent?.model?.current?.();
|
|
349
|
+
return target(current?.provider, current?.model);
|
|
350
|
+
} catch {
|
|
351
|
+
return undefined;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function lastRequestTarget(agent) {
|
|
356
|
+
try {
|
|
357
|
+
const config = agent?.session?.requestHeader?.()?.config;
|
|
358
|
+
return target(config?.provider, config?.model);
|
|
359
|
+
} catch {
|
|
360
|
+
return undefined;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function optionsTarget(agent) {
|
|
365
|
+
return target(agent?.options?.provider, agent?.options?.model);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* The summarizer. `llm` is the engine's LLM service (`resolveModelInfo`, `stream`, `listProviders`, `listModels`);
|
|
370
|
+
* `stock` is the engine's own summarize for the case it would already handle; `engineConfig` its resolved config.
|
|
371
|
+
*/
|
|
372
|
+
export function createSummarizer({ llm, stock, engineConfig = () => undefined, log = () => undefined }) {
|
|
373
|
+
const resolve = async (candidate, origin, signal) => {
|
|
374
|
+
try {
|
|
375
|
+
const info = await llm.resolveModelInfo(candidate.provider, candidate.model, signal);
|
|
376
|
+
const window = info?.context?.contextWindow;
|
|
377
|
+
return {
|
|
378
|
+
...candidate,
|
|
379
|
+
origin,
|
|
380
|
+
window: Number.isSafeInteger(window) && window > 0 ? window : FALLBACK_CONTEXT_WINDOW,
|
|
381
|
+
...(Number.isSafeInteger(info?.defaultMaxTokens) && info.defaultMaxTokens > 0 ? { defaultMaxTokens: info.defaultMaxTokens } : {}),
|
|
382
|
+
};
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (signal?.aborted) throw error;
|
|
385
|
+
// Its connection is gone or does not serve that model: never summarize on it.
|
|
386
|
+
log(`compaction: ${candidate.provider}/${candidate.model} is not available (${firstLine(error)})`);
|
|
387
|
+
return undefined;
|
|
388
|
+
}
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
/** Other connected models: the managed route first, then the largest window. */
|
|
392
|
+
const fallbacks = async (seen, signal) => {
|
|
393
|
+
let providers = [];
|
|
394
|
+
try {
|
|
395
|
+
providers = (await llm.listProviders?.()) ?? [];
|
|
396
|
+
} catch {
|
|
397
|
+
providers = [];
|
|
398
|
+
}
|
|
399
|
+
const ids = [...new Set(providers.map((entry) => (typeof entry === 'string' ? entry : entry?.id)).filter((id) => typeof id === 'string' && id.length > 0))];
|
|
400
|
+
ids.sort((a, b) => (a === MANAGED_ROUTE ? -1 : b === MANAGED_ROUTE ? 1 : 0));
|
|
401
|
+
const found = [];
|
|
402
|
+
for (const provider of ids.slice(0, MAX_PROVIDERS_SCANNED)) {
|
|
403
|
+
let models = [];
|
|
404
|
+
try {
|
|
405
|
+
models = (await llm.listModels?.(provider)) ?? [];
|
|
406
|
+
} catch {
|
|
407
|
+
models = [];
|
|
408
|
+
}
|
|
409
|
+
for (const model of models.slice(0, MAX_MODELS_PER_PROVIDER)) {
|
|
410
|
+
const candidate = target(provider, typeof model === 'string' ? model : model?.id);
|
|
411
|
+
if (candidate === undefined || seen.has(`${candidate.provider}\u0000${candidate.model}`)) continue;
|
|
412
|
+
seen.add(`${candidate.provider}\u0000${candidate.model}`);
|
|
413
|
+
const resolved = await resolve(candidate, 'fallback', signal);
|
|
414
|
+
if (resolved !== undefined) found.push(resolved);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
found.sort((a, b) => (a.provider === MANAGED_ROUTE) !== (b.provider === MANAGED_ROUTE) ? (a.provider === MANAGED_ROUTE ? -1 : 1) : b.window - a.window);
|
|
418
|
+
return found.slice(0, MAX_FALLBACK_ATTEMPTS);
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
/** Candidates in order, resolved lazily: a later one is only looked up when every earlier one failed. */
|
|
422
|
+
async function* candidates(agent, signal) {
|
|
423
|
+
const seen = new Set();
|
|
424
|
+
for (const [candidate, origin] of [[selectedTarget(agent), 'selection'], [lastRequestTarget(agent), 'last-request'], [optionsTarget(agent), 'configured']]) {
|
|
425
|
+
if (candidate === undefined) continue;
|
|
426
|
+
const key = `${candidate.provider}\u0000${candidate.model}`;
|
|
427
|
+
if (seen.has(key)) continue;
|
|
428
|
+
seen.add(key);
|
|
429
|
+
const resolved = await resolve(candidate, origin, signal);
|
|
430
|
+
if (resolved !== undefined) yield resolved;
|
|
431
|
+
}
|
|
432
|
+
yield* await fallbacks(seen, signal);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** The model the engine's own summarizer would call (configured, else the last request, else the agent's route). */
|
|
436
|
+
const stockTarget = (agent) => {
|
|
437
|
+
const config = engineConfig();
|
|
438
|
+
const configured = target(config?.summarizationProvider, config?.summarizationModel);
|
|
439
|
+
return configured ?? lastRequestTarget(agent) ?? optionsTarget(agent);
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const call = async (candidate, request, agent, signal) => {
|
|
443
|
+
const options = {
|
|
444
|
+
provider: candidate.provider,
|
|
445
|
+
model: candidate.model,
|
|
446
|
+
...request,
|
|
447
|
+
...(agent?.session?.id === undefined ? {} : { sessionId: agent.session.id }),
|
|
448
|
+
purpose: 'compaction',
|
|
449
|
+
...(signal === undefined ? {} : { signal }),
|
|
450
|
+
};
|
|
451
|
+
const { blocks, finish, usage } = await collect(llm.stream(options));
|
|
452
|
+
const failure = finishFailure(finish);
|
|
453
|
+
if (failure !== undefined) throw failure;
|
|
454
|
+
if (blocks.some((block) => block?.type === 'image')) throw new Error('the model answered with an image instead of a summary');
|
|
455
|
+
const summary = blocks.filter((block) => block?.type === 'text' && text(block.text).trim().length > 0);
|
|
456
|
+
if (summary.length === 0) {
|
|
457
|
+
const called = blocks.some((block) => block?.type === 'tool-call');
|
|
458
|
+
throw new Error(called ? 'the model called a tool instead of writing a summary' : 'the model returned an empty summary');
|
|
459
|
+
}
|
|
460
|
+
return { blocks, summary, text: summary.map((block) => block.text).join('\n'), usage };
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
const singleCall = async (candidate, input, agent, signal, maxTokens) => {
|
|
464
|
+
let toolHistory;
|
|
465
|
+
try {
|
|
466
|
+
toolHistory = agent?.session?.toolHistory?.();
|
|
467
|
+
} catch {
|
|
468
|
+
toolHistory = undefined;
|
|
469
|
+
}
|
|
470
|
+
const out = await call(candidate, {
|
|
471
|
+
messages: [...(input.messages ?? []), { role: 'user', content: [{ type: 'text', text: SUMMARY_INSTRUCTION }] }],
|
|
472
|
+
...(Array.isArray(input.tools) && input.tools.length > 0 ? { tools: [...input.tools] } : {}),
|
|
473
|
+
...(toolHistory === undefined ? {} : { toolHistory }),
|
|
474
|
+
maxTokens,
|
|
475
|
+
}, agent, signal);
|
|
476
|
+
return {
|
|
477
|
+
summary: out.summary.map((block) => ({ type: 'text', text: block.text })),
|
|
478
|
+
rawOutput: out.blocks,
|
|
479
|
+
llmStreamCall: true,
|
|
480
|
+
provider: candidate.provider,
|
|
481
|
+
model: candidate.model,
|
|
482
|
+
maxTokens,
|
|
483
|
+
...(out.usage === undefined ? {} : { usage: out.usage }),
|
|
484
|
+
};
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const textCall = async (candidate, prompt, agent, signal, maxTokens) =>
|
|
488
|
+
call(candidate, { messages: [{ role: 'user', content: [{ type: 'text', text: prompt }] }], maxTokens }, agent, signal);
|
|
489
|
+
|
|
490
|
+
const chunked = async (candidate, input, agent, signal, budget) => {
|
|
491
|
+
let usage;
|
|
492
|
+
const parts = transcriptParts(input.messages, budget.inputTokens);
|
|
493
|
+
if (parts.length === 0) throw new Error('there is no conversation text to summarize');
|
|
494
|
+
let summaries = [];
|
|
495
|
+
for (const [index, part] of parts.entries()) {
|
|
496
|
+
signal?.throwIfAborted?.();
|
|
497
|
+
const out = await textCall(candidate, partPrompt(part, index, parts.length), agent, signal, budget.maxTokens);
|
|
498
|
+
usage = addUsage(usage, out.usage);
|
|
499
|
+
summaries.push(out.text);
|
|
500
|
+
}
|
|
501
|
+
for (let round = 0; summaries.length > 1; round += 1) {
|
|
502
|
+
if (round >= MAX_MERGE_ROUNDS) throw new Error('the part summaries could not be merged into one');
|
|
503
|
+
const groups = packSummaries(summaries, budget.inputTokens);
|
|
504
|
+
if (groups.length === summaries.length && summaries.length > 1 && groups.every((group) => group.length === 1)) {
|
|
505
|
+
throw new Error('the part summaries are each too long to merge on this model');
|
|
506
|
+
}
|
|
507
|
+
const merged = [];
|
|
508
|
+
for (const group of groups) {
|
|
509
|
+
signal?.throwIfAborted?.();
|
|
510
|
+
if (group.length === 1) {
|
|
511
|
+
merged.push(group[0]);
|
|
512
|
+
continue;
|
|
513
|
+
}
|
|
514
|
+
const out = await textCall(candidate, mergePrompt(group), agent, signal, budget.maxTokens);
|
|
515
|
+
usage = addUsage(usage, out.usage);
|
|
516
|
+
merged.push(out.text);
|
|
517
|
+
}
|
|
518
|
+
summaries = merged;
|
|
519
|
+
}
|
|
520
|
+
log(`compaction: summarized ${parts.length} parts on ${candidate.provider}/${candidate.model}`);
|
|
521
|
+
return {
|
|
522
|
+
summary: [{ type: 'text', text: summaries[0] }],
|
|
523
|
+
provider: candidate.provider,
|
|
524
|
+
model: candidate.model,
|
|
525
|
+
maxTokens: budget.maxTokens,
|
|
526
|
+
...(usage === undefined ? {} : { usage }),
|
|
527
|
+
};
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Summarize on one model: the engine's call when it would make it and it fits, one call when it fits, else parts.
|
|
532
|
+
* A request the route refuses as too large (an HTTP 413: Picasso's managed API caps one request's size below the
|
|
533
|
+
* model's window) is retried on the same model in parts half the size, a few times.
|
|
534
|
+
*/
|
|
535
|
+
const summarizeOn = async (candidate, input, agent, signal) => {
|
|
536
|
+
const needed = estimateInputTokens(input);
|
|
537
|
+
const engineMax = engineConfig()?.maxTokens ?? ENGINE_DEFAULT_MAX_TOKENS;
|
|
538
|
+
const budget = callBudget(candidate.window, candidate.defaultMaxTokens);
|
|
539
|
+
let partTokens = budget.inputTokens;
|
|
540
|
+
let split = needed > budget.inputTokens;
|
|
541
|
+
for (let shrink = 0; ; shrink += 1) {
|
|
542
|
+
try {
|
|
543
|
+
if (!split) {
|
|
544
|
+
if (stock !== undefined && sameTarget(candidate, stockTarget(agent)) && needed <= inputRoom(candidate.window, engineMax)) {
|
|
545
|
+
return await stock(input, agent, signal);
|
|
546
|
+
}
|
|
547
|
+
return await singleCall(candidate, input, agent, signal, budget.maxTokens);
|
|
548
|
+
}
|
|
549
|
+
return await chunked(candidate, input, agent, signal, { ...budget, inputTokens: partTokens });
|
|
550
|
+
} catch (error) {
|
|
551
|
+
if (signal?.aborted || !isTooLarge(error) || shrink >= MAX_SHRINKS) throw error;
|
|
552
|
+
partTokens = Math.max(MIN_PART_TOKENS, Math.floor(Math.min(partTokens, needed) / 2));
|
|
553
|
+
split = true;
|
|
554
|
+
log(`compaction: ${candidate.provider}/${candidate.model} refused a request as too large; summarizing in parts of ~${partTokens} tokens`);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
return {
|
|
560
|
+
candidates,
|
|
561
|
+
async summarize(input, agent, signal) {
|
|
562
|
+
const attempts = [];
|
|
563
|
+
for await (const candidate of candidates(agent, signal)) {
|
|
564
|
+
signal?.throwIfAborted?.();
|
|
565
|
+
try {
|
|
566
|
+
return await summarizeOn(candidate, input, agent, signal);
|
|
567
|
+
} catch (error) {
|
|
568
|
+
if (signal?.aborted) throw error;
|
|
569
|
+
attempts.push({ provider: candidate.provider, model: candidate.model, reason: firstLine(error) });
|
|
570
|
+
log(`compaction: summary on ${candidate.provider}/${candidate.model} failed (${firstLine(error)})`);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
if (attempts.length === 0) {
|
|
574
|
+
throw new CompactionSummaryError("Couldn't shorten this conversation: none of your models is available to summarize it", attempts);
|
|
575
|
+
}
|
|
576
|
+
const tried = attempts.map((attempt) => `${attempt.model}: ${attempt.reason}`).join('; ');
|
|
577
|
+
throw new CompactionSummaryError(`Couldn't shorten this conversation: no model could summarize it (${tried})`, attempts);
|
|
578
|
+
},
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** The service object behind cordis's tracing proxy (its own `summarize` must be set on the real instance). */
|
|
583
|
+
function rawService(service) {
|
|
584
|
+
if (service === undefined || service === null) return undefined;
|
|
585
|
+
try {
|
|
586
|
+
return service[ORIGINAL] ?? service;
|
|
587
|
+
} catch {
|
|
588
|
+
return service;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** Installs the summarizer on the engine's compaction backend; returns the undo. */
|
|
593
|
+
export function installSummarizer(compaction, llm, log) {
|
|
594
|
+
const engine = rawService(compaction);
|
|
595
|
+
if (engine === undefined || typeof engine.summarize !== 'function') return () => undefined;
|
|
596
|
+
const stockSummarize = engine.summarize;
|
|
597
|
+
const summarizer = createSummarizer({
|
|
598
|
+
llm,
|
|
599
|
+
stock: (input, agent, signal) => stockSummarize.call(engine, input, agent, signal),
|
|
600
|
+
engineConfig: () => engine.config,
|
|
601
|
+
log,
|
|
602
|
+
});
|
|
603
|
+
const override = function summarize(input, agent, signal) {
|
|
604
|
+
return summarizer.summarize(input, agent, signal);
|
|
605
|
+
};
|
|
606
|
+
engine.summarize = override;
|
|
607
|
+
return () => {
|
|
608
|
+
if (engine.summarize === override) delete engine.summarize;
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
export function apply(ctx) {
|
|
613
|
+
ctx.inject?.(['compaction', 'llm'], (inner) => {
|
|
614
|
+
const log = (message) => {
|
|
615
|
+
try {
|
|
616
|
+
inner.logger?.info?.(message);
|
|
617
|
+
} catch {
|
|
618
|
+
// logging never fails a compaction
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
return installSummarizer(inner.compaction, inner.llm, log);
|
|
622
|
+
});
|
|
623
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@picasso/compaction",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "dsh cordis plugin: conversation compaction always summarizes on a model that works now (the model being switched to, else the last one used, else the best other connected model, managed first) and splits a history too long for that model into parts it summarizes and then merges. Never fabricates a summary. Zero runtime dependencies.",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js",
|
|
10
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"dshTarget": "0.2.2-rc.3",
|
|
14
|
+
"dsh": {
|
|
15
|
+
"bundle": {
|
|
16
|
+
"patch": "./cordis.patch.yml"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --import ../../test/setup/hermetic-preload.mjs --test test/*.test.mjs"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"index.js",
|
|
24
|
+
"cordis.patch.yml"
|
|
25
|
+
],
|
|
26
|
+
"license": "UNLICENSED"
|
|
27
|
+
}
|