@ai-launchpad-v1/expert-code 0.2.4 → 0.2.5
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/index.js +230 -44
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -102,15 +102,18 @@ async function* generateStream({
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
async function generateAgentTurn({ system, messages, tools, maxTokens, onTextDelta }, override) {
|
|
105
|
+
async function generateAgentTurn({ system, messages, tools, maxTokens, onTextDelta, effort }, override) {
|
|
106
106
|
const c2 = override?.apiKey ? new Anthropic({ apiKey: override.apiKey }) : client;
|
|
107
|
-
const
|
|
108
|
-
|
|
107
|
+
const model = override?.model || MODEL;
|
|
108
|
+
const params = {
|
|
109
|
+
model,
|
|
109
110
|
max_tokens: maxTokens,
|
|
110
111
|
system: systemBlocks(system),
|
|
111
112
|
messages: withTranscriptCache(messages),
|
|
112
113
|
tools
|
|
113
|
-
}
|
|
114
|
+
};
|
|
115
|
+
if (effort) params.output_config = { effort };
|
|
116
|
+
const stream = c2.messages.stream(params);
|
|
114
117
|
for await (const event of stream) {
|
|
115
118
|
if (event.type === "content_block_delta" && event.delta.type === "text_delta") {
|
|
116
119
|
onTextDelta?.(event.delta.text);
|
|
@@ -125,7 +128,16 @@ async function generateAgentTurn({ system, messages, tools, maxTokens, onTextDel
|
|
|
125
128
|
toolUses.push({ id: block.id, name: block.name, input: block.input });
|
|
126
129
|
}
|
|
127
130
|
}
|
|
128
|
-
return { text, toolUses, stopReason: response.stop_reason };
|
|
131
|
+
return { text, toolUses, stopReason: response.stop_reason, usage: readUsage(response.usage), model };
|
|
132
|
+
}
|
|
133
|
+
function readUsage(u) {
|
|
134
|
+
if (!u) return void 0;
|
|
135
|
+
return {
|
|
136
|
+
inputTokens: u.input_tokens ?? 0,
|
|
137
|
+
outputTokens: u.output_tokens ?? 0,
|
|
138
|
+
cacheReadTokens: u.cache_read_input_tokens ?? 0,
|
|
139
|
+
cacheWriteTokens: u.cache_creation_input_tokens ?? 0
|
|
140
|
+
};
|
|
129
141
|
}
|
|
130
142
|
var client, MODEL;
|
|
131
143
|
var init_anthropic = __esm({
|
|
@@ -139,6 +151,19 @@ var init_anthropic = __esm({
|
|
|
139
151
|
});
|
|
140
152
|
|
|
141
153
|
// ../lib/providers/_openaiCompat.ts
|
|
154
|
+
function readOpenAiUsage(u) {
|
|
155
|
+
if (!u || typeof u !== "object") return void 0;
|
|
156
|
+
const usage = u;
|
|
157
|
+
if (typeof usage.prompt_tokens !== "number" && typeof usage.completion_tokens !== "number") return void 0;
|
|
158
|
+
const prompt = usage.prompt_tokens ?? 0;
|
|
159
|
+
const cached = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
160
|
+
return {
|
|
161
|
+
inputTokens: Math.max(0, prompt - cached),
|
|
162
|
+
outputTokens: usage.completion_tokens ?? 0,
|
|
163
|
+
cacheReadTokens: cached,
|
|
164
|
+
cacheWriteTokens: 0
|
|
165
|
+
};
|
|
166
|
+
}
|
|
142
167
|
function buildOpenAiUserMessage(prompt, attachments) {
|
|
143
168
|
if (attachments.length === 0) return { role: "user", content: prompt };
|
|
144
169
|
const parts = [];
|
|
@@ -223,15 +248,23 @@ async function agentTurnOpenAICompat(client9, model, { system, messages, tools,
|
|
|
223
248
|
max_tokens: maxTokens,
|
|
224
249
|
messages: toOpenAiMessages(system, messages),
|
|
225
250
|
tools: toOpenAiTools(tools),
|
|
226
|
-
stream: true
|
|
251
|
+
stream: true,
|
|
252
|
+
// Ask the gateway to emit a final usage-only chunk so we get real token counts
|
|
253
|
+
// (incl. cached-prompt tokens) instead of estimating. Providers that don't
|
|
254
|
+
// support it simply omit usage, and the client falls back to its estimate.
|
|
255
|
+
stream_options: { include_usage: true }
|
|
227
256
|
});
|
|
228
257
|
let text = "";
|
|
229
258
|
const toolAcc = /* @__PURE__ */ new Map();
|
|
230
259
|
let finishReason = null;
|
|
231
260
|
let sawChunk = false;
|
|
232
261
|
let gatewayError;
|
|
262
|
+
let usage;
|
|
233
263
|
for await (const chunk of stream) {
|
|
234
264
|
sawChunk = true;
|
|
265
|
+
const chunkUsage = chunk.usage ?? chunk.x_groq?.usage;
|
|
266
|
+
const parsed = readOpenAiUsage(chunkUsage);
|
|
267
|
+
if (parsed) usage = parsed;
|
|
235
268
|
const choice = chunk.choices?.[0];
|
|
236
269
|
const err = chunk.error;
|
|
237
270
|
if (err?.message) gatewayError = err.message;
|
|
@@ -265,7 +298,7 @@ async function agentTurnOpenAICompat(client9, model, { system, messages, tools,
|
|
|
265
298
|
}
|
|
266
299
|
toolUses.push({ id: acc.id, name: acc.name, input });
|
|
267
300
|
}
|
|
268
|
-
return { text, toolUses, stopReason: finishReason };
|
|
301
|
+
return { text, toolUses, stopReason: finishReason, usage, model };
|
|
269
302
|
}
|
|
270
303
|
var init_openaiCompat = __esm({
|
|
271
304
|
"../lib/providers/_openaiCompat.ts"() {
|
|
@@ -336,6 +369,18 @@ __export(gemini_exports, {
|
|
|
336
369
|
generateStream: () => generateStream3
|
|
337
370
|
});
|
|
338
371
|
import { GoogleGenerativeAI } from "@google/generative-ai";
|
|
372
|
+
function readGeminiUsage(meta) {
|
|
373
|
+
if (!meta || typeof meta !== "object") return void 0;
|
|
374
|
+
const m = meta;
|
|
375
|
+
if (typeof m.promptTokenCount !== "number" && typeof m.candidatesTokenCount !== "number") return void 0;
|
|
376
|
+
const cached = m.cachedContentTokenCount ?? 0;
|
|
377
|
+
return {
|
|
378
|
+
inputTokens: Math.max(0, (m.promptTokenCount ?? 0) - cached),
|
|
379
|
+
outputTokens: m.candidatesTokenCount ?? 0,
|
|
380
|
+
cacheReadTokens: cached,
|
|
381
|
+
cacheWriteTokens: 0
|
|
382
|
+
};
|
|
383
|
+
}
|
|
339
384
|
function buildUserParts(prompt, attachments) {
|
|
340
385
|
if (attachments.length === 0) return prompt;
|
|
341
386
|
const parts = [];
|
|
@@ -432,8 +477,11 @@ async function generateAgentTurn3({ system, messages, tools, maxTokens, onTextDe
|
|
|
432
477
|
let text = "";
|
|
433
478
|
const toolUses = [];
|
|
434
479
|
let stopReason = null;
|
|
480
|
+
let usage;
|
|
435
481
|
let i = 0;
|
|
436
482
|
for await (const chunk of result.stream) {
|
|
483
|
+
const parsed = readGeminiUsage(chunk.usageMetadata);
|
|
484
|
+
if (parsed) usage = parsed;
|
|
437
485
|
const candidate = chunk.candidates?.[0];
|
|
438
486
|
for (const part of candidate?.content?.parts ?? []) {
|
|
439
487
|
if (part.text) {
|
|
@@ -450,7 +498,7 @@ async function generateAgentTurn3({ system, messages, tools, maxTokens, onTextDe
|
|
|
450
498
|
}
|
|
451
499
|
if (candidate?.finishReason) stopReason = candidate.finishReason;
|
|
452
500
|
}
|
|
453
|
-
return { text, toolUses, stopReason };
|
|
501
|
+
return { text, toolUses, stopReason, usage, model: modelName };
|
|
454
502
|
}
|
|
455
503
|
var client3, MODEL3;
|
|
456
504
|
var init_gemini = __esm({
|
|
@@ -699,11 +747,13 @@ var init_zai = __esm({
|
|
|
699
747
|
// ../lib/ai.ts
|
|
700
748
|
var ai_exports = {};
|
|
701
749
|
__export(ai_exports, {
|
|
750
|
+
AGENT_EFFORTS: () => AGENT_EFFORTS,
|
|
702
751
|
UnsupportedAttachmentError: () => UnsupportedAttachmentError,
|
|
703
752
|
formatTextDocument: () => formatTextDocument,
|
|
704
753
|
generateAgentTurn: () => generateAgentTurn8,
|
|
705
754
|
generateStream: () => generateStream8,
|
|
706
|
-
generateText: () => generateText
|
|
755
|
+
generateText: () => generateText,
|
|
756
|
+
isAgentEffort: () => isAgentEffort
|
|
707
757
|
});
|
|
708
758
|
function formatTextDocument(a) {
|
|
709
759
|
const text = Buffer.from(a.data, "base64").toString("utf-8");
|
|
@@ -714,6 +764,9 @@ function formatTextDocument(a) {
|
|
|
714
764
|
${text}
|
|
715
765
|
\`\`\``;
|
|
716
766
|
}
|
|
767
|
+
function isAgentEffort(v) {
|
|
768
|
+
return typeof v === "string" && AGENT_EFFORTS.includes(v);
|
|
769
|
+
}
|
|
717
770
|
function getProvider() {
|
|
718
771
|
const p = (process.env.AI_PROVIDER ?? "anthropic").toLowerCase();
|
|
719
772
|
if (!SUPPORTED.includes(p)) {
|
|
@@ -866,7 +919,7 @@ async function* generateStream8(options) {
|
|
|
866
919
|
}
|
|
867
920
|
}
|
|
868
921
|
}
|
|
869
|
-
var UnsupportedAttachmentError, SUPPORTED, FEATURE_ENV_PREFIX;
|
|
922
|
+
var UnsupportedAttachmentError, AGENT_EFFORTS, SUPPORTED, FEATURE_ENV_PREFIX;
|
|
870
923
|
var init_ai = __esm({
|
|
871
924
|
"../lib/ai.ts"() {
|
|
872
925
|
"use strict";
|
|
@@ -877,6 +930,7 @@ var init_ai = __esm({
|
|
|
877
930
|
this.name = "UnsupportedAttachmentError";
|
|
878
931
|
}
|
|
879
932
|
};
|
|
933
|
+
AGENT_EFFORTS = ["low", "medium", "high", "xhigh", "max"];
|
|
880
934
|
SUPPORTED = ["anthropic", "openai", "gemini", "groq", "openrouter", "deepseek", "zai"];
|
|
881
935
|
FEATURE_ENV_PREFIX = {
|
|
882
936
|
code: "CODE",
|
|
@@ -1345,6 +1399,10 @@ import { homedir } from "os";
|
|
|
1345
1399
|
import { join } from "path";
|
|
1346
1400
|
import { existsSync, readFileSync, writeFileSync, mkdirSync, chmodSync } from "fs";
|
|
1347
1401
|
var PROVIDERS = ["anthropic", "openai", "gemini", "groq", "openrouter", "deepseek", "zai"];
|
|
1402
|
+
var EFFORTS = ["low", "medium", "high", "xhigh", "max"];
|
|
1403
|
+
function isEffort(v) {
|
|
1404
|
+
return typeof v === "string" && EFFORTS.includes(v);
|
|
1405
|
+
}
|
|
1348
1406
|
var KEY_ENV = {
|
|
1349
1407
|
anthropic: "ANTHROPIC_API_KEY",
|
|
1350
1408
|
openai: "OPENAI_API_KEY",
|
|
@@ -1425,6 +1483,10 @@ function resolveModel(p, cfg) {
|
|
|
1425
1483
|
function resolveApiBase(cfg) {
|
|
1426
1484
|
return (process.env.EXPERT_API_BASE || cfg.apiBase || DEFAULT_API_BASE).replace(/\/+$/, "");
|
|
1427
1485
|
}
|
|
1486
|
+
function resolveEffort(cfg) {
|
|
1487
|
+
const v = (process.env.EXPERT_EFFORT || cfg.effort || "").toLowerCase();
|
|
1488
|
+
return isEffort(v) ? v : void 0;
|
|
1489
|
+
}
|
|
1428
1490
|
function isSettingsTrusted(root, hash) {
|
|
1429
1491
|
return loadConfig().trustedSettings?.[root] === hash;
|
|
1430
1492
|
}
|
|
@@ -2038,7 +2100,7 @@ var ApiError = class extends Error {
|
|
|
2038
2100
|
this.status = status;
|
|
2039
2101
|
}
|
|
2040
2102
|
};
|
|
2041
|
-
function createByokClient(label) {
|
|
2103
|
+
function createByokClient(label, effort) {
|
|
2042
2104
|
return {
|
|
2043
2105
|
label,
|
|
2044
2106
|
async runTurn(messages, onTextDelta) {
|
|
@@ -2046,10 +2108,10 @@ function createByokClient(label) {
|
|
|
2046
2108
|
const { CODE_SYSTEM_PROMPT: CODE_SYSTEM_PROMPT2 } = await Promise.resolve().then(() => (init_prompts(), prompts_exports));
|
|
2047
2109
|
const { CODE_TOOLS: CODE_TOOLS2 } = await Promise.resolve().then(() => (init_codeTools(), codeTools_exports));
|
|
2048
2110
|
const turn = await generateAgentTurn9(
|
|
2049
|
-
{ system: CODE_SYSTEM_PROMPT2, messages, tools: CODE_TOOLS2, maxTokens: 4096, onTextDelta },
|
|
2111
|
+
{ system: CODE_SYSTEM_PROMPT2, messages, tools: CODE_TOOLS2, maxTokens: 4096, onTextDelta, effort },
|
|
2050
2112
|
"code"
|
|
2051
2113
|
);
|
|
2052
|
-
return { text: turn.text, toolUses: turn.toolUses, stopReason: turn.stopReason };
|
|
2114
|
+
return { text: turn.text, toolUses: turn.toolUses, stopReason: turn.stopReason, usage: turn.usage, model: turn.model };
|
|
2053
2115
|
}
|
|
2054
2116
|
};
|
|
2055
2117
|
}
|
|
@@ -2096,6 +2158,8 @@ async function parseNdjsonTurn(body, onTextDelta) {
|
|
|
2096
2158
|
let text = "";
|
|
2097
2159
|
let toolUses = [];
|
|
2098
2160
|
let stopReason = null;
|
|
2161
|
+
let usage;
|
|
2162
|
+
let model;
|
|
2099
2163
|
let done = false;
|
|
2100
2164
|
const handle = (line2) => {
|
|
2101
2165
|
const trimmed = line2.trim();
|
|
@@ -2112,6 +2176,8 @@ async function parseNdjsonTurn(body, onTextDelta) {
|
|
|
2112
2176
|
} else if (evt.type === "done") {
|
|
2113
2177
|
toolUses = Array.isArray(evt.toolUses) ? evt.toolUses : [];
|
|
2114
2178
|
stopReason = evt.stopReason ?? null;
|
|
2179
|
+
usage = evt.usage;
|
|
2180
|
+
model = evt.model;
|
|
2115
2181
|
done = true;
|
|
2116
2182
|
} else if (evt.type === "error") {
|
|
2117
2183
|
throw new ApiError(evt.error || "The coding agent failed.", 0);
|
|
@@ -2128,7 +2194,7 @@ async function parseNdjsonTurn(body, onTextDelta) {
|
|
|
2128
2194
|
}
|
|
2129
2195
|
if (buf) handle(buf);
|
|
2130
2196
|
if (!done) throw new ApiError("The server closed the connection unexpectedly.", 0);
|
|
2131
|
-
return { text, toolUses, stopReason };
|
|
2197
|
+
return { text, toolUses, stopReason, usage, model };
|
|
2132
2198
|
}
|
|
2133
2199
|
|
|
2134
2200
|
// auth.ts
|
|
@@ -2919,6 +2985,55 @@ function humanizeTokens(n) {
|
|
|
2919
2985
|
if (n < 1e6) return `${(n / 1e3).toFixed(n < 1e4 ? 1 : 0)}k`;
|
|
2920
2986
|
return `${(n / 1e6).toFixed(1)}M`;
|
|
2921
2987
|
}
|
|
2988
|
+
var ZERO_USAGE = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, cacheWriteTokens: 0 };
|
|
2989
|
+
function addUsage(a, b) {
|
|
2990
|
+
return {
|
|
2991
|
+
inputTokens: a.inputTokens + b.inputTokens,
|
|
2992
|
+
outputTokens: a.outputTokens + b.outputTokens,
|
|
2993
|
+
cacheReadTokens: a.cacheReadTokens + b.cacheReadTokens,
|
|
2994
|
+
cacheWriteTokens: a.cacheWriteTokens + b.cacheWriteTokens
|
|
2995
|
+
};
|
|
2996
|
+
}
|
|
2997
|
+
function totalTokens(u) {
|
|
2998
|
+
return u.inputTokens + u.cacheReadTokens + u.cacheWriteTokens + u.outputTokens;
|
|
2999
|
+
}
|
|
3000
|
+
function cacheHitPct(u) {
|
|
3001
|
+
const prompt = u.inputTokens + u.cacheReadTokens;
|
|
3002
|
+
if (prompt <= 0) return null;
|
|
3003
|
+
return Math.round(u.cacheReadTokens / prompt * 100);
|
|
3004
|
+
}
|
|
3005
|
+
var PRICES = [
|
|
3006
|
+
["claude-opus-5-5", { input: 4, output: 20, cacheRead: 0.2, cacheWrite: 5 }],
|
|
3007
|
+
["claude-opus-5", { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 }],
|
|
3008
|
+
["claude-opus-4", { input: 5, output: 25, cacheRead: 0.5, cacheWrite: 6.25 }],
|
|
3009
|
+
["claude-sonnet-5-5", { input: 2, output: 10, cacheRead: 0.2, cacheWrite: 2.5 }],
|
|
3010
|
+
["claude-sonnet-5", { input: 2, output: 10, cacheRead: 0.2, cacheWrite: 2.5 }],
|
|
3011
|
+
["claude-sonnet-4", { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 }],
|
|
3012
|
+
["claude-haiku-5", { input: 0.1, output: 0.5, cacheRead: 0.02, cacheWrite: 0.125 }],
|
|
3013
|
+
["claude-haiku-4", { input: 1, output: 5, cacheRead: 0.1, cacheWrite: 1.25 }],
|
|
3014
|
+
["gpt-4o", { input: 2.5, output: 10, cacheRead: 1.25, cacheWrite: 0 }],
|
|
3015
|
+
["gemini-2.5-flash", { input: 0.3, output: 2.5, cacheRead: 0.075, cacheWrite: 0 }],
|
|
3016
|
+
["gemini-2.0-flash", { input: 0.1, output: 0.4, cacheRead: 0.025, cacheWrite: 0 }],
|
|
3017
|
+
["gemini", { input: 0.15, output: 0.6, cacheRead: 0.0375, cacheWrite: 0 }],
|
|
3018
|
+
["deepseek", { input: 0.28, output: 0.42, cacheRead: 0.028, cacheWrite: 0 }],
|
|
3019
|
+
["glm", { input: 0.6, output: 2.2, cacheRead: 0.11, cacheWrite: 0 }]
|
|
3020
|
+
];
|
|
3021
|
+
function priceFor(model) {
|
|
3022
|
+
if (!model) return null;
|
|
3023
|
+
const id = model.toLowerCase();
|
|
3024
|
+
for (const [key, price] of PRICES) if (id.includes(key)) return price;
|
|
3025
|
+
return null;
|
|
3026
|
+
}
|
|
3027
|
+
function costUsd(u, model) {
|
|
3028
|
+
const p = priceFor(model);
|
|
3029
|
+
if (!p) return null;
|
|
3030
|
+
return (u.inputTokens * p.input + u.outputTokens * p.output + u.cacheReadTokens * p.cacheRead + u.cacheWriteTokens * p.cacheWrite) / 1e6;
|
|
3031
|
+
}
|
|
3032
|
+
function formatUsd(n) {
|
|
3033
|
+
if (n <= 0) return "$0.00";
|
|
3034
|
+
if (n < 0.01) return "<$0.01";
|
|
3035
|
+
return `$${n.toFixed(2)}`;
|
|
3036
|
+
}
|
|
2922
3037
|
|
|
2923
3038
|
// agent.ts
|
|
2924
3039
|
var STEP_ATTEMPTS = 2;
|
|
@@ -3000,11 +3115,18 @@ async function runAgentTask(transcript, callbacks, signal, model, skills) {
|
|
|
3000
3115
|
}
|
|
3001
3116
|
if (signal.aborted) return { transcript: messages, finalText, stopped: "aborted" };
|
|
3002
3117
|
if (callbacks.onTurnUsage) {
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
|
|
3007
|
-
|
|
3118
|
+
if (turn.usage) {
|
|
3119
|
+
callbacks.onTurnUsage({ ...turn.usage, estimated: false, model: turn.model });
|
|
3120
|
+
} else {
|
|
3121
|
+
const outputText = turn.text + turn.toolUses.map((t) => JSON.stringify(t.input ?? "")).join("");
|
|
3122
|
+
callbacks.onTurnUsage({
|
|
3123
|
+
inputTokens: estimateMessagesTokens(messages),
|
|
3124
|
+
outputTokens: estimateTokens(outputText),
|
|
3125
|
+
cacheReadTokens: 0,
|
|
3126
|
+
cacheWriteTokens: 0,
|
|
3127
|
+
estimated: true
|
|
3128
|
+
});
|
|
3129
|
+
}
|
|
3008
3130
|
}
|
|
3009
3131
|
if (turn.toolUses.length > MAX_TOOL_USES_PER_TURN) {
|
|
3010
3132
|
return {
|
|
@@ -3107,10 +3229,17 @@ async function runAgentTask(transcript, callbacks, signal, model, skills) {
|
|
|
3107
3229
|
if (signal.aborted) return { transcript: messages, finalText, stopped: "aborted" };
|
|
3108
3230
|
if (sum.text.trim()) {
|
|
3109
3231
|
if (callbacks.onTurnUsage) {
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3232
|
+
if (sum.usage) {
|
|
3233
|
+
callbacks.onTurnUsage({ ...sum.usage, estimated: false, model: sum.model });
|
|
3234
|
+
} else {
|
|
3235
|
+
callbacks.onTurnUsage({
|
|
3236
|
+
inputTokens: estimateMessagesTokens(probe),
|
|
3237
|
+
outputTokens: estimateTokens(sum.text),
|
|
3238
|
+
cacheReadTokens: 0,
|
|
3239
|
+
cacheWriteTokens: 0,
|
|
3240
|
+
estimated: true
|
|
3241
|
+
});
|
|
3242
|
+
}
|
|
3114
3243
|
}
|
|
3115
3244
|
messages = rebuildWithSummary(messages, sum.text);
|
|
3116
3245
|
lastCompactStep = step;
|
|
@@ -3558,6 +3687,7 @@ var SLASH_COMMANDS = [
|
|
|
3558
3687
|
"/login",
|
|
3559
3688
|
"/logout",
|
|
3560
3689
|
"/model",
|
|
3690
|
+
"/effort",
|
|
3561
3691
|
"/diff",
|
|
3562
3692
|
"/revert",
|
|
3563
3693
|
"/status",
|
|
@@ -3568,8 +3698,15 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3568
3698
|
const alwaysAllow = /* @__PURE__ */ new Set();
|
|
3569
3699
|
const changes = [];
|
|
3570
3700
|
let pendingNext = null;
|
|
3571
|
-
let
|
|
3572
|
-
let
|
|
3701
|
+
let taskUsage = { ...ZERO_USAGE };
|
|
3702
|
+
let sessionUsage = { ...ZERO_USAGE };
|
|
3703
|
+
let usageEstimated = true;
|
|
3704
|
+
let lastModel;
|
|
3705
|
+
const usageLabel = (u) => {
|
|
3706
|
+
const tok = `${usageEstimated ? "~" : ""}${humanizeTokens(totalTokens(u))} tok`;
|
|
3707
|
+
const cost = usageEstimated ? null : costUsd(u, lastModel);
|
|
3708
|
+
return cost != null ? `${tok} \xB7 ${formatUsd(cost)}` : tok;
|
|
3709
|
+
};
|
|
3573
3710
|
const completer = (lineStr) => {
|
|
3574
3711
|
if (lineStr === "" && pendingNext) return [[pendingNext], ""];
|
|
3575
3712
|
if (lineStr.startsWith("/")) {
|
|
@@ -3666,7 +3803,7 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3666
3803
|
}
|
|
3667
3804
|
let transcript = [];
|
|
3668
3805
|
for (; ; ) {
|
|
3669
|
-
promptTop(
|
|
3806
|
+
promptTop(totalTokens(sessionUsage) > 0 ? `${mode} \xB7 ${usageLabel(sessionUsage)}` : mode);
|
|
3670
3807
|
const input = (await ask(PROMPT)).trim();
|
|
3671
3808
|
promptBottom();
|
|
3672
3809
|
pendingNext = null;
|
|
@@ -3802,9 +3939,10 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3802
3939
|
spinner.start("working");
|
|
3803
3940
|
},
|
|
3804
3941
|
onTurnUsage: (u) => {
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3942
|
+
taskUsage = addUsage(taskUsage, u);
|
|
3943
|
+
sessionUsage = addUsage(sessionUsage, u);
|
|
3944
|
+
usageEstimated = u.estimated;
|
|
3945
|
+
if (u.model) lastModel = u.model;
|
|
3808
3946
|
}
|
|
3809
3947
|
},
|
|
3810
3948
|
abort.signal,
|
|
@@ -3839,11 +3977,11 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3839
3977
|
}
|
|
3840
3978
|
line();
|
|
3841
3979
|
}
|
|
3842
|
-
if (
|
|
3843
|
-
|
|
3844
|
-
|
|
3845
|
-
`
|
|
3846
|
-
|
|
3980
|
+
if (totalTokens(taskUsage) > 0) {
|
|
3981
|
+
const hit = usageEstimated ? null : cacheHitPct(taskUsage);
|
|
3982
|
+
const cached = hit != null ? ` \xB7 ${hit}% cached` : "";
|
|
3983
|
+
notice(`tokens ${usageLabel(taskUsage)}${cached} task \xB7 ${usageLabel(sessionUsage)} session
|
|
3984
|
+
`);
|
|
3847
3985
|
}
|
|
3848
3986
|
}
|
|
3849
3987
|
function pathOf(tool) {
|
|
@@ -3890,7 +4028,8 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3890
4028
|
case "/clear":
|
|
3891
4029
|
transcript = [];
|
|
3892
4030
|
changes.length = 0;
|
|
3893
|
-
|
|
4031
|
+
taskUsage = { ...ZERO_USAGE };
|
|
4032
|
+
usageEstimated = true;
|
|
3894
4033
|
clearScreen();
|
|
3895
4034
|
banner(workspace.root, workspace.name, lane.client.label, mode);
|
|
3896
4035
|
notice("Started a fresh task (screen cleared).\n");
|
|
@@ -3916,6 +4055,9 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3916
4055
|
case "/model":
|
|
3917
4056
|
doModel(rest);
|
|
3918
4057
|
return true;
|
|
4058
|
+
case "/effort":
|
|
4059
|
+
doEffort(rest);
|
|
4060
|
+
return true;
|
|
3919
4061
|
default:
|
|
3920
4062
|
return false;
|
|
3921
4063
|
}
|
|
@@ -3927,7 +4069,10 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3927
4069
|
notice(`autonomy ${mode}`);
|
|
3928
4070
|
notice(`changes ${changes.length} applied this task`);
|
|
3929
4071
|
notice(`context ~${humanizeTokens(estimateMessagesTokens(transcript))} tokens in the conversation`);
|
|
3930
|
-
|
|
4072
|
+
const hit = usageEstimated ? null : cacheHitPct(taskUsage);
|
|
4073
|
+
notice(
|
|
4074
|
+
`tokens ${usageLabel(taskUsage)}${hit != null ? ` \xB7 ${hit}% cached` : ""} task \xB7 ${usageLabel(sessionUsage)} session${usageEstimated ? " (estimated)" : ""}`
|
|
4075
|
+
);
|
|
3931
4076
|
line();
|
|
3932
4077
|
}
|
|
3933
4078
|
function showDiff() {
|
|
@@ -3977,6 +4122,7 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3977
4122
|
line(c.bold("\n Compacting the conversation\u2026\n"));
|
|
3978
4123
|
spinner.start("summarizing");
|
|
3979
4124
|
let summary = "";
|
|
4125
|
+
let compactUsage = null;
|
|
3980
4126
|
try {
|
|
3981
4127
|
const turn = await lane.client.runTurn(
|
|
3982
4128
|
probe,
|
|
@@ -3988,6 +4134,11 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
3988
4134
|
abort.signal
|
|
3989
4135
|
);
|
|
3990
4136
|
summary = turn.text || summary;
|
|
4137
|
+
if (turn.usage) {
|
|
4138
|
+
compactUsage = turn.usage;
|
|
4139
|
+
if (turn.model) lastModel = turn.model;
|
|
4140
|
+
usageEstimated = false;
|
|
4141
|
+
}
|
|
3991
4142
|
} catch (e) {
|
|
3992
4143
|
spinner.stop();
|
|
3993
4144
|
activeAbort = null;
|
|
@@ -4009,13 +4160,29 @@ async function runRepl({ lane, workspace, initialMode, apiBase, getToken }) {
|
|
|
4009
4160
|
}
|
|
4010
4161
|
transcript = rebuildWithSummary(transcript, summary, 0);
|
|
4011
4162
|
const after = estimateMessagesTokens(transcript);
|
|
4012
|
-
const used =
|
|
4013
|
-
|
|
4014
|
-
|
|
4163
|
+
const used = compactUsage ?? {
|
|
4164
|
+
inputTokens: estimateMessagesTokens(probe),
|
|
4165
|
+
outputTokens: estimateTokens(summary),
|
|
4166
|
+
cacheReadTokens: 0,
|
|
4167
|
+
cacheWriteTokens: 0
|
|
4168
|
+
};
|
|
4169
|
+
taskUsage = addUsage(taskUsage, used);
|
|
4170
|
+
sessionUsage = addUsage(sessionUsage, used);
|
|
4015
4171
|
notice(`
|
|
4016
4172
|
Compacted: context ~${humanizeTokens(before)} \u2192 ~${humanizeTokens(after)} tokens.
|
|
4017
4173
|
`);
|
|
4018
4174
|
}
|
|
4175
|
+
function doEffort(rest) {
|
|
4176
|
+
const level = (rest[0] ?? "").toLowerCase();
|
|
4177
|
+
try {
|
|
4178
|
+
const label = lane.setEffort(level || void 0);
|
|
4179
|
+
notice(`${label}
|
|
4180
|
+
`);
|
|
4181
|
+
} catch (e) {
|
|
4182
|
+
line(c.red(` ${e instanceof Error ? e.message : "Could not set effort."}
|
|
4183
|
+
`));
|
|
4184
|
+
}
|
|
4185
|
+
}
|
|
4019
4186
|
async function doLogin() {
|
|
4020
4187
|
if (!lane.managedAvailable()) {
|
|
4021
4188
|
notice("Managed sign-in is not available in this build. Use BYOK (`expert setup`).\n");
|
|
@@ -4170,7 +4337,7 @@ async function forceUpdate(current) {
|
|
|
4170
4337
|
}
|
|
4171
4338
|
|
|
4172
4339
|
// index.ts
|
|
4173
|
-
var VERSION = true ? "0.2.
|
|
4340
|
+
var VERSION = true ? "0.2.5" : "dev";
|
|
4174
4341
|
function withReadline(fn) {
|
|
4175
4342
|
const rl = readline2.createInterface({ input: process.stdin, output: process.stdout });
|
|
4176
4343
|
const ask = (q) => new Promise((res) => rl.question(q, (a) => res(a)));
|
|
@@ -4202,11 +4369,12 @@ function resolveModelClient(cfg) {
|
|
|
4202
4369
|
printMissingKey(provider);
|
|
4203
4370
|
return null;
|
|
4204
4371
|
}
|
|
4205
|
-
return createByokClient(byokLabel(provider, cfg));
|
|
4372
|
+
return createByokClient(byokLabel(provider, cfg), resolveEffort(cfg));
|
|
4206
4373
|
}
|
|
4207
4374
|
function byokLabel(provider, cfg) {
|
|
4208
4375
|
const model = resolveModel(provider, cfg);
|
|
4209
|
-
|
|
4376
|
+
const effort = provider === "anthropic" ? resolveEffort(cfg) : void 0;
|
|
4377
|
+
return `${provider}${model ? ` (${model})` : " (provider default)"}${effort ? ` \xB7 ${effort} effort` : ""}`;
|
|
4210
4378
|
}
|
|
4211
4379
|
function createLaneController(cfg, initial) {
|
|
4212
4380
|
let client9 = initial;
|
|
@@ -4228,7 +4396,7 @@ function createLaneController(cfg, initial) {
|
|
|
4228
4396
|
if (!resolveKey(provider, cfg)) {
|
|
4229
4397
|
throw new Error("Signed out, but no BYOK key is configured. Run `expert setup`.");
|
|
4230
4398
|
}
|
|
4231
|
-
client9 = createByokClient(byokLabel(provider, cfg));
|
|
4399
|
+
client9 = createByokClient(byokLabel(provider, cfg), resolveEffort(cfg));
|
|
4232
4400
|
return client9.label;
|
|
4233
4401
|
},
|
|
4234
4402
|
setProvider(providerArg, modelArg) {
|
|
@@ -4250,9 +4418,27 @@ function createLaneController(cfg, initial) {
|
|
|
4250
4418
|
process.env[`CODE_${KEY_ENV[p]}`] = key;
|
|
4251
4419
|
const model = modelArg || resolveModel(p, cfg);
|
|
4252
4420
|
if (model) process.env[`CODE_${MODEL_ENV[p]}`] = model;
|
|
4253
|
-
client9 = createByokClient(byokLabel(p, cfg));
|
|
4421
|
+
client9 = createByokClient(byokLabel(p, cfg), resolveEffort(cfg));
|
|
4254
4422
|
return client9.label;
|
|
4255
4423
|
},
|
|
4424
|
+
setEffort(effortArg) {
|
|
4425
|
+
if (isLoggedIn(cfg) && chosenLane(cfg) === "managed") {
|
|
4426
|
+
throw new Error("Effort is set on the backend for the managed lane. It applies to your own key (BYOK) \u2014 sign out or use BYOK to control it here.");
|
|
4427
|
+
}
|
|
4428
|
+
const provider = resolveProvider(cfg);
|
|
4429
|
+
if (provider !== "anthropic") {
|
|
4430
|
+
throw new Error(`Effort only applies to Anthropic; the current provider is ${provider}.`);
|
|
4431
|
+
}
|
|
4432
|
+
if (effortArg && !isEffort(effortArg)) {
|
|
4433
|
+
throw new Error(`Unknown effort "${effortArg}". One of: ${EFFORTS.join(", ")} (or omit to reset to default).`);
|
|
4434
|
+
}
|
|
4435
|
+
cfg.effort = isEffort(effortArg) ? effortArg : void 0;
|
|
4436
|
+
if (cfg.effort) process.env.EXPERT_EFFORT = cfg.effort;
|
|
4437
|
+
else delete process.env.EXPERT_EFFORT;
|
|
4438
|
+
saveConfig(cfg);
|
|
4439
|
+
client9 = createByokClient(byokLabel(provider, cfg), resolveEffort(cfg));
|
|
4440
|
+
return cfg.effort ? `Effort set to ${cfg.effort}. Now using ${client9.label}.` : `Effort reset to the provider default. Now using ${client9.label}.`;
|
|
4441
|
+
},
|
|
4256
4442
|
status() {
|
|
4257
4443
|
const lines = [`current model ${client9.label}`];
|
|
4258
4444
|
const provider = resolveProvider(cfg);
|
package/package.json
CHANGED