@tryvira/opencode 0.1.14 → 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 +74 -51
- package/package.json +1 -1
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
|
@@ -1250,9 +1250,9 @@ function createViraHostRuntime(client, config) {
|
|
|
1250
1250
|
const observation = await afterTool({ ...proposal, ok: true, result, resultDigest, ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1251
1251
|
return { decision, executed: true, result, observation };
|
|
1252
1252
|
} 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(
|
|
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 });
|
|
1256
1256
|
}
|
|
1257
1257
|
};
|
|
1258
1258
|
const endSession = (identity) => {
|
|
@@ -1261,11 +1261,40 @@ function createViraHostRuntime(client, config) {
|
|
|
1261
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 });
|
|
1262
1262
|
}
|
|
1263
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
|
+
|
|
1264
1293
|
// src/index.ts
|
|
1265
1294
|
var Plugin = { define: (plugin) => plugin };
|
|
1266
|
-
var
|
|
1295
|
+
var text2 = (value) => typeof value === "string" ? value : "";
|
|
1267
1296
|
var required2 = (value, code) => {
|
|
1268
|
-
const result =
|
|
1297
|
+
const result = text2(value).trim();
|
|
1269
1298
|
if (!result) throw new Error(code);
|
|
1270
1299
|
return result;
|
|
1271
1300
|
};
|
|
@@ -1276,9 +1305,9 @@ var lastUserText = (event) => {
|
|
|
1276
1305
|
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
1277
1306
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
1278
1307
|
const message = asRecord(messages[index]);
|
|
1279
|
-
if (message.role === "user") return
|
|
1308
|
+
if (message.role === "user") return text2(message.content ?? message.text);
|
|
1280
1309
|
}
|
|
1281
|
-
return
|
|
1310
|
+
return text2(event.input ?? event.prompt);
|
|
1282
1311
|
};
|
|
1283
1312
|
var ViraOpenCodeActionError = class extends Error {
|
|
1284
1313
|
decision;
|
|
@@ -1307,8 +1336,8 @@ var v2Plugin = Plugin.define({
|
|
|
1307
1336
|
const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
|
|
1308
1337
|
const callKey = (proposal) => contextKey(proposal) + ":" + proposal.callId;
|
|
1309
1338
|
const clearSessionState = (event) => {
|
|
1310
|
-
const sessionId =
|
|
1311
|
-
const conversationId =
|
|
1339
|
+
const sessionId = text2(event.sessionID ?? event.sessionId);
|
|
1340
|
+
const conversationId = text2(event.conversationId ?? event.sessionID ?? event.sessionId);
|
|
1312
1341
|
for (const [key, bound] of actionContexts) {
|
|
1313
1342
|
if (sessionId && bound.sessionId === sessionId || conversationId && bound.conversationId === conversationId) actionContexts.delete(key);
|
|
1314
1343
|
}
|
|
@@ -1324,22 +1353,16 @@ var v2Plugin = Plugin.define({
|
|
|
1324
1353
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1325
1354
|
}
|
|
1326
1355
|
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 =
|
|
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);
|
|
1330
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" };
|
|
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
|
-
}
|
|
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 });
|
|
1343
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" } } });
|
|
1344
1367
|
let connectedAiSystemId = options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID") ?? "";
|
|
1345
1368
|
let connectedConnectionId = options.connectionId ?? env("VIRA_CONNECTION_ID") ?? "";
|
|
@@ -1350,46 +1373,46 @@ var v2Plugin = Plugin.define({
|
|
|
1350
1373
|
connectedConnectionId = identity?.connectionId ?? "";
|
|
1351
1374
|
}
|
|
1352
1375
|
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:
|
|
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 });
|
|
1354
1377
|
registrations.push(await ctx.session.hook("context", async (event) => {
|
|
1355
1378
|
const identity = sessionIdentity(event);
|
|
1356
|
-
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) });
|
|
1357
1380
|
const responseBody = asRecord(result.response?.body);
|
|
1358
1381
|
const runtimeContext = asRecord(event.viraRuntimeContext ?? responseBody.requestContext ?? responseBody.context);
|
|
1359
|
-
const executionId =
|
|
1382
|
+
const executionId = text2(event.executionId ?? responseBody.executionId);
|
|
1360
1383
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1361
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 });
|
|
1362
1385
|
else if (executionId) actionContexts.delete(contextKey(identity));
|
|
1363
1386
|
if (result.status === "applied" && result.projection) {
|
|
1364
1387
|
const system = Array.isArray(event.system) ? event.system : event.system = [];
|
|
1365
|
-
if (!system.some((item) =>
|
|
1388
|
+
if (!system.some((item) => text2(asRecord(item).text) === result.projection)) system.push({ type: "text", text: result.projection });
|
|
1366
1389
|
}
|
|
1367
1390
|
}));
|
|
1368
1391
|
registrations.push(await ctx.tool.hook("execute.before", async (event) => {
|
|
1369
1392
|
const identity = sessionIdentity(event);
|
|
1370
|
-
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) };
|
|
1371
1394
|
const decision = await runtime.beforeTool(proposal);
|
|
1372
1395
|
actionDecisions.set(callKey(proposal), decision);
|
|
1373
|
-
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");
|
|
1374
1397
|
}));
|
|
1375
1398
|
registrations.push(await ctx.tool.hook("execute.after", async (event) => {
|
|
1376
1399
|
const identity = sessionIdentity(event);
|
|
1377
|
-
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) };
|
|
1378
1401
|
const decision = actionDecisions.get(callKey(proposal));
|
|
1379
1402
|
actionDecisions.delete(callKey(proposal));
|
|
1380
1403
|
try {
|
|
1381
|
-
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 } : {} });
|
|
1382
1405
|
} catch (error) {
|
|
1383
1406
|
if (mode === "enforce") throw error;
|
|
1384
1407
|
}
|
|
1385
1408
|
}));
|
|
1386
1409
|
registrations.push(await ctx.session.hook("fork", (event) => {
|
|
1387
1410
|
clearSessionState(event);
|
|
1388
|
-
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 });
|
|
1389
1412
|
}));
|
|
1390
1413
|
registrations.push(await ctx.session.hook("compaction", (event) => {
|
|
1391
1414
|
clearSessionState(event);
|
|
1392
|
-
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 });
|
|
1393
1416
|
}));
|
|
1394
1417
|
return async () => {
|
|
1395
1418
|
actionContexts.clear();
|
|
@@ -1415,16 +1438,16 @@ var v1Runtime = () => {
|
|
|
1415
1438
|
return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_CONTEXT_EXPIRED" };
|
|
1416
1439
|
}
|
|
1417
1440
|
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 =
|
|
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);
|
|
1422
1445
|
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1423
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" };
|
|
1424
1447
|
try {
|
|
1425
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 } });
|
|
1426
1449
|
const body = asRecord(response?.body);
|
|
1427
|
-
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) };
|
|
1428
1451
|
} catch (error) {
|
|
1429
1452
|
return { decision: "ERROR", reasonCode: error instanceof Error ? error.message : "RUNTIME_ACTION_AUTHORIZATION_FAILED" };
|
|
1430
1453
|
}
|
|
@@ -1439,23 +1462,23 @@ var v1Fallback = { async server() {
|
|
|
1439
1462
|
const connectedIdentity = runtime.getConnectionIdentity();
|
|
1440
1463
|
const env = (name) => typeof process !== "undefined" ? process.env[name] : void 0;
|
|
1441
1464
|
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) =>
|
|
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") : "";
|
|
1444
1467
|
return {
|
|
1445
1468
|
"chat.message": async (input, output) => {
|
|
1446
|
-
const sessionId =
|
|
1469
|
+
const sessionId = text2(input.sessionID ?? input.conversationId ?? input.id);
|
|
1447
1470
|
const message = asRecord(output.message ?? input.message);
|
|
1448
|
-
const prompt =
|
|
1471
|
+
const prompt = text2(message.text ?? message.content) || partsText(output.parts ?? input.parts);
|
|
1449
1472
|
if (sessionId && prompt.trim()) prompts.set(sessionId, prompt);
|
|
1450
1473
|
},
|
|
1451
1474
|
"chat.params": async (input, output) => {
|
|
1452
|
-
const sessionId =
|
|
1453
|
-
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) || "";
|
|
1454
1477
|
if (!prompt.trim()) return;
|
|
1455
|
-
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) });
|
|
1456
1479
|
const responseBody = asRecord(result.response?.body);
|
|
1457
1480
|
const runtimeContext = asRecord(responseBody.requestContext ?? responseBody.context);
|
|
1458
|
-
const executionId =
|
|
1481
|
+
const executionId = text2(input.executionId ?? responseBody.executionId);
|
|
1459
1482
|
const expiresAt = Number(runtimeContext.expires_at_unix_ms);
|
|
1460
1483
|
const current = identity(input);
|
|
1461
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 });
|
|
@@ -1463,17 +1486,17 @@ var v1Fallback = { async server() {
|
|
|
1463
1486
|
if (result.status === "applied" && result.projection) output.system = [...Array.isArray(output.system) ? output.system : [], result.projection];
|
|
1464
1487
|
},
|
|
1465
1488
|
"tool.execute.before": async (input, output) => {
|
|
1466
|
-
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) };
|
|
1467
1490
|
const decision = await runtime.beforeTool(proposal);
|
|
1468
1491
|
actionDecisions.set(contextKey(proposal) + ":" + proposal.callId, decision);
|
|
1469
|
-
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");
|
|
1470
1493
|
},
|
|
1471
1494
|
"tool.execute.after": async (input, output) => {
|
|
1472
|
-
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) };
|
|
1473
1496
|
const decision = actionDecisions.get(contextKey(proposal) + ":" + proposal.callId);
|
|
1474
1497
|
actionDecisions.delete(contextKey(proposal) + ":" + proposal.callId);
|
|
1475
1498
|
try {
|
|
1476
|
-
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 } : {} });
|
|
1477
1500
|
} catch (error) {
|
|
1478
1501
|
if (mode === "enforce") throw error;
|
|
1479
1502
|
}
|