@tryvira/opencode 0.1.3 → 0.1.5

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 +26 -5
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -491,10 +491,11 @@ var ViraClient = class {
491
491
  required(input.scope.projectId, "SDK_ACTION_PROJECT_REQUIRED");
492
492
  required(input.scope.environmentId, "SDK_ACTION_ENVIRONMENT_REQUIRED");
493
493
  required(input.executionId, "SDK_ACTION_EXECUTION_REQUIRED");
494
+ required(input.sessionId, "SDK_ACTION_SESSION_REQUIRED");
494
495
  return this.request({
495
496
  method: "POST",
496
497
  path: "/v1/runtime/actions/authorize",
497
- body: { executionId: input.executionId, scope: input.scope, context: input.context, proposal: input.proposal }
498
+ body: { executionId: input.executionId, sessionId: input.sessionId, scope: input.scope, context: input.context, proposal: input.proposal, ...input.approval ? { approval: input.approval } : {} }
498
499
  });
499
500
  }
500
501
  listBehaviorStrategies() {
@@ -1209,7 +1210,9 @@ function createViraHostRuntime(client, config) {
1209
1210
  if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
1210
1211
  let decision;
1211
1212
  try {
1212
- decision = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
1213
+ const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
1214
+ const authorizedBody = bodyOf(authorized);
1215
+ decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
1213
1216
  } catch {
1214
1217
  return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
1215
1218
  }
@@ -1224,10 +1227,27 @@ function createViraHostRuntime(client, config) {
1224
1227
  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 } : {} };
1225
1228
  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");
1226
1229
  };
1230
+ const executeTool = async (input) => {
1231
+ if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
1232
+ const proposal = input.proposal;
1233
+ const decision = await beforeTool(proposal);
1234
+ if (decision.decision !== "ALLOW") return { decision, executed: false };
1235
+ if (!isAllow(decision)) throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
1236
+ try {
1237
+ const result = await input.execute(proposal.args);
1238
+ const resultDigest = digest(result);
1239
+ const observation = await afterTool({ ...proposal, ok: true, result, resultDigest });
1240
+ return { decision, executed: true, result, observation };
1241
+ } catch (error) {
1242
+ const errorCode = error instanceof Error ? error.message : "GOVERNED_TOOL_FAILED";
1243
+ const observation = await afterTool({ ...proposal, ok: false, errorCode, resultDigest: digest({ errorCode }) });
1244
+ throw Object.assign(error instanceof Error ? error : new Error(errorCode), { decision, observation });
1245
+ }
1246
+ };
1227
1247
  const endSession = (identity) => {
1228
1248
  projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
1229
1249
  };
1230
- return Object.freeze({ mode, capabilities, connectOnce, getConnectionIdentity: () => connectionIdentity, beforeModel, beforeTool, afterTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
1250
+ 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 });
1231
1251
  }
1232
1252
 
1233
1253
  // src/index.ts
@@ -1276,7 +1296,8 @@ var v2Plugin = Plugin.define({
1276
1296
  const bound = actionContexts.get(contextKey);
1277
1297
  if (!bound) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_UNAVAILABLE" };
1278
1298
  try {
1279
- const response = await client.authorizeHostAction({ executionId: bound.executionId, scope, context: bound.context, proposal: { proposal_id: proposal.conversationId + ":" + proposal.callId, tool_key: proposal.toolName, arguments: proposal.args, arguments_digest: digest2(proposal.args), request_context_digest: digest2(bound.context), schema_digest: String(asRecord(asRecord(bound.context).tools).schema_digest ?? "") } });
1299
+ const approval = asRecord(asRecord(proposal).approval);
1300
+ const response = await client.authorizeHostAction({ executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, scope, context: bound.context, proposal: { proposal_id: proposal.conversationId + ":" + proposal.callId, tool_key: proposal.toolName, arguments: proposal.args, arguments_digest: digest2(proposal.args), request_context_digest: digest2(bound.context), schema_digest: String(asRecord(asRecord(bound.context).tools).schema_digest ?? "") }, ...Object.keys(approval).length ? { approval } : {} });
1280
1301
  const body = asRecord(response?.body);
1281
1302
  return body.allowed === true ? { decision: "ALLOW", authorityReceiptDigest: text(body.authority_receipt_digest), decisionDigest: text(body.decision_digest) } : { decision: "BLOCK", reasonCode: text(body.reason_code) || "POLICY_DENIED", authorityReceiptDigest: text(body.authority_receipt_digest) };
1282
1303
  } catch (error) {
@@ -1308,7 +1329,7 @@ var v2Plugin = Plugin.define({
1308
1329
  registrations.push(await ctx.tool.hook("execute.before", async (event) => {
1309
1330
  const identity = sessionIdentity(event);
1310
1331
  const decision = await runtime.beforeTool({ ...identity, requestId: text(event.messageID ?? event.requestId) || `opencode:${Date.now()}`, callId: required2(event.callID ?? event.id, "VIRA_HOST_TOOL_CALL_REQUIRED"), toolName: required2(event.tool ?? event.name, "VIRA_HOST_TOOL_NAME_REQUIRED"), args: asRecord(event.input ?? event.args), traceId: text(event.traceID), turnId: text(event.turnID) });
1311
- if (decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1332
+ if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1312
1333
  }));
1313
1334
  registrations.push(await ctx.tool.hook("execute.after", async (event) => {
1314
1335
  const identity = sessionIdentity(event);
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryvira/opencode",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "OpenCode V2 host lifecycle integration for Vira",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
7
7
  "exports": {".": "./dist/index.js"},
8
8
  "files": ["dist", "README.md"],
9
9
  "scripts": {"build": "node ../../scripts/build-opencode-plugin.mjs", "pack:check": "npm run build && npm pack --dry-run"},
10
- "dependencies": {"@tryvira/sdk": "0.1.49"},
10
+ "dependencies": {"@tryvira/sdk": "0.1.51"},
11
11
  "peerDependencies": {"effect": "^4.0.0"},
12
12
  "engines": {"node": ">=20"},
13
13
  "license": "MIT"