create-acrylblends 0.2.8 → 0.3.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.
- package/README.md +5 -4
- package/dist/index.d.ts +1 -3
- package/dist/index.js +20 -198
- package/dist/index.js.map +1 -1
- package/docs/framework/README.md +2 -2
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/getting-started/basic.md +1 -2
- package/docs/framework/getting-started/desktop.md +1 -1
- package/docs/framework/guides/bring-your-own-tool.md +216 -0
- package/docs/framework/guides/create-acrylblends.md +5 -4
- package/docs/framework/guides/data.md +2 -1
- package/docs/framework/upstream.json +3 -3
- package/package.json +2 -3
- package/templates/bun/gitignore +0 -2
- package/templates/bun/package.json +0 -19
- package/templates/bun/src/index.ts +0 -7
- package/templates/bun/test/app.test.ts +0 -8
- package/templates/bun/tsconfig.json +0 -15
- package/templates/chat/gitignore +0 -7
- package/templates/chat/migrations/001-chat.sql +0 -12
- package/templates/chat/package.json +0 -26
- package/templates/chat/src/app.ts +0 -8
- package/templates/chat/src/chat-app.ts +0 -100
- package/templates/chat/src/client/chat.tsx +0 -115
- package/templates/chat/src/client/tsconfig.json +0 -15
- package/templates/chat/src/db.ts +0 -53
- package/templates/chat/src/index.ts +0 -27
- package/templates/chat/src/llm.ts +0 -42
- package/templates/chat/src/page.tsx +0 -38
- package/templates/chat/test/chat.test.ts +0 -165
- package/templates/chat/test/server.test.ts +0 -59
- package/templates/chat/tsconfig.json +0 -19
- package/templates/nodejs/gitignore +0 -3
- package/templates/nodejs/package.json +0 -23
- package/templates/nodejs/src/app.ts +0 -10
- package/templates/nodejs/src/client/counter.tsx +0 -12
- package/templates/nodejs/src/client/tsconfig.json +0 -15
- package/templates/nodejs/src/index.ts +0 -21
- package/templates/nodejs/src/page.tsx +0 -26
- package/templates/nodejs/src/routes.ts +0 -10
- package/templates/nodejs/test/app.test.ts +0 -40
- package/templates/nodejs/test/server.test.ts +0 -48
- package/templates/nodejs/tsconfig.json +0 -19
- package/templates/notes/gitignore +0 -7
- package/templates/notes/migrations/001-notes.sql +0 -18
- package/templates/notes/package.json +0 -26
- package/templates/notes/src/app.ts +0 -8
- package/templates/notes/src/auth.ts +0 -57
- package/templates/notes/src/client/counter.tsx +0 -21
- package/templates/notes/src/client/tsconfig.json +0 -15
- package/templates/notes/src/db.ts +0 -60
- package/templates/notes/src/index.ts +0 -27
- package/templates/notes/src/notes-app.ts +0 -205
- package/templates/notes/src/schema.ts +0 -38
- package/templates/notes/src/views.tsx +0 -134
- package/templates/notes/test/notes.test.ts +0 -252
- package/templates/notes/test/server.test.ts +0 -59
- package/templates/notes/tsconfig.json +0 -19
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "__APP_NAME__",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"private": true,
|
|
5
|
-
"type": "module",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"dev": "bun run --hot src/index.ts",
|
|
8
|
-
"start": "bun run src/index.ts",
|
|
9
|
-
"test": "bun test",
|
|
10
|
-
"check": "tsc --noEmit"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"__PACKAGE__": "__RANGE__"
|
|
14
|
-
},
|
|
15
|
-
"devDependencies": {
|
|
16
|
-
"@types/bun": "latest",
|
|
17
|
-
"typescript": "6.0.3"
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2024",
|
|
4
|
-
"lib": ["ES2024", "DOM", "DOM.Iterable"],
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"moduleResolution": "Bundler",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"verbatimModuleSyntax": true,
|
|
9
|
-
"allowImportingTsExtensions": true,
|
|
10
|
-
"noEmit": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"types": ["bun"]
|
|
13
|
-
},
|
|
14
|
-
"include": ["src", "test"]
|
|
15
|
-
}
|
package/templates/chat/gitignore
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
CREATE TABLE conversations (
|
|
2
|
-
id INTEGER PRIMARY KEY,
|
|
3
|
-
title TEXT NOT NULL,
|
|
4
|
-
created_at TEXT NOT NULL
|
|
5
|
-
);
|
|
6
|
-
|
|
7
|
-
CREATE TABLE messages (
|
|
8
|
-
id INTEGER PRIMARY KEY,
|
|
9
|
-
conversation_id INTEGER NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
|
|
10
|
-
role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
|
|
11
|
-
content TEXT NOT NULL
|
|
12
|
-
);
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "__APP_NAME__",
|
|
3
|
-
"version": "0.1.0",
|
|
4
|
-
"private": true,
|
|
5
|
-
"type": "module",
|
|
6
|
-
"acryl": {
|
|
7
|
-
"opener": "src/db.ts"
|
|
8
|
-
},
|
|
9
|
-
"engines": {
|
|
10
|
-
"node": ">=22.19.0"
|
|
11
|
-
},
|
|
12
|
-
"scripts": {
|
|
13
|
-
"dev": "node --watch --import acrylblends/node-server/register src/index.ts",
|
|
14
|
-
"start": "node --import acrylblends/node-server/register src/index.ts",
|
|
15
|
-
"test": "acrylblends check-deps && node --import acrylblends/node-server/register --test \"test/**/*.test.{ts,tsx}\"",
|
|
16
|
-
"check": "tsc --noEmit && tsc --noEmit -p src/client && acrylblends check-deps",
|
|
17
|
-
"build": "acrylblends build-client"
|
|
18
|
-
},
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"__PACKAGE__": "__RANGE__",
|
|
21
|
-
"typescript": "6.0.3"
|
|
22
|
-
},
|
|
23
|
-
"devDependencies": {
|
|
24
|
-
"@types/node": "^24.0.0"
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { createApp } from './chat-app.ts'
|
|
2
|
-
import { openChatDb } from './db.ts'
|
|
3
|
-
|
|
4
|
-
/** The composition root: opens the database (once) and builds the app. `index.ts` serves it; the desktop shell does too, and calls `close` when it stops. */
|
|
5
|
-
const { db, close } = await openChatDb()
|
|
6
|
-
|
|
7
|
-
export { close }
|
|
8
|
-
export default createApp(db)
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { __CLASS__ } from '__PACKAGE__'
|
|
2
|
-
import type { Db } from '__PACKAGE__/db'
|
|
3
|
-
import { clientModules } from '__PACKAGE__/node-server'
|
|
4
|
-
import { type StandardSchema, schemaValidator } from '__PACKAGE__/schema-validator'
|
|
5
|
-
import { streamSSE } from '__PACKAGE__/streaming'
|
|
6
|
-
import { chatStore } from './db.ts'
|
|
7
|
-
import { type Llm, openAiCompatible } from './llm.ts'
|
|
8
|
-
import { chatPage } from './page.tsx'
|
|
9
|
-
|
|
10
|
-
interface ChatInput {
|
|
11
|
-
message: string
|
|
12
|
-
conversation?: number
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const ChatSchema: StandardSchema<ChatInput> = {
|
|
16
|
-
'~standard': {
|
|
17
|
-
validate: (value) => {
|
|
18
|
-
const input = (value ?? {}) as Record<string, unknown>
|
|
19
|
-
const message = typeof input.message === 'string' ? input.message.trim() : ''
|
|
20
|
-
const issues = []
|
|
21
|
-
if (message === '') issues.push({ message: 'Write a message.', path: ['message'] })
|
|
22
|
-
else if (message.length > 4000)
|
|
23
|
-
issues.push({ message: 'The message is at most 4000 characters.', path: ['message'] })
|
|
24
|
-
if (input.conversation !== undefined && !Number.isInteger(input.conversation))
|
|
25
|
-
issues.push({ message: 'The conversation is a number.', path: ['conversation'] })
|
|
26
|
-
return issues.length > 0
|
|
27
|
-
? { issues }
|
|
28
|
-
: { value: { message, conversation: input.conversation as number | undefined } }
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function createApp(db: Db, llm: Llm = openAiCompatible) {
|
|
34
|
-
const store = chatStore(db)
|
|
35
|
-
const app = new __CLASS__()
|
|
36
|
-
|
|
37
|
-
app.get('/', async (c) => {
|
|
38
|
-
const id = Number(c.req.query('c'))
|
|
39
|
-
const current = Number.isInteger(id) ? await store.conversation(id) : undefined
|
|
40
|
-
return c.html(
|
|
41
|
-
chatPage(
|
|
42
|
-
await store.conversations(),
|
|
43
|
-
current,
|
|
44
|
-
current ? await store.messages(current.id) : [],
|
|
45
|
-
),
|
|
46
|
-
)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
app.get('/api/conversations', async (c) => c.json({ conversations: await store.conversations() }))
|
|
50
|
-
app.get('/api/conversations/:id', async (c) => {
|
|
51
|
-
const id = Number(c.req.param('id'))
|
|
52
|
-
const conversation = await store.conversation(id)
|
|
53
|
-
return conversation
|
|
54
|
-
? c.json({ conversation, messages: await store.messages(id) })
|
|
55
|
-
: c.json({ error: 'No such conversation.' }, 404)
|
|
56
|
-
})
|
|
57
|
-
app.delete('/api/conversations/:id', async (c) =>
|
|
58
|
-
(await store.remove(Number(c.req.param('id'))))
|
|
59
|
-
? c.json({ ok: true })
|
|
60
|
-
: c.json({ error: 'No such conversation.' }, 404),
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
// The answer streams as Server-Sent Events: `meta` (the conversation id), then one `token` per piece, then `done`, or `error`.
|
|
64
|
-
// The request's signal aborts when the client goes away (the Stop button, a closed tab); it is passed to the model call so the upstream request stops too.
|
|
65
|
-
app.post('/api/chat', schemaValidator('json', ChatSchema), async (c) => {
|
|
66
|
-
const { message, conversation } = c.req.valid('json')
|
|
67
|
-
const known = conversation === undefined ? undefined : await store.conversation(conversation)
|
|
68
|
-
if (conversation !== undefined && !known) return c.json({ error: 'No such conversation.' }, 404)
|
|
69
|
-
const id = known?.id ?? (await store.start(message))
|
|
70
|
-
await store.add(id, 'user', message)
|
|
71
|
-
const history = (await store.messages(id)).map(({ role, content }) => ({ role, content }))
|
|
72
|
-
const signal = c.req.raw.signal
|
|
73
|
-
return streamSSE(c, async (stream) => {
|
|
74
|
-
let answer = ''
|
|
75
|
-
await stream.writeSSE({ event: 'meta', data: JSON.stringify({ conversation: id }) })
|
|
76
|
-
try {
|
|
77
|
-
for await (const piece of llm(history, signal)) {
|
|
78
|
-
answer += piece
|
|
79
|
-
await stream.writeSSE({ event: 'token', data: JSON.stringify(piece) })
|
|
80
|
-
}
|
|
81
|
-
await stream.writeSSE({ event: 'done', data: '{}' })
|
|
82
|
-
} catch (error) {
|
|
83
|
-
if (!signal.aborted) {
|
|
84
|
-
console.error(error)
|
|
85
|
-
// The reason stays in the log; the page gets a plain message.
|
|
86
|
-
await stream.writeSSE({
|
|
87
|
-
event: 'error',
|
|
88
|
-
data: JSON.stringify('The model could not answer. Try again.'),
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
} finally {
|
|
92
|
-
// What arrived is kept, also when the reader pressed Stop.
|
|
93
|
-
if (answer !== '') await store.add(id, 'assistant', answer)
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
app.route('/client', clientModules({ root: 'src/client', compiled: 'dist/client' }))
|
|
99
|
-
return app
|
|
100
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { render, useRef, useState } from '__PACKAGE__/jsx/dom'
|
|
2
|
-
|
|
3
|
-
/** Read a Server-Sent Events body: calls `on(event, data)` for each event as it arrives. (EventSource only does GET, and this is a POST.) */
|
|
4
|
-
async function readEvents(
|
|
5
|
-
body: ReadableStream<Uint8Array>,
|
|
6
|
-
on: (event: string, data: string) => void,
|
|
7
|
-
): Promise<void> {
|
|
8
|
-
const reader = body.getReader()
|
|
9
|
-
const decoder = new TextDecoder()
|
|
10
|
-
let buffer = ''
|
|
11
|
-
for (;;) {
|
|
12
|
-
const { done, value } = await reader.read()
|
|
13
|
-
if (done) return
|
|
14
|
-
// Line endings may be CRLF; a \r at the end of a chunk is completed by the next chunk, so normalise the whole buffer.
|
|
15
|
-
buffer = (buffer + decoder.decode(value, { stream: true })).replaceAll('\r\n', '\n')
|
|
16
|
-
const blocks = buffer.split('\n\n')
|
|
17
|
-
buffer = blocks.pop() ?? ''
|
|
18
|
-
for (const block of blocks) {
|
|
19
|
-
let event = 'message'
|
|
20
|
-
const data: string[] = []
|
|
21
|
-
for (const line of block.split('\n')) {
|
|
22
|
-
if (line.startsWith('event:')) event = line.slice(6).trim()
|
|
23
|
-
else if (line.startsWith('data:')) data.push(line.slice(5).replace(/^ /, '')) // the SSE spec strips one leading space only
|
|
24
|
-
}
|
|
25
|
-
on(event, data.join('\n'))
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const Chat = ({ start }: { start: number | undefined }) => {
|
|
31
|
-
const [text, setText] = useState('')
|
|
32
|
-
const [asked, setAsked] = useState('')
|
|
33
|
-
const [answer, setAnswer] = useState('')
|
|
34
|
-
const [error, setError] = useState('')
|
|
35
|
-
const [busy, setBusy] = useState(false)
|
|
36
|
-
const conversation = useRef<number | undefined>(start)
|
|
37
|
-
const controller = useRef<AbortController | undefined>(undefined)
|
|
38
|
-
|
|
39
|
-
const send = async (event: Event) => {
|
|
40
|
-
event.preventDefault()
|
|
41
|
-
const message = text.trim()
|
|
42
|
-
if (message === '' || busy) return
|
|
43
|
-
setAsked(message)
|
|
44
|
-
setAnswer('')
|
|
45
|
-
setError('')
|
|
46
|
-
setText('')
|
|
47
|
-
setBusy(true)
|
|
48
|
-
controller.current = new AbortController()
|
|
49
|
-
try {
|
|
50
|
-
const response = await fetch('/api/chat', {
|
|
51
|
-
method: 'POST',
|
|
52
|
-
headers: { 'content-type': 'application/json' },
|
|
53
|
-
body: JSON.stringify({ message, conversation: conversation.current }),
|
|
54
|
-
signal: controller.current.signal,
|
|
55
|
-
})
|
|
56
|
-
if (!response.ok || !response.body) {
|
|
57
|
-
const problem = (await response.json().catch(() => ({}))) as {
|
|
58
|
-
issues?: { message: string }[]
|
|
59
|
-
error?: string
|
|
60
|
-
}
|
|
61
|
-
setError(problem.issues?.[0]?.message ?? problem.error ?? 'The message was not accepted.')
|
|
62
|
-
return
|
|
63
|
-
}
|
|
64
|
-
let soFar = ''
|
|
65
|
-
await readEvents(response.body, (name, data) => {
|
|
66
|
-
if (name === 'meta') {
|
|
67
|
-
conversation.current = (JSON.parse(data) as { conversation: number }).conversation
|
|
68
|
-
history.replaceState(null, '', `/?c=${conversation.current}`)
|
|
69
|
-
} else if (name === 'token') {
|
|
70
|
-
soFar += JSON.parse(data) as string
|
|
71
|
-
setAnswer(soFar)
|
|
72
|
-
} else if (name === 'error') setError(JSON.parse(data) as string)
|
|
73
|
-
})
|
|
74
|
-
} catch (caught) {
|
|
75
|
-
if (!(caught instanceof DOMException && caught.name === 'AbortError'))
|
|
76
|
-
setError('The connection was lost.')
|
|
77
|
-
} finally {
|
|
78
|
-
setBusy(false)
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return (
|
|
83
|
-
<div>
|
|
84
|
-
{asked !== '' && <p class="row user">{asked}</p>}
|
|
85
|
-
{answer !== '' && <p class="row">{answer}</p>}
|
|
86
|
-
{error !== '' && (
|
|
87
|
-
<p class="row error" role="alert">
|
|
88
|
-
{error}
|
|
89
|
-
</p>
|
|
90
|
-
)}
|
|
91
|
-
<form onSubmit={send}>
|
|
92
|
-
<textarea
|
|
93
|
-
rows={3}
|
|
94
|
-
value={text}
|
|
95
|
-
placeholder="Ask something"
|
|
96
|
-
onInput={(e) => setText((e.target as HTMLTextAreaElement).value)}
|
|
97
|
-
/>
|
|
98
|
-
<p>
|
|
99
|
-
<button type="submit" disabled={busy}>
|
|
100
|
-
Send
|
|
101
|
-
</button>{' '}
|
|
102
|
-
{busy && (
|
|
103
|
-
<button type="button" onClick={() => controller.current?.abort()}>
|
|
104
|
-
Stop
|
|
105
|
-
</button>
|
|
106
|
-
)}
|
|
107
|
-
</p>
|
|
108
|
-
</form>
|
|
109
|
-
</div>
|
|
110
|
-
)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const root = document.getElementById('chat')!
|
|
114
|
-
const start = root.dataset.conversation ? Number(root.dataset.conversation) : undefined
|
|
115
|
-
render(<Chat start={start} />, root)
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2024",
|
|
4
|
-
"lib": ["ES2024", "DOM", "DOM.Iterable"],
|
|
5
|
-
"module": "NodeNext",
|
|
6
|
-
"moduleResolution": "NodeNext",
|
|
7
|
-
"strict": true,
|
|
8
|
-
"noEmit": true,
|
|
9
|
-
"allowImportingTsExtensions": true,
|
|
10
|
-
"jsx": "react-jsx",
|
|
11
|
-
"jsxImportSource": "__PACKAGE__/jsx/dom",
|
|
12
|
-
"types": []
|
|
13
|
-
},
|
|
14
|
-
"include": ["**/*.ts", "**/*.tsx"]
|
|
15
|
-
}
|
package/templates/chat/src/db.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import type { Db } from '__PACKAGE__/db'
|
|
2
|
-
import { openDb } from '__PACKAGE__/db/sqlite'
|
|
3
|
-
import { fileURLToPath } from 'node:url'
|
|
4
|
-
|
|
5
|
-
export interface Message {
|
|
6
|
-
id: number
|
|
7
|
-
conversation_id: number
|
|
8
|
-
role: 'user' | 'assistant'
|
|
9
|
-
content: string
|
|
10
|
-
}
|
|
11
|
-
export interface Conversation {
|
|
12
|
-
id: number
|
|
13
|
-
title: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** The folder of numbered SQL files (001-chat.sql, ...). Applied on start, each once, in one transaction. */
|
|
17
|
-
const MIGRATIONS = fileURLToPath(new URL('../migrations', import.meta.url))
|
|
18
|
-
|
|
19
|
-
/** Open the chat database and apply the migrations. Call `close` once, when the process stops. */
|
|
20
|
-
export function openChatDb(path = process.env.CHAT_DB ?? 'data/chat.sqlite') {
|
|
21
|
-
return openDb({ path, migrations: MIGRATIONS })
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function chatStore(db: Db) {
|
|
25
|
-
return {
|
|
26
|
-
conversations: (): Promise<Conversation[]> =>
|
|
27
|
-
db.all<Conversation>('SELECT id, title FROM conversations ORDER BY id DESC'),
|
|
28
|
-
conversation: (id: number): Promise<Conversation | undefined> =>
|
|
29
|
-
db.get<Conversation>('SELECT id, title FROM conversations WHERE id = ?', id),
|
|
30
|
-
messages: (id: number): Promise<Message[]> =>
|
|
31
|
-
db.all<Message>('SELECT * FROM messages WHERE conversation_id = ? ORDER BY id', id),
|
|
32
|
-
async start(title: string): Promise<number> {
|
|
33
|
-
const row = await db.get<{ id: number }>(
|
|
34
|
-
'INSERT INTO conversations (title, created_at) VALUES (?, ?) RETURNING id',
|
|
35
|
-
title.slice(0, 60),
|
|
36
|
-
new Date().toISOString(),
|
|
37
|
-
)
|
|
38
|
-
return row!.id
|
|
39
|
-
},
|
|
40
|
-
async add(conversationId: number, role: Message['role'], content: string): Promise<void> {
|
|
41
|
-
await db.run(
|
|
42
|
-
'INSERT INTO messages (conversation_id, role, content) VALUES (?, ?, ?)',
|
|
43
|
-
conversationId,
|
|
44
|
-
role,
|
|
45
|
-
content,
|
|
46
|
-
)
|
|
47
|
-
},
|
|
48
|
-
async remove(id: number): Promise<boolean> {
|
|
49
|
-
const { changes } = await db.run('DELETE FROM conversations WHERE id = ?', id)
|
|
50
|
-
return changes > 0
|
|
51
|
-
},
|
|
52
|
-
}
|
|
53
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { serve } from '__PACKAGE__/node-server'
|
|
2
|
-
import app, { close } from './app.ts'
|
|
3
|
-
|
|
4
|
-
const port = Number(process.env.PORT || 3000)
|
|
5
|
-
// Local by default. Behind a reverse proxy on the same machine this is right; in a container set HOST=0.0.0.0.
|
|
6
|
-
const hostname = process.env.HOST || '127.0.0.1'
|
|
7
|
-
|
|
8
|
-
const server = serve({ fetch: app.fetch, port, hostname }, (info) => {
|
|
9
|
-
console.log(`listening on http://${info.address}:${info.port}`)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
// Stop taking requests, let the ones in flight finish, then close the database and exit. A connection that never ends cannot keep the process alive past 10 seconds.
|
|
13
|
-
function shutdown(): void {
|
|
14
|
-
server.close(() => {
|
|
15
|
-
close().then(
|
|
16
|
-
() => process.exit(0),
|
|
17
|
-
(error) => {
|
|
18
|
-
console.error(error)
|
|
19
|
-
process.exit(1)
|
|
20
|
-
},
|
|
21
|
-
)
|
|
22
|
-
})
|
|
23
|
-
server.closeIdleConnections()
|
|
24
|
-
setTimeout(() => process.exit(1), 10_000).unref()
|
|
25
|
-
}
|
|
26
|
-
process.once('SIGINT', shutdown)
|
|
27
|
-
process.once('SIGTERM', shutdown)
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/** What the app needs from a language model: the conversation so far in, the answer out piece by piece. Aborting the signal must stop the work. */
|
|
2
|
-
export type Llm = (
|
|
3
|
-
messages: { role: string; content: string }[],
|
|
4
|
-
signal: AbortSignal,
|
|
5
|
-
) => AsyncIterable<string>
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An OpenAI-compatible chat completions API with `stream: true` (DeepSeek by default). The key is read from DEEPSEEK_API_KEY at the moment of the call and
|
|
9
|
-
* is never stored; DEEPSEEK_BASE_URL and DEEPSEEK_MODEL choose the service.
|
|
10
|
-
*/
|
|
11
|
-
export const openAiCompatible: Llm = async function* (messages, signal) {
|
|
12
|
-
const key = process.env.DEEPSEEK_API_KEY
|
|
13
|
-
if (!key) throw new Error('DEEPSEEK_API_KEY is not set')
|
|
14
|
-
const base = (process.env.DEEPSEEK_BASE_URL ?? 'https://api.deepseek.com').replace(/\/$/, '')
|
|
15
|
-
const response = await fetch(`${base}/chat/completions`, {
|
|
16
|
-
method: 'POST',
|
|
17
|
-
headers: { 'content-type': 'application/json', authorization: `Bearer ${key}` },
|
|
18
|
-
body: JSON.stringify({
|
|
19
|
-
model: process.env.DEEPSEEK_MODEL ?? 'deepseek-chat',
|
|
20
|
-
messages,
|
|
21
|
-
stream: true,
|
|
22
|
-
}),
|
|
23
|
-
signal,
|
|
24
|
-
})
|
|
25
|
-
if (!response.ok || !response.body)
|
|
26
|
-
throw new Error(`the model service answered ${response.status}`)
|
|
27
|
-
const decoder = new TextDecoder()
|
|
28
|
-
let buffer = ''
|
|
29
|
-
for await (const chunk of response.body) {
|
|
30
|
-
buffer += decoder.decode(chunk, { stream: true })
|
|
31
|
-
const lines = buffer.split('\n')
|
|
32
|
-
buffer = lines.pop() ?? ''
|
|
33
|
-
for (const line of lines) {
|
|
34
|
-
if (!line.startsWith('data:')) continue
|
|
35
|
-
const data = line.slice(5).trim()
|
|
36
|
-
if (data === '[DONE]') return
|
|
37
|
-
const piece = (JSON.parse(data) as { choices?: { delta?: { content?: string } }[] })
|
|
38
|
-
.choices?.[0]?.delta?.content
|
|
39
|
-
if (piece) yield piece
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import type { Conversation, Message } from './db.ts'
|
|
2
|
-
|
|
3
|
-
export const chatPage = (
|
|
4
|
-
conversations: Conversation[],
|
|
5
|
-
current: Conversation | undefined,
|
|
6
|
-
messages: Message[],
|
|
7
|
-
): string =>
|
|
8
|
-
(
|
|
9
|
-
<html lang="en">
|
|
10
|
-
<head>
|
|
11
|
-
<meta charset="utf-8" />
|
|
12
|
-
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
13
|
-
<title>Chat</title>
|
|
14
|
-
<style
|
|
15
|
-
dangerouslySetInnerHTML={{
|
|
16
|
-
__html:
|
|
17
|
-
'body{font:16px system-ui;max-width:48rem;margin:2rem auto;padding:0 1rem}.row{white-space:pre-wrap;margin:.75rem 0}.user{font-weight:600}.error{color:#b00020}textarea{width:100%;box-sizing:border-box;padding:.5rem}nav a{margin-right:.75rem}',
|
|
18
|
-
}}
|
|
19
|
-
/>
|
|
20
|
-
</head>
|
|
21
|
-
<body>
|
|
22
|
-
<h1>Chat</h1>
|
|
23
|
-
<nav>
|
|
24
|
-
<a href="/">New chat</a>
|
|
25
|
-
{conversations.map((c) => (
|
|
26
|
-
<a href={`/?c=${c.id}`}>{c.title}</a>
|
|
27
|
-
))}
|
|
28
|
-
</nav>
|
|
29
|
-
<div id="log">
|
|
30
|
-
{messages.map((m) => (
|
|
31
|
-
<p class={`row ${m.role}`}>{m.content}</p>
|
|
32
|
-
))}
|
|
33
|
-
</div>
|
|
34
|
-
<div id="chat" data-conversation={current ? String(current.id) : ''} />
|
|
35
|
-
<script type="module" src="/client/chat.tsx" />
|
|
36
|
-
</body>
|
|
37
|
-
</html>
|
|
38
|
-
).toString()
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import type { Db } from '__PACKAGE__/db'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import { after, test } from 'node:test'
|
|
4
|
-
import { createApp } from '../src/chat-app.ts'
|
|
5
|
-
import { openChatDb } from '../src/db.ts'
|
|
6
|
-
import type { Llm } from '../src/llm.ts'
|
|
7
|
-
|
|
8
|
-
/** A fresh database in memory with the real migrations; every one is closed when the tests end. */
|
|
9
|
-
const closers: (() => Promise<void>)[] = []
|
|
10
|
-
after(() => Promise.all(closers.map((close) => close())))
|
|
11
|
-
async function memoryDb(): Promise<Db> {
|
|
12
|
-
const { db, close } = await openChatDb(':memory:')
|
|
13
|
-
closers.push(close)
|
|
14
|
-
return db
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** A model that answers with fixed pieces and records what it was asked. */
|
|
18
|
-
function mock(pieces: string[]) {
|
|
19
|
-
const asked: { role: string; content: string }[][] = []
|
|
20
|
-
const llm: Llm = async function* (messages) {
|
|
21
|
-
asked.push(messages)
|
|
22
|
-
for (const piece of pieces) yield piece
|
|
23
|
-
}
|
|
24
|
-
return { llm, asked }
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const post = (body: unknown): RequestInit => ({
|
|
28
|
-
method: 'POST',
|
|
29
|
-
headers: { 'content-type': 'application/json' },
|
|
30
|
-
body: JSON.stringify(body),
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
/** Parse a Server-Sent Events body into [event, data] pairs. */
|
|
34
|
-
function events(text: string): [string, string][] {
|
|
35
|
-
return text
|
|
36
|
-
.split('\n\n')
|
|
37
|
-
.filter(Boolean)
|
|
38
|
-
.map((block) => {
|
|
39
|
-
const event = /^event: (.*)$/m.exec(block)?.[1] ?? 'message'
|
|
40
|
-
const data = /^data: (.*)$/m.exec(block)?.[1] ?? ''
|
|
41
|
-
return [event, data]
|
|
42
|
-
})
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
test('the answer streams as events: meta, one token per piece, done', async () => {
|
|
46
|
-
const { llm } = mock(['Hel', 'lo ', 'there\nline'])
|
|
47
|
-
const app = createApp(await memoryDb(), llm)
|
|
48
|
-
const res = await app.request('/api/chat', post({ message: 'hi' }))
|
|
49
|
-
assert.equal(res.status, 200)
|
|
50
|
-
assert.match(res.headers.get('content-type') ?? '', /text\/event-stream/)
|
|
51
|
-
const got = events(await res.text())
|
|
52
|
-
assert.deepEqual(
|
|
53
|
-
got.map(([e]) => e),
|
|
54
|
-
['meta', 'token', 'token', 'token', 'done'],
|
|
55
|
-
)
|
|
56
|
-
assert.equal(
|
|
57
|
-
got
|
|
58
|
-
.filter(([e]) => e === 'token')
|
|
59
|
-
.map(([, d]) => JSON.parse(d) as string)
|
|
60
|
-
.join(''),
|
|
61
|
-
'Hello there\nline',
|
|
62
|
-
)
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
test('history is stored, sent back to the model, and listed', async () => {
|
|
66
|
-
const { llm, asked } = mock(['ok'])
|
|
67
|
-
const app = createApp(await memoryDb(), llm)
|
|
68
|
-
const first = events(
|
|
69
|
-
await (await app.request('/api/chat', post({ message: 'first question' }))).text(),
|
|
70
|
-
)
|
|
71
|
-
const id = (JSON.parse(first[0]![1]) as { conversation: number }).conversation
|
|
72
|
-
await (await app.request('/api/chat', post({ message: 'second', conversation: id }))).text()
|
|
73
|
-
assert.deepEqual(
|
|
74
|
-
asked[1]?.map((m) => m.content),
|
|
75
|
-
['first question', 'ok', 'second'],
|
|
76
|
-
)
|
|
77
|
-
const one = (await (await app.request(`/api/conversations/${id}`)).json()) as {
|
|
78
|
-
messages: { role: string }[]
|
|
79
|
-
}
|
|
80
|
-
assert.deepEqual(
|
|
81
|
-
one.messages.map((m) => m.role),
|
|
82
|
-
['user', 'assistant', 'user', 'assistant'],
|
|
83
|
-
)
|
|
84
|
-
const list = (await (await app.request('/api/conversations')).json()) as {
|
|
85
|
-
conversations: { title: string }[]
|
|
86
|
-
}
|
|
87
|
-
assert.equal(list.conversations[0]?.title, 'first question')
|
|
88
|
-
const page = await (await app.request(`/?c=${id}`)).text()
|
|
89
|
-
assert.match(page, /first question/)
|
|
90
|
-
assert.match(page, /src="\/client\/chat\.tsx"/)
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test('a bad message answers 400 with a message, an unknown conversation 404, and nothing reaches the model', async () => {
|
|
94
|
-
const { llm, asked } = mock(['x'])
|
|
95
|
-
const app = createApp(await memoryDb(), llm)
|
|
96
|
-
const empty = await app.request('/api/chat', post({ message: ' ' }))
|
|
97
|
-
assert.equal(empty.status, 400)
|
|
98
|
-
assert.match(JSON.stringify(await empty.json()), /Write a message/)
|
|
99
|
-
assert.equal(
|
|
100
|
-
(await app.request('/api/chat', post({ message: 'hi', conversation: 99 }))).status,
|
|
101
|
-
404,
|
|
102
|
-
)
|
|
103
|
-
assert.equal(asked.length, 0)
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
test('a conversation can be deleted', async () => {
|
|
107
|
-
const app = createApp(await memoryDb(), mock(['a']).llm)
|
|
108
|
-
const first = events(await (await app.request('/api/chat', post({ message: 'hi' }))).text())
|
|
109
|
-
const id = (JSON.parse(first[0]![1]) as { conversation: number }).conversation
|
|
110
|
-
assert.equal((await app.request(`/api/conversations/${id}`, { method: 'DELETE' })).status, 200)
|
|
111
|
-
assert.equal((await app.request(`/api/conversations/${id}`)).status, 404)
|
|
112
|
-
assert.equal((await app.request(`/api/conversations/${id}`, { method: 'DELETE' })).status, 404)
|
|
113
|
-
})
|
|
114
|
-
|
|
115
|
-
test('a failing model sends an error event with a plain message and no detail', async () => {
|
|
116
|
-
const llm: Llm = async function* () {
|
|
117
|
-
yield 'partial '
|
|
118
|
-
throw new Error('upstream secret detail')
|
|
119
|
-
}
|
|
120
|
-
const app = createApp(await memoryDb(), llm)
|
|
121
|
-
const text = await (await app.request('/api/chat', post({ message: 'hi' }))).text()
|
|
122
|
-
assert.doesNotMatch(text, /secret detail/)
|
|
123
|
-
const last = events(text).at(-1)
|
|
124
|
-
assert.equal(last?.[0], 'error')
|
|
125
|
-
assert.match(last?.[1] ?? '', /could not answer/)
|
|
126
|
-
})
|
|
127
|
-
|
|
128
|
-
test('when the client goes away the model call is aborted and what arrived is kept', async () => {
|
|
129
|
-
let sawAbort: (value: boolean) => void = () => {}
|
|
130
|
-
const aborted = new Promise<boolean>((resolve) => {
|
|
131
|
-
sawAbort = resolve
|
|
132
|
-
})
|
|
133
|
-
const llm: Llm = async function* (_messages, signal) {
|
|
134
|
-
yield 'first '
|
|
135
|
-
// The signal may already be aborted by the time the model is asked for more: an abort event that has fired does not fire again.
|
|
136
|
-
if (!signal.aborted)
|
|
137
|
-
await new Promise<void>((resolve) =>
|
|
138
|
-
signal.addEventListener('abort', () => resolve(), { once: true }),
|
|
139
|
-
)
|
|
140
|
-
sawAbort(signal.aborted)
|
|
141
|
-
if (signal.aborted) throw new DOMException('aborted', 'AbortError')
|
|
142
|
-
yield 'never sent'
|
|
143
|
-
}
|
|
144
|
-
const db = await memoryDb()
|
|
145
|
-
const app = createApp(db, llm)
|
|
146
|
-
const controller = new AbortController()
|
|
147
|
-
const res = await app.request('/api/chat', {
|
|
148
|
-
...post({ message: 'hi' }),
|
|
149
|
-
signal: controller.signal,
|
|
150
|
-
})
|
|
151
|
-
const reader = res.body!.getReader()
|
|
152
|
-
const decoder = new TextDecoder()
|
|
153
|
-
let seen = ''
|
|
154
|
-
while (!seen.includes('first')) seen += decoder.decode((await reader.read()).value)
|
|
155
|
-
controller.abort()
|
|
156
|
-
assert.equal(await aborted, true)
|
|
157
|
-
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
158
|
-
const rows = await db.all<{ role: string; content: string }>(
|
|
159
|
-
'SELECT role, content FROM messages ORDER BY id',
|
|
160
|
-
)
|
|
161
|
-
assert.deepEqual(
|
|
162
|
-
rows.map((r) => `${r.role}:${r.content}`),
|
|
163
|
-
['user:hi', 'assistant:first '],
|
|
164
|
-
)
|
|
165
|
-
})
|