@tryvira/opencode 0.1.16 → 0.1.18
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/dist/index.js +31 -10
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -433,7 +433,8 @@ var ViraClient = class {
|
|
|
433
433
|
body: {
|
|
434
434
|
...input.behaviorPoint ? { behaviorPoint: input.behaviorPoint } : {},
|
|
435
435
|
...input.executionBoundary ? { executionBoundary: input.executionBoundary } : {},
|
|
436
|
-
...input.semanticContext ? { semanticContext: input.semanticContext } : {}
|
|
436
|
+
...input.semanticContext ? { semanticContext: input.semanticContext } : {},
|
|
437
|
+
...input.signalAttestationRefs ? { signalAttestationRefs: input.signalAttestationRefs } : {}
|
|
437
438
|
}
|
|
438
439
|
},
|
|
439
440
|
{
|
|
@@ -482,11 +483,12 @@ var ViraClient = class {
|
|
|
482
483
|
behaviorPoint,
|
|
483
484
|
capsule: firstCapsule,
|
|
484
485
|
...input.requestId ? { requestId: input.requestId } : {},
|
|
485
|
-
...input.sessionId ? { conversationId: input.sessionId } : {}
|
|
486
|
+
...input.sessionId ? { conversationId: input.sessionId } : {},
|
|
487
|
+
...input.signalAttestationRefs ? { signalAttestationRefs: input.signalAttestationRefs } : {}
|
|
486
488
|
},
|
|
487
489
|
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
488
490
|
}) : await this.resolveRuntime({
|
|
489
|
-
semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule, ...input.requestId ? { requestId: input.requestId } : {}, ...input.sessionId ? { conversationId: input.sessionId } : {} },
|
|
491
|
+
semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule, ...input.requestId ? { requestId: input.requestId } : {}, ...input.sessionId ? { conversationId: input.sessionId } : {}, ...input.signalAttestationRefs ? { signalAttestationRefs: input.signalAttestationRefs } : {} },
|
|
490
492
|
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
491
493
|
});
|
|
492
494
|
return { semantic, runtime };
|
|
@@ -1182,6 +1184,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1182
1184
|
let connected = null;
|
|
1183
1185
|
let connectionIdentity = null;
|
|
1184
1186
|
const projections = /* @__PURE__ */ new Map();
|
|
1187
|
+
const activeToolSessions = /* @__PURE__ */ new Set();
|
|
1185
1188
|
const connectOnce = async () => {
|
|
1186
1189
|
if (mode === "off" || !config.connect) return null;
|
|
1187
1190
|
if (!connected) connected = withTimeout(client.connectOnce(config.connect), timeoutMs, "SDK_HOST_CONNECT_TIMEOUT").then((result) => {
|
|
@@ -1202,7 +1205,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1202
1205
|
if (mode === "off") return { status: "off" };
|
|
1203
1206
|
if (!capabilities.canInjectContext) return { status: "unavailable", reasonCode: "HOST_CONTEXT_INJECTION_UNSUPPORTED" };
|
|
1204
1207
|
await connectOnce();
|
|
1205
|
-
const semantic = await withTimeout(client.resolveSemanticRuntime({ input: request.input, sessionId: request.sessionId ?? request.conversationId, requestId: request.requestId }), timeoutMs, "SDK_HOST_RESOLVE_TIMEOUT");
|
|
1208
|
+
const semantic = await withTimeout(client.resolveSemanticRuntime({ input: request.input, sessionId: request.sessionId ?? request.conversationId, requestId: request.requestId, signalAttestationRefs: request.signalAttestationRefs }), timeoutMs, "SDK_HOST_RESOLVE_TIMEOUT");
|
|
1206
1209
|
if (!semantic) return { status: "unavailable", reasonCode: "VIRA_SEMANTIC_UNAVAILABLE" };
|
|
1207
1210
|
const semanticBody = bodyOf(semantic.semantic);
|
|
1208
1211
|
const receipt = semanticBody.receipt && typeof semanticBody.receipt === "object" ? semanticBody.receipt : {};
|
|
@@ -1224,24 +1227,40 @@ function createViraHostRuntime(client, config) {
|
|
|
1224
1227
|
nonEmpty(proposal.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1225
1228
|
nonEmpty(proposal.toolName, "SDK_HOST_TOOL_NAME_REQUIRED");
|
|
1226
1229
|
if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
|
|
1230
|
+
const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
|
|
1231
|
+
if (activeToolSessions.has(toolSessionKey)) return { decision: "BLOCK", reasonCode: "SDK_HOST_TOOL_IN_FLIGHT" };
|
|
1232
|
+
activeToolSessions.add(toolSessionKey);
|
|
1227
1233
|
let decision;
|
|
1228
1234
|
try {
|
|
1229
1235
|
const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
|
|
1230
1236
|
const authorizedBody = bodyOf(authorized);
|
|
1231
1237
|
decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
|
|
1232
1238
|
} catch {
|
|
1239
|
+
activeToolSessions.delete(toolSessionKey);
|
|
1233
1240
|
return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
|
|
1234
1241
|
}
|
|
1235
|
-
if (!decision || !["ALLOW", "BLOCK", "ESCALATE", "ABSTAIN", "ERROR"].includes(decision.decision))
|
|
1236
|
-
|
|
1242
|
+
if (!decision || !["ALLOW", "BLOCK", "ESCALATE", "ABSTAIN", "ERROR"].includes(decision.decision)) {
|
|
1243
|
+
activeToolSessions.delete(toolSessionKey);
|
|
1244
|
+
throw new Error("SDK_HOST_ACTION_DECISION_INVALID");
|
|
1245
|
+
}
|
|
1246
|
+
if (decision.decision !== "ALLOW") activeToolSessions.delete(toolSessionKey);
|
|
1247
|
+
if (decision.decision === "ALLOW" && !isAllow(decision)) {
|
|
1248
|
+
activeToolSessions.delete(toolSessionKey);
|
|
1249
|
+
throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
|
|
1250
|
+
}
|
|
1237
1251
|
return decision;
|
|
1238
1252
|
};
|
|
1239
1253
|
const afterTool = async (result) => {
|
|
1240
1254
|
nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1241
1255
|
if (mode === "off" || !capabilities.canInspectToolAfter) return { status: "off" };
|
|
1256
|
+
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1242
1257
|
const observedResult = result.ok ? result.result : { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" };
|
|
1243
1258
|
const evidence = { abi: "vira.host-tool-evidence.v1", eventId: result.callId, callId: result.callId, toolName: result.toolName, argumentsDigest: digest(result.args), resultDigest: result.resultDigest ?? digest(observedResult), outcome: result.ok ? "success" : "failure", ...result.authorityReceiptDigest ? { authorityReceiptDigest: result.authorityReceiptDigest } : {}, ...result.decisionDigest ? { decisionDigest: result.decisionDigest } : {}, requestId: result.requestId, conversationId: result.conversationId, ...result.sessionId ? { sessionId: result.sessionId } : {}, ...result.turnId ? { turnId: result.turnId } : {} };
|
|
1244
|
-
|
|
1259
|
+
try {
|
|
1260
|
+
return await withTimeout(client.observe({ eventId: result.callId, eventType: result.ok ? "tool.result" : "tool.failed", aiSystemId: result.aiSystemId, connectionId: result.connectionId, organizationId: result.organizationId, projectId: result.projectId, environmentId: result.environmentId, conversationId: result.conversationId, requestId: result.requestId, traceId: result.traceId, payload: { result: observedResult, evidence }, resultDigest: evidence.resultDigest }), timeoutMs, "SDK_HOST_OBSERVE_TIMEOUT");
|
|
1261
|
+
} finally {
|
|
1262
|
+
activeToolSessions.delete(toolSessionKey);
|
|
1263
|
+
}
|
|
1245
1264
|
};
|
|
1246
1265
|
const executeTool = async (input) => {
|
|
1247
1266
|
if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
|
|
@@ -1262,6 +1281,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1262
1281
|
};
|
|
1263
1282
|
const endSession = (identity) => {
|
|
1264
1283
|
projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
|
|
1284
|
+
activeToolSessions.delete(`${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`);
|
|
1265
1285
|
};
|
|
1266
1286
|
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 });
|
|
1267
1287
|
}
|
|
@@ -1274,7 +1294,7 @@ var errorCode = (error) => {
|
|
|
1274
1294
|
return text(body.code) || text(record(body.error).code) || (error instanceof Error ? error.message : "");
|
|
1275
1295
|
};
|
|
1276
1296
|
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 } } : {} });
|
|
1297
|
+
const authorize = (approvalId) => input.client.authorizeHostAction({ executionId: input.executionId, sessionId: input.sessionId, dataVersion: input.dataVersion, scope: input.scope, context: input.context, proposal: input.proposalBody, ...input.signalAttestationRefs?.length ? { signalAttestationRefs: input.signalAttestationRefs } : {}, ...approvalId ? { approval: { approvalId } } : {} });
|
|
1278
1298
|
try {
|
|
1279
1299
|
const response = await authorize(input.approvalId);
|
|
1280
1300
|
const body = record(response?.body);
|
|
@@ -1367,7 +1387,8 @@ var v2Plugin = Plugin.define({
|
|
|
1367
1387
|
const approval = typeof approvalCandidate.approvalId === "string" && approvalCandidate.approvalId.trim() ? { approvalId: approvalCandidate.approvalId.trim() } : void 0;
|
|
1368
1388
|
const dataVersion = text2(bound.context.release_digest);
|
|
1369
1389
|
if (!dataVersion) return { decision: "BLOCK", reasonCode: "RUNTIME_ACTION_DATA_VERSION_UNAVAILABLE" };
|
|
1370
|
-
|
|
1390
|
+
const refs = Array.isArray(bound.context.signal_attestation_refs) ? bound.context.signal_attestation_refs : [];
|
|
1391
|
+
return authorizeWithOneShotCustomerConfirmation({ client, proposal, proposalBody, signalAttestationRefs: refs, executionId: bound.executionId, sessionId: proposal.sessionId ?? proposal.conversationId, dataVersion, scope, context: bound.context, confirmAction: options.confirmAction, approvalId: approval?.approvalId });
|
|
1371
1392
|
}), ...(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" } } });
|
|
1372
1393
|
let connectedAiSystemId = options.aiSystemId ?? env("VIRA_AI_SYSTEM_ID") ?? "";
|
|
1373
1394
|
let connectedConnectionId = options.connectionId ?? env("VIRA_CONNECTION_ID") ?? "";
|
|
@@ -1381,7 +1402,7 @@ var v2Plugin = Plugin.define({
|
|
|
1381
1402
|
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 });
|
|
1382
1403
|
registrations.push(await ctx.session.hook("context", async (event) => {
|
|
1383
1404
|
const identity = sessionIdentity(event);
|
|
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) });
|
|
1405
|
+
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), signalAttestationRefs: options.signalAttestationRefs });
|
|
1385
1406
|
const responseBody = asRecord(result.response?.body);
|
|
1386
1407
|
const runtimeContext = asRecord(event.viraRuntimeContext ?? responseBody.requestContext ?? responseBody.context);
|
|
1387
1408
|
const executionId = text2(event.executionId ?? responseBody.executionId);
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/opencode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
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.58"},
|
|
11
11
|
"peerDependencies": {"effect": "^4.0.0"},
|
|
12
12
|
"engines": {"node": ">=20"},
|
|
13
13
|
"license": "MIT"
|