@tryvira/opencode 0.1.57 → 0.1.59
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 +27 -12
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -44,6 +44,13 @@ var contentIdempotencyKey = async (operation, request) => {
|
|
|
44
44
|
return `${operation}:${hex}`;
|
|
45
45
|
};
|
|
46
46
|
var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
47
|
+
var retryAfterMs = (value, now = Date.now()) => {
|
|
48
|
+
if (!value) return 0;
|
|
49
|
+
const seconds = Number(value);
|
|
50
|
+
if (Number.isFinite(seconds) && seconds >= 0) return Math.min(seconds * 1e3, 12e4);
|
|
51
|
+
const date = Date.parse(value);
|
|
52
|
+
return Number.isFinite(date) && date > now ? Math.min(date - now, 12e4) : 0;
|
|
53
|
+
};
|
|
47
54
|
var ViraClient = class {
|
|
48
55
|
evaluations;
|
|
49
56
|
teach;
|
|
@@ -134,8 +141,7 @@ var ViraClient = class {
|
|
|
134
141
|
requestId
|
|
135
142
|
);
|
|
136
143
|
if ((response.status >= 500 || response.status === 429) && attempt + 1 < attempts) {
|
|
137
|
-
const
|
|
138
|
-
const serverDelay = Number.isFinite(retryAfter) && retryAfter >= 0 ? Math.min(retryAfter * 1e3, 12e4) : 0;
|
|
144
|
+
const serverDelay = retryAfterMs(response.headers.get("retry-after"));
|
|
139
145
|
const jitter = Math.floor(Math.random() * 25);
|
|
140
146
|
await wait(Math.max(serverDelay, 25 * 2 ** attempt) + jitter);
|
|
141
147
|
continue;
|
|
@@ -1316,7 +1322,8 @@ function createViraHostRuntime(client, config) {
|
|
|
1316
1322
|
return connected;
|
|
1317
1323
|
};
|
|
1318
1324
|
const reconnect = async () => connectOnce({ force: true });
|
|
1319
|
-
const
|
|
1325
|
+
const scopeMemberKey = (identity) => `${identity.workspaceId ?? ""}:${identity.userId ?? ""}`;
|
|
1326
|
+
const sessionKey = (request) => `${request.organizationId}:${request.projectId}:${request.environmentId}:${scopeMemberKey(request)}:${request.conversationId}`;
|
|
1320
1327
|
const beforeModel = async (request) => {
|
|
1321
1328
|
nonEmpty(request.input, "SDK_HOST_MODEL_INPUT_REQUIRED");
|
|
1322
1329
|
if (mode === "off") return { status: "off" };
|
|
@@ -1326,7 +1333,10 @@ function createViraHostRuntime(client, config) {
|
|
|
1326
1333
|
void client.observe({ eventId: `${request.conversationId}:model.request:${request.requestId}`, eventType: "model.request", aiSystemId: request.aiSystemId, connectionId: request.connectionId, organizationId: request.organizationId, projectId: request.projectId, environmentId: request.environmentId, conversationId: request.conversationId, requestId: request.requestId, traceId: request.traceId, payload: { inputDigest: digest(request.input), minimization: { rawTextSent: false, digestAlgorithm: "sha256" } } }, { idempotencyKey: `sdk:model.request:${request.conversationId}:${request.requestId}` }).catch(() => void 0);
|
|
1327
1334
|
}
|
|
1328
1335
|
await connectOnce();
|
|
1329
|
-
const
|
|
1336
|
+
const profile = request.profile || {};
|
|
1337
|
+
const existingHierarchy = profile.scopeHierarchy && typeof profile.scopeHierarchy === "object" && !Array.isArray(profile.scopeHierarchy) ? profile.scopeHierarchy : {};
|
|
1338
|
+
const scopedProfile = request.workspaceId || request.userId ? { ...profile, scopeHierarchy: { ...existingHierarchy, ...request.workspaceId ? { workspaceId: request.workspaceId } : {}, ...request.userId ? { userId: request.userId } : {}, sessionId: request.sessionId ?? request.conversationId } } : request.profile;
|
|
1339
|
+
const semantic = await withTimeout(client.resolveSemanticRuntime({ input: request.input, profile: scopedProfile, revisionId: request.revisionId, sessionId: request.sessionId ?? request.conversationId, requestId: request.requestId, signalAttestationRefs: request.signalAttestationRefs }), timeoutMs, "SDK_HOST_RESOLVE_TIMEOUT");
|
|
1330
1340
|
if (!semantic) return { status: "unavailable", reasonCode: "VIRA_SEMANTIC_UNAVAILABLE" };
|
|
1331
1341
|
if (semantic.multiTask === true && semantic.taskKeys?.length > 1 && typeof client.startTaskRunFromPrompt === "function") {
|
|
1332
1342
|
try {
|
|
@@ -1337,7 +1347,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1337
1347
|
...capabilities.canPreventSideEffect ? ["external_action"] : [],
|
|
1338
1348
|
...capabilities.canInspectToolAfter ? ["tool_result"] : []
|
|
1339
1349
|
];
|
|
1340
|
-
const admitted = await withTimeout(client.startTaskRunFromPrompt({ input: request.input, taskRunId: `host-task-run-${request.requestId}`, requestId: request.requestId, conversationId: request.conversationId, traceRootRef: request.traceId, hostCapabilities }), timeoutMs, "SDK_HOST_TASKRUN_ADMISSION_TIMEOUT");
|
|
1350
|
+
const admitted = await withTimeout(client.startTaskRunFromPrompt({ input: request.input, taskRunId: `host-task-run-${request.requestId}`, requestId: request.requestId, conversationId: request.conversationId, traceRootRef: request.traceId, ...request.connectionId ? { connectionId: request.connectionId } : {}, hostCapabilities }), timeoutMs, "SDK_HOST_TASKRUN_ADMISSION_TIMEOUT");
|
|
1341
1351
|
const admittedBody = bodyOf(admitted);
|
|
1342
1352
|
const taskRun = admittedBody.taskRun && typeof admittedBody.taskRun === "object" ? admittedBody.taskRun : {};
|
|
1343
1353
|
const taskRunId = typeof taskRun.taskRunId === "string" ? taskRun.taskRunId : `host-task-run-${request.requestId}`;
|
|
@@ -1367,7 +1377,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1367
1377
|
nonEmpty(proposal.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1368
1378
|
nonEmpty(proposal.toolName, "SDK_HOST_TOOL_NAME_REQUIRED");
|
|
1369
1379
|
if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
|
|
1370
|
-
const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
|
|
1380
|
+
const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${scopeMemberKey(proposal)}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
|
|
1371
1381
|
const current = activeToolSessions.get(toolSessionKey);
|
|
1372
1382
|
if (current && current.expiresAt > Date.now()) return { decision: "BLOCK", reasonCode: "SDK_HOST_TOOL_IN_FLIGHT" };
|
|
1373
1383
|
if (current) {
|
|
@@ -1423,7 +1433,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1423
1433
|
nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1424
1434
|
if (mode === "off") return { status: "off" };
|
|
1425
1435
|
if (!capabilities.canInspectToolAfter) return { status: "skipped", executionCommitted: true };
|
|
1426
|
-
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1436
|
+
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${scopeMemberKey(result)}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1427
1437
|
const minimized = result.ok ? redactToolObservation(result.result, { maxBytes: toolObservationMaxBytes, redactKeys: toolObservationRedactKeys }) : { value: { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" }, redacted: false, truncated: false };
|
|
1428
1438
|
const observedResult = minimized.value;
|
|
1429
1439
|
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 } : {} };
|
|
@@ -1458,7 +1468,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1458
1468
|
}
|
|
1459
1469
|
};
|
|
1460
1470
|
const cancelTool = (identity, callId) => {
|
|
1461
|
-
const key = `${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`;
|
|
1471
|
+
const key = `${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${scopeMemberKey(identity)}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`;
|
|
1462
1472
|
const held = activeToolSessions.get(key);
|
|
1463
1473
|
if (!held || callId && held.callId !== callId) return false;
|
|
1464
1474
|
clearTimeout(held.timer);
|
|
@@ -1553,6 +1563,11 @@ var latestAssistantText = (event) => {
|
|
|
1553
1563
|
const message = asRecord(event.message);
|
|
1554
1564
|
return text2(message.role ?? asRecord(message.info).role) === "assistant" ? messageContentText(message.content ?? message.parts ?? message.text) : "";
|
|
1555
1565
|
};
|
|
1566
|
+
var scopeMembers = (event, options = {}) => {
|
|
1567
|
+
const workspaceId = text2(options.workspaceId ?? event.workspaceId ?? asRecord(event.workspace).id);
|
|
1568
|
+
const userId = text2(options.userId ?? event.userId ?? asRecord(event.user).id);
|
|
1569
|
+
return { ...workspaceId ? { workspaceId } : {}, ...userId ? { userId } : {} };
|
|
1570
|
+
};
|
|
1556
1571
|
var conversationProfile = (event, options = {}) => {
|
|
1557
1572
|
const messages = Array.isArray(event.messages) ? event.messages.slice(-20).map((value) => {
|
|
1558
1573
|
const message = asRecord(value);
|
|
@@ -1684,15 +1699,15 @@ var v2Plugin = Plugin.define({
|
|
|
1684
1699
|
const teachPrompts = /* @__PURE__ */ new Map();
|
|
1685
1700
|
const actionDecisions = /* @__PURE__ */ new Map();
|
|
1686
1701
|
const desktopPermission = createDesktopPermissionBridge();
|
|
1687
|
-
const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
|
|
1702
|
+
const contextKey = (identity) => identity.organizationId + ":" + identity.projectId + ":" + identity.environmentId + ":" + (identity.workspaceId ?? "") + ":" + (identity.userId ?? "") + ":" + identity.conversationId + ":" + (identity.sessionId ?? identity.conversationId);
|
|
1688
1703
|
const findActionContext = (proposal) => {
|
|
1689
1704
|
const exact = actionContexts.get(contextKey(proposal));
|
|
1690
1705
|
if (exact) return exact;
|
|
1691
1706
|
const sessionId = proposal.sessionId ?? proposal.conversationId;
|
|
1692
1707
|
for (const bound of actionContexts.values()) {
|
|
1693
|
-
if (bound.sessionId === sessionId && bound.conversationId === proposal.conversationId) return bound;
|
|
1708
|
+
if (bound.sessionId === sessionId && bound.conversationId === proposal.conversationId && bound.workspaceId === proposal.workspaceId && bound.userId === proposal.userId) return bound;
|
|
1694
1709
|
}
|
|
1695
|
-
for (const bound of actionContexts.values()) if (bound.sessionId === sessionId) return bound;
|
|
1710
|
+
for (const bound of actionContexts.values()) if (bound.sessionId === sessionId && bound.workspaceId === proposal.workspaceId && bound.userId === proposal.userId) return bound;
|
|
1696
1711
|
return void 0;
|
|
1697
1712
|
};
|
|
1698
1713
|
const callKey = (proposal) => contextKey(proposal) + ":" + proposal.callId;
|
|
@@ -1807,7 +1822,7 @@ var v2Plugin = Plugin.define({
|
|
|
1807
1822
|
}
|
|
1808
1823
|
const sessionIdentity = (event) => {
|
|
1809
1824
|
const sessionId = sessionRef(event);
|
|
1810
|
-
return { ...scope, hostName: options.hostName ?? "opencode", hostVersion: hostVersionFor(options, ctx), aiSystemId: required2(connectedAiSystemId, "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(connectedConnectionId, "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(event.conversationId ?? event.sessionID ?? event.sessionId ?? asRecord(event.session).id, "VIRA_HOST_SESSION_REQUIRED"), sessionId: sessionId || void 0 };
|
|
1825
|
+
return { ...scope, ...scopeMembers(event, options), hostName: options.hostName ?? "opencode", hostVersion: hostVersionFor(options, ctx), aiSystemId: required2(connectedAiSystemId, "VIRA_HOST_AI_SYSTEM_REQUIRED"), connectionId: required2(connectedConnectionId, "VIRA_HOST_CONNECTION_REQUIRED"), conversationId: required2(event.conversationId ?? event.sessionID ?? event.sessionId ?? asRecord(event.session).id, "VIRA_HOST_SESSION_REQUIRED"), sessionId: sessionId || void 0 };
|
|
1811
1826
|
};
|
|
1812
1827
|
if (mode === "enforce" && options.permissionMode !== "deny" && ctx.permission?.hook) registrations.push(await ctx.permission.hook("evaluate", async (event) => {
|
|
1813
1828
|
const identity = sessionIdentity(event);
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/opencode",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.59",
|
|
4
4
|
"description": "OpenCode 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 && node ../../scripts/opencode-plugin-smoke.mjs && node ../../scripts/opencode-plugin-host-acceptance.mjs"},
|
|
10
|
-
"dependencies": {"@tryvira/sdk": "0.1.
|
|
10
|
+
"dependencies": {"@tryvira/sdk": "0.1.66"},
|
|
11
11
|
"peerDependencies": {"effect": "^4.0.0"},
|
|
12
12
|
"engines": {"node": ">=20"},
|
|
13
13
|
"license": "MIT"
|