@skaile/forge-layer-chat 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,147 @@
1
+ import { and, desc, eq, notExists } from 'drizzle-orm'
2
+ import { createError } from 'h3'
3
+ import * as z from 'zod'
4
+ import { chatMessages, chatThreads } from '~~/server/db/schema'
5
+ import type { AppActionContext } from '~~/server/utils/actions/define'
6
+ import { defineAppAction } from '~~/server/utils/actions/define'
7
+ import { useDb } from '~~/server/utils/db'
8
+ import { requireScope } from '~~/server/utils/principal'
9
+ import {
10
+ createThread,
11
+ deleteThread,
12
+ listThreads,
13
+ loadThreadEvents,
14
+ renameThread,
15
+ requireOwnThread,
16
+ THREAD_TRANSPORTS,
17
+ } from './chat-threads'
18
+
19
+ /** The person whose threads these are: chat threads belong to people, never to the system principal. */
20
+ function threadOwner(ctx: AppActionContext, scope: 'chat:read' | 'chat:write'): string {
21
+ requireScope(ctx, scope)
22
+ if (ctx.principal.kind === 'system')
23
+ throw createError({
24
+ statusCode: 403,
25
+ statusMessage: 'Chat threads belong to people; the system principal has none',
26
+ })
27
+ return ctx.principal.id
28
+ }
29
+
30
+ const threadId = z.uuid().describe('Id of the chat thread (see chat.threads.list)')
31
+
32
+ /**
33
+ * Chat threads. Audience `user` only: they serve the chat UI, the API and the
34
+ * CLI; a model never reads or deletes conversations.
35
+ */
36
+ export const chatActions = [
37
+ defineAppAction({
38
+ id: 'chat.threads.list',
39
+ kind: 'query',
40
+ audience: ['user'],
41
+ description:
42
+ "The caller's chat threads with the assistant, most recently active first: id, title, transport (model or workspace), timestamps and number of stored events.",
43
+ input: z.object({}).strict(),
44
+ authorize: (_input, ctx) => {
45
+ threadOwner(ctx, 'chat:read')
46
+ },
47
+ run: async (_input, ctx) => ({ items: await listThreads(ctx.principal.id) }),
48
+ }),
49
+
50
+ defineAppAction({
51
+ id: 'chat.threads.create',
52
+ kind: 'effect',
53
+ destructive: false,
54
+ idempotent: true,
55
+ audience: ['user'],
56
+ description:
57
+ 'Start a conversation with an assistant backend. Reuses the caller\'s newest thread of that transport that has no messages yet, so opening "new conversation" twice does not pile up empty threads. Returns the thread.',
58
+ input: z
59
+ .object({
60
+ transport: z
61
+ .enum(THREAD_TRANSPORTS)
62
+ .default('model')
63
+ .describe('Assistant backend: model (in-app model, default) or workspace (workspace agent)'),
64
+ })
65
+ .strict(),
66
+ authorize: (_input, ctx) => {
67
+ threadOwner(ctx, 'chat:write')
68
+ },
69
+ run: async (input, ctx) => {
70
+ const [empty] = await useDb()
71
+ .select()
72
+ .from(chatThreads)
73
+ .where(
74
+ and(
75
+ eq(chatThreads.userId, ctx.principal.id),
76
+ eq(chatThreads.transport, input.transport),
77
+ notExists(
78
+ useDb()
79
+ .select({ id: chatMessages.id })
80
+ .from(chatMessages)
81
+ .where(eq(chatMessages.threadId, chatThreads.id)),
82
+ ),
83
+ ),
84
+ )
85
+ .orderBy(desc(chatThreads.createdAt))
86
+ .limit(1)
87
+ return empty ?? (await createThread(ctx.principal.id, { transport: input.transport }))
88
+ },
89
+ }),
90
+
91
+ defineAppAction({
92
+ id: 'chat.threads.get',
93
+ kind: 'query',
94
+ audience: ['user'],
95
+ description:
96
+ "One of the caller's chat threads with its stored events, oldest first (the chat transport replays them as history). 404 for unknown threads and for threads of other people.",
97
+ input: z.object({ id: threadId }).strict(),
98
+ authorize: (_input, ctx) => {
99
+ threadOwner(ctx, 'chat:read')
100
+ },
101
+ run: async (input, ctx) => {
102
+ const thread = await requireOwnThread(input.id, ctx.principal.id)
103
+ return { thread, events: await loadThreadEvents(thread.id) }
104
+ },
105
+ }),
106
+
107
+ defineAppAction({
108
+ id: 'chat.threads.rename',
109
+ kind: 'effect',
110
+ destructive: true,
111
+ idempotent: true,
112
+ audience: ['user'],
113
+ description: "Rename one of the caller's chat threads. Returns the thread.",
114
+ input: z
115
+ .object({
116
+ id: threadId,
117
+ title: z.string().trim().min(1).max(200).describe('New title'),
118
+ })
119
+ .strict(),
120
+ authorize: (_input, ctx) => {
121
+ threadOwner(ctx, 'chat:write')
122
+ },
123
+ run: async (input, ctx) => {
124
+ await requireOwnThread(input.id, ctx.principal.id)
125
+ return renameThread(input.id, input.title)
126
+ },
127
+ }),
128
+
129
+ defineAppAction({
130
+ id: 'chat.threads.delete',
131
+ kind: 'effect',
132
+ destructive: true,
133
+ audience: ['user'],
134
+ description: "Delete one of the caller's chat threads and all its messages. Cannot be undone.",
135
+ input: z.object({ id: threadId }).strict(),
136
+ authorize: (_input, ctx) => {
137
+ threadOwner(ctx, 'chat:write')
138
+ },
139
+ run: async (input, ctx) => {
140
+ await requireOwnThread(input.id, ctx.principal.id)
141
+ await deleteThread(input.id)
142
+ return { id: input.id, deleted: true }
143
+ },
144
+ }),
145
+ ] as const
146
+
147
+ export type ChatActions = typeof chatActions
@@ -0,0 +1,189 @@
1
+ import { serverActionToolSet } from '@skaile/forge-app-actions/chat/ai-sdk'
2
+ import { toolNameOf } from '@skaile/forge-app-actions/mcp'
3
+ import type { ServerAction } from '@skaile/forge-app-actions/server'
4
+ import { forAudience } from '@skaile/forge-app-actions/server'
5
+ import type { ChatStreamEvent } from '@skaile/forge-chat'
6
+ import { readSse } from '@skaile/forge-chat'
7
+ import { createAiSdkChatHandler } from '@skaile/forge-chat/server/ai-sdk'
8
+ import type { ModelRoleDefinition } from '@skaile/forge-settings/server'
9
+ import type { LanguageModel, ToolSet } from 'ai'
10
+ import type { H3Event } from 'h3'
11
+ import { createError, getRequestURL, readRawBody, setResponseStatus } from 'h3'
12
+ import * as z from 'zod'
13
+ import { allActions } from '~~/server/utils/actions/registry'
14
+ import { RoleNotConfigured, resolveRoleModel } from '~~/server/utils/ai/roles'
15
+ import { requirePrincipal } from '~~/server/utils/auth'
16
+ import type { Principal } from '~~/server/utils/principal'
17
+ import { appSettings, getSettings } from '~~/server/utils/settings'
18
+ import type { ChatContext } from '../../shared/chat-context'
19
+ import { chatContextSchema } from '../../shared/chat-context'
20
+ import { assistantSettings } from './assistant-settings'
21
+ import { appendThreadEvents, compactTurnEvents, requireOwnThread, titleThreadFromFirstTurn } from './chat-threads'
22
+ import { toDraft2020 } from './tool-schema'
23
+
24
+ /**
25
+ * The in-app assistant behind `POST /api/chat`: model resolution, system
26
+ * prompt, the tool set (the catalogue's `llm` actions, run with the signed-in
27
+ * person's principal) and thread persistence around forge-chat's handler.
28
+ */
29
+
30
+ /** The model role the assistant answers with; the plugin registers it, people assign its model in Settings › Harness. */
31
+ export const ASSISTANT_ROLE: ModelRoleDefinition = {
32
+ id: 'assistant',
33
+ kind: 'chat',
34
+ label: 'In-app assistant',
35
+ description: 'Answers in the assistant drawer, run inside the app server.',
36
+ enabled: true,
37
+ }
38
+
39
+ /** The catalogue's actions a model may call (audience `llm`; destructive ones are `user`-only and never tools). */
40
+ export function assistantActions(): ServerAction[] {
41
+ return forAudience(allActions(), 'llm')
42
+ }
43
+
44
+ /**
45
+ * The tools of one chat request: every `llm` action, executed with `principal`
46
+ * through the normal authorize path; input schemas in draft 2020-12 (`toDraft2020`).
47
+ */
48
+ export function assistantToolSet(principal: Principal, event?: H3Event): ToolSet {
49
+ const actions = assistantActions().map(action => ({ ...action, inputSchema: toDraft2020(action.inputSchema) }))
50
+ return serverActionToolSet(actions, { principal, event, deps: undefined })
51
+ }
52
+
53
+ export function systemPromptFor(input: {
54
+ appName: string
55
+ timezone: string
56
+ principal: Principal
57
+ context: ChatContext
58
+ actions: readonly ServerAction[]
59
+ addendum: string
60
+ now?: Date
61
+ }): string {
62
+ const { appName, timezone, principal, context, actions, addendum, now = new Date() } = input
63
+ const today = new Intl.DateTimeFormat('en-GB', { dateStyle: 'full', timeStyle: 'short', timeZone: timezone }).format(
64
+ now,
65
+ )
66
+ const effects = actions.filter(action => action.kind === 'effect').map(action => toolNameOf(action.name))
67
+ return [
68
+ `You are the assistant inside ${appName}. You help ${principal.name} (${principal.roles.join(', ')}) work with the app's data through the tools provided. Tools act with this person's rights, no more.`,
69
+ `Now: ${today} (${timezone}).`,
70
+ `The person is on the page ${context.path}.`,
71
+ context.entity && context.id
72
+ ? `The page shows one record of the entity "${context.entity}", id ${context.id}.`
73
+ : null,
74
+ effects.length
75
+ ? `These tools change data: ${effects.join(', ')}. Before calling one, state in one short sentence what you are about to change, then call it in the same turn. Ask first only when the request is ambiguous.`
76
+ : null,
77
+ 'Look things up with the tools instead of guessing ids or names. Answer briefly, in Markdown.',
78
+ addendum.trim() || null,
79
+ ]
80
+ .filter(Boolean)
81
+ .join('\n\n')
82
+ }
83
+
84
+ const chatRequestSchema = z
85
+ .object({
86
+ threadId: z.uuid(),
87
+ context: chatContextSchema.optional(),
88
+ input: z.object({ text: z.string(), continue: z.boolean().optional() }).loose(),
89
+ })
90
+ .loose()
91
+
92
+ /** Reads the SSE frames of one answer and returns its events once the stream ended. */
93
+ async function collectTurn(body: ReadableStream<Uint8Array>): Promise<ChatStreamEvent[]> {
94
+ const events: ChatStreamEvent[] = []
95
+ for await (const frame of readSse(new Response(body))) {
96
+ if (frame.event !== 'chunk') continue
97
+ try {
98
+ events.push(JSON.parse(frame.data) as ChatStreamEvent)
99
+ } catch {
100
+ // A frame that is not JSON carries nothing to replay.
101
+ }
102
+ }
103
+ return events
104
+ }
105
+
106
+ /**
107
+ * `POST /api/chat`: the signed-in person (401 otherwise) talks to the model in
108
+ * one of their threads (`threadId`, 404 for a foreign one). Without a usable
109
+ * model the answer is 503 `{ error: 'assistant_not_configured', reason }`.
110
+ * The user turn is stored once the handler accepted the request; the
111
+ * assistant's turn when its stream ended (also when the browser went away).
112
+ *
113
+ * The model is the `assistant` role (`resolveRoleModel`). `options.env` is where
114
+ * the `NUXT_AI_*` fallback and `AI_PROVIDERS_DIR` are read (default
115
+ * `process.env`); `options.model` skips resolution (tests).
116
+ */
117
+ export async function handleChatRequest(
118
+ event: H3Event,
119
+ options: { env?: Record<string, string | undefined>; model?: LanguageModel } = {},
120
+ ): Promise<Response | { error: string; reason: string }> {
121
+ const principal = await requirePrincipal(event)
122
+ const raw = await readRawBody(event, 'utf8')
123
+ let json: unknown
124
+ try {
125
+ json = JSON.parse(raw ?? '')
126
+ } catch {
127
+ throw createError({ statusCode: 400, statusMessage: 'Body must be JSON' })
128
+ }
129
+ const parsed = chatRequestSchema.safeParse(json)
130
+ if (!parsed.success)
131
+ throw createError({ statusCode: 400, statusMessage: `Invalid chat request: ${z.prettifyError(parsed.error)}` })
132
+ const { threadId, input } = parsed.data
133
+ const thread = await requireOwnThread(threadId, principal.id)
134
+
135
+ const assistant = await getSettings(assistantSettings)
136
+ let model: LanguageModel
137
+ try {
138
+ model = options.model ?? (await resolveRoleModel(ASSISTANT_ROLE.id, { env: options.env })).languageModel()
139
+ } catch (error) {
140
+ if (!(error instanceof RoleNotConfigured)) throw error
141
+ setResponseStatus(event, 503)
142
+ return { error: 'assistant_not_configured', reason: error.reason }
143
+ }
144
+
145
+ const app = await getSettings(appSettings)
146
+ const actions = assistantActions()
147
+ const handler = createAiSdkChatHandler({
148
+ model,
149
+ system: systemPromptFor({
150
+ appName: app.appName,
151
+ timezone: app.timezone,
152
+ principal,
153
+ context: parsed.data.context ?? { path: '/' },
154
+ actions,
155
+ addendum: assistant.systemPromptAddendum,
156
+ }),
157
+ tools: assistantToolSet(principal, event),
158
+ maxSteps: assistant.maxSteps,
159
+ })
160
+
161
+ // The browser closing the stream cancels the model call (turn-end `cancelled`).
162
+ const abort = new AbortController()
163
+ event.node.res.once('close', () => {
164
+ if (!event.node.res.writableFinished) abort.abort()
165
+ })
166
+ const response = await handler(
167
+ new Request(getRequestURL(event), {
168
+ method: 'POST',
169
+ headers: { 'content-type': 'application/json' },
170
+ body: raw,
171
+ signal: abort.signal,
172
+ }),
173
+ )
174
+ if (!response.ok || !response.body) return response
175
+
176
+ if (!input.continue) {
177
+ const user: ChatStreamEvent = { type: 'user', text: input.text, createdAt: Date.now() }
178
+ await appendThreadEvents(thread.id, [user])
179
+ await titleThreadFromFirstTurn(thread, input.text)
180
+ }
181
+ const [toClient, toStore] = response.body.tee()
182
+ // Nitro keeps the request alive until the turn is stored (`waitUntil`).
183
+ event.waitUntil(
184
+ collectTurn(toStore)
185
+ .then(events => appendThreadEvents(thread.id, compactTurnEvents(events)))
186
+ .catch((error: unknown) => console.error('[chat] could not store the assistant turn', error)),
187
+ )
188
+ return new Response(toClient, { status: response.status, headers: response.headers })
189
+ }
@@ -0,0 +1,149 @@
1
+ import type { ChatStreamEvent } from '@skaile/forge-chat'
2
+ import { and, asc, desc, eq, sql } from 'drizzle-orm'
3
+ import { createError } from 'h3'
4
+ import { chatMessages, chatThreads } from '~~/server/db/schema'
5
+ import { useDb, withTx } from '~~/server/utils/db'
6
+
7
+ /**
8
+ * Chat threads: one conversation of one person with an assistant backend, stored
9
+ * as the ordered stream events its transport replays through `history()`
10
+ * (`chat_threads` / `chat_messages`). Other layers (the workspace agent) use the
11
+ * same functions for their own transport.
12
+ */
13
+
14
+ export type ThreadTransport = (typeof chatThreads.$inferSelect)['transport']
15
+ export const THREAD_TRANSPORTS = chatThreads.transport.enumValues
16
+ export const DEFAULT_THREAD_TITLE = 'New conversation'
17
+
18
+ /** Any persisted event: forge-chat `ChatStreamEvent`s, or another transport's own events. */
19
+ export type ThreadEvent = { type: string } & Record<string, unknown>
20
+
21
+ export type ChatThread = typeof chatThreads.$inferSelect
22
+
23
+ export async function createThread(
24
+ userId: string,
25
+ options: { transport?: ThreadTransport; title?: string } = {},
26
+ ): Promise<ChatThread> {
27
+ const [thread] = await useDb()
28
+ .insert(chatThreads)
29
+ .values({ userId, transport: options.transport ?? 'model', title: options.title ?? DEFAULT_THREAD_TITLE })
30
+ .returning()
31
+ return thread!
32
+ }
33
+
34
+ /** The person's threads, most recently active first, with their number of stored events. */
35
+ export async function listThreads(userId: string) {
36
+ return useDb()
37
+ .select({
38
+ id: chatThreads.id,
39
+ title: chatThreads.title,
40
+ transport: chatThreads.transport,
41
+ createdAt: chatThreads.createdAt,
42
+ updatedAt: chatThreads.updatedAt,
43
+ eventCount: sql<number>`(select count(*)::int from ${chatMessages} where ${chatMessages.threadId} = ${chatThreads.id})`,
44
+ })
45
+ .from(chatThreads)
46
+ .where(eq(chatThreads.userId, userId))
47
+ .orderBy(desc(chatThreads.updatedAt), desc(chatThreads.createdAt))
48
+ }
49
+
50
+ /**
51
+ * The thread if it belongs to `userId`. Another person's thread answers 404,
52
+ * exactly like a missing one, so ids of foreign threads reveal nothing.
53
+ */
54
+ export async function requireOwnThread(threadId: string, userId: string): Promise<ChatThread> {
55
+ const [thread] = await useDb()
56
+ .select()
57
+ .from(chatThreads)
58
+ .where(and(eq(chatThreads.id, threadId), eq(chatThreads.userId, userId)))
59
+ .limit(1)
60
+ if (!thread) throw createError({ statusCode: 404, statusMessage: `Chat thread ${threadId} not found` })
61
+ return thread
62
+ }
63
+
64
+ function roleOf(event: ThreadEvent): 'user' | 'assistant' | 'system' {
65
+ if (event.type === 'user') return 'user'
66
+ return event.role === 'system' ? 'system' : 'assistant'
67
+ }
68
+
69
+ /**
70
+ * Appends events at the end of the thread (sequence numbers continue after the
71
+ * last stored one) and marks the thread as active now. The thread row is locked
72
+ * for the transaction, so concurrent appends to one thread stay ordered.
73
+ */
74
+ export async function appendThreadEvents(threadId: string, events: readonly ThreadEvent[]): Promise<void> {
75
+ if (events.length === 0) return
76
+ await withTx(async tx => {
77
+ const [thread] = await tx
78
+ .select({ id: chatThreads.id })
79
+ .from(chatThreads)
80
+ .where(eq(chatThreads.id, threadId))
81
+ .for('update')
82
+ if (!thread) throw createError({ statusCode: 404, statusMessage: `Chat thread ${threadId} not found` })
83
+ const [last] = await tx
84
+ .select({ seq: sql<number>`coalesce(max(${chatMessages.seq}), 0)::int` })
85
+ .from(chatMessages)
86
+ .where(eq(chatMessages.threadId, threadId))
87
+ const start = (last?.seq ?? 0) + 1
88
+ await tx.insert(chatMessages).values(
89
+ events.map((event, index) => ({
90
+ threadId,
91
+ seq: start + index,
92
+ role: roleOf(event),
93
+ event,
94
+ })),
95
+ )
96
+ await tx.update(chatThreads).set({ updatedAt: new Date() }).where(eq(chatThreads.id, threadId))
97
+ })
98
+ }
99
+
100
+ /** The stored events, oldest first; `E` is the event type the thread's transport writes. */
101
+ export async function loadThreadEvents<E extends ThreadEvent = ThreadEvent & ChatStreamEvent>(
102
+ threadId: string,
103
+ ): Promise<E[]> {
104
+ const rows = await useDb()
105
+ .select({ event: chatMessages.event })
106
+ .from(chatMessages)
107
+ .where(eq(chatMessages.threadId, threadId))
108
+ .orderBy(asc(chatMessages.seq))
109
+ return rows.map(row => row.event as E)
110
+ }
111
+
112
+ export async function renameThread(threadId: string, title: string): Promise<ChatThread> {
113
+ const [thread] = await useDb()
114
+ .update(chatThreads)
115
+ .set({ title, updatedAt: new Date() })
116
+ .where(eq(chatThreads.id, threadId))
117
+ .returning()
118
+ return thread!
119
+ }
120
+
121
+ export async function deleteThread(threadId: string): Promise<void> {
122
+ await useDb().delete(chatThreads).where(eq(chatThreads.id, threadId))
123
+ }
124
+
125
+ /** Titles a thread after its first question, if it still has the default title. */
126
+ export async function titleThreadFromFirstTurn(thread: ChatThread, text: string): Promise<void> {
127
+ if (thread.title !== DEFAULT_THREAD_TITLE) return
128
+ const title = text.replace(/\s+/g, ' ').trim().slice(0, 80)
129
+ if (title) await useDb().update(chatThreads).set({ title }).where(eq(chatThreads.id, thread.id))
130
+ }
131
+
132
+ /**
133
+ * The events of one finished assistant turn as they are stored: consecutive
134
+ * text and reasoning deltas joined into one event each (replaying them yields
135
+ * the same message), transient `activity` lines dropped.
136
+ */
137
+ export function compactTurnEvents(events: readonly ChatStreamEvent[]): ChatStreamEvent[] {
138
+ const out: ChatStreamEvent[] = []
139
+ for (const event of events) {
140
+ if (event.type === 'activity') continue
141
+ const previous = out.at(-1)
142
+ if ((event.type === 'text-delta' || event.type === 'reasoning-delta') && previous?.type === event.type) {
143
+ out[out.length - 1] = { type: event.type, text: previous.text + event.text }
144
+ continue
145
+ }
146
+ out.push(event)
147
+ }
148
+ return out
149
+ }
@@ -0,0 +1,56 @@
1
+ const DRAFT_2020 = 'https://json-schema.org/draft/2020-12/schema'
2
+
3
+ /** Keywords whose values are data, not schemas: copied as they are. */
4
+ const DATA_KEYWORDS: Record<string, true> = { default: true, const: true, enum: true, examples: true }
5
+
6
+ /** Keywords whose values map names to schemas: the names are not keywords. */
7
+ const SCHEMA_MAPS: Record<string, true> = {
8
+ properties: true,
9
+ patternProperties: true,
10
+ $defs: true,
11
+ definitions: true,
12
+ dependentSchemas: true,
13
+ }
14
+
15
+ /**
16
+ * A tool input schema in JSON Schema draft 2020-12. The action catalogue emits
17
+ * draft-07 (`@skaile/forge-app-actions`, for MCP); Anthropic's Messages API
18
+ * rejects the whole request when one tool schema is not valid 2020-12. The one
19
+ * construct zod emits differently is a tuple: draft-07 `items: [..]` with
20
+ * `additionalItems` becomes `prefixItems` with `items`. A declared `$schema`
21
+ * is rewritten to 2020-12.
22
+ */
23
+ export function toDraft2020(schema: Record<string, unknown>): Record<string, unknown> {
24
+ const out = convertSchema(schema)
25
+ if ('$schema' in out) out.$schema = DRAFT_2020
26
+ return out
27
+ }
28
+
29
+ function convertSchema(schema: Record<string, unknown>): Record<string, unknown> {
30
+ const out: Record<string, unknown> = {}
31
+ for (const [key, value] of Object.entries(schema)) {
32
+ if (Object.hasOwn(DATA_KEYWORDS, key)) out[key] = value
33
+ else if (Object.hasOwn(SCHEMA_MAPS, key) && isObject(value))
34
+ out[key] = Object.fromEntries(Object.entries(value).map(([name, sub]) => [name, convert(sub)]))
35
+ else out[key] = convert(value)
36
+ }
37
+ if (Array.isArray(out.items)) {
38
+ out.prefixItems = out.items
39
+ if ('additionalItems' in out) {
40
+ out.items = out.additionalItems
41
+ delete out.additionalItems
42
+ } else {
43
+ delete out.items
44
+ }
45
+ }
46
+ return out
47
+ }
48
+
49
+ function convert(value: unknown): unknown {
50
+ if (Array.isArray(value)) return value.map(convert)
51
+ return isObject(value) ? convertSchema(value) : value
52
+ }
53
+
54
+ function isObject(value: unknown): value is Record<string, unknown> {
55
+ return !!value && typeof value === 'object' && !Array.isArray(value)
56
+ }
@@ -0,0 +1,40 @@
1
+ import * as z from 'zod'
2
+
3
+ /**
4
+ * Where the person is in the app, sent by the browser with every chat request
5
+ * and written into the system prompt, so "this project" needs no id. Browser
6
+ * and server share this schema; it is also the guard against prompt injection
7
+ * through the address bar: only a whitespace-free path, a lowercase entity
8
+ * name and a real uuid get in.
9
+ */
10
+ export const CHAT_CONTEXT_PATH_MAX = 200
11
+
12
+ export const chatContextSchema = z
13
+ .object({
14
+ path: z
15
+ .string()
16
+ .max(CHAT_CONTEXT_PATH_MAX)
17
+ .regex(/^\/\S*$/),
18
+ /** Set on a record's detail page (`/<entity>/<uuid>`, e.g. `/projects/<id>`): the entity name. */
19
+ entity: z
20
+ .string()
21
+ .regex(/^[a-z][a-z0-9_-]{0,63}$/)
22
+ .optional(),
23
+ /** The record's id on such a page. */
24
+ id: z.uuid().optional(),
25
+ })
26
+ .strict()
27
+
28
+ export type ChatContext = z.infer<typeof chatContextSchema>
29
+
30
+ const RECORD_PAGE = /^\/([^/]+)\/([^/]+)\/?$/
31
+
32
+ /** The context of a route path; entity and id only appear on a `/<entity>/<uuid>` page. */
33
+ export function chatContextFor(path: string): ChatContext {
34
+ const short = path.slice(0, CHAT_CONTEXT_PATH_MAX)
35
+ const [, entity, id] = RECORD_PAGE.exec(path) ?? []
36
+ const withRecord = chatContextSchema.safeParse({ path: short, entity, id })
37
+ if (withRecord.success && entity && id) return withRecord.data
38
+ const bare = chatContextSchema.safeParse({ path: short })
39
+ return bare.success ? bare.data : { path: '/' }
40
+ }