agent-loft 0.2.72 → 0.2.73
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/codex/protocol.ts +11 -1
- package/src/agents/codex/session.ts +52 -1
- package/src/core/hub.ts +10 -0
- package/src/core/interactions.ts +12 -1
- package/src/core/task-runner.ts +2 -2
- package/src/core/types.ts +12 -1
- package/web/css/messages.css +1 -0
package/package.json
CHANGED
|
@@ -209,9 +209,19 @@ export type ToolRequestUserInputResponse = {
|
|
|
209
209
|
|
|
210
210
|
// ---------- 服务端 → 客户端:通知 ----------
|
|
211
211
|
|
|
212
|
+
export type AsyncUserInputQuestion = { title: string; options: string[] | null };
|
|
213
|
+
|
|
212
214
|
export type ThreadItem =
|
|
213
215
|
| { type: "userMessage"; id: string }
|
|
214
|
-
| {
|
|
216
|
+
| {
|
|
217
|
+
type: "agentMessage";
|
|
218
|
+
id: string;
|
|
219
|
+
text: string;
|
|
220
|
+
phase?: string | null;
|
|
221
|
+
/** async + questions:模型调了 request_user_input_async,提问混在普通消息里发出,不走 item/tool/requestUserInput */
|
|
222
|
+
delivery?: string | null;
|
|
223
|
+
questions?: AsyncUserInputQuestion[] | null;
|
|
224
|
+
}
|
|
215
225
|
| { type: "reasoning"; id: string; summary?: string[]; content?: string[] }
|
|
216
226
|
| { type: "plan"; id: string; text: string }
|
|
217
227
|
| {
|
|
@@ -222,6 +222,38 @@ type TokenBreakdown = {
|
|
|
222
222
|
reasoningOutputTokens?: number;
|
|
223
223
|
};
|
|
224
224
|
|
|
225
|
+
/** request_user_input_async 的问题:只有标题和选项文字,且没有 id */
|
|
226
|
+
function parseAsyncQuestions(raw: unknown): QuestionSpec[] | null {
|
|
227
|
+
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
228
|
+
const specs: QuestionSpec[] = [];
|
|
229
|
+
for (const [i, item] of raw.entries()) {
|
|
230
|
+
const rec = (typeof item === "object" && item !== null ? item : {}) as { title?: unknown; options?: unknown };
|
|
231
|
+
const question = typeof rec.title === "string" ? rec.title.trim() : "";
|
|
232
|
+
if (!question) return null;
|
|
233
|
+
const labels = Array.isArray(rec.options) ? rec.options : [];
|
|
234
|
+
const options = labels.flatMap((o) => (typeof o === "string" && o.trim() ? [{ label: o.trim() }] : []));
|
|
235
|
+
specs.push({
|
|
236
|
+
id: `q${i}`,
|
|
237
|
+
header: options.length > 0 ? "选择" : "回答",
|
|
238
|
+
question,
|
|
239
|
+
options,
|
|
240
|
+
multiSelect: false,
|
|
241
|
+
allowOther: true,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return specs;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** 提问卡的答案写成用户消息:一问就是答案本身,多问要带上问的是什么 */
|
|
248
|
+
function answersAsMessage(questions: QuestionSpec[], answers: Record<string, string[]>): string {
|
|
249
|
+
const answered = questions.flatMap((q) => {
|
|
250
|
+
const picked = answers[q.id];
|
|
251
|
+
return picked?.length ? [{ q, text: picked.join("、") }] : [];
|
|
252
|
+
});
|
|
253
|
+
if (questions.length === 1) return answered[0]?.text ?? "";
|
|
254
|
+
return answered.map(({ q, text }) => `${q.question}\n答:${text}`).join("\n\n");
|
|
255
|
+
}
|
|
256
|
+
|
|
225
257
|
function parseQuestions(raw: unknown): ParsedQuestion[] | null {
|
|
226
258
|
if (!Array.isArray(raw) || raw.length === 0) return null;
|
|
227
259
|
const specs: ParsedQuestion[] = [];
|
|
@@ -707,6 +739,17 @@ export class CodexSession implements AgentSession {
|
|
|
707
739
|
return outcome;
|
|
708
740
|
}
|
|
709
741
|
|
|
742
|
+
/** 不阻塞的提问:这一轮不等它(Codex 已经告诉模型 accepted),用户答了再作为下一条消息发出 */
|
|
743
|
+
private askDetached(questions: QuestionSpec[]): void {
|
|
744
|
+
void this.host({ kind: "question", title: "Codex 想确认一下", questions, detached: true })
|
|
745
|
+
.then((outcome) => {
|
|
746
|
+
if (outcome.settlement !== "answered" || this.disposed) return;
|
|
747
|
+
const text = answersAsMessage(questions, outcome.answers);
|
|
748
|
+
if (text) this.events.onFollowUp?.(text);
|
|
749
|
+
})
|
|
750
|
+
.catch((err) => console.warn("[codex] 不阻塞的提问没能弹出:", err));
|
|
751
|
+
}
|
|
752
|
+
|
|
710
753
|
/**
|
|
711
754
|
* 每一条都**必须**返回点什么 —— 漏回一次,Codex 那一轮就永远挂在等审批上。
|
|
712
755
|
* 未知方法也要按它自己的协议结构明确拒绝。
|
|
@@ -955,7 +998,15 @@ export class CodexSession implements AgentSession {
|
|
|
955
998
|
case "agentMessage": {
|
|
956
999
|
const message = item as Extract<ThreadItem, { type: "agentMessage" }>;
|
|
957
1000
|
if (message.phase === "commentary") this.commentaryItems.add(message.id);
|
|
958
|
-
if (phase !== "completed"
|
|
1001
|
+
if (phase !== "completed") return;
|
|
1002
|
+
if (message.delivery === "async") {
|
|
1003
|
+
const questions = parseAsyncQuestions(message.questions);
|
|
1004
|
+
if (questions) {
|
|
1005
|
+
this.askDetached(questions);
|
|
1006
|
+
return;
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (!message.text.trim()) return;
|
|
959
1010
|
// commentary 是 Codex 边干边说的旁白,属于过程;正文才是要给人看的回复
|
|
960
1011
|
if (message.phase === "commentary") {
|
|
961
1012
|
this.events.onStep("note", `💬 ${summarize(message.text)}`);
|
package/src/core/hub.ts
CHANGED
|
@@ -733,6 +733,14 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd] }>
|
|
|
733
733
|
onTodos: (todos) => alive() && this.emitEvent(key, { type: "todos", todos }),
|
|
734
734
|
onTextDelta: (text) => alive() && this.emitEvent(key, { type: "delta", text }),
|
|
735
735
|
onText: (text) => alive() && this.emitEvent(key, { type: "text", text }),
|
|
736
|
+
onFollowUp: (text) => {
|
|
737
|
+
if (!alive()) return;
|
|
738
|
+
try {
|
|
739
|
+
this.send(key, { text, attachments: [] }, { channel: "card", channelId: "card", name: "提问卡" });
|
|
740
|
+
} catch (err) {
|
|
741
|
+
this.emitEvent(key, { type: "notice", text: `你的回答没有发出(${err instanceof Error ? err.message : err}),请重新发送。` });
|
|
742
|
+
}
|
|
743
|
+
},
|
|
736
744
|
onResult: (result) => {
|
|
737
745
|
if (!alive()) return;
|
|
738
746
|
const parts = [`用时 ${formatDuration(result.durationMs)}`];
|
|
@@ -1017,6 +1025,8 @@ class Hub extends EventEmitter<{ event: [EventEnvelope]; task: [TaskTurnEnd] }>
|
|
|
1017
1025
|
};
|
|
1018
1026
|
input = { ...input, id: shown.id };
|
|
1019
1027
|
const by = `${origin.name ?? "用户"}以消息作答`;
|
|
1028
|
+
// 不阻塞的提问不等这条消息:用户另外说了话,卡就不用再答了(这条消息照常发给 agent)
|
|
1029
|
+
if (!task) interactions.dismissDetached(key, "已改为发消息");
|
|
1020
1030
|
|
|
1021
1031
|
/**
|
|
1022
1032
|
* 拿这条消息回答挂着的提问。条件:提问属于当前活着的会话进程、这条消息收到后
|
package/src/core/interactions.ts
CHANGED
|
@@ -177,12 +177,19 @@ class InteractionCenter extends EventEmitter<InteractionEvents> {
|
|
|
177
177
|
findQuestion(convKey: string): Interaction | undefined {
|
|
178
178
|
let oldest: Entry | undefined;
|
|
179
179
|
for (const entry of this.pending.values()) {
|
|
180
|
-
if (entry.spec.kind !== "question" || entry.convKey !== convKey) continue;
|
|
180
|
+
if (entry.spec.kind !== "question" || entry.spec.detached || entry.convKey !== convKey) continue;
|
|
181
181
|
if (!oldest || entry.createdAt < oldest.createdAt) oldest = entry;
|
|
182
182
|
}
|
|
183
183
|
return oldest ? publicView(oldest) : undefined;
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
+
/** 用户发了新消息:不阻塞的提问不再需要回答,撤掉。 */
|
|
187
|
+
dismissDetached(convKey: string, note: string): void {
|
|
188
|
+
for (const entry of [...this.pending.values()]) {
|
|
189
|
+
if (entry.convKey === convKey && isDetached(entry)) this.settle(entry.id, "skipped", note);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
186
193
|
/** 取消某会话上所有挂着的请求。中断、切目录、开新会话时必须调用。 */
|
|
187
194
|
cancelAll(convKey: string, note: string): Interaction[] {
|
|
188
195
|
const out: Interaction[] = [];
|
|
@@ -224,6 +231,10 @@ function publicView(entry: Entry | Interaction): Interaction {
|
|
|
224
231
|
};
|
|
225
232
|
}
|
|
226
233
|
|
|
234
|
+
export function isDetached(interaction: Interaction): boolean {
|
|
235
|
+
return interaction.spec.kind === "question" && !!interaction.spec.detached;
|
|
236
|
+
}
|
|
237
|
+
|
|
227
238
|
export function unansweredCount(entry: Interaction): number {
|
|
228
239
|
return entry.selections.filter((picked, i) => picked.length === 0 && !entry.freeText[i]).length;
|
|
229
240
|
}
|
package/src/core/task-runner.ts
CHANGED
|
@@ -9,7 +9,7 @@ import crypto from "node:crypto";
|
|
|
9
9
|
import { resolveWorkspacePath } from "./config.js";
|
|
10
10
|
import { canUseExpert, getExpert } from "./experts.js";
|
|
11
11
|
import { hub, type TaskTurnEnd } from "./hub.js";
|
|
12
|
-
import { interactions } from "./interactions.js";
|
|
12
|
+
import { interactions, isDetached } from "./interactions.js";
|
|
13
13
|
import { memoryOwnerOf } from "./personal.js";
|
|
14
14
|
import { getConversation, updateAgentBinding, updateConversation, type Conversation } from "./store.js";
|
|
15
15
|
import { formatDateTime } from "./task-schedule.js";
|
|
@@ -130,7 +130,7 @@ function fire(task: Task, manual: boolean, now = Date.now()): string | undefined
|
|
|
130
130
|
return `${hub.label(conv.agent)} 没有启用`;
|
|
131
131
|
}
|
|
132
132
|
// 刚点过停止的也当忙:被打断的那一轮可能还会晚到一个出错,别让它算到这次运行头上
|
|
133
|
-
if (hub.isBusy(key) || hub.recentlyStopped(key) || interactions.listPending(key).
|
|
133
|
+
if (hub.isBusy(key) || hub.recentlyStopped(key) || interactions.listPending(key).some((i) => !isDetached(i))) {
|
|
134
134
|
if (manual) return "这条对话正在回复或者在等你处理授权,等它结束再试";
|
|
135
135
|
const since = waiting.get(task.id)?.since ?? now;
|
|
136
136
|
if (now - since > WAIT_MAX_MS) {
|
package/src/core/types.ts
CHANGED
|
@@ -60,7 +60,16 @@ export type InteractionSpec =
|
|
|
60
60
|
/** 给了就显示「本会话全部允许」按钮。只有 agent 能真正兑现时才给 */
|
|
61
61
|
allowSession?: boolean;
|
|
62
62
|
}
|
|
63
|
-
| {
|
|
63
|
+
| {
|
|
64
|
+
kind: "question";
|
|
65
|
+
title: string;
|
|
66
|
+
questions: QuestionSpec[];
|
|
67
|
+
/**
|
|
68
|
+
* 发起方不等这张卡:那一轮照常结束,答案作为下一条用户消息发出(Codex 的 request_user_input_async)。
|
|
69
|
+
* 普通消息不算作答,而是撤掉这张卡、照常发给 agent。
|
|
70
|
+
*/
|
|
71
|
+
detached?: boolean;
|
|
72
|
+
};
|
|
64
73
|
|
|
65
74
|
export type InteractionOutcome = {
|
|
66
75
|
settlement: Settlement;
|
|
@@ -98,6 +107,8 @@ export type AgentEvents = {
|
|
|
98
107
|
onTextDelta: (delta: string) => void;
|
|
99
108
|
/** 一段完整的回复正文 */
|
|
100
109
|
onText: (text: string) => void;
|
|
110
|
+
/** 用户回答了不阻塞的提问(detached):把答案当下一条用户消息发出 */
|
|
111
|
+
onFollowUp?: (text: string) => void;
|
|
101
112
|
onResult: (result: TurnResult) => void;
|
|
102
113
|
onError: (err: unknown) => void;
|
|
103
114
|
/** 拿到 / 更新了 agent 侧的会话 id,持久化后用于 resume */
|