@tryvira/opencode 0.1.0 → 0.1.2

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 +59 -7
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -97,8 +97,11 @@ var ViraClient = class {
97
97
  };
98
98
  if (request.body) headers["content-type"] = "application/json";
99
99
  if (this.apiKey) {
100
- headers.authorization = `Bearer ${this.apiKey}`;
101
- headers["x-vira-api-key"] = this.apiKey;
100
+ if (this.apiKey.startsWith("vira_live_")) headers["x-vira-api-key"] = this.apiKey;
101
+ else {
102
+ headers.authorization = `Bearer ${this.apiKey}`;
103
+ headers["x-vira-api-key"] = this.apiKey;
104
+ }
102
105
  }
103
106
  if (options.idempotencyKey)
104
107
  headers["idempotency-key"] = options.idempotencyKey;
@@ -472,11 +475,13 @@ var ViraClient = class {
472
475
  semanticContext: {
473
476
  taskKey: receipt.taskKey,
474
477
  behaviorPoint,
475
- capsule: firstCapsule
478
+ capsule: firstCapsule,
479
+ ...input.requestId ? { requestId: input.requestId } : {},
480
+ ...input.sessionId ? { conversationId: input.sessionId } : {}
476
481
  },
477
482
  idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
478
483
  }) : await this.resolveRuntime({
479
- semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule },
484
+ semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule, ...input.requestId ? { requestId: input.requestId } : {}, ...input.sessionId ? { conversationId: input.sessionId } : {} },
480
485
  idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
481
486
  });
482
487
  return { semantic, runtime };
@@ -1242,7 +1247,7 @@ var ViraOpenCodeActionError = class extends Error {
1242
1247
  this.decision = decision;
1243
1248
  }
1244
1249
  };
1245
- var index_default = Plugin.define({
1250
+ var v2Plugin = Plugin.define({
1246
1251
  id: "vira",
1247
1252
  async setup(rawContext) {
1248
1253
  const ctx = rawContext;
@@ -1267,7 +1272,7 @@ var index_default = Plugin.define({
1267
1272
  } catch (error) {
1268
1273
  return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
1269
1274
  }
1270
- }), connect: { ...scope, name: "OpenCode", systemType: "agent", adapterType: "opencode", adapterVersion: options.hostVersion ?? ctx.app?.version ?? "unknown", idempotencyKey: "opencode:vira:connect" } });
1275
+ }), ...(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" } } });
1271
1276
  const registrations = [];
1272
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") });
1273
1278
  registrations.push(await ctx.session.hook("context", async (event) => {
@@ -1289,7 +1294,11 @@ var index_default = Plugin.define({
1289
1294
  }));
1290
1295
  registrations.push(await ctx.tool.hook("execute.after", async (event) => {
1291
1296
  const identity = sessionIdentity(event);
1292
- await runtime.afterTool({ ...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), ok: event.status === "completed", result: event.result, errorCode: text(asRecord(event.error).code), traceId: text(event.traceID), turnId: text(event.turnID) });
1297
+ try {
1298
+ await runtime.afterTool({ ...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), ok: event.status === "completed", result: event.result, errorCode: text(asRecord(event.error).code), traceId: text(event.traceID), turnId: text(event.turnID) });
1299
+ } catch (error) {
1300
+ if (mode === "enforce") throw error;
1301
+ }
1293
1302
  }));
1294
1303
  registrations.push(await ctx.session.hook("fork", (event) => runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId: text(event.sessionID) })));
1295
1304
  return async () => {
@@ -1298,6 +1307,49 @@ var index_default = Plugin.define({
1298
1307
  };
1299
1308
  }
1300
1309
  });
1310
+ var v1Runtime = () => {
1311
+ const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
1312
+ const mode = env("VIRA_HOST_MODE") ?? "off";
1313
+ if (mode === "off") return null;
1314
+ 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
+ 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" }) }) };
1317
+ };
1318
+ var index_default = { ...v2Plugin, async server() {
1319
+ const configured = v1Runtime();
1320
+ if (!configured) return {};
1321
+ const { mode, scope, runtime } = configured;
1322
+ const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
1323
+ 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") });
1325
+ const partsText = (value) => Array.isArray(value) ? value.map((part) => text(asRecord(part).text)).filter(Boolean).join("\n") : "";
1326
+ return {
1327
+ "chat.message": async (input, output) => {
1328
+ const sessionId = text(input.sessionID ?? input.conversationId ?? input.id);
1329
+ const message = asRecord(output.message ?? input.message);
1330
+ const prompt = text(message.text ?? message.content) || partsText(output.parts ?? input.parts);
1331
+ if (sessionId && prompt.trim()) prompts.set(sessionId, prompt);
1332
+ },
1333
+ "chat.params": async (input, output) => {
1334
+ const sessionId = text(input.sessionID ?? input.conversationId ?? input.id);
1335
+ const prompt = text(input.prompt ?? asRecord(input.message).text ?? input.input ?? output.prompt) || prompts.get(sessionId) || "";
1336
+ if (!prompt.trim()) return;
1337
+ 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) });
1338
+ if (result.status === "applied" && result.projection) output.system = [...Array.isArray(output.system) ? output.system : [], result.projection];
1339
+ },
1340
+ "tool.execute.before": async (input, output) => {
1341
+ 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) });
1342
+ if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
1343
+ },
1344
+ "tool.execute.after": async (input, output) => {
1345
+ try {
1346
+ 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) });
1347
+ } catch (error) {
1348
+ if (mode === "enforce") throw error;
1349
+ }
1350
+ }
1351
+ };
1352
+ } };
1301
1353
  export {
1302
1354
  ViraOpenCodeActionError,
1303
1355
  index_default as default
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tryvira/opencode",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
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.46"},
10
+ "dependencies": {"@tryvira/sdk": "0.1.48"},
11
11
  "peerDependencies": {"effect": "^4.0.0"},
12
12
  "engines": {"node": ">=20"},
13
13
  "license": "MIT"