agent-loft 0.2.61 → 0.2.62
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/package.json +1 -1
- package/src/agents/claude/session.ts +18 -4
- package/src/agents/codex/session.ts +15 -10
- package/src/core/task-schedule.ts +1 -1
- package/web/css/tasks.css +11 -9
- package/web/views/tasks.js +18 -9
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
type SDKUserMessage,
|
|
18
18
|
} from "@anthropic-ai/claude-agent-sdk";
|
|
19
19
|
import { agentEnvironment } from "../../core/agent-env.js";
|
|
20
|
+
import { usageOf } from "../../core/usage.js";
|
|
20
21
|
import { expertAppend, expertSystemPrompt, RICH_REPLY_LINES, searchLabel } from "../../core/expert-prompt.js";
|
|
21
22
|
import { ACCESS_LEVELS, type ExpertAccess } from "../../core/experts.js";
|
|
22
23
|
import { MEMORY_MCP_NAME, memoryMcpSpawn } from "../../core/memory-mcp.js";
|
|
@@ -313,6 +314,8 @@ export class ClaudeSession implements AgentSession {
|
|
|
313
314
|
* 恢复 / 分叉来的会话第一轮不知道之前累计了多少,这一轮先不报费用
|
|
314
315
|
*/
|
|
315
316
|
private costSoFar: number | undefined;
|
|
317
|
+
/** 上一轮的累计 Token;恢复 / 分叉时优先从已保存的会话用量建立起点。 */
|
|
318
|
+
private tokensSoFar: number | undefined;
|
|
316
319
|
/** 任务清单(见 todos.ts)。只记主线的,子 agent 自己列的不算 */
|
|
317
320
|
private todos = new TodoTracker();
|
|
318
321
|
|
|
@@ -322,6 +325,10 @@ export class ClaudeSession implements AgentSession {
|
|
|
322
325
|
private readonly host: InteractionHost,
|
|
323
326
|
) {
|
|
324
327
|
this.costSoFar = opts.resumeId ? undefined : 0;
|
|
328
|
+
const previousUsage = usageOf(opts.resumeId);
|
|
329
|
+
this.tokensSoFar = previousUsage
|
|
330
|
+
? previousUsage.input + previousUsage.cacheRead + previousUsage.cacheWrite + previousUsage.output
|
|
331
|
+
: opts.resumeId ? undefined : 0;
|
|
325
332
|
// 接着老会话说:之前列的任务后面可能接着改(TaskUpdate 只给编号),先读回来。
|
|
326
333
|
// 读一个本地文件,比模型第一次调工具快得多
|
|
327
334
|
if (opts.resumeId) void readTodos(opts.resumeId, opts.cwd).then((items) => this.todos.seed(items));
|
|
@@ -449,11 +456,12 @@ export class ClaudeSession implements AgentSession {
|
|
|
449
456
|
|
|
450
457
|
if (message.type === "result") {
|
|
451
458
|
const costUsd = this.turnCost(message.total_cost_usd);
|
|
452
|
-
this.reportUsage(message.modelUsage);
|
|
459
|
+
const tokens = this.reportUsage(message.modelUsage);
|
|
453
460
|
if (message.subtype === "success") {
|
|
454
461
|
this.events.onResult({
|
|
455
462
|
text: message.result,
|
|
456
463
|
...(costUsd !== undefined ? { costUsd } : {}),
|
|
464
|
+
...(tokens !== undefined ? { tokens } : {}),
|
|
457
465
|
durationMs: message.duration_ms,
|
|
458
466
|
...(this.lastAssistantUuid ? { anchor: this.lastAssistantUuid } : {}),
|
|
459
467
|
});
|
|
@@ -477,7 +485,7 @@ export class ClaudeSession implements AgentSession {
|
|
|
477
485
|
* 报这个会话到目前的累计用量。用 modelUsage 而不是 usage:usage 只有主循环这一轮的,
|
|
478
486
|
* modelUsage 含子 agent、压缩等所有调用,而且是累计值(恢复的会话接着原来的总数算)
|
|
479
487
|
*/
|
|
480
|
-
private reportUsage(modelUsage: Record<string, ModelUsage> | undefined):
|
|
488
|
+
private reportUsage(modelUsage: Record<string, ModelUsage> | undefined): number | undefined {
|
|
481
489
|
const sum: TokenUsage = { input: 0, cacheRead: 0, cacheWrite: 0, output: 0, reasoning: 0, costUsd: 0 };
|
|
482
490
|
for (const m of Object.values(modelUsage ?? {})) {
|
|
483
491
|
sum.input += m.inputTokens || 0;
|
|
@@ -487,8 +495,14 @@ export class ClaudeSession implements AgentSession {
|
|
|
487
495
|
sum.reasoning! += m.thinkingTokens || 0;
|
|
488
496
|
sum.costUsd! += m.costUSD || 0;
|
|
489
497
|
}
|
|
490
|
-
|
|
491
|
-
|
|
498
|
+
const total = sum.input + sum.cacheRead + sum.cacheWrite + sum.output;
|
|
499
|
+
// 崩溃时的 result 全是 0,不覆盖上一轮的起点。
|
|
500
|
+
if (!(total > 0)) return undefined;
|
|
501
|
+
const prev = this.tokensSoFar;
|
|
502
|
+
this.tokensSoFar = total;
|
|
503
|
+
this.events.onUsage?.(sum, true);
|
|
504
|
+
// 没有历史起点或累计值倒退时,先建立起点,避免把旧用量当成本轮的。
|
|
505
|
+
return prev !== undefined && total >= prev ? total - prev : undefined;
|
|
492
506
|
}
|
|
493
507
|
|
|
494
508
|
/**
|
|
@@ -284,10 +284,11 @@ export class CodexSession implements AgentSession {
|
|
|
284
284
|
/** 用户点过「本会话全部允许」。只在内存里,会话对象一换就重新逐条询问。 */
|
|
285
285
|
private allowAll = false;
|
|
286
286
|
private replyText = "";
|
|
287
|
-
|
|
287
|
+
/** 本轮所有模型请求的用量,和会话汇总使用相同的输入 / 缓存 / 输出口径。 */
|
|
288
|
+
private turnTokens: number | undefined;
|
|
288
289
|
/** 上次看到的 thread 累计 token 数:变了才说明有新的模型请求(同一次请求可能报好几遍) */
|
|
289
290
|
private seenTotal: number | undefined;
|
|
290
|
-
/**
|
|
291
|
+
/** 当前轮次已开始:恢复 thread 时补报的旧用量不算新的消耗 */
|
|
291
292
|
private turnSeen = false;
|
|
292
293
|
/** 分叉已经真正完成(拿到并接管了新 thread)。失败了不算,重试还要接着分叉 */
|
|
293
294
|
private forked = false;
|
|
@@ -360,6 +361,7 @@ export class CodexSession implements AgentSession {
|
|
|
360
361
|
if (this.endedTurns.size > 50) this.endedTurns.delete(this.endedTurns.values().next().value!);
|
|
361
362
|
}
|
|
362
363
|
this.phase = "idle";
|
|
364
|
+
this.turnSeen = false;
|
|
363
365
|
this.turnId = undefined;
|
|
364
366
|
this.stopRequested = false;
|
|
365
367
|
this.turnSeq += 1;
|
|
@@ -554,6 +556,8 @@ export class CodexSession implements AgentSession {
|
|
|
554
556
|
private startTurn(input: UserInput): void {
|
|
555
557
|
this.currentInput = input;
|
|
556
558
|
this.replyText = "";
|
|
559
|
+
this.turnTokens = undefined;
|
|
560
|
+
this.turnSeen = false;
|
|
557
561
|
this.phase = "starting";
|
|
558
562
|
this.turnId = undefined;
|
|
559
563
|
this.stopRequested = false;
|
|
@@ -854,7 +858,7 @@ export class CodexSession implements AgentSession {
|
|
|
854
858
|
}
|
|
855
859
|
|
|
856
860
|
case "thread/tokenUsage/updated": {
|
|
857
|
-
// last 是最近一次模型请求的用量,total 是整条 thread
|
|
861
|
+
// last 是最近一次模型请求的用量,total 是整条 thread 的累计;去重后累加到本轮页脚和会话汇总
|
|
858
862
|
const usage = (params as { tokenUsage?: { total?: TokenBreakdown; last?: TokenBreakdown; modelContextWindow?: number | null } })
|
|
859
863
|
.tokenUsage;
|
|
860
864
|
const total = usage?.total?.totalTokens;
|
|
@@ -863,21 +867,22 @@ export class CodexSession implements AgentSession {
|
|
|
863
867
|
// OpenAI 的 inputTokens 含命中缓存的部分,拆出来
|
|
864
868
|
const cached = l.cachedInputTokens ?? 0;
|
|
865
869
|
const written = l.cacheWriteInputTokens ?? 0;
|
|
866
|
-
|
|
870
|
+
const delta = {
|
|
867
871
|
input: Math.max(0, (l.inputTokens ?? 0) - cached - written),
|
|
868
872
|
cacheRead: cached,
|
|
869
873
|
cacheWrite: written,
|
|
870
874
|
output: l.outputTokens ?? 0,
|
|
871
875
|
reasoning: l.reasoningOutputTokens ?? 0,
|
|
872
|
-
}
|
|
876
|
+
};
|
|
877
|
+
this.turnTokens = (this.turnTokens ?? 0) + delta.input + delta.cacheRead + delta.cacheWrite + delta.output;
|
|
878
|
+
this.events.onUsage?.(delta);
|
|
873
879
|
}
|
|
874
880
|
if (typeof total === "number") this.seenTotal = total;
|
|
875
|
-
const
|
|
876
|
-
if (typeof turnTokens === "number") this.lastTokens = turnTokens;
|
|
881
|
+
const requestTokens = usage?.last?.totalTokens;
|
|
877
882
|
// 最近一次请求的用量 ≈ 当前上下文大小(Codex 自己的「剩余上下文」也是这么算的)
|
|
878
883
|
const window = usage?.modelContextWindow;
|
|
879
|
-
if (typeof
|
|
880
|
-
this.events.onContext?.({ used:
|
|
884
|
+
if (typeof requestTokens === "number" && window) {
|
|
885
|
+
this.events.onContext?.({ used: requestTokens, window, percent: Math.round((requestTokens / window) * 100) });
|
|
881
886
|
}
|
|
882
887
|
return;
|
|
883
888
|
}
|
|
@@ -902,7 +907,7 @@ export class CodexSession implements AgentSession {
|
|
|
902
907
|
this.events.onResult({
|
|
903
908
|
text,
|
|
904
909
|
durationMs: turn.durationMs ?? 0,
|
|
905
|
-
...(this.
|
|
910
|
+
...(this.turnTokens !== undefined ? { tokens: this.turnTokens } : {}),
|
|
906
911
|
...(turn.id ? { anchor: turn.id } : {}),
|
|
907
912
|
});
|
|
908
913
|
}
|
|
@@ -228,7 +228,7 @@ export function describeSchedule(s: TaskSchedule): string {
|
|
|
228
228
|
case "weekdays":
|
|
229
229
|
return `工作日 ${s.time}`;
|
|
230
230
|
case "weekly":
|
|
231
|
-
return s.days.length === 7 ? `每天 ${s.time}` : `每周${s.days.map((d) => WEEK[d]).join("、")} ${s.time}`;
|
|
231
|
+
return s.days.length === 7 ? `每天 ${s.time}` : `每周${[...s.days].sort((a, b) => (a || 7) - (b || 7)).map((d) => WEEK[d]).join("、")} ${s.time}`;
|
|
232
232
|
case "monthly":
|
|
233
233
|
return `每月 ${s.day} 日 ${s.time}`;
|
|
234
234
|
case "hourly":
|
package/web/css/tasks.css
CHANGED
|
@@ -351,13 +351,7 @@ button.task-run:hover {
|
|
|
351
351
|
gap: 12px;
|
|
352
352
|
}
|
|
353
353
|
|
|
354
|
-
/*
|
|
355
|
-
.task-freq-row {
|
|
356
|
-
display: flex;
|
|
357
|
-
flex-direction: column;
|
|
358
|
-
gap: 8px;
|
|
359
|
-
}
|
|
360
|
-
|
|
354
|
+
/* 执行时间:选择星期和当天的时间 */
|
|
361
355
|
.task-freq {
|
|
362
356
|
display: flex;
|
|
363
357
|
flex-wrap: wrap;
|
|
@@ -391,13 +385,16 @@ button.task-run:hover {
|
|
|
391
385
|
|
|
392
386
|
.day-chips {
|
|
393
387
|
display: flex;
|
|
388
|
+
flex-wrap: wrap;
|
|
389
|
+
width: 100%;
|
|
394
390
|
gap: 4px;
|
|
395
391
|
}
|
|
396
392
|
|
|
397
393
|
.day-chip {
|
|
398
|
-
width:
|
|
394
|
+
min-width: 44px;
|
|
395
|
+
padding: 0 8px;
|
|
399
396
|
height: 34px;
|
|
400
|
-
border-radius:
|
|
397
|
+
border-radius: 8px;
|
|
401
398
|
border: 1px solid var(--border);
|
|
402
399
|
font-size: 13px;
|
|
403
400
|
}
|
|
@@ -408,6 +405,11 @@ button.task-run:hover {
|
|
|
408
405
|
color: var(--accent-fg);
|
|
409
406
|
}
|
|
410
407
|
|
|
408
|
+
.day-chip:focus-visible {
|
|
409
|
+
outline: 2px solid var(--accent);
|
|
410
|
+
outline-offset: 2px;
|
|
411
|
+
}
|
|
412
|
+
|
|
411
413
|
.task-static {
|
|
412
414
|
font-size: 14px;
|
|
413
415
|
color: var(--fg-soft);
|
package/web/views/tasks.js
CHANGED
|
@@ -29,7 +29,7 @@ const FREQS = [
|
|
|
29
29
|
["once", "一次"],
|
|
30
30
|
["daily", "每天"],
|
|
31
31
|
["weekdays", "工作日(周一到周五)"],
|
|
32
|
-
["weekly", "
|
|
32
|
+
["weekly", "每周(可多选)"],
|
|
33
33
|
["monthly", "每月"],
|
|
34
34
|
["hourly", "每隔几小时"],
|
|
35
35
|
["cron", "自定义(cron)"],
|
|
@@ -288,7 +288,8 @@ export function openTaskForm(task, preset) {
|
|
|
288
288
|
|
|
289
289
|
// ---- 频率 ----
|
|
290
290
|
const sch = { kind: "daily", time: "09:00", days: [1], day: 1, every: 2, minute: 0, expr: "0 9 * * *", at: Date.now() + 3600_000, ...(src.schedule ?? {}) };
|
|
291
|
-
if (sch.kind === "
|
|
291
|
+
if (sch.kind === "daily") sch.days = [...WEEK_ORDER];
|
|
292
|
+
else if (sch.kind === "weekdays") sch.days = [1, 2, 3, 4, 5];
|
|
292
293
|
const freq = h("select", {}, FREQS.map(([v, label]) => h("option", { value: v, selected: v === sch.kind }, label)));
|
|
293
294
|
const freqBox = h("div", { class: "task-freq" });
|
|
294
295
|
const timeInput = () => {
|
|
@@ -303,23 +304,25 @@ export function openTaskForm(task, preset) {
|
|
|
303
304
|
const at = h("input", { type: "datetime-local", value: toLocalInput(sch.at) });
|
|
304
305
|
at.oninput = () => (sch.at = new Date(at.value).getTime());
|
|
305
306
|
parts.push(at);
|
|
306
|
-
} else if (
|
|
307
|
-
parts.push(timeInput());
|
|
308
|
-
} else if (sch.kind === "weekly") {
|
|
307
|
+
} else if (["daily", "weekdays", "weekly"].includes(sch.kind)) {
|
|
309
308
|
parts.push(
|
|
310
309
|
h(
|
|
311
310
|
"div",
|
|
312
|
-
{ class: "day-chips" },
|
|
311
|
+
{ class: "day-chips", role: "group", "aria-label": "执行星期(可多选)" },
|
|
313
312
|
WEEK_ORDER.map((d) => {
|
|
314
|
-
const b = h("button", { type: "button", class: `day-chip${sch.days.includes(d) ? " on" : ""}
|
|
313
|
+
const b = h("button", { type: "button", class: `day-chip${sch.days.includes(d) ? " on" : ""}`, "aria-pressed": String(sch.days.includes(d)) }, `周${WEEK_NAME[d]}`);
|
|
315
314
|
b.onclick = () => {
|
|
316
315
|
sch.days = sch.days.includes(d) ? sch.days.filter((x) => x !== d) : [...sch.days, d];
|
|
316
|
+
// 改了预设的星期,就按自选的每周计划保存。
|
|
317
|
+
sch.kind = freq.value = "weekly";
|
|
317
318
|
b.classList.toggle("on", sch.days.includes(d));
|
|
319
|
+
b.setAttribute("aria-pressed", String(sch.days.includes(d)));
|
|
318
320
|
};
|
|
319
321
|
return b;
|
|
320
322
|
}),
|
|
321
323
|
),
|
|
322
324
|
timeInput(),
|
|
325
|
+
h("span", { class: "freq-word" }, "在选中的星期执行,可多选"),
|
|
323
326
|
);
|
|
324
327
|
} else if (sch.kind === "monthly") {
|
|
325
328
|
const day = h("input", { type: "number", min: "1", max: "31", value: String(sch.day), class: "narrow" });
|
|
@@ -338,7 +341,11 @@ export function openTaskForm(task, preset) {
|
|
|
338
341
|
}
|
|
339
342
|
fill(freqBox, parts);
|
|
340
343
|
};
|
|
341
|
-
freq.onchange =
|
|
344
|
+
freq.onchange = () => {
|
|
345
|
+
if (freq.value === "daily") sch.days = [...WEEK_ORDER];
|
|
346
|
+
else if (freq.value === "weekdays") sch.days = [1, 2, 3, 4, 5];
|
|
347
|
+
renderFreq();
|
|
348
|
+
};
|
|
342
349
|
renderFreq();
|
|
343
350
|
const freqHint = `按服务端时区(${T.tz || "本地时间"})。两次运行至少隔 15 分钟。`;
|
|
344
351
|
|
|
@@ -401,7 +408,8 @@ export function openTaskForm(task, preset) {
|
|
|
401
408
|
{ class: "modal-body task-form" },
|
|
402
409
|
field("名称", title),
|
|
403
410
|
field("要做的事", prompt, "需要对比的写清楚怎么判断,比如「和上次的结果比」「版本号比当前的新才算更新」。"),
|
|
404
|
-
field("频率",
|
|
411
|
+
field("频率", freq),
|
|
412
|
+
field("执行时间", freqBox, freqHint),
|
|
405
413
|
field("提醒", notify),
|
|
406
414
|
target ? field("结果发到", target) : field("结果发到", h("div", { class: "task-static" }, task?.convKey ? `专用对话「${task.convTitle || "对话"}」` : "专用对话(第一次运行时新建,之后都发到这条)")),
|
|
407
415
|
dedicatedBox,
|
|
@@ -425,6 +433,7 @@ export function openTaskForm(task, preset) {
|
|
|
425
433
|
? { kind: "hourly", every: sch.every, minute: sch.minute }
|
|
426
434
|
: { kind: "cron", expr: sch.expr };
|
|
427
435
|
if (schedule.kind === "once" && !(schedule.at > Date.now())) return toast("运行时间要在现在之后");
|
|
436
|
+
if (schedule.kind === "weekly" && !schedule.days.length) return toast("至少选择一个执行星期");
|
|
428
437
|
const dedicated = !target || target.value === "dedicated";
|
|
429
438
|
if (dedicated && !expert.value) return toast("选一个专家");
|
|
430
439
|
const payload = {
|