@tryvira/opencode 0.1.14 → 0.1.16
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 +2 -2
- package/dist/index.js +80 -52
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# `@tryvira/opencode` 0.1.
|
|
1
|
+
# `@tryvira/opencode` 0.1.15
|
|
2
2
|
|
|
3
3
|
OpenCode V2 server plugin for Vira host lifecycle governance. The package default
|
|
4
4
|
exports the V2 setup surface plus a compatibility `server()` transport required
|
|
@@ -7,7 +7,7 @@ exported separately as `v1Fallback` and is not the old unconditional-deny path.
|
|
|
7
7
|
|
|
8
8
|
It resolves Vira behavior on every `context` hook, observes tool results, and in `enforce` mode blocks tool execution unless a host action authorizer returns an exact Vira `ALLOW` receipt. OpenCode V2 does not expose a supported response-before-display hook, so post-model verification is reported as unsupported rather than claimed as enforced.
|
|
9
9
|
|
|
10
|
-
Configuration is supplied through OpenCode plugin options or environment variables: `VIRA_CONTROL_URL`, `VIRA_API_KEY`, `VIRA_ORGANIZATION_ID`, `VIRA_PROJECT_ID`, `VIRA_ENVIRONMENT_ID`, `VIRA_AI_SYSTEM_ID`, `VIRA_CONNECTION_ID`, and `VIRA_HOST_MODE` (`off`, `observe`, `enforce`).
|
|
10
|
+
Configuration is supplied through OpenCode plugin options or environment variables: `VIRA_CONTROL_URL`, `VIRA_API_KEY`, `VIRA_ORGANIZATION_ID`, `VIRA_PROJECT_ID`, `VIRA_ENVIRONMENT_ID`, `VIRA_AI_SYSTEM_ID`, `VIRA_CONNECTION_ID`, and `VIRA_HOST_MODE` (`off`, `observe`, `enforce`). In enforce mode, provide an explicit `confirmAction(proposal)` callback for effectful actions. The callback must return `true` only after the customer confirms the exact tool and arguments; the plugin then creates a server-owned one-use approval bound to that proposal. A denial, expiry, changed argument set, or replay remains blocked. The plugin never treats model text or `confirmed:true` supplied by a caller as confirmation.
|
|
11
11
|
For an isolated loopback-only test boundary, `VIRA_ALLOW_INSECURE_LOCALHOST=true` may be set; production API URLs must remain HTTPS.
|
|
12
12
|
|
|
13
13
|
`enforce` requires a host-provided `authorizeAction` callback that calls the
|
package/dist/index.js
CHANGED
|
@@ -507,7 +507,12 @@ var ViraClient = class {
|
|
|
507
507
|
async createHostActionApproval(input) {
|
|
508
508
|
required(input.sessionId, "SDK_ACTION_SESSION_REQUIRED");
|
|
509
509
|
required(input.dataVersion, "SDK_ACTION_DATA_VERSION_REQUIRED");
|
|
510
|
-
|
|
510
|
+
required(input.confirmation.proposalId, "SDK_ACTION_CONFIRMATION_PROPOSAL_REQUIRED");
|
|
511
|
+
required(input.confirmation.toolName, "SDK_ACTION_CONFIRMATION_TOOL_REQUIRED");
|
|
512
|
+
required(input.confirmation.argumentsDigest, "SDK_ACTION_CONFIRMATION_ARGUMENTS_REQUIRED");
|
|
513
|
+
required(input.confirmation.requestContextDigest, "SDK_ACTION_CONFIRMATION_CONTEXT_REQUIRED");
|
|
514
|
+
required(input.confirmation.acknowledgedAt, "SDK_ACTION_CONFIRMATION_TIME_REQUIRED");
|
|
515
|
+
return this.request({ method: "POST", path: "/v1/runtime/actions/approvals", body: { sessionId: input.sessionId, dataVersion: input.dataVersion, proposal: input.proposal, confirmation: input.confirmation, ...input.ttlSeconds !== void 0 ? { ttlSeconds: input.ttlSeconds } : {} } });
|
|
511
516
|
}
|
|
512
517
|
listBehaviorStrategies() {
|
|
513
518
|
return this.request({
|
|
@@ -1250,9 +1255,9 @@ function createViraHostRuntime(client, config) {
|
|
|
1250
1255
|
const observation = await afterTool({ ...proposal, ok: true, result, resultDigest, ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1251
1256
|
return { decision, executed: true, result, observation };
|
|
1252
1257
|
} catch (error) {
|
|
1253
|
-
const
|
|
1254
|
-
const observation = await afterTool({ ...proposal, ok: false, errorCode, resultDigest: digest({ errorCode }), ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1255
|
-
throw Object.assign(error instanceof Error ? error : new Error(
|
|
1258
|
+
const errorCode2 = error instanceof Error ? error.message : "GOVERNED_TOOL_FAILED";
|
|
1259
|
+
const observation = await afterTool({ ...proposal, ok: false, errorCode: errorCode2, resultDigest: digest({ errorCode: errorCode2 }), ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1260
|
+
throw Object.assign(error instanceof Error ? error : new Error(errorCode2), { decision, observation });
|
|
1256
1261
|
}
|
|
1257
1262
|
};
|
|
1258
1263
|
const endSession = (identity) => {
|
|
@@ -1261,11 +1266,40 @@ function createViraHostRuntime(client, config) {
|
|
|
1261
1266
|
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 });
|
|
1262
1267
|
}
|
|
1263
1268
|
|
|
1269
|
+
// src/approval.ts
|
|
1270
|
+
var text = (value) => typeof value === "string" ? value.trim() : "";
|
|
1271
|
+
var record = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1272
|
+
var errorCode = (error) => {
|
|
1273
|
+
const body = record(error instanceof ViraApiError ? error.body : {});
|
|
1274
|
+
return text(body.code) || text(record(body.error).code) || (error instanceof Error ? error.message : "");
|
|
1275
|
+
};
|
|
1276
|
+
async function authorizeWithOneShotCustomerConfirmation(input) {
|
|
1277
|
+
const authorize = (approvalId) => input.client.authorizeHostAction({ executionId: input.executionId, sessionId: input.sessionId, dataVersion: input.dataVersion, scope: input.scope, context: input.context, proposal: input.proposalBody, ...approvalId ? { approval: { approvalId } } : {} });
|
|
1278
|
+
try {
|
|
1279
|
+
const response = await authorize(input.approvalId);
|
|
1280
|
+
const body = record(response?.body);
|
|
1281
|
+
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
|
+
} catch (error) {
|
|
1283
|
+
if (errorCode(error) !== "RUNTIME_ACTION_USER_APPROVAL_REQUIRED" || input.approvalId || typeof input.confirmAction !== "function") return { decision: "BLOCK", reasonCode: errorCode(error) || "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1284
|
+
if (await input.confirmAction({ ...input.proposal, approvalRequired: true }) !== true) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CUSTOMER_CONFIRMATION_DENIED" };
|
|
1285
|
+
const issued = await input.client.createHostActionApproval({ sessionId: input.sessionId, dataVersion: input.dataVersion, proposal: input.proposalBody, confirmation: { proposalId: text(input.proposalBody.proposal_id), toolName: text(input.proposalBody.tool_key), argumentsDigest: text(input.proposalBody.arguments_digest), requestContextDigest: text(input.proposalBody.request_context_digest), acknowledgedAt: (/* @__PURE__ */ new Date()).toISOString() } });
|
|
1286
|
+
const approvalId = text(record(issued?.body).approvalId);
|
|
1287
|
+
if (!approvalId) return { decision: "ERROR", reasonCode: "RUNTIME_ACTION_APPROVAL_RECEIPT_MISSING" };
|
|
1288
|
+
try {
|
|
1289
|
+
const response = await authorize(approvalId);
|
|
1290
|
+
const body = record(response?.body);
|
|
1291
|
+
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) };
|
|
1292
|
+
} catch (retryError) {
|
|
1293
|
+
return { decision: "BLOCK", reasonCode: errorCode(retryError) || "RUNTIME_ACTION_APPROVAL_REJECTED" };
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1264
1298
|
// src/index.ts
|
|
1265
1299
|
var Plugin = { define: (plugin) => plugin };
|
|
1266
|
-
var
|
|
1300
|
+
var text2 = (value) => typeof value === "string" ? value : "";
|
|
1267
1301
|
var required2 = (value, code) => {
|
|
1268
|
-
const result =
|
|
1302
|
+
const result = text2(value).trim();
|
|
1269
1303
|
if (!result) throw new Error(code);
|
|
1270
1304
|
return result;
|
|
1271
1305
|
};
|
|
@@ -1276,9 +1310,9 @@ var lastUserText = (event) => {
|
|
|
1276
1310
|
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
1277
1311
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
1278
1312
|
const message = asRecord(messages[index]);
|
|
1279
|
-
if (message.role === "user") return
|
|
1313
|
+
if (message.role === "user") return text2(message.content ?? message.text);
|
|
1280
1314
|
}
|
|
1281
|
-
return
|
|
1315
|
+
return text2(event.input ?? event.prompt);
|
|
1282
1316
|
};
|
|
1283
1317
|
var ViraOpenCodeActionError = class extends Error {
|
|
1284
1318
|
decision;
|
|
@@ -1307,8 +1341,8 @@ var v2Plugin = Plugin.define({
|
|
|
1307
1341
|
const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
|
|
1308
1342
|
const callKey = (proposal) => contextKey(proposal) + ":" + proposal.callId;
|
|
1309
1343
|
const clearSessionState = (event) => {
|
|
1310
|
-
const sessionId =
|
|
1311
|
-
const conversationId =
|
|
1344
|
+
const sessionId = text2(event.sessionID ?? event.sessionId);
|
|
1345
|
+
const conversationId = text2(event.conversationId ?? event.sessionID ?? event.sessionId);
|
|
1312
1346
|
for (const [key, bound] of actionContexts) {
|
|
1313
1347
|
if (sessionId && bound.sessionId === sessionId || conversationId && bound.conversationId === conversationId) actionContexts.delete(key);
|
|
1314
1348
|
}
|
|
@@ -1324,22 +1358,16 @@ var v2Plugin = Plugin.define({
|
|
|
1324
1358
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1325
1359
|
}
|
|
1326
1360
|
const tools = asRecord(bound.context.tools);
|
|
1327
|
-
const allowedToolIds = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(
|
|
1328
|
-
const schemaDigest =
|
|
1329
|
-
const bindingDigest =
|
|
1361
|
+
const allowedToolIds = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(text2) : [];
|
|
1362
|
+
const schemaDigest = text2(tools.schema_digest);
|
|
1363
|
+
const bindingDigest = text2(bound.context.binding_digest);
|
|
1330
1364
|
if (!allowedToolIds.includes(proposal.toolName) || !/^[a-f0-9]{64}$/.test(schemaDigest) || !/^[a-f0-9]{64}$/.test(bindingDigest)) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_BINDING_UNAVAILABLE" };
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
const response = await client.authorizeHostAction({ executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, dataVersion, scope, context: bound.context, proposal: proposalBody, ...approval ? { approval } : {} });
|
|
1338
|
-
const body = asRecord(response?.body);
|
|
1339
|
-
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) };
|
|
1340
|
-
} catch (error) {
|
|
1341
|
-
return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1342
|
-
}
|
|
1365
|
+
const proposalBody = { 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 ?? "") };
|
|
1366
|
+
const approvalCandidate = asRecord(asRecord(proposal).approval);
|
|
1367
|
+
const approval = typeof approvalCandidate.approvalId === "string" && approvalCandidate.approvalId.trim() ? { approvalId: approvalCandidate.approvalId.trim() } : void 0;
|
|
1368
|
+
const dataVersion = text2(bound.context.release_digest);
|
|
1369
|
+
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1370
|
+
return authorizeWithOneShotCustomerConfirmation({ client, proposal, proposalBody, executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, dataVersion, scope, context: bound.context, confirmAction: options.confirmAction, approvalId: approval?.approvalId });
|
|
1343
1371
|
}), ...(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" } } });
|
|
1344
1372
|
let connectedAiSystemId = options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID") ?? "";
|
|
1345
1373
|
let connectedConnectionId = options.connectionId ?? env("VIRA_CONNECTION_ID") ?? "";
|
|
@@ -1350,46 +1378,46 @@ var v2Plugin = Plugin.define({
|
|
|
1350
1378
|
connectedConnectionId = identity?.connectionId ?? "";
|
|
1351
1379
|
}
|
|
1352
1380
|
const registrations = [];
|
|
1353
|
-
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.conversationId ?? event.sessionID, "VIRA_HOST_SESSION_REQUIRED"), sessionId:
|
|
1381
|
+
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.conversationId ?? event.sessionID, "VIRA_HOST_SESSION_REQUIRED"), sessionId: text2(event.sessionId ?? event.sessionID ?? event.conversationId) || void 0 });
|
|
1354
1382
|
registrations.push(await ctx.session.hook("context", async (event) => {
|
|
1355
1383
|
const identity = sessionIdentity(event);
|
|
1356
|
-
const result = await runtime.beforeModel({ ...identity, requestId:
|
|
1384
|
+
const result = await runtime.beforeModel({ ...identity, requestId: text2(event.messageID ?? event.requestId) || `opencode:${Date.now()}`, input: required2(lastUserText(event), "VIRA_HOST_MODEL_INPUT_REQUIRED"), traceId: text2(event.traceID), turnId: text2(event.turnID) });
|
|
1357
1385
|
const responseBody = asRecord(result.response?.body);
|
|
1358
1386
|
const runtimeContext = asRecord(event.viraRuntimeContext ?? responseBody.requestContext ?? responseBody.context);
|
|
1359
|
-
const executionId =
|
|
1387
|
+
const executionId = text2(event.executionId ?? responseBody.executionId);
|
|
1360
1388
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1361
1389
|
if (executionId && runtimeContext.abi === "vira.request-behavior-context.v2" && Number.isFinite(expiresAt) && expiresAt > Date.now()) actionContexts.set(contextKey(identity), { executionId, context: runtimeContext, sessionId: identity.sessionId ?? identity.conversationId, conversationId: identity.conversationId, expiresAt });
|
|
1362
1390
|
else if (executionId) actionContexts.delete(contextKey(identity));
|
|
1363
1391
|
if (result.status === "applied" && result.projection) {
|
|
1364
1392
|
const system = Array.isArray(event.system) ? event.system : event.system = [];
|
|
1365
|
-
if (!system.some((item) =>
|
|
1393
|
+
if (!system.some((item) => text2(asRecord(item).text) === result.projection)) system.push({ type: "text", text: result.projection });
|
|
1366
1394
|
}
|
|
1367
1395
|
}));
|
|
1368
1396
|
registrations.push(await ctx.tool.hook("execute.before", async (event) => {
|
|
1369
1397
|
const identity = sessionIdentity(event);
|
|
1370
|
-
const proposal = { ...identity, requestId:
|
|
1398
|
+
const proposal = { ...identity, requestId: text2(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: text2(event.traceID), turnId: text2(event.turnID) };
|
|
1371
1399
|
const decision = await runtime.beforeTool(proposal);
|
|
1372
1400
|
actionDecisions.set(callKey(proposal), decision);
|
|
1373
|
-
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision,
|
|
1401
|
+
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text2(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
|
|
1374
1402
|
}));
|
|
1375
1403
|
registrations.push(await ctx.tool.hook("execute.after", async (event) => {
|
|
1376
1404
|
const identity = sessionIdentity(event);
|
|
1377
|
-
const proposal = { ...identity, requestId:
|
|
1405
|
+
const proposal = { ...identity, requestId: text2(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: text2(event.traceID), turnId: text2(event.turnID) };
|
|
1378
1406
|
const decision = actionDecisions.get(callKey(proposal));
|
|
1379
1407
|
actionDecisions.delete(callKey(proposal));
|
|
1380
1408
|
try {
|
|
1381
|
-
await runtime.afterTool({ ...proposal, ok: event.status === "completed", result: event.result, errorCode:
|
|
1409
|
+
await runtime.afterTool({ ...proposal, ok: event.status === "completed", result: event.result, errorCode: text2(asRecord(event.error).code), ...decision?.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision?.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1382
1410
|
} catch (error) {
|
|
1383
1411
|
if (mode === "enforce") throw error;
|
|
1384
1412
|
}
|
|
1385
1413
|
}));
|
|
1386
1414
|
registrations.push(await ctx.session.hook("fork", (event) => {
|
|
1387
1415
|
clearSessionState(event);
|
|
1388
|
-
return runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId:
|
|
1416
|
+
return runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId: text2(event.sessionID), sessionId: text2(event.sessionID) || void 0 });
|
|
1389
1417
|
}));
|
|
1390
1418
|
registrations.push(await ctx.session.hook("compaction", (event) => {
|
|
1391
1419
|
clearSessionState(event);
|
|
1392
|
-
runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId:
|
|
1420
|
+
runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId: text2(event.sessionID ?? event.conversationId), sessionId: text2(event.sessionID ?? event.conversationId) || void 0 });
|
|
1393
1421
|
}));
|
|
1394
1422
|
return async () => {
|
|
1395
1423
|
actionContexts.clear();
|
|
@@ -1415,16 +1443,16 @@ var v1Runtime = () => {
|
|
|
1415
1443
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1416
1444
|
}
|
|
1417
1445
|
const tools = asRecord(bound.context.tools);
|
|
1418
|
-
const allowed = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(
|
|
1419
|
-
const schemaDigest =
|
|
1420
|
-
const bindingDigest =
|
|
1421
|
-
const dataVersion =
|
|
1446
|
+
const allowed = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(text2) : [];
|
|
1447
|
+
const schemaDigest = text2(tools.schema_digest);
|
|
1448
|
+
const bindingDigest = text2(bound.context.binding_digest);
|
|
1449
|
+
const dataVersion = text2(bound.context.release_digest);
|
|
1422
1450
|
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1423
1451
|
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" };
|
|
1424
1452
|
try {
|
|
1425
1453
|
const response = await client.authorizeHostAction({ executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, dataVersion, 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 } });
|
|
1426
1454
|
const body = asRecord(response?.body);
|
|
1427
|
-
return body.allowed === true ? { decision: "ALLOW", authorityReceiptDigest:
|
|
1455
|
+
return body.allowed === true ? { decision: "ALLOW", authorityReceiptDigest: text2(body.authority_receipt_digest), decisionDigest: text2(body.decision_digest) } : { decision: "BLOCK", reasonCode: text2(body.reason_code) || "POLICY_DENIED", authorityReceiptDigest: text2(body.authority_receipt_digest) };
|
|
1428
1456
|
} catch (error) {
|
|
1429
1457
|
return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1430
1458
|
}
|
|
@@ -1439,23 +1467,23 @@ var v1Fallback = { async server() {
|
|
|
1439
1467
|
const connectedIdentity = runtime.getConnectionIdentity();
|
|
1440
1468
|
const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
|
|
1441
1469
|
const prompts = /* @__PURE__ */ new Map();
|
|
1442
|
-
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:
|
|
1443
|
-
const partsText = (value) => Array.isArray(value) ? value.map((part) =>
|
|
1470
|
+
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: text2(input.sessionID ?? input.sessionId ?? input.conversationId) || void 0 });
|
|
1471
|
+
const partsText = (value) => Array.isArray(value) ? value.map((part) => text2(asRecord(part).text)).filter(Boolean).join("\n") : "";
|
|
1444
1472
|
return {
|
|
1445
1473
|
"chat.message": async (input, output) => {
|
|
1446
|
-
const sessionId =
|
|
1474
|
+
const sessionId = text2(input.sessionID ?? input.conversationId ?? input.id);
|
|
1447
1475
|
const message = asRecord(output.message ?? input.message);
|
|
1448
|
-
const prompt =
|
|
1476
|
+
const prompt = text2(message.text ?? message.content) || partsText(output.parts ?? input.parts);
|
|
1449
1477
|
if (sessionId && prompt.trim()) prompts.set(sessionId, prompt);
|
|
1450
1478
|
},
|
|
1451
1479
|
"chat.params": async (input, output) => {
|
|
1452
|
-
const sessionId =
|
|
1453
|
-
const prompt =
|
|
1480
|
+
const sessionId = text2(input.sessionID ?? input.conversationId ?? input.id);
|
|
1481
|
+
const prompt = text2(input.prompt ?? asRecord(input.message).text ?? input.input ?? output.prompt) || prompts.get(sessionId) || "";
|
|
1454
1482
|
if (!prompt.trim()) return;
|
|
1455
|
-
const result = await runtime.beforeModel({ ...identity(input), requestId:
|
|
1483
|
+
const result = await runtime.beforeModel({ ...identity(input), requestId: text2(input.messageID ?? input.requestId) || `opencode:${Date.now()}`, input: prompt, traceId: text2(input.traceID), turnId: text2(input.turnID) });
|
|
1456
1484
|
const responseBody = asRecord(result.response?.body);
|
|
1457
1485
|
const runtimeContext = asRecord(responseBody.requestContext ?? responseBody.context);
|
|
1458
|
-
const executionId =
|
|
1486
|
+
const executionId = text2(input.executionId ?? responseBody.executionId);
|
|
1459
1487
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1460
1488
|
const current = identity(input);
|
|
1461
1489
|
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 });
|
|
@@ -1463,17 +1491,17 @@ var v1Fallback = { async server() {
|
|
|
1463
1491
|
if (result.status === "applied" && result.projection) output.system = [...Array.isArray(output.system) ? output.system : [], result.projection];
|
|
1464
1492
|
},
|
|
1465
1493
|
"tool.execute.before": async (input, output) => {
|
|
1466
|
-
const proposal = { ...identity(input), requestId:
|
|
1494
|
+
const proposal = { ...identity(input), requestId: text2(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: text2(input.traceID), turnId: text2(input.turnID) };
|
|
1467
1495
|
const decision = await runtime.beforeTool(proposal);
|
|
1468
1496
|
actionDecisions.set(contextKey(proposal) + ":" + proposal.callId, decision);
|
|
1469
|
-
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision,
|
|
1497
|
+
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text2(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
|
|
1470
1498
|
},
|
|
1471
1499
|
"tool.execute.after": async (input, output) => {
|
|
1472
|
-
const proposal = { ...identity(input), requestId:
|
|
1500
|
+
const proposal = { ...identity(input), requestId: text2(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: text2(input.traceID), turnId: text2(input.turnID) };
|
|
1473
1501
|
const decision = actionDecisions.get(contextKey(proposal) + ":" + proposal.callId);
|
|
1474
1502
|
actionDecisions.delete(contextKey(proposal) + ":" + proposal.callId);
|
|
1475
1503
|
try {
|
|
1476
|
-
await runtime.afterTool({ ...proposal, ok: true, result: output.output, errorCode:
|
|
1504
|
+
await runtime.afterTool({ ...proposal, ok: true, result: output.output, errorCode: text2(asRecord(output.error).code), ...decision?.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision?.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1477
1505
|
} catch (error) {
|
|
1478
1506
|
if (mode === "enforce") throw error;
|
|
1479
1507
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/opencode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
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/index.js", "README.md"],
|
|
9
9
|
"scripts": {"build": "node ../../scripts/build-opencode-plugin.mjs", "pack:check": "npm run build && npm pack --dry-run && node ../../scripts/opencode-plugin-smoke.mjs"},
|
|
10
|
-
"dependencies": {"@tryvira/sdk": "0.1.
|
|
10
|
+
"dependencies": {"@tryvira/sdk": "0.1.56"},
|
|
11
11
|
"peerDependencies": {"effect": "^4.0.0"},
|
|
12
12
|
"engines": {"node": ">=20"},
|
|
13
13
|
"license": "MIT"
|