Repository navigation
Expand file tree
/
Copy pathcomputerworld-cloud.mjs
More file actions
1026 lines (959 loc) · 43.3 KB
/
Copy pathcomputerworld-cloud.mjs
File metadata and controls
1026 lines (959 loc) · 43.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// computerworld-cloud: a client for the hosted ComputerWorld API.
//
// One ES module, no dependencies, for Node 18+ and browsers. It can be imported from a
// checkout, from `npm install <path to clients/js>`, or straight from a URL:
//
// import { Client } from 'https://cdn.jsdelivr.net/gh/ComputaCo/computerworld@main/computerworld-cloud.mjs';
//
// Types are in computerworld-cloud.d.mts beside it. The contract is cloud/openapi.yaml.
export const VERSION = '0.3.0';
export const DEFAULT_BASE_URL = 'https://computaco.ai/v1';
export const USER_AGENT = `computerworld-cloud-js/${VERSION}`;
const DEFAULT_WIDTH = 1280;
const DEFAULT_HEIGHT = 800;
// 429, and a 503 with Retry-After, mean the request was not processed, so any method may be sent again.
// 500, 502, 504 and a 503 without Retry-After (a failure of ours, which the API answers 503 because the edge
// replaces a 502's body) may come after the simulation changed, so only idempotent methods retry.
const ALWAYS_RETRY = new Set([429]);
const IDEMPOTENT_RETRY = new Set([500, 502, 503, 504]);
const IDEMPOTENT = new Set(['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS']);
// A browser sends its own User-Agent and a cross-origin fetch may not set one (it is not a
// CORS-safelisted header), so the header is set only outside browsers.
const IS_SERVER = typeof process !== 'undefined' && !!process.versions?.node;
const env = (name) => (typeof process !== 'undefined' ? process.env?.[name] : undefined);
// ------------------------------------------------------------------------------- errors
export class ComputerWorldError extends Error {
constructor(message) {
super(message);
this.name = new.target.name;
}
}
/** The API answered with an error. `code` is the API's (`not_found`, `rate_limited`, ...). */
export class ApiError extends ComputerWorldError {
constructor(status, code, message, { method = '', path = '', retryAfter = null, body = null } = {}) {
super(`${status} ${code}: ${message}${method ? ` (${method} ${path})` : ''}`);
this.status = status;
this.code = code;
this.detail = message;
this.method = method;
this.path = path;
this.retryAfter = retryAfter;
this.body = body;
}
}
/** The request never got an answer (network failure or timeout). */
export class ConnectionFailed extends ComputerWorldError {}
/** No element of the scene matched a find. */
export class ElementNotFound extends ComputerWorldError {}
/** A helper's action came back unsuccessful. */
export class ActionFailed extends ComputerWorldError {
constructor(action, outcome) {
super(`${action.family} ${action.op} failed: ${JSON.stringify(outcome?.error ?? null)}`);
this.action = action;
this.outcome = outcome;
}
}
// ------------------------------------------------------------------------------- transport
export function normalizeBaseUrl(baseUrl) {
let base = String(baseUrl).replace(/\/+$/, '');
if (!base.endsWith('/v1')) base += '/v1';
return base;
}
/** `Retry-After` in seconds: delta-seconds or an HTTP date. */
export function parseRetryAfter(value, now = Date.now()) {
if (value == null || value === '') return null;
const s = Number(value);
if (Number.isFinite(s)) return Math.max(0, s);
const when = Date.parse(value);
return Number.isNaN(when) ? null : Math.max(0, (when - now) / 1000);
}
const sleepMs = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
class Transport {
constructor(baseUrl, token, opts = {}) {
this.baseUrl = normalizeBaseUrl(baseUrl);
this.token = token ?? null;
this.timeout = opts.timeout ?? 120_000;
this.maxRetries = opts.maxRetries ?? 4;
this.backoff = opts.backoff ?? 500;
this.maxBackoff = opts.maxBackoff ?? 30_000;
this.maxRetryAfter = opts.maxRetryAfter ?? 120;
this.userAgent = opts.userAgent ? `${opts.userAgent} ${USER_AGENT}` : USER_AGENT;
this.sendUserAgent = opts.sendUserAgent ?? IS_SERVER;
this.fetch = opts.fetch ?? globalThis.fetch.bind(globalThis);
this.sleep = opts.sleep ?? sleepMs;
this.opts = opts;
}
withToken(token) {
return new Transport(this.baseUrl, token, this.opts);
}
async request(method, path, { json, form, params, headers, ok = [], auth = true } = {}) {
method = method.toUpperCase();
let url = this.baseUrl + path;
if (params) {
const q = new URLSearchParams();
for (const [k, v] of Object.entries(params)) {
if (v === undefined || v === null) continue;
q.set(k, v === true ? '1' : v === false ? '0' : String(v));
}
const qs = q.toString();
if (qs) url += `?${qs}`;
}
const h = { accept: 'application/json', ...headers };
if (this.sendUserAgent) h['user-agent'] = this.userAgent;
if (auth && this.token) h.authorization = `Bearer ${this.token}`;
let body;
if (json !== undefined) {
body = JSON.stringify(json);
h['content-type'] = 'application/json';
} else if (form !== undefined) {
body = form; // a FormData sets its own type and boundary
}
for (let attempt = 0; ; attempt++) {
let resp;
try {
resp = await this.fetch(url, { method, headers: h, body, signal: AbortSignal.timeout(this.timeout) });
} catch (err) {
if (IDEMPOTENT.has(method) && attempt < this.maxRetries) {
await this.sleep(this.delay(attempt, null));
continue;
}
throw new ConnectionFailed(`${method} ${url}: ${err?.message ?? err}`);
}
if (resp.status < 400 || ok.includes(resp.status)) return resp;
const err = await toError(resp, method, path, this.baseUrl);
const retryable = ALWAYS_RETRY.has(resp.status) || (resp.status === 503 && err.retryAfter != null) || (IDEMPOTENT_RETRY.has(resp.status) && IDEMPOTENT.has(method));
if (!retryable || attempt >= this.maxRetries || (err.retryAfter != null && err.retryAfter > this.maxRetryAfter)) {
throw err;
}
await this.sleep(this.delay(attempt, err.retryAfter));
}
}
delay(attempt, retryAfter) {
if (retryAfter != null) return retryAfter * 1000;
return Math.min(this.maxBackoff, this.backoff * 2 ** attempt) * (0.5 + Math.random() / 2);
}
async json(method, path, json, opts = {}) {
const resp = await this.request(method, path, { ...opts, json });
const text = await resp.text();
return text ? JSON.parse(text) : null;
}
}
/** What a refused key most often means: it was made on another deployment, whose accounts are its own
* (dev.computaco.ai's keys are unknown to computaco.ai, and the other way round). */
const keyHint = (baseUrl) =>
`This request went to ${baseUrl}. A key works only with the API of the site where it was made: ` +
'set COMPUTERWORLD_API_BASE (or baseUrl) to that site\'s /v1, for example https://dev.computaco.ai/v1.';
async function toError(resp, method, path, baseUrl = '') {
const retryAfter = parseRetryAfter(resp.headers.get('retry-after'));
const text = await resp.text().catch(() => '');
let code = `http_${resp.status}`;
let message = '';
let parsed = null;
try {
parsed = JSON.parse(text);
} catch {}
if (parsed && typeof parsed.error === 'object' && parsed.error) {
code = String(parsed.error.code || code);
message = String(parsed.error.message || '');
} else {
message = text.trim().slice(0, 300) || resp.statusText || '';
if (text.includes('error code: 1010')) message += " (Cloudflare refused the client's User-Agent)";
}
if (resp.status === 401 && baseUrl) message = (message ? `${message} ` : '') + keyHint(baseUrl);
return new ApiError(resp.status, code, message, { method, path, retryAfter, body: parsed ?? text });
}
// ------------------------------------------------------------------------------- scenes
function matches(value, want) {
if (want === undefined || want === null) return true;
if (value === undefined || value === null) return false;
if (want instanceof RegExp) return want.test(String(value));
return String(value) === want;
}
/** Every element of a scene (`scene({ elements: true })`) matching all fields given, front first. */
export function findElements(scene, { label, role, interaction, value, includeDisabled = false } = {}) {
if (!Array.isArray(scene?.elements)) throw new Error('the scene has no elements; ask for it with { elements: true }');
return scene.elements.filter(
(e) =>
(includeDisabled || !e.disabled) &&
matches(e.label, label) &&
matches(e.role, role) &&
matches(e.interaction, interaction) &&
matches(e.value, value),
);
}
/** The `index`-th match (the frontmost by default); throws ElementNotFound naming what is on screen. */
export function findElement(scene, { index = 0, ...query } = {}) {
const found = findElements(scene, query);
if (index < found.length) return found[index];
const want = Object.fromEntries(
Object.entries(query)
.filter(([k, v]) => v != null && k !== 'includeDisabled')
.map(([k, v]) => [k, String(v)]),
);
const seen = [...new Set((scene.elements || []).map((e) => `${e.role}:${e.label}`))].sort().slice(0, 20);
throw new ElementNotFound(
`no element matches ${JSON.stringify(want)} (found ${found.length}, wanted index ${index}); ` +
`on screen: ${seen.join(', ') || 'nothing interactive'}`,
);
}
/** An element's centre in screen pixels, `[x, y]`. */
export function center(element) {
if (Array.isArray(element.center) && element.center.length === 2) return [element.center[0], element.center[1]];
const r = element.rect;
return [r.x + Math.floor(r.width / 2), r.y + Math.floor(r.height / 2)];
}
// ------------------------------------------------------------------------------- helpers
const q = encodeURIComponent;
const dropUndefined = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined && v !== null));
const idOf = (x) => (typeof x === 'string' ? x : x.id);
/** A picture answer as `{ bytes, etag, stateHash, tick, width, height, format }`. */
async function frameOf(resp, format) {
const int = (name) => (resp.headers.get(name) == null ? null : Number(resp.headers.get(name)));
return {
bytes: new Uint8Array(await resp.arrayBuffer()),
etag: resp.headers.get('etag'),
stateHash: resp.headers.get('x-state-hash'),
tick: int('x-tick'),
width: int('x-width'),
height: int('x-height'),
format,
};
}
/** `'template/id'`, `{ template, id }`, or a whole procedure → the API's `ref | procedure`. */
function procedureBody(procedure) {
if (typeof procedure === 'string') {
const i = procedure.indexOf('/');
if (i <= 0 || i === procedure.length - 1) throw new Error(`a procedure reference is 'template/id', got '${procedure}'`);
return { ref: { template: procedure.slice(0, i), id: procedure.slice(i + 1) } };
}
if (procedure && typeof procedure === 'object') {
if (Array.isArray(procedure.phases)) return { procedure };
if (procedure.template && procedure.id) return { ref: { template: procedure.template, id: procedure.id } };
}
throw new Error("a procedure is 'template/id', { template, id }, or a procedure with phases");
}
const agentRef = (a) =>
a instanceof Agent
? a.id
: a && typeof a === 'object'
? Object.fromEntries(Object.entries(a).map(([role, x]) => [role, agentRef(x)]))
: a;
/**
* An export as the API carries it: a form of the bundle's JSON (`bundle`) and the snapshot's
* bytes (`snapshot`); as code holds it, the bundle with `snapshot` a Uint8Array.
*/
async function bundleOf(resp) {
const form = await resp.formData();
const bundle = JSON.parse(await form.get('bundle').text());
bundle.snapshot = new Uint8Array(await form.get('snapshot').arrayBuffer());
return bundle;
}
function bundleForm(bundle, extra = {}) {
const { snapshot, ...rest } = bundle;
const form = new FormData();
form.append('bundle', new Blob([JSON.stringify(rest)], { type: 'application/json' }), 'bundle.json');
form.append('snapshot', new Blob([snapshot], { type: 'application/octet-stream' }), 'snapshot');
for (const [k, v] of Object.entries(extra)) form.append(k, new Blob([JSON.stringify(v)], { type: 'application/json' }), `${k}.json`);
return form;
}
const ndjson = (text) =>
text
.split('\n')
.filter((l) => l.trim())
.map((l) => JSON.parse(l));
// ------------------------------------------------------------------------------- client
/**
* The owner's door to the hosted API, authenticated with an API key. `apiKey` and `baseUrl`
* default to COMPUTERWORLD_API_KEY and COMPUTERWORLD_API_BASE where `process.env` exists.
*/
export class Client {
constructor(apiKey, { baseUrl, transport, ...opts } = {}) {
this._t =
transport ??
new Transport(baseUrl ?? env('COMPUTERWORLD_API_BASE') ?? DEFAULT_BASE_URL, apiKey ?? env('COMPUTERWORLD_API_KEY'), opts);
}
get baseUrl() {
return this._t.baseUrl;
}
_get(path, params) {
return this._t.json('GET', path, undefined, { params });
}
_post(path, body = {}) {
return this._t.json('POST', path, body);
}
async _delete(path) {
await this._t.request('DELETE', path);
}
/**
* Make an account, then an API key with its session (every scope unless `scopes` names some:
* simulations, browser, templates, models, account). Resolves to `{ client, user, key }`: a client
* holding the key, the account, and the key as made (its `key` is the secret, shown once).
* Server side only: a browser does not let a script read the session cookie.
*/
static async signup(username, password, { baseUrl, keyName, scopes, ...opts } = {}) {
const t = new Transport(baseUrl ?? env('COMPUTERWORLD_API_BASE') ?? DEFAULT_BASE_URL, null, opts);
const resp = await t.request('POST', '/auth/signup', { json: { username, password } });
const { user } = await resp.json();
const cookie = (resp.headers.get('set-cookie') ?? '').split(';')[0];
const key = await t.json('POST', '/keys', dropUndefined({ name: keyName, scopes }), { headers: { cookie } });
return { client: new Client(null, { transport: t.withToken(key.key) }), user, key };
}
// accounts
health() {
return this._t.json('GET', '/health', undefined, { auth: false });
}
me() {
return this._get('/me');
}
/** Set profile fields (name, date_of_birth); a field left out is kept, `null` clears it. */
async updateMe(fields) {
return (await this._t.json('PATCH', '/me', fields)).user;
}
/** Set the account's e-mail address and send it a six-digit code; then `verifyEmail(code)`. */
async setEmail(email) {
return (await this._t.json('POST', '/me/email', { email })).email;
}
/** Confirm the code: `{user, email, standing, tag}` (the tag an academic domain gave, or null). */
verifyEmail(code) {
return this._t.json('POST', '/me/email/verify', { code });
}
async usage(days = 30) {
return (await this._get('/me/usage', { days })).days;
}
async listKeys() {
return (await this._get('/keys')).keys;
}
/** A new API key; `scopes` defaults to every one. Its `key` field is the secret, shown once. */
createKey(name, { scopes } = {}) {
return this._post('/keys', dropUndefined({ name, scopes }));
}
revokeKey(keyId) {
return this._delete(`/keys/${q(keyId)}`);
}
// templates
/** The built-in templates and the organization's own, one list (`source: 'builtin' | 'own'`). */
async listTemplates() {
return (await this._get('/templates')).templates;
}
/** A template's definition. */
getTemplate(id) {
return this._get(`/templates/${q(id)}`);
}
/** Save a definition as one of the organization's templates. */
createTemplate({ name, definition } = {}) {
return this._post('/templates', dropUndefined({ name, definition }));
}
updateTemplate(id, { name, definition } = {}) {
return this._t.json('PUT', `/templates/${q(id)}`, dropUndefined({ name, definition }));
}
deleteTemplate(id) {
return this._delete(`/templates/${q(id)}`);
}
templateTopology(id) {
return this._get(`/templates/${q(id)}/topology`);
}
/** Apply `[{ op, ... }]` in order, all or nothing; resolves to the new topology. */
editTemplateTopology(id, edits) {
return this._post(`/templates/${q(id)}/topology`, { edits });
}
async templateProcedures(id) {
return (await this._get(`/templates/${q(id)}/procedures`)).procedures;
}
templateProcedure(id, procedureId) {
return this._get(`/templates/${q(id)}/procedures/${q(procedureId)}`);
}
/** Draw an instance of a template's procedure. */
instantiate(template, procedureId, { seed = 1, overrides } = {}) {
return this._post(`/templates/${q(template)}/procedures/${q(procedureId)}/instantiate`, dropUndefined({ seed, overrides }));
}
async templateSocieties(id) {
return (await this._get(`/templates/${q(id)}/societies`)).societies;
}
templateSociety(id, societyId) {
return this._get(`/templates/${q(id)}/societies/${q(societyId)}`);
}
vocabulary() {
return this._get('/vocabulary');
}
/** Boot a definition with the engine and drop it; with `society`, also the people it compiles to. */
validate(definition, { society } = {}) {
return this._post('/validate', dropUndefined({ definition, society }));
}
// simulations
async listSimulations() {
return (await this._get('/simulations')).simulations;
}
/**
* Boot a simulation: `createSimulation('agent-desktop', { seed })`, or
* `createSimulation(null, { definition })`. `society` (`{ id | spec, rule, journal, rounds }`,
* or a template's society id) adds simulated agents. `idleTtl` (seconds): the service deletes
* the simulation after that long with no request to it. `place`: where it runs, `'lightning'`
* or `'heavy'` (or a Lightning place's id: `'core'`, `'office'`, `'media'`), or a list of those
* tried in order; left out, Lightning first, and Heavy only for a stated reason. `info.place` says where it runs and why, `info.refused` the places that refused it.
*/
async createSimulation(template, { definition, seed = 1, name, platform, society, idleTtl, place } = {}) {
if ((template == null) === (definition == null)) throw new Error('pass exactly one of template, definition');
const soc = typeof society === 'string' ? { id: society } : society;
return new Simulation(
this,
await this._post('/simulations', dropUndefined({ template, definition, seed, name, platform, society: soc, idle_ttl: idleTtl, place })),
);
}
async getSimulation(id) {
return new Simulation(this, await this._get(`/simulations/${q(id)}`));
}
/** Boot a new simulation from what `simulation.exportSnapshot()` returned; `place` is as for `createSimulation`. */
async importSimulation(bundle, name, { place, idleTtl } = {}) {
const form = bundleForm(bundle, { options: dropUndefined({ name, place, idle_ttl: idleTtl }) });
const resp = await this._t.request('POST', '/simulations/import', { form });
return new Simulation(this, await resp.json());
}
/**
* The browser: a web browser on a simulated Linux machine whose whole screen is the browser.
* One-shot reads (`client.browser.screenshot(url)`) and sessions (`client.browser.createSession()`).
*/
get browser() {
return (this._browser ??= new Browser(this));
}
// agent models (OpenAI's paths)
async models() {
return (await this._get('/models')).data;
}
/** OpenAI chat completions: `chat({ model: 'deepseek-flash', messages, tools })`. */
chat(body) {
return this._post('/chat/completions', body);
}
}
/** A running simulation the organization owns: one world with its clock, machines, snapshots, agents and forks. */
export class Simulation {
constructor(client, info) {
this._c = client;
this.info = info;
this.id = info.id;
this._p = `/simulations/${q(this.id)}`;
this._agents = new Map();
for (const grant of Array.isArray(info.agents) ? info.agents : []) {
if (grant?.token) this._agents.set(grant.id, Agent._fromGrant(client, this.id, grant));
}
this.society = new Society(client, this._p);
}
async refresh() {
return (this.info = await this._c._get(this._p));
}
async rename(name) {
return (this.info = await this._c._t.json('PATCH', this._p, { name }));
}
/** Delete the simulation, its snapshots and its agents. */
delete() {
return this._c._delete(this._p);
}
/** `await using sim = await client.createSimulation(...)` deletes it at the end of the block. */
[Symbol.asyncDispose ?? Symbol.for('Symbol.asyncDispose')]() {
return this.delete();
}
/** A file on a machine, as the owner reads it: `{ machine, path, content, base64, bytes }`. */
readFile(path, { machine } = {}) {
return this._c._get(`${this._p}/files`, dropUndefined({ machine, path }));
}
/** Write a file onto a machine: text, or bytes (a Uint8Array). */
writeFile(path, data, { machine } = {}) {
const body = typeof data === 'string' ? { content: data } : { base64: btoa(Array.from(data, (b) => String.fromCharCode(b)).join('')) };
return this._c._t.json('PUT', `${this._p}/files`, body, { params: dropUndefined({ machine, path }) });
}
// clock and state
async stateHash() {
return (await this.clock()).state_hash;
}
clock() {
return this._c._get(`${this._p}/state_hash`);
}
/** Move the clock: `advance('30s')`, `advance({ ticks: 1_000_000 })`. */
advance(arg) {
const body = typeof arg === 'string' ? { duration: arg } : typeof arg === 'number' ? { ticks: arg } : dropUndefined(arg ?? {});
if (('duration' in body) === ('ticks' in body)) throw new Error('pass one of duration or ticks');
return this._c._post(`${this._p}/advance`, body);
}
/** Back to the definition; external agents are dropped. */
reset(seed) {
this._agents.clear();
return this._c._post(`${this._p}/reset`, dropUndefined({ seed }));
}
inspect(pointer) {
return this._c._get(`${this._p}/inspect`, { pointer });
}
definition() {
return this._c._get(`${this._p}/definition`);
}
trajectory() {
return this._c._get(`${this._p}/trajectory`);
}
serviceState(serviceId) {
return this._c._get(`${this._p}/services/${q(serviceId)}/state`);
}
patchServiceState(serviceId, patch) {
return this._c._t.json('PATCH', `${this._p}/services/${q(serviceId)}/state`, patch);
}
/** The questions the world's sites have for a model, and when something is next due. */
asks() {
return this._c._get(`${this._p}/asks`);
}
/** Answer them: {services?, advance?: "30m", ask_model?: false}. */
answerAsks(options = {}) {
return this._c._t.json('POST', `${this._p}/asks`, options);
}
// topology
topology() {
return this._c._get(`${this._p}/topology`);
}
addComputer(computer, node, links, theme) {
return this._c._post(`${this._p}/machines`, dropUndefined({ computer, node, links, theme }));
}
removeComputer(machineId) {
return this._c._t.json('DELETE', `${this._p}/machines/${q(machineId)}`);
}
// snapshots and history
/** Keep a snapshot: `{ id, label, state_hash, tick, parent, author, frames, head, … }`. It is the simulation's head now. */
snapshot(label) {
return this._c._post(`${this._p}/snapshots`, dropUndefined({ label }));
}
/** The kept snapshots, oldest first. `history()` and `refresh()` name the head. */
async snapshots() {
return (await this._c._get(`${this._p}/snapshots`)).snapshots;
}
/**
* Restore a snapshot: `{ state_hash, tick, head, elapsed_ms, conversations_kept, agents }`. The agents,
* their connections and their threads become the snapshot's; an agent removed after it comes back
* with a new `token` in `agents`.
*/
restore(snapshot) {
return this._c._post(`${this._p}/snapshots/${q(idOf(snapshot))}/restore`);
}
/** The agents a snapshot kept: `{ agents, conversations_kept }`. */
snapshotAgents(snapshot) {
return this._c._get(`${this._p}/snapshots/${q(idOf(snapshot))}/agents`);
}
/** One agent's thread as a snapshot kept it: `{ messages, state }`, after `after`. */
snapshotMessages(snapshot, agent, { after = 0 } = {}) {
return this._c._t.json('GET', `${this._p}/snapshots/${q(idOf(snapshot))}/agents/${q(idOf(agent))}/messages`, undefined, { params: { after } });
}
/** Drop a snapshot; its children hang from its parent. Resolves to what is left: `{ snapshots, head }`. */
dropSnapshot(snapshot) {
return this._c._t.json('DELETE', `${this._p}/snapshots/${q(idOf(snapshot))}`);
}
/**
* The PNG of a machine's screen when the snapshot was taken, for each machine the
* snapshot's `frames` names: `{ bytes, etag, stateHash, tick, width, height, format }`,
* a header the answer lacks being null.
*/
async snapshotFrame(snapshot, machine) {
const resp = await this._c._t.request('GET', `${this._p}/snapshots/${q(idOf(snapshot))}/frames/${q(machine)}`, {
headers: { accept: 'image/png' },
});
return frameOf(resp, 'png');
}
/**
* The family's history: `{ simulation, simulations }`, the furthest ancestor first, each
* with its place, origin, head and snapshots. No simulation is woken to answer.
*/
history() {
return this._c._get(`${this._p}/history`);
}
/** The simulation as a bundle `client.importSimulation` boots: its definition, seed and agents, and `snapshot`, the engine's bytes. */
async exportSnapshot() {
return bundleOf(await this._c._t.request('GET', `${this._p}/export`));
}
// forks
/**
* A new simulation copied from this one as it is now (`forked_from` names this one). A
* society comes over with its runner state, and `societyOps` (an arm's rule ops) are applied
* to it. External agents come over under new tokens: `fork.getAgent(id)`. `snapshot` forks from
* a kept snapshot. `place` is as for a new simulation; left out, the fork runs where its
* parent does (`'lightning'` moves a Heavy simulation's copy to Lightning).
*/
async fork({ name, snapshot, societyOps, place, idleTtl } = {}) {
const body = dropUndefined({ name, snapshot: snapshot == null ? undefined : idOf(snapshot), society_ops: societyOps, place, idle_ttl: idleTtl });
return new Simulation(this._c, await this._c._post(`${this._p}/fork`, body));
}
/** The ids of this simulation's forks. */
async forks() {
return (await this.refresh()).forks ?? [];
}
// agents
/** Grant an external agent and resolve to its handle, which holds the actor token. */
async agent(config = {}) {
const grant = await this._c._post(`${this._p}/agents`, config);
const agent = Agent._fromGrant(this._c, this.id, grant);
this._agents.set(agent.id, agent);
return agent;
}
/**
* A hosted agent: one the platform runs (Computatrum's agent loop) on `machine`, as its user.
* Prompt it with `agent.prompt(text)` and follow its thread with `agent.stream()`.
*/
async hostedAgent(machine, { name } = {}) {
const made = await this._c._post(`${this._p}/agents`, dropUndefined({ kind: 'hosted', machine, name }));
return Agent._fromGrant(this._c, this.id, made);
}
/** Every agent: the external ones (`kind: 'external'`, never their tokens), the hosted ones, and the society's people (`kind: 'simulated'`). */
async agents() {
return (await this._c._get(`${this._p}/agents`)).agents;
}
/** A handle on one of the society's people, for its thread (what they did in the rounds; it takes no prompt). */
person(personId) {
return new Agent(this._c._t, this.id, personId, { grant: { id: personId, kind: 'simulated' }, owner: this._c });
}
/** A handle on an external or hosted agent: with its token if this object holds it, else fetched with the owner's key. */
async getAgent(agentId) {
if (this._agents.has(agentId)) return this._agents.get(agentId);
const grant = await this._c._get(`${this._p}/agents/${q(agentId)}`);
return Agent._fromGrant(this._c, this.id, grant);
}
revoke(agent) {
const id = idOf(agent);
this._agents.delete(id);
return this._c._delete(`${this._p}/agents/${q(id)}`);
}
/**
* Connect an agent to a machine; resolves to the agent. It keeps its id, token and thread,
* and its session is made again. A hosted agent drives one machine, so this moves it.
*/
connectAgent(agent, machine) {
return this._c._t.json('PUT', `${this._p}/agents/${q(idOf(agent))}/machines/${q(machine)}`);
}
/** Disconnect an agent from a machine; resolves to the agent. */
disconnectAgent(agent, machine) {
return this._c._t.json('DELETE', `${this._p}/agents/${q(idOf(agent))}/machines/${q(machine)}`);
}
/** One grounded gesture, `{ do: 'click', target: { label: 'Post' } }`, found on the agent's screen. */
perform(agent, gesture, { width, height } = {}) {
const body = { agent: agentRef(agent), gesture };
const options = dropUndefined({ width, height });
if (Object.keys(options).length) body.options = options;
return this._c._post(`${this._p}/perform`, body);
}
// ingress (a real client's way in: an HTTPS GET of one published service, or a stock ssh)
/**
* Publish one of this world's services to real HTTP clients: `publish(agent, 'html.example', 8090)`. Each GET of
* `url + path` with `header` is that agent's `http.v1 / request` from its machine to the service. Resolves to the
* grant: `url`, `credential` (shown once), `header`, `expires_at`. `ttl` is in seconds (60 to 86400).
*/
publish(agent, host, port, { machine, ttl } = {}) {
return this._c._post(`${this._p}/publications`, dropUndefined({ agent: agentRef(agent), host, port, machine, ttl_s: ttl }));
}
/** This simulation's publications (never their credentials). */
async publications() {
return (await this._c._get(`${this._p}/publications`)).grants;
}
/** Revoke a publication or an SSH grant; the next request with it fails. */
revokeGrant(grant, { kind } = {}) {
const handle = typeof grant === 'string' ? grant : grant.handle;
const route = (kind ?? grant?.kind) === 'ssh' ? 'ssh' : 'publications';
return this._c._delete(`${this._p}/${route}/${q(handle)}`);
}
/**
* Let a stock OpenSSH client act as `agent`: each `ssh … 'command'` is its `terminal.v1 / execute`. `publicKey` is
* the client's OpenSSH public key line; `forward: { host, port }` also allows `ssh -L` to that one published
* service (`exec: false` allows only that). Resolves to the grant with `login`, `websocket_url`, `credential` (shown
* once), `known_hosts` (the host key to pin), `host_key_alias` and a copyable `ssh_command`, which runs through
* the `cw-ssh-proxy` helper this package installs. Write the grant to a file with mode 0600 and name it in
* CW_SSH_GRANT.
*/
ssh(agent, publicKey, { machine, exec, forward, ttl } = {}) {
return this._c._post(`${this._p}/ssh`, dropUndefined({ agent: agentRef(agent), public_key: publicKey, machine, exec, forward, ttl_s: ttl }));
}
/** This simulation's SSH grants (never their credentials). */
async sshGrants() {
return (await this._c._get(`${this._p}/ssh`)).grants;
}
// procedures
stage(instance) {
return this._c._post(`${this._p}/procedures/stage`, { instance });
}
/** Perform an instance through an agent (or `{ role: agent }`). */
project(agent, instance, options) {
return this._c._post(`${this._p}/procedures/project`, dropUndefined({ instance, agent: agentRef(agent), options }));
}
verify(instance) {
return this._c._post(`${this._p}/procedures/verify`, { instance });
}
/** Instantiate, stage, project and verify. `fork: true` leaves this simulation as it was. */
run(procedure, { seed = 1, fork = false, includeSteps = false, options, overrides, agent } = {}) {
return this._c._post(`${this._p}/procedures/run`, {
...procedureBody(procedure),
...dropUndefined({ seed, fork, include_steps: includeSteps, options, overrides, agent: agentRef(agent) }),
});
}
}
/**
* An agent of a simulation, with its thread (`messages`, `prompt`, `stream`, `stop`); an
* external agent is also the actor's door to the simulation. Actor calls use the actor token when
* the handle has one, so the handle can be given to an agent program without the owner's key.
* `width`/`height` are the viewport the scene, the render and the pointer helpers use.
*/
export class Agent {
constructor(transport, simulationId, agentId, { token = null, grant = {}, owner = null, width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT } = {}) {
this._t = transport;
this.simulationId = simulationId;
this.id = agentId;
this.token = token;
this.grant = grant;
this._owner = owner;
this.width = width;
this.height = height;
this._p = `/simulations/${q(simulationId)}/agents/${q(agentId)}`;
}
static _fromGrant(client, simulationId, grant) {
const t = grant.token ? client._t.withToken(grant.token) : client._t;
return new Agent(t, simulationId, grant.id, { token: grant.token ?? null, grant, owner: client });
}
/** An agent's handle from an actor token alone. */
static connect(token, simulationId, agentId, { baseUrl, ...opts } = {}) {
const t = new Transport(baseUrl ?? env('COMPUTERWORLD_API_BASE') ?? DEFAULT_BASE_URL, token, opts);
return new Agent(t, simulationId, agentId, { token });
}
get config() {
return this.grant.config ?? {};
}
/**
* Send up to 64 actions in order. An action outside the grant is an unsuccessful outcome.
* `render: { width, height, ifChanged }` answers the screen they left as `frame` (a base64 PNG).
*/
step(actions, { render } = {}) {
const body = { actions: Array.isArray(actions) ? actions : [actions] };
if (render) body.render = dropUndefined({ width: render.width ?? this.width, height: render.height ?? this.height, if_changed: render.ifChanged });
return this._t.json('POST', `${this._p}/step`, body);
}
observe() {
return this._t.json('GET', `${this._p}/observe`);
}
/** The scene graph; `{ elements: true }` adds every interactive element, front first. */
scene({ width, height, elements = false } = {}) {
return this._t.json('GET', `${this._p}/scene`, undefined, {
params: { width: width ?? this.width, height: height ?? this.height, elements: elements ? 1 : undefined },
});
}
/**
* The screen as `{ bytes, etag, stateHash, tick, width, height, format }`; with
* `ifNoneMatch` (a previous frame's etag) an unchanged screen resolves to null.
*/
async render({ width, height, format = 'png', ifNoneMatch } = {}) {
const headers = { accept: format === 'png' ? 'image/png' : 'application/octet-stream' };
if (ifNoneMatch) headers['if-none-match'] = ifNoneMatch;
const resp = await this._t.request('GET', `${this._p}/render`, {
params: { width: width ?? this.width, height: height ?? this.height, format },
headers,
ok: [304],
});
if (resp.status === 304) return null;
return frameOf(resp, format);
}
async info() {
return (this.grant = await this._t.json('GET', this._p));
}
// the thread
/** The owner's transport: prompts and stops are the owner's. */
get _ownerT() {
if (!this._owner?._t.token) throw new ComputerWorldError('this is an owner call; this handle has only an actor token');
return this._owner._t;
}
/**
* The thread after the message `after` (0: from the start), `{ messages, state }`; with `wait`
* (seconds, at most 25) a read with nothing new waits for a message.
*/
messages({ after = 0, wait } = {}) {
return this._t.json('GET', `${this._p}/messages`, undefined, { params: dropUndefined({ after, wait }) });
}
/** A prompt from the owner: `{ message, state }`. A hosted agent starts on it (or after the task it is on). */
async prompt(text) {
return this._ownerT.json('POST', `${this._p}/messages`, { text });
}
/** What an external agent writes into its own thread: `{ role: 'agent' | 'action' | 'status', text, status?, … }`. */
post(message) {
return this._t.json('POST', `${this._p}/messages`, message);
}
/** Stop it (the owner): a hosted agent's task is cut short; an external agent's thread says it was stopped. `{ state }`. */
async stop() {
return this._ownerT.json('POST', `${this._p}/stop`);
}
/**
* The thread's messages as they come, by long-polling: `for await (const m of agent.stream()) …`.
* It ends when `until(message, state)` is true of a message, or when `signal` aborts.
*/
async *stream({ after = 0, wait = 25, until, signal } = {}) {
while (!signal?.aborted) {
const { messages, state } = await this.messages({ after, wait });
for (const m of messages) {
after = m.seq;
yield m;
if (until?.(m, state)) return;
}
}
}
// helpers
async findAll(query = {}) {
return findElements(query.scene ?? (await this.scene({ elements: true })), query);
}
/** The frontmost element matching `{ label, role, interaction, value }` (strings exact, or RegExps). */
async find(query = {}) {
return findElement(query.scene ?? (await this.scene({ elements: true })), query);
}
/**
* Click `{ x, y }`, an `element`, or the centre of what a fresh scene finds by
* `label`/`role`/`interaction`. With `check` (default) an unsuccessful outcome throws.
*/
async click({ x, y, element, label, role, interaction, index = 0, button, modifiers, double = false, machine, check = true } = {}) {
if (x == null || y == null) {
if (!element) {
if (label == null && role == null && interaction == null) throw new Error('click needs x and y, an element, or a label/role/interaction');
element = await this.find({ label, role, interaction, index });
}
[x, y] = center(element);
}
const payload = dropUndefined({ x, y, width: this.width, height: this.height, button, modifiers });
return this._act({ family: 'pointer.v1', op: double ? 'double_click' : 'click', payload }, machine, check);
}
typeText(text, { machine, check = true } = {}) {
return this._act({ family: 'keyboard.v1', op: 'type', payload: { text } }, machine, check);
}
/** A key or chord: `'Enter'`, `'Ctrl+S'`. */
press(key, { machine, check = true } = {}) {
return this._act({ family: 'keyboard.v1', op: 'key', payload: { key } }, machine, check);
}
async _act(action, machine, check) {
if (machine) action.machine = machine;
const result = await this.step([action]);
const outcome = result.outcomes?.[0] ?? {};
if (check && !outcome.success) throw new ActionFailed(action, outcome);
return result;
}
/** One grounded gesture through this agent (an owner call: needs the API key). */
perform(gesture) {
if (!this._owner?._t.token) throw new ComputerWorldError('perform is an owner call; this handle has only an actor token');
return new Simulation(this._owner, { id: this.simulationId }).perform(this.id, gesture, { width: this.width, height: this.height });
}
}
/** A simulation's society: its simulated agents, their rules and rounds. */
export class Society {
constructor(client, simulationPath) {
this._c = client;
this._p = `${simulationPath}/society`;
}
/** `{ spec_id, people, round, rounds, metrics }`; a 404 when the simulation has no society. */
info() {
return this._c._get(this._p);
}
/** Run 1 to 100 rounds in this simulation only (a fork runs its own); `pending` lists the model decisions the journal lacks. */
runRounds(count = 1, { askModel } = {}) {
return this._c._post(`${this._p}/rounds`, dropUndefined({ count, ask_model: askModel || undefined }));
}
async journal() {
return ndjson(await (await this._c._t.request('GET', `${this._p}/journal`)).text());
}
/** Answers for pending decisions: `[{ key, reply }]`. */
addJournal(entries) {
return this._c._post(`${this._p}/journal`, { entries });
}
async trace({ since } = {}) {
return ndjson(await (await this._c._t.request('GET', `${this._p}/trace`, { params: { since } })).text());
}
/** Outcomes per round for this simulation and each fork, their divergence and state hashes. */
summary() {
return this._c._get(`${this._p}/summary`);
}
}
// ------------------------------------------------------------------------------- the browser
/** `{ viewport, waitMs, fullPage }` → the API's body beside `url`. */
const browserBody = (url, { viewport, waitMs, fullPage } = {}) => dropUndefined({ url, viewport, wait_ms: waitMs, full_page: fullPage });
/** A screenshot's answer: `{ bytes, width, height, stateHash, clipped, browserMsUsed }`. */
async function shotOf(resp) {
const int = (name) => (resp.headers.get(name) == null ? null : Number(resp.headers.get(name)));
return {
bytes: new Uint8Array(await resp.arrayBuffer()),
width: int('x-width'),
height: int('x-height'),
stateHash: resp.headers.get('x-state-hash'),
clipped: resp.headers.get('x-full-page-clipped') === 'true',
browserMsUsed: int('x-browser-ms-used'),
};
}
/** The browser API (`client.browser`): one-shot calls, each in a throwaway session, and sessions. */
export class Browser {
constructor(client) {
this._c = client;
}
/** The page at `url` drawn as a PNG: `{ bytes, width, height, … }`. `fullPage` draws the whole page. */
async screenshot(url, opts = {}) {
return shotOf(await this._c._t.request('POST', '/browser/screenshot', { json: browserBody(url, opts), headers: { accept: 'image/png' } }));
}
/** `{ url, status, title, html }`: the page's markup after its script ran. */
content(url, opts = {}) {
return this._c._post('/browser/content', browserBody(url, opts));
}
/** `{ url, title, markdown }`. */
markdown(url, opts = {}) {
return this._c._post('/browser/markdown', browserBody(url, opts));
}
/** `{ url, links: [{ href, text }] }`. */
links(url, opts = {}) {
return this._c._post('/browser/links', browserBody(url, opts));
}
/** `{ url, status, title, html, screenshot }`, the screenshot a base64 PNG. */
snapshot(url, opts = {}) {
return this._c._post('/browser/snapshot', browserBody(url, opts));
}
/** Open a session, at `url` when given; `idleTtlS` (seconds, default 300) deletes it after that long unused. */
async createSession({ url, viewport, idleTtlS, waitMs } = {}) {
return new BrowserSession(this._c, await this._c._post('/browser/sessions', dropUndefined({ url, viewport, idle_ttl_s: idleTtlS, wait_ms: waitMs })));
}
async getSession(id) {
return new BrowserSession(this._c, await this._c._get(`/browser/sessions/${q(id)}`));
}
async listSessions() {
return (await this._c._get('/browser/sessions')).sessions.map((s) => new BrowserSession(this._c, s));
}
}
/** A browser session: a simulation whose machine's whole screen is the browser, driven by actions. */
export class BrowserSession {
constructor(client, info) {
this._c = client;
this.id = info.id;
this.info = info;
this._p = `/browser/sessions/${q(info.id)}`;
}
get url() {
return this.info.url;
}
get viewport() {
return this.info.viewport;
}
async refresh() {
return (this.info = await this._c._get(this._p));
}
/** Up to 64 `browser.v1`, `keyboard.v1` or `pointer.v1` actions `{ family, op, payload }`: `{ outcomes, state_hash, tick }`. */
act(actions) {