@kodycodes/cli 1.17.0 → 1.19.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.
Files changed (79) hide show
  1. package/README.md +51 -5
  2. package/dist/auth.d.ts +30 -4
  3. package/dist/auth.js +94 -13
  4. package/dist/auth.js.map +1 -1
  5. package/dist/capability-proxy.d.ts +7 -0
  6. package/dist/capability-proxy.js +8 -2
  7. package/dist/capability-proxy.js.map +1 -1
  8. package/dist/cli.d.ts +9 -1
  9. package/dist/cli.js +426 -100
  10. package/dist/cli.js.map +1 -1
  11. package/dist/dev-command.d.ts +93 -0
  12. package/dist/dev-command.js +369 -0
  13. package/dist/dev-command.js.map +1 -0
  14. package/dist/discover-org.d.ts +10 -0
  15. package/dist/discover-org.js +62 -0
  16. package/dist/discover-org.js.map +1 -0
  17. package/dist/help.d.ts +13 -1
  18. package/dist/help.js +112 -15
  19. package/dist/help.js.map +1 -1
  20. package/dist/local-execute-auth.d.ts +1 -0
  21. package/dist/local-execute-auth.js +7 -1
  22. package/dist/local-execute-auth.js.map +1 -1
  23. package/dist/local-execute.d.ts +2 -0
  24. package/dist/local-execute.js +6 -1
  25. package/dist/local-execute.js.map +1 -1
  26. package/dist/local-package-graph.js +2 -0
  27. package/dist/local-package-graph.js.map +1 -1
  28. package/dist/local-package-run.d.ts +28 -0
  29. package/dist/local-package-run.js +299 -0
  30. package/dist/local-package-run.js.map +1 -0
  31. package/dist/mcp.d.ts +4 -0
  32. package/dist/mcp.js +3 -0
  33. package/dist/mcp.js.map +1 -1
  34. package/dist/mock/mock-server.d.ts +42 -0
  35. package/dist/mock/mock-server.js +373 -0
  36. package/dist/mock/mock-server.js.map +1 -0
  37. package/dist/mock/mock-storage.d.ts +46 -0
  38. package/dist/mock/mock-storage.js +96 -0
  39. package/dist/mock/mock-storage.js.map +1 -0
  40. package/dist/mock/mock-supported.d.ts +18 -0
  41. package/dist/mock/mock-supported.js +43 -0
  42. package/dist/mock/mock-supported.js.map +1 -0
  43. package/dist/oauth-provider.d.ts +1 -2
  44. package/dist/oauth-provider.js +2 -8
  45. package/dist/oauth-provider.js.map +1 -1
  46. package/dist/open-api-client.d.ts +5 -0
  47. package/dist/open-api-client.js +4 -0
  48. package/dist/open-api-client.js.map +1 -1
  49. package/dist/org-resolve.d.ts +9 -0
  50. package/dist/org-resolve.js +16 -0
  51. package/dist/org-resolve.js.map +1 -0
  52. package/dist/org-slug.d.ts +4 -0
  53. package/dist/org-slug.js +11 -0
  54. package/dist/org-slug.js.map +1 -0
  55. package/dist/org.d.ts +22 -0
  56. package/dist/org.js +100 -0
  57. package/dist/org.js.map +1 -0
  58. package/dist/package-app-dev.d.ts +43 -0
  59. package/dist/package-app-dev.js +356 -0
  60. package/dist/package-app-dev.js.map +1 -0
  61. package/dist/package-local-runtime-source.d.ts +35 -0
  62. package/dist/package-local-runtime-source.js +317 -0
  63. package/dist/package-local-runtime-source.js.map +1 -0
  64. package/dist/package-manifest.d.ts +43 -0
  65. package/dist/package-manifest.js +264 -0
  66. package/dist/package-manifest.js.map +1 -0
  67. package/dist/remote-execute.d.ts +2 -0
  68. package/dist/remote-execute.js +6 -1
  69. package/dist/remote-execute.js.map +1 -1
  70. package/dist/store.d.ts +93 -3
  71. package/dist/store.js +454 -23
  72. package/dist/store.js.map +1 -1
  73. package/dist/structured-error.d.ts +28 -0
  74. package/dist/structured-error.js +57 -0
  75. package/dist/structured-error.js.map +1 -0
  76. package/dist/target.d.ts +25 -0
  77. package/dist/target.js +45 -0
  78. package/dist/target.js.map +1 -0
  79. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"remote-execute.js","sourceRoot":"","sources":["../src/remote-execute.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,qBAAqB,EACrB,mBAAmB,EACnB,0BAA0B,GAC1B,MAAM,uBAAuB,CAAA;AAG9B,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAA;AAiBlE,MAAM,CAAC,MAAM,u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AAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,EAAE,EAAE,CAAC;YACpD,iBAAiB,EAAE,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE;YAC5D,OAAO,EAAE,IAAI;SACb,CAAA;IACF,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAA;IAC5B,MAAM,IAAI,GACT,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,WAAW,CAAC,CAAA;IACvF,OAAO;QACN,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QACjC,iBAAiB,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE;QACtC,OAAO,EAAE,KAAK;KACd,CAAA;AACF,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC/B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC5E,CAAC"}
package/dist/store.d.ts CHANGED
@@ -1,6 +1,39 @@
1
+ /**
2
+ * Per-org OAuth credentials used by login / MCP / local-execute Bearer.
3
+ * `org` is the bound organization slug (ADR 0064). Legacy single-org payloads
4
+ * may omit it until silent migration discovers the slug via metaGetCurrentUser.
5
+ */
1
6
  export type StoredCredentials = {
2
7
  version: 1;
3
8
  mcpUrl: string;
9
+ /** Organization slug this grant is bound to. */
10
+ org?: string;
11
+ resource: string;
12
+ authorizationServerUrl: string;
13
+ clientId: string;
14
+ clientSecret?: string;
15
+ accessToken: string;
16
+ refreshToken?: string;
17
+ tokenType: string;
18
+ expiresAt?: number;
19
+ scope?: string;
20
+ };
21
+ /** On-disk / keychain envelope for one or more org logins (same MCP origin). */
22
+ export type MultiOrgCredentialStore = {
23
+ version: 2;
24
+ mcpUrl: string;
25
+ /** Active org slug; null while a legacy v1 session awaits slug discovery. */
26
+ activeOrg: string | null;
27
+ /** Org slug → credentials (without version/mcpUrl envelope fields). */
28
+ orgs: Record<string, OrgCredentialRecord>;
29
+ /**
30
+ * Legacy single-org tokens waiting for an org slug. At most one. Cleared
31
+ * once metaGetCurrentUser (or login --org) promotes them into `orgs`.
32
+ */
33
+ pending?: OrgCredentialRecord;
34
+ };
35
+ export type OrgCredentialRecord = {
36
+ org?: string;
4
37
  resource: string;
5
38
  authorizationServerUrl: string;
6
39
  clientId: string;
@@ -28,17 +61,74 @@ export type StoreResolution = {
28
61
  createKeyring?: (mcpUrl: string) => SecretBackend;
29
62
  fileStorePath?: (mcpUrl: string) => string;
30
63
  };
64
+ export type LoadCredentialsOptions = {
65
+ /** Prefer this org slug (already normalized). */
66
+ org?: string;
67
+ backend?: SecretBackend;
68
+ resolution?: StoreResolution;
69
+ };
31
70
  export declare function accountForMcpUrl(mcpUrl: string): string;
32
71
  export declare function fileStorePath(mcpUrl: string, home?: string): string;
33
72
  export declare function createFileBackend(path: string): SecretBackend;
34
73
  export declare function createKeyringBackend(mcpUrl: string): SecretBackend;
35
74
  export declare function resolveBackend(mcpUrl: string, preferred?: SecretBackend, resolution?: StoreResolution): SecretBackend;
75
+ /** Parse v1 single-org or v2 multi-org JSON from the credential backend. */
76
+ export declare function parseCredentialStore(raw: string, fallbackMcpUrl?: string): MultiOrgCredentialStore;
77
+ /**
78
+ * Silent offline migration: wrap a v1 single-org payload as a multi-org store.
79
+ * When `org` is already known, it becomes the active org; otherwise tokens sit
80
+ * in `pending` until metaGetCurrentUser (or login) supplies the slug.
81
+ */
82
+ export declare function migrateV1ToMultiOrg(credentials: StoredCredentials): MultiOrgCredentialStore;
83
+ /** @deprecated Prefer parseCredentialStore; kept for tests and callers that expect v1. */
36
84
  export declare function parseCredentials(raw: string): StoredCredentials;
37
- export declare function loadCredentials(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution): StoredCredentials | null;
38
- export declare function saveCredentials(credentials: StoredCredentials, backend?: SecretBackend, resolution?: StoreResolution): {
85
+ export declare function loadCredentialStore(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution): MultiOrgCredentialStore | null;
86
+ export declare function saveCredentialStore(snapshot: MultiOrgCredentialStore, backend?: SecretBackend, resolution?: StoreResolution): {
87
+ backend: SecretBackend;
88
+ };
89
+ /**
90
+ * Resolve one org's credentials. Precedence for which org: explicit `org`
91
+ * option, else the store's active org, else the sole pending legacy session.
92
+ */
93
+ export declare function loadCredentials(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution, options?: {
94
+ org?: string;
95
+ }): StoredCredentials | null;
96
+ /** Legacy pending session only (not the active named org). */
97
+ export declare function loadPendingCredentials(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution): StoredCredentials | null;
98
+ export declare function pickCredentialsFromStore(snapshot: MultiOrgCredentialStore, org?: string): StoredCredentials | null;
99
+ /**
100
+ * Upsert one org's credentials. Replaces that org's prior tokens when the slug
101
+ * already exists; leaves other orgs untouched.
102
+ *
103
+ * `activate` (default true) makes the saved org active — used by `kody login`.
104
+ * Pass `activate: false` for token refresh so a one-shot `--org` / `KODY_ORG`
105
+ * refresh does not steal the active org from `kody org use`.
106
+ */
107
+ export declare function saveCredentials(credentials: StoredCredentials, backend?: SecretBackend, resolution?: StoreResolution, options?: {
108
+ activate?: boolean;
109
+ }): {
39
110
  backend: SecretBackend;
40
111
  };
41
- export declare function deleteCredentials(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution): {
112
+ /**
113
+ * Promote pending legacy tokens into `orgs[slug]`.
114
+ * Never overwrites an existing named login (a fresh `kody login` wins), and
115
+ * never steals `activeOrg` away from an already-active named org.
116
+ */
117
+ export declare function promotePendingOrg(mcpUrl: string, org: string, backend?: SecretBackend, resolution?: StoreResolution): MultiOrgCredentialStore | null;
118
+ export declare function setActiveOrg(mcpUrl: string, org: string, backend?: SecretBackend, resolution?: StoreResolution): MultiOrgCredentialStore;
119
+ export declare function listOrgSlugs(snapshot: MultiOrgCredentialStore): Array<string>;
120
+ export type ListedOrg = {
121
+ slug: string;
122
+ active: boolean;
123
+ /** True when this row is the unresolved legacy session (no slug yet). */
124
+ pending?: boolean;
125
+ };
126
+ /** Local-only listing for help / `org list` (no network). */
127
+ export declare function listStoredOrgs(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution): Array<ListedOrg>;
128
+ export declare function deleteCredentials(mcpUrl?: string, backend?: SecretBackend, resolution?: StoreResolution, options?: {
129
+ org?: string;
130
+ }): {
42
131
  deleted: boolean;
43
132
  backend: SecretBackend;
133
+ remaining: number;
44
134
  };
package/dist/store.js CHANGED
@@ -91,36 +91,198 @@ export function resolveBackend(mcpUrl, preferred, resolution) {
91
91
  return fileBackendFor(mcpUrl, resolution);
92
92
  }
93
93
  }
94
- export function parseCredentials(raw) {
95
- const parsed = JSON.parse(raw);
96
- if (parsed.version !== 1 || !parsed.accessToken || !parsed.clientId) {
97
- throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
98
- }
94
+ function withDerivedIssuer(parsed) {
99
95
  // Pre-SEP-2352 / partial payloads may omit authorizationServerUrl. Derive
100
96
  // the issuer stamp from the MCP origin so reload does not warn, and the
101
97
  // next successful save/refresh persists the real AS issuer.
102
- if (typeof parsed.authorizationServerUrl !== 'string' ||
103
- !parsed.authorizationServerUrl.trim()) {
104
- if (typeof parsed.mcpUrl !== 'string' || !parsed.mcpUrl) {
98
+ if (typeof parsed.authorizationServerUrl === 'string' &&
99
+ parsed.authorizationServerUrl.trim()) {
100
+ return parsed;
101
+ }
102
+ if (typeof parsed.mcpUrl !== 'string' || !parsed.mcpUrl) {
103
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
104
+ }
105
+ return {
106
+ ...parsed,
107
+ authorizationServerUrl: new URL(parsed.mcpUrl).origin,
108
+ };
109
+ }
110
+ function recordToCredentials(mcpUrl, record, org) {
111
+ const slug = org ?? record.org;
112
+ return withDerivedIssuer({
113
+ version: 1,
114
+ mcpUrl,
115
+ ...(slug ? { org: slug } : {}),
116
+ resource: record.resource,
117
+ authorizationServerUrl: record.authorizationServerUrl,
118
+ clientId: record.clientId,
119
+ ...(record.clientSecret !== undefined
120
+ ? { clientSecret: record.clientSecret }
121
+ : {}),
122
+ accessToken: record.accessToken,
123
+ ...(record.refreshToken !== undefined
124
+ ? { refreshToken: record.refreshToken }
125
+ : {}),
126
+ tokenType: record.tokenType,
127
+ ...(record.expiresAt !== undefined ? { expiresAt: record.expiresAt } : {}),
128
+ ...(record.scope !== undefined ? { scope: record.scope } : {}),
129
+ });
130
+ }
131
+ function credentialsToRecord(credentials) {
132
+ return {
133
+ ...(credentials.org ? { org: credentials.org } : {}),
134
+ resource: credentials.resource,
135
+ authorizationServerUrl: credentials.authorizationServerUrl,
136
+ clientId: credentials.clientId,
137
+ ...(credentials.clientSecret !== undefined
138
+ ? { clientSecret: credentials.clientSecret }
139
+ : {}),
140
+ accessToken: credentials.accessToken,
141
+ ...(credentials.refreshToken !== undefined
142
+ ? { refreshToken: credentials.refreshToken }
143
+ : {}),
144
+ tokenType: credentials.tokenType,
145
+ ...(credentials.expiresAt !== undefined
146
+ ? { expiresAt: credentials.expiresAt }
147
+ : {}),
148
+ ...(credentials.scope !== undefined ? { scope: credentials.scope } : {}),
149
+ };
150
+ }
151
+ function isRecord(value) {
152
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
153
+ }
154
+ function parseOrgRecord(value) {
155
+ if (!isRecord(value))
156
+ return null;
157
+ if (typeof value.accessToken !== 'string' || !value.accessToken)
158
+ return null;
159
+ if (typeof value.clientId !== 'string' || !value.clientId)
160
+ return null;
161
+ if (typeof value.resource !== 'string' || !value.resource)
162
+ return null;
163
+ if (typeof value.tokenType !== 'string' || !value.tokenType)
164
+ return null;
165
+ const authorizationServerUrl = typeof value.authorizationServerUrl === 'string'
166
+ ? value.authorizationServerUrl
167
+ : '';
168
+ return {
169
+ ...(typeof value.org === 'string' && value.org.trim()
170
+ ? { org: value.org.trim().toLowerCase() }
171
+ : {}),
172
+ resource: value.resource,
173
+ authorizationServerUrl,
174
+ clientId: value.clientId,
175
+ ...(typeof value.clientSecret === 'string'
176
+ ? { clientSecret: value.clientSecret }
177
+ : {}),
178
+ accessToken: value.accessToken,
179
+ ...(typeof value.refreshToken === 'string'
180
+ ? { refreshToken: value.refreshToken }
181
+ : {}),
182
+ tokenType: value.tokenType,
183
+ ...(typeof value.expiresAt === 'number' ? { expiresAt: value.expiresAt } : {}),
184
+ ...(typeof value.scope === 'string' ? { scope: value.scope } : {}),
185
+ };
186
+ }
187
+ /** Parse v1 single-org or v2 multi-org JSON from the credential backend. */
188
+ export function parseCredentialStore(raw, fallbackMcpUrl = defaultMcpUrl) {
189
+ const parsed = JSON.parse(raw);
190
+ if (!isRecord(parsed) || typeof parsed.version !== 'number') {
191
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
192
+ }
193
+ if (parsed.version === 2) {
194
+ const mcpUrl = typeof parsed.mcpUrl === 'string' && parsed.mcpUrl
195
+ ? parsed.mcpUrl
196
+ : fallbackMcpUrl;
197
+ const orgs = {};
198
+ if (isRecord(parsed.orgs)) {
199
+ for (const [key, value] of Object.entries(parsed.orgs)) {
200
+ const record = parseOrgRecord(value);
201
+ if (!record) {
202
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
203
+ }
204
+ const slug = (record.org ?? key).trim().toLowerCase();
205
+ if (!slug) {
206
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
207
+ }
208
+ orgs[slug] = { ...record, org: slug };
209
+ }
210
+ }
211
+ const pending = parsed.pending !== undefined ? parseOrgRecord(parsed.pending) : undefined;
212
+ if (parsed.pending !== undefined && !pending) {
105
213
  throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
106
214
  }
215
+ const activeOrgRaw = typeof parsed.activeOrg === 'string' ? parsed.activeOrg.trim().toLowerCase() : null;
216
+ const activeOrg = activeOrgRaw && orgs[activeOrgRaw]
217
+ ? activeOrgRaw
218
+ : Object.keys(orgs).sort((a, b) => a.localeCompare(b))[0] ?? null;
107
219
  return {
108
- ...parsed,
109
- authorizationServerUrl: new URL(parsed.mcpUrl).origin,
220
+ version: 2,
221
+ mcpUrl,
222
+ activeOrg,
223
+ orgs,
224
+ ...(pending ? { pending } : {}),
110
225
  };
111
226
  }
112
- return parsed;
227
+ if (parsed.version === 1) {
228
+ const credentials = parseCredentials(JSON.stringify(parsed));
229
+ return migrateV1ToMultiOrg(credentials);
230
+ }
231
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
113
232
  }
114
- function readParsed(store) {
233
+ /**
234
+ * Silent offline migration: wrap a v1 single-org payload as a multi-org store.
235
+ * When `org` is already known, it becomes the active org; otherwise tokens sit
236
+ * in `pending` until metaGetCurrentUser (or login) supplies the slug.
237
+ */
238
+ export function migrateV1ToMultiOrg(credentials) {
239
+ const record = credentialsToRecord(withDerivedIssuer(credentials));
240
+ const slug = typeof credentials.org === 'string' && credentials.org.trim()
241
+ ? credentials.org.trim().toLowerCase()
242
+ : null;
243
+ if (slug) {
244
+ return {
245
+ version: 2,
246
+ mcpUrl: credentials.mcpUrl,
247
+ activeOrg: slug,
248
+ orgs: { [slug]: { ...record, org: slug } },
249
+ };
250
+ }
251
+ return {
252
+ version: 2,
253
+ mcpUrl: credentials.mcpUrl,
254
+ activeOrg: null,
255
+ orgs: {},
256
+ pending: record,
257
+ };
258
+ }
259
+ /** @deprecated Prefer parseCredentialStore; kept for tests and callers that expect v1. */
260
+ export function parseCredentials(raw) {
261
+ const parsed = JSON.parse(raw);
262
+ if (parsed.version !== 1 || !parsed.accessToken || !parsed.clientId) {
263
+ throw new Error('Stored Kody credentials are invalid. Run `kody login`.');
264
+ }
265
+ const withOrg = typeof parsed.org === 'string' && parsed.org.trim()
266
+ ? { ...parsed, org: parsed.org.trim().toLowerCase() }
267
+ : parsed;
268
+ return withDerivedIssuer(withOrg);
269
+ }
270
+ function readStore(store, mcpUrl) {
115
271
  const raw = store.get();
116
272
  if (!raw)
117
273
  return null;
118
- return parseCredentials(raw);
274
+ return parseCredentialStore(raw, mcpUrl);
119
275
  }
120
- export function loadCredentials(mcpUrl = defaultMcpUrl, backend, resolution) {
276
+ function writeStore(store, snapshot) {
277
+ store.set(JSON.stringify(snapshot));
278
+ }
279
+ function emptyStore(mcpUrl) {
280
+ return { version: 2, mcpUrl, activeOrg: null, orgs: {} };
281
+ }
282
+ export function loadCredentialStore(mcpUrl = defaultMcpUrl, backend, resolution) {
121
283
  const store = resolveBackend(mcpUrl, backend, resolution);
122
284
  try {
123
- const loaded = readParsed(store);
285
+ const loaded = readStore(store, mcpUrl);
124
286
  if (loaded)
125
287
  return loaded;
126
288
  }
@@ -129,27 +291,296 @@ export function loadCredentials(mcpUrl = defaultMcpUrl, backend, resolution) {
129
291
  throw error;
130
292
  }
131
293
  if (store.kind === 'keyring' && !backend) {
132
- return readParsed(fileBackendFor(mcpUrl, resolution));
294
+ return readStore(fileBackendFor(mcpUrl, resolution), mcpUrl);
133
295
  }
134
296
  return null;
135
297
  }
136
- export function saveCredentials(credentials, backend, resolution) {
137
- const store = resolveBackend(credentials.mcpUrl, backend, resolution);
298
+ export function saveCredentialStore(snapshot, backend, resolution) {
299
+ const store = resolveBackend(snapshot.mcpUrl, backend, resolution);
138
300
  try {
139
- store.set(JSON.stringify(credentials));
301
+ writeStore(store, snapshot);
140
302
  return { backend: store };
141
303
  }
142
304
  catch (error) {
143
305
  if (store.kind === 'keyring' && !backend) {
144
- const fallback = fileBackendFor(credentials.mcpUrl, resolution);
145
- fallback.set(JSON.stringify(credentials));
306
+ const fallback = fileBackendFor(snapshot.mcpUrl, resolution);
307
+ writeStore(fallback, snapshot);
146
308
  return { backend: fallback };
147
309
  }
148
310
  throw error;
149
311
  }
150
312
  }
151
- export function deleteCredentials(mcpUrl = defaultMcpUrl, backend, resolution) {
313
+ /**
314
+ * Resolve one org's credentials. Precedence for which org: explicit `org`
315
+ * option, else the store's active org, else the sole pending legacy session.
316
+ */
317
+ export function loadCredentials(mcpUrl = defaultMcpUrl, backend, resolution, options) {
318
+ const snapshot = loadCredentialStore(mcpUrl, backend, resolution);
319
+ if (!snapshot)
320
+ return null;
321
+ return pickCredentialsFromStore(snapshot, options?.org);
322
+ }
323
+ /** Legacy pending session only (not the active named org). */
324
+ export function loadPendingCredentials(mcpUrl = defaultMcpUrl, backend, resolution) {
325
+ const snapshot = loadCredentialStore(mcpUrl, backend, resolution);
326
+ if (!snapshot?.pending)
327
+ return null;
328
+ return recordToCredentials(snapshot.mcpUrl, snapshot.pending);
329
+ }
330
+ export function pickCredentialsFromStore(snapshot, org) {
331
+ const requested = org?.trim().toLowerCase();
332
+ if (requested) {
333
+ const record = snapshot.orgs[requested];
334
+ if (record)
335
+ return recordToCredentials(snapshot.mcpUrl, record, requested);
336
+ // Pending-only legacy store: keep commands working when discovery has
337
+ // not named the session yet, but do NOT attach the requested slug —
338
+ // that would let a refresh persist an unverified org name.
339
+ if (snapshot.pending && Object.keys(snapshot.orgs).length === 0) {
340
+ return recordToCredentials(snapshot.mcpUrl, snapshot.pending);
341
+ }
342
+ return null;
343
+ }
344
+ if (snapshot.activeOrg && snapshot.orgs[snapshot.activeOrg]) {
345
+ return recordToCredentials(snapshot.mcpUrl, snapshot.orgs[snapshot.activeOrg], snapshot.activeOrg);
346
+ }
347
+ if (snapshot.pending) {
348
+ return recordToCredentials(snapshot.mcpUrl, snapshot.pending);
349
+ }
350
+ const only = Object.keys(snapshot.orgs);
351
+ if (only.length === 1) {
352
+ const slug = only[0];
353
+ return recordToCredentials(snapshot.mcpUrl, snapshot.orgs[slug], slug);
354
+ }
355
+ return null;
356
+ }
357
+ /**
358
+ * Upsert one org's credentials. Replaces that org's prior tokens when the slug
359
+ * already exists; leaves other orgs untouched.
360
+ *
361
+ * `activate` (default true) makes the saved org active — used by `kody login`.
362
+ * Pass `activate: false` for token refresh so a one-shot `--org` / `KODY_ORG`
363
+ * refresh does not steal the active org from `kody org use`.
364
+ */
365
+ export function saveCredentials(credentials, backend, resolution, options) {
366
+ const mcpUrl = credentials.mcpUrl;
367
+ const existing = loadCredentialStore(mcpUrl, backend, resolution) ?? emptyStore(mcpUrl);
368
+ const slug = typeof credentials.org === 'string' && credentials.org.trim()
369
+ ? credentials.org.trim().toLowerCase()
370
+ : null;
371
+ const record = credentialsToRecord({
372
+ ...credentials,
373
+ ...(slug ? { org: slug } : {}),
374
+ authorizationServerUrl: withDerivedIssuer(credentials).authorizationServerUrl,
375
+ });
376
+ const activate = options?.activate !== false;
377
+ let next;
378
+ if (slug) {
379
+ const currentActive = typeof existing.activeOrg === 'string' &&
380
+ existing.activeOrg.length > 0 &&
381
+ existing.orgs[existing.activeOrg]
382
+ ? existing.activeOrg
383
+ : null;
384
+ next = {
385
+ version: 2,
386
+ mcpUrl,
387
+ activeOrg: activate || !currentActive ? slug : currentActive,
388
+ orgs: {
389
+ ...existing.orgs,
390
+ [slug]: { ...record, org: slug },
391
+ },
392
+ // Keep an unresolved legacy session unless this save is promoting it
393
+ // (pending tokens matched / were the only login being named).
394
+ ...(existing.pending &&
395
+ existing.pending.accessToken !== record.accessToken
396
+ ? { pending: existing.pending }
397
+ : {}),
398
+ };
399
+ }
400
+ else {
401
+ next = {
402
+ version: 2,
403
+ mcpUrl,
404
+ activeOrg: existing.activeOrg,
405
+ orgs: existing.orgs,
406
+ pending: record,
407
+ };
408
+ }
409
+ return saveCredentialStore(next, backend, resolution);
410
+ }
411
+ /**
412
+ * Promote pending legacy tokens into `orgs[slug]`.
413
+ * Never overwrites an existing named login (a fresh `kody login` wins), and
414
+ * never steals `activeOrg` away from an already-active named org.
415
+ */
416
+ export function promotePendingOrg(mcpUrl, org, backend, resolution) {
417
+ const snapshot = loadCredentialStore(mcpUrl, backend, resolution);
418
+ if (!snapshot?.pending)
419
+ return snapshot;
420
+ const slug = org.trim().toLowerCase();
421
+ if (!slug) {
422
+ throw new Error('Organization slug is required to finish credential migration.');
423
+ }
424
+ const existingNamed = snapshot.orgs[slug];
425
+ const orgs = existingNamed
426
+ ? snapshot.orgs
427
+ : {
428
+ ...snapshot.orgs,
429
+ [slug]: { ...snapshot.pending, org: slug },
430
+ };
431
+ const activeIsNamed = typeof snapshot.activeOrg === 'string' &&
432
+ snapshot.activeOrg.length > 0 &&
433
+ snapshot.orgs[snapshot.activeOrg] != null;
434
+ const next = {
435
+ version: 2,
436
+ mcpUrl: snapshot.mcpUrl,
437
+ activeOrg: activeIsNamed ? snapshot.activeOrg : slug,
438
+ orgs,
439
+ };
440
+ saveCredentialStore(next, backend, resolution);
441
+ return next;
442
+ }
443
+ export function setActiveOrg(mcpUrl, org, backend, resolution) {
444
+ const snapshot = loadCredentialStore(mcpUrl, backend, resolution);
445
+ if (!snapshot) {
446
+ throw new Error('Not logged in. Run `kody login`.');
447
+ }
448
+ const slug = org.trim().toLowerCase();
449
+ if (!snapshot.orgs[slug]) {
450
+ const known = listOrgSlugs(snapshot);
451
+ throw new Error(known.length > 0
452
+ ? `No login stored for org "${slug}". Logged-in orgs: ${known.join(', ')}. Run \`kody login --org ${slug}\`.`
453
+ : `No login stored for org "${slug}". Run \`kody login --org ${slug}\`.`);
454
+ }
455
+ const next = {
456
+ ...snapshot,
457
+ activeOrg: slug,
458
+ };
459
+ saveCredentialStore(next, backend, resolution);
460
+ return next;
461
+ }
462
+ export function listOrgSlugs(snapshot) {
463
+ return Object.keys(snapshot.orgs).sort((a, b) => a.localeCompare(b));
464
+ }
465
+ /** Local-only listing for help / `org list` (no network). */
466
+ export function listStoredOrgs(mcpUrl = defaultMcpUrl, backend, resolution) {
467
+ const snapshot = loadCredentialStore(mcpUrl, backend, resolution);
468
+ if (!snapshot)
469
+ return [];
470
+ const rows = listOrgSlugs(snapshot).map((slug) => ({
471
+ slug,
472
+ active: snapshot.activeOrg === slug,
473
+ }));
474
+ if (snapshot.pending && rows.length === 0) {
475
+ rows.push({ slug: '(pending)', active: true, pending: true });
476
+ }
477
+ return rows;
478
+ }
479
+ export function deleteCredentials(mcpUrl = defaultMcpUrl, backend, resolution, options) {
152
480
  const store = resolveBackend(mcpUrl, backend, resolution);
481
+ const snapshot = (() => {
482
+ try {
483
+ return readStore(store, mcpUrl);
484
+ }
485
+ catch {
486
+ return null;
487
+ }
488
+ })();
489
+ if (!options?.org) {
490
+ // Plain logout: remove the active org only (or pending / entire store
491
+ // when that is all that remains). Documented in help.
492
+ if (!snapshot) {
493
+ if (store.kind === 'keyring' && !backend) {
494
+ const file = fileBackendFor(mcpUrl, resolution);
495
+ const fileSnap = readStore(file, mcpUrl);
496
+ if (!fileSnap)
497
+ return { deleted: false, backend: store, remaining: 0 };
498
+ return deleteCredentials(mcpUrl, file, resolution, options);
499
+ }
500
+ return { deleted: false, backend: store, remaining: 0 };
501
+ }
502
+ const target = snapshot.activeOrg ??
503
+ (snapshot.pending ? null : listOrgSlugs(snapshot)[0] ?? null);
504
+ if (target) {
505
+ return deleteOrgFromStore(snapshot, target, store, backend, resolution);
506
+ }
507
+ if (snapshot.pending) {
508
+ const next = {
509
+ version: 2,
510
+ mcpUrl: snapshot.mcpUrl,
511
+ activeOrg: snapshot.activeOrg,
512
+ orgs: snapshot.orgs,
513
+ };
514
+ if (Object.keys(next.orgs).length === 0) {
515
+ return wipeStore(mcpUrl, store, backend, resolution);
516
+ }
517
+ writeStore(store, next);
518
+ return {
519
+ deleted: true,
520
+ backend: store,
521
+ remaining: Object.keys(next.orgs).length,
522
+ };
523
+ }
524
+ return wipeStore(mcpUrl, store, backend, resolution);
525
+ }
526
+ const slug = options.org.trim().toLowerCase();
527
+ if (!snapshot) {
528
+ if (store.kind === 'keyring' && !backend) {
529
+ const file = fileBackendFor(mcpUrl, resolution);
530
+ const fileSnap = readStore(file, mcpUrl);
531
+ if (!fileSnap)
532
+ return { deleted: false, backend: store, remaining: 0 };
533
+ return deleteCredentials(mcpUrl, file, resolution, options);
534
+ }
535
+ return { deleted: false, backend: store, remaining: 0 };
536
+ }
537
+ return deleteOrgFromStore(snapshot, slug, store, backend, resolution);
538
+ }
539
+ function deleteOrgFromStore(snapshot, slug, store, backend, resolution) {
540
+ if (!snapshot.orgs[slug]) {
541
+ return {
542
+ deleted: false,
543
+ backend: store,
544
+ remaining: Object.keys(snapshot.orgs).length + (snapshot.pending ? 1 : 0),
545
+ };
546
+ }
547
+ const { [slug]: _removed, ...rest } = snapshot.orgs;
548
+ const remainingSlugs = Object.keys(rest);
549
+ if (remainingSlugs.length === 0 && !snapshot.pending) {
550
+ return wipeStore(snapshot.mcpUrl, store, backend, resolution);
551
+ }
552
+ const nextActive = snapshot.activeOrg === slug
553
+ ? remainingSlugs.sort((a, b) => a.localeCompare(b))[0] ?? null
554
+ : snapshot.activeOrg;
555
+ const next = {
556
+ version: 2,
557
+ mcpUrl: snapshot.mcpUrl,
558
+ activeOrg: nextActive,
559
+ orgs: rest,
560
+ ...(snapshot.pending ? { pending: snapshot.pending } : {}),
561
+ };
562
+ try {
563
+ writeStore(store, next);
564
+ return { deleted: true, backend: store, remaining: remainingSlugs.length };
565
+ }
566
+ catch (error) {
567
+ if (store.kind === 'keyring' && !backend) {
568
+ const fallback = fileBackendFor(snapshot.mcpUrl, resolution);
569
+ writeStore(fallback, next);
570
+ // Drop the stale keyring secret so the next load prefers the file
571
+ // (loadCredentials prefers keyring over file when both exist).
572
+ try {
573
+ store.delete();
574
+ }
575
+ catch {
576
+ // best-effort; file has the reduced store either way
577
+ }
578
+ return { deleted: true, backend: fallback, remaining: remainingSlugs.length };
579
+ }
580
+ throw error;
581
+ }
582
+ }
583
+ function wipeStore(mcpUrl, store, backend, resolution) {
153
584
  let deleted = false;
154
585
  try {
155
586
  deleted = store.delete();
@@ -161,6 +592,6 @@ export function deleteCredentials(mcpUrl = defaultMcpUrl, backend, resolution) {
161
592
  const file = fileBackendFor(mcpUrl, resolution);
162
593
  deleted = file.delete() || deleted;
163
594
  }
164
- return { deleted, backend: store };
595
+ return { deleted, backend: store, remaining: 0 };
165
596
  }
166
597
  //# sourceMappingURL=store.js.map