@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,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `mcp` capability on frame-host's side: the inner shell's routes (`FRAME_MCP_ROUTES`), checked before anything
|
|
3
|
+
* reaches the daemon, and the relay of a call's NDJSON stream back to the inner shell. The daemon checks every call
|
|
4
|
+
* again and does the rest (the person's permission, the connector, the count).
|
|
5
|
+
*
|
|
6
|
+
* Platform-neutral, like core.ts (its only user): no Node-only module.
|
|
7
|
+
*/
|
|
8
|
+
import { type FrameMcpState, type McpDeclaredServer, type McpStreamEvent, type PageMcpError, type PageMcpErrorCode, type PageMcpListResult, type PageMcpToolDescription, type TypeApiMcpPageCallRequest, type TypeApiMcpPageDescribeRequest, type TypeApiMcpPageToolsRequest } from '@openartifactsai/protocol';
|
|
9
|
+
import type { FrameReply, FrameViewer } from './env.js';
|
|
10
|
+
/**
|
|
11
|
+
* A page's connector calls, as the daemon's Type API serves them (`mcp.page.*`). The core checks each request first
|
|
12
|
+
* (declaration, view) and only then hands it on.
|
|
13
|
+
*/
|
|
14
|
+
export interface FrameMcpService {
|
|
15
|
+
/** Starts one call: the daemon's NDJSON stream of `McpStreamEvent`s once it answered 200. Aborting cancels it. */
|
|
16
|
+
start(body: TypeApiMcpPageCallRequest, signal: AbortSignal): Promise<AsyncIterable<Uint8Array>>;
|
|
17
|
+
tools(body: TypeApiMcpPageToolsRequest): Promise<PageMcpListResult>;
|
|
18
|
+
describe(body: TypeApiMcpPageDescribeRequest): Promise<PageMcpToolDescription>;
|
|
19
|
+
state(id: string, view: string): Promise<FrameMcpState>;
|
|
20
|
+
}
|
|
21
|
+
/** What a declared `mcp` answers where nothing serves it (no daemon, or no usable declaration). */
|
|
22
|
+
export declare const MCP_UNSERVED: PageMcpErrorCode;
|
|
23
|
+
/** The largest `POST /api/frame/mcp/*` body: the input at its largest, escaped, and the rest. */
|
|
24
|
+
export declare const MCP_BODY_MAX: number;
|
|
25
|
+
export declare function mcpError(code: PageMcpErrorCode, message: string, extra?: Partial<PageMcpError>): PageMcpError;
|
|
26
|
+
/** One NDJSON line. */
|
|
27
|
+
export declare function mcpLine(event: McpStreamEvent): Uint8Array;
|
|
28
|
+
/** A whole call stream that is one `error` line (HTTP 200, as the stream would be). */
|
|
29
|
+
export declare function mcpErrorReply(error: PageMcpError): FrameReply;
|
|
30
|
+
/** What a call's body asks, checked as far as frame-host can, or the error it is answered with. */
|
|
31
|
+
export declare function checkMcpCall(body: Record<string, unknown>, declared: readonly McpDeclaredServer[]): {
|
|
32
|
+
server: string;
|
|
33
|
+
tool: string;
|
|
34
|
+
input: Record<string, unknown>;
|
|
35
|
+
watch: boolean;
|
|
36
|
+
} | PageMcpError;
|
|
37
|
+
/**
|
|
38
|
+
* The daemon's stream for one call, as the inner shell is answered: each event on its own line, nothing after `done`
|
|
39
|
+
* or `error`, and an `upstream_error` line when the call could not start or the stream ended without either.
|
|
40
|
+
*/
|
|
41
|
+
export declare function relayMcp(service: FrameMcpService, request: TypeApiMcpPageCallRequest, signal: AbortSignal, log: (line: string) => void): AsyncGenerator<Uint8Array>;
|
|
42
|
+
/** What {@link FrameMcp} reads of the core. */
|
|
43
|
+
export interface FrameMcpContext {
|
|
44
|
+
id(): string;
|
|
45
|
+
service(): FrameMcpService | undefined;
|
|
46
|
+
/** The declared capabilities now. */
|
|
47
|
+
capabilities(): Record<string, unknown>;
|
|
48
|
+
/** The page's stub code for `mcp`, when it has one. */
|
|
49
|
+
stub(): string | undefined;
|
|
50
|
+
granted(viewer: FrameViewer): boolean;
|
|
51
|
+
/** A snapshot's or the read-only token's view: granted, but not to be used. */
|
|
52
|
+
disabled(viewer: FrameViewer): boolean;
|
|
53
|
+
brokerGen(): number;
|
|
54
|
+
validClient(client: string): boolean;
|
|
55
|
+
log(line: string): void;
|
|
56
|
+
}
|
|
57
|
+
/** Whether frame-host serves `mcp` for these capabilities: a daemon to ask, and a declaration that names tools. */
|
|
58
|
+
export declare function mcpServed(service: FrameMcpService | undefined, capabilities: Record<string, unknown>): boolean;
|
|
59
|
+
/** frame-host's `mcp` routes. */
|
|
60
|
+
export declare class FrameMcp {
|
|
61
|
+
#private;
|
|
62
|
+
constructor(context: FrameMcpContext);
|
|
63
|
+
/** The routes this answers, beside the streamed call. */
|
|
64
|
+
static handles(path: string): boolean;
|
|
65
|
+
/** A call's body as the daemon is asked, or why it is refused first. */
|
|
66
|
+
call(body: Record<string, unknown>, viewer: FrameViewer): TypeApiMcpPageCallRequest | PageMcpError;
|
|
67
|
+
/** `POST /api/frame/mcp/call` once its checks passed: the relayed stream; a refusal is one `error` line. */
|
|
68
|
+
stream(body: Record<string, unknown>, viewer: FrameViewer, signal: AbortSignal): FrameReply | {
|
|
69
|
+
status: 200;
|
|
70
|
+
headers: Record<string, string | number>;
|
|
71
|
+
stream: AsyncIterable<Uint8Array>;
|
|
72
|
+
};
|
|
73
|
+
/** `tools`, `describe` (POST) and `state` (GET): `{result}` or `{error}`. */
|
|
74
|
+
answer(path: string, body: Record<string, unknown>, query: URLSearchParams, viewer: FrameViewer): Promise<{
|
|
75
|
+
result: unknown;
|
|
76
|
+
} | {
|
|
77
|
+
error: PageMcpError;
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `mcp` capability on frame-host's side: the inner shell's routes (`FRAME_MCP_ROUTES`), checked before anything
|
|
3
|
+
* reaches the daemon, and the relay of a call's NDJSON stream back to the inner shell. The daemon checks every call
|
|
4
|
+
* again and does the rest (the person's permission, the connector, the count).
|
|
5
|
+
*
|
|
6
|
+
* Platform-neutral, like core.ts (its only user): no Node-only module.
|
|
7
|
+
*/
|
|
8
|
+
import { FRAME_MCP_ROUTES, MCP_INPUT_MAX_BYTES, SAMPLE_STREAM_CONTENT_TYPE, declaredMcpServers, lineDecoder, mcpToolDeclared, } from '@openartifactsai/protocol';
|
|
9
|
+
/** What a declared `mcp` answers where nothing serves it (no daemon, or no usable declaration). */
|
|
10
|
+
export const MCP_UNSERVED = 'server_not_connected';
|
|
11
|
+
/** The largest `POST /api/frame/mcp/*` body: the input at its largest, escaped, and the rest. */
|
|
12
|
+
export const MCP_BODY_MAX = MCP_INPUT_MAX_BYTES * 2 + 16 * 1024;
|
|
13
|
+
const STREAM_HEADERS = Object.freeze({
|
|
14
|
+
'Content-Type': `${SAMPLE_STREAM_CONTENT_TYPE}; charset=utf-8`,
|
|
15
|
+
'Cache-Control': 'no-store',
|
|
16
|
+
'X-Content-Type-Options': 'nosniff',
|
|
17
|
+
'Referrer-Policy': 'no-referrer',
|
|
18
|
+
});
|
|
19
|
+
const EVENTS = new Set(['wait', 'ask', 'done', 'error']);
|
|
20
|
+
const NAME = /^[A-Za-z0-9_.:@/-]{1,128}$/;
|
|
21
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
22
|
+
const utf8 = new TextEncoder();
|
|
23
|
+
export function mcpError(code, message, extra = {}) {
|
|
24
|
+
return { code, message, ...extra };
|
|
25
|
+
}
|
|
26
|
+
/** One NDJSON line. */
|
|
27
|
+
export function mcpLine(event) {
|
|
28
|
+
return utf8.encode(`${JSON.stringify(event)}\n`);
|
|
29
|
+
}
|
|
30
|
+
/** A whole call stream that is one `error` line (HTTP 200, as the stream would be). */
|
|
31
|
+
export function mcpErrorReply(error) {
|
|
32
|
+
const body = mcpLine({ t: 'error', ...error });
|
|
33
|
+
return { status: 200, headers: { ...STREAM_HEADERS, 'Content-Length': body.length }, body };
|
|
34
|
+
}
|
|
35
|
+
/** What a call's body asks, checked as far as frame-host can, or the error it is answered with. */
|
|
36
|
+
export function checkMcpCall(body, declared) {
|
|
37
|
+
const server = body.server;
|
|
38
|
+
const tool = body.tool;
|
|
39
|
+
if (typeof server !== 'string' || !NAME.test(server) || typeof tool !== 'string' || !NAME.test(tool))
|
|
40
|
+
return mcpError('bad_request', 'callTool(server, tool, input): server and tool are names.');
|
|
41
|
+
const input = body.input === undefined || body.input === null ? {} : body.input;
|
|
42
|
+
if (!isObject(input))
|
|
43
|
+
return mcpError('bad_request', "callTool: input is an object of the tool's arguments.", { server, tool });
|
|
44
|
+
if (utf8.encode(JSON.stringify(input)).length > MCP_INPUT_MAX_BYTES)
|
|
45
|
+
return mcpError('bad_request', `callTool: input is larger than ${MCP_INPUT_MAX_BYTES} bytes.`, { server, tool });
|
|
46
|
+
if (!mcpToolDeclared(declared, server, tool))
|
|
47
|
+
return mcpError('not_in_manifest', `The page does not declare ${tool} of ${server}: add it to capabilities.mcp.servers.`, { server, tool });
|
|
48
|
+
return { server, tool, input, watch: body.watch === true };
|
|
49
|
+
}
|
|
50
|
+
function eventOf(line) {
|
|
51
|
+
let value;
|
|
52
|
+
try {
|
|
53
|
+
value = JSON.parse(line);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
if (!isObject(value) || typeof value.t !== 'string' || !EVENTS.has(value.t))
|
|
59
|
+
return null;
|
|
60
|
+
if (value.t === 'error' && (typeof value.code !== 'string' || typeof value.message !== 'string')) {
|
|
61
|
+
return { t: 'error', code: 'upstream_error', message: typeof value.message === 'string' ? value.message : 'The connector call failed.', retryable: true };
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The daemon's stream for one call, as the inner shell is answered: each event on its own line, nothing after `done`
|
|
67
|
+
* or `error`, and an `upstream_error` line when the call could not start or the stream ended without either.
|
|
68
|
+
*/
|
|
69
|
+
export async function* relayMcp(service, request, signal, log) {
|
|
70
|
+
let source;
|
|
71
|
+
try {
|
|
72
|
+
source = await service.start(request, signal);
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (signal.aborted)
|
|
76
|
+
return;
|
|
77
|
+
const message = error?.message ?? String(error);
|
|
78
|
+
log(`mcp: not started: ${message}`);
|
|
79
|
+
yield mcpLine({ t: 'error', code: 'upstream_error', message: `The connector call could not be started: ${message}`, server: request.server, tool: request.tool, retryable: true });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const decoder = lineDecoder();
|
|
83
|
+
const events = function* (lines) {
|
|
84
|
+
for (const line of lines) {
|
|
85
|
+
const text = line.trim();
|
|
86
|
+
const event = text ? eventOf(text) : null;
|
|
87
|
+
if (event)
|
|
88
|
+
yield event;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
try {
|
|
92
|
+
for await (const chunk of source) {
|
|
93
|
+
for (const event of events(decoder.push(chunk))) {
|
|
94
|
+
yield mcpLine(event);
|
|
95
|
+
if (event.t === 'done' || event.t === 'error')
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
for (const event of events(decoder.end())) {
|
|
100
|
+
yield mcpLine(event);
|
|
101
|
+
if (event.t === 'done' || event.t === 'error')
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
if (signal.aborted)
|
|
107
|
+
return;
|
|
108
|
+
log(`mcp: the stream broke: ${error?.message ?? error}`);
|
|
109
|
+
}
|
|
110
|
+
if (!signal.aborted)
|
|
111
|
+
yield mcpLine({ t: 'error', code: 'upstream_error', message: 'The connector call ended without an answer.', server: request.server, tool: request.tool, retryable: true });
|
|
112
|
+
}
|
|
113
|
+
/** Whether frame-host serves `mcp` for these capabilities: a daemon to ask, and a declaration that names tools. */
|
|
114
|
+
export function mcpServed(service, capabilities) {
|
|
115
|
+
return !!service && declaredMcpServers(capabilities).length > 0;
|
|
116
|
+
}
|
|
117
|
+
/** frame-host's `mcp` routes. */
|
|
118
|
+
export class FrameMcp {
|
|
119
|
+
#c;
|
|
120
|
+
constructor(context) {
|
|
121
|
+
this.#c = context;
|
|
122
|
+
}
|
|
123
|
+
/** The routes this answers, beside the streamed call. */
|
|
124
|
+
static handles(path) {
|
|
125
|
+
return path === FRAME_MCP_ROUTES.tools || path === FRAME_MCP_ROUTES.describe || path === FRAME_MCP_ROUTES.state;
|
|
126
|
+
}
|
|
127
|
+
#unusable(viewer, forState = false) {
|
|
128
|
+
const stub = this.#c.stub();
|
|
129
|
+
if (stub)
|
|
130
|
+
return mcpError(stub, 'mcp is not available on this computer: no connectors are declared with their tools, or nothing here can run them.');
|
|
131
|
+
if (!this.#c.granted(viewer))
|
|
132
|
+
return mcpError('not_granted', 'mcp: the version this view runs does not declare it.');
|
|
133
|
+
if (!this.#c.service())
|
|
134
|
+
return mcpError(MCP_UNSERVED, 'mcp is not available on this computer.');
|
|
135
|
+
if (!forState && this.#c.disabled(viewer))
|
|
136
|
+
return mcpError('capability_disabled', 'mcp is not available in this view (a snapshot of the page).');
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
/** A call's body as the daemon is asked, or why it is refused first. */
|
|
140
|
+
call(body, viewer) {
|
|
141
|
+
if (typeof body.brokerGen === 'number' && body.brokerGen !== this.#c.brokerGen())
|
|
142
|
+
return mcpError('capability_removed', 'not run: the artifact was updated');
|
|
143
|
+
const unusable = this.#unusable(viewer);
|
|
144
|
+
if (unusable)
|
|
145
|
+
return unusable;
|
|
146
|
+
const client = String(body.client ?? '');
|
|
147
|
+
if (!this.#c.validClient(client))
|
|
148
|
+
return mcpError('bad_request', 'Not a client id.');
|
|
149
|
+
const checked = checkMcpCall(body, declaredMcpServers(this.#c.capabilities()));
|
|
150
|
+
if ('code' in checked)
|
|
151
|
+
return checked;
|
|
152
|
+
return { id: this.#c.id(), view: client, server: checked.server, tool: checked.tool, input: checked.input, ...(checked.watch ? { watch: true } : {}) };
|
|
153
|
+
}
|
|
154
|
+
/** `POST /api/frame/mcp/call` once its checks passed: the relayed stream; a refusal is one `error` line. */
|
|
155
|
+
stream(body, viewer, signal) {
|
|
156
|
+
const call = this.call(body, viewer);
|
|
157
|
+
if ('code' in call)
|
|
158
|
+
return mcpErrorReply(call);
|
|
159
|
+
return { status: 200, headers: { ...STREAM_HEADERS }, stream: relayMcp(this.#c.service(), call, signal, (line) => this.#c.log(line)) };
|
|
160
|
+
}
|
|
161
|
+
/** `tools`, `describe` (POST) and `state` (GET): `{result}` or `{error}`. */
|
|
162
|
+
async answer(path, body, query, viewer) {
|
|
163
|
+
const unusable = this.#unusable(viewer, path === FRAME_MCP_ROUTES.state);
|
|
164
|
+
if (unusable)
|
|
165
|
+
return { error: unusable };
|
|
166
|
+
const client = String((path === FRAME_MCP_ROUTES.state ? query.get('client') : body.client) ?? '');
|
|
167
|
+
if (!this.#c.validClient(client))
|
|
168
|
+
return { error: mcpError('bad_request', 'Not a client id.') };
|
|
169
|
+
const service = this.#c.service();
|
|
170
|
+
try {
|
|
171
|
+
if (path === FRAME_MCP_ROUTES.state)
|
|
172
|
+
return { result: await service.state(this.#c.id(), client) };
|
|
173
|
+
const server = body.server === undefined ? undefined : String(body.server);
|
|
174
|
+
if (server !== undefined && !NAME.test(server))
|
|
175
|
+
return { error: mcpError('bad_request', 'server: a connector name.') };
|
|
176
|
+
if (path === FRAME_MCP_ROUTES.tools)
|
|
177
|
+
return { result: await service.tools({ id: this.#c.id(), view: client, ...(server === undefined ? {} : { server }) }) };
|
|
178
|
+
const tool = String(body.tool ?? '');
|
|
179
|
+
if (!server || !NAME.test(tool))
|
|
180
|
+
return { error: mcpError('bad_request', 'describeTool(server, tool): both are names.') };
|
|
181
|
+
if (!mcpToolDeclared(declaredMcpServers(this.#c.capabilities()), server, tool))
|
|
182
|
+
return { error: mcpError('not_in_manifest', `The page does not declare ${tool} of ${server}.`, { server, tool }) };
|
|
183
|
+
return { result: await service.describe({ id: this.#c.id(), view: client, server, tool }) };
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
const e = error;
|
|
187
|
+
const code = typeof e?.code === 'string' ? e.code : 'upstream_error';
|
|
188
|
+
return { error: mcpError(code, typeof e?.message === 'string' ? e.message : String(error)) };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { FrameDatabase } from './page-db.js';
|
|
2
|
+
import type { FrameServices } from './page-services.js';
|
|
3
|
+
import type { FrameComments } from './page-comments.js';
|
|
4
|
+
import type { SampleFunction } from '@openartifactsai/protocol/sample';
|
|
5
|
+
/** Namespaces the page host can supply. Each viewer's grants determine availability. */
|
|
6
|
+
export interface PageCapabilities extends FrameServices {
|
|
7
|
+
db: FrameDatabase;
|
|
8
|
+
comments: FrameComments;
|
|
9
|
+
/** Asks a model, the person's choice of source (the `sample` declaration): a function, with `json` and `limits`. */
|
|
10
|
+
sample: SampleFunction;
|
|
11
|
+
hot: {
|
|
12
|
+
readonly data: undefined;
|
|
13
|
+
readonly from: null;
|
|
14
|
+
readonly gen: 0;
|
|
15
|
+
readonly signal: undefined;
|
|
16
|
+
snapshot(...args: unknown[]): void;
|
|
17
|
+
ready(...args: unknown[]): void;
|
|
18
|
+
accept(...args: unknown[]): void;
|
|
19
|
+
restart(...args: unknown[]): void;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Unknown names have no statically known operations. */
|
|
23
|
+
export type PageNamespace<Name extends string> = Name extends keyof PageCapabilities ? Namespaced<PageCapabilities[Name]> : Readonly<Record<string, unknown>>;
|
|
24
|
+
/** A namespace object is read-only; a callable one (`sample`) stays callable. */
|
|
25
|
+
type Namespaced<T> = T extends (...args: never[]) => unknown ? T : Readonly<T>;
|
|
26
|
+
type WaitingMember<Member> = Member extends (...args: infer Args) => infer Result ? Result extends (() => void) ? (...args: [...Args, onError?: (reason: unknown) => void]) => Result : Result extends Promise<unknown> ? Member : (...args: Args) => Promise<Awaited<Result>> : unknown;
|
|
27
|
+
type WaitingNamespace<Name extends string> = {
|
|
28
|
+
readonly [Key in keyof PageNamespace<Name>]: WaitingMember<PageNamespace<Name>[Key]>;
|
|
29
|
+
};
|
|
30
|
+
/** A granted reference has synchronous builders. Before the grant, ordinary calls queue
|
|
31
|
+
* and listeners return a cancellation function immediately. The same reference is updated in place. */
|
|
32
|
+
export type PageReference<Name extends string> = (PageNamespace<Name> & {
|
|
33
|
+
readonly available: true;
|
|
34
|
+
}) | (WaitingNamespace<Name> & {
|
|
35
|
+
readonly available: false;
|
|
36
|
+
});
|
|
37
|
+
export interface PageImportOptions {
|
|
38
|
+
contract?: string;
|
|
39
|
+
}
|
|
40
|
+
/** The injected page API exposes initial grants directly and resolves missing grants to null. */
|
|
41
|
+
export type FramePageRuntime = Readonly<Partial<PageCapabilities>> & {
|
|
42
|
+
readonly contract: string;
|
|
43
|
+
readonly hot: Readonly<PageCapabilities['hot']>;
|
|
44
|
+
use<Name extends string>(name: Name): Promise<PageNamespace<Name> | null>;
|
|
45
|
+
};
|
|
46
|
+
export type * from './page-db.js';
|
|
47
|
+
export type * from './page-comments.js';
|
|
48
|
+
export type * from './page-services.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { WakeStatus } from '@openartifactsai/protocol';
|
|
2
|
+
import type { Author, CommentMessage, Thread, ThreadAgentState } from './page-values.js';
|
|
3
|
+
/** A pin's position relative to an element, produced by anchorFor and domAnchor. */
|
|
4
|
+
export interface PageElementAnchor {
|
|
5
|
+
path: string;
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
}
|
|
9
|
+
export interface PagePoint {
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
}
|
|
13
|
+
type PinPosition = PagePoint | Element | null;
|
|
14
|
+
type Opened = {
|
|
15
|
+
opened: boolean;
|
|
16
|
+
};
|
|
17
|
+
type MessageId = {
|
|
18
|
+
threadId: string;
|
|
19
|
+
commentId: string;
|
|
20
|
+
};
|
|
21
|
+
export interface PageThreadPin {
|
|
22
|
+
id: string;
|
|
23
|
+
anchor: unknown;
|
|
24
|
+
resolved: boolean;
|
|
25
|
+
active: boolean;
|
|
26
|
+
}
|
|
27
|
+
/** What comments.list() exposes to this viewer. Author identity depends on the viewer's grants. */
|
|
28
|
+
export interface PageCommentThread extends Pick<Thread, 'id' | 'label' | 'createdAt' | 'updatedAt' | 'status'> {
|
|
29
|
+
anchor: unknown;
|
|
30
|
+
resolved: boolean;
|
|
31
|
+
agent: ThreadAgentState | null;
|
|
32
|
+
messages: (Pick<CommentMessage, 'id' | 'text' | 'at'> & {
|
|
33
|
+
author: Author & {
|
|
34
|
+
principal?: string;
|
|
35
|
+
mine?: true;
|
|
36
|
+
};
|
|
37
|
+
})[];
|
|
38
|
+
}
|
|
39
|
+
export interface PageCommentSend {
|
|
40
|
+
threadId?: string;
|
|
41
|
+
anchor?: unknown;
|
|
42
|
+
text?: string;
|
|
43
|
+
label?: string;
|
|
44
|
+
detail?: string;
|
|
45
|
+
}
|
|
46
|
+
export interface PageCommentDecision {
|
|
47
|
+
handled?: boolean;
|
|
48
|
+
reply?: string;
|
|
49
|
+
handoffNote?: string;
|
|
50
|
+
}
|
|
51
|
+
export type PageCommentListener = (payload: PageCommentSend) => void | PageCommentDecision | Promise<void | PageCommentDecision>;
|
|
52
|
+
export interface FrameAnchorCallbacks {
|
|
53
|
+
pinsOutsideMode?: boolean;
|
|
54
|
+
mode?(on: boolean): void;
|
|
55
|
+
threads?(pins: PageThreadPin[]): void;
|
|
56
|
+
reveal?(threadId: string): void;
|
|
57
|
+
composing?(open: boolean | null): void;
|
|
58
|
+
move?(threadId: string, documentPoint: PagePoint): PageAnchorMove | null | void | Promise<PageAnchorMove | null | void>;
|
|
59
|
+
}
|
|
60
|
+
export interface PageAnchorMove {
|
|
61
|
+
anchor: string;
|
|
62
|
+
label?: string;
|
|
63
|
+
detail?: string;
|
|
64
|
+
}
|
|
65
|
+
export interface PageAnchorController {
|
|
66
|
+
compose(anchor: unknown, at?: PinPosition, options?: {
|
|
67
|
+
label?: string;
|
|
68
|
+
detail?: string;
|
|
69
|
+
area?: boolean;
|
|
70
|
+
}): Promise<Opened>;
|
|
71
|
+
open(threadId: string, at?: PinPosition): Promise<Opened>;
|
|
72
|
+
placed(positions: Readonly<Record<string, PinPosition>>): void;
|
|
73
|
+
domAnchor(element: Element, event?: MouseEvent): PageElementAnchor;
|
|
74
|
+
exitMode(): void;
|
|
75
|
+
release(): void;
|
|
76
|
+
readonly areas: undefined;
|
|
77
|
+
}
|
|
78
|
+
export interface FrameComments {
|
|
79
|
+
openComposer(options?: {
|
|
80
|
+
element?: Element;
|
|
81
|
+
}): Promise<Opened>;
|
|
82
|
+
anchorFor(element: Element): PageElementAnchor;
|
|
83
|
+
create(options: {
|
|
84
|
+
anchor?: unknown;
|
|
85
|
+
text: string;
|
|
86
|
+
label?: string;
|
|
87
|
+
}): Promise<MessageId>;
|
|
88
|
+
reply(threadId: string, text: string): Promise<MessageId>;
|
|
89
|
+
reply(comment: {
|
|
90
|
+
threadId: string;
|
|
91
|
+
text: string;
|
|
92
|
+
}): Promise<MessageId>;
|
|
93
|
+
resolve(threadId: string, resolved?: boolean): Promise<void>;
|
|
94
|
+
delete(threadId: string, commentId?: string): Promise<void>;
|
|
95
|
+
list(): Promise<PageCommentThread[]>;
|
|
96
|
+
sendToAgent(options: PageCommentSend): Promise<{
|
|
97
|
+
status: 'handled';
|
|
98
|
+
reply?: string;
|
|
99
|
+
} | {
|
|
100
|
+
status: WakeStatus;
|
|
101
|
+
threadId: string;
|
|
102
|
+
thread: PageCommentThread;
|
|
103
|
+
reply?: string;
|
|
104
|
+
}>;
|
|
105
|
+
canSendToAgent(): Promise<'available' | 'no_session' | 'off' | 'writers_only'>;
|
|
106
|
+
onSendToAgent(listener: PageCommentListener): () => void;
|
|
107
|
+
customAnchors(callbacks?: FrameAnchorCallbacks): Promise<PageAnchorController>;
|
|
108
|
+
}
|
|
109
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/** JSON stored in page documents. Snapshots also freeze nested arrays and objects. */
|
|
2
|
+
export type FrameDbValue = null | string | number | boolean | readonly FrameDbValue[] | FrameDocument;
|
|
3
|
+
export interface FrameDocument {
|
|
4
|
+
readonly [field: string]: FrameDbValue;
|
|
5
|
+
}
|
|
6
|
+
export type FrameWhereOp = '==' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not-in' | 'array-contains';
|
|
7
|
+
export type FrameDbError = Error & {
|
|
8
|
+
code: string;
|
|
9
|
+
};
|
|
10
|
+
export type FrameSnapshotListener<T> = ((snapshot: T) => void) | {
|
|
11
|
+
next(snapshot: T): void;
|
|
12
|
+
error?(reason: FrameDbError): void;
|
|
13
|
+
};
|
|
14
|
+
export interface FrameSnapshotMetadata {
|
|
15
|
+
readonly fromCache: boolean;
|
|
16
|
+
readonly hasPendingWrites: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface FrameDocSnapshot {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
readonly ref: FrameDocRef;
|
|
21
|
+
readonly exists: boolean;
|
|
22
|
+
readonly metadata: FrameSnapshotMetadata;
|
|
23
|
+
data(): FrameDocument | undefined;
|
|
24
|
+
get(field: string): FrameDbValue | undefined;
|
|
25
|
+
}
|
|
26
|
+
export interface FrameDocumentChange {
|
|
27
|
+
readonly type: 'added' | 'modified' | 'removed';
|
|
28
|
+
readonly doc: FrameDocSnapshot;
|
|
29
|
+
readonly oldIndex: number;
|
|
30
|
+
readonly newIndex: number;
|
|
31
|
+
}
|
|
32
|
+
export interface FrameQuerySnapshot {
|
|
33
|
+
readonly docs: readonly FrameDocSnapshot[];
|
|
34
|
+
readonly size: number;
|
|
35
|
+
readonly empty: boolean;
|
|
36
|
+
readonly metadata: FrameSnapshotMetadata;
|
|
37
|
+
forEach(fn: (doc: FrameDocSnapshot, index: number, docs: readonly FrameDocSnapshot[]) => void, thisArg?: unknown): void;
|
|
38
|
+
docChanges(): readonly FrameDocumentChange[];
|
|
39
|
+
}
|
|
40
|
+
export interface FrameQuery {
|
|
41
|
+
where(field: string, op: FrameWhereOp, value: FrameDbValue): FrameQuery;
|
|
42
|
+
orderBy(field: string, dir?: 'asc' | 'desc'): FrameQuery;
|
|
43
|
+
limit(count: number): FrameQuery;
|
|
44
|
+
get(): Promise<FrameQuerySnapshot>;
|
|
45
|
+
onSnapshot(listener: FrameSnapshotListener<FrameQuerySnapshot>, error?: (reason: FrameDbError) => void): () => void;
|
|
46
|
+
}
|
|
47
|
+
export interface FrameCollectionRef extends FrameQuery {
|
|
48
|
+
readonly id: string;
|
|
49
|
+
readonly path: string;
|
|
50
|
+
readonly parent: FrameDocRef | null;
|
|
51
|
+
doc(id?: string): FrameDocRef;
|
|
52
|
+
add(data: FrameDocument): Promise<FrameDocRef>;
|
|
53
|
+
}
|
|
54
|
+
/** Times are Unix milliseconds; a busy lease only names its holder when the viewer owns it. */
|
|
55
|
+
export type FrameLease = {
|
|
56
|
+
acquired: true;
|
|
57
|
+
version: number;
|
|
58
|
+
expiresAt: number;
|
|
59
|
+
holder: string;
|
|
60
|
+
} | {
|
|
61
|
+
acquired: false;
|
|
62
|
+
expiresAt?: number;
|
|
63
|
+
holder?: string;
|
|
64
|
+
};
|
|
65
|
+
export interface FrameLeaseOptions {
|
|
66
|
+
holder?: string;
|
|
67
|
+
ttlMs?: number;
|
|
68
|
+
data?: FrameDocument;
|
|
69
|
+
}
|
|
70
|
+
export interface FrameDocRef {
|
|
71
|
+
readonly id: string;
|
|
72
|
+
readonly path: string;
|
|
73
|
+
readonly parent: FrameCollectionRef;
|
|
74
|
+
collection(name: string): FrameCollectionRef;
|
|
75
|
+
get(): Promise<FrameDocSnapshot>;
|
|
76
|
+
set(data: FrameDocument): Promise<void>;
|
|
77
|
+
update(data: FrameDocument): Promise<void>;
|
|
78
|
+
delete(): Promise<void>;
|
|
79
|
+
acquire(options?: FrameLeaseOptions): Promise<FrameLease>;
|
|
80
|
+
release(holder?: string): Promise<{
|
|
81
|
+
released: boolean;
|
|
82
|
+
}>;
|
|
83
|
+
onSnapshot(listener: FrameSnapshotListener<FrameDocSnapshot>, error?: (reason: FrameDbError) => void): () => void;
|
|
84
|
+
}
|
|
85
|
+
export interface FrameDatabase {
|
|
86
|
+
collection(path: string): FrameCollectionRef;
|
|
87
|
+
doc(path: string): FrameDocRef;
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { BlobInfo, BlobList, FileEntry, FrameLevel, Json, JsonObject, PublishResult } from './page-values.js';
|
|
2
|
+
import type { LocalUser, Peer } from './wire.js';
|
|
3
|
+
export type PageFileContent = string | Blob | ArrayBuffer | ArrayBufferView;
|
|
4
|
+
export type PageFileWrite = PageFileContent | null | {
|
|
5
|
+
content: PageFileContent;
|
|
6
|
+
contentType?: string;
|
|
7
|
+
ifMatch?: string | null;
|
|
8
|
+
} | {
|
|
9
|
+
delete: true;
|
|
10
|
+
ifMatch?: string | null;
|
|
11
|
+
};
|
|
12
|
+
export interface PageArtifactInfo {
|
|
13
|
+
id: string;
|
|
14
|
+
title: string;
|
|
15
|
+
type: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface PageArtifact {
|
|
18
|
+
readonly id: string;
|
|
19
|
+
publish(html: string): Promise<PublishResult>;
|
|
20
|
+
publish(files: Record<string, PageFileWrite>, options?: {
|
|
21
|
+
ifMatch?: Record<string, string | null>;
|
|
22
|
+
}): Promise<PublishResult>;
|
|
23
|
+
sync(): Promise<{
|
|
24
|
+
version: string;
|
|
25
|
+
files: Record<string, FileEntry>;
|
|
26
|
+
}>;
|
|
27
|
+
metadata(refs: string | string[]): Promise<Record<string, PageArtifactInfo>>;
|
|
28
|
+
search(query: string, options?: Record<string, unknown>): Promise<{
|
|
29
|
+
results: PageArtifactInfo[];
|
|
30
|
+
}>;
|
|
31
|
+
edit(): Promise<never>;
|
|
32
|
+
}
|
|
33
|
+
export interface PageAssets {
|
|
34
|
+
upload(blob: Blob, options?: {
|
|
35
|
+
type?: string;
|
|
36
|
+
}): Promise<BlobInfo>;
|
|
37
|
+
list(): Promise<BlobList>;
|
|
38
|
+
delete(ref: string | {
|
|
39
|
+
id?: string;
|
|
40
|
+
url?: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
deleted: boolean;
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
export interface PageDownloads {
|
|
46
|
+
save(options: {
|
|
47
|
+
filename?: string;
|
|
48
|
+
data: PageFileContent | Json;
|
|
49
|
+
request?: string;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
status: 'saved';
|
|
52
|
+
}>;
|
|
53
|
+
}
|
|
54
|
+
export type PagePermissionState = PermissionState | 'unavailable';
|
|
55
|
+
export interface PagePermissions {
|
|
56
|
+
state(): Promise<Record<string, PagePermissionState>>;
|
|
57
|
+
state(name: string): Promise<PagePermissionState>;
|
|
58
|
+
state(name?: string): Promise<PagePermissionState | Record<string, PagePermissionState>>;
|
|
59
|
+
request(names?: string | string[]): Promise<Record<string, PagePermissionState>>;
|
|
60
|
+
manage(): Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
export type PageMe = Omit<LocalUser, 'id'> & {
|
|
63
|
+
id: string | null;
|
|
64
|
+
};
|
|
65
|
+
export interface PagePerson extends Pick<LocalUser, 'id' | 'name' | 'avatarUrl' | 'color' | 'email'> {
|
|
66
|
+
isMe: boolean;
|
|
67
|
+
guest: boolean;
|
|
68
|
+
roles: null;
|
|
69
|
+
}
|
|
70
|
+
export interface PageUser {
|
|
71
|
+
me(): Promise<PageMe>;
|
|
72
|
+
id(): Promise<string | null>;
|
|
73
|
+
name(): Promise<string>;
|
|
74
|
+
avatarUrl(): Promise<string | null>;
|
|
75
|
+
email(): Promise<string | null>;
|
|
76
|
+
isOwner(): Promise<boolean>;
|
|
77
|
+
canEdit(): Promise<boolean>;
|
|
78
|
+
can(permission: string): Promise<boolean | null>;
|
|
79
|
+
profiles(ids: string | string[]): Promise<Record<string, PagePerson> | null>;
|
|
80
|
+
access(ids: string | string[]): Promise<Record<string, FrameLevel | null> | null>;
|
|
81
|
+
search(query: string): Promise<PagePerson[] | null>;
|
|
82
|
+
}
|
|
83
|
+
export interface PageRoomPeer extends Peer {
|
|
84
|
+
isMe: boolean;
|
|
85
|
+
}
|
|
86
|
+
export interface PageRoomSender {
|
|
87
|
+
peer: string;
|
|
88
|
+
by: string | null;
|
|
89
|
+
isMe: boolean;
|
|
90
|
+
sameTab: boolean;
|
|
91
|
+
kind: 'user';
|
|
92
|
+
guest: false;
|
|
93
|
+
}
|
|
94
|
+
export type PageRoomMessageListener = (data: Json, sender: PageRoomSender) => void;
|
|
95
|
+
export type PageSendStatus = 'sending' | 'sent' | 'failed';
|
|
96
|
+
export interface PageRoom {
|
|
97
|
+
emit(topic: string, data: Json): Promise<Record<string, never>>;
|
|
98
|
+
on(topic: string, listener: PageRoomMessageListener): () => void;
|
|
99
|
+
presence(patch: JsonObject): Promise<Record<string, never>>;
|
|
100
|
+
peers(): PageRoomPeer[];
|
|
101
|
+
onPeers(listener: (peers: PageRoomPeer[]) => void): () => void;
|
|
102
|
+
connected(): boolean;
|
|
103
|
+
onConnection(listener: (connected: boolean) => void): () => void;
|
|
104
|
+
sendToAgentSession(data: Json, options?: {
|
|
105
|
+
deliver?: 'send' | 'stage';
|
|
106
|
+
onStatus?: (status: PageSendStatus) => void;
|
|
107
|
+
}): Promise<{
|
|
108
|
+
to: string;
|
|
109
|
+
}>;
|
|
110
|
+
canSendToAgentSession(): Promise<boolean>;
|
|
111
|
+
join(name: string): PageRoom;
|
|
112
|
+
}
|
|
113
|
+
export interface FrameServices {
|
|
114
|
+
artifact: PageArtifact;
|
|
115
|
+
self: PageArtifact;
|
|
116
|
+
assets: PageAssets;
|
|
117
|
+
downloads: PageDownloads;
|
|
118
|
+
permissions: PagePermissions;
|
|
119
|
+
user: PageUser;
|
|
120
|
+
room: PageRoom;
|
|
121
|
+
network: {
|
|
122
|
+
origins(): Promise<string[]>;
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|