@openartifactsai/harness-kit 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 +210 -2
- package/package.json +52 -4
- package/src/client.d.ts +160 -0
- package/src/client.js +409 -0
- package/src/context.d.ts +31 -0
- package/src/context.js +61 -0
- package/src/daemon.d.ts +177 -0
- package/src/daemon.js +293 -0
- package/src/embed/index.d.ts +230 -0
- package/src/embed/index.js +376 -0
- package/src/embed/remote.d.ts +39 -0
- package/src/embed/remote.js +51 -0
- package/src/harness.d.ts +383 -0
- package/src/harness.js +678 -0
- package/src/index.d.ts +28 -0
- package/src/index.js +20 -0
- package/src/legacy.d.ts +106 -0
- package/src/legacy.js +278 -0
- package/src/mcp.d.ts +39 -0
- package/src/mcp.js +85 -0
- package/src/runtimes.d.ts +174 -0
- package/src/runtimes.js +398 -0
- package/src/skill.d.ts +58 -0
- package/src/skill.js +88 -0
- package/src/testing-ws.d.ts +44 -0
- package/src/testing-ws.js +204 -0
- package/src/testing.d.ts +227 -0
- package/src/testing.js +564 -0
package/src/daemon.js
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finding and starting the local daemon, and calling its Library API.
|
|
3
|
+
*
|
|
4
|
+
* One daemon runs per Open Artifacts home (`~/.agents/open-artifacts`, or `$OPEN_ARTIFACTS_HOME`); it writes where it
|
|
5
|
+
* listens and its token to `daemon.json` there. Uses the global `fetch` (Node ≥ 22, Electron's Node).
|
|
6
|
+
*
|
|
7
|
+
* Which runtime starts it is chosen in `runtimes.ts` (design 10.1): the newest compatible one among the caller's own,
|
|
8
|
+
* the ones adapters registered and the shared ones. `$OPEN_ARTIFACTS_DAEMON_BIN` and an explicit `bin` skip the choice.
|
|
9
|
+
*/
|
|
10
|
+
import { spawn } from 'node:child_process';
|
|
11
|
+
import { closeSync, mkdirSync, openSync, readFileSync } from 'node:fs';
|
|
12
|
+
import { join } from 'node:path';
|
|
13
|
+
import { OpenArtifactsError, PROTOCOL_MAX, PROTOCOL_MIN } from '@openartifactsai/protocol';
|
|
14
|
+
import { fetchCurrentOrLegacy, legacyUrl, markerOf, withLegacyHeaderNames } from '@openartifactsai/protocol/legacy-wire';
|
|
15
|
+
import { adoptLegacyEnv, homeFromEnv, resolveHome, withLegacyEnv } from "./legacy.js";
|
|
16
|
+
import { compareVersions, rankRuntimes, readRuntimePin, realOrResolved, runtimeCandidates, runtimeEnv, within } from "./runtimes.js";
|
|
17
|
+
/**
|
|
18
|
+
* The Open Artifacts home: `home` when given, else `$OPEN_ARTIFACTS_HOME`, else `~/.agents/open-artifacts`. A leading `~`
|
|
19
|
+
* is the home folder. A home from before 0.23 (`~/.agents/open-artifact`) is moved there once (see `resolveHome`).
|
|
20
|
+
*/
|
|
21
|
+
export function openArtifactsHome(home) {
|
|
22
|
+
return resolveHome(home ? { home } : {});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The environment a daemon is started with: both spellings of every variable (the runtime may be older than 0.23),
|
|
26
|
+
* and the old `OPEN_ARTIFACT_HOME` naming the home this kit found when no home is named, so an older runtime uses
|
|
27
|
+
* that one too.
|
|
28
|
+
*/
|
|
29
|
+
function daemonEnv(env, home) {
|
|
30
|
+
const out = withLegacyEnv(home ? { ...env, OPEN_ARTIFACTS_HOME: home } : env);
|
|
31
|
+
if (!homeFromEnv(out))
|
|
32
|
+
out.OPEN_ARTIFACT_HOME = resolveHome({ env: out });
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
/** `env` with the old names of its variables adopted, as a copy (the caller's object is left alone). */
|
|
36
|
+
function adopted(env) {
|
|
37
|
+
const copy = { ...env };
|
|
38
|
+
adoptLegacyEnv(copy);
|
|
39
|
+
return copy;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Whether version `a` is older than `b`, by major.minor.patch only: a pre-release (a local build) is not replaced by
|
|
43
|
+
* its own release. Choosing among runtimes uses full semver order (`compareVersions`).
|
|
44
|
+
*/
|
|
45
|
+
export function isOlderVersion(a, b) {
|
|
46
|
+
const core = (v) => v.replace(/[-+].*$/, '');
|
|
47
|
+
return compareVersions(core(a), core(b)) < 0;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The `open-artifacts` entry to start when no daemon runs, as far as the kit knows it: `$OPEN_ARTIFACTS_DAEMON_BIN`.
|
|
51
|
+
* `undefined` when it is not set; a host then passes `bin` (or `runtime.dir` to `createHarness`).
|
|
52
|
+
*/
|
|
53
|
+
export function defaultDaemonBin() {
|
|
54
|
+
return process.env.OPEN_ARTIFACTS_DAEMON_BIN || undefined;
|
|
55
|
+
}
|
|
56
|
+
/** True when a process with this id is alive. */
|
|
57
|
+
function alive(pid) {
|
|
58
|
+
try {
|
|
59
|
+
process.kill(pid, 0);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
return error.code === 'EPERM';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* `daemon.json` of the home (see {@link openArtifactsHome}) when its process is alive and answers health; `null`
|
|
68
|
+
* otherwise.
|
|
69
|
+
*/
|
|
70
|
+
export async function readDaemon(home) {
|
|
71
|
+
let state = null;
|
|
72
|
+
try {
|
|
73
|
+
state = JSON.parse(readFileSync(join(openArtifactsHome(home), 'daemon.json'), 'utf8'));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
if (!state?.url || !state.pid || !alive(state.pid))
|
|
79
|
+
return null;
|
|
80
|
+
try {
|
|
81
|
+
const body = await daemonHealth(state, 1500);
|
|
82
|
+
return markerOf(body) === 1 ? state : null;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* What each daemon this process talked to answers to (by address and process): a daemon from before 0.23 answers only
|
|
90
|
+
* `/__open-artifact/*` and `/api/open-artifact.*`. Such a daemon is still found (so a newer runtime replaces it, as any
|
|
91
|
+
* older daemon) and still called while it runs.
|
|
92
|
+
*/
|
|
93
|
+
const peers = new Map();
|
|
94
|
+
function peerOf(state) {
|
|
95
|
+
const key = `${state.url} ${state.pid}`;
|
|
96
|
+
let peer = peers.get(key);
|
|
97
|
+
if (!peer)
|
|
98
|
+
peers.set(key, (peer = {}));
|
|
99
|
+
return peer;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A fetch of the daemon at the path given, or at its name before 0.23 when that daemon is older; the older path is sent
|
|
103
|
+
* the request headers by their older names too (`X-Open-Artifact-*`).
|
|
104
|
+
*/
|
|
105
|
+
function daemonFetch(state, url, init) {
|
|
106
|
+
return fetchCurrentOrLegacy((target, options) => fetch(target, target === url ? options : { ...options, headers: withLegacyHeaderNames((options?.headers ?? {})) }), url, init, peerOf(state), legacyUrl);
|
|
107
|
+
}
|
|
108
|
+
/** The daemon's health answer (`/__open-artifacts/health`; a daemon before 0.23: `/__open-artifact/health`). */
|
|
109
|
+
export async function daemonHealth(state, timeoutMs = 3000) {
|
|
110
|
+
const response = await daemonFetch(state, `${state.url}/__open-artifacts/health`, { signal: AbortSignal.timeout(timeoutMs) });
|
|
111
|
+
return (await response.json());
|
|
112
|
+
}
|
|
113
|
+
/** The entry override in `env`, unless it is the caller's own runtime (a launcher names itself there). */
|
|
114
|
+
function envOverride(env, own) {
|
|
115
|
+
const named = env.OPEN_ARTIFACTS_DAEMON_BIN;
|
|
116
|
+
if (!named)
|
|
117
|
+
return undefined;
|
|
118
|
+
if (own?.bin && realOrResolved(own.bin) === realOrResolved(named))
|
|
119
|
+
return undefined;
|
|
120
|
+
if (own?.dir && within(own.dir, named))
|
|
121
|
+
return undefined;
|
|
122
|
+
return named;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* What {@link ensureDaemon} starts for these options: the override when there is one, else the compatible runtimes
|
|
126
|
+
* of the home, newest first (`runtimes.ts`).
|
|
127
|
+
*/
|
|
128
|
+
export function daemonStartPlan(options = {}) {
|
|
129
|
+
const env = adopted(options.env ?? process.env);
|
|
130
|
+
const home = openArtifactsHome(options.home ?? env.OPEN_ARTIFACTS_HOME ?? undefined);
|
|
131
|
+
const override = options.bin ?? envOverride(env, options.own);
|
|
132
|
+
if (override) {
|
|
133
|
+
const version = options.version ?? (options.own?.bin && realOrResolved(options.own.bin) === realOrResolved(override) ? options.own.version : undefined);
|
|
134
|
+
return { tries: [{ bin: override, ...(version ? { version } : {}), env }], ...(version ? { floor: version } : {}), override: true };
|
|
135
|
+
}
|
|
136
|
+
const candidates = runtimeCandidates(home, options.own);
|
|
137
|
+
const ranked = rankRuntimes(candidates, { protocols: options.protocols ?? { min: PROTOCOL_MIN, max: PROTOCOL_MAX }, pin: readRuntimePin(home) });
|
|
138
|
+
const floor = options.own?.version ?? options.version;
|
|
139
|
+
return {
|
|
140
|
+
tries: ranked.slice(0, 3).map((candidate) => ({ bin: candidate.bin, version: candidate.version, env: runtimeEnv(env, candidate, candidates), candidate })),
|
|
141
|
+
...(floor ? { floor } : {}),
|
|
142
|
+
override: false,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
/** Stops a running daemon and waits until it has gone, so a new one can take its port. */
|
|
146
|
+
async function replaceDaemon(state, timeoutMs) {
|
|
147
|
+
try {
|
|
148
|
+
process.kill(state.pid, 'SIGTERM');
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const deadline = Date.now() + timeoutMs;
|
|
154
|
+
while (alive(state.pid) && Date.now() < deadline)
|
|
155
|
+
await new Promise((done) => setTimeout(done, 100));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Starts `bin daemon run` in the background for a home, its output appended to `<home>/logs/daemon.out`. Runs with
|
|
159
|
+
* this process's executable (`ELECTRON_RUN_AS_NODE` lets an Electron host's run plain Node).
|
|
160
|
+
*/
|
|
161
|
+
export function startDaemonProcess(bin, options = {}) {
|
|
162
|
+
const env = adopted(options.env ?? process.env);
|
|
163
|
+
const logs = join(openArtifactsHome(options.home ?? env.OPEN_ARTIFACTS_HOME ?? undefined), 'logs');
|
|
164
|
+
mkdirSync(logs, { recursive: true });
|
|
165
|
+
const fd = openSync(join(logs, 'daemon.out'), 'a');
|
|
166
|
+
try {
|
|
167
|
+
const child = spawn(process.execPath, [bin, 'daemon', 'run'], {
|
|
168
|
+
detached: true,
|
|
169
|
+
stdio: ['ignore', fd, fd],
|
|
170
|
+
env: { ...daemonEnv(env, options.home), ELECTRON_RUN_AS_NODE: '1' },
|
|
171
|
+
});
|
|
172
|
+
child.on('error', () => undefined);
|
|
173
|
+
child.unref();
|
|
174
|
+
return child;
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
closeSync(fd);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* The running daemon, started in the background when there is none. The runtime that starts is the newest one
|
|
182
|
+
* compatible with the caller (see {@link daemonStartPlan}); when it exits without starting, the next one is tried. A
|
|
183
|
+
* running daemon older than the caller's own runtime (`own`, or `version` with `bin`) is replaced; the harnesses
|
|
184
|
+
* connected to it reconnect. A newer runtime that is only on disk does not replace a running daemon: the daemon
|
|
185
|
+
* switches to it by itself once idle.
|
|
186
|
+
*/
|
|
187
|
+
export async function ensureDaemon(options = {}) {
|
|
188
|
+
const { timeoutMs = 15_000 } = options;
|
|
189
|
+
const env = adopted(options.env ?? process.env);
|
|
190
|
+
const home = options.home ?? env.OPEN_ARTIFACTS_HOME ?? undefined;
|
|
191
|
+
const running = await readDaemon(home);
|
|
192
|
+
const plan = daemonStartPlan({ ...options, env });
|
|
193
|
+
if (running && (!plan.floor || !isOlderVersion(running.version ?? '0.0.0', plan.floor)))
|
|
194
|
+
return running;
|
|
195
|
+
if (!plan.tries.length) {
|
|
196
|
+
throw new OpenArtifactsError('no_daemon', 'No open-artifacts daemon is running, and there is no runtime to start one.', {
|
|
197
|
+
fix: 'Set OPEN_ARTIFACTS_DAEMON_BIN to the open-artifacts entry, or pass bin (createHarness: runtime.dir).',
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (running)
|
|
201
|
+
await replaceDaemon(running, 5_000);
|
|
202
|
+
const logFile = join(openArtifactsHome(home), 'logs', 'daemon.out');
|
|
203
|
+
const deadline = Date.now() + timeoutMs;
|
|
204
|
+
// What the host's own start threw, per runtime: the error names them when no daemon came up.
|
|
205
|
+
const thrown = [];
|
|
206
|
+
for (const attempt of plan.tries) {
|
|
207
|
+
let exited = false;
|
|
208
|
+
const onExit = () => {
|
|
209
|
+
exited = true;
|
|
210
|
+
};
|
|
211
|
+
if (options.spawn) {
|
|
212
|
+
const start = { bin: attempt.bin, args: ['daemon', 'run'], env: daemonEnv(attempt.env, home), logFile };
|
|
213
|
+
delete start.env.ELECTRON_RUN_AS_NODE;
|
|
214
|
+
try {
|
|
215
|
+
mkdirSync(join(openArtifactsHome(home), 'logs'), { recursive: true });
|
|
216
|
+
const started = await options.spawn(start);
|
|
217
|
+
if (started && typeof started.once === 'function')
|
|
218
|
+
started.once('exit', onExit);
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
thrown.push(`${attempt.bin}: ${error?.message ?? String(error)}`);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
startDaemonProcess(attempt.bin, { env: attempt.env, ...(home ? { home } : {}) }).once('exit', onExit);
|
|
227
|
+
}
|
|
228
|
+
while (Date.now() < deadline) {
|
|
229
|
+
await new Promise((done) => setTimeout(done, 150));
|
|
230
|
+
const state = await readDaemon(home);
|
|
231
|
+
if (state)
|
|
232
|
+
return state;
|
|
233
|
+
// It ended without a daemon (a broken runtime): the next one; one that lost a race to another starter is found above.
|
|
234
|
+
if (exited)
|
|
235
|
+
break;
|
|
236
|
+
}
|
|
237
|
+
if (!exited)
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
const state = await readDaemon(home);
|
|
241
|
+
if (state)
|
|
242
|
+
return state;
|
|
243
|
+
const why = thrown.length ? ` The host's start failed: ${thrown.join('; ')}.` : '';
|
|
244
|
+
throw new OpenArtifactsError('daemon_failed', `The open-artifacts daemon did not start; see ${logFile}.${why}`);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Stops the home's daemon (`SIGTERM`, as `open-artifacts daemon stop`) and waits until its process has exited.
|
|
248
|
+
* Resolves with the daemon it stopped, or `null` when none ran. Only a daemon that answers health is stopped; one that
|
|
249
|
+
* runs in this very process is refused (`invalid_argument`), and one that does not exit in time fails
|
|
250
|
+
* (`daemon_failed`). Starts nothing: whoever needs a daemon next (any app's kit, a command line) starts one.
|
|
251
|
+
*/
|
|
252
|
+
export async function stopDaemon(options = {}) {
|
|
253
|
+
const home = options.home ?? homeFromEnv(options.env ?? process.env);
|
|
254
|
+
const running = await readDaemon(home);
|
|
255
|
+
if (!running)
|
|
256
|
+
return null;
|
|
257
|
+
if (running.pid === process.pid)
|
|
258
|
+
throw new OpenArtifactsError('invalid_argument', 'The daemon runs in this process: it cannot be stopped from here.');
|
|
259
|
+
await replaceDaemon(running, options.stopTimeoutMs ?? 10_000);
|
|
260
|
+
if (alive(running.pid)) {
|
|
261
|
+
throw new OpenArtifactsError('daemon_failed', `The open-artifacts daemon (pid ${running.pid}) did not exit in time.`, { fix: 'Run `open-artifacts daemon status`, then try again.' });
|
|
262
|
+
}
|
|
263
|
+
return running;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Restarts the home's daemon: stops the running one (`SIGTERM`, as `open-artifacts daemon stop`), waits until its
|
|
267
|
+
* process has exited, then starts one as {@link ensureDaemon} does (the newest compatible runtime). With no daemon
|
|
268
|
+
* running it only starts one. The harnesses connected to the old one reconnect to the new one. Only a daemon that
|
|
269
|
+
* answers health is stopped; one that runs in this very process is refused (`invalid_argument`), and one that does not
|
|
270
|
+
* exit in time fails (`daemon_failed`) without starting another.
|
|
271
|
+
*/
|
|
272
|
+
export async function restartDaemon(options = {}) {
|
|
273
|
+
const { stopTimeoutMs, ...ensure } = options;
|
|
274
|
+
const stopped = await stopDaemon({ ...(options.home ? { home: options.home } : {}), ...(options.env ? { env: options.env } : {}), ...(stopTimeoutMs ? { stopTimeoutMs } : {}) });
|
|
275
|
+
return { stopped, daemon: await ensureDaemon(ensure) };
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* One Library API call (`/api/open-artifacts.<name>`) with the daemon token. Returns the parsed JSON body. A daemon from
|
|
279
|
+
* before 0.23 still running is called at `/api/open-artifact.<name>`.
|
|
280
|
+
*/
|
|
281
|
+
export async function api(state, name, body, options = {}) {
|
|
282
|
+
const url = new URL(`/api/open-artifacts.${name}`, state.url);
|
|
283
|
+
for (const [key, value] of Object.entries(options.query ?? {}))
|
|
284
|
+
if (value !== undefined)
|
|
285
|
+
url.searchParams.set(key, value);
|
|
286
|
+
const response = await daemonFetch(state, url.href, {
|
|
287
|
+
method: options.method ?? (body === undefined ? 'GET' : 'POST'),
|
|
288
|
+
headers: { ...options.headers, Authorization: `Bearer ${state.token}`, ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}) },
|
|
289
|
+
...(body !== undefined ? { body: JSON.stringify(body) } : {}),
|
|
290
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
291
|
+
});
|
|
292
|
+
return (await response.json());
|
|
293
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/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
|
+
* Model use: with the Shell bar hidden, `onModelUse` and `onModelNotice` hear what its AI mark and notice would show.
|
|
31
|
+
*
|
|
32
|
+
* Save state: `onSaved` hears the page's save state and whether leaving now loses work (the Shell's `saved`, after it
|
|
33
|
+
* is ready and on each change), and `saved` keeps the last one, so a host can ask before it closes its view.
|
|
34
|
+
*/
|
|
35
|
+
import { type ChromeSaved, type HostDestination, type HostTheme, type HostToShell, type ShellToHost } from '@openartifactsai/protocol';
|
|
36
|
+
export type { ChromeSaved, HostDestination, HostTheme, HostToShell, ShellToHost };
|
|
37
|
+
/** The page's save state as the Shell reports it (`saved`): `state` is `null` before the page said one. */
|
|
38
|
+
export interface EmbedSavedState {
|
|
39
|
+
state: ChromeSaved | null;
|
|
40
|
+
/** Leaving now would lose work: ask before closing the view. */
|
|
41
|
+
leaveLosesWork: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A page of the view used a model while the Shell bar is hidden (the Shell's `model_use`, on each call's start and
|
|
45
|
+
* end): the source by name, calls running now, and today's calls and tokens of the artifact (`tokens` is `null` when
|
|
46
|
+
* none were reported; `approx` when some are estimated). The bar's AI mark would show it; show it your way.
|
|
47
|
+
*/
|
|
48
|
+
export interface EmbedModelUse {
|
|
49
|
+
artifact: string;
|
|
50
|
+
source: string;
|
|
51
|
+
running: number;
|
|
52
|
+
calls: number;
|
|
53
|
+
tokens: number | null;
|
|
54
|
+
approx: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* A page of the view started using a model without a dialog, because its type's permission (`scope: 'type'`) or
|
|
58
|
+
* "Always allow" (`'all'`) let it (the Shell's `model_notice`, while its bar is hidden). Say so your way, and send the
|
|
59
|
+
* person to the Shell page (its AI models panel) to change it.
|
|
60
|
+
*/
|
|
61
|
+
export interface EmbedModelNotice {
|
|
62
|
+
artifact: string;
|
|
63
|
+
source: string;
|
|
64
|
+
scope: 'type' | 'all';
|
|
65
|
+
}
|
|
66
|
+
/** How long after the Shell is ready the first thumbnail is captured. */
|
|
67
|
+
export declare const THUMBNAIL_DELAY_MS = 2000;
|
|
68
|
+
/** The least time between two thumbnails of one view. */
|
|
69
|
+
export declare const THUMBNAIL_INTERVAL_MS = 60000;
|
|
70
|
+
/** The bits of `window` this module uses (a test passes its own). */
|
|
71
|
+
export interface EmbedWindow {
|
|
72
|
+
addEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
73
|
+
removeEventListener(type: 'message', listener: (event: MessageEvent) => void): void;
|
|
74
|
+
open?(url: string, target?: string, features?: string): unknown;
|
|
75
|
+
}
|
|
76
|
+
/** The bits of an `<iframe>` this module uses. */
|
|
77
|
+
export interface EmbedFrame {
|
|
78
|
+
src: string;
|
|
79
|
+
readonly contentWindow: {
|
|
80
|
+
postMessage(message: unknown, targetOrigin: string): void;
|
|
81
|
+
} | null;
|
|
82
|
+
}
|
|
83
|
+
/** Carries host messages to a top-level view's Shell and its messages back (through the adapter and the daemon). */
|
|
84
|
+
export interface EmbedTransport {
|
|
85
|
+
/** Sends a host message to the Shell (the adapter calls `Harness.shellCommand(view, command)`). */
|
|
86
|
+
send(command: HostToShell): void | Promise<void>;
|
|
87
|
+
/** Listens to what the Shell says (the adapter's `on('shell')` events of this view); returns a function that stops. */
|
|
88
|
+
onEvent(listener: (event: unknown) => void): () => void;
|
|
89
|
+
}
|
|
90
|
+
/** The artifact a Shell said hello for. */
|
|
91
|
+
export interface EmbedReadyInfo {
|
|
92
|
+
/** `open-artifacts://artifact/<id>`. */
|
|
93
|
+
artifact: string;
|
|
94
|
+
id: string;
|
|
95
|
+
title: string;
|
|
96
|
+
type: string;
|
|
97
|
+
/** The Shell's version and host protocol. */
|
|
98
|
+
version: string;
|
|
99
|
+
protocol: number;
|
|
100
|
+
}
|
|
101
|
+
/** Options of {@link embedArtifact}. */
|
|
102
|
+
export interface EmbedArtifactOptions {
|
|
103
|
+
/**
|
|
104
|
+
* The Shell's address: the artifact's `viewUrl` (frame) or `embed_url` (top-level view). In frame mode it is set as
|
|
105
|
+
* the iframe's `src` when that differs; the Shell's origin is taken from it.
|
|
106
|
+
*/
|
|
107
|
+
url?: string;
|
|
108
|
+
/** The app's name, shown by the Shell ("Ask My Agent"). */
|
|
109
|
+
agentName?: string;
|
|
110
|
+
/** The harness the host belongs to. */
|
|
111
|
+
harness?: string;
|
|
112
|
+
/** The host draws its own toolbar: the Shell leaves out its close button. */
|
|
113
|
+
toolbar?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* The host has its own way between artifacts (its library, tabs or windows): the Shell leaves out "Switch artifact"
|
|
116
|
+
* and "All artifacts", which would navigate this view to another page. Sets caps `switcher`.
|
|
117
|
+
*/
|
|
118
|
+
switcher?: boolean;
|
|
119
|
+
/** The host's colour scheme and accent (`#rrggbb`). */
|
|
120
|
+
theme?: HostTheme;
|
|
121
|
+
/** Places the host sends exports to; the Shell lists at most {@link MAX_HOST_DESTINATIONS} in its Export tab. */
|
|
122
|
+
destinations?: HostDestination[];
|
|
123
|
+
/** The Shell said hello and took the host's answer. */
|
|
124
|
+
onReady?(info: EmbedReadyInfo): void;
|
|
125
|
+
/** Show the session that owns the artifact (`session` is `null` when none does). Sets caps `sessions`. */
|
|
126
|
+
onOpenSession?(request: {
|
|
127
|
+
session: string | null;
|
|
128
|
+
harness: string;
|
|
129
|
+
artifact: string | null;
|
|
130
|
+
}): void;
|
|
131
|
+
/** Start a new session with this prompt about the artifact (the person's words; empty to an undeclared host). Sets caps `sessions`. */
|
|
132
|
+
onNewSession?(request: {
|
|
133
|
+
prompt: string;
|
|
134
|
+
artifact: string;
|
|
135
|
+
}): void;
|
|
136
|
+
/** A comment was sent to the agent (its text; empty to an undeclared host). */
|
|
137
|
+
onCommentSent?(event: {
|
|
138
|
+
text: string;
|
|
139
|
+
}): void;
|
|
140
|
+
/** Where the artifact is now: a fragment to keep and send back with `goto`. */
|
|
141
|
+
onPlace?(event: {
|
|
142
|
+
artifact: string;
|
|
143
|
+
hash: string;
|
|
144
|
+
}): void;
|
|
145
|
+
/** The Shell's close button. */
|
|
146
|
+
onClose?(): void;
|
|
147
|
+
/** The person followed a link out of the Shell. Without this, the kit opens it with `window.open`. */
|
|
148
|
+
onOpenExternal?(url: string): void;
|
|
149
|
+
/** An export was saved to this file (absolute path on the computer that runs the type's server). */
|
|
150
|
+
onSavedFile?(path: string): void;
|
|
151
|
+
/** The page's save state changed (and once when the Shell is ready). A Shell older than this kit never sends it. */
|
|
152
|
+
onSaved?(state: EmbedSavedState): void;
|
|
153
|
+
/** A page used a model while the Shell bar is hidden (the Shell loaded with `?toolbar=0`, or an inline view). Declared hosts only. */
|
|
154
|
+
onModelUse?(event: EmbedModelUse): void;
|
|
155
|
+
/** A page started using a model without a dialog while the Shell bar is hidden. Declared hosts only. */
|
|
156
|
+
onModelNotice?(event: EmbedModelNotice): void;
|
|
157
|
+
/** Takes a JPEG of the view, or `null` when it cannot now. */
|
|
158
|
+
capture?(): Promise<Blob | Uint8Array | ArrayBuffer | null> | Blob | Uint8Array | ArrayBuffer | null;
|
|
159
|
+
/** Stores a captured thumbnail (JPEG bytes) of `artifact`. */
|
|
160
|
+
saveThumbnail?(jpeg: Uint8Array, artifact: string): Promise<void> | void;
|
|
161
|
+
/** A thumbnail or a callback failed (default: ignored). */
|
|
162
|
+
onError?(error: unknown): void;
|
|
163
|
+
/** The window to listen on (default `globalThis.window`). */
|
|
164
|
+
window?: EmbedWindow;
|
|
165
|
+
}
|
|
166
|
+
/** An embedded artifact. */
|
|
167
|
+
export interface EmbeddedArtifact {
|
|
168
|
+
/** The artifact the Shell said hello for, or `null` before. */
|
|
169
|
+
readonly artifact: string | null;
|
|
170
|
+
/** The last place the Shell reported, or `null`. */
|
|
171
|
+
readonly place: string | null;
|
|
172
|
+
/** The last save state the Shell reported, or `null` (before it did, or a Shell that does not report it). */
|
|
173
|
+
readonly saved: EmbedSavedState | null;
|
|
174
|
+
/** Resolves once the Shell is ready. */
|
|
175
|
+
readonly ready: Promise<EmbedReadyInfo>;
|
|
176
|
+
/** Shows a place (a fragment from `onPlace`, or a link's own). */
|
|
177
|
+
goto(hash: string): void;
|
|
178
|
+
/** Follows a new colour scheme or accent (sends `host_hello` again). */
|
|
179
|
+
setTheme(theme: HostTheme): void;
|
|
180
|
+
/** The Shell's address with the place last reported (without a view ticket), or `null` when unknown. */
|
|
181
|
+
url(): string | null;
|
|
182
|
+
/** Stops listening (and leaves the iframe as it is). */
|
|
183
|
+
destroy(): void;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Shows an artifact's Shell in the host's UI: `target` is the `<iframe>` (frame mode) or a transport to a top-level
|
|
187
|
+
* view (see the module's notes). Answers the Shell's hello and turns its messages into the callbacks.
|
|
188
|
+
*/
|
|
189
|
+
export declare function embedArtifact(target: EmbedFrame | EmbedTransport, options?: EmbedArtifactOptions): EmbeddedArtifact;
|
|
190
|
+
/** An artifact the home page asked the host to show. */
|
|
191
|
+
export interface LibraryArtifact {
|
|
192
|
+
url: string;
|
|
193
|
+
id: string;
|
|
194
|
+
title: string;
|
|
195
|
+
type: string;
|
|
196
|
+
}
|
|
197
|
+
/** Options of {@link embedLibrary}. */
|
|
198
|
+
export interface EmbedLibraryOptions {
|
|
199
|
+
/** The home page's address (the daemon's origin); set as the iframe's `src` when it differs. */
|
|
200
|
+
url?: string;
|
|
201
|
+
/** The harness the host belongs to: the page lists only artifacts of its sessions. */
|
|
202
|
+
harness?: string;
|
|
203
|
+
/** Only artifacts of sessions working in this folder (or under it). */
|
|
204
|
+
workspace?: string;
|
|
205
|
+
agentName?: string;
|
|
206
|
+
theme?: HostTheme;
|
|
207
|
+
/** Show this artifact (sets caps `artifacts`: the page sends it instead of navigating). */
|
|
208
|
+
onOpenArtifact?(artifact: LibraryArtifact): void;
|
|
209
|
+
onOpenSession?(request: {
|
|
210
|
+
session: string | null;
|
|
211
|
+
harness: string;
|
|
212
|
+
}): void;
|
|
213
|
+
onNewSession?(request: {
|
|
214
|
+
prompt: string;
|
|
215
|
+
artifact: string;
|
|
216
|
+
}): void;
|
|
217
|
+
/** The home page took the host's answer. */
|
|
218
|
+
onReady?(): void;
|
|
219
|
+
onError?(error: unknown): void;
|
|
220
|
+
window?: EmbedWindow;
|
|
221
|
+
}
|
|
222
|
+
/** The embedded home page. */
|
|
223
|
+
export interface EmbeddedLibrary {
|
|
224
|
+
/** Follows a new colour scheme or accent. */
|
|
225
|
+
setTheme(theme: HostTheme): void;
|
|
226
|
+
destroy(): void;
|
|
227
|
+
}
|
|
228
|
+
/** Shows the library's home page in an `<iframe>`, listing the host's own artifacts, and turns its messages into callbacks. */
|
|
229
|
+
export declare function embedLibrary(iframe: EmbedFrame, options?: EmbedLibraryOptions): EmbeddedLibrary;
|
|
230
|
+
export { remoteGate, remotePolicyOf, type AllowRemote, type RemotePage, type RemotePagePolicy, type RemotePageRequest } from './remote.js';
|