mitra-interactions-sdk 1.0.60-beta.43 → 1.0.60-beta.44

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.d.mts CHANGED
@@ -327,6 +327,10 @@ interface GetAgentTaskCreateOptions {
327
327
  * texto — use o id que a aplicação já tem no vínculo canal→usuário.
328
328
  * Omitido = chat do caller, byte a byte como sempre.
329
329
  * Com agente AUTÔNOMO é contradição: 400 AUTONOMOUS_TASK_HAS_NO_OWNER.
330
+ * Em contexto ANÔNIMO (SF sem identidade própria, ex.: SF pública), criar
331
+ * chat de agente NORMAL EXIGE userId: 400 ANONYMOUS_CALLER_REQUIRES_OWNER
332
+ * sem ele — a execução anônima não tem identidade própria, nomeie por quem
333
+ * ela age.
330
334
  */
331
335
  userId?: string;
332
336
  /** Transporte do prompt (default 'ws'). */
package/dist/index.d.ts CHANGED
@@ -327,6 +327,10 @@ interface GetAgentTaskCreateOptions {
327
327
  * texto — use o id que a aplicação já tem no vínculo canal→usuário.
328
328
  * Omitido = chat do caller, byte a byte como sempre.
329
329
  * Com agente AUTÔNOMO é contradição: 400 AUTONOMOUS_TASK_HAS_NO_OWNER.
330
+ * Em contexto ANÔNIMO (SF sem identidade própria, ex.: SF pública), criar
331
+ * chat de agente NORMAL EXIGE userId: 400 ANONYMOUS_CALLER_REQUIRES_OWNER
332
+ * sem ele — a execução anônima não tem identidade própria, nomeie por quem
333
+ * ela age.
330
334
  */
331
335
  userId?: string;
332
336
  /** Transporte do prompt (default 'ws'). */
package/dist/index.js CHANGED
@@ -360,14 +360,15 @@ async function tryRefreshToken() {
360
360
  }
361
361
  }
362
362
  async function handleResponse(response) {
363
- var _a, _b, _c, _d;
363
+ var _a, _b, _c, _d, _e;
364
364
  const text = await response.text();
365
365
  const { data, isJson } = parseBody(text);
366
366
  if (!response.ok) {
367
367
  const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
368
- const base = isJson && ((data == null ? void 0 : data.message) || ((_a = data == null ? void 0 : data.error) == null ? void 0 : _a.message) || (data == null ? void 0 : data.error)) || fallback;
368
+ const rawMsg = data == null ? void 0 : data.message;
369
+ const base = isJson && (typeof rawMsg === "string" && rawMsg || rawMsg && typeof rawMsg === "object" && typeof rawMsg.message === "string" && rawMsg.message || typeof ((_a = data == null ? void 0 : data.error) == null ? void 0 : _a.message) === "string" && data.error.message || typeof (data == null ? void 0 : data.error) === "string" && data.error) || fallback;
369
370
  const msg = isJson && (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
370
- const code = isJson && ((_d = (_c = data == null ? void 0 : data.error_code) != null ? _c : (_b = data == null ? void 0 : data.error) == null ? void 0 : _b.code) != null ? _d : data == null ? void 0 : data.code) || void 0;
371
+ const code = isJson && ((_e = (_d = (_c = data == null ? void 0 : data.error_code) != null ? _c : (_b = data == null ? void 0 : data.error) == null ? void 0 : _b.code) != null ? _d : data == null ? void 0 : data.code) != null ? _e : rawMsg && typeof rawMsg === "object" ? rawMsg.code : void 0) || void 0;
371
372
  throw {
372
373
  message: typeof msg === "string" ? msg : fallback,
373
374
  status: response.status,
@@ -550,7 +551,7 @@ var AgentTaskSession = class {
550
551
  if (this._transport === "http") await this._ensureSse();
551
552
  else await this._ensureWs();
552
553
  } catch (err) {
553
- this._emit("error", { error: `Falha ao abrir chat: ${errMsg(err)}` });
554
+ this._emit("error", { ...errCode(err) ? { code: errCode(err) } : {}, error: `Falha ao abrir chat: ${errMsg(err)}` });
554
555
  this._setStatus("error");
555
556
  }
556
557
  }
@@ -624,7 +625,7 @@ var AgentTaskSession = class {
624
625
  ws.send(JSON.stringify(input));
625
626
  }
626
627
  } catch (err) {
627
- this._emit("error", { error: `Falha ao enviar prompt: ${errMsg(err)}` });
628
+ this._emit("error", { ...errCode(err) ? { code: errCode(err) } : {}, error: `Falha ao enviar prompt: ${errMsg(err)}` });
628
629
  if (this._status === "streaming") this._setStatus("idle");
629
630
  }
630
631
  }
@@ -645,13 +646,13 @@ var AgentTaskSession = class {
645
646
  if (this._status === "cancelled") this._setStatus("idle");
646
647
  }, CANCEL_SAFETY_MS);
647
648
  } catch (err) {
648
- this._emit("error", { error: `Falha ao cancelar: ${errMsg(err)}` });
649
+ this._emit("error", { ...errCode(err) ? { code: errCode(err) } : {}, error: `Falha ao cancelar: ${errMsg(err)}` });
649
650
  }
650
651
  }
651
652
  respondApproval(approved) {
652
653
  if (this._transport === "http") {
653
654
  void this._postInput({ type: "approval_response", approved }).catch((err) => {
654
- this._emit("error", { error: `Falha ao aprovar: ${errMsg(err)}` });
655
+ this._emit("error", { ...errCode(err) ? { code: errCode(err) } : {}, error: `Falha ao aprovar: ${errMsg(err)}` });
655
656
  });
656
657
  return;
657
658
  }
@@ -997,9 +998,27 @@ var AgentTaskSession = class {
997
998
  };
998
999
  function errMsg(err) {
999
1000
  if (err instanceof Error) return err.message;
1000
- if (err && typeof err === "object" && "message" in err) return String(err.message);
1001
+ if (err && typeof err === "object" && "message" in err) {
1002
+ const m = err.message;
1003
+ if (typeof m === "string") return m;
1004
+ if (m && typeof m === "object") {
1005
+ if (typeof m.message === "string") return m.code ? `${m.code}: ${m.message}` : m.message;
1006
+ try {
1007
+ return JSON.stringify(m);
1008
+ } catch (e) {
1009
+ return String(m);
1010
+ }
1011
+ }
1012
+ return String(m);
1013
+ }
1001
1014
  return String(err);
1002
1015
  }
1016
+ function errCode(err) {
1017
+ var _a, _b, _c, _d, _e, _f, _g;
1018
+ if (!err || typeof err !== "object") return void 0;
1019
+ const e = err;
1020
+ return (_g = (_f = (_d = (_b = e.code) != null ? _b : (_a = e.message) == null ? void 0 : _a.code) != null ? _d : (_c = e.details) == null ? void 0 : _c.code) != null ? _f : (_e = e.details) == null ? void 0 : _e.error_code) != null ? _g : void 0;
1021
+ }
1003
1022
  function toTimelineItem(message) {
1004
1023
  const tool = message.type === "TOOL_USE" ? toToolEvent(message) : null;
1005
1024
  if (tool) return { id: message.id, kind: "tool", tool, at: message.createdAt };