@tryvira/sdk 0.1.50 → 0.1.52
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/framework-adapters.d.ts +34 -0
- package/dist/host-lifecycle.d.ts +15 -0
- package/dist/index.js +41 -4
- package/package.json +1 -1
|
@@ -20,6 +20,7 @@ export interface FrameworkObservationAdapter {
|
|
|
20
20
|
observe(event: FrameworkEvent, kind: 'trace' | 'outcome' | 'feedback' | 'observation'): Promise<unknown> | unknown;
|
|
21
21
|
}
|
|
22
22
|
export interface FrameworkLifecycleAdapter extends FrameworkObservationAdapter {
|
|
23
|
+
readonly runtime: FrameworkRuntimeCore;
|
|
23
24
|
resolveBoundary(input: {
|
|
24
25
|
executionBoundary: Record<string, unknown>;
|
|
25
26
|
semanticContext?: Record<string, unknown>;
|
|
@@ -38,6 +39,39 @@ export interface FrameworkLifecycleAdapter extends FrameworkObservationAdapter {
|
|
|
38
39
|
resultDigest?: string;
|
|
39
40
|
}): Promise<unknown> | unknown;
|
|
40
41
|
}
|
|
42
|
+
/** The single framework-independent authority contract. Adapters only translate hooks into this API. */
|
|
43
|
+
export interface FrameworkRuntimeCore {
|
|
44
|
+
resolveBehavior(input: {
|
|
45
|
+
input?: string;
|
|
46
|
+
executionBoundary?: Record<string, unknown>;
|
|
47
|
+
semanticContext?: Record<string, unknown>;
|
|
48
|
+
behaviorPoint?: string;
|
|
49
|
+
sessionId?: string;
|
|
50
|
+
idempotencyKey?: string;
|
|
51
|
+
}): Promise<unknown>;
|
|
52
|
+
authorizeAction(input: {
|
|
53
|
+
executionId: string;
|
|
54
|
+
sessionId: string;
|
|
55
|
+
scope: {
|
|
56
|
+
organizationId: string;
|
|
57
|
+
projectId: string;
|
|
58
|
+
environmentId: string;
|
|
59
|
+
};
|
|
60
|
+
context: Record<string, unknown>;
|
|
61
|
+
proposal: Record<string, unknown>;
|
|
62
|
+
approval?: Record<string, unknown>;
|
|
63
|
+
}): Promise<unknown>;
|
|
64
|
+
recordOutcome(event: FrameworkEvent & {
|
|
65
|
+
outcome?: unknown;
|
|
66
|
+
result?: unknown;
|
|
67
|
+
error?: unknown;
|
|
68
|
+
}): Promise<unknown>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Shared Vira lifecycle. This is intentionally the only place where framework
|
|
72
|
+
* integrations select runtime, authorize effects, or admit outcomes.
|
|
73
|
+
*/
|
|
74
|
+
export declare function createFrameworkRuntimeCore(client: ViraClient): FrameworkRuntimeCore;
|
|
41
75
|
export interface RuntimeHookRequest {
|
|
42
76
|
executionBoundary?: Record<string, unknown>;
|
|
43
77
|
semanticContext?: Record<string, unknown>;
|
package/dist/host-lifecycle.d.ts
CHANGED
|
@@ -42,6 +42,8 @@ export interface HostToolResult extends HostToolProposal {
|
|
|
42
42
|
result?: unknown;
|
|
43
43
|
errorCode?: string;
|
|
44
44
|
resultDigest?: string;
|
|
45
|
+
authorityReceiptDigest?: string;
|
|
46
|
+
decisionDigest?: string;
|
|
45
47
|
}
|
|
46
48
|
export interface HostToolEvidence {
|
|
47
49
|
abi: 'vira.host-tool-evidence.v1';
|
|
@@ -51,6 +53,8 @@ export interface HostToolEvidence {
|
|
|
51
53
|
argumentsDigest: string;
|
|
52
54
|
resultDigest: string;
|
|
53
55
|
outcome: 'success' | 'failure';
|
|
56
|
+
authorityReceiptDigest?: string;
|
|
57
|
+
decisionDigest?: string;
|
|
54
58
|
requestId: string;
|
|
55
59
|
conversationId: string;
|
|
56
60
|
sessionId?: string;
|
|
@@ -71,6 +75,16 @@ export interface HostLifecycleConfig {
|
|
|
71
75
|
projectionMaxBytes?: number;
|
|
72
76
|
timeoutMs?: number;
|
|
73
77
|
}
|
|
78
|
+
export interface GovernedToolExecution<T = unknown> {
|
|
79
|
+
proposal: HostToolProposal;
|
|
80
|
+
execute: (args: Record<string, unknown>) => Promise<T> | T;
|
|
81
|
+
}
|
|
82
|
+
export interface GovernedToolResult<T = unknown> {
|
|
83
|
+
decision: HostActionAuthorization;
|
|
84
|
+
executed: boolean;
|
|
85
|
+
result?: T;
|
|
86
|
+
observation?: unknown;
|
|
87
|
+
}
|
|
74
88
|
export interface HostConnectionIdentity {
|
|
75
89
|
aiSystemId: string;
|
|
76
90
|
connectionId: string;
|
|
@@ -92,6 +106,7 @@ export declare function createViraHostRuntime(client: ViraClient, config: HostLi
|
|
|
92
106
|
beforeModel: (request: HostModelRequest) => Promise<HostProjectionResult>;
|
|
93
107
|
beforeTool: (proposal: HostToolProposal) => Promise<HostActionAuthorization>;
|
|
94
108
|
afterTool: (result: HostToolResult) => Promise<unknown>;
|
|
109
|
+
executeTool: <T>(input: GovernedToolExecution<T>) => Promise<GovernedToolResult<T>>;
|
|
95
110
|
endSession: (identity: HostSessionIdentity) => void;
|
|
96
111
|
getProjection: (identity: HostSessionIdentity) => HostProjectionResult | undefined;
|
|
97
112
|
supportsPostModelVerification: boolean;
|
package/dist/index.js
CHANGED
|
@@ -1138,6 +1138,21 @@ function buildObservationRequest(event) {
|
|
|
1138
1138
|
}
|
|
1139
1139
|
|
|
1140
1140
|
// ../../sdks/framework-adapters.ts
|
|
1141
|
+
function createFrameworkRuntimeCore(client) {
|
|
1142
|
+
if (!client) throw new Error("SDK_FRAMEWORK_CLIENT_REQUIRED");
|
|
1143
|
+
return Object.freeze({
|
|
1144
|
+
async resolveBehavior(input) {
|
|
1145
|
+
if (input.input?.trim()) return client.resolveSemanticRuntime(input);
|
|
1146
|
+
return client.resolveBoundary({ executionBoundary: input.executionBoundary ?? {}, ...input.semanticContext ? { semanticContext: input.semanticContext } : {}, ...input.idempotencyKey ? { idempotencyKey: input.idempotencyKey } : {} });
|
|
1147
|
+
},
|
|
1148
|
+
async authorizeAction(input) {
|
|
1149
|
+
return client.authorizeHostAction(input);
|
|
1150
|
+
},
|
|
1151
|
+
async recordOutcome(event) {
|
|
1152
|
+
return client.observe({ ...event, eventType: event.eventType ?? "business.outcome" });
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1141
1156
|
async function resolveFrameworkBoundary(client, request) {
|
|
1142
1157
|
if (!request || typeof request !== "object") throw new Error("SDK_RUNTIME_REQUEST_REQUIRED");
|
|
1143
1158
|
const resolution = request.executionBoundary ? await client.resolveBoundary({
|
|
@@ -1188,11 +1203,13 @@ function createFrameworkLifecycleAdapter(client, framework, options = {}) {
|
|
|
1188
1203
|
const mode = options.mode ?? "observe";
|
|
1189
1204
|
if (mode !== "observe" && mode !== "enforce") throw new Error("SDK_FRAMEWORK_MODE_INVALID");
|
|
1190
1205
|
const observation = createFrameworkObservationAdapter(client, framework);
|
|
1206
|
+
const runtime = createFrameworkRuntimeCore(client);
|
|
1191
1207
|
const emit = (event, type) => observation.observe({ ...event, eventType: type }, "observation");
|
|
1192
1208
|
return Object.freeze({
|
|
1193
1209
|
...observation,
|
|
1194
1210
|
mode,
|
|
1195
|
-
|
|
1211
|
+
runtime,
|
|
1212
|
+
resolveBoundary: (input) => runtime.resolveBehavior(input),
|
|
1196
1213
|
start: (event) => emit(event, lifecycleType.start),
|
|
1197
1214
|
complete: (event) => emit(event, lifecycleType.complete),
|
|
1198
1215
|
error: (event) => emit(event, lifecycleType.error),
|
|
@@ -1297,7 +1314,9 @@ function createViraHostRuntime(client, config) {
|
|
|
1297
1314
|
if (mode !== "enforce") return { decision: "ABSTAIN", reasonCode: mode === "off" ? "HOST_MODE_OFF" : "OBSERVE_ONLY" };
|
|
1298
1315
|
let decision;
|
|
1299
1316
|
try {
|
|
1300
|
-
|
|
1317
|
+
const authorized = await withTimeout(Promise.resolve(config.authorizeAction(proposal)), timeoutMs, "SDK_HOST_ACTION_AUTHORIZATION_TIMEOUT");
|
|
1318
|
+
const authorizedBody = bodyOf(authorized);
|
|
1319
|
+
decision = typeof authorizedBody.decision === "string" ? authorizedBody : authorized;
|
|
1301
1320
|
} catch {
|
|
1302
1321
|
return { decision: "ERROR", reasonCode: "VIRA_ACTION_AUTHORIZATION_TIMEOUT" };
|
|
1303
1322
|
}
|
|
@@ -1309,13 +1328,30 @@ function createViraHostRuntime(client, config) {
|
|
|
1309
1328
|
nonEmpty(result.callId, "SDK_HOST_TOOL_CALL_ID_REQUIRED");
|
|
1310
1329
|
if (mode === "off" || !capabilities.canInspectToolAfter) return { status: "off" };
|
|
1311
1330
|
const observedResult = result.ok ? result.result : { errorCode: result.errorCode ?? "HOST_TOOL_FAILED" };
|
|
1312
|
-
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", requestId: result.requestId, conversationId: result.conversationId, ...result.sessionId ? { sessionId: result.sessionId } : {}, ...result.turnId ? { turnId: result.turnId } : {} };
|
|
1331
|
+
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 } : {} };
|
|
1313
1332
|
return 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");
|
|
1314
1333
|
};
|
|
1334
|
+
const executeTool = async (input) => {
|
|
1335
|
+
if (!input || typeof input !== "object" || typeof input.execute !== "function") throw new Error("SDK_GOVERNED_TOOL_EXECUTION_REQUIRED");
|
|
1336
|
+
const proposal = input.proposal;
|
|
1337
|
+
const decision = await beforeTool(proposal);
|
|
1338
|
+
if (decision.decision !== "ALLOW") return { decision, executed: false };
|
|
1339
|
+
if (!isAllow(decision)) throw new Error("SDK_HOST_ALLOW_RECEIPT_INVALID");
|
|
1340
|
+
try {
|
|
1341
|
+
const result = await input.execute(proposal.args);
|
|
1342
|
+
const resultDigest = digest(result);
|
|
1343
|
+
const observation = await afterTool({ ...proposal, ok: true, result, resultDigest, ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1344
|
+
return { decision, executed: true, result, observation };
|
|
1345
|
+
} catch (error) {
|
|
1346
|
+
const errorCode = error instanceof Error ? error.message : "GOVERNED_TOOL_FAILED";
|
|
1347
|
+
const observation = await afterTool({ ...proposal, ok: false, errorCode, resultDigest: digest({ errorCode }), ...decision.authorityReceiptDigest ? { authorityReceiptDigest: decision.authorityReceiptDigest } : {}, ...decision.decisionDigest ? { decisionDigest: decision.decisionDigest } : {} });
|
|
1348
|
+
throw Object.assign(error instanceof Error ? error : new Error(errorCode), { decision, observation });
|
|
1349
|
+
}
|
|
1350
|
+
};
|
|
1315
1351
|
const endSession = (identity) => {
|
|
1316
1352
|
projections.delete(sessionKey({ ...identity, requestId: "session-end", input: "" }));
|
|
1317
1353
|
};
|
|
1318
|
-
return Object.freeze({ mode, capabilities, connectOnce, getConnectionIdentity: () => connectionIdentity, beforeModel, beforeTool, afterTool, endSession, getProjection: (identity) => projections.get(sessionKey({ ...identity, requestId: "projection", input: "" })), supportsPostModelVerification: capabilities.canInterceptResponseBeforeDisplay });
|
|
1354
|
+
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 });
|
|
1319
1355
|
}
|
|
1320
1356
|
export {
|
|
1321
1357
|
ViraApiError,
|
|
@@ -1328,6 +1364,7 @@ export {
|
|
|
1328
1364
|
createFrameworkAdapters,
|
|
1329
1365
|
createFrameworkLifecycleAdapter,
|
|
1330
1366
|
createFrameworkObservationAdapter,
|
|
1367
|
+
createFrameworkRuntimeCore,
|
|
1331
1368
|
createGenericHttpHooks,
|
|
1332
1369
|
createGenericRuntimeHooks,
|
|
1333
1370
|
createLangGraphHooks,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryvira/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.52",
|
|
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"},
|