This commit is contained in:
wassname
2026-04-21 18:09:09 +08:00
parent 5da34c33a2
commit c28436503f
4 changed files with 108 additions and 5 deletions
+39
View File
@@ -84,6 +84,45 @@ function buildContextSummary(
return `${percent}/${formatTokens(contextWindow)}`;
}
export interface TurnCostInfo {
input: number;
output: number;
cacheRead: number;
cost: number;
}
/**
* Extract per-turn cost/token info from the agent_end messages array.
* Returns undefined if no assistant messages with usage found.
*/
export function extractTurnCost(messages: Array<{ role?: string; usage?: { input: number; output: number; cacheRead: number; cost: { total: number } } }>): TurnCostInfo | undefined {
let input = 0, output = 0, cacheRead = 0, cost = 0;
let found = false;
for (const msg of messages) {
if (msg.role === "assistant" && msg.usage) {
input += msg.usage.input;
output += msg.usage.output;
cacheRead += msg.usage.cacheRead;
cost += msg.usage.cost.total;
found = true;
}
}
return found ? { input, output, cacheRead, cost } : undefined;
}
/**
* Format turn cost as a one-line summary for appending to trace replies.
*/
export function formatTurnCostLine(turnCost: TurnCostInfo, contextPercent: number | null): string {
const parts: string[] = [];
parts.push(`$${turnCost.cost.toFixed(3)}`);
const tokens = [`${formatTokens(turnCost.input)}`, `${formatTokens(turnCost.output)}`];
if (turnCost.cacheRead) tokens.push(`R${formatTokens(turnCost.cacheRead)}`);
parts.push(tokens.join(" "));
if (contextPercent !== null) parts.push(`ctx ${contextPercent.toFixed(0)}%`);
return parts.join(" | ");
}
export function buildStatusHtml(
ctx: ExtensionContext,
activeModel: Model<any> | undefined,
+3 -2
View File
@@ -65,8 +65,9 @@ export function buildTelegramTurnPrompt(options: {
files: DownloadedTelegramTurnFileLike[];
historyTurns?: Pick<PendingTelegramTurn, "historyText">[];
}): string {
// Let pi handle `!` shell commands natively - don't prepend [telegram] prefix
let prompt = options.rawText.trimStart().startsWith("!") ? "" : options.telegramPrefix;
// Let pi handle `!` shell commands and `/` slash commands natively - don't prepend [telegram] prefix
const raw = options.rawText.trimStart();
let prompt = (raw.startsWith("!") || raw.startsWith("/")) ? "" : options.telegramPrefix;
if ((options.historyTurns?.length ?? 0) > 0) {
prompt +=
"\n\nEarlier Telegram messages arrived after an aborted turn. Treat them as prior user messages, in order:";