@agentage/cli 0.49.0 → 0.50.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.
@@ -3,6 +3,7 @@ import { authedFetch } from '../../lib/auth/api.js';
3
3
  import { resolveAuth } from '../../lib/auth/credentials.js';
4
4
  import { detectEnvMismatch } from '../../lib/auth/env-match.js';
5
5
  import { introspectToken } from '../../lib/auth/introspect.js';
6
+ import { discoverPlatform } from '../../lib/net/discovery.js';
6
7
  import { apiOrigin, links, siteFqdn } from '../../lib/net/origins.js';
7
8
  const CLIENT = { 'x-client-type': 'cli' };
8
9
  // Read before any extension loads: the token is captured here, the fetch at CLI start.
@@ -12,7 +13,12 @@ export const signedInAccount = (opts = {}) => {
12
13
  if (auth === null || detectEnvMismatch(auth, fqdn) !== null)
13
14
  return null;
14
15
  const target = links(fqdn);
15
- const fetch = (path, { headers, timeoutMs, ...init } = {}) => authedFetch(auth, target, `${apiOrigin(fqdn)}${path}`, { ...init, headers: { ...CLIENT, ...headers } }, { timeoutMs });
16
+ const to = (base) => async (path, { headers, timeoutMs, ...init } = {}) => authedFetch(auth, target, `${await base()}${path}`, { ...init, headers: { ...CLIENT, ...headers } }, { timeoutMs });
17
+ let found;
18
+ const discovered = () => (found ??= discoverPlatform(fqdn));
19
+ const platform = async () => (await discovered()).url;
20
+ const fetch = to(() => apiOrigin(fqdn));
21
+ const machines = to(async () => (await discovered()).machines);
16
22
  const id = () => introspectToken(auth, target).then((s) => s.userId ?? null, () => null);
17
- return { fetch, id };
23
+ return { fetch, machines, platform, id };
18
24
  };
@@ -42,6 +42,7 @@ export interface ExtensionsStatus {
42
42
  id: string;
43
43
  name: string;
44
44
  registered?: boolean | null;
45
+ platform?: string | null;
45
46
  };
46
47
  profile: ProfileStatus;
47
48
  plan: PlanSummary;
@@ -77,13 +77,15 @@ export const accountSetup = async (deps) => {
77
77
  const account = deps.account();
78
78
  if (account === null)
79
79
  return fromLocalCopy('not signed in', deps);
80
+ const platform = await account.platform();
80
81
  const machine = await connect(account);
81
- const registered = registerMachine(account.fetch, machine);
82
+ const registered = registerMachine(account.machines, machine);
82
83
  try {
83
84
  await profileFrom(account, machine, deps);
84
85
  }
85
86
  finally {
86
- await registered;
87
+ if (await registered)
88
+ console.log(line('registered', `${machine.id} at ${platform}`));
87
89
  }
88
90
  };
89
91
  // setup --disconnect: the account marks this machine disconnected, then it forgets the account.
@@ -91,7 +93,7 @@ export const disconnect = async (deps) => {
91
93
  const machine = readMachine();
92
94
  const account = deps.account();
93
95
  if (machine !== null && account !== null)
94
- await disconnectMachine(account.fetch, machine.id);
96
+ await disconnectMachine(account.machines, machine.id);
95
97
  await deps.signOut();
96
98
  if (machine !== null)
97
99
  await forgetAccount();
@@ -18,10 +18,15 @@ export const gatherWithAccount = async (account) => {
18
18
  const [registered, cloud] = account === null
19
19
  ? [null, null]
20
20
  : await Promise.all([
21
- seenMachine(account.fetch, machine.id),
21
+ seenMachine(account.machines, machine.id),
22
22
  accountProfile(account.fetch, profile),
23
23
  ]);
24
24
  const compared = cloud !== null && profile.state === 'ok' ? { ...profile, cloud } : profile;
25
- const status = { ...gathered.status, machine: { ...machine, registered }, profile: compared };
25
+ const platform = account === null ? null : await account.platform().catch(() => null);
26
+ const status = {
27
+ ...gathered.status,
28
+ machine: { ...machine, registered, platform },
29
+ profile: compared,
30
+ };
26
31
  return { ...gathered, status };
27
32
  };
@@ -10,6 +10,8 @@ export interface CloudInit {
10
10
  export type CloudFetch = (path: string, init?: CloudInit) => Promise<Response>;
11
11
  export interface Account {
12
12
  fetch: CloudFetch;
13
+ machines: CloudFetch;
14
+ platform: () => Promise<string>;
13
15
  id: () => Promise<string | null>;
14
16
  }
15
17
  export interface ProfileWrite {
@@ -1,5 +1,5 @@
1
1
  import { type CloudFetch } from './cloud.js';
2
2
  import { type MachineFile } from './machine.js';
3
- export declare const registerMachine: (fetch: CloudFetch, machine: Pick<MachineFile, "id" | "name">) => Promise<void>;
3
+ export declare const registerMachine: (fetch: CloudFetch, machine: Pick<MachineFile, "id" | "name">) => Promise<boolean>;
4
4
  export declare const seenMachine: (fetch: CloudFetch, id: string) => Promise<boolean | null>;
5
5
  export declare const disconnectMachine: (fetch: CloudFetch, id: string) => Promise<void>;
@@ -4,7 +4,7 @@ import {} from './machine.js';
4
4
  // Fire and forget: never longer than this, never a failure, silent unless AGENTAGE_DEBUG=1.
5
5
  const BUDGET_MS = 2_000;
6
6
  const NAME_MAX = 128;
7
- const routeOf = (id) => `/v1/machines/${id}`;
7
+ const routeOf = (id) => `/${id}`;
8
8
  const debug = (text) => {
9
9
  if (process.env['AGENTAGE_DEBUG'] === '1')
10
10
  console.error(`machines: ${text}`);
@@ -41,8 +41,9 @@ const registration = (machine) => ({
41
41
  export const registerMachine = async (fetch, machine) => {
42
42
  const deadline = Date.now() + BUDGET_MS;
43
43
  const path = routeOf(machine.id);
44
- await send(fetch, { method: 'PUT', path, body: registration(machine) });
44
+ const put = await send(fetch, { method: 'PUT', path, body: registration(machine) });
45
45
  await send(fetch, { method: 'POST', path: `${path}/seen` }, Math.max(1, deadline - Date.now()));
46
+ return put !== null && put.status >= 200 && put.status < 300;
46
47
  };
47
48
  // true when the account knows this machine, false on MACHINE_NOT_FOUND, null when it did not
48
49
  // say: any other 404 is an API without the route, as unknown as no answer.
@@ -0,0 +1,7 @@
1
+ type Get = (url: string) => Promise<Response>;
2
+ export interface Platform {
3
+ url: string;
4
+ machines: string;
5
+ }
6
+ export declare const discoverPlatform: (fqdn: string, get?: Get) => Promise<Platform>;
7
+ export {};
@@ -0,0 +1,43 @@
1
+ import { isRecord } from '../fs/json-file.js';
2
+ import { cliFetch } from './fetch.js';
3
+ import { apiOrigin, links } from './origins.js';
4
+ const debug = (text) => {
5
+ if (process.env['AGENTAGE_DEBUG'] === '1')
6
+ console.error(`discovery: ${text}`);
7
+ };
8
+ // The bearer must not leave the site: same scheme, and the site's host or a subdomain of it.
9
+ const sameSite = (platform, site) => {
10
+ if (!URL.canParse(platform))
11
+ return false;
12
+ const [p, s] = [new URL(platform), new URL(site)];
13
+ return (p.protocol === s.protocol &&
14
+ (p.hostname === s.hostname || p.hostname.endsWith(`.${s.hostname}`)));
15
+ };
16
+ const read = async (url, get) => {
17
+ try {
18
+ const res = await get(url);
19
+ if (res.ok)
20
+ return await res.json();
21
+ if (res.status !== 404)
22
+ debug(`${url} answered ${res.status}, using the api origin`);
23
+ }
24
+ catch (err) {
25
+ debug(`${url} failed (${err.message}), using the api origin`);
26
+ }
27
+ return undefined;
28
+ };
29
+ // An unserved document (404, 5xx, redirect, unreachable) keeps the api origin; a served one without a platform fails.
30
+ export const discoverPlatform = async (fqdn, get = cliFetch) => {
31
+ const site = links(fqdn).site;
32
+ const url = `${site}/.well-known/agentage.json`;
33
+ const doc = await read(url, get);
34
+ if (doc === undefined) {
35
+ const api = apiOrigin(fqdn);
36
+ return { url: api, machines: `${api}/v1/machines` };
37
+ }
38
+ const platform = isRecord(doc) ? doc['platform'] : undefined;
39
+ if (typeof platform !== 'string' || !sameSite(platform, site))
40
+ throw new Error(`FAIL discovery: ${url} has no "platform"`);
41
+ const origin = new URL(platform).origin;
42
+ return { url: origin, machines: `${origin}/api/v1/machines` };
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentage/cli",
3
- "version": "0.49.0",
3
+ "version": "0.50.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",
@@ -55,7 +55,7 @@
55
55
  "prepublishOnly": "npm run verify"
56
56
  },
57
57
  "dependencies": {
58
- "@agentage/extension": "^0.9.0",
58
+ "@agentage/extension": "^0.10.0",
59
59
  "chalk": "^6.0.0",
60
60
  "commander": "^15.0.0",
61
61
  "open": "^11.0.0"