@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.
Files changed (83) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/bin/frame-host.d.ts +2 -0
  5. package/bin/frame-host.js +47 -0
  6. package/package.json +124 -4
  7. package/src/child.d.ts +37 -0
  8. package/src/child.js +64 -0
  9. package/src/connectors/connectors.d.ts +101 -0
  10. package/src/connectors/connectors.js +256 -0
  11. package/src/connectors/daemon-connectors.d.ts +34 -0
  12. package/src/connectors/daemon-connectors.js +73 -0
  13. package/src/connectors/index.d.ts +7 -0
  14. package/src/connectors/index.js +7 -0
  15. package/src/connectors/mcp-client.d.ts +157 -0
  16. package/src/connectors/mcp-client.js +483 -0
  17. package/src/daemon.d.ts +119 -0
  18. package/src/daemon.js +301 -0
  19. package/src/frame/access.d.ts +69 -0
  20. package/src/frame/access.js +168 -0
  21. package/src/frame/client/comments.css +1 -0
  22. package/src/frame/client/comments.js +58 -0
  23. package/src/frame/client/imports.d.ts +6 -0
  24. package/src/frame/client/imports.js +112 -0
  25. package/src/frame/client/mcp-shell.js +345 -0
  26. package/src/frame/client/mcp.js +253 -0
  27. package/src/frame/client/runtime.js +1363 -0
  28. package/src/frame/client/sample.js +762 -0
  29. package/src/frame/client/shell.css +51 -0
  30. package/src/frame/client/shell.html +29 -0
  31. package/src/frame/client/shell.js +1417 -0
  32. package/src/frame/comments.d.ts +102 -0
  33. package/src/frame/comments.js +379 -0
  34. package/src/frame/core.d.ts +313 -0
  35. package/src/frame/core.js +1757 -0
  36. package/src/frame/db.d.ts +162 -0
  37. package/src/frame/db.js +604 -0
  38. package/src/frame/declaration.d.ts +30 -0
  39. package/src/frame/declaration.js +60 -0
  40. package/src/frame/env.d.ts +262 -0
  41. package/src/frame/env.js +13 -0
  42. package/src/frame/files.d.ts +130 -0
  43. package/src/frame/files.js +472 -0
  44. package/src/frame/index.d.ts +35 -0
  45. package/src/frame/index.js +33 -0
  46. package/src/frame/local.d.ts +126 -0
  47. package/src/frame/local.js +612 -0
  48. package/src/frame/mcp-relay.d.ts +79 -0
  49. package/src/frame/mcp-relay.js +191 -0
  50. package/src/frame/page-api.d.ts +48 -0
  51. package/src/frame/page-api.js +1 -0
  52. package/src/frame/page-comments.d.ts +109 -0
  53. package/src/frame/page-comments.js +1 -0
  54. package/src/frame/page-db.d.ts +88 -0
  55. package/src/frame/page-db.js +1 -0
  56. package/src/frame/page-imports.d.ts +2 -0
  57. package/src/frame/page-imports.js +2 -0
  58. package/src/frame/page-services.d.ts +124 -0
  59. package/src/frame/page-services.js +1 -0
  60. package/src/frame/page-values.d.ts +90 -0
  61. package/src/frame/page-values.js +1 -0
  62. package/src/frame/sample-relay.d.ts +42 -0
  63. package/src/frame/sample-relay.js +186 -0
  64. package/src/frame/snapshot-action.d.ts +18 -0
  65. package/src/frame/snapshot-action.js +82 -0
  66. package/src/frame/wire.d.ts +198 -0
  67. package/src/frame/wire.js +21 -0
  68. package/src/index.d.ts +17 -0
  69. package/src/index.js +17 -0
  70. package/src/lan.d.ts +75 -0
  71. package/src/lan.js +200 -0
  72. package/src/legacy-env.d.ts +7 -0
  73. package/src/legacy-env.js +14 -0
  74. package/src/serve.d.ts +375 -0
  75. package/src/serve.js +811 -0
  76. package/src/snapshot.d.ts +41 -0
  77. package/src/snapshot.js +224 -0
  78. package/src/type-package.d.ts +48 -0
  79. package/src/type-package.js +114 -0
  80. package/src/ws.d.ts +50 -0
  81. package/src/ws.js +203 -0
  82. package/src/zip.d.ts +197 -0
  83. package/src/zip.js +662 -0
@@ -0,0 +1,313 @@
1
+ /**
2
+ * The frame core: one open artifact of an original type (or a page artifact),
3
+ * served to any number of viewers. It answers plain-data requests
4
+ * ({@link FrameRequest} → {@link FrameReply}) and holds event connections
5
+ * ({@link FrameCore.connect}); everything it takes from where it runs comes
6
+ * through a {@link FrameEnv}. Nothing here imports a Node-only module (no
7
+ * `node:http`, `node:os`, `node:module`, `node:url`, `import.meta`, `fs.watch`
8
+ * or streams): local.ts adapts it to frame-host on this computer, and a hosted
9
+ * site can run it as is. Its storage (db/, comments.json, the published files,
10
+ * _blobs/) uses `node:fs` sync calls that a site's virtual file system also has.
11
+ *
12
+ * Every answer that depends on who asks takes the viewer from the request (or
13
+ * from the connection it belongs to), never from the instance: one core serves
14
+ * many viewers at once, and checks each call against that viewer's level and the
15
+ * declaration's access settings (access.ts). Nothing the page or the inner shell
16
+ * says about who it is counts: not a holder, not a client id, not an author.
17
+ */
18
+ import type { ServerAction } from '../serve.js';
19
+ import { CommentStore } from './comments.js';
20
+ import { type DbRules } from './access.js';
21
+ import { DbStore } from './db.js';
22
+ import type { Declaration } from './declaration.js';
23
+ import type { FrameEnv, FrameEventsAsk, FrameFile, FrameReply, FrameRequest, FrameSampleStream, FrameViewer } from './env.js';
24
+ import { BlobStore, FileStore, type PublishResult } from './files.js';
25
+ import { type CapReply, type FrameEvent, type FrameSession, type LocalUser } from './wire.js';
26
+ /** The Frame Runtime contract reported to pages. */
27
+ export declare const CONTRACT = "0.2.92";
28
+ /** A page artifact's marker: its declared capabilities are there (`capabilities`), put by the daemon. */
29
+ export declare const MARKER_FILE = ".artifact.json";
30
+ type Args = Record<string, unknown>;
31
+ /** A colour of the palette, by id. */
32
+ export declare function colorOf(id: string): string;
33
+ /**
34
+ * The viewer as the wire's `me` (`FrameSession.me`): its name and avatar only under the declaration's `profile` scope,
35
+ * its email only under `email` (without `scopes`, all that a page is told).
36
+ */
37
+ export declare function wireUser(viewer: FrameViewer, scopes?: {
38
+ profile: boolean;
39
+ email: boolean;
40
+ }): LocalUser;
41
+ /** A page artifact's declaration: the capabilities its `.artifact.json` declares (none when it has none). */
42
+ export declare function pageDeclaration(root: string): Declaration;
43
+ /**
44
+ * Mounts one artifact folder on an environment. Throws when the type carries no declaration, or when the page is
45
+ * restricted and its origin is one the inner shell runs on (see {@link FrameCore}).
46
+ */
47
+ export declare function mountFrameCore(root: string, env: FrameEnv): FrameCore;
48
+ /**
49
+ * A document as the page wrote it, without the runtime sendIndex put in its <head>: what a page that publishes itself
50
+ * from `document.documentElement.outerHTML` sends back. Kept, the boot script would carry this run's shell origin and
51
+ * grants into the file, and run before the fresh one on every later load. (`safe()` escapes `<`, so the boot script's
52
+ * own text holds no `</script>`.)
53
+ */
54
+ export declare function withoutFrameRuntime(html: string): string;
55
+ /** One open artifact of an original type. */
56
+ export declare class FrameCore {
57
+ readonly root: string;
58
+ readonly id: string;
59
+ readonly page: string;
60
+ readonly contentOrigin: string;
61
+ readonly shellOrigin: string;
62
+ readonly db: DbStore;
63
+ readonly comments: CommentStore;
64
+ readonly files: FileStore;
65
+ readonly blobs: BlobStore;
66
+ /** The broker generation: bumped when the type or the files change under an open page. */
67
+ brokerGen: number;
68
+ /** Page mode ({@link FrameEnv.page}). */
69
+ readonly pageMode: boolean;
70
+ /** {@link FrameEnv.contentSecurityPolicy}. */
71
+ readonly contentSecurityPolicy: string | null;
72
+ /** {@link FrameEnv.restricted}. */
73
+ readonly restricted: boolean;
74
+ declaration: Declaration;
75
+ title: string;
76
+ ver: string;
77
+ /** What the declaration grants (each viewer gets those of these its level allows: {@link grantsFor}). */
78
+ grants: string[];
79
+ stubs: Record<string, string>;
80
+ /** The declaration's db rules, compiled. */
81
+ dbRules: DbRules;
82
+ protected readonly env: FrameEnv;
83
+ private readonly shellOrigins;
84
+ private readonly config;
85
+ private flagKeys;
86
+ private typeVer;
87
+ /** Open event connections, by viewer and client id. */
88
+ private readonly clients;
89
+ private touchTimer;
90
+ /** Paths the page wrote, by the author the daemon records them under. */
91
+ private readonly touchPaths;
92
+ private presenceCache;
93
+ private watcher;
94
+ private watchTimer;
95
+ private typeTimer;
96
+ private readonly pending;
97
+ /** Page mode: the capabilities last read from the marker, to tell when they change. */
98
+ private capsSeen;
99
+ /** The `mcp` routes (mcp-relay.ts). */
100
+ private readonly mcp;
101
+ constructor(root: string, declaration: Declaration, env: FrameEnv);
102
+ private applyDeclaration;
103
+ private get caps();
104
+ /**
105
+ * What the page is told of people: the declaration's `user` scopes where the environment withholds identity (a
106
+ * site); else as before (names, no email).
107
+ */
108
+ private get scopes();
109
+ /** Whether room senders carry their viewer's id (`by`): with `user` declared, or wherever identity is not withheld. */
110
+ private get vouches();
111
+ /**
112
+ * The capabilities one viewer is granted: the declaration's, less `comments` for a viewer who may not comment and
113
+ * `assets` below the upload level (`admin`, or `interact` when the declaration opens uploads to it).
114
+ */
115
+ grantsFor(viewer: FrameViewer): string[];
116
+ /** Whether a viewer may read / write a db path (`segs`): the declared rules, else the defaults. */
117
+ mayDb(viewer: FrameViewer, segs: readonly string[], action: 'read' | 'write'): boolean;
118
+ /** A db write the viewer may make, or `invalid_argument` (as the contract answers a refused write). */
119
+ private mustWrite;
120
+ /** Whether the viewer edits the artifact (admin and above). */
121
+ private static edits;
122
+ /**
123
+ * Whether the page may write itself. On a site, `{artifact: {publish: false}}` (or `self`) says no, for every viewer,
124
+ * writers included (the contract: `publish`, `edit` and `sync` reject `not_granted`; editors and the agent still
125
+ * publish the usual way, outside the page). On this computer, as before: the declaration is not read for it.
126
+ */
127
+ private get publishes();
128
+ /** A write of the artifact's files: `not_granted` where the page declares `publish: false`, `not_writer` below admin. */
129
+ private mustPublish;
130
+ /** Whether a viewer may put text in front of the owner's agent: an editor who may comment. */
131
+ private static reachesAgent;
132
+ protected log(line: string): void;
133
+ /** The viewer of a request. */
134
+ viewerOf(req: FrameRequest): FrameViewer;
135
+ close(): void;
136
+ hasBlob(id: string): boolean;
137
+ /** Starts the environment's watcher, when it has one; else every change comes through the core. */
138
+ private startWatching;
139
+ /** Re-reads the type; when what it serves changed, rewires. */
140
+ typeChanged(): boolean;
141
+ /**
142
+ * Page mode: re-reads the capabilities `.artifact.json` declares; when they changed, rewires as for a new type, and
143
+ * open pages reload with the new set. Also run when a shell asks for its session, in case a watcher missed it.
144
+ */
145
+ capabilitiesChanged(): boolean;
146
+ /** Page mode: the title of the page's index.html (its `<title>`) becomes the artifact's, when it says one. */
147
+ private pageTitle;
148
+ /** A new broker generation: calls stamped with the old one are not run, open shells cut theirs and reload. */
149
+ private rewire;
150
+ /** Applies what the folder watcher gathered (exposed for tests: watchers are slow to settle). */
151
+ flushWatch(): void;
152
+ /**
153
+ * Paths of the artifact folder that changed without the core writing them (relative, `/`-separated): the watcher's
154
+ * gatherings, or, where there is no watcher, what the host wrote itself (a site's sync). Open pages get the events.
155
+ */
156
+ noteChanges(paths: string[]): void;
157
+ /** Files changed on disk, not by this page's own publish: a new manifest rewires and reloads open pages. */
158
+ filesChanged(paths?: string[]): boolean;
159
+ /**
160
+ * Sends an event to every open connection, or what `event` makes of it for that connection's viewer (null: nothing
161
+ * for this one).
162
+ */
163
+ private broadcast;
164
+ /**
165
+ * Documents changed: each connection hears of the ones its viewer may read, and of none it may not. No paths (a
166
+ * change the watcher could not place) reruns every listener, whose reads are each checked.
167
+ */
168
+ private dbChanged;
169
+ /** A write from the page: the daemon records it (best effort, gathered), under the viewer who made it. */
170
+ private touched;
171
+ /**
172
+ * Whether a request may open the event stream over a WebSocket: admitted, from the inner shell's origin (a browser
173
+ * sends `Origin` on every WebSocket), and not the page's read-only token.
174
+ */
175
+ acceptsStream(req: FrameRequest): boolean;
176
+ /** Whether the request's viewer is the page's read-only token; null when the environment places no viewer. */
177
+ private readOnly;
178
+ /**
179
+ * One request on the inner shell's origin. `{events: true}`: it asks for its event stream, which the adapter opens
180
+ * and hands to {@link connect}.
181
+ */
182
+ request(req: FrameRequest): Promise<FrameReply | FrameEventsAsk>;
183
+ private shellReply;
184
+ /**
185
+ * `POST /api/frame/mcp/call`, for an adapter that can stream (as {@link sampleStream}): the checks, then the call's
186
+ * NDJSON stream from the daemon (mcp-relay.ts), or one `error` line.
187
+ */
188
+ mcpStream(req: FrameRequest, signal: AbortSignal): Promise<FrameReply | FrameSampleStream>;
189
+ /** Whether the declaration lets the page send images with `sample` (`{sample: {images: true}}`). */
190
+ get sampleImages(): boolean;
191
+ /**
192
+ * Whether `viewer` is granted `sample` but this view cannot use it (`capability_disabled`): the page's read-only
193
+ * token, and, in an adapter that has them, a snapshot's visit.
194
+ */
195
+ protected sampleDisabled(viewer: FrameViewer): boolean;
196
+ /** Why one view may not use `sample` at all, or null. */
197
+ private sampleUnusable;
198
+ /** A `POST /api/frame/sample` body as the daemon is asked, or why it is refused before the daemon sees it. */
199
+ private sampleCall;
200
+ /**
201
+ * `POST /api/frame/sample`, for an adapter that can stream: the same checks as every inner shell route, then the
202
+ * call's NDJSON stream from the daemon (see sample-relay.ts), or a reply (a refusal is one `error` line). Aborting
203
+ * `signal` (the inner shell closed the request) cancels the call.
204
+ */
205
+ sampleStream(req: FrameRequest, signal: AbortSignal): Promise<FrameReply | FrameSampleStream>;
206
+ /** `POST /api/frame/sample/tool`: one round's tool results, handed on (`{ok: true}`, or `{error}`). */
207
+ private sampleTool;
208
+ /** `GET /api/frame/sample/limits?client=`: the view's limits (a snapshot may read them too), or `{error}`. */
209
+ private sampleLimits;
210
+ /**
211
+ * `GET /api/frame/sample/state`: whether the page's calls would be asked about (`permissions.state("sample")`), read
212
+ * from the person's permissions without asking, and the Shell page where they can change it. Or `{error}`.
213
+ */
214
+ private sampleState;
215
+ /** The inner shell's own requests: from its origin, never from a page elsewhere. */
216
+ private sameOrigin;
217
+ /** Per-capability config, as `__frame_init.capabilities[cap].config` or a grant carries it. */
218
+ private configs;
219
+ /** What one viewer's inner shell needs to start. */
220
+ session(viewer: FrameViewer): FrameSession;
221
+ /**
222
+ * The page's origin for one viewer ({@link FrameEnv.pageOrigin}): an origin alone, and, for a restricted page, never
223
+ * one the inner shell runs on (as the mount checks for the instance's).
224
+ */
225
+ private pageOriginFor;
226
+ /** Whether `client` is an inner shell's client id. */
227
+ static validClient(client: string): boolean;
228
+ /**
229
+ * Opens one inner shell's event connection (`client`, its own id), for the viewer of `req`: a connection of the same
230
+ * viewer and id is ended first. `out` carries the events; the answer is to be called when the connection closes.
231
+ * Throws `invalid_request` (400) for a client id that is not one ({@link FrameCore.validClient}).
232
+ */
233
+ connect(req: FrameRequest, client: string, out: {
234
+ send(event: FrameEvent): void;
235
+ end(): void;
236
+ }): () => void;
237
+ /** The open connection of a viewer's client id. */
238
+ private clientOf;
239
+ /**
240
+ * Who an event names as its sender (`from`): on a site, only a connection of the viewer's own (a page cannot speak
241
+ * as another client); on this computer, where one person is every viewer, the client id as given, as before.
242
+ */
243
+ private senderOf;
244
+ /** The connections as peers: `by` only when the declaration has `user`, a name only under its `profile` scope. */
245
+ private peers;
246
+ gate(cap: string, gen: number | undefined): CapReply | null;
247
+ /** `POST /api/frame/cap`: one call of `viewer`'s, answered as `{result}` or `{error}`. */
248
+ call(body: Args, viewer: FrameViewer): Promise<CapReply>;
249
+ private author;
250
+ /**
251
+ * How a thread reaches the caller. The inner shell's own comments UI (`forShell`), and every caller on this computer:
252
+ * as stored. A page on a site: an author's name only under the `profile` scope, its principal only when `user` is
253
+ * declared, and `mine` on the viewer's own messages either way (so a page's moderation still knows them).
254
+ */
255
+ private threadView;
256
+ capability(cap: string, method: string, a: Args, client: string, viewer: FrameViewer, forShell?: boolean): Promise<unknown>;
257
+ /** The page's publish: its wire form (`{text}` | `{b64}` | null | `{delete:true}`, each with an optional ifMatch). */
258
+ publish(files: Record<string, unknown>, client: string, viewer: FrameViewer): PublishResult;
259
+ /**
260
+ * Page mode's `publish(html)`: the whole document replaces the root index.html, only while the files are still the
261
+ * version this view was loaded from (`ver`; else `conflict`, nothing written). Every open view reloads, this one too.
262
+ */
263
+ publishPage(html: string, ver: string | undefined, viewer: FrameViewer): PublishResult;
264
+ /** The comments changed: only viewers who may comment hear of it. */
265
+ commentsChanged(): void;
266
+ /** Resolving, moving or deleting a thread (or one message of it): its starter (that message's author) or an editor. */
267
+ private mustModerate;
268
+ /** Whether a viewer uploaded an asset (`uploader`): one with none is the owner's. */
269
+ private uploadedBy;
270
+ /** The people `user` answers about: those who can reach the artifact, and the viewer. */
271
+ private reachable;
272
+ /** A person as `user.profiles` / `user.search` give them: names under `profile`, emails under `email`. */
273
+ private profileOf;
274
+ private presence;
275
+ private canSendToAgent;
276
+ private sendToAgent;
277
+ private proposeTitle;
278
+ private askAgent;
279
+ private activity;
280
+ /** One request on the page's own origin; every answer carries the page's CSP, when there is one. */
281
+ contentRequest(req: FrameRequest): Promise<FrameReply>;
282
+ /** The page origin's answer, for its reader (read-only, whatever the environment said). */
283
+ private contentReply;
284
+ /**
285
+ * Who may load the page: the inner shell's frame (it sends its origin as the Referer), the page itself, a visit
286
+ * typed into the address bar, or a client with no fetch metadata (a script, an old browser). A frame put up by
287
+ * anyone else is refused here, not by CSP `frame-ancestors`: that directive checks every ancestor, and the app
288
+ * around the Open Artifacts Shell may be a `file://` page or a scheme of its own (`app://`), which no source list
289
+ * written on this computer can name.
290
+ */
291
+ private loadedByShell;
292
+ /** The type's index.html (page mode: the artifact's, or an empty page) with the Frame Runtime first in its <head>. */
293
+ private sendIndex;
294
+ private sendClientFile;
295
+ /** ESM addresses contain only the capability name and the optional contract of this host. */
296
+ private capabilityModule;
297
+ /** A browser script: the built `<name>.js`, or one the environment makes (from source, locally). */
298
+ private sendScript;
299
+ private sendBlob;
300
+ /** A file of the artifact's storage. */
301
+ private sendStored;
302
+ }
303
+ /** A file of the artifact's storage, or null when it is not a file. */
304
+ export declare function storedFile(path: string): FrameFile | null;
305
+ /** A body's bytes: a stored file read whole, for a host that cannot stream it (a Durable Object). */
306
+ export declare function replyBytes(body: FrameReply['body']): Uint8Array | null;
307
+ /**
308
+ * The type's actions (`/__open-artifacts/actions/<name>`): `info` for Artifact `call`,
309
+ * and the hidden `open-artifacts.db.*` and `open-artifacts.comments.*` that ArtifactData and
310
+ * ArtifactComments use, in the shapes `libraryActions` answers.
311
+ */
312
+ export declare const frameActions: Record<string, ServerAction>;
313
+ export {};