@tryvira/opencode 0.1.13 → 0.1.15
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 +78 -50
- 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
|
@@ -497,10 +497,11 @@ var ViraClient = class {
|
|
|
497
497
|
required(input.scope.environmentId, "SDK_ACTION_ENVIRONMENT_REQUIRED");
|
|
498
498
|
required(input.executionId, "SDK_ACTION_EXECUTION_REQUIRED");
|
|
499
499
|
required(input.sessionId, "SDK_ACTION_SESSION_REQUIRED");
|
|
500
|
+
required(input.dataVersion, "SDK_ACTION_DATA_VERSION_REQUIRED");
|
|
500
501
|
return this.request({
|
|
501
502
|
method: "POST",
|
|
502
503
|
path: "/v1/runtime/actions/authorize",
|
|
503
|
-
body: { executionId: input.executionId, sessionId: input.sessionId, scope: input.scope, context: input.context, proposal: input.proposal, ...input.approval ? { approval: input.approval } : {} }
|
|
504
|
+
body: { executionId: input.executionId, sessionId: input.sessionId, dataVersion: input.dataVersion, scope: input.scope, context: input.context, proposal: input.proposal, ...input.approval ? { approval: input.approval } : {} }
|
|
504
505
|
});
|
|
505
506
|
}
|
|
506
507
|
async createHostActionApproval(input) {
|
|
@@ -1249,9 +1250,9 @@ function createViraHostRuntime(client, config) {
|
|
|
1249
1250
|
const observation = await afterTool({ ...proposal, ok: true, result, resultDigest, ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1250
1251
|
return { decision, executed: true, result, observation };
|
|
1251
1252
|
} catch (error) {
|
|
1252
|
-
const
|
|
1253
|
-
const observation = await afterTool({ ...proposal, ok: false, errorCode, resultDigest: digest({ errorCode }), ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1254
|
-
throw Object.assign(error instanceof Error ? error : new Error(
|
|
1253
|
+
const errorCode2 = error instanceof Error ? error.message : "GOVERNED_TOOL_FAILED";
|
|
1254
|
+
const observation = await afterTool({ ...proposal, ok: false, errorCode: errorCode2, resultDigest: digest({ errorCode: errorCode2 }), ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1255
|
+
throw Object.assign(error instanceof Error ? error : new Error(errorCode2), { decision, observation });
|
|
1255
1256
|
}
|
|
1256
1257
|
};
|
|
1257
1258
|
const endSession = (identity) => {
|
|
@@ -1260,11 +1261,40 @@ function createViraHostRuntime(client, config) {
|
|
|
1260
1261
|
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 });
|
|
1261
1262
|
}
|
|
1262
1263
|
|
|
1264
|
+
// src/approval.ts
|
|
1265
|
+
var text = (value) => typeof value === "string" ? value.trim() : "";
|
|
1266
|
+
var record = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1267
|
+
var errorCode = (error) => {
|
|
1268
|
+
const body = record(error instanceof ViraApiError ? error.body : {});
|
|
1269
|
+
return text(body.code) || text(record(body.error).code) || (error instanceof Error ? error.message : "");
|
|
1270
|
+
};
|
|
1271
|
+
async function authorizeWithOneShotCustomerConfirmation(input) {
|
|
1272
|
+
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 } } : {} });
|
|
1273
|
+
try {
|
|
1274
|
+
const response = await authorize(input.approvalId);
|
|
1275
|
+
const body = record(response?.body);
|
|
1276
|
+
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) };
|
|
1277
|
+
} catch (error) {
|
|
1278
|
+
if (errorCode(error) !== "RUNTIME_ACTION_USER_APPROVAL_REQUIRED" || input.approvalId || typeof input.confirmAction !== "function") return { decision: "BLOCK", reasonCode: errorCode(error) || "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1279
|
+
if (await input.confirmAction({ ...input.proposal, approvalRequired: true }) !== true) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CUSTOMER_CONFIRMATION_DENIED" };
|
|
1280
|
+
const issued = await input.client.createHostActionApproval({ sessionId: input.sessionId, dataVersion: input.dataVersion, proposal: input.proposalBody, confirmed: true });
|
|
1281
|
+
const approvalId = text(record(issued?.body).approvalId);
|
|
1282
|
+
if (!approvalId) return { decision: "ERROR", reasonCode: "RUNTIME_ACTION_APPROVAL_RECEIPT_MISSING" };
|
|
1283
|
+
try {
|
|
1284
|
+
const response = await authorize(approvalId);
|
|
1285
|
+
const body = record(response?.body);
|
|
1286
|
+
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) };
|
|
1287
|
+
} catch (retryError) {
|
|
1288
|
+
return { decision: "BLOCK", reasonCode: errorCode(retryError) || "RUNTIME_ACTION_APPROVAL_REJECTED" };
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1263
1293
|
// src/index.ts
|
|
1264
1294
|
var Plugin = { define: (plugin) => plugin };
|
|
1265
|
-
var
|
|
1295
|
+
var text2 = (value) => typeof value === "string" ? value : "";
|
|
1266
1296
|
var required2 = (value, code) => {
|
|
1267
|
-
const result =
|
|
1297
|
+
const result = text2(value).trim();
|
|
1268
1298
|
if (!result) throw new Error(code);
|
|
1269
1299
|
return result;
|
|
1270
1300
|
};
|
|
@@ -1275,9 +1305,9 @@ var lastUserText = (event) => {
|
|
|
1275
1305
|
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
1276
1306
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
1277
1307
|
const message = asRecord(messages[index]);
|
|
1278
|
-
if (message.role === "user") return
|
|
1308
|
+
if (message.role === "user") return text2(message.content ?? message.text);
|
|
1279
1309
|
}
|
|
1280
|
-
return
|
|
1310
|
+
return text2(event.input ?? event.prompt);
|
|
1281
1311
|
};
|
|
1282
1312
|
var ViraOpenCodeActionError = class extends Error {
|
|
1283
1313
|
decision;
|
|
@@ -1306,8 +1336,8 @@ var v2Plugin = Plugin.define({
|
|
|
1306
1336
|
const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
|
|
1307
1337
|
const callKey = (proposal) => contextKey(proposal) + ":" + proposal.callId;
|
|
1308
1338
|
const clearSessionState = (event) => {
|
|
1309
|
-
const sessionId =
|
|
1310
|
-
const conversationId =
|
|
1339
|
+
const sessionId = text2(event.sessionID ?? event.sessionId);
|
|
1340
|
+
const conversationId = text2(event.conversationId ?? event.sessionID ?? event.sessionId);
|
|
1311
1341
|
for (const [key, bound] of actionContexts) {
|
|
1312
1342
|
if (sessionId && bound.sessionId === sessionId || conversationId && bound.conversationId === conversationId) actionContexts.delete(key);
|
|
1313
1343
|
}
|
|
@@ -1323,20 +1353,16 @@ var v2Plugin = Plugin.define({
|
|
|
1323
1353
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1324
1354
|
}
|
|
1325
1355
|
const tools = asRecord(bound.context.tools);
|
|
1326
|
-
const allowedToolIds = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(
|
|
1327
|
-
const schemaDigest =
|
|
1328
|
-
const bindingDigest =
|
|
1356
|
+
const allowedToolIds = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(text2) : [];
|
|
1357
|
+
const schemaDigest = text2(tools.schema_digest);
|
|
1358
|
+
const bindingDigest = text2(bound.context.binding_digest);
|
|
1329
1359
|
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" };
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
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) };
|
|
1337
|
-
} catch (error) {
|
|
1338
|
-
return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1339
|
-
}
|
|
1360
|
+
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 ?? "") };
|
|
1361
|
+
const approvalCandidate = asRecord(asRecord(proposal).approval);
|
|
1362
|
+
const approval = typeof approvalCandidate.approvalId === "string" && approvalCandidate.approvalId.trim() ? { approvalId: approvalCandidate.approvalId.trim() } : void 0;
|
|
1363
|
+
const dataVersion = text2(bound.context.release_digest);
|
|
1364
|
+
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1365
|
+
return authorizeWithOneShotCustomerConfirmation({ client, proposal, proposalBody, executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, dataVersion, scope, context: bound.context, confirmAction: options.confirmAction, approvalId: approval?.approvalId });
|
|
1340
1366
|
}), ...(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" } } });
|
|
1341
1367
|
let connectedAiSystemId = options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID") ?? "";
|
|
1342
1368
|
let connectedConnectionId = options.connectionId ?? env("VIRA_CONNECTION_ID") ?? "";
|
|
@@ -1347,46 +1373,46 @@ var v2Plugin = Plugin.define({
|
|
|
1347
1373
|
connectedConnectionId = identity?.connectionId ?? "";
|
|
1348
1374
|
}
|
|
1349
1375
|
const registrations = [];
|
|
1350
|
-
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:
|
|
1376
|
+
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 });
|
|
1351
1377
|
registrations.push(await ctx.session.hook("context", async (event) => {
|
|
1352
1378
|
const identity = sessionIdentity(event);
|
|
1353
|
-
const result = await runtime.beforeModel({ ...identity, requestId:
|
|
1379
|
+
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) });
|
|
1354
1380
|
const responseBody = asRecord(result.response?.body);
|
|
1355
1381
|
const runtimeContext = asRecord(event.viraRuntimeContext ?? responseBody.requestContext ?? responseBody.context);
|
|
1356
|
-
const executionId =
|
|
1382
|
+
const executionId = text2(event.executionId ?? responseBody.executionId);
|
|
1357
1383
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1358
1384
|
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 });
|
|
1359
1385
|
else if (executionId) actionContexts.delete(contextKey(identity));
|
|
1360
1386
|
if (result.status === "applied" && result.projection) {
|
|
1361
1387
|
const system = Array.isArray(event.system) ? event.system : event.system = [];
|
|
1362
|
-
if (!system.some((item) =>
|
|
1388
|
+
if (!system.some((item) => text2(asRecord(item).text) === result.projection)) system.push({ type: "text", text: result.projection });
|
|
1363
1389
|
}
|
|
1364
1390
|
}));
|
|
1365
1391
|
registrations.push(await ctx.tool.hook("execute.before", async (event) => {
|
|
1366
1392
|
const identity = sessionIdentity(event);
|
|
1367
|
-
const proposal = { ...identity, requestId:
|
|
1393
|
+
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) };
|
|
1368
1394
|
const decision = await runtime.beforeTool(proposal);
|
|
1369
1395
|
actionDecisions.set(callKey(proposal), decision);
|
|
1370
|
-
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision,
|
|
1396
|
+
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text2(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
|
|
1371
1397
|
}));
|
|
1372
1398
|
registrations.push(await ctx.tool.hook("execute.after", async (event) => {
|
|
1373
1399
|
const identity = sessionIdentity(event);
|
|
1374
|
-
const proposal = { ...identity, requestId:
|
|
1400
|
+
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) };
|
|
1375
1401
|
const decision = actionDecisions.get(callKey(proposal));
|
|
1376
1402
|
actionDecisions.delete(callKey(proposal));
|
|
1377
1403
|
try {
|
|
1378
|
-
await runtime.afterTool({ ...proposal, ok: event.status === "completed", result: event.result, errorCode:
|
|
1404
|
+
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 } : {} });
|
|
1379
1405
|
} catch (error) {
|
|
1380
1406
|
if (mode === "enforce") throw error;
|
|
1381
1407
|
}
|
|
1382
1408
|
}));
|
|
1383
1409
|
registrations.push(await ctx.session.hook("fork", (event) => {
|
|
1384
1410
|
clearSessionState(event);
|
|
1385
|
-
return runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId:
|
|
1411
|
+
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 });
|
|
1386
1412
|
}));
|
|
1387
1413
|
registrations.push(await ctx.session.hook("compaction", (event) => {
|
|
1388
1414
|
clearSessionState(event);
|
|
1389
|
-
runtime.endSession({ ...scope, hostName: "opencode", hostVersion: ctx.app?.version ?? "unknown", aiSystemId: options.aiSystemId ?? "", connectionId: options.connectionId ?? "", conversationId:
|
|
1415
|
+
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 });
|
|
1390
1416
|
}));
|
|
1391
1417
|
return async () => {
|
|
1392
1418
|
actionContexts.clear();
|
|
@@ -1412,14 +1438,16 @@ var v1Runtime = () => {
|
|
|
1412
1438
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1413
1439
|
}
|
|
1414
1440
|
const tools = asRecord(bound.context.tools);
|
|
1415
|
-
const allowed = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(
|
|
1416
|
-
const schemaDigest =
|
|
1417
|
-
const bindingDigest =
|
|
1441
|
+
const allowed = Array.isArray(tools.allowed_tool_ids) ? tools.allowed_tool_ids.map(text2) : [];
|
|
1442
|
+
const schemaDigest = text2(tools.schema_digest);
|
|
1443
|
+
const bindingDigest = text2(bound.context.binding_digest);
|
|
1444
|
+
const dataVersion = text2(bound.context.release_digest);
|
|
1445
|
+
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1418
1446
|
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" };
|
|
1419
1447
|
try {
|
|
1420
|
-
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 } });
|
|
1448
|
+
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 } });
|
|
1421
1449
|
const body = asRecord(response?.body);
|
|
1422
|
-
return body.allowed === true ? { decision: "ALLOW", authorityReceiptDigest:
|
|
1450
|
+
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) };
|
|
1423
1451
|
} catch (error) {
|
|
1424
1452
|
return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1425
1453
|
}
|
|
@@ -1434,23 +1462,23 @@ var v1Fallback = { async server() {
|
|
|
1434
1462
|
const connectedIdentity = runtime.getConnectionIdentity();
|
|
1435
1463
|
const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
|
|
1436
1464
|
const prompts = /* @__PURE__ */ new Map();
|
|
1437
|
-
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:
|
|
1438
|
-
const partsText = (value) => Array.isArray(value) ? value.map((part) =>
|
|
1465
|
+
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 });
|
|
1466
|
+
const partsText = (value) => Array.isArray(value) ? value.map((part) => text2(asRecord(part).text)).filter(Boolean).join("\n") : "";
|
|
1439
1467
|
return {
|
|
1440
1468
|
"chat.message": async (input, output) => {
|
|
1441
|
-
const sessionId =
|
|
1469
|
+
const sessionId = text2(input.sessionID ?? input.conversationId ?? input.id);
|
|
1442
1470
|
const message = asRecord(output.message ?? input.message);
|
|
1443
|
-
const prompt =
|
|
1471
|
+
const prompt = text2(message.text ?? message.content) || partsText(output.parts ?? input.parts);
|
|
1444
1472
|
if (sessionId && prompt.trim()) prompts.set(sessionId, prompt);
|
|
1445
1473
|
},
|
|
1446
1474
|
"chat.params": async (input, output) => {
|
|
1447
|
-
const sessionId =
|
|
1448
|
-
const prompt =
|
|
1475
|
+
const sessionId = text2(input.sessionID ?? input.conversationId ?? input.id);
|
|
1476
|
+
const prompt = text2(input.prompt ?? asRecord(input.message).text ?? input.input ?? output.prompt) || prompts.get(sessionId) || "";
|
|
1449
1477
|
if (!prompt.trim()) return;
|
|
1450
|
-
const result = await runtime.beforeModel({ ...identity(input), requestId:
|
|
1478
|
+
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) });
|
|
1451
1479
|
const responseBody = asRecord(result.response?.body);
|
|
1452
1480
|
const runtimeContext = asRecord(responseBody.requestContext ?? responseBody.context);
|
|
1453
|
-
const executionId =
|
|
1481
|
+
const executionId = text2(input.executionId ?? responseBody.executionId);
|
|
1454
1482
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1455
1483
|
const current = identity(input);
|
|
1456
1484
|
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 });
|
|
@@ -1458,17 +1486,17 @@ var v1Fallback = { async server() {
|
|
|
1458
1486
|
if (result.status === "applied" && result.projection) output.system = [...Array.isArray(output.system) ? output.system : [], result.projection];
|
|
1459
1487
|
},
|
|
1460
1488
|
"tool.execute.before": async (input, output) => {
|
|
1461
|
-
const proposal = { ...identity(input), requestId:
|
|
1489
|
+
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) };
|
|
1462
1490
|
const decision = await runtime.beforeTool(proposal);
|
|
1463
1491
|
actionDecisions.set(contextKey(proposal) + ":" + proposal.callId, decision);
|
|
1464
|
-
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision,
|
|
1492
|
+
if (mode === "enforce" && decision.decision !== "ALLOW") throw new ViraOpenCodeActionError(decision.decision, text2(decision.reasonCode) || "VIRA_ACTION_NOT_ALLOWED");
|
|
1465
1493
|
},
|
|
1466
1494
|
"tool.execute.after": async (input, output) => {
|
|
1467
|
-
const proposal = { ...identity(input), requestId:
|
|
1495
|
+
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) };
|
|
1468
1496
|
const decision = actionDecisions.get(contextKey(proposal) + ":" + proposal.callId);
|
|
1469
1497
|
actionDecisions.delete(contextKey(proposal) + ":" + proposal.callId);
|
|
1470
1498
|
try {
|
|
1471
|
-
await runtime.afterTool({ ...proposal, ok: true, result: output.output, errorCode:
|
|
1499
|
+
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 } : {} });
|
|
1472
1500
|
} catch (error) {
|
|
1473
1501
|
if (mode === "enforce") throw error;
|
|
1474
1502
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/opencode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
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.55"},
|
|
11
11
|
"peerDependencies": {"effect": "^4.0.0"},
|
|
12
12
|
"engines": {"node": ">=20"},
|
|
13
13
|
"license": "MIT"
|