@tryvira/sdk 0.1.65 → 0.1.67
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/host-lifecycle.d.ts +3 -0
- package/dist/index.js +72 -8
- package/dist/js-client.d.ts +17 -0
- package/package.json +1 -1
package/dist/host-lifecycle.d.ts
CHANGED
|
@@ -19,6 +19,9 @@ export interface HostSessionIdentity {
|
|
|
19
19
|
organizationId: string;
|
|
20
20
|
projectId: string;
|
|
21
21
|
environmentId: string;
|
|
22
|
+
/** Optional members of the composed workspace/user scope. */
|
|
23
|
+
workspaceId?: string;
|
|
24
|
+
userId?: string;
|
|
22
25
|
conversationId: string;
|
|
23
26
|
sessionId?: string;
|
|
24
27
|
}
|
package/dist/index.js
CHANGED
|
@@ -41,6 +41,66 @@ var contentIdempotencyKey = async (operation, request) => {
|
|
|
41
41
|
return `${operation}:${hex}`;
|
|
42
42
|
};
|
|
43
43
|
var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
|
+
var retryAfterMs = (value, now = Date.now()) => {
|
|
45
|
+
if (!value) return 0;
|
|
46
|
+
const seconds = Number(value);
|
|
47
|
+
if (Number.isFinite(seconds) && seconds >= 0) return Math.min(seconds * 1e3, 12e4);
|
|
48
|
+
const date = Date.parse(value);
|
|
49
|
+
return Number.isFinite(date) && date > now ? Math.min(date - now, 12e4) : 0;
|
|
50
|
+
};
|
|
51
|
+
async function viraHttpRequest(url, options) {
|
|
52
|
+
const timeoutMs = Math.max(1e3, Math.min(options.timeoutMs ?? 15e3, 12e4));
|
|
53
|
+
const retries = Math.max(0, Math.min(options.retries ?? 2, 5));
|
|
54
|
+
const headers = { accept: "application/json", ...options.defaultHeaders ?? {} };
|
|
55
|
+
if (options.body !== void 0) headers["content-type"] = "application/json";
|
|
56
|
+
if (options.apiKey) {
|
|
57
|
+
if (options.apiKey.startsWith("vira_live_")) headers["x-vira-api-key"] = options.apiKey;
|
|
58
|
+
else {
|
|
59
|
+
headers.authorization = `Bearer ${options.apiKey}`;
|
|
60
|
+
headers["x-vira-api-key"] = options.apiKey;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (options.idempotencyKey) headers["idempotency-key"] = options.idempotencyKey;
|
|
64
|
+
const retryable = options.method === "GET" || Boolean(options.idempotencyKey);
|
|
65
|
+
const attempts = retryable ? retries + 1 : 1;
|
|
66
|
+
let lastError;
|
|
67
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
68
|
+
const controller = new AbortController();
|
|
69
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
70
|
+
try {
|
|
71
|
+
const response = await (options.fetcher ?? fetch)(url, { method: options.method, headers, body: options.body === void 0 ? void 0 : JSON.stringify(options.body), signal: controller.signal });
|
|
72
|
+
const raw = await response.text();
|
|
73
|
+
let body = null;
|
|
74
|
+
try {
|
|
75
|
+
body = raw ? JSON.parse(raw) : null;
|
|
76
|
+
} catch {
|
|
77
|
+
body = raw;
|
|
78
|
+
}
|
|
79
|
+
const requestId = response.headers.get("x-request-id") ?? "";
|
|
80
|
+
if (!response.ok) {
|
|
81
|
+
const error = new ViraApiError(`VIRA_API_${response.status}`, response.status, body, requestId);
|
|
82
|
+
if ((response.status >= 500 || response.status === 429) && attempt + 1 < attempts) {
|
|
83
|
+
const serverDelay = retryAfterMs(response.headers.get("retry-after"));
|
|
84
|
+
await wait(Math.max(serverDelay, 25 * 2 ** attempt) + Math.floor(Math.random() * 25));
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
return { status: response.status, body, requestId };
|
|
90
|
+
} catch (error) {
|
|
91
|
+
lastError = error;
|
|
92
|
+
if (error instanceof ViraApiError) throw error;
|
|
93
|
+
if (attempt + 1 < attempts) {
|
|
94
|
+
await wait(25 * 2 ** attempt);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
throw error;
|
|
98
|
+
} finally {
|
|
99
|
+
clearTimeout(timer);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw lastError;
|
|
103
|
+
}
|
|
44
104
|
var ViraClient = class {
|
|
45
105
|
evaluations;
|
|
46
106
|
teach;
|
|
@@ -131,8 +191,7 @@ var ViraClient = class {
|
|
|
131
191
|
requestId
|
|
132
192
|
);
|
|
133
193
|
if ((response.status >= 500 || response.status === 429) && attempt + 1 < attempts) {
|
|
134
|
-
const
|
|
135
|
-
const serverDelay = Number.isFinite(retryAfter) && retryAfter >= 0 ? Math.min(retryAfter * 1e3, 12e4) : 0;
|
|
194
|
+
const serverDelay = retryAfterMs(response.headers.get("retry-after"));
|
|
136
195
|
const jitter = Math.floor(Math.random() * 25);
|
|
137
196
|
await wait(Math.max(serverDelay, 25 * 2 ** attempt) + jitter);
|
|
138
197
|
continue;
|
|
@@ -1426,7 +1485,8 @@ function createViraHostRuntime(client, config) {
|
|
|
1426
1485
|
return connected;
|
|
1427
1486
|
};
|
|
1428
1487
|
const reconnect = async () => connectOnce({ force: true });
|
|
1429
|
-
const
|
|
1488
|
+
const scopeMemberKey = (identity) => `${identity.workspaceId ?? ""}:${identity.userId ?? ""}`;
|
|
1489
|
+
const sessionKey = (request) => `${request.organizationId}:${request.projectId}:${request.environmentId}:${scopeMemberKey(request)}:${request.conversationId}`;
|
|
1430
1490
|
const beforeModel = async (request) => {
|
|
1431
1491
|
nonEmpty(request.input, "SDK_HOST_MODEL_INPUT_REQUIRED");
|
|
1432
1492
|
if (mode === "off") return { status: "off" };
|
|
@@ -1436,7 +1496,10 @@ function createViraHostRuntime(client, config) {
|
|
|
1436
1496
|
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);
|
|
1437
1497
|
}
|
|
1438
1498
|
await connectOnce();
|
|
1439
|
-
const
|
|
1499
|
+
const profile = request.profile || {};
|
|
1500
|
+
const existingHierarchy = profile.scopeHierarchy && typeof profile.scopeHierarchy === "object" && !Array.isArray(profile.scopeHierarchy) ? profile.scopeHierarchy : {};
|
|
1501
|
+
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;
|
|
1502
|
+
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");
|
|
1440
1503
|
if (!semantic) return { status: "unavailable", reasonCode: "VIRA_SEMANTIC_UNAVAILABLE" };
|
|
1441
1504
|
if (semantic.multiTask === true && semantic.taskKeys?.length > 1 && typeof client.startTaskRunFromPrompt === "function") {
|
|
1442
1505
|
try {
|
|
@@ -1477,7 +1540,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1477
1540
|
nonEmpty(proposal.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1478
1541
|
nonEmpty(proposal.toolName, "SDK_HOST_TOOL_NAME_REQUIRED");
|
|
1479
1542
|
if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
|
|
1480
|
-
const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
|
|
1543
|
+
const toolSessionKey = `${proposal.organizationId}:${proposal.projectId}:${proposal.environmentId}:${scopeMemberKey(proposal)}:${proposal.conversationId}:${proposal.sessionId ?? proposal.conversationId}`;
|
|
1481
1544
|
const current = activeToolSessions.get(toolSessionKey);
|
|
1482
1545
|
if (current && current.expiresAt > Date.now()) return { decision: "BLOCK", reasonCode: "SDK_HOST_TOOL_IN_FLIGHT" };
|
|
1483
1546
|
if (current) {
|
|
@@ -1533,7 +1596,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1533
1596
|
nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1534
1597
|
if (mode === "off") return { status: "off" };
|
|
1535
1598
|
if (!capabilities.canInspectToolAfter) return { status: "skipped", executionCommitted: true };
|
|
1536
|
-
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1599
|
+
const toolSessionKey = `${result.organizationId}:${result.projectId}:${result.environmentId}:${scopeMemberKey(result)}:${result.conversationId}:${result.sessionId ?? result.conversationId}`;
|
|
1537
1600
|
const minimized = result.ok ? redactToolObservation(result.result, { maxBytes: toolObservationMaxBytes, redactKeys: toolObservationRedactKeys }) : { value: { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" }, redacted: false, truncated: false };
|
|
1538
1601
|
const observedResult = minimized.value;
|
|
1539
1602
|
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 } : {} };
|
|
@@ -1568,7 +1631,7 @@ function createViraHostRuntime(client, config) {
|
|
|
1568
1631
|
}
|
|
1569
1632
|
};
|
|
1570
1633
|
const cancelTool = (identity, callId) => {
|
|
1571
|
-
const key = `${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`;
|
|
1634
|
+
const key = `${identity.organizationId}:${identity.projectId}:${identity.environmentId}:${scopeMemberKey(identity)}:${identity.conversationId}:${identity.sessionId ?? identity.conversationId}`;
|
|
1572
1635
|
const held = activeToolSessions.get(key);
|
|
1573
1636
|
if (!held || callId && held.callId !== callId) return false;
|
|
1574
1637
|
clearTimeout(held.timer);
|
|
@@ -1604,5 +1667,6 @@ export {
|
|
|
1604
1667
|
createViraMiddleware,
|
|
1605
1668
|
redactToolObservation,
|
|
1606
1669
|
resolveFrameworkBoundary,
|
|
1607
|
-
resolveHostToolBinding
|
|
1670
|
+
resolveHostToolBinding,
|
|
1671
|
+
viraHttpRequest
|
|
1608
1672
|
};
|
package/dist/js-client.d.ts
CHANGED
|
@@ -177,8 +177,25 @@ type ClientObservation = ClientObservationScope & {
|
|
|
177
177
|
eventType: string;
|
|
178
178
|
aiSystemId: string;
|
|
179
179
|
connectionId: string;
|
|
180
|
+
/** Optional composed-scope members. They are carried as server-readable
|
|
181
|
+
* identity, never as model/client authority. */
|
|
182
|
+
workspaceId?: string;
|
|
183
|
+
userId?: string;
|
|
184
|
+
sessionId?: string;
|
|
180
185
|
[key: string]: unknown;
|
|
181
186
|
};
|
|
187
|
+
export interface ViraHttpRequestOptions {
|
|
188
|
+
apiKey?: string;
|
|
189
|
+
method: "GET" | "POST" | "DELETE";
|
|
190
|
+
body?: unknown;
|
|
191
|
+
idempotencyKey?: string;
|
|
192
|
+
fetcher?: typeof fetch;
|
|
193
|
+
timeoutMs?: number;
|
|
194
|
+
retries?: number;
|
|
195
|
+
defaultHeaders?: Readonly<Record<string, string>>;
|
|
196
|
+
}
|
|
197
|
+
/** Shared transport policy for SDK-adjacent integrations such as MCP. */
|
|
198
|
+
export declare function viraHttpRequest<T = unknown>(url: string | URL, options: ViraHttpRequestOptions): Promise<ViraResponse<T>>;
|
|
182
199
|
/** Small, authority-aware client over the existing versioned control API. */
|
|
183
200
|
export declare class ViraClient {
|
|
184
201
|
readonly evaluations: ViraLearningNamespaces['evaluations'];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.67",
|
|
4
4
|
"description": "Typed Vira SDK for connect-once, observation and governed runtime execution.",
|
|
5
5
|
"keywords": ["vira", "ai-governance", "ai-runtime", "semantic-routing"],
|
|
6
6
|
"engines": {"node": ">=20"},
|