@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.
@@ -1,4 +1,4 @@
1
- import { n as Prompt, h as RecordObjectOptions, i as ManifestContent, t as EnvironmentSession, m as SessionHeapSnapshot, T as ToolWithHandler, G as Granular, a_ as ConnectOptions, be as CreateEnvironmentData, aY as GranularOptions } from './client-CljnHfK3.mjs';
1
+ import { n as Prompt, h as RecordObjectOptions, i as ManifestContent, t as EnvironmentSession, m as SessionHeapSnapshot, T as ToolWithHandler, G as Granular, a_ as ConnectOptions, be as CreateEnvironmentData, aY as GranularOptions } from './client-BZtpnc8M.mjs';
2
2
  import { GranularAgentToolInfo, GeneratedJobCodeIssue, BuildGranularAgentSystemPromptInput, HarnessRenderedPrompt, HarnessRenderedContinuation, HarnessControllerBudgets } from './agent-harness.mjs';
3
3
  import { G as GranularSpendContext, a as OpenAITokenSpend } from './spend-ZMtzJoMN.mjs';
4
4
  import '@automerge/automerge';
@@ -1,4 +1,4 @@
1
- import { n as Prompt, h as RecordObjectOptions, i as ManifestContent, t as EnvironmentSession, m as SessionHeapSnapshot, T as ToolWithHandler, G as Granular, a_ as ConnectOptions, be as CreateEnvironmentData, aY as GranularOptions } from './client-Dbam34Zn.js';
1
+ import { n as Prompt, h as RecordObjectOptions, i as ManifestContent, t as EnvironmentSession, m as SessionHeapSnapshot, T as ToolWithHandler, G as Granular, a_ as ConnectOptions, be as CreateEnvironmentData, aY as GranularOptions } from './client-C7bj1UeD.js';
2
2
  import { GranularAgentToolInfo, GeneratedJobCodeIssue, BuildGranularAgentSystemPromptInput, HarnessRenderedPrompt, HarnessRenderedContinuation, HarnessControllerBudgets } from './agent-harness.js';
3
3
  import { G as GranularSpendContext, a as OpenAITokenSpend } from './spend-ZMtzJoMN.js';
4
4
  import '@automerge/automerge';
@@ -4019,6 +4019,34 @@ var init_wrapper = __esm({
4019
4019
  }
4020
4020
  });
4021
4021
 
4022
+ // src/api-error.ts
4023
+ var GranularApiError = class extends Error {
4024
+ constructor(message, status, path2, details = {}) {
4025
+ super(message);
4026
+ this.status = status;
4027
+ this.path = path2;
4028
+ this.name = "GranularApiError";
4029
+ this.idempotencyKey = details.idempotencyKey;
4030
+ let body = details.body;
4031
+ if (typeof body === "string") {
4032
+ try {
4033
+ body = JSON.parse(body);
4034
+ } catch {
4035
+ }
4036
+ }
4037
+ this.body = body;
4038
+ const payload = body && typeof body === "object" ? body : void 0;
4039
+ const firstError = Array.isArray(payload?.errors) ? payload.errors[0] : void 0;
4040
+ const extensions = firstError && typeof firstError === "object" ? firstError.extensions : void 0;
4041
+ this.code = [payload?.code, extensions?.code, firstError?.code].find(
4042
+ (value) => typeof value === "string" && value.length > 0
4043
+ ) ?? `HTTP_${status}`;
4044
+ }
4045
+ code;
4046
+ body;
4047
+ idempotencyKey;
4048
+ };
4049
+
4022
4050
  // ../../node_modules/zod/v3/external.js
4023
4051
  var external_exports = {};
4024
4052
  __export(external_exports, {
@@ -14107,8 +14135,11 @@ import { ${tools[0]?.name || "example"} } from "@granular/actions/backend";
14107
14135
  this.emit("prompt:answered", { ...prompt, status: "answered" });
14108
14136
  return;
14109
14137
  }
14138
+ const previous = this.promptCache.get(prompt.id);
14110
14139
  this.promptCache.set(prompt.id, prompt);
14111
- this.emit("prompt", prompt);
14140
+ if (!previous || promptSnapshotFingerprint(previous) !== promptSnapshotFingerprint(prompt)) {
14141
+ this.emit("prompt", prompt);
14142
+ }
14112
14143
  };
14113
14144
  this.client.on("prompt", emitPrompt);
14114
14145
  this.client.on("prompt.request", emitPrompt);
@@ -16942,8 +16973,12 @@ var Environment = class _Environment {
16942
16973
  }
16943
16974
  });
16944
16975
  if (!response.ok) {
16945
- throw new Error(
16946
- `Control Plane API Error (${response.status}): ${await response.text()}`
16976
+ const errorText = await response.text();
16977
+ throw new GranularApiError(
16978
+ `Control Plane API Error (${response.status}): ${errorText}`,
16979
+ response.status,
16980
+ path2,
16981
+ { body: errorText }
16947
16982
  );
16948
16983
  }
16949
16984
  return response.json();
@@ -17000,12 +17035,12 @@ var Environment = class _Environment {
17000
17035
  * );
17001
17036
  * ```
17002
17037
  */
17003
- async graphql(query, variables) {
17038
+ async graphql(query, variables, options) {
17004
17039
  const authToken = await this.resolveAuthToken();
17005
17040
  if (this._beforeMutationSend && isGraphQLMutationDocument(query)) {
17006
17041
  this._beforeMutationSend?.();
17007
17042
  }
17008
- const idempotencyKey = this._graphqlIdempotencyKey?.(query, variables);
17043
+ const idempotencyKey = options?.idempotencyKey || this._graphqlIdempotencyKey?.(query, variables) || `gr1:${Date.now()}:${crypto.randomUUID()}`;
17009
17044
  const response = await fetch(this._apiEndpoint, {
17010
17045
  method: "POST",
17011
17046
  headers: {
@@ -17016,12 +17051,18 @@ var Environment = class _Environment {
17016
17051
  body: JSON.stringify({
17017
17052
  environmentId: this.environmentId,
17018
17053
  query,
17019
- variables
17054
+ variables,
17055
+ ...options?.readPreference ? { readPreference: options.readPreference } : {}
17020
17056
  })
17021
17057
  });
17022
17058
  if (!response.ok) {
17023
17059
  const errorText = await response.text();
17024
- throw new Error(`GraphQL API Error (${response.status}): ${errorText}`);
17060
+ throw new GranularApiError(
17061
+ `GraphQL API Error (${response.status}): ${errorText}`,
17062
+ response.status,
17063
+ this._apiEndpoint,
17064
+ { body: errorText, idempotencyKey }
17065
+ );
17025
17066
  }
17026
17067
  return response.json();
17027
17068
  }
@@ -19173,7 +19214,7 @@ var Granular = class _Granular {
19173
19214
  const outdatedMatches = this.sortEnvironmentsByRecency(
19174
19215
  userEnvironments.filter((environment2) => environment2.tagId === tag.tagId)
19175
19216
  );
19176
- if (outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19217
+ if (!resetKey && outdatedMatches.length > 0 && options.createFreshIfOutdated !== true) {
19177
19218
  requireMatchingEnvironmentProfile(outdatedMatches[0]);
19178
19219
  const environment2 = requireMatchingEnvironmentProfile(
19179
19220
  await activate(outdatedMatches[0])
@@ -20337,6 +20378,9 @@ var Granular = class _Granular {
20337
20378
  )
20338
20379
  );
20339
20380
  },
20381
+ /** Retire a settled environment and release its named slot. Ordinary
20382
+ * access is revoked; durable graph data and history are retained.
20383
+ * Mounted or unsettled environments reject with HTTP409. */
20340
20384
  delete: async (environmentId) => {
20341
20385
  return this.request(
20342
20386
  `/control/environments/${environmentId}`,
@@ -20551,7 +20595,12 @@ var Granular = class _Granular {
20551
20595
  await sleep(LOCAL_CONTROL_REQUEST_RETRY_DELAY_MS * attempt);
20552
20596
  continue;
20553
20597
  }
20554
- throw new Error(`Granular API Error (${response.status}): ${errorText}`);
20598
+ throw new GranularApiError(
20599
+ `Granular API Error (${response.status}): ${errorText}`,
20600
+ response.status,
20601
+ path2,
20602
+ { body: errorText }
20603
+ );
20555
20604
  }
20556
20605
  throw new Error(`Granular API Error: exhausted retries for ${url}`);
20557
20606
  }