@granular-software/sdk 0.4.75 → 0.4.77

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.
@@ -3993,6 +3993,34 @@ var init_wrapper = __esm({
3993
3993
  }
3994
3994
  });
3995
3995
 
3996
+ // src/api-error.ts
3997
+ var GranularApiError = class extends Error {
3998
+ constructor(message, status, path2, details = {}) {
3999
+ super(message);
4000
+ this.status = status;
4001
+ this.path = path2;
4002
+ this.name = "GranularApiError";
4003
+ this.idempotencyKey = details.idempotencyKey;
4004
+ let body = details.body;
4005
+ if (typeof body === "string") {
4006
+ try {
4007
+ body = JSON.parse(body);
4008
+ } catch {
4009
+ }
4010
+ }
4011
+ this.body = body;
4012
+ const payload = body && typeof body === "object" ? body : void 0;
4013
+ const firstError = Array.isArray(payload?.errors) ? payload.errors[0] : void 0;
4014
+ const extensions = firstError && typeof firstError === "object" ? firstError.extensions : void 0;
4015
+ this.code = [payload?.code, extensions?.code, firstError?.code].find(
4016
+ (value) => typeof value === "string" && value.length > 0
4017
+ ) ?? `HTTP_${status}`;
4018
+ }
4019
+ code;
4020
+ body;
4021
+ idempotencyKey;
4022
+ };
4023
+
3996
4024
  // ../../node_modules/zod/v3/external.js
3997
4025
  var external_exports = {};
3998
4026
  __export(external_exports, {
@@ -14081,8 +14109,11 @@ import { ${tools[0]?.name || "example"} } from "@granular/actions/backend";
14081
14109
  this.emit("prompt:answered", { ...prompt, status: "answered" });
14082
14110
  return;
14083
14111
  }
14112
+ const previous = this.promptCache.get(prompt.id);
14084
14113
  this.promptCache.set(prompt.id, prompt);
14085
- this.emit("prompt", prompt);
14114
+ if (!previous || promptSnapshotFingerprint(previous) !== promptSnapshotFingerprint(prompt)) {
14115
+ this.emit("prompt", prompt);
14116
+ }
14086
14117
  };
14087
14118
  this.client.on("prompt", emitPrompt);
14088
14119
  this.client.on("prompt.request", emitPrompt);
@@ -16916,8 +16947,12 @@ var Environment = class _Environment {
16916
16947
  }
16917
16948
  });
16918
16949
  if (!response.ok) {
16919
- throw new Error(
16920
- `Control Plane API Error (${response.status}): ${await response.text()}`
16950
+ const errorText = await response.text();
16951
+ throw new GranularApiError(
16952
+ `Control Plane API Error (${response.status}): ${errorText}`,
16953
+ response.status,
16954
+ path2,
16955
+ { body: errorText }
16921
16956
  );
16922
16957
  }
16923
16958
  return response.json();
@@ -16974,12 +17009,12 @@ var Environment = class _Environment {
16974
17009
  * );
16975
17010
  * ```
16976
17011
  */
16977
- async graphql(query, variables) {
17012
+ async graphql(query, variables, options) {
16978
17013
  const authToken = await this.resolveAuthToken();
16979
17014
  if (this._beforeMutationSend && isGraphQLMutationDocument(query)) {
16980
17015
  this._beforeMutationSend?.();
16981
17016
  }
16982
- const idempotencyKey = this._graphqlIdempotencyKey?.(query, variables);
17017
+ const idempotencyKey = options?.idempotencyKey || this._graphqlIdempotencyKey?.(query, variables) || `gr1:${Date.now()}:${crypto.randomUUID()}`;
16983
17018
  const response = await fetch(this._apiEndpoint, {
16984
17019
  method: "POST",
16985
17020
  headers: {
@@ -16990,12 +17025,18 @@ var Environment = class _Environment {
16990
17025
  body: JSON.stringify({
16991
17026
  environmentId: this.environmentId,
16992
17027
  query,
16993
- variables
17028
+ variables,
17029
+ ...options?.readPreference ? { readPreference: options.readPreference } : {}
16994
17030
  })
16995
17031
  });
16996
17032
  if (!response.ok) {
16997
17033
  const errorText = await response.text();
16998
- throw new Error(`GraphQL API Error (${response.status}): ${errorText}`);
17034
+ throw new GranularApiError(
17035
+ `GraphQL API Error (${response.status}): ${errorText}`,
17036
+ response.status,
17037
+ this._apiEndpoint,
17038
+ { body: errorText, idempotencyKey }
17039
+ );
16999
17040
  }
17000
17041
  return response.json();
17001
17042
  }
@@ -19147,7 +19188,7 @@ var Granular = class _Granular {
19147
19188
  const outdatedMatches = this.sortEnvironmentsByRecency(
19148
19189
  userEnvironments.filter((environment2) => environment2.tagId === tag.tagId)
19149
19190
  );
19150
- if (outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19191
+ if (!resetKey && outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19151
19192
  requireMatchingEnvironmentProfile(outdatedMatches[0]);
19152
19193
  const environment2 = requireMatchingEnvironmentProfile(
19153
19194
  await activate(outdatedMatches[0])
@@ -20311,6 +20352,9 @@ var Granular = class _Granular {
20311
20352
  )
20312
20353
  );
20313
20354
  },
20355
+ /** Retire a settled environment and release its named slot. Ordinary
20356
+ * access is revoked; durable graph data and history are retained.
20357
+ * Mounted or unsettled environments reject with HTTP409. */
20314
20358
  delete: async (environmentId) => {
20315
20359
  return this.request(
20316
20360
  `/control/environments/${environmentId}`,
@@ -20525,7 +20569,12 @@ var Granular = class _Granular {
20525
20569
  await sleep(LOCAL_CONTROL_REQUEST_RETRY_DELAY_MS * attempt);
20526
20570
  continue;
20527
20571
  }
20528
- throw new Error(`Granular API Error (${response.status}): ${errorText}`);
20572
+ throw new GranularApiError(
20573
+ `Granular API Error (${response.status}): ${errorText}`,
20574
+ response.status,
20575
+ path2,
20576
+ { body: errorText }
20577
+ );
20529
20578
  }
20530
20579
  throw new Error(`Granular API Error: exhausted retries for ${url}`);
20531
20580
  }