@antasphere/cli-core 0.4.0 → 0.5.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/dist/resolve.d.ts CHANGED
@@ -1,9 +1,23 @@
1
1
  import type { CliConfig, CliEnv, CliProfile } from './config.js';
2
- /** A usage-level error: runners print `Error: <message>` and exit 1. */
2
+ /**
3
+ * A usage-level error: runners print `Error: <message>` and exit with
4
+ * `exitCode`. 1 is a usage or wire error; 3 is a refusal, the code the tool
5
+ * docs promise when the account may not do what it asked (the thrower says
6
+ * so, nothing else does).
7
+ */
3
8
  export declare class CliUsageError extends Error {
9
+ readonly exitCode: number;
10
+ constructor(message: string, opts?: {
11
+ exitCode?: number;
12
+ });
4
13
  }
5
- /** The profile name tools fall back to when none is named or active. */
6
- export declare const DEFAULT_PROFILE = "default";
14
+ /**
15
+ * The implicit profile every tool CLI has at its cloud URL. It exists before
16
+ * anything is written to disk: a clean machine with an `antasphere login`
17
+ * runs the tool on it. `default` is not a magic name any more, just a
18
+ * profile a person may have.
19
+ */
20
+ export declare const CLOUD_PROFILE = "cloud";
7
21
  export interface ResolvedProfile {
8
22
  /** The resolved profile name (undefined = flags/env only, nothing stored). */
9
23
  name: string | undefined;
@@ -15,8 +29,85 @@ export interface ResolvedProfile {
15
29
  * remedy (e.g. 'run `slideless profiles` to list them.').
16
30
  */
17
31
  export declare function resolveProfile(config: CliConfig, requested: string | undefined, opts?: {
18
- unknownHint?: string;
32
+ unknownHint?: string | undefined;
19
33
  }): ResolvedProfile;
34
+ /**
35
+ * The comparable form of an instance URL: the scheme, the host (case-folded,
36
+ * port kept) and the path without trailing slashes; query and fragment
37
+ * dropped. Two URLs name the same instance when their normalized forms are
38
+ * equal. A string the URL parser refuses is only stripped of its trailing
39
+ * slashes.
40
+ */
41
+ export declare function normalizeUrl(url: string): string;
42
+ /** Whether two URLs name the same instance (`normalizeUrl` equality). */
43
+ export declare function sameInstance(a: string, b: string): boolean;
44
+ /**
45
+ * The name an instance URL's profile gets when nothing named it: `cloud`
46
+ * for the tool's cloud URL, else the host with its port
47
+ * (`share.antasphere.com`, `localhost:3400`). The person may rename it.
48
+ */
49
+ export declare function profileNameFor(url: string, cloudUrl?: string | undefined): string;
50
+ /**
51
+ * The first value that is set, trimmed. An empty or whitespace-only flag,
52
+ * env var or profile field counts as unset and the next source applies (a
53
+ * `.env` leftover such as `SLIDELESS_URL=""` never hides the cloud URL).
54
+ */
55
+ export declare function firstSet(...values: Array<string | undefined>): string | undefined;
56
+ /** The instance a profile is at: its `baseUrl`, else the tool's cloud URL.
57
+ * A blank `baseUrl` is no `baseUrl`. */
58
+ export declare function profileUrl(profile: CliProfile, cloudUrl: string | undefined): string | undefined;
59
+ /** The rule that picked the profile. */
60
+ export type ProfileSource = 'flag' | 'url' | 'active' | 'cloud';
61
+ export interface SelectedProfile extends ResolvedProfile {
62
+ /** Which rule chose it; undefined when nothing could be selected. */
63
+ source: ProfileSource | undefined;
64
+ /**
65
+ * true = the profile is not on disk yet: it was made in memory for the
66
+ * instance URL given, or it is the implicit cloud profile, under a name
67
+ * no profile on disk has. `storeConnectKey` writes it on first contact,
68
+ * nothing else does.
69
+ */
70
+ created: boolean;
71
+ }
72
+ export interface SelectProfileOptions {
73
+ config: CliConfig;
74
+ /** Value of --profile when given: wins over everything, must exist. */
75
+ requested?: string | undefined;
76
+ /**
77
+ * The instance URL the run was pointed at, when one was: --api-url, else
78
+ * the tool's URL env var (the caller applies that order; a blank value
79
+ * counts as none). It selects the profile of that instance, or makes one
80
+ * in memory named for its host.
81
+ */
82
+ apiUrl?: string | undefined;
83
+ /** The tool's cloud home; the implicit `cloud` profile lives there. */
84
+ cloudUrl?: string | undefined;
85
+ /** Appended to the unknown-profile error, e.g. 'run `slideless profiles`'. */
86
+ unknownHint?: string;
87
+ }
88
+ /**
89
+ * A profile is an instance. The selection rule every tool CLI applies, in
90
+ * this order:
91
+ *
92
+ * 1. --profile <name>: that profile, which must exist.
93
+ * 2. --api-url / the URL env var: the profile whose instance that is (the
94
+ * active one first when several name it, then `cloud`, then the first
95
+ * declared); none → a profile made in memory, named for the host under
96
+ * a free name (`created: true`), so a cached key never travels to
97
+ * another origin and nothing is ever written onto another instance's
98
+ * profile.
99
+ * 3. The active profile, the choice the person made with `<tool> use`.
100
+ * 4. The profile at `cloudUrl`, `cloud` by default: the one on disk at
101
+ * that instance whatever its name (a `cloud` profile that lives
102
+ * elsewhere is not it), else made in memory under a free name
103
+ * (`created: true`).
104
+ *
105
+ * A profile with no `baseUrl` is at the cloud URL. Nothing here writes to
106
+ * disk, and no profile is ever repointed. A tool without a cloud URL and
107
+ * without a profile selects nothing (both fields undefined), and its URL
108
+ * resolution errors with the tool's own message.
109
+ */
110
+ export declare function selectProfile(opts: SelectProfileOptions): SelectedProfile;
20
111
  export interface ResolveBaseUrlOptions {
21
112
  /** Value of --api-url (or an alias) when given. */
22
113
  flag?: string | undefined;
@@ -24,15 +115,21 @@ export interface ResolveBaseUrlOptions {
24
115
  /** The tool's URL env var, e.g. 'ANTASPHERE_URL' / 'SLIDELESS_URL'. */
25
116
  envVar: string;
26
117
  profile?: CliProfile | undefined;
27
- /** Optional tool default (the hub has one; self-hosted tools error instead). */
28
- defaultUrl?: string;
29
- /** Error text when nothing resolves and there is no default. */
118
+ /**
119
+ * The tool's cloud home, the last fallback: every CLI that has a cloud
120
+ * instance ships it (`https://slideless.antasphere.com`), so a clean
121
+ * machine with an `antasphere login` needs no flag. A tool with none (a
122
+ * template placeholder) errors with `missingMessage` as before.
123
+ */
124
+ cloudUrl?: string | undefined;
125
+ /** Error text when nothing resolves and there is no cloud URL. */
30
126
  missingMessage?: string;
31
127
  }
32
128
  /**
33
129
  * Base-URL resolution in the ecosystem-wide order:
34
- * flag → tool env var → profile → optional default → CliUsageError.
35
- * The result is origin-normalized (trailing slashes stripped).
130
+ * flag → tool env var → profile → cloud URL → CliUsageError.
131
+ * A blank value counts as unset (the next source applies). The result is
132
+ * origin-normalized (trailing slashes stripped).
36
133
  */
37
134
  export declare function resolveBaseUrl(opts: ResolveBaseUrlOptions): string;
38
135
  export interface ResolveApiKeyOptions {
@@ -43,6 +140,24 @@ export interface ResolveApiKeyOptions {
43
140
  envVar: string;
44
141
  profile?: CliProfile | undefined;
45
142
  }
46
- /** API-key resolution: flag → tool env var → profile (undefined = public only). */
143
+ /** API-key resolution: flag → tool env var → profile (undefined = public
144
+ * only); a blank value counts as unset. */
47
145
  export declare function resolveApiKey(opts: ResolveApiKeyOptions): string | undefined;
146
+ export interface ResolveOrgOptions {
147
+ /** Value of --org when given: a hub organization id or name. */
148
+ flag?: string | undefined;
149
+ env: CliEnv;
150
+ /** The tool's org env var, e.g. 'ANTASPHERE_ORG' / 'SLIDELESS_ORG'. */
151
+ envVar: string;
152
+ }
153
+ /**
154
+ * The organization override for one command: --org, else the tool's org
155
+ * env var, else undefined. There is one organization notion in the family:
156
+ * the hub's server-side default organization applies when this answers
157
+ * undefined, and the override names a hub organization (id or name) that
158
+ * the tool maps to its own workspace through `central_account_id`. An empty
159
+ * or blank value counts as unset and the next source applies. Nothing is
160
+ * stored on this machine; nothing here ever reaches the exchange.
161
+ */
162
+ export declare function resolveOrg(opts: ResolveOrgOptions): string | undefined;
48
163
  //# sourceMappingURL=resolve.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEjE,wEAAwE;AACxE,qBAAa,aAAc,SAAQ,KAAK;CAAG;AAE3C,wEAAwE;AACxE,eAAO,MAAM,eAAe,YAAY,CAAC;AAEzC,MAAM,WAAW,eAAe;IAC9B,8EAA8E;IAC9E,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IACzB,OAAO,EAAE,UAAU,GAAG,SAAS,CAAC;CACjC;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAC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AI,EAAE,iBAAiB,GAAG,MAAM,GAAG,SAAS,CAEtE"}
package/dist/resolve.js CHANGED
@@ -1,8 +1,24 @@
1
- /** A usage-level error: runners print `Error: <message>` and exit 1. */
1
+ /**
2
+ * A usage-level error: runners print `Error: <message>` and exit with
3
+ * `exitCode`. 1 is a usage or wire error; 3 is a refusal, the code the tool
4
+ * docs promise when the account may not do what it asked (the thrower says
5
+ * so, nothing else does).
6
+ */
2
7
  export class CliUsageError extends Error {
8
+ exitCode;
9
+ constructor(message, opts = {}) {
10
+ super(message);
11
+ this.name = 'CliUsageError';
12
+ this.exitCode = opts.exitCode ?? 1;
13
+ }
3
14
  }
4
- /** The profile name tools fall back to when none is named or active. */
5
- export const DEFAULT_PROFILE = 'default';
15
+ /**
16
+ * The implicit profile every tool CLI has at its cloud URL. It exists before
17
+ * anything is written to disk: a clean machine with an `antasphere login`
18
+ * runs the tool on it. `default` is not a magic name any more, just a
19
+ * profile a person may have.
20
+ */
21
+ export const CLOUD_PROFILE = 'cloud';
6
22
  /**
7
23
  * The named (or active) profile of a tool's config, erroring on an
8
24
  * explicitly named missing one. `unknownHint` lets the tool append its own
@@ -20,20 +36,163 @@ export function resolveProfile(config, requested, opts = {}) {
20
36
  const profile = name ? config.profiles[name] : undefined;
21
37
  return { name: profile ? name : undefined, profile };
22
38
  }
39
+ /**
40
+ * The comparable form of an instance URL: the scheme, the host (case-folded,
41
+ * port kept) and the path without trailing slashes; query and fragment
42
+ * dropped. Two URLs name the same instance when their normalized forms are
43
+ * equal. A string the URL parser refuses is only stripped of its trailing
44
+ * slashes.
45
+ */
46
+ export function normalizeUrl(url) {
47
+ try {
48
+ const u = new URL(url);
49
+ return `${u.protocol}//${u.host}${u.pathname}`.replace(/\/+$/, '');
50
+ }
51
+ catch {
52
+ return url.replace(/\/+$/, '');
53
+ }
54
+ }
55
+ /** Whether two URLs name the same instance (`normalizeUrl` equality). */
56
+ export function sameInstance(a, b) {
57
+ return normalizeUrl(a) === normalizeUrl(b);
58
+ }
59
+ /**
60
+ * The name an instance URL's profile gets when nothing named it: `cloud`
61
+ * for the tool's cloud URL, else the host with its port
62
+ * (`share.antasphere.com`, `localhost:3400`). The person may rename it.
63
+ */
64
+ export function profileNameFor(url, cloudUrl) {
65
+ if (cloudUrl && sameInstance(url, cloudUrl))
66
+ return CLOUD_PROFILE;
67
+ try {
68
+ return new URL(url).host;
69
+ }
70
+ catch {
71
+ return normalizeUrl(url).replace(/^[a-z][a-z0-9+.-]*:\/\//i, '');
72
+ }
73
+ }
74
+ /**
75
+ * The first value that is set, trimmed. An empty or whitespace-only flag,
76
+ * env var or profile field counts as unset and the next source applies (a
77
+ * `.env` leftover such as `SLIDELESS_URL=""` never hides the cloud URL).
78
+ */
79
+ export function firstSet(...values) {
80
+ for (const v of values) {
81
+ const t = v?.trim();
82
+ if (t)
83
+ return t;
84
+ }
85
+ return undefined;
86
+ }
87
+ /** The instance a profile is at: its `baseUrl`, else the tool's cloud URL.
88
+ * A blank `baseUrl` is no `baseUrl`. */
89
+ export function profileUrl(profile, cloudUrl) {
90
+ return firstSet(profile.baseUrl, cloudUrl);
91
+ }
92
+ /**
93
+ * The profile on disk at an instance, if any. When several name it: the
94
+ * active one, then the one named `cloud`, then the first declared.
95
+ */
96
+ function profileAt(config, url, cloudUrl) {
97
+ const at = Object.entries(config.profiles).filter(([, p]) => {
98
+ const u = profileUrl(p, cloudUrl);
99
+ return u !== undefined && sameInstance(u, url);
100
+ });
101
+ const pick = at.find(([name]) => name === config.activeProfile) ??
102
+ at.find(([name]) => name === CLOUD_PROFILE) ??
103
+ at[0];
104
+ return pick ? { name: pick[0], profile: pick[1] } : undefined;
105
+ }
106
+ /**
107
+ * A profile made in memory for an instance nothing on disk names, under a
108
+ * name nothing on disk has: `cloud` for the cloud URL, else the host; when
109
+ * that name belongs to a profile at ANOTHER instance, the host, then
110
+ * `<host>-2`, `<host>-3`, … so `storeConnectKey` never writes onto a
111
+ * profile that is not this instance's.
112
+ */
113
+ function freshProfileAt(config, url, cloudUrl) {
114
+ const host = profileNameFor(url);
115
+ let name = [profileNameFor(url, cloudUrl), host].find((c) => !(c in config.profiles));
116
+ for (let n = 2; name === undefined; n += 1) {
117
+ if (!(`${host}-${n}` in config.profiles))
118
+ name = `${host}-${n}`;
119
+ }
120
+ return { name, profile: { baseUrl: normalizeUrl(url) } };
121
+ }
122
+ /**
123
+ * A profile is an instance. The selection rule every tool CLI applies, in
124
+ * this order:
125
+ *
126
+ * 1. --profile <name>: that profile, which must exist.
127
+ * 2. --api-url / the URL env var: the profile whose instance that is (the
128
+ * active one first when several name it, then `cloud`, then the first
129
+ * declared); none → a profile made in memory, named for the host under
130
+ * a free name (`created: true`), so a cached key never travels to
131
+ * another origin and nothing is ever written onto another instance's
132
+ * profile.
133
+ * 3. The active profile, the choice the person made with `<tool> use`.
134
+ * 4. The profile at `cloudUrl`, `cloud` by default: the one on disk at
135
+ * that instance whatever its name (a `cloud` profile that lives
136
+ * elsewhere is not it), else made in memory under a free name
137
+ * (`created: true`).
138
+ *
139
+ * A profile with no `baseUrl` is at the cloud URL. Nothing here writes to
140
+ * disk, and no profile is ever repointed. A tool without a cloud URL and
141
+ * without a profile selects nothing (both fields undefined), and its URL
142
+ * resolution errors with the tool's own message.
143
+ */
144
+ export function selectProfile(opts) {
145
+ const { config, cloudUrl } = opts;
146
+ if (opts.requested) {
147
+ const { name, profile } = resolveProfile(config, opts.requested, { unknownHint: opts.unknownHint });
148
+ return { name, profile, source: 'flag', created: false };
149
+ }
150
+ const apiUrl = firstSet(opts.apiUrl);
151
+ if (apiUrl) {
152
+ const found = profileAt(config, apiUrl, cloudUrl);
153
+ if (found)
154
+ return { ...found, source: 'url', created: false };
155
+ return { ...freshProfileAt(config, apiUrl, cloudUrl), source: 'url', created: true };
156
+ }
157
+ const active = resolveProfile(config, undefined);
158
+ if (active.name && active.profile)
159
+ return { ...active, source: 'active', created: false };
160
+ if (cloudUrl) {
161
+ const found = profileAt(config, cloudUrl, cloudUrl);
162
+ if (found)
163
+ return { ...found, source: 'cloud', created: false };
164
+ return { ...freshProfileAt(config, cloudUrl, cloudUrl), source: 'cloud', created: true };
165
+ }
166
+ return { name: undefined, profile: undefined, source: undefined, created: false };
167
+ }
23
168
  /**
24
169
  * Base-URL resolution in the ecosystem-wide order:
25
- * flag → tool env var → profile → optional default → CliUsageError.
26
- * The result is origin-normalized (trailing slashes stripped).
170
+ * flag → tool env var → profile → cloud URL → CliUsageError.
171
+ * A blank value counts as unset (the next source applies). The result is
172
+ * origin-normalized (trailing slashes stripped).
27
173
  */
28
174
  export function resolveBaseUrl(opts) {
29
- const raw = opts.flag ?? opts.env[opts.envVar] ?? opts.profile?.baseUrl ?? opts.defaultUrl;
175
+ const raw = firstSet(opts.flag, opts.env[opts.envVar], opts.profile?.baseUrl, opts.cloudUrl);
30
176
  if (!raw) {
31
177
  throw new CliUsageError(opts.missingMessage ?? `No instance configured — pass --api-url <url> or set ${opts.envVar}.`);
32
178
  }
33
179
  return raw.replace(/\/+$/, '');
34
180
  }
35
- /** API-key resolution: flag → tool env var → profile (undefined = public only). */
181
+ /** API-key resolution: flag → tool env var → profile (undefined = public
182
+ * only); a blank value counts as unset. */
36
183
  export function resolveApiKey(opts) {
37
- return opts.flag ?? opts.env[opts.envVar] ?? opts.profile?.apiKey;
184
+ return firstSet(opts.flag, opts.env[opts.envVar], opts.profile?.apiKey);
185
+ }
186
+ /**
187
+ * The organization override for one command: --org, else the tool's org
188
+ * env var, else undefined. There is one organization notion in the family:
189
+ * the hub's server-side default organization applies when this answers
190
+ * undefined, and the override names a hub organization (id or name) that
191
+ * the tool maps to its own workspace through `central_account_id`. An empty
192
+ * or blank value counts as unset and the next source applies. Nothing is
193
+ * stored on this machine; nothing here ever reaches the exchange.
194
+ */
195
+ export function resolveOrg(opts) {
196
+ return firstSet(opts.flag, opts.env[opts.envVar]);
38
197
  }
39
198
  //# sourceMappingURL=resolve.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve.js","sourceRoot":"","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GAAG,CAC9F,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;AACjC,CAAC;AAWD;4CAC4C;AAC5C,MAAM,UAAU,aAAa,CAAC,IAA0B;IACtD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;AAC1E,CAAC;AAUD;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAuB;IAChD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;AACpD,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@antasphere/cli-core",
3
- "version": "0.4.0",
4
- "description": "Shared CLI foundation for Antasphere tool CLIs: the ~/.config/antasphere config home, tool-namespaced profiles, URL resolution, the instance email-OTP auth client, the hub→tool cross-tool connect exchange with its connect-on-demand seam, and the CliIo prompt seam",
3
+ "version": "0.5.0",
4
+ "description": "Shared CLI foundation for Antasphere tool CLIs: the ~/.config/antasphere config home, profiles that are instances (with the implicit cloud profile), the flag → env → profile → cloud URL resolution, the per-command organization override, the instance email-OTP auth client, the hub→tool connect exchange with its connect-on-demand seam and stranded-key recovery, and the CliIo prompt seam",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
package/src/config.ts CHANGED
@@ -16,9 +16,16 @@ import { dirname, join } from 'node:path';
16
16
  * └── <tool>.json 0600 every other tool's profiles
17
17
  * (e.g. tools/slideless.json)
18
18
  *
19
- * Each file holds one tool's `{ activeProfile?, profiles }`: multiple named
20
- * profiles per tool, one active profile per tool. Hub profiles additionally
21
- * carry the active org (`workspaceId`) their credential is bound to.
19
+ * Each file holds one tool's `{ activeProfile?, profiles }`. A PROFILE IS AN
20
+ * INSTANCE: it names the tool instance it talks to (`baseUrl`; absent = the
21
+ * tool's cloud URL) and carries the credentials for that instance, either
22
+ * the tool's own key (`apiKey`, the tool's login) or the keys the hub
23
+ * exchange minted (`connectKeys`, one per hub profile). Profiles are named
24
+ * for their host by default (`cloud` for the cloud instance,
25
+ * `share.antasphere.com`, `localhost:3400`); the person may rename them and
26
+ * chooses the active one with `<tool> use`. The hub profile additionally
27
+ * carries `workspaceId`, the default organization at login (informational;
28
+ * the hub's server-side default is what applies).
22
29
  *
23
30
  * Files store API keys, so directories are created 0700 and files kept 0600
24
31
  * on every write. All I/O is synchronous and env-injected (no process.env
@@ -30,11 +37,11 @@ export type CliEnv = Record<string, string | undefined>;
30
37
 
31
38
  /**
32
39
  * A tool-local key minted by the cross-tool connect exchange (binding
33
- * patterns §7), cached in the tool's profile under the HUB PROFILE whose
34
- * `antasphere login` credential minted it. The key is USER-scoped (hub
35
- * ADR 014): one key serves every org — the org is a per-request parameter,
36
- * never part of the credential or of this cache's slot. Every field beyond
37
- * the key itself is informational.
40
+ * patterns §7), cached on the profile of the instance it was minted for,
41
+ * under the HUB PROFILE whose `antasphere login` credential minted it. The
42
+ * key is USER-scoped (hub ADR 014): one key serves every org — the org is a
43
+ * per-request parameter, never part of the credential or of this cache's
44
+ * slot. Every field beyond the key itself is informational.
38
45
  */
39
46
  export interface CliConnectKey {
40
47
  /** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
@@ -46,20 +53,20 @@ export interface CliConnectKey {
46
53
  }
47
54
 
48
55
  export interface CliProfile {
49
- /** `<prefix>_...` API key for this profile. */
56
+ /** `<prefix>_...` API key for this profile (the tool's own login). */
50
57
  apiKey?: string;
51
- /** Instance origin; absent = the tool's default instance, when it has one. */
58
+ /** The instance this profile IS: its origin. Absent = the tool's cloud URL. */
52
59
  baseUrl?: string;
53
60
  /** The signed-in account's email (informational, set by login). */
54
61
  email?: string;
55
- /** The org/workspace the key is bound to (one workspace per credential). */
62
+ /** Hub profile: the default organization at login (informational; the
63
+ * hub's server-side default is what applies, `--org` overrides per
64
+ * command). Tool profiles from a tool's own login may carry the
65
+ * workspace that login bound. */
56
66
  workspaceId?: string;
57
- /** Hub profiles only: the org selected via `antasphere org use` (the
58
- * default per-request org context; never part of any credential). */
59
- activeWorkspaceId?: string;
60
67
  /** Tool profiles only: exchange-minted USER-scoped keys, ONE per hub
61
- * profile — the (tool, hub profile) cache of the cross-tool connect
62
- * flow. Org-independent: a cached key serves every org the user selects. */
68
+ * profile — the (tool profile, hub profile) cache of the cross-tool
69
+ * connect flow. Org-independent: a cached key serves every org. */
63
70
  connectKeys?: Record<string, CliConnectKey>;
64
71
  }
65
72
 
@@ -161,33 +168,71 @@ export function lookupConnectKey(
161
168
  return profile?.connectKeys?.[hubProfileName]?.apiKey;
162
169
  }
163
170
 
171
+ /** How a profile signs in, for a `profiles`-style listing. */
172
+ export type ProfileCredential = 'key' | 'hub' | 'none';
173
+
174
+ /**
175
+ * What a profile signs in with, in the order the tool resolves it: its own
176
+ * `apiKey` (`key`, the tool's login), else a key the hub exchange minted
177
+ * (`hub`: under the named hub profile, or under any when none is named),
178
+ * else `none`. A listing can then say which profiles ride the hub login.
179
+ */
180
+ export function profileCredential(
181
+ profile: CliProfile | undefined,
182
+ hubProfileName?: string
183
+ ): ProfileCredential {
184
+ if (!profile) return 'none';
185
+ if (profile.apiKey) return 'key';
186
+ const slots = profile.connectKeys ?? {};
187
+ const connected = hubProfileName
188
+ ? Boolean(slots[hubProfileName]?.apiKey)
189
+ : Object.values(slots).some((k) => k.apiKey);
190
+ return connected ? 'hub' : 'none';
191
+ }
192
+
193
+ export interface StoreConnectKeyOptions {
194
+ /**
195
+ * The instance the key was minted for. Written on the profile when it is
196
+ * created here or has no `baseUrl` of its own (a blank one counts as
197
+ * none); a `baseUrl` that is there is never overwritten (a profile is
198
+ * never repointed).
199
+ */
200
+ baseUrl?: string;
201
+ }
202
+
164
203
  /**
165
- * Cache an exchange-minted tool key under (profile, hub profile), creating
166
- * the profile on first contact (and making it active when no profile is
167
- * active yet — first cloud connect on a fresh machine). Every other profile
168
- * field is preserved untouched. Returns the file written.
204
+ * Cache an exchange-minted tool key on the SELECTED profile under the hub
205
+ * profile's name. A profile not on disk yet is created (with `baseUrl` when
206
+ * given) and made active only when the tool had NO profile at all: the
207
+ * first cloud connect on a fresh machine. An existing profile is never
208
+ * renamed, repointed or made active; every other field is preserved.
209
+ * Returns the file written.
169
210
  */
170
211
  export function storeConnectKey(
171
212
  env: CliEnv,
172
213
  tool: string,
173
214
  profileName: string,
174
215
  hubProfileName: string,
175
- entry: CliConnectKey
216
+ entry: CliConnectKey,
217
+ opts: StoreConnectKeyOptions = {}
176
218
  ): string {
177
219
  const config = loadConfig(env, tool);
220
+ const first = Object.keys(config.profiles).length === 0;
178
221
  const profile = config.profiles[profileName] ?? {};
179
222
  config.profiles[profileName] = {
180
223
  ...profile,
224
+ ...(!profile.baseUrl?.trim() && opts.baseUrl ? { baseUrl: opts.baseUrl } : {}),
181
225
  connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
182
226
  };
183
- if (!config.activeProfile) config.activeProfile = profileName;
227
+ if (first) config.activeProfile = profileName;
184
228
  return saveConfig(env, tool, config);
185
229
  }
186
230
 
187
231
  /**
188
- * Drop one hub profile's cached connect key (disconnect this tool from the
189
- * hub account). Returns whether anything was removed; the legacy single
190
- * `apiKey` is never touched — it belongs to the tool's own login flow.
232
+ * Drop one hub profile's cached connect key from a profile (disconnect this
233
+ * tool from the hub account, or evict a key the hub revoked). Returns
234
+ * whether anything was removed; the tool's own `apiKey` is never touched —
235
+ * it belongs to the tool's own login flow.
191
236
  */
192
237
  export function removeConnectKey(
193
238
  env: CliEnv,