@amigo-ai/platform-sdk 0.10.0 → 0.11.1
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/dist/core/device-code.js +460 -0
- package/dist/core/device-code.js.map +1 -0
- package/dist/index.cjs +448 -0
- package/dist/index.cjs.map +4 -4
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +448 -0
- package/dist/index.mjs.map +4 -4
- package/dist/types/core/device-code.d.ts +136 -0
- package/dist/types/core/device-code.d.ts.map +1 -0
- package/dist/types/index.d.cts +2 -0
- package/dist/types/index.d.cts.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -199,6 +199,8 @@ export { paginate } from './core/utils.js';
|
|
|
199
199
|
export { buildLastResponse, extractRequestId } from './core/utils.js';
|
|
200
200
|
export { parseRateLimitHeaders } from './core/rate-limit.js';
|
|
201
201
|
export { verifyWebhookSignature, parseWebhookEvent, WebhookVerificationError, } from './core/webhooks.js';
|
|
202
|
+
// Device code auth (desktop / CLI login)
|
|
203
|
+
export { loginWithDeviceCode, TokenManager, FileTokenStorage, MemoryTokenStorage, DeviceCodeExpiredError, DeviceCodeDeniedError, RefreshTokenExpiredError, LoginCancelledError, formatDeviceCodeInstructions, formatDeviceCodeLink, formatWorkspaceList, openBrowser, } from './core/device-code.js';
|
|
202
204
|
function withWorkspaceId(path, init, workspaceId) {
|
|
203
205
|
if (!path.includes('{workspace_id}')) {
|
|
204
206
|
return (init ?? {});
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAIH,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AACrD,OAAO,EACL,2BAA2B,EAC3B,oBAAoB,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function toCryptoBuffer(bytes) {
|
|
|
3063
3063
|
return copy.buffer;
|
|
3064
3064
|
}
|
|
3065
3065
|
|
|
3066
|
+
// src/core/device-code.ts
|
|
3067
|
+
var DeviceCodeExpiredError = class extends AmigoError {
|
|
3068
|
+
constructor(message = "Device code expired. Please restart the login flow.") {
|
|
3069
|
+
super(message, { errorCode: "device_code_expired" });
|
|
3070
|
+
}
|
|
3071
|
+
};
|
|
3072
|
+
var DeviceCodeDeniedError = class extends AmigoError {
|
|
3073
|
+
constructor(message = "Authorization request was denied.") {
|
|
3074
|
+
super(message, { errorCode: "device_code_denied" });
|
|
3075
|
+
}
|
|
3076
|
+
};
|
|
3077
|
+
var RefreshTokenExpiredError = class extends AuthenticationError {
|
|
3078
|
+
constructor(message = "Refresh token expired. Please log in again.") {
|
|
3079
|
+
super(message, { errorCode: "refresh_token_expired" });
|
|
3080
|
+
}
|
|
3081
|
+
};
|
|
3082
|
+
var LoginCancelledError = class extends AmigoError {
|
|
3083
|
+
constructor() {
|
|
3084
|
+
super("Login cancelled", { errorCode: "login_cancelled" });
|
|
3085
|
+
}
|
|
3086
|
+
};
|
|
3087
|
+
var DEFAULT_IDENTITY_URL = "https://identity.platform.amigo.ai";
|
|
3088
|
+
async function identityPost(baseUrl, path, body, fetchFn) {
|
|
3089
|
+
try {
|
|
3090
|
+
return await fetchFn(`${baseUrl}${path}`, {
|
|
3091
|
+
method: "POST",
|
|
3092
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
3093
|
+
body: body.toString(),
|
|
3094
|
+
redirect: "manual"
|
|
3095
|
+
});
|
|
3096
|
+
} catch (err) {
|
|
3097
|
+
throw new NetworkError("Network error contacting identity service", err);
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
async function requestDeviceCode(baseUrl, params, fetchFn) {
|
|
3101
|
+
const body = new URLSearchParams();
|
|
3102
|
+
if (params.clientDescription) body.set("client_description", params.clientDescription);
|
|
3103
|
+
if (params.scope) body.set("scope", params.scope);
|
|
3104
|
+
const res = await identityPost(baseUrl, "/device/code", body, fetchFn);
|
|
3105
|
+
if (res.status === 429) {
|
|
3106
|
+
const retryAfter = parseInt(res.headers.get("Retry-After") ?? "", 10);
|
|
3107
|
+
throw new RateLimitError("Rate limited", { retryAfter: isNaN(retryAfter) ? void 0 : retryAfter });
|
|
3108
|
+
}
|
|
3109
|
+
if (!res.ok) {
|
|
3110
|
+
const err = await res.json().catch(() => ({}));
|
|
3111
|
+
throw new AmigoError(err.error_description ?? `Identity error (${res.status})`, {
|
|
3112
|
+
statusCode: res.status,
|
|
3113
|
+
errorCode: err.error
|
|
3114
|
+
});
|
|
3115
|
+
}
|
|
3116
|
+
return await res.json();
|
|
3117
|
+
}
|
|
3118
|
+
async function pollDeviceCode(baseUrl, deviceCode, scope, workspaceId2, fetchFn) {
|
|
3119
|
+
const body = new URLSearchParams({ grant_type: "device_code", device_code: deviceCode });
|
|
3120
|
+
if (scope) body.set("scope", scope);
|
|
3121
|
+
if (workspaceId2) body.set("workspace_id", workspaceId2);
|
|
3122
|
+
const res = await identityPost(baseUrl, "/token", body, fetchFn);
|
|
3123
|
+
if (res.status === 300)
|
|
3124
|
+
return { type: "multi_workspace", data: await res.json() };
|
|
3125
|
+
if (res.status === 200) return { type: "token", data: await res.json() };
|
|
3126
|
+
if (res.status === 400) {
|
|
3127
|
+
const err = await res.json().catch(() => ({ error: "unknown" }));
|
|
3128
|
+
if (err.error === "authorization_pending") return { type: "pending" };
|
|
3129
|
+
if (err.error === "slow_down") return { type: "slow_down" };
|
|
3130
|
+
throw new AmigoError(err.error_description ?? err.error ?? `Identity error (400)`, {
|
|
3131
|
+
statusCode: 400,
|
|
3132
|
+
errorCode: err.error
|
|
3133
|
+
});
|
|
3134
|
+
}
|
|
3135
|
+
throw new AmigoError(`Identity error (${res.status})`, { statusCode: res.status });
|
|
3136
|
+
}
|
|
3137
|
+
async function doRefreshToken(baseUrl, params, fetchFn) {
|
|
3138
|
+
const body = new URLSearchParams({
|
|
3139
|
+
grant_type: "refresh_token",
|
|
3140
|
+
refresh_token: params.refreshToken
|
|
3141
|
+
});
|
|
3142
|
+
if (params.workspaceId) body.set("workspace_id", params.workspaceId);
|
|
3143
|
+
if (params.scope) body.set("scope", params.scope);
|
|
3144
|
+
const res = await identityPost(baseUrl, "/token", body, fetchFn);
|
|
3145
|
+
if (!res.ok) {
|
|
3146
|
+
const err = await res.json().catch(() => ({}));
|
|
3147
|
+
throw new AmigoError(err.error_description ?? `Identity error (${res.status})`, {
|
|
3148
|
+
statusCode: res.status,
|
|
3149
|
+
errorCode: err.error
|
|
3150
|
+
});
|
|
3151
|
+
}
|
|
3152
|
+
return await res.json();
|
|
3153
|
+
}
|
|
3154
|
+
function decodeJwtPayload(jwt) {
|
|
3155
|
+
try {
|
|
3156
|
+
const parts = jwt.split(".");
|
|
3157
|
+
if (parts.length !== 3 || !parts[1]) return null;
|
|
3158
|
+
const payload = Buffer.from(parts[1], "base64url").toString();
|
|
3159
|
+
return JSON.parse(payload);
|
|
3160
|
+
} catch {
|
|
3161
|
+
return null;
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
function sleep2(ms, signal) {
|
|
3165
|
+
return new Promise((resolve, reject) => {
|
|
3166
|
+
if (signal?.aborted) {
|
|
3167
|
+
reject(new LoginCancelledError());
|
|
3168
|
+
return;
|
|
3169
|
+
}
|
|
3170
|
+
const timer = setTimeout(resolve, ms);
|
|
3171
|
+
signal?.addEventListener(
|
|
3172
|
+
"abort",
|
|
3173
|
+
() => {
|
|
3174
|
+
clearTimeout(timer);
|
|
3175
|
+
reject(new LoginCancelledError());
|
|
3176
|
+
},
|
|
3177
|
+
{ once: true }
|
|
3178
|
+
);
|
|
3179
|
+
});
|
|
3180
|
+
}
|
|
3181
|
+
function toAuthResult(token, workspaceIdOverride) {
|
|
3182
|
+
const claims = decodeJwtPayload(token.access_token);
|
|
3183
|
+
const workspaceId2 = workspaceIdOverride ?? claims?.workspace_id ?? "";
|
|
3184
|
+
if (!workspaceId2) {
|
|
3185
|
+
throw new AmigoError("Token does not contain a workspace_id claim", {
|
|
3186
|
+
errorCode: "missing_workspace"
|
|
3187
|
+
});
|
|
3188
|
+
}
|
|
3189
|
+
return {
|
|
3190
|
+
accessToken: token.access_token,
|
|
3191
|
+
refreshToken: token.refresh_token ?? "",
|
|
3192
|
+
workspaceId: workspaceId2,
|
|
3193
|
+
expiresAt: claims?.exp ?? (token.expires_in ? Math.floor(Date.now() / 1e3) + token.expires_in : Math.floor(Date.now() / 1e3) + 900),
|
|
3194
|
+
scope: token.scope
|
|
3195
|
+
};
|
|
3196
|
+
}
|
|
3197
|
+
async function fetchWorkspaces(baseUrl, accessToken, fetchFn) {
|
|
3198
|
+
for (const path of ["/self/profile", "/self"]) {
|
|
3199
|
+
const res = await fetchFn(`${baseUrl}${path}`, {
|
|
3200
|
+
headers: { Authorization: `Bearer ${accessToken}` }
|
|
3201
|
+
});
|
|
3202
|
+
if (res.ok) {
|
|
3203
|
+
const data = await res.json();
|
|
3204
|
+
return (data.workspaces ?? []).map((ws) => ({
|
|
3205
|
+
workspace_id: ws.workspace_id,
|
|
3206
|
+
role: ws.role,
|
|
3207
|
+
name: ws.name
|
|
3208
|
+
}));
|
|
3209
|
+
}
|
|
3210
|
+
if (res.status !== 404) {
|
|
3211
|
+
throw new AmigoError(`Failed to fetch workspaces (${res.status})`, { statusCode: res.status });
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
return [];
|
|
3215
|
+
}
|
|
3216
|
+
async function resolveWorkspaceFromBootstrap(baseUrl, token, options, fetchFn) {
|
|
3217
|
+
if (!token.refresh_token) {
|
|
3218
|
+
throw new AmigoError("Bootstrap token missing refresh_token", { errorCode: "server_error" });
|
|
3219
|
+
}
|
|
3220
|
+
const workspaces = await fetchWorkspaces(baseUrl, token.access_token, fetchFn);
|
|
3221
|
+
if (workspaces.length === 0) {
|
|
3222
|
+
throw new AmigoError(
|
|
3223
|
+
"No workspace memberships found. Create a workspace or request an invitation.",
|
|
3224
|
+
{ errorCode: "no_workspaces" }
|
|
3225
|
+
);
|
|
3226
|
+
}
|
|
3227
|
+
if (workspaces.length === 1 && workspaces[0]) {
|
|
3228
|
+
const scoped2 = await doRefreshToken(
|
|
3229
|
+
baseUrl,
|
|
3230
|
+
{ refreshToken: token.refresh_token, workspaceId: workspaces[0].workspace_id, scope: options.scope },
|
|
3231
|
+
fetchFn
|
|
3232
|
+
);
|
|
3233
|
+
return toAuthResult(scoped2, workspaces[0].workspace_id);
|
|
3234
|
+
}
|
|
3235
|
+
const workspaceId2 = await options.onWorkspaceRequired(workspaces);
|
|
3236
|
+
const scoped = await doRefreshToken(
|
|
3237
|
+
baseUrl,
|
|
3238
|
+
{ refreshToken: token.refresh_token, workspaceId: workspaceId2, scope: options.scope },
|
|
3239
|
+
fetchFn
|
|
3240
|
+
);
|
|
3241
|
+
return toAuthResult(scoped, workspaceId2);
|
|
3242
|
+
}
|
|
3243
|
+
async function resolveWorkspaceFromMulti(baseUrl, multi, options, fetchFn) {
|
|
3244
|
+
if (!multi.refresh_token) {
|
|
3245
|
+
throw new AmigoError("Multi-workspace response missing refresh_token", { errorCode: "server_error" });
|
|
3246
|
+
}
|
|
3247
|
+
const workspaceId2 = await options.onWorkspaceRequired(multi.workspaces);
|
|
3248
|
+
const scoped = await doRefreshToken(
|
|
3249
|
+
baseUrl,
|
|
3250
|
+
{ refreshToken: multi.refresh_token, workspaceId: workspaceId2, scope: options.scope },
|
|
3251
|
+
fetchFn
|
|
3252
|
+
);
|
|
3253
|
+
return toAuthResult(scoped, workspaceId2);
|
|
3254
|
+
}
|
|
3255
|
+
async function loginWithDeviceCode(options) {
|
|
3256
|
+
const baseUrl = (options.identityBaseUrl ?? DEFAULT_IDENTITY_URL).replace(/\/+$/, "");
|
|
3257
|
+
const fetchFn = options.fetch ?? globalThis.fetch;
|
|
3258
|
+
const issuance = await requestDeviceCode(
|
|
3259
|
+
baseUrl,
|
|
3260
|
+
{ clientDescription: options.clientDescription, scope: options.scope },
|
|
3261
|
+
fetchFn
|
|
3262
|
+
);
|
|
3263
|
+
await options.onCode(issuance);
|
|
3264
|
+
let interval = issuance.interval * 1e3;
|
|
3265
|
+
const deadline = Date.now() + issuance.expires_in * 1e3;
|
|
3266
|
+
while (Date.now() < deadline) {
|
|
3267
|
+
if (options.signal?.aborted) throw new LoginCancelledError();
|
|
3268
|
+
await sleep2(interval, options.signal);
|
|
3269
|
+
if (options.signal?.aborted) throw new LoginCancelledError();
|
|
3270
|
+
options.onStatus?.("polling");
|
|
3271
|
+
try {
|
|
3272
|
+
const result = await pollDeviceCode(baseUrl, issuance.device_code, options.scope, options.workspaceId, fetchFn);
|
|
3273
|
+
if (result.type === "pending") {
|
|
3274
|
+
options.onStatus?.("authorization_pending");
|
|
3275
|
+
continue;
|
|
3276
|
+
}
|
|
3277
|
+
if (result.type === "slow_down") {
|
|
3278
|
+
interval += 5e3;
|
|
3279
|
+
options.onStatus?.("slow_down");
|
|
3280
|
+
continue;
|
|
3281
|
+
}
|
|
3282
|
+
options.onStatus?.("approved");
|
|
3283
|
+
if (result.type === "token") {
|
|
3284
|
+
const claims = decodeJwtPayload(result.data.access_token);
|
|
3285
|
+
const isBootstrap = claims?.workspace_bootstrap || !claims?.workspace_id;
|
|
3286
|
+
if (isBootstrap && result.data.refresh_token) {
|
|
3287
|
+
if (options.workspaceId) {
|
|
3288
|
+
const scoped = await doRefreshToken(
|
|
3289
|
+
baseUrl,
|
|
3290
|
+
{ refreshToken: result.data.refresh_token, workspaceId: options.workspaceId, scope: options.scope },
|
|
3291
|
+
fetchFn
|
|
3292
|
+
);
|
|
3293
|
+
return toAuthResult(scoped, options.workspaceId);
|
|
3294
|
+
}
|
|
3295
|
+
return await resolveWorkspaceFromBootstrap(baseUrl, result.data, options, fetchFn);
|
|
3296
|
+
}
|
|
3297
|
+
return toAuthResult(result.data);
|
|
3298
|
+
}
|
|
3299
|
+
return await resolveWorkspaceFromMulti(baseUrl, result.data, options, fetchFn);
|
|
3300
|
+
} catch (err) {
|
|
3301
|
+
if (err instanceof AmigoError && err.errorCode === "expired_token") {
|
|
3302
|
+
options.onStatus?.("expired");
|
|
3303
|
+
throw new DeviceCodeExpiredError();
|
|
3304
|
+
}
|
|
3305
|
+
if (err instanceof AmigoError && err.errorCode === "access_denied") {
|
|
3306
|
+
options.onStatus?.("denied");
|
|
3307
|
+
throw new DeviceCodeDeniedError();
|
|
3308
|
+
}
|
|
3309
|
+
throw err;
|
|
3310
|
+
}
|
|
3311
|
+
}
|
|
3312
|
+
options.onStatus?.("expired");
|
|
3313
|
+
throw new DeviceCodeExpiredError();
|
|
3314
|
+
}
|
|
3315
|
+
var REFRESH_BUFFER_SECONDS = 60;
|
|
3316
|
+
var TokenManager = class {
|
|
3317
|
+
_storage;
|
|
3318
|
+
_baseUrl;
|
|
3319
|
+
_fetch;
|
|
3320
|
+
_cached = null;
|
|
3321
|
+
_refreshPromise = null;
|
|
3322
|
+
constructor(config = {}) {
|
|
3323
|
+
this._storage = config.storage ?? new FileTokenStorage();
|
|
3324
|
+
this._baseUrl = (config.identityBaseUrl ?? DEFAULT_IDENTITY_URL).replace(/\/+$/, "");
|
|
3325
|
+
this._fetch = config.fetch ?? globalThis.fetch;
|
|
3326
|
+
}
|
|
3327
|
+
async store(result) {
|
|
3328
|
+
const creds = {
|
|
3329
|
+
access_token: result.accessToken,
|
|
3330
|
+
refresh_token: result.refreshToken,
|
|
3331
|
+
workspace_id: result.workspaceId,
|
|
3332
|
+
expires_at: result.expiresAt,
|
|
3333
|
+
scope: result.scope
|
|
3334
|
+
};
|
|
3335
|
+
this._cached = creds;
|
|
3336
|
+
await this._storage.save(creds);
|
|
3337
|
+
}
|
|
3338
|
+
async getAccessToken() {
|
|
3339
|
+
const creds = await this._loadCached();
|
|
3340
|
+
if (!creds) return null;
|
|
3341
|
+
if (creds.expires_at - Math.floor(Date.now() / 1e3) >= REFRESH_BUFFER_SECONDS) {
|
|
3342
|
+
return { token: creds.access_token, workspaceId: creds.workspace_id };
|
|
3343
|
+
}
|
|
3344
|
+
const refreshed = await this._refresh(creds);
|
|
3345
|
+
return { token: refreshed.access_token, workspaceId: refreshed.workspace_id };
|
|
3346
|
+
}
|
|
3347
|
+
async hasCredentials() {
|
|
3348
|
+
return await this._loadCached() !== null;
|
|
3349
|
+
}
|
|
3350
|
+
async clear() {
|
|
3351
|
+
this._cached = null;
|
|
3352
|
+
this._refreshPromise = null;
|
|
3353
|
+
await this._storage.clear();
|
|
3354
|
+
}
|
|
3355
|
+
async _loadCached() {
|
|
3356
|
+
if (this._cached) return this._cached;
|
|
3357
|
+
const loaded = await this._storage.load();
|
|
3358
|
+
if (loaded) this._cached = loaded;
|
|
3359
|
+
return loaded;
|
|
3360
|
+
}
|
|
3361
|
+
async _refresh(current) {
|
|
3362
|
+
if (this._refreshPromise) return this._refreshPromise;
|
|
3363
|
+
this._refreshPromise = this._doRefresh(current).finally(() => {
|
|
3364
|
+
this._refreshPromise = null;
|
|
3365
|
+
});
|
|
3366
|
+
return this._refreshPromise;
|
|
3367
|
+
}
|
|
3368
|
+
async _doRefresh(current) {
|
|
3369
|
+
if (!current.refresh_token) throw new RefreshTokenExpiredError("No refresh token available");
|
|
3370
|
+
try {
|
|
3371
|
+
const response = await doRefreshToken(
|
|
3372
|
+
this._baseUrl,
|
|
3373
|
+
{ refreshToken: current.refresh_token, workspaceId: current.workspace_id, scope: current.scope },
|
|
3374
|
+
this._fetch
|
|
3375
|
+
);
|
|
3376
|
+
const claims = decodeJwtPayload(response.access_token);
|
|
3377
|
+
const refreshed = {
|
|
3378
|
+
access_token: response.access_token,
|
|
3379
|
+
refresh_token: response.refresh_token ?? current.refresh_token,
|
|
3380
|
+
workspace_id: current.workspace_id,
|
|
3381
|
+
expires_at: claims?.exp ?? Math.floor(Date.now() / 1e3) + response.expires_in,
|
|
3382
|
+
scope: response.scope ?? current.scope
|
|
3383
|
+
};
|
|
3384
|
+
this._cached = refreshed;
|
|
3385
|
+
await this._storage.save(refreshed);
|
|
3386
|
+
return refreshed;
|
|
3387
|
+
} catch (err) {
|
|
3388
|
+
if (err instanceof AmigoError && (err.statusCode === 401 || err.errorCode === "invalid_grant")) {
|
|
3389
|
+
await this.clear();
|
|
3390
|
+
throw new RefreshTokenExpiredError();
|
|
3391
|
+
}
|
|
3392
|
+
throw err;
|
|
3393
|
+
}
|
|
3394
|
+
}
|
|
3395
|
+
};
|
|
3396
|
+
var FileTokenStorage = class {
|
|
3397
|
+
_explicitPath;
|
|
3398
|
+
_resolvedPath;
|
|
3399
|
+
constructor(filePath) {
|
|
3400
|
+
this._explicitPath = filePath;
|
|
3401
|
+
}
|
|
3402
|
+
async _filePath() {
|
|
3403
|
+
if (this._explicitPath) return this._explicitPath;
|
|
3404
|
+
if (this._resolvedPath) return this._resolvedPath;
|
|
3405
|
+
const os = await import("node:os");
|
|
3406
|
+
const path = await import("node:path");
|
|
3407
|
+
this._resolvedPath = path.join(os.homedir(), ".amigo", "credentials.json");
|
|
3408
|
+
return this._resolvedPath;
|
|
3409
|
+
}
|
|
3410
|
+
async load() {
|
|
3411
|
+
const fs = await import("node:fs/promises");
|
|
3412
|
+
const filePath = await this._filePath();
|
|
3413
|
+
try {
|
|
3414
|
+
const raw = await fs.readFile(filePath, "utf-8");
|
|
3415
|
+
const data = JSON.parse(raw);
|
|
3416
|
+
if (typeof data.access_token === "string" && typeof data.refresh_token === "string" && typeof data.workspace_id === "string" && typeof data.expires_at === "number") {
|
|
3417
|
+
return data;
|
|
3418
|
+
}
|
|
3419
|
+
return null;
|
|
3420
|
+
} catch (err) {
|
|
3421
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") return null;
|
|
3422
|
+
throw err;
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
async save(credentials) {
|
|
3426
|
+
const fs = await import("node:fs/promises");
|
|
3427
|
+
const path = await import("node:path");
|
|
3428
|
+
const filePath = await this._filePath();
|
|
3429
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true, mode: 448 });
|
|
3430
|
+
await fs.writeFile(filePath, JSON.stringify(credentials, null, 2) + "\n", { mode: 384 });
|
|
3431
|
+
await fs.chmod(filePath, 384);
|
|
3432
|
+
}
|
|
3433
|
+
async clear() {
|
|
3434
|
+
const fs = await import("node:fs/promises");
|
|
3435
|
+
const filePath = await this._filePath();
|
|
3436
|
+
try {
|
|
3437
|
+
await fs.unlink(filePath);
|
|
3438
|
+
} catch (err) {
|
|
3439
|
+
if (err && typeof err === "object" && "code" in err && err.code === "ENOENT") return;
|
|
3440
|
+
throw err;
|
|
3441
|
+
}
|
|
3442
|
+
}
|
|
3443
|
+
};
|
|
3444
|
+
var MemoryTokenStorage = class {
|
|
3445
|
+
_credentials = null;
|
|
3446
|
+
async load() {
|
|
3447
|
+
return this._credentials;
|
|
3448
|
+
}
|
|
3449
|
+
async save(credentials) {
|
|
3450
|
+
this._credentials = { ...credentials };
|
|
3451
|
+
}
|
|
3452
|
+
async clear() {
|
|
3453
|
+
this._credentials = null;
|
|
3454
|
+
}
|
|
3455
|
+
};
|
|
3456
|
+
function formatDeviceCodeInstructions(issuance) {
|
|
3457
|
+
return [
|
|
3458
|
+
"",
|
|
3459
|
+
" To sign in, open your browser and visit:",
|
|
3460
|
+
"",
|
|
3461
|
+
` ${issuance.verification_uri}`,
|
|
3462
|
+
"",
|
|
3463
|
+
" Then enter this code:",
|
|
3464
|
+
"",
|
|
3465
|
+
` ${issuance.user_code}`,
|
|
3466
|
+
"",
|
|
3467
|
+
` This code expires in ${Math.floor(issuance.expires_in / 60)} minutes.`,
|
|
3468
|
+
""
|
|
3469
|
+
].join("\n");
|
|
3470
|
+
}
|
|
3471
|
+
function formatDeviceCodeLink(issuance) {
|
|
3472
|
+
return `\x1B]8;;${issuance.verification_uri_complete}\x07Open browser to approve\x1B]8;;\x07`;
|
|
3473
|
+
}
|
|
3474
|
+
async function openBrowser(url) {
|
|
3475
|
+
const { spawn } = await import("node:child_process");
|
|
3476
|
+
const openers = {
|
|
3477
|
+
darwin: { cmd: "open", args: [url] },
|
|
3478
|
+
win32: { cmd: "cmd", args: ["/c", "start", "", url] },
|
|
3479
|
+
linux: { cmd: "xdg-open", args: [url] }
|
|
3480
|
+
};
|
|
3481
|
+
const opener = openers[process.platform];
|
|
3482
|
+
if (!opener) return false;
|
|
3483
|
+
return new Promise((resolve) => {
|
|
3484
|
+
const child = spawn(opener.cmd, opener.args, { stdio: "ignore", shell: false, detached: true });
|
|
3485
|
+
child.on("error", () => resolve(false));
|
|
3486
|
+
child.on("close", (code) => resolve(code === 0));
|
|
3487
|
+
child.unref();
|
|
3488
|
+
});
|
|
3489
|
+
}
|
|
3490
|
+
function formatWorkspaceList(workspaces) {
|
|
3491
|
+
const lines = ["", " Available workspaces:", ""];
|
|
3492
|
+
workspaces.forEach((ws, i) => {
|
|
3493
|
+
const name = ws.name ?? ws.workspace_id;
|
|
3494
|
+
const role = ws.role ? ` (${ws.role})` : "";
|
|
3495
|
+
lines.push(` ${i + 1}. ${name}${role}`);
|
|
3496
|
+
if (ws.name) lines.push(` ${ws.workspace_id}`);
|
|
3497
|
+
});
|
|
3498
|
+
lines.push("");
|
|
3499
|
+
return lines.join("\n");
|
|
3500
|
+
}
|
|
3501
|
+
|
|
3066
3502
|
// src/index.ts
|
|
3067
3503
|
var DEFAULT_BASE_URL = "https://api.platform.amigo.ai";
|
|
3068
3504
|
var AmigoClient = class _AmigoClient {
|
|
@@ -3239,14 +3675,21 @@ export {
|
|
|
3239
3675
|
ConfigurationError,
|
|
3240
3676
|
ConflictError,
|
|
3241
3677
|
DEFAULT_BASE_URL,
|
|
3678
|
+
DeviceCodeDeniedError,
|
|
3679
|
+
DeviceCodeExpiredError,
|
|
3680
|
+
FileTokenStorage,
|
|
3681
|
+
LoginCancelledError,
|
|
3682
|
+
MemoryTokenStorage,
|
|
3242
3683
|
NetworkError,
|
|
3243
3684
|
NotFoundError,
|
|
3244
3685
|
ParseError,
|
|
3245
3686
|
PermissionError,
|
|
3246
3687
|
RateLimitError,
|
|
3688
|
+
RefreshTokenExpiredError,
|
|
3247
3689
|
RequestTimeoutError,
|
|
3248
3690
|
ServerError,
|
|
3249
3691
|
ServiceUnavailableError,
|
|
3692
|
+
TokenManager,
|
|
3250
3693
|
ValidationError,
|
|
3251
3694
|
WebhookVerificationError,
|
|
3252
3695
|
actionId,
|
|
@@ -3259,6 +3702,9 @@ export {
|
|
|
3259
3702
|
entityId,
|
|
3260
3703
|
eventId,
|
|
3261
3704
|
extractRequestId,
|
|
3705
|
+
formatDeviceCodeInstructions,
|
|
3706
|
+
formatDeviceCodeLink,
|
|
3707
|
+
formatWorkspaceList,
|
|
3262
3708
|
functionId,
|
|
3263
3709
|
integrationId,
|
|
3264
3710
|
isAmigoError,
|
|
@@ -3266,6 +3712,8 @@ export {
|
|
|
3266
3712
|
isNotFoundError,
|
|
3267
3713
|
isRateLimitError,
|
|
3268
3714
|
isRequestTimeoutError,
|
|
3715
|
+
loginWithDeviceCode,
|
|
3716
|
+
openBrowser,
|
|
3269
3717
|
paginate,
|
|
3270
3718
|
parseRateLimitHeaders,
|
|
3271
3719
|
parseWebhookEvent,
|