@genesislcap/ai-assistant 15.4.1 → 15.6.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/ai-assistant.api.json +571 -82
- package/dist/ai-assistant.d.ts +336 -36
- package/dist/chat-driver.cjs +104 -26
- package/dist/chat-driver.cjs.map +3 -3
- package/dist/chat-driver.mjs +104 -26
- package/dist/chat-driver.mjs.map +3 -3
- package/dist/custom-elements.json +314 -36
- package/dist/dts/components/chat-driver/chat-driver.d.ts +12 -0
- package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
- package/dist/dts/components/settings-modal/settings-modal.styles.d.ts.map +1 -1
- package/dist/dts/components/settings-modal/settings-modal.template.d.ts +9 -2
- package/dist/dts/components/settings-modal/settings-modal.template.d.ts.map +1 -1
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/main/cost-session-banking.test.d.ts +2 -0
- package/dist/dts/main/cost-session-banking.test.d.ts.map +1 -0
- package/dist/dts/main/main.d.ts +184 -24
- package/dist/dts/main/main.d.ts.map +1 -1
- package/dist/dts/provider/assistant-app-settings.d.ts +30 -5
- package/dist/dts/provider/assistant-app-settings.d.ts.map +1 -1
- package/dist/dts/state/ai-assistant-slice.d.ts +11 -8
- package/dist/dts/state/ai-assistant-slice.d.ts.map +1 -1
- package/dist/dts/state/persistence/session-persistence-provider.d.ts +23 -0
- package/dist/dts/state/persistence/session-persistence-provider.d.ts.map +1 -1
- package/dist/dts/state/persistence/session-snapshot.d.ts.map +1 -1
- package/dist/dts/state/session-store.d.ts +1 -2
- package/dist/dts/state/session-store.d.ts.map +1 -1
- package/dist/dts/styles/settings-section.d.ts +29 -0
- package/dist/dts/styles/settings-section.d.ts.map +1 -0
- package/dist/dts/utils/condense-history.d.ts +14 -0
- package/dist/dts/utils/condense-history.d.ts.map +1 -1
- package/dist/dts/utils/cost-session-history.d.ts +103 -12
- package/dist/dts/utils/cost-session-history.d.ts.map +1 -1
- package/dist/dts/utils/resolve-cost-history-config.d.ts +9 -3
- package/dist/dts/utils/resolve-cost-history-config.d.ts.map +1 -1
- package/dist/dts/utils/sum-costs.d.ts.map +1 -1
- package/dist/dts/utils/sum-tokens.d.ts +8 -8
- package/dist/dts/utils/sum-tokens.d.ts.map +1 -1
- package/dist/dts/utils/sum-usage.d.ts +59 -0
- package/dist/dts/utils/sum-usage.d.ts.map +1 -0
- package/dist/dts/utils/sum-usage.test.d.ts +2 -0
- package/dist/dts/utils/sum-usage.test.d.ts.map +1 -0
- package/dist/esm/components/chat-driver/chat-driver.js +9 -1
- package/dist/esm/components/settings-modal/settings-modal.styles.js +237 -18
- package/dist/esm/components/settings-modal/settings-modal.template.js +229 -73
- package/dist/esm/index.js +1 -0
- package/dist/esm/main/cost-session-banking.test.js +308 -0
- package/dist/esm/main/main.js +424 -71
- package/dist/esm/state/ai-assistant-slice.js +11 -8
- package/dist/esm/state/ai-assistant-slice.test.js +12 -5
- package/dist/esm/state/debug-event-log.js +2 -2
- package/dist/esm/state/persistence/session-persistence.integration.test.js +5 -1
- package/dist/esm/state/persistence/session-persister.js +2 -2
- package/dist/esm/state/persistence/session-persister.test.js +10 -1
- package/dist/esm/state/persistence/session-snapshot.js +6 -2
- package/dist/esm/state/persistence/session-snapshot.test.js +4 -1
- package/dist/esm/state/persistence/stateful-restore.e2e.test.js +10 -1
- package/dist/esm/styles/settings-section.js +39 -0
- package/dist/esm/utils/condense-history.js +18 -5
- package/dist/esm/utils/condense-history.test.js +67 -0
- package/dist/esm/utils/cost-session-history.js +92 -15
- package/dist/esm/utils/cost-session-history.test.js +155 -13
- package/dist/esm/utils/resolve-cost-history-config.js +2 -1
- package/dist/esm/utils/sum-costs.js +2 -13
- package/dist/esm/utils/sum-tokens.js +10 -27
- package/dist/esm/utils/sum-tokens.test.js +1 -5
- package/dist/esm/utils/sum-usage.js +123 -0
- package/dist/esm/utils/sum-usage.test.js +120 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +17 -17
- package/src/components/chat-driver/chat-driver.ts +21 -0
- package/src/components/settings-modal/settings-modal.styles.ts +237 -18
- package/src/components/settings-modal/settings-modal.template.ts +270 -81
- package/src/index.ts +1 -0
- package/src/main/cost-session-banking.test.ts +407 -0
- package/src/main/main.ts +433 -68
- package/src/provider/assistant-app-settings.ts +31 -5
- package/src/state/ai-assistant-slice.test.ts +12 -5
- package/src/state/ai-assistant-slice.ts +21 -13
- package/src/state/debug-event-log.ts +2 -2
- package/src/state/persistence/session-persistence-provider.ts +24 -0
- package/src/state/persistence/session-persistence.integration.test.ts +8 -1
- package/src/state/persistence/session-persister.test.ts +10 -1
- package/src/state/persistence/session-persister.ts +2 -2
- package/src/state/persistence/session-snapshot.test.ts +4 -1
- package/src/state/persistence/session-snapshot.ts +5 -1
- package/src/state/persistence/stateful-restore.e2e.test.ts +9 -1
- package/src/styles/settings-section.ts +40 -0
- package/src/utils/condense-history.test.ts +103 -0
- package/src/utils/condense-history.ts +33 -3
- package/src/utils/cost-session-history.test.ts +187 -16
- package/src/utils/cost-session-history.ts +142 -23
- package/src/utils/resolve-cost-history-config.ts +10 -3
- package/src/utils/sum-costs.ts +2 -9
- package/src/utils/sum-tokens.test.ts +1 -11
- package/src/utils/sum-tokens.ts +10 -26
- package/src/utils/sum-usage.test.ts +140 -0
- package/src/utils/sum-usage.ts +130 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { ChatMessage } from '@genesislcap/foundation-ai';
|
|
2
|
+
import { assert, createLogicSuite } from '@genesislcap/foundation-testing';
|
|
3
|
+
import { addUsage, emptyUsage, sumUsage, totalTokens } from './sum-usage';
|
|
4
|
+
|
|
5
|
+
const suite = createLogicSuite('sumUsage');
|
|
6
|
+
|
|
7
|
+
const msg = (over: Partial<ChatMessage> = {}): ChatMessage =>
|
|
8
|
+
({ role: 'assistant', content: '', ...over }) as ChatMessage;
|
|
9
|
+
|
|
10
|
+
suite('returns zeroes when no message carries usage', () => {
|
|
11
|
+
const usage = sumUsage([msg({ role: 'user', content: 'hi' })]);
|
|
12
|
+
assert.equal(usage, emptyUsage());
|
|
13
|
+
assert.is(totalTokens(usage), 0);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
suite('splits the prompt into three disjoint input buckets', () => {
|
|
17
|
+
// `inputTokens` is the WHOLE prompt; the cache fields break it down. So uncached is
|
|
18
|
+
// the remainder, and the three must re-add to the prompt exactly — that identity is
|
|
19
|
+
// what stops the buckets double-counting the prompt when summed.
|
|
20
|
+
const usage = sumUsage([
|
|
21
|
+
msg({ inputTokens: 1000, cacheReadTokens: 700, cacheWriteTokens: 200, outputTokens: 50 }),
|
|
22
|
+
]);
|
|
23
|
+
assert.is(usage.uncachedInputTokens, 100);
|
|
24
|
+
assert.is(usage.cacheReadTokens, 700);
|
|
25
|
+
assert.is(usage.cacheWriteTokens, 200);
|
|
26
|
+
assert.is(usage.outputTokens, 50);
|
|
27
|
+
assert.is(
|
|
28
|
+
usage.uncachedInputTokens + usage.cacheReadTokens + usage.cacheWriteTokens,
|
|
29
|
+
1000,
|
|
30
|
+
'input buckets re-add to the prompt total',
|
|
31
|
+
);
|
|
32
|
+
assert.is(totalTokens(usage), 1050);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
suite('treats the whole prompt as uncached when no cache split is reported', () => {
|
|
36
|
+
// Providers that report no cache breakdown (and messages persisted before the fields
|
|
37
|
+
// existed) must still contribute their full prompt, not drop it.
|
|
38
|
+
const usage = sumUsage([msg({ inputTokens: 400, outputTokens: 60 })]);
|
|
39
|
+
assert.is(usage.uncachedInputTokens, 400);
|
|
40
|
+
assert.is(usage.cacheReadTokens, 0);
|
|
41
|
+
assert.is(usage.cacheWriteTokens, 0);
|
|
42
|
+
assert.is(totalTokens(usage), 460);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
suite('clamps uncached input at zero when buckets exceed the prompt total', () => {
|
|
46
|
+
// Malformed or hand-edited history must not be able to drive a cost display negative.
|
|
47
|
+
const usage = sumUsage([
|
|
48
|
+
msg({ inputTokens: 100, cacheReadTokens: 90, cacheWriteTokens: 90, outputTokens: 0 }),
|
|
49
|
+
]);
|
|
50
|
+
assert.is(usage.uncachedInputTokens, 0);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
suite('sums LLM cost and widget-reported external cost', () => {
|
|
54
|
+
const usage = sumUsage([msg({ cost: 0.25 }), msg({ externalCostUsd: 0.1, cost: 0.05 })]);
|
|
55
|
+
assert.is(Math.round(usage.costUsd * 100) / 100, 0.4);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
suite('recurses into sub-agent traces', () => {
|
|
59
|
+
// A sub-agent's whole conversation is embedded in the parent's tool call, so this is
|
|
60
|
+
// the only place its spend is visible to a host total.
|
|
61
|
+
const usage = sumUsage([
|
|
62
|
+
msg({
|
|
63
|
+
toolCalls: [
|
|
64
|
+
{
|
|
65
|
+
id: 'tc-1',
|
|
66
|
+
name: 'delegate',
|
|
67
|
+
args: {},
|
|
68
|
+
subAgentTrace: [
|
|
69
|
+
msg({ cost: 0.2, inputTokens: 300, cacheReadTokens: 100, outputTokens: 40 }),
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
}),
|
|
74
|
+
]);
|
|
75
|
+
assert.is(usage.costUsd, 0.2);
|
|
76
|
+
assert.is(usage.uncachedInputTokens, 200);
|
|
77
|
+
assert.is(usage.cacheReadTokens, 100);
|
|
78
|
+
assert.is(usage.outputTokens, 40);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
suite('counts usage banked onto a compaction summary', () => {
|
|
82
|
+
// Compaction destroys the messages it summarises. Without the banked rollup, a total
|
|
83
|
+
// re-derived from the transcript would fall by whatever those turns had spent.
|
|
84
|
+
const before = sumUsage([
|
|
85
|
+
msg({ cost: 0.5, inputTokens: 1000, cacheReadTokens: 400, outputTokens: 100 }),
|
|
86
|
+
msg({ cost: 0.3, inputTokens: 500, outputTokens: 50 }),
|
|
87
|
+
]);
|
|
88
|
+
const afterCompaction = sumUsage([
|
|
89
|
+
msg({
|
|
90
|
+
role: 'compacted-summary',
|
|
91
|
+
content: 'summary of the above',
|
|
92
|
+
compaction: {
|
|
93
|
+
compactedCount: 2,
|
|
94
|
+
createdAt: '2026-06-14T12:00:00.000Z',
|
|
95
|
+
rolledUpUsage: before,
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
]);
|
|
99
|
+
assert.equal(afterCompaction, before, 'compaction preserves the running total');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
suite('ignores tokensBefore, which is a context reading rather than spend', () => {
|
|
103
|
+
// `tokensBefore` is one turn's context-window size. Counting it as cumulative spend
|
|
104
|
+
// would silently inflate the session total on every compaction.
|
|
105
|
+
const usage = sumUsage([
|
|
106
|
+
msg({
|
|
107
|
+
role: 'compacted-summary',
|
|
108
|
+
content: 'summary',
|
|
109
|
+
compaction: {
|
|
110
|
+
compactedCount: 3,
|
|
111
|
+
createdAt: '2026-06-14T12:00:00.000Z',
|
|
112
|
+
tokensBefore: 90_000,
|
|
113
|
+
},
|
|
114
|
+
}),
|
|
115
|
+
]);
|
|
116
|
+
assert.is(totalTokens(usage), 0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
suite('addUsage adds every bucket and mutates neither operand', () => {
|
|
120
|
+
const a = { ...emptyUsage(), costUsd: 1.5, uncachedInputTokens: 100, outputTokens: 10 };
|
|
121
|
+
const b = { ...emptyUsage(), costUsd: 0.25, cacheReadTokens: 900, outputTokens: 5 };
|
|
122
|
+
const sum = addUsage(a, b);
|
|
123
|
+
|
|
124
|
+
assert.is(sum.costUsd, 1.75);
|
|
125
|
+
assert.is(sum.uncachedInputTokens, 100);
|
|
126
|
+
assert.is(sum.cacheReadTokens, 900);
|
|
127
|
+
assert.is(sum.outputTokens, 15);
|
|
128
|
+
// Both operands are live state (a row's banked figure and the session total), so a
|
|
129
|
+
// caller must be able to add without either being altered underneath it.
|
|
130
|
+
assert.is(a.costUsd, 1.5);
|
|
131
|
+
assert.is(b.cacheReadTokens, 900);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
suite('addUsage with an empty operand is the identity', () => {
|
|
135
|
+
const a = { ...emptyUsage(), costUsd: 3, cacheWriteTokens: 42 };
|
|
136
|
+
assert.equal(addUsage(a, emptyUsage()), a);
|
|
137
|
+
assert.equal(addUsage(emptyUsage(), a), a);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
suite.run();
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { AggregateUsage, ChatMessage } from '@genesislcap/foundation-ai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Zeroed {@link AggregateUsage}. A factory, not a shared const, because callers
|
|
5
|
+
* accumulate into the returned object.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export function emptyUsage(): AggregateUsage {
|
|
10
|
+
return {
|
|
11
|
+
costUsd: 0,
|
|
12
|
+
uncachedInputTokens: 0,
|
|
13
|
+
cacheReadTokens: 0,
|
|
14
|
+
cacheWriteTokens: 0,
|
|
15
|
+
outputTokens: 0,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Total tokens processed — the four buckets added up. Safe to sum because the
|
|
21
|
+
* buckets are disjoint (see {@link AggregateUsage}); the equivalent sum over
|
|
22
|
+
* per-message fields would double-count the prompt.
|
|
23
|
+
*
|
|
24
|
+
* Bear in mind what this number is: because every turn resends the conversation,
|
|
25
|
+
* a turn's prompt is counted again in the next turn's. It is billed throughput,
|
|
26
|
+
* NOT the count of distinct tokens in the conversation, so it grows
|
|
27
|
+
* super-linearly with turn count and is expected to dwarf the context size.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export function totalTokens(usage: AggregateUsage): number {
|
|
32
|
+
return (
|
|
33
|
+
usage.uncachedInputTokens + usage.cacheReadTokens + usage.cacheWriteTokens + usage.outputTokens
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* `a + b`, per bucket, as a new object.
|
|
39
|
+
*
|
|
40
|
+
* Used to lay a row's banked (pre-transcript) spend over what the live transcript
|
|
41
|
+
* proves. Pure rather than in-place because both operands are live state a caller
|
|
42
|
+
* must not mutate.
|
|
43
|
+
*
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
export function addUsage(a: AggregateUsage, b: AggregateUsage): AggregateUsage {
|
|
47
|
+
return {
|
|
48
|
+
costUsd: a.costUsd + b.costUsd,
|
|
49
|
+
uncachedInputTokens: a.uncachedInputTokens + b.uncachedInputTokens,
|
|
50
|
+
cacheReadTokens: a.cacheReadTokens + b.cacheReadTokens,
|
|
51
|
+
cacheWriteTokens: a.cacheWriteTokens + b.cacheWriteTokens,
|
|
52
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Sum cost and per-bucket token usage across a message list.
|
|
58
|
+
*
|
|
59
|
+
* One walk produces everything a usage display needs, replacing separate cost and
|
|
60
|
+
* token passes over the same (potentially deep) transcript. Recurses into each
|
|
61
|
+
* tool call's `subAgentTrace`, so LLM work a sub-agent did — possibly on another
|
|
62
|
+
* provider at other rates — lands in the total; the driver embeds a whole
|
|
63
|
+
* sub-agent conversation in the parent's tool result, so this is the only place
|
|
64
|
+
* that spend is visible.
|
|
65
|
+
*
|
|
66
|
+
* Splitting the prompt into buckets is the point: `cost` already has the cache
|
|
67
|
+
* discount applied, but a single token total cannot show that a session was cheap
|
|
68
|
+
* because most of its prompt was a cache hit. The three input buckets are derived
|
|
69
|
+
* from the per-message fields, where `inputTokens` is the whole prompt and the
|
|
70
|
+
* cache fields break it down — so uncached input is the remainder, never an
|
|
71
|
+
* addition.
|
|
72
|
+
*
|
|
73
|
+
* Also counts `compaction.rolledUpUsage`: compaction destroys the messages it
|
|
74
|
+
* summarises, and without their banked usage every total re-derived from the
|
|
75
|
+
* transcript would drop by whatever those turns had already spent.
|
|
76
|
+
*
|
|
77
|
+
* Returns a zeroed total when no message carries usage — providers that report
|
|
78
|
+
* none (e.g. Chrome built-in) are skipped silently rather than guessed at.
|
|
79
|
+
*
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
export function sumUsage(messages: readonly ChatMessage[]): AggregateUsage {
|
|
83
|
+
const total = emptyUsage();
|
|
84
|
+
accumulate(messages, total);
|
|
85
|
+
return total;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Add `from` into `into`, in place. */
|
|
89
|
+
function addInto(into: AggregateUsage, from: AggregateUsage): void {
|
|
90
|
+
into.costUsd += from.costUsd;
|
|
91
|
+
into.uncachedInputTokens += from.uncachedInputTokens;
|
|
92
|
+
into.cacheReadTokens += from.cacheReadTokens;
|
|
93
|
+
into.cacheWriteTokens += from.cacheWriteTokens;
|
|
94
|
+
into.outputTokens += from.outputTokens;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Walk `messages`, accumulating into `into`. Mutates a single accumulator rather
|
|
99
|
+
* than merging returned objects — this runs on every transcript change, so the
|
|
100
|
+
* per-message allocation is worth avoiding.
|
|
101
|
+
*/
|
|
102
|
+
function accumulate(messages: readonly ChatMessage[], into: AggregateUsage): void {
|
|
103
|
+
for (const m of messages) {
|
|
104
|
+
// Provider/LLM cost plus any non-LLM cost a widget reported for its own
|
|
105
|
+
// external service calls. Both are real spend on this session.
|
|
106
|
+
if (m.cost != null) into.costUsd += m.cost;
|
|
107
|
+
if (m.externalCostUsd != null) into.costUsd += m.externalCostUsd;
|
|
108
|
+
|
|
109
|
+
const cacheRead = m.cacheReadTokens ?? 0;
|
|
110
|
+
const cacheWrite = m.cacheWriteTokens ?? 0;
|
|
111
|
+
into.cacheReadTokens += cacheRead;
|
|
112
|
+
into.cacheWriteTokens += cacheWrite;
|
|
113
|
+
// The uncached remainder of the prompt. Clamped at zero so malformed or
|
|
114
|
+
// hand-edited history whose buckets exceed the prompt total can't drive the
|
|
115
|
+
// session total negative — a wrong-but-plausible number is worse than a
|
|
116
|
+
// conservative one here, since this feeds a cost display.
|
|
117
|
+
into.uncachedInputTokens += Math.max(0, (m.inputTokens ?? 0) - cacheRead - cacheWrite);
|
|
118
|
+
into.outputTokens += m.outputTokens ?? 0;
|
|
119
|
+
|
|
120
|
+
// Spend banked by a compaction on this summary's behalf. NOTE: deliberately
|
|
121
|
+
// not `compaction.tokensBefore` — that is a one-turn context-window reading,
|
|
122
|
+
// not cumulative spend, and counting it here would inflate the total.
|
|
123
|
+
const rolled = m.compaction?.rolledUpUsage;
|
|
124
|
+
if (rolled) addInto(into, rolled);
|
|
125
|
+
|
|
126
|
+
for (const tc of m.toolCalls ?? []) {
|
|
127
|
+
if (tc.subAgentTrace) accumulate(tc.subAgentTrace, into);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|