@granular-software/sdk 0.4.10 → 0.4.12

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.mjs CHANGED
@@ -4628,7 +4628,7 @@ var Session = class {
4628
4628
  * Respond to a prompt request from the sandbox
4629
4629
  */
4630
4630
  async answerPrompt(promptId, answer) {
4631
- await this.client.call("prompt.answer", { promptId, value: answer });
4631
+ await this.client.call("prompt.answer", { promptId, answer, value: answer });
4632
4632
  }
4633
4633
  /**
4634
4634
  * Get the current list of available effects.
@@ -4945,13 +4945,31 @@ import { ${allImports} } from "./sandbox-tools";
4945
4945
  });
4946
4946
  }
4947
4947
  setupEventHandlers() {
4948
+ this.client.on("open", (payload) => this.emit("open", payload || {}));
4948
4949
  this.client.on("sync", (doc) => {
4949
4950
  this.currentDomainRevision = this.extractDomainRevisionFromDoc(doc);
4950
4951
  this.emit("sync", doc);
4951
4952
  this.checkForToolChanges();
4952
4953
  });
4953
- this.client.on("prompt", (prompt) => this.emit("prompt", prompt));
4954
+ const emitPrompt = (payload) => {
4955
+ const raw = payload;
4956
+ const prompt = raw.prompt || {
4957
+ id: typeof raw.id === "string" ? raw.id : String(raw.promptId || ""),
4958
+ type: raw.type === "confirm" || raw.type === "choice" || raw.type === "input" ? raw.type : raw.promptType === "confirm" || raw.promptType === "choice" ? raw.promptType : "input",
4959
+ title: typeof raw.title === "string" ? raw.title : "Input required",
4960
+ message: typeof raw.message === "string" ? raw.message : "",
4961
+ options: raw.options,
4962
+ defaultValue: raw.defaultValue,
4963
+ placeholder: raw.placeholder,
4964
+ allowEmpty: raw.allowEmpty,
4965
+ metadata: raw.metadata
4966
+ };
4967
+ this.emit("prompt", prompt);
4968
+ };
4969
+ this.client.on("prompt", emitPrompt);
4970
+ this.client.on("prompt.request", emitPrompt);
4954
4971
  this.client.on("disconnect", (payload) => this.emit("disconnect", payload || {}));
4972
+ this.client.on("reconnect_error", (payload) => this.emit("reconnect_error", payload || {}));
4955
4973
  this.client.on("job.status", (data) => {
4956
4974
  this.emit("job:status", data);
4957
4975
  });
@@ -5023,6 +5041,8 @@ function normalizeJobStatus(status) {
5023
5041
  case "failed":
5024
5042
  case "running":
5025
5043
  case "queued":
5044
+ case "awaitingTool":
5045
+ case "awaitingHuman":
5026
5046
  case "succeeded":
5027
5047
  case "timeout":
5028
5048
  case "canceled":
@@ -5157,6 +5177,19 @@ var JobImplementation = class {
5157
5177
  this.client.on(`job.${id}.error`, (error) => {
5158
5178
  this.finalize("failed", void 0, error);
5159
5179
  });
5180
+ this.client.on("job.status", (data) => {
5181
+ const jobData = data;
5182
+ if (jobData.jobId !== id) {
5183
+ return;
5184
+ }
5185
+ const normalizedStatus = normalizeJobStatus(jobData.status);
5186
+ this.status = normalizedStatus;
5187
+ this.metadata.status = normalizedStatus;
5188
+ if (normalizedStatus === "running") {
5189
+ this.markStarted();
5190
+ }
5191
+ this.emit("status", normalizedStatus);
5192
+ });
5160
5193
  this.client.on("job.completed", (data) => {
5161
5194
  const jobData = data;
5162
5195
  if (jobData.jobId === id) {
@@ -5294,6 +5327,7 @@ var JobImplementation = class {
5294
5327
  // src/endpoints.ts
5295
5328
  var LOCAL_API_URL = "ws://localhost:8787/granular";
5296
5329
  var PRODUCTION_API_URL = "wss://cf-api-gateway.arthur6084.workers.dev/granular";
5330
+ var DEFAULT_LOCAL_API_KEY = "gn_sk_tenant_default_principal_local_e2e_00000000";
5297
5331
  function readEnv(name) {
5298
5332
  if (typeof process === "undefined" || !process.env) return void 0;
5299
5333
  return process.env[name];
@@ -5311,6 +5345,21 @@ function isTruthy(value) {
5311
5345
  const normalized = value.trim().toLowerCase();
5312
5346
  return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
5313
5347
  }
5348
+ function isLocalApiUrl(url) {
5349
+ try {
5350
+ const parsed = new URL(url);
5351
+ return parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1";
5352
+ } catch {
5353
+ return false;
5354
+ }
5355
+ }
5356
+ function resolveAuthTokenForApiUrl(authToken, apiUrl) {
5357
+ if (!authToken.startsWith("sk_") || !isLocalApiUrl(apiUrl) || isTruthy(readEnv("GRANULAR_DISABLE_LOCAL_API_KEY_FALLBACK"))) {
5358
+ return authToken;
5359
+ }
5360
+ const override = readEnv("GRANULAR_LOCAL_API_KEY")?.trim();
5361
+ return override || DEFAULT_LOCAL_API_KEY;
5362
+ }
5314
5363
  function resolveEndpointMode(explicitMode) {
5315
5364
  const explicit = normalizeMode(explicitMode);
5316
5365
  if (explicit === "local" || explicit === "production") {
@@ -6229,8 +6278,8 @@ var Granular = class {
6229
6278
  if (!auth) {
6230
6279
  throw new Error("Granular client requires either apiKey or token. Set GRANULAR_API_KEY or GRANULAR_TOKEN, or pass one in options.");
6231
6280
  }
6232
- this.apiKey = auth;
6233
6281
  this.apiUrl = resolveApiUrl(options.apiUrl, options.endpointMode);
6282
+ this.apiKey = resolveAuthTokenForApiUrl(auth, this.apiUrl);
6234
6283
  this.tokenProvider = options.tokenProvider;
6235
6284
  this.WebSocketCtor = options.WebSocketCtor;
6236
6285
  this.onUnexpectedClose = options.onUnexpectedClose;