@tryvira/opencode 0.1.2 → 0.1.4

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 +30 -8
  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() {
@@ -1163,10 +1164,20 @@ function createViraHostRuntime(client, config) {
1163
1164
  if (mode === "enforce" && (!capabilities.canInterceptToolBefore || !capabilities.canPreventSideEffect)) throw new Error("SDK_HOST_ENFORCE_CAPABILITY_REQUIRED");
1164
1165
  if (mode === "enforce" && typeof config.authorizeAction !== "function") throw new Error("SDK_HOST_ACTION_AUTHORIZER_REQUIRED");
1165
1166
  let connected = null;
1167
+ let connectionIdentity = null;
1166
1168
  const projections = /* @__PURE__ */ new Map();
1167
1169
  const connectOnce = async () => {
1168
1170
  if (mode === "off" || !config.connect) return null;
1169
- if (!connected) connected = withTimeout(client.connectOnce(config.connect), timeoutMs, "SDK_HOST_CONNECT_TIMEOUT");
1171
+ if (!connected) connected = withTimeout(client.connectOnce(config.connect), timeoutMs, "SDK_HOST_CONNECT_TIMEOUT").then((result) => {
1172
+ const body = bodyOf(result);
1173
+ const system = body.system && typeof body.system === "object" ? body.system : {};
1174
+ const connection = body.connection && typeof body.connection === "object" ? body.connection : {};
1175
+ const aiSystemId = typeof system.id === "string" ? system.id.trim() : "";
1176
+ const connectionId = typeof connection.id === "string" ? connection.id.trim() : "";
1177
+ if (!aiSystemId || !connectionId) throw new Error("SDK_HOST_CONNECT_IDENTITY_MISSING");
1178
+ connectionIdentity = { aiSystemId, connectionId };
1179
+ return result;
1180
+ });
1170
1181
  return connected;
1171
1182
  };
1172
1183
  const sessionKey = (request) => `${request.organizationId}:${request.projectId}:${request.environmentId}:${request.conversationId}`;
@@ -1217,7 +1228,7 @@ function createViraHostRuntime(client, config) {
1217
1228
  const endSession = (identity) => {
1218
1229
  projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
1219
1230
  };
1220
- return Object.freeze({ mode, capabilities, connectOnce, beforeModel, beforeTool, afterTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
1231
+ return Object.freeze({ mode, capabilities, connectOnce, getConnectionIdentity: () => connectionIdentity, beforeModel, beforeTool, afterTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
1221
1232
  }
1222
1233
 
1223
1234
  // src/index.ts
@@ -1266,15 +1277,24 @@ var v2Plugin = Plugin.define({
1266
1277
  const bound = actionContexts.get(contextKey);
1267
1278
  if (!bound) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_UNAVAILABLE" };
1268
1279
  try {
1269
- 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 ?? "") } });
1280
+ const approval = asRecord(asRecord(proposal).approval);
1281
+ 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 } : {} });
1270
1282
  const body = asRecord(response?.body);
1271
1283
  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) };
1272
1284
  } catch (error) {
1273
1285
  return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
1274
1286
  }
1275
1287
  }), ...(options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID")) && (options.connectionId ?? env("VIRA_CONNECTION_ID")) ? {} : { connect: { ...scope, name: "OpenCode", systemType: "agent", adapterType: "custom-node", adapterVersion: options.hostVersion ?? ctx.app?.version ?? "unknown", idempotencyKey: "opencode:vira:connect" } } });
1288
+ let connectedAiSystemId = options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID") ?? "";
1289
+ let connectedConnectionId = options.connectionId ?? env("VIRA_CONNECTION_ID") ?? "";
1290
+ if (!connectedAiSystemId || !connectedConnectionId) {
1291
+ await runtime.connectOnce();
1292
+ const identity = runtime.getConnectionIdentity();
1293
+ connectedAiSystemId = identity?.aiSystemId ?? "";
1294
+ connectedConnectionId = identity?.connectionId ?? "";
1295
+ }
1276
1296
  const registrations = [];
1277
- const sessionIdentity = (event) => ({ ...scope, hostName: options.hostName ?? "opencode", hostVersion: options.hostVersion ?? ctx.app?.version ?? "unknown", aiSystemId: required2(options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID"), "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(options.connectionId ?? env("VIRA_CONNECTION_ID"), "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(event.sessionID ?? event.conversationId, "VIRA_HOST_SESSION_REQUIRED") });
1297
+ const sessionIdentity = (event) => ({ ...scope, hostName: options.hostName ?? "opencode", hostVersion: options.hostVersion ?? ctx.app?.version ?? "unknown", aiSystemId: required2(connectedAiSystemId, "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(connectedConnectionId, "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(event.sessionID ?? event.conversationId, "VIRA_HOST_SESSION_REQUIRED") });
1278
1298
  registrations.push(await ctx.session.hook("context", async (event) => {
1279
1299
  const identity = sessionIdentity(event);
1280
1300
  const result = await runtime.beforeModel({ ...identity, requestId: text(event.messageID ?? event.requestId) || `opencode:${Date.now()}`, input: required2(lastUserText(event), "VIRA_HOST_MODEL_INPUT_REQUIRED"), traceId: text(event.traceID), turnId: text(event.turnID) });
@@ -1290,7 +1310,7 @@ var v2Plugin = Plugin.define({
1290
1310
  registrations.push(await ctx.tool.hook("execute.before", async (event) => {
1291
1311
  const identity = sessionIdentity(event);
1292
1312
  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) });
1293
- if (decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1313
+ if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1294
1314
  }));
1295
1315
  registrations.push(await ctx.tool.hook("execute.after", async (event) => {
1296
1316
  const identity = sessionIdentity(event);
@@ -1313,15 +1333,17 @@ var v1Runtime = () => {
1313
1333
  if (mode === "off") return null;
1314
1334
  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") };
1315
1335
  const client = new ViraClient({ baseUrl: required2(env("VIRA_CONTROL_URL") ?? env("VIRA_API_URL"), "VIRA_HOST_API_URL_REQUIRED"), apiKey: env("VIRA_API_KEY") });
1316
- return { mode, scope, client, runtime: createViraHostRuntime(client, { mode, capabilities: { canObserveModel: true, canInjectContext: true, canInterceptToolBefore: true, canInspectToolAfter: true, canPreventSideEffect: mode === "enforce", canInterceptResponseBeforeDisplay: false }, authorizeAction: async () => ({ decision: "BLOCK", reasonCode: "VIRA_V1_ACTION_AUTHORIZER_UNAVAILABLE" }) }) };
1336
+ 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" }) }) };
1317
1337
  };
1318
1338
  var index_default = { ...v2Plugin, async server() {
1319
1339
  const configured = v1Runtime();
1320
1340
  if (!configured) return {};
1321
1341
  const { mode, scope, runtime } = configured;
1342
+ await runtime.connectOnce();
1343
+ const connectedIdentity = runtime.getConnectionIdentity();
1322
1344
  const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
1323
1345
  const prompts = /* @__PURE__ */ new Map();
1324
- const identity = (input) => ({ ...scope, hostName: "opencode", hostVersion: "1.19.5", aiSystemId: required2(env("VIRA_AI_SYSTEM_ID"), "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(env("VIRA_CONNECTION_ID"), "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(input.sessionID ?? input.conversationId ?? input.id, "VIRA_HOST_SESSION_REQUIRED") });
1346
+ 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") });
1325
1347
  const partsText = (value) => Array.isArray(value) ? value.map((part) => text(asRecord(part).text)).filter(Boolean).join("\n") : "";
1326
1348
  return {
1327
1349
  "chat.message": async (input, output) => {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryvira/opencode",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
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.48"},
10
+ "dependencies": {"@tryvira/sdk": "0.1.50"},
11
11
  "peerDependencies": {"effect": "^4.0.0"},
12
12
  "engines": {"node": ">=20"},
13
13
  "license": "MIT"