@tryvira/opencode 0.1.17 → 0.1.19
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 +1 -1
- package/dist/index.js +43 -6
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1184,6 +1184,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1184
1184
|
let connected = null;
|
|
1185
1185
|
let connectionIdentity = null;
|
|
1186
1186
|
const projections = /* @__PURE__ */ new Map();
|
|
1187
|
+
const activeToolSessions = /* @__PURE__ */ new Set();
|
|
1187
1188
|
const connectOnce = async () => {
|
|
1188
1189
|
if (mode === "off" || !config.connect) return null;
|
|
1189
1190
|
if (!connected) connected = withTimeout(client.connectOnce(config.connect), timeoutMs, "SDK_HOST_CONNECT_TIMEOUT").then((result) => {
|
|
@@ -1226,24 +1227,40 @@ function createViraHostRuntime(client, config) {
|
|
|
1226
1227
|
nonEmpty(proposal.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1227
1228
|
nonEmpty(proposal.toolName, "SDK_HOST_TOOL_NAME_REQUIRED");
|
|
1228
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);
|
|
1229
1233
|
let decision;
|
|
1230
1234
|
try {
|
|
1231
1235
|
const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
|
|
1232
1236
|
const authorizedBody = bodyOf(authorized);
|
|
1233
1237
|
decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
|
|
1234
1238
|
} catch {
|
|
1239
|
+
activeToolSessions.delete(toolSessionKey);
|
|
1235
1240
|
return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
|
|
1236
1241
|
}
|
|
1237
|
-
if (!decision || !["ALLOW", "BLOCK", "ESCALATE", "ABSTAIN", "ERROR"].includes(decision.decision))
|
|
1238
|
-
|
|
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
|
+
}
|
|
1239
1251
|
return decision;
|
|
1240
1252
|
};
|
|
1241
1253
|
const afterTool = async (result) => {
|
|
1242
1254
|
nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1243
1255
|
if (mode === "off" || !capabilities.canInspectToolAfter) return { status: "off" };
|
|
1256
|
+
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1244
1257
|
const observedResult = result.ok ? result.result : { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" };
|
|
1245
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 } : {} };
|
|
1246
|
-
|
|
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
|
+
}
|
|
1247
1264
|
};
|
|
1248
1265
|
const executeTool = async (input) => {
|
|
1249
1266
|
if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
|
|
@@ -1264,6 +1281,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1264
1281
|
};
|
|
1265
1282
|
const endSession = (identity) => {
|
|
1266
1283
|
projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
|
|
1284
|
+
activeToolSessions.delete(`${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`);
|
|
1267
1285
|
};
|
|
1268
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 });
|
|
1269
1287
|
}
|
|
@@ -1308,13 +1326,31 @@ var required2 = (value, code) => {
|
|
|
1308
1326
|
var asRecord = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1309
1327
|
var canonical2 = (value) => Array.isArray(value) ? "[" + value.map(canonical2).join(",") + "]" : value && typeof value === "object" ? "{" + Object.entries(value).filter(([, child]) => child !== void 0).sort(([a], [b]) => a.localeCompare(b)).map(([key, child]) => JSON.stringify(key) + ":" + canonical2(child)).join(",") + "}" : JSON.stringify(value);
|
|
1310
1328
|
var digest2 = (value) => createHash2("sha256").update(canonical2(value), "utf8").digest("hex");
|
|
1329
|
+
var messageContentText = (value) => {
|
|
1330
|
+
if (typeof value === "string") return value.trim();
|
|
1331
|
+
if (!value || typeof value !== "object") return "";
|
|
1332
|
+
if (Array.isArray(value)) return value.map(messageContentText).filter(Boolean).join("\n").trim();
|
|
1333
|
+
const record2 = asRecord(value);
|
|
1334
|
+
return text2(record2.text ?? record2.content);
|
|
1335
|
+
};
|
|
1311
1336
|
var lastUserText = (event) => {
|
|
1312
1337
|
const messages = Array.isArray(event.messages) ? event.messages : [];
|
|
1313
1338
|
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
1314
1339
|
const message = asRecord(messages[index]);
|
|
1315
|
-
if (message.role === "user")
|
|
1340
|
+
if (message.role === "user") {
|
|
1341
|
+
const content = messageContentText(message.content ?? message.text);
|
|
1342
|
+
if (content) return content;
|
|
1343
|
+
}
|
|
1316
1344
|
}
|
|
1317
|
-
return
|
|
1345
|
+
return messageContentText(event.input ?? event.prompt);
|
|
1346
|
+
};
|
|
1347
|
+
var toolExecutionSucceeded = (input, output) => {
|
|
1348
|
+
const error = output.error ?? input.error;
|
|
1349
|
+
if (error !== void 0 && error !== null && error !== "") return false;
|
|
1350
|
+
const status = text2(output.status ?? input.status).toLowerCase();
|
|
1351
|
+
if (["failed", "failure", "error", "cancelled", "canceled", "blocked"].includes(status)) return false;
|
|
1352
|
+
if (["completed", "complete", "success", "succeeded", "ok"].includes(status)) return true;
|
|
1353
|
+
return output.ok === true || input.ok === true;
|
|
1318
1354
|
};
|
|
1319
1355
|
var ViraOpenCodeActionError = class extends Error {
|
|
1320
1356
|
decision;
|
|
@@ -1504,7 +1540,7 @@ var v1Fallback = { async server() {
|
|
|
1504
1540
|
const decision = actionDecisions.get(contextKey(proposal) + ":" + proposal.callId);
|
|
1505
1541
|
actionDecisions.delete(contextKey(proposal) + ":" + proposal.callId);
|
|
1506
1542
|
try {
|
|
1507
|
-
await runtime.afterTool({ ...proposal, ok:
|
|
1543
|
+
await runtime.afterTool({ ...proposal, ok: toolExecutionSucceeded(input, output), result: output.output, errorCode: text2(asRecord(output.error ?? input.error).code), ...decision?.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision?.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1508
1544
|
} catch (error) {
|
|
1509
1545
|
if (mode === "enforce") throw error;
|
|
1510
1546
|
}
|
|
@@ -1515,5 +1551,6 @@ var index_default = { ...v2Plugin, ...v1Fallback };
|
|
|
1515
1551
|
export {
|
|
1516
1552
|
ViraOpenCodeActionError,
|
|
1517
1553
|
index_default as default,
|
|
1554
|
+
toolExecutionSucceeded,
|
|
1518
1555
|
v1Fallback
|
|
1519
1556
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/opencode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
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"
|