@tryvira/sdk 0.1.50 → 0.1.51

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.
@@ -71,6 +71,16 @@ export interface HostLifecycleConfig {
71
71
  projectionMaxBytes?: number;
72
72
  timeoutMs?: number;
73
73
  }
74
+ export interface GovernedToolExecution<T = unknown> {
75
+ proposal: HostToolProposal;
76
+ execute: (args: Record<string, unknown>) => Promise<T> | T;
77
+ }
78
+ export interface GovernedToolResult<T = unknown> {
79
+ decision: HostActionAuthorization;
80
+ executed: boolean;
81
+ result?: T;
82
+ observation?: unknown;
83
+ }
74
84
  export interface HostConnectionIdentity {
75
85
  aiSystemId: string;
76
86
  connectionId: string;
@@ -92,6 +102,7 @@ export declare function createViraHostRuntime(client: ViraClient, config: HostLi
92
102
  beforeModel: (request: HostModelRequest) => Promise<HostProjectionResult>;
93
103
  beforeTool: (proposal: HostToolProposal) => Promise<HostActionAuthorization>;
94
104
  afterTool: (result: HostToolResult) => Promise<unknown>;
105
+ executeTool: <T>(input: GovernedToolExecution<T>) => Promise<GovernedToolResult<T>>;
95
106
  endSession: (identity: HostSessionIdentity) => void;
96
107
  getProjection: (identity: HostSessionIdentity) => HostProjectionResult | undefined;
97
108
  supportsPostModelVerification: boolean;
package/dist/index.js CHANGED
@@ -1297,7 +1297,9 @@ function createViraHostRuntime(client, config) {
1297
1297
  if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
1298
1298
  let decision;
1299
1299
  try {
1300
- decision = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
1300
+ const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
1301
+ const authorizedBody = bodyOf(authorized);
1302
+ decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
1301
1303
  } catch {
1302
1304
  return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
1303
1305
  }
@@ -1312,10 +1314,27 @@ function createViraHostRuntime(client, config) {
1312
1314
  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", requestId: result.requestId, conversationId: result.conversationId, ...result.sessionId ? { sessionId: result.sessionId } : {}, ...result.turnId ? { turnId: result.turnId } : {} };
1313
1315
  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");
1314
1316
  };
1317
+ const executeTool = async (input) => {
1318
+ if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
1319
+ const proposal = input.proposal;
1320
+ const decision = await beforeTool(proposal);
1321
+ if (decision.decision !== "ALLOW") return { decision, executed: false };
1322
+ if (!isAllow(decision)) throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
1323
+ try {
1324
+ const result = await input.execute(proposal.args);
1325
+ const resultDigest = digest(result);
1326
+ const observation = await afterTool({ ...proposal, ok: true, result, resultDigest });
1327
+ return { decision, executed: true, result, observation };
1328
+ } catch (error) {
1329
+ const errorCode = error instanceof Error ? error.message : "GOVERNED_TOOL_FAILED";
1330
+ const observation = await afterTool({ ...proposal, ok: false, errorCode, resultDigest: digest({ errorCode }) });
1331
+ throw Object.assign(error instanceof Error ? error : new Error(errorCode), { decision, observation });
1332
+ }
1333
+ };
1315
1334
  const endSession = (identity) => {
1316
1335
  projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
1317
1336
  };
1318
- return Object.freeze({ mode, capabilities, connectOnce, getConnectionIdentity: () => connectionIdentity, beforeModel, beforeTool, afterTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
1337
+ 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 });
1319
1338
  }
1320
1339
  export {
1321
1340
  ViraApiError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryvira/sdk",
3
- "version": "0.1.50",
3
+ "version": "0.1.51",
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"},