@tryvira/sdk 0.1.49 → 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
@@ -488,10 +488,11 @@ var ViraClient = class {
488
488
  required(input.scope.projectId, "SDK_ACTION_PROJECT_REQUIRED");
489
489
  required(input.scope.environmentId, "SDK_ACTION_ENVIRONMENT_REQUIRED");
490
490
  required(input.executionId, "SDK_ACTION_EXECUTION_REQUIRED");
491
+ required(input.sessionId, "SDK_ACTION_SESSION_REQUIRED");
491
492
  return this.request({
492
493
  method: "POST",
493
494
  path: "/v1/runtime/actions/authorize",
494
- body: { executionId: input.executionId, scope: input.scope, context: input.context, proposal: input.proposal }
495
+ body: { executionId: input.executionId, sessionId: input.sessionId, scope: input.scope, context: input.context, proposal: input.proposal, ...input.approval ? { approval: input.approval } : {} }
495
496
  });
496
497
  }
497
498
  listBehaviorStrategies() {
@@ -1296,7 +1297,9 @@ function createViraHostRuntime(client, config) {
1296
1297
  if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
1297
1298
  let decision;
1298
1299
  try {
1299
- 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;
1300
1303
  } catch {
1301
1304
  return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
1302
1305
  }
@@ -1311,10 +1314,27 @@ function createViraHostRuntime(client, config) {
1311
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 } : {} };
1312
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");
1313
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
+ };
1314
1334
  const endSession = (identity) => {
1315
1335
  projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
1316
1336
  };
1317
- 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 });
1318
1338
  }
1319
1339
  export {
1320
1340
  ViraApiError,
@@ -368,6 +368,7 @@ export declare class ViraClient {
368
368
  } | null>;
369
369
  authorizeHostAction(input: {
370
370
  executionId: string;
371
+ sessionId: string;
371
372
  scope: {
372
373
  organizationId: string;
373
374
  projectId: string;
@@ -375,6 +376,7 @@ export declare class ViraClient {
375
376
  };
376
377
  context: Record<string, unknown>;
377
378
  proposal: Record<string, unknown>;
379
+ approval?: Record<string, unknown>;
378
380
  }): Promise<ViraResponse<ClientBody> | null>;
379
381
  listBehaviorStrategies(): Promise<ViraResponse<ClientBody> | null>;
380
382
  deleteBehaviorStrategy(strategyId: string, options?: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryvira/sdk",
3
- "version": "0.1.49",
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"},