@firstlovecenter/ai-chat 0.8.1 → 0.9.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 +43 -0
- package/dist/drizzle/index.cjs +24 -16
- package/dist/drizzle/index.cjs.map +1 -1
- package/dist/drizzle/index.d.cts +17 -17
- package/dist/drizzle/index.d.ts +17 -17
- package/dist/drizzle/index.js +24 -16
- package/dist/drizzle/index.js.map +1 -1
- package/dist/prisma/index.cjs +20 -4
- package/dist/prisma/index.cjs.map +1 -1
- package/dist/prisma/index.d.cts +19 -10
- package/dist/prisma/index.d.ts +19 -10
- package/dist/prisma/index.js +20 -4
- package/dist/prisma/index.js.map +1 -1
- package/dist/server/index.cjs +5 -5
- package/dist/server/index.cjs.map +1 -1
- package/dist/server/index.d.cts +3 -3
- package/dist/server/index.d.ts +3 -3
- package/dist/server/index.js +5 -5
- package/dist/server/index.js.map +1 -1
- package/dist/{types-BnwUkqKb.d.cts → types-BOgBJ7CD.d.cts} +14 -8
- package/dist/{types-BnwUkqKb.d.ts → types-BOgBJ7CD.d.ts} +14 -8
- package/dist/ui/index.cjs +14 -12
- package/dist/ui/index.cjs.map +1 -1
- package/dist/ui/index.d.cts +21 -5
- package/dist/ui/index.d.ts +21 -5
- package/dist/ui/index.js +14 -12
- package/dist/ui/index.js.map +1 -1
- package/package.json +1 -1
- package/prisma/chat-models.prisma +7 -3
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,49 @@ 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.9.1] — 2026-05-09
|
|
9
|
+
|
|
10
|
+
Lets a host mount multiple chat surfaces side-by-side without yanking users between shells.
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **`AiChatProps.basePath`** (`string`, optional, default `'/chat'`) — URL prefix the chat surface is mounted at. Sidebar `<Link>` entries resolve to `${basePath}/${sessionId}`, `+ New chat` pushes `${basePath}?new`, and the open-session router push uses the same prefix. Both `AiChat` and `VercelChat` honour it.
|
|
15
|
+
|
|
16
|
+
### Migration notes
|
|
17
|
+
|
|
18
|
+
- Pure addition — `basePath` defaults to `/chat`, so existing `/chat`-mounted surfaces work unchanged.
|
|
19
|
+
- Hosts that want a second surface (e.g. an admin AI page at `/admin/ai`) need to (a) pass `basePath="/admin/ai"`, (b) add a matching dynamic segment (`/admin/ai/[id]`) so reload/bookmark URLs resolve, and (c) wire a redirect-to-most-recent at the basePath that respects `?new`.
|
|
20
|
+
- **Tailwind v4 hosts**: the package ships no CSS, so its component classes need to be in your Tailwind content scan. Add `@source "../node_modules/@firstlovecenter/ai-chat";` to your `globals.css` (path relative to the CSS file). Without this, the in-chat sidebar's `absolute`/`translate-x-*`/`bg-sidebar` classes are stripped from the production bundle and the sidebar renders as a static flex column instead of an overlay.
|
|
21
|
+
|
|
22
|
+
## [0.9.0] — 2026-05-09
|
|
23
|
+
|
|
24
|
+
Chat session and message identifiers are now short URL-safe UIDs (12 chars) instead of auto-incrementing BIGINTs. URLs like `/chat/V1StGXR8_Z5j` don't leak per-user chat counts and are unguessable, which matters as soon as a session URL is ever shared.
|
|
25
|
+
|
|
26
|
+
### Breaking
|
|
27
|
+
|
|
28
|
+
- **`ChatSession.id`, `ChatMessage.id`, `ChatMessage.sessionId`** are `string` instead of `number`.
|
|
29
|
+
- **`PersistencePort.getSession`, `updateSession`, `deleteSession`, `listMessagesForSession`** all take `string` ids.
|
|
30
|
+
- **`AppendMessageInput.sessionId`** is `string`.
|
|
31
|
+
- **`AiChatProps.initialSessionId`** is `string | null` (was `number | null`).
|
|
32
|
+
- **`AiChatSessionSummary.id`** is `string`.
|
|
33
|
+
- **`POST /api/agent`, `POST /api/agent/vercel`, `GET/PATCH/DELETE /api/chat/sessions/[id]`** all accept and return string ids.
|
|
34
|
+
- **DB schema**: `chat_sessions.id`, `chat_messages.id`, `chat_messages.session_id` are now `VARCHAR(12)`. The drizzle table helpers (`bigintPk()` for these tables) become `uidPk()` / `uidFk()`. The Prisma schema fragment (`prisma/chat-models.prisma`) updates to match.
|
|
35
|
+
|
|
36
|
+
### Added
|
|
37
|
+
|
|
38
|
+
- **`generateChatUid()`** in `./server/uid` — 12-char URL-safe UID generator (~72 bits of entropy, alphabet matches nanoid's URL-safe set). Adapters call it on every `createSession` / `appendMessage` insert. Hosts can call it directly to mint extra ids if they need to.
|
|
39
|
+
|
|
40
|
+
### Migration notes
|
|
41
|
+
|
|
42
|
+
Hosts on the drizzle adapter need a one-shot data migration (no auto-migration is shipped — the package can't safely guess your foreign-key topology):
|
|
43
|
+
|
|
44
|
+
1. Add new VARCHAR(12) columns alongside the existing BIGINT id columns.
|
|
45
|
+
2. Backfill new ids: generate one UID per `chat_sessions` row, then update each `chat_messages` row's `session_id_uid` to match its parent's new id.
|
|
46
|
+
3. Drop the old PK / FK / index, drop the old BIGINT columns, rename the new columns into place, recreate the PK / FK / index.
|
|
47
|
+
4. Deploy 0.9.0.
|
|
48
|
+
|
|
49
|
+
A reference migration script lives in the consuming repo (FLC `flc-data-intelligence`) at `scripts/migrate-chat-uids.ts`. Other hosts should adapt the same shape.
|
|
50
|
+
|
|
8
51
|
## [0.8.1] — 2026-05-09
|
|
9
52
|
|
|
10
53
|
UX polish on top of 0.8.0's URL-driven session restore.
|
package/dist/drizzle/index.cjs
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
var drizzleOrm = require('drizzle-orm');
|
|
4
4
|
var mysqlCore = require('drizzle-orm/mysql-core');
|
|
5
|
+
var crypto = require('crypto');
|
|
5
6
|
|
|
6
7
|
// src/adapters/drizzle/tables.ts
|
|
7
|
-
var bigintPk = () => mysqlCore.bigint("id", { mode: "number", unsigned: true }).notNull().primaryKey().autoincrement();
|
|
8
8
|
var bigintFk = (name) => mysqlCore.bigint(name, { mode: "number", unsigned: true }).notNull();
|
|
9
|
+
var uidPk = () => mysqlCore.varchar("id", { length: 12 }).notNull().primaryKey();
|
|
10
|
+
var uidFk = (name) => mysqlCore.varchar(name, { length: 12 }).notNull();
|
|
9
11
|
var bigintFkNullable = (name) => mysqlCore.bigint(name, { mode: "number", unsigned: true });
|
|
10
12
|
var createdAt = () => mysqlCore.datetime("created_at", { mode: "string" }).notNull().default(drizzleOrm.sql`CURRENT_TIMESTAMP`);
|
|
11
13
|
var aiSettings = mysqlCore.mysqlTable("ai_settings", {
|
|
@@ -33,7 +35,7 @@ var aiSettings = mysqlCore.mysqlTable("ai_settings", {
|
|
|
33
35
|
var chatSessions = mysqlCore.mysqlTable(
|
|
34
36
|
"chat_sessions",
|
|
35
37
|
{
|
|
36
|
-
id:
|
|
38
|
+
id: uidPk(),
|
|
37
39
|
userId: bigintFk("user_id"),
|
|
38
40
|
title: mysqlCore.varchar("title", { length: 200 }).notNull(),
|
|
39
41
|
createdAt: createdAt(),
|
|
@@ -47,8 +49,8 @@ var chatMessageRoleEnum = ["user", "assistant"];
|
|
|
47
49
|
var chatMessages = mysqlCore.mysqlTable(
|
|
48
50
|
"chat_messages",
|
|
49
51
|
{
|
|
50
|
-
id:
|
|
51
|
-
sessionId:
|
|
52
|
+
id: uidPk(),
|
|
53
|
+
sessionId: uidFk("session_id"),
|
|
52
54
|
role: mysqlCore.mysqlEnum("role", chatMessageRoleEnum).notNull(),
|
|
53
55
|
question: mysqlCore.text("question"),
|
|
54
56
|
blocks: mysqlCore.json("blocks"),
|
|
@@ -63,6 +65,16 @@ var chatMessages = mysqlCore.mysqlTable(
|
|
|
63
65
|
|
|
64
66
|
// src/server/ports/types.ts
|
|
65
67
|
var DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;
|
|
68
|
+
var ALPHABET = "ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW";
|
|
69
|
+
var CHAT_UID_LENGTH = 12;
|
|
70
|
+
function generateChatUid(size = CHAT_UID_LENGTH) {
|
|
71
|
+
const bytes = crypto.randomBytes(size);
|
|
72
|
+
let id = "";
|
|
73
|
+
for (let i = 0; i < size; i += 1) {
|
|
74
|
+
id += ALPHABET[bytes[i] & 63];
|
|
75
|
+
}
|
|
76
|
+
return id;
|
|
77
|
+
}
|
|
66
78
|
|
|
67
79
|
// src/adapters/drizzle/adapter.ts
|
|
68
80
|
function mapSession(row) {
|
|
@@ -104,14 +116,12 @@ function createDrizzlePersistence(db) {
|
|
|
104
116
|
// Sessions
|
|
105
117
|
// ---------------------------------------------------------------------
|
|
106
118
|
async createSession(input) {
|
|
107
|
-
const
|
|
119
|
+
const id = generateChatUid();
|
|
120
|
+
await db.insert(chatSessions).values({
|
|
121
|
+
id,
|
|
108
122
|
userId: input.userId,
|
|
109
123
|
title: input.title
|
|
110
|
-
})
|
|
111
|
-
const id = inserted[0]?.id;
|
|
112
|
-
if (id == null) {
|
|
113
|
-
throw new Error("createSession: insert returned no id");
|
|
114
|
-
}
|
|
124
|
+
});
|
|
115
125
|
const [row] = await db.select().from(chatSessions).where(drizzleOrm.eq(chatSessions.id, id)).limit(1);
|
|
116
126
|
if (!row) {
|
|
117
127
|
throw new Error(`createSession: row ${id} not found after insert`);
|
|
@@ -141,18 +151,16 @@ function createDrizzlePersistence(db) {
|
|
|
141
151
|
// Messages
|
|
142
152
|
// ---------------------------------------------------------------------
|
|
143
153
|
async appendMessage(input) {
|
|
144
|
-
const
|
|
154
|
+
const id = generateChatUid();
|
|
155
|
+
await db.insert(chatMessages).values({
|
|
156
|
+
id,
|
|
145
157
|
sessionId: input.sessionId,
|
|
146
158
|
role: input.role,
|
|
147
159
|
question: input.question ?? null,
|
|
148
160
|
blocks: input.blocks ?? null,
|
|
149
161
|
prose: input.prose ?? null,
|
|
150
162
|
errorJson: input.errorJson ?? null
|
|
151
|
-
})
|
|
152
|
-
const id = inserted[0]?.id;
|
|
153
|
-
if (id == null) {
|
|
154
|
-
throw new Error("appendMessage: insert returned no id");
|
|
155
|
-
}
|
|
163
|
+
});
|
|
156
164
|
await db.update(chatSessions).set({ updatedAt: drizzleOrm.sql`CURRENT_TIMESTAMP` }).where(drizzleOrm.eq(chatSessions.id, input.sessionId));
|
|
157
165
|
const [row] = await db.select().from(chatMessages).where(drizzleOrm.eq(chatMessages.id, id)).limit(1);
|
|
158
166
|
if (!row) {
|
|
@@ -1 +1 @@
|
|
|
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","int","text","index","mysqlEnum","json","eq","and","desc"],"mappings":";;;;;;AAwCA,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,CAAA,CACtC,OAAA,EAAQ,CACR,OAAA,CAAQC,cAAA,CAAA,iBAAA,CAAsB,CAAA;AAW5B,IAAM,UAAA,GAAaC,qBAAW,aAAA,EAAe;AAAA,EAClD,EAAA,EAAIC,kBAAQ,IAAI,CAAA,CAAE,SAAQ,CAAE,UAAA,EAAW,CAAE,OAAA,CAAQ,CAAC,CA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MA,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 /**\n * Admin-editable GCP project override. NULL means the host's static\n * `VertexPort.projectId` is used. Lets admins flip the active GCP\n * project without redeploying — useful for staging/prod migrations\n * or running multiple FLC tenants from one runtime.\n */\n gcpProjectId: varchar('gcp_project_id', { length: 64 }),\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 *\n * `gcpProjectId` is an admin-editable override for the GCP project that\n * every Vertex API call targets. When non-null, it takes precedence over\n * the host's static `VertexPort.projectId` so admins can flip projects\n * (staging ↔ prod, multi-tenant) without redeploying.\n */\nexport type AiSettings = {\n toolProvider: string;\n gcpLocation: string;\n chatInterface: string;\n maxOutputTokens: number;\n rolePrompt: string | null;\n gcpProjectId: 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 /** Pass `null` to clear back to the host's static VertexPort.projectId. */\n gcpProjectId?: 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, sql } 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 gcpProjectId: row.gcpProjectId,\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 // Bump the parent session's `updated_at` so `listSessionsForUser`\n // (ordered by updatedAt DESC) reflects \"most recent activity\"\n // rather than creation time. The column has no MySQL ON UPDATE\n // clause, so we set it explicitly here.\n await db\n .update(chatSessions)\n .set({ updatedAt: sql`CURRENT_TIMESTAMP` })\n .where(eq(chatSessions.id, input.sessionId));\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 gcpProjectId: 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 gcpProjectId: patch.gcpProjectId ?? 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 if (patch.gcpProjectId !== undefined) {\n updateSet.gcpProjectId = patch.gcpProjectId;\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/server/uid.ts","../../src/adapters/drizzle/adapter.ts"],"names":["bigint","varchar","datetime","sql","mysqlTable","tinyint","int","text","index","mysqlEnum","json","randomBytes","eq","and","desc"],"mappings":";;;;;;;AA+CA,IAAM,QAAA,GAAW,CAAC,IAAA,KAChBA,gBAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA,CAAE,OAAA,EAAQ;AAO3D,IAAM,KAAA,GAAQ,MACZC,iBAAA,CAAQ,IAAA,EAAM,EAAE,MAAA,EAAQ,EAAA,EAAI,CAAA,CAAE,OAAA,EAAQ,CAAE,UAAA,EAAW;AAGrD,IAAM,KAAA,GAAQ,CAAC,IAAA,KAAiBA,iBAAA,CAAQ,IAAA,EAAM,EAAE,MAAA,EAAQ,EAAA,EAAI,CAAA,CAAE,OAAA,EAAQ;AAGtE,IAAM,gBAAA,GAAmB,CAAC,IAAA,KACxBD,gBAAA,CAAO,IAAA,EAAM,EAAE,IAAA,EAAM,QAAA,EAAU,QAAA,EAAU,IAAA,EAAM,CAAA;AAGjD,IAAM,SAAA,GAAY,MAChBE,kBAAA,CAAS,YAAA,EAAc,EAAE,IAAA,EAAM,QAAA,EAAU,CAAA,CACtC,OAAA,EAAQ,CACR,OAAA,CAAQC,cAAA,CAAA,iBAAA,CAAsB,CAAA;AAW5B,IAAM,UAAA,GAAaC,qBAAW,aAAA,EAAe;AAAA,EAClD,EAAA,EAAIC,kBAAQ,IAAI,CAAA,CAAE,SAAQ,CAAE,UAAA,EAAW,CAAE,OAAA,CAA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,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/**\n * VARCHAR(12) PK for chat session/message rows. The package generates\n * UIDs via `generateChatUid()` (12 chars, ~72 bits, URL-safe alphabet)\n * and writes them on insert — the column has no DB-side default.\n */\nconst uidPk = () =>\n varchar('id', { length: 12 }).notNull().primaryKey();\n\n/** NOT NULL VARCHAR(12) FK referencing a chat-session UID. */\nconst uidFk = (name: string) => varchar(name, { length: 12 }).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 /**\n * Admin-editable GCP project override. NULL means the host's static\n * `VertexPort.projectId` is used. Lets admins flip the active GCP\n * project without redeploying — useful for staging/prod migrations\n * or running multiple FLC tenants from one runtime.\n */\n gcpProjectId: varchar('gcp_project_id', { length: 64 }),\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: uidPk(),\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: uidPk(),\n sessionId: uidFk('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\n/**\n * Chat session and message identifiers are short URL-safe UIDs (12 chars,\n * ~72 bits of entropy, alphabet matches nanoid's URL-safe set). The\n * package generates them in `createSession`/`appendMessage` so adapters\n * never need to know about auto-increment vs UID.\n */\nexport type ChatSession = {\n id: string;\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: string;\n sessionId: string;\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 *\n * `gcpProjectId` is an admin-editable override for the GCP project that\n * every Vertex API call targets. When non-null, it takes precedence over\n * the host's static `VertexPort.projectId` so admins can flip projects\n * (staging ↔ prod, multi-tenant) without redeploying.\n */\nexport type AiSettings = {\n toolProvider: string;\n gcpLocation: string;\n chatInterface: string;\n maxOutputTokens: number;\n rolePrompt: string | null;\n gcpProjectId: 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: string;\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 /** Pass `null` to clear back to the host's static VertexPort.projectId. */\n gcpProjectId?: 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: string, 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: string, userId: number, patch: { title?: string }): Promise<void>;\n deleteSession(id: string, userId: number): Promise<void>;\n\n // Messages ---------------------------------------------------------------\n appendMessage(input: AppendMessageInput): Promise<ChatMessage>;\n listMessagesForSession(sessionId: string, 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 * Short URL-safe identifier generator for chat session and message rows.\n *\n * 12 chars from a 64-char URL-safe alphabet ≈ 72 bits of entropy. That's\n * collision-safe well past any per-user chat count and short enough to\n * appear unobtrusively in URLs (`/chat/V1StGXR8_Z5j`). The alphabet\n * matches nanoid's URL-safe set so the IDs round-trip cleanly through\n * URL paths, query strings, JSON, and SQL VARCHAR columns.\n */\nimport { randomBytes } from 'crypto';\n\nconst ALPHABET =\n 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW';\n\nexport const CHAT_UID_LENGTH = 12;\n\nexport function generateChatUid(size: number = CHAT_UID_LENGTH): string {\n const bytes = randomBytes(size);\n let id = '';\n for (let i = 0; i < size; i += 1) {\n // Mask to 6 bits → uniform index into the 64-char alphabet.\n id += ALPHABET[bytes[i] & 63];\n }\n return id;\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, sql } 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';\nimport { generateChatUid } from '../../server/uid';\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 gcpProjectId: row.gcpProjectId,\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 // We generate the UID client-side rather than relying on a DB\n // default — keeps the schema portable (Postgres, SQLite ports\n // would otherwise need their own gen_random_uuid()/etc. shapes).\n const id = generateChatUid();\n await db.insert(chatSessions).values({\n id,\n userId: input.userId,\n title: input.title\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: string, 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: string,\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: string, 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 id = generateChatUid();\n await db.insert(chatMessages).values({\n id,\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\n // Bump the parent session's `updated_at` so `listSessionsForUser`\n // (ordered by updatedAt DESC) reflects \"most recent activity\"\n // rather than creation time. The column has no MySQL ON UPDATE\n // clause, so we set it explicitly here.\n await db\n .update(chatSessions)\n .set({ updatedAt: sql`CURRENT_TIMESTAMP` })\n .where(eq(chatSessions.id, input.sessionId));\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: string,\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 gcpProjectId: 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 gcpProjectId: patch.gcpProjectId ?? 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 if (patch.gcpProjectId !== undefined) {\n updateSet.gcpProjectId = patch.gcpProjectId;\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-BOgBJ7CD.cjs';
|
|
4
4
|
import 'google-auth-library';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -176,16 +176,16 @@ declare const chatSessions: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
176
176
|
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
177
177
|
name: "id";
|
|
178
178
|
tableName: "chat_sessions";
|
|
179
|
-
dataType: "
|
|
180
|
-
columnType: "
|
|
181
|
-
data:
|
|
179
|
+
dataType: "string";
|
|
180
|
+
columnType: "MySqlVarChar";
|
|
181
|
+
data: string;
|
|
182
182
|
driverParam: string | number;
|
|
183
183
|
notNull: true;
|
|
184
|
-
hasDefault:
|
|
184
|
+
hasDefault: false;
|
|
185
185
|
isPrimaryKey: true;
|
|
186
|
-
isAutoincrement:
|
|
186
|
+
isAutoincrement: false;
|
|
187
187
|
hasRuntimeDefault: false;
|
|
188
|
-
enumValues:
|
|
188
|
+
enumValues: [string, ...string[]];
|
|
189
189
|
baseColumn: never;
|
|
190
190
|
identity: undefined;
|
|
191
191
|
generated: undefined;
|
|
@@ -268,16 +268,16 @@ declare const chatMessages: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
268
268
|
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
269
269
|
name: "id";
|
|
270
270
|
tableName: "chat_messages";
|
|
271
|
-
dataType: "
|
|
272
|
-
columnType: "
|
|
273
|
-
data:
|
|
271
|
+
dataType: "string";
|
|
272
|
+
columnType: "MySqlVarChar";
|
|
273
|
+
data: string;
|
|
274
274
|
driverParam: string | number;
|
|
275
275
|
notNull: true;
|
|
276
|
-
hasDefault:
|
|
276
|
+
hasDefault: false;
|
|
277
277
|
isPrimaryKey: true;
|
|
278
|
-
isAutoincrement:
|
|
278
|
+
isAutoincrement: false;
|
|
279
279
|
hasRuntimeDefault: false;
|
|
280
|
-
enumValues:
|
|
280
|
+
enumValues: [string, ...string[]];
|
|
281
281
|
baseColumn: never;
|
|
282
282
|
identity: undefined;
|
|
283
283
|
generated: undefined;
|
|
@@ -285,16 +285,16 @@ declare const chatMessages: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
285
285
|
sessionId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
286
286
|
name: string;
|
|
287
287
|
tableName: "chat_messages";
|
|
288
|
-
dataType: "
|
|
289
|
-
columnType: "
|
|
290
|
-
data:
|
|
288
|
+
dataType: "string";
|
|
289
|
+
columnType: "MySqlVarChar";
|
|
290
|
+
data: string;
|
|
291
291
|
driverParam: string | number;
|
|
292
292
|
notNull: true;
|
|
293
293
|
hasDefault: false;
|
|
294
294
|
isPrimaryKey: false;
|
|
295
295
|
isAutoincrement: false;
|
|
296
296
|
hasRuntimeDefault: false;
|
|
297
|
-
enumValues:
|
|
297
|
+
enumValues: [string, ...string[]];
|
|
298
298
|
baseColumn: never;
|
|
299
299
|
identity: undefined;
|
|
300
300
|
generated: undefined;
|
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-BOgBJ7CD.js';
|
|
4
4
|
import 'google-auth-library';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
@@ -176,16 +176,16 @@ declare const chatSessions: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
176
176
|
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
177
177
|
name: "id";
|
|
178
178
|
tableName: "chat_sessions";
|
|
179
|
-
dataType: "
|
|
180
|
-
columnType: "
|
|
181
|
-
data:
|
|
179
|
+
dataType: "string";
|
|
180
|
+
columnType: "MySqlVarChar";
|
|
181
|
+
data: string;
|
|
182
182
|
driverParam: string | number;
|
|
183
183
|
notNull: true;
|
|
184
|
-
hasDefault:
|
|
184
|
+
hasDefault: false;
|
|
185
185
|
isPrimaryKey: true;
|
|
186
|
-
isAutoincrement:
|
|
186
|
+
isAutoincrement: false;
|
|
187
187
|
hasRuntimeDefault: false;
|
|
188
|
-
enumValues:
|
|
188
|
+
enumValues: [string, ...string[]];
|
|
189
189
|
baseColumn: never;
|
|
190
190
|
identity: undefined;
|
|
191
191
|
generated: undefined;
|
|
@@ -268,16 +268,16 @@ declare const chatMessages: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
268
268
|
id: drizzle_orm_mysql_core.MySqlColumn<{
|
|
269
269
|
name: "id";
|
|
270
270
|
tableName: "chat_messages";
|
|
271
|
-
dataType: "
|
|
272
|
-
columnType: "
|
|
273
|
-
data:
|
|
271
|
+
dataType: "string";
|
|
272
|
+
columnType: "MySqlVarChar";
|
|
273
|
+
data: string;
|
|
274
274
|
driverParam: string | number;
|
|
275
275
|
notNull: true;
|
|
276
|
-
hasDefault:
|
|
276
|
+
hasDefault: false;
|
|
277
277
|
isPrimaryKey: true;
|
|
278
|
-
isAutoincrement:
|
|
278
|
+
isAutoincrement: false;
|
|
279
279
|
hasRuntimeDefault: false;
|
|
280
|
-
enumValues:
|
|
280
|
+
enumValues: [string, ...string[]];
|
|
281
281
|
baseColumn: never;
|
|
282
282
|
identity: undefined;
|
|
283
283
|
generated: undefined;
|
|
@@ -285,16 +285,16 @@ declare const chatMessages: drizzle_orm_mysql_core.MySqlTableWithColumns<{
|
|
|
285
285
|
sessionId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
286
286
|
name: string;
|
|
287
287
|
tableName: "chat_messages";
|
|
288
|
-
dataType: "
|
|
289
|
-
columnType: "
|
|
290
|
-
data:
|
|
288
|
+
dataType: "string";
|
|
289
|
+
columnType: "MySqlVarChar";
|
|
290
|
+
data: string;
|
|
291
291
|
driverParam: string | number;
|
|
292
292
|
notNull: true;
|
|
293
293
|
hasDefault: false;
|
|
294
294
|
isPrimaryKey: false;
|
|
295
295
|
isAutoincrement: false;
|
|
296
296
|
hasRuntimeDefault: false;
|
|
297
|
-
enumValues:
|
|
297
|
+
enumValues: [string, ...string[]];
|
|
298
298
|
baseColumn: never;
|
|
299
299
|
identity: undefined;
|
|
300
300
|
generated: undefined;
|
package/dist/drizzle/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { sql, eq, and, desc } from 'drizzle-orm';
|
|
2
2
|
import { mysqlTable, datetime, text, int, varchar, tinyint, index, json, mysqlEnum, bigint } from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { randomBytes } from 'crypto';
|
|
3
4
|
|
|
4
5
|
// src/adapters/drizzle/tables.ts
|
|
5
|
-
var bigintPk = () => bigint("id", { mode: "number", unsigned: true }).notNull().primaryKey().autoincrement();
|
|
6
6
|
var bigintFk = (name) => bigint(name, { mode: "number", unsigned: true }).notNull();
|
|
7
|
+
var uidPk = () => varchar("id", { length: 12 }).notNull().primaryKey();
|
|
8
|
+
var uidFk = (name) => varchar(name, { length: 12 }).notNull();
|
|
7
9
|
var bigintFkNullable = (name) => bigint(name, { mode: "number", unsigned: true });
|
|
8
10
|
var createdAt = () => datetime("created_at", { mode: "string" }).notNull().default(sql`CURRENT_TIMESTAMP`);
|
|
9
11
|
var aiSettings = mysqlTable("ai_settings", {
|
|
@@ -31,7 +33,7 @@ var aiSettings = mysqlTable("ai_settings", {
|
|
|
31
33
|
var chatSessions = mysqlTable(
|
|
32
34
|
"chat_sessions",
|
|
33
35
|
{
|
|
34
|
-
id:
|
|
36
|
+
id: uidPk(),
|
|
35
37
|
userId: bigintFk("user_id"),
|
|
36
38
|
title: varchar("title", { length: 200 }).notNull(),
|
|
37
39
|
createdAt: createdAt(),
|
|
@@ -45,8 +47,8 @@ var chatMessageRoleEnum = ["user", "assistant"];
|
|
|
45
47
|
var chatMessages = mysqlTable(
|
|
46
48
|
"chat_messages",
|
|
47
49
|
{
|
|
48
|
-
id:
|
|
49
|
-
sessionId:
|
|
50
|
+
id: uidPk(),
|
|
51
|
+
sessionId: uidFk("session_id"),
|
|
50
52
|
role: mysqlEnum("role", chatMessageRoleEnum).notNull(),
|
|
51
53
|
question: text("question"),
|
|
52
54
|
blocks: json("blocks"),
|
|
@@ -61,6 +63,16 @@ var chatMessages = mysqlTable(
|
|
|
61
63
|
|
|
62
64
|
// src/server/ports/types.ts
|
|
63
65
|
var DEFAULT_AI_MAX_OUTPUT_TOKENS = 4096;
|
|
66
|
+
var ALPHABET = "ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW";
|
|
67
|
+
var CHAT_UID_LENGTH = 12;
|
|
68
|
+
function generateChatUid(size = CHAT_UID_LENGTH) {
|
|
69
|
+
const bytes = randomBytes(size);
|
|
70
|
+
let id = "";
|
|
71
|
+
for (let i = 0; i < size; i += 1) {
|
|
72
|
+
id += ALPHABET[bytes[i] & 63];
|
|
73
|
+
}
|
|
74
|
+
return id;
|
|
75
|
+
}
|
|
64
76
|
|
|
65
77
|
// src/adapters/drizzle/adapter.ts
|
|
66
78
|
function mapSession(row) {
|
|
@@ -102,14 +114,12 @@ function createDrizzlePersistence(db) {
|
|
|
102
114
|
// Sessions
|
|
103
115
|
// ---------------------------------------------------------------------
|
|
104
116
|
async createSession(input) {
|
|
105
|
-
const
|
|
117
|
+
const id = generateChatUid();
|
|
118
|
+
await db.insert(chatSessions).values({
|
|
119
|
+
id,
|
|
106
120
|
userId: input.userId,
|
|
107
121
|
title: input.title
|
|
108
|
-
})
|
|
109
|
-
const id = inserted[0]?.id;
|
|
110
|
-
if (id == null) {
|
|
111
|
-
throw new Error("createSession: insert returned no id");
|
|
112
|
-
}
|
|
122
|
+
});
|
|
113
123
|
const [row] = await db.select().from(chatSessions).where(eq(chatSessions.id, id)).limit(1);
|
|
114
124
|
if (!row) {
|
|
115
125
|
throw new Error(`createSession: row ${id} not found after insert`);
|
|
@@ -139,18 +149,16 @@ function createDrizzlePersistence(db) {
|
|
|
139
149
|
// Messages
|
|
140
150
|
// ---------------------------------------------------------------------
|
|
141
151
|
async appendMessage(input) {
|
|
142
|
-
const
|
|
152
|
+
const id = generateChatUid();
|
|
153
|
+
await db.insert(chatMessages).values({
|
|
154
|
+
id,
|
|
143
155
|
sessionId: input.sessionId,
|
|
144
156
|
role: input.role,
|
|
145
157
|
question: input.question ?? null,
|
|
146
158
|
blocks: input.blocks ?? null,
|
|
147
159
|
prose: input.prose ?? null,
|
|
148
160
|
errorJson: input.errorJson ?? null
|
|
149
|
-
})
|
|
150
|
-
const id = inserted[0]?.id;
|
|
151
|
-
if (id == null) {
|
|
152
|
-
throw new Error("appendMessage: insert returned no id");
|
|
153
|
-
}
|
|
161
|
+
});
|
|
154
162
|
await db.update(chatSessions).set({ updatedAt: sql`CURRENT_TIMESTAMP` }).where(eq(chatSessions.id, input.sessionId));
|
|
155
163
|
const [row] = await db.select().from(chatMessages).where(eq(chatMessages.id, id)).limit(1);
|
|
156
164
|
if (!row) {
|