@tryvira/sdk 0.1.57 → 0.1.58

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.
Files changed (2) hide show
  1. package/dist/index.js +21 -3
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1288,6 +1288,7 @@ function createViraHostRuntime(client, config) {
1288
1288
  let connected = null;
1289
1289
  let connectionIdentity = null;
1290
1290
  const projections = /* @__PURE__ */ new Map();
1291
+ const activeToolSessions = /* @__PURE__ */ new Set();
1291
1292
  const connectOnce = async () => {
1292
1293
  if (mode === "off" || !config.connect) return null;
1293
1294
  if (!connected) connected = withTimeout(client.connectOnce(config.connect), timeoutMs, "SDK_HOST_CONNECT_TIMEOUT").then((result) => {
@@ -1330,24 +1331,40 @@ function createViraHostRuntime(client, config) {
1330
1331
  nonEmpty(proposal.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
1331
1332
  nonEmpty(proposal.toolName, "SDK_HOST_TOOL_NAME_REQUIRED");
1332
1333
  if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
1334
+ const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
1335
+ if (activeToolSessions.has(toolSessionKey)) return { decision: "BLOCK", reasonCode: "SDK_HOST_TOOL_IN_FLIGHT" };
1336
+ activeToolSessions.add(toolSessionKey);
1333
1337
  let decision;
1334
1338
  try {
1335
1339
  const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
1336
1340
  const authorizedBody = bodyOf(authorized);
1337
1341
  decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
1338
1342
  } catch {
1343
+ activeToolSessions.delete(toolSessionKey);
1339
1344
  return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
1340
1345
  }
1341
- if (!decision || !["ALLOW", "BLOCK", "ESCALATE", "ABSTAIN", "ERROR"].includes(decision.decision)) throw new Error("SDK_HOST_ACTION_DECISION_INVALID");
1342
- if (decision.decision === "ALLOW" && !isAllow(decision)) throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
1346
+ if (!decision || !["ALLOW", "BLOCK", "ESCALATE", "ABSTAIN", "ERROR"].includes(decision.decision)) {
1347
+ activeToolSessions.delete(toolSessionKey);
1348
+ throw new Error("SDK_HOST_ACTION_DECISION_INVALID");
1349
+ }
1350
+ if (decision.decision !== "ALLOW") activeToolSessions.delete(toolSessionKey);
1351
+ if (decision.decision === "ALLOW" && !isAllow(decision)) {
1352
+ activeToolSessions.delete(toolSessionKey);
1353
+ throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
1354
+ }
1343
1355
  return decision;
1344
1356
  };
1345
1357
  const afterTool = async (result) => {
1346
1358
  nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
1347
1359
  if (mode === "off" || !capabilities.canInspectToolAfter) return { status: "off" };
1360
+ const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
1348
1361
  const observedResult = result.ok ? result.result : { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" };
1349
1362
  const evidence = { abi: "vira.host-tool-evidence.v1", eventId: result.callId, callId: result.callId, toolName: result.toolName, argumentsDigest: digest(result.args), resultDigest: result.resultDigest ?? digest(observedResult), outcome: result.ok ? "success" : "failure", ...result.authorityReceiptDigest ? { authorityReceiptDigest: result.authorityReceiptDigest } : {}, ...result.decisionDigest ? { decisionDigest: result.decisionDigest } : {}, requestId: result.requestId, conversationId: result.conversationId, ...result.sessionId ? { sessionId: result.sessionId } : {}, ...result.turnId ? { turnId: result.turnId } : {} };
1350
- return withTimeout(client.observe({ eventId: result.callId, eventType: result.ok ? "tool.result" : "tool.failed", aiSystemId: result.aiSystemId, connectionId: result.connectionId, organizationId: result.organizationId, projectId: result.projectId, environmentId: result.environmentId, conversationId: result.conversationId, requestId: result.requestId, traceId: result.traceId, payload: { result: observedResult, evidence }, resultDigest: evidence.resultDigest }), timeoutMs, "SDK_HOST_OBSERVE_TIMEOUT");
1363
+ try {
1364
+ return await withTimeout(client.observe({ eventId: result.callId, eventType: result.ok ? "tool.result" : "tool.failed", aiSystemId: result.aiSystemId, connectionId: result.connectionId, organizationId: result.organizationId, projectId: result.projectId, environmentId: result.environmentId, conversationId: result.conversationId, requestId: result.requestId, traceId: result.traceId, payload: { result: observedResult, evidence }, resultDigest: evidence.resultDigest }), timeoutMs, "SDK_HOST_OBSERVE_TIMEOUT");
1365
+ } finally {
1366
+ activeToolSessions.delete(toolSessionKey);
1367
+ }
1351
1368
  };
1352
1369
  const executeTool = async (input) => {
1353
1370
  if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
@@ -1368,6 +1385,7 @@ function createViraHostRuntime(client, config) {
1368
1385
  };
1369
1386
  const endSession = (identity) => {
1370
1387
  projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
1388
+ activeToolSessions.delete(`${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`);
1371
1389
  };
1372
1390
  return Object.freeze({ mode, capabilities, connectOnce, getConnectionIdentity: () => connectionIdentity, beforeModel, beforeTool, afterTool, executeTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
1373
1391
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryvira/sdk",
3
- "version": "0.1.57",
3
+ "version": "0.1.58",
4
4
  "description": "Typed Vira SDK for connect-once, observation and governed runtime execution.",
5
5
  "keywords": ["vira", "ai-governance", "ai-runtime", "semantic-routing"],
6
6
  "engines": {"node": ">=20"},