@madewithremy/admin 0.1.52 → 0.1.54

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/index.js CHANGED
@@ -59,13 +59,6 @@ function loadWorkspaceAppId(workspaceDir = WORKSPACE_DIR) {
59
59
  return result.value.appId;
60
60
  }
61
61
 
62
- // src/ops/agent.ts
63
- var agent_exports = {};
64
- __export(agent_exports, {
65
- threadsGet: () => threadsGet,
66
- threadsList: () => threadsList
67
- });
68
-
69
62
  // src/errors.ts
70
63
  var AdminApiError = class extends Error {
71
64
  constructor(method, path3, status2, body) {
@@ -180,7 +173,51 @@ async function tryCall(ctx, method, apiPath) {
180
173
  return { ok: res.ok, status: res.status, body: await readBody(res) };
181
174
  }
182
175
 
176
+ // src/runningMethod.ts
177
+ var AGENT_SDK = "@mindstudio-ai/agent";
178
+ var agentSdk;
179
+ var loadAgentSdk = () => agentSdk ??= import(AGENT_SDK).then(
180
+ (mod) => mod,
181
+ () => null
182
+ );
183
+ var appIds = /* @__PURE__ */ new Map();
184
+ var MAX_REMEMBERED = 500;
185
+ function appIdOf(token, baseUrl) {
186
+ let known = appIds.get(token);
187
+ if (!known) {
188
+ known = call(
189
+ { apiKey: token, appId: "", baseUrl },
190
+ "GET",
191
+ "/_internal/v2/execution/identity"
192
+ ).then((identity) => identity.appId);
193
+ known.catch(() => appIds.delete(token));
194
+ if (appIds.size >= MAX_REMEMBERED) {
195
+ appIds.delete(appIds.keys().next().value);
196
+ }
197
+ appIds.set(token, known);
198
+ }
199
+ return known;
200
+ }
201
+ async function runningMethodContext(options) {
202
+ const sdk = await loadAgentSdk();
203
+ const run3 = sdk?.getRequestContext?.();
204
+ if (!run3?.callbackToken) {
205
+ return null;
206
+ }
207
+ const baseUrl = (options.baseUrl ?? run3.remoteHostname ?? process.env["MINDSTUDIO_BASE_URL"] ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
208
+ return {
209
+ apiKey: run3.callbackToken,
210
+ appId: options.appId ?? await appIdOf(run3.callbackToken, baseUrl),
211
+ baseUrl
212
+ };
213
+ }
214
+
183
215
  // src/ops/agent.ts
216
+ var agent_exports = {};
217
+ __export(agent_exports, {
218
+ threadsGet: () => threadsGet,
219
+ threadsList: () => threadsList
220
+ });
184
221
  function threadsList(ctx, params = {}) {
185
222
  return call(
186
223
  ctx,
@@ -1743,7 +1780,7 @@ async function primaryUrl(ctx) {
1743
1780
  return dashboard.primaryUrl;
1744
1781
  }
1745
1782
  function summarizeRelease(release) {
1746
- const summary2 = {
1783
+ const summary3 = {
1747
1784
  releaseId: release.id,
1748
1785
  commitSha: release.commitSha,
1749
1786
  branch: release.branch ?? null,
@@ -1755,16 +1792,16 @@ function summarizeRelease(release) {
1755
1792
  };
1756
1793
  const readers = (release.dataSources ?? []).filter((d) => d.reader);
1757
1794
  if (readers.length > 0 && release.status === "live") {
1758
- summary2.readers = readers.map((d) => ({
1795
+ summary3.readers = readers.map((d) => ({
1759
1796
  slug: d.slug,
1760
1797
  unread: d.readerUnread ?? 0
1761
1798
  }));
1762
1799
  }
1763
1800
  if (release.status !== "failed") {
1764
- return summary2;
1801
+ return summary3;
1765
1802
  }
1766
1803
  return {
1767
- ...summary2,
1804
+ ...summary3,
1768
1805
  error: release.failureReason ?? "Build failed (no error entry in the build log)",
1769
1806
  ...release.failureKind ? { failureKind: release.failureKind } : {},
1770
1807
  ...release.failurePhase ? { failurePhase: release.failurePhase } : {}
@@ -3028,6 +3065,19 @@ function updateSettings(ctx, partial) {
3028
3065
  ).then((res) => res.settings ?? {});
3029
3066
  }
3030
3067
 
3068
+ // src/ops/usage.ts
3069
+ var usage_exports = {};
3070
+ __export(usage_exports, {
3071
+ summary: () => summary2
3072
+ });
3073
+ function summary2(ctx, params) {
3074
+ return call(
3075
+ ctx,
3076
+ "GET",
3077
+ `/_internal/v2/apps/${ctx.appId}/usage/summary${qs(params)}`
3078
+ );
3079
+ }
3080
+
3031
3081
  // src/ops/users.ts
3032
3082
  var users_exports = {};
3033
3083
  __export(users_exports, {
@@ -3183,11 +3233,11 @@ function settingsSet(ctx, params) {
3183
3233
  }
3184
3234
 
3185
3235
  // src/client.ts
3186
- function bindOps(ops, ctx) {
3236
+ function bindOps(ops, source) {
3187
3237
  const bound = {};
3188
3238
  for (const [name, value] of Object.entries(ops)) {
3189
3239
  if (typeof value === "function") {
3190
- bound[name] = (...args) => value(ctx, ...args);
3240
+ bound[name] = async (...args) => value(await source(), ...args);
3191
3241
  }
3192
3242
  }
3193
3243
  return bound;
@@ -3207,71 +3257,103 @@ function resolveOptions(options) {
3207
3257
  baseUrl: options.baseUrl ?? process.env["MINDSTUDIO_BASE_URL"] ?? DEFAULT_BASE_URL
3208
3258
  };
3209
3259
  }
3210
- function buildClient(ctx) {
3260
+ function retarget(ctx, appId) {
3261
+ return {
3262
+ ...ctx,
3263
+ appId,
3264
+ originAppId: appId === ctx.appId ? ctx.originAppId : ctx.originAppId ?? ctx.appId
3265
+ };
3266
+ }
3267
+ function buildClient(source, context) {
3211
3268
  return {
3212
- /** The resolved context this client is bound to. */
3213
- context: ctx,
3269
+ /**
3270
+ * The context the client uses outside a running method: its options, or
3271
+ * the environment. Null when neither names a key, as in a method, where
3272
+ * each call takes the run's own.
3273
+ */
3274
+ context,
3214
3275
  /**
3215
3276
  * A sibling client for another app, sharing credentials.
3216
3277
  *
3217
3278
  * The app we came from becomes the origin, so a write through the returned
3218
3279
  * client records which app acted — the same rule the CLI's `--app` follows.
3219
3280
  * Chaining `forApp` twice keeps the FIRST app as the origin, since that is
3220
- * the one whose workspace this code is running in.
3281
+ * the one whose workspace this code is running in. A method's own
3282
+ * credential acts on its own app only, so from a method this needs a key.
3221
3283
  */
3222
3284
  forApp(appId) {
3223
- return buildClient({
3224
- ...ctx,
3225
- appId,
3226
- originAppId: appId === ctx.appId ? ctx.originAppId : ctx.originAppId ?? ctx.appId
3227
- });
3285
+ return buildClient(
3286
+ async () => retarget(await source(), appId),
3287
+ context && retarget(context, appId)
3288
+ );
3228
3289
  },
3229
- agent: bindOps(agent_exports, ctx),
3230
- tasks: bindOps(tasks_exports, ctx),
3231
- analytics: bindOps(analytics_exports, ctx),
3232
- apps: bindOps(apps_exports, ctx),
3233
- events: bindOps(events_exports, ctx),
3234
- crashes: bindOps(crashes_exports, ctx),
3235
- cron: bindOps(cron_exports, ctx),
3236
- feedback: bindOps(feedback_exports, ctx),
3237
- data: bindOps(data_exports, ctx),
3238
- dataSources: bindOps(dataSources_exports, ctx),
3239
- dataSourceJobs: bindOps(dataSourceJobs_exports, ctx),
3240
- dataSourceConnectors: bindOps(dataSourceConnectors_exports, ctx),
3241
- dataSourceEvals: bindOps(dataSourceEvals_exports, ctx),
3242
- dataSourceMappers: bindOps(dataSourceMappers_exports, ctx),
3243
- db: bindOps(db_exports, ctx),
3244
- diagnostics: bindOps(diagnostics_exports, ctx),
3245
- domains: bindOps(domains_exports, ctx),
3290
+ agent: bindOps(agent_exports, source),
3291
+ tasks: bindOps(tasks_exports, source),
3292
+ analytics: bindOps(analytics_exports, source),
3293
+ apps: bindOps(apps_exports, source),
3294
+ events: bindOps(events_exports, source),
3295
+ crashes: bindOps(crashes_exports, source),
3296
+ cron: bindOps(cron_exports, source),
3297
+ feedback: bindOps(feedback_exports, source),
3298
+ data: bindOps(data_exports, source),
3299
+ dataSources: bindOps(dataSources_exports, source),
3300
+ dataSourceJobs: bindOps(dataSourceJobs_exports, source),
3301
+ dataSourceConnectors: bindOps(dataSourceConnectors_exports, source),
3302
+ dataSourceEvals: bindOps(dataSourceEvals_exports, source),
3303
+ dataSourceMappers: bindOps(dataSourceMappers_exports, source),
3304
+ db: bindOps(db_exports, source),
3305
+ diagnostics: bindOps(diagnostics_exports, source),
3306
+ domains: bindOps(domains_exports, source),
3246
3307
  email: {
3247
- ...bindOps(email_exports, ctx),
3308
+ ...bindOps(email_exports, source),
3248
3309
  // Hand-bound: these are generic over the direction ('sending' |
3249
3310
  // 'inbound') and bindOps' mapped type would erase the generic,
3250
3311
  // collapsing every result to the cross-direction union.
3251
- listDomains: (direction) => listDomains(ctx, direction),
3252
- checkDomain: (direction, domain) => checkDomain(ctx, direction, domain),
3253
- addDomain: (direction, domain) => addDomain(ctx, direction, domain),
3254
- verifyDomain: (direction, domain) => verifyDomain(ctx, direction, domain),
3255
- removeDomain: (direction, domain) => removeDomain(ctx, direction, domain)
3312
+ listDomains: async (direction) => listDomains(await source(), direction),
3313
+ checkDomain: async (direction, domain) => checkDomain(await source(), direction, domain),
3314
+ addDomain: async (direction, domain) => addDomain(await source(), direction, domain),
3315
+ verifyDomain: async (direction, domain) => verifyDomain(await source(), direction, domain),
3316
+ removeDomain: async (direction, domain) => removeDomain(await source(), direction, domain)
3256
3317
  },
3257
- files: bindOps(files_exports, ctx),
3258
- infra: bindOps(infra_exports, ctx),
3259
- issues: bindOps(issues_exports, ctx),
3260
- jewels: bindOps(jewels_exports, ctx),
3261
- methods: bindOps(methods_exports, ctx),
3262
- prerender: bindOps(prerender_exports, ctx),
3263
- previews: bindOps(previews_exports, ctx),
3264
- qaRecordings: bindOps(qaRecordings_exports, ctx),
3265
- releases: bindOps(releases_exports, ctx),
3266
- requests: bindOps(requests_exports, ctx),
3267
- secrets: bindOps(secrets_exports, ctx),
3268
- settings: bindOps(settings_exports, ctx),
3269
- users: bindOps(users_exports, ctx),
3270
- voice: bindOps(voice_exports, ctx)
3318
+ files: bindOps(files_exports, source),
3319
+ infra: bindOps(infra_exports, source),
3320
+ issues: bindOps(issues_exports, source),
3321
+ jewels: bindOps(jewels_exports, source),
3322
+ methods: bindOps(methods_exports, source),
3323
+ prerender: bindOps(prerender_exports, source),
3324
+ previews: bindOps(previews_exports, source),
3325
+ qaRecordings: bindOps(qaRecordings_exports, source),
3326
+ releases: bindOps(releases_exports, source),
3327
+ requests: bindOps(requests_exports, source),
3328
+ secrets: bindOps(secrets_exports, source),
3329
+ settings: bindOps(settings_exports, source),
3330
+ usage: bindOps(usage_exports, source),
3331
+ users: bindOps(users_exports, source),
3332
+ voice: bindOps(voice_exports, source)
3271
3333
  };
3272
3334
  }
3273
3335
  function createAdminClient(options = {}) {
3274
- return buildClient(resolveOptions(options));
3336
+ if (options.apiKey) {
3337
+ const context2 = resolveOptions(options);
3338
+ return buildClient(async () => context2, context2);
3339
+ }
3340
+ let context = null;
3341
+ let unresolved = null;
3342
+ try {
3343
+ context = resolveOptions(options);
3344
+ } catch (err) {
3345
+ unresolved = err;
3346
+ }
3347
+ return buildClient(async () => {
3348
+ const running = await runningMethodContext(options);
3349
+ if (running) {
3350
+ return running;
3351
+ }
3352
+ if (!context) {
3353
+ throw unresolved;
3354
+ }
3355
+ return context;
3356
+ }, context);
3275
3357
  }
3276
3358
 
3277
3359
  // src/index.ts