Skip to content

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

Report files a routine sent after its last words or without any

Xiaolei Shieda0844c1498
diff --git a/server/automation-reports.test.ts b/server/automation-reports.test.ts
index 0fe0fedc57..91a879dc59 100644
--- a/server/automation-reports.test.ts
+++ b/server/automation-reports.test.ts
@@ -451,13 +451,27 @@ describe("automation reports", () => {
const page = { kind: "image" as const, path: "page-1.png", mime: "image/png", fromTool: true as const };
const drawn = { kind: "image" as const, path: "chart-1.png", mime: "image/png" };
const note = { kind: "audio" as const, path: "note-1.ogg", mime: "audio/ogg", text: "Today's brief." };
+ const slides = { kind: "file" as const, path: "brief-1.pptx", mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation", name: "Brief.pptx" };
f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [earlier] });
f.store.appendMessage(thread, { role: "user", kind: "text", text: "Go on." });
f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [pdf] });
+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Add the audio too.", steered: true });
f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [mp3] });
- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Your brief.", attachments: [page, drawn, note], turnId: `turn-${run.id}`, turnTerminal: true });
+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Your brief.", attachments: [page, drawn, note], turnId: `turn-${run.id}` });
+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [slides] });
f.reports.runSettled(run, `turn-${run.id}`);
- expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf, mp3, drawn, note] })]);
+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf, mp3, drawn, note, slides] })]);
+ });
+
+ it("reports a run that only sent files, without a word", () => {
+ const f = fixture();
+ const run = f.finished(null);
+ const pdf = { kind: "file" as const, path: "brief-1.pdf", mime: "application/pdf", name: "Brief.pdf" };
+ f.store.appendMessage(run.threadId!, { role: "user", kind: "text", text: "Do the Daily brief check." });
+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "", attachments: [pdf] });
+ f.reports.runSettled(run, `turn-${run.id}`);
+ expect(f.lines()).toEqual([expect.objectContaining({ text: "", attachments: [pdf] })]);
+ expect(f.runs.get(run.id)!.report).toMatchObject({ state: "posted" });
});
it("keeps a run quiet that said nothing and sent nothing, though its tools returned images", () => {
diff --git a/server/automation-reports.ts b/server/automation-reports.ts
index 8d1e9f62a4..daa88f404f 100644
--- a/server/automation-reports.ts
+++ b/server/automation-reports.ts
@@ -79,6 +79,17 @@ export function newestAutomationLines(messages: readonly Pick<Message, "id" | "a
return new Set(newest.values());
}
+const saidLine = (message: Message) => message.role === "bot" && message.kind === "text";
+/** A person's line that starts a turn, not one folded into a turn already running. */
+const asks = (message: Message) => message.role === "user" && !message.steered && !message.aside;
+
+/** The lines of the turn that holds this line: after the person's line that started it, up to their next. */
+function turnOf(path: readonly Message[], line: Message): Message[] {
+ const at = path.findIndex((message) => message.id === line.id);
+ const next = path.findIndex((message, index) => index > at && asks(message));
+ return path.slice(path.findLastIndex((message, index) => index < at && asks(message)) + 1, next < 0 ? undefined : next);
+}
+
type Waiting =
| { kind: "reply"; run: RoutineRun; messageId?: string }
| { kind: "failure"; run: RoutineRun; detail: string }
@@ -118,18 +129,14 @@ export function createAutomationReports(deps: AutomationReportDeps) {
/** The reply a run ends with: its last turn's last text, or after a restart the last one a turn settled on. */
const replyOf = (run: RoutineRun, messageId?: string) => {
const path = run.threadId ? store.activePath(run.threadId) : [];
- const said = (message: Message) => message.role === "bot" && message.kind === "text";
- return messageId ? path.find((message) => message.id === messageId) : path.findLast((message) => said(message) && message.turnTerminal) ?? path.findLast(said);
+ return messageId ? path.find((message) => message.id === messageId) : path.findLast((message) => saidLine(message) && message.turnTerminal) ?? path.findLast(saidLine);
};
/** The files the reply's turn sent the person, in order: its attach_file lines' and the reply's own, without the
* images its tools returned. */
const sentWith = (run: RoutineRun, reply: Message | undefined): NonNullable<Message["attachments"]> => {
if (!reply) return [];
- const path = store.activePath(run.threadId!);
- const end = path.findIndex((message) => message.id === reply.id);
- const turn = path.slice(path.findLastIndex((message, at) => at < end && message.role === "user") + 1, end + 1);
- return turn.flatMap((message) => message.role === "bot" && message.kind === "text" ? message.attachments ?? [] : [])
+ return turnOf(store.activePath(run.threadId!), reply).flatMap((message) => message.role === "bot" && message.kind === "text" ? message.attachments ?? [] : [])
.filter((file) => file.kind !== "image" || !file.fromTool);
};
@@ -256,7 +263,9 @@ export function createAutomationReports(deps: AutomationReportDeps) {
runSettled(run: RoutineRun, turnId?: string): void {
if (run.target !== "bot" || run.status !== "completed" || !run.threadId) return;
if (run.deliver === "brief") return deps.brief?.(run, turnId);
- const said = turnId ? store.activePath(run.threadId).findLast((message) => message.role === "bot" && message.kind === "text" && message.turnId === turnId) : replyOf(run);
+ const path = store.activePath(run.threadId);
+ const said = turnId ? path.findLast((message) => saidLine(message) && message.turnId === turnId) ?? path.slice(path.findLastIndex(asks) + 1).findLast(saidLine)
+ : replyOf(run);
const reply = read(run, said);
const bot = store.bot(run.botId);
const target = destination(run);