Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(threads): name a thread by its words, not OpenMausBot's sender note
AdityaUmale257794a69ce0
diff --git a/server/store.test.ts b/server/store.test.tsindex 54378bf815..f6dc4ca489 100644--- a/server/store.test.ts+++ b/server/store.test.ts@@ -2087,6 +2087,19 @@ describe("Store change stream", () => {expect(store.retitleTask(recipient.id, row.threadId, `@Scout ${brief}`, "Late title")).toBeNull();});+ it("a first line that is only OpenMausBot's note leaves the thread untitled for the next line", () => {+ const store = new Store(selection);+ const bot = store.createBot({ name: "Scout" });+ const retry = "[From OpenMausBot · retry · information only — requested by OpenMausBot, automatically. Continue the request above.]";+ const row = store.createTask(bot.id)!;+ expect(store.titleTaskFromFirstMessage(bot.id, retry, row.threadId)).toBeNull();+ expect(store.titleTaskFromFirstMessage(bot.id, "Check the invoices", row.threadId)?.title).toBe("Check the invoices");+ const group = store.createGroup("Ops", [bot.id])!;+ const task = store.activeGroupTask(group.id)!;+ expect(store.titleGroupTaskFromFirstMessage(group.id, retry, task.threadId)).toBeNull();+ expect(store.titleGroupTaskFromFirstMessage(group.id, "Audit the payroll", task.threadId)?.title).toBe("Audit the payroll");+ });+it("channel first-message titling reports the row it named, and a late retitle cannot undo a rename", () => {const store = new Store(selection);const bot = store.createBot({ name: "Scout" });diff --git a/server/store.ts b/server/store.tsindex 4e839d68c9..8bd2ccc0b8 100644--- a/server/store.ts+++ b/server/store.ts@@ -1003,11 +1003,11 @@ export class Store {}titleGroupTaskFromFirstMessage(groupId: string, text: string, threadId?: string): GroupTaskRecord | null {- const group = this.group(groupId);+ const group = this.group(groupId), title = titleFromMessage(text);const task = threadId ? this.groupTaskByThread(groupId, threadId) : this.activeGroupTask(groupId);- if (!group || !task || task.titleFromFirstMessage || task.title !== UNTITLED_TASK) return null;+ if (!group || !task || task.titleFromFirstMessage || task.title !== UNTITLED_TASK || title === UNTITLED_TASK) return null;this.commitGroupChange(group, "the thread's title", () => {- task.title = titleFromMessage(text);+ task.title = title;task.titleFromFirstMessage = true;});this.emit({ type: "group", groupId });@@ -2201,11 +2201,11 @@ export class Store {}titleTaskFromFirstMessage(botId: string, text: string, threadId?: string): TaskRecord | null {- const bot = this.own(botId);+ const bot = this.own(botId), title = titleFromMessage(text);const task = threadId ? this.taskByThread(botId, threadId) : this.activeTask(botId);- if (!bot || !task || task.titleFromFirstMessage || (task.title !== UNTITLED_TASK && task.title !== UNTITLED_THREAD)) return null;+ if (!bot || !task || task.titleFromFirstMessage || (task.title !== UNTITLED_TASK && task.title !== UNTITLED_THREAD) || title === UNTITLED_TASK) return null;this.commitBotChange(bot, "the thread's title", () => {- task.title = titleFromMessage(text);+ task.title = title;task.titleFromFirstMessage = true;}, undefined, true);this.emit({ type: "bot", botId });diff --git a/server/thread-title.test.ts b/server/thread-title.test.tsindex 59d1842c70..7753693fb8 100644--- a/server/thread-title.test.ts+++ b/server/thread-title.test.ts@@ -3,6 +3,8 @@ import { describe, expect, it, vi } from "vitest";import { createJobSlots } from "./job-slots.ts";import type { Message } from "./store.ts";import { callTitleExcerpt, threadTitlePrompt, titleConversationExcerpt, titleFromMessage, TITLE_INPUT_MAX_CHARS, UNTITLED_TASK } from "./thread-title.ts";+import { AUTO_RETRY_LEAD } from "./incidents.ts";+import { AUTHORITY, botWho, fromNote, fromNoteWith, routineWho } from "./provenance-label.ts";import { generateThreadTitle, THREAD_TITLE_TIMEOUT_MS } from "./thread-title-generate.ts";describe("a generated title and the background slots", () => {@@ -166,3 +168,19 @@ describe("titleFromMessage", () => {expect(titleFromMessage('<pasted-text index="1">\nnot closed')).toBe('<pasted-text index="1">');});});++describe("titleFromMessage and OpenMausBot's sender note", () => {+ it("names a delivered line by its words, not by the note that opens it", () => {+ expect(titleFromMessage(`${fromNote(botWho("Sales"), "assignment", AUTHORITY.behalf)}\n\nFind last month's unpaid invoices`)).toBe("Find last month's unpaid invoices");+ expect(titleFromMessage(`${fromNoteWith(routineWho("Daily brief"), "run", AUTHORITY.behalf, "no one is present")}\nSummarise the inbox`)).toBe("Summarise the inbox");+ });++ it("gives no title for a line that is only a note, like the automatic retry", () => {+ expect(titleFromMessage(`${AUTO_RETRY_LEAD}, once, before telling Chief, your Chief of Staff, that this thread's last run failed. Continue the request above from where it stopped and finish it.]`)).toBe(UNTITLED_TASK);+ });++ it("keeps a typed line that only looks like a note", () => {+ expect(titleFromMessage("[From the docs] how do routines repeat?")).toBe("[From the docs] how do routines repeat?");+ expect(titleFromMessage("Plan the offsite\n[From @Sales (bot) · message · information only]")).toBe("Plan the offsite");+ });+});diff --git a/server/thread-title.ts b/server/thread-title.tsindex b7eb9c963d..1f1ef60127 100644--- a/server/thread-title.ts+++ b/server/thread-title.ts@@ -1,3 +1,4 @@+import { AUTHORITY } from "./provenance-label.ts";import { redactSecretsInText } from "./redact.ts";import type { Message } from "./store.ts";import { toolResultPrefix } from "./tool-results.ts";@@ -5,9 +6,12 @@ import { toolResultPrefix } from "./tool-results.ts";export const UNTITLED_TASK = "New task";const PASTE_WRAPPER_LINE = /^\s*<\/?pasted-text(?:\s+index="\d+")?\s*>\s*$/i;+/** OpenMausBot's own note that opens a delivered line ([From <who> · <what> · <authority>], provenance-label.ts). */+const LEADING_FROM_NOTE = new RegExp(`^\\[From [^\\]\\n]* · [^\\]\\n]* · (?:${Object.values(AUTHORITY).join("|")})(?: — [^\\]\\n]*)?\\]`, "u");+/** A line is named by what it says, not by the note saying who sent it; a line that is only a note gives UNTITLED_TASK. */export function titleFromMessage(text: string): string {- const lines = text.trim().split("\n");+ const lines = text.trim().replace(LEADING_FROM_NOTE, "").trim().split("\n");// A message that opens with a paste is named by the pasted text, not the wrapper the composer puts around it.const opensWithPaste = PASTE_WRAPPER_LINE.test(lines[0]!) && /<\/pasted-text>/i.test(text);const line = opensWithPaste ? firstPastedLine(lines) : lines[0]!.trim();