@firstlovecenter/ai-chat 0.5.0 → 0.6.1
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/CHANGELOG.md +39 -0
- package/dist/drizzle/index.cjs +24 -0
- package/dist/drizzle/index.cjs.map +1 -1
- package/dist/drizzle/index.d.cts +35 -1
- package/dist/drizzle/index.d.ts +35 -1
- package/dist/drizzle/index.js +25 -1
- package/dist/drizzle/index.js.map +1 -1
- package/dist/prisma/index.cjs +7 -0
- package/dist/prisma/index.cjs.map +1 -1
- package/dist/prisma/index.d.cts +7 -1
- package/dist/prisma/index.d.ts +7 -1
- package/dist/prisma/index.js +7 -0
- package/dist/prisma/index.js.map +1 -1
- package/dist/server/index.cjs +47 -12
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +12 -3
- package/dist/server/index.d.ts +12 -3
- package/dist/server/index.js +47 -12
- package/dist/server/index.js.map +1 -1
- package/dist/{types-CDKxdzQc.d.cts → types-CQntnyDJ.d.cts} +15 -2
- package/dist/{types-CDKxdzQc.d.ts → types-CQntnyDJ.d.ts} +15 -2
- package/dist/ui/index.cjs +85 -9
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.js +85 -9
- package/dist/ui/index.js.map +1 -1
- package/package.json +1 -1
- package/prisma/chat-models.prisma +7 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,43 @@ All notable changes to `@firstlovecenter/ai-chat` are documented here.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
6
|
and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.6.1] — 2026-05-08
|
|
9
|
+
|
|
10
|
+
### Fixed
|
|
11
|
+
|
|
12
|
+
- **`isBlockEmpty()` crash on malformed historical rows** — `AnswerBlocks`'s render-time filter called `f.trim()` on every entry of `key_facts` / `items`, which threw `Cannot read properties of undefined (reading 'trim')` when a stored chat had a `null` or non-string entry in either array. Stored chats from truncated reasoning-model responses (pre-0.6.0, before `max_output_tokens` was admin-configurable) commonly contained such entries. The new `isBlockEmpty` treats null / undefined / non-string entries as blank via a small `isBlankString()` helper, and guards `.length === 0` checks behind `Array.isArray(...)` so a row missing the array entirely also no-ops cleanly. Auto-resuming an older session now renders without throwing.
|
|
13
|
+
|
|
14
|
+
## [0.6.0] — 2026-05-08
|
|
15
|
+
|
|
16
|
+
Admin-configurable runtime knobs and chat continuity. Two new fields on `ai_settings` give admins control over the token budget and the assistant's persona without a redeploy, and both chat UIs now auto-resume the most recent session on return.
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- **`ai_settings.max_output_tokens`** (`INT NOT NULL DEFAULT 4096`) — caps both the agent loop's per-turn output AND each narrator's prose pass. Replaces the hardcoded `max_tokens: 400` that was silently truncating prose on reasoning models (`xai/grok-4.1-fast-reasoning` and similar charge internal thinking against this budget; 400 was getting consumed before the model produced any visible text). Bump to 8000–16000 for reasoning models, 16000–24000 for complex multi-tool queries.
|
|
21
|
+
- **`ai_settings.role_prompt`** (`TEXT NULL`) — admin-editable assistant persona. When non-null, takes precedence over the host's static `configureAiChat({ rolePrompt })` option. Resolution order per turn: DB value → static config option → no role block. Hosts can drop their hardcoded role string entirely once the admin UI is wired.
|
|
22
|
+
- **Auto-open most recent session** in `AiChat` and `VercelChat`. On first mount, if the user has chat history, the most recent session is opened automatically; the empty hero greeting only renders for fresh users. Guarded by a `useRef` so subsequent session-list refreshes (e.g. after submit) don't override an explicit "+ New chat".
|
|
23
|
+
- **`DEFAULT_AI_MAX_OUTPUT_TOKENS`** (= 4096) — exported from `@firstlovecenter/ai-chat/server` for hosts that want the same fallback constant in their own glue.
|
|
24
|
+
|
|
25
|
+
### Changed
|
|
26
|
+
|
|
27
|
+
- **`AiSettings` and `AiSettingsPatch` types** gain `maxOutputTokens: number` and `rolePrompt: string | null` (patch type makes both optional; passing `null` for `rolePrompt` clears the override). All three adapters (drizzle, prisma, memory) thread the new fields through identically; column names match across ORMs so DB-level swaps remain feasible.
|
|
28
|
+
- **Narrator function signatures** (`StreamClaudeNarrationOpts`, `StreamGrokNarrationOpts`, `StreamGeminiNarrationOpts`, and the unioned `NarrationOpts`) now require `maxTokens: number`. Callers must pass `aiSettings.maxOutputTokens` from the persistence layer.
|
|
29
|
+
- **`agent-custom` and `agent-vercel` routes** now pass `aiSettings.maxOutputTokens` into both `runAgent({ maxOutputTokens })` and the narrator (custom path) or `streamText({ maxTokens })` (vercel path).
|
|
30
|
+
- **`admin-settings` PATCH** validates `max_output_tokens` (positive integer in `[256, 64000]`) and `role_prompt` (string or null; whitespace-only strings canonicalised to null). New error codes: `invalid_max_output_tokens`, `invalid_role_prompt`. The GET response now includes both fields.
|
|
31
|
+
- **`configureAiChat` rolePrompt resolution** — the wrapped `buildSystemBlocks` consults `aiSettings.rolePrompt` per request and uses it when set; falls back to `opts.rolePrompt` (string or function) otherwise. Empty / whitespace-only strings on either side are treated as "no role prompt".
|
|
32
|
+
|
|
33
|
+
### Drizzle migration
|
|
34
|
+
|
|
35
|
+
Hosts using the drizzle adapter run `pnpm drizzle:generate` after upgrading to pick up the two new columns:
|
|
36
|
+
|
|
37
|
+
```sql
|
|
38
|
+
ALTER TABLE `ai_settings`
|
|
39
|
+
ADD `max_output_tokens` int NOT NULL DEFAULT 4096,
|
|
40
|
+
ADD `role_prompt` text;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Prisma hosts re-paste the updated `ai_settings` model fragment from `node_modules/@firstlovecenter/ai-chat/prisma/chat-models.prisma` and run `pnpm prisma migrate dev`.
|
|
44
|
+
|
|
8
45
|
## [0.5.0] — 2026-05-08
|
|
9
46
|
|
|
10
47
|
Phase 2 of the package: a second chat interface that streams via the **Vercel AI SDK** alongside the existing custom SSE chat, with full structured-tool parity (charts, tables, callouts) and shared persistence so a session opened under one UI is identical under the other.
|
|
@@ -115,6 +152,8 @@ Initial public release on npm under `@firstlovecenter/ai-chat`.
|
|
|
115
152
|
- Dual ESM + CJS output via `tsup` with full `.d.ts` (and `.d.cts`) generation.
|
|
116
153
|
- UI bundle gets a post-build `'use client';` directive injection (tsup otherwise strips module-level directives during bundling, breaking RSC consumers that import the UI from a server file).
|
|
117
154
|
|
|
155
|
+
[0.6.1]: https://github.com/firstlovecenter/flc-ai-chat/compare/v0.6.0...v0.6.1
|
|
156
|
+
[0.6.0]: https://github.com/firstlovecenter/flc-ai-chat/compare/v0.5.0...v0.6.0
|
|
118
157
|
[0.5.0]: https://github.com/firstlovecenter/flc-ai-chat/compare/v0.2.3...v0.5.0
|
|
119
158
|
[0.2.3]: https://github.com/firstlovecenter/flc-ai-chat/compare/v0.2.2...v0.2.3
|
|
120
159
|
[0.2.2]: https://github.com/firstlovecenter/flc-ai-chat/compare/v0.2.1...v0.2.2
|
package/dist/drizzle/index.cjs
CHANGED
|
@@ -13,6 +13,13 @@ var aiSettings = mysqlCore.mysqlTable("ai_settings", {
|
|
|
13
13
|
toolProvider: mysqlCore.varchar("tool_provider", { length: 32 }).notNull().default("claude"),
|
|
14
14
|
gcpLocation: mysqlCore.varchar("gcp_location", { length: 32 }).notNull().default("us-east5"),
|
|
15
15
|
chatInterface: mysqlCore.varchar("chat_interface", { length: 32 }).notNull().default("custom"),
|
|
16
|
+
// Caps the per-turn output budget for both the agent loop AND the prose
|
|
17
|
+
// narrator. Reasoning models charge internal thinking against this; bump
|
|
18
|
+
// it well past 4096 when one is in use.
|
|
19
|
+
maxOutputTokens: mysqlCore.int("max_output_tokens").notNull().default(4096),
|
|
20
|
+
// Optional admin-editable role/persona. NULL means the host's static
|
|
21
|
+
// `rolePrompt` (passed to configureAiChat) is used as the fallback.
|
|
22
|
+
rolePrompt: mysqlCore.text("role_prompt"),
|
|
16
23
|
updatedAt: mysqlCore.datetime("updated_at", { mode: "string" }).notNull().default(drizzleOrm.sql`CURRENT_TIMESTAMP`),
|
|
17
24
|
updatedByUserId: bigintFkNullable("updated_by_user_id")
|
|
18
25
|
});
|
|
@@ -46,6 +53,11 @@ var chatMessages = mysqlCore.mysqlTable(
|
|
|
46
53
|
idxSessionCreated: mysqlCore.index("idx_chat_msg_session_created").on(t.sessionId, t.createdAt)
|
|
47
54
|
})
|
|
48
55
|
);
|
|
56
|
+
|
|
57
|
+
// src/server/ports/types.ts
|
|
58
|
+
var DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;
|
|
59
|
+
|
|
60
|
+
// src/adapters/drizzle/adapter.ts
|
|
49
61
|
function mapSession(row) {
|
|
50
62
|
return {
|
|
51
63
|
id: row.id,
|
|
@@ -72,6 +84,8 @@ function mapSettings(row) {
|
|
|
72
84
|
toolProvider: row.toolProvider,
|
|
73
85
|
gcpLocation: row.gcpLocation,
|
|
74
86
|
chatInterface: row.chatInterface,
|
|
87
|
+
maxOutputTokens: row.maxOutputTokens,
|
|
88
|
+
rolePrompt: row.rolePrompt,
|
|
75
89
|
updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,
|
|
76
90
|
updatedByUserId: row.updatedByUserId
|
|
77
91
|
};
|
|
@@ -155,6 +169,8 @@ function createDrizzlePersistence(db) {
|
|
|
155
169
|
toolProvider: "claude",
|
|
156
170
|
gcpLocation: "us-east5",
|
|
157
171
|
chatInterface: "custom",
|
|
172
|
+
maxOutputTokens: DEFAULT_AI_MAX_OUTPUT_TOKENS,
|
|
173
|
+
rolePrompt: null,
|
|
158
174
|
updatedAt: null,
|
|
159
175
|
updatedByUserId: null
|
|
160
176
|
};
|
|
@@ -167,6 +183,8 @@ function createDrizzlePersistence(db) {
|
|
|
167
183
|
toolProvider: patch.toolProvider ?? "claude",
|
|
168
184
|
gcpLocation: patch.gcpLocation ?? "us-east5",
|
|
169
185
|
chatInterface: patch.chatInterface ?? "custom",
|
|
186
|
+
maxOutputTokens: patch.maxOutputTokens ?? DEFAULT_AI_MAX_OUTPUT_TOKENS,
|
|
187
|
+
rolePrompt: patch.rolePrompt ?? null,
|
|
170
188
|
updatedByUserId: byUserId
|
|
171
189
|
};
|
|
172
190
|
const updateSet = {
|
|
@@ -181,6 +199,12 @@ function createDrizzlePersistence(db) {
|
|
|
181
199
|
if (patch.chatInterface !== void 0) {
|
|
182
200
|
updateSet.chatInterface = patch.chatInterface;
|
|
183
201
|
}
|
|
202
|
+
if (patch.maxOutputTokens !== void 0) {
|
|
203
|
+
updateSet.maxOutputTokens = patch.maxOutputTokens;
|
|
204
|
+
}
|
|
205
|
+
if (patch.rolePrompt !== void 0) {
|
|
206
|
+
updateSet.rolePrompt = patch.rolePrompt;
|
|
207
|
+
}
|
|
184
208
|
await db.insert(aiSettings).values(insertValues).onDuplicateKeyUpdate({ set: updateSet });
|
|
185
209
|
const [row] = await db.select().from(aiSettings).where(drizzleOrm.eq(aiSettings.id, 1)).limit(1);
|
|
186
210
|
if (!row) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/drizzle/tables.ts","../../src/adapters/drizzle/adapter.ts"],"names":["bigint","datetime","sql","mysqlTable","tinyint","varchar","index","mysqlEnum","text","json","eq","and","desc"],"mappings":";;;;;;AAuCA,IAAM,QAAA,GAAW,MACfA,gBAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAC5C,OAAA,EAAQ,CACR,UAAA,GACA,aAAA,EAAc;AAGnB,IAAM,QAAA,GAAW,CAAC,IAAA,KAChBA,gBAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAAE,OAAA,EAAQ;AAG3D,IAAM,gBAAA,GAAmB,CAAC,IAAA,KACxBA,gBAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA;AAGjD,IAAM,SAAA,GAAY,MAChBC,kBAAA,CAAS,YAAA,EAAc,EAAE,IAAA,EAAM,QAAA,EAAU,CA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AAEA,MAAA,MAAM,EAAA,CACH,MAAA,CAAO,UAAU,CAAA,CACjB,MAAA,CAAO,YAAY,CAAA,CACnB,oBAAA,CAAqB,EAAE,GAAA,EAAK,SAAA,EAAW,CAAA;AAE1C,MAAA,MAAM,CAAC,GAAG,CAAA,GAAI,MAAM,EAAA,CACjB,MAAA,GACA,IAAA,CAAK,UAAU,CAAA,CACf,KAAA,CAAMA,cAAG,UAAA,CAAW,EAAA,EAAI,CAAC,CAAC,CAAA,CAC1B,MAAM,CAAC,CAAA;AAEV,MAAA,IAAI,CAAC,GAAA,EAAK;AACR,QAAA,MAAM,IAAI,MAAM,sDAAsD,CAAA;AAAA,MACxE;AACA,MAAA,OAAO,YAAY,GAAG,CAAA;AAAA,IACxB;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Canonical drizzle MySQL table definitions for `@firstlovecenter/ai-chat`.\n *\n * These three tables (`chat_sessions`, `chat_messages`, `ai_settings`) are\n * the data shape the package needs to persist its conversations and global\n * config. The host re-exports them from its own schema so that an existing\n * deployment keeps the same column names, lengths, FKs, and indexes — making\n * the host's data fully portable into and out of the package.\n *\n * Deviations from the host's prior shape are deliberate:\n * - `tool_provider` and `gcp_location` are plain VARCHAR (not enums) so a\n * consumer can register additional providers / regions without a schema\n * migration. Validation against the runtime registries happens at a\n * higher layer.\n * - `chat_interface` is a new VARCHAR column controlling which chat UI\n * (custom vs. vercel-ai) renders globally; default 'custom'.\n *\n * The small column helpers from the host (`bigintPk`, `bigintFk`,\n * `bigintFkNullable`, `createdAt`) are re-defined inline here — this package\n * never imports from the host repo.\n */\nimport { sql } from 'drizzle-orm';\nimport {\n bigint,\n datetime,\n index,\n json,\n mysqlEnum,\n mysqlTable,\n text,\n tinyint,\n varchar\n} from 'drizzle-orm/mysql-core';\n\n// ---------------------------------------------------------------------------\n// Inline column helpers (mirrors host `src/db/columns.ts` shapes)\n// ---------------------------------------------------------------------------\n\n/** BIGINT UNSIGNED PK with auto-increment, returned as `number`. */\nconst bigintPk = () =>\n bigint('id', { mode: 'number', unsigned: true })\n .notNull()\n .primaryKey()\n .autoincrement();\n\n/** NOT NULL BIGINT UNSIGNED FK column, returned as `number`. */\nconst bigintFk = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true }).notNull();\n\n/** Nullable BIGINT UNSIGNED FK column, returned as `number | null`. */\nconst bigintFkNullable = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true });\n\n/** `created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP`, mode 'string'. */\nconst createdAt = () =>\n datetime('created_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`);\n\n// ---------------------------------------------------------------------------\n// ai_settings (singleton — always one row at id=1)\n// ---------------------------------------------------------------------------\n\n/**\n * Global AI configuration. Singleton row enforced by `id=1` default + PK.\n * `tool_provider`, `gcp_location`, and `chat_interface` are open VARCHARs;\n * the runtime layer validates against the host's registries before write.\n */\nexport const aiSettings = mysqlTable('ai_settings', {\n id: tinyint('id').notNull().primaryKey().default(1),\n toolProvider: varchar('tool_provider', { length: 32 })\n .notNull()\n .default('claude'),\n gcpLocation: varchar('gcp_location', { length: 32 })\n .notNull()\n .default('us-east5'),\n chatInterface: varchar('chat_interface', { length: 32 })\n .notNull()\n .default('custom'),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`),\n updatedByUserId: bigintFkNullable('updated_by_user_id')\n});\n\n// ---------------------------------------------------------------------------\n// chat_sessions (one per conversation)\n// ---------------------------------------------------------------------------\n\nexport const chatSessions = mysqlTable(\n 'chat_sessions',\n {\n id: bigintPk(),\n userId: bigintFk('user_id'),\n title: varchar('title', { length: 200 }).notNull(),\n createdAt: createdAt(),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`)\n },\n (t) => ({\n idxUserUpdated: index('idx_chat_session_user_updated').on(t.userId, t.updatedAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// chat_messages (one row per turn in a chat_session)\n// ---------------------------------------------------------------------------\n\nexport const chatMessageRoleEnum = ['user', 'assistant'] as const;\n\nexport const chatMessages = mysqlTable(\n 'chat_messages',\n {\n id: bigintPk(),\n sessionId: bigintFk('session_id'),\n role: mysqlEnum('role', chatMessageRoleEnum).notNull(),\n question: text('question'),\n blocks: json('blocks'),\n prose: json('prose'),\n errorJson: json('error_json'),\n createdAt: createdAt()\n },\n (t) => ({\n idxSessionCreated: index('idx_chat_msg_session_created').on(t.sessionId, t.createdAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// Inferred row types — distinct from the domain types in ports/types.ts.\n// Adapters map these row shapes (datetime as string) to the domain shapes\n// (Date) at the boundary.\n// ---------------------------------------------------------------------------\n\nexport type ChatSessionRow = typeof chatSessions.$inferSelect;\nexport type ChatMessageRow = typeof chatMessages.$inferSelect;\nexport type AiSettingsRow = typeof aiSettings.$inferSelect;\n","/**\n * Drizzle MySQL implementation of `PersistencePort`.\n *\n * The package never imports a concrete drizzle client — the host passes a\n * `MySql2Database` (or any compatible drizzle MySQL handle) and we use it\n * to issue the queries this port describes.\n *\n * Boundary mapping: the drizzle MySQL `datetime` columns are declared with\n * `mode: 'string'` to match the host's existing schema, so row reads return\n * ISO strings. The PersistencePort domain types use `Date`, so every row\n * crossing the boundary is converted via `new Date(row.createdAt)`.\n *\n * Per-user safety: `getSession`, `updateSession`, `deleteSession`, and\n * `listMessagesForSession` all join `userId` into the WHERE clause — the\n * port's contract is that no caller should be able to read or mutate\n * another user's data even if they've forged a session id.\n */\nimport { and, desc, eq } from 'drizzle-orm';\nimport type { MySql2Database } from 'drizzle-orm/mysql2';\n\nimport type {\n AiSettings,\n AiSettingsPatch,\n AppendMessageInput,\n ChatMessage,\n ChatMessageRole,\n ChatSession,\n CreateSessionInput,\n ListSessionsOpts,\n PersistencePort\n} from '../../server/ports/types';\n\nimport {\n aiSettings,\n chatMessages,\n chatSessions,\n type AiSettingsRow,\n type ChatMessageRow,\n type ChatSessionRow\n} from './tables';\n\n// ---------------------------------------------------------------------------\n// Row -> domain mappers\n// ---------------------------------------------------------------------------\n\nfunction mapSession(row: ChatSessionRow): ChatSession {\n return {\n id: row.id,\n userId: row.userId,\n title: row.title,\n createdAt: new Date(row.createdAt),\n updatedAt: new Date(row.updatedAt)\n };\n}\n\nfunction mapMessage(row: ChatMessageRow): ChatMessage {\n return {\n id: row.id,\n sessionId: row.sessionId,\n role: row.role as ChatMessageRole,\n question: row.question,\n blocks: row.blocks,\n prose: row.prose,\n errorJson: row.errorJson,\n createdAt: new Date(row.createdAt)\n };\n}\n\nfunction mapSettings(row: AiSettingsRow): AiSettings {\n return {\n toolProvider: row.toolProvider,\n gcpLocation: row.gcpLocation,\n chatInterface: row.chatInterface,\n updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,\n updatedByUserId: row.updatedByUserId\n };\n}\n\n// ---------------------------------------------------------------------------\n// Factory\n// ---------------------------------------------------------------------------\n\nexport function createDrizzlePersistence(\n db: MySql2Database<any>\n): PersistencePort {\n return {\n // ---------------------------------------------------------------------\n // Sessions\n // ---------------------------------------------------------------------\n\n async createSession(input: CreateSessionInput): Promise<ChatSession> {\n const inserted = await db\n .insert(chatSessions)\n .values({\n userId: input.userId,\n title: input.title\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('createSession: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`createSession: row ${id} not found after insert`);\n }\n return mapSession(row);\n },\n\n async getSession(id: number, userId: number): Promise<ChatSession | null> {\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n return row ? mapSession(row) : null;\n },\n\n async listSessionsForUser(\n userId: number,\n opts?: ListSessionsOpts\n ): Promise<ChatSession[]> {\n const limit = opts?.limit ?? 50;\n const rows = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.userId, userId))\n .orderBy(desc(chatSessions.updatedAt))\n .limit(limit);\n\n return rows.map(mapSession);\n },\n\n async updateSession(\n id: number,\n userId: number,\n patch: { title?: string }\n ): Promise<void> {\n if (patch.title === undefined) return;\n\n await db\n .update(chatSessions)\n .set({ title: patch.title })\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n async deleteSession(id: number, userId: number): Promise<void> {\n // Delete messages first (no ON DELETE CASCADE assumed at table level).\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n if (owned.length === 0) return;\n\n await db.delete(chatMessages).where(eq(chatMessages.sessionId, id));\n await db\n .delete(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n // ---------------------------------------------------------------------\n // Messages\n // ---------------------------------------------------------------------\n\n async appendMessage(input: AppendMessageInput): Promise<ChatMessage> {\n const inserted = await db\n .insert(chatMessages)\n .values({\n sessionId: input.sessionId,\n role: input.role,\n question: input.question ?? null,\n blocks: input.blocks ?? null,\n prose: input.prose ?? null,\n errorJson: input.errorJson ?? null\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('appendMessage: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`appendMessage: row ${id} not found after insert`);\n }\n return mapMessage(row);\n },\n\n async listMessagesForSession(\n sessionId: number,\n userId: number\n ): Promise<ChatMessage[]> {\n // Verify ownership before returning rows — never trust the caller to\n // have filtered already.\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(\n and(eq(chatSessions.id, sessionId), eq(chatSessions.userId, userId))\n )\n .limit(1);\n\n if (owned.length === 0) return [];\n\n const rows = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.sessionId, sessionId))\n .orderBy(chatMessages.createdAt, chatMessages.id);\n\n return rows.map(mapMessage);\n },\n\n // ---------------------------------------------------------------------\n // AI settings (singleton row)\n // ---------------------------------------------------------------------\n\n async getAiSettings(): Promise<AiSettings> {\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n // Synthesize defaults rather than failing — the row is created on\n // first write via INSERT … ON DUPLICATE KEY UPDATE.\n return {\n toolProvider: 'claude',\n gcpLocation: 'us-east5',\n chatInterface: 'custom',\n updatedAt: null,\n updatedByUserId: null\n };\n }\n\n return mapSettings(row);\n },\n\n async updateAiSettings(\n patch: AiSettingsPatch,\n byUserId: number\n ): Promise<AiSettings> {\n // Build the values object with the singleton id and any defaults the\n // INSERT branch needs. ON DUPLICATE KEY UPDATE only touches keys in\n // the patch (plus updated_at / updated_by_user_id audit columns).\n const insertValues = {\n id: 1 as const,\n toolProvider: patch.toolProvider ?? 'claude',\n gcpLocation: patch.gcpLocation ?? 'us-east5',\n chatInterface: patch.chatInterface ?? 'custom',\n updatedByUserId: byUserId\n };\n\n const updateSet: Record<string, unknown> = {\n updatedByUserId: byUserId\n };\n if (patch.toolProvider !== undefined) {\n updateSet.toolProvider = patch.toolProvider;\n }\n if (patch.gcpLocation !== undefined) {\n updateSet.gcpLocation = patch.gcpLocation;\n }\n if (patch.chatInterface !== undefined) {\n updateSet.chatInterface = patch.chatInterface;\n }\n\n await db\n .insert(aiSettings)\n .values(insertValues)\n .onDuplicateKeyUpdate({ set: updateSet });\n\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n throw new Error('updateAiSettings: singleton row missing after upsert');\n }\n return mapSettings(row);\n }\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/drizzle/tables.ts","../../src/server/ports/types.ts","../../src/adapters/drizzle/adapter.ts"],"names":["bigint","datetime","sql","mysqlTable","tinyint","varchar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AC/B;AAEA,MAAA,MAAM,EAAA,CACH,MAAA,CAAO,UAAU,CAAA,CACjB,MAAA,CAAO,YAAY,CAAA,CACnB,oBAAA,CAAqB,EAAE,GAAA,EAAK,SAAA,EAAW,CAAA;AAE1C,MAAA,MAAM,CAAC,GAAG,CAAA,GAAI,MAAM,EAAA,CACjB,MAAA,GACA,IAAA,CAAK,UAAU,CAAA,CACf,KAAA,CAAMA,cAAG,UAAA,CAAW,EAAA,EAAI,CAAC,CAAC,CAAA,CAC1B,MAAM,CAAC,CAAA;AAEV,MAAA,IAAI,CAAC,GAAA,EAAK;AACR,QAAA,MAAM,IAAI,MAAM,sDAAsD,CAAA;AAAA,MACxE;AACA,MAAA,OAAO,YAAY,GAAG,CAAA;AAAA,IACxB;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["/**\n * Canonical drizzle MySQL table definitions for `@firstlovecenter/ai-chat`.\n *\n * These three tables (`chat_sessions`, `chat_messages`, `ai_settings`) are\n * the data shape the package needs to persist its conversations and global\n * config. The host re-exports them from its own schema so that an existing\n * deployment keeps the same column names, lengths, FKs, and indexes — making\n * the host's data fully portable into and out of the package.\n *\n * Deviations from the host's prior shape are deliberate:\n * - `tool_provider` and `gcp_location` are plain VARCHAR (not enums) so a\n * consumer can register additional providers / regions without a schema\n * migration. Validation against the runtime registries happens at a\n * higher layer.\n * - `chat_interface` is a new VARCHAR column controlling which chat UI\n * (custom vs. vercel-ai) renders globally; default 'custom'.\n *\n * The small column helpers from the host (`bigintPk`, `bigintFk`,\n * `bigintFkNullable`, `createdAt`) are re-defined inline here — this package\n * never imports from the host repo.\n */\nimport { sql } from 'drizzle-orm';\nimport {\n bigint,\n datetime,\n index,\n int,\n json,\n mysqlEnum,\n mysqlTable,\n text,\n tinyint,\n varchar\n} from 'drizzle-orm/mysql-core';\n\n// ---------------------------------------------------------------------------\n// Inline column helpers (mirrors host `src/db/columns.ts` shapes)\n// ---------------------------------------------------------------------------\n\n/** BIGINT UNSIGNED PK with auto-increment, returned as `number`. */\nconst bigintPk = () =>\n bigint('id', { mode: 'number', unsigned: true })\n .notNull()\n .primaryKey()\n .autoincrement();\n\n/** NOT NULL BIGINT UNSIGNED FK column, returned as `number`. */\nconst bigintFk = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true }).notNull();\n\n/** Nullable BIGINT UNSIGNED FK column, returned as `number | null`. */\nconst bigintFkNullable = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true });\n\n/** `created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP`, mode 'string'. */\nconst createdAt = () =>\n datetime('created_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`);\n\n// ---------------------------------------------------------------------------\n// ai_settings (singleton — always one row at id=1)\n// ---------------------------------------------------------------------------\n\n/**\n * Global AI configuration. Singleton row enforced by `id=1` default + PK.\n * `tool_provider`, `gcp_location`, and `chat_interface` are open VARCHARs;\n * the runtime layer validates against the host's registries before write.\n */\nexport const aiSettings = mysqlTable('ai_settings', {\n id: tinyint('id').notNull().primaryKey().default(1),\n toolProvider: varchar('tool_provider', { length: 32 })\n .notNull()\n .default('claude'),\n gcpLocation: varchar('gcp_location', { length: 32 })\n .notNull()\n .default('us-east5'),\n chatInterface: varchar('chat_interface', { length: 32 })\n .notNull()\n .default('custom'),\n // Caps the per-turn output budget for both the agent loop AND the prose\n // narrator. Reasoning models charge internal thinking against this; bump\n // it well past 4096 when one is in use.\n maxOutputTokens: int('max_output_tokens').notNull().default(4096),\n // Optional admin-editable role/persona. NULL means the host's static\n // `rolePrompt` (passed to configureAiChat) is used as the fallback.\n rolePrompt: text('role_prompt'),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`),\n updatedByUserId: bigintFkNullable('updated_by_user_id')\n});\n\n// ---------------------------------------------------------------------------\n// chat_sessions (one per conversation)\n// ---------------------------------------------------------------------------\n\nexport const chatSessions = mysqlTable(\n 'chat_sessions',\n {\n id: bigintPk(),\n userId: bigintFk('user_id'),\n title: varchar('title', { length: 200 }).notNull(),\n createdAt: createdAt(),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`)\n },\n (t) => ({\n idxUserUpdated: index('idx_chat_session_user_updated').on(t.userId, t.updatedAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// chat_messages (one row per turn in a chat_session)\n// ---------------------------------------------------------------------------\n\nexport const chatMessageRoleEnum = ['user', 'assistant'] as const;\n\nexport const chatMessages = mysqlTable(\n 'chat_messages',\n {\n id: bigintPk(),\n sessionId: bigintFk('session_id'),\n role: mysqlEnum('role', chatMessageRoleEnum).notNull(),\n question: text('question'),\n blocks: json('blocks'),\n prose: json('prose'),\n errorJson: json('error_json'),\n createdAt: createdAt()\n },\n (t) => ({\n idxSessionCreated: index('idx_chat_msg_session_created').on(t.sessionId, t.createdAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// Inferred row types — distinct from the domain types in ports/types.ts.\n// Adapters map these row shapes (datetime as string) to the domain shapes\n// (Date) at the boundary.\n// ---------------------------------------------------------------------------\n\nexport type ChatSessionRow = typeof chatSessions.$inferSelect;\nexport type ChatMessageRow = typeof chatMessages.$inferSelect;\nexport type AiSettingsRow = typeof aiSettings.$inferSelect;\n","/**\n * Ports — the contract between this package and any host that consumes it.\n *\n * Every project-specific concern (auth, scope, persistence, tools, credentials)\n * crosses the boundary as a typed port the host implements and passes into\n * `configureAiChat({...})`. The package itself has zero knowledge of the\n * host's database, RBAC model, or environment.\n *\n * Domain types (ChatSession, ChatMessage, AiSettings, PresentPayload, Block)\n * are pure TS — no ORM coupling. The Drizzle and Prisma adapters each map\n * their respective row types into these shapes so callers see one contract\n * regardless of the chosen ORM.\n */\nimport type { GoogleAuth } from 'google-auth-library';\nimport type { ToolDefinition, ToolContext, SystemBlock } from '../tools/types';\n\n// ---------------------------------------------------------------------------\n// Domain types — used by every port\n// ---------------------------------------------------------------------------\n\nexport type ChatSession = {\n id: number;\n userId: number;\n title: string;\n createdAt: Date;\n updatedAt: Date;\n};\n\nexport type ChatMessageRole = 'user' | 'assistant';\n\n/**\n * One message turn. For 'user' rows, `question` carries the raw question text.\n * For 'assistant' rows, `blocks` holds the structured PresentPayload and\n * `prose` holds any paragraph_brief prose collected from the streamed\n * narrator. `errorJson` is set when an assistant turn failed mid-stream.\n */\nexport type ChatMessage = {\n id: number;\n sessionId: number;\n role: ChatMessageRole;\n question: string | null;\n blocks: unknown | null;\n prose: unknown | null;\n errorJson: unknown | null;\n createdAt: Date;\n};\n\n/**\n * Singleton row controlling global AI runtime settings. All open-string\n * fields are validated at runtime against the registries the host configures\n * — they are not enums in the package's types so consumers can register\n * additional providers / interfaces without a schema change.\n *\n * `maxOutputTokens` caps both the agent loop's per-turn output AND each\n * narrator's prose pass. Reasoning models (e.g. `xai/grok-4.1-fast-reasoning`)\n * charge internal thinking against this budget — set it generously when\n * those are in use.\n *\n * `rolePrompt` is the persona the assistant adopts. When non-null, it\n * takes precedence over the host's static `rolePrompt` configureAiChat\n * option, so admins can edit live in the settings UI.\n */\nexport type AiSettings = {\n toolProvider: string;\n gcpLocation: string;\n chatInterface: string;\n maxOutputTokens: number;\n rolePrompt: string | null;\n updatedAt: Date | null;\n updatedByUserId: number | null;\n};\n\n/**\n * Default cap when nothing is persisted. Conservative so non-reasoning\n * models don't pay for headroom they don't need; admins bump it for\n * reasoning models via the settings UI.\n */\nexport const DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;\n\n// ---------------------------------------------------------------------------\n// AuthPort — the host tells us who's calling\n// ---------------------------------------------------------------------------\n\n/**\n * The host's resolved Scope (org/tenant/RBAC shape) is opaque to the package.\n * `S` is whatever the host wants — typically a discriminated union with at\n * least `userId: number` plus role/tenancy fields.\n */\nexport type AuthOk<S> = { ok: true; scope: S; userId: number };\nexport type AuthFail = { ok: false; response: Response };\nexport type AuthResult<S> = AuthOk<S> | AuthFail;\n\nexport type AuthPort<S = unknown> = {\n /**\n * Resolve the calling user from the request. Returning `{ ok: false }`\n * lets the route hand back the prepared 401/403 response untouched.\n */\n requireAuth(req: Request): Promise<AuthResult<S>>;\n /**\n * Predicate the admin-settings route uses to gate writes. Hosts that\n * don't model admins can return `() => true` if they trust their own\n * routing layer.\n */\n isSuperAdmin(scope: S): boolean;\n};\n\n// ---------------------------------------------------------------------------\n// ScopePort — human-readable scope description fed into the system prompt\n// ---------------------------------------------------------------------------\n\nexport type ScopePort<S = unknown> = {\n /** Short label rendered in the chat UI (e.g. \"Ghana\", \"All countries\"). */\n resolveScopeLabel(scope: S): Promise<string>;\n /** Longer narrative the system prompt uses to describe the data slice. */\n buildScopeSummary(scope: S): Promise<string>;\n};\n\n// ---------------------------------------------------------------------------\n// ToolsPort — host supplies its own tool registry + system prompts\n// ---------------------------------------------------------------------------\n\nexport type ToolsPort = {\n /**\n * Map of tool name → definition. Keys must match `definition.schema.name`.\n * Must include the terminal `present` tool — the agent loop refuses to\n * end a turn without it.\n */\n tools: Record<string, ToolDefinition>;\n /**\n * Build the ordered list of system blocks for one agent run. The host\n * decides how much of the prompt is project-specific (schema doc,\n * semantic-layer doc, scope summary, etc.). Blocks marked `cached: true`\n * become Anthropic ephemeral cache markers and Vertex prefix-cache hints.\n */\n buildSystemBlocks(ctx: ToolContext): Promise<SystemBlock[]>;\n};\n\n// ---------------------------------------------------------------------------\n// VertexPort — credentials are host-supplied; the package never reads env\n// ---------------------------------------------------------------------------\n\nexport type VertexPort = {\n projectId: string;\n /** e.g. 'us-east5' or 'global' — used unless `aiSettings.gcpLocation` overrides per-request. */\n defaultLocation: string;\n /**\n * A constructed `GoogleAuth` instance. Any auth scheme that yields one\n * works (ADC, Workload Identity Federation, split-key env vars,\n * Secret Manager, etc.). The package never reads `process.env.GCP_*`.\n */\n auth: GoogleAuth;\n /** Vertex model IDs pinned by the host (orgs pin their own versions). */\n modelIds: { claude: string; gemini: string };\n};\n\n// ---------------------------------------------------------------------------\n// LoggerPort — optional structured logging\n// ---------------------------------------------------------------------------\n\nexport type LoggerPort = {\n debug(...args: unknown[]): void;\n info(...args: unknown[]): void;\n warn(...args: unknown[]): void;\n error(...args: unknown[]): void;\n};\n\n// ---------------------------------------------------------------------------\n// PersistencePort — domain-shaped queries the package needs\n// ---------------------------------------------------------------------------\n\nexport type CreateSessionInput = {\n userId: number;\n title: string;\n};\n\nexport type AppendMessageInput = {\n sessionId: number;\n role: ChatMessageRole;\n question?: string | null;\n blocks?: unknown | null;\n prose?: unknown | null;\n errorJson?: unknown | null;\n};\n\nexport type ListSessionsOpts = {\n limit?: number;\n};\n\nexport type AiSettingsPatch = {\n toolProvider?: string;\n gcpLocation?: string;\n chatInterface?: string;\n maxOutputTokens?: number;\n /** Pass `null` to clear back to the host's static fallback. */\n rolePrompt?: string | null;\n};\n\n/**\n * The whole reason this package is ORM-agnostic. Implemented by:\n * - createDrizzlePersistence (src/adapters/drizzle/)\n * - createPrismaPersistence (src/adapters/prisma/)\n * - createMemoryPersistence (src/adapters/memory/, internal test use)\n *\n * Routes, agent loop, and UI never touch ORMs directly — they only call\n * methods on this port. A parameterized contract test runs the same\n * assertions against all three adapters to keep them in sync.\n */\nexport type PersistencePort = {\n // Sessions ---------------------------------------------------------------\n createSession(input: CreateSessionInput): Promise<ChatSession>;\n /** Returns null when the session doesn't exist OR doesn't belong to this user. */\n getSession(id: number, userId: number): Promise<ChatSession | null>;\n listSessionsForUser(userId: number, opts?: ListSessionsOpts): Promise<ChatSession[]>;\n /** No-op when session doesn't exist or doesn't belong to user — caller asserted authorisation. */\n updateSession(id: number, userId: number, patch: { title?: string }): Promise<void>;\n deleteSession(id: number, userId: number): Promise<void>;\n\n // Messages ---------------------------------------------------------------\n appendMessage(input: AppendMessageInput): Promise<ChatMessage>;\n listMessagesForSession(sessionId: number, userId: number): Promise<ChatMessage[]>;\n\n // AI settings (singleton row) -------------------------------------------\n /** Returns the singleton row, applying defaults if it's never been written. */\n getAiSettings(): Promise<AiSettings>;\n /**\n * Upsert. Validates `toolProvider` and `chatInterface` against the runtime\n * registries before writing — invalid values throw before SQL is issued.\n */\n updateAiSettings(patch: AiSettingsPatch, byUserId: number): Promise<AiSettings>;\n};\n","/**\n * Drizzle MySQL implementation of `PersistencePort`.\n *\n * The package never imports a concrete drizzle client — the host passes a\n * `MySql2Database` (or any compatible drizzle MySQL handle) and we use it\n * to issue the queries this port describes.\n *\n * Boundary mapping: the drizzle MySQL `datetime` columns are declared with\n * `mode: 'string'` to match the host's existing schema, so row reads return\n * ISO strings. The PersistencePort domain types use `Date`, so every row\n * crossing the boundary is converted via `new Date(row.createdAt)`.\n *\n * Per-user safety: `getSession`, `updateSession`, `deleteSession`, and\n * `listMessagesForSession` all join `userId` into the WHERE clause — the\n * port's contract is that no caller should be able to read or mutate\n * another user's data even if they've forged a session id.\n */\nimport { and, desc, eq } from 'drizzle-orm';\nimport type { MySql2Database } from 'drizzle-orm/mysql2';\n\nimport {\n DEFAULT_AI_MAX_OUTPUT_TOKENS,\n type AiSettings,\n type AiSettingsPatch,\n type AppendMessageInput,\n type ChatMessage,\n type ChatMessageRole,\n type ChatSession,\n type CreateSessionInput,\n type ListSessionsOpts,\n type PersistencePort\n} from '../../server/ports/types';\n\nimport {\n aiSettings,\n chatMessages,\n chatSessions,\n type AiSettingsRow,\n type ChatMessageRow,\n type ChatSessionRow\n} from './tables';\n\n// ---------------------------------------------------------------------------\n// Row -> domain mappers\n// ---------------------------------------------------------------------------\n\nfunction mapSession(row: ChatSessionRow): ChatSession {\n return {\n id: row.id,\n userId: row.userId,\n title: row.title,\n createdAt: new Date(row.createdAt),\n updatedAt: new Date(row.updatedAt)\n };\n}\n\nfunction mapMessage(row: ChatMessageRow): ChatMessage {\n return {\n id: row.id,\n sessionId: row.sessionId,\n role: row.role as ChatMessageRole,\n question: row.question,\n blocks: row.blocks,\n prose: row.prose,\n errorJson: row.errorJson,\n createdAt: new Date(row.createdAt)\n };\n}\n\nfunction mapSettings(row: AiSettingsRow): AiSettings {\n return {\n toolProvider: row.toolProvider,\n gcpLocation: row.gcpLocation,\n chatInterface: row.chatInterface,\n maxOutputTokens: row.maxOutputTokens,\n rolePrompt: row.rolePrompt,\n updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,\n updatedByUserId: row.updatedByUserId\n };\n}\n\n// ---------------------------------------------------------------------------\n// Factory\n// ---------------------------------------------------------------------------\n\nexport function createDrizzlePersistence(\n db: MySql2Database<any>\n): PersistencePort {\n return {\n // ---------------------------------------------------------------------\n // Sessions\n // ---------------------------------------------------------------------\n\n async createSession(input: CreateSessionInput): Promise<ChatSession> {\n const inserted = await db\n .insert(chatSessions)\n .values({\n userId: input.userId,\n title: input.title\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('createSession: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`createSession: row ${id} not found after insert`);\n }\n return mapSession(row);\n },\n\n async getSession(id: number, userId: number): Promise<ChatSession | null> {\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n return row ? mapSession(row) : null;\n },\n\n async listSessionsForUser(\n userId: number,\n opts?: ListSessionsOpts\n ): Promise<ChatSession[]> {\n const limit = opts?.limit ?? 50;\n const rows = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.userId, userId))\n .orderBy(desc(chatSessions.updatedAt))\n .limit(limit);\n\n return rows.map(mapSession);\n },\n\n async updateSession(\n id: number,\n userId: number,\n patch: { title?: string }\n ): Promise<void> {\n if (patch.title === undefined) return;\n\n await db\n .update(chatSessions)\n .set({ title: patch.title })\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n async deleteSession(id: number, userId: number): Promise<void> {\n // Delete messages first (no ON DELETE CASCADE assumed at table level).\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n if (owned.length === 0) return;\n\n await db.delete(chatMessages).where(eq(chatMessages.sessionId, id));\n await db\n .delete(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n // ---------------------------------------------------------------------\n // Messages\n // ---------------------------------------------------------------------\n\n async appendMessage(input: AppendMessageInput): Promise<ChatMessage> {\n const inserted = await db\n .insert(chatMessages)\n .values({\n sessionId: input.sessionId,\n role: input.role,\n question: input.question ?? null,\n blocks: input.blocks ?? null,\n prose: input.prose ?? null,\n errorJson: input.errorJson ?? null\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('appendMessage: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`appendMessage: row ${id} not found after insert`);\n }\n return mapMessage(row);\n },\n\n async listMessagesForSession(\n sessionId: number,\n userId: number\n ): Promise<ChatMessage[]> {\n // Verify ownership before returning rows — never trust the caller to\n // have filtered already.\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(\n and(eq(chatSessions.id, sessionId), eq(chatSessions.userId, userId))\n )\n .limit(1);\n\n if (owned.length === 0) return [];\n\n const rows = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.sessionId, sessionId))\n .orderBy(chatMessages.createdAt, chatMessages.id);\n\n return rows.map(mapMessage);\n },\n\n // ---------------------------------------------------------------------\n // AI settings (singleton row)\n // ---------------------------------------------------------------------\n\n async getAiSettings(): Promise<AiSettings> {\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n // Synthesize defaults rather than failing — the row is created on\n // first write via INSERT … ON DUPLICATE KEY UPDATE.\n return {\n toolProvider: 'claude',\n gcpLocation: 'us-east5',\n chatInterface: 'custom',\n maxOutputTokens: DEFAULT_AI_MAX_OUTPUT_TOKENS,\n rolePrompt: null,\n updatedAt: null,\n updatedByUserId: null\n };\n }\n\n return mapSettings(row);\n },\n\n async updateAiSettings(\n patch: AiSettingsPatch,\n byUserId: number\n ): Promise<AiSettings> {\n // Build the values object with the singleton id and any defaults the\n // INSERT branch needs. ON DUPLICATE KEY UPDATE only touches keys in\n // the patch (plus updated_at / updated_by_user_id audit columns).\n const insertValues = {\n id: 1 as const,\n toolProvider: patch.toolProvider ?? 'claude',\n gcpLocation: patch.gcpLocation ?? 'us-east5',\n chatInterface: patch.chatInterface ?? 'custom',\n maxOutputTokens:\n patch.maxOutputTokens ?? DEFAULT_AI_MAX_OUTPUT_TOKENS,\n rolePrompt: patch.rolePrompt ?? null,\n updatedByUserId: byUserId\n };\n\n const updateSet: Record<string, unknown> = {\n updatedByUserId: byUserId\n };\n if (patch.toolProvider !== undefined) {\n updateSet.toolProvider = patch.toolProvider;\n }\n if (patch.gcpLocation !== undefined) {\n updateSet.gcpLocation = patch.gcpLocation;\n }\n if (patch.chatInterface !== undefined) {\n updateSet.chatInterface = patch.chatInterface;\n }\n if (patch.maxOutputTokens !== undefined) {\n updateSet.maxOutputTokens = patch.maxOutputTokens;\n }\n if (patch.rolePrompt !== undefined) {\n updateSet.rolePrompt = patch.rolePrompt;\n }\n\n await db\n .insert(aiSettings)\n .values(insertValues)\n .onDuplicateKeyUpdate({ set: updateSet });\n\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n throw new Error('updateAiSettings: singleton row missing after upsert');\n }\n return mapSettings(row);\n }\n };\n}\n"]}
|
package/dist/drizzle/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
2
2
|
import { MySql2Database } from 'drizzle-orm/mysql2';
|
|
3
|
-
import { c as PersistencePort } from '../types-
|
|
3
|
+
import { c as PersistencePort } from '../types-CQntnyDJ.cjs';
|
|
4
4
|
import 'google-auth-library';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -81,6 +81,40 @@ declare const aiSettings: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
81
81
|
identity: undefined;
|
|
82
82
|
generated: undefined;
|
|
83
83
|
}, object>;
|
|
84
|
+
maxOutputTokens: drizzle_orm_mysql_core.MySqlColumn<{
|
|
85
|
+
name: "max_output_tokens";
|
|
86
|
+
tableName: "ai_settings";
|
|
87
|
+
dataType: "number";
|
|
88
|
+
columnType: "MySqlInt";
|
|
89
|
+
data: number;
|
|
90
|
+
driverParam: string | number;
|
|
91
|
+
notNull: true;
|
|
92
|
+
hasDefault: true;
|
|
93
|
+
isPrimaryKey: false;
|
|
94
|
+
isAutoincrement: false;
|
|
95
|
+
hasRuntimeDefault: false;
|
|
96
|
+
enumValues: undefined;
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
identity: undefined;
|
|
99
|
+
generated: undefined;
|
|
100
|
+
}, object>;
|
|
101
|
+
rolePrompt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
102
|
+
name: "role_prompt";
|
|
103
|
+
tableName: "ai_settings";
|
|
104
|
+
dataType: "string";
|
|
105
|
+
columnType: "MySqlText";
|
|
106
|
+
data: string;
|
|
107
|
+
driverParam: string;
|
|
108
|
+
notNull: false;
|
|
109
|
+
hasDefault: false;
|
|
110
|
+
isPrimaryKey: false;
|
|
111
|
+
isAutoincrement: false;
|
|
112
|
+
hasRuntimeDefault: false;
|
|
113
|
+
enumValues: [string, ...string[]];
|
|
114
|
+
baseColumn: never;
|
|
115
|
+
identity: undefined;
|
|
116
|
+
generated: undefined;
|
|
117
|
+
}, object>;
|
|
84
118
|
updatedAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
85
119
|
name: "updated_at";
|
|
86
120
|
tableName: "ai_settings";
|
package/dist/drizzle/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
2
2
|
import { MySql2Database } from 'drizzle-orm/mysql2';
|
|
3
|
-
import { c as PersistencePort } from '../types-
|
|
3
|
+
import { c as PersistencePort } from '../types-CQntnyDJ.js';
|
|
4
4
|
import 'google-auth-library';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -81,6 +81,40 @@ declare const aiSettings: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
81
81
|
identity: undefined;
|
|
82
82
|
generated: undefined;
|
|
83
83
|
}, object>;
|
|
84
|
+
maxOutputTokens: drizzle_orm_mysql_core.MySqlColumn<{
|
|
85
|
+
name: "max_output_tokens";
|
|
86
|
+
tableName: "ai_settings";
|
|
87
|
+
dataType: "number";
|
|
88
|
+
columnType: "MySqlInt";
|
|
89
|
+
data: number;
|
|
90
|
+
driverParam: string | number;
|
|
91
|
+
notNull: true;
|
|
92
|
+
hasDefault: true;
|
|
93
|
+
isPrimaryKey: false;
|
|
94
|
+
isAutoincrement: false;
|
|
95
|
+
hasRuntimeDefault: false;
|
|
96
|
+
enumValues: undefined;
|
|
97
|
+
baseColumn: never;
|
|
98
|
+
identity: undefined;
|
|
99
|
+
generated: undefined;
|
|
100
|
+
}, object>;
|
|
101
|
+
rolePrompt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
102
|
+
name: "role_prompt";
|
|
103
|
+
tableName: "ai_settings";
|
|
104
|
+
dataType: "string";
|
|
105
|
+
columnType: "MySqlText";
|
|
106
|
+
data: string;
|
|
107
|
+
driverParam: string;
|
|
108
|
+
notNull: false;
|
|
109
|
+
hasDefault: false;
|
|
110
|
+
isPrimaryKey: false;
|
|
111
|
+
isAutoincrement: false;
|
|
112
|
+
hasRuntimeDefault: false;
|
|
113
|
+
enumValues: [string, ...string[]];
|
|
114
|
+
baseColumn: never;
|
|
115
|
+
identity: undefined;
|
|
116
|
+
generated: undefined;
|
|
117
|
+
}, object>;
|
|
84
118
|
updatedAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
85
119
|
name: "updated_at";
|
|
86
120
|
tableName: "ai_settings";
|
package/dist/drizzle/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { sql, eq, and, desc } from 'drizzle-orm';
|
|
2
|
-
import { mysqlTable, datetime, varchar, tinyint, index, json,
|
|
2
|
+
import { mysqlTable, datetime, text, int, varchar, tinyint, index, json, mysqlEnum, bigint } from 'drizzle-orm/mysql-core';
|
|
3
3
|
|
|
4
4
|
// src/adapters/drizzle/tables.ts
|
|
5
5
|
var bigintPk = () => bigint("id", { mode: "number", unsigned: true }).notNull().primaryKey().autoincrement();
|
|
@@ -11,6 +11,13 @@ var aiSettings = mysqlTable("ai_settings", {
|
|
|
11
11
|
toolProvider: varchar("tool_provider", { length: 32 }).notNull().default("claude"),
|
|
12
12
|
gcpLocation: varchar("gcp_location", { length: 32 }).notNull().default("us-east5"),
|
|
13
13
|
chatInterface: varchar("chat_interface", { length: 32 }).notNull().default("custom"),
|
|
14
|
+
// Caps the per-turn output budget for both the agent loop AND the prose
|
|
15
|
+
// narrator. Reasoning models charge internal thinking against this; bump
|
|
16
|
+
// it well past 4096 when one is in use.
|
|
17
|
+
maxOutputTokens: int("max_output_tokens").notNull().default(4096),
|
|
18
|
+
// Optional admin-editable role/persona. NULL means the host's static
|
|
19
|
+
// `rolePrompt` (passed to configureAiChat) is used as the fallback.
|
|
20
|
+
rolePrompt: text("role_prompt"),
|
|
14
21
|
updatedAt: datetime("updated_at", { mode: "string" }).notNull().default(sql`CURRENT_TIMESTAMP`),
|
|
15
22
|
updatedByUserId: bigintFkNullable("updated_by_user_id")
|
|
16
23
|
});
|
|
@@ -44,6 +51,11 @@ var chatMessages = mysqlTable(
|
|
|
44
51
|
idxSessionCreated: index("idx_chat_msg_session_created").on(t.sessionId, t.createdAt)
|
|
45
52
|
})
|
|
46
53
|
);
|
|
54
|
+
|
|
55
|
+
// src/server/ports/types.ts
|
|
56
|
+
var DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;
|
|
57
|
+
|
|
58
|
+
// src/adapters/drizzle/adapter.ts
|
|
47
59
|
function mapSession(row) {
|
|
48
60
|
return {
|
|
49
61
|
id: row.id,
|
|
@@ -70,6 +82,8 @@ function mapSettings(row) {
|
|
|
70
82
|
toolProvider: row.toolProvider,
|
|
71
83
|
gcpLocation: row.gcpLocation,
|
|
72
84
|
chatInterface: row.chatInterface,
|
|
85
|
+
maxOutputTokens: row.maxOutputTokens,
|
|
86
|
+
rolePrompt: row.rolePrompt,
|
|
73
87
|
updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,
|
|
74
88
|
updatedByUserId: row.updatedByUserId
|
|
75
89
|
};
|
|
@@ -153,6 +167,8 @@ function createDrizzlePersistence(db) {
|
|
|
153
167
|
toolProvider: "claude",
|
|
154
168
|
gcpLocation: "us-east5",
|
|
155
169
|
chatInterface: "custom",
|
|
170
|
+
maxOutputTokens: DEFAULT_AI_MAX_OUTPUT_TOKENS,
|
|
171
|
+
rolePrompt: null,
|
|
156
172
|
updatedAt: null,
|
|
157
173
|
updatedByUserId: null
|
|
158
174
|
};
|
|
@@ -165,6 +181,8 @@ function createDrizzlePersistence(db) {
|
|
|
165
181
|
toolProvider: patch.toolProvider ?? "claude",
|
|
166
182
|
gcpLocation: patch.gcpLocation ?? "us-east5",
|
|
167
183
|
chatInterface: patch.chatInterface ?? "custom",
|
|
184
|
+
maxOutputTokens: patch.maxOutputTokens ?? DEFAULT_AI_MAX_OUTPUT_TOKENS,
|
|
185
|
+
rolePrompt: patch.rolePrompt ?? null,
|
|
168
186
|
updatedByUserId: byUserId
|
|
169
187
|
};
|
|
170
188
|
const updateSet = {
|
|
@@ -179,6 +197,12 @@ function createDrizzlePersistence(db) {
|
|
|
179
197
|
if (patch.chatInterface !== void 0) {
|
|
180
198
|
updateSet.chatInterface = patch.chatInterface;
|
|
181
199
|
}
|
|
200
|
+
if (patch.maxOutputTokens !== void 0) {
|
|
201
|
+
updateSet.maxOutputTokens = patch.maxOutputTokens;
|
|
202
|
+
}
|
|
203
|
+
if (patch.rolePrompt !== void 0) {
|
|
204
|
+
updateSet.rolePrompt = patch.rolePrompt;
|
|
205
|
+
}
|
|
182
206
|
await db.insert(aiSettings).values(insertValues).onDuplicateKeyUpdate({ set: updateSet });
|
|
183
207
|
const [row] = await db.select().from(aiSettings).where(eq(aiSettings.id, 1)).limit(1);
|
|
184
208
|
if (!row) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/drizzle/tables.ts","../../src/adapters/drizzle/adapter.ts"],"names":[],"mappings":";;;;AAuCA,IAAM,QAAA,GAAW,MACf,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAC5C,OAAA,EAAQ,CACR,UAAA,GACA,aAAA,EAAc;AAGnB,IAAM,QAAA,GAAW,CAAC,IAAA,KAChB,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAAE,OAAA,EAAQ;AAG3D,IAAM,gBAAA,GAAmB,CAAC,IAAA,KACxB,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA;AAGjD,IAAM,SAAA,GAAY,MAChB,QAAA,CAAS,YAAA,EAAc,EAAE,IAAA,EAAM,QAAA,EAAU,CAAA,CACtC,OAAA,EAAQ,CACR,OAAA,CAAQ,GAAA,CAAA,iBAAA,CAAsB,CAAA;AAW5B,IAAM,UAAA,GAAa,WAAW,aAAA,EAAe;AAAA,EAClD,EAAA,EAAI,QAAQ,IAAI,CAAA,CAAE,SAAQ,CAAE,UAAA,EAAW,CAAE,OAAA,CAAQ,CAAC,CAAA;AAAA,EAClD,YAAA,EAAc,OAAA,CAAQ,eAAA,EAAiB,EAAE,MAAA,EAAQ,EAAA,EAAI,CAAA,CAClD,OAAA,EAAQ,CACR,OAAA,CAAQ,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;AAAA,MACxE;AACA,MAAA,OAAO,YAAY,GAAG,CAAA;AAAA,IACxB;AAAA,GACF;AACF","file":"index.js","sourcesContent":["/**\n * Canonical drizzle MySQL table definitions for `@firstlovecenter/ai-chat`.\n *\n * These three tables (`chat_sessions`, `chat_messages`, `ai_settings`) are\n * the data shape the package needs to persist its conversations and global\n * config. The host re-exports them from its own schema so that an existing\n * deployment keeps the same column names, lengths, FKs, and indexes — making\n * the host's data fully portable into and out of the package.\n *\n * Deviations from the host's prior shape are deliberate:\n * - `tool_provider` and `gcp_location` are plain VARCHAR (not enums) so a\n * consumer can register additional providers / regions without a schema\n * migration. Validation against the runtime registries happens at a\n * higher layer.\n * - `chat_interface` is a new VARCHAR column controlling which chat UI\n * (custom vs. vercel-ai) renders globally; default 'custom'.\n *\n * The small column helpers from the host (`bigintPk`, `bigintFk`,\n * `bigintFkNullable`, `createdAt`) are re-defined inline here — this package\n * never imports from the host repo.\n */\nimport { sql } from 'drizzle-orm';\nimport {\n bigint,\n datetime,\n index,\n json,\n mysqlEnum,\n mysqlTable,\n text,\n tinyint,\n varchar\n} from 'drizzle-orm/mysql-core';\n\n// ---------------------------------------------------------------------------\n// Inline column helpers (mirrors host `src/db/columns.ts` shapes)\n// ---------------------------------------------------------------------------\n\n/** BIGINT UNSIGNED PK with auto-increment, returned as `number`. */\nconst bigintPk = () =>\n bigint('id', { mode: 'number', unsigned: true })\n .notNull()\n .primaryKey()\n .autoincrement();\n\n/** NOT NULL BIGINT UNSIGNED FK column, returned as `number`. */\nconst bigintFk = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true }).notNull();\n\n/** Nullable BIGINT UNSIGNED FK column, returned as `number | null`. */\nconst bigintFkNullable = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true });\n\n/** `created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP`, mode 'string'. */\nconst createdAt = () =>\n datetime('created_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`);\n\n// ---------------------------------------------------------------------------\n// ai_settings (singleton — always one row at id=1)\n// ---------------------------------------------------------------------------\n\n/**\n * Global AI configuration. Singleton row enforced by `id=1` default + PK.\n * `tool_provider`, `gcp_location`, and `chat_interface` are open VARCHARs;\n * the runtime layer validates against the host's registries before write.\n */\nexport const aiSettings = mysqlTable('ai_settings', {\n id: tinyint('id').notNull().primaryKey().default(1),\n toolProvider: varchar('tool_provider', { length: 32 })\n .notNull()\n .default('claude'),\n gcpLocation: varchar('gcp_location', { length: 32 })\n .notNull()\n .default('us-east5'),\n chatInterface: varchar('chat_interface', { length: 32 })\n .notNull()\n .default('custom'),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`),\n updatedByUserId: bigintFkNullable('updated_by_user_id')\n});\n\n// ---------------------------------------------------------------------------\n// chat_sessions (one per conversation)\n// ---------------------------------------------------------------------------\n\nexport const chatSessions = mysqlTable(\n 'chat_sessions',\n {\n id: bigintPk(),\n userId: bigintFk('user_id'),\n title: varchar('title', { length: 200 }).notNull(),\n createdAt: createdAt(),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`)\n },\n (t) => ({\n idxUserUpdated: index('idx_chat_session_user_updated').on(t.userId, t.updatedAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// chat_messages (one row per turn in a chat_session)\n// ---------------------------------------------------------------------------\n\nexport const chatMessageRoleEnum = ['user', 'assistant'] as const;\n\nexport const chatMessages = mysqlTable(\n 'chat_messages',\n {\n id: bigintPk(),\n sessionId: bigintFk('session_id'),\n role: mysqlEnum('role', chatMessageRoleEnum).notNull(),\n question: text('question'),\n blocks: json('blocks'),\n prose: json('prose'),\n errorJson: json('error_json'),\n createdAt: createdAt()\n },\n (t) => ({\n idxSessionCreated: index('idx_chat_msg_session_created').on(t.sessionId, t.createdAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// Inferred row types — distinct from the domain types in ports/types.ts.\n// Adapters map these row shapes (datetime as string) to the domain shapes\n// (Date) at the boundary.\n// ---------------------------------------------------------------------------\n\nexport type ChatSessionRow = typeof chatSessions.$inferSelect;\nexport type ChatMessageRow = typeof chatMessages.$inferSelect;\nexport type AiSettingsRow = typeof aiSettings.$inferSelect;\n","/**\n * Drizzle MySQL implementation of `PersistencePort`.\n *\n * The package never imports a concrete drizzle client — the host passes a\n * `MySql2Database` (or any compatible drizzle MySQL handle) and we use it\n * to issue the queries this port describes.\n *\n * Boundary mapping: the drizzle MySQL `datetime` columns are declared with\n * `mode: 'string'` to match the host's existing schema, so row reads return\n * ISO strings. The PersistencePort domain types use `Date`, so every row\n * crossing the boundary is converted via `new Date(row.createdAt)`.\n *\n * Per-user safety: `getSession`, `updateSession`, `deleteSession`, and\n * `listMessagesForSession` all join `userId` into the WHERE clause — the\n * port's contract is that no caller should be able to read or mutate\n * another user's data even if they've forged a session id.\n */\nimport { and, desc, eq } from 'drizzle-orm';\nimport type { MySql2Database } from 'drizzle-orm/mysql2';\n\nimport type {\n AiSettings,\n AiSettingsPatch,\n AppendMessageInput,\n ChatMessage,\n ChatMessageRole,\n ChatSession,\n CreateSessionInput,\n ListSessionsOpts,\n PersistencePort\n} from '../../server/ports/types';\n\nimport {\n aiSettings,\n chatMessages,\n chatSessions,\n type AiSettingsRow,\n type ChatMessageRow,\n type ChatSessionRow\n} from './tables';\n\n// ---------------------------------------------------------------------------\n// Row -> domain mappers\n// ---------------------------------------------------------------------------\n\nfunction mapSession(row: ChatSessionRow): ChatSession {\n return {\n id: row.id,\n userId: row.userId,\n title: row.title,\n createdAt: new Date(row.createdAt),\n updatedAt: new Date(row.updatedAt)\n };\n}\n\nfunction mapMessage(row: ChatMessageRow): ChatMessage {\n return {\n id: row.id,\n sessionId: row.sessionId,\n role: row.role as ChatMessageRole,\n question: row.question,\n blocks: row.blocks,\n prose: row.prose,\n errorJson: row.errorJson,\n createdAt: new Date(row.createdAt)\n };\n}\n\nfunction mapSettings(row: AiSettingsRow): AiSettings {\n return {\n toolProvider: row.toolProvider,\n gcpLocation: row.gcpLocation,\n chatInterface: row.chatInterface,\n updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,\n updatedByUserId: row.updatedByUserId\n };\n}\n\n// ---------------------------------------------------------------------------\n// Factory\n// ---------------------------------------------------------------------------\n\nexport function createDrizzlePersistence(\n db: MySql2Database<any>\n): PersistencePort {\n return {\n // ---------------------------------------------------------------------\n // Sessions\n // ---------------------------------------------------------------------\n\n async createSession(input: CreateSessionInput): Promise<ChatSession> {\n const inserted = await db\n .insert(chatSessions)\n .values({\n userId: input.userId,\n title: input.title\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('createSession: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`createSession: row ${id} not found after insert`);\n }\n return mapSession(row);\n },\n\n async getSession(id: number, userId: number): Promise<ChatSession | null> {\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n return row ? mapSession(row) : null;\n },\n\n async listSessionsForUser(\n userId: number,\n opts?: ListSessionsOpts\n ): Promise<ChatSession[]> {\n const limit = opts?.limit ?? 50;\n const rows = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.userId, userId))\n .orderBy(desc(chatSessions.updatedAt))\n .limit(limit);\n\n return rows.map(mapSession);\n },\n\n async updateSession(\n id: number,\n userId: number,\n patch: { title?: string }\n ): Promise<void> {\n if (patch.title === undefined) return;\n\n await db\n .update(chatSessions)\n .set({ title: patch.title })\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n async deleteSession(id: number, userId: number): Promise<void> {\n // Delete messages first (no ON DELETE CASCADE assumed at table level).\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n if (owned.length === 0) return;\n\n await db.delete(chatMessages).where(eq(chatMessages.sessionId, id));\n await db\n .delete(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n // ---------------------------------------------------------------------\n // Messages\n // ---------------------------------------------------------------------\n\n async appendMessage(input: AppendMessageInput): Promise<ChatMessage> {\n const inserted = await db\n .insert(chatMessages)\n .values({\n sessionId: input.sessionId,\n role: input.role,\n question: input.question ?? null,\n blocks: input.blocks ?? null,\n prose: input.prose ?? null,\n errorJson: input.errorJson ?? null\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('appendMessage: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`appendMessage: row ${id} not found after insert`);\n }\n return mapMessage(row);\n },\n\n async listMessagesForSession(\n sessionId: number,\n userId: number\n ): Promise<ChatMessage[]> {\n // Verify ownership before returning rows — never trust the caller to\n // have filtered already.\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(\n and(eq(chatSessions.id, sessionId), eq(chatSessions.userId, userId))\n )\n .limit(1);\n\n if (owned.length === 0) return [];\n\n const rows = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.sessionId, sessionId))\n .orderBy(chatMessages.createdAt, chatMessages.id);\n\n return rows.map(mapMessage);\n },\n\n // ---------------------------------------------------------------------\n // AI settings (singleton row)\n // ---------------------------------------------------------------------\n\n async getAiSettings(): Promise<AiSettings> {\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n // Synthesize defaults rather than failing — the row is created on\n // first write via INSERT … ON DUPLICATE KEY UPDATE.\n return {\n toolProvider: 'claude',\n gcpLocation: 'us-east5',\n chatInterface: 'custom',\n updatedAt: null,\n updatedByUserId: null\n };\n }\n\n return mapSettings(row);\n },\n\n async updateAiSettings(\n patch: AiSettingsPatch,\n byUserId: number\n ): Promise<AiSettings> {\n // Build the values object with the singleton id and any defaults the\n // INSERT branch needs. ON DUPLICATE KEY UPDATE only touches keys in\n // the patch (plus updated_at / updated_by_user_id audit columns).\n const insertValues = {\n id: 1 as const,\n toolProvider: patch.toolProvider ?? 'claude',\n gcpLocation: patch.gcpLocation ?? 'us-east5',\n chatInterface: patch.chatInterface ?? 'custom',\n updatedByUserId: byUserId\n };\n\n const updateSet: Record<string, unknown> = {\n updatedByUserId: byUserId\n };\n if (patch.toolProvider !== undefined) {\n updateSet.toolProvider = patch.toolProvider;\n }\n if (patch.gcpLocation !== undefined) {\n updateSet.gcpLocation = patch.gcpLocation;\n }\n if (patch.chatInterface !== undefined) {\n updateSet.chatInterface = patch.chatInterface;\n }\n\n await db\n .insert(aiSettings)\n .values(insertValues)\n .onDuplicateKeyUpdate({ set: updateSet });\n\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n throw new Error('updateAiSettings: singleton row missing after upsert');\n }\n return mapSettings(row);\n }\n };\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/drizzle/tables.ts","../../src/server/ports/types.ts","../../src/adapters/drizzle/adapter.ts"],"names":[],"mappings":";;;;AAwCA,IAAM,QAAA,GAAW,MACf,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAC5C,OAAA,EAAQ,CACR,UAAA,GACA,aAAA,EAAc;AAGnB,IAAM,QAAA,GAAW,CAAC,IAAA,KAChB,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAAE,OAAA,EAAQ;AAG3D,IAAM,gBAAA,GAAmB,CAAC,IAAA,KACxB,MAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,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enter/ai-chat`.\n *\n * These three tables (`chat_sessions`, `chat_messages`, `ai_settings`) are\n * the data shape the package needs to persist its conversations and global\n * config. The host re-exports them from its own schema so that an existing\n * deployment keeps the same column names, lengths, FKs, and indexes — making\n * the host's data fully portable into and out of the package.\n *\n * Deviations from the host's prior shape are deliberate:\n * - `tool_provider` and `gcp_location` are plain VARCHAR (not enums) so a\n * consumer can register additional providers / regions without a schema\n * migration. Validation against the runtime registries happens at a\n * higher layer.\n * - `chat_interface` is a new VARCHAR column controlling which chat UI\n * (custom vs. vercel-ai) renders globally; default 'custom'.\n *\n * The small column helpers from the host (`bigintPk`, `bigintFk`,\n * `bigintFkNullable`, `createdAt`) are re-defined inline here — this package\n * never imports from the host repo.\n */\nimport { sql } from 'drizzle-orm';\nimport {\n bigint,\n datetime,\n index,\n int,\n json,\n mysqlEnum,\n mysqlTable,\n text,\n tinyint,\n varchar\n} from 'drizzle-orm/mysql-core';\n\n// ---------------------------------------------------------------------------\n// Inline column helpers (mirrors host `src/db/columns.ts` shapes)\n// ---------------------------------------------------------------------------\n\n/** BIGINT UNSIGNED PK with auto-increment, returned as `number`. */\nconst bigintPk = () =>\n bigint('id', { mode: 'number', unsigned: true })\n .notNull()\n .primaryKey()\n .autoincrement();\n\n/** NOT NULL BIGINT UNSIGNED FK column, returned as `number`. */\nconst bigintFk = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true }).notNull();\n\n/** Nullable BIGINT UNSIGNED FK column, returned as `number | null`. */\nconst bigintFkNullable = (name: string) =>\n bigint(name, { mode: 'number', unsigned: true });\n\n/** `created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP`, mode 'string'. */\nconst createdAt = () =>\n datetime('created_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`);\n\n// ---------------------------------------------------------------------------\n// ai_settings (singleton — always one row at id=1)\n// ---------------------------------------------------------------------------\n\n/**\n * Global AI configuration. Singleton row enforced by `id=1` default + PK.\n * `tool_provider`, `gcp_location`, and `chat_interface` are open VARCHARs;\n * the runtime layer validates against the host's registries before write.\n */\nexport const aiSettings = mysqlTable('ai_settings', {\n id: tinyint('id').notNull().primaryKey().default(1),\n toolProvider: varchar('tool_provider', { length: 32 })\n .notNull()\n .default('claude'),\n gcpLocation: varchar('gcp_location', { length: 32 })\n .notNull()\n .default('us-east5'),\n chatInterface: varchar('chat_interface', { length: 32 })\n .notNull()\n .default('custom'),\n // Caps the per-turn output budget for both the agent loop AND the prose\n // narrator. Reasoning models charge internal thinking against this; bump\n // it well past 4096 when one is in use.\n maxOutputTokens: int('max_output_tokens').notNull().default(4096),\n // Optional admin-editable role/persona. NULL means the host's static\n // `rolePrompt` (passed to configureAiChat) is used as the fallback.\n rolePrompt: text('role_prompt'),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`),\n updatedByUserId: bigintFkNullable('updated_by_user_id')\n});\n\n// ---------------------------------------------------------------------------\n// chat_sessions (one per conversation)\n// ---------------------------------------------------------------------------\n\nexport const chatSessions = mysqlTable(\n 'chat_sessions',\n {\n id: bigintPk(),\n userId: bigintFk('user_id'),\n title: varchar('title', { length: 200 }).notNull(),\n createdAt: createdAt(),\n updatedAt: datetime('updated_at', { mode: 'string' })\n .notNull()\n .default(sql`CURRENT_TIMESTAMP`)\n },\n (t) => ({\n idxUserUpdated: index('idx_chat_session_user_updated').on(t.userId, t.updatedAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// chat_messages (one row per turn in a chat_session)\n// ---------------------------------------------------------------------------\n\nexport const chatMessageRoleEnum = ['user', 'assistant'] as const;\n\nexport const chatMessages = mysqlTable(\n 'chat_messages',\n {\n id: bigintPk(),\n sessionId: bigintFk('session_id'),\n role: mysqlEnum('role', chatMessageRoleEnum).notNull(),\n question: text('question'),\n blocks: json('blocks'),\n prose: json('prose'),\n errorJson: json('error_json'),\n createdAt: createdAt()\n },\n (t) => ({\n idxSessionCreated: index('idx_chat_msg_session_created').on(t.sessionId, t.createdAt)\n })\n);\n\n// ---------------------------------------------------------------------------\n// Inferred row types — distinct from the domain types in ports/types.ts.\n// Adapters map these row shapes (datetime as string) to the domain shapes\n// (Date) at the boundary.\n// ---------------------------------------------------------------------------\n\nexport type ChatSessionRow = typeof chatSessions.$inferSelect;\nexport type ChatMessageRow = typeof chatMessages.$inferSelect;\nexport type AiSettingsRow = typeof aiSettings.$inferSelect;\n","/**\n * Ports — the contract between this package and any host that consumes it.\n *\n * Every project-specific concern (auth, scope, persistence, tools, credentials)\n * crosses the boundary as a typed port the host implements and passes into\n * `configureAiChat({...})`. The package itself has zero knowledge of the\n * host's database, RBAC model, or environment.\n *\n * Domain types (ChatSession, ChatMessage, AiSettings, PresentPayload, Block)\n * are pure TS — no ORM coupling. The Drizzle and Prisma adapters each map\n * their respective row types into these shapes so callers see one contract\n * regardless of the chosen ORM.\n */\nimport type { GoogleAuth } from 'google-auth-library';\nimport type { ToolDefinition, ToolContext, SystemBlock } from '../tools/types';\n\n// ---------------------------------------------------------------------------\n// Domain types — used by every port\n// ---------------------------------------------------------------------------\n\nexport type ChatSession = {\n id: number;\n userId: number;\n title: string;\n createdAt: Date;\n updatedAt: Date;\n};\n\nexport type ChatMessageRole = 'user' | 'assistant';\n\n/**\n * One message turn. For 'user' rows, `question` carries the raw question text.\n * For 'assistant' rows, `blocks` holds the structured PresentPayload and\n * `prose` holds any paragraph_brief prose collected from the streamed\n * narrator. `errorJson` is set when an assistant turn failed mid-stream.\n */\nexport type ChatMessage = {\n id: number;\n sessionId: number;\n role: ChatMessageRole;\n question: string | null;\n blocks: unknown | null;\n prose: unknown | null;\n errorJson: unknown | null;\n createdAt: Date;\n};\n\n/**\n * Singleton row controlling global AI runtime settings. All open-string\n * fields are validated at runtime against the registries the host configures\n * — they are not enums in the package's types so consumers can register\n * additional providers / interfaces without a schema change.\n *\n * `maxOutputTokens` caps both the agent loop's per-turn output AND each\n * narrator's prose pass. Reasoning models (e.g. `xai/grok-4.1-fast-reasoning`)\n * charge internal thinking against this budget — set it generously when\n * those are in use.\n *\n * `rolePrompt` is the persona the assistant adopts. When non-null, it\n * takes precedence over the host's static `rolePrompt` configureAiChat\n * option, so admins can edit live in the settings UI.\n */\nexport type AiSettings = {\n toolProvider: string;\n gcpLocation: string;\n chatInterface: string;\n maxOutputTokens: number;\n rolePrompt: string | null;\n updatedAt: Date | null;\n updatedByUserId: number | null;\n};\n\n/**\n * Default cap when nothing is persisted. Conservative so non-reasoning\n * models don't pay for headroom they don't need; admins bump it for\n * reasoning models via the settings UI.\n */\nexport const DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;\n\n// ---------------------------------------------------------------------------\n// AuthPort — the host tells us who's calling\n// ---------------------------------------------------------------------------\n\n/**\n * The host's resolved Scope (org/tenant/RBAC shape) is opaque to the package.\n * `S` is whatever the host wants — typically a discriminated union with at\n * least `userId: number` plus role/tenancy fields.\n */\nexport type AuthOk<S> = { ok: true; scope: S; userId: number };\nexport type AuthFail = { ok: false; response: Response };\nexport type AuthResult<S> = AuthOk<S> | AuthFail;\n\nexport type AuthPort<S = unknown> = {\n /**\n * Resolve the calling user from the request. Returning `{ ok: false }`\n * lets the route hand back the prepared 401/403 response untouched.\n */\n requireAuth(req: Request): Promise<AuthResult<S>>;\n /**\n * Predicate the admin-settings route uses to gate writes. Hosts that\n * don't model admins can return `() => true` if they trust their own\n * routing layer.\n */\n isSuperAdmin(scope: S): boolean;\n};\n\n// ---------------------------------------------------------------------------\n// ScopePort — human-readable scope description fed into the system prompt\n// ---------------------------------------------------------------------------\n\nexport type ScopePort<S = unknown> = {\n /** Short label rendered in the chat UI (e.g. \"Ghana\", \"All countries\"). */\n resolveScopeLabel(scope: S): Promise<string>;\n /** Longer narrative the system prompt uses to describe the data slice. */\n buildScopeSummary(scope: S): Promise<string>;\n};\n\n// ---------------------------------------------------------------------------\n// ToolsPort — host supplies its own tool registry + system prompts\n// ---------------------------------------------------------------------------\n\nexport type ToolsPort = {\n /**\n * Map of tool name → definition. Keys must match `definition.schema.name`.\n * Must include the terminal `present` tool — the agent loop refuses to\n * end a turn without it.\n */\n tools: Record<string, ToolDefinition>;\n /**\n * Build the ordered list of system blocks for one agent run. The host\n * decides how much of the prompt is project-specific (schema doc,\n * semantic-layer doc, scope summary, etc.). Blocks marked `cached: true`\n * become Anthropic ephemeral cache markers and Vertex prefix-cache hints.\n */\n buildSystemBlocks(ctx: ToolContext): Promise<SystemBlock[]>;\n};\n\n// ---------------------------------------------------------------------------\n// VertexPort — credentials are host-supplied; the package never reads env\n// ---------------------------------------------------------------------------\n\nexport type VertexPort = {\n projectId: string;\n /** e.g. 'us-east5' or 'global' — used unless `aiSettings.gcpLocation` overrides per-request. */\n defaultLocation: string;\n /**\n * A constructed `GoogleAuth` instance. Any auth scheme that yields one\n * works (ADC, Workload Identity Federation, split-key env vars,\n * Secret Manager, etc.). The package never reads `process.env.GCP_*`.\n */\n auth: GoogleAuth;\n /** Vertex model IDs pinned by the host (orgs pin their own versions). */\n modelIds: { claude: string; gemini: string };\n};\n\n// ---------------------------------------------------------------------------\n// LoggerPort — optional structured logging\n// ---------------------------------------------------------------------------\n\nexport type LoggerPort = {\n debug(...args: unknown[]): void;\n info(...args: unknown[]): void;\n warn(...args: unknown[]): void;\n error(...args: unknown[]): void;\n};\n\n// ---------------------------------------------------------------------------\n// PersistencePort — domain-shaped queries the package needs\n// ---------------------------------------------------------------------------\n\nexport type CreateSessionInput = {\n userId: number;\n title: string;\n};\n\nexport type AppendMessageInput = {\n sessionId: number;\n role: ChatMessageRole;\n question?: string | null;\n blocks?: unknown | null;\n prose?: unknown | null;\n errorJson?: unknown | null;\n};\n\nexport type ListSessionsOpts = {\n limit?: number;\n};\n\nexport type AiSettingsPatch = {\n toolProvider?: string;\n gcpLocation?: string;\n chatInterface?: string;\n maxOutputTokens?: number;\n /** Pass `null` to clear back to the host's static fallback. */\n rolePrompt?: string | null;\n};\n\n/**\n * The whole reason this package is ORM-agnostic. Implemented by:\n * - createDrizzlePersistence (src/adapters/drizzle/)\n * - createPrismaPersistence (src/adapters/prisma/)\n * - createMemoryPersistence (src/adapters/memory/, internal test use)\n *\n * Routes, agent loop, and UI never touch ORMs directly — they only call\n * methods on this port. A parameterized contract test runs the same\n * assertions against all three adapters to keep them in sync.\n */\nexport type PersistencePort = {\n // Sessions ---------------------------------------------------------------\n createSession(input: CreateSessionInput): Promise<ChatSession>;\n /** Returns null when the session doesn't exist OR doesn't belong to this user. */\n getSession(id: number, userId: number): Promise<ChatSession | null>;\n listSessionsForUser(userId: number, opts?: ListSessionsOpts): Promise<ChatSession[]>;\n /** No-op when session doesn't exist or doesn't belong to user — caller asserted authorisation. */\n updateSession(id: number, userId: number, patch: { title?: string }): Promise<void>;\n deleteSession(id: number, userId: number): Promise<void>;\n\n // Messages ---------------------------------------------------------------\n appendMessage(input: AppendMessageInput): Promise<ChatMessage>;\n listMessagesForSession(sessionId: number, userId: number): Promise<ChatMessage[]>;\n\n // AI settings (singleton row) -------------------------------------------\n /** Returns the singleton row, applying defaults if it's never been written. */\n getAiSettings(): Promise<AiSettings>;\n /**\n * Upsert. Validates `toolProvider` and `chatInterface` against the runtime\n * registries before writing — invalid values throw before SQL is issued.\n */\n updateAiSettings(patch: AiSettingsPatch, byUserId: number): Promise<AiSettings>;\n};\n","/**\n * Drizzle MySQL implementation of `PersistencePort`.\n *\n * The package never imports a concrete drizzle client — the host passes a\n * `MySql2Database` (or any compatible drizzle MySQL handle) and we use it\n * to issue the queries this port describes.\n *\n * Boundary mapping: the drizzle MySQL `datetime` columns are declared with\n * `mode: 'string'` to match the host's existing schema, so row reads return\n * ISO strings. The PersistencePort domain types use `Date`, so every row\n * crossing the boundary is converted via `new Date(row.createdAt)`.\n *\n * Per-user safety: `getSession`, `updateSession`, `deleteSession`, and\n * `listMessagesForSession` all join `userId` into the WHERE clause — the\n * port's contract is that no caller should be able to read or mutate\n * another user's data even if they've forged a session id.\n */\nimport { and, desc, eq } from 'drizzle-orm';\nimport type { MySql2Database } from 'drizzle-orm/mysql2';\n\nimport {\n DEFAULT_AI_MAX_OUTPUT_TOKENS,\n type AiSettings,\n type AiSettingsPatch,\n type AppendMessageInput,\n type ChatMessage,\n type ChatMessageRole,\n type ChatSession,\n type CreateSessionInput,\n type ListSessionsOpts,\n type PersistencePort\n} from '../../server/ports/types';\n\nimport {\n aiSettings,\n chatMessages,\n chatSessions,\n type AiSettingsRow,\n type ChatMessageRow,\n type ChatSessionRow\n} from './tables';\n\n// ---------------------------------------------------------------------------\n// Row -> domain mappers\n// ---------------------------------------------------------------------------\n\nfunction mapSession(row: ChatSessionRow): ChatSession {\n return {\n id: row.id,\n userId: row.userId,\n title: row.title,\n createdAt: new Date(row.createdAt),\n updatedAt: new Date(row.updatedAt)\n };\n}\n\nfunction mapMessage(row: ChatMessageRow): ChatMessage {\n return {\n id: row.id,\n sessionId: row.sessionId,\n role: row.role as ChatMessageRole,\n question: row.question,\n blocks: row.blocks,\n prose: row.prose,\n errorJson: row.errorJson,\n createdAt: new Date(row.createdAt)\n };\n}\n\nfunction mapSettings(row: AiSettingsRow): AiSettings {\n return {\n toolProvider: row.toolProvider,\n gcpLocation: row.gcpLocation,\n chatInterface: row.chatInterface,\n maxOutputTokens: row.maxOutputTokens,\n rolePrompt: row.rolePrompt,\n updatedAt: row.updatedAt ? new Date(row.updatedAt) : null,\n updatedByUserId: row.updatedByUserId\n };\n}\n\n// ---------------------------------------------------------------------------\n// Factory\n// ---------------------------------------------------------------------------\n\nexport function createDrizzlePersistence(\n db: MySql2Database<any>\n): PersistencePort {\n return {\n // ---------------------------------------------------------------------\n // Sessions\n // ---------------------------------------------------------------------\n\n async createSession(input: CreateSessionInput): Promise<ChatSession> {\n const inserted = await db\n .insert(chatSessions)\n .values({\n userId: input.userId,\n title: input.title\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('createSession: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`createSession: row ${id} not found after insert`);\n }\n return mapSession(row);\n },\n\n async getSession(id: number, userId: number): Promise<ChatSession | null> {\n const [row] = await db\n .select()\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n return row ? mapSession(row) : null;\n },\n\n async listSessionsForUser(\n userId: number,\n opts?: ListSessionsOpts\n ): Promise<ChatSession[]> {\n const limit = opts?.limit ?? 50;\n const rows = await db\n .select()\n .from(chatSessions)\n .where(eq(chatSessions.userId, userId))\n .orderBy(desc(chatSessions.updatedAt))\n .limit(limit);\n\n return rows.map(mapSession);\n },\n\n async updateSession(\n id: number,\n userId: number,\n patch: { title?: string }\n ): Promise<void> {\n if (patch.title === undefined) return;\n\n await db\n .update(chatSessions)\n .set({ title: patch.title })\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n async deleteSession(id: number, userId: number): Promise<void> {\n // Delete messages first (no ON DELETE CASCADE assumed at table level).\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)))\n .limit(1);\n\n if (owned.length === 0) return;\n\n await db.delete(chatMessages).where(eq(chatMessages.sessionId, id));\n await db\n .delete(chatSessions)\n .where(and(eq(chatSessions.id, id), eq(chatSessions.userId, userId)));\n },\n\n // ---------------------------------------------------------------------\n // Messages\n // ---------------------------------------------------------------------\n\n async appendMessage(input: AppendMessageInput): Promise<ChatMessage> {\n const inserted = await db\n .insert(chatMessages)\n .values({\n sessionId: input.sessionId,\n role: input.role,\n question: input.question ?? null,\n blocks: input.blocks ?? null,\n prose: input.prose ?? null,\n errorJson: input.errorJson ?? null\n })\n .$returningId();\n\n const id = inserted[0]?.id;\n if (id == null) {\n throw new Error('appendMessage: insert returned no id');\n }\n\n const [row] = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.id, id))\n .limit(1);\n\n if (!row) {\n throw new Error(`appendMessage: row ${id} not found after insert`);\n }\n return mapMessage(row);\n },\n\n async listMessagesForSession(\n sessionId: number,\n userId: number\n ): Promise<ChatMessage[]> {\n // Verify ownership before returning rows — never trust the caller to\n // have filtered already.\n const owned = await db\n .select({ id: chatSessions.id })\n .from(chatSessions)\n .where(\n and(eq(chatSessions.id, sessionId), eq(chatSessions.userId, userId))\n )\n .limit(1);\n\n if (owned.length === 0) return [];\n\n const rows = await db\n .select()\n .from(chatMessages)\n .where(eq(chatMessages.sessionId, sessionId))\n .orderBy(chatMessages.createdAt, chatMessages.id);\n\n return rows.map(mapMessage);\n },\n\n // ---------------------------------------------------------------------\n // AI settings (singleton row)\n // ---------------------------------------------------------------------\n\n async getAiSettings(): Promise<AiSettings> {\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n // Synthesize defaults rather than failing — the row is created on\n // first write via INSERT … ON DUPLICATE KEY UPDATE.\n return {\n toolProvider: 'claude',\n gcpLocation: 'us-east5',\n chatInterface: 'custom',\n maxOutputTokens: DEFAULT_AI_MAX_OUTPUT_TOKENS,\n rolePrompt: null,\n updatedAt: null,\n updatedByUserId: null\n };\n }\n\n return mapSettings(row);\n },\n\n async updateAiSettings(\n patch: AiSettingsPatch,\n byUserId: number\n ): Promise<AiSettings> {\n // Build the values object with the singleton id and any defaults the\n // INSERT branch needs. ON DUPLICATE KEY UPDATE only touches keys in\n // the patch (plus updated_at / updated_by_user_id audit columns).\n const insertValues = {\n id: 1 as const,\n toolProvider: patch.toolProvider ?? 'claude',\n gcpLocation: patch.gcpLocation ?? 'us-east5',\n chatInterface: patch.chatInterface ?? 'custom',\n maxOutputTokens:\n patch.maxOutputTokens ?? DEFAULT_AI_MAX_OUTPUT_TOKENS,\n rolePrompt: patch.rolePrompt ?? null,\n updatedByUserId: byUserId\n };\n\n const updateSet: Record<string, unknown> = {\n updatedByUserId: byUserId\n };\n if (patch.toolProvider !== undefined) {\n updateSet.toolProvider = patch.toolProvider;\n }\n if (patch.gcpLocation !== undefined) {\n updateSet.gcpLocation = patch.gcpLocation;\n }\n if (patch.chatInterface !== undefined) {\n updateSet.chatInterface = patch.chatInterface;\n }\n if (patch.maxOutputTokens !== undefined) {\n updateSet.maxOutputTokens = patch.maxOutputTokens;\n }\n if (patch.rolePrompt !== undefined) {\n updateSet.rolePrompt = patch.rolePrompt;\n }\n\n await db\n .insert(aiSettings)\n .values(insertValues)\n .onDuplicateKeyUpdate({ set: updateSet });\n\n const [row] = await db\n .select()\n .from(aiSettings)\n .where(eq(aiSettings.id, 1))\n .limit(1);\n\n if (!row) {\n throw new Error('updateAiSettings: singleton row missing after upsert');\n }\n return mapSettings(row);\n }\n };\n}\n"]}
|
package/dist/prisma/index.cjs
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/server/ports/types.ts
|
|
4
|
+
var DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;
|
|
5
|
+
|
|
3
6
|
// src/adapters/prisma/adapter.ts
|
|
4
7
|
var toSession = (row) => ({
|
|
5
8
|
id: Number(row.id),
|
|
@@ -23,6 +26,8 @@ var toAiSettings = (row) => ({
|
|
|
23
26
|
toolProvider: row.toolProvider,
|
|
24
27
|
gcpLocation: row.gcpLocation,
|
|
25
28
|
chatInterface: row.chatInterface,
|
|
29
|
+
maxOutputTokens: row.maxOutputTokens,
|
|
30
|
+
rolePrompt: row.rolePrompt,
|
|
26
31
|
updatedAt: row.updatedAt,
|
|
27
32
|
updatedByUserId: row.updatedByUserId === null ? null : Number(row.updatedByUserId)
|
|
28
33
|
});
|
|
@@ -30,6 +35,8 @@ var DEFAULT_AI_SETTINGS = {
|
|
|
30
35
|
toolProvider: "claude",
|
|
31
36
|
gcpLocation: "us-east5",
|
|
32
37
|
chatInterface: "custom",
|
|
38
|
+
maxOutputTokens: DEFAULT_AI_MAX_OUTPUT_TOKENS,
|
|
39
|
+
rolePrompt: null,
|
|
33
40
|
updatedAt: null,
|
|
34
41
|
updatedByUserId: null
|
|
35
42
|
};
|