@antasphere/cli-core 0.1.0 → 0.2.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
@@ -65,6 +65,25 @@ expiresInDays?, workspaceId? })` (the full key appears exactly once),
65
65
  with `status`/`code`/`message`.
66
66
  - `CliIo` + `ask` + `printJson` — the injectable I/O seam (env, streams,
67
67
  fetch, interactive prompt) so tool CLIs run in-process under test.
68
+ - `CliConnectClient({ hubBaseUrl, toolBaseUrl })` — the cross-tool connect
69
+ 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
+ - `getActiveWorkspace / setActiveWorkspace / resolveTargetWorkspace (env)` —
84
+ hub-profile org context: what `antasphere org use` reads/writes
85
+ (`activeWorkspaceId` on the hub profile), and the exchange-target order
86
+ `--org` override → active org → the hub key's bound workspace.
68
87
  - `migrateLegacyConfig(env, { tool, legacyDir })` — one-shot, non-destructive
69
88
  import of a tool's pre-cli-core `$XDG_CONFIG_HOME/<legacyDir>/config.json`
70
89
  into the shared home. Imports only when the tool has no profiles in the
package/dist/config.d.ts CHANGED
@@ -23,6 +23,21 @@
23
23
  * XDG_CONFIG_HOME.
24
24
  */
25
25
  export type CliEnv = Record<string, string | undefined>;
26
+ /**
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 org it was
29
+ * exchanged for. Every field beyond the key itself is informational.
30
+ */
31
+ export interface CliWorkspaceKey {
32
+ /** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
33
+ apiKey: string;
34
+ /** The tool-local workspace the key is bound to (the hub org's projection). */
35
+ toolWorkspaceId?: string;
36
+ /** The signed-in account's email at mint. */
37
+ email?: string;
38
+ /** ISO timestamp of the exchange that minted the key. */
39
+ mintedAt?: string;
40
+ }
26
41
  export interface CliProfile {
27
42
  /** `<prefix>_...` API key for this profile. */
28
43
  apiKey?: string;
@@ -32,6 +47,12 @@ export interface CliProfile {
32
47
  email?: string;
33
48
  /** The org/workspace the key is bound to (one workspace per credential). */
34
49
  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>;
35
56
  }
36
57
  export interface CliConfig {
37
58
  activeProfile?: string;
@@ -53,4 +74,24 @@ export declare function saveConfig(env: CliEnv, tool: string, config: CliConfig)
53
74
  export declare function clearConfig(env: CliEnv, tool: string): void;
54
75
  /** `ant_abcd1234_…` → `ant_abcd1234_…c3f9` — safe for terminals and logs. */
55
76
  export declare function redactKey(key: string): string;
77
+ /**
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.
82
+ */
83
+ export declare function lookupWorkspaceKey(profile: CliProfile | undefined, workspaceId: string): string | undefined;
84
+ /**
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.
89
+ */
90
+ export declare function storeWorkspaceKey(env: CliEnv, tool: string, profileName: string, workspaceId: string, entry: CliWorkspaceKey): string;
91
+ /**
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.
95
+ */
96
+ export declare function removeWorkspaceKey(env: CliEnv, tool: string, profileName: string, workspaceId: string): boolean;
56
97
  //# 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,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;CACtB;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,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAItD;AAED,oFAAoF;AACpF,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMnE;AAID,oFAAoF;AACpF,w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CAChC,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,GAClB,OAAO,CAaT"}
package/dist/config.js CHANGED
@@ -75,4 +75,58 @@ export function redactKey(key) {
75
75
  return '****';
76
76
  return `${key.slice(0, 13)}…${key.slice(-4)}`;
77
77
  }
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.
83
+ */
84
+ export function lookupWorkspaceKey(profile, workspaceId) {
85
+ 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;
93
+ }
94
+ /**
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.
99
+ */
100
+ export function storeWorkspaceKey(env, tool, profileName, workspaceId, entry) {
101
+ const config = loadConfig(env, tool);
102
+ const profile = config.profiles[profileName] ?? {};
103
+ config.profiles[profileName] = {
104
+ ...profile,
105
+ workspaceKeys: { ...profile.workspaceKeys, [workspaceId]: entry }
106
+ };
107
+ if (!config.activeProfile)
108
+ config.activeProfile = profileName;
109
+ return saveConfig(env, tool, config);
110
+ }
111
+ /**
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.
115
+ */
116
+ export function removeWorkspaceKey(env, tool, profileName, workspaceId) {
117
+ const config = loadConfig(env, tool);
118
+ const profile = config.profiles[profileName];
119
+ if (!profile?.workspaceKeys || !(workspaceId in profile.workspaceKeys))
120
+ return false;
121
+ const { [workspaceId]: _dropped, ...rest } = profile.workspaceKeys;
122
+ if (Object.keys(rest).length === 0) {
123
+ const { workspaceKeys: _empty, ...bare } = profile;
124
+ config.profiles[profileName] = bare;
125
+ }
126
+ else {
127
+ config.profiles[profileName] = { ...profile, workspaceKeys: rest };
128
+ }
129
+ saveConfig(env, tool, config);
130
+ return true;
131
+ }
78
132
  //# sourceMappingURL=config.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAChG,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AA6C1C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,QAAQ,GAAG,KAAK,CAAC;AAE9B,MAAM,OAAO,GAAG,mBAAmB,CAAC;AAEpC,oEAAoE;AACpE,MAAM,UAAU,mBAAmB,CAAC,IAAY;IAC9C,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CAAC,2BAA2B,IAAI,6CAA6C,CAAC,CAAC;IAChG,CAAC;AACH,CAAC;AAED,oFAAoF;AACpF,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,IAAY;IAClD,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAC1B,MAAM,IAAI,GAAG,GAAG,CAAC,eAAe,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAClF,IAAI,CAAC,IAAI;QAAE,OAAO,IAAI,CAAC;IACvB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACtC,OAAO,IAAI,K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,CAAC,CAAC;IAC7C,IAAI,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC,WAAW,IAAI,OAAO,CAAC,aAAa,CAAC;QAAE,OAAO,KAAK,CAAC;IACrF,MAAM,EAAE,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC,aAAa,CAAC;IACnE,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACnC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACnD,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,aAAa,EAAE,IAAI,EAAE,CAAC;IACrE,CAAC;IACD,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IAC9B,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,109 @@
1
+ import { CliAuthError, type CliAuthMintedKey } from './auth.js';
2
+ /**
3
+ * The CLI cross-tool connect exchange (binding patterns §7, gcloud model):
4
+ * one `antasphere login` serves every federated tool. On demand, the hub
5
+ * API key is traded for a tool-local key in two calls:
6
+ *
7
+ * 1. POST {hub}/api/v1/sso/tool-token — auth = hub API key (Bearer),
8
+ * body `{ resource, workspaceId? }` → a single-org, 120-second JWT
9
+ * for the tool's resource URL. The token is OPAQUE to this client:
10
+ * it is relayed hub → tool, never inspected or stored.
11
+ * 2. POST {tool}/api/v1/sso/cli-connect — NO auth header (the JWT is
12
+ * the credential), body `{ token }` → the tool verifies, JIT-provisions,
13
+ * and mints an ordinary tool-local API key, returned exactly once.
14
+ *
15
+ * The hub key is sent to the HUB only — it never reaches the tool. The
16
+ * minted key is cached per (tool, hub org) in the tool's config namespace
17
+ * (config.ts `workspaceKeys`); day-to-day tool CLI calls then take the
18
+ * ordinary local API-key path with the hub out of the loop.
19
+ */
20
+ export type CliConnectStage = 'hub' | 'tool';
21
+ /**
22
+ * A failed exchange call, tagged with WHICH side rejected it so a CLI can
23
+ * pick the remedy without string-matching:
24
+ *
25
+ * - hub 400 `unknown_resource` — the resource URL is not in the registry.
26
+ * - hub 403 `no_membership` / `workspace_suspended` — the target org is
27
+ * not exchangeable for this user; pick another org.
28
+ * - `needsHubLogin` — the hub rejected the KEY itself; re-run
29
+ * `antasphere login`.
30
+ * - `needsReExchange` — the tool rejected the 120 s JWT (expired/replayed
31
+ * in transit); run the exchange again.
32
+ * - tool 429 `rate_limited` — back off and retry.
33
+ */
34
+ export declare class CliConnectError extends CliAuthError {
35
+ readonly stage: CliConnectStage;
36
+ constructor(stage: CliConnectStage, status: number, code: string, message: string, details?: unknown);
37
+ /**
38
+ * The hub rejected the key itself: invalid/revoked (401), or a key
39
+ * minted before the `sso:exchange` scope existed (403 insufficient_scope
40
+ * — old keys simply lack it). The fix is a fresh `antasphere login`.
41
+ */
42
+ get needsHubLogin(): boolean;
43
+ /** The tool rejected the JWT (expired/replayed) — run the exchange again. */
44
+ get needsReExchange(): boolean;
45
+ }
46
+ export interface CliToolTokenBody {
47
+ /** The tool's registry resource URL (e.g. `https://app.slideless.ai/mcp`). */
48
+ resource: string;
49
+ /** Target hub org — any live ACTIVE membership of the user (gcloud
50
+ * model); omit = the hub key's bound workspace. */
51
+ workspaceId?: string;
52
+ }
53
+ export interface CliToolTokenMinted {
54
+ /** The single-org exchange JWT — opaque, ~120 s; relay it, never keep it. */
55
+ token: string;
56
+ /** ISO expiry of the JWT (informational; the minted key outlives it). */
57
+ expiresAt: string;
58
+ /** The hub org the token was minted for (explicit target or the key's bound org). */
59
+ workspaceId: string;
60
+ }
61
+ export interface CliConnected {
62
+ /** Full tool-local key (`slk_`-style) — shown once, never retrievable. */
63
+ key: string;
64
+ apiKey: CliAuthMintedKey;
65
+ user: {
66
+ id: string;
67
+ email: string;
68
+ name: string;
69
+ };
70
+ /** The tool-local workspace the key is bound to (the hub org's projection). */
71
+ workspaceId: string;
72
+ }
73
+ /** The flattened outcome of a full exchange — everything a CLI caches. */
74
+ export interface CliConnectResult {
75
+ /** Full tool-local key — cache it per (tool, `hubWorkspaceId`). */
76
+ key: string;
77
+ apiKey: CliAuthMintedKey;
78
+ user: {
79
+ id: string;
80
+ email: string;
81
+ name: string;
82
+ };
83
+ /** The HUB org the exchange resolved to — the cache key (config.ts). */
84
+ hubWorkspaceId: string;
85
+ /** The tool-local workspace the minted key is bound to (informational). */
86
+ toolWorkspaceId: string;
87
+ }
88
+ export interface CliConnectClientOptions {
89
+ /** Hub origin, e.g. https://account.antasphere.com */
90
+ hubBaseUrl: string;
91
+ /** Tool instance origin, e.g. https://app.slideless.ai */
92
+ toolBaseUrl: string;
93
+ fetch?: typeof globalThis.fetch;
94
+ }
95
+ export declare class CliConnectClient {
96
+ private readonly hubBaseUrl;
97
+ private readonly toolBaseUrl;
98
+ private readonly fetchImpl;
99
+ constructor(options: CliConnectClientOptions);
100
+ /** Step 1 — the hub mints the 120 s single-org JWT for the tool's resource. */
101
+ toolToken(hubApiKey: string, body: CliToolTokenBody): Promise<CliToolTokenMinted>;
102
+ /** Step 2 — the tool verifies the JWT (its only credential) and mints its key. */
103
+ cliConnect(token: string): Promise<CliConnected>;
104
+ /** The full two-call exchange. The hub key goes to the hub ONLY. */
105
+ exchange(hubApiKey: string, body: CliToolTokenBody): Promise<CliConnectResult>;
106
+ /** Mirrors CliAuthClient's parse: `{ error: { code, message, details } }` envelopes. */
107
+ private call;
108
+ }
109
+ //# sourceMappingURL=connect.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEhE;;;;;;;;;;;;;;;;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C;IACvB,2EAA2E;IAC3E,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,uBAAuB;IACtC,sDAAsD;IACtD,UAAU,EAAE,MAAM,CAAC;IACnB,0DAA0D;IAC1D,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CACjC;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA0B;gBAExC,OAAO,EAAE,uBAAuB;IAM5C,+EAA+E;IAC/E,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIjF,kFAAkF;IAClF,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAIhD,oEAAoE;IAC9D,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAYpF,wFAAwF;YAC1E,IAAI;CAkBnB"}
@@ -0,0 +1,79 @@
1
+ import { CliAuthError } from './auth.js';
2
+ /**
3
+ * A failed exchange call, tagged with WHICH side rejected it so a CLI can
4
+ * pick the remedy without string-matching:
5
+ *
6
+ * - hub 400 `unknown_resource` — the resource URL is not in the registry.
7
+ * - hub 403 `no_membership` / `workspace_suspended` — the target org is
8
+ * not exchangeable for this user; pick another org.
9
+ * - `needsHubLogin` — the hub rejected the KEY itself; re-run
10
+ * `antasphere login`.
11
+ * - `needsReExchange` — the tool rejected the 120 s JWT (expired/replayed
12
+ * in transit); run the exchange again.
13
+ * - tool 429 `rate_limited` — back off and retry.
14
+ */
15
+ export class CliConnectError extends CliAuthError {
16
+ stage;
17
+ constructor(stage, status, code, message, details) {
18
+ super(status, code, message, details);
19
+ this.stage = stage;
20
+ this.name = 'CliConnectError';
21
+ }
22
+ /**
23
+ * The hub rejected the key itself: invalid/revoked (401), or a key
24
+ * minted before the `sso:exchange` scope existed (403 insufficient_scope
25
+ * — old keys simply lack it). The fix is a fresh `antasphere login`.
26
+ */
27
+ get needsHubLogin() {
28
+ return (this.stage === 'hub' &&
29
+ (this.status === 401 || (this.status === 403 && this.code === 'insufficient_scope')));
30
+ }
31
+ /** The tool rejected the JWT (expired/replayed) — run the exchange again. */
32
+ get needsReExchange() {
33
+ return this.stage === 'tool' && this.status === 401;
34
+ }
35
+ }
36
+ export class CliConnectClient {
37
+ hubBaseUrl;
38
+ toolBaseUrl;
39
+ fetchImpl;
40
+ constructor(options) {
41
+ this.hubBaseUrl = options.hubBaseUrl.replace(/\/+$/, '');
42
+ this.toolBaseUrl = options.toolBaseUrl.replace(/\/+$/, '');
43
+ this.fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
44
+ }
45
+ /** Step 1 — the hub mints the 120 s single-org JWT for the tool's resource. */
46
+ toolToken(hubApiKey, body) {
47
+ return this.call('hub', `${this.hubBaseUrl}/api/v1/sso/tool-token`, body, hubApiKey);
48
+ }
49
+ /** Step 2 — the tool verifies the JWT (its only credential) and mints its key. */
50
+ cliConnect(token) {
51
+ return this.call('tool', `${this.toolBaseUrl}/api/v1/sso/cli-connect`, { token });
52
+ }
53
+ /** The full two-call exchange. The hub key goes to the hub ONLY. */
54
+ async exchange(hubApiKey, body) {
55
+ const minted = await this.toolToken(hubApiKey, body);
56
+ const connected = await this.cliConnect(minted.token);
57
+ return {
58
+ key: connected.key,
59
+ apiKey: connected.apiKey,
60
+ user: connected.user,
61
+ hubWorkspaceId: minted.workspaceId,
62
+ toolWorkspaceId: connected.workspaceId
63
+ };
64
+ }
65
+ /** Mirrors CliAuthClient's parse: `{ error: { code, message, details } }` envelopes. */
66
+ async call(stage, url, body, apiKey) {
67
+ const headers = { 'content-type': 'application/json' };
68
+ if (apiKey)
69
+ headers['authorization'] = `Bearer ${apiKey}`;
70
+ const res = await this.fetchImpl(url, { method: 'POST', headers, body: JSON.stringify(body) });
71
+ const json = (await res.json().catch(() => null));
72
+ if (!res.ok) {
73
+ const errBody = (json ?? {});
74
+ throw new CliConnectError(stage, res.status, errBody.error?.code ?? 'unknown_error', errBody.error?.message ?? `Request failed with ${res.status}`, errBody.error?.details);
75
+ }
76
+ return json;
77
+ }
78
+ }
79
+ //# sourceMappingURL=connect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAyB,MAAM,WAAW,CAAC;AAuBhE;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,eAAgB,SAAQ,YAAY;IAE7B;IADlB,YACkB,KAAsB,EACtC,MAAc,EACd,IAAY,EACZ,OAAe,EACf,OAAiB;QAEjB,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QANtB,UAAK,GAAL,KAAK,CAAiB;QAOtC,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;IAED;;;;OAIG;IACH,IAAI,aAAa;QACf,OAAO,CACL,IAAI,CAAC,KAAK,KAAK,KAAK;YACpB,CAAC,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,GAAG,IAAI,IAAI,CAAC,IAAI,KAAK,oBAAoB,CAAC,CAAC,CACrF,CAAC;IACJ,CAAC;IAED,6EAA6E;IAC7E,IAAI,eAAe;QACjB,OAAO,IAAI,CAAC,KAAK,KAAK,MAAM,IAAI,IAAI,CAAC,MAAM,KAAK,GAAG,CAAC;IACtD,CAAC;CACF;AAgDD,MAAM,OAAO,gBAAgB;IACV,UAAU,CAAS;IACnB,WAAW,CAAS;IACpB,SAAS,CAA0B;IAEpD,YAAY,OAAgC;QAC1C,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU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;YAAE,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,MAAM,EAAE,CAAC;QAC1D,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAC/F,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAC+B,CAAC;QAChF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;YACZ,MAAM,OAAO,GAAG,CAAC,IAAI,IAAI,EAAE,CAAuE,CAAC;YACnG,MAAM,IAAI,eAAe,CACvB,KAAK,EACL,GAAG,CAAC,MAAM,EACV,OAAO,CAAC,KAAK,EAAE,IAAI,IAAI,eAAe,EACtC,OAAO,CAAC,KAAK,EAAE,OAAO,IAAI,uBAAuB,GAAG,CAAC,MAAM,EAAE,EAC7D,OAAO,CAAC,KAAK,EAAE,OAAO,CACvB,CAAC;QACJ,CAAC;QACD,OAAO,IAAS,CAAC;IACnB,CAAC;CACF"}
package/dist/index.d.ts CHANGED
@@ -2,18 +2,24 @@
2
2
  * @antasphere/cli-core — the shared foundation every Antasphere tool CLI
3
3
  * builds on (hub-architecture §8): the `~/.config/antasphere/` config home
4
4
  * with tool-namespaced profiles, the flag → env → profile → default URL
5
- * resolution order, the instance email-OTP auth client, and the CliIo
6
- * prompt seam. Tool commands, wire types, and API surfaces live in the
7
- * tools; this package carries only config/login plumbing, never secrets.
5
+ * resolution order, the instance email-OTP auth client, the hub→tool
6
+ * cross-tool connect exchange (+ its per-(tool, org) key cache and the
7
+ * hub-profile org context), and the CliIo prompt seam. Tool commands, wire
8
+ * types, and API surfaces live in the tools; this package carries only
9
+ * config/login plumbing, never secrets.
8
10
  */
9
- export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, parseConfig, redactKey, saveConfig } from './config.js';
10
- export type { CliConfig, CliEnv, CliProfile } from './config.js';
11
+ export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, lookupWorkspaceKey, parseConfig, redactKey, removeWorkspaceKey, saveConfig, storeWorkspaceKey } from './config.js';
12
+ export type { CliConfig, CliEnv, CliProfile, CliWorkspaceKey } from './config.js';
11
13
  export { ask, printJson } from './io.js';
12
14
  export type { CliIo } from './io.js';
13
15
  export { CliUsageError, DEFAULT_PROFILE, resolveApiKey, resolveBaseUrl, resolveProfile } from './resolve.js';
14
16
  export type { ResolveApiKeyOptions, ResolveBaseUrlOptions, ResolvedProfile } from './resolve.js';
15
17
  export { CliAuthClient, CliAuthError } from './auth.js';
16
18
  export type { CliAuthClientOptions, CliAuthCompleteBody, CliAuthCompleted, CliAuthMintedKey, CliAuthRequestBody, CliAuthRequested, CliAuthRevoked } from './auth.js';
19
+ export { CliConnectClient, CliConnectError } from './connect.js';
20
+ export type { CliConnectClientOptions, CliConnected, CliConnectResult, CliConnectStage, CliToolTokenBody, CliToolTokenMinted } from './connect.js';
21
+ export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
22
+ export type { OrgContextOptions, ResolveTargetWorkspaceOptions } from './org.js';
17
23
  export { migrateLegacyConfig } from './migrate.js';
18
24
  export type { LegacyMigrationSpec, MigrationResult } from './migrate.js';
19
25
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,mBAAmB,EACnB,WAAW,EACX,UAAU,EACV,QAAQ,EACR,UAAU,EACV,WAAW,EACX,SAAS,EACT,UAAU,EACX,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,EAAE,GAAG,EAAE,SAAS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AW,CAAC;AACxD,YAAY,EACV,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,gBAAgB,EAChB,cAAc,EACf,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACjE,YAAY,EACV,uBAAuB,EACvB,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,kBAAkB,EACnB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC1F,YAAY,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,MAAM,UAAU,CAAC;AAEjF,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,YAAY,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js CHANGED
@@ -2,13 +2,17 @@
2
2
  * @antasphere/cli-core — the shared foundation every Antasphere tool CLI
3
3
  * builds on (hub-architecture §8): the `~/.config/antasphere/` config home
4
4
  * with tool-namespaced profiles, the flag → env → profile → default URL
5
- * resolution order, the instance email-OTP auth client, and the CliIo
6
- * prompt seam. Tool commands, wire types, and API surfaces live in the
7
- * tools; this package carries only config/login plumbing, never secrets.
5
+ * resolution order, the instance email-OTP auth client, the hub→tool
6
+ * cross-tool connect exchange (+ its per-(tool, org) key cache and the
7
+ * hub-profile org context), and the CliIo prompt seam. Tool commands, wire
8
+ * types, and API surfaces live in the tools; this package carries only
9
+ * config/login plumbing, never secrets.
8
10
  */
9
- export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, parseConfig, redactKey, saveConfig } from './config.js';
11
+ export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, lookupWorkspaceKey, parseConfig, redactKey, removeWorkspaceKey, saveConfig, storeWorkspaceKey } from './config.js';
10
12
  export { ask, printJson } from './io.js';
11
13
  export { CliUsageError, DEFAULT_PROFILE, resolveApiKey, resolveBaseUrl, resolveProfile } from './resolve.js';
12
14
  export { CliAuthClient, CliAuthError } from './auth.js';
15
+ export { CliConnectClient, CliConnectError } from './connect.js';
16
+ export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
13
17
  export { migrateLegacyConfig } from './migrate.js';
14
18
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EACL,mBAAmB,EACnB,WAAW,EACX,UAAU,EACV,QAAQ,EACR,UAAU,EACV,WAAW,EACX,SAAS,EACT,UAAU,EACX,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAGzC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG7G,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAWxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,mBAAmB,EACnB,WAAW,EACX,UAAU,EACV,QAAQ,EACR,UAAU,EACV,kBAAkB,EAClB,WAAW,EACX,SAAS,EACT,kBAAkB,EAClB,UAAU,EACV,iBAAiB,EAClB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAGzC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG7G,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAWxD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAUjE,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAG1F,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
package/dist/org.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import { type CliEnv } from './config.js';
2
+ /**
3
+ * Org context for the cross-tool connect flow (binding patterns §7). Orgs
4
+ * are HUB-level entities and the hub login is the one credential shared
5
+ * across the tool family, so the active org lives on the HUB profile:
6
+ * `antasphere org use` sets it, every tool CLI reads it when picking the
7
+ * org to exchange for.
8
+ *
9
+ * The effective target org for an exchange resolves in this order:
10
+ *
11
+ * explicit per-invocation override (--org)
12
+ * → the hub profile's active org (`org use`)
13
+ * → the hub key's bound workspace (set at login)
14
+ * → undefined — the exchange then omits `workspaceId` and the hub
15
+ * defaults to the key's bound org server-side.
16
+ */
17
+ export interface OrgContextOptions {
18
+ /** Hub profile name; default = the hub config's active profile. */
19
+ profile?: string;
20
+ }
21
+ /**
22
+ * The hub profile's effective active org: the `org use` choice when one is
23
+ * set, else the org the hub key was bound to at login. Undefined when not
24
+ * logged in (or an explicitly named profile is missing → CliUsageError).
25
+ */
26
+ export declare function getActiveWorkspace(env: CliEnv, opts?: OrgContextOptions): string | undefined;
27
+ /**
28
+ * What `antasphere org use` calls: persist the org choice on the hub
29
+ * profile (`null` clears it, falling back to the key's bound workspace).
30
+ * Every other profile field is preserved. Returns the file written.
31
+ */
32
+ export declare function setActiveWorkspace(env: CliEnv, workspaceId: string | null, opts?: OrgContextOptions): string;
33
+ export interface ResolveTargetWorkspaceOptions extends OrgContextOptions {
34
+ /** Per-invocation override (--org). */
35
+ override?: string;
36
+ }
37
+ /**
38
+ * The org a cross-tool exchange should target: explicit override → active
39
+ * org → the hub key's bound workspace → undefined (hub decides).
40
+ */
41
+ export declare function resolveTargetWorkspace(env: CliEnv, opts?: ResolveTargetWorkspaceOptions): string | undefined;
42
+ //# sourceMappingURL=org.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org.d.ts","sourceRoot":"","sources":["../src/org.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoC,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAG5E;;;;;;;;;;;;;;GAcG;AAEH,MAAM,WAAW,iBAAiB;IAChC,mEAAmE;IACnE,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,GAAE,iBAAsB,GAAG,MAAM,GAAG,SAAS,CAGhG;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,GAAG,IAAI,EAC1B,IAAI,GAAE,iBAAsB,GAC3B,MAAM,CAaR;AAED,MAAM,WAAW,6BAA8B,SAAQ,iBAAiB;IACtE,uCAAuC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,MAAM,EACX,IAAI,GAAE,6BAAkC,GACvC,MAAM,GAAG,SAAS,CAEpB"}
package/dist/org.js ADDED
@@ -0,0 +1,39 @@
1
+ import { HUB_TOOL, loadConfig, saveConfig } from './config.js';
2
+ import { CliUsageError, resolveProfile } from './resolve.js';
3
+ /**
4
+ * The hub profile's effective active org: the `org use` choice when one is
5
+ * set, else the org the hub key was bound to at login. Undefined when not
6
+ * logged in (or an explicitly named profile is missing → CliUsageError).
7
+ */
8
+ export function getActiveWorkspace(env, opts = {}) {
9
+ const { profile } = resolveProfile(loadConfig(env, HUB_TOOL), opts.profile);
10
+ return profile?.activeWorkspaceId ?? profile?.workspaceId;
11
+ }
12
+ /**
13
+ * What `antasphere org use` calls: persist the org choice on the hub
14
+ * profile (`null` clears it, falling back to the key's bound workspace).
15
+ * Every other profile field is preserved. Returns the file written.
16
+ */
17
+ export function setActiveWorkspace(env, workspaceId, opts = {}) {
18
+ const config = loadConfig(env, HUB_TOOL);
19
+ const { name, profile } = resolveProfile(config, opts.profile);
20
+ if (!name || !profile) {
21
+ throw new CliUsageError('No hub profile to set the org on — run `antasphere login` first.');
22
+ }
23
+ if (workspaceId === null) {
24
+ const { activeWorkspaceId: _cleared, ...rest } = profile;
25
+ config.profiles[name] = rest;
26
+ }
27
+ else {
28
+ config.profiles[name] = { ...profile, activeWorkspaceId: workspaceId };
29
+ }
30
+ return saveConfig(env, HUB_TOOL, config);
31
+ }
32
+ /**
33
+ * The org a cross-tool exchange should target: explicit override → active
34
+ * org → the hub key's bound workspace → undefined (hub decides).
35
+ */
36
+ export function resolveTargetWorkspace(env, opts = {}) {
37
+ return opts.override ?? getActiveWorkspace(env, opts);
38
+ }
39
+ //# sourceMappingURL=org.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"org.js","sourceRoot":"","sources":["../src/org.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAe,MAAM,aAAa,CAAC;AAC5E,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAuB7D;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAW,EAAE,OAA0B,EAAE;IAC1E,MAAM,EAAE,OAAO,EAAE,GAAG,cAAc,CAAC,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAC5E,OAAO,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,WAAW,CAAC;AAC5D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAChC,GAAW,EACX,WAA0B,EAC1B,OAA0B,EAAE;IAE5B,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;IACzC,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;IAC/D,IAAI,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACtB,MAAM,IAAI,aAAa,CAAC,kEAAkE,CAAC,CAAC;IAC9F,CAAC;IACD,IAAI,WAAW,KAAK,IAAI,EAAE,CAAC;QACzB,MAAM,EAAE,iBAAiB,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACzD,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAC/B,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,CAAC;IACzE,CAAC;IACD,OAAO,UAAU,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;AAC3C,CAAC;AAOD;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACpC,GAAW,EACX,OAAsC,EAAE;IAExC,OAAO,IAAI,CAAC,QAAQ,IAAI,kBAAkB,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;AACxD,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@antasphere/cli-core",
3
- "version": "0.1.0",
4
- "description": "Shared CLI foundation for Antasphere tool CLIs: the ~/.config/antasphere config home, tool-namespaced profiles, URL resolution, the instance email-OTP auth client, and the CliIo prompt seam",
3
+ "version": "0.2.0",
4
+ "description": "Shared CLI foundation for Antasphere tool CLIs: the ~/.config/antasphere config home, tool-namespaced profiles, URL resolution, the instance email-OTP auth client, the hub→tool cross-tool connect exchange, and the CliIo prompt seam",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
package/src/config.ts CHANGED
@@ -28,6 +28,22 @@ import { dirname, join } from 'node:path';
28
28
 
29
29
  export type CliEnv = Record<string, string | undefined>;
30
30
 
31
+ /**
32
+ * A tool-local key minted by the cross-tool connect exchange (binding
33
+ * patterns §7), cached in the tool's profile under the HUB org it was
34
+ * exchanged for. Every field beyond the key itself is informational.
35
+ */
36
+ export interface CliWorkspaceKey {
37
+ /** Full tool-local API key (`slk_`-style) — shown once at mint, cached here. */
38
+ apiKey: string;
39
+ /** The tool-local workspace the key is bound to (the hub org's projection). */
40
+ toolWorkspaceId?: string;
41
+ /** The signed-in account's email at mint. */
42
+ email?: string;
43
+ /** ISO timestamp of the exchange that minted the key. */
44
+ mintedAt?: string;
45
+ }
46
+
31
47
  export interface CliProfile {
32
48
  /** `<prefix>_...` API key for this profile. */
33
49
  apiKey?: string;
@@ -37,6 +53,12 @@ export interface CliProfile {
37
53
  email?: string;
38
54
  /** The org/workspace the key is bound to (one workspace per credential). */
39
55
  workspaceId?: string;
56
+ /** Hub profiles only: the org selected via `antasphere org use` (overrides
57
+ * the key's bound `workspaceId` as the default exchange target). */
58
+ activeWorkspaceId?: string;
59
+ /** Tool profiles only: exchange-minted keys, one per HUB org — the
60
+ * per-(tool, org) cache of the cross-tool connect flow. */
61
+ workspaceKeys?: Record<string, CliWorkspaceKey>;
40
62
  }
41
63
 
42
64
  export interface CliConfig {
@@ -123,3 +145,65 @@ export function redactKey(key: string): string {
123
145
  if (key.length <= 16) return '****';
124
146
  return `${key.slice(0, 13)}…${key.slice(-4)}`;
125
147
  }
148
+
149
+ /**
150
+ * The cached tool-local key for a HUB org, if any. Strictly org-matched:
151
+ * the per-org cache first, then the legacy single `apiKey` — but ONLY when
152
+ * the profile's `workspaceId` says it is bound to exactly this workspace.
153
+ * A key whose binding is unknown is never returned for an org lookup.
154
+ */
155
+ export function lookupWorkspaceKey(profile: CliProfile | undefined, workspaceId: string): string | undefined {
156
+ if (!profile) return undefined;
157
+ const entry = profile.workspaceKeys?.[workspaceId];
158
+ if (entry?.apiKey) return entry.apiKey;
159
+ if (profile.apiKey && profile.workspaceId === workspaceId) return profile.apiKey;
160
+ return undefined;
161
+ }
162
+
163
+ /**
164
+ * Cache an exchange-minted tool key under (profile, hub org), creating the
165
+ * profile on first contact (and making it active when no profile is active
166
+ * yet — first cloud connect on a fresh machine). Every other profile field
167
+ * is preserved untouched. Returns the file written.
168
+ */
169
+ export function storeWorkspaceKey(
170
+ env: CliEnv,
171
+ tool: string,
172
+ profileName: string,
173
+ workspaceId: string,
174
+ entry: CliWorkspaceKey
175
+ ): string {
176
+ const config = loadConfig(env, tool);
177
+ const profile = config.profiles[profileName] ?? {};
178
+ config.profiles[profileName] = {
179
+ ...profile,
180
+ workspaceKeys: { ...profile.workspaceKeys, [workspaceId]: entry }
181
+ };
182
+ if (!config.activeProfile) config.activeProfile = profileName;
183
+ return saveConfig(env, tool, config);
184
+ }
185
+
186
+ /**
187
+ * Drop one org's cached key from a profile (tool logout for that org).
188
+ * Returns whether anything was removed; the legacy single `apiKey` is never
189
+ * touched — it belongs to the tool's own login flow.
190
+ */
191
+ export function removeWorkspaceKey(
192
+ env: CliEnv,
193
+ tool: string,
194
+ profileName: string,
195
+ workspaceId: string
196
+ ): boolean {
197
+ const config = loadConfig(env, tool);
198
+ const profile = config.profiles[profileName];
199
+ if (!profile?.workspaceKeys || !(workspaceId in profile.workspaceKeys)) return false;
200
+ const { [workspaceId]: _dropped, ...rest } = profile.workspaceKeys;
201
+ if (Object.keys(rest).length === 0) {
202
+ const { workspaceKeys: _empty, ...bare } = profile;
203
+ config.profiles[profileName] = bare;
204
+ } else {
205
+ config.profiles[profileName] = { ...profile, workspaceKeys: rest };
206
+ }
207
+ saveConfig(env, tool, config);
208
+ return true;
209
+ }
package/src/connect.ts ADDED
@@ -0,0 +1,166 @@
1
+ import { CliAuthError, type CliAuthMintedKey } from './auth.js';
2
+
3
+ /**
4
+ * The CLI cross-tool connect exchange (binding patterns §7, gcloud model):
5
+ * one `antasphere login` serves every federated tool. On demand, the hub
6
+ * API key is traded for a tool-local key in two calls:
7
+ *
8
+ * 1. POST {hub}/api/v1/sso/tool-token — auth = hub API key (Bearer),
9
+ * body `{ resource, workspaceId? }` → a single-org, 120-second JWT
10
+ * for the tool's resource URL. The token is OPAQUE to this client:
11
+ * it is relayed hub → tool, never inspected or stored.
12
+ * 2. POST {tool}/api/v1/sso/cli-connect — NO auth header (the JWT is
13
+ * the credential), body `{ token }` → the tool verifies, JIT-provisions,
14
+ * and mints an ordinary tool-local API key, returned exactly once.
15
+ *
16
+ * The hub key is sent to the HUB only — it never reaches the tool. The
17
+ * minted key is cached per (tool, hub org) in the tool's config namespace
18
+ * (config.ts `workspaceKeys`); day-to-day tool CLI calls then take the
19
+ * ordinary local API-key path with the hub out of the loop.
20
+ */
21
+
22
+ export type CliConnectStage = 'hub' | 'tool';
23
+
24
+ /**
25
+ * A failed exchange call, tagged with WHICH side rejected it so a CLI can
26
+ * pick the remedy without string-matching:
27
+ *
28
+ * - hub 400 `unknown_resource` — the resource URL is not in the registry.
29
+ * - hub 403 `no_membership` / `workspace_suspended` — the target org is
30
+ * not exchangeable for this user; pick another org.
31
+ * - `needsHubLogin` — the hub rejected the KEY itself; re-run
32
+ * `antasphere login`.
33
+ * - `needsReExchange` — the tool rejected the 120 s JWT (expired/replayed
34
+ * in transit); run the exchange again.
35
+ * - tool 429 `rate_limited` — back off and retry.
36
+ */
37
+ export class CliConnectError extends CliAuthError {
38
+ constructor(
39
+ public readonly stage: CliConnectStage,
40
+ status: number,
41
+ code: string,
42
+ message: string,
43
+ details?: unknown
44
+ ) {
45
+ super(status, code, message, details);
46
+ this.name = 'CliConnectError';
47
+ }
48
+
49
+ /**
50
+ * The hub rejected the key itself: invalid/revoked (401), or a key
51
+ * minted before the `sso:exchange` scope existed (403 insufficient_scope
52
+ * — old keys simply lack it). The fix is a fresh `antasphere login`.
53
+ */
54
+ get needsHubLogin(): boolean {
55
+ return (
56
+ this.stage === 'hub' &&
57
+ (this.status === 401 || (this.status === 403 && this.code === 'insufficient_scope'))
58
+ );
59
+ }
60
+
61
+ /** The tool rejected the JWT (expired/replayed) — run the exchange again. */
62
+ get needsReExchange(): boolean {
63
+ return this.stage === 'tool' && this.status === 401;
64
+ }
65
+ }
66
+
67
+ export interface CliToolTokenBody {
68
+ /** The tool's registry resource URL (e.g. `https://app.slideless.ai/mcp`). */
69
+ resource: string;
70
+ /** Target hub org — any live ACTIVE membership of the user (gcloud
71
+ * model); omit = the hub key's bound workspace. */
72
+ workspaceId?: string;
73
+ }
74
+
75
+ export interface CliToolTokenMinted {
76
+ /** The single-org exchange JWT — opaque, ~120 s; relay it, never keep it. */
77
+ token: string;
78
+ /** ISO expiry of the JWT (informational; the minted key outlives it). */
79
+ expiresAt: string;
80
+ /** The hub org the token was minted for (explicit target or the key's bound org). */
81
+ workspaceId: string;
82
+ }
83
+
84
+ export interface CliConnected {
85
+ /** Full tool-local key (`slk_`-style) — shown once, never retrievable. */
86
+ key: string;
87
+ apiKey: CliAuthMintedKey;
88
+ user: { id: string; email: string; name: string };
89
+ /** The tool-local workspace the key is bound to (the hub org's projection). */
90
+ workspaceId: string;
91
+ }
92
+
93
+ /** The flattened outcome of a full exchange — everything a CLI caches. */
94
+ export interface CliConnectResult {
95
+ /** Full tool-local key — cache it per (tool, `hubWorkspaceId`). */
96
+ key: string;
97
+ apiKey: CliAuthMintedKey;
98
+ user: { id: string; email: string; name: string };
99
+ /** The HUB org the exchange resolved to — the cache key (config.ts). */
100
+ hubWorkspaceId: string;
101
+ /** The tool-local workspace the minted key is bound to (informational). */
102
+ toolWorkspaceId: string;
103
+ }
104
+
105
+ export interface CliConnectClientOptions {
106
+ /** Hub origin, e.g. https://account.antasphere.com */
107
+ hubBaseUrl: string;
108
+ /** Tool instance origin, e.g. https://app.slideless.ai */
109
+ toolBaseUrl: string;
110
+ fetch?: typeof globalThis.fetch;
111
+ }
112
+
113
+ export class CliConnectClient {
114
+ private readonly hubBaseUrl: string;
115
+ private readonly toolBaseUrl: string;
116
+ private readonly fetchImpl: typeof globalThis.fetch;
117
+
118
+ constructor(options: CliConnectClientOptions) {
119
+ this.hubBaseUrl = options.hubBaseUrl.replace(/\/+$/, '');
120
+ this.toolBaseUrl = options.toolBaseUrl.replace(/\/+$/, '');
121
+ this.fetchImpl = options.fetch ?? globalThis.fetch.bind(globalThis);
122
+ }
123
+
124
+ /** Step 1 — the hub mints the 120 s single-org JWT for the tool's resource. */
125
+ toolToken(hubApiKey: string, body: CliToolTokenBody): Promise<CliToolTokenMinted> {
126
+ return this.call('hub', `${this.hubBaseUrl}/api/v1/sso/tool-token`, body, hubApiKey);
127
+ }
128
+
129
+ /** Step 2 — the tool verifies the JWT (its only credential) and mints its key. */
130
+ cliConnect(token: string): Promise<CliConnected> {
131
+ return this.call('tool', `${this.toolBaseUrl}/api/v1/sso/cli-connect`, { token });
132
+ }
133
+
134
+ /** The full two-call exchange. The hub key goes to the hub ONLY. */
135
+ async exchange(hubApiKey: string, body: CliToolTokenBody): Promise<CliConnectResult> {
136
+ const minted = await this.toolToken(hubApiKey, body);
137
+ const connected = await this.cliConnect(minted.token);
138
+ return {
139
+ key: connected.key,
140
+ apiKey: connected.apiKey,
141
+ user: connected.user,
142
+ hubWorkspaceId: minted.workspaceId,
143
+ toolWorkspaceId: connected.workspaceId
144
+ };
145
+ }
146
+
147
+ /** Mirrors CliAuthClient's parse: `{ error: { code, message, details } }` envelopes. */
148
+ private async call<T>(stage: CliConnectStage, url: string, body: unknown, apiKey?: string): Promise<T> {
149
+ const headers: Record<string, string> = { 'content-type': 'application/json' };
150
+ if (apiKey) headers['authorization'] = `Bearer ${apiKey}`;
151
+ const res = await this.fetchImpl(url, { method: 'POST', headers, body: JSON.stringify(body) });
152
+ const json = (await res.json().catch(() => null)) as
153
+ { error?: { code?: string; message?: string; details?: unknown } } | T | null;
154
+ if (!res.ok) {
155
+ const errBody = (json ?? {}) as { error?: { code?: string; message?: string; details?: unknown } };
156
+ throw new CliConnectError(
157
+ stage,
158
+ res.status,
159
+ errBody.error?.code ?? 'unknown_error',
160
+ errBody.error?.message ?? `Request failed with ${res.status}`,
161
+ errBody.error?.details
162
+ );
163
+ }
164
+ return json as T;
165
+ }
166
+ }
package/src/index.ts CHANGED
@@ -2,9 +2,11 @@
2
2
  * @antasphere/cli-core — the shared foundation every Antasphere tool CLI
3
3
  * builds on (hub-architecture §8): the `~/.config/antasphere/` config home
4
4
  * with tool-namespaced profiles, the flag → env → profile → default URL
5
- * resolution order, the instance email-OTP auth client, and the CliIo
6
- * prompt seam. Tool commands, wire types, and API surfaces live in the
7
- * tools; this package carries only config/login plumbing, never secrets.
5
+ * resolution order, the instance email-OTP auth client, the hub→tool
6
+ * cross-tool connect exchange (+ its per-(tool, org) key cache and the
7
+ * hub-profile org context), and the CliIo prompt seam. Tool commands, wire
8
+ * types, and API surfaces live in the tools; this package carries only
9
+ * config/login plumbing, never secrets.
8
10
  */
9
11
 
10
12
  export {
@@ -13,11 +15,14 @@ export {
13
15
  configPath,
14
16
  HUB_TOOL,
15
17
  loadConfig,
18
+ lookupWorkspaceKey,
16
19
  parseConfig,
17
20
  redactKey,
18
- saveConfig
21
+ removeWorkspaceKey,
22
+ saveConfig,
23
+ storeWorkspaceKey
19
24
  } from './config.js';
20
- export type { CliConfig, CliEnv, CliProfile } from './config.js';
25
+ export type { CliConfig, CliEnv, CliProfile, CliWorkspaceKey } from './config.js';
21
26
 
22
27
  export { ask, printJson } from './io.js';
23
28
  export type { CliIo } from './io.js';
@@ -36,5 +41,18 @@ export type {
36
41
  CliAuthRevoked
37
42
  } from './auth.js';
38
43
 
44
+ export { CliConnectClient, CliConnectError } from './connect.js';
45
+ export type {
46
+ CliConnectClientOptions,
47
+ CliConnected,
48
+ CliConnectResult,
49
+ CliConnectStage,
50
+ CliToolTokenBody,
51
+ CliToolTokenMinted
52
+ } from './connect.js';
53
+
54
+ export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
55
+ export type { OrgContextOptions, ResolveTargetWorkspaceOptions } from './org.js';
56
+
39
57
  export { migrateLegacyConfig } from './migrate.js';
40
58
  export type { LegacyMigrationSpec, MigrationResult } from './migrate.js';
package/src/org.ts ADDED
@@ -0,0 +1,73 @@
1
+ import { HUB_TOOL, loadConfig, saveConfig, type CliEnv } from './config.js';
2
+ import { CliUsageError, resolveProfile } from './resolve.js';
3
+
4
+ /**
5
+ * Org context for the cross-tool connect flow (binding patterns §7). Orgs
6
+ * are HUB-level entities and the hub login is the one credential shared
7
+ * across the tool family, so the active org lives on the HUB profile:
8
+ * `antasphere org use` sets it, every tool CLI reads it when picking the
9
+ * org to exchange for.
10
+ *
11
+ * The effective target org for an exchange resolves in this order:
12
+ *
13
+ * explicit per-invocation override (--org)
14
+ * → the hub profile's active org (`org use`)
15
+ * → the hub key's bound workspace (set at login)
16
+ * → undefined — the exchange then omits `workspaceId` and the hub
17
+ * defaults to the key's bound org server-side.
18
+ */
19
+
20
+ export interface OrgContextOptions {
21
+ /** Hub profile name; default = the hub config's active profile. */
22
+ profile?: string;
23
+ }
24
+
25
+ /**
26
+ * The hub profile's effective active org: the `org use` choice when one is
27
+ * set, else the org the hub key was bound to at login. Undefined when not
28
+ * logged in (or an explicitly named profile is missing → CliUsageError).
29
+ */
30
+ export function getActiveWorkspace(env: CliEnv, opts: OrgContextOptions = {}): string | undefined {
31
+ const { profile } = resolveProfile(loadConfig(env, HUB_TOOL), opts.profile);
32
+ return profile?.activeWorkspaceId ?? profile?.workspaceId;
33
+ }
34
+
35
+ /**
36
+ * What `antasphere org use` calls: persist the org choice on the hub
37
+ * profile (`null` clears it, falling back to the key's bound workspace).
38
+ * Every other profile field is preserved. Returns the file written.
39
+ */
40
+ export function setActiveWorkspace(
41
+ env: CliEnv,
42
+ workspaceId: string | null,
43
+ opts: OrgContextOptions = {}
44
+ ): string {
45
+ const config = loadConfig(env, HUB_TOOL);
46
+ const { name, profile } = resolveProfile(config, opts.profile);
47
+ if (!name || !profile) {
48
+ throw new CliUsageError('No hub profile to set the org on — run `antasphere login` first.');
49
+ }
50
+ if (workspaceId === null) {
51
+ const { activeWorkspaceId: _cleared, ...rest } = profile;
52
+ config.profiles[name] = rest;
53
+ } else {
54
+ config.profiles[name] = { ...profile, activeWorkspaceId: workspaceId };
55
+ }
56
+ return saveConfig(env, HUB_TOOL, config);
57
+ }
58
+
59
+ export interface ResolveTargetWorkspaceOptions extends OrgContextOptions {
60
+ /** Per-invocation override (--org). */
61
+ override?: string;
62
+ }
63
+
64
+ /**
65
+ * The org a cross-tool exchange should target: explicit override → active
66
+ * org → the hub key's bound workspace → undefined (hub decides).
67
+ */
68
+ export function resolveTargetWorkspace(
69
+ env: CliEnv,
70
+ opts: ResolveTargetWorkspaceOptions = {}
71
+ ): string | undefined {
72
+ return opts.override ?? getActiveWorkspace(env, opts);
73
+ }