@agentage/cli 0.55.0 → 0.56.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.
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Platform } from '../net/discovery.js';
|
|
2
|
+
type Call = (url: string) => Promise<Response>;
|
|
3
|
+
export declare const forgetMachinesBases: () => void;
|
|
4
|
+
export declare const machinesCall: (platform: Platform, path: string, call: Call) => Promise<Response>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isRecord } from '../fs/json-file.js';
|
|
2
|
+
import {} from '../net/discovery.js';
|
|
3
|
+
// The base that answered, per platform: a v13 row costs one 404 per run, not one per call.
|
|
4
|
+
const chosen = new Map();
|
|
5
|
+
export const forgetMachinesBases = () => chosen.clear();
|
|
6
|
+
// The machines route's own 404 is MACHINE_NOT_FOUND; any other 404 is a host without the route.
|
|
7
|
+
const missingRoute = async (res) => {
|
|
8
|
+
if (res.status !== 404)
|
|
9
|
+
return false;
|
|
10
|
+
const body = await res
|
|
11
|
+
.clone()
|
|
12
|
+
.json()
|
|
13
|
+
.catch(() => null);
|
|
14
|
+
const error = isRecord(body) && isRecord(body['error']) ? body['error'] : {};
|
|
15
|
+
return error['code'] !== 'MACHINE_NOT_FOUND';
|
|
16
|
+
};
|
|
17
|
+
// `machines` first, then the v13 `fallback` on a missing route; a 5xx decides nothing.
|
|
18
|
+
export const machinesCall = async (platform, path, call) => {
|
|
19
|
+
const { machines, fallback } = platform;
|
|
20
|
+
const known = chosen.get(machines);
|
|
21
|
+
if (known !== undefined || fallback === undefined)
|
|
22
|
+
return call(`${known ?? machines}${path}`);
|
|
23
|
+
const res = await call(`${machines}${path}`);
|
|
24
|
+
if (!(await missingRoute(res))) {
|
|
25
|
+
if (res.status < 500)
|
|
26
|
+
chosen.set(machines, machines);
|
|
27
|
+
return res;
|
|
28
|
+
}
|
|
29
|
+
await res.body?.cancel();
|
|
30
|
+
const old = await call(`${fallback}${path}`);
|
|
31
|
+
if (old.status < 500 && !(await missingRoute(old)))
|
|
32
|
+
chosen.set(machines, fallback);
|
|
33
|
+
return old;
|
|
34
|
+
};
|
|
@@ -5,6 +5,7 @@ import { cliFetch } from '../net/fetch.js';
|
|
|
5
5
|
import {} from '../net/origins.js';
|
|
6
6
|
import { VERSION } from '../../utils/version.js';
|
|
7
7
|
import { authedFetch, updateSession } from './api.js';
|
|
8
|
+
import { machinesCall } from './machines-base.js';
|
|
8
9
|
const DAY_MS = 86_400_000;
|
|
9
10
|
const SCOPES = ['platform:read', 'platform:write'];
|
|
10
11
|
const MINT = JSON.stringify({
|
|
@@ -72,8 +73,7 @@ const tokenFor = async (auth, links, renew, opts) => {
|
|
|
72
73
|
return { minted: true, token };
|
|
73
74
|
};
|
|
74
75
|
// A discovered platform takes only a JWT PAT the session mints; an env PAT goes as is.
|
|
75
|
-
|
|
76
|
-
const url = `${found.platform.machines}${path}`;
|
|
76
|
+
const fetchAt = async (auth, links, found, url, init, opts) => {
|
|
77
77
|
if (!found.discovered)
|
|
78
78
|
return authedFetch(auth, links, url, init, opts);
|
|
79
79
|
if (found.links.auth !== links.auth)
|
|
@@ -97,3 +97,5 @@ export const platformFetch = async (auth, links, found, path, init, opts) => {
|
|
|
97
97
|
await res.body?.cancel();
|
|
98
98
|
throw refuse(links.auth, 'platform token refused (401) by the platform');
|
|
99
99
|
};
|
|
100
|
+
// The machines routes at `<platform>/api/platform/machines`, else the v13 `/api/v1/machines`.
|
|
101
|
+
export const platformFetch = (auth, links, found, path, init, opts) => machinesCall(found.platform, path, (url) => fetchAt(auth, links, found, url, init, opts));
|
|
@@ -48,7 +48,11 @@ const platformOf = (doc, site, url) => {
|
|
|
48
48
|
if (typeof platform !== 'string' || !sameSite(platform, site))
|
|
49
49
|
throw new Error(`FAIL discovery: ${url} has no "platform"`);
|
|
50
50
|
const origin = new URL(platform).origin;
|
|
51
|
-
return {
|
|
51
|
+
return {
|
|
52
|
+
url: origin,
|
|
53
|
+
machines: `${origin}/api/platform/machines`,
|
|
54
|
+
fallback: `${origin}/api/v1/machines`,
|
|
55
|
+
};
|
|
52
56
|
};
|
|
53
57
|
const issuerOf = (doc, base, url) => {
|
|
54
58
|
const issuer = isRecord(doc) ? doc['issuer'] : undefined;
|