@antasphere/cli-core 0.3.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/README.md CHANGED
@@ -1,14 +1,28 @@
1
1
  # @antasphere/cli-core
2
2
 
3
3
  The shared foundation every Antasphere tool CLI builds on. One config home for
4
- the whole ecosystem, per-tool profile namespaces, the flag → env → profile →
5
- default URL resolution order, the instance email-OTP auth client, and the
6
- `CliIo` prompt seam. Tool commands, wire types, and API surfaces live in the
7
- tools — this package carries only config/login plumbing, never secrets.
4
+ the whole ecosystem, per-tool profile namespaces where a profile is an
5
+ instance, the flag → env → profile → cloud URL resolution order, the
6
+ per-command organization override, the instance email-OTP auth client, the
7
+ hub → tool connect exchange with its connect-on-demand seam and the
8
+ stranded-key recovery, and the `CliIo` prompt seam. Tool commands, wire types,
9
+ and API surfaces live in the tools — this package carries only config/login
10
+ plumbing, never secrets.
8
11
 
9
- Consumers today: the `antasphere` account CLI (the hub's `packages/cli`) and
10
- the `slideless` CLI. The design decisions live in the hub architecture brief
11
- (`hub-architecture.md` §8, CLI family; §4c, the CLI token flow).
12
+ The model: one identity at the hub (`antasphere login`, a user-scoped `ant_`
13
+ key); per-tool credentials minted from it by the exchange
14
+ (`POST {hub}/api/v1/sso/tool-token`, then `POST {tool}/api/v1/sso/cli-connect`
15
+ — the exchange names no organization, hub ADR 014); the organization a
16
+ per-request parameter (the hub's server-side default organization, or
17
+ `--org <hub org id or name>` on one command, which the tool maps to its own
18
+ workspace through `central_account_id`). Every tool CLI ships its cloud URL and
19
+ resolves it last, so `antasphere login` then `slideless list` works on a clean
20
+ machine.
21
+
22
+ Consumers today: the `antasphere` account CLI (the hub's `packages/cli`), the
23
+ `slideless` CLI and the `hackathon` CLI. The design decisions live in the hub
24
+ architecture brief (`hub-architecture.md` §8, CLI family; §4c, the CLI token
25
+ flow).
12
26
 
13
27
  ## The config home
14
28
 
@@ -25,7 +39,40 @@ antasphere/ 0700
25
39
  └── <tool>.json 0600 every other tool, e.g. tools/slideless.json
26
40
  ```
27
41
 
28
- Each file holds one tool's profiles:
42
+ Each file holds one tool's profiles. A tool file, e.g. `tools/slideless.json`:
43
+
44
+ ```json
45
+ {
46
+ "activeProfile": "cloud",
47
+ "profiles": {
48
+ "cloud": {
49
+ "baseUrl": "https://slideless.antasphere.com",
50
+ "connectKeys": {
51
+ "default": {
52
+ "apiKey": "slk_…",
53
+ "email": "you@example.com",
54
+ "mintedAt": "2026-09-30T09:12:44.000Z"
55
+ }
56
+ }
57
+ },
58
+ "share.antasphere.com": {
59
+ "baseUrl": "https://share.antasphere.com",
60
+ "apiKey": "slk_…"
61
+ },
62
+ "localhost:3400": {
63
+ "baseUrl": "http://localhost:3400",
64
+ "apiKey": "slk_…"
65
+ }
66
+ }
67
+ }
68
+ ```
69
+
70
+ `baseUrl` is the instance the profile is; a profile with no `baseUrl` is at the
71
+ tool's cloud URL. `apiKey` is the tool's own login; `connectKeys` holds the keys
72
+ the hub exchange minted, one per hub profile (the slot name, here `default`, is
73
+ the hub profile's name).
74
+
75
+ The hub's `config.json`:
29
76
 
30
77
  ```json
31
78
  {
@@ -41,106 +88,178 @@ Each file holds one tool's profiles:
41
88
  }
42
89
  ```
43
90
 
44
- `email` and `workspaceId` (the org the credential is bound to) are hub-profile
45
- fields; instance tools typically store `apiKey` + `baseUrl` only. All config
46
- I/O is env-injected (no `process.env` reads) — tests point the whole config
47
- world at a temp dir via `XDG_CONFIG_HOME`.
91
+ `workspaceId` is the default organization at login — informational; the hub's
92
+ server-side default is what applies. All config I/O is env-injected (no
93
+ `process.env` reads) — tests point the whole config world at a temp dir via
94
+ `XDG_CONFIG_HOME`.
95
+
96
+ ## Profiles
97
+
98
+ A profile is an instance, named for its host by default (`cloud` for the tool's
99
+ cloud URL, `share.antasphere.com`, `localhost:3400`). `selectProfile` applies
100
+ one rule, in this order:
101
+
102
+ 1. `--profile <name>` wins, and the profile must exist.
103
+ 2. `--api-url`, else the tool's URL env var, selects the profile of that
104
+ instance; when several name it, the active one first, then `cloud`, then the
105
+ first declared. When none does, a profile is made in memory, named for the
106
+ host — `cloud` when it is the cloud URL — under a name no profile on disk
107
+ has (a taken name falls back to the host, then `<host>-2`, `<host>-3`). The
108
+ chassis passes `apiUrl: firstSet(flags.apiUrl, env[URL_VAR])`, the same
109
+ merge `resolveBaseUrl` makes, so a blank flag never hides the env var.
110
+ 3. The active profile — the choice the person makes with `<tool> use <profile>`.
111
+ 4. The profile at the tool's cloud URL, `cloud` by default: the one on disk at
112
+ that instance whatever its name (a profile named `cloud` that lives at
113
+ another instance is not it), else made in memory under a free name —
114
+ `cloud`, or the host (then `<host>-2`, …) when `cloud` is taken elsewhere.
115
+
116
+ `created: true` means the selected profile is not on disk yet (made in memory
117
+ for an instance URL, or the implicit `cloud` profile) and its name is free. A
118
+ profile with no `baseUrl` is at the cloud URL, for the selection and for the
119
+ connect seam alike (the chassis hands `connectOnDemand` the same `cloudUrl`).
120
+ Selection never writes;
121
+ `storeConnectKey` is the only thing that writes such a profile, on first
122
+ contact, and makes it active only when the tool had no profile at all. It never
123
+ renames or repoints an existing profile, and never changes which profile is
124
+ active once the tool has one.
125
+
126
+ An active profile pointing at an unreachable instance is still the selection —
127
+ there is no silent fallback to `cloud`. The tool's error names the profile and
128
+ says the hub login exists; that wording is the tool's. `default` is an ordinary
129
+ profile name now, nothing pins it.
130
+
131
+ An illustration of what a tool can print for the three profiles above (a
132
+ `slideless profiles`-style listing, not a command this package provides):
133
+
134
+ ```
135
+ PROFILE INSTANCE SIGNS IN WITH
136
+ * cloud https://slideless.antasphere.com hub
137
+ share.antasphere.com https://share.antasphere.com key
138
+ localhost:3400 http://localhost:3400 key
139
+ ```
140
+
141
+ The last column is `profileCredential`: `key` (the profile's own `apiKey`, the
142
+ tool's login), else `hub` (a key the exchange minted), else `none`.
48
143
 
49
144
  ## Surface
50
145
 
51
146
  - `configPath / loadConfig / saveConfig / clearConfig (env, tool)` — the
52
147
  tool-namespaced store. `HUB_TOOL` names the hub namespace. `redactKey`
53
- renders keys safely for terminals.
54
- - `resolveProfile(config, requested, { unknownHint })` — the named (or
55
- active) profile; an explicitly named missing profile throws `CliUsageError`.
56
- - `resolveBaseUrl({ flag, env, envVar, profile, defaultUrl, missingMessage })`
57
- — flag → tool env var → profile → optional default, origin-normalized;
58
- throws the tool's message when nothing resolves (tools without a default
59
- URL, i.e. the self-hosted ones, error deliberately).
60
- - `resolveApiKey({ flag, env, envVar, profile })` — flag → env → profile.
61
- - `CliAuthClient(baseUrl)` — the browserless email-OTP flow every instance
148
+ renders keys safely for terminals. `parseConfig(raw)` is the tolerant parse
149
+ (corrupt or foreign shapes → empty config); `assertToolNamespace(tool)`
150
+ refuses anything but a lowercase slug.
151
+ - `resolveProfile(config, requested, { unknownHint })` — the named (or active)
152
+ profile; an explicitly named missing profile throws `CliUsageError`.
153
+ - `selectProfile({ config, requested, apiUrl, cloudUrl, unknownHint })` — the
154
+ selection rule of [Profiles](#profiles); returns
155
+ `{ name, profile, source, created }`, `source` being `flag`, `url`, `active`
156
+ or `cloud`. Nothing is written, no profile is repointed.
157
+ - `CLOUD_PROFILE` — `'cloud'`, the implicit profile at the tool's cloud URL.
158
+ - `normalizeUrl(url) / sameInstance(a, b) / profileNameFor(url, cloudUrl)` —
159
+ the comparable form of an instance URL (scheme, host with port, path; no
160
+ trailing slash, query or fragment), equality on it, and the name an
161
+ instance's profile gets (`cloud`, else the host with its port).
162
+ `profileUrl(profile, cloudUrl)` is the instance a profile is at (its
163
+ `baseUrl`, a blank one counting as none, else the cloud URL);
164
+ `firstSet(...values)` the first non-blank value, trimmed — the one rule
165
+ every resolver applies to flags, env vars and profile fields, the hub
166
+ profile's `apiKey` and `baseUrl` included.
167
+ - `resolveBaseUrl({ flag, env, envVar, profile, cloudUrl, missingMessage })` —
168
+ flag → tool env var → profile → cloud URL, trailing slashes stripped; a
169
+ blank value counts as unset (`SLIDELESS_URL=""` never hides the cloud URL);
170
+ throws `missingMessage` as a `CliUsageError` only when nothing resolves and
171
+ the tool has no cloud URL.
172
+ - `resolveApiKey({ flag, env, envVar, profile })` — flag → env → profile, blank
173
+ values skipped.
174
+ - `resolveOrg({ flag, env, envVar })` — the organization override for one
175
+ command: `--org`, else the tool's org env var, else `undefined` (the hub's
176
+ server-side default applies); blank values skipped. Nothing is stored; it
177
+ never reaches the exchange.
178
+ - `CliUsageError(message, { exitCode })` — a usage-level error; runners print
179
+ it and exit with `exitCode` (default 1).
180
+ - `CliAuthClient({ baseUrl, fetch? })` — the browserless email-OTP flow every instance
62
181
  speaks: `request({ email })`, `complete({ email, otp, keyName?,
63
182
  expiresInDays?, workspaceId? })` (the full key appears exactly once),
64
- `revoke(apiKey)` (self-revocation = logout). Errors are `CliAuthError`
65
- with `status`/`code`/`message`.
66
- - `CliIo` + `ask` + `printJson` — the injectable I/O seam (env, streams,
67
- fetch, interactive prompt) so tool CLIs run in-process under test.
68
- - `CliConnectClient({ hubBaseUrl, toolBaseUrl })` — the cross-tool connect
183
+ `revoke(apiKey)` (self-revocation = logout). Errors are `CliAuthError` with
184
+ `status`/`code`/`message`.
185
+ - `CliIo` + `ask` + `printJson` — the injectable I/O seam (env, streams, fetch,
186
+ interactive prompt) so tool CLIs run in-process under test.
187
+ - `CliConnectClient({ hubBaseUrl, toolBaseUrl, fetch? })` — the cross-tool connect
69
188
  exchange (binding patterns §7, gcloud model — one hub login serves every
70
- federated tool): `toolToken(hubApiKey, { resource, workspaceId? })` asks
71
- the hub for a 120 s single-org JWT, `cliConnect(token)` trades it at the
72
- tool for a tool-local key (no auth header — the JWT is the credential),
73
- `exchange(hubApiKey, body)` composes both; the hub key never reaches the
74
- tool. Errors are `CliConnectError` (a `CliAuthError`) tagged with the
75
- failing `stage` plus the two remedies: `needsHubLogin` (hub 401, or 403
76
- `insufficient_scope` from a pre-`sso:exchange` key) and `needsReExchange`
77
- (tool 401 on an expired/replayed JWT).
78
- - `lookupWorkspaceKey / storeWorkspaceKey / removeWorkspaceKey` — the
79
- per-(tool, HUB org) cache of exchange-minted keys, held in the tool
80
- profile's additive `workspaceKeys` map. The legacy single `apiKey` keeps
81
- working unchanged and answers an org lookup only when the profile says it
82
- is bound to exactly that workspace.
83
- - `connectOnDemand({ tool, toolBaseUrl, resource?, org?, profileName?, env,
84
- fetch?, notify?, messages? })` + `probeToolInstance(toolBaseUrl, fetch?)`
85
- — the connect-on-demand seam a tool CLI runs when NO direct credential
86
- resolved (extracted from the Slideless CLI): the anonymous `GET
87
- /instance` probe gates the flow (only an Antasphere-cloud instance takes
88
- it — self-hosted/unreachable ones answer `{ outcome: 'not_cloud' }` and
89
- the tool raises its classic no-key error unchanged), the hub credential
90
- is read from the `HUB_TOOL` profile, `exchange` runs with one
91
- re-exchange retry on a dead 120 s JWT (missing/rejected hub login →
92
- guided `antasphere login` `CliUsageError`), and the minted key is cached
93
- per (tool, hub org) with first-contact baseUrl pinning — never onto a
94
- profile that names another instance. `notify` receives the human
95
- "Connected to …" notice (wire your stderr); `messages` overrides the
96
- branded copy (defaults reproduce the Slideless strings modulo tool
97
- name). Success: `{ outcome: 'connected', key, result, cached }`.
98
- - `getActiveWorkspace / setActiveWorkspace / resolveTargetWorkspace (env)` —
99
- hub-profile org context: what `antasphere org use` reads/writes
100
- (`activeWorkspaceId` on the hub profile), and the exchange-target order
101
- `--org` override → active org → the hub key's bound workspace.
189
+ federated tool): `toolToken(hubApiKey, { resource })` asks the hub for a
190
+ 120 s USER-scoped JWT plus the tool's one-time offline grant
191
+ (`hubRefreshToken`; the exchange names NO org — hub ADR 014),
192
+ `cliConnect(token, hubRefreshToken?)` trades the pair at the tool for a
193
+ user-scoped tool-local key (no auth header — the JWT is the credential; the
194
+ grant is relayed so a fresh user's connect is never refused
195
+ `hub_grant_missing`), `exchange(hubApiKey, body)` composes both; the hub key
196
+ never reaches the tool, the grant never reaches anything but the tool, and
197
+ neither is ever persisted here. Errors are `CliConnectError` (a
198
+ `CliAuthError`) tagged with the failing `stage` plus four remedies:
199
+ `needsHubLogin` (hub 401, or 403 `insufficient_scope` from a
200
+ pre-`sso:exchange` key), `needsReExchange` (tool 401 on an expired/replayed
201
+ JWT), `refusal` (a 403 that is not the scope case, from either side — the
202
+ account was refused, not the key) and `exitCode` (3 for a refusal, else 1).
203
+ - `lookupConnectKey / storeConnectKey / removeConnectKey / profileCredential` —
204
+ the cache of exchange-minted user-scoped keys, held on the profile of the
205
+ instance they were minted for, in its `connectKeys` map (slot = the hub
206
+ profile name; `activeHubProfile(env)` resolves it). ONE key per hub account
207
+ serves every org — the org is never part of the credential or the slot.
208
+ `storeConnectKey(env, tool, profileName, hubProfileName, entry, { baseUrl })`
209
+ writes `baseUrl` only on a profile that has none, a blank one counting as
210
+ none (a profile is never repointed), creates a profile not on disk yet, and makes it active only when
211
+ the tool had no profile at all. `removeConnectKey` never touches the tool's
212
+ own `apiKey`. `profileCredential(profile, hubProfileName?)` answers `key` /
213
+ `hub` / `none`.
214
+ - `connectOnDemand({ tool, toolBaseUrl, cloudUrl?, profileName, env, resource?,
215
+ fetch?, notify?, messages?, cached? })` — the seam a tool CLI runs when NO
216
+ direct credential resolved. `profileName` is REQUIRED: the profile
217
+ `selectProfile` chose; `cloudUrl` is what a profile with no `baseUrl` is at,
218
+ the same value the selection got. The cache is read first, on that profile
219
+ only when it is at this instance (a hit costs zero network); `cached: false`
220
+ skips it. On a miss the
221
+ anonymous `GET /api/v1/instance` probe gates the flow (a self-hosted or
222
+ unreachable instance answers `{ outcome: 'not_cloud' }` and the tool raises
223
+ its classic no-key error), the hub credential is read from the `HUB_TOOL`
224
+ profile, `exchange` runs with one re-exchange retry on a dead JWT (a missing
225
+ or rejected hub login → a guided `antasphere login` `CliUsageError`, on
226
+ either attempt; a refusal propagates as its `CliConnectError`), and the key
227
+ is stored on the selected profile — never on one that is at another
228
+ instance. Success:
229
+ `{ outcome: 'connected', key, cached, result?, stored? }` — `cached: true` =
230
+ served from the cache. `notify` receives the "Connected to …" notice on fresh
231
+ exchanges (cache hits are silent); `messages` overrides the copy.
232
+ `probeToolInstance(toolBaseUrl, fetch?)` is the discovery probe on its own;
233
+ `activeHubProfile(env)` is the active hub profile, whose name is the cache
234
+ slot.
235
+ - `refreshConnectKey(opts)` — the stranded-key recovery, same options as
236
+ `connectOnDemand`. When a request answers 401 with a key served
237
+ `cached: true`, the chassis calls it once: it evicts that key (only from a
238
+ profile at this instance; a key of another instance is never touched) and
239
+ exchanges again with the cache skipped. The chassis retries its request once
240
+ with the new key, and a second 401 fails; `not_cloud` means nothing to
241
+ recover with.
102
242
  - `migrateLegacyConfig(env, { tool, legacyDir })` — one-shot, non-destructive
103
243
  import of a tool's pre-cli-core `$XDG_CONFIG_HOME/<legacyDir>/config.json`
104
244
  into the shared home. Imports only when the tool has no profiles in the
105
245
  shared home yet and the legacy file holds at least one; the legacy file is
106
246
  left in place.
107
247
 
108
- ## Consuming it
109
-
110
- Until the package is on npm, pin the git tag (pnpm resolves the `github:`
111
- shorthand over SSH — the repo is private):
248
+ ## Exit codes
112
249
 
113
- ```json
114
- "dependencies": {
115
- "@antasphere/cli-core": "github:antasphere/cli-core#v0.1.0"
116
- }
117
- ```
250
+ - `1` — a usage or wire error.
251
+ - `3` — a refusal: a 403 at the exchange that is not the scope case; the
252
+ message is the server's own sentence, printed as is.
118
253
 
119
- The `prepare` script builds `dist/` on install, so git installs work from a
120
- scratch clone — but pnpm ≥10 blocks dependency build scripts by default, and
121
- for a git dependency it honors the allowlist **only as the exact resolved
122
- spec** (no glob patterns, no bare package name — proven against an empty
123
- store: a bare-name or missing entry fails with
124
- `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`). We standardize the entry in
125
- `pnpm-workspace.yaml`. Each consumer therefore carries:
126
-
127
- ```yaml
128
- # pnpm-workspace.yaml
129
- onlyBuiltDependencies:
130
- - esbuild
131
- - '@antasphere/cli-core@git+ssh://git@github.com/antasphere/cli-core.git#<commit-sha-of-the-tag>'
132
- ```
254
+ Runners read `exitCode` off `CliUsageError` and `CliConnectError`.
133
255
 
134
- Bumping the pinned tag: update the dependency, run `pnpm install`, and paste
135
- the new allowlist line pnpm prints in its error. At npm-publish time the
136
- switch is one line in each consumer:
256
+ ## Consuming it
137
257
 
138
258
  ```json
139
- "@antasphere/cli-core": "^0.1.0"
259
+ "dependencies": { "@antasphere/cli-core": "^0.5.0" }
140
260
  ```
141
261
 
142
- plus dropping the consumer's allowlist entry (registry tarballs ship a
143
- prebuilt `dist/`, no build script runs).
262
+ Registry tarballs ship a prebuilt `dist/`, no build script runs on install.
144
263
 
145
264
  ## Release
146
265
 
@@ -151,21 +270,11 @@ with no npm token — npm authenticates the workflow's OIDC identity directly.
151
270
 
152
271
  ```bash
153
272
  # bump package.json#version, commit, then:
154
- git tag v0.3.0 && git push origin v0.3.0
155
- ```
156
-
157
- **First publish is manual.** Trusted publishing can only be configured on a
158
- package that already exists on npm, so the very first publish of the name is
159
- done by hand from a clean checkout while logged in to an account that owns
160
- the npm `antasphere` org:
161
-
162
- ```bash
163
- pnpm install # runs prepare → builds dist/
164
- npm publish # publishConfig.access = public
273
+ git tag v0.5.0 && git push origin v0.5.0
165
274
  ```
166
275
 
167
- After that, configure the trusted publisher on npmjs.com
168
- (package → Settings → Trusted Publisher → GitHub Actions) with exactly:
276
+ The first publish was done by hand, and the trusted publisher is configured on
277
+ npmjs.com; every release is the tag.
169
278
 
170
279
  | Field | Value |
171
280
  | -------------------- | --------------- |
@@ -174,8 +283,6 @@ After that, configure the trusted publisher on npmjs.com
174
283
  | Workflow filename | `publish.yml` |
175
284
  | Environment | _(leave empty)_ |
176
285
 
177
- and every further release is just the tag.
178
-
179
286
  ## Development
180
287
 
181
288
  ```bash
@@ -185,4 +292,4 @@ pnpm lint && pnpm typecheck && pnpm test && pnpm build
185
292
 
186
293
  Branches per the workspace rule: `prod` (default, releases/tags) + `dev`
187
294
  (day-to-day work). License: MIT (permissive on purpose — this is plumbing
188
- consumed by public CLIs; confirm before the first npm publish).
295
+ consumed by public CLIs).
package/dist/config.d.ts CHANGED
@@ -13,9 +13,16 @@
13
13
  * └── <tool>.json 0600 every other tool's profiles
14
14
  * (e.g. tools/slideless.json)
15
15
  *
16
- * Each file holds one tool's `{ activeProfile?, profiles }`: multiple named
17
- * profiles per tool, one active profile per tool. Hub profiles additionally
18
- * carry the active org (`workspaceId`) their credential is bound to.
16
+ * Each file holds one tool's `{ activeProfile?, profiles }`. A PROFILE IS AN
17
+ * INSTANCE: it names the tool instance it talks to (`baseUrl`; absent = the
18
+ * tool's cloud URL) and carries the credentials for that instance, either
19
+ * the tool's own key (`apiKey`, the tool's login) or the keys the hub
20
+ * exchange minted (`connectKeys`, one per hub profile). Profiles are named
21
+ * for their host by default (`cloud` for the cloud instance,
22
+ * `share.antasphere.com`, `localhost:3400`); the person may rename them and
23
+ * chooses the active one with `<tool> use`. The hub profile additionally
24
+ * carries `workspaceId`, the default organization at login (informational;
25
+ * the hub's server-side default is what applies).
19
26
  *
20
27
  * Files store API keys, so directories are created 0700 and files kept 0600
21
28
  * on every write. All I/O is synchronous and env-injected (no process.env
@@ -25,34 +32,36 @@
25
32
  export type CliEnv = Record<string, string | undefined>;
26
33
  /**
27
34
  * A tool-local key minted by the cross-tool connect exchange (binding
28
- * patterns §7), cached in the tool's profile under the HUB org it was
29
- * exchanged for. Every field beyond the key itself is informational.
35
+ * patterns §7), cached on the profile of the instance it was minted for,
36
+ * under the HUB PROFILE whose `antasphere login` credential minted it. The
37
+ * key is USER-scoped (hub ADR 014): one key serves every org — the org is a
38
+ * per-request parameter, never part of the credential or of this cache's
39
+ * slot. Every field beyond the key itself is informational.
30
40
  */
31
- export interface CliWorkspaceKey {
41
+ export interface CliConnectKey {
32
42
  /** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
33
43
  apiKey: string;
34
- /** The tool-local workspace the key is bound to (the hub org's projection). */
35
- toolWorkspaceId?: string;
36
44
  /** The signed-in account's email at mint. */
37
45
  email?: string;
38
46
  /** ISO timestamp of the exchange that minted the key. */
39
47
  mintedAt?: string;
40
48
  }
41
49
  export interface CliProfile {
42
- /** `<prefix>_...` API key for this profile. */
50
+ /** `<prefix>_...` API key for this profile (the tool's own login). */
43
51
  apiKey?: string;
44
- /** Instance origin; absent = the tool's default instance, when it has one. */
52
+ /** The instance this profile IS: its origin. Absent = the tool's cloud URL. */
45
53
  baseUrl?: string;
46
54
  /** The signed-in account's email (informational, set by login). */
47
55
  email?: string;
48
- /** The org/workspace the key is bound to (one workspace per credential). */
56
+ /** Hub profile: the default organization at login (informational; the
57
+ * hub's server-side default is what applies, `--org` overrides per
58
+ * command). Tool profiles from a tool's own login may carry the
59
+ * workspace that login bound. */
49
60
  workspaceId?: string;
50
- /** Hub profiles only: the org selected via `antasphere org use` (overrides
51
- * the key's bound `workspaceId` as the default exchange target). */
52
- activeWorkspaceId?: string;
53
- /** Tool profiles only: exchange-minted keys, one per HUB org — the
54
- * per-(tool, org) cache of the cross-tool connect flow. */
55
- workspaceKeys?: Record<string, CliWorkspaceKey>;
61
+ /** Tool profiles only: exchange-minted USER-scoped keys, ONE per hub
62
+ * profile — the (tool profile, hub profile) cache of the cross-tool
63
+ * connect flow. Org-independent: a cached key serves every org. */
64
+ connectKeys?: Record<string, CliConnectKey>;
56
65
  }
57
66
  export interface CliConfig {
58
67
  activeProfile?: string;
@@ -75,23 +84,44 @@ export declare function clearConfig(env: CliEnv, tool: string): void;
75
84
  /** `ant_abcd1234_…` → `ant_abcd1234_…c3f9` — safe for terminals and logs. */
76
85
  export declare function redactKey(key: string): string;
77
86
  /**
78
- * The cached tool-local key for a HUB org, if any. Strictly org-matched:
79
- * the per-org cache first, then the legacy single `apiKey` — but ONLY when
80
- * the profile's `workspaceId` says it is bound to exactly this workspace.
81
- * A key whose binding is unknown is never returned for an org lookup.
87
+ * The cached user-scoped connect key a HUB PROFILE minted for this tool
88
+ * profile, if any. The slot is the hub profile NAME — stable across runs
89
+ * and orgs (the key identifies the user; selecting a different org between
90
+ * runs never misses the cache).
82
91
  */
83
- export declare function lookupWorkspaceKey(profile: CliProfile | undefined, workspaceId: string): string | undefined;
92
+ export declare function lookupConnectKey(profile: CliProfile | undefined, hubProfileName: string): string | undefined;
93
+ /** How a profile signs in, for a `profiles`-style listing. */
94
+ export type ProfileCredential = 'key' | 'hub' | 'none';
84
95
  /**
85
- * Cache an exchange-minted tool key under (profile, hub org), creating the
86
- * profile on first contact (and making it active when no profile is active
87
- * yet — first cloud connect on a fresh machine). Every other profile field
88
- * is preserved untouched. Returns the file written.
96
+ * What a profile signs in with, in the order the tool resolves it: its own
97
+ * `apiKey` (`key`, the tool's login), else a key the hub exchange minted
98
+ * (`hub`: under the named hub profile, or under any when none is named),
99
+ * else `none`. A listing can then say which profiles ride the hub login.
89
100
  */
90
- export declare function storeWorkspaceKey(env: CliEnv, tool: string, profileName: string, workspaceId: string, entry: CliWorkspaceKey): string;
101
+ export declare function profileCredential(profile: CliProfile | undefined, hubProfileName?: string): ProfileCredential;
102
+ export interface StoreConnectKeyOptions {
103
+ /**
104
+ * The instance the key was minted for. Written on the profile when it is
105
+ * created here or has no `baseUrl` of its own (a blank one counts as
106
+ * none); a `baseUrl` that is there is never overwritten (a profile is
107
+ * never repointed).
108
+ */
109
+ baseUrl?: string;
110
+ }
111
+ /**
112
+ * Cache an exchange-minted tool key on the SELECTED profile under the hub
113
+ * profile's name. A profile not on disk yet is created (with `baseUrl` when
114
+ * given) and made active only when the tool had NO profile at all: the
115
+ * first cloud connect on a fresh machine. An existing profile is never
116
+ * renamed, repointed or made active; every other field is preserved.
117
+ * Returns the file written.
118
+ */
119
+ export declare function storeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string, entry: CliConnectKey, opts?: StoreConnectKeyOptions): string;
91
120
  /**
92
- * Drop one org's cached key from a profile (tool logout for that org).
93
- * Returns whether anything was removed; the legacy single `apiKey` is never
94
- * touched — it belongs to the tool's own login flow.
121
+ * Drop one hub profile's cached connect key from a profile (disconnect this
122
+ * tool from the hub account, or evict a key the hub revoked). Returns
123
+ * whether anything was removed; the tool's own `apiKey` is never touched —
124
+ * it belongs to the tool's own login flow.
95
125
  */
96
- export declare function removeWorkspaceKey(env: CliEnv, tool: string, profileName: string, workspaceId: string): boolean;
126
+ export declare function removeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string): boolean;
97
127
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAExD;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAC;IACf,+EAA+E;IAC/E,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,6CAA6C;IAC7C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,yDAAyD;IACzD,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,UAAU;IACzB,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mEAAmE;IACnE,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;yEACqE;IACrE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;gEAC4D;IAC5D,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;CACjD;AAED,MAAM,WAAW,SAAS;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACtC;AAED,+EAA+E;AAC/E,eAAO,MAAM,QAAQ,QAAQ,CAAC;AAI9B,oEAAoE;AACpE,wBAAgB,mBAAm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GAAG,MAAM,CAiB/E;AAED,qEAAqE;AACrE,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAG3D;AAED,6EAA6E;AAC7E,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG7C;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,UAAU,GAAG,SAAS,EAC/B,cAAc,EAAE,MAAM,GACrB,MAAM,GAAG,SAAS,CAEpB;AAED,8DAA8D;AAC9D,MAAM,MAAM,iBAAiB,GAAG,KAAK,GAAG,KAAK,GAAG,MAAM,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,OAAO,EAAE,UAAU,GAAG,SAAS,EAC/B,cAAc,CAAC,EAAE,MAAM,GACtB,iBAAiB,CAQnB;AAED,MAAM,WAAW,sBAAsB;IACrC;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,EACtB,KAAK,EAAE,aAAa,EACpB,IAAI,GAAE,sBAA2B,GAChC,MAAM,CAWR;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,cAAc,EAAE,MAAM,GACrB,OAAO,CAaT"}
package/dist/config.js CHANGED
@@ -76,55 +76,70 @@ export function redactKey(key) {
76
76
  return `${key.slice(0, 13)}…${key.slice(-4)}`;
77
77
  }
78
78
  /**
79
- * The cached tool-local key for a HUB org, if any. Strictly org-matched:
80
- * the per-org cache first, then the legacy single `apiKey` — but ONLY when
81
- * the profile's `workspaceId` says it is bound to exactly this workspace.
82
- * A key whose binding is unknown is never returned for an org lookup.
79
+ * The cached user-scoped connect key a HUB PROFILE minted for this tool
80
+ * profile, if any. The slot is the hub profile NAME — stable across runs
81
+ * and orgs (the key identifies the user; selecting a different org between
82
+ * runs never misses the cache).
83
83
  */
84
- export function lookupWorkspaceKey(profile, workspaceId) {
84
+ export function lookupConnectKey(profile, hubProfileName) {
85
+ return profile?.connectKeys?.[hubProfileName]?.apiKey;
86
+ }
87
+ /**
88
+ * What a profile signs in with, in the order the tool resolves it: its own
89
+ * `apiKey` (`key`, the tool's login), else a key the hub exchange minted
90
+ * (`hub`: under the named hub profile, or under any when none is named),
91
+ * else `none`. A listing can then say which profiles ride the hub login.
92
+ */
93
+ export function profileCredential(profile, hubProfileName) {
85
94
  if (!profile)
86
- return undefined;
87
- const entry = profile.workspaceKeys?.[workspaceId];
88
- if (entry?.apiKey)
89
- return entry.apiKey;
90
- if (profile.apiKey && profile.workspaceId === workspaceId)
91
- return profile.apiKey;
92
- return undefined;
95
+ return 'none';
96
+ if (profile.apiKey)
97
+ return 'key';
98
+ const slots = profile.connectKeys ?? {};
99
+ const connected = hubProfileName
100
+ ? Boolean(slots[hubProfileName]?.apiKey)
101
+ : Object.values(slots).some((k) => k.apiKey);
102
+ return connected ? 'hub' : 'none';
93
103
  }
94
104
  /**
95
- * Cache an exchange-minted tool key under (profile, hub org), creating the
96
- * profile on first contact (and making it active when no profile is active
97
- * yet — first cloud connect on a fresh machine). Every other profile field
98
- * is preserved untouched. Returns the file written.
105
+ * Cache an exchange-minted tool key on the SELECTED profile under the hub
106
+ * profile's name. A profile not on disk yet is created (with `baseUrl` when
107
+ * given) and made active only when the tool had NO profile at all: the
108
+ * first cloud connect on a fresh machine. An existing profile is never
109
+ * renamed, repointed or made active; every other field is preserved.
110
+ * Returns the file written.
99
111
  */
100
- export function storeWorkspaceKey(env, tool, profileName, workspaceId, entry) {
112
+ export function storeConnectKey(env, tool, profileName, hubProfileName, entry, opts = {}) {
101
113
  const config = loadConfig(env, tool);
114
+ const first = Object.keys(config.profiles).length === 0;
102
115
  const profile = config.profiles[profileName] ?? {};
103
116
  config.profiles[profileName] = {
104
117
  ...profile,
105
- workspaceKeys: { ...profile.workspaceKeys, [workspaceId]: entry }
118
+ ...(!profile.baseUrl?.trim() && opts.baseUrl ? { baseUrl: opts.baseUrl } : {}),
119
+ connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
106
120
  };
107
- if (!config.activeProfile)
121
+ if (first)
108
122
  config.activeProfile = profileName;
109
123
  return saveConfig(env, tool, config);
110
124
  }
111
125
  /**
112
- * Drop one org's cached key from a profile (tool logout for that org).
113
- * Returns whether anything was removed; the legacy single `apiKey` is never
114
- * touched — it belongs to the tool's own login flow.
126
+ * Drop one hub profile's cached connect key from a profile (disconnect this
127
+ * tool from the hub account, or evict a key the hub revoked). Returns
128
+ * whether anything was removed; the tool's own `apiKey` is never touched —
129
+ * it belongs to the tool's own login flow.
115
130
  */
116
- export function removeWorkspaceKey(env, tool, profileName, workspaceId) {
131
+ export function removeConnectKey(env, tool, profileName, hubProfileName) {
117
132
  const config = loadConfig(env, tool);
118
133
  const profile = config.profiles[profileName];
119
- if (!profile?.workspaceKeys || !(workspaceId in profile.workspaceKeys))
134
+ if (!profile?.connectKeys || !(hubProfileName in profile.connectKeys))
120
135
  return false;
121
- const { [workspaceId]: _dropped, ...rest } = profile.workspaceKeys;
136
+ const { [hubProfileName]: _dropped, ...rest } = profile.connectKeys;
122
137
  if (Object.keys(rest).length === 0) {
123
- const { workspaceKeys: _empty, ...bare } = profile;
138
+ const { connectKeys: _empty, ...bare } = profile;
124
139
  config.profiles[profileName] = bare;
125
140
  }
126
141
  else {
127
- config.profiles[profileName] = { ...profile, workspaceKeys: rest };
142
+ config.profiles[profileName] = { ...profile, connectKeys: rest };
128
143
  }
129
144
  saveConfig(env, tool, config);
130
145
  return true;