@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,90 @@
|
|
|
1
|
+
/** Data shared by page capabilities and the frame host. */
|
|
2
|
+
export type Json = null | boolean | number | string | Json[] | {
|
|
3
|
+
[key: string]: Json;
|
|
4
|
+
};
|
|
5
|
+
export type JsonObject = {
|
|
6
|
+
[key: string]: Json;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* A viewer's level, lowest first: `view` reads, `interact` writes what a page's rules let it, `admin` edits the
|
|
10
|
+
* artifact, `owner` owns it. The core checks every call against it (access.ts holds the rules).
|
|
11
|
+
*/
|
|
12
|
+
export type FrameLevel = 'view' | 'interact' | 'admin' | 'owner';
|
|
13
|
+
export interface FileEntry {
|
|
14
|
+
sha256: string;
|
|
15
|
+
size: number;
|
|
16
|
+
contentType: string;
|
|
17
|
+
}
|
|
18
|
+
export interface PublishResult {
|
|
19
|
+
version: string;
|
|
20
|
+
changed: {
|
|
21
|
+
path: string;
|
|
22
|
+
sha256: string | null;
|
|
23
|
+
}[];
|
|
24
|
+
shas: Record<string, string>;
|
|
25
|
+
unchanged?: true;
|
|
26
|
+
}
|
|
27
|
+
export interface BlobInfo {
|
|
28
|
+
id: string;
|
|
29
|
+
contentType: string;
|
|
30
|
+
sizeBytes: number;
|
|
31
|
+
createdAt: string;
|
|
32
|
+
url: string;
|
|
33
|
+
}
|
|
34
|
+
export interface BlobList {
|
|
35
|
+
assets: BlobInfo[];
|
|
36
|
+
usage: {
|
|
37
|
+
files: number;
|
|
38
|
+
bytes: number;
|
|
39
|
+
maxFiles: number;
|
|
40
|
+
maxBytes: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface Author {
|
|
44
|
+
kind: 'user' | 'agent';
|
|
45
|
+
name: string;
|
|
46
|
+
}
|
|
47
|
+
/** One person's emoji on a message. */
|
|
48
|
+
export interface MessageReaction {
|
|
49
|
+
emoji: string;
|
|
50
|
+
author: Author;
|
|
51
|
+
at: string;
|
|
52
|
+
}
|
|
53
|
+
export interface CommentMessage {
|
|
54
|
+
id: string;
|
|
55
|
+
author: Author;
|
|
56
|
+
text: string;
|
|
57
|
+
at: string;
|
|
58
|
+
editedAt?: string;
|
|
59
|
+
/** Who the message asks, as the page named them (`agent` asks the agent). */
|
|
60
|
+
to?: string[];
|
|
61
|
+
reactions?: MessageReaction[];
|
|
62
|
+
/** The agent's reply to a message that was sent to it without asking it (no `@agent`): it joined without being asked. */
|
|
63
|
+
joined?: boolean;
|
|
64
|
+
/** A person's message that assigned the thread to this person; `null`: it left the thread unassigned. */
|
|
65
|
+
assignee?: string | null;
|
|
66
|
+
}
|
|
67
|
+
export interface ThreadAgentState {
|
|
68
|
+
status: 'pending' | 'delivered' | 'replied';
|
|
69
|
+
sentAt?: string;
|
|
70
|
+
deliveredAt?: string;
|
|
71
|
+
repliedAt?: string;
|
|
72
|
+
via?: string;
|
|
73
|
+
/** When a reply that did not ask the agent was sent to it after it had answered; its next reply has `joined`. */
|
|
74
|
+
unaskedAt?: string;
|
|
75
|
+
}
|
|
76
|
+
export interface Thread {
|
|
77
|
+
id: string;
|
|
78
|
+
anchor: string;
|
|
79
|
+
label: string;
|
|
80
|
+
detail?: string;
|
|
81
|
+
location: Record<string, unknown>;
|
|
82
|
+
status: 'open' | 'resolved';
|
|
83
|
+
agent?: ThreadAgentState;
|
|
84
|
+
createdAt: string;
|
|
85
|
+
updatedAt: string;
|
|
86
|
+
resolvedAt?: string;
|
|
87
|
+
/** Who resolved it, while it is resolved. */
|
|
88
|
+
resolvedBy?: Author;
|
|
89
|
+
messages: CommentMessage[];
|
|
90
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `sample` capability on frame-host's side: what the inner shell's three routes (`FRAME_SAMPLE_ROUTES`) check
|
|
3
|
+
* before anything reaches the daemon, and the relay of the daemon's NDJSON stream back to the inner shell.
|
|
4
|
+
*
|
|
5
|
+
* Platform-neutral, like core.ts (which is its only user): no Node-only module, so a hosted site can carry it.
|
|
6
|
+
*/
|
|
7
|
+
import { type SampleError, type SampleModelTier, type SampleStreamEvent, type SampleToolResult, type SampleWireOptions, type TypeApiSampleRequest } from '@openartifactsai/protocol';
|
|
8
|
+
import type { FrameReply, FrameSampleService } from './env.js';
|
|
9
|
+
/**
|
|
10
|
+
* The largest `POST /api/frame/sample` body: a call's images at their largest total as sent (the page downsizes them
|
|
11
|
+
* first), base64, and room for the prompt (escaped) and the tool definitions.
|
|
12
|
+
*/
|
|
13
|
+
export declare const SAMPLE_BODY_MAX: number;
|
|
14
|
+
/** The headers of every `sample` stream frame-host answers. */
|
|
15
|
+
export declare const SAMPLE_STREAM_HEADERS: Readonly<Record<string, string>>;
|
|
16
|
+
/** A sample error. */
|
|
17
|
+
export declare function sampleError(code: SampleError['code'], message: string): SampleError;
|
|
18
|
+
/** One NDJSON line. */
|
|
19
|
+
export declare function sampleLine(event: SampleStreamEvent): Uint8Array;
|
|
20
|
+
/** A whole `sample` stream that is one `error` line: how a refusal is answered (HTTP 200, as the stream would be). */
|
|
21
|
+
export declare function sampleErrorReply(error: SampleError): FrameReply;
|
|
22
|
+
/**
|
|
23
|
+
* What a `POST /api/frame/sample` body asks for, checked as far as frame-host can (the daemon checks again, and the
|
|
24
|
+
* rest): the call's parts, or the error it is answered with. `images`: whether the declaration lets the page send any.
|
|
25
|
+
*/
|
|
26
|
+
export declare function checkSampleRequest(body: Record<string, unknown>, images: boolean): {
|
|
27
|
+
input: TypeApiSampleRequest['input'];
|
|
28
|
+
modelTier: SampleModelTier;
|
|
29
|
+
options: SampleWireOptions;
|
|
30
|
+
} | SampleError;
|
|
31
|
+
/** A `POST /api/frame/sample/tool` body, checked: its parts, or the error it is answered with. */
|
|
32
|
+
export declare function checkToolRequest(body: Record<string, unknown>): {
|
|
33
|
+
sample: string;
|
|
34
|
+
round: number;
|
|
35
|
+
results: SampleToolResult[];
|
|
36
|
+
} | SampleError;
|
|
37
|
+
/**
|
|
38
|
+
* The daemon's stream for one call, as the inner shell is answered: each event on a line of its own, nothing after
|
|
39
|
+
* `done` or `error`, and an `upstream_error` line when the call could not be started or the stream ended without
|
|
40
|
+
* either. Aborting `signal` (the inner shell went away) cancels the daemon's request and ends this quietly.
|
|
41
|
+
*/
|
|
42
|
+
export declare function relaySample(service: FrameSampleService, request: TypeApiSampleRequest, signal: AbortSignal, log: (line: string) => void): AsyncGenerator<Uint8Array>;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `sample` capability on frame-host's side: what the inner shell's three routes (`FRAME_SAMPLE_ROUTES`) check
|
|
3
|
+
* before anything reaches the daemon, and the relay of the daemon's NDJSON stream back to the inner shell.
|
|
4
|
+
*
|
|
5
|
+
* Platform-neutral, like core.ts (which is its only user): no Node-only module, so a hosted site can carry it.
|
|
6
|
+
*/
|
|
7
|
+
import { MODEL_TIERS, SAMPLE_IMAGE_MAX_BYTES, SAMPLE_IMAGE_MEDIA_TYPES, SAMPLE_IMAGES_MAX_TOTAL_BYTES, SAMPLE_MAX_IMAGES, SAMPLE_MAX_PROMPT_BYTES, SAMPLE_MAX_TOOL_RESULT_BYTES, SAMPLE_EFFORTS, SAMPLE_STREAM_CONTENT_TYPE, lineDecoder, sampleInputBytes, sampleInputProblem, sampleToolsProblem, } from '@openartifactsai/protocol';
|
|
8
|
+
/**
|
|
9
|
+
* The largest `POST /api/frame/sample` body: a call's images at their largest total as sent (the page downsizes them
|
|
10
|
+
* first), base64, and room for the prompt (escaped) and the tool definitions.
|
|
11
|
+
*/
|
|
12
|
+
export const SAMPLE_BODY_MAX = Math.ceil(SAMPLE_IMAGES_MAX_TOTAL_BYTES / 3) * 4 + 4 * 1024 * 1024;
|
|
13
|
+
/** The headers of every `sample` stream frame-host answers. */
|
|
14
|
+
export const SAMPLE_STREAM_HEADERS = Object.freeze({
|
|
15
|
+
'Content-Type': `${SAMPLE_STREAM_CONTENT_TYPE}; charset=utf-8`,
|
|
16
|
+
'Cache-Control': 'no-store',
|
|
17
|
+
'X-Content-Type-Options': 'nosniff',
|
|
18
|
+
'Referrer-Policy': 'no-referrer',
|
|
19
|
+
});
|
|
20
|
+
const TIERS = new Set([...MODEL_TIERS, 'inherit']);
|
|
21
|
+
const MEDIA_TYPES = new Set(SAMPLE_IMAGE_MEDIA_TYPES);
|
|
22
|
+
const EVENTS = new Set(['start', 'wait', 'ask', 'delta', 'tool', 'done', 'error']);
|
|
23
|
+
/** The option names that travel; anything else a page sent is dropped here. */
|
|
24
|
+
const WIRE_OPTIONS = ['images', 'cache', 'format', 'tools', 'model', 'effort', 'fast'];
|
|
25
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
26
|
+
const utf8 = new TextEncoder();
|
|
27
|
+
/** A sample error. */
|
|
28
|
+
export function sampleError(code, message) {
|
|
29
|
+
return { code, message };
|
|
30
|
+
}
|
|
31
|
+
/** One NDJSON line. */
|
|
32
|
+
export function sampleLine(event) {
|
|
33
|
+
return utf8.encode(`${JSON.stringify(event)}\n`);
|
|
34
|
+
}
|
|
35
|
+
/** A whole `sample` stream that is one `error` line: how a refusal is answered (HTTP 200, as the stream would be). */
|
|
36
|
+
export function sampleErrorReply(error) {
|
|
37
|
+
const body = sampleLine({ t: 'error', ...error });
|
|
38
|
+
return { status: 200, headers: { ...SAMPLE_STREAM_HEADERS, 'Content-Length': body.length }, body };
|
|
39
|
+
}
|
|
40
|
+
/** The base64 text's decoded size, or -1 when it is not base64. */
|
|
41
|
+
function base64Bytes(data) {
|
|
42
|
+
if (data.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}$/.test(data))
|
|
43
|
+
return -1;
|
|
44
|
+
const pad = data.endsWith('==') ? 2 : data.endsWith('=') ? 1 : 0;
|
|
45
|
+
return (data.length / 4) * 3 - pad;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* What a `POST /api/frame/sample` body asks for, checked as far as frame-host can (the daemon checks again, and the
|
|
49
|
+
* rest): the call's parts, or the error it is answered with. `images`: whether the declaration lets the page send any.
|
|
50
|
+
*/
|
|
51
|
+
export function checkSampleRequest(body, images) {
|
|
52
|
+
const problem = sampleInputProblem(body.input);
|
|
53
|
+
if (problem)
|
|
54
|
+
return sampleError('invalid_request', problem);
|
|
55
|
+
if (sampleInputBytes(body.input) > SAMPLE_MAX_PROMPT_BYTES)
|
|
56
|
+
return sampleError('prompt_too_large', `input: at most ${SAMPLE_MAX_PROMPT_BYTES} bytes of text.`);
|
|
57
|
+
const modelTier = body.modelTier === undefined ? 'default' : body.modelTier;
|
|
58
|
+
if (typeof modelTier !== 'string' || !TIERS.has(modelTier))
|
|
59
|
+
return sampleError('invalid_request', 'modelTier: "default", "complex", "quick" or "inherit".');
|
|
60
|
+
if (body.options !== undefined && !isObject(body.options))
|
|
61
|
+
return sampleError('invalid_request', 'options: an object.');
|
|
62
|
+
const given = body.options ?? {};
|
|
63
|
+
const options = {};
|
|
64
|
+
for (const key of WIRE_OPTIONS)
|
|
65
|
+
if (given[key] !== undefined)
|
|
66
|
+
options[key] = given[key];
|
|
67
|
+
if (options.effort !== undefined && !SAMPLE_EFFORTS.includes(options.effort))
|
|
68
|
+
return sampleError('invalid_request', `options.effort: one of ${SAMPLE_EFFORTS.join(', ')}.`);
|
|
69
|
+
if (options.tools !== undefined) {
|
|
70
|
+
const tools = sampleToolsProblem(options.tools);
|
|
71
|
+
if (tools)
|
|
72
|
+
return sampleError('invalid_request', tools);
|
|
73
|
+
}
|
|
74
|
+
if (options.images !== undefined) {
|
|
75
|
+
const list = options.images;
|
|
76
|
+
if (!Array.isArray(list))
|
|
77
|
+
return sampleError('invalid_request', 'options.images: a list of {mediaType, data}.');
|
|
78
|
+
if (list.length && !images)
|
|
79
|
+
return sampleError('images_unavailable', 'This page does not declare images: capabilities: {sample: {images: true}}.');
|
|
80
|
+
if (list.length > SAMPLE_MAX_IMAGES)
|
|
81
|
+
return sampleError('image_rejected', `At most ${SAMPLE_MAX_IMAGES} images per call.`);
|
|
82
|
+
let total = 0;
|
|
83
|
+
for (let i = 0; i < list.length; i++) {
|
|
84
|
+
const image = list[i];
|
|
85
|
+
if (!isObject(image) || typeof image.data !== 'string' || typeof image.mediaType !== 'string')
|
|
86
|
+
return sampleError('invalid_request', `options.images[${i}]: {mediaType, data}.`);
|
|
87
|
+
if (!MEDIA_TYPES.has(image.mediaType))
|
|
88
|
+
return sampleError('image_rejected', `options.images[${i}]: ${image.mediaType} is not one of ${SAMPLE_IMAGE_MEDIA_TYPES.join(', ')}.`);
|
|
89
|
+
const bytes = base64Bytes(image.data);
|
|
90
|
+
if (bytes <= 0)
|
|
91
|
+
return sampleError('image_rejected', `options.images[${i}]: the bytes are not base64.`);
|
|
92
|
+
if (bytes > SAMPLE_IMAGE_MAX_BYTES)
|
|
93
|
+
return sampleError('image_rejected', `options.images[${i}]: over ${SAMPLE_IMAGE_MAX_BYTES} bytes as sent (the page downsizes images first).`);
|
|
94
|
+
total += bytes;
|
|
95
|
+
}
|
|
96
|
+
if (total > SAMPLE_IMAGES_MAX_TOTAL_BYTES)
|
|
97
|
+
return sampleError('image_rejected', `options.images: over ${SAMPLE_IMAGES_MAX_TOTAL_BYTES} bytes together as sent.`);
|
|
98
|
+
if (!list.length)
|
|
99
|
+
delete options.images;
|
|
100
|
+
}
|
|
101
|
+
return { input: body.input, modelTier: modelTier, options: options };
|
|
102
|
+
}
|
|
103
|
+
/** A `POST /api/frame/sample/tool` body, checked: its parts, or the error it is answered with. */
|
|
104
|
+
export function checkToolRequest(body) {
|
|
105
|
+
if (typeof body.sample !== 'string' || !body.sample || body.sample.length > 128)
|
|
106
|
+
return sampleError('invalid_request', 'sample: the call id of its start event.');
|
|
107
|
+
if (typeof body.round !== 'number' || !Number.isInteger(body.round) || body.round < 1)
|
|
108
|
+
return sampleError('invalid_request', 'round: the round of its tool event.');
|
|
109
|
+
if (!Array.isArray(body.results))
|
|
110
|
+
return sampleError('invalid_request', 'results: a list of {id, content, isError?}.');
|
|
111
|
+
const results = [];
|
|
112
|
+
for (let i = 0; i < body.results.length; i++) {
|
|
113
|
+
const r = body.results[i];
|
|
114
|
+
if (!isObject(r) || typeof r.id !== 'string' || typeof r.content !== 'string')
|
|
115
|
+
return sampleError('invalid_request', `results[${i}]: {id, content, isError?}.`);
|
|
116
|
+
if (utf8.encode(r.content).length > SAMPLE_MAX_TOOL_RESULT_BYTES)
|
|
117
|
+
return sampleError('invalid_request', `results[${i}].content: at most ${SAMPLE_MAX_TOOL_RESULT_BYTES} bytes.`);
|
|
118
|
+
results.push({ id: r.id, content: r.content, ...(r.isError === true ? { isError: true } : {}) });
|
|
119
|
+
}
|
|
120
|
+
return { sample: body.sample, round: body.round, results };
|
|
121
|
+
}
|
|
122
|
+
/** One line of the daemon's stream as an event, or null for a line that is not one (skipped). */
|
|
123
|
+
function eventOf(line) {
|
|
124
|
+
let value;
|
|
125
|
+
try {
|
|
126
|
+
value = JSON.parse(line);
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
if (!isObject(value) || typeof value.t !== 'string' || !EVENTS.has(value.t))
|
|
132
|
+
return null;
|
|
133
|
+
if (value.t === 'error' && (typeof value.code !== 'string' || typeof value.message !== 'string')) {
|
|
134
|
+
return { t: 'error', code: 'upstream_error', message: typeof value.message === 'string' ? value.message : 'The model call failed.' };
|
|
135
|
+
}
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The daemon's stream for one call, as the inner shell is answered: each event on a line of its own, nothing after
|
|
140
|
+
* `done` or `error`, and an `upstream_error` line when the call could not be started or the stream ended without
|
|
141
|
+
* either. Aborting `signal` (the inner shell went away) cancels the daemon's request and ends this quietly.
|
|
142
|
+
*/
|
|
143
|
+
export async function* relaySample(service, request, signal, log) {
|
|
144
|
+
let source;
|
|
145
|
+
try {
|
|
146
|
+
source = await service.start(request, signal);
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (signal.aborted)
|
|
150
|
+
return;
|
|
151
|
+
const message = error?.message ?? String(error);
|
|
152
|
+
log(`sample: not started: ${message}`);
|
|
153
|
+
yield sampleLine({ t: 'error', code: 'upstream_error', message: `The model call could not be started: ${message}` });
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const decoder = lineDecoder();
|
|
157
|
+
const events = function* (lines) {
|
|
158
|
+
for (const line of lines) {
|
|
159
|
+
const text = line.trim();
|
|
160
|
+
const event = text ? eventOf(text) : null;
|
|
161
|
+
if (event)
|
|
162
|
+
yield event;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
try {
|
|
166
|
+
for await (const chunk of source) {
|
|
167
|
+
for (const event of events(decoder.push(chunk))) {
|
|
168
|
+
yield sampleLine(event);
|
|
169
|
+
if (event.t === 'done' || event.t === 'error')
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
for (const event of events(decoder.end())) {
|
|
174
|
+
yield sampleLine(event);
|
|
175
|
+
if (event.t === 'done' || event.t === 'error')
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
catch (error) {
|
|
180
|
+
if (signal.aborted)
|
|
181
|
+
return;
|
|
182
|
+
log(`sample: the stream broke: ${error?.message ?? error}`);
|
|
183
|
+
}
|
|
184
|
+
if (!signal.aborted)
|
|
185
|
+
yield sampleLine({ t: 'error', code: 'upstream_error', message: 'The model call ended without an answer.' });
|
|
186
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* frame-host's `snapshot` action: a PNG of the page as headless Chrome draws it, for the agent to look at its own
|
|
3
|
+
* work. The same contract as the built-in types' action (`@openartifactsai/type-server`), plus `scheme`.
|
|
4
|
+
*
|
|
5
|
+
* Local only: frame-host passes {@link localFrameActions}; `frameActions` (core.ts, which a hosted site serves) has no
|
|
6
|
+
* `snapshot`, and nothing here is imported by the core. The camera loads the inner shell under a one-time credential
|
|
7
|
+
* (`FrameSite.beginSnapshot`), a read-only viewer nobody else sees, and waits for the shell to say what it shows is
|
|
8
|
+
* final (`data-open-artifacts-shell`: the page revealed, or the problem card).
|
|
9
|
+
*/
|
|
10
|
+
import type { ServerAction } from '../serve.js';
|
|
11
|
+
/** Snapshots kept per artifact under `_agent/snapshots/`; older ones are removed (as the type server keeps). */
|
|
12
|
+
export declare const SNAPSHOTS_KEPT = 20;
|
|
13
|
+
/** What the camera waits for on the inner shell: the page revealed after its load, or the problem card. */
|
|
14
|
+
export declare const SHELL_READY = "['revealed', 'problem'].includes(document.documentElement.dataset.openArtifactsShell || '')";
|
|
15
|
+
/** frame-host's `snapshot`. */
|
|
16
|
+
export declare const snapshotAction: ServerAction;
|
|
17
|
+
/** frame-host's actions: the core's ({@link frameActions}), and `snapshot`, which needs this computer's Chrome. */
|
|
18
|
+
export declare const localFrameActions: Record<string, ServerAction>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* frame-host's `snapshot` action: a PNG of the page as headless Chrome draws it, for the agent to look at its own
|
|
3
|
+
* work. The same contract as the built-in types' action (`@openartifactsai/type-server`), plus `scheme`.
|
|
4
|
+
*
|
|
5
|
+
* Local only: frame-host passes {@link localFrameActions}; `frameActions` (core.ts, which a hosted site serves) has no
|
|
6
|
+
* `snapshot`, and nothing here is imported by the core. The camera loads the inner shell under a one-time credential
|
|
7
|
+
* (`FrameSite.beginSnapshot`), a read-only viewer nobody else sees, and waits for the shell to say what it shows is
|
|
8
|
+
* final (`data-open-artifacts-shell`: the page revealed, or the problem card).
|
|
9
|
+
*/
|
|
10
|
+
import { mkdirSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { findChrome, snapshotPage } from "../snapshot.js";
|
|
13
|
+
import { frameActions } from "./core.js";
|
|
14
|
+
import { FrameError, staysInside } from "./files.js";
|
|
15
|
+
import { FrameSite } from "./local.js";
|
|
16
|
+
/** Snapshots kept per artifact under `_agent/snapshots/`; older ones are removed (as the type server keeps). */
|
|
17
|
+
export const SNAPSHOTS_KEPT = 20;
|
|
18
|
+
const SNAPSHOT_NOTE = 'image is a PNG of the page as headless Chrome draws it now. Look at it with your own image or file tools.';
|
|
19
|
+
/** What the camera waits for on the inner shell: the page revealed after its load, or the problem card. */
|
|
20
|
+
export const SHELL_READY = "['revealed', 'problem'].includes(document.documentElement.dataset.openArtifactsShell || '')";
|
|
21
|
+
/** frame-host's `snapshot`. */
|
|
22
|
+
export const snapshotAction = {
|
|
23
|
+
title: 'A PNG of the page as it draws now, to look at your own work; returns its path as image',
|
|
24
|
+
effect: 'read',
|
|
25
|
+
schema: {
|
|
26
|
+
type: 'object',
|
|
27
|
+
properties: {
|
|
28
|
+
width: { type: 'integer', description: 'Viewport width in pixels, 320 to 3840 (default 1440).' },
|
|
29
|
+
height: { type: 'integer', description: 'Viewport height in pixels, 240 to 2160 (default 900).' },
|
|
30
|
+
scheme: { type: 'string', enum: ['light', 'dark'], description: 'The colour scheme the page is drawn in (prefers-color-scheme), light (default) or dark.' },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
run: async (artifact, args) => {
|
|
34
|
+
const size = (value, min, max, fallback) => {
|
|
35
|
+
if (value === undefined)
|
|
36
|
+
return fallback;
|
|
37
|
+
if (typeof value !== 'number' || !Number.isInteger(value) || value < min || value > max) {
|
|
38
|
+
throw new FrameError('invalid_argument', 'snapshot: width is 320 to 3840 pixels, height 240 to 2160.');
|
|
39
|
+
}
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
const width = size(args.width, 320, 3840, 1440);
|
|
43
|
+
const height = size(args.height, 240, 2160, 900);
|
|
44
|
+
const scheme = args.scheme ?? 'light';
|
|
45
|
+
if (scheme !== 'light' && scheme !== 'dark')
|
|
46
|
+
throw new FrameError('invalid_argument', 'snapshot: scheme is "light" or "dark".');
|
|
47
|
+
const chrome = findChrome();
|
|
48
|
+
if (!chrome) {
|
|
49
|
+
throw new FrameError('unsupported', 'snapshot needs Chrome, Chromium or Edge on this computer, and none was found.', 400, {
|
|
50
|
+
fix: "Install Google Chrome, or set OPEN_ARTIFACTS_CHROME to a Chromium-based browser's path. Until then, ask the person to look at the page.",
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const site = artifact.site;
|
|
54
|
+
if (!(site instanceof FrameSite))
|
|
55
|
+
throw new FrameError('not_found', `${artifact.id} is not open here.`, 404);
|
|
56
|
+
const visit = site.beginSnapshot();
|
|
57
|
+
let png;
|
|
58
|
+
try {
|
|
59
|
+
png = await snapshotPage(chrome, { url: visit.url, width, height, scheme, ready: SHELL_READY });
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw new FrameError('failed', `snapshot: ${error instanceof Error ? error.message : String(error)}`, 500);
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
visit.end();
|
|
66
|
+
}
|
|
67
|
+
// `_agent/` is this computer's agent channel: never served, archived, synced or listed (see test/frame-snapshot).
|
|
68
|
+
const dir = join(artifact.root, '_agent', 'snapshots');
|
|
69
|
+
// A link at `_agent` or `_agent/snapshots` to a place outside the artifact's folder: the picture is not written there.
|
|
70
|
+
if (!staysInside(artifact.root, join(dir, 'snapshot.png')))
|
|
71
|
+
throw new FrameError('failed', 'snapshot: _agent/snapshots leaves the artifact folder (a link): remove it and try again.', 500);
|
|
72
|
+
mkdirSync(dir, { recursive: true });
|
|
73
|
+
const image = join(dir, `snapshot-${new Date().toISOString().replace(/[:.]/g, '-')}.png`);
|
|
74
|
+
writeFileSync(image, png);
|
|
75
|
+
const old = readdirSync(dir).filter((name) => /^snapshot-.*\.png$/.test(name)).sort();
|
|
76
|
+
for (const name of old.slice(0, Math.max(0, old.length - SNAPSHOTS_KEPT)))
|
|
77
|
+
rmSync(join(dir, name), { force: true });
|
|
78
|
+
return { image, width, height, scheme, bytes: png.length, note: SNAPSHOT_NOTE };
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
/** frame-host's actions: the core's ({@link frameActions}), and `snapshot`, which needs this computer's Chrome. */
|
|
82
|
+
export const localFrameActions = { ...frameActions, snapshot: snapshotAction };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types shared by the frame core (core.ts, local.ts), the inner shell
|
|
3
|
+
* (client/shell.ts) and the Frame Runtime injected into the page
|
|
4
|
+
* (client/runtime.ts). Types only: the client scripts are plain scripts with
|
|
5
|
+
* no imports at run time.
|
|
6
|
+
*
|
|
7
|
+
* Page ⇄ inner shell messages follow the frame protocol (the
|
|
8
|
+
* `__frame_*` envelope). Inner shell ⇄ server is this host's own HTTP API.
|
|
9
|
+
*/
|
|
10
|
+
/** The Frame Runtime contract this host reports in `__frame_init`. */
|
|
11
|
+
export type Contract = string;
|
|
12
|
+
/** What the server writes into the page as `window.__OPEN_ARTIFACTS_FRAME`, before the runtime loads. */
|
|
13
|
+
export interface FrameBoot {
|
|
14
|
+
/** The inner shell's origin: the only window the runtime talks to. */
|
|
15
|
+
shellOrigin: string;
|
|
16
|
+
/** The artifact id. */
|
|
17
|
+
id: string;
|
|
18
|
+
/** The type key from the declaration. */
|
|
19
|
+
key: string | null;
|
|
20
|
+
/** Capabilities the page may use (backed here). */
|
|
21
|
+
grants: string[];
|
|
22
|
+
/** Declared capabilities this host cannot serve: they answer with an error code. */
|
|
23
|
+
stubs: string[];
|
|
24
|
+
contract: Contract;
|
|
25
|
+
/** Relay the page's console errors to the inner shell (OPEN_ARTIFACTS_FRAME_DEBUG). */
|
|
26
|
+
debug?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** What `window.__FRAME_PREAMBLE` carries. */
|
|
29
|
+
export interface FramePreamble {
|
|
30
|
+
origins: string[];
|
|
31
|
+
}
|
|
32
|
+
/** A capability's config, as `__frame_init.capabilities[cap]` or a grant carries it. */
|
|
33
|
+
export interface CapGrant {
|
|
34
|
+
config?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
/** `GET /api/frame/session`: what the inner shell needs to start. */
|
|
37
|
+
export interface FrameSession {
|
|
38
|
+
id: string;
|
|
39
|
+
title: string;
|
|
40
|
+
key: string | null;
|
|
41
|
+
/** The artifact files' manifest version (the `/_f/<ver>/` segment). */
|
|
42
|
+
ver: string;
|
|
43
|
+
contract: Contract;
|
|
44
|
+
/** The page's origin for this viewer (on a site, it carries the page's read-only token). */
|
|
45
|
+
contentOrigin: string;
|
|
46
|
+
shellOrigin: string;
|
|
47
|
+
/** The outer Shells besides a loopback one the inner shell may speak to (`FrameOrigins.outer`); absent: none. */
|
|
48
|
+
outerOrigins?: string[];
|
|
49
|
+
/** `shell`: the outer Shell answers `sample` with the viewer's own key (`FrameEnv.sampleBy`); absent: frame-host does. */
|
|
50
|
+
sampleBy?: 'shell';
|
|
51
|
+
/** The broker generation: calls stamped with another one are not run. */
|
|
52
|
+
brokerGen: number;
|
|
53
|
+
grants: string[];
|
|
54
|
+
/** cap → error code. */
|
|
55
|
+
stubs: Record<string, string>;
|
|
56
|
+
/** cap → config, for `__frame_init.capabilities` and grants. */
|
|
57
|
+
configs: Record<string, Record<string, unknown>>;
|
|
58
|
+
/** The declaration's capabilities block, as written. */
|
|
59
|
+
capabilities: Record<string, unknown>;
|
|
60
|
+
flags: Record<string, boolean>;
|
|
61
|
+
readyTimeoutS: number;
|
|
62
|
+
/** The page frame's `sandbox`, for a restricted type (installed, or a project not trusted yet); absent: none. */
|
|
63
|
+
sandbox?: string;
|
|
64
|
+
me: LocalUser;
|
|
65
|
+
comments: {
|
|
66
|
+
available: boolean;
|
|
67
|
+
count: number;
|
|
68
|
+
};
|
|
69
|
+
/** wakesIdle false: an idle agent is not woken; what is sent waits for its next turn. */
|
|
70
|
+
agent: {
|
|
71
|
+
available: boolean;
|
|
72
|
+
name: string | null;
|
|
73
|
+
wakesIdle?: boolean;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* The view of a `snapshot` on this computer (frame-host's headless Chrome): it reads only, shows no toast, and does
|
|
77
|
+
* not wait out the usual deadlines before it says what it shows is final. Absent: an ordinary view.
|
|
78
|
+
*/
|
|
79
|
+
snapshot?: boolean;
|
|
80
|
+
}
|
|
81
|
+
export interface LocalUser {
|
|
82
|
+
id: string;
|
|
83
|
+
name: string;
|
|
84
|
+
avatarUrl: string | null;
|
|
85
|
+
color: string;
|
|
86
|
+
email: string | null;
|
|
87
|
+
isOwner: boolean;
|
|
88
|
+
canEdit: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface CapRequest {
|
|
91
|
+
cap: string;
|
|
92
|
+
method: string;
|
|
93
|
+
args: unknown[];
|
|
94
|
+
/** The inner shell's client id (room sender, own-publish echo). */
|
|
95
|
+
client: string;
|
|
96
|
+
brokerGen?: number;
|
|
97
|
+
}
|
|
98
|
+
export interface CapErrorBody {
|
|
99
|
+
code: string;
|
|
100
|
+
message: string;
|
|
101
|
+
[extra: string]: unknown;
|
|
102
|
+
}
|
|
103
|
+
/** `POST /api/frame/cap` answer (always HTTP 200 once the request was understood). */
|
|
104
|
+
export type CapReply = {
|
|
105
|
+
result: unknown;
|
|
106
|
+
error?: undefined;
|
|
107
|
+
} | {
|
|
108
|
+
error: CapErrorBody;
|
|
109
|
+
result?: undefined;
|
|
110
|
+
};
|
|
111
|
+
/** Events of the inner shell's stream (`/api/frame/events`). */
|
|
112
|
+
export type FrameEvent = {
|
|
113
|
+
t: 'hello';
|
|
114
|
+
ver: string;
|
|
115
|
+
brokerGen: number;
|
|
116
|
+
peers: Peer[];
|
|
117
|
+
count: number;
|
|
118
|
+
} | {
|
|
119
|
+
t: 'db';
|
|
120
|
+
paths: string[];
|
|
121
|
+
} | {
|
|
122
|
+
t: 'room';
|
|
123
|
+
topic: string;
|
|
124
|
+
data: unknown;
|
|
125
|
+
from: string;
|
|
126
|
+
by: string | null;
|
|
127
|
+
} | {
|
|
128
|
+
t: 'peers';
|
|
129
|
+
peers: Peer[];
|
|
130
|
+
} | {
|
|
131
|
+
t: 'files';
|
|
132
|
+
ver: string;
|
|
133
|
+
paths: string[];
|
|
134
|
+
from?: string;
|
|
135
|
+
} | {
|
|
136
|
+
t: 'comments';
|
|
137
|
+
count: number;
|
|
138
|
+
}
|
|
139
|
+
/** The broker was rewired: a new generation, and the caps it now serves. */
|
|
140
|
+
| {
|
|
141
|
+
t: 'rewired';
|
|
142
|
+
brokerGen: number;
|
|
143
|
+
caps: string[];
|
|
144
|
+
reason: 'type' | 'files';
|
|
145
|
+
};
|
|
146
|
+
export interface Peer {
|
|
147
|
+
peer: string;
|
|
148
|
+
/** The viewer's id, when the declaration has `user`; else null. */
|
|
149
|
+
by: string | null;
|
|
150
|
+
/** The viewer's name under the `profile` scope; else ''. */
|
|
151
|
+
name: string;
|
|
152
|
+
presence: Record<string, unknown>;
|
|
153
|
+
kind: 'user';
|
|
154
|
+
}
|
|
155
|
+
export interface CapCall {
|
|
156
|
+
__frame_cap: true;
|
|
157
|
+
cap: string;
|
|
158
|
+
id: string;
|
|
159
|
+
method: string;
|
|
160
|
+
args: unknown[];
|
|
161
|
+
brokerGen?: number;
|
|
162
|
+
holds?: boolean;
|
|
163
|
+
}
|
|
164
|
+
export interface CapResult {
|
|
165
|
+
__frame_cap_r: true;
|
|
166
|
+
id: string;
|
|
167
|
+
result?: unknown;
|
|
168
|
+
error?: CapErrorBody;
|
|
169
|
+
}
|
|
170
|
+
export interface FrameInit {
|
|
171
|
+
contract: Contract;
|
|
172
|
+
changes: string[];
|
|
173
|
+
flags: Record<string, boolean>;
|
|
174
|
+
capabilities: Record<string, CapGrant>;
|
|
175
|
+
usable: string[];
|
|
176
|
+
theme: 'light' | 'dark' | 'system';
|
|
177
|
+
hostVisible: boolean;
|
|
178
|
+
hostOverlays: unknown[];
|
|
179
|
+
hostSides: Record<string, unknown>;
|
|
180
|
+
hostKeys: unknown[];
|
|
181
|
+
hot: {
|
|
182
|
+
gen: number;
|
|
183
|
+
phase: 'final' | 'pending';
|
|
184
|
+
from: string | null;
|
|
185
|
+
};
|
|
186
|
+
capBudgets: Record<string, Record<string, number>>;
|
|
187
|
+
capWants: true;
|
|
188
|
+
brokerGen: number;
|
|
189
|
+
/** This host's: the inner shell's client id, so room events can tell "me". */
|
|
190
|
+
client: string;
|
|
191
|
+
}
|
|
192
|
+
export declare const DEFAULT_CAP_BUDGETS: Record<string, Record<string, number>>;
|
|
193
|
+
/** Message of a call refused because it was stamped with an older broker generation. */
|
|
194
|
+
export declare const STALE_GEN_MESSAGE = "not run: the artifact was updated";
|
|
195
|
+
/** Message of a pending call cut by `__frame_cap_rewired`. */
|
|
196
|
+
export declare const REWIRED_CUT_MESSAGE = "may not have run: the artifact was updated before this call was answered";
|
|
197
|
+
/** Message of a call to a capability the running version does not declare. */
|
|
198
|
+
export declare const UNDECLARED_MESSAGE = "the version this view runs does not declare it";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types shared by the frame core (core.ts, local.ts), the inner shell
|
|
3
|
+
* (client/shell.ts) and the Frame Runtime injected into the page
|
|
4
|
+
* (client/runtime.ts). Types only: the client scripts are plain scripts with
|
|
5
|
+
* no imports at run time.
|
|
6
|
+
*
|
|
7
|
+
* Page ⇄ inner shell messages follow the frame protocol (the
|
|
8
|
+
* `__frame_*` envelope). Inner shell ⇄ server is this host's own HTTP API.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_CAP_BUDGETS = {
|
|
11
|
+
mcp: { callTool: 130_000 },
|
|
12
|
+
sample: { sample: 330_000 },
|
|
13
|
+
endpoints: { fetch: 130_000 },
|
|
14
|
+
assets: { upload: 270_000 },
|
|
15
|
+
};
|
|
16
|
+
/** Message of a call refused because it was stamped with an older broker generation. */
|
|
17
|
+
export const STALE_GEN_MESSAGE = 'not run: the artifact was updated';
|
|
18
|
+
/** Message of a pending call cut by `__frame_cap_rewired`. */
|
|
19
|
+
export const REWIRED_CUT_MESSAGE = 'may not have run: the artifact was updated before this call was answered';
|
|
20
|
+
/** Message of a call to a capability the running version does not declare. */
|
|
21
|
+
export const UNDECLARED_MESSAGE = 'the version this view runs does not declare it';
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/server-sdk`: shared code for type servers.
|
|
3
|
+
*
|
|
4
|
+
* - `serve.ts`: the server skeleton (`serve`, `TypeServer`), the Site contract and `libraryActions`.
|
|
5
|
+
* - `daemon.ts`: the Type API client (`TypeApi`) and the `OPEN_ARTIFACTS_*` environment.
|
|
6
|
+
* - `lan.ts`: LAN listeners for sharing.
|
|
7
|
+
* - `ws.ts`: a minimal WebSocket server side.
|
|
8
|
+
* - `child.ts`: stopping a child process (`stopChild`, `hasExited`) and ending this one (`endProcess`).
|
|
9
|
+
*
|
|
10
|
+
* The daemon cannot tell a server built with it from one that speaks the
|
|
11
|
+
* protocol by itself.
|
|
12
|
+
*/
|
|
13
|
+
export * from './serve.js';
|
|
14
|
+
export * from './daemon.js';
|
|
15
|
+
export * from './lan.js';
|
|
16
|
+
export * from './ws.js';
|
|
17
|
+
export * from './child.js';
|