@open-artifact/harness-kit 0.0.0-stage → 0.14.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 +96 -2
- package/package.json +48 -4
- package/src/client.d.ts +110 -0
- package/src/client.js +272 -0
- package/src/context.d.ts +31 -0
- package/src/context.js +61 -0
- package/src/daemon.d.ts +108 -0
- package/src/daemon.js +200 -0
- package/src/embed/index.d.ts +183 -0
- package/src/embed/index.js +343 -0
- package/src/embed/remote.d.ts +39 -0
- package/src/embed/remote.js +51 -0
- package/src/harness.d.ts +237 -0
- package/src/harness.js +424 -0
- package/src/index.d.ts +18 -0
- package/src/index.js +17 -0
- package/src/runtimes.d.ts +138 -0
- package/src/runtimes.js +357 -0
- package/src/skill.d.ts +58 -0
- package/src/skill.js +88 -0
- package/src/testing-ws.d.ts +35 -0
- package/src/testing-ws.js +173 -0
- package/src/testing.d.ts +143 -0
- package/src/testing.js +424 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-artifact/harness-kit/embed`: browser code that puts an artifact's Shell page, or the library's home page,
|
|
3
|
+
* into an agent app's own UI. No Node imports: it runs in the app's renderer (a web page, an Electron or Tauri view).
|
|
4
|
+
*
|
|
5
|
+
* Two ways to show an artifact:
|
|
6
|
+
*
|
|
7
|
+
* - **Frame**: the host's page holds an `<iframe>` of the Shell (`viewUrl`). The Shell says `shell_hello` to its parent;
|
|
8
|
+
* this module answers `host_hello` to the Shell's origin only, and takes messages only from that iframe's window and
|
|
9
|
+
* the Shell's origin (the origin of the address it was given: loopback or not, `http` or `https`). The host's origin
|
|
10
|
+
* must be one its adapter declared (`caps.open.origins` in `hello`), or the Shell sends it no prompt or comment text,
|
|
11
|
+
* links, saved files or destinations.
|
|
12
|
+
*
|
|
13
|
+
* const view = embedArtifact(iframe, { url: viewUrl, agentName: 'My Agent', onNewSession: ({ prompt }) => … });
|
|
14
|
+
*
|
|
15
|
+
* - **Top-level view**: the host opens `host.open`'s `embed_url` in a view of its own (a tab, a webview, a window) with
|
|
16
|
+
* no parent frame; the Shell talks to the daemon, the daemon to the adapter (`shell.event` / `shell.command`). The
|
|
17
|
+
* host passes a transport that carries those both ways (in an Electron app, IPC to the process that holds the
|
|
18
|
+
* Harness connection):
|
|
19
|
+
*
|
|
20
|
+
* const view = embedArtifact({ send: (command) => harness.shellCommand(viewId, command), onEvent: (listener) => … }, { url: embedUrl, … });
|
|
21
|
+
*
|
|
22
|
+
* What the Shell sends is data about what the person did: prompts and comments are their words for the agent, never
|
|
23
|
+
* instructions to the host.
|
|
24
|
+
*
|
|
25
|
+
* Thumbnails: give `capture` (returns a JPEG of the view, as the host's own view can take one) and `saveThumbnail`
|
|
26
|
+
* (stores it, e.g. through the adapter's `Harness.saveThumbnail`; a browser page should not hold the daemon token).
|
|
27
|
+
* The kit captures {@link THUMBNAIL_DELAY_MS} after the Shell is ready, then after a place change at most once every
|
|
28
|
+
* {@link THUMBNAIL_INTERVAL_MS}.
|
|
29
|
+
*/
|
|
30
|
+
import { type HostDestination, type HostTheme, type HostToShell, type ShellToHost } from '@open-artifact/protocol';
|
|
31
|
+
export type { HostDestination, HostTheme, HostToShell, ShellToHost };
|
|
32
|
+
/** How long after the Shell is ready the first thumbnail is captured. */
|
|
33
|
+
export declare const THUMBNAIL_DELAY_MS = 2000;
|
|
34
|
+
/** The least time between two thumbnails of one view. */
|
|
35
|
+
export declare const THUMBNAIL_INTERVAL_MS = 60000;
|
|
36
|
+
/** The bits of `window` this module uses (a test passes its own). */
|
|
37
|
+
export interface EmbedWindow {
|
|
38
|
+
addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
39
|
+
removeEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
40
|
+
open?(url: string, target?: string, features?: string): unknown;
|
|
41
|
+
}
|
|
42
|
+
/** The bits of an `<iframe>` this module uses. */
|
|
43
|
+
export interface EmbedFrame {
|
|
44
|
+
src: string;
|
|
45
|
+
readonly contentWindow: {
|
|
46
|
+
postMessage(message: unknown, targetOrigin: string): void;
|
|
47
|
+
} | null;
|
|
48
|
+
}
|
|
49
|
+
/** Carries host messages to a top-level view's Shell and its messages back (through the adapter and the daemon). */
|
|
50
|
+
export interface EmbedTransport {
|
|
51
|
+
/** Sends a host message to the Shell (the adapter calls `Harness.shellCommand(view, command)`). */
|
|
52
|
+
send(command: HostToShell): void | Promise<void>;
|
|
53
|
+
/** Listens to what the Shell says (the adapter's `on('shell')` events of this view); returns a function that stops. */
|
|
54
|
+
onEvent(listener: (event: unknown) => void): () => void;
|
|
55
|
+
}
|
|
56
|
+
/** The artifact a Shell said hello for. */
|
|
57
|
+
export interface EmbedReadyInfo {
|
|
58
|
+
/** `open-artifact://artifact/<id>`. */
|
|
59
|
+
artifact: string;
|
|
60
|
+
id: string;
|
|
61
|
+
title: string;
|
|
62
|
+
type: string;
|
|
63
|
+
/** The Shell's version and host protocol. */
|
|
64
|
+
version: string;
|
|
65
|
+
protocol: number;
|
|
66
|
+
}
|
|
67
|
+
/** Options of {@link embedArtifact}. */
|
|
68
|
+
export interface EmbedArtifactOptions {
|
|
69
|
+
/**
|
|
70
|
+
* The Shell's address: the artifact's `viewUrl` (frame) or `embed_url` (top-level view). In frame mode it is set as
|
|
71
|
+
* the iframe's `src` when that differs; the Shell's origin is taken from it.
|
|
72
|
+
*/
|
|
73
|
+
url?: string;
|
|
74
|
+
/** The app's name, shown by the Shell ("Ask My Agent"). */
|
|
75
|
+
agentName?: string;
|
|
76
|
+
/** The harness the host belongs to. */
|
|
77
|
+
harness?: string;
|
|
78
|
+
/** The host draws its own toolbar: the Shell leaves out its close button. */
|
|
79
|
+
toolbar?: boolean;
|
|
80
|
+
/** The host's colour scheme and accent (`#rrggbb`). */
|
|
81
|
+
theme?: HostTheme;
|
|
82
|
+
/** Places the host sends exports to; the Shell lists at most {@link MAX_HOST_DESTINATIONS} in its Export tab. */
|
|
83
|
+
destinations?: HostDestination[];
|
|
84
|
+
/** The Shell said hello and took the host's answer. */
|
|
85
|
+
onReady?(info: EmbedReadyInfo): void;
|
|
86
|
+
/** Show the session that owns the artifact (`session` is `null` when none does). Sets caps `sessions`. */
|
|
87
|
+
onOpenSession?(request: {
|
|
88
|
+
session: string | null;
|
|
89
|
+
harness: string;
|
|
90
|
+
artifact: string | null;
|
|
91
|
+
}): void;
|
|
92
|
+
/** Start a new session with this prompt about the artifact (the person's words; empty to an undeclared host). Sets caps `sessions`. */
|
|
93
|
+
onNewSession?(request: {
|
|
94
|
+
prompt: string;
|
|
95
|
+
artifact: string;
|
|
96
|
+
}): void;
|
|
97
|
+
/** A comment was sent to the agent (its text; empty to an undeclared host). */
|
|
98
|
+
onCommentSent?(event: {
|
|
99
|
+
text: string;
|
|
100
|
+
}): void;
|
|
101
|
+
/** Where the artifact is now: a fragment to keep and send back with `goto`. */
|
|
102
|
+
onPlace?(event: {
|
|
103
|
+
artifact: string;
|
|
104
|
+
hash: string;
|
|
105
|
+
}): void;
|
|
106
|
+
/** The Shell's close button. */
|
|
107
|
+
onClose?(): void;
|
|
108
|
+
/** The person followed a link out of the Shell. Without this, the kit opens it with `window.open`. */
|
|
109
|
+
onOpenExternal?(url: string): void;
|
|
110
|
+
/** An export was saved to this file (absolute path on the computer that runs the type's server). */
|
|
111
|
+
onSavedFile?(path: string): void;
|
|
112
|
+
/** Takes a JPEG of the view, or `null` when it cannot now. */
|
|
113
|
+
capture?(): Promise<Blob | Uint8Array | ArrayBuffer | null> | Blob | Uint8Array | ArrayBuffer | null;
|
|
114
|
+
/** Stores a captured thumbnail (JPEG bytes) of `artifact`. */
|
|
115
|
+
saveThumbnail?(jpeg: Uint8Array, artifact: string): Promise<void> | void;
|
|
116
|
+
/** A thumbnail or a callback failed (default: ignored). */
|
|
117
|
+
onError?(error: unknown): void;
|
|
118
|
+
/** The window to listen on (default `globalThis.window`). */
|
|
119
|
+
window?: EmbedWindow;
|
|
120
|
+
}
|
|
121
|
+
/** An embedded artifact. */
|
|
122
|
+
export interface EmbeddedArtifact {
|
|
123
|
+
/** The artifact the Shell said hello for, or `null` before. */
|
|
124
|
+
readonly artifact: string | null;
|
|
125
|
+
/** The last place the Shell reported, or `null`. */
|
|
126
|
+
readonly place: string | null;
|
|
127
|
+
/** Resolves once the Shell is ready. */
|
|
128
|
+
readonly ready: Promise<EmbedReadyInfo>;
|
|
129
|
+
/** Shows a place (a fragment from `onPlace`, or a link's own). */
|
|
130
|
+
goto(hash: string): void;
|
|
131
|
+
/** Follows a new colour scheme or accent (sends `host_hello` again). */
|
|
132
|
+
setTheme(theme: HostTheme): void;
|
|
133
|
+
/** The Shell's address with the place last reported (without a view ticket), or `null` when unknown. */
|
|
134
|
+
url(): string | null;
|
|
135
|
+
/** Stops listening (and leaves the iframe as it is). */
|
|
136
|
+
destroy(): void;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Shows an artifact's Shell in the host's UI: `target` is the `<iframe>` (frame mode) or a transport to a top-level
|
|
140
|
+
* view (see the module's notes). Answers the Shell's hello and turns its messages into the callbacks.
|
|
141
|
+
*/
|
|
142
|
+
export declare function embedArtifact(target: EmbedFrame | EmbedTransport, options?: EmbedArtifactOptions): EmbeddedArtifact;
|
|
143
|
+
/** An artifact the home page asked the host to show. */
|
|
144
|
+
export interface LibraryArtifact {
|
|
145
|
+
url: string;
|
|
146
|
+
id: string;
|
|
147
|
+
title: string;
|
|
148
|
+
type: string;
|
|
149
|
+
}
|
|
150
|
+
/** Options of {@link embedLibrary}. */
|
|
151
|
+
export interface EmbedLibraryOptions {
|
|
152
|
+
/** The home page's address (the daemon's origin); set as the iframe's `src` when it differs. */
|
|
153
|
+
url?: string;
|
|
154
|
+
/** The harness the host belongs to: the page lists only artifacts of its sessions. */
|
|
155
|
+
harness?: string;
|
|
156
|
+
/** Only artifacts of sessions working in this folder (or under it). */
|
|
157
|
+
workspace?: string;
|
|
158
|
+
agentName?: string;
|
|
159
|
+
theme?: HostTheme;
|
|
160
|
+
/** Show this artifact (sets caps `artifacts`: the page sends it instead of navigating). */
|
|
161
|
+
onOpenArtifact?(artifact: LibraryArtifact): void;
|
|
162
|
+
onOpenSession?(request: {
|
|
163
|
+
session: string | null;
|
|
164
|
+
harness: string;
|
|
165
|
+
}): void;
|
|
166
|
+
onNewSession?(request: {
|
|
167
|
+
prompt: string;
|
|
168
|
+
artifact: string;
|
|
169
|
+
}): void;
|
|
170
|
+
/** The home page took the host's answer. */
|
|
171
|
+
onReady?(): void;
|
|
172
|
+
onError?(error: unknown): void;
|
|
173
|
+
window?: EmbedWindow;
|
|
174
|
+
}
|
|
175
|
+
/** The embedded home page. */
|
|
176
|
+
export interface EmbeddedLibrary {
|
|
177
|
+
/** Follows a new colour scheme or accent. */
|
|
178
|
+
setTheme(theme: HostTheme): void;
|
|
179
|
+
destroy(): void;
|
|
180
|
+
}
|
|
181
|
+
/** Shows the library's home page in an `<iframe>`, listing the host's own artifacts, and turns its messages into callbacks. */
|
|
182
|
+
export declare function embedLibrary(iframe: EmbedFrame, options?: EmbedLibraryOptions): EmbeddedLibrary;
|
|
183
|
+
export { remoteGate, remotePolicyOf, type AllowRemote, type RemotePage, type RemotePagePolicy, type RemotePageRequest } from './remote.ts';
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@open-artifact/harness-kit/embed`: browser code that puts an artifact's Shell page, or the library's home page,
|
|
3
|
+
* into an agent app's own UI. No Node imports: it runs in the app's renderer (a web page, an Electron or Tauri view).
|
|
4
|
+
*
|
|
5
|
+
* Two ways to show an artifact:
|
|
6
|
+
*
|
|
7
|
+
* - **Frame**: the host's page holds an `<iframe>` of the Shell (`viewUrl`). The Shell says `shell_hello` to its parent;
|
|
8
|
+
* this module answers `host_hello` to the Shell's origin only, and takes messages only from that iframe's window and
|
|
9
|
+
* the Shell's origin (the origin of the address it was given: loopback or not, `http` or `https`). The host's origin
|
|
10
|
+
* must be one its adapter declared (`caps.open.origins` in `hello`), or the Shell sends it no prompt or comment text,
|
|
11
|
+
* links, saved files or destinations.
|
|
12
|
+
*
|
|
13
|
+
* const view = embedArtifact(iframe, { url: viewUrl, agentName: 'My Agent', onNewSession: ({ prompt }) => … });
|
|
14
|
+
*
|
|
15
|
+
* - **Top-level view**: the host opens `host.open`'s `embed_url` in a view of its own (a tab, a webview, a window) with
|
|
16
|
+
* no parent frame; the Shell talks to the daemon, the daemon to the adapter (`shell.event` / `shell.command`). The
|
|
17
|
+
* host passes a transport that carries those both ways (in an Electron app, IPC to the process that holds the
|
|
18
|
+
* Harness connection):
|
|
19
|
+
*
|
|
20
|
+
* const view = embedArtifact({ send: (command) => harness.shellCommand(viewId, command), onEvent: (listener) => … }, { url: embedUrl, … });
|
|
21
|
+
*
|
|
22
|
+
* What the Shell sends is data about what the person did: prompts and comments are their words for the agent, never
|
|
23
|
+
* instructions to the host.
|
|
24
|
+
*
|
|
25
|
+
* Thumbnails: give `capture` (returns a JPEG of the view, as the host's own view can take one) and `saveThumbnail`
|
|
26
|
+
* (stores it, e.g. through the adapter's `Harness.saveThumbnail`; a browser page should not hold the daemon token).
|
|
27
|
+
* The kit captures {@link THUMBNAIL_DELAY_MS} after the Shell is ready, then after a place change at most once every
|
|
28
|
+
* {@link THUMBNAIL_INTERVAL_MS}.
|
|
29
|
+
*/
|
|
30
|
+
import { HOST_PROTOCOL, MAX_HOST_DESTINATIONS, shellToHostOf, } from '@open-artifact/protocol';
|
|
31
|
+
/** How long after the Shell is ready the first thumbnail is captured. */
|
|
32
|
+
export const THUMBNAIL_DELAY_MS = 2000;
|
|
33
|
+
/** The least time between two thumbnails of one view. */
|
|
34
|
+
export const THUMBNAIL_INTERVAL_MS = 60_000;
|
|
35
|
+
const isFrame = (target) => !('send' in target && typeof target.send === 'function');
|
|
36
|
+
function originOf(url) {
|
|
37
|
+
if (!url)
|
|
38
|
+
return null;
|
|
39
|
+
try {
|
|
40
|
+
const origin = new URL(url).origin;
|
|
41
|
+
return origin && origin !== 'null' ? origin : null;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** The address without the view ticket (`oa_view`) and with `hash` as its fragment. */
|
|
48
|
+
function addressOf(url, hash) {
|
|
49
|
+
if (!url)
|
|
50
|
+
return null;
|
|
51
|
+
try {
|
|
52
|
+
const address = new URL(url);
|
|
53
|
+
address.searchParams.delete('oa_view');
|
|
54
|
+
if (hash)
|
|
55
|
+
address.hash = hash.startsWith('#') ? hash : `#${hash}`;
|
|
56
|
+
return address.href;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async function jpegOf(value) {
|
|
63
|
+
if (!value)
|
|
64
|
+
return null;
|
|
65
|
+
const bytes = value instanceof Uint8Array ? value : value instanceof ArrayBuffer ? new Uint8Array(value) : new Uint8Array(await value.arrayBuffer());
|
|
66
|
+
return bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8 ? bytes : null;
|
|
67
|
+
}
|
|
68
|
+
function defaultWindow() {
|
|
69
|
+
return globalThis.window ?? null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Shows an artifact's Shell in the host's UI: `target` is the `<iframe>` (frame mode) or a transport to a top-level
|
|
73
|
+
* view (see the module's notes). Answers the Shell's hello and turns its messages into the callbacks.
|
|
74
|
+
*/
|
|
75
|
+
export function embedArtifact(target, options = {}) {
|
|
76
|
+
const win = options.window ?? defaultWindow();
|
|
77
|
+
const frame = isFrame(target) ? target : null;
|
|
78
|
+
const transport = frame ? null : target;
|
|
79
|
+
if (frame && options.url && frame.src !== options.url)
|
|
80
|
+
frame.src = options.url;
|
|
81
|
+
const shellUrl = options.url ?? frame?.src;
|
|
82
|
+
const shellOrigin = frame ? originOf(shellUrl) : null;
|
|
83
|
+
if (frame && !shellOrigin)
|
|
84
|
+
throw new Error('embedArtifact: the Shell address (options.url or the iframe src) is not an http(s) address.');
|
|
85
|
+
if (frame && !win)
|
|
86
|
+
throw new Error('embedArtifact: no window to listen on.');
|
|
87
|
+
let theme = options.theme;
|
|
88
|
+
let info = null;
|
|
89
|
+
let place = null;
|
|
90
|
+
let helloed = false;
|
|
91
|
+
let stopped = false;
|
|
92
|
+
let resolveReady;
|
|
93
|
+
const ready = new Promise((resolve) => (resolveReady = resolve));
|
|
94
|
+
let thumbTimer = null;
|
|
95
|
+
let lastThumb = 0;
|
|
96
|
+
const report = (error) => {
|
|
97
|
+
try {
|
|
98
|
+
options.onError?.(error);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// The host's own error handler failed: nothing more to do.
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const safely = (run) => {
|
|
105
|
+
try {
|
|
106
|
+
run();
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
report(error);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const send = (message) => {
|
|
113
|
+
if (stopped)
|
|
114
|
+
return;
|
|
115
|
+
if (frame)
|
|
116
|
+
frame.contentWindow?.postMessage(message, shellOrigin);
|
|
117
|
+
else
|
|
118
|
+
void Promise.resolve(transport.send(message)).catch(report);
|
|
119
|
+
};
|
|
120
|
+
const hello = () => {
|
|
121
|
+
const caps = {};
|
|
122
|
+
if (options.onOpenSession || options.onNewSession)
|
|
123
|
+
caps.sessions = true;
|
|
124
|
+
if (options.toolbar)
|
|
125
|
+
caps.toolbar = true;
|
|
126
|
+
return {
|
|
127
|
+
openArtifact: 'host_hello',
|
|
128
|
+
caps,
|
|
129
|
+
protocol: HOST_PROTOCOL,
|
|
130
|
+
...(options.agentName ? { agentName: options.agentName } : {}),
|
|
131
|
+
...(options.harness ? { harness: options.harness } : {}),
|
|
132
|
+
...(theme ? { theme } : {}),
|
|
133
|
+
...(options.destinations?.length ? { destinations: options.destinations.slice(0, MAX_HOST_DESTINATIONS).map((d) => ({ id: d.id, label: d.label })) } : {}),
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
const thumbnail = () => {
|
|
137
|
+
thumbTimer = null;
|
|
138
|
+
if (stopped || !info || !options.capture || !options.saveThumbnail)
|
|
139
|
+
return;
|
|
140
|
+
lastThumb = Date.now();
|
|
141
|
+
const artifact = info.artifact;
|
|
142
|
+
void (async () => {
|
|
143
|
+
const jpeg = await jpegOf(await options.capture());
|
|
144
|
+
if (jpeg && !stopped)
|
|
145
|
+
await options.saveThumbnail(jpeg, artifact);
|
|
146
|
+
})().catch(report);
|
|
147
|
+
};
|
|
148
|
+
const scheduleThumbnail = (first) => {
|
|
149
|
+
if (stopped || thumbTimer || !options.capture || !options.saveThumbnail)
|
|
150
|
+
return;
|
|
151
|
+
const wait = first ? THUMBNAIL_DELAY_MS : Math.max(THUMBNAIL_DELAY_MS, lastThumb + THUMBNAIL_INTERVAL_MS - Date.now());
|
|
152
|
+
thumbTimer = setTimeout(thumbnail, wait);
|
|
153
|
+
};
|
|
154
|
+
const onShellMessage = (raw) => {
|
|
155
|
+
const message = shellToHostOf(raw);
|
|
156
|
+
if (!message || stopped)
|
|
157
|
+
return;
|
|
158
|
+
switch (message.openArtifact) {
|
|
159
|
+
case 'shell_hello':
|
|
160
|
+
info = { artifact: message.artifact, id: message.id, title: message.title, type: message.type, version: message.version, protocol: message.protocol };
|
|
161
|
+
helloed = true;
|
|
162
|
+
send(hello());
|
|
163
|
+
break;
|
|
164
|
+
case 'shell_ready': {
|
|
165
|
+
// A top-level view's hello may have come before the host listened: what `shell_ready` says is enough.
|
|
166
|
+
info ??= { artifact: message.artifact, id: message.artifact.replace(/^open-artifact:\/\/artifact\//, ''), title: '', type: '', version: '', protocol: HOST_PROTOCOL };
|
|
167
|
+
const ready = info;
|
|
168
|
+
safely(() => options.onReady?.(ready));
|
|
169
|
+
resolveReady(ready);
|
|
170
|
+
if (!lastThumb)
|
|
171
|
+
scheduleThumbnail(true);
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case 'place':
|
|
175
|
+
place = message.hash;
|
|
176
|
+
safely(() => options.onPlace?.({ artifact: message.artifact, hash: message.hash }));
|
|
177
|
+
if (lastThumb)
|
|
178
|
+
scheduleThumbnail(false);
|
|
179
|
+
break;
|
|
180
|
+
case 'new_session':
|
|
181
|
+
safely(() => options.onNewSession?.({ prompt: message.prompt, artifact: message.artifact }));
|
|
182
|
+
break;
|
|
183
|
+
case 'open_session':
|
|
184
|
+
safely(() => options.onOpenSession?.({ session: message.session, harness: message.harness, artifact: info?.artifact ?? null }));
|
|
185
|
+
break;
|
|
186
|
+
case 'comment_summon':
|
|
187
|
+
safely(() => options.onCommentSent?.({ text: message.text }));
|
|
188
|
+
break;
|
|
189
|
+
case 'close':
|
|
190
|
+
safely(() => options.onClose?.());
|
|
191
|
+
break;
|
|
192
|
+
case 'open_external':
|
|
193
|
+
safely(() => {
|
|
194
|
+
if (options.onOpenExternal)
|
|
195
|
+
options.onOpenExternal(message.url);
|
|
196
|
+
else
|
|
197
|
+
win?.open?.(message.url, '_blank', 'noopener');
|
|
198
|
+
});
|
|
199
|
+
break;
|
|
200
|
+
case 'saved_file':
|
|
201
|
+
safely(() => options.onSavedFile?.(message.path));
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const onWindowMessage = (event) => {
|
|
206
|
+
// Only the Shell's own window, from the Shell's origin.
|
|
207
|
+
if (!frame || event.source !== frame.contentWindow || event.origin !== shellOrigin)
|
|
208
|
+
return;
|
|
209
|
+
onShellMessage(event.data);
|
|
210
|
+
};
|
|
211
|
+
let stopTransport = null;
|
|
212
|
+
if (frame)
|
|
213
|
+
win.addEventListener('message', onWindowMessage);
|
|
214
|
+
else {
|
|
215
|
+
stopTransport = transport.onEvent(onShellMessage);
|
|
216
|
+
// A top-level view's Shell may have said hello before the host listened: answer now; it takes the latest.
|
|
217
|
+
send(hello());
|
|
218
|
+
helloed = true;
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
get artifact() {
|
|
222
|
+
return info?.artifact ?? null;
|
|
223
|
+
},
|
|
224
|
+
get place() {
|
|
225
|
+
return place;
|
|
226
|
+
},
|
|
227
|
+
ready,
|
|
228
|
+
goto(hash) {
|
|
229
|
+
if (typeof hash === 'string' && hash)
|
|
230
|
+
send({ openArtifact: 'goto', hash });
|
|
231
|
+
},
|
|
232
|
+
setTheme(next) {
|
|
233
|
+
theme = next;
|
|
234
|
+
if (helloed)
|
|
235
|
+
send(hello());
|
|
236
|
+
},
|
|
237
|
+
url: () => addressOf(shellUrl, place),
|
|
238
|
+
destroy() {
|
|
239
|
+
if (stopped)
|
|
240
|
+
return;
|
|
241
|
+
stopped = true;
|
|
242
|
+
if (thumbTimer)
|
|
243
|
+
clearTimeout(thumbTimer);
|
|
244
|
+
if (frame)
|
|
245
|
+
win?.removeEventListener('message', onWindowMessage);
|
|
246
|
+
stopTransport?.();
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
const text = (value, max) => (typeof value === 'string' ? value.slice(0, max) : '');
|
|
251
|
+
/** Shows the library's home page in an `<iframe>`, listing the host's own artifacts, and turns its messages into callbacks. */
|
|
252
|
+
export function embedLibrary(iframe, options = {}) {
|
|
253
|
+
const win = options.window ?? defaultWindow();
|
|
254
|
+
if (!win)
|
|
255
|
+
throw new Error('embedLibrary: no window to listen on.');
|
|
256
|
+
if (options.url && iframe.src !== options.url)
|
|
257
|
+
iframe.src = options.url;
|
|
258
|
+
const origin = originOf(options.url ?? iframe.src);
|
|
259
|
+
if (!origin)
|
|
260
|
+
throw new Error('embedLibrary: the home page address (options.url or the iframe src) is not an http(s) address.');
|
|
261
|
+
let theme = options.theme;
|
|
262
|
+
let helloed = false;
|
|
263
|
+
let stopped = false;
|
|
264
|
+
const hello = () => {
|
|
265
|
+
const caps = {};
|
|
266
|
+
if (options.onOpenSession || options.onNewSession)
|
|
267
|
+
caps.sessions = true;
|
|
268
|
+
if (options.onOpenArtifact)
|
|
269
|
+
caps.artifacts = true;
|
|
270
|
+
const library = {};
|
|
271
|
+
if (options.harness)
|
|
272
|
+
library.harness = options.harness;
|
|
273
|
+
if (options.workspace)
|
|
274
|
+
library.workspace = options.workspace;
|
|
275
|
+
return {
|
|
276
|
+
openArtifact: 'host_hello',
|
|
277
|
+
caps,
|
|
278
|
+
protocol: HOST_PROTOCOL,
|
|
279
|
+
...(options.harness ? { harness: options.harness } : {}),
|
|
280
|
+
...(options.agentName ? { agentName: options.agentName } : {}),
|
|
281
|
+
...(theme ? { theme } : {}),
|
|
282
|
+
...(library.harness || library.workspace ? { library } : {}),
|
|
283
|
+
};
|
|
284
|
+
};
|
|
285
|
+
const send = (message) => {
|
|
286
|
+
if (!stopped)
|
|
287
|
+
iframe.contentWindow?.postMessage(message, origin);
|
|
288
|
+
};
|
|
289
|
+
const safely = (run) => {
|
|
290
|
+
try {
|
|
291
|
+
run();
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
try {
|
|
295
|
+
options.onError?.(error);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
// Nothing more to do.
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
const onMessage = (event) => {
|
|
303
|
+
if (stopped || event.source !== iframe.contentWindow || event.origin !== origin)
|
|
304
|
+
return;
|
|
305
|
+
const data = event.data;
|
|
306
|
+
if (!data || typeof data !== 'object')
|
|
307
|
+
return;
|
|
308
|
+
switch (data.openArtifact) {
|
|
309
|
+
case 'home_hello':
|
|
310
|
+
helloed = true;
|
|
311
|
+
send(hello());
|
|
312
|
+
break;
|
|
313
|
+
case 'home_ready':
|
|
314
|
+
safely(() => options.onReady?.());
|
|
315
|
+
break;
|
|
316
|
+
case 'open_artifact': {
|
|
317
|
+
const artifact = { url: text(data.url, 200), id: text(data.id, 100), title: text(data.title, 500), type: text(data.type, 100) };
|
|
318
|
+
if (artifact.url)
|
|
319
|
+
safely(() => options.onOpenArtifact?.(artifact));
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
case 'open_session':
|
|
323
|
+
safely(() => options.onOpenSession?.({ session: typeof data.session === 'string' ? data.session.slice(0, 200) : null, harness: text(data.harness, 64) }));
|
|
324
|
+
break;
|
|
325
|
+
case 'new_session':
|
|
326
|
+
safely(() => options.onNewSession?.({ prompt: text(data.prompt, 8000), artifact: text(data.artifact, 200) }));
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
win.addEventListener('message', onMessage);
|
|
331
|
+
return {
|
|
332
|
+
setTheme(next) {
|
|
333
|
+
theme = next;
|
|
334
|
+
if (helloed)
|
|
335
|
+
send(hello());
|
|
336
|
+
},
|
|
337
|
+
destroy() {
|
|
338
|
+
stopped = true;
|
|
339
|
+
win.removeEventListener('message', onMessage);
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
export { remoteGate, remotePolicyOf } from "./remote.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a host shows a page that comes from another computer: a remote type's page (`host.open`'s `remote`).
|
|
3
|
+
*
|
|
4
|
+
* The host picks a policy (`HarnessOptions.embed.remote`, said in `hello` as `caps.open.remote`):
|
|
5
|
+
*
|
|
6
|
+
* - `allow`: show it, as any other page.
|
|
7
|
+
* - `ask` (the default): ask the person through the host's `allowRemote({ origin, type, title })`. A yes is kept per
|
|
8
|
+
* origin for the life of the process; a no (or a question that throws) is not, so the next open asks again. With no
|
|
9
|
+
* `allowRemote`, the answer is no.
|
|
10
|
+
* - `deny`: never show it; the daemon is told the page was not opened.
|
|
11
|
+
*
|
|
12
|
+
* No Node imports: an app may also run this in its renderer.
|
|
13
|
+
*/
|
|
14
|
+
import type { RemotePagePolicy } from '@open-artifact/protocol';
|
|
15
|
+
export type { RemotePagePolicy };
|
|
16
|
+
/** What `allowRemote` is asked: may a page from `origin` (of type `type`) be shown? */
|
|
17
|
+
export interface RemotePageRequest {
|
|
18
|
+
/** The other computer's origin, e.g. `https://types.example.com`. */
|
|
19
|
+
origin: string;
|
|
20
|
+
/** The type id. */
|
|
21
|
+
type: string;
|
|
22
|
+
/** The artifact's title, when the daemon said it. */
|
|
23
|
+
title?: string;
|
|
24
|
+
}
|
|
25
|
+
/** The host's answer: `true` shows the page. */
|
|
26
|
+
export type AllowRemote = (request: RemotePageRequest) => boolean | Promise<boolean>;
|
|
27
|
+
/** The remote part of a `host.open`: the type and the origins its page comes from. */
|
|
28
|
+
export interface RemotePage {
|
|
29
|
+
type: string;
|
|
30
|
+
origins: string[];
|
|
31
|
+
}
|
|
32
|
+
/** The policy a host's option names, or the default (`ask`) for anything else. */
|
|
33
|
+
export declare function remotePolicyOf(value: unknown): RemotePagePolicy;
|
|
34
|
+
/**
|
|
35
|
+
* A gate for remote pages under one policy: `(remote, title) → shown?`. A page with no `remote` (a type on this
|
|
36
|
+
* computer) always passes. Under `ask`, a yes is kept per origin for the gate's life; a no, or a question that
|
|
37
|
+
* throws, is not kept: the person is asked again the next time.
|
|
38
|
+
*/
|
|
39
|
+
export declare function remoteGate(policy?: RemotePagePolicy, allowRemote?: AllowRemote): (remote: RemotePage | null | undefined, title?: string) => Promise<boolean>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a host shows a page that comes from another computer: a remote type's page (`host.open`'s `remote`).
|
|
3
|
+
*
|
|
4
|
+
* The host picks a policy (`HarnessOptions.embed.remote`, said in `hello` as `caps.open.remote`):
|
|
5
|
+
*
|
|
6
|
+
* - `allow`: show it, as any other page.
|
|
7
|
+
* - `ask` (the default): ask the person through the host's `allowRemote({ origin, type, title })`. A yes is kept per
|
|
8
|
+
* origin for the life of the process; a no (or a question that throws) is not, so the next open asks again. With no
|
|
9
|
+
* `allowRemote`, the answer is no.
|
|
10
|
+
* - `deny`: never show it; the daemon is told the page was not opened.
|
|
11
|
+
*
|
|
12
|
+
* No Node imports: an app may also run this in its renderer.
|
|
13
|
+
*/
|
|
14
|
+
/** The policy a host's option names, or the default (`ask`) for anything else. */
|
|
15
|
+
export function remotePolicyOf(value) {
|
|
16
|
+
return value === 'allow' || value === 'deny' || value === 'ask' ? value : 'ask';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A gate for remote pages under one policy: `(remote, title) → shown?`. A page with no `remote` (a type on this
|
|
20
|
+
* computer) always passes. Under `ask`, a yes is kept per origin for the gate's life; a no, or a question that
|
|
21
|
+
* throws, is not kept: the person is asked again the next time.
|
|
22
|
+
*/
|
|
23
|
+
export function remoteGate(policy = 'ask', allowRemote) {
|
|
24
|
+
const answers = new Map();
|
|
25
|
+
const ask = (origin, type, title) => {
|
|
26
|
+
let answer = answers.get(origin);
|
|
27
|
+
if (!answer) {
|
|
28
|
+
answer = (async () => (await allowRemote({ origin, type, ...(title !== undefined ? { title } : {}) })) === true)();
|
|
29
|
+
// Shared while the question is open (two opens at once ask once); only a yes stays after it.
|
|
30
|
+
answers.set(origin, answer);
|
|
31
|
+
answer.then((yes) => {
|
|
32
|
+
if (!yes)
|
|
33
|
+
answers.delete(origin);
|
|
34
|
+
}, () => answers.delete(origin));
|
|
35
|
+
}
|
|
36
|
+
return answer.catch(() => false);
|
|
37
|
+
};
|
|
38
|
+
return async (remote, title) => {
|
|
39
|
+
if (!remote || !Array.isArray(remote.origins) || !remote.origins.length)
|
|
40
|
+
return true;
|
|
41
|
+
if (policy === 'allow')
|
|
42
|
+
return true;
|
|
43
|
+
if (policy === 'deny' || !allowRemote)
|
|
44
|
+
return false;
|
|
45
|
+
for (const origin of remote.origins) {
|
|
46
|
+
if (!(await ask(origin, remote.type, title)))
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
}
|