@agentage/cli 0.56.0 → 0.57.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.
@@ -1,8 +1,7 @@
1
1
  import { isRecord } from '../fs/json-file.js';
2
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();
3
+ import { forgetRouteBases, routeCall } from '../net/route-base.js';
4
+ export const forgetMachinesBases = forgetRouteBases;
6
5
  // The machines route's own 404 is MACHINE_NOT_FOUND; any other 404 is a host without the route.
7
6
  const missingRoute = async (res) => {
8
7
  if (res.status !== 404)
@@ -15,20 +14,4 @@ const missingRoute = async (res) => {
15
14
  return error['code'] !== 'MACHINE_NOT_FOUND';
16
15
  };
17
16
  // `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
- };
17
+ export const machinesCall = (platform, path, call) => routeCall({ primary: platform.machines, fallback: platform.fallback }, path, call, missingRoute);
@@ -3,6 +3,7 @@ import { CliError, messageOf } from '../json-output.js';
3
3
  import { cliFetch } from './fetch.js';
4
4
  import { answered, isTimeout, marketError, noDiscovery, noMarket, notTheDocument, unreached, } from './market-error.js';
5
5
  import { links, siteFqdn } from './origins.js';
6
+ import { routeCall } from './route-base.js';
6
7
  const TIMEOUT_MS = 5_000;
7
8
  const EXACT = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
8
9
  const timed = (url) => cliFetch(url, {}, { timeoutMs: TIMEOUT_MS });
@@ -12,8 +13,18 @@ const fetched = (get, url) => get(url).catch((err) => {
12
13
  const bodyOf = (res, url) => res.json().catch((err) => {
13
14
  throw isTimeout(err) ? unreached(url, err, TIMEOUT_MS) : answered(url, res.status, 'no JSON');
14
15
  });
15
- const json = async (get, url) => {
16
- const res = await fetched(get, url);
16
+ // Every market answers 404 NOT_FOUND for an unknown id and for a route it lacks alike.
17
+ const anyNotFound = (res) => Promise.resolve(res.status === 404);
18
+ // `/api/market/extensions` (one-image host) first, else the v13 `/api/v1/extensions`.
19
+ const json = async (get, origin, path) => {
20
+ const bases = {
21
+ primary: `${origin}/api/market/extensions`,
22
+ fallback: `${origin}/api/v1/extensions`,
23
+ };
24
+ const at = { url: '' };
25
+ const call = (url) => fetched(get, (at.url = url));
26
+ const res = await routeCall(bases, path, call, anyNotFound);
27
+ const url = at.url;
17
28
  if (res.status === 404)
18
29
  return null;
19
30
  if (!res.ok)
@@ -67,8 +78,8 @@ export const openMarket = (fqdn = siteFqdn(), get = timed, known) => {
67
78
  let data;
68
79
  try {
69
80
  base ??= marketUrl(fqdn, get);
70
- const path = `/api/v1/extensions/${encodeURIComponent(id)}/latest`;
71
- data = (await json(get, `${await base}${path}`))?.['data'];
81
+ const path = `/${encodeURIComponent(id)}/latest`;
82
+ data = (await json(get, await base, path))?.['data'];
72
83
  }
73
84
  catch (err) {
74
85
  failure ??= err instanceof CliError ? err : marketError('market_failed', messageOf(err));
@@ -84,15 +95,15 @@ const AT_ONCE = 8;
84
95
  // A market that repeats its cursor would page for ever; past this the listing stops.
85
96
  export const MAX_PAGES = 20;
86
97
  // Every row of the market list, page after page while `meta.nextCursor` names one.
87
- const pages = async (get, url, cursor, page = 1) => {
98
+ const pages = async (get, base, cursor, page = 1) => {
88
99
  if (page > MAX_PAGES)
89
100
  throw marketError('market_failed', `the market list has more than ${MAX_PAGES} pages of ${PAGE}; stopped there`);
90
101
  const query = `?limit=${PAGE}${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`;
91
- const body = await json(get, `${url}${query}`);
102
+ const body = await json(get, base, query);
92
103
  const data = Array.isArray(body?.['data']) ? body['data'] : [];
93
104
  const meta = body?.['meta'];
94
105
  const next = isRecord(meta) && typeof meta['nextCursor'] === 'string' ? meta['nextCursor'] : '';
95
- return next && data.length > 0 ? [...data, ...(await pages(get, url, next, page + 1))] : data;
106
+ return next && data.length > 0 ? [...data, ...(await pages(get, base, next, page + 1))] : data;
96
107
  };
97
108
  // A row without an exact version (the market of today lists none) asks /latest for it.
98
109
  const offered = async (row, market) => {
@@ -116,7 +127,7 @@ const offered = async (row, market) => {
116
127
  // fails it; a row that cannot be read is skipped with its reason.
117
128
  export const listMarket = async (fqdn = siteFqdn(), get = timed) => {
118
129
  const base = await marketUrl(fqdn, get);
119
- const rows = await pages(get, `${base}/api/v1/extensions`);
130
+ const rows = await pages(get, base);
120
131
  const found = [];
121
132
  for (let i = 0; i < rows.length; i += AT_ONCE) {
122
133
  const batch = rows.slice(i, i + AT_ONCE);
@@ -0,0 +1,9 @@
1
+ type Call = (url: string) => Promise<Response>;
2
+ export interface Bases {
3
+ primary: string;
4
+ fallback?: string;
5
+ }
6
+ export type Missing = (res: Response) => Promise<boolean>;
7
+ export declare const forgetRouteBases: () => void;
8
+ export declare const routeCall: ({ primary, fallback }: Bases, path: string, call: Call, missing: Missing) => Promise<Response>;
9
+ export {};
@@ -0,0 +1,34 @@
1
+ import { isRecord } from '../fs/json-file.js';
2
+ // The base that answered, per primary base: a v13 row costs one 404 per run, not one per call.
3
+ const chosen = new Map();
4
+ export const forgetRouteBases = () => chosen.clear();
5
+ // 4xx codes a route itself answers; 401, 403, 429 or an unknown body may be the edge's, not the route's.
6
+ const OWNED = new Set(['VALIDATION_ERROR', 'CONFLICT', 'PRECONDITION_FAILED', 'MACHINE_NOT_FOUND']);
7
+ const codeOf = async (res) => {
8
+ const body = await res
9
+ .clone()
10
+ .json()
11
+ .catch(() => null);
12
+ const error = isRecord(body) && isRecord(body['error']) ? body['error'] : {};
13
+ return error['code'];
14
+ };
15
+ // Only a 2xx or the route's own refusal pins a base.
16
+ const pins = async (res) => res.ok || (res.status >= 400 && res.status < 500 && OWNED.has(String(await codeOf(res))));
17
+ // `primary` first, then `fallback` on a missing route; a 5xx, 401, 403 or 429 decides nothing.
18
+ export const routeCall = async ({ primary, fallback }, path, call, missing) => {
19
+ const known = chosen.get(primary);
20
+ if (known !== undefined || fallback === undefined)
21
+ return call(`${known ?? primary}${path}`);
22
+ const res = await call(`${primary}${path}`);
23
+ if (!(await missing(res))) {
24
+ if (await pins(res))
25
+ chosen.set(primary, primary);
26
+ return res;
27
+ }
28
+ // Unawaited: a tee'd body (a clone) cancels only once its sibling does.
29
+ void res.body?.cancel().catch(() => undefined);
30
+ const old = await call(`${fallback}${path}`);
31
+ if (!(await missing(old)) && (await pins(old)))
32
+ chosen.set(primary, fallback);
33
+ return old;
34
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentage/cli",
3
- "version": "0.56.0",
3
+ "version": "0.57.0",
4
4
  "description": "The agentage CLI - connect this machine to agentage from the terminal",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",