@antasphere/cli-core 0.4.0 → 0.6.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,118 +88,189 @@ 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
189
  federated tool): `toolToken(hubApiKey, { resource })` asks the hub for a
71
190
  120 s USER-scoped JWT plus the tool's one-time offline grant
72
191
  (`hubRefreshToken`; the exchange names NO org — hub ADR 014),
73
192
  `cliConnect(token, hubRefreshToken?)` trades the pair at the tool for a
74
- user-scoped tool-local key (no auth header — the JWT is the credential;
75
- the grant is relayed so a fresh user's connect is never refused
76
- `hub_grant_missing`), `exchange(hubApiKey, body)` composes both; the hub
77
- key never reaches the tool, the grant never reaches anything but the
78
- tool, and neither is ever persisted here. Errors are `CliConnectError`
79
- (a `CliAuthError`) tagged with the failing `stage` plus the two
80
- remedies: `needsHubLogin` (hub 401, or 403 `insufficient_scope` from a
81
- pre-`sso:exchange` key) and `needsReExchange` (tool 401 on an
82
- expired/replayed JWT).
83
- - `lookupConnectKey / storeConnectKey / removeConnectKey` — the
84
- per-(tool, HUB PROFILE) cache of exchange-minted user-scoped keys, held
85
- in the tool profile's additive `connectKeys` map (slot = the hub profile
86
- name; `activeHubProfile(env)` resolves it). ONE key per hub account
87
- serves every org — the org is a per-request parameter, never part of the
88
- credential or the cache slot. The legacy single `apiKey` belongs to the
89
- tool's own login flow and is never consulted here.
90
- - `connectOnDemand({ tool, toolBaseUrl, resource?, profileName?, env,
91
- fetch?, notify?, messages? })` + `probeToolInstance(toolBaseUrl, fetch?)`
92
- — the connect-on-demand seam a tool CLI runs when NO direct credential
93
- resolved (extracted from the Slideless CLI): the cache is consulted
94
- FIRST (a hit costs zero network — no probe, no hub, no fresh mint), then
95
- the anonymous `GET /instance` probe gates the flow (only an
96
- Antasphere-cloud instance takes it — self-hosted/unreachable ones answer
97
- `{ outcome: 'not_cloud' }` and the tool raises its classic no-key error
98
- unchanged), the hub credential is read from the `HUB_TOOL` profile,
99
- `exchange` runs with one re-exchange retry on a dead 120 s JWT
100
- (missing/rejected hub login → guided `antasphere login`
101
- `CliUsageError`), and the minted key is cached per (tool, hub profile)
102
- with first-contact baseUrl pinning — never onto a profile that names
103
- another instance. `notify` receives the human "Connected to …" notice on
104
- fresh mints (wire your stderr; cache hits are silent); `messages`
105
- overrides the branded copy (defaults reproduce the Slideless strings
106
- modulo tool name). Success: `{ outcome: 'connected', key, cached,
107
- result?, stored? }` — `cached: true` = served from the cache.
108
- - `getActiveWorkspace / setActiveWorkspace / resolveTargetWorkspace (env)` —
109
- hub-profile org context: what `antasphere org use` reads/writes
110
- (`activeWorkspaceId` on the hub profile), resolved override → active org
111
- → the hub key's bound workspace. Org context is a per-request selection
112
- input for tool calls — it never targets the exchange and never keys the
113
- connect cache.
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; `selectHubProfile(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`. `cachedConnectKey(profile, hubProfileName, hubEmail)` is the
214
+ slot read the seam serves from: the key only when both e-mails are there and
215
+ match.
216
+ - `connectOnDemand({ tool, toolBaseUrl, cloudUrl?, hubCloudUrl?, profileName, env,
217
+ resource?, fetch?, notify?, messages?, cached? })` — the seam a tool CLI runs when NO
218
+ direct credential resolved. `profileName` is REQUIRED: the profile
219
+ `selectProfile` chose; `cloudUrl` is what a profile with no `baseUrl` is at,
220
+ the same value the selection got. The cache is read first, on that profile
221
+ only when it is at this instance (a hit costs zero network); `cached: false`
222
+ skips it. On a miss the
223
+ anonymous `GET /api/v1/instance` probe gates the flow (a self-hosted or
224
+ unreachable instance answers `{ outcome: 'not_cloud' }` and the tool raises
225
+ its classic no-key error), the hub credential is read from the `HUB_TOOL`
226
+ profile, `exchange` runs with one re-exchange retry on a dead JWT (a missing
227
+ or rejected hub login → a guided `antasphere login` `CliUsageError`, on
228
+ either attempt; a refusal propagates as its `CliConnectError`), and the key
229
+ is stored on the selected profile — never on one that is at another
230
+ instance. The hub profile the exchange uses is the one `ANTASPHERE_URL`
231
+ names (the profile at that instance, the way `antasphere login` picks it;
232
+ `hubCloudUrl` lets that URL name the `cloud` slot, places a hub profile with no
233
+ `baseUrl` at the hub and is the hub URL its exchange goes to; a profile's own
234
+ `baseUrl` always wins), else the active one, and
235
+ none when no hub profile is active (the account CLI would then take its cloud
236
+ profile; a tool CLI asks for `antasphere login`). A
237
+ cached tool key is served only when the hub profile's e-mail and the cached
238
+ key's e-mail match, and never when either is missing. Success:
239
+ `{ outcome: 'connected', key, cached, result?, stored? }` — `cached: true` =
240
+ served from the cache. `notify` receives the "Connected to …" notice on fresh
241
+ exchanges (cache hits are silent); `messages` overrides the copy.
242
+ `probeToolInstance(toolBaseUrl, fetch?)` is the discovery probe on its own;
243
+ `selectHubProfile(env, { cloudUrl? })` is that hub profile, whose name is the
244
+ cache slot (`HUB_URL_ENV` is `ANTASPHERE_URL`; `activeHubProfile(env)` is
245
+ kept, deprecated, as `selectHubProfile(env)`).
246
+ - `refreshConnectKey(opts)` — the stranded-key recovery, same options as
247
+ `connectOnDemand`. When a request answers 401 with a key served
248
+ `cached: true`, the chassis calls it once: it evicts that key (only from a
249
+ profile at this instance; a key of another instance is never touched) and
250
+ exchanges again with the cache skipped. The chassis retries its request once
251
+ with the new key, and a second 401 fails; `not_cloud` means nothing to
252
+ recover with.
114
253
  - `migrateLegacyConfig(env, { tool, legacyDir })` — one-shot, non-destructive
115
254
  import of a tool's pre-cli-core `$XDG_CONFIG_HOME/<legacyDir>/config.json`
116
255
  into the shared home. Imports only when the tool has no profiles in the
117
256
  shared home yet and the legacy file holds at least one; the legacy file is
118
257
  left in place.
119
258
 
120
- ## Consuming it
121
-
122
- Until the package is on npm, pin the git tag (pnpm resolves the `github:`
123
- shorthand over SSH — the repo is private):
259
+ ## Exit codes
124
260
 
125
- ```json
126
- "dependencies": {
127
- "@antasphere/cli-core": "github:antasphere/cli-core#v0.1.0"
128
- }
129
- ```
261
+ - `1` — a usage or wire error.
262
+ - `3` — a refusal: a 403 at the exchange that is not the scope case; the
263
+ message is the server's own sentence, printed as is.
130
264
 
131
- The `prepare` script builds `dist/` on install, so git installs work from a
132
- scratch clone — but pnpm ≥10 blocks dependency build scripts by default, and
133
- for a git dependency it honors the allowlist **only as the exact resolved
134
- spec** (no glob patterns, no bare package name — proven against an empty
135
- store: a bare-name or missing entry fails with
136
- `ERR_PNPM_GIT_DEP_PREPARE_NOT_ALLOWED`). We standardize the entry in
137
- `pnpm-workspace.yaml`. Each consumer therefore carries:
138
-
139
- ```yaml
140
- # pnpm-workspace.yaml
141
- onlyBuiltDependencies:
142
- - esbuild
143
- - '@antasphere/cli-core@git+ssh://git@github.com/antasphere/cli-core.git#<commit-sha-of-the-tag>'
144
- ```
265
+ Runners read `exitCode` off `CliUsageError` and `CliConnectError`.
145
266
 
146
- Bumping the pinned tag: update the dependency, run `pnpm install`, and paste
147
- the new allowlist line pnpm prints in its error. At npm-publish time the
148
- switch is one line in each consumer:
267
+ ## Consuming it
149
268
 
150
269
  ```json
151
- "@antasphere/cli-core": "^0.1.0"
270
+ "dependencies": { "@antasphere/cli-core": "^0.6.0" }
152
271
  ```
153
272
 
154
- plus dropping the consumer's allowlist entry (registry tarballs ship a
155
- prebuilt `dist/`, no build script runs).
273
+ Registry tarballs ship a prebuilt `dist/`, no build script runs on install.
156
274
 
157
275
  ## Release
158
276
 
@@ -163,21 +281,11 @@ with no npm token — npm authenticates the workflow's OIDC identity directly.
163
281
 
164
282
  ```bash
165
283
  # bump package.json#version, commit, then:
166
- git tag v0.3.0 && git push origin v0.3.0
167
- ```
168
-
169
- **First publish is manual.** Trusted publishing can only be configured on a
170
- package that already exists on npm, so the very first publish of the name is
171
- done by hand from a clean checkout while logged in to an account that owns
172
- the npm `antasphere` org:
173
-
174
- ```bash
175
- pnpm install # runs prepare → builds dist/
176
- npm publish # publishConfig.access = public
284
+ git tag v0.6.0 && git push origin v0.6.0
177
285
  ```
178
286
 
179
- After that, configure the trusted publisher on npmjs.com
180
- (package → Settings → Trusted Publisher → GitHub Actions) with exactly:
287
+ The first publish was done by hand, and the trusted publisher is configured on
288
+ npmjs.com; every release is the tag.
181
289
 
182
290
  | Field | Value |
183
291
  | -------------------- | --------------- |
@@ -186,8 +294,6 @@ After that, configure the trusted publisher on npmjs.com
186
294
  | Workflow filename | `publish.yml` |
187
295
  | Environment | _(leave empty)_ |
188
296
 
189
- and every further release is just the tag.
190
-
191
297
  ## Development
192
298
 
193
299
  ```bash
@@ -197,4 +303,4 @@ pnpm lint && pnpm typecheck && pnpm test && pnpm build
197
303
 
198
304
  Branches per the workspace rule: `prod` (default, releases/tags) + `dev`
199
305
  (day-to-day work). License: MIT (permissive on purpose — this is plumbing
200
- consumed by public CLIs; confirm before the first npm publish).
306
+ 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,11 +32,11 @@
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 PROFILE whose
29
- * `antasphere login` credential minted it. The key is USER-scoped (hub
30
- * ADR 014): one key serves every org — the org is a per-request parameter,
31
- * never part of the credential or of this cache's slot. Every field beyond
32
- * 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.
33
40
  */
34
41
  export interface CliConnectKey {
35
42
  /** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
@@ -40,20 +47,20 @@ export interface CliConnectKey {
40
47
  mintedAt?: string;
41
48
  }
42
49
  export interface CliProfile {
43
- /** `<prefix>_...` API key for this profile. */
50
+ /** `<prefix>_...` API key for this profile (the tool's own login). */
44
51
  apiKey?: string;
45
- /** 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. */
46
53
  baseUrl?: string;
47
54
  /** The signed-in account's email (informational, set by login). */
48
55
  email?: string;
49
- /** 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. */
50
60
  workspaceId?: string;
51
- /** Hub profiles only: the org selected via `antasphere org use` (the
52
- * default per-request org context; never part of any credential). */
53
- activeWorkspaceId?: string;
54
61
  /** Tool profiles only: exchange-minted USER-scoped keys, ONE per hub
55
- * profile — the (tool, hub profile) cache of the cross-tool connect
56
- * flow. Org-independent: a cached key serves every org the user selects. */
62
+ * profile — the (tool profile, hub profile) cache of the cross-tool
63
+ * connect flow. Org-independent: a cached key serves every org. */
57
64
  connectKeys?: Record<string, CliConnectKey>;
58
65
  }
59
66
  export interface CliConfig {
@@ -84,16 +91,48 @@ export declare function redactKey(key: string): string;
84
91
  */
85
92
  export declare function lookupConnectKey(profile: CliProfile | undefined, hubProfileName: string): string | undefined;
86
93
  /**
87
- * Cache an exchange-minted tool key under (profile, hub profile), creating
88
- * the profile on first contact (and making it active when no profile is
89
- * active yet — first cloud connect on a fresh machine). Every other profile
90
- * field is preserved untouched. Returns the file written.
94
+ * The cached connect key the connect-on-demand flow may SERVE: the slot's
95
+ * key, only when the hub profile carries an e-mail address, the cached
96
+ * entry carries one too, and the two are the same address (trimmed,
97
+ * case-insensitive). A key is the credential of the person who minted it
98
+ * (PRDCT-3032), so the cache serves only that person; a hub profile with no
99
+ * address is someone unknown, and an entry with no address was minted by
100
+ * someone unknown: neither is served (PRDCT-3046 F7). `lookupConnectKey`
101
+ * stays the plain slot read.
91
102
  */
92
- export declare function storeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string, entry: CliConnectKey): string;
103
+ export declare function cachedConnectKey(profile: CliProfile | undefined, hubProfileName: string, hubEmail: string | undefined): string | undefined;
104
+ /** How a profile signs in, for a `profiles`-style listing. */
105
+ export type ProfileCredential = 'key' | 'hub' | 'none';
93
106
  /**
94
- * Drop one hub profile's cached connect key (disconnect this tool from the
95
- * hub account). Returns whether anything was removed; the legacy single
96
- * `apiKey` is never touched — it belongs to the tool's own login flow.
107
+ * What a profile signs in with, in the order the tool resolves it: its own
108
+ * `apiKey` (`key`, the tool's login), else a key the hub exchange minted
109
+ * (`hub`: under the named hub profile, or under any when none is named),
110
+ * else `none`. A listing can then say which profiles ride the hub login.
111
+ */
112
+ export declare function profileCredential(profile: CliProfile | undefined, hubProfileName?: string): ProfileCredential;
113
+ export interface StoreConnectKeyOptions {
114
+ /**
115
+ * The instance the key was minted for. Written on the profile when it is
116
+ * created here or has no `baseUrl` of its own (a blank one counts as
117
+ * none); a `baseUrl` that is there is never overwritten (a profile is
118
+ * never repointed).
119
+ */
120
+ baseUrl?: string;
121
+ }
122
+ /**
123
+ * Cache an exchange-minted tool key on the SELECTED profile under the hub
124
+ * profile's name. A profile not on disk yet is created (with `baseUrl` when
125
+ * given) and made active only when the tool had NO profile at all: the
126
+ * first cloud connect on a fresh machine. An existing profile is never
127
+ * renamed, repointed or made active; every other field is preserved.
128
+ * Returns the file written.
129
+ */
130
+ export declare function storeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string, entry: CliConnectKey, opts?: StoreConnectKeyOptions): string;
131
+ /**
132
+ * Drop one hub profile's cached connect key from a profile (disconnect this
133
+ * tool from the hub account, or evict a key the hub revoked). Returns
134
+ * whether anything was removed; the tool's own `apiKey` is never touched —
135
+ * it belongs to the tool's own login flow.
97
136
  */
98
137
  export declare function removeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string): boolean;
99
138
  //# 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;;;;;;;GAOG;AACH,MAAM,WAAW,aAAa;IAC5B,gFAAgF;IAChF,MAAM,EAAE,MAAM,CAAC;IACf,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;I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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
@@ -85,26 +85,66 @@ export function lookupConnectKey(profile, hubProfileName) {
85
85
  return profile?.connectKeys?.[hubProfileName]?.apiKey;
86
86
  }
87
87
  /**
88
- * Cache an exchange-minted tool key under (profile, hub profile), creating
89
- * the profile on first contact (and making it active when no profile is
90
- * active yet — first cloud connect on a fresh machine). Every other profile
91
- * field is preserved untouched. Returns the file written.
88
+ * The cached connect key the connect-on-demand flow may SERVE: the slot's
89
+ * key, only when the hub profile carries an e-mail address, the cached
90
+ * entry carries one too, and the two are the same address (trimmed,
91
+ * case-insensitive). A key is the credential of the person who minted it
92
+ * (PRDCT-3032), so the cache serves only that person; a hub profile with no
93
+ * address is someone unknown, and an entry with no address was minted by
94
+ * someone unknown: neither is served (PRDCT-3046 F7). `lookupConnectKey`
95
+ * stays the plain slot read.
92
96
  */
93
- export function storeConnectKey(env, tool, profileName, hubProfileName, entry) {
97
+ export function cachedConnectKey(profile, hubProfileName, hubEmail) {
98
+ const entry = profile?.connectKeys?.[hubProfileName];
99
+ const hub = hubEmail?.trim().toLowerCase();
100
+ const minted = entry?.email?.trim().toLowerCase();
101
+ if (!hub || !minted || hub !== minted)
102
+ return undefined;
103
+ return entry?.apiKey;
104
+ }
105
+ /**
106
+ * What a profile signs in with, in the order the tool resolves it: its own
107
+ * `apiKey` (`key`, the tool's login), else a key the hub exchange minted
108
+ * (`hub`: under the named hub profile, or under any when none is named),
109
+ * else `none`. A listing can then say which profiles ride the hub login.
110
+ */
111
+ export function profileCredential(profile, hubProfileName) {
112
+ if (!profile)
113
+ return 'none';
114
+ if (profile.apiKey)
115
+ return 'key';
116
+ const slots = profile.connectKeys ?? {};
117
+ const connected = hubProfileName
118
+ ? Boolean(slots[hubProfileName]?.apiKey)
119
+ : Object.values(slots).some((k) => k.apiKey);
120
+ return connected ? 'hub' : 'none';
121
+ }
122
+ /**
123
+ * Cache an exchange-minted tool key on the SELECTED profile under the hub
124
+ * profile's name. A profile not on disk yet is created (with `baseUrl` when
125
+ * given) and made active only when the tool had NO profile at all: the
126
+ * first cloud connect on a fresh machine. An existing profile is never
127
+ * renamed, repointed or made active; every other field is preserved.
128
+ * Returns the file written.
129
+ */
130
+ export function storeConnectKey(env, tool, profileName, hubProfileName, entry, opts = {}) {
94
131
  const config = loadConfig(env, tool);
132
+ const first = Object.keys(config.profiles).length === 0;
95
133
  const profile = config.profiles[profileName] ?? {};
96
134
  config.profiles[profileName] = {
97
135
  ...profile,
136
+ ...(!profile.baseUrl?.trim() && opts.baseUrl ? { baseUrl: opts.baseUrl } : {}),
98
137
  connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
99
138
  };
100
- if (!config.activeProfile)
139
+ if (first)
101
140
  config.activeProfile = profileName;
102
141
  return saveConfig(env, tool, config);
103
142
  }
104
143
  /**
105
- * Drop one hub profile's cached connect key (disconnect this tool from the
106
- * hub account). Returns whether anything was removed; the legacy single
107
- * `apiKey` is never touched — it belongs to the tool's own login flow.
144
+ * Drop one hub profile's cached connect key from a profile (disconnect this
145
+ * tool from the hub account, or evict a key the hub revoked). Returns
146
+ * whether anything was removed; the tool's own `apiKey` is never touched —
147
+ * it belongs to the tool's own login flow.
108
148
  */
109
149
  export function removeConnectKey(env, tool, profileName, hubProfileName) {
110
150
  const config = loadConfig(env, tool);