@antasphere/cli-core 0.2.0 → 0.3.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 +22 -7
- package/dist/connect-on-demand.d.ts +114 -0
- package/dist/connect-on-demand.d.ts.map +1 -0
- package/dist/connect-on-demand.js +130 -0
- package/dist/connect-on-demand.js.map +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/connect-on-demand.ts +250 -0
- package/src/index.ts +14 -4
package/README.md
CHANGED
|
@@ -80,6 +80,21 @@ expiresInDays?, workspaceId? })` (the full key appears exactly once),
|
|
|
80
80
|
profile's additive `workspaceKeys` map. The legacy single `apiKey` keeps
|
|
81
81
|
working unchanged and answers an org lookup only when the profile says it
|
|
82
82
|
is bound to exactly that workspace.
|
|
83
|
+
- `connectOnDemand({ tool, toolBaseUrl, resource?, org?, profileName?, env,
|
|
84
|
+
fetch?, notify?, messages? })` + `probeToolInstance(toolBaseUrl, fetch?)`
|
|
85
|
+
— the connect-on-demand seam a tool CLI runs when NO direct credential
|
|
86
|
+
resolved (extracted from the Slideless CLI): the anonymous `GET
|
|
87
|
+
/instance` probe gates the flow (only an Antasphere-cloud instance takes
|
|
88
|
+
it — self-hosted/unreachable ones answer `{ outcome: 'not_cloud' }` and
|
|
89
|
+
the tool raises its classic no-key error unchanged), the hub credential
|
|
90
|
+
is read from the `HUB_TOOL` profile, `exchange` runs with one
|
|
91
|
+
re-exchange retry on a dead 120 s JWT (missing/rejected hub login →
|
|
92
|
+
guided `antasphere login` `CliUsageError`), and the minted key is cached
|
|
93
|
+
per (tool, hub org) with first-contact baseUrl pinning — never onto a
|
|
94
|
+
profile that names another instance. `notify` receives the human
|
|
95
|
+
"Connected to …" notice (wire your stderr); `messages` overrides the
|
|
96
|
+
branded copy (defaults reproduce the Slideless strings modulo tool
|
|
97
|
+
name). Success: `{ outcome: 'connected', key, result, cached }`.
|
|
83
98
|
- `getActiveWorkspace / setActiveWorkspace / resolveTargetWorkspace (env)` —
|
|
84
99
|
hub-profile org context: what `antasphere org use` reads/writes
|
|
85
100
|
(`activeWorkspaceId` on the hub profile), and the exchange-target order
|
|
@@ -136,7 +151,7 @@ with no npm token — npm authenticates the workflow's OIDC identity directly.
|
|
|
136
151
|
|
|
137
152
|
```bash
|
|
138
153
|
# bump package.json#version, commit, then:
|
|
139
|
-
git tag v0.
|
|
154
|
+
git tag v0.3.0 && git push origin v0.3.0
|
|
140
155
|
```
|
|
141
156
|
|
|
142
157
|
**First publish is manual.** Trusted publishing can only be configured on a
|
|
@@ -152,12 +167,12 @@ npm publish # publishConfig.access = public
|
|
|
152
167
|
After that, configure the trusted publisher on npmjs.com
|
|
153
168
|
(package → Settings → Trusted Publisher → GitHub Actions) with exactly:
|
|
154
169
|
|
|
155
|
-
| Field | Value
|
|
156
|
-
| -------------------- |
|
|
157
|
-
| Organization or user | `antasphere`
|
|
158
|
-
| Repository | `cli-core`
|
|
159
|
-
| Workflow filename | `publish.yml`
|
|
160
|
-
| Environment |
|
|
170
|
+
| Field | Value |
|
|
171
|
+
| -------------------- | --------------- |
|
|
172
|
+
| Organization or user | `antasphere` |
|
|
173
|
+
| Repository | `cli-core` |
|
|
174
|
+
| Workflow filename | `publish.yml` |
|
|
175
|
+
| Environment | _(leave empty)_ |
|
|
161
176
|
|
|
162
177
|
and every further release is just the tag.
|
|
163
178
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { CliEnv } from './config.js';
|
|
2
|
+
import type { CliConnectResult } from './connect.js';
|
|
3
|
+
/**
|
|
4
|
+
* Connect-on-demand (binding patterns §7, gcloud model), extracted from the
|
|
5
|
+
* Slideless CLI so every Antasphere tool CLI inherits "one `antasphere login`
|
|
6
|
+
* serves every federated tool". When a tool CLI resolves NO credential of its
|
|
7
|
+
* own, it calls `connectOnDemand`:
|
|
8
|
+
*
|
|
9
|
+
* 1. Discovery gates the flow: only an Antasphere-CLOUD instance can mint
|
|
10
|
+
* keys through the hub (unauthenticated GET /instance — never guess).
|
|
11
|
+
* Unreachable, foreign, or self-hosted instances answer `not_cloud`
|
|
12
|
+
* and the tool raises its classic no-key error unchanged.
|
|
13
|
+
* 2. The hub credential is what `antasphere login` stored on the hub
|
|
14
|
+
* profile (the config home's HUB_TOOL namespace) — hub URL included,
|
|
15
|
+
* never a hardcoded constant here.
|
|
16
|
+
* 3. `CliConnectClient.exchange` trades it for a tool-local key (one
|
|
17
|
+
* re-exchange retry when the 120 s JWT dies in transit; a rejected hub
|
|
18
|
+
* credential maps to guided `antasphere login` advice).
|
|
19
|
+
* 4. The minted key is cached per (tool, HUB org) — but only onto a
|
|
20
|
+
* profile that names THIS instance (a fresh profile gets pinned to it);
|
|
21
|
+
* a profile pointing elsewhere never receives it.
|
|
22
|
+
*
|
|
23
|
+
* A new tool needs only its namespace, base URL, and copy — the flow itself
|
|
24
|
+
* is tool-agnostic.
|
|
25
|
+
*/
|
|
26
|
+
/** What the unauthenticated discovery probe learned about a tool instance. */
|
|
27
|
+
export interface ToolInstanceProbe {
|
|
28
|
+
/** Whether the instance signs in through the Antasphere hub (advertises
|
|
29
|
+
* the `antasphere` auth method, or calls itself the cloud edition). */
|
|
30
|
+
cloud: boolean;
|
|
31
|
+
/** The instance's self-reported edition (`cloud`, `oss`, …). */
|
|
32
|
+
edition: string | undefined;
|
|
33
|
+
/** The advertised auth methods (open enum — unknown entries kept). */
|
|
34
|
+
authMethods: string[];
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Probe a tool instance's unauthenticated discovery endpoint
|
|
38
|
+
* (`GET {toolBaseUrl}/api/v1/instance`). Returns what it advertises, or
|
|
39
|
+
* `null` when the instance is unreachable or answers a foreign shape —
|
|
40
|
+
* callers treat both exactly like "not cloud".
|
|
41
|
+
*/
|
|
42
|
+
export declare function probeToolInstance(toolBaseUrl: string, fetchImpl?: typeof globalThis.fetch): Promise<ToolInstanceProbe | null>;
|
|
43
|
+
/** The facts the connected notice is rendered from. */
|
|
44
|
+
export interface ConnectNoticeFacts {
|
|
45
|
+
/** The tool instance the exchange connected to (origin-normalized). */
|
|
46
|
+
toolBaseUrl: string;
|
|
47
|
+
/** The signed-in account's email at mint. */
|
|
48
|
+
email: string;
|
|
49
|
+
/** The HUB org the exchange resolved to. */
|
|
50
|
+
hubWorkspaceId: string;
|
|
51
|
+
/** Whether the minted key was cached (per (tool, hub org)). */
|
|
52
|
+
cached: boolean;
|
|
53
|
+
/** The tool profile the cache targeted. */
|
|
54
|
+
profileName: string;
|
|
55
|
+
/** When NOT cached: the other instance the profile points at. */
|
|
56
|
+
profileBaseUrl?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Branded copy seams. Defaults reproduce the Slideless CLI's strings modulo
|
|
60
|
+
* the tool name (the tool namespace stands in for the display name, and the
|
|
61
|
+
* key-env-var name is derived from it: `slideless` → `SLIDELESS_API_KEY`).
|
|
62
|
+
*/
|
|
63
|
+
export interface ConnectOnDemandMessages {
|
|
64
|
+
/** `CliUsageError` text when the instance is cloud but no `antasphere
|
|
65
|
+
* login` credential is stored on the hub profile. */
|
|
66
|
+
missingHubLogin?: string;
|
|
67
|
+
/** `CliUsageError` text when the hub rejected the stored credential. */
|
|
68
|
+
hubLoginRejected?: string;
|
|
69
|
+
/** Renders the success notice handed to `notify` (newline included). */
|
|
70
|
+
connected?: (facts: ConnectNoticeFacts) => string;
|
|
71
|
+
}
|
|
72
|
+
export interface ConnectOnDemandOptions {
|
|
73
|
+
/** The tool's config namespace (lowercase slug — it names the cache file). */
|
|
74
|
+
tool: string;
|
|
75
|
+
/** The resolved tool instance origin the CLI is talking to. */
|
|
76
|
+
toolBaseUrl: string;
|
|
77
|
+
/** The exchange resource URL; default = this instance's MCP endpoint,
|
|
78
|
+
* `${toolBaseUrl}/mcp` (the tool's registered resource URL). */
|
|
79
|
+
resource?: string;
|
|
80
|
+
/** Target hub org for the exchange (`--org` → `antasphere org use` → the
|
|
81
|
+
* hub key's bound org — resolve via `resolveTargetWorkspace`); omit =
|
|
82
|
+
* the hub defaults to the key's bound org server-side. */
|
|
83
|
+
org?: string;
|
|
84
|
+
/** The tool profile the minted key is cached on; default `DEFAULT_PROFILE`. */
|
|
85
|
+
profileName?: string;
|
|
86
|
+
env: CliEnv;
|
|
87
|
+
fetch?: typeof globalThis.fetch;
|
|
88
|
+
/** Receives the human "Connected to …" notice (tools wire their stderr);
|
|
89
|
+
* omitted = silent. Machine output stays untouched either way. */
|
|
90
|
+
notify?: (line: string) => void;
|
|
91
|
+
messages?: ConnectOnDemandMessages;
|
|
92
|
+
}
|
|
93
|
+
export type ConnectOnDemandOutcome = {
|
|
94
|
+
outcome: 'connected';
|
|
95
|
+
/** The minted tool-local key — this invocation's credential. */
|
|
96
|
+
key: string;
|
|
97
|
+
/** The full exchange result (user, hub org, tool workspace, key meta). */
|
|
98
|
+
result: CliConnectResult;
|
|
99
|
+
/** Whether the key was cached per (tool, hub org) for the next run. */
|
|
100
|
+
cached: boolean;
|
|
101
|
+
} | {
|
|
102
|
+
/** Not an Antasphere-cloud instance (or unreachable): the caller
|
|
103
|
+
* raises its classic no-key error unchanged. */
|
|
104
|
+
outcome: 'not_cloud';
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
108
|
+
* resolved. Returns `not_cloud` when the instance is not an Antasphere-cloud
|
|
109
|
+
* one (or unreachable). Throws a guided `CliUsageError` when the instance IS
|
|
110
|
+
* cloud but the hub login is missing or rejected; every other exchange
|
|
111
|
+
* failure propagates as its `CliConnectError`.
|
|
112
|
+
*/
|
|
113
|
+
export declare function connectOnDemand(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome>;
|
|
114
|
+
//# sourceMappingURL=connect-on-demand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-on-demand.d.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C;IACpB;qEACiE;IACjE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;+DAE2D;IAC3D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,+EAA+E;IAC/E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC;uEACmE;IACnE,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,gEAAgE;IAChE,GAAG,EAAE,MAAM,CAAC;IACZ,0EAA0E;IAC1E,MAAM,EAAE,gBAAgB,CAAC;IACzB,uEAAuE;IACvE,MAAM,EAAE,OAAO,CAAC;CACjB,GACD;IACE;qDACiD;IACjD,OAAO,EAAE,WAAW,CAAC;CACtB,CAAC;AAEN;;;;;;GAMG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CA8EnG"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { assertToolNamespace, HUB_TOOL, loadConfig, saveConfig, storeWorkspaceKey } 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
|
+
function defaultMissingHubLogin(tool) {
|
|
34
|
+
const envVar = `${tool.replace(/-/g, '_').toUpperCase()}_API_KEY`;
|
|
35
|
+
return (`This ${tool} instance signs in through the Antasphere hub. Run \`antasphere login\` once, ` +
|
|
36
|
+
`then retry — or pass --api-key / set ${envVar}.`);
|
|
37
|
+
}
|
|
38
|
+
const DEFAULT_HUB_LOGIN_REJECTED = 'The stored Antasphere hub credential was rejected — run `antasphere login` again, then retry.';
|
|
39
|
+
function defaultConnected(facts) {
|
|
40
|
+
return (`Connected to ${facts.toolBaseUrl} as ${facts.email} via Antasphere (org ${facts.hubWorkspaceId})` +
|
|
41
|
+
(facts.cached
|
|
42
|
+
? ''
|
|
43
|
+
: ` — key NOT cached: profile '${facts.profileName}' points at ${facts.profileBaseUrl}`) +
|
|
44
|
+
'.\n');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
48
|
+
* resolved. Returns `not_cloud` when the instance is not an Antasphere-cloud
|
|
49
|
+
* one (or unreachable). Throws a guided `CliUsageError` when the instance IS
|
|
50
|
+
* cloud but the hub login is missing or rejected; every other exchange
|
|
51
|
+
* failure propagates as its `CliConnectError`.
|
|
52
|
+
*/
|
|
53
|
+
export async function connectOnDemand(opts) {
|
|
54
|
+
assertToolNamespace(opts.tool);
|
|
55
|
+
const toolBaseUrl = opts.toolBaseUrl.replace(/\/+$/, '');
|
|
56
|
+
const messages = opts.messages ?? {};
|
|
57
|
+
// 1. Only a CLOUD instance can mint keys through the hub — ask discovery,
|
|
58
|
+
// never guess. An unreachable or foreign instance is `not_cloud`.
|
|
59
|
+
const probe = await probeToolInstance(toolBaseUrl, opts.fetch);
|
|
60
|
+
if (!probe?.cloud)
|
|
61
|
+
return { outcome: 'not_cloud' };
|
|
62
|
+
// 2. The hub credential is what `antasphere login` stored on the hub
|
|
63
|
+
// profile (HUB_TOOL namespace) — one login for the whole tool family.
|
|
64
|
+
// The hub URL comes from that same profile, never from a constant here.
|
|
65
|
+
const { profile: hubProfile } = resolveProfile(loadConfig(opts.env, HUB_TOOL), undefined);
|
|
66
|
+
const hubKey = hubProfile?.apiKey;
|
|
67
|
+
const hubBaseUrl = hubProfile?.baseUrl;
|
|
68
|
+
if (!hubKey || !hubBaseUrl) {
|
|
69
|
+
throw new CliUsageError(messages.missingHubLogin ?? defaultMissingHubLogin(opts.tool));
|
|
70
|
+
}
|
|
71
|
+
// 3. The exchange resource defaults to THIS instance's registered resource
|
|
72
|
+
// URL — its MCP endpoint, derived from the resolved instance origin.
|
|
73
|
+
const resource = opts.resource ?? `${toolBaseUrl}/mcp`;
|
|
74
|
+
const connect = new CliConnectClient({
|
|
75
|
+
hubBaseUrl,
|
|
76
|
+
toolBaseUrl,
|
|
77
|
+
...(opts.fetch ? { fetch: opts.fetch } : {})
|
|
78
|
+
});
|
|
79
|
+
const body = { resource, ...(opts.org ? { workspaceId: opts.org } : {}) };
|
|
80
|
+
let result;
|
|
81
|
+
try {
|
|
82
|
+
result = await connect.exchange(hubKey, body);
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
if (e instanceof CliConnectError && e.needsReExchange) {
|
|
86
|
+
// The 120 s JWT died in transit (expired/replayed) — one fresh
|
|
87
|
+
// exchange; a second rejection propagates.
|
|
88
|
+
result = await connect.exchange(hubKey, body);
|
|
89
|
+
}
|
|
90
|
+
else if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
91
|
+
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
throw e;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// 4. Cache the minted key per (tool, hub org) — but only onto a profile
|
|
98
|
+
// that names THIS instance (or a fresh one, which gets pinned to it),
|
|
99
|
+
// never onto a profile pointing somewhere else.
|
|
100
|
+
const profileName = opts.profileName ?? DEFAULT_PROFILE;
|
|
101
|
+
const existing = loadConfig(opts.env, opts.tool).profiles[profileName];
|
|
102
|
+
const cached = !existing?.baseUrl || existing.baseUrl.replace(/\/+$/, '') === toolBaseUrl;
|
|
103
|
+
if (cached) {
|
|
104
|
+
storeWorkspaceKey(opts.env, opts.tool, profileName, result.hubWorkspaceId, {
|
|
105
|
+
apiKey: result.key,
|
|
106
|
+
toolWorkspaceId: result.toolWorkspaceId,
|
|
107
|
+
email: result.user.email,
|
|
108
|
+
mintedAt: new Date().toISOString()
|
|
109
|
+
});
|
|
110
|
+
if (!existing?.baseUrl) {
|
|
111
|
+
// First contact: pin the profile to this instance so the next
|
|
112
|
+
// invocation resolves the URL and the cached key with no flags.
|
|
113
|
+
const cfg = loadConfig(opts.env, opts.tool);
|
|
114
|
+
cfg.profiles[profileName] = { ...cfg.profiles[profileName], baseUrl: toolBaseUrl };
|
|
115
|
+
saveConfig(opts.env, opts.tool, cfg);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const facts = {
|
|
119
|
+
toolBaseUrl,
|
|
120
|
+
email: result.user.email,
|
|
121
|
+
hubWorkspaceId: result.hubWorkspaceId,
|
|
122
|
+
cached,
|
|
123
|
+
profileName,
|
|
124
|
+
// `cached` is false only when the profile names another instance.
|
|
125
|
+
...(cached ? {} : { profileBaseUrl: existing?.baseUrl })
|
|
126
|
+
};
|
|
127
|
+
opts.notify?.((messages.connected ?? defaultConnected)(facts));
|
|
128
|
+
return { outcome: 'connected', key: result.key, result, cached };
|
|
129
|
+
}
|
|
130
|
+
//# 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,EAAE,mBAAmB,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEvG,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAqC9E;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,WAAmB,EACnB,SAAmC;IAEnC,MAAM,CAAC,GAAG,SAAS,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IACzD,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,CAAC,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,kBAAkB,EAAE;YACxE,MAAM,EAAE,KAAK;YACb,mEAAmE;YACnE,+DAA+D;YAC/D,KAAK,EAAE,UAAU;SAClB,CAAC,CAAC;QACH,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,OAAO,IAAI,CAAC;QACzB,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IA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;YAClB,eAAe,EAAE,MAAM,CAAC,eAAe;YACvC,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK;YACxB,QAAQ,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;SACnC,CAAC,CAAC;QACH,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE,CAAC;YACvB,8DAA8D;YAC9D,gEAAgE;YAChE,MAAM,GAAG,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAC5C,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC;YACnF,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAuB;QAChC,WAAW;QACX,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK;QACxB,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,MAAM;QACN,WAAW;QACX,kEAAkE;QAClE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;KACzD,CAAC;IACF,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC,SAAS,IAAI,gBAAgB,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAE/D,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AACnE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
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
5
|
* resolution order, the instance email-OTP auth client, the hub→tool
|
|
6
|
-
* cross-tool connect exchange (+ its per-(tool, org) key cache
|
|
7
|
-
* hub-profile org context
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* cross-tool connect exchange (+ its per-(tool, org) key cache, the
|
|
7
|
+
* hub-profile org context, and the connect-on-demand seam a tool CLI runs
|
|
8
|
+
* when no direct credential resolved), and the CliIo prompt seam. Tool
|
|
9
|
+
* commands, wire types, and API surfaces live in the tools; this package
|
|
10
|
+
* carries only config/login plumbing, never secrets.
|
|
10
11
|
*/
|
|
11
12
|
export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, lookupWorkspaceKey, parseConfig, redactKey, removeWorkspaceKey, saveConfig, storeWorkspaceKey } from './config.js';
|
|
12
13
|
export type { CliConfig, CliEnv, CliProfile, CliWorkspaceKey } from './config.js';
|
|
@@ -18,6 +19,8 @@ export { CliAuthClient, CliAuthError } from './auth.js';
|
|
|
18
19
|
export type { CliAuthClientOptions, CliAuthCompleteBody, CliAuthCompleted, CliAuthMintedKey, CliAuthRequestBody, CliAuthRequested, CliAuthRevoked } from './auth.js';
|
|
19
20
|
export { CliConnectClient, CliConnectError } from './connect.js';
|
|
20
21
|
export type { CliConnectClientOptions, CliConnected, CliConnectResult, CliConnectStage, CliToolTokenBody, CliToolTokenMinted } from './connect.js';
|
|
22
|
+
export { connectOnDemand, probeToolInstance } from './connect-on-demand.js';
|
|
23
|
+
export type { ConnectNoticeFacts, ConnectOnDemandMessages, ConnectOnDemandOptions, ConnectOnDemandOutcome, ToolInstanceProbe } from './connect-on-demand.js';
|
|
21
24
|
export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
|
|
22
25
|
export type { OrgContextOptions, ResolveTargetWorkspaceOptions } from './org.js';
|
|
23
26
|
export { migrateLegacyConfig } from './migrate.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;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;AACrB,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAElF,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,YAAY,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC7G,YAAY,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEjG,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,WAAW,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,eAAe,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC5E,YAAY,EACV,kBAAkB,EAClB,uBAAuB,EACvB,sBAAsB,EACtB,sBAAsB,EACtB,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AAEhC,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
|
@@ -3,16 +3,18 @@
|
|
|
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
5
|
* resolution order, the instance email-OTP auth client, the hub→tool
|
|
6
|
-
* cross-tool connect exchange (+ its per-(tool, org) key cache
|
|
7
|
-
* hub-profile org context
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* cross-tool connect exchange (+ its per-(tool, org) key cache, the
|
|
7
|
+
* hub-profile org context, and the connect-on-demand seam a tool CLI runs
|
|
8
|
+
* when no direct credential resolved), and the CliIo prompt seam. Tool
|
|
9
|
+
* commands, wire types, and API surfaces live in the tools; this package
|
|
10
|
+
* carries only config/login plumbing, never secrets.
|
|
10
11
|
*/
|
|
11
12
|
export { assertToolNamespace, clearConfig, configPath, HUB_TOOL, loadConfig, lookupWorkspaceKey, parseConfig, redactKey, removeWorkspaceKey, saveConfig, storeWorkspaceKey } from './config.js';
|
|
12
13
|
export { ask, printJson } from './io.js';
|
|
13
14
|
export { CliUsageError, DEFAULT_PROFILE, resolveApiKey, resolveBaseUrl, resolveProfile } from './resolve.js';
|
|
14
15
|
export { CliAuthClient, CliAuthError } from './auth.js';
|
|
15
16
|
export { CliConnectClient, CliConnectError } from './connect.js';
|
|
17
|
+
export { connectOnDemand, probeToolInstance } from './connect-on-demand.js';
|
|
16
18
|
export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
|
|
17
19
|
export { migrateLegacyConfig } from './migrate.js';
|
|
18
20
|
//# 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
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;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,eAAe,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAS5E,OAAO,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAG1F,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antasphere/cli-core",
|
|
3
|
-
"version": "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",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Shared CLI foundation for Antasphere tool CLIs: the ~/.config/antasphere config home, tool-namespaced profiles, URL resolution, the instance email-OTP auth client, the hub→tool cross-tool connect exchange with its connect-on-demand seam, and the CliIo prompt seam",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { assertToolNamespace, HUB_TOOL, loadConfig, saveConfig, storeWorkspaceKey } from './config.js';
|
|
2
|
+
import type { CliEnv } from './config.js';
|
|
3
|
+
import { CliConnectClient, CliConnectError } from './connect.js';
|
|
4
|
+
import type { CliConnectResult } from './connect.js';
|
|
5
|
+
import { CliUsageError, DEFAULT_PROFILE, resolveProfile } from './resolve.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Connect-on-demand (binding patterns §7, gcloud model), extracted from the
|
|
9
|
+
* Slideless CLI so every Antasphere tool CLI inherits "one `antasphere login`
|
|
10
|
+
* serves every federated tool". When a tool CLI resolves NO credential of its
|
|
11
|
+
* own, it calls `connectOnDemand`:
|
|
12
|
+
*
|
|
13
|
+
* 1. Discovery gates the flow: only an Antasphere-CLOUD instance can mint
|
|
14
|
+
* keys through the hub (unauthenticated GET /instance — never guess).
|
|
15
|
+
* Unreachable, foreign, or self-hosted instances answer `not_cloud`
|
|
16
|
+
* and the tool raises its classic no-key error unchanged.
|
|
17
|
+
* 2. The hub credential is what `antasphere login` stored on the hub
|
|
18
|
+
* profile (the config home's HUB_TOOL namespace) — hub URL included,
|
|
19
|
+
* never a hardcoded constant here.
|
|
20
|
+
* 3. `CliConnectClient.exchange` trades it for a tool-local key (one
|
|
21
|
+
* re-exchange retry when the 120 s JWT dies in transit; a rejected hub
|
|
22
|
+
* credential maps to guided `antasphere login` advice).
|
|
23
|
+
* 4. The minted key is cached per (tool, HUB org) — but only onto a
|
|
24
|
+
* profile that names THIS instance (a fresh profile gets pinned to it);
|
|
25
|
+
* a profile pointing elsewhere never receives it.
|
|
26
|
+
*
|
|
27
|
+
* A new tool needs only its namespace, base URL, and copy — the flow itself
|
|
28
|
+
* is tool-agnostic.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** What the unauthenticated discovery probe learned about a tool instance. */
|
|
32
|
+
export interface ToolInstanceProbe {
|
|
33
|
+
/** Whether the instance signs in through the Antasphere hub (advertises
|
|
34
|
+
* the `antasphere` auth method, or calls itself the cloud edition). */
|
|
35
|
+
cloud: boolean;
|
|
36
|
+
/** The instance's self-reported edition (`cloud`, `oss`, …). */
|
|
37
|
+
edition: string | undefined;
|
|
38
|
+
/** The advertised auth methods (open enum — unknown entries kept). */
|
|
39
|
+
authMethods: string[];
|
|
40
|
+
}
|
|
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 async function probeToolInstance(
|
|
49
|
+
toolBaseUrl: string,
|
|
50
|
+
fetchImpl?: typeof globalThis.fetch
|
|
51
|
+
): Promise<ToolInstanceProbe | null> {
|
|
52
|
+
const f = fetchImpl ?? globalThis.fetch.bind(globalThis);
|
|
53
|
+
try {
|
|
54
|
+
const res = await f(`${toolBaseUrl.replace(/\/+$/, '')}/api/v1/instance`, {
|
|
55
|
+
method: 'GET',
|
|
56
|
+
// Discovery is served cacheable for CLI/MCP clients, but the probe
|
|
57
|
+
// must see live state (mirrors the tool SDKs' discovery call).
|
|
58
|
+
cache: 'no-store'
|
|
59
|
+
});
|
|
60
|
+
if (!res.ok) return null;
|
|
61
|
+
const info = (await res.json().catch(() => null)) as {
|
|
62
|
+
edition?: unknown;
|
|
63
|
+
auth?: { methods?: unknown };
|
|
64
|
+
} | null;
|
|
65
|
+
const methods = info?.auth?.methods;
|
|
66
|
+
if (!Array.isArray(methods)) return null;
|
|
67
|
+
const authMethods = methods.filter((m): m is string => typeof m === 'string');
|
|
68
|
+
const edition = typeof info?.edition === 'string' ? info.edition : undefined;
|
|
69
|
+
return { cloud: authMethods.includes('antasphere') || edition === 'cloud', edition, authMethods };
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The facts the connected notice is rendered from. */
|
|
76
|
+
export interface ConnectNoticeFacts {
|
|
77
|
+
/** The tool instance the exchange connected to (origin-normalized). */
|
|
78
|
+
toolBaseUrl: string;
|
|
79
|
+
/** The signed-in account's email at mint. */
|
|
80
|
+
email: string;
|
|
81
|
+
/** The HUB org the exchange resolved to. */
|
|
82
|
+
hubWorkspaceId: string;
|
|
83
|
+
/** Whether the minted key was cached (per (tool, hub org)). */
|
|
84
|
+
cached: boolean;
|
|
85
|
+
/** The tool profile the cache targeted. */
|
|
86
|
+
profileName: string;
|
|
87
|
+
/** When NOT cached: the other instance the profile points at. */
|
|
88
|
+
profileBaseUrl?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Branded copy seams. Defaults reproduce the Slideless CLI's strings modulo
|
|
93
|
+
* the tool name (the tool namespace stands in for the display name, and the
|
|
94
|
+
* key-env-var name is derived from it: `slideless` → `SLIDELESS_API_KEY`).
|
|
95
|
+
*/
|
|
96
|
+
export interface ConnectOnDemandMessages {
|
|
97
|
+
/** `CliUsageError` text when the instance is cloud but no `antasphere
|
|
98
|
+
* login` credential is stored on the hub profile. */
|
|
99
|
+
missingHubLogin?: string;
|
|
100
|
+
/** `CliUsageError` text when the hub rejected the stored credential. */
|
|
101
|
+
hubLoginRejected?: string;
|
|
102
|
+
/** Renders the success notice handed to `notify` (newline included). */
|
|
103
|
+
connected?: (facts: ConnectNoticeFacts) => string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function defaultMissingHubLogin(tool: string): string {
|
|
107
|
+
const envVar = `${tool.replace(/-/g, '_').toUpperCase()}_API_KEY`;
|
|
108
|
+
return (
|
|
109
|
+
`This ${tool} instance signs in through the Antasphere hub. Run \`antasphere login\` once, ` +
|
|
110
|
+
`then retry — or pass --api-key / set ${envVar}.`
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const DEFAULT_HUB_LOGIN_REJECTED =
|
|
115
|
+
'The stored Antasphere hub credential was rejected — run `antasphere login` again, then retry.';
|
|
116
|
+
|
|
117
|
+
function defaultConnected(facts: ConnectNoticeFacts): string {
|
|
118
|
+
return (
|
|
119
|
+
`Connected to ${facts.toolBaseUrl} as ${facts.email} via Antasphere (org ${facts.hubWorkspaceId})` +
|
|
120
|
+
(facts.cached
|
|
121
|
+
? ''
|
|
122
|
+
: ` — key NOT cached: profile '${facts.profileName}' points at ${facts.profileBaseUrl}`) +
|
|
123
|
+
'.\n'
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface ConnectOnDemandOptions {
|
|
128
|
+
/** The tool's config namespace (lowercase slug — it names the cache file). */
|
|
129
|
+
tool: string;
|
|
130
|
+
/** The resolved tool instance origin the CLI is talking to. */
|
|
131
|
+
toolBaseUrl: string;
|
|
132
|
+
/** The exchange resource URL; default = this instance's MCP endpoint,
|
|
133
|
+
* `${toolBaseUrl}/mcp` (the tool's registered resource URL). */
|
|
134
|
+
resource?: string;
|
|
135
|
+
/** Target hub org for the exchange (`--org` → `antasphere org use` → the
|
|
136
|
+
* hub key's bound org — resolve via `resolveTargetWorkspace`); omit =
|
|
137
|
+
* the hub defaults to the key's bound org server-side. */
|
|
138
|
+
org?: string;
|
|
139
|
+
/** The tool profile the minted key is cached on; default `DEFAULT_PROFILE`. */
|
|
140
|
+
profileName?: string;
|
|
141
|
+
env: CliEnv;
|
|
142
|
+
fetch?: typeof globalThis.fetch;
|
|
143
|
+
/** Receives the human "Connected to …" notice (tools wire their stderr);
|
|
144
|
+
* omitted = silent. Machine output stays untouched either way. */
|
|
145
|
+
notify?: (line: string) => void;
|
|
146
|
+
messages?: ConnectOnDemandMessages;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export type ConnectOnDemandOutcome =
|
|
150
|
+
| {
|
|
151
|
+
outcome: 'connected';
|
|
152
|
+
/** The minted tool-local key — this invocation's credential. */
|
|
153
|
+
key: string;
|
|
154
|
+
/** The full exchange result (user, hub org, tool workspace, key meta). */
|
|
155
|
+
result: CliConnectResult;
|
|
156
|
+
/** Whether the key was cached per (tool, hub org) for the next run. */
|
|
157
|
+
cached: boolean;
|
|
158
|
+
}
|
|
159
|
+
| {
|
|
160
|
+
/** Not an Antasphere-cloud instance (or unreachable): the caller
|
|
161
|
+
* raises its classic no-key error unchanged. */
|
|
162
|
+
outcome: 'not_cloud';
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* The hub → tool exchange fallback a tool CLI runs when no direct credential
|
|
167
|
+
* resolved. Returns `not_cloud` when the instance is not an Antasphere-cloud
|
|
168
|
+
* one (or unreachable). Throws a guided `CliUsageError` when the instance IS
|
|
169
|
+
* cloud but the hub login is missing or rejected; every other exchange
|
|
170
|
+
* failure propagates as its `CliConnectError`.
|
|
171
|
+
*/
|
|
172
|
+
export async function connectOnDemand(opts: ConnectOnDemandOptions): Promise<ConnectOnDemandOutcome> {
|
|
173
|
+
assertToolNamespace(opts.tool);
|
|
174
|
+
const toolBaseUrl = opts.toolBaseUrl.replace(/\/+$/, '');
|
|
175
|
+
const messages = opts.messages ?? {};
|
|
176
|
+
|
|
177
|
+
// 1. Only a CLOUD instance can mint keys through the hub — ask discovery,
|
|
178
|
+
// never guess. An unreachable or foreign instance is `not_cloud`.
|
|
179
|
+
const probe = await probeToolInstance(toolBaseUrl, opts.fetch);
|
|
180
|
+
if (!probe?.cloud) return { outcome: 'not_cloud' };
|
|
181
|
+
|
|
182
|
+
// 2. The hub credential is what `antasphere login` stored on the hub
|
|
183
|
+
// profile (HUB_TOOL namespace) — one login for the whole tool family.
|
|
184
|
+
// The hub URL comes from that same profile, never from a constant here.
|
|
185
|
+
const { profile: hubProfile } = resolveProfile(loadConfig(opts.env, HUB_TOOL), undefined);
|
|
186
|
+
const hubKey = hubProfile?.apiKey;
|
|
187
|
+
const hubBaseUrl = hubProfile?.baseUrl;
|
|
188
|
+
if (!hubKey || !hubBaseUrl) {
|
|
189
|
+
throw new CliUsageError(messages.missingHubLogin ?? defaultMissingHubLogin(opts.tool));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 3. The exchange resource defaults to THIS instance's registered resource
|
|
193
|
+
// URL — its MCP endpoint, derived from the resolved instance origin.
|
|
194
|
+
const resource = opts.resource ?? `${toolBaseUrl}/mcp`;
|
|
195
|
+
const connect = new CliConnectClient({
|
|
196
|
+
hubBaseUrl,
|
|
197
|
+
toolBaseUrl,
|
|
198
|
+
...(opts.fetch ? { fetch: opts.fetch } : {})
|
|
199
|
+
});
|
|
200
|
+
const body = { resource, ...(opts.org ? { workspaceId: opts.org } : {}) };
|
|
201
|
+
let result: CliConnectResult;
|
|
202
|
+
try {
|
|
203
|
+
result = await connect.exchange(hubKey, body);
|
|
204
|
+
} catch (e) {
|
|
205
|
+
if (e instanceof CliConnectError && e.needsReExchange) {
|
|
206
|
+
// The 120 s JWT died in transit (expired/replayed) — one fresh
|
|
207
|
+
// exchange; a second rejection propagates.
|
|
208
|
+
result = await connect.exchange(hubKey, body);
|
|
209
|
+
} else if (e instanceof CliConnectError && e.needsHubLogin) {
|
|
210
|
+
throw new CliUsageError(messages.hubLoginRejected ?? DEFAULT_HUB_LOGIN_REJECTED);
|
|
211
|
+
} else {
|
|
212
|
+
throw e;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// 4. Cache the minted key per (tool, hub org) — but only onto a profile
|
|
217
|
+
// that names THIS instance (or a fresh one, which gets pinned to it),
|
|
218
|
+
// never onto a profile pointing somewhere else.
|
|
219
|
+
const profileName = opts.profileName ?? DEFAULT_PROFILE;
|
|
220
|
+
const existing = loadConfig(opts.env, opts.tool).profiles[profileName];
|
|
221
|
+
const cached = !existing?.baseUrl || existing.baseUrl.replace(/\/+$/, '') === toolBaseUrl;
|
|
222
|
+
if (cached) {
|
|
223
|
+
storeWorkspaceKey(opts.env, opts.tool, profileName, result.hubWorkspaceId, {
|
|
224
|
+
apiKey: result.key,
|
|
225
|
+
toolWorkspaceId: result.toolWorkspaceId,
|
|
226
|
+
email: result.user.email,
|
|
227
|
+
mintedAt: new Date().toISOString()
|
|
228
|
+
});
|
|
229
|
+
if (!existing?.baseUrl) {
|
|
230
|
+
// First contact: pin the profile to this instance so the next
|
|
231
|
+
// invocation resolves the URL and the cached key with no flags.
|
|
232
|
+
const cfg = loadConfig(opts.env, opts.tool);
|
|
233
|
+
cfg.profiles[profileName] = { ...cfg.profiles[profileName], baseUrl: toolBaseUrl };
|
|
234
|
+
saveConfig(opts.env, opts.tool, cfg);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const facts: ConnectNoticeFacts = {
|
|
239
|
+
toolBaseUrl,
|
|
240
|
+
email: result.user.email,
|
|
241
|
+
hubWorkspaceId: result.hubWorkspaceId,
|
|
242
|
+
cached,
|
|
243
|
+
profileName,
|
|
244
|
+
// `cached` is false only when the profile names another instance.
|
|
245
|
+
...(cached ? {} : { profileBaseUrl: existing?.baseUrl })
|
|
246
|
+
};
|
|
247
|
+
opts.notify?.((messages.connected ?? defaultConnected)(facts));
|
|
248
|
+
|
|
249
|
+
return { outcome: 'connected', key: result.key, result, cached };
|
|
250
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -3,10 +3,11 @@
|
|
|
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
5
|
* resolution order, the instance email-OTP auth client, the hub→tool
|
|
6
|
-
* cross-tool connect exchange (+ its per-(tool, org) key cache
|
|
7
|
-
* hub-profile org context
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* cross-tool connect exchange (+ its per-(tool, org) key cache, the
|
|
7
|
+
* hub-profile org context, and the connect-on-demand seam a tool CLI runs
|
|
8
|
+
* when no direct credential resolved), and the CliIo prompt seam. Tool
|
|
9
|
+
* commands, wire types, and API surfaces live in the tools; this package
|
|
10
|
+
* carries only config/login plumbing, never secrets.
|
|
10
11
|
*/
|
|
11
12
|
|
|
12
13
|
export {
|
|
@@ -51,6 +52,15 @@ export type {
|
|
|
51
52
|
CliToolTokenMinted
|
|
52
53
|
} from './connect.js';
|
|
53
54
|
|
|
55
|
+
export { connectOnDemand, probeToolInstance } from './connect-on-demand.js';
|
|
56
|
+
export type {
|
|
57
|
+
ConnectNoticeFacts,
|
|
58
|
+
ConnectOnDemandMessages,
|
|
59
|
+
ConnectOnDemandOptions,
|
|
60
|
+
ConnectOnDemandOutcome,
|
|
61
|
+
ToolInstanceProbe
|
|
62
|
+
} from './connect-on-demand.js';
|
|
63
|
+
|
|
54
64
|
export { getActiveWorkspace, resolveTargetWorkspace, setActiveWorkspace } from './org.js';
|
|
55
65
|
export type { OrgContextOptions, ResolveTargetWorkspaceOptions } from './org.js';
|
|
56
66
|
|