Skip to content

🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.

Chat: the model's words in the reply, delivered files open, images inline

Marlae1e5fb77f68c
diff --git a/electron/main.ts b/electron/main.ts
index 956dc30..7a22b74 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -231,6 +231,7 @@ async function boot(): Promise<void> {
},
openDataDir: () => void shell.openPath(config.dataDir),
openPath: (p) => void shell.openPath(p),
+ showItemInFolder: (p) => shell.showItemInFolder(p),
openInBrowser: () => void shell.openExternal(entryUrl),
openExternal: (url) => {
if (/^https?:/i.test(url)) void shell.openExternal(url);
diff --git a/src/agent/activity.ts b/src/agent/activity.ts
index 39dc4ce..b4fda7b 100644
--- a/src/agent/activity.ts
+++ b/src/agent/activity.ts
@@ -63,10 +63,14 @@ function prettyArgs(args: string): string {
export class ActivityLog {
readonly steps: ActivityStep[] = [];
- /** model:这一轮用的模型(DSH 只在会话第一次请求时报 request/context,之后的回合靠它起名) */
+ /**
+ * model:这一轮用的模型(DSH 只在会话第一次请求时报 request/context,之后的回合靠它起名)。
+ * chat:聊天回合——模型说的话(回复和回合中间说的话)进了对话,模型那一步不再重复这些文字,只留调用了什么和思考
+ */
constructor(
private onChange?: (step: ActivityStep) => void,
private model = "",
+ private opts: { chat?: boolean } = {},
) {}
get last(): ActivityStep | undefined {
@@ -103,12 +107,15 @@ export class ActivityLog {
const prev = this.find(id);
const at = prev?.at ?? time;
const u = data.usage ?? {};
+ // 聊天里已经显示了这段话(被打断的那一步除外,它的话不进对话)
+ const said = this.opts.chat && !data.interrupted ? text : "";
+ const callLine = calls.length ? tr("activity.calls", { list: calls.join(tr("activity.listSep")) }) : "";
this.put({
id,
kind: "model",
title: model || tr("activity.model"),
- summary: text ? oneLine(text) : calls.length ? tr("activity.calls", { list: calls.join(tr("activity.listSep")) }) : thinking ? tr("activity.thinkingOnly") : tr("activity.emptyReply"),
- output: clip([thinking && `${tr("activity.thinkingTag")}\n${thinking}`, text].filter(Boolean).join("\n\n"), MAX_OUTPUT) || undefined,
+ summary: said ? callLine || undefined : text ? oneLine(text) : callLine || (thinking ? tr("activity.thinkingOnly") : tr("activity.emptyReply")),
+ output: clip([thinking && `${tr("activity.thinkingTag")}\n${thinking}`, said ? "" : text].filter(Boolean).join("\n\n"), MAX_OUTPUT) || undefined,
status: "ok",
at,
ms: Math.max(0, time - at),
diff --git a/src/agent/host.ts b/src/agent/host.ts
index 07dbe2a..94a9d5c 100644
--- a/src/agent/host.ts
+++ b/src/agent/host.ts
@@ -139,6 +139,10 @@ export interface TurnObserver {
onEvent?(type: string, data: any, time?: number): void;
/** 消息已经交给代理(之前是等进程、等会话) */
onStart?(): void;
+ /** 模型在回合中间说的话(见 interimText),按说的顺序 */
+ onInterim?(text: string): void;
+ /** 代理用 present 交付了文件:已按 deliverables 过滤(工作区里真实存在的普通文件,真实路径) */
+ onPresent?(files: string[]): void;
}
/**
@@ -564,6 +568,18 @@ export function answerText(data: any): string {
.trim();
}
+/** 一条 assistant/message 是不是回合中间说的话(带工具调用、没被打断,比如「我先看看下载目录里有什么。」):是就返回它的文字 */
+export function interimText(data: any): string {
+ if (data?.interrupted) return "";
+ const content: any[] = Array.isArray(data?.message?.content) ? data.message.content : [];
+ if (!content.some((b) => b?.type === "tool-call")) return "";
+ return content
+ .filter((b) => b?.type === "text")
+ .map((b) => String(b.text ?? ""))
+ .join("")
+ .trim();
+}
+
/**
* 每个 bot 一个 DSH 子进程(懒启动):DSH_HOME = <data>/dsh/<botId>,工作区 = <data>/workspaces/<botId>,
* workspace-write 沙箱;模型请求经本地 bridge 代理到 magpie(子进程拿不到真实密钥)。
@@ -1519,7 +1535,12 @@ exec "$REAL" "$@"
st.toolCalls++;
this.calls.set(String(data.callId), { name: String(data.name), args: String(data.arguments ?? ""), at: Date.now() });
if (this.calls.size > 500) this.calls.delete(this.calls.keys().next().value!);
- if (data.name === "present") st.presented.push(...presentPaths(String(data.arguments ?? "")));
+ if (data.name === "present") {
+ const paths = presentPaths(String(data.arguments ?? ""));
+ st.presented.push(...paths);
+ const files = deliverables(this.workspace, paths);
+ if (files.length) observer?.onPresent?.(files);
+ }
observer?.onProgress?.(`🔧 ${summarizeCall(String(data.name), String(data.arguments ?? "")).split(this.workspace + "/").join("")}`);
}
if (ev?.type === "tool/result") {
@@ -1537,6 +1558,8 @@ exec "$REAL" "$@"
}
const answer = answerText(data);
if (answer) st.answers.push(answer);
+ const interim = interimText(data);
+ if (interim) observer?.onInterim?.(interim);
}
if (ev?.type === "turn/end" && data?.reason?.kind === "error") st.turnError = String(data.reason.error?.message ?? "unknown error");
observer?.onEvent?.(String(ev?.type), data, Number(ev?.time) || Date.now());
diff --git a/src/agent/manager.ts b/src/agent/manager.ts
index 5a2e059..81c7298 100644
--- a/src/agent/manager.ts
+++ b/src/agent/manager.ts
@@ -81,6 +81,8 @@ export interface TurnProgress {
jobs: Set<string>;
/** 进行中的生图 / 生视频任务 */
media: number;
+ /** 模型在这一轮中间说过的话(信息面板里和步骤一起显示;步骤里不再重复这些文字) */
+ interim: string[];
}
/** 生成确认里"取消"那一项(问题建好后界面语言可能变了:两种语言的都认) */
@@ -328,7 +330,7 @@ export class AgentManager {
private async track(botId: string, input: TurnInput & { onStep?: (step: ActivityStep) => void }, run: (input: TurnInput) => Promise<TurnResult>): Promise<TurnResult> {
const conv = input.conversationId;
- const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model), abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0 };
+ const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model, { chat: true }), abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0, interim: [] };
this.turnsInProgress.set(conv, t);
// 看门狗:代理已经在处理,却长时间没有进展(模型不回、只在重试),又不是在等用户批准或回答、
// 也没有工具、命令或生成任务在跑(生视频、转到后台的长命令、子代理里的工具)→ 中止
@@ -347,6 +349,10 @@ export class AgentManager {
t.lastProgress = Date.now();
input.observer?.onStart?.();
},
+ onInterim: (text) => {
+ t.interim.push(text);
+ input.observer?.onInterim?.(text);
+ },
onEvent: (type, data, time) => {
if (type === "subagent/event") {
// 子代理在干活:它的工具调用、命令同样记下;它在重试模型请求不算进展
diff --git a/src/app.ts b/src/app.ts
index 0842b2c..354450f 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -60,8 +60,10 @@ export interface DesktopHooks {
/** Windows 桌面版:窗口右上角的最小化 / 最大化 / 关闭由系统画在界面上,符号颜色跟界面主题(主题只存在界面里) */
setTitleBarTheme?(dark: boolean): void;
openDataDir(): void;
- /** 在系统的文件管理器里打开一个文件夹(任务详情里的「打开」,3f) */
+ /** 用系统的默认程序打开一个文件或文件夹(任务详情里的「打开」,3f;对话里交付的文件) */
openPath?(p: string): void;
+ /** 在系统的文件管理器里显示一个文件(选中它) */
+ showItemInFolder?(p: string): void;
openInBrowser(): void;
/** 在系统浏览器里打开外部链接(Modelflare 登录、充值) */
openExternal(url: string): void;
diff --git a/src/channels/feishu/card.ts b/src/channels/feishu/card.ts
index 7018646..3ef05ae 100644
--- a/src/channels/feishu/card.ts
+++ b/src/channels/feishu/card.ts
@@ -100,6 +100,7 @@ export interface FeishuOutbox {
/**
* 飞书回复会话:CardKit 流式卡片(~400ms 节流,单卡 ≤10 次/秒),结束时关闭 streaming_mode。
+ * 模型在回合中间说的话(interim)一段段写进卡片,最终回复接在它们后面,卡片上留着全部。
* 卡片创建/发送失败或中途失败 → 降级为纯文本回复。
* 最终回复、文件、审批卡片先写进出站队列(持久化、按会话排队、失败重试);流式的中间内容不经过它。没给队列时(单元测试)直接发。
*/
@@ -108,6 +109,8 @@ export class FeishuReply implements ReplySession {
private seq = 0;
private latest = "";
private flushed = "";
+ /** 模型在回合中间说的话:卡片上显示在最终回复前面 */
+ private interim: string[] = [];
private timer: NodeJS.Timeout | undefined;
private inflight: Promise<void> = Promise.resolve();
private fallback = false;
@@ -136,6 +139,12 @@ export class FeishuReply implements ReplySession {
this.latest = op.text;
this.schedule();
return;
+ case "interim":
+ // 说了话,等代理启动时的临时文字就不要了
+ this.interim.push(op.text);
+ this.latest = this.interim.join("\n\n");
+ this.schedule();
+ return;
case "stream.finish":
return this.finish(op.text);
case "text":
@@ -201,8 +210,11 @@ export class FeishuReply implements ReplySession {
}
}
- /** 最终回复:写进卡片并收尾(卡片不可用时发文本)——这一步经出站队列,崩溃重启后会补发 */
- private async finish(text: string): Promise<void> {
+ /** 最终回复(接在回合中间说的话后面):写进卡片并收尾(卡片不可用时发文本)——这一步经出站队列,崩溃重启后会补发 */
+ private async finish(answer: string): Promise<void> {
+ // 最后没再说话时,回复就是中间说的最后一句:不重复
+ const before = this.interim.at(-1) === answer ? this.interim.slice(0, -1) : this.interim;
+ const text = [...before, answer].filter(Boolean).join("\n\n");
this.closed = true;
if (this.timer) {
clearTimeout(this.timer);
diff --git a/src/core/runtime.ts b/src/core/runtime.ts
index f9a206c..c5fcc42 100644
--- a/src/core/runtime.ts
+++ b/src/core/runtime.ts
@@ -133,6 +133,52 @@ export const stoppedText = (ms: number) => t("runtime.stopped", { d: fmtSecs(ms)
/** 处理中又发来一句、并进正在进行的回合时回的那句(按当前语言) */
export const steerAck = () => t("runtime.steerAck");
+/** 一条回复最多显示几个交付的文件(同 deliverables) */
+const MAX_SHOWN_FILES = 8;
+
+/**
+ * 一轮里模型说的话和交付的文件:回合中间说的话一段一段交给渠道(网页、飞书卡片显示,微信不发),
+ * present 交付的文件马上给网页看(文件本身照旧在回复之后发);回合结束时随回复存进历史。
+ */
+export class TurnSaid {
+ readonly interim: string[] = [];
+ readonly presented: string[] = [];
+ /** 等代理启动时显示过的临时文字(「代理已就绪,正在处理…」):第一段话来了就换掉 */
+ provisional = false;
+
+ constructor(private out: ReplySession) {}
+
+ readonly observer = {
+ onInterim: (text: string) => {
+ // 不 await:几个操作按调用顺序交给渠道
+ if (this.provisional) {
+ this.provisional = false;
+ void this.out.apply({ type: "stream.update", text: "" });
+ }
+ this.interim.push(text);
+ void this.out.apply({ type: "interim", text });
+ },
+ onPresent: (files: string[]) => {
+ for (const f of files) {
+ if (this.presented.includes(f) || this.presented.length >= MAX_SHOWN_FILES) continue;
+ this.presented.push(f);
+ void this.out.apply({ type: "presented", path: f, name: path.basename(f) });
+ }
+ },
+ };
+
+ /**
+ * 回合结束时的回复:模型最后的回答;最后没再说话时,回合中间说的最后一句就是回复(不再算在中间说的话里),
+ * 什么都没说时用 empty。interim 是显示在回复前面的那些话
+ */
+ reply(answer: string, empty: string): { text: string; interim: string[] } {
+ const text = answer.trim();
+ if (text) return { text, interim: this.interim.filter((x, i, a) => !(i === a.length - 1 && x === text)) };
+ if (this.interim.length) return { text: this.interim.at(-1)!, interim: this.interim.slice(0, -1) };
+ return { text: empty, interim: [] };
+ }
+}
+
/**
* 不是回复某条消息、而是主动发到会话里(代理自己醒来的一轮:后台命令结束):渠道给出这个会话的出站会话。
* 微信、飞书的回复会话经出站队列发(和普通回复同一条路:落库、按会话排队、重试、发送时才查微信的窗口和配额)。
@@ -553,14 +599,17 @@ export class BotRuntime {
visible = true;
await out.apply(interactiveOp(row));
});
+ // 模型在回合中间说的话、交付的文件:马上显示,结束时随回复存下
+ const said = new TurnSaid(out);
await out.apply({ type: "typing", on: true });
await out.apply({ type: "stream.start" });
- // 代理进程还没就绪(第一次启动可能要几分钟):先说清楚在等什么,而不是一直"思考中"。最终回复会替换这段文字
+ // 代理进程还没就绪(第一次启动可能要几分钟):先说清楚在等什么,而不是一直"思考中"。模型说的第一段话或最终回复会替换这段文字
const host = agent.host(botId);
let startup: NodeJS.Timeout | undefined;
if (host.state !== "running") {
const since = Date.now();
const show = () => {
+ said.provisional = true;
if (host.state === "running") {
clearInterval(startup);
return void out.apply({ type: "stream.update", text: t("runtime.agentReady") });
@@ -627,6 +676,7 @@ export class BotRuntime {
inflight.userStored = true;
inflight.release?.();
},
+ ...said.observer,
},
});
const halt = () => agent.stopTurn(convId);
@@ -646,9 +696,9 @@ export class BotRuntime {
.finally(() => clearInterval(startup));
if (!inflight.userStored) store.addMessage(convId, "user", text);
inflight.userStored = true;
- const finalText = r.text.trim() || t("runtime.doneNoText");
- store.addMessage(convId, "assistant", finalText, [...steps.values()]);
- await out.apply({ type: "stream.finish", text: finalText });
+ const reply = said.reply(r.text, t("runtime.doneNoText"));
+ store.addMessage(convId, "assistant", reply.text, [...steps.values()], { interim: reply.interim, files: r.files });
+ await out.apply({ type: "stream.finish", text: reply.text });
for (const f of r.files ?? []) {
try {
await out.apply({ type: "file", path: f, name: path.basename(f) });
@@ -673,7 +723,8 @@ export class BotRuntime {
const msg = turnFailedText(err, [...steps.values()]);
if (!inflight.userStored) store.addMessage(convId, "user", text);
inflight.userStored = true;
- store.addMessage(convId, "assistant", msg, [...steps.values()]);
+ // 出错、中止之前说过的话和交付过的文件留在历史里(文件不再发到其它渠道)
+ store.addMessage(convId, "assistant", msg, [...steps.values()], { interim: said.interim, files: said.presented });
await out.apply({ type: "stream.finish", text: msg });
this.answered(convId, inflight);
return true;
@@ -731,6 +782,7 @@ export class BotRuntime {
const asked = store.listMessages(convId, 20).filter((m) => m.role === "user").at(-1)?.content;
markInflight(store, convId, asked || t("runtime.jobResult"));
const steps = new Map<string, ActivityStep>();
+ const said = new TurnSaid(out);
const unbind = agent.approvals.bindSink(convId, async (row) => void (await out.apply(interactiveOp(row))));
const halt = () => agent.stopTurn(convId);
fl.abort.signal.addEventListener("abort", halt, { once: true });
@@ -744,6 +796,7 @@ export class BotRuntime {
res = await agent.followTurn(botId, {
conversationId: convId,
model: routed,
+ observer: said.observer,
onStep: (step) => {
steps.set(step.id, step);
void out.apply({ type: "activity", step });
@@ -756,14 +809,14 @@ export class BotRuntime {
return;
}
const msg = turnFailedText(e as Error, [...steps.values()]);
- store.addMessage(convId, "assistant", msg, [...steps.values()]);
+ store.addMessage(convId, "assistant", msg, [...steps.values()], { interim: said.interim, files: said.presented });
await out.apply({ type: "stream.finish", text: msg });
this.answered(convId, fl);
return;
}
- const finalText = res.text.trim() || t("runtime.jobDoneNoText");
- store.addMessage(convId, "assistant", finalText, [...steps.values()]);
- await out.apply({ type: "stream.finish", text: finalText });
+ const reply = said.reply(res.text, t("runtime.jobDoneNoText"));
+ store.addMessage(convId, "assistant", reply.text, [...steps.values()], { interim: reply.interim, files: res.files });
+ await out.apply({ type: "stream.finish", text: reply.text });
for (const f of res.files ?? []) {
try {
await out.apply({ type: "file", path: f, name: path.basename(f) });
diff --git a/src/core/types.ts b/src/core/types.ts
index 04f47ed..2813bbf 100644
--- a/src/core/types.ts
+++ b/src/core/types.ts
@@ -52,8 +52,15 @@ export type OutboundOp =
| { type: "text"; text: string; ack?: boolean }
/** 代理回合的一步(新增或更新,按 id):只有网页对话显示,其它渠道忽略 */
| { type: "activity"; step: ActivityStep }
- /** 代理交付的本地文件(present):微信走 CDN 媒体消息,飞书上传文件/图片,网页列出文件名 */
+ /** 代理交付的本地文件(present):微信走 CDN 媒体消息,飞书上传文件/图片,网页显示成文件卡片(图片直接显示) */
| { type: "file"; path: string; name: string }
+ /**
+ * 模型在回合中间说的话(带工具调用的那一步里的文字),按说的顺序一段一段来:网页对话和飞书的流式卡片显示在回复前面,
+ * 微信不发(只发最后的回复,配额留给它)
+ */
+ | { type: "interim"; text: string }
+ /** 代理在回合中交付了一个文件(present):网页对话马上显示;文件本身照旧在回复之后由 file 发出,其它渠道忽略这个操作 */
+ | { type: "presented"; path: string; name: string }
/**
* ask_user / approval / secret_request 等交互,按能力映射到卡片按钮或编号文本。
* prompt 为完整的纯文本提示(文本渠道直接发送);buttons 供卡片渠道使用,value 会原样回传。
diff --git a/src/i18n/messages/web.ts b/src/i18n/messages/web.ts
index 807eb71..329f8ae 100644
--- a/src/i18n/messages/web.ts
+++ b/src/i18n/messages/web.ts
@@ -21,6 +21,7 @@ export const zh = {
requestDone: "请求已处理或已过期",
submitFailed: "提交失败",
conversationNotFound: "会话不存在",
+ fileNotFound: "找不到这个文件",
nameRequired: "名称不能为空",
budgetRange: "预算要在 0 到 100000 美元之间",
groupNotOwned: "群不属于该机器人",
@@ -59,6 +60,7 @@ export const en: Catalog<typeof zh> = {
requestDone: "This request was already handled or has expired",
submitFailed: "Couldn't submit",
conversationNotFound: "Conversation not found",
+ fileNotFound: "File not found",
nameRequired: "Name can't be empty",
budgetRange: "The budget must be between $0 and $100,000",
groupNotOwned: "This group doesn't belong to this bot",
diff --git a/src/store/db.ts b/src/store/db.ts
index 5dc9940..491044a 100644
--- a/src/store/db.ts
+++ b/src/store/db.ts
@@ -44,6 +44,16 @@ export interface Message {
createdAt: number;
/** 代理回合的活动记录有几步(内容按需用 getActivity 取) */
steps?: number;
+ /** 模型在回合中间说的话(按说的顺序),显示在 content 前面 */
+ interim?: string[];
+ /** 这条回复交付的文件(真实路径) */
+ files?: string[];
+}
+
+/** 一条助手消息除了正文之外的东西:回合中间说的话、交付的文件 */
+export interface MessageExtra {
+ interim?: string[];
+ files?: string[];
}
export interface WeixinAccount {
@@ -353,6 +363,12 @@ function parseJson<T>(v: unknown): T | undefined {
}
}
+/** JSON 字符串数组列;坏的、空的都当空数组 */
+function strings(v: unknown): string[] {
+ const a = parseJson<unknown>(v);
+ return Array.isArray(a) ? a.filter((x): x is string => typeof x === "string" && x !== "") : [];
+}
+
/** 按字段名改一行:cols 是字段 → 列名,布尔值存 0/1,对象存 JSON */
function updateRow(db: DatabaseSync, table: string, cols: Record<string, string>, id: string, patch: Record<string, unknown>): void {
const keys = Object.keys(patch).filter((k) => k in cols && patch[k] !== undefined);
@@ -513,6 +529,9 @@ const COLUMNS: Array<[string, string, string]> = [
["conversations", "kind", "TEXT NOT NULL DEFAULT 'dm'"],
// 代理回合的活动记录(JSON 数组,见 agent/activity.ts);只在助手消息上
["messages", "activity", "TEXT NOT NULL DEFAULT ''"],
+ // 模型在回合中间说的话、交付的文件(JSON 字符串数组);只在助手消息上
+ ["messages", "interim", "TEXT NOT NULL DEFAULT ''"],
+ ["messages", "files", "TEXT NOT NULL DEFAULT ''"],
// 主动消息的送达规则(core/delivery.ts)
["outbox", "purpose", "TEXT NOT NULL DEFAULT ''"],
["outbox", "subject", "TEXT NOT NULL DEFAULT ''"],
@@ -730,11 +749,12 @@ export class Store {
}
// ---------- messages ----------
- addMessage(conversationId: string, role: Message["role"], content: string, activity?: unknown[]): number {
+ addMessage(conversationId: string, role: Message["role"], content: string, activity?: unknown[], extra: MessageExtra = {}): number {
const now = Date.now();
+ const json = (a: unknown[] | undefined) => (a?.length ? JSON.stringify(a) : "");
const res = this.db
- .prepare("INSERT INTO messages(conversation_id, role, content, created_at, activity) VALUES(?, ?, ?, ?, ?)")
- .run(conversationId, role, content, now, activity?.length ? JSON.stringify(activity) : "");
+ .prepare("INSERT INTO messages(conversation_id, role, content, created_at, activity, interim, files) VALUES(?, ?, ?, ?, ?, ?, ?)")
+ .run(conversationId, role, content, now, json(activity), json(extra.interim), json(extra.files));
this.db.prepare("UPDATE conversations SET updated_at = ? WHERE id = ?").run(now, conversationId);
return Number(res.lastInsertRowid);
}
@@ -751,17 +771,28 @@ export class Store {
listMessages(conversationId: string, limit = 500): Message[] {
const rows = this.db
.prepare(
- "SELECT * FROM (SELECT id, conversation_id, role, content, created_at, CASE WHEN json_valid(activity) THEN json_array_length(activity) ELSE 0 END AS steps FROM messages WHERE conversation_id = ? ORDER BY id DESC LIMIT ?) ORDER BY id ASC",
+ "SELECT * FROM (SELECT id, conversation_id, role, content, created_at, interim, files, CASE WHEN json_valid(activity) THEN json_array_length(activity) ELSE 0 END AS steps FROM messages WHERE conversation_id = ? ORDER BY id DESC LIMIT ?) ORDER BY id ASC",
)
.all(conversationId, limit) as Row[];
- return rows.map((r) => ({
- id: Number(r.id),
- conversationId: String(r.conversation_id),
- role: String(r.role) as Message["role"],
- content: String(r.content),
- createdAt: Number(r.created_at),
- ...(Number(r.steps) ? { steps: Number(r.steps) } : {}),
- }));
+ return rows.map((r) => {
+ const interim = strings(r.interim);
+ const files = strings(r.files);
+ return {
+ id: Number(r.id),
+ conversationId: String(r.conversation_id),
+ role: String(r.role) as Message["role"],
+ content: String(r.content),
+ createdAt: Number(r.created_at),
+ ...(Number(r.steps) ? { steps: Number(r.steps) } : {}),
+ ...(interim.length ? { interim } : {}),
+ ...(files.length ? { files } : {}),
+ };
+ });
+ }
+ /** 这个会话里交付过的文件(真实路径,旧→新,去重) */
+ deliveredFiles(conversationId: string): string[] {
+ const rows = this.db.prepare("SELECT files FROM messages WHERE conversation_id = ? AND files != '' ORDER BY id").all(conversationId) as Row[];
+ return [...new Set(rows.flatMap((r) => strings(r.files)))];
}
/** 一条消息的活动记录(没有则为空数组) */
getActivity(conversationId: string, messageId: number): unknown[] {
diff --git a/src/tasks/service.ts b/src/tasks/service.ts
index f205485..af4f371 100644
--- a/src/tasks/service.ts
+++ b/src/tasks/service.ts
@@ -1202,7 +1202,8 @@ export class TaskService {
const text = outcome === "done" ? clip(msg, MESSAGE_MAX - (hint ? hint.length + 2 : 0)) || tr("tasks.done", { title: task.title }) : tr("tasks.notDone", { title: task.title, why: clip(msg || error || tr("tasks.noResult"), MESSAGE_MAX) });
const files = outcome === "done" ? this.files(task, report?.files ?? []) : [];
if (pushed) this.post(conv, task, id, purpose, text, files, hint);
- this.store.addMessage(conv.id, "assistant", files.length && conv.channel === "web" ? `${text}\n\n${files.map((f) => `- ${path.relative(this.o.host.workspace(task.botId), f)}`).join("\n")}` : text);
+ // 交付的文件跟着这条消息存下:网页对话里是文件卡片(图片直接显示),点开就能看
+ this.store.addMessage(conv.id, "assistant", text, undefined, { files });
this.store.updateRun(runId, { delivery: id });
this.note(conv.id, outcome === "done" ? `[后台任务「${task.title}」已完成,已把结果发给用户] ${text}` : `[后台任务「${task.title}」失败了,已告诉用户] ${text}`);
}
diff --git a/src/web/files.ts b/src/web/files.ts
new file mode 100644
index 0000000..70f3c4a
--- /dev/null
+++ b/src/web/files.ts
@@ -0,0 +1,176 @@
+/**
+ * 对话里交付的文件:网页里图片直接显示、点开在新标签页打开,桌面版用系统的默认程序打开、在文件夹中显示。
+ *
+ * 只给两种文件:这个会话交付过的,和这个机器人工作区里的。都按解析符号链接之后的真实路径判断,路径里不许有 ..;
+ * 数据目录里工作区以外的地方(数据库、令牌、密钥、别的机器人)一律不给。硬链接也不给:看不出它指向哪里。
+ *
+ * 代理生成的内容不在应用的源里运行:浏览器里直接显示的只有图片(SVG 除外)、PDF 和纯文本,其余一律下载(attachment);
+ * 全部带 nosniff,PDF 以外都加 CSP sandbox(浏览器自带的 PDF 阅读器在 sandbox 里打不开)。
+ * 桌面版里打开就会运行的文件(.exe、.bat、脚本…)不直接打开,只在文件夹中显示。
+ */
+import type http from "node:http";
+import fs from "node:fs";
+import path from "node:path";
+
+/** 界面上的一个交付文件:image 的在对话里直接显示;gone = 文件已经不在了 */
+export interface ShownFile {
+ path: string;
+ name: string;
+ size?: number;
+ image?: boolean;
+ gone?: boolean;
+}
+
+/** 在对话里直接显示的图片(SVG 能带脚本,不算) */
+export const IMAGE_TYPES: Record<string, string> = {
+ ".png": "image/png",
+ ".jpg": "image/jpeg",
+ ".jpeg": "image/jpeg",
+ ".gif": "image/gif",
+ ".webp": "image/webp",
+ ".bmp": "image/bmp",
+};
+
+const TEXT = "text/plain; charset=utf-8";
+/** 浏览器里可以直接显示的类型;其余一律下载 */
+const INLINE_TYPES: Record<string, string> = {
+ ...IMAGE_TYPES,
+ ".pdf": "application/pdf",
+ ".txt": TEXT,
+ ".log": TEXT,
+ ".md": TEXT,
+ ".csv": TEXT,
+ ".tsv": TEXT,
+ ".json": TEXT,
+};
+
+/** 打开就会运行的类型(Windows、macOS、Linux):桌面版只在文件夹中显示 */
+const RUNNABLE = new Set(
+ [
+ "exe com bat cmd msi msp mst msc msix msixbundle appx appxbundle ps1 psm1 psd1 vb vbs vbe js jse ws wsc wsf wsh sct hta scr pif cpl dll ocx sys",
+ "lnk url reg inf scf shb shs appref-ms application gadget settingcontent-ms library-ms search-ms diagcab chm hlp xll mde ade adp iso img vhd vhdx jar",
+ "docm dotm xlsm xltm xlam pptm potm ppam",
+ "app command tool terminal workflow pkg mpkg dmg sh bash zsh csh ksh fish run bin appimage desktop py pyw rb pl php",
+ ]
+ .join(" ")
+ .split(" ")
+ .map((e) => "." + e),
+);
+
+const isWin = process.platform === "win32";
+/** Windows 的路径不分大小写 */
+const norm = (p: string) => (isWin ? p.toLowerCase() : p);
+const samePath = (a: string, b: string) => norm(a) === norm(b);
+function inside(p: string, root: string): boolean {
+ const r = norm(root);
+ const x = norm(p);
+ return x === r || x.startsWith(r.endsWith(path.sep) ? r : r + path.sep);
+}
+
+function realpath(p: string): string | undefined {
+ try {
+ return fs.realpathSync(p);
+ } catch {
+ return undefined;
+ }
+}
+
+/** 界面要的样子:名字、大小、是不是直接显示的图片;文件不在了标 gone */
+export function describeFile(p: string, name = path.basename(p)): ShownFile {
+ try {
+ const st = fs.statSync(p);
+ if (!st.isFile()) throw new Error("not a file");
+ return { path: p, name, size: st.size, ...(IMAGE_TYPES[path.extname(name).toLowerCase()] ? { image: true } : {}) };
+ } catch {
+ return { path: p, name, gone: true };
+ }
+}
+
+/** 机器人的工作区 <数据目录>/workspaces/<id>(同 AgentHost.workspace);id 不是一段普通的名字时不给 */
+export function botWorkspace(dataDir: string, botId: string): string | undefined {
+ return /^[\w-][\w.-]*$/.test(botId) ? path.join(dataDir, "workspaces", botId) : undefined;
+}
+
+export interface FileScope {
+ /** 机器人工作区 */
+ workspace: string;
+ /** 数据目录:工作区以外的部分一律不给 */
+ dataDir: string;
+ /** 这个会话交付过的文件(存下的真实路径) */
+ delivered: string[];
+}
+
+export interface ResolvedFile {
+ /** 真实路径 */
+ path: string;
+ size: number;
+ mtimeMs: number;
+ mode: number;
+}
+
+/**
+ * 界面给的路径(交付时存下的真实路径,或相对工作区的路径)→ 能给出去的文件;不能给(不存在、越界、不是普通文件)返回 undefined。
+ */
+export function resolveFile(input: string, scope: FileScope): ResolvedFile | undefined {
+ if (!input || input.includes("\0") || input.split(/[\\/]/).includes("..")) return undefined;
+ const ws = realpath(scope.workspace);
+ const data = realpath(scope.dataDir);
+ if (!ws || !data) return undefined;
+ const real = realpath(path.resolve(ws, input));
+ if (!real) return undefined;
+ // 数据目录里只认这个机器人的工作区
+ if (inside(real, data) && !inside(real, ws)) return undefined;
+ if (!inside(real, ws) && !scope.delivered.some((d) => samePath(realpath(d) ?? "", real))) return undefined;
+ let st: fs.Stats;
+ try {
+ st = fs.statSync(real);
+ } catch {
+ return undefined;
+ }
+ if (!st.isFile() || st.nlink > 1) return undefined;
+ return { path: real, size: st.size, mtimeMs: st.mtimeMs, mode: st.mode };
+}
+
+/** 桌面版打开就会运行它:只在文件夹中显示 */
+export function runnable(f: Pick<ResolvedFile, "path" | "mode">): boolean {
+ const ext = path.extname(f.path).toLowerCase();
+ if (RUNNABLE.has(ext)) return true;
+ // 没有扩展名的可执行文件(macOS 会交给终端运行)
+ return !isWin && !ext && (f.mode & 0o111) !== 0;
+}
+
+/** 回给浏览器的头:能直接显示的 inline,其余 attachment;都带 nosniff,PDF 以外加 CSP sandbox */
+export function fileHeaders(name: string): Record<string, string> {
+ const ext = path.extname(name).toLowerCase();
+ const inline = INLINE_TYPES[ext];
+ const ascii = name.replace(/[^\x20-\x7e]/g, "_").replace(/["\\]/g, "_");
+ return {
+ "content-type": inline ?? "application/octet-stream",
+ "content-disposition": `${inline ? "inline" : "attachment"}; filename="${ascii}"; filename*=UTF-8''${encodeURIComponent(name)}`,
+ "x-content-type-options": "nosniff",
+ ...(ext === ".pdf" ? {} : { "content-security-policy": "sandbox" }),
+ // 别的网站不能把它嵌进自己的页面
+ "cross-origin-resource-policy": "same-origin",
+ "cache-control": "private, no-cache",
+ };
+}
+
+/** 把文件发给浏览器(带 ETag,没变时 304) */
+export function sendFile(req: http.IncomingMessage, res: http.ServerResponse, f: ResolvedFile): void {
+ const etag = `W/"${f.size.toString(36)}-${Math.floor(f.mtimeMs).toString(36)}"`;
+ const headers = { ...fileHeaders(path.basename(f.path)), etag };
+ if (req.headers["if-none-match"] === etag) {
+ res.writeHead(304, headers).end();
+ return;
+ }
+ const stream = fs.createReadStream(f.path);
+ stream.once("open", () => {
+ res.writeHead(200, { ...headers, "content-length": String(f.size) });
+ stream.pipe(res);
+ });
+ stream.once("error", () => {
+ if (!res.headersSent) res.writeHead(404, { "content-type": "text/plain; charset=utf-8" }).end("not found");
+ else res.destroy();
+ });
+ res.once("close", () => stream.destroy());
+}
diff --git a/src/web/live.ts b/src/web/live.ts
index bb18746..b179d63 100644
--- a/src/web/live.ts
+++ b/src/web/live.ts
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
import type { ActivityStep } from "../agent/activity.js";
import type { OutboundOp, ReplySession } from "../core/types.js";
import { opEvent } from "./sse-reply.js";
+import { describeFile, type ShownFile } from "./files.js";
export interface LivePrompt {
kind: string;
@@ -15,10 +16,12 @@ export interface LiveSnapshot {
id: string;
startedAt: number;
userText: string;
+ /** 模型在回合中间说的话(按说的顺序),显示在 text 前面 */
+ interim: string[];
text: string;
steps: ActivityStep[];
prompts: LivePrompt[];
- files: string[];
+ files: ShownFile[];
/** 已经按了停止,正在收尾 */
stopping: boolean;
/** 这条并进了它前面那条正在进行的处理:没有自己的回复,回复在前面那条里 */
@@ -37,9 +40,10 @@ export class LiveTurn implements ReplySession {
readonly id = crypto.randomUUID();
readonly startedAt = Date.now();
text = "";
+ readonly interim: string[] = [];
readonly steps = new Map<string, ActivityStep>();
readonly prompts: LivePrompt[] = [];
- readonly files: string[] = [];
+ readonly files: ShownFile[] = [];
stopping = false;
/** 并进了正在进行的处理(见 LiveSnapshot.steered) */
steered = false;
@@ -60,9 +64,15 @@ export class LiveTurn implements ReplySession {
if (this.done) return;
if (op.type === "text" && op.ack) this.steered = true;
if (op.type === "stream.update" || op.type === "stream.finish" || op.type === "text") this.text = op.text;
+ else if (op.type === "interim") this.interim.push(op.text);
else if (op.type === "activity") this.steps.set(op.step.id, op.step);
- else if (op.type === "file") this.files.push(op.name);
- else if (op.type === "interactive" && !this.prompts.some((p) => p.requestId && p.requestId === op.requestId)) {
+ else if (op.type === "file" || op.type === "presented") {
+ // 回合中已经显示过的(presented),结束时的 file 不再显示一遍
+ if (this.files.some((f) => f.path === op.path)) return;
+ const f = describeFile(op.path, op.name);
+ this.files.push(f);
+ return this.emit("file", f);
+ } else if (op.type === "interactive" && !this.prompts.some((p) => p.requestId && p.requestId === op.requestId)) {
this.prompts.push({ kind: op.kind, prompt: op.prompt, requestId: op.requestId, code: op.code });
}
const ev = opEvent(op);
@@ -74,6 +84,7 @@ export class LiveTurn implements ReplySession {
id: this.id,
startedAt: this.startedAt,
userText: this.userText,
+ interim: [...this.interim],
text: this.text,
steps: [...this.steps.values()],
prompts: [...this.prompts],
diff --git a/src/web/server.ts b/src/web/server.ts
index baf34d1..a942361 100644
--- a/src/web/server.ts
+++ b/src/web/server.ts
@@ -15,8 +15,9 @@ import { OAuthError } from "../account/oauth.js";
import { normalizeLine } from "../modelflare.js";
import { SseReply } from "./sse-reply.js";
import { LiveTurns, type LiveTurn } from "./live.js";
+import { botWorkspace, describeFile, resolveFile, runnable, sendFile } from "./files.js";
import { connectionFromPids } from "../agent/peer-guard.js";
-import type { ApprovalRow, Store } from "../store/db.js";
+import type { ApprovalRow, Message, Store } from "../store/db.js";
import { defaultLine } from "../agent/approvals.js";
import { normalizeSettings } from "../agent/settings.js";
import { scopeLabel } from "../agent/memory.js";
@@ -120,6 +121,11 @@ function publicBot(b: NonNullable<ReturnType<App["store"]["getBot"]>>) {
};
}
+/** 历史消息给界面:交付的文件带上名字、大小、是不是直接显示的图片、还在不在 */
+function publicMessage(m: Message) {
+ return m.files ? { ...m, files: m.files.map((p) => describeFile(p)) } : m;
+}
+
/** 网页“测试对话”渠道:收集输出 */
class CollectReply implements ReplySession {
text = "";
@@ -779,7 +785,7 @@ export async function startWebServer(app: App, opts: { port?: number; host?: str
if (seg.length === 3 && seg[2] === "messages" && m === "GET") {
const conv = app.store.getConversation(decodeURIComponent(seg[1]));
if (!conv) throw new HttpError(404, t("web.conversationNotFound"));
- return send(res, 200, { conversation: conv, messages: app.store.listMessages(conv.id) });
+ return send(res, 200, { conversation: conv, messages: app.store.listMessages(conv.id).map(publicMessage), canOpen: !!app.desktop?.openPath });
}
// 一条回复的代理活动记录(展开时才取)
if (seg.length === 5 && seg[2] === "messages" && seg[4] === "activity" && m === "GET") {
@@ -788,12 +794,33 @@ export async function startWebServer(app: App, opts: { port?: number; host?: str
// 正在处理的回合:处理了多久、最近一次进展、到目前为止的步骤(信息面板轮询)
if (seg.length === 3 && seg[2] === "turn" && m === "GET") {
const t = app.agent.turnsInProgress.get(decodeURIComponent(seg[1]));
- return send(res, 200, { turn: t ? { since: t.since, lastProgress: t.lastProgress, steps: t.activity.steps } : null });
+ return send(res, 200, { turn: t ? { since: t.since, lastProgress: t.lastProgress, steps: t.activity.steps, interim: t.interim } : null });
}
// 停止:网页消息(排队的也算)、纯对话、还在等代理启动的、代理回合,都在这里停
if (seg.length === 3 && seg[2] === "stop" && m === "POST") {
return send(res, 200, { stopped: stopConversation(decodeURIComponent(seg[1])) });
}
+ // 交付的文件(web/files.ts):GET file?path= 发给浏览器(图片、PDF、纯文本直接显示,其余下载);
+ // 桌面版 POST file/open 用系统的默认程序打开(打开就会运行的只在文件夹中显示),file/reveal 在文件夹中显示。
+ // 只给这个会话交付过的文件和这个机器人工作区里的文件
+ if (seg[2] === "file" && (seg.length === 3 ? m === "GET" : seg.length === 4 && (seg[3] === "open" || seg[3] === "reveal") && m === "POST")) {
+ const conv = app.store.getConversation(decodeURIComponent(seg[1]));
+ if (!conv) throw new HttpError(404, t("web.conversationNotFound"));
+ const desktop = app.desktop;
+ if (m === "POST" && !desktop?.openPath) throw new HttpError(400, t("web.desktopOnly"));
+ const input = m === "GET" ? url.searchParams.get("path") : (await readJson(req)).path;
+ const ws = botWorkspace(cfg.dataDir, conv.botId);
+ const f = ws ? resolveFile(String(input ?? ""), { workspace: ws, dataDir: cfg.dataDir, delivered: app.store.deliveredFiles(conv.id) }) : undefined;
+ if (!f) throw new HttpError(404, t("web.fileNotFound"));
+ if (m === "GET") return sendFile(req, res, f);
+ if (seg[3] === "open" && !runnable(f)) {
+ desktop!.openPath!(f.path);
+ return send(res, 200, { ok: true });
+ }
+ if (desktop!.showItemInFolder) desktop!.showItemInFolder(f.path);
+ else desktop!.openPath!(path.dirname(f.path));
+ return send(res, 200, { ok: true, revealed: true });
+ }
}
if (seg[0] === "bots") {
if (seg.length === 1) {
@@ -988,9 +1015,11 @@ export async function startWebServer(app: App, opts: { port?: number; host?: str
return send(res, 200, {
conversationId: consoleConvId(id),
model: conv?.model ?? "",
- messages: conv ? app.store.listMessages(conv.id, 200).filter((x) => x.id <= cut) : [],
+ messages: conv ? app.store.listMessages(conv.id, 200).filter((x) => x.id <= cut).map(publicMessage) : [],
pending: app.store.listApprovals({ status: "pending", conversationId: consoleConvId(id), limit: 50 }).map((r) => publicApproval(r, app.store)),
live: turns.map((t) => t.snapshot()),
+ // 桌面版:交付的文件用系统程序打开;网页里在新标签页打开
+ canOpen: !!app.desktop?.openPath,
});
}
// 接上进行中的消息(切走再切回、刷新后):先发 snapshot,再转发之后的事件(同 chat/stream),最后 done / error。
diff --git a/src/web/sse-reply.ts b/src/web/sse-reply.ts
index e97809e..600f8da 100644
--- a/src/web/sse-reply.ts
+++ b/src/web/sse-reply.ts
@@ -1,9 +1,11 @@
import type http from "node:http";
import type { OutboundOp, ReplySession } from "../core/types.js";
+import { describeFile } from "./files.js";
/**
- * 运行时的 OutboundOp → 网页对话的 SSE 事件:text(到目前为止的完整回复,覆盖式)、typing、prompt(审批/提问)、
- * file、activity(代理的一步,按 id 覆盖)。stream.start 不发。
+ * 运行时的 OutboundOp → 网页对话的 SSE 事件:text(到目前为止的完整回复,覆盖式)、interim(模型在回合中间说的一段话,追加)、
+ * typing、prompt(审批/提问)、file(交付的文件,见 ShownFile;回合中 present 时就发)、activity(代理的一步,按 id 覆盖)。
+ * stream.start 不发。
*/
export function opEvent(op: OutboundOp): [string, unknown] | undefined {
switch (op.type) {
@@ -16,8 +18,11 @@ export function opEvent(op: OutboundOp): [string, unknown] | undefined {
case "stream.finish":
case "text":
return ["text", { text: op.text, final: true }];
+ case "interim":
+ return ["interim", { text: op.text }];
case "file":
- return ["file", { name: op.name }];
+ case "presented":
+ return ["file", describeFile(op.path, op.name)];
case "interactive":
return ["prompt", { kind: op.kind, prompt: op.prompt, requestId: op.requestId, code: op.code }];
case "activity":
diff --git a/test/agent.integration.test.ts b/test/agent.integration.test.ts
index 6523d5e..a521c3f 100644
--- a/test/agent.integration.test.ts
+++ b/test/agent.integration.test.ts
@@ -243,12 +243,13 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
const urls = (Array.isArray(content) ? content : []).filter((p: any) => p?.type === "image_url").map((p: any) => String(p.image_url?.url ?? "").split(",")[0]);
return { text: `SEEN<<${urls.join(" ")}|${/\[图片 inbox\/[^\]]+\.png\]/.test(user) ? "path" : "nopath"}>>` };
}
- const scripts: Array<[RegExp, Array<{ name: string; args: any }>]> = [
+ const scripts: Array<[RegExp, Array<{ name: string; args: any; say?: string }>]> = [
[/QUICKSORT/, [{ name: "bash", args: { description: "run", command: "cat > qs.py <<'PY'\ndef qs(a):\n return a if len(a)<2 else qs([x for x in a[1:] if x<a[0]])+[a[0]]+qs([x for x in a[1:] if x>=a[0]])\nprint(qs([5,3,9,1,7,2]))\nPY\npython3 qs.py" } }]],
[/ESCALATE/, [{ name: "bash", args: { description: "run", command: "echo escalated-$((40+2))", sandbox_permissions: "danger-full-access", justification: "测试提权" } }]],
[/SECRET/, [{ name: "bash", args: { description: "run", command: `ls ${dataDir} 2>&1; cat ${path.join(dataDir, "anymodel.db")} >/dev/null 2>&1 && echo DB-READABLE || echo DB-HIDDEN` } }]],
[/READFILE/, [{ name: "read", args: { file_path: path.join(dataDir, "config.json") } }, { name: "read", args: { file_path: "qs.py" } }]],
- [/PRESENT/, [{ name: "bash", args: { description: "run", command: "printf 'hello deliverable' > report.txt" } }, { name: "present", args: { files: [{ path: "report.txt", description: "报告" }, { path: "/etc/hostname" }] } }]],
+ // 每步先说一句(回合中间说的话),再调工具
+ [/PRESENT/, [{ name: "bash", args: { description: "run", command: "printf 'hello deliverable' > report.txt" }, say: "我先写个报告。" }, { name: "present", args: { files: [{ path: "report.txt", description: "报告" }, { path: "/etc/hostname" }] }, say: "写好了,交给你。" }]],
[/ASKQ/, [{ name: "ask_user_question", args: { questions: [{ id: "lang", question: "用哪种语言?", options: [{ label: "Python" }, { label: "Go" }] }] } }]],
// 原生依赖与子进程:ripgrep(grep/glob)、PTC 的 Node 子进程(workflow)
[/NATIVES/, [
@@ -280,7 +281,7 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
];
for (const [re, steps] of scripts) {
if (!re.test(user)) continue;
- if (tools.length < steps.length) return { tool: steps[tools.length] };
+ if (tools.length < steps.length) return { tool: steps[tools.length], text: steps[tools.length].say };
if (/BGJOB/.test(user)) return { text: `STILL-RUNNING<<${tools.map((t) => textOf(t.content)).join("|")}>>` };
return { text: `RESULT<<${tools.map((t) => textOf(t.content)).join("|")}>>` };
}
@@ -355,6 +356,8 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
res.writeHead(200, { "content-type": "text/event-stream" });
const send = (o: any) => res.write(`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", model: b.model, ...o })}\n\n`);
if (step.tool) {
+ // 带工具调用的那一步也可以先说一句话(content 和 tool_calls 一起来)
+ if (step.text) send({ choices: [{ index: 0, delta: { role: "assistant", content: step.text } }] });
send({ choices: [{ index: 0, delta: { role: "assistant", tool_calls: [{ index: 0, id: "call_" + Math.random().toString(36).slice(2, 8), type: "function", function: { name: step.tool.name, arguments: JSON.stringify(step.tool.args) } }] } }] });
send({ choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] });
} else {
@@ -551,9 +554,20 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
expect(r.reply).toContain("def qs");
}, 120_000);
- it("present delivers workspace files only", async () => {
+ it("present delivers workspace files only; what the model says between tool calls and the delivered file stay in the history and the file opens", async () => {
const r = await chat("PRESENT report");
expect(r.files).toEqual(["report.txt"]);
+ const d = (await (await fetch(`${base}/amb/api/bots/default/chat`)).json()) as any;
+ const m = d.messages.at(-1);
+ expect(m.interim).toEqual(["我先写个报告。", "写好了,交给你。"]);
+ expect(m.content).toMatch(/^RESULT<</);
+ expect(m.files).toEqual([{ path: fs.realpathSync(path.join(dataDir, "workspaces", "default", "report.txt")), name: "report.txt", size: 17 }]);
+ // 处理过程里模型那一步不再重复说过的话
+ const steps = ((await (await fetch(`${base}/amb/api/conversations/${encodeURIComponent(d.conversationId)}/messages/${m.id}/activity`)).json()) as any).steps;
+ expect(JSON.stringify(steps)).not.toContain("我先写个报告");
+ const f = await fetch(`${base}/amb/api/conversations/${encodeURIComponent(d.conversationId)}/file?path=${encodeURIComponent(m.files[0].path)}`);
+ expect(f.headers.get("content-type")).toBe("text/plain; charset=utf-8");
+ expect(await f.text()).toBe("hello deliverable");
}, 120_000);
it("ask_user_question becomes a numbered prompt answered by reply", async () => {
diff --git a/test/chat-files.test.ts b/test/chat-files.test.ts
new file mode 100644
index 0000000..93ddd65
--- /dev/null
+++ b/test/chat-files.test.ts
@@ -0,0 +1,320 @@
+/**
+ * 对话里交付的文件:哪些文件能给(这个会话交付过的、这个机器人工作区里的;数据目录的其它地方、.. 、符号链接逃出去、硬链接都不给),
+ * 浏览器里怎么给(只有图片、PDF、纯文本直接显示,其余下载,都带 nosniff / CSP sandbox),桌面版打开 / 在文件夹中显示,
+ * 以及历史里存下的说的话和文件(重启后照样在、照样能打开)。
+ */
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { App, type DesktopHooks } from "../src/app.js";
+import { loadConfig } from "../src/config.js";
+import { Store } from "../src/store/db.js";
+import { botWorkspace, describeFile, fileHeaders, resolveFile, runnable } from "../src/web/files.js";
+
+const isWin = process.platform === "win32";
+/** 符号链接、硬链接在有的环境里建不了(Windows 没开开发者模式):建不了就跳过那一条 */
+function tryLink(fn: () => void): boolean {
+ try {
+ fn();
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+function layout() {
+ const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "amb-files-")));
+ const data = path.join(root, "data");
+ const ws = path.join(data, "workspaces", "b1");
+ const other = path.join(data, "workspaces", "b2");
+ const outside = path.join(root, "outside");
+ for (const d of [ws, other, outside, path.join(ws, "out"), path.join(data, "dsh", "b1")]) fs.mkdirSync(d, { recursive: true });
+ fs.writeFileSync(path.join(data, "anymodel.db"), "SQLite format 3");
+ fs.writeFileSync(path.join(data, "ui-token"), "SECRET-TOKEN");
+ fs.writeFileSync(path.join(data, "dsh", "b1", "config.yaml"), "key: x");
+ fs.writeFileSync(path.join(other, "theirs.txt"), "other bot");
+ fs.writeFileSync(path.join(outside, "secret.txt"), "outside");
+ fs.writeFileSync(path.join(ws, "out", "cover.png"), Buffer.from("89504e470d0a1a0a0000", "hex"));
+ fs.writeFileSync(path.join(ws, "page.html"), "<script>alert(1)</script>");
+ fs.writeFileSync(path.join(ws, "run.bat"), "echo hi");
+ return { root, data, ws, other, outside };
+}
+
+describe("哪些文件能给", () => {
+ const L = layout();
+ const scope = (delivered: string[] = []) => ({ workspace: L.ws, dataDir: L.data, delivered });
+ const ok = (p: string, delivered?: string[]) => resolveFile(p, scope(delivered))?.path;
+
+ it("工作区里的文件:绝对路径和相对工作区的路径都行,给的是真实路径", () => {
+ const png = path.join(L.ws, "out", "cover.png");
+ expect(ok(png)).toBe(png);
+ expect(ok("out/cover.png")).toBe(png);
+ expect(resolveFile(png, scope())).toMatchObject({ size: 10 });
+ });
+
+ it("路径里有 .. 一律不给(哪怕解析完还在工作区里)", () => {
+ expect(ok("../../anymodel.db")).toBeUndefined();
+ expect(ok("out/../out/cover.png")).toBeUndefined();
+ expect(ok(path.join(L.ws, "..", "..", "ui-token"))).toBeUndefined();
+ expect(ok(`${L.ws}${path.sep}..${path.sep}b2${path.sep}theirs.txt`)).toBeUndefined();
+ });
+
+ it("工作区外、没交付过的不给;数据目录里工作区以外的地方(数据库、令牌、DSH 配置、别的机器人)交付过也不给", () => {
+ expect(ok(path.join(L.outside, "secret.txt"))).toBeUndefined();
+ for (const p of [path.join(L.data, "anymodel.db"), path.join(L.data, "ui-token"), path.join(L.data, "dsh", "b1", "config.yaml"), path.join(L.other, "theirs.txt")]) {
+ expect(ok(p)).toBeUndefined();
+ expect(ok(p, [p])).toBeUndefined();
+ }
+ });
+
+ it("这个会话交付过的文件(不在数据目录里)能给", () => {
+ const p = path.join(L.outside, "secret.txt");
+ expect(ok(p, [p])).toBe(p);
+ });
+
+ it("符号链接逃出工作区:指向外面的文件、数据库、外面的文件夹,都不给", () => {
+ const made =
+ tryLink(() => fs.symlinkSync(path.join(L.outside, "secret.txt"), path.join(L.ws, "link.txt"))) &&
+ tryLink(() => fs.symlinkSync(path.join(L.data, "anymodel.db"), path.join(L.ws, "db-link"))) &&
+ tryLink(() => fs.symlinkSync(L.outside, path.join(L.ws, "outdir"), "dir"));
+ if (!made) return;
+ expect(ok("link.txt")).toBeUndefined();
+ expect(ok(path.join(L.ws, "db-link"))).toBeUndefined();
+ expect(ok("outdir/secret.txt")).toBeUndefined();
+ // 交付过的是链接本身:解析完是数据库,照样不给
+ expect(ok(path.join(L.ws, "db-link"), [path.join(L.ws, "db-link")])).toBeUndefined();
+ });
+
+ it("硬链接不给(看不出指向哪里);文件夹、不存在的、带 NUL 的都不给", () => {
+ if (tryLink(() => fs.linkSync(path.join(L.data, "anymodel.db"), path.join(L.ws, "hard.db")))) expect(ok("hard.db")).toBeUndefined();
+ expect(ok("out")).toBeUndefined();
+ expect(ok("nope.txt")).toBeUndefined();
+ expect(ok("out/cover.png\0.txt")).toBeUndefined();
+ expect(ok("")).toBeUndefined();
+ });
+
+ it("机器人 id 不是普通名字时没有工作区", () => {
+ expect(botWorkspace(L.data, "b1")).toBe(L.ws);
+ for (const id of ["..", "../b2", "a/b", "", ".hidden"]) expect(botWorkspace(L.data, id)).toBeUndefined();
+ });
+
+ it("界面上的样子:名字、大小、是不是直接显示的图片;不在了标 gone", () => {
+ expect(describeFile(path.join(L.ws, "out", "cover.png"))).toEqual({ path: path.join(L.ws, "out", "cover.png"), name: "cover.png", size: 10, image: true });
+ expect(describeFile(path.join(L.ws, "page.html"))).toMatchObject({ name: "page.html", size: 25 });
+ expect(describeFile(path.join(L.ws, "page.html")).image).toBeUndefined();
+ expect(describeFile(path.join(L.ws, "gone.svg"))).toEqual({ path: path.join(L.ws, "gone.svg"), name: "gone.svg", gone: true });
+ });
+});
+
+describe("浏览器里怎么给", () => {
+ it("图片(SVG 除外)、PDF、纯文本直接显示,其余一律下载;都带 nosniff,PDF 以外都有 CSP sandbox", () => {
+ for (const [name, type] of [["a.png", "image/png"], ["a.JPG", "image/jpeg"], ["a.webp", "image/webp"], ["a.txt", "text/plain; charset=utf-8"], ["a.json", "text/plain; charset=utf-8"]]) {
+ const h = fileHeaders(name);
+ expect(h["content-type"]).toBe(type);
+ expect(h["content-disposition"]).toMatch(/^inline;/);
+ expect(h["content-security-policy"]).toBe("sandbox");
+ expect(h["x-content-type-options"]).toBe("nosniff");
+ }
+ const pdf = fileHeaders("a.pdf");
+ expect(pdf).toMatchObject({ "content-type": "application/pdf", "x-content-type-options": "nosniff" });
+ expect(pdf["content-disposition"]).toMatch(/^inline;/);
+ expect(pdf["content-security-policy"]).toBeUndefined();
+ for (const name of ["a.html", "a.htm", "a.svg", "a.xml", "a.js", "a.mjs", "a.xhtml", "a.exe", "noext"]) {
+ const h = fileHeaders(name);
+ expect(h["content-type"]).toBe("application/octet-stream");
+ expect(h["content-disposition"]).toMatch(/^attachment;/);
+ expect(h["content-security-policy"]).toBe("sandbox");
+ expect(h["x-content-type-options"]).toBe("nosniff");
+ }
+ });
+
+ it("中文文件名:filename 是 ASCII 兜底,filename* 是 UTF-8", () => {
+ expect(fileHeaders("报告 \"v2\".pdf")["content-disposition"]).toBe(`inline; filename="__ _v2_.pdf"; filename*=UTF-8''${encodeURIComponent("报告 \"v2\".pdf")}`);
+ });
+
+ it("桌面版打开就会运行的文件只在文件夹中显示", () => {
+ for (const n of ["a.exe", "a.BAT", "a.cmd", "a.ps1", "a.js", "a.vbs", "a.lnk", "a.sh", "a.command", "a.jar", "a.py"]) expect(runnable({ path: n, mode: 0o644 })).toBe(true);
+ for (const n of ["a.png", "a.pdf", "a.txt", "a.docx", "a.html", "a.md"]) expect(runnable({ path: n, mode: 0o755 })).toBe(false);
+ expect(runnable({ path: "/w/tool", mode: 0o755 })).toBe(!isWin);
+ expect(runnable({ path: "/w/notes", mode: 0o644 })).toBe(false);
+ });
+});
+
+describe("接口与历史", () => {
+ let app: App;
+ let base: string;
+ let dataDir: string;
+ let ws: string;
+ const conv = "default:web:console";
+ const enc = encodeURIComponent;
+ const opened: Array<[string, string]> = [];
+ const get = (p: string, headers: Record<string, string> = {}) => fetch(`${base}/conversations/${enc(conv)}/file?path=${enc(p)}`, { headers });
+ const post = (action: string, p: string) => fetch(`${base}/conversations/${enc(conv)}/file/${action}`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ path: p }) });
+
+ beforeAll(async () => {
+ dataDir = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "amb-files-app-")));
+ app = new App(loadConfig({ dataDir, env: { AMB_MAGPIE_BASE_URL: "http://127.0.0.1:9/v1", AMB_MAGPIE_API_KEY: "k", AMB_UI_PORT: "0", AMB_AGENT: "off", AMB_WEIXIN_API_BASE: "http://127.0.0.1:9" } }), { printQrToTerminal: false });
+ await app.start();
+ base = app.web!.url.replace(/\/$/, "") + "/amb/api";
+ ws = path.join(dataDir, "workspaces", "default");
+ fs.mkdirSync(path.join(ws, "out"), { recursive: true });
+ fs.writeFileSync(path.join(ws, "out", "cover.png"), Buffer.from("89504e470d0a1a0a0000", "hex"));
+ fs.writeFileSync(path.join(ws, "out", "page.html"), "<script>alert(1)</script>");
+ fs.writeFileSync(path.join(ws, "out", "run.bat"), "echo hi");
+ app.store.ensureConversation("default", "web", "console", "控制台");
+ app.store.addMessage(conv, "user", "做张图");
+ app.store.addMessage(conv, "assistant", "做好了。", [{ id: "m1.1", kind: "model", title: "m", status: "ok", at: 1 }], {
+ interim: ["我先看看。", "开始画。"],
+ files: [path.join(ws, "out", "cover.png"), path.join(ws, "out", "page.html"), path.join(ws, "out", "gone.pdf")],
+ });
+ });
+ afterAll(async () => {
+ await app?.stop();
+ });
+
+ it("GET /chat:历史里有说的话和文件(名字、大小、图片、不在了),网页里 canOpen 是 false", async () => {
+ const d = (await (await fetch(`${base}/bots/default/chat`)).json()) as any;
+ expect(d.canOpen).toBe(false);
+ const m = d.messages.at(-1);
+ expect(m).toMatchObject({ role: "assistant", content: "做好了。", interim: ["我先看看。", "开始画。"], steps: 1 });
+ expect(m.files).toEqual([
+ { path: path.join(ws, "out", "cover.png"), name: "cover.png", size: 10, image: true },
+ { path: path.join(ws, "out", "page.html"), name: "page.html", size: 25 },
+ { path: path.join(ws, "out", "gone.pdf"), name: "gone.pdf", gone: true },
+ ]);
+ const c = (await (await fetch(`${base}/conversations/${enc(conv)}/messages`)).json()) as any;
+ expect(c.messages.at(-1).files[0].image).toBe(true);
+ expect(c.messages.at(-1).interim).toEqual(["我先看看。", "开始画。"]);
+ });
+
+ it("图片直接显示,HTML 下载;没变时 304", async () => {
+ const r = await get(path.join(ws, "out", "cover.png"));
+ expect(r.status).toBe(200);
+ expect(r.headers.get("content-type")).toBe("image/png");
+ expect(r.headers.get("content-disposition")).toMatch(/^inline; filename="cover.png"/);
+ expect(r.headers.get("x-content-type-options")).toBe("nosniff");
+ expect(r.headers.get("content-security-policy")).toBe("sandbox");
+ expect(r.headers.get("cross-origin-resource-policy")).toBe("same-origin");
+ expect(Buffer.from(await r.arrayBuffer()).toString("hex")).toBe("89504e470d0a1a0a0000");
+ const again = await get(path.join(ws, "out", "cover.png"), { "if-none-match": r.headers.get("etag")! });
+ expect(again.status).toBe(304);
+ const h = await get("out/page.html");
+ expect(h.status).toBe(200);
+ expect(h.headers.get("content-type")).toBe("application/octet-stream");
+ expect(h.headers.get("content-disposition")).toMatch(/^attachment;/);
+ await h.arrayBuffer();
+ });
+
+ it("数据库、令牌、.. 、不存在的文件、别的会话:404", async () => {
+ fs.writeFileSync(path.join(dataDir, "ui-token"), "SECRET");
+ for (const p of [path.join(dataDir, "anymodel.db"), path.join(dataDir, "ui-token"), "../../anymodel.db", path.join(ws, "..", "..", "anymodel.db"), path.join(ws, "out", "gone.pdf")]) {
+ const r = await get(p);
+ expect(r.status).toBe(404);
+ expect(((await r.json()) as any).error).toBe("找不到这个文件");
+ }
+ const r = await fetch(`${base}/conversations/${enc("nope:web:console")}/file?path=${enc(path.join(ws, "out", "cover.png"))}`);
+ expect(r.status).toBe(404);
+ await r.arrayBuffer();
+ });
+
+ it("桌面版:打开用系统程序(打开就会运行的只在文件夹中显示);在文件夹中显示;越界的不碰;网页里没有这两个操作", async () => {
+ const web = await post("open", path.join(ws, "out", "cover.png"));
+ expect(web.status).toBe(400);
+ await web.arrayBuffer();
+ const hooks: Partial<DesktopHooks> = { openPath: (p) => void opened.push(["open", p]), showItemInFolder: (p) => void opened.push(["reveal", p]) };
+ app.desktop = hooks as DesktopHooks;
+ try {
+ const d = (await (await fetch(`${base}/bots/default/chat`)).json()) as any;
+ expect(d.canOpen).toBe(true);
+ expect(await (await post("open", path.join(ws, "out", "cover.png"))).json()).toEqual({ ok: true });
+ expect(await (await post("open", path.join(ws, "out", "run.bat"))).json()).toEqual({ ok: true, revealed: true });
+ expect(await (await post("reveal", "out/page.html")).json()).toEqual({ ok: true, revealed: true });
+ for (const p of [path.join(dataDir, "anymodel.db"), "../../anymodel.db"]) {
+ const r = await post("open", p);
+ expect(r.status).toBe(404);
+ await r.arrayBuffer();
+ }
+ expect(opened).toEqual([
+ ["open", path.join(ws, "out", "cover.png")],
+ ["reveal", path.join(ws, "out", "run.bat")],
+ ["reveal", path.join(ws, "out", "page.html")],
+ ]);
+ } finally {
+ app.desktop = undefined;
+ }
+ });
+
+ it("重启之后:存下的说的话和文件照样在,交付过的文件照样认得", () => {
+ const file = path.join(dataDir, "restart.db");
+ const s1 = new Store(file);
+ s1.createBot({ id: "b1", name: "x" });
+ s1.ensureConversation("b1", "web", "console", "x");
+ s1.addMessage("b1:web:console", "assistant", "好了", undefined, { interim: ["先看看"], files: ["/w/a.png", "/w/b.pdf"] });
+ s1.addMessage("b1:web:console", "assistant", "又一个", undefined, { files: ["/w/a.png"] });
+ s1.close();
+ const s2 = new Store(file);
+ expect(s2.listMessages("b1:web:console").map((m) => [m.content, m.interim, m.files])).toEqual([
+ ["好了", ["先看看"], ["/w/a.png", "/w/b.pdf"]],
+ ["又一个", undefined, ["/w/a.png"]],
+ ]);
+ expect(s2.deliveredFiles("b1:web:console")).toEqual(["/w/a.png", "/w/b.pdf"]);
+ s2.close();
+ });
+});
+
+describe("界面:交付的文件", () => {
+ /** 组件返回的 vnode 树里找节点(htm 生成的是 preact 的 vnode:type + props.children) */
+ function find(node: any, pred: (n: any) => boolean, out: any[] = []): any[] {
+ if (Array.isArray(node)) node.forEach((n) => find(n, pred, out));
+ else if (node && typeof node === "object") {
+ if (pred(node)) out.push(node);
+ find(node.props?.children, pred, out);
+ }
+ return out;
+ }
+ const load = async () => {
+ (globalThis as any).window ??= { matchMedia: () => ({ matches: false, addEventListener() {}, removeEventListener() {} }), addEventListener() {}, removeEventListener() {} };
+ // @ts-expect-error 界面代码是不经构建的 JS
+ const files = await import("../ui/components/files.js");
+ // @ts-expect-error 同上
+ const ui = await import("../ui/lib/i18n.js");
+ ui.setLangPref("zh-CN");
+ return files;
+ };
+ const conv = "b1:web:console";
+ const pdf = { path: "/d/workspaces/b1/报告.pdf", name: "报告.pdf", size: 1258291 };
+ const url = `/amb/api/conversations/${encodeURIComponent(conv)}/file?path=${encodeURIComponent(pdf.path)}`;
+
+ it("网页:整张卡片是链接,在新标签页打开;名字下面是大小;没有「在文件夹中显示」", async () => {
+ const { FileCard, fileUrl } = await load();
+ expect(fileUrl(conv, pdf.path)).toBe(url);
+ const v = FileCard({ file: pdf, conv, canOpen: false });
+ expect(v.type).toBe("a");
+ expect(v.props).toMatchObject({ class: "file-card", href: url, target: "_blank", rel: "noopener", title: "打开 报告.pdf" });
+ expect(find(v, (n) => n.props?.class === "fsub num")[0].props.children).toBe("1.2 MB");
+ expect(find(v, (n) => n.props?.icon === "folder")).toHaveLength(0);
+ });
+
+ it("桌面版:点卡片用系统程序打开,右边是「在文件夹中显示」;找不到的文件不能点", async () => {
+ const { FileCard } = await load();
+ const v = FileCard({ file: pdf, conv, canOpen: true });
+ expect(v.type).toBe("div");
+ expect(find(v, (n) => n.type === "button" && n.props?.class === "file-open")).toHaveLength(1);
+ expect(find(v, (n) => n.props?.icon === "folder")[0].props.label).toBe("在文件夹中显示");
+ const gone = FileCard({ file: { ...pdf, gone: true }, conv, canOpen: true });
+ expect(gone.props.class).toBe("file-card off");
+ expect(find(gone, (n) => n.type === "a" || n.type === "button")).toHaveLength(0);
+ expect(find(gone, (n) => n.props?.class === "fsub num")[0].props.children).toBe("找不到文件");
+ });
+
+ it("图片:缩略图直接显示,网页里点开是新标签页,桌面版是按钮;其它文件是卡片", async () => {
+ const { ImageFile, DeliveredFiles } = await load();
+ const png = { path: "/d/workspaces/b1/cover.png", name: "cover.png", size: 10, image: true };
+ // ImageFile 用了 useState:在组件外调用时给它一个假的 hooks 环境太麻烦,这里看 DeliveredFiles 把图片和文件分给谁
+ const list = DeliveredFiles({ files: [png, pdf], conv, canOpen: false });
+ const kids = find(list, (n) => n.type === ImageFile || (n.type && n.type.name === "FileCard"));
+ expect(kids.map((k) => k.props.file.name)).toEqual(["cover.png", "报告.pdf"]);
+ expect(DeliveredFiles({ files: [], conv, canOpen: false })).toBeNull();
+ });
+});
diff --git a/test/chat-stream.unit.test.ts b/test/chat-stream.unit.test.ts
new file mode 100644
index 0000000..55ca80c
--- /dev/null
+++ b/test/chat-stream.unit.test.ts
@@ -0,0 +1,227 @@
+/**
+ * 模型在回合中间说的话(interim)和回合中交付的文件(presented):运行时怎么发给渠道、怎么存进历史;
+ * 网页的进行中消息(LiveTurn)、微信(不多发)、飞书流式卡片(写进卡片)怎么接;处理过程里模型那一步不再重复这些话。
+ */
+import { describe, expect, it } from "vitest";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { BotRuntime, TurnSaid } from "../src/core/runtime.js";
+import { Store } from "../src/store/db.js";
+import { TurnStopped, interimText, type TurnObserver } from "../src/agent/host.js";
+import { ActivityLog } from "../src/agent/activity.js";
+import { DEFAULT_SETTINGS } from "../src/agent/settings.js";
+import { LiveTurn } from "../src/web/live.js";
+import { WeixinReply } from "../src/channels/weixin/channel.js";
+import { WeixinQuota } from "../src/channels/weixin/quota.js";
+import { FeishuReply } from "../src/channels/feishu/card.js";
+import type { AgentManager } from "../src/agent/manager.js";
+import type { InboundEvent, OutboundOp } from "../src/core/types.js";
+import { FakeFeishu } from "./helpers.js";
+
+const dir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-said-"));
+const ws = path.join(dir, "ws");
+fs.mkdirSync(ws);
+const png = path.join(fs.realpathSync(ws), "cover.png");
+fs.writeFileSync(png, Buffer.from("89504e470d0a1a0a", "hex"));
+const pdf = path.join(fs.realpathSync(ws), "report.pdf");
+fs.writeFileSync(pdf, "%PDF-1.4");
+
+const ev = (text: string, channel: InboundEvent["channel"] = "web"): InboundEvent => ({
+ id: Math.random().toString(36),
+ channel,
+ botId: "b1",
+ conversation: { kind: "dm", id: "console" },
+ sender: { id: "owner" },
+ parts: [{ type: "text", text }],
+ replyHandle: {},
+ receivedAt: Date.now(),
+});
+
+/** 假的代理:script 拿到观察者,像宿主那样陆续报"说了一段话""交付了文件",返回这一轮的结果(或抛错) */
+function setup(script: (o: TurnObserver) => Promise<{ text: string; files?: string[] }>) {
+ const store = new Store(path.join(dir, `${Math.random().toString(36).slice(2)}.db`));
+ store.createBot({ id: "b1", name: "测试" });
+ const host = { state: "running", workspace: ws, diagnostics: () => ({}) };
+ const agent = {
+ available: true,
+ host: () => host,
+ approvals: { bindSink: () => () => {}, handleReply: () => ({ handled: false }), pending: () => [] },
+ turnsInProgress: new Map(),
+ settings: () => DEFAULT_SETTINGS,
+ memoryPrefix: () => "",
+ takeSeed: () => "",
+ runTurn: async (_bot: string, input: any) => {
+ input.observer?.onStart?.();
+ input.observer?.onProgress?.("🔧 bash");
+ const r = await script(input.observer);
+ return { sessionId: "s1", usage: { tokens: 0, window: 0, maxTokens: 0 }, files: [], ...r };
+ },
+ stopTurn: () => false,
+ } as unknown as AgentManager;
+ const rt = new BotRuntime({ store, models: { listModels: async () => ["m1"], streamChat: async () => "纯对话" } as any, defaultModel: () => "m1", contextMessages: 20, contextChars: 20_000, agent });
+ const ops: OutboundOp[] = [];
+ const out = { apply: async (op: OutboundOp) => void ops.push(op) };
+ const last = () => store.listMessages("b1:web:console").at(-1)!;
+ return { rt, store, ops, out, last };
+}
+
+const kinds = (ops: OutboundOp[]) => ops.filter((o) => o.type !== "typing" && o.type !== "stream.start").map((o) => ("text" in o ? `${o.type}:${o.text}` : "path" in o ? `${o.type}:${path.basename(o.path)}` : o.type));
+
+describe("运行时:回合中间说的话和交付的文件", () => {
+ it("说的话一段段马上发出,文件在 present 时就给网页看;结束时回复照旧,历史里按顺序存下说的话和文件", async () => {
+ const { rt, ops, out, last } = setup(async (o) => {
+ o.onInterim?.("我先看看下载目录里有什么。");
+ o.onPresent?.([png]);
+ o.onInterim?.("找到 12 张截图。");
+ o.onPresent?.([png, pdf]);
+ return { text: "整理好了。", files: [png, pdf] };
+ });
+ await rt.handle(ev("整理截图"), out);
+ expect(kinds(ops)).toEqual([
+ "interim:我先看看下载目录里有什么。",
+ "presented:cover.png",
+ "interim:找到 12 张截图。",
+ "presented:report.pdf",
+ "stream.finish:整理好了。",
+ "file:cover.png",
+ "file:report.pdf",
+ ]);
+ expect(last()).toMatchObject({ role: "assistant", content: "整理好了。", interim: ["我先看看下载目录里有什么。", "找到 12 张截图。"], files: [png, pdf] });
+ });
+
+ it("最后没再说话:回合中间说的最后一句就是回复(微信收到的也是它),不在说的话里重复", async () => {
+ const { rt, ops, out, last } = setup(async (o) => {
+ o.onInterim?.("我先看看。");
+ o.onInterim?.("图做好了,发给你:");
+ return { text: "", files: [png] };
+ });
+ await rt.handle(ev("做张图"), out);
+ expect(kinds(ops).filter((k) => k.startsWith("stream.finish"))).toEqual(["stream.finish:图做好了,发给你:"]);
+ expect(last()).toMatchObject({ content: "图做好了,发给你:", interim: ["我先看看。"], files: [png] });
+ });
+
+ it("什么都没说:照旧是「已完成,没有文字回复」,历史里没有多余的字段", async () => {
+ const { rt, out, last } = setup(async () => ({ text: "" }));
+ await rt.handle(ev("hi"), out);
+ expect(last().content).toMatch(/已完成/);
+ expect(last().interim).toBeUndefined();
+ expect(last().files).toBeUndefined();
+ });
+
+ it("出错或中止:之前说过的话和交付过的文件留在历史里,回复是出错的说明", async () => {
+ const { rt, out, last, ops } = setup(async (o) => {
+ o.onInterim?.("我先看看。");
+ o.onPresent?.([png]);
+ throw new TurnStopped("user", 3000);
+ });
+ await rt.handle(ev("干活"), out);
+ expect(last().content).toMatch(/^已中止/);
+ expect(last()).toMatchObject({ interim: ["我先看看。"], files: [png] });
+ // 出错时文件不再发到渠道
+ expect(kinds(ops).filter((k) => k.startsWith("file:"))).toEqual([]);
+ });
+
+ it("等代理启动时的临时文字:模型说的第一段话来了就换掉", async () => {
+ const ops: OutboundOp[] = [];
+ const said = new TurnSaid({ apply: async (op) => void ops.push(op) });
+ said.provisional = true;
+ said.observer.onInterim("第一段");
+ said.observer.onInterim("第二段");
+ expect(kinds(ops)).toEqual(["stream.update:", "interim:第一段", "interim:第二段"]);
+ });
+});
+
+describe("渠道怎么接", () => {
+ it("网页(LiveTurn):snapshot 里有说的话和文件;present 过的文件结束时不再显示一遍;文件带名字、大小、是不是图片", async () => {
+ const t = new LiveTurn("b1:web:console", "b1", "整理截图", 0);
+ const events: Array<[string, any]> = [];
+ t.subscribe((e, d) => events.push([e, d]));
+ await t.apply({ type: "interim", text: "我先看看。" });
+ await t.apply({ type: "presented", path: png, name: "cover.png" });
+ await t.apply({ type: "stream.finish", text: "好了" });
+ await t.apply({ type: "file", path: png, name: "cover.png" });
+ await t.apply({ type: "file", path: pdf, name: "report.pdf" });
+ expect(events.map(([e]) => e)).toEqual(["interim", "file", "text", "file"]);
+ const s = t.snapshot();
+ expect(s.interim).toEqual(["我先看看。"]);
+ expect(s.text).toBe("好了");
+ expect(s.files).toEqual([
+ { path: png, name: "cover.png", size: 8, image: true },
+ { path: pdf, name: "report.pdf", size: 8 },
+ ]);
+ });
+
+ it("微信:回合中间说的话和 present 都不发,只发最后的回复和文件(配额)", async () => {
+ const store = new Store(path.join(dir, "wx.db"));
+ store.upsertWeixinPeer({ botId: "b", userId: "u1", contextToken: "ctx", quotaRemaining: 10, windowStartedAt: Date.now() });
+ const sent: string[] = [];
+ const client = { sendText: async (_u: string, t: string) => void sent.push(t), getTypingTicket: async () => "ticket", sendTyping: async () => {} } as any;
+ const reply = new WeixinReply(client, "u1", "ctx", new WeixinQuota(store, "b"));
+ await reply.apply({ type: "stream.start" });
+ await reply.apply({ type: "interim", text: "我先看看。" });
+ await reply.apply({ type: "presented", path: png, name: "cover.png" });
+ await reply.apply({ type: "interim", text: "快好了。" });
+ await reply.apply({ type: "stream.finish", text: "整理好了。" });
+ await new Promise((r) => setTimeout(r, 10));
+ expect(sent).toEqual(["整理好了。"]);
+ store.close();
+ });
+
+ it("飞书:说的话写进流式卡片,最终回复接在后面,卡片上留着全部;回复就是最后一句时不重复", async () => {
+ const t = new FakeFeishu();
+ const r = new FeishuReply(t, "om_1", 5);
+ await r.apply({ type: "stream.start" });
+ await r.apply({ type: "stream.update", text: "代理已就绪,正在处理…" });
+ await r.apply({ type: "stream.update", text: "" });
+ await r.apply({ type: "interim", text: "我先看看。" });
+ await new Promise((res) => setTimeout(res, 30));
+ expect(t.cards.get("card1")).toBe("我先看看。");
+ await r.apply({ type: "interim", text: "找到了。" });
+ await r.apply({ type: "stream.finish", text: "整理好了。" });
+ expect(t.cards.get("card1")).toBe("我先看看。\n\n找到了。\n\n整理好了。");
+ expect(t.finished.has("card1")).toBe(true);
+ expect(t.texts).toEqual([]);
+
+ const t2 = new FakeFeishu();
+ const r2 = new FeishuReply(t2, "om_2", 5);
+ await r2.apply({ type: "stream.start" });
+ await r2.apply({ type: "interim", text: "我先看看。" });
+ await r2.apply({ type: "interim", text: "图做好了:" });
+ await r2.apply({ type: "stream.finish", text: "图做好了:" });
+ expect(t2.cards.get("card1")).toBe("我先看看。\n\n图做好了:");
+ });
+});
+
+describe("处理过程不重复模型说的话", () => {
+ const T0 = 1_800_000_000_000;
+ const msg = (step: number, content: any[], extra: Record<string, unknown> = {}) => ({ turn: 1, step, message: { role: "assistant", content, source: { model: "grok-4.7" } }, ...extra });
+ const call = { type: "tool-call", id: "c1", name: "bash", arguments: "{}" };
+
+ it("聊天回合:模型那一步只留调用了什么和思考;最后的回答那一步不再带回答", () => {
+ const log = new ActivityLog(undefined, "grok-4.7", { chat: true });
+ log.apply("assistant/message", msg(1, [{ type: "reasoning", text: "看目录" }, { type: "text", text: "我先看看下载目录里有什么。" }, call]), T0);
+ expect(log.last).toMatchObject({ kind: "model", summary: "调用 bash" });
+ expect(log.last!.output).toContain("看目录");
+ expect(log.last!.output).not.toContain("我先看看");
+ log.apply("assistant/message", msg(2, [{ type: "text", text: "整理好了。" }]), T0 + 10);
+ expect(log.last!.summary).toBeUndefined();
+ expect(log.last!.output).toBeUndefined();
+ });
+
+ it("被打断的那一步说的话不进对话:步骤里照旧留着;任务(不是聊天)的步骤照旧", () => {
+ const log = new ActivityLog(undefined, "grok-4.7", { chat: true });
+ log.apply("assistant/message", msg(1, [{ type: "text", text: "说到一半" }, call], { interrupted: true }), T0);
+ expect(log.last!.summary).toBe("说到一半");
+ const task = new ActivityLog(undefined, "grok-4.7");
+ task.apply("assistant/message", msg(1, [{ type: "text", text: "我先看看。" }, call]), T0);
+ expect(task.last!.summary).toBe("我先看看。");
+ });
+
+ it("interimText:只认带工具调用、没被打断的那一步的文字", () => {
+ expect(interimText(msg(1, [{ type: "text", text: " 我先看看。 " }, call]))).toBe("我先看看。");
+ expect(interimText(msg(1, [{ type: "text", text: "最后的回答" }]))).toBe("");
+ expect(interimText(msg(1, [{ type: "text", text: "半句" }, call], { interrupted: true }))).toBe("");
+ expect(interimText(msg(1, [call]))).toBe("");
+ });
+});
diff --git a/test/server-compaction.unit.test.ts b/test/server-compaction.unit.test.ts
index ce55f95..391158e 100644
--- a/test/server-compaction.unit.test.ts
+++ b/test/server-compaction.unit.test.ts
@@ -766,7 +766,7 @@ describe("服务端压缩:活动记录与模型资料", () => {
expect(mgr.serverCompaction).toBeDefined();
store.setAgentSession("c1", "b1", "sess-1");
const activity = new ActivityLog();
- mgr.turnsInProgress.set("c1", { botId: "b1", since: 0, lastProgress: 0, activity, abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0 });
+ mgr.turnsInProgress.set("c1", { botId: "b1", since: 0, lastProgress: 0, activity, abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0, interim: [] });
(mgr as any).onServerCompaction({ session: "sess-1", model: "mf/good", ok: true, before: 210_000, after: 31_000, at: 1000, ms: 4200 });
(mgr as any).onServerCompaction({ session: "sess-1", model: "mf/good", ok: false, before: 210_000, at: 2000, ms: 10, error: "HTTP 404" });
(mgr as any).onServerCompaction({ session: "other", model: "mf/good", ok: true, before: 1, after: 1, at: 3000, ms: 1 });
diff --git a/ui/components/files.js b/ui/components/files.js
new file mode 100644
index 0000000..a9c18dd
--- /dev/null
+++ b/ui/components/files.js
@@ -0,0 +1,48 @@
+// 对话里交付的文件(代理的 present、后台任务的结果):图片直接显示成缩略图,其它是一张文件卡片。
+// 桌面版点开用系统的默认程序打开(打开就会运行的文件,服务端改成在文件夹中显示),卡片上另有「在文件夹中显示」;
+// 网页里点开在新标签页打开(浏览器不该直接显示的类型,服务端让它下载)。文件由 /conversations/:id/file 提供,见 src/web/files.ts。
+import { useState } from "preact/hooks";
+import { html, Icon, IconButton } from "./ui.js";
+import { api } from "../lib/api.js";
+import { failed } from "../lib/store.js";
+import { fmtSize } from "../lib/format.js";
+import { t } from "../lib/i18n.js";
+
+const enc = encodeURIComponent;
+export const fileUrl = (conv, path) => `/amb/api/conversations/${enc(conv)}/file?path=${enc(path)}`;
+
+/** 桌面版:open 用系统程序打开,reveal 在文件夹中显示 */
+function desktop(conv, path, action) {
+ return api(`/conversations/${enc(conv)}/file/${action}`, { method: "POST", body: { path } }).catch(failed);
+}
+
+/** 文件卡片:名字 + 大小;不在了的不能点 */
+export function FileCard({ file, conv, canOpen }) {
+ const sub = file.gone ? t("chat.file.gone") : file.size != null ? fmtSize(file.size) : "";
+ const body = html`<${Icon} name="file" size="xl" class="fic" />
+ <span class="grow"><span class="fname">${file.name}</span>${sub && html`<span class="fsub num">${sub}</span>`}</span>`;
+ if (file.gone || !conv) return html`<div class="file-card off">${body}</div>`;
+ if (!canOpen) return html`<a class="file-card" href=${fileUrl(conv, file.path)} target="_blank" rel="noopener" title=${t("chat.file.open", { name: file.name })}>${body}</a>`;
+ return html`<div class="file-card">
+ <button type="button" class="file-open" title=${t("chat.file.open", { name: file.name })} onClick=${() => desktop(conv, file.path, "open")}>${body}</button>
+ <${IconButton} icon="folder" label=${t("chat.file.reveal")} onClick=${() => desktop(conv, file.path, "reveal")} />
+ </div>`;
+}
+
+/** 图片:缩略图(保持比例,限宽限高),点开看大图;读不出来时退回文件卡片 */
+export function ImageFile({ file, conv, canOpen, onLoad }) {
+ const [broken, setBroken] = useState(false);
+ if (broken || file.gone || !conv) return html`<${FileCard} file=${file} conv=${conv} canOpen=${canOpen} />`;
+ const img = html`<img src=${fileUrl(conv, file.path)} alt=${file.name} loading="lazy" onLoad=${onLoad} onError=${() => setBroken(true)} />`;
+ const title = t("chat.file.open", { name: file.name });
+ if (!canOpen) return html`<a class="msg-img" href=${fileUrl(conv, file.path)} target="_blank" rel="noopener" title=${title}>${img}</a>`;
+ return html`<button type="button" class="msg-img" title=${title} onClick=${() => desktop(conv, file.path, "open")}>${img}</button>`;
+}
+
+/** 一条回复交付的文件,按交付的顺序;放得下就排在一行 */
+export function DeliveredFiles({ files, conv, canOpen, onLoad }) {
+ if (!files?.length) return null;
+ return html`<div class="msg-files">
+ ${files.map((f) => (f.image ? html`<${ImageFile} key=${f.path} file=${f} conv=${conv} canOpen=${canOpen} onLoad=${onLoad} />` : html`<${FileCard} key=${f.path} file=${f} conv=${conv} canOpen=${canOpen} />`))}
+ </div>`;
+}
diff --git a/ui/components/ui.js b/ui/components/ui.js
index 77b35db..70b5ca3 100644
--- a/ui/components/ui.js
+++ b/ui/components/ui.js
@@ -31,6 +31,7 @@ const P = {
paste: "M5.5 2.75h5v1.5h-5zM10.5 3.5h1.75c.41 0 .75.34.75.75v9c0 .41-.34.75-.75.75h-8.5a.75.75 0 0 1-.75-.75v-9c0-.41.34-.75.75-.75H5.5",
trash: "M2.75 4.25h10.5M6.25 4.25V2.75h3.5v1.5M4 4.25l.6 8.6c.03.4.36.7.75.7h5.3c.4 0 .72-.3.75-.7l.6-8.6",
file: "M9 1.75H4.25a.75.75 0 0 0-.75.75v11c0 .41.34.75.75.75h7.5c.41 0 .75-.34.75-.75V5.25L9 1.75ZM9 1.75v3.5h3.5M5.75 8.25h4.5M5.75 10.75h4.5",
+ folder: "M2.25 4.25c0-.41.34-.75.75-.75h3.1l1.5 1.5H13c.41 0 .75.34.75.75v6.5c0 .41-.34.75-.75.75H3a.75.75 0 0 1-.75-.75v-8Z",
refresh: "M13.25 8a5.25 5.25 0 1 1-1.54-3.71M13.25 2.75v2.5h-2.5",
stop: { f: "M5.75 4h4.5c.97 0 1.75.78 1.75 1.75v4.5c0 .97-.78 1.75-1.75 1.75h-4.5C4.78 12 4 11.22 4 10.25v-4.5C4 4.78 4.78 4 5.75 4Z" },
more: { f: dot(3.5, 8, 1.25) + dot(8, 8, 1.25) + dot(12.5, 8, 1.25) },
diff --git a/ui/css/chat.css b/ui/css/chat.css
index 529149e..527f931 100644
--- a/ui/css/chat.css
+++ b/ui/css/chat.css
@@ -241,13 +241,31 @@ button.act-head:active { background: var(--pill-hover); }
white-space: pre-wrap; word-break: break-all; color: var(--fg-2); max-height: 160px; overflow: auto;
}
.card .answer { display: flex; gap: var(--ctl-gap); }
+/* 交付的文件(components/files.js):按交付的顺序挂在回复的文字下面,放得下就排在一行。
+ 文件卡片是一块 --well,整块可点(打开),悬停深一级;桌面版右边一个「在文件夹中显示」。找不到的文件不能点 */
+.msg-files { display: flex; flex-wrap: wrap; align-items: flex-start; gap: var(--space-2); }
+.msg-row .msg + .msg-files { margin-top: var(--space-3); }
+.msg-row :is(.msg, .msg-files) + .notice { margin-top: var(--space-3); }
+/* 回合中交付的:轻轻浮起(已经在的不重播) */
+.msg-row.live .msg-files > * { --rise: 6px; animation: rise-in var(--dur-moderate) var(--ease-out); }
.file-card {
display: flex; align-items: center; gap: var(--space-3); width: min(360px, 100%); padding: var(--space-2-5) var(--space-3-5) var(--space-2-5) var(--space-2-5);
- background: var(--well); border-radius: var(--r-well);
+ background: var(--well); border-radius: var(--r-well); color: var(--fg); text-align: left; transition: background var(--t-fast);
}
-.file-card > .ic { --ic-size: var(--ic-xl); margin: var(--space-2); color: var(--fg-2); } /* 24px 图标 + 四周 8 = 40 见方,和两行文字等高 */
+a.file-card:hover { text-decoration: none; }
+a.file-card:hover, .file-card:has(> .file-open:hover) { background: var(--well-hover); }
+a.file-card:active, .file-card:has(> .file-open:active) { background: var(--well-press); }
+.file-card:has(> .file-open) { padding-right: var(--space-2); }
+.file-open { display: flex; align-items: center; gap: var(--space-3); flex: 1; min-width: 0; text-align: left; border-radius: var(--r-sm); }
+.file-card .fic { flex: none; --ic-size: var(--ic-xl); margin: var(--space-2); color: var(--fg-2); } /* 24px 图标 + 四周 8 = 40 见方,和两行文字等高 */
+.file-card .grow { display: grid; min-width: 0; }
.file-card .fname { font-size: var(--fs-lg); font-weight: var(--fw-medium); color: var(--fg); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.file-card .fsub { margin-top: var(--space-0-5); font-size: var(--fs-xs); color: var(--muted); }
+.file-card.off .fname { color: var(--muted); }
+/* 图片直接显示:缩略图保持比例,限宽限高(读出来之前先占一小块 --well);点开看大图 */
+.msg-img { display: block; max-width: 100%; overflow: hidden; border-radius: var(--r-well); background: var(--well); cursor: zoom-in; transition: transform var(--t-fast); }
+.msg-img:active { transform: scale(var(--press)); }
+.msg-img img { display: block; width: auto; height: auto; min-width: 48px; min-height: 48px; max-width: min(320px, 100%); max-height: 240px; }
.msg-row .notice { max-width: min(var(--bubble-w), 100%); }
.chat-empty { margin: auto; padding-top: 16vh; display: grid; justify-items: center; gap: var(--space-2-5); text-align: center; color: var(--muted); }
diff --git a/ui/design/README.md b/ui/design/README.md
index 18d0e92..209a321 100644
--- a/ui/design/README.md
+++ b/ui/design/README.md
@@ -311,7 +311,8 @@ JS 组件(`ui/components/ui.js`)只是给常用组合一个带逻辑的写
| 思考 | 工作状态行 | 比气泡晚 `--stagger` 淡入;"思考中"是 `sheen` 微光:`--muted` 底上一道 `--fg-2` 的光,`--dur-sheen` 扫过去再停一拍 |
| 输出 | 新段落 / 列表项 | 新挂载的块淡入(moderate),已在的不重播;最后一段末尾一个 `breathe` 小点(代码块、表格结尾不画) |
| 收尾 | 状态行、操作 | "用时"换字淡入、细线淡入;短于 3 秒的整行平滑收起;最后一条回复的复制 / 时刻晚一拍淡入 |
-| 插入 | 提问 / 审批卡、文件、出错提示 | `rise-in`(6px,moderate) |
+| 插入 | 提问 / 审批卡、出错提示 | `rise-in`(6px,moderate) |
+| 说话与交付 | 模型在回合中间说的话、交付的文件 | 说的话按顺序写进回复正文(同"输出"),处理过程里不再重复;文件挂在正文下面,回合中交付时 `rise-in`,图片读完前先占一小块 `--well` |
| 输入框 | 投影、发送钮 | 聚焦时投影 moderate 长大;空时箭头小一号,有字时 `--ease-back` 回弹到位;发出后停止方块 `scale-in`,结束后箭头 `rise-in` 升回(只在发过之后);点发送 / 停止不抢输入框焦点 |
| 其他 | 首次加载、回到底部 | 历史读到后整列淡入一次;回到底部 `rise-in` 出现、`sink-out` 退场,新消息点 `scale-in` |
diff --git a/ui/design/gallery.js b/ui/design/gallery.js
index 6cb0eef..4560c87 100644
--- a/ui/design/gallery.js
+++ b/ui/design/gallery.js
@@ -360,6 +360,8 @@ function StateRow({ name, note, ctx = "surface", cells }) {
</figure>`;
}
+// 回复里先是模型在回合中间说的话,最后的回复接在后面(chat.js 的 shownText)
+const INTERIM_REPLY = "我先看看下载目录里有什么。\n\n找到 12 张截图,按日期分进了 3 个文件夹,顺手挑了一张做封面。\n\n整理好了:截图都在「截图/2026-10」下面,清单和封面在下面。";
const MD = "支持 **粗体**、`行内代码`、[链接](https://modelflare.dev):\n\n- 列表项\n- 第二项\n\n```js\nconst ok = true;\n```";
function Gallery() {
@@ -594,7 +596,6 @@ const { mounted, leaving } = usePresence(open); /* design/motion.js:退场
<div class="msg-row assistant"><div class="card"><div class="meta"><span>「竞品周报」想问你</span></div><div class="q-block"><div class="q">报告用中文还是英文?</div><div class="q-detail">不回复就按中文做</div>
<div class="opts"><button type="button" class="opt"><span class="letter">A</span><span>中文</span></button>
<button type="button" class="opt"><span class="letter">B</span><span>英文</span></button></div></div></div></div>
- <div class="msg-row assistant"><div class="file-card"><${Icon} name="file" size="xl" /><div class="grow"><div class="fname">report.pdf</div><div class="fsub">已交付到机器人工作区</div></div></div></div>
</div>
<div class="composer"><textarea rows="1" placeholder="给「我的助手」发消息,/ 查看命令"></textarea>
<div class="composer-bar"><button type="button" class="plus" aria-label="更多"><${Icon} name="plus" size="md" /></button><span class="grow"></span>
@@ -602,6 +603,23 @@ const { mounted, leaving } = usePresence(open); /* design/motion.js:退场
<button type="button" class="send" aria-label="发送"><${Icon} name="up" size="md" strong /></button></div></div>
<div class="chips"><button type="button" class="chip">总结今天的消息</button><button type="button" class="chip">写一封邮件</button></div>`}
<//>
+ <${Demo} col title="回合中间说的话与交付的文件" note="模型在工具调用之间说的话按顺序写在回复里,最后的回复接在后面,处理过程只列步骤;present 交付的文件挂在文字下面:图片直接显示(限宽 320、限高 240,保持比例,点开看大图),其它是文件卡片(桌面版点开用系统程序打开,右边在文件夹中显示;网页里点开在新标签页打开),找不到的文件不能点">
+ ${() => html`<div class="gal-chat">
+ <div class="msg-row user"><div class="msg">把下载目录里的截图整理一下,挑一张做封面</div></div>
+ <div class="msg-row assistant live"><div class="activity"><button type="button" class="work-line toggle live" aria-expanded="false"><span class="wl shimmer">运行 bash,已用 6 秒</span><${Icon} name="chevRight" size=${12} class="chev" /></button></div>
+ <div class="msg"><${Markdown} text="我先看看下载目录里有什么。" /></div></div>
+ <div class="msg-row assistant last"><div class="activity"><button type="button" class="work-line toggle" aria-expanded="false"><span class="wl">用时 18 秒</span><${Icon} name="chevRight" size=${12} class="chev" /></button></div>
+ <div class="msg"><${Markdown} text=${INTERIM_REPLY} /></div>
+ <div class="msg-files">
+ <button type="button" class="msg-img" title="打开 cover.png"><img src="../assets/guide-demo.svg" alt="cover.png" /></button>
+ <div class="file-card"><button type="button" class="file-open" title="打开 截图清单.pdf"><${Icon} name="file" size="xl" class="fic" /><span class="grow"><span class="fname">截图清单.pdf</span><span class="fsub num">1.2 MB</span></span></button>
+ <${IconButton} icon="folder" label="在文件夹中显示" /></div>
+ <a class="file-card" href="#patterns" title="打开 整理记录.md"><${Icon} name="file" size="xl" class="fic" /><span class="grow"><span class="fname">整理记录.md</span><span class="fsub num">2.4 KB</span></span></a>
+ <div class="file-card off"><${Icon} name="file" size="xl" class="fic" /><span class="grow"><span class="fname">旧的截图.zip</span><span class="fsub">找不到文件</span></span></div>
+ </div>
+ <div class="msg-actions"><${CopyButton} text=${INTERIM_REPLY} /><span class="when num">10:02</span></div></div>
+ </div>`}
+ <//>
<//>
<${Section} id="needs" title="需要你" lead="所有机器人里在等你的事:提问和审批在前,再是失败、没送到、登录,问要不要改为执行的最后。侧栏那一行和托盘角标只在有事时出现;一件一块,操作在行里,点标题行展开任务详情。">
diff --git a/ui/locales/en/chat.js b/ui/locales/en/chat.js
index ed90ce1..c4aa347 100644
--- a/ui/locales/en/chat.js
+++ b/ui/locales/en/chat.js
@@ -39,7 +39,9 @@ export default {
submit: "Submit",
},
file: {
- delivered: "Delivered to the bot's workspace; on WeChat and Feishu it's sent directly",
+ open: "Open {name}",
+ reveal: "Show in folder",
+ gone: "File not found",
},
work: {
stopping: "Stopping…",
diff --git a/ui/locales/zh-CN/chat.js b/ui/locales/zh-CN/chat.js
index e384dcb..3311278 100644
--- a/ui/locales/zh-CN/chat.js
+++ b/ui/locales/zh-CN/chat.js
@@ -39,7 +39,9 @@ export default {
submit: "提交",
},
file: {
- delivered: "已交付到机器人工作区,微信和飞书会直接发送",
+ open: "打开 {name}",
+ reveal: "在文件夹中显示",
+ gone: "找不到文件",
},
/** 回复上方的工作状态;{d} 是 fmtDur 的用时 */
work: {
diff --git a/ui/screens/chat.js b/ui/screens/chat.js
index 20e7a7e..faa5478 100644
--- a/ui/screens/chat.js
+++ b/ui/screens/chat.js
@@ -8,6 +8,7 @@ import { ModelPicker, shortModel, sameModel } from "../components/model-picker.j
import { PwshHint } from "../components/pwsh-hint.js";
import { ActivityLine, currentStep, upsertStep } from "../components/activity.js";
import { TaskChip } from "../components/tasks.js";
+import { DeliveredFiles } from "../components/files.js";
import { GuideChecklist, MediaDemo } from "../components/guide.js";
import { api, attachChat, streamChat } from "../lib/api.js";
import { confirmSheet, failed, saved, useStore } from "../lib/store.js";
@@ -41,23 +42,32 @@ const errText = (s) => s.replace(/^⚠️\s*/, "");
// 服务端报错的文字(两种语言都认)
const isAuthError = (s) => /HTTP 401\b|\b401\b.*(key|密钥|unauthor)/i.test(s); // i18n-ignore
-// steps:代理回合的活动记录有几步(展开时按 mid 取)
+// steps:代理回合的活动记录有几步(展开时按 mid 取);interim:模型在回合中间说的话;files:交付的文件
function toItems(messages) {
return messages.map((m) =>
m.role === "assistant" && isError(m.content)
- ? { kind: "error", id: "m" + m.id, mid: m.id, text: errText(m.content), at: m.createdAt, steps: m.steps }
- : { kind: "msg", id: "m" + m.id, mid: m.id, role: m.role === "user" ? "user" : "assistant", text: m.content, at: m.createdAt, steps: m.steps },
+ ? { kind: "error", id: "m" + m.id, mid: m.id, text: errText(m.content), at: m.createdAt, steps: m.steps, interim: m.interim, files: m.files }
+ : { kind: "msg", id: "m" + m.id, mid: m.id, role: m.role === "user" ? "user" : "assistant", text: m.content, at: m.createdAt, steps: m.steps, interim: m.interim, files: m.files },
);
}
+/** 回复显示的全文:模型在回合中间说的话在前,按说的顺序,最后的回复接着(最后没再说话时回复就是中间说的最后一句,不重复) */
+function shownText(it) {
+ const before = it.interim || [];
+ const rest = before.length && before.at(-1) === it.text ? before.slice(0, -1) : before;
+ return [...rest, it.text].filter(Boolean).join("\n\n");
+}
+
+/** 按路径加一个交付的文件(回合中 present 时来一次,结束时可能再来一次) */
+const addFile = (files, f) => ((files || []).some((x) => x.path === f.path) ? files : [...(files || []), f]);
+
// 进行中的一条消息(服务端的 live 快照):用户消息 + 到目前为止的回复;turn 标记这些项属于哪条进行中的消息。
// 代理自己醒来的一轮(后台命令结束)没有用户消息;并进前一条处理的消息没有自己的回复
function liveItems(t, cards = []) {
return [
...(t.userText ? [{ kind: "msg", id: "u" + t.id, role: "user", text: t.userText, at: t.startedAt, turn: t.id }] : []),
...cards.map((c) => ({ ...c, turn: t.id })),
- ...(t.steered ? [] : [{ kind: "msg", id: "a" + t.id, role: "assistant", text: t.text, at: t.startedAt, live: true, startedAt: t.startedAt, activity: t.steps, turn: t.id }]),
- ...t.files.map((name, i) => ({ kind: "file", id: `f${t.id}-${i}`, name, turn: t.id })),
+ ...(t.steered ? [] : [{ kind: "msg", id: "a" + t.id, role: "assistant", text: t.text, interim: t.interim, files: t.files, at: t.startedAt, live: true, startedAt: t.startedAt, activity: t.steps, turn: t.id }]),
];
}
@@ -158,16 +168,6 @@ export function PromptCard({ requestId, fallback }) {
</div>`;
}
-function FileCard({ name }) {
- return html`<div class="file-card">
- <${Icon} name="file" size="xl" />
- <div class="grow">
- <div class="fname">${name}</div>
- <div class="fsub">${t("chat.file.delivered")}</div>
- </div>
- </div>`;
-}
-
/**
* 回复上方的工作状态,一句话:没出字时"思考中 N 秒",有一步在做时是那一步"运行 bash,已用 N 秒",出字后"正在回复 N 秒",
* 结束后"用时 N 秒"(短于 3 秒且没有步骤时不显示)。
@@ -182,18 +182,19 @@ function WorkLine({ live, stopping, text, startedAt, workedMs, steps, count, loa
}, [live]);
const ms = live ? Date.now() - startedAt : workedMs;
if (!live && !(ms >= 3000) && !steps?.length && !count) return null;
- const cur = live && !text ? currentStep(steps) : "";
+ // 有一步在做时说那一步(模型在回合中间说过话也一样);没有时出了字是"正在回复"
+ const cur = live ? currentStep(steps) : "";
// 按了停止:代理要重启进程,可能要几秒,先说清楚在停
const long = ms >= 3000;
const label = live
? stopping
? long ? t("chat.work.stoppingAfter", { d: fmtDur(ms) }) : t("chat.work.stopping")
- : text ? t("chat.work.replying", { d: fmtDur(ms) })
: cur ? (long ? t("activity.stepFor", { step: cur, d: fmtDur(ms) }) : cur)
+ : text ? t("chat.work.replying", { d: fmtDur(ms) })
: long ? t("chat.work.thinkingFor", { d: fmtDur(ms) }) : t("chat.work.thinking")
: long ? t("chat.work.done", { d: fmtDur(ms) }) : t("chat.work.process");
// 换阶段(思考 → 回复 → 用时)时文字淡入:key 变才重播,秒数跳动不重播
- const phase = !live ? "done" : stopping ? "stop" : text ? "reply" : "think";
+ const phase = !live ? "done" : stopping ? "stop" : !cur && text ? "reply" : "think";
return html`<${ActivityLine} label=${label} phase=${phase} live=${live} steps=${steps} count=${count} load=${load} />`;
}
@@ -224,6 +225,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
const [slashOff, setSlashOff] = useState(false);
const [loadErr, setLoadErr] = useState("");
const [cue, setCue] = useState(null); // 头像的一次性反应(出错);完成时头像自己会播
+ const [canOpen, setCanOpen] = useState(false); // 桌面版:交付的文件用系统程序打开
const lang = useLang();
const scroller = useRef(null);
const stick = useRef(true);
@@ -276,6 +278,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
if (!mounted.current) return;
convId.current = d.conversationId || "";
setConvModel(d.model || "");
+ setCanOpen(!!d.canOpen);
const list = toItems(d.messages || []);
const reply = ended && list.findLast((x) => x.role !== "user");
if (reply && reply.at >= ended.since) reply.workedMs = ended.ms;
@@ -389,7 +392,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
{
snapshot: (d) => {
finalText = d.text;
- patchLive(liveId, (it) => ({ ...it, text: d.text, activity: d.steps, startedAt: d.startedAt }));
+ patchLive(liveId, (it) => ({ ...it, text: d.text, interim: d.interim || [], files: d.files || [], activity: d.steps, startedAt: d.startedAt }));
d.prompts.forEach(card);
if (d.stopping && mounted.current) setStopping(true);
},
@@ -397,14 +400,17 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
finalText = d.text;
patchLive(liveId, (it) => ({ ...it, text: d.text }));
},
+ // 模型在回合中间说的一段话:接在已经说过的后面
+ interim: (d) => patchLive(liveId, (it) => ({ ...it, interim: [...(it.interim || []), d.text] })),
activity: (d) => patchLive(liveId, (it) => ({ ...it, activity: upsertStep(it.activity, d) })),
prompt: (d) => {
notify(bot.name, t("notify.prompt"), `amb-prompt-${bot.id}`);
card(d);
},
+ // 交付的文件(回合中 present 时就来):挂在这条回复下面,图片直接显示
file: (d) => {
markDone(isMediaFile(d.name) ? "media" : "hands");
- if (mounted.current) setItems((l) => [...l, { kind: "file", id: uid(), name: d.name, turn, enter: true }]);
+ patchLive(liveId, (it) => ({ ...it, files: addFile(it.files, d) }));
},
stopping: () => mounted.current && setStopping(true),
error: (d) => {
@@ -505,8 +511,9 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
}
const { turn, ...it } = item; // 结束了:不再属于进行中的消息
if (it.id !== liveId) out.push(it);
- else if (isError(finalText)) out.push({ kind: "error", id: liveId, text: errText(finalText), retry: msg.startsWith("/") ? undefined : msg, at: it.at, activity: it.activity, workedMs: Date.now() - it.startedAt, enter: true });
- else if (finalText || it.activity?.length) out.push({ ...it, text: finalText, live: false, workedMs: Date.now() - it.startedAt });
+ else if (isError(finalText))
+ out.push({ kind: "error", id: liveId, text: errText(finalText), retry: msg.startsWith("/") ? undefined : msg, at: it.at, activity: it.activity, interim: it.interim, files: it.files, workedMs: Date.now() - it.startedAt, enter: true });
+ else if (finalText || it.activity?.length || it.interim?.length || it.files?.length) out.push({ ...it, text: finalText, live: false, workedMs: Date.now() - it.startedAt });
}
return isReset && !isError(finalText) ? out.filter((x) => x.id === liveId) : out;
});
@@ -650,6 +657,11 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
}, [items, tasks]);
const chips = (it) => (chipsAfter.get(it.id) || []).map((t) => html`<${TaskChip} key=${t.id} task=${t} onChanged=${loadTasks} />`);
+ // 图片读完了高度才定下来:本来就跟着底部时,再贴一次底
+ const onMedia = () => {
+ if (stick.current && scroller.current) scroller.current.scrollTop = scroller.current.scrollHeight;
+ };
+ const files = (it) => html`<${DeliveredFiles} files=${it.files} conv=${convId.current || `${bot.id}:web:console`} canOpen=${canOpen} onLoad=${onMedia} />`;
const rows = useMemo(() => {
const out = [];
let prev = null;
@@ -663,24 +675,27 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
const cont = prev && (prev.kind === "msg" ? prev.role : "assistant") === role;
const cls = `msg-row ${role}${cont ? " cont" : ""}${it.enter ? " enter" : ""}${it.live ? " live" : ""}`;
if (it.kind === "msg") {
+ // 回复:工作状态行(处理过程折叠在里面)、模型说的话(回合中间说的在前)、交付的文件
+ const shown = it.role === "user" ? it.text : shownText(it);
out.push(html`<div class=${`${cls}${it.id === lastAi ? " last" : ""}`} key=${it.id}>
${it.role === "user"
? html`<div class="msg"><${Linkified} text=${it.text} /></div><${MsgActions} text=${it.text} at=${it.at} />`
: html`${(it.live || it.workedMs || it.steps) && html`<${WorkLine} live=${it.live} stopping=${it.live && stopping} text=${it.text} startedAt=${it.startedAt} workedMs=${it.workedMs}
steps=${it.activity} count=${it.steps} load=${it.mid ? loadActivity(it.mid) : undefined} />`}
- ${it.text && html`<div class="msg"><${Markdown} text=${it.text} /></div>`}
+ ${shown && html`<div class="msg"><${Markdown} text=${shown} /></div>`}
+ ${files(it)}
${chips(it)}
- ${!it.live && html`<${MsgActions} text=${it.text} at=${it.at} />`}`}
+ ${!it.live && html`<${MsgActions} text=${shown} at=${it.at} />`}`}
</div>`);
} else if (it.kind === "prompt") {
out.push(html`<div class=${cls} key=${it.id}><${PromptCard} requestId=${it.requestId} fallback=${it.prompt} /></div>`);
- } else if (it.kind === "file") {
- out.push(html`<div class=${cls} key=${it.id}><${FileCard} name=${it.name} /></div>`);
} else if (it.kind === "media-demo") {
out.push(html`<div class=${cls} key=${it.id}><${MediaDemo} prompt=${it.text} /></div>`);
} else if (it.kind === "error") {
out.push(html`<div class=${cls} key=${it.id}>
${(it.activity?.length || it.steps) && html`<${WorkLine} workedMs=${it.workedMs} steps=${it.activity} count=${it.steps} load=${it.mid ? loadActivity(it.mid) : undefined} />`}
+ ${it.interim?.length > 0 && html`<div class="msg"><${Markdown} text=${it.interim.join("\n\n")} /></div>`}
+ ${files(it)}
<${ErrorNotice} text=${it.text} onRetry=${it.retry ? () => send(it.retry) : undefined} onFixKey=${() => onOpenSettings("modelflare", "connection")} />
</div>`);
}
@@ -689,7 +704,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
prev = it;
}
return out;
- }, [items, generating, stopping, chipsAfter, lang]);
+ }, [items, generating, stopping, chipsAfter, lang, canOpen]);
const empty = items && items.length === 0;
const showGuide = isActive(g) && !g.seen.includes("intro") && !!guideStep(g);
diff --git a/ui/screens/info-panel.js b/ui/screens/info-panel.js
index 0064ddb..445c627 100644
--- a/ui/screens/info-panel.js
+++ b/ui/screens/info-panel.js
@@ -9,6 +9,7 @@ import { markDone } from "../lib/guide.js";
import { fmtAgo, fmtClock, fmtDur, fmtTime } from "../lib/format.js";
import { channelName as accessChannel } from "../lib/access.js";
import { PausedLine, TaskGroup } from "../components/tasks.js";
+import { DeliveredFiles } from "../components/files.js";
import { t } from "../lib/i18n.js";
export function usePoll(fn, ms, deps = []) {
@@ -185,6 +186,7 @@ function LiveTurn({ turn, convId }) {
const label = step ? t("activity.stepFor", { step, d }) : t("info.turn.processing", { d });
return html`<div class="transcript-turn">
<${ActivityLine} label=${label} live steps=${turn.steps} />
+ ${turn.interim?.length > 0 && html`<div class="transcript-msg"><div class="bubble selectable">${turn.interim.join("\n\n")}</div></div>`}
<div class="cluster">
${idle >= 60_000 && html`<span class="hint">${t("info.turn.idle", { d: fmtDur(idle) })}</span>`}
<span class="grow"></span>
@@ -210,7 +212,8 @@ function ConversationSheet({ conv, onClose }) {
${d && !d.messages.length && !turn && html`<span class="hint">${t("info.noMessages")}</span>`}
${d && d.messages.map((m) => html`<div class=${`transcript-msg${m.role === "user" ? " user" : ""}`}>
${m.steps && html`<${ActivityLine} label=${t("info.turn.activity")} count=${m.steps} load=${() => api(`/conversations/${enc}/messages/${m.id}/activity`).then((x) => x.steps)} />`}
- <div class="bubble selectable">${m.content}</div>
+ <div class="bubble selectable">${[...(m.interim || []), m.content].join("\n\n")}</div>
+ <${DeliveredFiles} files=${m.files} conv=${conv.id} canOpen=${d.canOpen} />
<div class="hint">${fmtTime(m.createdAt)}</div>
</div>`)}
${turn && html`<${LiveTurn} key=${turn.since} turn=${turn} convId=${conv.id} />`}