@columenlabs/lumen-ai 0.1.273 → 0.1.275
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 +3 -1
- package/dist/agent/agent.d.ts +5 -0
- package/dist/agent/agent.js +11 -1
- package/dist/agent/usage.d.ts +3 -1
- package/dist/agent/usage.js +4 -1
- package/dist/assistant/engine.d.ts +21 -0
- package/dist/assistant/engine.js +50 -5
- package/dist/assistant/models.d.ts +31 -0
- package/dist/assistant/models.js +33 -0
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -117,7 +117,9 @@ app.all('/api/assistant/*', (c) => routes(c.req.raw));
|
|
|
117
117
|
- **Hand off and advice.** The `escalate` tool hands one task to a higher tier (the person sets next, any or only when asked); Anthropic's advisor tool lets a higher tier send a plan, at most two an answer. A hand-off gives up after `HAND_OFF_MS` (4 minutes) and the turn goes on without it.
|
|
118
118
|
- **Chats** run on the agent (`./agent`, `LumenAgent`, below): the app routes `POST /chat` and each chat's reads, cards and Undo to its own agent, and `assistantRoutes` answers `POST /chat` with 410 `moved`. Chats saved by the old turn loop stay readable (`GET /chats`, `GET|DELETE /chats/:id`) until `assistant.purge()` drops them 30 days after their last message.
|
|
119
119
|
- **Screen moves**: while the person's Settings ▸ Screen is Move (the default), the panel sends its page map (`AssistantMessage.screen`: routes, nav pages, Settings sections, cards on screen, rows to open) and `LumenAgent` gives the model a `screen` tool (`./agent` `screen.ts`). Each call's moves (go, settings, chart, open, point) are checked against the map, kept in the chat and streamed as a `screen` event; the panel runs them through `AssistantScreenProvider` (mounted by `LumenShell`) with an Undo chip, and their outcomes go back with the next message (`outcomes`). Nothing is saved by a move.
|
|
120
|
-
- **Background work** an app runs for a person (a score, a summary) is `assistant.complete(caller, { task, system, prompt, images?, maxTokens })`: one answer on the task's tier (`tasks`, so the person picks its model in Settings ▸ Models), no chat and no tools (`images`, base64 with their media type, are read with the prompt: a scanned page, a screenshot), billed to their payer and counted in their spend and allowance like a chat turn. `maxTokens` (default 1,000) goes up to `COMPLETE_MAX_TOKENS` (32,000, thinking included); the answer is streamed, so a long one keeps its connection, and `cut: true` says it stopped at the cap before it ended (the app can ask again, shorter, or continue). It throws `AssistantError` `not_connected`, `app_cap` or `own_cap` when nobody can pay, so the caller waits and tries later. It returns the answer's `usd`, `model`, `usage` (tokens in, cache reads, out, per model) and `payer` (`app` or `own`: who paid, so the app can show a cost on the person's own key as money and the rest as credits). `effort` sets how hard the model thinks on that one call (`low` to `max`; on an OpenAI or xAI model it is the request's reasoning effort), over what the person saved for the tier. `run: { id, title }` makes the calls that share an `id` one line of the person's usage history (every round of one rebuild); without it a task's calls that day share a line. The month's spend also adds up per task and model (`spend:YYYY-MM` `tasks`: answers, calls, tokens, cost and time, a chat answer as `chat`), so an app's admin page can show what each kind of work costs; `tag` counts one call under another name when a task does more than one kind of work (a first try, a repair). **Prompt cache:**
|
|
120
|
+
- **Background work** an app runs for a person (a score, a summary) is `assistant.complete(caller, { task, system, prompt, images?, maxTokens })`: one answer on the task's tier (`tasks`, so the person picks its model in Settings ▸ Models), no chat and no tools (`images`, base64 with their media type, are read with the prompt: a scanned page, a screenshot), billed to their payer and counted in their spend and allowance like a chat turn. `maxTokens` (default 1,000) goes up to `COMPLETE_MAX_TOKENS` (32,000, thinking included); the answer is streamed, so a long one keeps its connection, and `cut: true` says it stopped at the cap before it ended (the app can ask again, shorter, or continue). It throws `AssistantError` `not_connected`, `app_cap` or `own_cap` when nobody can pay, so the caller waits and tries later. It returns the answer's `usd`, `model`, `usage` (tokens in, cache reads, out, per model) and `payer` (`app` or `own`: who paid, so the app can show a cost on the person's own key as money and the rest as credits). `effort` sets how hard the model thinks on that one call (`low` to `max`; on an OpenAI or xAI model it is the request's reasoning effort), over what the person saved for the tier. `run: { id, title }` makes the calls that share an `id` one line of the person's usage history (every round of one rebuild); without it a task's calls that day share a line. The month's spend also adds up per task and model (`spend:YYYY-MM` `tasks`: answers, calls, tokens, cost and time, a chat answer as `chat`), so an app's admin page can show what each kind of work costs; `tag` counts one call under another name when a task does more than one kind of work (a first try, a repair). **Prompt cache:** a provider's cache reuses whole earlier messages, not text added inside the same message (OpenAI read 0% of a prompt that only gained a part, and 99.7% when the same words came as a new message). So a job of several steps (a study that asks for more pictures, then a build) passes `history`: the conversation so far as `{ role, text, images? }` messages, oldest first, ending with the model's answer, and `prompt` and `images` as the newest message; each call then pays only for what is new. `prompt` may also be a list of parts (Claude marks every part but the last), which helps only an exact repeat on OpenAI. OpenAI calls carry a cache key per person, task and run (a ChatGPT plan sign-in caches nothing without one).
|
|
121
|
+
|
|
122
|
+
**Call log:** every model call (chat steps, hand-offs, compaction, `complete`) writes one JSON line on the console, `{ event: 'ai.call', model, label, task, run, in, read, written, out, hit, usd, free }`: tokens in (cache included), cache reads and writes, out, the share read from cache (`hit`), the cost at list price, and `free` for a plan sign-in. Workers Logs keeps it, so an app sees each call's cache use with no wiring. `setCallLog(fn)` sends the records elsewhere; `setCallLog(null)` turns it off.
|
|
121
123
|
- **Images** an app makes for a person are `assistant.image(caller, { task, prompt, images?, size?, quality?, run? })`: one image on the payer's OpenAI key (`IMAGE_MODEL`, gpt-image-2; a ChatGPT plan sign-in cannot make one), from `images` when given (a reference, a sketch), billed and capped like `complete` at the model's list price per token. It returns the PNG as base64 with its `usd`. A chat tool that returns `image: { url, alt?, cost? }` shows as an image card in the panel (`StepItem.image`); `LumenAssistant` `imageTool` names that tool (its "Making the image" box while it runs, Make another) and `modes` adds composer toggles (Make an image) whose id goes with the message as `use`.
|
|
122
124
|
- **Usage history.** Each chat (`agentTurn` with `chat`) and each run of background work is one line of the person's month (`usage:YYYY-MM`, the newest 300): its title, first and last use, tokens, cost and its parts per tier and model. `GET /usage?month=YYYY-MM` returns it beside that month's spend and the months that have any; `LumenAssistant`'s footer shows this month's total and opens it (`UsageHistoryView`).
|
|
123
125
|
|
package/dist/agent/agent.d.ts
CHANGED
|
@@ -23,6 +23,11 @@ import { UsageMeter } from './usage.js';
|
|
|
23
23
|
/** What a plan's extra tools get: the open tools, a meter of their own and the cap. */
|
|
24
24
|
export interface TurnTools {
|
|
25
25
|
opened: OpenedTools;
|
|
26
|
+
/**
|
|
27
|
+
* The agent's own file tools over its workspace (read, list, find, grep and the rest, as Think
|
|
28
|
+
* gives the main model), so a hand-off reads the same files the app wrote for this turn.
|
|
29
|
+
*/
|
|
30
|
+
files: ToolSet;
|
|
26
31
|
/** A new part of this answer's usage (a hand off), counted against the cap. */
|
|
27
32
|
meter(model: ModelDef, label: string, free?: boolean): UsageMeter;
|
|
28
33
|
/** Spent this answer reached what the payer has left. */
|
package/dist/agent/agent.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* An `Assistant` gives both: `assistant.agentTurn(caller, input)` returns a plan and its settle.
|
|
13
13
|
*/
|
|
14
14
|
import { anthropic } from '@ai-sdk/anthropic';
|
|
15
|
+
import { createAITools } from '@cloudflare/computer/tools/ai-sdk';
|
|
15
16
|
import { Think, } from '@cloudflare/think';
|
|
16
17
|
import { ASSISTANT_IMAGES_MAX, ASSISTANT_IMAGE_CHARS, ASSISTANT_IMAGE_TYPES, ASSISTANT_QUOTE_MAX, } from '@columenlabs/lumen-ui-types/assistant';
|
|
17
18
|
import { createCompactFunction } from 'agents/sessions';
|
|
@@ -631,6 +632,7 @@ export class LumenAgent extends Think {
|
|
|
631
632
|
throw new Error('No model to compact with until the next answer.');
|
|
632
633
|
const { plan } = last;
|
|
633
634
|
let meter = new UsageMeter(plan.model, `${plan.label} · compact`, !!plan.key.plan);
|
|
635
|
+
meter.ctx = { task: 'chat', run: this.name };
|
|
634
636
|
this.#owed.push(meter);
|
|
635
637
|
const r = await generateText({
|
|
636
638
|
maxRetries: 0,
|
|
@@ -639,6 +641,7 @@ export class LumenAgent extends Think {
|
|
|
639
641
|
if (!fb)
|
|
640
642
|
return;
|
|
641
643
|
meter = new UsageMeter(fb.model, `${fb.label} · compact`, false);
|
|
644
|
+
meter.ctx = { task: 'chat', run: this.name };
|
|
642
645
|
this.#owed.push(meter);
|
|
643
646
|
}),
|
|
644
647
|
prompt,
|
|
@@ -683,14 +686,18 @@ export class LumenAgent extends Think {
|
|
|
683
686
|
...plan.tools,
|
|
684
687
|
});
|
|
685
688
|
const free = !!plan.key.plan;
|
|
689
|
+
const call = { task: 'chat', run: this.name };
|
|
686
690
|
const main = new UsageMeter(plan.model, plan.label, free);
|
|
691
|
+
main.ctx = call;
|
|
687
692
|
const meters = [main, ...this.#owed];
|
|
688
693
|
this.#owed = [];
|
|
689
694
|
const advice = plan.advisor
|
|
690
695
|
? new UsageMeter(plan.advisor.model, plan.advisor.label, free)
|
|
691
696
|
: undefined;
|
|
692
|
-
if (advice)
|
|
697
|
+
if (advice) {
|
|
698
|
+
advice.ctx = call;
|
|
693
699
|
meters.push(advice);
|
|
700
|
+
}
|
|
694
701
|
let system = typeof plan.system === 'string' ? plan.system : plan.system(opened);
|
|
695
702
|
this.#keepTitles(opened.info);
|
|
696
703
|
this.#turn = {
|
|
@@ -706,9 +713,11 @@ export class LumenAgent extends Think {
|
|
|
706
713
|
};
|
|
707
714
|
const more = plan.more?.({
|
|
708
715
|
opened,
|
|
716
|
+
files: createAITools({ workspace: this.workspace }),
|
|
709
717
|
over: () => plan.left !== undefined && this.#spent() >= plan.left,
|
|
710
718
|
meter: (model, label, f = free) => {
|
|
711
719
|
const m = new UsageMeter(model, label, f);
|
|
720
|
+
m.ctx = call;
|
|
712
721
|
meters.push(m);
|
|
713
722
|
return m;
|
|
714
723
|
},
|
|
@@ -767,6 +776,7 @@ export class LumenAgent extends Think {
|
|
|
767
776
|
return;
|
|
768
777
|
// Claude answers the rest of the turn on the payer's key: its use is its own part.
|
|
769
778
|
turn.main = new UsageMeter(fb.model, fb.label, false);
|
|
779
|
+
turn.main.ctx = { task: 'chat', run: this.name };
|
|
770
780
|
turn.meters.push(turn.main);
|
|
771
781
|
}),
|
|
772
782
|
// Think types instructions as a string; the SDK it hands them to takes a system message too.
|
package/dist/agent/usage.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** An AI SDK step's usage as the kit's tokens and list-price cost. */
|
|
2
2
|
import type { UsagePart } from '@columenlabs/lumen-ui-types/assistant';
|
|
3
3
|
import type { LanguageModelUsage } from 'ai';
|
|
4
|
-
import { type ModelDef, type TokenUsage } from '../assistant/models.js';
|
|
4
|
+
import { type CallContext, type ModelDef, type TokenUsage } from '../assistant/models.js';
|
|
5
5
|
/**
|
|
6
6
|
* One call's usage in the kit's terms. Input splits into plain, cache writes and cache reads where
|
|
7
7
|
* the provider says (Claude reports all three; OpenAI reports reads, its writes cost nothing extra).
|
|
@@ -18,6 +18,8 @@ export declare class UsageMeter {
|
|
|
18
18
|
readonly model: ModelDef;
|
|
19
19
|
private readonly free;
|
|
20
20
|
private p;
|
|
21
|
+
/** What the calls are for, on each call's log line (`logCall`). */
|
|
22
|
+
ctx?: CallContext;
|
|
21
23
|
constructor(model: ModelDef, label: string, free?: boolean);
|
|
22
24
|
add(u: LanguageModelUsage): {
|
|
23
25
|
in: number;
|
package/dist/agent/usage.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { priced } from '../assistant/models.js';
|
|
1
|
+
import { logCall, priced, } from '../assistant/models.js';
|
|
2
2
|
/**
|
|
3
3
|
* One call's usage in the kit's terms. Input splits into plain, cache writes and cache reads where
|
|
4
4
|
* the provider says (Claude reports all three; OpenAI reports reads, its writes cost nothing extra).
|
|
@@ -21,6 +21,8 @@ export class UsageMeter {
|
|
|
21
21
|
model;
|
|
22
22
|
free;
|
|
23
23
|
p;
|
|
24
|
+
/** What the calls are for, on each call's log line (`logCall`). */
|
|
25
|
+
ctx;
|
|
24
26
|
constructor(model, label, free = false) {
|
|
25
27
|
this.model = model;
|
|
26
28
|
this.free = free;
|
|
@@ -32,6 +34,7 @@ export class UsageMeter {
|
|
|
32
34
|
/** Tokens a provider reports apart from the step's usage (Claude's advisor calls). */
|
|
33
35
|
addTokens(u) {
|
|
34
36
|
const t = priced(this.model, u);
|
|
37
|
+
logCall(this.model, this.p.label, u, t, this.free, this.ctx);
|
|
35
38
|
this.p.calls += 1;
|
|
36
39
|
this.p.in += t.in;
|
|
37
40
|
this.p.read += t.read;
|
|
@@ -36,6 +36,20 @@ export interface CompleteImage {
|
|
|
36
36
|
mediaType: string;
|
|
37
37
|
data: string;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* One earlier message of a `complete` conversation: what was asked (with its own photos) or what the
|
|
41
|
+
* model answered. Sent as real messages, in order, before the newest prompt: a provider's prompt
|
|
42
|
+
* cache reuses whole earlier messages, so a conversation that only grows at its end is read from
|
|
43
|
+
* the cache at a tenth of the price. Text added inside one message is not (OpenAI, checked
|
|
44
|
+
* 2026-11-14: 0 of 3,889 tokens read when a part was added to the same message, 9,563 of 9,592
|
|
45
|
+
* when the same words came as a new message).
|
|
46
|
+
*/
|
|
47
|
+
export interface CompleteTurn {
|
|
48
|
+
role: 'user' | 'assistant';
|
|
49
|
+
text: string;
|
|
50
|
+
/** A user message's photos, sent before its text. */
|
|
51
|
+
images?: CompleteImage[];
|
|
52
|
+
}
|
|
39
53
|
export interface Caller {
|
|
40
54
|
id: string;
|
|
41
55
|
admin: boolean;
|
|
@@ -407,6 +421,13 @@ export declare class Assistant {
|
|
|
407
421
|
prompt: string | string[];
|
|
408
422
|
/** Photos read with the prompt (a scanned page, a screenshot), sent before it and cached. */
|
|
409
423
|
images?: CompleteImage[];
|
|
424
|
+
/**
|
|
425
|
+
* The conversation so far, oldest first, alternating user and assistant and ending with the
|
|
426
|
+
* model's answer; `prompt` and `images` are the newest message. Each earlier message is read
|
|
427
|
+
* from the prompt cache on the next call, so a multi-step job (a study that asks for more
|
|
428
|
+
* pictures, then a build) re-pays only what is new.
|
|
429
|
+
*/
|
|
430
|
+
history?: CompleteTurn[];
|
|
410
431
|
/** The answer's token cap, thinking included (default 1,000, at most COMPLETE_MAX_TOKENS). */
|
|
411
432
|
maxTokens?: number;
|
|
412
433
|
/**
|
package/dist/assistant/engine.js
CHANGED
|
@@ -38,6 +38,34 @@ export const COMPLETE_MAX_TOKENS = 32_000;
|
|
|
38
38
|
/** A turn's failure the person can act on. */
|
|
39
39
|
/** Claude's prompt cache mark on a block: the prompt up to it is kept for the next call. */
|
|
40
40
|
const CACHE_MARK = { type: 'ephemeral' };
|
|
41
|
+
/**
|
|
42
|
+
* A `complete` conversation's earlier messages for Claude, the last block marked so the next call
|
|
43
|
+
* reads the whole conversation so far from the cache.
|
|
44
|
+
*/
|
|
45
|
+
function claudeHistory(history) {
|
|
46
|
+
return history.map((t, n) => {
|
|
47
|
+
const last = n === history.length - 1;
|
|
48
|
+
if (t.role === 'assistant')
|
|
49
|
+
return {
|
|
50
|
+
role: 'assistant',
|
|
51
|
+
content: [{ type: 'text', text: t.text, ...(last ? { cache_control: CACHE_MARK } : {}) }],
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
role: 'user',
|
|
55
|
+
content: [
|
|
56
|
+
...(t.images ?? []).map((i) => ({
|
|
57
|
+
type: 'image',
|
|
58
|
+
source: {
|
|
59
|
+
type: 'base64',
|
|
60
|
+
media_type: i.mediaType,
|
|
61
|
+
data: i.data,
|
|
62
|
+
},
|
|
63
|
+
})),
|
|
64
|
+
{ type: 'text', text: t.text, ...(last ? { cache_control: CACHE_MARK } : {}) },
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}
|
|
41
69
|
/** OpenAI's prompt cache key for one person's task or run: a short, stable hash of `of`. */
|
|
42
70
|
function cacheKey(of) {
|
|
43
71
|
let h = 0x811c9dc5;
|
|
@@ -645,7 +673,7 @@ export class Assistant {
|
|
|
645
673
|
}),
|
|
646
674
|
instructions: c.system(tt.opened),
|
|
647
675
|
prompt: `${c.message}\n\n${this.name} handed you this task (${WHY_TEXT[shown]}): ${task}\n\nWork it out with the tools, then end with a memo for ${this.name} of at most 200 words: the answer and the facts it rests on. ${this.name} answers the person.`,
|
|
648
|
-
tools: tt.opened.tools,
|
|
676
|
+
tools: { ...tt.files, ...tt.opened.tools },
|
|
649
677
|
stopWhen: stepCountIs(MAX_ROUNDS),
|
|
650
678
|
prepareStep: () => (tt.over() ? { activeTools: [] } : undefined),
|
|
651
679
|
onStepFinish: (s) => {
|
|
@@ -1132,7 +1160,7 @@ export class Assistant {
|
|
|
1132
1160
|
* events like the chat loop, because a ChatGPT plan sign-in answers only streamed calls, takes no
|
|
1133
1161
|
* output cap and may send its words only as deltas.
|
|
1134
1162
|
*/
|
|
1135
|
-
async openaiOnce(clients, s, instructions, input, max, signal, images, effort, cacheKey) {
|
|
1163
|
+
async openaiOnce(clients, s, instructions, input, max, signal, images, effort, cacheKey, history = []) {
|
|
1136
1164
|
// xAI's API is OpenAI's Responses API at another address.
|
|
1137
1165
|
const xai = s.model.provider === 'xai';
|
|
1138
1166
|
const client = (xai ? clients.xai : clients.openai);
|
|
@@ -1144,8 +1172,24 @@ export class Assistant {
|
|
|
1144
1172
|
model: s.model.id,
|
|
1145
1173
|
instructions,
|
|
1146
1174
|
// A plan sign-in takes only a list of items, never bare text ("Input must be a list").
|
|
1147
|
-
//
|
|
1175
|
+
// The earlier messages as they were, then the newest: pictures first, then its text parts.
|
|
1148
1176
|
input: [
|
|
1177
|
+
...history.map((t) => t.role === 'assistant'
|
|
1178
|
+
? {
|
|
1179
|
+
role: 'assistant',
|
|
1180
|
+
content: [{ type: 'output_text', text: t.text }],
|
|
1181
|
+
}
|
|
1182
|
+
: {
|
|
1183
|
+
role: 'user',
|
|
1184
|
+
content: [
|
|
1185
|
+
...(t.images ?? []).map((i) => ({
|
|
1186
|
+
type: 'input_image',
|
|
1187
|
+
image_url: `data:${i.mediaType};base64,${i.data}`,
|
|
1188
|
+
detail: 'auto',
|
|
1189
|
+
})),
|
|
1190
|
+
{ type: 'input_text', text: t.text },
|
|
1191
|
+
],
|
|
1192
|
+
}),
|
|
1149
1193
|
{
|
|
1150
1194
|
role: 'user',
|
|
1151
1195
|
content: [
|
|
@@ -1217,6 +1261,7 @@ export class Assistant {
|
|
|
1217
1261
|
const began = Date.now();
|
|
1218
1262
|
const s = await this.session(caller, await this.mainTier(caller, o.task));
|
|
1219
1263
|
const { rules, free } = await this.rulesFor(caller, s.tier.id, o.payer);
|
|
1264
|
+
s.meter.ctx = { task: o.task, ...(o.run && { run: o.run.id }) };
|
|
1220
1265
|
const { paying, wait } = await this.state.paying(caller.id, s.model.provider, rules);
|
|
1221
1266
|
if (!paying) {
|
|
1222
1267
|
const code = wait ?? 'not_connected';
|
|
@@ -1231,7 +1276,7 @@ export class Assistant {
|
|
|
1231
1276
|
try {
|
|
1232
1277
|
if ((s.model.provider === 'openai' && clients.openai) ||
|
|
1233
1278
|
(s.model.provider === 'xai' && clients.xai)) {
|
|
1234
|
-
({ text, cut } = await this.openaiOnce(clients, s, o.system, parts, max, o.signal, o.images, o.effort, cacheKey(`${caller.id}:${o.task}:${o.run?.id ?? ''}`)));
|
|
1279
|
+
({ text, cut } = await this.openaiOnce(clients, s, o.system, parts, max, o.signal, o.images, o.effort, cacheKey(`${caller.id}:${o.task}:${o.run?.id ?? ''}`), o.history));
|
|
1235
1280
|
}
|
|
1236
1281
|
else if (clients.anthropic) {
|
|
1237
1282
|
// Streamed and read whole: a long answer (minutes at a high cap) keeps its connection alive,
|
|
@@ -1260,7 +1305,7 @@ export class Assistant {
|
|
|
1260
1305
|
model: s.model.id,
|
|
1261
1306
|
max_tokens: max,
|
|
1262
1307
|
system: [{ type: 'text', text: o.system, cache_control: CACHE_MARK }],
|
|
1263
|
-
messages: [{ role: 'user', content }],
|
|
1308
|
+
messages: [...claudeHistory(o.history ?? []), { role: 'user', content }],
|
|
1264
1309
|
...effortParams(o.effort ?? s.effort),
|
|
1265
1310
|
}, o.signal ? { signal: o.signal } : undefined)
|
|
1266
1311
|
.finalMessage();
|
|
@@ -73,6 +73,35 @@ export declare function priced(model: ModelDef, usage: TokenUsage): {
|
|
|
73
73
|
read: number;
|
|
74
74
|
usd: number;
|
|
75
75
|
};
|
|
76
|
+
/** What a model call was for, as far as the code that made it knows: its task and its run (a chat, a rebuild). */
|
|
77
|
+
export interface CallContext {
|
|
78
|
+
task?: string;
|
|
79
|
+
run?: string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* One model call, as every app logs it: the model, the part's label (`Tier · Model`), tokens in (cache
|
|
83
|
+
* reads and writes included), cache reads, cache writes and tokens out, the share of the input read
|
|
84
|
+
* from cache (`hit`, 0 to 1), the cost at list price and whether a plan sign-in ran it (`free`).
|
|
85
|
+
*/
|
|
86
|
+
export interface CallRecord extends CallContext {
|
|
87
|
+
event: 'ai.call';
|
|
88
|
+
model: string;
|
|
89
|
+
label: string;
|
|
90
|
+
in: number;
|
|
91
|
+
read: number;
|
|
92
|
+
written: number;
|
|
93
|
+
out: number;
|
|
94
|
+
hit: number;
|
|
95
|
+
usd: number;
|
|
96
|
+
free: boolean;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Where each model call's record goes. By default one JSON line on the console (Workers Logs keeps
|
|
100
|
+
* it), so every app sees each call's tokens and cache reads with no wiring. `null` turns it off.
|
|
101
|
+
*/
|
|
102
|
+
export declare function setCallLog(fn: ((r: CallRecord) => void) | null): void;
|
|
103
|
+
/** Logs one priced call. Never throws: a log that fails never fails the call. */
|
|
104
|
+
export declare function logCall(model: ModelDef, label: string, usage: TokenUsage, p: ReturnType<typeof priced>, free: boolean, ctx?: CallContext): void;
|
|
76
105
|
/** The running use of one tier in one answer. */
|
|
77
106
|
export declare class Meter {
|
|
78
107
|
readonly model: ModelDef;
|
|
@@ -82,6 +111,8 @@ export declare class Meter {
|
|
|
82
111
|
read: number;
|
|
83
112
|
out: number;
|
|
84
113
|
usd: number;
|
|
114
|
+
/** What the calls are for, on each call's log line. */
|
|
115
|
+
ctx?: CallContext;
|
|
85
116
|
constructor(model: ModelDef, label: string);
|
|
86
117
|
/** `free`: the call ran on a plan sign-in, so it counts tokens and no dollars. */
|
|
87
118
|
add(usage: TokenUsage, free?: boolean): void;
|
package/dist/assistant/models.js
CHANGED
|
@@ -101,6 +101,36 @@ export function priced(model, usage) {
|
|
|
101
101
|
1e6,
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
+
let callLog = (r) => console.log(JSON.stringify(r));
|
|
105
|
+
/**
|
|
106
|
+
* Where each model call's record goes. By default one JSON line on the console (Workers Logs keeps
|
|
107
|
+
* it), so every app sees each call's tokens and cache reads with no wiring. `null` turns it off.
|
|
108
|
+
*/
|
|
109
|
+
export function setCallLog(fn) {
|
|
110
|
+
callLog = fn;
|
|
111
|
+
}
|
|
112
|
+
/** Logs one priced call. Never throws: a log that fails never fails the call. */
|
|
113
|
+
export function logCall(model, label, usage, p, free, ctx) {
|
|
114
|
+
try {
|
|
115
|
+
callLog?.({
|
|
116
|
+
event: 'ai.call',
|
|
117
|
+
model: model.id,
|
|
118
|
+
label,
|
|
119
|
+
...(ctx?.task && { task: ctx.task }),
|
|
120
|
+
...(ctx?.run && { run: ctx.run }),
|
|
121
|
+
in: p.in,
|
|
122
|
+
read: p.read,
|
|
123
|
+
written: usage.cache_creation_input_tokens ?? 0,
|
|
124
|
+
out: p.out,
|
|
125
|
+
hit: p.in ? Math.round((p.read / p.in) * 1000) / 1000 : 0,
|
|
126
|
+
usd: free ? 0 : Math.round(p.usd * 1e6) / 1e6,
|
|
127
|
+
free,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch {
|
|
131
|
+
// A log is never worth a failed call.
|
|
132
|
+
}
|
|
133
|
+
}
|
|
104
134
|
/** The running use of one tier in one answer. */
|
|
105
135
|
export class Meter {
|
|
106
136
|
model;
|
|
@@ -110,6 +140,8 @@ export class Meter {
|
|
|
110
140
|
read = 0;
|
|
111
141
|
out = 0;
|
|
112
142
|
usd = 0;
|
|
143
|
+
/** What the calls are for, on each call's log line. */
|
|
144
|
+
ctx;
|
|
113
145
|
constructor(model, label) {
|
|
114
146
|
this.model = model;
|
|
115
147
|
this.label = label;
|
|
@@ -117,6 +149,7 @@ export class Meter {
|
|
|
117
149
|
/** `free`: the call ran on a plan sign-in, so it counts tokens and no dollars. */
|
|
118
150
|
add(usage, free = false) {
|
|
119
151
|
const p = priced(this.model, usage);
|
|
152
|
+
logCall(this.model, this.label, usage, p, free, this.ctx);
|
|
120
153
|
this.calls += 1;
|
|
121
154
|
this.in += p.in;
|
|
122
155
|
this.read += p.read;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@columenlabs/lumen-ai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.275",
|
|
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",
|
|
@@ -55,9 +55,10 @@
|
|
|
55
55
|
"@ai-sdk/anthropic": "4.0.96",
|
|
56
56
|
"@ai-sdk/openai": "4.0.108",
|
|
57
57
|
"@anthropic-ai/sdk": "0.133.0",
|
|
58
|
+
"@cloudflare/computer": "0.8.0",
|
|
58
59
|
"@cloudflare/think": "0.23.0",
|
|
59
|
-
"@columenlabs/lumen-ops": "0.1.
|
|
60
|
-
"@columenlabs/lumen-ui-types": "0.1.
|
|
60
|
+
"@columenlabs/lumen-ops": "0.1.275",
|
|
61
|
+
"@columenlabs/lumen-ui-types": "0.1.275",
|
|
61
62
|
"agents": "0.32.0",
|
|
62
63
|
"ai": "7.0.162",
|
|
63
64
|
"just-bash": "3.6.0",
|