feat(FN-2909): merge fusion/fn-2909
This commit is contained in:
@@ -430,6 +430,38 @@ describe("ChatStore", () => {
|
||||
expect(message.metadata).toEqual({ tokens: 150, finishReason: "stop" });
|
||||
});
|
||||
|
||||
it("round-trips attachments metadata", () => {
|
||||
const session = createTestSession(store);
|
||||
const attachments = [{
|
||||
id: "att-abc123",
|
||||
filename: "123-file.png",
|
||||
originalName: "file.png",
|
||||
mimeType: "image/png",
|
||||
size: 1024,
|
||||
createdAt: new Date().toISOString(),
|
||||
}];
|
||||
|
||||
const created = store.addMessage(session.id, {
|
||||
role: "user",
|
||||
content: "with attachment",
|
||||
attachments,
|
||||
});
|
||||
|
||||
expect(created.attachments).toEqual(attachments);
|
||||
const loaded = store.getMessage(created.id);
|
||||
expect(loaded?.attachments).toEqual(attachments);
|
||||
});
|
||||
|
||||
it("returns undefined attachments when not provided", () => {
|
||||
const session = createTestSession(store);
|
||||
const created = store.addMessage(session.id, {
|
||||
role: "user",
|
||||
content: "without attachment",
|
||||
});
|
||||
|
||||
expect(created.attachments).toBeUndefined();
|
||||
});
|
||||
|
||||
it("throws error when session does not exist", () => {
|
||||
expect(() => {
|
||||
store.addMessage("chat-nonexistent", {
|
||||
@@ -454,6 +486,53 @@ describe("ChatStore", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("addMessageAttachment", () => {
|
||||
it("appends to existing attachments", () => {
|
||||
const session = createTestSession(store);
|
||||
const message = store.addMessage(session.id, {
|
||||
role: "user",
|
||||
content: "hello",
|
||||
attachments: [{
|
||||
id: "att-1",
|
||||
filename: "a.txt",
|
||||
originalName: "a.txt",
|
||||
mimeType: "text/plain",
|
||||
size: 1,
|
||||
createdAt: new Date().toISOString(),
|
||||
}],
|
||||
});
|
||||
|
||||
const updated = store.addMessageAttachment(session.id, message.id, {
|
||||
id: "att-2",
|
||||
filename: "b.txt",
|
||||
originalName: "b.txt",
|
||||
mimeType: "text/plain",
|
||||
size: 2,
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
expect(updated.attachments).toHaveLength(2);
|
||||
expect(updated.attachments?.[1]?.id).toBe("att-2");
|
||||
});
|
||||
|
||||
it("creates attachment array when message has none", () => {
|
||||
const session = createTestSession(store);
|
||||
const message = store.addMessage(session.id, { role: "user", content: "hello" });
|
||||
|
||||
const updated = store.addMessageAttachment(session.id, message.id, {
|
||||
id: "att-3",
|
||||
filename: "c.txt",
|
||||
originalName: "c.txt",
|
||||
mimeType: "text/plain",
|
||||
size: 3,
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
expect(updated.attachments).toHaveLength(1);
|
||||
expect(updated.attachments?.[0]?.id).toBe("att-3");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getMessages", () => {
|
||||
it("returns messages for a session ordered by createdAt ASC", async () => {
|
||||
const session = createTestSession(store);
|
||||
@@ -771,6 +850,26 @@ describe("ChatStore", () => {
|
||||
expect(handler.mock.calls[0][0].id).toBe(session.id);
|
||||
});
|
||||
|
||||
it("addMessageAttachment emits chat:message:updated", () => {
|
||||
const handler = vi.fn();
|
||||
store.on("chat:message:updated", handler);
|
||||
|
||||
const session = createTestSession(store);
|
||||
const message = store.addMessage(session.id, { role: "user", content: "hello" });
|
||||
|
||||
const updated = store.addMessageAttachment(session.id, message.id, {
|
||||
id: "att-evt",
|
||||
filename: "evt.txt",
|
||||
originalName: "evt.txt",
|
||||
mimeType: "text/plain",
|
||||
size: 4,
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
expect(handler).toHaveBeenCalledWith(updated);
|
||||
});
|
||||
|
||||
it("deleteMessage does NOT emit for non-existent message", () => {
|
||||
const handler = vi.fn();
|
||||
store.on("chat:message:deleted", handler);
|
||||
|
||||
@@ -131,7 +131,7 @@ describe("Database", () => {
|
||||
});
|
||||
|
||||
it("seeds schema version", () => {
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
});
|
||||
|
||||
it("seeds lastModified", () => {
|
||||
@@ -154,7 +154,7 @@ describe("Database", () => {
|
||||
|
||||
it("is idempotent - calling init() twice does not fail", () => {
|
||||
expect(() => db.init()).not.toThrow();
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
});
|
||||
|
||||
it("does not overwrite existing config on re-init", () => {
|
||||
@@ -761,7 +761,7 @@ describe("schema migrations", () => {
|
||||
db.init();
|
||||
|
||||
// Verify version bumped to 29 (includes v1→v2 through v26→v29)
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
// Verify new columns exist and existing data is intact
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
@@ -786,11 +786,11 @@ describe("schema migrations", () => {
|
||||
const db = new Database(fusionDir);
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
// Re-init should not fail
|
||||
db.init();
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
db.close();
|
||||
});
|
||||
@@ -825,7 +825,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
expect(cols.map((col) => col.name)).toContain("priority");
|
||||
@@ -866,7 +866,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
const colNames = cols.map((col) => col.name);
|
||||
@@ -935,7 +935,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
const colNames = cols.map((col) => col.name);
|
||||
@@ -976,7 +976,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'agentRatings'").all() as Array<{ name: string }>;
|
||||
expect(tables).toEqual([{ name: "agentRatings" }]);
|
||||
@@ -1000,7 +1000,7 @@ describe("schema migrations", () => {
|
||||
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const tables = db.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = 'mission_events'").all() as Array<{ name: string }>;
|
||||
expect(tables).toEqual([{ name: "mission_events" }]);
|
||||
@@ -1104,7 +1104,7 @@ describe("schema migrations", () => {
|
||||
db.init();
|
||||
|
||||
// Verify version bumped to 29
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
// Verify new columns exist and existing data is intact
|
||||
const cols = db.prepare("PRAGMA table_info(tasks)").all() as Array<{ name: string }>;
|
||||
@@ -1555,7 +1555,7 @@ describe("createDatabase factory", () => {
|
||||
const db = createDatabase(fusionDir);
|
||||
db.init();
|
||||
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
expect(db.getLastModified()).toBeGreaterThan(0);
|
||||
|
||||
db.close();
|
||||
|
||||
@@ -779,7 +779,7 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
// Step 1: Create a fresh database at v33 (runs all migrations up to 33)
|
||||
const db1 = createDatabase(legacyDir);
|
||||
db1.init();
|
||||
expect(db1.getSchemaVersion()).toBe(50);
|
||||
expect(db1.getSchemaVersion()).toBe(51);
|
||||
db1.close();
|
||||
|
||||
// Step 2: Manually downgrade to version 32 and drop insight tables
|
||||
@@ -814,7 +814,7 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
expect(tableNamesBefore).not.toContain("project_insight_runs");
|
||||
// Now run init — this triggers the v32→v33 migration
|
||||
db3.init();
|
||||
expect(db3.getSchemaVersion()).toBe(50);
|
||||
expect(db3.getSchemaVersion()).toBe(51);
|
||||
|
||||
// Step 4: Verify insight tables exist after migration
|
||||
const tablesAfter = db3.prepare(
|
||||
@@ -845,12 +845,12 @@ describe("Migration: pre-33 DB upgrade", () => {
|
||||
try {
|
||||
const db1 = createDatabase(testDir);
|
||||
db1.init();
|
||||
expect(db1.getSchemaVersion()).toBe(50);
|
||||
expect(db1.getSchemaVersion()).toBe(51);
|
||||
db1.close();
|
||||
|
||||
const db2 = createDatabase(testDir);
|
||||
expect(() => db2.init()).not.toThrow();
|
||||
expect(db2.getSchemaVersion()).toBe(50);
|
||||
expect(db2.getSchemaVersion()).toBe(51);
|
||||
db2.close();
|
||||
} finally {
|
||||
rmSync(testDir, { recursive: true, force: true });
|
||||
|
||||
@@ -2629,7 +2629,7 @@ describe("MissionStore", () => {
|
||||
|
||||
describe("Loop State & Validator Run Schema (v31)", () => {
|
||||
it("schema version is 40 after migration", () => {
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
});
|
||||
|
||||
it("mission_features table has loop state columns", () => {
|
||||
|
||||
@@ -742,7 +742,7 @@ describe("RoadmapStore", () => {
|
||||
|
||||
describe("schema version", () => {
|
||||
it("schema version is 40 after init", () => {
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -465,7 +465,7 @@ describe("Run Audit", () => {
|
||||
});
|
||||
|
||||
it("schema version is bumped to 40", () => {
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,7 +51,7 @@ describe("TaskStore task documents", () => {
|
||||
|
||||
expect(tableNames.has("task_documents")).toBe(true);
|
||||
expect(tableNames.has("task_document_revisions")).toBe(true);
|
||||
expect(db.getSchemaVersion()).toBe(50);
|
||||
expect(db.getSchemaVersion()).toBe(51);
|
||||
|
||||
const index = db
|
||||
.prepare(
|
||||
|
||||
@@ -19,6 +19,7 @@ import type {
|
||||
ChatSessionStatus,
|
||||
ChatMessage,
|
||||
ChatMessageRole,
|
||||
ChatAttachment,
|
||||
ChatMessageCreateInput,
|
||||
ChatSessionCreateInput,
|
||||
ChatSessionUpdateInput,
|
||||
@@ -38,6 +39,8 @@ export interface ChatStoreEvents {
|
||||
"chat:message:added": [message: ChatMessage];
|
||||
/** Emitted when a message is deleted from a session */
|
||||
"chat:message:deleted": [messageId: string];
|
||||
/** Emitted when a message is updated (e.g., attachment appended) */
|
||||
"chat:message:updated": [message: ChatMessage];
|
||||
}
|
||||
|
||||
// ── Row Interfaces ───────────────────────────────────────────────────
|
||||
@@ -63,6 +66,7 @@ interface ChatMessageRow {
|
||||
content: string;
|
||||
thinkingOutput: string | null;
|
||||
metadata: string | null;
|
||||
attachments: string | null;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
@@ -107,6 +111,7 @@ export class ChatStore extends EventEmitter<ChatStoreEvents> {
|
||||
content: row.content,
|
||||
thinkingOutput: row.thinkingOutput ?? null,
|
||||
metadata: fromJson<Record<string, unknown>>(row.metadata) ?? null,
|
||||
attachments: fromJson<ChatAttachment[]>(row.attachments) ?? undefined,
|
||||
createdAt: row.createdAt,
|
||||
};
|
||||
}
|
||||
@@ -369,12 +374,13 @@ export class ChatStore extends EventEmitter<ChatStoreEvents> {
|
||||
content: input.content,
|
||||
thinkingOutput: input.thinkingOutput ?? null,
|
||||
metadata: input.metadata ?? null,
|
||||
attachments: input.attachments,
|
||||
createdAt: now,
|
||||
};
|
||||
|
||||
this.db.prepare(`
|
||||
INSERT INTO chat_messages (id, sessionId, role, content, thinkingOutput, metadata, createdAt)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO chat_messages (id, sessionId, role, content, thinkingOutput, metadata, attachments, createdAt)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`).run(
|
||||
message.id,
|
||||
message.sessionId,
|
||||
@@ -382,6 +388,7 @@ export class ChatStore extends EventEmitter<ChatStoreEvents> {
|
||||
message.content,
|
||||
message.thinkingOutput,
|
||||
toJsonNullable(message.metadata),
|
||||
toJsonNullable(message.attachments),
|
||||
message.createdAt,
|
||||
);
|
||||
|
||||
@@ -393,6 +400,32 @@ export class ChatStore extends EventEmitter<ChatStoreEvents> {
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a file attachment metadata record to an existing message.
|
||||
*/
|
||||
addMessageAttachment(sessionId: string, messageId: string, attachment: ChatAttachment): ChatMessage {
|
||||
const message = this.getMessage(messageId);
|
||||
if (!message || message.sessionId !== sessionId) {
|
||||
throw new Error(`Message ${messageId} not found in session ${sessionId}`);
|
||||
}
|
||||
|
||||
const updatedAttachments = [...(message.attachments ?? []), attachment];
|
||||
this.db.prepare(`
|
||||
UPDATE chat_messages
|
||||
SET attachments = ?
|
||||
WHERE id = ?
|
||||
`).run(toJsonNullable(updatedAttachments), messageId);
|
||||
|
||||
const updated = this.getMessage(messageId);
|
||||
if (!updated) {
|
||||
throw new Error(`Failed to update message ${messageId}`);
|
||||
}
|
||||
|
||||
this.db.bumpLastModified();
|
||||
this.emit("chat:message:updated", updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get messages for a chat session with optional filtering.
|
||||
*
|
||||
|
||||
@@ -63,6 +63,18 @@ export interface ChatMention {
|
||||
agentName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* File attachment metadata associated with a chat message.
|
||||
*/
|
||||
export interface ChatAttachment {
|
||||
id: string;
|
||||
filename: string;
|
||||
originalName: string;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A single message within a chat session.
|
||||
*/
|
||||
@@ -78,6 +90,8 @@ export interface ChatMessage {
|
||||
thinkingOutput: string | null;
|
||||
/** Additional metadata about the message (model, tokens, finish reason, etc.) */
|
||||
metadata: Record<string, unknown> | null;
|
||||
/** Optional file attachments uploaded before sending this message */
|
||||
attachments?: ChatAttachment[];
|
||||
/** When the message was created */
|
||||
createdAt: string;
|
||||
}
|
||||
@@ -94,6 +108,8 @@ export interface ChatMessageCreateInput {
|
||||
thinkingOutput?: string | null;
|
||||
/** Optional metadata (e.g., { tokens: 150, finishReason: "stop" }) */
|
||||
metadata?: Record<string, unknown> | null;
|
||||
/** Optional attachment metadata uploaded before send */
|
||||
attachments?: ChatAttachment[];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -86,7 +86,7 @@ export function probeFts5(db: DatabaseSync): boolean {
|
||||
|
||||
// ── Schema Definition ────────────────────────────────────────────────
|
||||
|
||||
const SCHEMA_VERSION = 50;
|
||||
const SCHEMA_VERSION = 51;
|
||||
|
||||
function normalizeTaskComments(
|
||||
steeringComments: SteeringComment[] | undefined,
|
||||
@@ -1941,6 +1941,14 @@ export class Database {
|
||||
});
|
||||
}
|
||||
|
||||
if (version < 51) {
|
||||
this.applyMigration(51, () => {
|
||||
if (this.hasTable("chat_messages")) {
|
||||
this.addColumnIfMissing("chat_messages", "attachments", "TEXT");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -628,6 +628,7 @@ export type {
|
||||
ChatSessionSummary,
|
||||
EnrichedChatSession,
|
||||
ChatMention,
|
||||
ChatAttachment,
|
||||
ChatMessage,
|
||||
ChatMessageCreateInput,
|
||||
ChatSessionCreateInput,
|
||||
|
||||
Reference in New Issue
Block a user