Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

feat(ai): add renameThread helper with atomic save

Abdurrahman Fakhruld9055ee05165
diff --git a/lib/ai/threads.test.ts b/lib/ai/threads.test.ts
index 4e8231dc..2cb30486 100644
--- a/lib/ai/threads.test.ts
+++ b/lib/ai/threads.test.ts
@@ -5,7 +5,7 @@ import { promises as fs } from "node:fs";
// Point the store at a temp dir BEFORE importing (the dir is read at module load).
process.env.OS_THREADS_DIR = path.join(os.tmpdir(), `mso-threads-test-${process.pid}`);
-const { saveThread, getThread, listThreads, deleteThread } = await import("./threads");
+const { saveThread, getThread, listThreads, deleteThread, renameThread } = await import("./threads");
describe("thread YAML store", () => {
it("round-trips a thread through YAML (save → get → list → delete)", async () => {
@@ -35,4 +35,37 @@ describe("thread YAML store", () => {
const names = await fs.readdir(process.env.OS_THREADS_DIR!);
expect(names.every((n) => !n.includes("/") && !n.includes(".."))).toBe(true);
});
+
+ it("renames a thread without touching messages, history, or creation time", async () => {
+ const originalMessages = [{ role: "user", text: "keep me" }];
+ const originalHistory = [{ role: "user", text: "wire turn" }];
+ await saveThread({
+ id: "thread_rename_test",
+ title: "Before Rename",
+ createdAt: 12345,
+ updatedAt: 12345,
+ messages: originalMessages,
+ history: originalHistory,
+ });
+
+ const summary = await renameThread("thread_rename_test", "After Rename");
+ expect(summary).not.toBeNull();
+ expect(summary?.id).toBe("thread_rename_test");
+ expect(summary?.title).toBe("After Rename");
+ expect(summary?.createdAt).toBe(12345);
+ expect(summary?.updatedAt).toBeGreaterThanOrEqual(12345);
+
+ const reloaded = await getThread("thread_rename_test");
+ expect(reloaded?.title).toBe("After Rename");
+ expect(reloaded?.createdAt).toBe(12345);
+ expect(reloaded?.messages).toEqual(originalMessages);
+ expect(reloaded?.history).toEqual(originalHistory);
+
+ // Non-existent thread returns null
+ const missing = await renameThread("thread_not_found", "New Title");
+ expect(missing).toBeNull();
+
+ // Invalid id throws
+ await expect(renameThread("../escape", "Title")).rejects.toThrow("invalid thread id");
+ });
});
diff --git a/lib/ai/threads.ts b/lib/ai/threads.ts
index 132e8dfa..b7e2deee 100644
--- a/lib/ai/threads.ts
+++ b/lib/ai/threads.ts
@@ -73,6 +73,15 @@ export async function saveThread(t: ChatThread): Promise<void> {
await fs.rename(tmp, dest);
}
+export async function renameThread(id: string, title: string): Promise<ThreadSummary | null> {
+ const t = await getThread(id);
+ if (!t) return null;
+ t.title = title.trim().slice(0, 120) || "Untitled";
+ t.updatedAt = Date.now();
+ await saveThread(t);
+ return { id: t.id, title: t.title, createdAt: t.createdAt || 0, updatedAt: t.updatedAt };
+}
+
export async function deleteThread(id: string): Promise<void> {
const file = fileFor(id); // invalid ids are authorization/input errors, not "already gone"
try {