@lienat/pi-jev-compaction 0.1.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 +21 -0
- package/README.en.md +215 -0
- package/README.md +171 -0
- package/assets/tamarajtran-jev-compaction.gif +0 -0
- package/assets/tamarajtran-jev-compaction.mp4 +0 -0
- package/extensions/fast-jev-compaction.ts +94 -0
- package/node_modules/fast-jev-compaction/LICENSE +21 -0
- package/node_modules/fast-jev-compaction/README.md +276 -0
- package/node_modules/fast-jev-compaction/cli/jev-find.mjs +103 -0
- package/node_modules/fast-jev-compaction/cli/jev-qa.mjs +159 -0
- package/node_modules/fast-jev-compaction/dist/client.d.ts +21 -0
- package/node_modules/fast-jev-compaction/dist/client.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/client.js +26 -0
- package/node_modules/fast-jev-compaction/dist/client.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/compact.d.ts +30 -0
- package/node_modules/fast-jev-compaction/dist/compact.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/compact.js +234 -0
- package/node_modules/fast-jev-compaction/dist/compact.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/index.d.ts +7 -0
- package/node_modules/fast-jev-compaction/dist/index.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/index.js +7 -0
- package/node_modules/fast-jev-compaction/dist/index.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/messages.d.ts +6 -0
- package/node_modules/fast-jev-compaction/dist/messages.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/messages.js +7 -0
- package/node_modules/fast-jev-compaction/dist/messages.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/request.d.ts +20 -0
- package/node_modules/fast-jev-compaction/dist/request.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/request.js +51 -0
- package/node_modules/fast-jev-compaction/dist/request.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/state.d.ts +29 -0
- package/node_modules/fast-jev-compaction/dist/state.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/state.js +256 -0
- package/node_modules/fast-jev-compaction/dist/state.js.map +1 -0
- package/node_modules/fast-jev-compaction/dist/types.d.ts +178 -0
- package/node_modules/fast-jev-compaction/dist/types.d.ts.map +1 -0
- package/node_modules/fast-jev-compaction/dist/types.js +2 -0
- package/node_modules/fast-jev-compaction/dist/types.js.map +1 -0
- package/node_modules/fast-jev-compaction/package.json +39 -0
- package/package.json +42 -0
- package/src/adapter.ts +373 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { noulAnswer } from './request.js';
|
|
2
|
+
import { collectToolCalls, estimateTokens, fitState } from './state.js';
|
|
3
|
+
export const DEFAULT_OPTIONS = {
|
|
4
|
+
goal: '',
|
|
5
|
+
keepThreshold: 0.5,
|
|
6
|
+
preserveRecentMessages: 6,
|
|
7
|
+
maxStateTokens: 25_000,
|
|
8
|
+
maxRequestTokens: 30_000,
|
|
9
|
+
truncateHeadChars: 300,
|
|
10
|
+
};
|
|
11
|
+
/** Tokens the request envelope (`model`, key names) adds around state and questions. */
|
|
12
|
+
const REQUEST_OVERHEAD_TOKENS = 20;
|
|
13
|
+
function finite(value, fallback) {
|
|
14
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : fallback;
|
|
15
|
+
}
|
|
16
|
+
export function resolveOptions(options = {}) {
|
|
17
|
+
return {
|
|
18
|
+
goal: options.goal ?? DEFAULT_OPTIONS.goal,
|
|
19
|
+
keepThreshold: finite(options.keepThreshold, DEFAULT_OPTIONS.keepThreshold),
|
|
20
|
+
preserveRecentMessages: Math.max(0, Math.floor(finite(options.preserveRecentMessages, DEFAULT_OPTIONS.preserveRecentMessages))),
|
|
21
|
+
maxStateTokens: Math.max(1, finite(options.maxStateTokens, DEFAULT_OPTIONS.maxStateTokens)),
|
|
22
|
+
maxRequestTokens: Math.max(1, finite(options.maxRequestTokens, DEFAULT_OPTIONS.maxRequestTokens)),
|
|
23
|
+
truncateHeadChars: Math.max(0, Math.floor(finite(options.truncateHeadChars, DEFAULT_OPTIONS.truncateHeadChars))),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** The two `noul` questions asked about one call: keep the call, keep its result. */
|
|
27
|
+
export function questionsFor(call) {
|
|
28
|
+
return {
|
|
29
|
+
[`call_${call.id}`]: {
|
|
30
|
+
type: 'noul',
|
|
31
|
+
instructions: `Tool call ${call.id} (${call.tool}) should stay in the history: knowing this call was made, with its input, still matters for what the assistant does next`,
|
|
32
|
+
},
|
|
33
|
+
[`result_${call.id}`]: {
|
|
34
|
+
type: 'noul',
|
|
35
|
+
instructions: `The full output of tool call ${call.id} (${call.tool}, ${call.resultChars} chars) should stay in the history verbatim: the assistant still needs its contents and re-running the tool would not do`,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Splits the candidate calls into batches whose questions, together with the
|
|
41
|
+
* (always complete) state, fit one request.
|
|
42
|
+
*/
|
|
43
|
+
export function batchCalls(calls, stateTokens, options) {
|
|
44
|
+
const budget = options.maxRequestTokens - stateTokens - REQUEST_OVERHEAD_TOKENS;
|
|
45
|
+
const batches = [];
|
|
46
|
+
let current = [];
|
|
47
|
+
let currentTokens = 0;
|
|
48
|
+
for (const call of calls) {
|
|
49
|
+
const tokens = estimateTokens(JSON.stringify(questionsFor(call)));
|
|
50
|
+
if (current.length > 0 && currentTokens + tokens > budget) {
|
|
51
|
+
batches.push(current);
|
|
52
|
+
current = [];
|
|
53
|
+
currentTokens = 0;
|
|
54
|
+
}
|
|
55
|
+
if (current.length === 0 && tokens > budget) {
|
|
56
|
+
throw new Error(`state leaves no room for questions (~${stateTokens} of ${options.maxRequestTokens} tokens)`);
|
|
57
|
+
}
|
|
58
|
+
current.push(call);
|
|
59
|
+
currentTokens += tokens;
|
|
60
|
+
}
|
|
61
|
+
if (current.length > 0)
|
|
62
|
+
batches.push(current);
|
|
63
|
+
return batches;
|
|
64
|
+
}
|
|
65
|
+
export function decideCall(call, answer, options) {
|
|
66
|
+
const base = { id: call.id, tool: call.tool, ...answer };
|
|
67
|
+
if (call.pinned)
|
|
68
|
+
return { ...base, action: 'keep', reason: 'pinned' };
|
|
69
|
+
if (answer.keepResult >= options.keepThreshold) {
|
|
70
|
+
return { ...base, action: 'keep', reason: 'kept' };
|
|
71
|
+
}
|
|
72
|
+
if (answer.keepCall >= options.keepThreshold) {
|
|
73
|
+
return { ...base, action: 'drop_result', reason: 'result_dropped' };
|
|
74
|
+
}
|
|
75
|
+
return { ...base, action: 'drop_call', reason: 'call_dropped' };
|
|
76
|
+
}
|
|
77
|
+
async function askBatch(asker, state, batch) {
|
|
78
|
+
const questions = Object.assign({}, ...batch.map(questionsFor));
|
|
79
|
+
const { answers } = await asker.ask(state, questions);
|
|
80
|
+
return new Map(batch.map((call) => [
|
|
81
|
+
call.id,
|
|
82
|
+
{
|
|
83
|
+
keepCall: noulAnswer(answers, `call_${call.id}`),
|
|
84
|
+
keepResult: noulAnswer(answers, `result_${call.id}`),
|
|
85
|
+
},
|
|
86
|
+
]));
|
|
87
|
+
}
|
|
88
|
+
function truncatedResultText(text, isError, headChars) {
|
|
89
|
+
if (text.length <= headChars + 120)
|
|
90
|
+
return text;
|
|
91
|
+
const head = headChars > 0 ? `${text.slice(0, headChars)}\n` : '';
|
|
92
|
+
return `${head}[fast-jev-compaction truncated ${text.length - headChars} chars of this tool result${isError ? ' (error)' : ''}; re-run the tool if needed]`;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Rebuilds the conversation from the decisions. A dropped call disappears
|
|
96
|
+
* together with its result; a dropped result keeps a bounded head and note.
|
|
97
|
+
* Messages that lose all their content are removed; untouched messages are
|
|
98
|
+
* returned as the same objects they came in as.
|
|
99
|
+
*/
|
|
100
|
+
export function applyDecisions(messages, decisions, calls, headChars) {
|
|
101
|
+
const byId = new Map(calls.map((call) => [call.id, call]));
|
|
102
|
+
const actions = new Map();
|
|
103
|
+
for (const decision of decisions) {
|
|
104
|
+
const call = byId.get(decision.id);
|
|
105
|
+
if (call && decision.action !== 'keep')
|
|
106
|
+
actions.set(call.tool_use_id, decision.action);
|
|
107
|
+
}
|
|
108
|
+
const kept = [];
|
|
109
|
+
for (const message of messages) {
|
|
110
|
+
const touched = message.toolUses.some((tool) => actions.has(tool.tool_use_id)) ||
|
|
111
|
+
(message.toolResults ?? []).some((result) => actions.has(result.tool_use_id));
|
|
112
|
+
if (!touched) {
|
|
113
|
+
kept.push(message);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
const toolUses = message.toolUses
|
|
117
|
+
.filter((tool) => actions.get(tool.tool_use_id) !== 'drop_call')
|
|
118
|
+
.map((tool) => {
|
|
119
|
+
if (actions.get(tool.tool_use_id) !== 'drop_result')
|
|
120
|
+
return tool;
|
|
121
|
+
const text = truncatedResultText(tool.text ?? '', tool.isError ?? false, headChars);
|
|
122
|
+
if ((tool.text ?? '') === text)
|
|
123
|
+
return tool;
|
|
124
|
+
const copy = {
|
|
125
|
+
tool_use_id: tool.tool_use_id,
|
|
126
|
+
tool: tool.tool,
|
|
127
|
+
input: tool.input,
|
|
128
|
+
text,
|
|
129
|
+
};
|
|
130
|
+
if (tool.isError)
|
|
131
|
+
copy.isError = true;
|
|
132
|
+
return copy;
|
|
133
|
+
});
|
|
134
|
+
const toolResults = (message.toolResults ?? [])
|
|
135
|
+
.filter((result) => actions.get(result.tool_use_id) !== 'drop_call')
|
|
136
|
+
.map((result) => {
|
|
137
|
+
if (actions.get(result.tool_use_id) !== 'drop_result')
|
|
138
|
+
return result;
|
|
139
|
+
const text = truncatedResultText(result.text, result.isError ?? false, headChars);
|
|
140
|
+
return text === result.text
|
|
141
|
+
? result
|
|
142
|
+
: {
|
|
143
|
+
tool_use_id: result.tool_use_id,
|
|
144
|
+
text,
|
|
145
|
+
isError: result.isError,
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
if (!message.toolUses.some((tool) => actions.get(tool.tool_use_id) === 'drop_call') &&
|
|
149
|
+
!(message.toolResults ?? []).some((result) => actions.get(result.tool_use_id) === 'drop_call') &&
|
|
150
|
+
toolUses.every((tool, index) => tool === message.toolUses[index]) &&
|
|
151
|
+
toolResults.every((result, index) => result === message.toolResults?.[index])) {
|
|
152
|
+
kept.push(message);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (message.text.trim().length === 0 && toolUses.length === 0 && toolResults.length === 0) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const rebuilt = { role: message.role, text: message.text, toolUses };
|
|
159
|
+
if (toolResults.length > 0)
|
|
160
|
+
rebuilt.toolResults = toolResults;
|
|
161
|
+
kept.push(rebuilt);
|
|
162
|
+
}
|
|
163
|
+
return kept;
|
|
164
|
+
}
|
|
165
|
+
/** Characters of text, tool input and tool output a message holds. */
|
|
166
|
+
export function messageChars(message) {
|
|
167
|
+
let total = message.text.length;
|
|
168
|
+
for (const tool of message.toolUses) {
|
|
169
|
+
try {
|
|
170
|
+
total += JSON.stringify(tool.input).length;
|
|
171
|
+
}
|
|
172
|
+
catch {
|
|
173
|
+
total += 20;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const result of message.toolResults ?? [])
|
|
177
|
+
total += result.text.length;
|
|
178
|
+
return total;
|
|
179
|
+
}
|
|
180
|
+
export function reductionRatio(result) {
|
|
181
|
+
const { charsBefore, charsAfter } = result.stats;
|
|
182
|
+
return charsBefore === 0 ? 0 : (charsBefore - charsAfter) / charsBefore;
|
|
183
|
+
}
|
|
184
|
+
function count(decisions, reason) {
|
|
185
|
+
return decisions.filter((decision) => decision.reason === reason).length;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Compacts a transcript by asking Jev, for every tool call outside the pinned
|
|
189
|
+
* first and newest messages, whether the call and whether its result must
|
|
190
|
+
* stay. The whole history (results omitted, fitted into `maxStateTokens`) is
|
|
191
|
+
* sent as state with every batch of questions. Throws when Jev fails or the
|
|
192
|
+
* history cannot be fitted; the caller decides whether to fall back.
|
|
193
|
+
*/
|
|
194
|
+
export async function compact(messages, asker, options = {}) {
|
|
195
|
+
const started = Date.now();
|
|
196
|
+
const resolved = resolveOptions(options);
|
|
197
|
+
const calls = collectToolCalls(messages, resolved.preserveRecentMessages);
|
|
198
|
+
const candidates = calls.filter((call) => !call.pinned);
|
|
199
|
+
const charsBefore = messages.reduce((sum, message) => sum + messageChars(message), 0);
|
|
200
|
+
let fitted = { tokens: 0, stage: '' };
|
|
201
|
+
let batches = [];
|
|
202
|
+
const answers = new Map();
|
|
203
|
+
if (candidates.length > 0) {
|
|
204
|
+
const state = fitState(messages, calls, resolved);
|
|
205
|
+
fitted = state;
|
|
206
|
+
batches = batchCalls(candidates, state.tokens, resolved);
|
|
207
|
+
const answered = await Promise.all(batches.map((batch) => askBatch(asker, state.state, batch)));
|
|
208
|
+
for (const map of answered)
|
|
209
|
+
for (const [id, answer] of map)
|
|
210
|
+
answers.set(id, answer);
|
|
211
|
+
}
|
|
212
|
+
const decisions = calls.map((call) => decideCall(call, answers.get(call.id) ?? { keepCall: 1, keepResult: 1 }, resolved));
|
|
213
|
+
const kept = applyDecisions(messages, decisions, calls, resolved.truncateHeadChars);
|
|
214
|
+
return {
|
|
215
|
+
messages: kept,
|
|
216
|
+
decisions,
|
|
217
|
+
stats: {
|
|
218
|
+
messagesBefore: messages.length,
|
|
219
|
+
messagesAfter: kept.length,
|
|
220
|
+
charsBefore,
|
|
221
|
+
charsAfter: kept.reduce((sum, message) => sum + messageChars(message), 0),
|
|
222
|
+
calls: calls.length,
|
|
223
|
+
kept: count(decisions, 'kept'),
|
|
224
|
+
resultsDropped: count(decisions, 'result_dropped'),
|
|
225
|
+
callsDropped: count(decisions, 'call_dropped'),
|
|
226
|
+
pinned: count(decisions, 'pinned'),
|
|
227
|
+
stateTokens: fitted.tokens,
|
|
228
|
+
stateStage: fitted.stage,
|
|
229
|
+
requests: batches.length,
|
|
230
|
+
ms: Date.now() - started,
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
//# sourceMappingURL=compact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact.js","sourceRoot":"","sources":["../src/compact.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAexE,MAAM,CAAC,MAAM,eAAe,GAA2B;IACrD,IAAI,EAAE,EAAE;IACR,aAAa,EAAE,GAAG;IAClB,sBAAsB,EAAE,CAAC;IACzB,cAAc,EAAE,MAAM;IACtB,gBAAgB,EAAE,MAAM;IACxB,iBAAiB,EAAE,GAAG;CACvB,CAAC;AAEF,wFAAwF;AACxF,MAAM,uBAAuB,GAAG,EAAE,CAAC;AAEnC,SAAS,MAAM,CAAC,KAAyB,EAAE,QAAgB;IACzD,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC;AAChF,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,UAA0B,EAAE;IACzD,OAAO;QACL,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,eAAe,CAAC,IAAI;QAC1C,aAAa,EAAE,MAAM,CAAC,OAAO,CAAC,aAAa,EAAE,eAAe,CAAC,aAAa,CAAC;QAC3E,sBAAsB,EAAE,IAAI,CAAC,GAAG,CAC9B,CAAC,EACD,IAAI,CAAC,KAAK,CACR,MAAM,CAAC,OAAO,CAAC,sBAAsB,EAAE,eAAe,CAAC,sBAAsB,CAAC,CAC/E,CACF;QACD,cAAc,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,eAAe,CAAC,cAAc,CAAC,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IAAI;QACd,SAAS;QACT,KAAK,EAAE;YACL,cAAc,EAAE,QAAQ,CAAC,MAAM;YAC/B,aAAa,EAAE,IAAI,CAAC,MAAM;YAC1B,WAAW;YACX,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,GAAG,YAAY,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YACzE,KAAK,EAAE,KAAK,CAAC,MAAM;YACnB,IAAI,EAAE,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC;YAC9B,cAAc,EAAE,KAAK,CAAC,SAAS,EAAE,gBAAgB,CAAC;YAClD,YAAY,EAAE,KAAK,CAAC,SAAS,EAAE,cAAc,CAAC;YAC9C,MAAM,EAAE,KAAK,CAAC,SAAS,EAAE,QAAQ,CAAC;YAClC,WAAW,EAAE,MAAM,CAAC,MAAM;YAC1B,UAAU,EAAE,MAAM,CAAC,KAAK;YACxB,QAAQ,EAAE,OAAO,CAAC,MAAM;YACxB,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO;SACzB;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type JevClientOptions } from './client.js';
|
|
2
|
+
import type { CompactOptions, CompactResult, Message } from './types.js';
|
|
3
|
+
export type CompactMessagesOptions = CompactOptions & JevClientOptions;
|
|
4
|
+
/** `compact` with a `JevClient` built from the options (key from `TYPESAFE_API_KEY` by default). */
|
|
5
|
+
export declare function compactMessages(messages: readonly Message[], options?: CompactMessagesOptions): Promise<CompactResult>;
|
|
6
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAEzE,MAAM,MAAM,sBAAsB,GAAG,cAAc,GAAG,gBAAgB,CAAC;AAEvE,oGAAoG;AACpG,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,aAAa,CAAC,CAExB"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { JevClient } from './client.js';
|
|
2
|
+
import { compact } from './compact.js';
|
|
3
|
+
/** `compact` with a `JevClient` built from the options (key from `TYPESAFE_API_KEY` by default). */
|
|
4
|
+
export function compactMessages(messages, options = {}) {
|
|
5
|
+
return compact(messages, new JevClient(options), options);
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../src/messages.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAyB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAKvC,oGAAoG;AACpG,MAAM,UAAU,eAAe,CAC7B,QAA4B,EAC5B,UAAkC,EAAE;IAEpC,OAAO,OAAO,CAAC,QAAQ,EAAE,IAAI,SAAS,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { JevAnswer, JevQuestions, JevResponse, JevState } from './types.js';
|
|
2
|
+
export declare const SYSTEM_ONE_URL = "https://api.typesafe.ai/v1/systemone";
|
|
3
|
+
export declare const DEFAULT_MODEL = "jev-latest";
|
|
4
|
+
export interface JevRequest {
|
|
5
|
+
url: string;
|
|
6
|
+
method: 'POST';
|
|
7
|
+
headers: Record<string, string>;
|
|
8
|
+
body: string;
|
|
9
|
+
}
|
|
10
|
+
/** The HTTP request for one Jev call, for any fetch-like transport. */
|
|
11
|
+
export declare function buildJevRequest(params: {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
model?: string;
|
|
14
|
+
baseUrl?: string;
|
|
15
|
+
}, state: JevState, questions: JevQuestions): JevRequest;
|
|
16
|
+
/** Validates a Jev response body; throws on anything but an `answers` object. */
|
|
17
|
+
export declare function parseJevResponse(status: number, ok: boolean, text: string): JevResponse;
|
|
18
|
+
/** The `noul` probability of one answer; throws when it is not there. */
|
|
19
|
+
export declare function noulAnswer(answers: Record<string, JevAnswer>, name: string): number;
|
|
20
|
+
//# sourceMappingURL=request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEjF,eAAO,MAAM,cAAc,yCAAyC,CAAC;AACrE,eAAO,MAAM,aAAa,eAAe,CAAC;AAE1C,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,uEAAuE;AACvE,wBAAgB,eAAe,CAC7B,MAAM,EAAE;IACN,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,EACD,KAAK,EAAE,QAAQ,EACf,SAAS,EAAE,YAAY,GACtB,UAAU,CAcZ;AAED,iFAAiF;AACjF,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,EACd,EAAE,EAAE,OAAO,EACX,IAAI,EAAE,MAAM,GACX,WAAW,CAoBb;AAED,yEAAyE;AACzE,wBAAgB,UAAU,CACxB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,EAClC,IAAI,EAAE,MAAM,GACX,MAAM,CAWR"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const SYSTEM_ONE_URL = 'https://api.typesafe.ai/v1/systemone';
|
|
2
|
+
export const DEFAULT_MODEL = 'jev-latest';
|
|
3
|
+
/** The HTTP request for one Jev call, for any fetch-like transport. */
|
|
4
|
+
export function buildJevRequest(params, state, questions) {
|
|
5
|
+
return {
|
|
6
|
+
url: params.baseUrl ?? SYSTEM_ONE_URL,
|
|
7
|
+
method: 'POST',
|
|
8
|
+
headers: {
|
|
9
|
+
authorization: `Bearer ${params.apiKey}`,
|
|
10
|
+
'content-type': 'application/json',
|
|
11
|
+
},
|
|
12
|
+
body: JSON.stringify({
|
|
13
|
+
model: params.model ?? DEFAULT_MODEL,
|
|
14
|
+
state,
|
|
15
|
+
questions,
|
|
16
|
+
}),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Validates a Jev response body; throws on anything but an `answers` object. */
|
|
20
|
+
export function parseJevResponse(status, ok, text) {
|
|
21
|
+
if (!ok) {
|
|
22
|
+
throw new Error(`Jev request failed (${status}): ${text.slice(0, 200)}`);
|
|
23
|
+
}
|
|
24
|
+
let parsed;
|
|
25
|
+
try {
|
|
26
|
+
parsed = JSON.parse(text);
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new Error('Jev returned malformed JSON');
|
|
30
|
+
}
|
|
31
|
+
if (parsed === null ||
|
|
32
|
+
typeof parsed !== 'object' ||
|
|
33
|
+
!('answers' in parsed) ||
|
|
34
|
+
parsed.answers === null ||
|
|
35
|
+
typeof parsed.answers !== 'object') {
|
|
36
|
+
throw new Error('Jev response is missing answers');
|
|
37
|
+
}
|
|
38
|
+
return parsed;
|
|
39
|
+
}
|
|
40
|
+
/** The `noul` probability of one answer; throws when it is not there. */
|
|
41
|
+
export function noulAnswer(answers, name) {
|
|
42
|
+
const answer = answers[name];
|
|
43
|
+
if (!answer ||
|
|
44
|
+
!('noul' in answer) ||
|
|
45
|
+
typeof answer.noul !== 'number' ||
|
|
46
|
+
!Number.isFinite(answer.noul)) {
|
|
47
|
+
throw new Error(`Invalid Jev answer for ${name}`);
|
|
48
|
+
}
|
|
49
|
+
return answer.noul;
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../src/request.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAG,sCAAsC,CAAC;AACrE,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAC;AAS1C,uEAAuE;AACvE,MAAM,UAAU,eAAe,CAC7B,MAIC,EACD,KAAe,EACf,SAAuB;IAEvB,OAAO;QACL,GAAG,EAAE,MAAM,CAAC,OAAO,IAAI,cAAc;QACrC,MAAM,EAAE,MAAM;QACd,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,MAAM,CAAC,MAAM,EAAE;YACxC,cAAc,EAAE,kBAAkB;SACnC;QACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;YACnB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,aAAa;YACpC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1
|
+
import type { FittedState, Message, ResolvedCompactOptions, ToolCall } from './types.js';
|
|
2
|
+
export declare const STATE_CONTEXT = "A coding assistant conversation is being compacted to free context. `history` is the whole conversation so far, oldest first; tool outputs are replaced by a short `result` note and long texts may be abridged. Each question asks whether one tool call, or the full output of that call, still needs to stay in the history verbatim. Whatever is not kept is deleted permanently, but the assistant can always re-run a tool or re-read a file.";
|
|
3
|
+
/**
|
|
4
|
+
* Estimates tokens without a tokenizer: a word costs one token per six
|
|
5
|
+
* letters, a digit half a token, any other symbol nine tenths. Calibrated
|
|
6
|
+
* against the usage Jev reports for real transcripts, where it lands 2–18%
|
|
7
|
+
* above the true count; a plain characters-per-token ratio undercounts the
|
|
8
|
+
* JSON-heavy states by up to 40%.
|
|
9
|
+
*/
|
|
10
|
+
export declare function estimateTokens(text: string): number;
|
|
11
|
+
export declare function truncate(text: string, limit: number): string;
|
|
12
|
+
export declare function isPinned(index: number, total: number, preserveRecentMessages: number): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Pairs every tool_use with its tool_result by `tool_use_id`. Calls without a
|
|
15
|
+
* result are not candidates (there is nothing to drop yet).
|
|
16
|
+
*/
|
|
17
|
+
export declare function collectToolCalls(messages: readonly Message[], preserveRecentMessages: number): ToolCall[];
|
|
18
|
+
/** The last three user prompts, as the default `goal`. */
|
|
19
|
+
export declare function goalFromMessages(messages: readonly Message[]): string;
|
|
20
|
+
/**
|
|
21
|
+
* Builds the Jev state from the whole conversation and shrinks it in stages
|
|
22
|
+
* until it fits `maxStateTokens`: tool inputs are truncated, then long texts
|
|
23
|
+
* are abridged oldest-first (pinned messages last), then old messages collapse
|
|
24
|
+
* to a one-line note, then old tool calls shrink to one line each, then old
|
|
25
|
+
* messages that carry no call are left out, then runs of old call-only
|
|
26
|
+
* messages are folded into one entry. Throws when even that is too big.
|
|
27
|
+
*/
|
|
28
|
+
export declare function fitState(messages: readonly Message[], calls: readonly ToolCall[], options: Pick<ResolvedCompactOptions, 'maxStateTokens' | 'preserveRecentMessages' | 'goal'>): FittedState;
|
|
29
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,WAAW,EAEX,OAAO,EACP,sBAAsB,EACtB,QAAQ,EAET,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,aAAa,wbAC6Z,CAAC;AASxb;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAUnD;AAED,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE5D;AAQD,wBAAgB,QAAQ,CACtB,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,sBAAsB,EAAE,MAAM,GAC7B,OAAO,CAET;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,sBAAsB,EAAE,MAAM,GAC7B,QAAQ,EAAE,CA4BZ;AAgFD,0DAA0D;AAC1D,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,SAAS,OAAO,EAAE,GAAG,MAAM,CAWrE;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CACtB,QAAQ,EAAE,SAAS,OAAO,EAAE,EAC5B,KAAK,EAAE,SAAS,QAAQ,EAAE,EAC1B,OAAO,EAAE,IAAI,CAAC,sBAAsB,EAAE,gBAAgB,GAAG,wBAAwB,GAAG,MAAM,CAAC,GAC1F,WAAW,CAyGb"}
|