agent-loft 0.2.118 → 0.2.119
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/pi/driver.ts +58 -2
- package/src/agents/pi/session.ts +4 -0
- package/src/core/config.ts +5 -1
- package/web/settings/agents.js +17 -9
package/package.json
CHANGED
package/src/agents/pi/driver.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// 会话怎么跑见 session.ts;会话列表 / 历史自己读 data/pi/sessions 下的 JSONL(history.ts)。
|
|
4
4
|
// 记录是树结构(id / parentId),分叉、回退都用 SDK 的 SessionManager 另存一条新会话,原会话不动。
|
|
5
|
+
import type { AgentSession as PiAgentSession } from "@earendil-works/pi-coding-agent";
|
|
5
6
|
import type { AgentCommand } from "../../core/command-catalog.js";
|
|
6
7
|
import { config } from "../../core/config.js";
|
|
7
8
|
import type {
|
|
@@ -38,8 +39,16 @@ export class PiDriver implements AgentDriver {
|
|
|
38
39
|
}
|
|
39
40
|
async stop(): Promise<void> {}
|
|
40
41
|
|
|
42
|
+
private sessions = new Map<string, PiAgentSession>();
|
|
43
|
+
private titled = new Set<string>();
|
|
44
|
+
private titling = new Map<string, Promise<string | undefined>>();
|
|
45
|
+
|
|
41
46
|
createSession(opts: SessionOptions, events: AgentEvents, host: InteractionHost): AgentSession {
|
|
42
|
-
return new PiSession(opts, events, host)
|
|
47
|
+
return new PiSession(opts, events, host, (session, active) => {
|
|
48
|
+
const id = session.sessionId;
|
|
49
|
+
if (active) this.sessions.set(id, session);
|
|
50
|
+
else if (this.sessions.get(id) === session) this.sessions.delete(id);
|
|
51
|
+
});
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
async listSessions(cwd: string | undefined, limit = 10): Promise<SessionSummary[]> {
|
|
@@ -56,7 +65,54 @@ export class PiDriver implements AgentDriver {
|
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
async sessionTitle(id: string): Promise<string | undefined> {
|
|
59
|
-
|
|
68
|
+
const name = sessionTitleOf(id);
|
|
69
|
+
if (name) return name;
|
|
70
|
+
const c = config().agents.pi;
|
|
71
|
+
if (!c.autoTitle) return undefined;
|
|
72
|
+
const pending = this.titling.get(id);
|
|
73
|
+
if (pending) return pending;
|
|
74
|
+
if (this.titled.has(id) || !sessionFile(id)) return undefined;
|
|
75
|
+
const history = readHistoryFile(id);
|
|
76
|
+
const first = history.findIndex((m) => m.role === "user" && m.text.trim());
|
|
77
|
+
if (first < 0) return undefined; // 空会话不消耗生成机会
|
|
78
|
+
this.titled.add(id); // 失败也不在每轮 / 同步重试时重复花 token
|
|
79
|
+
const task = this.generateTitle(id, c.titleModel, history.slice(first))
|
|
80
|
+
.catch((err) => {
|
|
81
|
+
console.error("[pi] 生成会话名称失败:", err instanceof Error ? err.message : err);
|
|
82
|
+
return undefined;
|
|
83
|
+
})
|
|
84
|
+
.finally(() => this.titling.delete(id));
|
|
85
|
+
this.titling.set(id, task);
|
|
86
|
+
return task;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 只发第一轮的问答,不带系统提示、工具结果或思考;后台调用不建新会话。 */
|
|
90
|
+
private async generateTitle(id: string, model: string, history: HistoryMessage[]): Promise<string | undefined> {
|
|
91
|
+
const file = sessionFile(id);
|
|
92
|
+
if (!file) return undefined;
|
|
93
|
+
const { pi } = await loadSdk();
|
|
94
|
+
const live = this.sessions.get(id);
|
|
95
|
+
const sm = live?.sessionManager ?? pi.SessionManager.open(file, SESSIONS_DIR);
|
|
96
|
+
const selected = sm.buildSessionContext().model;
|
|
97
|
+
const titleModel = model || (live?.model ? modelKey(live.model) : selected ? `${selected.provider}/${selected.modelId}` : "");
|
|
98
|
+
const answer = history[1]?.role === "assistant" ? history[1].text : undefined;
|
|
99
|
+
const out = (await this.complete({
|
|
100
|
+
system: "你给对话起标题。根据用户的第一条消息(和助手的回复)概括这次对话在做什么,输出一个简短的标题:和用户用同一种语言,中文不超过 15 个字,不加引号、句号和表情。",
|
|
101
|
+
prompt: `用户:\n${history[0]!.text.slice(0, 2000)}${answer ? `\n\n助手:\n${answer.slice(0, 1500)}` : ""}`,
|
|
102
|
+
schema: { type: "object", properties: { title: { type: "string" } }, required: ["title"], additionalProperties: false },
|
|
103
|
+
...(titleModel ? { model: titleModel } : {}),
|
|
104
|
+
timeoutMs: 60_000,
|
|
105
|
+
})) as { title?: unknown };
|
|
106
|
+
if (typeof out?.title !== "string") return undefined;
|
|
107
|
+
const title = out.title.replace(/\s+/g, " ").trim().replace(/^["'“”‘’「」《》]+|["'“”‘’「」《》。.]+$/g, "").trim().slice(0, 80);
|
|
108
|
+
if (!title || !config().agents.pi.autoTitle || sessionFile(id) !== file) return undefined;
|
|
109
|
+
// 生成期间可能已有名称、续聊或恢复:重新取活实例 / 重读文件,不能接在旧 leaf 上丢掉后续分支。
|
|
110
|
+
const existing = sessionTitleOf(id);
|
|
111
|
+
if (existing) return existing;
|
|
112
|
+
const current = this.sessions.get(id);
|
|
113
|
+
if (current) current.setSessionName(title);
|
|
114
|
+
else pi.SessionManager.open(file, SESSIONS_DIR).appendSessionInfo(title);
|
|
115
|
+
return title;
|
|
60
116
|
}
|
|
61
117
|
|
|
62
118
|
async readHistory(id: string): Promise<HistoryMessage[]> {
|
package/src/agents/pi/session.ts
CHANGED
|
@@ -219,6 +219,8 @@ export class PiSession implements AgentSession {
|
|
|
219
219
|
private readonly opts: SessionOptions,
|
|
220
220
|
private readonly events: AgentEvents,
|
|
221
221
|
private readonly host: InteractionHost,
|
|
222
|
+
/** 驱动写会话元数据时必须用活着的实例,不能另开一个持有旧 leaf 的 SessionManager */
|
|
223
|
+
private readonly onSession?: (session: PiAgentSession, active: boolean) => void,
|
|
222
224
|
) {
|
|
223
225
|
this.allowAll = opts.permission === "full";
|
|
224
226
|
if (opts.model) this.runtime.model = opts.model;
|
|
@@ -313,6 +315,7 @@ export class PiSession implements AgentSession {
|
|
|
313
315
|
this.syncPlanTool(session);
|
|
314
316
|
if (fork) this.forked = true;
|
|
315
317
|
this.session = session;
|
|
318
|
+
this.onSession?.(session, true);
|
|
316
319
|
this.runtime = { model: modelKey(model), ...(session.supportsThinking() ? { effort: session.thinkingLevel } : {}) };
|
|
317
320
|
return session;
|
|
318
321
|
}
|
|
@@ -560,6 +563,7 @@ export class PiSession implements AgentSession {
|
|
|
560
563
|
private drop(session: PiAgentSession): void {
|
|
561
564
|
if (this.session !== session) return;
|
|
562
565
|
this.session = null;
|
|
566
|
+
this.onSession?.(session, false);
|
|
563
567
|
try {
|
|
564
568
|
session.dispose();
|
|
565
569
|
} catch {
|
package/src/core/config.ts
CHANGED
|
@@ -88,6 +88,10 @@ export type AppConfig = {
|
|
|
88
88
|
/** 思考程度(off / minimal / low / medium / high / xhigh),空 = pi 默认 */
|
|
89
89
|
defaultEffort: string;
|
|
90
90
|
models: string[];
|
|
91
|
+
/** SDK 不自动起名,由 agent-loft 在第一轮后后台生成一次 */
|
|
92
|
+
autoTitle: boolean;
|
|
93
|
+
/** 空 = 跟随会话模型 */
|
|
94
|
+
titleModel: string;
|
|
91
95
|
};
|
|
92
96
|
/**
|
|
93
97
|
* dsh(DeepSeek Harness,npm i -g @deepseek-ai/dsh):每个会话一个 `dsh --profile acp` 进程。
|
|
@@ -186,7 +190,7 @@ export function defaultConfig(): AppConfig {
|
|
|
186
190
|
// 默认不开:要先在设置里登录
|
|
187
191
|
workbuddy: { enabled: false, environment: "workbuddy", cliPath: "", defaultModel: "", defaultEffort: "", models: [] },
|
|
188
192
|
// 默认不开:要先填一家服务商的 API Key
|
|
189
|
-
pi: { enabled: false, defaultModel: "", defaultEffort: "", models: [] },
|
|
193
|
+
pi: { enabled: false, defaultModel: "", defaultEffort: "", models: [], autoTitle: true, titleModel: "" },
|
|
190
194
|
// 默认不开:要先装 dsh、配 DeepSeek API Key
|
|
191
195
|
dsh: { enabled: false, path: "dsh", home: "", patch: "", defaultModel: "", defaultEffort: "", models: [] },
|
|
192
196
|
},
|
package/web/settings/agents.js
CHANGED
|
@@ -237,15 +237,19 @@ export function agentsTab(cfg, page) {
|
|
|
237
237
|
const path = h("input", { value: settings.path });
|
|
238
238
|
const home = h("input", { value: settings.home, placeholder: "留空用 ~/.codex" });
|
|
239
239
|
const sandbox = h("select", {}, ["read-only", "workspace-write", "danger-full-access"].map((mode) => h("option", { value: mode, selected: mode === settings.sandbox }, mode)));
|
|
240
|
-
|
|
241
|
-
const titlePick = modelPicker("codex", settings.titleModel ?? "", "", "跟随 config.toml");
|
|
242
|
-
const titleModel = h("div", { class: "grow field" }, titlePick.modelEl);
|
|
243
|
-
const syncTitle = () => { titleModel.hidden = !titleOn.checked; };
|
|
244
|
-
titleOn.addEventListener("change", syncTitle);
|
|
245
|
-
syncTitle();
|
|
246
|
-
codex = { path, home, sandbox, titleOn, titlePick, titleModel };
|
|
240
|
+
codex = { path, home, sandbox };
|
|
247
241
|
config.append(field("codex 可执行文件", path, "以系统服务运行时 PATH 很短,建议写绝对路径"), field("CODEX_HOME", home), field("沙箱", sandbox, "workspace-write:目录内可读写,越界才弹授权。danger-full-access 等于关掉沙箱"));
|
|
248
242
|
}
|
|
243
|
+
let titles = null;
|
|
244
|
+
if (engine === "codex" || engine === "pi") {
|
|
245
|
+
const on = h("input", { type: "checkbox", checked: settings.autoTitle ?? engine === "pi" });
|
|
246
|
+
const pick = modelPicker(engine, settings.titleModel ?? "", "", engine === "pi" ? "跟随会话模型" : "跟随 config.toml");
|
|
247
|
+
const model = h("div", { class: "grow field" }, pick.modelEl);
|
|
248
|
+
const sync = () => { model.hidden = !on.checked; };
|
|
249
|
+
on.addEventListener("change", sync);
|
|
250
|
+
sync();
|
|
251
|
+
titles = { on, pick, model };
|
|
252
|
+
}
|
|
249
253
|
let site = null;
|
|
250
254
|
let cliPath = null;
|
|
251
255
|
if (engine === "workbuddy") {
|
|
@@ -283,7 +287,10 @@ export function agentsTab(cfg, page) {
|
|
|
283
287
|
}
|
|
284
288
|
config.append(h("small", { class: "muted" }, "默认模型、思考程度、权限和默认加载的技能 / MCP,在「设置 → 专家」里这个 Agent 的「设置」中修改。"));
|
|
285
289
|
config.append(h("div", { class: "field" }, h("span", {}, "输入框可选模型"), models.el));
|
|
286
|
-
if (
|
|
290
|
+
if (titles) {
|
|
291
|
+
config.append(h("div", { class: "row title-row" }, h("label", { class: "check" }, titles.on, "自动生成会话名称"), titles.model));
|
|
292
|
+
if (engine === "pi") config.append(h("small", { class: "muted" }, "第一轮回复后在后台生成一次,会额外调用模型;留空跟随会话模型,可另选低成本模型。手动改名不会被覆盖。"));
|
|
293
|
+
}
|
|
287
294
|
config.append(h("div", { class: "row" }, saveButton(() => {
|
|
288
295
|
const selected = models.value();
|
|
289
296
|
if (!selected) {
|
|
@@ -294,7 +301,8 @@ export function agentsTab(cfg, page) {
|
|
|
294
301
|
enabled: on.checked, models: selected,
|
|
295
302
|
...(site ? { environment: site.value, cliPath: cliPath.value.trim() } : {}),
|
|
296
303
|
...(dsh ? { path: dsh.path.value.trim() || "dsh", home: dsh.home.value.trim(), patch: dsh.patch.value.trim() } : {}),
|
|
297
|
-
...(codex ? { path: codex.path.value.trim() || "codex", home: codex.home.value.trim(), sandbox: codex.sandbox.value
|
|
304
|
+
...(codex ? { path: codex.path.value.trim() || "codex", home: codex.home.value.trim(), sandbox: codex.sandbox.value } : {}),
|
|
305
|
+
...(titles ? { autoTitle: titles.on.checked, titleModel: titles.pick.model() } : {}),
|
|
298
306
|
} } });
|
|
299
307
|
}, `保存 ${label} 设置`)));
|
|
300
308
|
page.append(h("div", { class: "section", "data-agent": engine }, config));
|