@openartifactsai/server-sdk 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/bin/frame-host.d.ts +2 -0
- package/bin/frame-host.js +47 -0
- package/package.json +124 -4
- package/src/child.d.ts +37 -0
- package/src/child.js +64 -0
- package/src/connectors/connectors.d.ts +101 -0
- package/src/connectors/connectors.js +256 -0
- package/src/connectors/daemon-connectors.d.ts +34 -0
- package/src/connectors/daemon-connectors.js +73 -0
- package/src/connectors/index.d.ts +7 -0
- package/src/connectors/index.js +7 -0
- package/src/connectors/mcp-client.d.ts +157 -0
- package/src/connectors/mcp-client.js +483 -0
- package/src/daemon.d.ts +119 -0
- package/src/daemon.js +301 -0
- package/src/frame/access.d.ts +69 -0
- package/src/frame/access.js +168 -0
- package/src/frame/client/comments.css +1 -0
- package/src/frame/client/comments.js +58 -0
- package/src/frame/client/imports.d.ts +6 -0
- package/src/frame/client/imports.js +112 -0
- package/src/frame/client/mcp-shell.js +345 -0
- package/src/frame/client/mcp.js +253 -0
- package/src/frame/client/runtime.js +1363 -0
- package/src/frame/client/sample.js +762 -0
- package/src/frame/client/shell.css +51 -0
- package/src/frame/client/shell.html +29 -0
- package/src/frame/client/shell.js +1417 -0
- package/src/frame/comments.d.ts +102 -0
- package/src/frame/comments.js +379 -0
- package/src/frame/core.d.ts +313 -0
- package/src/frame/core.js +1757 -0
- package/src/frame/db.d.ts +162 -0
- package/src/frame/db.js +604 -0
- package/src/frame/declaration.d.ts +30 -0
- package/src/frame/declaration.js +60 -0
- package/src/frame/env.d.ts +262 -0
- package/src/frame/env.js +13 -0
- package/src/frame/files.d.ts +130 -0
- package/src/frame/files.js +472 -0
- package/src/frame/index.d.ts +35 -0
- package/src/frame/index.js +33 -0
- package/src/frame/local.d.ts +126 -0
- package/src/frame/local.js +612 -0
- package/src/frame/mcp-relay.d.ts +79 -0
- package/src/frame/mcp-relay.js +191 -0
- package/src/frame/page-api.d.ts +48 -0
- package/src/frame/page-api.js +1 -0
- package/src/frame/page-comments.d.ts +109 -0
- package/src/frame/page-comments.js +1 -0
- package/src/frame/page-db.d.ts +88 -0
- package/src/frame/page-db.js +1 -0
- package/src/frame/page-imports.d.ts +2 -0
- package/src/frame/page-imports.js +2 -0
- package/src/frame/page-services.d.ts +124 -0
- package/src/frame/page-services.js +1 -0
- package/src/frame/page-values.d.ts +90 -0
- package/src/frame/page-values.js +1 -0
- package/src/frame/sample-relay.d.ts +42 -0
- package/src/frame/sample-relay.js +186 -0
- package/src/frame/snapshot-action.d.ts +18 -0
- package/src/frame/snapshot-action.js +82 -0
- package/src/frame/wire.d.ts +198 -0
- package/src/frame/wire.js +21 -0
- package/src/index.d.ts +17 -0
- package/src/index.js +17 -0
- package/src/lan.d.ts +75 -0
- package/src/lan.js +200 -0
- package/src/legacy-env.d.ts +7 -0
- package/src/legacy-env.js +14 -0
- package/src/serve.d.ts +375 -0
- package/src/serve.js +811 -0
- package/src/snapshot.d.ts +41 -0
- package/src/snapshot.js +224 -0
- package/src/type-package.d.ts +48 -0
- package/src/type-package.js +114 -0
- package/src/ws.d.ts +50 -0
- package/src/ws.js +203 -0
- package/src/zip.d.ts +197 -0
- package/src/zip.js +662 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The inline declaration of an original artifact type: a type's index.html
|
|
3
|
+
* carries, near the top of its first script, a block that starts with the
|
|
4
|
+
* line comment `// artifact.json` and declares
|
|
5
|
+
* `var key = "<type key>"; var capabilities = {...}; var monitoring = {...};`
|
|
6
|
+
* in some order (a type may leave capabilities out). Only that block is read:
|
|
7
|
+
* it ends at the next line comment (`declarationSource` in the protocol, which
|
|
8
|
+
* `open-artifacts types check` reads too). It is read as literal data
|
|
9
|
+
* (`parseLiteralDeclarations`), never run: nothing from a type folder is
|
|
10
|
+
* evaluated in Node, so an installed type's page is read the same way as a
|
|
11
|
+
* built-in one's.
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from 'node:fs';
|
|
14
|
+
import { join } from 'node:path';
|
|
15
|
+
import { declarationSource, parseLiteralDeclarations } from '@openartifactsai/protocol';
|
|
16
|
+
export { DECLARATION_LINE } from '@openartifactsai/protocol';
|
|
17
|
+
/**
|
|
18
|
+
* The declaration in a type's index.html, or null when the page carries none. Only `var` bindings count (a `let` or
|
|
19
|
+
* `const` is not a global of the page, and `types check` flags it); the last initialized one of a name wins, as when the
|
|
20
|
+
* block runs. Throws when the block is not literal data.
|
|
21
|
+
*/
|
|
22
|
+
export function parseDeclaration(html) {
|
|
23
|
+
const found = declarationSource(html);
|
|
24
|
+
if (!found)
|
|
25
|
+
return null;
|
|
26
|
+
const values = new Map();
|
|
27
|
+
try {
|
|
28
|
+
for (const b of parseLiteralDeclarations(found.source, html.slice(0, found.offset))) {
|
|
29
|
+
if (b.kind !== 'var')
|
|
30
|
+
continue;
|
|
31
|
+
if (b.initialized || !values.has(b.name))
|
|
32
|
+
values.set(b.name, b.value);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
throw new Error(`The artifact.json block of index.html is not literal data: ${error?.message ?? error}`);
|
|
37
|
+
}
|
|
38
|
+
const key = values.get('key');
|
|
39
|
+
const capabilities = values.get('capabilities');
|
|
40
|
+
const monitoring = values.get('monitoring');
|
|
41
|
+
return {
|
|
42
|
+
key: typeof key === 'string' ? key : null,
|
|
43
|
+
capabilities: isObject(capabilities) ? capabilities : {},
|
|
44
|
+
monitoring: isObject(monitoring) ? monitoring : {},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** The declaration of the type folder `dir` (its index.html), or null. */
|
|
48
|
+
export function readDeclaration(dir) {
|
|
49
|
+
let html;
|
|
50
|
+
try {
|
|
51
|
+
html = readFileSync(join(dir, 'index.html'), 'utf8');
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return parseDeclaration(html);
|
|
57
|
+
}
|
|
58
|
+
function isObject(v) {
|
|
59
|
+
return typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
60
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the frame core takes from where it runs ({@link FrameEnv}). The core
|
|
3
|
+
* (core.ts) serves one artifact folder to any number of viewers; everything it
|
|
4
|
+
* needs from its surroundings comes through this interface, so the same core
|
|
5
|
+
* runs as frame-host on this computer (local.ts, {@link localFrameEnv}) and,
|
|
6
|
+
* later, inside a hosted site (a Node server, or a Cloudflare Durable Object
|
|
7
|
+
* whose `node:fs` is a virtual file system).
|
|
8
|
+
*
|
|
9
|
+
* Types and constants only, and no Node-only module: a site imports this file
|
|
10
|
+
* and core.ts, never local.ts.
|
|
11
|
+
*/
|
|
12
|
+
import type { SampleLimits, TypeApiSampleLimitsQuery, TypeApiSampleRequest, TypeApiSampleToolRequest } from '@openartifactsai/protocol';
|
|
13
|
+
import type { FrameMcpService } from './mcp-relay.js';
|
|
14
|
+
import type { AgentHook } from '../serve.js';
|
|
15
|
+
import type { TypeApi } from '../daemon.js';
|
|
16
|
+
import type { Declaration } from './declaration.js';
|
|
17
|
+
import type { PathGuard } from './files.js';
|
|
18
|
+
import type { FrameLevel } from './page-values.js';
|
|
19
|
+
export type { FrameLevel } from './page-values.js';
|
|
20
|
+
export type { PathGuard } from './files.js';
|
|
21
|
+
/** The levels in order, lowest first. */
|
|
22
|
+
export declare const FRAME_LEVELS: readonly FrameLevel[];
|
|
23
|
+
/** Who one request (or one event connection) comes from. */
|
|
24
|
+
export interface FrameViewer {
|
|
25
|
+
/** A stable id: `local` on this computer, the site's user id on a site. */
|
|
26
|
+
id: string;
|
|
27
|
+
name: string;
|
|
28
|
+
email: string | null;
|
|
29
|
+
avatarUrl: string | null;
|
|
30
|
+
color: string;
|
|
31
|
+
level: FrameLevel;
|
|
32
|
+
/** A visitor without an account (a site's public link). */
|
|
33
|
+
guest: boolean;
|
|
34
|
+
/** Whether this viewer may comment: its own input, not derived from {@link level}. */
|
|
35
|
+
canComment: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* A request that came with the page's read-only token (see {@link FrameEnv.reader}): it may GET this artifact's
|
|
38
|
+
* files and the type's, nothing else. The core refuses it on every route of the inner shell's origin.
|
|
39
|
+
*/
|
|
40
|
+
readOnly?: boolean;
|
|
41
|
+
}
|
|
42
|
+
/** Someone who can reach this artifact, as `user.profiles` / `user.search` may answer about them. */
|
|
43
|
+
export interface FramePerson {
|
|
44
|
+
id: string;
|
|
45
|
+
name: string;
|
|
46
|
+
email: string | null;
|
|
47
|
+
avatarUrl: string | null;
|
|
48
|
+
color?: string;
|
|
49
|
+
level: FrameLevel;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* One request as plain data: what a Durable Object or a Node server can both hand over. Header names are lowercase.
|
|
53
|
+
*/
|
|
54
|
+
export interface FrameRequest {
|
|
55
|
+
method: string;
|
|
56
|
+
/** The path and query: below `Site.page` for the inner shell's requests, as sent for the page's origin. */
|
|
57
|
+
url: string;
|
|
58
|
+
headers: Readonly<Record<string, string | undefined>>;
|
|
59
|
+
/**
|
|
60
|
+
* The body, read when the core asks for it (never before its checks pass). Rejects with a `FrameError`
|
|
61
|
+
* `too_large` (413) past `limit` bytes.
|
|
62
|
+
*/
|
|
63
|
+
body(limit: number): Promise<Uint8Array>;
|
|
64
|
+
/** What the environment needs to place the request (local: the `IncomingMessage`); opaque to the core. */
|
|
65
|
+
peer?: unknown;
|
|
66
|
+
}
|
|
67
|
+
/** A body the core answers with: bytes, or a file of the artifact's storage (read with `node:fs`, or streamed). */
|
|
68
|
+
export type FrameBody = Uint8Array | {
|
|
69
|
+
path: string;
|
|
70
|
+
} | null;
|
|
71
|
+
/** One answer as plain data. */
|
|
72
|
+
export interface FrameReply {
|
|
73
|
+
status: number;
|
|
74
|
+
headers: Record<string, string | number>;
|
|
75
|
+
/** null: no body (HEAD, 304, a redirect). */
|
|
76
|
+
body: FrameBody;
|
|
77
|
+
}
|
|
78
|
+
/** The inner shell asked for its event stream: the adapter opens it and calls `FrameCore.connect`. */
|
|
79
|
+
export interface FrameEventsAsk {
|
|
80
|
+
events: true;
|
|
81
|
+
}
|
|
82
|
+
/** A file the core serves: its size, its time (null: no ETag, as for a script built on the fly) and its bytes. */
|
|
83
|
+
export interface FrameFile {
|
|
84
|
+
size: number;
|
|
85
|
+
mtimeMs: number | null;
|
|
86
|
+
body: Uint8Array | {
|
|
87
|
+
path: string;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/** Where the inner shell and the page run. */
|
|
91
|
+
export interface FrameOrigins {
|
|
92
|
+
/** The inner shell's origin (the capability API's): what the page talks to. */
|
|
93
|
+
shell: string;
|
|
94
|
+
/** Every origin the inner shell may be loaded from (the shell's first). */
|
|
95
|
+
shellOrigins: readonly string[];
|
|
96
|
+
/** The page's own origin. It must differ from every shell origin when the page is restricted (see the mount check). */
|
|
97
|
+
content: string;
|
|
98
|
+
/** The shell page's CSP `connect-src` sources besides `'self'`. */
|
|
99
|
+
connectSrc: readonly string[];
|
|
100
|
+
/** The shell page's CSP `frame-src`: the page origin(s). */
|
|
101
|
+
frameSrc: string;
|
|
102
|
+
/**
|
|
103
|
+
* The outer Shells (origins) the inner shell may speak the chrome protocol with besides a loopback one, when the
|
|
104
|
+
* `open_artifacts_shell` parameter names one of them (a site: its app origin). Absent: loopback only.
|
|
105
|
+
*/
|
|
106
|
+
outer?: readonly string[];
|
|
107
|
+
}
|
|
108
|
+
/** The type the artifact is of: its files, read the way the environment keeps them. */
|
|
109
|
+
export interface FrameTypeSource {
|
|
110
|
+
/** For messages: where the type is (local: its folder). */
|
|
111
|
+
label: string;
|
|
112
|
+
/** What the type serves now (index.html and artifact-type/), as a short hash: a change rewires open pages. */
|
|
113
|
+
fingerprint(): string;
|
|
114
|
+
/** The declaration in the type's index.html, or null; throws when its block is not literal data. */
|
|
115
|
+
declaration(): Declaration | null;
|
|
116
|
+
/** `artifact-type.json` (the manifest), or `{}`. */
|
|
117
|
+
manifest(): Record<string, unknown>;
|
|
118
|
+
/** The type's index.html, or null. */
|
|
119
|
+
indexHtml(): string | null;
|
|
120
|
+
/** A static file under `artifact-type/` (`rel` starts with it, already cleaned), or null. */
|
|
121
|
+
file(rel: string): FrameFile | null;
|
|
122
|
+
}
|
|
123
|
+
/** The browser files the core serves itself (`runtime.js`, `sample.js`, `imports.js`, `shell.js`, `shell.html`, `shell.css`, `comments.js`, `comments.css`). */
|
|
124
|
+
export interface FrameClientFiles {
|
|
125
|
+
/** The file, or null when there is none; throws when a script cannot be made (the message is logged). */
|
|
126
|
+
file(name: string): FrameFile | null;
|
|
127
|
+
}
|
|
128
|
+
/** What a change watcher reports, as paths relative to the artifact folder or the type folder (`/`-separated). */
|
|
129
|
+
export interface FrameWatchHandlers {
|
|
130
|
+
artifact(rel: string): void;
|
|
131
|
+
type(rel: string | null): void;
|
|
132
|
+
}
|
|
133
|
+
/** The daemon's side, on this computer: the change log and the agent's presence. */
|
|
134
|
+
export type FrameDaemon = Pick<TypeApi, 'available' | 'touch' | 'presence'>;
|
|
135
|
+
/**
|
|
136
|
+
* The model calls of the `sample` capability, as the daemon's Type API serves them (`sample`, `sample.tool`,
|
|
137
|
+
* `sample.limits`). The core checks every request first (grant, declaration, view) and only then hands it on.
|
|
138
|
+
*/
|
|
139
|
+
export interface FrameSampleService {
|
|
140
|
+
/**
|
|
141
|
+
* Starts one call. Resolves once the daemon answered (HTTP 200) with the NDJSON stream of `SampleStreamEvent`s,
|
|
142
|
+
* as raw bytes; rejects when it could not be started. Aborting `signal` cancels the call.
|
|
143
|
+
*/
|
|
144
|
+
start(body: TypeApiSampleRequest, signal: AbortSignal): Promise<AsyncIterable<Uint8Array>>;
|
|
145
|
+
/** Hands back one round's tool results (`{ok: true}`, or `{error}`). */
|
|
146
|
+
tool(body: TypeApiSampleToolRequest): Promise<unknown>;
|
|
147
|
+
/** The view's limits. */
|
|
148
|
+
limits(query: TypeApiSampleLimitsQuery): Promise<SampleLimits>;
|
|
149
|
+
/**
|
|
150
|
+
* Whether the person's permissions already cover the artifact's calls (the daemon's `sample.state`), never asking;
|
|
151
|
+
* absent: `permissions.state("sample")` answers `prompt`.
|
|
152
|
+
*/
|
|
153
|
+
state?(id: string): Promise<{
|
|
154
|
+
state: 'granted' | 'denied' | 'none';
|
|
155
|
+
}>;
|
|
156
|
+
/** The Shell page of the artifact, where the person can allow it when no Shell shows it; absent or null: none. */
|
|
157
|
+
shellUrl?(id: string): Promise<string | null>;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The answer to `POST /api/frame/sample` once its checks passed: a stream of NDJSON lines (`SampleStreamEvent`s),
|
|
161
|
+
* ended after `done` or `error`. Answered by {@link FrameCore.sampleStream}; an adapter that cannot stream answers
|
|
162
|
+
* through `FrameCore.request`, which says so in one `error` line.
|
|
163
|
+
*/
|
|
164
|
+
export interface FrameSampleStream {
|
|
165
|
+
status: 200;
|
|
166
|
+
headers: Record<string, string | number>;
|
|
167
|
+
stream: AsyncIterable<Uint8Array>;
|
|
168
|
+
}
|
|
169
|
+
/** Everything {@link FrameCore} takes from where it runs. */
|
|
170
|
+
export interface FrameEnv {
|
|
171
|
+
/** Artifact id. */
|
|
172
|
+
id: string;
|
|
173
|
+
/** Artifact title, as the host knows it (may be empty). */
|
|
174
|
+
title: string;
|
|
175
|
+
log(line: string): void;
|
|
176
|
+
origins: FrameOrigins;
|
|
177
|
+
/** The viewer of this request (one of the inner shell's origin). Local: the owner, for every request. */
|
|
178
|
+
viewer(req: FrameRequest): FrameViewer;
|
|
179
|
+
/**
|
|
180
|
+
* The viewer of a request to the page's origin (`/_f/<ver>/…`, `/_blob/<id>`, `/_runtime/…`, `/`), resolved from
|
|
181
|
+
* the page's own read-only token, never the viewer's page token; the core treats it as read-only whatever it says.
|
|
182
|
+
* null: refused (403). Local: the owner (the page's address carries no token on this computer).
|
|
183
|
+
*/
|
|
184
|
+
reader(req: FrameRequest): FrameViewer | null;
|
|
185
|
+
/**
|
|
186
|
+
* The page's origin for one viewer (`scheme://host[:port]`, no path), what the inner shell frames and posts to
|
|
187
|
+
* (`session.contentOrigin`). A site carries the read-only token in it, as a host label (`rt-<token>.pages.…`), and
|
|
188
|
+
* resolves {@link reader} from the `Host`: the page's own addresses are root-relative (`/_runtime/…`, `/_blob/<id>`,
|
|
189
|
+
* the `/` redirect), so a token in a path would be lost on them. Never the viewer's page token. It must not be an
|
|
190
|
+
* origin the inner shell runs on (the session refuses one, as the mount does), and `origins.frameSrc` must cover it.
|
|
191
|
+
* Local: `origins.content`.
|
|
192
|
+
*/
|
|
193
|
+
pageOrigin(viewer: FrameViewer): string;
|
|
194
|
+
/**
|
|
195
|
+
* The people who can reach this artifact (its owner, collaborators, …): all that `user.profiles` and `user.search`
|
|
196
|
+
* answer about. Local: the owner alone.
|
|
197
|
+
*/
|
|
198
|
+
people(): readonly FramePerson[] | Promise<readonly FramePerson[]>;
|
|
199
|
+
/**
|
|
200
|
+
* Whether the declaration's `user` scopes withhold identity from the page (a site: true, as the contract says): a
|
|
201
|
+
* name and avatar only under `profile`, an email only under `email`, a room sender's id only when `user` is
|
|
202
|
+
* declared, `user.search` over {@link people}, and an event's `from` only for a connection of the viewer's own.
|
|
203
|
+
* Local: false, as before: one person on their own computer, whose page is told its name and never an email,
|
|
204
|
+
* whose search finds no one, and whose client ids are taken as given.
|
|
205
|
+
*/
|
|
206
|
+
withholdIdentity: boolean;
|
|
207
|
+
/**
|
|
208
|
+
* Whether a request may come in at all. Local: from this computer (loopback). A site: a token it already
|
|
209
|
+
* verified. The core then checks the inner shell's own origin itself.
|
|
210
|
+
*/
|
|
211
|
+
admit(req: FrameRequest): boolean;
|
|
212
|
+
type: FrameTypeSource;
|
|
213
|
+
client: FrameClientFiles;
|
|
214
|
+
/**
|
|
215
|
+
* Starts watching for writes that did not go through the core (local: `fs.watch` on both folders, for the agent's
|
|
216
|
+
* edits). null: no watcher (a site): every write goes through the core, which sends the events itself, and a write
|
|
217
|
+
* the host makes on its own is handed in with `FrameCore.noteChanges`.
|
|
218
|
+
*/
|
|
219
|
+
watch: ((on: FrameWatchHandlers) => {
|
|
220
|
+
close(): void;
|
|
221
|
+
}) | null;
|
|
222
|
+
/** Whether the type's page is restricted (CSP + `sandbox`). A site always says yes. */
|
|
223
|
+
restricted: boolean;
|
|
224
|
+
/** Sent on every response of the page's origin; null: none. */
|
|
225
|
+
contentSecurityPolicy: string | null;
|
|
226
|
+
/** External origins the host granted to this type. Omitting the grant exposes an empty list to the page. */
|
|
227
|
+
allowedNetworkOrigins?: readonly string[];
|
|
228
|
+
/** Page mode: the page is the artifact's own index.html, its capabilities the ones `.artifact.json` declares. */
|
|
229
|
+
page: boolean;
|
|
230
|
+
/** How the stores keep inside the artifact folder: local follows links; a virtual file system has none. */
|
|
231
|
+
guard: PathGuard;
|
|
232
|
+
/**
|
|
233
|
+
* Where files named by absolute path (an `ArtifactData` write's `file_path`) may be read from: in a
|
|
234
|
+
* workspace-confined daemon, the artifact's workspace; checks a path and returns it (or throws). None: anywhere.
|
|
235
|
+
*/
|
|
236
|
+
imports?: {
|
|
237
|
+
resolve(path: string): string;
|
|
238
|
+
} | undefined;
|
|
239
|
+
/** Whether pages relay their console errors (`OPEN_ARTIFACTS_FRAME_DEBUG` locally). */
|
|
240
|
+
debug(): boolean;
|
|
241
|
+
/** The daemon (this computer), or null. */
|
|
242
|
+
daemon: FrameDaemon | null;
|
|
243
|
+
/**
|
|
244
|
+
* The `sample` capability's model calls. Absent: a declared `sample` answers only `tools_unavailable`, as a
|
|
245
|
+
* capability nothing here serves (every host but this computer's frame-host, for now).
|
|
246
|
+
*/
|
|
247
|
+
sample?: FrameSampleService | undefined;
|
|
248
|
+
/**
|
|
249
|
+
* `shell`: the outer Shell answers a declared `sample` itself, in the browser, with the viewer's own key (a site with
|
|
250
|
+
* `OA_SAMPLE=viewer-key`): the inner shell hands the page's calls to it (`sample_call`), and nothing here serves them.
|
|
251
|
+
* Without an outer Shell a call answers `capability_disabled`.
|
|
252
|
+
*/
|
|
253
|
+
sampleBy?: 'shell' | undefined;
|
|
254
|
+
/**
|
|
255
|
+
* A page's connector calls (`mcp`, through the daemon). Absent: a declared `mcp` answers only `server_not_connected`.
|
|
256
|
+
*/
|
|
257
|
+
mcp?: FrameMcpService | undefined;
|
|
258
|
+
/** Wakes the owner's session. */
|
|
259
|
+
agentHook: AgentHook;
|
|
260
|
+
agentName?: string;
|
|
261
|
+
agentWakesIdle?: boolean;
|
|
262
|
+
}
|
package/src/frame/env.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the frame core takes from where it runs ({@link FrameEnv}). The core
|
|
3
|
+
* (core.ts) serves one artifact folder to any number of viewers; everything it
|
|
4
|
+
* needs from its surroundings comes through this interface, so the same core
|
|
5
|
+
* runs as frame-host on this computer (local.ts, {@link localFrameEnv}) and,
|
|
6
|
+
* later, inside a hosted site (a Node server, or a Cloudflare Durable Object
|
|
7
|
+
* whose `node:fs` is a virtual file system).
|
|
8
|
+
*
|
|
9
|
+
* Types and constants only, and no Node-only module: a site imports this file
|
|
10
|
+
* and core.ts, never local.ts.
|
|
11
|
+
*/
|
|
12
|
+
/** The levels in order, lowest first. */
|
|
13
|
+
export const FRAME_LEVELS = ['view', 'interact', 'admin', 'owner'];
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The artifact's files as an original page sees them: everything in the
|
|
3
|
+
* artifact folder except what the host keeps for itself, listed as
|
|
4
|
+
* `_files.json` `{ver, files: {path: {sha256, size, contentType}}}`. `ver`
|
|
5
|
+
* names one state of that list. `publish` writes a batch of files with
|
|
6
|
+
* optional per-file preconditions (`ifMatch`: the sha256 the page last saw, or
|
|
7
|
+
* null for "must not exist") and answers `conflict` when one does not hold.
|
|
8
|
+
* Blobs (the assets capability) are content-addressed files under `_blobs/`.
|
|
9
|
+
*/
|
|
10
|
+
import type { FileEntry, PublishResult, BlobInfo, BlobList } from './page-values.js';
|
|
11
|
+
export type { FileEntry, PublishResult, BlobInfo, BlobList } from './page-values.js';
|
|
12
|
+
export declare const PUBLISH_MAX_PATHS = 256;
|
|
13
|
+
export declare const FILE_MAX_BYTES: number;
|
|
14
|
+
export declare const BLOB_MAX_BYTES: number;
|
|
15
|
+
export declare const BLOB_SVG_MAX_BYTES: number;
|
|
16
|
+
export declare const BLOB_CODE_MAX_BYTES: number;
|
|
17
|
+
export declare const BLOBS_MAX_FILES = 2000;
|
|
18
|
+
export declare const BLOBS_MAX_BYTES: number;
|
|
19
|
+
export declare const BLOB_TYPES: ReadonlySet<string>;
|
|
20
|
+
export declare const contentTypeOf: (file: string) => string;
|
|
21
|
+
export declare class FrameError extends Error {
|
|
22
|
+
readonly code: string;
|
|
23
|
+
readonly status: number;
|
|
24
|
+
readonly extra: Record<string, unknown>;
|
|
25
|
+
constructor(code: string, message: string, status?: number, extra?: Record<string, unknown>);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Paths of the type (never the artifact's): neither listed nor writable by a page. In any case: a folder on macOS or
|
|
29
|
+
* Windows is case-insensitive, so `SKILL.MD` there is `SKILL.md`.
|
|
30
|
+
*/
|
|
31
|
+
export declare function typePath(rel: string): boolean;
|
|
32
|
+
/** Paths the host keeps for itself (in any case, as {@link typePath}). */
|
|
33
|
+
export declare function reservedPath(rel: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Whether `file` (there or not yet) stays under `root` once links are followed: the real path of `file`, or of its
|
|
36
|
+
* nearest ancestor that exists, is `root`'s or under it. A link in the artifact folder (or in `db/`, `_blobs/`) to a
|
|
37
|
+
* folder or file outside is how a write or a read would leave it; this is checked before each.
|
|
38
|
+
*/
|
|
39
|
+
export declare function staysInside(root: string, file: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* How a store keeps inside the artifact folder: `inside(root, file)` (the check before each read and write) and
|
|
42
|
+
* `real(path)` (the path a file is served from). Each store takes one; the default is {@link LINK_GUARD}.
|
|
43
|
+
*/
|
|
44
|
+
export interface PathGuard {
|
|
45
|
+
inside(root: string, file: string): boolean;
|
|
46
|
+
real(path: string): string;
|
|
47
|
+
}
|
|
48
|
+
/** A folder on disk, where a link can lead out of it: {@link staysInside}, and links followed (`realpathSync`). */
|
|
49
|
+
export declare const LINK_GUARD: PathGuard;
|
|
50
|
+
/** A storage with no links (a virtual file system): containment is the path's own. */
|
|
51
|
+
export declare const PLAIN_GUARD: PathGuard;
|
|
52
|
+
/** A page-relative path, normalized, or null when it is not one. */
|
|
53
|
+
export declare function cleanPath(p: unknown): string | null;
|
|
54
|
+
export interface FileManifest {
|
|
55
|
+
ver: string;
|
|
56
|
+
files: Record<string, FileEntry>;
|
|
57
|
+
}
|
|
58
|
+
/** One write of a publish: content, or a delete; `ifMatch` is the sha256 expected now (null: must not exist). */
|
|
59
|
+
export type FileWrite = {
|
|
60
|
+
data: Buffer;
|
|
61
|
+
contentType?: string;
|
|
62
|
+
ifMatch?: string | null;
|
|
63
|
+
delete?: false;
|
|
64
|
+
} | {
|
|
65
|
+
delete: true;
|
|
66
|
+
ifMatch?: string | null;
|
|
67
|
+
};
|
|
68
|
+
declare function writeAtomic(file: string, data: Buffer | string): void;
|
|
69
|
+
/** Options of {@link FileStore}. */
|
|
70
|
+
export interface FileStoreOptions {
|
|
71
|
+
/** Page mode: `index.html` is the artifact's own (listed, served and writable), not the type's. */
|
|
72
|
+
ownIndex?: boolean;
|
|
73
|
+
/** How it keeps inside the folder (default {@link LINK_GUARD}). */
|
|
74
|
+
guard?: PathGuard;
|
|
75
|
+
}
|
|
76
|
+
export declare class FileStore {
|
|
77
|
+
readonly root: string;
|
|
78
|
+
/** rel → the hash of the file at that mtime and size. */
|
|
79
|
+
private readonly cache;
|
|
80
|
+
private readonly ownIndex;
|
|
81
|
+
private readonly guard;
|
|
82
|
+
current: FileManifest | null;
|
|
83
|
+
constructor(root: string, options?: FileStoreOptions);
|
|
84
|
+
/** A path of the type's here: {@link typePath}, but for a page's own `index.html`. */
|
|
85
|
+
private ofType;
|
|
86
|
+
private sha;
|
|
87
|
+
/** The current listing. */
|
|
88
|
+
manifest(): FileManifest;
|
|
89
|
+
/** The artifact file a page asks for: an absolute path, or null. */
|
|
90
|
+
resolve(rel: string): string | null;
|
|
91
|
+
/** Writes a batch. Throws {@link FrameError} `conflict` (with `live`, `paths`) when a precondition fails. */
|
|
92
|
+
publish(files: Record<string, FileWrite>): PublishResult;
|
|
93
|
+
}
|
|
94
|
+
export interface BlobMeta extends Omit<BlobInfo, 'url'> {
|
|
95
|
+
/** Everyone who uploaded these bytes (viewer ids), first first: the same bytes twice are one asset, claimed by both. */
|
|
96
|
+
uploaders?: string[];
|
|
97
|
+
/** The one uploader metadata written before `uploaders` records; read as `uploaders: [uploader]`. */
|
|
98
|
+
uploader?: string;
|
|
99
|
+
}
|
|
100
|
+
/** Who claims an asset: `uploaders`, or the older single `uploader`; none (`[]`): a blob from before either, the owner's. */
|
|
101
|
+
export declare function blobClaims(meta: Pick<BlobMeta, 'uploaders' | 'uploader'>): string[];
|
|
102
|
+
/** The most bytes an asset of `type` may have. */
|
|
103
|
+
export declare function blobLimit(type: string): number;
|
|
104
|
+
export declare class BlobStore {
|
|
105
|
+
readonly root: string;
|
|
106
|
+
readonly dir: string;
|
|
107
|
+
private readonly guard;
|
|
108
|
+
constructor(root: string, options?: {
|
|
109
|
+
guard?: PathGuard;
|
|
110
|
+
});
|
|
111
|
+
meta(id: string): BlobMeta | null;
|
|
112
|
+
fileOf(id: string): string | null;
|
|
113
|
+
/** The assets, oldest first; `only`: those this test keeps (the usage still counts them all). */
|
|
114
|
+
list(only?: (meta: BlobMeta) => boolean): BlobList;
|
|
115
|
+
/**
|
|
116
|
+
* Stores an asset by its content: the same bytes twice are one asset, and a second uploader adds its claim (so it
|
|
117
|
+
* lists and deletes it as its own). A claim added to a blob that had none (the owner's) leaves the owner, an editor,
|
|
118
|
+
* holding it as before.
|
|
119
|
+
*/
|
|
120
|
+
upload(data: Buffer, contentType: unknown, uploader?: string): BlobInfo;
|
|
121
|
+
private writeMeta;
|
|
122
|
+
/**
|
|
123
|
+
* Deletes by id or `/_blob/<id>` url. `claimant`: a viewer below editor, who removes only its own claim (else
|
|
124
|
+
* `not_uploader`); the bytes go once no claim is left. Without one (an editor): the asset goes, whoever claims it.
|
|
125
|
+
*/
|
|
126
|
+
delete(ref: unknown, claimant?: string): {
|
|
127
|
+
deleted: boolean;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export { writeAtomic };
|