@vellumai/assistant 0.11.2-dev.202608050900.b357993 → 0.11.2-dev.202608051337.65512a7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/openapi.yaml +8 -2
- package/package.json +1 -1
- package/src/__tests__/conversation-key-store-bootstrap-cleanup.test.ts +119 -0
- package/src/__tests__/system-prompt.test.ts +82 -7
- package/src/persistence/conversation-key-store.ts +23 -9
- package/src/persistence/conversation-types.ts +9 -0
- package/src/prompts/system-prompt.ts +72 -17
- package/src/runtime/routes/__tests__/conversation-management-routes.test.ts +46 -0
- package/src/runtime/routes/conversation-management-routes.ts +31 -7
package/openapi.yaml
CHANGED
|
@@ -6798,9 +6798,15 @@ paths:
|
|
|
6798
6798
|
`id`.
|
|
6799
6799
|
type: string
|
|
6800
6800
|
conversationType:
|
|
6801
|
-
description:
|
|
6801
|
+
description:
|
|
6802
|
+
Conversation type for the new row. "background" keeps it out of the foreground list and the sidebar's
|
|
6803
|
+
Recents grouping, used by internal side-channel flows (onboarding research, persona/identity
|
|
6804
|
+
rewrites) that mint a throwaway thread the user should never see. "scheduled" is not accepted here;
|
|
6805
|
+
scheduled rows are owned by the schedule pipeline.
|
|
6802
6806
|
type: string
|
|
6803
|
-
|
|
6807
|
+
enum:
|
|
6808
|
+
- standard
|
|
6809
|
+
- background
|
|
6804
6810
|
title:
|
|
6805
6811
|
description:
|
|
6806
6812
|
Explicit title for the conversation. When provided on creation, it is persisted as a user-set title (never
|
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `getOrCreateConversation` doubles as the first-conversation bootstrap
|
|
3
|
+
* bookkeeper: the first standard conversation is the onboarding one (keep
|
|
4
|
+
* BOOTSTRAP.md), and the second means onboarding is over (delete it).
|
|
5
|
+
*
|
|
6
|
+
* Onboarding also mints internal side threads (research, personality rewrite,
|
|
7
|
+
* identity rewrite) as `conversationType: "background"` rows that the user
|
|
8
|
+
* never opens. Those must be completely inert with respect to this bookkeeping:
|
|
9
|
+
* if a hidden side thread consumed the first-conversation slot, the user's
|
|
10
|
+
* FIRST visible chat would look like the second conversation and lose
|
|
11
|
+
* BOOTSTRAP.md before its opening turn.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
17
|
+
|
|
18
|
+
import { assertNotLiveDb } from "./assert-not-live-db.js";
|
|
19
|
+
|
|
20
|
+
const testDir = process.env.VELLUM_WORKSPACE_DIR!;
|
|
21
|
+
const conversationsDir = join(testDir, "conversations");
|
|
22
|
+
mkdirSync(conversationsDir, { recursive: true });
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
_resetFirstConversationSeenForTesting,
|
|
26
|
+
getOrCreateConversation,
|
|
27
|
+
} from "../persistence/conversation-key-store.js";
|
|
28
|
+
import { getDb } from "../persistence/db-connection.js";
|
|
29
|
+
import { initializeDb } from "../persistence/db-init.js";
|
|
30
|
+
import {
|
|
31
|
+
conversationKeys,
|
|
32
|
+
conversations,
|
|
33
|
+
} from "../persistence/schema/index.js";
|
|
34
|
+
|
|
35
|
+
await initializeDb();
|
|
36
|
+
|
|
37
|
+
const bootstrapPath = join(testDir, "BOOTSTRAP.md");
|
|
38
|
+
const bootstrapReferencePath = join(testDir, "BOOTSTRAP-REFERENCE.md");
|
|
39
|
+
|
|
40
|
+
function seedBootstrapFiles(): void {
|
|
41
|
+
writeFileSync(bootstrapPath, "# First run\n", "utf-8");
|
|
42
|
+
writeFileSync(bootstrapReferencePath, "# Reference\n", "utf-8");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
const db = getDb();
|
|
47
|
+
db.delete(conversationKeys).run();
|
|
48
|
+
db.delete(conversations).run();
|
|
49
|
+
|
|
50
|
+
assertNotLiveDb(conversationsDir);
|
|
51
|
+
rmSync(conversationsDir, { recursive: true, force: true });
|
|
52
|
+
mkdirSync(conversationsDir, { recursive: true });
|
|
53
|
+
|
|
54
|
+
// `firstConversationSeen` is module-level (process-global) state, so each
|
|
55
|
+
// test has to replay the fresh-install sequence from a known-clean flag.
|
|
56
|
+
_resetFirstConversationSeenForTesting();
|
|
57
|
+
seedBootstrapFiles();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("getOrCreateConversation bootstrap bookkeeping", () => {
|
|
61
|
+
test("background side threads do not consume the first-conversation slot", () => {
|
|
62
|
+
// The exact onboarding sequence: three hidden side threads, then the
|
|
63
|
+
// user's first real chat.
|
|
64
|
+
for (const key of ["onboarding-research", "personality", "identity"]) {
|
|
65
|
+
const created = getOrCreateConversation(key, {
|
|
66
|
+
conversationType: "background",
|
|
67
|
+
});
|
|
68
|
+
expect(created.created).toBe(true);
|
|
69
|
+
expect(created.conversationType).toBe("background");
|
|
70
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const firstVisible = getOrCreateConversation("user-first-chat");
|
|
74
|
+
expect(firstVisible.created).toBe(true);
|
|
75
|
+
expect(firstVisible.conversationType).toBe("standard");
|
|
76
|
+
|
|
77
|
+
// The user's first visible chat is still the FIRST conversation as far as
|
|
78
|
+
// onboarding is concerned, so its first-run context survives.
|
|
79
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
80
|
+
expect(existsSync(bootstrapReferencePath)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("a background create after the first standard one is still inert", () => {
|
|
84
|
+
getOrCreateConversation("user-first-chat");
|
|
85
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
86
|
+
|
|
87
|
+
getOrCreateConversation("side-thread", { conversationType: "background" });
|
|
88
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
89
|
+
expect(existsSync(bootstrapReferencePath)).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("the second standard conversation still deletes the bootstrap files", () => {
|
|
93
|
+
getOrCreateConversation("first-chat");
|
|
94
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
95
|
+
|
|
96
|
+
getOrCreateConversation("second-chat");
|
|
97
|
+
expect(existsSync(bootstrapPath)).toBe(false);
|
|
98
|
+
expect(existsSync(bootstrapReferencePath)).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("background creates interleaved with standard ones do not shift the slot", () => {
|
|
102
|
+
getOrCreateConversation("bg-a", { conversationType: "background" });
|
|
103
|
+
getOrCreateConversation("first-chat");
|
|
104
|
+
getOrCreateConversation("bg-b", { conversationType: "background" });
|
|
105
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
106
|
+
|
|
107
|
+
// Only the second STANDARD conversation ends onboarding.
|
|
108
|
+
getOrCreateConversation("second-chat");
|
|
109
|
+
expect(existsSync(bootstrapPath)).toBe(false);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("resolving an existing key never re-runs the bookkeeping", () => {
|
|
113
|
+
const first = getOrCreateConversation("first-chat");
|
|
114
|
+
const again = getOrCreateConversation("first-chat");
|
|
115
|
+
expect(again.created).toBe(false);
|
|
116
|
+
expect(again.conversationId).toBe(first.conversationId);
|
|
117
|
+
expect(existsSync(bootstrapPath)).toBe(true);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
@@ -1518,10 +1518,7 @@ describe("ensurePromptFiles", () => {
|
|
|
1518
1518
|
writeFileSync(join(TEST_DIR, "SOUL.md"), "My soul");
|
|
1519
1519
|
writeFileSync(join(TEST_DIR, "BOOTSTRAP.md"), "# Stale bootstrap");
|
|
1520
1520
|
|
|
1521
|
-
|
|
1522
|
-
const convDir = join(TEST_DIR, "conversations");
|
|
1523
|
-
mkdirSync(convDir, { recursive: true });
|
|
1524
|
-
writeFileSync(join(convDir, "conv-001.json"), "{}");
|
|
1521
|
+
writeConversationDir("conv-001", "standard");
|
|
1525
1522
|
|
|
1526
1523
|
ensurePromptFiles();
|
|
1527
1524
|
|
|
@@ -1533,15 +1530,93 @@ describe("ensurePromptFiles", () => {
|
|
|
1533
1530
|
// will be re-seeded from templates) but still carries prior
|
|
1534
1531
|
// conversations. Existing conversation history signals a non-fresh
|
|
1535
1532
|
// install, so onboarding must not re-trigger.
|
|
1536
|
-
|
|
1537
|
-
mkdirSync(convDir, { recursive: true });
|
|
1538
|
-
writeFileSync(join(convDir, "conv-001.json"), "{}");
|
|
1533
|
+
writeConversationDir("conv-001", "standard");
|
|
1539
1534
|
|
|
1540
1535
|
ensurePromptFiles();
|
|
1541
1536
|
|
|
1542
1537
|
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(false);
|
|
1543
1538
|
});
|
|
1544
1539
|
|
|
1540
|
+
/** Project a conversation onto disk the way `initConversationDir` does. */
|
|
1541
|
+
function writeConversationDir(id: string, type: string): void {
|
|
1542
|
+
const dir = join(
|
|
1543
|
+
TEST_DIR,
|
|
1544
|
+
"conversations",
|
|
1545
|
+
`2026-01-01T00-00-00.000Z_${id}`,
|
|
1546
|
+
);
|
|
1547
|
+
mkdirSync(dir, { recursive: true });
|
|
1548
|
+
writeFileSync(join(dir, "meta.json"), JSON.stringify({ id, type }));
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
test("keeps BOOTSTRAP.md when the only conversations are background side threads", () => {
|
|
1552
|
+
// Onboarding mints throwaway background threads (research, personality and
|
|
1553
|
+
// identity rewrites) before the user's first visible chat, and each gets a
|
|
1554
|
+
// disk-view directory. A daemon restart in that window must not read them
|
|
1555
|
+
// as evidence that onboarding ended.
|
|
1556
|
+
writeFileSync(join(TEST_DIR, "IDENTITY.md"), "My identity");
|
|
1557
|
+
writeFileSync(join(TEST_DIR, "SOUL.md"), "My soul");
|
|
1558
|
+
writeFileSync(join(TEST_DIR, "BOOTSTRAP.md"), "# Bootstrap");
|
|
1559
|
+
writeConversationDir("bg-001", "background");
|
|
1560
|
+
writeConversationDir("bg-002", "background");
|
|
1561
|
+
|
|
1562
|
+
ensurePromptFiles();
|
|
1563
|
+
|
|
1564
|
+
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(true);
|
|
1565
|
+
});
|
|
1566
|
+
|
|
1567
|
+
test("keeps BOOTSTRAP.md when a stray file sits beside background side threads", () => {
|
|
1568
|
+
// The disk view is browsable, so a file explorer can drop `.DS_Store` into
|
|
1569
|
+
// the conversations directory. It is not a conversation and must not be
|
|
1570
|
+
// classified as one.
|
|
1571
|
+
writeFileSync(join(TEST_DIR, "IDENTITY.md"), "My identity");
|
|
1572
|
+
writeFileSync(join(TEST_DIR, "SOUL.md"), "My soul");
|
|
1573
|
+
writeFileSync(join(TEST_DIR, "BOOTSTRAP.md"), "# Bootstrap");
|
|
1574
|
+
writeConversationDir("bg-001", "background");
|
|
1575
|
+
writeFileSync(join(TEST_DIR, "conversations", ".DS_Store"), "junk");
|
|
1576
|
+
|
|
1577
|
+
ensurePromptFiles();
|
|
1578
|
+
|
|
1579
|
+
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(true);
|
|
1580
|
+
});
|
|
1581
|
+
|
|
1582
|
+
test("auto-deletes stale BOOTSTRAP.md when a conversation directory has no meta.json", () => {
|
|
1583
|
+
// A directory the daemon cannot classify counts as standard: it is far
|
|
1584
|
+
// likelier to be legacy history than a side thread, and over-counting only
|
|
1585
|
+
// suppresses an onboarding re-run.
|
|
1586
|
+
writeFileSync(join(TEST_DIR, "IDENTITY.md"), "My identity");
|
|
1587
|
+
writeFileSync(join(TEST_DIR, "SOUL.md"), "My soul");
|
|
1588
|
+
writeFileSync(join(TEST_DIR, "BOOTSTRAP.md"), "# Stale bootstrap");
|
|
1589
|
+
mkdirSync(join(TEST_DIR, "conversations", "2026-01-01T00-00-00.000Z_old"), {
|
|
1590
|
+
recursive: true,
|
|
1591
|
+
});
|
|
1592
|
+
|
|
1593
|
+
ensurePromptFiles();
|
|
1594
|
+
|
|
1595
|
+
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(false);
|
|
1596
|
+
});
|
|
1597
|
+
|
|
1598
|
+
test("auto-deletes stale BOOTSTRAP.md when a standard conversation sits alongside background ones", () => {
|
|
1599
|
+
writeFileSync(join(TEST_DIR, "IDENTITY.md"), "My identity");
|
|
1600
|
+
writeFileSync(join(TEST_DIR, "SOUL.md"), "My soul");
|
|
1601
|
+
writeFileSync(join(TEST_DIR, "BOOTSTRAP.md"), "# Stale bootstrap");
|
|
1602
|
+
writeConversationDir("bg-001", "background");
|
|
1603
|
+
writeConversationDir("std-001", "standard");
|
|
1604
|
+
|
|
1605
|
+
ensurePromptFiles();
|
|
1606
|
+
|
|
1607
|
+
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(false);
|
|
1608
|
+
});
|
|
1609
|
+
|
|
1610
|
+
test("seeds BOOTSTRAP.md on a fresh workspace whose only conversations are background", () => {
|
|
1611
|
+
// Background side threads must not make a fresh workspace look like an
|
|
1612
|
+
// upgraded one, or onboarding never gets its bootstrap file at all.
|
|
1613
|
+
writeConversationDir("bg-001", "background");
|
|
1614
|
+
|
|
1615
|
+
ensurePromptFiles();
|
|
1616
|
+
|
|
1617
|
+
expect(existsSync(join(TEST_DIR, "BOOTSTRAP.md"))).toBe(true);
|
|
1618
|
+
});
|
|
1619
|
+
|
|
1545
1620
|
test("keeps BOOTSTRAP.md when no conversations exist yet", () => {
|
|
1546
1621
|
// Non-first-run but no conversations — user hasn't chatted yet
|
|
1547
1622
|
writeFileSync(join(TEST_DIR, "IDENTITY.md"), "My identity");
|
|
@@ -15,14 +15,26 @@ import { getCurrentSeq } from "../runtime/assistant-stream-state.js";
|
|
|
15
15
|
import { getLogger } from "../util/logger.js";
|
|
16
16
|
import { initConversationDir } from "./conversation-disk-view.js";
|
|
17
17
|
import { GENERATING_TITLE } from "./conversation-title-service.js";
|
|
18
|
+
import type { NonScheduledConversationType } from "./conversation-types.js";
|
|
18
19
|
import { getDb } from "./db-connection.js";
|
|
19
20
|
import { conversationKeys, conversations } from "./schema/index.js";
|
|
20
21
|
|
|
21
22
|
const log = getLogger("conversation-key-store");
|
|
22
23
|
|
|
23
|
-
/**
|
|
24
|
+
/**
|
|
25
|
+
* Set after the first *standard* conversation is created so BOOTSTRAP.md is
|
|
26
|
+
* deleted on the second. Background creates never touch it.
|
|
27
|
+
*/
|
|
24
28
|
let firstConversationSeen = false;
|
|
25
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Test-only reset of the process-global first-conversation flag, so a test file
|
|
32
|
+
* can replay the "fresh install" sequence more than once.
|
|
33
|
+
*/
|
|
34
|
+
export function _resetFirstConversationSeenForTesting(): void {
|
|
35
|
+
firstConversationSeen = false;
|
|
36
|
+
}
|
|
37
|
+
|
|
26
38
|
export interface ConversationKeyMapping {
|
|
27
39
|
id: string;
|
|
28
40
|
conversationKey: string;
|
|
@@ -142,7 +154,7 @@ export function resolveConversationId(idOrKey: string): string | null {
|
|
|
142
154
|
export function getOrCreateConversation(
|
|
143
155
|
conversationKey: string,
|
|
144
156
|
opts?: {
|
|
145
|
-
conversationType?:
|
|
157
|
+
conversationType?: NonScheduledConversationType;
|
|
146
158
|
/**
|
|
147
159
|
* Caller-supplied title for the conversation, used only when this call
|
|
148
160
|
* actually creates the row. Treated as a user-set title (`isAutoTitle = 0`)
|
|
@@ -209,14 +221,16 @@ export function getOrCreateConversation(
|
|
|
209
221
|
};
|
|
210
222
|
}
|
|
211
223
|
|
|
212
|
-
//
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
//
|
|
216
|
-
if (
|
|
217
|
-
|
|
224
|
+
// The first standard conversation is the onboarding one, so BOOTSTRAP.md
|
|
225
|
+
// survives its whole duration and any later standard create means
|
|
226
|
+
// onboarding is over. Background rows stay inert: a hidden side thread
|
|
227
|
+
// minted before the user's first visible chat must not consume that slot.
|
|
228
|
+
if (conversationType === "standard") {
|
|
229
|
+
if (firstConversationSeen) {
|
|
230
|
+
cleanupBootstrapFiles("new conversation created after onboarding");
|
|
231
|
+
}
|
|
232
|
+
firstConversationSeen = true;
|
|
218
233
|
}
|
|
219
|
-
firstConversationSeen = true;
|
|
220
234
|
|
|
221
235
|
const now = Date.now();
|
|
222
236
|
const conversationId = uuid();
|
|
@@ -24,6 +24,15 @@ export type ConversationCreateType = "standard" | "background" | "scheduled";
|
|
|
24
24
|
/** Read-side alias of {@link ConversationCreateType}. */
|
|
25
25
|
export type ConversationType = ConversationCreateType;
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Conversation types minted outside the schedule pipeline. Scheduled rows are
|
|
29
|
+
* owned by it and created through `createConversation`.
|
|
30
|
+
*/
|
|
31
|
+
export type NonScheduledConversationType = Exclude<
|
|
32
|
+
ConversationCreateType,
|
|
33
|
+
"scheduled"
|
|
34
|
+
>;
|
|
35
|
+
|
|
27
36
|
/**
|
|
28
37
|
* Conversation types created by background machinery (heartbeat runs,
|
|
29
38
|
* scheduled runs, retrospective forks) rather than by a person. Exported as a
|
|
@@ -48,13 +48,61 @@ function hasPopulatedUsersDir(): boolean {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Conversation types that are internal side channels rather than threads the
|
|
53
|
+
* user opens: onboarding research, personality/identity rewrites, heartbeat
|
|
54
|
+
* and scheduled runs, plus legacy private rows.
|
|
55
|
+
*/
|
|
56
|
+
const HIDDEN_CONVERSATION_TYPES = new Set([
|
|
57
|
+
"background",
|
|
58
|
+
"scheduled",
|
|
59
|
+
"private",
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether the workspace carries at least one standard (user-visible)
|
|
64
|
+
* conversation.
|
|
65
|
+
*
|
|
66
|
+
* Classifies each disk-view directory by the `type` in its `meta.json` rather
|
|
67
|
+
* than counting raw directory entries, because hidden side threads get a
|
|
68
|
+
* disk-view directory too and must not be mistaken for the user's own history.
|
|
69
|
+
* The type read here is the same value `countConversations("standard")` filters
|
|
70
|
+
* on, but the answer comes off disk rather than out of the database:
|
|
71
|
+
* `ensurePromptFiles()` runs before `initializeDb()` settles in
|
|
72
|
+
* `daemon/lifecycle.ts`, so a query here would hit a partially-migrated schema.
|
|
73
|
+
*
|
|
74
|
+
* Only directories are classified. The layout is browsable, so stray files such
|
|
75
|
+
* as `.DS_Store` sit beside the conversations, and a `meta.json` read under one
|
|
76
|
+
* fails indistinguishably from an unreadable conversation. Directory names are
|
|
77
|
+
* not filtered further, because both the canonical `<ISO>_<id>` layout and the
|
|
78
|
+
* legacy `<id>_<ISO>` one name real conversations.
|
|
79
|
+
*
|
|
80
|
+
* A directory whose `meta.json` is missing or unreadable counts as standard: a
|
|
81
|
+
* directory the daemon cannot classify is far more likely to be legacy history
|
|
82
|
+
* than a side thread, and over-counting only keeps onboarding from re-running.
|
|
83
|
+
*/
|
|
84
|
+
function hasStandardConversations(): boolean {
|
|
52
85
|
try {
|
|
53
86
|
const convDir = getConversationsDir();
|
|
54
87
|
if (!existsSync(convDir)) {
|
|
55
88
|
return false;
|
|
56
89
|
}
|
|
57
|
-
return readdirSync(convDir).
|
|
90
|
+
return readdirSync(convDir, { withFileTypes: true }).some((entry) => {
|
|
91
|
+
if (!entry.isDirectory()) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
try {
|
|
95
|
+
const meta = JSON.parse(
|
|
96
|
+
readFileSync(join(convDir, entry.name, "meta.json"), "utf-8"),
|
|
97
|
+
) as { type?: unknown };
|
|
98
|
+
return (
|
|
99
|
+
typeof meta.type !== "string" ||
|
|
100
|
+
!HIDDEN_CONVERSATION_TYPES.has(meta.type)
|
|
101
|
+
);
|
|
102
|
+
} catch {
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
58
106
|
} catch {
|
|
59
107
|
return false;
|
|
60
108
|
}
|
|
@@ -78,14 +126,14 @@ export function ensurePromptFiles(): void {
|
|
|
78
126
|
|
|
79
127
|
// Track whether this is a fresh workspace. A workspace counts as fresh
|
|
80
128
|
// only when none of these signals are present: core prompt files, a
|
|
81
|
-
// populated `users/` directory, or existing conversations.
|
|
82
|
-
// workspaces that dropped USER.md but still carry personas or
|
|
83
|
-
// would otherwise be mistaken for fresh installs and re-trigger
|
|
129
|
+
// populated `users/` directory, or existing standard conversations.
|
|
130
|
+
// Upgraded workspaces that dropped USER.md but still carry personas or
|
|
131
|
+
// history would otherwise be mistaken for fresh installs and re-trigger
|
|
84
132
|
// onboarding.
|
|
85
133
|
const isFirstRun =
|
|
86
134
|
PROMPT_FILES.every((file) => !existsSync(getWorkspacePromptPath(file))) &&
|
|
87
135
|
!hasPopulatedUsersDir() &&
|
|
88
|
-
!
|
|
136
|
+
!hasStandardConversations();
|
|
89
137
|
|
|
90
138
|
for (const file of PROMPT_FILES) {
|
|
91
139
|
const dest = getWorkspacePromptPath(file);
|
|
@@ -152,18 +200,25 @@ export function ensurePromptFiles(): void {
|
|
|
152
200
|
// Auto-delete stale BOOTSTRAP.md at startup. The model is instructed to
|
|
153
201
|
// delete it at the end of the first conversation, but if the user closes
|
|
154
202
|
// the app or starts a new thread before the model gets another turn, it
|
|
155
|
-
// never gets the chance. If BOOTSTRAP.md still exists but prior
|
|
156
|
-
// conversations are present, the onboarding window has passed
|
|
203
|
+
// never gets the chance. If BOOTSTRAP.md still exists but prior standard
|
|
204
|
+
// conversations are present, the onboarding window has passed, so clean up.
|
|
205
|
+
//
|
|
206
|
+
// Only standard conversations count. Onboarding mints hidden side threads
|
|
207
|
+
// before the user's first visible chat, so counting those would delete
|
|
208
|
+
// BOOTSTRAP.md out from under that chat on the next daemon restart.
|
|
209
|
+
//
|
|
210
|
+
// This gate is one conversation stricter than the in-process gate in
|
|
211
|
+
// `persistence/conversation-key-store.ts`, which keeps BOOTSTRAP.md through
|
|
212
|
+
// the whole first standard conversation and deletes it when a second one is
|
|
213
|
+
// created. A restart while the user is still inside that first conversation
|
|
214
|
+
// deletes BOOTSTRAP.md from under it.
|
|
157
215
|
const bootstrapCleanup = getWorkspacePromptPath("BOOTSTRAP.md");
|
|
158
|
-
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
} catch (err) {
|
|
165
|
-
log.warn({ err }, "Failed to auto-delete stale BOOTSTRAP.md");
|
|
166
|
-
}
|
|
216
|
+
if (
|
|
217
|
+
!isFirstRun &&
|
|
218
|
+
existsSync(bootstrapCleanup) &&
|
|
219
|
+
hasStandardConversations()
|
|
220
|
+
) {
|
|
221
|
+
cleanupBootstrapFiles("prior conversations exist");
|
|
167
222
|
}
|
|
168
223
|
|
|
169
224
|
// Seed HEARTBEAT.md — always created if missing so the heartbeat service
|
|
@@ -111,6 +111,7 @@ describe("POST /v1/conversations (createConversation)", () => {
|
|
|
111
111
|
.select({
|
|
112
112
|
title: conversations.title,
|
|
113
113
|
isAutoTitle: conversations.isAutoTitle,
|
|
114
|
+
conversationType: conversations.conversationType,
|
|
114
115
|
})
|
|
115
116
|
.from(conversations)
|
|
116
117
|
.where(eq(conversations.id, id))
|
|
@@ -160,6 +161,51 @@ describe("POST /v1/conversations (createConversation)", () => {
|
|
|
160
161
|
).toThrow(/title must be a string/);
|
|
161
162
|
expect(getDb().select().from(conversations).all()).toHaveLength(0);
|
|
162
163
|
});
|
|
164
|
+
|
|
165
|
+
test("conversationType: background → persists a background row and echoes it back", async () => {
|
|
166
|
+
const result = (await createHandler({
|
|
167
|
+
body: {
|
|
168
|
+
conversationType: "background",
|
|
169
|
+
title: "Updating personality",
|
|
170
|
+
},
|
|
171
|
+
})) as { id: string; conversationType: string; created: boolean };
|
|
172
|
+
|
|
173
|
+
expect(result.created).toBe(true);
|
|
174
|
+
expect(result.conversationType).toBe("background");
|
|
175
|
+
const row = readConversation(result.id);
|
|
176
|
+
expect(row?.conversationType).toBe("background");
|
|
177
|
+
// An explicit title stays user-set on a background row too.
|
|
178
|
+
expect(row?.title).toBe("Updating personality");
|
|
179
|
+
expect(row?.isAutoTitle).toBe(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("omitted conversationType still creates a standard row", async () => {
|
|
183
|
+
const result = (await createHandler({
|
|
184
|
+
body: { title: "Visible thread" },
|
|
185
|
+
})) as { id: string; conversationType: string };
|
|
186
|
+
|
|
187
|
+
expect(result.conversationType).toBe("standard");
|
|
188
|
+
expect(readConversation(result.id)?.conversationType).toBe("standard");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("explicit conversationType: null behaves exactly like an omitted one", async () => {
|
|
192
|
+
const result = (await createHandler({
|
|
193
|
+
body: { conversationType: null, title: "Visible thread" },
|
|
194
|
+
})) as { id: string; conversationType: string };
|
|
195
|
+
|
|
196
|
+
expect(result.conversationType).toBe("standard");
|
|
197
|
+
expect(readConversation(result.id)?.conversationType).toBe("standard");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test.each(["scheduled", "nonsense"])(
|
|
201
|
+
"conversationType: %p → BadRequestError, no row created",
|
|
202
|
+
(conversationType) => {
|
|
203
|
+
expect(() => createHandler({ body: { conversationType } })).toThrow(
|
|
204
|
+
/conversationType must be one of standard, background/,
|
|
205
|
+
);
|
|
206
|
+
expect(getDb().select().from(conversations).all()).toHaveLength(0);
|
|
207
|
+
},
|
|
208
|
+
);
|
|
163
209
|
});
|
|
164
210
|
|
|
165
211
|
describe("PUT /v1/conversations/:id/inference-profile", () => {
|
|
@@ -63,6 +63,7 @@ import {
|
|
|
63
63
|
resolveConversationId,
|
|
64
64
|
setConversationKeyIfAbsent,
|
|
65
65
|
} from "../../persistence/conversation-key-store.js";
|
|
66
|
+
import type { NonScheduledConversationType } from "../../persistence/conversation-types.js";
|
|
66
67
|
import { enqueueMemoryJob } from "../../persistence/jobs-store.js";
|
|
67
68
|
import { linkRequestLogsToMessage } from "../../persistence/llm-request-log-store.js";
|
|
68
69
|
import { deleteSchedule } from "../../schedule/schedule-store.js";
|
|
@@ -100,6 +101,12 @@ const log = getLogger("conversation-management-routes");
|
|
|
100
101
|
// Helpers
|
|
101
102
|
// ---------------------------------------------------------------------------
|
|
102
103
|
|
|
104
|
+
/** Conversation types this endpoint may mint. */
|
|
105
|
+
const createConversationTypeSchema = z.enum([
|
|
106
|
+
"standard",
|
|
107
|
+
"background",
|
|
108
|
+
] as const satisfies readonly NonScheduledConversationType[]);
|
|
109
|
+
|
|
103
110
|
function resolveOrThrow(rawId: string): string {
|
|
104
111
|
const id = resolveConversationId(rawId);
|
|
105
112
|
if (!id) {
|
|
@@ -126,14 +133,26 @@ function handleCreateConversation({ body = {}, headers }: RouteHandlerArgs) {
|
|
|
126
133
|
const conversationKey =
|
|
127
134
|
(body.conversationKey as string | undefined) ?? crypto.randomUUID();
|
|
128
135
|
// The shared route adapter does not runtime-validate the body against the
|
|
129
|
-
// Zod requestBody (it's codegen-only), so guard the
|
|
130
|
-
//
|
|
136
|
+
// Zod requestBody (it's codegen-only), so guard the types here: a malformed
|
|
137
|
+
// `{ title: 123 }` would otherwise throw on `.trim()` and 500, and an
|
|
138
|
+
// unsupported conversationType would reach the store unchecked.
|
|
139
|
+
// `.nullish()`: an explicit JSON `null` is how serializers spell an absent
|
|
140
|
+
// optional, so it means the same thing as omitting the key.
|
|
141
|
+
const requestedType = createConversationTypeSchema
|
|
142
|
+
.nullish()
|
|
143
|
+
.safeParse(body.conversationType);
|
|
144
|
+
if (!requestedType.success) {
|
|
145
|
+
throw new BadRequestError(
|
|
146
|
+
`conversationType must be one of ${createConversationTypeSchema.options.join(", ")}`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
131
149
|
if (body.title !== undefined && typeof body.title !== "string") {
|
|
132
150
|
throw new BadRequestError("title must be a string");
|
|
133
151
|
}
|
|
134
152
|
const customTitle = body.title?.trim() || undefined;
|
|
153
|
+
const conversationType = requestedType.data ?? "standard";
|
|
135
154
|
const result = getOrCreateConversation(conversationKey, {
|
|
136
|
-
conversationType
|
|
155
|
+
conversationType,
|
|
137
156
|
});
|
|
138
157
|
if (result.created) {
|
|
139
158
|
// A caller-supplied title is user-set: persist it with isAutoTitle = 0 so
|
|
@@ -151,10 +170,14 @@ function handleCreateConversation({ body = {}, headers }: RouteHandlerArgs) {
|
|
|
151
170
|
headers?.["x-vellum-client-id"]?.trim() || undefined,
|
|
152
171
|
);
|
|
153
172
|
}
|
|
173
|
+
// On a conversationKey hit the row already exists, so the type it carries
|
|
174
|
+
// can differ from the requested one.
|
|
175
|
+
const storedType = normalizeConversationType(result.conversationType);
|
|
154
176
|
log.info(
|
|
155
177
|
{
|
|
156
178
|
conversationId: result.conversationId,
|
|
157
179
|
conversationKey,
|
|
180
|
+
conversationType: storedType,
|
|
158
181
|
created: result.created,
|
|
159
182
|
},
|
|
160
183
|
"Created conversation via POST",
|
|
@@ -162,7 +185,7 @@ function handleCreateConversation({ body = {}, headers }: RouteHandlerArgs) {
|
|
|
162
185
|
return {
|
|
163
186
|
id: result.conversationId,
|
|
164
187
|
conversationKey,
|
|
165
|
-
conversationType:
|
|
188
|
+
conversationType: storedType,
|
|
166
189
|
created: result.created,
|
|
167
190
|
};
|
|
168
191
|
}
|
|
@@ -863,10 +886,11 @@ export const ROUTES: RouteDefinition[] = [
|
|
|
863
886
|
.describe(
|
|
864
887
|
"Optional external key. Echoed back in the response. Non-vellum channels (Telegram, WhatsApp) use this to scope to a logical channel thread; vellum-web clients can omit it and rely on the assistant-minted `id`.",
|
|
865
888
|
),
|
|
866
|
-
conversationType:
|
|
867
|
-
.literal("standard")
|
|
889
|
+
conversationType: createConversationTypeSchema
|
|
868
890
|
.optional()
|
|
869
|
-
.describe(
|
|
891
|
+
.describe(
|
|
892
|
+
'Conversation type for the new row. "background" keeps it out of the foreground list and the sidebar\'s Recents grouping, used by internal side-channel flows (onboarding research, persona/identity rewrites) that mint a throwaway thread the user should never see. "scheduled" is not accepted here; scheduled rows are owned by the schedule pipeline.',
|
|
893
|
+
),
|
|
870
894
|
title: z
|
|
871
895
|
.string()
|
|
872
896
|
.optional()
|