@antasphere/cli-core 0.2.0 → 0.4.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 +49 -22
- package/dist/config.d.ts +26 -24
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +20 -27
- package/dist/config.js.map +1 -1
- package/dist/connect-on-demand.d.ts +127 -0
- package/dist/connect-on-demand.d.ts.map +1 -0
- package/dist/connect-on-demand.js +149 -0
- package/dist/connect-on-demand.js.map +1 -0
- package/dist/connect.d.ts +43 -28
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +14 -8
- package/dist/connect.js.map +1 -1
- package/dist/index.d.ts +10 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -5
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +38 -37
- package/src/connect-on-demand.ts +283 -0
- package/src/connect.ts +48 -31
- package/src/index.ts +19 -8
package/README.md
CHANGED
|
@@ -67,23 +67,50 @@ expiresInDays?, workspaceId? })` (the full key appears exactly once),
|
|
|
67
67
|
fetch, interactive prompt) so tool CLIs run in-process under test.
|
|
68
68
|
- `CliConnectClient({ hubBaseUrl, toolBaseUrl })` — the cross-tool connect
|
|
69
69
|
exchange (binding patterns §7, gcloud model — one hub login serves every
|
|
70
|
-
federated tool): `toolToken(hubApiKey, { resource
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
`
|
|
74
|
-
tool
|
|
75
|
-
|
|
76
|
-
`
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
70
|
+
federated tool): `toolToken(hubApiKey, { resource })` asks the hub for a
|
|
71
|
+
120 s USER-scoped JWT plus the tool's one-time offline grant
|
|
72
|
+
(`hubRefreshToken`; the exchange names NO org — hub ADR 014),
|
|
73
|
+
`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.
|
|
83
108
|
- `getActiveWorkspace / setActiveWorkspace / resolveTargetWorkspace (env)` —
|
|
84
109
|
hub-profile org context: what `antasphere org use` reads/writes
|
|
85
|
-
(`activeWorkspaceId` on the hub profile),
|
|
86
|
-
|
|
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.
|
|
87
114
|
- `migrateLegacyConfig(env, { tool, legacyDir })` — one-shot, non-destructive
|
|
88
115
|
import of a tool's pre-cli-core `$XDG_CONFIG_HOME/<legacyDir>/config.json`
|
|
89
116
|
into the shared home. Imports only when the tool has no profiles in the
|
|
@@ -136,7 +163,7 @@ with no npm token — npm authenticates the workflow's OIDC identity directly.
|
|
|
136
163
|
|
|
137
164
|
```bash
|
|
138
165
|
# bump package.json#version, commit, then:
|
|
139
|
-
git tag v0.
|
|
166
|
+
git tag v0.3.0 && git push origin v0.3.0
|
|
140
167
|
```
|
|
141
168
|
|
|
142
169
|
**First publish is manual.** Trusted publishing can only be configured on a
|
|
@@ -152,12 +179,12 @@ npm publish # publishConfig.access = public
|
|
|
152
179
|
After that, configure the trusted publisher on npmjs.com
|
|
153
180
|
(package → Settings → Trusted Publisher → GitHub Actions) with exactly:
|
|
154
181
|
|
|
155
|
-
| Field | Value
|
|
156
|
-
| -------------------- |
|
|
157
|
-
| Organization or user | `antasphere`
|
|
158
|
-
| Repository | `cli-core`
|
|
159
|
-
| Workflow filename | `publish.yml`
|
|
160
|
-
| Environment |
|
|
182
|
+
| Field | Value |
|
|
183
|
+
| -------------------- | --------------- |
|
|
184
|
+
| Organization or user | `antasphere` |
|
|
185
|
+
| Repository | `cli-core` |
|
|
186
|
+
| Workflow filename | `publish.yml` |
|
|
187
|
+
| Environment | _(leave empty)_ |
|
|
161
188
|
|
|
162
189
|
and every further release is just the tag.
|
|
163
190
|
|
package/dist/config.d.ts
CHANGED
|
@@ -25,14 +25,15 @@
|
|
|
25
25
|
export type CliEnv = Record<string, string | undefined>;
|
|
26
26
|
/**
|
|
27
27
|
* A tool-local key minted by the cross-tool connect exchange (binding
|
|
28
|
-
* patterns §7), cached in the tool's profile under the HUB
|
|
29
|
-
*
|
|
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.
|
|
30
33
|
*/
|
|
31
|
-
export interface
|
|
34
|
+
export interface CliConnectKey {
|
|
32
35
|
/** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
|
|
33
36
|
apiKey: string;
|
|
34
|
-
/** The tool-local workspace the key is bound to (the hub org's projection). */
|
|
35
|
-
toolWorkspaceId?: string;
|
|
36
37
|
/** The signed-in account's email at mint. */
|
|
37
38
|
email?: string;
|
|
38
39
|
/** ISO timestamp of the exchange that minted the key. */
|
|
@@ -47,12 +48,13 @@ export interface CliProfile {
|
|
|
47
48
|
email?: string;
|
|
48
49
|
/** The org/workspace the key is bound to (one workspace per credential). */
|
|
49
50
|
workspaceId?: string;
|
|
50
|
-
/** Hub profiles only: the org selected via `antasphere org use` (
|
|
51
|
-
*
|
|
51
|
+
/** Hub profiles only: the org selected via `antasphere org use` (the
|
|
52
|
+
* default per-request org context; never part of any credential). */
|
|
52
53
|
activeWorkspaceId?: string;
|
|
53
|
-
/** Tool profiles only: exchange-minted keys,
|
|
54
|
-
*
|
|
55
|
-
|
|
54
|
+
/** 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. */
|
|
57
|
+
connectKeys?: Record<string, CliConnectKey>;
|
|
56
58
|
}
|
|
57
59
|
export interface CliConfig {
|
|
58
60
|
activeProfile?: string;
|
|
@@ -75,23 +77,23 @@ export declare function clearConfig(env: CliEnv, tool: string): void;
|
|
|
75
77
|
/** `ant_abcd1234_…` → `ant_abcd1234_…c3f9` — safe for terminals and logs. */
|
|
76
78
|
export declare function redactKey(key: string): string;
|
|
77
79
|
/**
|
|
78
|
-
* The cached
|
|
79
|
-
*
|
|
80
|
-
* the
|
|
81
|
-
*
|
|
80
|
+
* The cached user-scoped connect key a HUB PROFILE minted for this tool
|
|
81
|
+
* profile, if any. The slot is the hub profile NAME — stable across runs
|
|
82
|
+
* and orgs (the key identifies the user; selecting a different org between
|
|
83
|
+
* runs never misses the cache).
|
|
82
84
|
*/
|
|
83
|
-
export declare function
|
|
85
|
+
export declare function lookupConnectKey(profile: CliProfile | undefined, hubProfileName: string): string | undefined;
|
|
84
86
|
/**
|
|
85
|
-
* Cache an exchange-minted tool key under (profile, hub
|
|
86
|
-
* profile on first contact (and making it active when no profile is
|
|
87
|
-
* yet — first cloud connect on a fresh machine). Every other profile
|
|
88
|
-
* is preserved untouched. Returns the file written.
|
|
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.
|
|
89
91
|
*/
|
|
90
|
-
export declare function
|
|
92
|
+
export declare function storeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string, entry: CliConnectKey): string;
|
|
91
93
|
/**
|
|
92
|
-
* Drop one
|
|
93
|
-
* Returns whether anything was removed; the legacy single
|
|
94
|
-
* touched — it belongs to the tool's own login flow.
|
|
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.
|
|
95
97
|
*/
|
|
96
|
-
export declare function
|
|
98
|
+
export declare function removeConnectKey(env: CliEnv, tool: string, profileName: string, hubProfileName: string): boolean;
|
|
97
99
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -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
|
|
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;IACf,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;0EACsE;IACtE,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;iFAE6E;IAC7E,WAA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;GAIG;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,48 @@ export function redactKey(key) {
|
|
|
76
76
|
return `${key.slice(0, 13)}…${key.slice(-4)}`;
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* The cached
|
|
80
|
-
*
|
|
81
|
-
* the
|
|
82
|
-
*
|
|
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
|
|
85
|
-
|
|
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;
|
|
84
|
+
export function lookupConnectKey(profile, hubProfileName) {
|
|
85
|
+
return profile?.connectKeys?.[hubProfileName]?.apiKey;
|
|
93
86
|
}
|
|
94
87
|
/**
|
|
95
|
-
* Cache an exchange-minted tool key under (profile, hub
|
|
96
|
-
* profile on first contact (and making it active when no profile is
|
|
97
|
-
* yet — first cloud connect on a fresh machine). Every other profile
|
|
98
|
-
* is preserved untouched. Returns the file written.
|
|
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.
|
|
99
92
|
*/
|
|
100
|
-
export function
|
|
93
|
+
export function storeConnectKey(env, tool, profileName, hubProfileName, entry) {
|
|
101
94
|
const config = loadConfig(env, tool);
|
|
102
95
|
const profile = config.profiles[profileName] ?? {};
|
|
103
96
|
config.profiles[profileName] = {
|
|
104
97
|
...profile,
|
|
105
|
-
|
|
98
|
+
connectKeys: { ...profile.connectKeys, [hubProfileName]: entry }
|
|
106
99
|
};
|
|
107
100
|
if (!config.activeProfile)
|
|
108
101
|
config.activeProfile = profileName;
|
|
109
102
|
return saveConfig(env, tool, config);
|
|
110
103
|
}
|
|
111
104
|
/**
|
|
112
|
-
* Drop one
|
|
113
|
-
* Returns whether anything was removed; the legacy single
|
|
114
|
-
* touched — it belongs to the tool's own login flow.
|
|
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.
|
|
115
108
|
*/
|
|
116
|
-
export function
|
|
109
|
+
export function removeConnectKey(env, tool, profileName, hubProfileName) {
|
|
117
110
|
const config = loadConfig(env, tool);
|
|
118
111
|
const profile = config.profiles[profileName];
|
|
119
|
-
if (!profile?.
|
|
112
|
+
if (!profile?.connectKeys || !(hubProfileName in profile.connectKeys))
|
|
120
113
|
return false;
|
|
121
|
-
const { [
|
|
114
|
+
const { [hubProfileName]: _dropped, ...rest } = profile.connectKeys;
|
|
122
115
|
if (Object.keys(rest).length === 0) {
|
|
123
|
-
const {
|
|
116
|
+
const { connectKeys: _empty, ...bare } = profile;
|
|
124
117
|
config.profiles[profileName] = bare;
|
|
125
118
|
}
|
|
126
119
|
else {
|
|
127
|
-
config.profiles[profileName] = { ...profile,
|
|
120
|
+
config.profiles[profileName] = { ...profile, connectKeys: rest };
|
|
128
121
|
}
|
|
129
122
|
saveConfig(env, tool, config);
|
|
130
123
|
return true;
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,E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AC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACjD,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC;IACtC,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;IACnE,CAAC;IACD,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { CliEnv } from './config.js';
|
|
2
|
+
import type { CliConnectResult } from './connect.js';
|
|
3
|
+
import type { ResolvedProfile } from './resolve.js';
|
|
4
|
+
/**
|
|
5
|
+
* Connect-on-demand (binding patterns §7, gcloud model), extracted from the
|
|
6
|
+
* Slideless CLI so every Antasphere tool CLI inherits "one `antasphere login`
|
|
7
|
+
* serves every federated tool". When a tool CLI resolves NO credential of its
|
|
8
|
+
* own, it calls `connectOnDemand`:
|
|
9
|
+
*
|
|
10
|
+
* 0. The cache is consulted FIRST: a key minted earlier for THIS instance
|
|
11
|
+
* under the active hub profile is USER-scoped (hub ADR 014) — it serves
|
|
12
|
+
* every subsequent run and every org, with zero network (no probe, no
|
|
13
|
+
* hub round-trip, no fresh mint).
|
|
14
|
+
* 1. On a miss, discovery gates the flow: only an Antasphere-CLOUD
|
|
15
|
+
* instance can mint keys through the hub (unauthenticated GET
|
|
16
|
+
* /instance — never guess). Unreachable, foreign, or self-hosted
|
|
17
|
+
* instances answer `not_cloud` and the tool raises its classic no-key
|
|
18
|
+
* error unchanged.
|
|
19
|
+
* 2. The hub credential is what `antasphere login` stored on the hub
|
|
20
|
+
* profile (the config home's HUB_TOOL namespace) — hub URL included,
|
|
21
|
+
* never a hardcoded constant here.
|
|
22
|
+
* 3. `CliConnectClient.exchange` trades it for a tool-local key (one
|
|
23
|
+
* re-exchange retry when the 120 s JWT dies in transit; a rejected hub
|
|
24
|
+
* credential maps to guided `antasphere login` advice).
|
|
25
|
+
* 4. The minted key is cached per (tool, HUB PROFILE) — but only onto a
|
|
26
|
+
* profile that names THIS instance (a fresh profile gets pinned to it);
|
|
27
|
+
* a profile pointing elsewhere never receives it.
|
|
28
|
+
*
|
|
29
|
+
* A new tool needs only its namespace, base URL, and copy — the flow itself
|
|
30
|
+
* is tool-agnostic.
|
|
31
|
+
*/
|
|
32
|
+
/** What the unauthenticated discovery probe learned about a tool instance. */
|
|
33
|
+
export interface ToolInstanceProbe {
|
|
34
|
+
/** Whether the instance signs in through the Antasphere hub (advertises
|
|
35
|
+
* the `antasphere` auth method, or calls itself the cloud edition). */
|
|
36
|
+
cloud: boolean;
|
|
37
|
+
/** The instance's self-reported edition (`cloud`, `oss`, …). */
|
|
38
|
+
edition: string | undefined;
|
|
39
|
+
/** The advertised auth methods (open enum — unknown entries kept). */
|
|
40
|
+
authMethods: string[];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Probe a tool instance's unauthenticated discovery endpoint
|
|
44
|
+
* (`GET {toolBaseUrl}/api/v1/instance`). Returns what it advertises, or
|
|
45
|
+
* `null` when the instance is unreachable or answers a foreign shape —
|
|
46
|
+
* callers treat both exactly like "not cloud".
|
|
47
|
+
*/
|
|
48
|
+
export declare function probeToolInstance(toolBaseUrl: string, fetchImpl?: typeof globalThis.fetch): Promise<ToolInstanceProbe | null>;
|
|
49
|
+
/**
|
|
50
|
+
* The ACTIVE hub profile — what `antasphere login` stored. Its NAME is the
|
|
51
|
+
* connect cache's slot (config.ts `connectKeys`): stable per hub account on
|
|
52
|
+
* this machine, independent of any org selection.
|
|
53
|
+
*/
|
|
54
|
+
export declare function activeHubProfile(env: CliEnv): ResolvedProfile;
|
|
55
|
+
/** The facts the connected notice is rendered from (fresh exchanges only —
|
|
56
|
+
* cache hits are silent). */
|
|
57
|
+
export interface ConnectNoticeFacts {
|
|
58
|
+
/** The tool instance the exchange connected to (origin-normalized). */
|
|
59
|
+
toolBaseUrl: string;
|
|
60
|
+
/** The signed-in account's email at mint. */
|
|
61
|
+
email: string;
|
|
62
|
+
/** Whether the minted key was persisted for the next run (per
|
|
63
|
+
* (tool, hub profile)); false = the profile names another instance. */
|
|
64
|
+
stored: boolean;
|
|
65
|
+
/** The tool profile the cache targeted. */
|
|
66
|
+
profileName: string;
|
|
67
|
+
/** When NOT stored: the other instance the profile points at. */
|
|
68
|
+
profileBaseUrl?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Branded copy seams. Defaults reproduce the Slideless CLI's strings modulo
|
|
72
|
+
* the tool name (the tool namespace stands in for the display name, and the
|
|
73
|
+
* key-env-var name is derived from it: `slideless` → `SLIDELESS_API_KEY`).
|
|
74
|
+
*/
|
|
75
|
+
export interface ConnectOnDemandMessages {
|
|
76
|
+
/** `CliUsageError` text when the instance is cloud but no `antasphere
|
|
77
|
+
* login` credential is stored on the hub profile. */
|
|
78
|
+
missingHubLogin?: string;
|
|
79
|
+
/** `CliUsageError` text when the hub rejected the stored credential. */
|
|
80
|
+
hubLoginRejected?: string;
|
|
81
|
+
/** Renders the success notice handed to `notify` (newline included). */
|
|
82
|
+
connected?: (facts: ConnectNoticeFacts) => string;
|
|
83
|
+
}
|
|
84
|
+
export interface ConnectOnDemandOptions {
|
|
85
|
+
/** The tool's config namespace (lowercase slug — it names the cache file). */
|
|
86
|
+
tool: string;
|
|
87
|
+
/** The resolved tool instance origin the CLI is talking to. */
|
|
88
|
+
toolBaseUrl: string;
|
|
89
|
+
/** The exchange resource URL; default = this instance's MCP endpoint,
|
|
90
|
+
* `${toolBaseUrl}/mcp` (the tool's registered resource URL). */
|
|
91
|
+
resource?: string;
|
|
92
|
+
/** The tool profile the minted key is cached on; default `DEFAULT_PROFILE`. */
|
|
93
|
+
profileName?: string;
|
|
94
|
+
env: CliEnv;
|
|
95
|
+
fetch?: typeof globalThis.fetch;
|
|
96
|
+
/** Receives the human "Connected to …" notice (tools wire their stderr);
|
|
97
|
+
* omitted = silent. Cache hits are always silent either way. */
|
|
98
|
+
notify?: (line: string) => void;
|
|
99
|
+
messages?: ConnectOnDemandMessages;
|
|
100
|
+
}
|
|
101
|
+
export type ConnectOnDemandOutcome = {
|
|
102
|
+
outcome: 'connected';
|
|
103
|
+
/** The tool-local key — this invocation's credential. */
|
|
104
|
+
key: string;
|
|
105
|
+
/** true = served from the (tool, hub profile) config cache: zero
|
|
106
|
+
* network, nothing minted. false = a fresh exchange ran. */
|
|
107
|
+
cached: boolean;
|
|
108
|
+
/** Fresh exchanges only: the full result (user, key meta). */
|
|
109
|
+
result?: CliConnectResult;
|
|
110
|
+
/** Fresh exchanges only: whether the minted key was persisted for
|
|
111
|
+
* the next run (false = the profile names another instance). */
|
|
112
|
+
stored?: boolean;
|
|
113
|
+
} | {
|
|
114
|
+
/** Not an Antasphere-cloud instance (or unreachable): the caller
|
|
115
|
+
* raises its classic no-key error unchanged. */
|
|
116
|
+
outcome: 'not_cloud';
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
120
|
+
* resolved. Serves the cached user-scoped key when one exists for this
|
|
121
|
+
* (instance, hub profile); otherwise returns `not_cloud` when the instance
|
|
122
|
+
* is not an Antasphere-cloud one (or unreachable). Throws a guided
|
|
123
|
+
* `CliUsageError` when the instance IS cloud but the hub login is missing or
|
|
124
|
+
* rejected; every other exchange failure propagates as its `CliConnectError`.
|
|
125
|
+
*/
|
|
126
|
+
export declare function connectOnDemand(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome>;
|
|
127
|
+
//# sourceMappingURL=connect-on-demand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-on-demand.d.ts","sourceRoot":"","sources":["../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IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,+DAA+D;IAC/D,WAAW,EAAE,MAAM,CAAC;IACpB;qEACiE;IACjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,+EAA+E;IAC/E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC;qEACiE;IACjE,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAChC,QAAQ,CAAC,EAAE,uBAAuB,CAAC;CACpC;AAED,MAAM,MAAM,sBAAsB,GAC9B;IACE,OAAO,EAAE,WAAW,CAAC;IACrB,yDAAyD;IACzD,GAAG,EAAE,MAAM,CAAC;IACZ;iEAC6D;IAC7D,MAAM,EAAE,OAAO,CAAC;IAChB,8DAA8D;IAC9D,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B;qEACiE;IACjE,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,GACD;IACE;qDACiD;IACjD,OAAO,EAAE,WAAW,CAAC;CACtB,CAAC;AAEN;;;;;;;GAOG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAwFnG"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { assertToolNamespace, HUB_TOOL, loadConfig, lookupConnectKey, saveConfig, storeConnectKey } from './config.js';
|
|
2
|
+
import { CliConnectClient, CliConnectError } from './connect.js';
|
|
3
|
+
import { CliUsageError, DEFAULT_PROFILE, resolveProfile } from './resolve.js';
|
|
4
|
+
/**
|
|
5
|
+
* Probe a tool instance's unauthenticated discovery endpoint
|
|
6
|
+
* (`GET {toolBaseUrl}/api/v1/instance`). Returns what it advertises, or
|
|
7
|
+
* `null` when the instance is unreachable or answers a foreign shape —
|
|
8
|
+
* callers treat both exactly like "not cloud".
|
|
9
|
+
*/
|
|
10
|
+
export async function probeToolInstance(toolBaseUrl, fetchImpl) {
|
|
11
|
+
const f = fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
12
|
+
try {
|
|
13
|
+
const res = await f(`${toolBaseUrl.replace(/\/+$/, '')}/api/v1/instance`, {
|
|
14
|
+
method: 'GET',
|
|
15
|
+
// Discovery is served cacheable for CLI/MCP clients, but the probe
|
|
16
|
+
// must see live state (mirrors the tool SDKs' discovery call).
|
|
17
|
+
cache: 'no-store'
|
|
18
|
+
});
|
|
19
|
+
if (!res.ok)
|
|
20
|
+
return null;
|
|
21
|
+
const info = (await res.json().catch(() => null));
|
|
22
|
+
const methods = info?.auth?.methods;
|
|
23
|
+
if (!Array.isArray(methods))
|
|
24
|
+
return null;
|
|
25
|
+
const authMethods = methods.filter((m) => typeof m === 'string');
|
|
26
|
+
const edition = typeof info?.edition === 'string' ? info.edition : undefined;
|
|
27
|
+
return { cloud: authMethods.includes('antasphere') || edition === 'cloud', edition, authMethods };
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The ACTIVE hub profile — what `antasphere login` stored. Its NAME is the
|
|
35
|
+
* connect cache's slot (config.ts `connectKeys`): stable per hub account on
|
|
36
|
+
* this machine, independent of any org selection.
|
|
37
|
+
*/
|
|
38
|
+
export function activeHubProfile(env) {
|
|
39
|
+
return resolveProfile(loadConfig(env, HUB_TOOL), undefined);
|
|
40
|
+
}
|
|
41
|
+
function defaultMissingHubLogin(tool) {
|
|
42
|
+
const envVar = `${tool.replace(/-/g, '_').toUpperCase()}_API_KEY`;
|
|
43
|
+
return (`This ${tool} instance signs in through the Antasphere hub. Run \`antasphere login\` once, ` +
|
|
44
|
+
`then retry — or pass --api-key / set ${envVar}.`);
|
|
45
|
+
}
|
|
46
|
+
const DEFAULT_HUB_LOGIN_REJECTED = 'The stored Antasphere hub credential was rejected — run `antasphere login` again, then retry.';
|
|
47
|
+
function defaultConnected(facts) {
|
|
48
|
+
return (`Connected to ${facts.toolBaseUrl} as ${facts.email} via Antasphere` +
|
|
49
|
+
(facts.stored
|
|
50
|
+
? ''
|
|
51
|
+
: ` — key NOT cached: profile '${facts.profileName}' points at ${facts.profileBaseUrl}`) +
|
|
52
|
+
'.\n');
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
56
|
+
* resolved. Serves the cached user-scoped key when one exists for this
|
|
57
|
+
* (instance, hub profile); otherwise returns `not_cloud` when the instance
|
|
58
|
+
* is not an Antasphere-cloud one (or unreachable). Throws a guided
|
|
59
|
+
* `CliUsageError` when the instance IS cloud but the hub login is missing or
|
|
60
|
+
* rejected; every other exchange failure propagates as its `CliConnectError`.
|
|
61
|
+
*/
|
|
62
|
+
export async function connectOnDemand(opts) {
|
|
63
|
+
assertToolNamespace(opts.tool);
|
|
64
|
+
const toolBaseUrl = opts.toolBaseUrl.replace(/\/+$/, '');
|
|
65
|
+
const messages = opts.messages ?? {};
|
|
66
|
+
const profileName = opts.profileName ?? DEFAULT_PROFILE;
|
|
67
|
+
// 0. The cache: a key minted earlier by the active hub profile is
|
|
68
|
+
// user-scoped — it serves every run and every org (hub ADR 014; the
|
|
69
|
+
// org is a per-request parameter, never part of the credential). Only
|
|
70
|
+
// a profile that names THIS instance is trusted: a cached key never
|
|
71
|
+
// travels to a different instance. A hit costs zero network.
|
|
72
|
+
const hub = activeHubProfile(opts.env);
|
|
73
|
+
const existing = loadConfig(opts.env, opts.tool).profiles[profileName];
|
|
74
|
+
const pinnedHere = existing?.baseUrl?.replace(/\/+$/, '') === toolBaseUrl;
|
|
75
|
+
if (pinnedHere && hub.name) {
|
|
76
|
+
const cachedKey = lookupConnectKey(existing, hub.name);
|
|
77
|
+
if (cachedKey)
|
|
78
|
+
return { outcome: 'connected', key: cachedKey, cached: true };
|
|
79
|
+
}
|
|
80
|
+
// 1. Only a CLOUD instance can mint keys through the hub — ask discovery,
|
|
81
|
+
// never guess. An unreachable or foreign instance is `not_cloud`.
|
|
82
|
+
const probe = await probeToolInstance(toolBaseUrl, opts.fetch);
|
|
83
|
+
if (!probe?.cloud)
|
|
84
|
+
return { outcome: 'not_cloud' };
|
|
85
|
+
// 2. The hub credential is what `antasphere login` stored on the hub
|
|
86
|
+
// profile (HUB_TOOL namespace) — one login for the whole tool family.
|
|
87
|
+
// The hub URL comes from that same profile, never from a constant here.
|
|
88
|
+
const hubKey = hub.profile?.apiKey;
|
|
89
|
+
const hubBaseUrl = hub.profile?.baseUrl;
|
|
90
|
+
if (!hubKey || !hubBaseUrl || !hub.name) {
|
|
91
|
+
throw new CliUsageError(messages.missingHubLogin ?? defaultMissingHubLogin(opts.tool));
|
|
92
|
+
}
|
|
93
|
+
// 3. The exchange resource defaults to THIS instance's registered resource
|
|
94
|
+
// URL — its MCP endpoint, derived from the resolved instance origin.
|
|
95
|
+
// The body names NO org: the exchange is user-scoped end to end.
|
|
96
|
+
const resource = opts.resource ?? `${toolBaseUrl}/mcp`;
|
|
97
|
+
const connect = new CliConnectClient({
|
|
98
|
+
hubBaseUrl,
|
|
99
|
+
toolBaseUrl,
|
|
100
|
+
...(opts.fetch ? { fetch: opts.fetch } : {})
|
|
101
|
+
});
|
|
102
|
+
const body = { resource };
|
|
103
|
+
let result;
|
|
104
|
+
try {
|
|
105
|
+
result = await connect.exchange(hubKey, body);
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
if (e instanceof CliConnectError && e.needsReExchange) {
|
|
109
|
+
// The 120 s JWT died in transit (expired/replayed) — one fresh
|
|
110
|
+
// exchange; a second rejection propagates.
|
|
111
|
+
result = await connect.exchange(hubKey, body);
|
|
112
|
+
}
|
|
113
|
+
else if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
114
|
+
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
throw e;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// 4. Cache the minted key per (tool, hub profile) — but only onto a
|
|
121
|
+
// profile that names THIS instance (or a fresh one, which gets pinned
|
|
122
|
+
// to it), never onto a profile pointing somewhere else.
|
|
123
|
+
const stored = !existing?.baseUrl || pinnedHere;
|
|
124
|
+
if (stored) {
|
|
125
|
+
storeConnectKey(opts.env, opts.tool, profileName, hub.name, {
|
|
126
|
+
apiKey: result.key,
|
|
127
|
+
email: result.user.email,
|
|
128
|
+
mintedAt: new Date().toISOString()
|
|
129
|
+
});
|
|
130
|
+
if (!existing?.baseUrl) {
|
|
131
|
+
// First contact: pin the profile to this instance so the next
|
|
132
|
+
// invocation resolves the URL and the cached key with no flags.
|
|
133
|
+
const cfg = loadConfig(opts.env, opts.tool);
|
|
134
|
+
cfg.profiles[profileName] = { ...cfg.profiles[profileName], baseUrl: toolBaseUrl };
|
|
135
|
+
saveConfig(opts.env, opts.tool, cfg);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const facts = {
|
|
139
|
+
toolBaseUrl,
|
|
140
|
+
email: result.user.email,
|
|
141
|
+
stored,
|
|
142
|
+
profileName,
|
|
143
|
+
// `stored` is false only when the profile names another instance.
|
|
144
|
+
...(stored ? {} : { profileBaseUrl: existing?.baseUrl })
|
|
145
|
+
};
|
|
146
|
+
opts.notify?.((messages.connected ?? defaultConnected)(facts));
|
|
147
|
+
return { outcome: 'connected', key: result.key, result, cached: false, stored };
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=connect-on-demand.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-on-demand.js","sourceRoot":"","sources":["../src/connect-on-demand.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,QAAQ,EACR,UAAU,EACV,gBAAgB,EAChB,UAAU,EACV,eAAe,EAChB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,gBAAgB,EAAE,eAA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