mitra-interactions-sdk 1.0.62 → 1.0.63

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
@@ -387,7 +387,7 @@ function buildUrl(endpoint, params) {
387
387
  async function tryRefreshToken() {
388
388
  var _a, _b;
389
389
  const config = getConfig();
390
- if (!config.token || config.projectId == null) return false;
390
+ if (!config.token || config.projectId == null || !config.baseURL) return false;
391
391
  try {
392
392
  const baseURL = config.baseURL.replace(/\/+$/, "");
393
393
  const resp = await getFetch2()(`${baseURL}/mitraspace/project/refreshedToken/${config.projectId}`, {
@@ -1364,6 +1364,46 @@ function getWsUrl() {
1364
1364
  }
1365
1365
  throw new Error("Agent Chat: agentWsUrl indispon\xEDvel. Passe em configureSdkMitra({ agentWsUrl }) ou rode em uma app publicada pelo Mitra (build-proxy injeta window.__mitraEnv.agentWsUrl).");
1366
1366
  }
1367
+ function isHttpEnabled() {
1368
+ return getConfig().httpEnable === true;
1369
+ }
1370
+ function getAgentHttpBase() {
1371
+ return getWsUrl().split("?")[0].replace(/^ws/i, "http").replace(/\/+$/, "");
1372
+ }
1373
+ async function agentHttpPost(path, body) {
1374
+ const fetchFn = globalThis.fetch;
1375
+ if (typeof fetchFn !== "function") {
1376
+ throw new Error("Agent Chat (HTTP): fetch indispon\xEDvel. Use Node 18+ ou um browser.");
1377
+ }
1378
+ const resp = await fetchFn(`${getAgentHttpBase()}${path}`, {
1379
+ method: "POST",
1380
+ headers: {
1381
+ "Content-Type": "application/json",
1382
+ "Authorization": `Bearer ${getRawToken()}`
1383
+ },
1384
+ body: JSON.stringify(body)
1385
+ });
1386
+ const text = await resp.text().catch(() => "");
1387
+ const data = text ? JSON.parse(text) : null;
1388
+ if (!resp.ok) {
1389
+ throw new Error((data == null ? void 0 : data.error) || (data == null ? void 0 : data.message) || `Agent Chat (HTTP ${resp.status})`);
1390
+ }
1391
+ return data;
1392
+ }
1393
+ async function httpSendPrompt(payload, taskId) {
1394
+ var _a;
1395
+ const data = await agentHttpPost("/prompt", { ...payload, ...taskId ? { taskId } : {} });
1396
+ const resolvedTaskId = (data == null ? void 0 : data.taskId) || taskId || "";
1397
+ const content = (_a = data == null ? void 0 : data.content) != null ? _a : "";
1398
+ if (data == null ? void 0 : data.task) {
1399
+ routeMessage({ type: "task_update", payload: { action: "created", task: data.task } });
1400
+ }
1401
+ if (resolvedTaskId) {
1402
+ routeMessage({ type: "turn_started", taskId: resolvedTaskId });
1403
+ routeMessage({ type: "stream_delta", taskId: resolvedTaskId, payload: { delta: content } });
1404
+ routeMessage({ type: "stream_end", taskId: resolvedTaskId });
1405
+ }
1406
+ }
1367
1407
  function connect() {
1368
1408
  if (ws && ws.readyState === WebSocket.OPEN) {
1369
1409
  return Promise.resolve(ws);
@@ -1452,9 +1492,16 @@ function routeMessage(raw) {
1452
1492
  }
1453
1493
  var transport = {
1454
1494
  async ensureConnected() {
1495
+ if (isHttpEnabled()) return;
1455
1496
  await connect();
1456
1497
  },
1457
1498
  async request(type, payload) {
1499
+ var _a;
1500
+ if (isHttpEnabled()) {
1501
+ const data = await agentHttpPost("/request", { type, payload });
1502
+ if (data && data.ok === false) throw new Error(data.error || "Erro desconhecido");
1503
+ return (_a = data == null ? void 0 : data.data) != null ? _a : data;
1504
+ }
1458
1505
  const socket = await connect();
1459
1506
  const requestId = nextRequestId();
1460
1507
  return new Promise((resolve, reject) => {
@@ -1471,6 +1518,16 @@ var transport = {
1471
1518
  });
1472
1519
  },
1473
1520
  send(type, payload, taskId) {
1521
+ if (isHttpEnabled()) {
1522
+ if (type === "send_prompt") {
1523
+ httpSendPrompt(payload, taskId).catch((err) => {
1524
+ const tid = taskId || "";
1525
+ if (tid) routeMessage({ type: "error", taskId: tid, payload: { error: (err == null ? void 0 : err.message) || String(err) } });
1526
+ else console.error("[agent-chat] httpSendPrompt falhou:", err);
1527
+ });
1528
+ }
1529
+ return;
1530
+ }
1474
1531
  connect().then((socket) => {
1475
1532
  const requestId = nextRequestId();
1476
1533
  socket.send(JSON.stringify({ type, requestId, payload, taskId }));