Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix: 同步自动化问题副本的结束状态

Zhehui Bi298bbc1c83f7
diff --git a/docs/verification/automation-reports.md b/docs/verification/automation-reports.md
index e9aa3cefdb..b1f08e2b51 100644
--- a/docs/verification/automation-reports.md
+++ b/docs/verification/automation-reports.md
@@ -16,7 +16,10 @@ the chat it reports to (the bot's main chat unless the routine chose another):
it reaches the bot in that chat with the report in hand.
- **A copy of a card** the run waits on (a question, an approval, a proposal).
Answering the copy, or giving it a 👍 or 👎, answers the run in its own
- conversation; both cards then show the same answer.
+ conversation. A copied question clears "needs you" when its original is
+ answered, dismissed or expired, including after the person continues the
+ run's conversation as a normal chat. Startup checks existing question copies
+ against their originals for runs still in history.
- **One line naming a sign-in, connection or credential** the run waits on
("I need you to sign in to example.com."), marked "needs you" until that card
is settled; **See run** opens it. A sign-in can also be finished from the
diff --git a/server/automation-reports.e2e.test.ts b/server/automation-reports.e2e.test.ts
index 18e323e301..6db01b12b2 100644
--- a/server/automation-reports.e2e.test.ts
+++ b/server/automation-reports.e2e.test.ts
@@ -209,7 +209,10 @@ it("copies a question a run waits on into the main chat, and the answer given th
await s.api("POST", `/api/threads/${s.main}/respond`, { requestId: copy.card.requestId, behavior: "answer", message: "Work" });
await expect.poll(() => answers.find((answer) => answer.id === "which-account"), { timeout: 20_000 }).toMatchObject({ t: "answer", behavior: "answer", message: "Work", source: "user" });
await s.settled(id, "running");
- await expect.poll(async () => (await s.messages(s.main)).find((message) => message.id === copy.id)?.card?.answered, { timeout: 20_000 }).toBeTruthy();
+ await expect.poll(async () => {
+ const settledCopy = (await s.messages(s.main)).find((message) => message.id === copy.id);
+ return [settledCopy?.card?.answered, settledCopy?.automation?.needsYou];
+ }, { timeout: 20_000 }).toEqual(["answer", undefined]);
expect((await s.messages(threadId)).find((message) => message.id === original.id)?.card?.answered).toBeTruthy();
writeFileSync(gate, "done");
await s.reported(id, "posted");
diff --git a/server/automation-reports.test.ts b/server/automation-reports.test.ts
index 9e3e941299..a2464793d2 100644
--- a/server/automation-reports.test.ts
+++ b/server/automation-reports.test.ts
@@ -469,6 +469,57 @@ describe("automation reports", () => {
expect(f.added()).toEqual([]);
});
+ it.each([
+ { answered: "answer", answeredText: "Work" },
+ { dismissed: true },
+ { expired: true },
+ ])("settles a copied question after the person took over: %j", (resolution) => {
+ const f = fixture();
+ const run = f.finished(null, "Inbox brief", { status: "waiting" });
+ const card = { title: "Question", subtitle: "Which account?", options: ["Work"], requestId: "ask-1", requestType: "question" as const };
+ const original = f.store.appendMessage(run.threadId!, { role: "bot", kind: "options", card });
+ const [copy] = f.lines();
+ expect(copy?.automation).toMatchObject({ copyOf: original.id, needsYou: true });
+ f.store.patchTask(f.bot.id, run.threadId!, { routineRunId: undefined });
+ f.store.patchTask(f.bot.id, f.main, { unread: false });
+
+ f.store.patchMessage(run.threadId!, original.id, { card: { ...card, ...resolution } });
+
+ expect(f.lines()).toEqual([expect.objectContaining({ id: copy!.id, card: { ...card, ...resolution } })]);
+ expect(f.lines()[0]!.automation).not.toHaveProperty("needsYou");
+ expect(f.store.taskByThread(f.bot.id, f.main)?.unread).toBe(false);
+ expect(f.notified).toEqual([]);
+ });
+
+ it("reconciles only matching, already copied questions on startup", () => {
+ const f = fixture();
+ const run = f.finished(null, "Inbox brief", { status: "waiting" });
+ const card = { title: "Question", subtitle: "Which account?", options: ["Work"], requestId: "ask-1", requestType: "question" as const };
+ const original = f.store.appendMessage(run.threadId!, { role: "bot", kind: "options", card });
+ const stillOpen = f.store.appendMessage(run.threadId!, { role: "bot", kind: "options", card: { ...card, requestId: "ask-2" } });
+ const [copy, openCopy] = f.lines();
+ expect(openCopy?.automation?.copyOf).toBe(stillOpen.id);
+ f.store.patchTask(f.bot.id, run.threadId!, { routineRunId: undefined });
+ f.offline();
+ const settled = { ...card, expired: true };
+ f.store.patchMessage(run.threadId!, original.id, { card: settled });
+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "options", card: { ...settled, requestId: "never-copied" } });
+ const mismatched = f.store.appendMessage(f.main, {
+ role: "bot", kind: "options", card: { ...card, requestId: "different-request" }, automation: copy!.automation,
+ });
+ expect(f.lines()[0]?.card).toEqual(card);
+ f.online();
+
+ const restarted = f.create();
+ restarted.flush();
+ expect(f.lines()).toEqual([expect.objectContaining({ id: copy!.id, card: settled }), openCopy, mismatched]);
+ expect(f.lines()[0]!.automation).not.toHaveProperty("needsYou");
+ const patched = f.lines()[0];
+ f.create().flush();
+ expect(f.lines()[0]).toBe(patched);
+ expect(f.notified).toEqual([]);
+ });
+
it("carries what the run attached with its last reply", () => {
const f = fixture();
const run = f.finished(null);
diff --git a/server/automation-reports.ts b/server/automation-reports.ts
index 890c8fceba..b6b57a6156 100644
--- a/server/automation-reports.ts
+++ b/server/automation-reports.ts
@@ -89,6 +89,11 @@ export function newestAutomationLines(messages: readonly Pick<Message, "id" | "a
const saidLine = (message: Message) => message.role === "bot" && message.kind === "text";
const turnOfLine = (message: Message) => message.turnId ?? message.sentInTurn;
+const settledQuestion = (message: Message) => {
+ const card = message.card;
+ return message.kind === "options" && !message.automation && card?.requestType === "question"
+ && card.requestId && !liveCardKind(card) ? card : undefined;
+};
type Waiting =
/** `messageId`: the line the run's reply is read from; null when its last turn said and sent nothing, absent when it is
@@ -164,6 +169,38 @@ export function createAutomationReports(deps: AutomationReportDeps) {
const copyOf = (run: RoutineRun, threadId: string, originalId: string) =>
store.messagesFor(threadId).find((message) => message.automation?.runId === run.id && message.automation.copyOf === originalId);
+ const settleQuestionCopy = (threadId: string, executionThreadId: string, copy: Message, card: NonNullable<Message["card"]>) => {
+ const line = copy.automation;
+ if (line?.executionThreadId !== executionThreadId || copy.kind !== "options"
+ || copy.card?.requestType !== "question" || copy.card.requestId !== card.requestId) return;
+ if (!line.needsYou && JSON.stringify(copy.card) === JSON.stringify(card)) return;
+ const { needsYou: _settled, ...automation } = line;
+ store.patchMessage(threadId, copy.id, { card: { ...card }, automation });
+ };
+
+ const reconcileQuestionCopies = (runs: RoutineRun[]) => {
+ const runsById = new Map<string, RoutineRun>();
+ const resultsThreads = new Set<string>();
+ for (const run of runs) {
+ if (run.target !== "bot" || !run.threadId || !store.taskByThread(run.botId, run.threadId)) continue;
+ const target = destination(run);
+ if (!target) continue;
+ runsById.set(run.id, run);
+ resultsThreads.add(target.threadId);
+ }
+ for (const threadId of resultsThreads) {
+ for (const copy of store.messagesFor(threadId)) {
+ const line = copy.automation;
+ if (!line?.needsYou || !line.copyOf || copy.kind !== "options" || copy.card?.requestType !== "question") continue;
+ const run = runsById.get(line.runId);
+ if (!run?.threadId || run.resultsThreadId !== threadId || run.threadId !== line.executionThreadId) continue;
+ const original = store.messagesFor(run.threadId).find((message) => message.id === line.copyOf);
+ const card = original && settledQuestion(original);
+ if (card) settleQuestionCopy(threadId, run.threadId, copy, card);
+ }
+ }
+ };
+
/** An option card is copied whole and answered through its request; any other card the run waits on is named. */
const writeCopy = (line: Extract<Waiting, { kind: "card" }>, target: { bot: BotRecord; threadId: string }) => {
const original = store.messagesFor(line.threadId).find((message) => message.id === line.messageId);
@@ -224,7 +261,9 @@ export function createAutomationReports(deps: AutomationReportDeps) {
try {
if (!resumed) {
resumed = true;
- for (const run of manager.listRuns().reverse()) {
+ const runs = manager.listRuns().reverse();
+ reconcileQuestionCopies(runs);
+ for (const run of runs) {
if (run.report?.state !== "pending" || waiting.has(run.id)) continue;
waiting.set(run.id, run.status === "failed" ? { kind: "failure", run, detail: run.error ?? "" } : { kind: "reply", run });
}
@@ -247,8 +286,13 @@ export function createAutomationReports(deps: AutomationReportDeps) {
const option = message.kind === "options" && Boolean(message.card?.requestId);
if (message.automation || (!option && message.kind !== "signin" && message.kind !== "connector" && message.kind !== "secret")) return;
const run = deps.routines()?.runForThread(change.threadId);
- const target = run?.target === "bot" && store.taskByThread(run.botId, change.threadId)?.routineRunId === run.id ? destination(run) : null;
- if (!run || !target) return;
+ if (run?.target !== "bot") return;
+ const source = store.taskByThread(run.botId, change.threadId);
+ if (!source) return;
+ const settled = change.type === "message.patch" ? settledQuestion(message) : undefined;
+ if (!settled && source.routineRunId !== run.id) return;
+ const target = destination(run);
+ if (!target) return;
if (change.type === "message") {
if (option ? !message.card!.autoApplied : waitingCardText(message)) {
hold(`card:${message.id}`, { kind: "card", run, threadId: change.threadId, messageId: message.id });
@@ -257,6 +301,10 @@ export function createAutomationReports(deps: AutomationReportDeps) {
}
const copy = copyOf(run, target.threadId, message.id);
if (!copy) return;
+ if (settled) {
+ settleQuestionCopy(target.threadId, change.threadId, copy, settled);
+ return;
+ }
if (option && JSON.stringify(copy.card) !== JSON.stringify(message.card)) store.patchMessage(target.threadId, copy.id, { card: { ...message.card! } });
if (!option && copy.automation?.needsYou && !waitingCardText(message)) {
const { needsYou: _settled, ...line } = copy.automation;