@tryvira/opencode 0.1.9 → 0.1.10

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # `@tryvira/opencode` 0.1.9
1
+ # `@tryvira/opencode` 0.1.10
2
2
 
3
3
  OpenCode V2 server plugin for Vira host lifecycle governance. The package default
4
4
  export is the V2 lifecycle plugin; the legacy V1 surface is exported separately
@@ -24,4 +24,7 @@ the server expiry, and is discarded on fork, compaction, expiry, or plugin
24
24
  shutdown. Tool proposals must be present in the server-issued tool allowlist
25
25
  and carry the context's schema and binding digests. Tool result and failure
26
26
  observations retain the matching authority and decision digests. V1 hook names
27
- remain a separate compatibility surface and do not provide V2 action context.
27
+ remain a separate compatibility surface. On OpenCode 1.19.x, which only accepts
28
+ the `server()` contract, that fallback uses the same session-bound context,
29
+ expiry, digest, authorization, and receipt rules rather than an unconditional
30
+ deny path.
package/dist/index.js CHANGED
@@ -1389,17 +1389,40 @@ var v1Runtime = () => {
1389
1389
  if (mode === "off") return null;
1390
1390
  const scope = { organizationId: required2(env("VIRA_ORGANIZATION_ID"), "VIRA_HOST_ORGANIZATION_REQUIRED"), projectId: required2(env("VIRA_PROJECT_ID"), "VIRA_HOST_PROJECT_REQUIRED"), environmentId: required2(env("VIRA_ENVIRONMENT_ID"), "VIRA_HOST_ENVIRONMENT_REQUIRED") };
1391
1391
  const client = new ViraClient({ baseUrl: required2(env("VIRA_CONTROL_URL") ?? env("VIRA_API_URL"), "VIRA_HOST_API_URL_REQUIRED"), apiKey: env("VIRA_API_KEY"), allowInsecureLocalhost: env("VIRA_ALLOW_INSECURE_LOCALHOST") === "true" });
1392
- return { mode, scope, client, runtime: createViraHostRuntime(client, { mode, connect: { ...scope, name: "OpenCode", systemType: "agent", adapterType: "custom-node", adapterVersion: "1.19.5", idempotencyKey: "opencode:vira:connect" }, capabilities: { canObserveModel: true, canInjectContext: true, canInterceptToolBefore: true, canInspectToolAfter: true, canPreventSideEffect: mode === "enforce", canInterceptResponseBeforeDisplay: false }, authorizeAction: async () => ({ decision: "BLOCK", reasonCode: "VIRA_V1_ACTION_AUTHORIZER_UNAVAILABLE" }) }) };
1392
+ const actionContexts = /* @__PURE__ */ new Map();
1393
+ const actionDecisions = /* @__PURE__ */ new Map();
1394
+ const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
1395
+ const authorizeAction = async (proposal) => {
1396
+ const bound = actionContexts.get(contextKey(proposal));
1397
+ if (!bound) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_UNAVAILABLE" };
1398
+ if (!Number.isFinite(bound.expiresAt) || Date.now() >= bound.expiresAt) {
1399
+ actionContexts.delete(contextKey(proposal));
1400
+ return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
1401
+ }
1402
+ const tools = asRecord(bound.context.tools);
1403
+ const allowed = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(text) : [];
1404
+ const schemaDigest = text(tools.schema_digest);
1405
+ const bindingDigest = text(bound.context.binding_digest);
1406
+ if (!allowed.includes(proposal.toolName) || !/^[a-f0-9]{64}$/.test(schemaDigest) || !/^[a-f0-9]{64}$/.test(bindingDigest)) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_BINDING_UNAVAILABLE" };
1407
+ try {
1408
+ 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: schemaDigest } });
1409
+ const body = asRecord(response?.body);
1410
+ 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) };
1411
+ } catch (error) {
1412
+ return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
1413
+ }
1414
+ };
1415
+ return { mode, scope, client, actionContexts, actionDecisions, contextKey, runtime: createViraHostRuntime(client, { mode, connect: { ...scope, name: "OpenCode", systemType: "agent", adapterType: "custom-node", adapterVersion: "1.19.5", idempotencyKey: "opencode:vira:connect" }, capabilities: { canObserveModel: true, canInjectContext: true, canInterceptToolBefore: true, canInspectToolAfter: true, canPreventSideEffect: mode === "enforce", canInterceptResponseBeforeDisplay: false }, authorizeAction }) };
1393
1416
  };
1394
1417
  var v1Fallback = { async server() {
1395
1418
  const configured = v1Runtime();
1396
1419
  if (!configured) return {};
1397
- const { mode, scope, runtime } = configured;
1420
+ const { mode, scope, runtime, actionContexts, actionDecisions, contextKey } = configured;
1398
1421
  await runtime.connectOnce();
1399
1422
  const connectedIdentity = runtime.getConnectionIdentity();
1400
1423
  const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
1401
1424
  const prompts = /* @__PURE__ */ new Map();
1402
- const identity = (input) => ({ ...scope, hostName: "opencode", hostVersion: "1.19.5", aiSystemId: required2(connectedIdentity?.aiSystemId ?? env("VIRA_AI_SYSTEM_ID"), "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(connectedIdentity?.connectionId ?? env("VIRA_CONNECTION_ID"), "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(input.sessionID ?? input.conversationId ?? input.id, "VIRA_HOST_SESSION_REQUIRED") });
1425
+ const identity = (input) => ({ ...scope, hostName: "opencode", hostVersion: "1.19.5", aiSystemId: required2(connectedIdentity?.aiSystemId ?? env("VIRA_AI_SYSTEM_ID"), "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(connectedIdentity?.connectionId ?? env("VIRA_CONNECTION_ID"), "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(input.sessionID ?? input.conversationId ?? input.id, "VIRA_HOST_SESSION_REQUIRED"), sessionId: text(input.sessionID ?? input.sessionId ?? input.conversationId) || void 0 });
1403
1426
  const partsText = (value) => Array.isArray(value) ? value.map((part) => text(asRecord(part).text)).filter(Boolean).join("\n") : "";
1404
1427
  return {
1405
1428
  "chat.message": async (input, output) => {
@@ -1413,15 +1436,27 @@ var v1Fallback = { async server() {
1413
1436
  const prompt = text(input.prompt ?? asRecord(input.message).text ?? input.input ?? output.prompt) || prompts.get(sessionId) || "";
1414
1437
  if (!prompt.trim()) return;
1415
1438
  const result = await runtime.beforeModel({ ...identity(input), requestId: text(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, input: prompt, traceId: text(input.traceID), turnId: text(input.turnID) });
1439
+ const responseBody = asRecord(result.response?.body);
1440
+ const runtimeContext = asRecord(responseBody.requestContext ?? responseBody.context);
1441
+ const executionId = text(input.executionId ?? responseBody.executionId);
1442
+ const expiresAt = Number(runtimeContext.expires_at_unix_ms);
1443
+ const current = identity(input);
1444
+ if (executionId && runtimeContext.abi === "vira.request-behavior-context.v2" && Number.isFinite(expiresAt) && expiresAt > Date.now()) actionContexts.set(contextKey(current), { executionId, context: runtimeContext, sessionId: current.sessionId ?? current.conversationId, conversationId: current.conversationId, expiresAt });
1445
+ else actionContexts.delete(contextKey(current));
1416
1446
  if (result.status === "applied" && result.projection) output.system = [...Array.isArray(output.system) ? output.system : [], result.projection];
1417
1447
  },
1418
1448
  "tool.execute.before": async (input, output) => {
1419
- const decision = await runtime.beforeTool({ ...identity(input), requestId: text(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, callId: required2(input.callID ?? input.id, "VIRA_HOST_TOOL_CALL_REQUIRED"), toolName: required2(input.tool ?? input.name, "VIRA_HOST_TOOL_NAME_REQUIRED"), args: asRecord(output.args ?? input.args ?? input.input), traceId: text(input.traceID), turnId: text(input.turnID) });
1449
+ const proposal = { ...identity(input), requestId: text(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, callId: required2(input.callID ?? input.id, "VIRA_HOST_TOOL_CALL_REQUIRED"), toolName: required2(input.tool ?? input.name, "VIRA_HOST_TOOL_NAME_REQUIRED"), args: asRecord(output.args ?? input.args ?? input.input), traceId: text(input.traceID), turnId: text(input.turnID) };
1450
+ const decision = await runtime.beforeTool(proposal);
1451
+ actionDecisions.set(contextKey(proposal) + ":" + proposal.callId, decision);
1420
1452
  if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1421
1453
  },
1422
1454
  "tool.execute.after": async (input, output) => {
1455
+ const proposal = { ...identity(input), requestId: text(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, callId: required2(input.callID ?? input.id, "VIRA_HOST_TOOL_CALL_REQUIRED"), toolName: required2(input.tool ?? input.name, "VIRA_HOST_TOOL_NAME_REQUIRED"), args: asRecord(input.args ?? input.input), traceId: text(input.traceID), turnId: text(input.turnID) };
1456
+ const decision = actionDecisions.get(contextKey(proposal) + ":" + proposal.callId);
1457
+ actionDecisions.delete(contextKey(proposal) + ":" + proposal.callId);
1423
1458
  try {
1424
- await runtime.afterTool({ ...identity(input), requestId: text(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, callId: required2(input.callID ?? input.id, "VIRA_HOST_TOOL_CALL_REQUIRED"), toolName: required2(input.tool ?? input.name, "VIRA_HOST_TOOL_NAME_REQUIRED"), args: asRecord(input.args ?? input.input), ok: true, result: output.output, errorCode: text(asRecord(output.error).code), traceId: text(input.traceID), turnId: text(input.turnID) });
1459
+ await runtime.afterTool({ ...proposal, ok: true, result: output.output, errorCode: text(asRecord(output.error).code), ...decision?.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision?.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
1425
1460
  } catch (error) {
1426
1461
  if (mode === "enforce") throw error;
1427
1462
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryvira/opencode",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "description": "OpenCode V2 host lifecycle integration for Vira",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",