@dexto/agent-management 1.15.0-beta.2 → 1.16.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 (61) hide show
  1. package/dist/auth/index.cjs +0 -25
  2. package/dist/auth/index.d.ts +0 -6
  3. package/dist/auth/index.d.ts.map +1 -1
  4. package/dist/auth/index.js +0 -10
  5. package/dist/auth/model-auth-handlers.cjs +1 -3
  6. package/dist/auth/model-auth-handlers.d.ts +1 -6
  7. package/dist/auth/model-auth-handlers.d.ts.map +1 -1
  8. package/dist/auth/model-auth-handlers.js +1 -6
  9. package/dist/config/errors.cjs +6 -6
  10. package/dist/config/errors.js +6 -6
  11. package/dist/images/image-store.cjs +4 -2
  12. package/dist/images/image-store.d.ts.map +1 -1
  13. package/dist/images/image-store.js +4 -2
  14. package/dist/index.cjs +29 -40
  15. package/dist/index.d.ts +2 -3
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +6 -9
  18. package/dist/installation.cjs +2 -2
  19. package/dist/installation.js +2 -2
  20. package/dist/plugins/discover-skills.cjs +2 -5
  21. package/dist/plugins/discover-skills.d.ts +2 -6
  22. package/dist/plugins/discover-skills.d.ts.map +1 -1
  23. package/dist/plugins/discover-skills.js +2 -5
  24. package/dist/plugins/errors.cjs +2 -2
  25. package/dist/plugins/errors.js +2 -2
  26. package/dist/plugins/marketplace/errors.cjs +4 -4
  27. package/dist/plugins/marketplace/errors.js +4 -4
  28. package/dist/preferences/errors.cjs +1 -1
  29. package/dist/preferences/errors.js +1 -1
  30. package/dist/registry/registry.cjs +3 -3
  31. package/dist/registry/registry.js +3 -3
  32. package/dist/resolver.cjs +1 -1
  33. package/dist/resolver.js +1 -1
  34. package/dist/tool-factories/agent-spawner/factory.cjs +218 -1
  35. package/dist/tool-factories/agent-spawner/factory.d.ts.map +1 -1
  36. package/dist/tool-factories/agent-spawner/factory.js +228 -1
  37. package/dist/utils/dexto-auth.cjs +58 -3
  38. package/dist/utils/dexto-auth.d.ts +18 -4
  39. package/dist/utils/dexto-auth.d.ts.map +1 -1
  40. package/dist/utils/dexto-auth.js +57 -3
  41. package/package.json +6 -5
  42. package/dist/auth/dexto-accounts.cjs +0 -277
  43. package/dist/auth/dexto-accounts.d.ts +0 -144
  44. package/dist/auth/dexto-accounts.d.ts.map +0 -1
  45. package/dist/auth/dexto-accounts.js +0 -230
  46. package/dist/auth/dexto-cloud.cjs +0 -51
  47. package/dist/auth/dexto-cloud.d.ts +0 -9
  48. package/dist/auth/dexto-cloud.d.ts.map +0 -1
  49. package/dist/auth/dexto-cloud.js +0 -27
  50. package/dist/auth/dexto-gateway.cjs +0 -162
  51. package/dist/auth/dexto-gateway.d.ts +0 -55
  52. package/dist/auth/dexto-gateway.d.ts.map +0 -1
  53. package/dist/auth/dexto-gateway.js +0 -133
  54. package/dist/auth/dexto-mcp.cjs +0 -77
  55. package/dist/auth/dexto-mcp.d.ts +0 -23
  56. package/dist/auth/dexto-mcp.d.ts.map +0 -1
  57. package/dist/auth/dexto-mcp.js +0 -49
  58. package/dist/auth/dexto-sign-in.cjs +0 -345
  59. package/dist/auth/dexto-sign-in.d.ts +0 -86
  60. package/dist/auth/dexto-sign-in.d.ts.map +0 -1
  61. package/dist/auth/dexto-sign-in.js +0 -314
@@ -1,314 +0,0 @@
1
- import { createHash, randomBytes } from "node:crypto";
2
- import { z } from "zod";
3
- import { SignedInAsSchema } from "./dexto-accounts.js";
4
- const DEXTO_CLI_CLIENT_ID = "dexto-cli";
5
- const BASE_CLOUD_SCOPES = ["cloud:read"];
6
- const DEVICE_CODE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
7
- const LOGIN_TIMEOUT_MS = 5 * 6e4;
8
- const END_SIGN_IN_TIMEOUT_MS = 1e4;
9
- function signInPromptText(prompt) {
10
- if (prompt.kind === "browser")
11
- return `Sign in to Dexto in your browser. If it doesn't open, visit:
12
- ${prompt.authorizeUrl}`;
13
- return [
14
- prompt.verificationUriComplete === null ? `To sign in, open this link on any device and enter ${prompt.userCode}:
15
- ${prompt.verificationUri}` : `To sign in, open this link on any device and check it shows ${prompt.userCode}:
16
- ${prompt.verificationUriComplete}`,
17
- "Only approve this if you just ran dexto login."
18
- ].join("\n");
19
- }
20
- class SignInEnded extends Error {
21
- constructor(reason, message) {
22
- super(message);
23
- this.reason = reason;
24
- this.name = "SignInEnded";
25
- }
26
- }
27
- const ProtectedResourceSchema = z.object({
28
- authorization_servers: z.array(z.string().url()).min(1),
29
- resource: z.string().url(),
30
- scopes_supported: z.array(z.string())
31
- });
32
- const AuthorizationServerSchema = z.object({
33
- authorization_endpoint: z.string().url(),
34
- device_authorization_endpoint: z.string().url(),
35
- revocation_endpoint: z.string().url(),
36
- token_endpoint: z.string().url()
37
- });
38
- const TokenResponseSchema = z.object({
39
- access_token: z.string().min(1),
40
- refresh_token: z.string().min(1),
41
- expires_in: z.number().int().positive(),
42
- scope: z.string(),
43
- /** Who the tokens act as. */
44
- dexto_account: SignedInAsSchema.shape.account.optional(),
45
- dexto_organization: SignedInAsSchema.shape.organization.optional()
46
- });
47
- const DeviceAuthorizationSchema = z.object({
48
- device_code: z.string().min(1),
49
- user_code: z.string().min(1),
50
- expires_in: z.number().int().positive(),
51
- interval: z.number().int().positive().optional(),
52
- verification_uri: z.string().url(),
53
- verification_uri_complete: z.string().url().optional()
54
- });
55
- async function readMetadata(runtime, appUrl) {
56
- const resource = ProtectedResourceSchema.parse(
57
- await getJson(runtime, `${appUrl}/.well-known/oauth-protected-resource/mcp`)
58
- );
59
- const issuer = (resource.authorization_servers[0] ?? appUrl).replace(/\/+$/u, "");
60
- const server = AuthorizationServerSchema.parse(
61
- await getJson(runtime, `${issuer}/.well-known/oauth-authorization-server`)
62
- );
63
- return { resource, server };
64
- }
65
- async function discover(runtime, input) {
66
- const appUrl = input.appUrl.replace(/\/+$/u, "");
67
- const { resource, server } = await readMetadata(runtime, appUrl);
68
- const reading = resource.scopes_supported.filter((scope) => scope.endsWith(":read"));
69
- const scopes = [.../* @__PURE__ */ new Set([...reading, ...BASE_CLOUD_SCOPES, ...input.extraScopes])];
70
- return { appUrl, resource: resource.resource, scopes, server };
71
- }
72
- async function signInWithBrowser(runtime, input) {
73
- const { appUrl, resource, scopes, server } = await discover(runtime, input);
74
- const redirect = await runtime.listenForRedirect(appUrl);
75
- try {
76
- const verifier = randomBytes(32).toString("base64url");
77
- const state = randomBytes(16).toString("base64url");
78
- const authorizeUrl = new URL(server.authorization_endpoint);
79
- for (const [name, value] of Object.entries({
80
- client_id: DEXTO_CLI_CLIENT_ID,
81
- code_challenge: createHash("sha256").update(verifier).digest("base64url"),
82
- code_challenge_method: "S256",
83
- redirect_uri: redirect.redirectUri,
84
- resource,
85
- response_type: "code",
86
- scope: scopes.join(" "),
87
- state,
88
- ...input.organization === null ? {} : { organization: input.organization }
89
- }))
90
- authorizeUrl.searchParams.set(name, value);
91
- runtime.prompt({ kind: "browser", authorizeUrl: authorizeUrl.toString() });
92
- if (input.openBrowser) await runtime.openBrowser(authorizeUrl.toString());
93
- const params = await withTimeout(redirect.result, LOGIN_TIMEOUT_MS, runtime.signal);
94
- if (params.get("state") !== state)
95
- throw new Error("The sign-in reply didn't match this login.");
96
- const error = params.get("error");
97
- if (error !== null)
98
- throw error === "access_denied" ? new SignInEnded("declined", "Sign-in was declined.") : new Error(`Sign-in failed: ${params.get("error_description") ?? error}.`);
99
- const code = params.get("code");
100
- if (code === null) throw new Error("The sign-in reply had no code.");
101
- const tokens = TokenResponseSchema.parse(
102
- await postForm(runtime, server.token_endpoint, {
103
- client_id: DEXTO_CLI_CLIENT_ID,
104
- code,
105
- code_verifier: verifier,
106
- grant_type: "authorization_code",
107
- redirect_uri: redirect.redirectUri,
108
- resource
109
- })
110
- );
111
- return toLogin(tokens, {
112
- apiBaseUrl: input.apiBaseUrl,
113
- appUrl,
114
- resource,
115
- tokenEndpoint: server.token_endpoint
116
- });
117
- } finally {
118
- redirect.close();
119
- }
120
- }
121
- async function signInWithDevice(runtime, input) {
122
- const { appUrl, resource, scopes, server } = await discover(runtime, input);
123
- const device = DeviceAuthorizationSchema.parse(
124
- await postForm(runtime, server.device_authorization_endpoint, {
125
- client_id: DEXTO_CLI_CLIENT_ID,
126
- resource,
127
- scope: scopes.join(" "),
128
- ...input.organization === null ? {} : { organization: input.organization }
129
- })
130
- );
131
- runtime.prompt({
132
- kind: "device",
133
- userCode: device.user_code,
134
- verificationUri: device.verification_uri,
135
- verificationUriComplete: device.verification_uri_complete ?? null,
136
- expiresInSeconds: device.expires_in
137
- });
138
- const deadline = Date.now() + device.expires_in * 1e3;
139
- let intervalSeconds = device.interval ?? 5;
140
- while (Date.now() < deadline) {
141
- await runtime.sleep(intervalSeconds * 1e3);
142
- try {
143
- const tokens = TokenResponseSchema.parse(
144
- await postForm(runtime, server.token_endpoint, {
145
- client_id: DEXTO_CLI_CLIENT_ID,
146
- device_code: device.device_code,
147
- grant_type: DEVICE_CODE_GRANT_TYPE,
148
- resource
149
- })
150
- );
151
- return toLogin(tokens, {
152
- apiBaseUrl: input.apiBaseUrl,
153
- appUrl,
154
- resource,
155
- tokenEndpoint: server.token_endpoint
156
- });
157
- } catch (error) {
158
- if (error instanceof TypeError) continue;
159
- if (!(error instanceof OAuthHttpError)) throw error;
160
- if (error.status >= 500 || error.code === "authorization_pending") continue;
161
- if (error.code === "slow_down") {
162
- intervalSeconds += 5;
163
- continue;
164
- }
165
- if (error.code === "access_denied")
166
- throw new SignInEnded("declined", "Sign-in was declined.");
167
- if (error.code !== "expired_token") throw error;
168
- break;
169
- }
170
- }
171
- throw new SignInEnded("expired", "The sign-in code expired. Run dexto login --device again.");
172
- }
173
- function toLogin(tokens, client) {
174
- return {
175
- kind: "oauth",
176
- ...client,
177
- clientId: DEXTO_CLI_CLIENT_ID,
178
- accessToken: tokens.access_token,
179
- refreshToken: tokens.refresh_token,
180
- expiresAt: expiresAt(tokens.expires_in),
181
- scopes: splitScopes(tokens.scope),
182
- savedAt: (/* @__PURE__ */ new Date()).toISOString(),
183
- signedInAs: signedInAsFrom(tokens),
184
- cloudProject: null
185
- };
186
- }
187
- function signedInAsFrom(tokens) {
188
- return tokens.dexto_account === void 0 || tokens.dexto_organization === void 0 ? null : { account: tokens.dexto_account, organization: tokens.dexto_organization };
189
- }
190
- async function refreshSignIn(runtime, login) {
191
- let body;
192
- try {
193
- body = await postForm(runtime, login.tokenEndpoint, {
194
- client_id: login.clientId,
195
- grant_type: "refresh_token",
196
- refresh_token: login.refreshToken,
197
- resource: login.resource
198
- });
199
- } catch (error) {
200
- if (error instanceof OAuthHttpError && error.status === 400)
201
- throw new Error("Your Dexto sign-in has expired. Run dexto login again.");
202
- throw error;
203
- }
204
- const tokens = TokenResponseSchema.parse(body);
205
- return {
206
- ...login,
207
- accessToken: tokens.access_token,
208
- refreshToken: tokens.refresh_token,
209
- expiresAt: expiresAt(tokens.expires_in),
210
- scopes: splitScopes(tokens.scope),
211
- signedInAs: signedInAsFrom(tokens) ?? login.signedInAs
212
- };
213
- }
214
- async function endSignIn(runtime, login) {
215
- const bounded = {
216
- ...runtime,
217
- signal: AbortSignal.any([runtime.signal, AbortSignal.timeout(END_SIGN_IN_TIMEOUT_MS)])
218
- };
219
- try {
220
- const { server } = await readMetadata(bounded, login.appUrl.replace(/\/+$/u, ""));
221
- await postForm(bounded, server.revocation_endpoint, {
222
- client_id: login.clientId,
223
- token: login.refreshToken,
224
- token_type_hint: "refresh_token"
225
- });
226
- return { status: "ended" };
227
- } catch (error) {
228
- return { status: "failed", reason: error instanceof Error ? error.message : String(error) };
229
- }
230
- }
231
- class OAuthHttpError extends Error {
232
- /** The OAuth `error` code, such as `authorization_pending`; null when the reply had none. */
233
- code;
234
- status;
235
- constructor(status, code, message) {
236
- super(message);
237
- this.name = "OAuthHttpError";
238
- this.code = code;
239
- this.status = status;
240
- }
241
- }
242
- async function getJson(runtime, url) {
243
- return await readJson(
244
- await runtime.fetch(url, {
245
- headers: { accept: "application/json" },
246
- signal: runtime.signal
247
- }),
248
- url
249
- );
250
- }
251
- async function postForm(runtime, url, fields) {
252
- return await readJson(
253
- await runtime.fetch(url, {
254
- body: new URLSearchParams(fields),
255
- headers: { accept: "application/json" },
256
- method: "POST",
257
- signal: runtime.signal
258
- }),
259
- url
260
- );
261
- }
262
- const OAuthErrorSchema = z.object({
263
- error: z.string().optional(),
264
- error_description: z.string().optional()
265
- });
266
- async function readJson(response, url) {
267
- const text = await response.text();
268
- if (response.ok) return text.length === 0 ? null : JSON.parse(text);
269
- const reply = OAuthErrorSchema.safeParse(parseOrNull(text));
270
- const code = reply.success ? reply.data.error ?? null : null;
271
- const description = (reply.success ? reply.data.error_description : void 0) ?? code ?? `HTTP ${response.status}`;
272
- throw new OAuthHttpError(response.status, code, `${new URL(url).pathname}: ${description}`);
273
- }
274
- function parseOrNull(text) {
275
- try {
276
- return JSON.parse(text);
277
- } catch {
278
- return null;
279
- }
280
- }
281
- async function withTimeout(promise, ms, signal) {
282
- let timer;
283
- const stop = new Promise((_, reject) => {
284
- timer = setTimeout(() => reject(new SignInEnded("timed_out", "Sign-in timed out.")), ms);
285
- signal.addEventListener(
286
- "abort",
287
- () => reject(new SignInEnded("cancelled", "Sign-in was cancelled.")),
288
- {
289
- once: true
290
- }
291
- );
292
- });
293
- try {
294
- return await Promise.race([promise, stop]);
295
- } finally {
296
- clearTimeout(timer);
297
- }
298
- }
299
- function splitScopes(scope) {
300
- return scope.split(" ").filter((each) => each.length > 0);
301
- }
302
- function expiresAt(expiresInSeconds) {
303
- return new Date(Date.now() + expiresInSeconds * 1e3).toISOString();
304
- }
305
- export {
306
- DEXTO_CLI_CLIENT_ID,
307
- OAuthHttpError,
308
- SignInEnded,
309
- endSignIn,
310
- refreshSignIn,
311
- signInPromptText,
312
- signInWithBrowser,
313
- signInWithDevice
314
- };