acrylblends 1.3.1 → 1.3.2
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/legacy/README.md +4 -1
- package/legacy/agent/858de7a1ad0b/src/app.ts +289 -0
- package/legacy/agent/858de7a1ad0b/src/routes.ts +145 -0
- package/legacy/agent/b96ba6e3598a/src/app.ts +285 -0
- package/legacy/agent/b96ba6e3598a/src/routes.ts +145 -0
- package/legacy/agent/ecbc84f1c1e5/src/app.ts +291 -0
- package/legacy/agent/ecbc84f1c1e5/src/routes.ts +145 -0
- package/legacy/blank/2447ac6d5d24/src/app.ts +61 -0
- package/legacy/blank/387f7e25aaeb/src/app.ts +81 -0
- package/legacy/blank/5b9f10e1ba5e/src/app.ts +61 -0
- package/legacy/blank/5f6dd8910c50/src/app.ts +152 -0
- package/legacy/blank/5f6dd8910c50/src/routes.ts +75 -0
- package/legacy/blank/a7e8f539ffee/src/app.ts +160 -0
- package/legacy/blank/a7e8f539ffee/src/routes.ts +75 -0
- package/legacy/blank/bdb9735e013b/src/app.ts +61 -0
- package/legacy/blank/c3be1d735b1a/src/app.ts +61 -0
- package/legacy/blank/c3be1d735b1a/src/routes.ts +18 -0
- package/legacy/blank/e04303a7f8dd/src/app.ts +61 -0
- package/legacy/chat/5120c4a9fa61/src/app.ts +157 -0
- package/legacy/chat/5120c4a9fa61/src/routes.ts +29 -0
- package/legacy/chat/d4464dfb3b04/src/app.ts +165 -0
- package/legacy/chat/d4464dfb3b04/src/routes.ts +29 -0
- package/legacy/frozen.json +7 -0
- package/legacy/layouts.json +135 -45
- package/legacy/nodejs/9d59115118c7/src/app.ts +150 -0
- package/legacy/nodejs/9d59115118c7/src/routes.ts +59 -0
- package/legacy/nodejs/a6a340b4d3a6/src/app.ts +158 -0
- package/legacy/nodejs/a6a340b4d3a6/src/routes.ts +59 -0
- package/legacy/notes/fb43a1c5f3b4/src/app.ts +165 -0
- package/legacy/notes/fb43a1c5f3b4/src/routes.ts +21 -0
- package/legacy/notes/fc3fe7c749d2/src/app.ts +157 -0
- package/legacy/notes/fc3fe7c749d2/src/routes.ts +21 -0
- package/legacy/todo/164274ffcf7f/src/app.ts +145 -0
- package/legacy/todo/164274ffcf7f/src/todo.ts +137 -0
- package/package.json +1 -1
- /package/legacy/blank/{d6d371c9a65e → 2447ac6d5d24}/src/routes.ts +0 -0
- /package/legacy/blank/{5a91c558af22 → 387f7e25aaeb}/src/routes.ts +0 -0
- /package/legacy/blank/{b2cbb759c889 → 5b9f10e1ba5e}/src/routes.ts +0 -0
- /package/legacy/blank/{9c650cf52bf3 → bdb9735e013b}/src/routes.ts +0 -0
- /package/legacy/blank/{aea589283445 → e04303a7f8dd}/src/routes.ts +0 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { fileURLToPath } from 'node:url'
|
|
3
|
+
import { Context, type Fiber } from '@deepseek-ai/cordis'
|
|
4
|
+
import * as door from '../plugins/acryl-door/src/index.ts'
|
|
5
|
+
import * as nodeServer from '../plugins/acryl-node-server/src/index.ts'
|
|
6
|
+
import * as router from '../plugins/acryl-router/src/index.ts'
|
|
7
|
+
import * as state from '../plugins/acryl-state/src/index.ts'
|
|
8
|
+
import { type AppInstance, instance } from '../plugins/instance/index.ts'
|
|
9
|
+
import {
|
|
10
|
+
appPluginNames,
|
|
11
|
+
blueprintsIn,
|
|
12
|
+
compose,
|
|
13
|
+
loadAppPlugins,
|
|
14
|
+
loadManifest,
|
|
15
|
+
type ParameterValue,
|
|
16
|
+
} from '../plugins/manifest/index.ts'
|
|
17
|
+
import { doorEnabled } from './door.ts'
|
|
18
|
+
import * as todo from './todo.ts'
|
|
19
|
+
import { FRAME_MOUNT_PREFIX } from './todo.ts'
|
|
20
|
+
|
|
21
|
+
/** The plugins this app's code provides. `blend.yaml` chooses, orders and configures them, and adds the app's own plugins from `plugins/` by name. */
|
|
22
|
+
export const registry = {
|
|
23
|
+
'acryl-router': router,
|
|
24
|
+
'acryl-state': state,
|
|
25
|
+
todo,
|
|
26
|
+
'acryl-node-server': nodeServer,
|
|
27
|
+
'acryl-door': door,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const APP_NAME = '__APP_NAME__'
|
|
31
|
+
|
|
32
|
+
/** The manifest next to this app's source. */
|
|
33
|
+
export const MANIFEST = join(fileURLToPath(new URL('..', import.meta.url)), 'blend.yaml')
|
|
34
|
+
/** The app's plugins: the library copies `registry` imports, and the app's own plugins (a feature each), which `blend.yaml` mounts by their folder name. */
|
|
35
|
+
const PLUGINS_DIR = join(fileURLToPath(new URL('..', import.meta.url)), 'plugins')
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The app's manifest, resolved. A Blend finds its Blueprint in `blueprints/<id>.yaml`; an app that is itself a Blueprint never looks.
|
|
39
|
+
* `createApp` and the tests read it the same way.
|
|
40
|
+
*/
|
|
41
|
+
export function readManifest(values?: Readonly<Record<string, ParameterValue>>) {
|
|
42
|
+
return loadManifest(MANIFEST, {
|
|
43
|
+
known: [...Object.keys(registry), ...appPluginNames(PLUGINS_DIR)],
|
|
44
|
+
getBlueprint: blueprintsIn(join(fileURLToPath(new URL('..', import.meta.url)), 'blueprints')),
|
|
45
|
+
...(values ? { values } : {}),
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The manifest without some rows: without its server (and so its door) the app is reached in process, through `root.router.request`; the door is also left out in production. */
|
|
50
|
+
function without<M extends { plugins: readonly { use: string }[] }>(
|
|
51
|
+
manifest: M,
|
|
52
|
+
...uses: string[]
|
|
53
|
+
): M {
|
|
54
|
+
return {
|
|
55
|
+
...manifest,
|
|
56
|
+
plugins: manifest.plugins.map((row) =>
|
|
57
|
+
uses.includes(row.use) ? { ...row, disabled: true } : row,
|
|
58
|
+
),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface App {
|
|
63
|
+
readonly root: Context
|
|
64
|
+
/** The address the server listens on; empty when the app was made with `listen: false`. */
|
|
65
|
+
readonly url: string
|
|
66
|
+
stop(): Promise<void>
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface AppOptions {
|
|
70
|
+
readonly values?: Readonly<Record<string, ParameterValue>>
|
|
71
|
+
/** The path the app is mounted at; absent on its own server, where it keeps `/todo`, and {@link FRAME_MOUNT_PREFIX} in the frame. */
|
|
72
|
+
readonly prefix?: string
|
|
73
|
+
/** Start the HTTP server (default). Tests pass `false` and call `app.root.router.request(path)`: it needs no port, so it also runs where a sandbox forbids listening. */
|
|
74
|
+
readonly listen?: boolean
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The rows the frame host provides: the app contributes only the plugins that are its own. */
|
|
78
|
+
const HOST_ROWS = [
|
|
79
|
+
'instance',
|
|
80
|
+
'acryl-router',
|
|
81
|
+
'acryl-node-server',
|
|
82
|
+
'acryl-web-server',
|
|
83
|
+
'acryl-page',
|
|
84
|
+
'manifest',
|
|
85
|
+
'save',
|
|
86
|
+
'acryl-door',
|
|
87
|
+
]
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The app's rows, composed into `ctx`: the app's own server calls this from `createApp`, and the generated frame layer mounts it as one row, so the frame
|
|
91
|
+
* runs the app exactly as the app's own server does (same rows, same order, same config). `host: true` leaves the infrastructure rows to the frame host,
|
|
92
|
+
* which is where the door lives too, and mounts the app at {@link FRAME_MOUNT_PREFIX}.
|
|
93
|
+
*/
|
|
94
|
+
export async function composeInto(
|
|
95
|
+
ctx: Context,
|
|
96
|
+
chosen: AppInstance,
|
|
97
|
+
options: AppOptions & { readonly host?: boolean } = {},
|
|
98
|
+
): Promise<Fiber[]> {
|
|
99
|
+
const read = readManifest(options.values)
|
|
100
|
+
const own = options.host !== true && options.listen !== false
|
|
101
|
+
// The frame host mounts the app under its own prefix; the app's own server keeps the app's old path.
|
|
102
|
+
const prefix = options.prefix ?? (options.host === true ? FRAME_MOUNT_PREFIX : undefined)
|
|
103
|
+
const leaveOut = new Set<string>(options.host === true ? HOST_ROWS : [])
|
|
104
|
+
// The server, and the door that needs it, are not mounted when the app is reached in process or inside the frame host.
|
|
105
|
+
if (!own) leaveOut.add('acryl-node-server').add('acryl-door')
|
|
106
|
+
if (!doorEnabled()) leaveOut.add('acryl-door')
|
|
107
|
+
const manifest = without(read, ...leaveOut)
|
|
108
|
+
// The rows that name one of the app's own plugin folders (a feature: plugins/<name>/ and its row) rather than a registry import; each one that runs is imported here.
|
|
109
|
+
const ownRows = [
|
|
110
|
+
...new Set(read.plugins.map((row) => row.use).filter((use) => !Object.hasOwn(registry, use))),
|
|
111
|
+
]
|
|
112
|
+
const ownPlugins = await loadAppPlugins(
|
|
113
|
+
PLUGINS_DIR,
|
|
114
|
+
ownRows.filter((use) => manifest.plugins.some((row) => row.use === use && !row.disabled)),
|
|
115
|
+
)
|
|
116
|
+
return compose(
|
|
117
|
+
ctx,
|
|
118
|
+
manifest,
|
|
119
|
+
{ ...registry, ...ownPlugins },
|
|
120
|
+
{
|
|
121
|
+
// Runtime values the manifest cannot hold: where the list is kept (in the app's home) and, in the frame, the prefix the app answers at.
|
|
122
|
+
todo: {
|
|
123
|
+
file: join(chosen.home, 'todo.json'),
|
|
124
|
+
...(prefix === undefined ? {} : { prefix }),
|
|
125
|
+
},
|
|
126
|
+
// Only the app's own server listens; the frame host owns the server and its port.
|
|
127
|
+
...(own ? { server: { port: chosen.port.start, scan: chosen.port.scan ? 20 : 0 } } : {}),
|
|
128
|
+
},
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The app: `instance` first (where it lives, chosen at run time), then the manifest's rows in order. */
|
|
133
|
+
export async function createApp(chosen: AppInstance, options: AppOptions = {}): Promise<App> {
|
|
134
|
+
const root = new Context()
|
|
135
|
+
const first = root.plugin(instance, chosen)
|
|
136
|
+
await first.await()
|
|
137
|
+
const fibers: Fiber[] = [first, ...(await composeInto(root, chosen, options))]
|
|
138
|
+
return {
|
|
139
|
+
root,
|
|
140
|
+
url: options.listen === false ? '' : root.nodeServer.url,
|
|
141
|
+
async stop() {
|
|
142
|
+
for (const fiber of [...fibers].reverse()) await fiber.dispose()
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reference app of the ACRYL Framework: a todo list in one plugin. It needs `router` and `state`, keeps the list in a cell, serves
|
|
3
|
+
* JSON CRUD plus a page, and writes the list to a file so it survives a restart. Nothing else.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
7
|
+
import { dirname } from 'node:path'
|
|
8
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
9
|
+
import { Acryl, HTTPException } from '../plugins/acryl-router/src/index.ts'
|
|
10
|
+
import { bodyLimit, loopbackOnly, trimTrailingSlash } from '../plugins/acryl-router/src/middleware/index.ts'
|
|
11
|
+
import { schemaValidator, streamSSE, type StandardSchema } from '../plugins/acryl-router/src/helpers/index.ts'
|
|
12
|
+
import type {} from '../plugins/acryl-state/src/index.ts'
|
|
13
|
+
import { pageHtml } from './page.ts'
|
|
14
|
+
|
|
15
|
+
/** Stable Cordis plugin name, equal to the package name and the Loader row id. */
|
|
16
|
+
export const name = 'acryl-todo'
|
|
17
|
+
export const inject = ['router', 'state']
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The only place the app's mount prefix is named: the app answers under `/todo` on its own server (`npm start`), and the ACRYL frame mounts the same app
|
|
21
|
+
* at `/app` (`npm run dev`). Every URL the page writes is derived from the prefix it is mounted at, so both are served by one source.
|
|
22
|
+
*/
|
|
23
|
+
export const FRAME_MOUNT_PREFIX = '/app'
|
|
24
|
+
|
|
25
|
+
export interface Todo { id: number, title: string, done: boolean }
|
|
26
|
+
export interface Config {
|
|
27
|
+
file?: string
|
|
28
|
+
/**
|
|
29
|
+
* The path the app is mounted at, and the base of every URL its page writes. `/todo` on the app's own server (and the default); the ACRYL frame passes
|
|
30
|
+
* `/app` so the same app answers at the frame's app prefix, with nothing else of its URLs moved.
|
|
31
|
+
*/
|
|
32
|
+
prefix?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Standard Schema for the body of a new todo, written by hand so the example has no schema library. */
|
|
36
|
+
const NewTodo: StandardSchema<{ title: string }> = {
|
|
37
|
+
'~standard': {
|
|
38
|
+
validate: value => {
|
|
39
|
+
const title = (value as { title?: unknown } | null)?.title
|
|
40
|
+
return typeof title === 'string' && title.trim() !== '' && title.length <= 200
|
|
41
|
+
? { value: { title: title.trim() } }
|
|
42
|
+
: { issues: [{ message: 'title must be 1 to 200 characters', path: ['title'] }] }
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Standard Schema for a change to a todo. */
|
|
48
|
+
const TodoChange: StandardSchema<{ done: boolean }> = {
|
|
49
|
+
'~standard': {
|
|
50
|
+
validate: value => {
|
|
51
|
+
const done = (value as { done?: unknown } | null)?.done
|
|
52
|
+
return typeof done === 'boolean' ? { value: { done } } : { issues: [{ message: 'done must be true or false', path: ['done'] }] }
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A todo body is small; anything bigger is refused before it is read. */
|
|
58
|
+
const MAX_BODY_BYTES = 16 * 1024
|
|
59
|
+
|
|
60
|
+
/** A missing file is a new list. Any other failure (unreadable, corrupt, half-written) refuses to start: starting empty would overwrite the user's real list on the first save. */
|
|
61
|
+
function load(file: string | undefined): Todo[] {
|
|
62
|
+
if (file === undefined) return []
|
|
63
|
+
let text: string
|
|
64
|
+
try {
|
|
65
|
+
text = readFileSync(file, 'utf8')
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return []
|
|
68
|
+
throw new Error(`acryl-todo: cannot read ${file}: ${(error as Error).message}`, { cause: error })
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
const parsed: unknown = JSON.parse(text)
|
|
72
|
+
if (!Array.isArray(parsed)) throw new Error('not a list')
|
|
73
|
+
return parsed as Todo[]
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(`acryl-todo: ${file} is not a valid todo list (${(error as Error).message}); fix or remove it, it was left untouched`, { cause: error })
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function save(file: string, todos: readonly Todo[]): void {
|
|
80
|
+
mkdirSync(dirname(file), { recursive: true })
|
|
81
|
+
const temporary = `${file}.tmp`
|
|
82
|
+
writeFileSync(temporary, JSON.stringify(todos))
|
|
83
|
+
renameSync(temporary, file)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function apply(ctx: Context, config: Config = {}): void {
|
|
87
|
+
const prefix = config.prefix ?? '/todo'
|
|
88
|
+
const todos = ctx.state.cell<readonly Todo[]>('todo.list', load(config.file))
|
|
89
|
+
if (config.file !== undefined) {
|
|
90
|
+
const file = config.file
|
|
91
|
+
ctx.effect(() => todos.subscribe(list => { save(file, list) }), 'acryl-todo: save to file')
|
|
92
|
+
}
|
|
93
|
+
const nextId = (): number => todos.get().reduce((max, todo) => Math.max(max, todo.id), 0) + 1
|
|
94
|
+
|
|
95
|
+
const find = (id: number): Todo => {
|
|
96
|
+
const todo = todos.get().find(item => item.id === id)
|
|
97
|
+
if (todo === undefined) throw new HTTPException(404, { message: 'no such todo' })
|
|
98
|
+
return todo
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// `/todo/` is what people type: trimTrailingSlash answers a GET for an unknown path ending in `/` with a 301 to the path without it,
|
|
102
|
+
// so the page is found either way while each route keeps one canonical path (the router itself treats `/a` and `/a/` as different).
|
|
103
|
+
const api = new Acryl()
|
|
104
|
+
.use(loopbackOnly({ inProcess: 'allow' }), bodyLimit({ maxSize: MAX_BODY_BYTES }), trimTrailingSlash())
|
|
105
|
+
.get('/', c => c.html(pageHtml(prefix)))
|
|
106
|
+
.get('/api', c => c.json(todos.get()))
|
|
107
|
+
.post('/api', schemaValidator('json', NewTodo), c => {
|
|
108
|
+
const todo: Todo = { id: nextId(), title: c.req.valid('json').title, done: false }
|
|
109
|
+
todos.update(list => [...list, todo])
|
|
110
|
+
return c.json(todo, 201)
|
|
111
|
+
})
|
|
112
|
+
.patch('/api/:id', schemaValidator('json', TodoChange), c => {
|
|
113
|
+
const { id } = find(Number(c.req.param('id')))
|
|
114
|
+
const { done } = c.req.valid('json')
|
|
115
|
+
todos.update(list => list.map(todo => todo.id === id ? { ...todo, done } : todo))
|
|
116
|
+
return c.json(find(id))
|
|
117
|
+
})
|
|
118
|
+
.delete('/api/:id', c => {
|
|
119
|
+
const { id } = find(Number(c.req.param('id')))
|
|
120
|
+
todos.update(list => list.filter(todo => todo.id !== id))
|
|
121
|
+
return c.body(null, 204)
|
|
122
|
+
})
|
|
123
|
+
.get('/events', c => streamSSE(c, async stream => {
|
|
124
|
+
const ended = new Promise<void>(resolve => { stream.onAbort(resolve) })
|
|
125
|
+
// Writes are asynchronous; chaining them keeps events in the order the list changed.
|
|
126
|
+
let queue: Promise<unknown> = Promise.resolve()
|
|
127
|
+
const send = (list: readonly Todo[]): void => { queue = queue.then(() => stream.writeSSE({ data: JSON.stringify(list) })).catch(() => {}) }
|
|
128
|
+
send(todos.get())
|
|
129
|
+
const unsubscribe = todos.subscribe(send)
|
|
130
|
+
try {
|
|
131
|
+
await ended
|
|
132
|
+
} finally {
|
|
133
|
+
unsubscribe()
|
|
134
|
+
}
|
|
135
|
+
}))
|
|
136
|
+
ctx.router.route(prefix === '' ? '/' : prefix, api)
|
|
137
|
+
}
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|