@tryvira/sdk 0.1.12 → 0.1.13
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/LICENSE +21 -21
- package/README.md +90 -90
- package/dist/index.d.ts +1 -1
- package/dist/index.js +835 -831
- package/dist/js-client.d.ts +410 -407
- package/package.json +18 -18
package/dist/index.js
CHANGED
|
@@ -1,831 +1,835 @@
|
|
|
1
|
-
// ../../sdks/js-client.ts
|
|
2
|
-
var ViraApiError = class extends Error {
|
|
3
|
-
status;
|
|
4
|
-
body;
|
|
5
|
-
requestId;
|
|
6
|
-
constructor(message, status, body, requestId) {
|
|
7
|
-
super(message);
|
|
8
|
-
this.name = "ViraApiError";
|
|
9
|
-
this.status = status;
|
|
10
|
-
this.body = body;
|
|
11
|
-
this.requestId = requestId;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var required = (value, code) => {
|
|
15
|
-
if (typeof value !== "string" || !value.trim()) throw new Error(code);
|
|
16
|
-
return value;
|
|
17
|
-
};
|
|
18
|
-
var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
-
var ViraClient = class {
|
|
20
|
-
baseUrl;
|
|
21
|
-
apiKey;
|
|
22
|
-
fetcher;
|
|
23
|
-
timeoutMs;
|
|
24
|
-
retries;
|
|
25
|
-
failureMode;
|
|
26
|
-
defaultHeaders;
|
|
27
|
-
constructor(options) {
|
|
28
|
-
this.baseUrl = required(options.baseUrl, "SDK_BASE_URL_REQUIRED").replace(
|
|
29
|
-
/\/$/,
|
|
30
|
-
""
|
|
31
|
-
);
|
|
32
|
-
this.apiKey = options.apiKey;
|
|
33
|
-
this.fetcher = options.fetcher ?? fetch;
|
|
34
|
-
this.timeoutMs = Math.max(
|
|
35
|
-
1e3,
|
|
36
|
-
Math.min(options.timeoutMs ?? 15e3, 12e4)
|
|
37
|
-
);
|
|
38
|
-
this.retries = Math.max(0, Math.min(options.retries ?? 2, 5));
|
|
39
|
-
this.failureMode = options.failureMode ?? "throw";
|
|
40
|
-
this.defaultHeaders = options.defaultHeaders ?? {};
|
|
41
|
-
}
|
|
42
|
-
async request(request, options = {}) {
|
|
43
|
-
const url = new URL(this.baseUrl + request.path);
|
|
44
|
-
for (const [key, value] of Object.entries(request.query ?? {}))
|
|
45
|
-
url.searchParams.set(key, value);
|
|
46
|
-
const headers = {
|
|
47
|
-
accept: "application/json",
|
|
48
|
-
...this.defaultHeaders
|
|
49
|
-
};
|
|
50
|
-
if (request.body) headers["content-type"] = "application/json";
|
|
51
|
-
if (this.apiKey) headers.authorization = `Bearer ${this.apiKey}`;
|
|
52
|
-
if (options.idempotencyKey)
|
|
53
|
-
headers["idempotency-key"] = options.idempotencyKey;
|
|
54
|
-
const retryable = request.method === "GET" || Boolean(options.idempotencyKey);
|
|
55
|
-
const attempts = retryable ? this.retries + 1 : 1;
|
|
56
|
-
let lastError;
|
|
57
|
-
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
58
|
-
const controller = new AbortController();
|
|
59
|
-
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
60
|
-
try {
|
|
61
|
-
const response = await this.fetcher(url, {
|
|
62
|
-
method: request.method,
|
|
63
|
-
headers,
|
|
64
|
-
body: request.body ? JSON.stringify(request.body) : void 0,
|
|
65
|
-
signal: controller.signal
|
|
66
|
-
});
|
|
67
|
-
const raw = await response.text();
|
|
68
|
-
let body = null;
|
|
69
|
-
try {
|
|
70
|
-
body = raw ? JSON.parse(raw) : null;
|
|
71
|
-
} catch {
|
|
72
|
-
body = raw;
|
|
73
|
-
}
|
|
74
|
-
const requestId = response.headers.get("x-request-id") ?? "";
|
|
75
|
-
if (!response.ok) {
|
|
76
|
-
const error = new ViraApiError(
|
|
77
|
-
`VIRA_API_${response.status}`,
|
|
78
|
-
response.status,
|
|
79
|
-
body,
|
|
80
|
-
requestId
|
|
81
|
-
);
|
|
82
|
-
if ((response.status >= 500 || response.status === 429) && attempt + 1 < attempts) {
|
|
83
|
-
const retryAfter = Number(response.headers.get("retry-after") ?? "");
|
|
84
|
-
const serverDelay = Number.isFinite(retryAfter) && retryAfter >= 0 ? Math.min(retryAfter * 1e3, 12e4) : 0;
|
|
85
|
-
const jitter = Math.floor(Math.random() * 25);
|
|
86
|
-
await wait(Math.max(serverDelay, 25 * 2 ** attempt) + jitter);
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
if (this.failureMode === "open" && !options.enforcing && response.status >= 500)
|
|
90
|
-
return null;
|
|
91
|
-
throw error;
|
|
92
|
-
}
|
|
93
|
-
return { status: response.status, body, requestId };
|
|
94
|
-
} catch (error) {
|
|
95
|
-
lastError = error;
|
|
96
|
-
if (error instanceof ViraApiError) throw error;
|
|
97
|
-
if (attempt + 1 < attempts) {
|
|
98
|
-
await wait(25 * 2 ** attempt);
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
if (this.failureMode === "open" && !options.enforcing) return null;
|
|
102
|
-
throw error;
|
|
103
|
-
} finally {
|
|
104
|
-
clearTimeout(timer);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
throw lastError;
|
|
108
|
-
}
|
|
109
|
-
connectSystem(input) {
|
|
110
|
-
const request = buildAISystemRequest(input);
|
|
111
|
-
return this.request(request, {
|
|
112
|
-
idempotencyKey: input.idempotencyKey
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
listAISystems() {
|
|
116
|
-
return this.request({ method: "GET", path: "/v1/ai-systems" });
|
|
117
|
-
}
|
|
118
|
-
deleteAISystem(systemId, options = {}) {
|
|
119
|
-
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
120
|
-
return this.request({ method: "DELETE", path: `/v1/ai-systems/${encodeURIComponent(systemId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:ai-system:${systemId}` });
|
|
121
|
-
}
|
|
122
|
-
getAISystem(systemId) {
|
|
123
|
-
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
124
|
-
return this.request({ method: "GET", path: `/v1/ai-systems/${encodeURIComponent(systemId)}` });
|
|
125
|
-
}
|
|
126
|
-
connectAdapter(systemId, input) {
|
|
127
|
-
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
128
|
-
const body = {
|
|
129
|
-
...input,
|
|
130
|
-
capabilities: [...input.capabilities ?? []]
|
|
131
|
-
};
|
|
132
|
-
delete body.idempotencyKey;
|
|
133
|
-
return this.request(
|
|
134
|
-
{
|
|
135
|
-
method: "POST",
|
|
136
|
-
path: `/v1/ai-systems/${encodeURIComponent(systemId)}/connections`,
|
|
137
|
-
body
|
|
138
|
-
},
|
|
139
|
-
{ idempotencyKey: input.idempotencyKey }
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
async connectOnce(input) {
|
|
143
|
-
const systemResult = await this.connectSystem(input);
|
|
144
|
-
if (!systemResult) return null;
|
|
145
|
-
const systemId = String(systemResult.body.system.id ?? "");
|
|
146
|
-
required(systemId, "SDK_SYSTEM_ID_MISSING");
|
|
147
|
-
const connectionResult = await this.connectAdapter(systemId, {
|
|
148
|
-
adapterType: input.adapterType,
|
|
149
|
-
adapterVersion: input.adapterVersion,
|
|
150
|
-
capabilities: input.capabilities,
|
|
151
|
-
config: input.config,
|
|
152
|
-
idempotencyKey: input.idempotencyKey
|
|
153
|
-
});
|
|
154
|
-
if (!connectionResult) return null;
|
|
155
|
-
return {
|
|
156
|
-
status: connectionResult.status,
|
|
157
|
-
requestId: connectionResult.requestId,
|
|
158
|
-
body: {
|
|
159
|
-
system: systemResult.body.system,
|
|
160
|
-
connection: connectionResult.body.connection,
|
|
161
|
-
handshake: connectionResult.body.handshake
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
observe(event, options = {}) {
|
|
166
|
-
return this.request(buildObservationRequest(event), {
|
|
167
|
-
idempotencyKey: options.idempotencyKey ?? event.eventId
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
trace(event, options = {}) {
|
|
171
|
-
return this.observe({ ...event, eventType: "model.response" }, options);
|
|
172
|
-
}
|
|
173
|
-
outcome(event, options = {}) {
|
|
174
|
-
return this.observe({ ...event, eventType: "business.outcome" }, options);
|
|
175
|
-
}
|
|
176
|
-
feedback(event, options = {}) {
|
|
177
|
-
return this.observe({ ...event, eventType: "feedback.user" }, options);
|
|
178
|
-
}
|
|
179
|
-
observeBatch(events, options = {}) {
|
|
180
|
-
if (!Array.isArray(events) || events.length < 1 || events.length > 100)
|
|
181
|
-
throw new Error("SDK_OBSERVATION_BATCH_INVALID");
|
|
182
|
-
return this.request(
|
|
183
|
-
{
|
|
184
|
-
method: "POST",
|
|
185
|
-
path: "/v1/observations/batch",
|
|
186
|
-
body: { events: [...events] }
|
|
187
|
-
},
|
|
188
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
observeSdk(delivery, options = {}) {
|
|
192
|
-
return this.request(
|
|
193
|
-
{ method: "POST", path: "/v1/observations/sdk", body: delivery },
|
|
194
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
discover(reviewState) {
|
|
198
|
-
return this.request({
|
|
199
|
-
method: "GET",
|
|
200
|
-
path: "/v1/discovery/candidates",
|
|
201
|
-
query: reviewState ? { reviewState } : void 0
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
createSignalDiscoveryProposal(input, options = {}) {
|
|
205
|
-
return this.request({ method: "POST", path: "/v1/signals/discovery-proposals", body: input }, { idempotencyKey: options.idempotencyKey ?? String(input.signalStableKey ?? "") });
|
|
206
|
-
}
|
|
207
|
-
reviewSignalDiscoveryProposal(proposalId, decision, options = {}) {
|
|
208
|
-
required(proposalId, "SDK_SIGNAL_PROPOSAL_ID_REQUIRED");
|
|
209
|
-
return this.request({ method: "POST", path: `/v1/signals/discovery-proposals/${encodeURIComponent(proposalId)}/review`, body: { decision } }, { idempotencyKey: options.idempotencyKey ?? `signal-review:${proposalId}` });
|
|
210
|
-
}
|
|
211
|
-
materializeSignalDiscovery(proposalId, input = {}, options = {}) {
|
|
212
|
-
required(proposalId, "SDK_SIGNAL_PROPOSAL_ID_REQUIRED");
|
|
213
|
-
return this.request({ method: "POST", path: `/v1/signals/discovery-proposals/${encodeURIComponent(proposalId)}/materialize`, body: input }, { idempotencyKey: options.idempotencyKey ?? `signal-materialize:${proposalId}` });
|
|
214
|
-
}
|
|
215
|
-
createSignalMapping(input, options = {}) {
|
|
216
|
-
return this.request({ method: "POST", path: "/v1/signals/mappings", body: input }, { idempotencyKey: options.idempotencyKey ?? String(input.signalStableKey ?? "") });
|
|
217
|
-
}
|
|
218
|
-
getSignalMapping(mappingId) {
|
|
219
|
-
required(mappingId, "SDK_SIGNAL_MAPPING_ID_REQUIRED");
|
|
220
|
-
return this.request({ method: "GET", path: `/v1/signals/mappings/${encodeURIComponent(mappingId)}` });
|
|
221
|
-
}
|
|
222
|
-
reviewSignalMapping(mappingId, input = {}, options = {}) {
|
|
223
|
-
required(mappingId, "SDK_SIGNAL_MAPPING_ID_REQUIRED");
|
|
224
|
-
return this.request({ method: "POST", path: `/v1/signals/mappings/${encodeURIComponent(mappingId)}/review`, body: input }, { idempotencyKey: options.idempotencyKey ?? `signal-mapping-review:${mappingId}` });
|
|
225
|
-
}
|
|
226
|
-
connectionHealth(connectionId) {
|
|
227
|
-
required(connectionId, "SDK_CONNECTION_ID_REQUIRED");
|
|
228
|
-
return this.request({
|
|
229
|
-
method: "GET",
|
|
230
|
-
path: `/v1/connections/${encodeURIComponent(connectionId)}/health`
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
inventory(systemId) {
|
|
234
|
-
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
235
|
-
return this.request({
|
|
236
|
-
method: "GET",
|
|
237
|
-
path: `/v1/ai-systems/${encodeURIComponent(systemId)}/inventory`
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
listPacks() {
|
|
241
|
-
return this.request({
|
|
242
|
-
method: "GET",
|
|
243
|
-
path: "/v1/packs"
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
importPack(input) {
|
|
247
|
-
if (!input?.manifest && !input?.archiveBase64)
|
|
248
|
-
throw new Error("SDK_PACK_MANIFEST_OR_ARCHIVE_REQUIRED");
|
|
249
|
-
if (input.archiveBase64 && !input.fileName)
|
|
250
|
-
throw new Error("SDK_PACK_FILE_NAME_REQUIRED");
|
|
251
|
-
return this.request(
|
|
252
|
-
{
|
|
253
|
-
method: "POST",
|
|
254
|
-
path: "/v1/packs/import",
|
|
255
|
-
body: {
|
|
256
|
-
...input.manifest ? { manifest: input.manifest } : {},
|
|
257
|
-
objects: input.objects ?? {},
|
|
258
|
-
...input.archiveBase64 ? { archiveBase64: input.archiveBase64, fileName: input.fileName } : {}
|
|
259
|
-
}
|
|
260
|
-
},
|
|
261
|
-
{ idempotencyKey: input.idempotencyKey ?? `sdk:pack:${Date.now()}` }
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
installPack(packId, variables = {}) {
|
|
265
|
-
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
266
|
-
return this.request(
|
|
267
|
-
{
|
|
268
|
-
method: "POST",
|
|
269
|
-
path: `/v1/packs/${encodeURIComponent(packId)}/install`,
|
|
270
|
-
body: { variables }
|
|
271
|
-
},
|
|
272
|
-
{ idempotencyKey: `sdk:pack:install:${packId}` }
|
|
273
|
-
);
|
|
274
|
-
}
|
|
275
|
-
evaluatePack(packId, input) {
|
|
276
|
-
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
277
|
-
return this.request(
|
|
278
|
-
{
|
|
279
|
-
method: "POST",
|
|
280
|
-
path: `/v1/packs/${encodeURIComponent(packId)}/evaluate`,
|
|
281
|
-
body: { ...input }
|
|
282
|
-
},
|
|
283
|
-
{ idempotencyKey: `sdk:pack:evaluate:${packId}` }
|
|
284
|
-
);
|
|
285
|
-
}
|
|
286
|
-
approvePack(packId) {
|
|
287
|
-
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
288
|
-
return this.request(
|
|
289
|
-
{
|
|
290
|
-
method: "POST",
|
|
291
|
-
path: `/v1/packs/${encodeURIComponent(packId)}/approve`,
|
|
292
|
-
body: {}
|
|
293
|
-
},
|
|
294
|
-
{ idempotencyKey: `sdk:pack:approve:${packId}` }
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
releasePack(packId) {
|
|
298
|
-
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
299
|
-
return this.request(
|
|
300
|
-
{
|
|
301
|
-
method: "POST",
|
|
302
|
-
path: `/v1/packs/${encodeURIComponent(packId)}/release`,
|
|
303
|
-
body: {}
|
|
304
|
-
},
|
|
305
|
-
{ idempotencyKey: `sdk:pack:release:${packId}` }
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
previewPackUpgrade(packId, incoming) {
|
|
309
|
-
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
310
|
-
if (!incoming || typeof incoming !== "object")
|
|
311
|
-
throw new Error("SDK_PACK_UPGRADE_INPUT_REQUIRED");
|
|
312
|
-
return this.request(
|
|
313
|
-
{
|
|
314
|
-
method: "POST",
|
|
315
|
-
path: `/v1/packs/${encodeURIComponent(packId)}/upgrade-preview`,
|
|
316
|
-
body: { incoming }
|
|
317
|
-
},
|
|
318
|
-
{ idempotencyKey: `sdk:pack:upgrade-preview:${packId}` }
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
executeRuntime(input) {
|
|
322
|
-
return this.request(
|
|
323
|
-
{
|
|
324
|
-
method: "POST",
|
|
325
|
-
path: "/v1/runtime/execute",
|
|
326
|
-
body: buildRuntimeRequest(
|
|
327
|
-
input
|
|
328
|
-
)
|
|
329
|
-
},
|
|
330
|
-
{ idempotencyKey: input.idempotencyKey, enforcing: true }
|
|
331
|
-
);
|
|
332
|
-
}
|
|
333
|
-
resolveRuntime(input) {
|
|
334
|
-
if (!input.behaviorPoint && !input.executionBoundary && !input.semanticContext)
|
|
335
|
-
throw new Error("SDK_RUNTIME_ROUTE_REQUIRED");
|
|
336
|
-
return this.request(
|
|
337
|
-
{
|
|
338
|
-
method: "POST",
|
|
339
|
-
path: "/v1/runtime/resolve",
|
|
340
|
-
body: {
|
|
341
|
-
...input.behaviorPoint ? { behaviorPoint: input.behaviorPoint } : {},
|
|
342
|
-
...input.executionBoundary ? { executionBoundary: input.executionBoundary } : {},
|
|
343
|
-
...input.semanticContext ? { semanticContext: input.semanticContext } : {}
|
|
344
|
-
}
|
|
345
|
-
},
|
|
346
|
-
{
|
|
347
|
-
idempotencyKey: input.idempotencyKey ?? `sdk:resolve:${input.behaviorPoint ?? "boundary"}`,
|
|
348
|
-
enforcing: true
|
|
349
|
-
}
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
/** Resolve a runtime route from a governed execution boundary and semantic context. */
|
|
353
|
-
resolveBoundary(input) {
|
|
354
|
-
if (!input.executionBoundary || typeof input.executionBoundary !== "object")
|
|
355
|
-
throw new Error("SDK_EXECUTION_BOUNDARY_REQUIRED");
|
|
356
|
-
return this.resolveRuntime({
|
|
357
|
-
executionBoundary: input.executionBoundary,
|
|
358
|
-
...input.semanticContext ? { semanticContext: input.semanticContext } : {},
|
|
359
|
-
idempotencyKey: input.idempotencyKey
|
|
360
|
-
});
|
|
361
|
-
}
|
|
362
|
-
resolveSemantic(input) {
|
|
363
|
-
required(input.input, "SDK_SEMANTIC_INPUT_REQUIRED");
|
|
364
|
-
const { idempotencyKey, requestId, ...body } = input;
|
|
365
|
-
const resolvedRequestId = requestId ?? `sdk-semantic-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
366
|
-
return this.request(
|
|
367
|
-
{
|
|
368
|
-
method: "POST",
|
|
369
|
-
path: "/v1/semantic/resolve",
|
|
370
|
-
body: { ...body, requestId: resolvedRequestId }
|
|
371
|
-
},
|
|
372
|
-
{ idempotencyKey: idempotencyKey ?? `sdk:semantic:${resolvedRequestId}` }
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
async resolveSemanticRuntime(input) {
|
|
376
|
-
const semantic = await this.resolveSemantic(input);
|
|
377
|
-
if (!semantic) return null;
|
|
378
|
-
const body = semantic.body;
|
|
379
|
-
const receipt = body.receipt && typeof body.receipt === "object" ? body.receipt : {};
|
|
380
|
-
if (receipt.outcome !== "select" || typeof receipt.taskKey !== "string")
|
|
381
|
-
return { semantic, runtime: null };
|
|
382
|
-
const capsules = Array.isArray(body.capsules) ? body.capsules : [];
|
|
383
|
-
const firstCapsule = capsules[0] && typeof capsules[0] === "object" ? capsules[0] : {};
|
|
384
|
-
const behaviorPoint = typeof firstCapsule.behaviorKey === "string" && firstCapsule.behaviorKey.trim() ? firstCapsule.behaviorKey : receipt.taskKey;
|
|
385
|
-
const runtime = input.executionBoundary ? await this.resolveBoundary({
|
|
386
|
-
executionBoundary: input.executionBoundary,
|
|
387
|
-
semanticContext: {
|
|
388
|
-
taskKey: receipt.taskKey,
|
|
389
|
-
behaviorPoint,
|
|
390
|
-
capsule: firstCapsule
|
|
391
|
-
},
|
|
392
|
-
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
393
|
-
}) : await this.resolveRuntime({
|
|
394
|
-
semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule },
|
|
395
|
-
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
396
|
-
});
|
|
397
|
-
return { semantic, runtime };
|
|
398
|
-
}
|
|
399
|
-
listBehaviorStrategies() {
|
|
400
|
-
return this.request({
|
|
401
|
-
method: "GET",
|
|
402
|
-
path: "/v1/behavior-strategies"
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
deleteBehaviorStrategy(strategyId, options = {}) {
|
|
406
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
407
|
-
return this.request({ method: "DELETE", path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:strategy:${strategyId}` });
|
|
408
|
-
}
|
|
409
|
-
getBehaviorStrategy(strategyId) {
|
|
410
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
411
|
-
return this.request({
|
|
412
|
-
method: "GET",
|
|
413
|
-
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}`
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
createBehaviorStrategy(definition, id) {
|
|
417
|
-
if (!definition) throw new Error("SDK_STRATEGY_DEFINITION_REQUIRED");
|
|
418
|
-
return this.request({
|
|
419
|
-
method: "POST",
|
|
420
|
-
path: "/v1/behavior-strategies",
|
|
421
|
-
body: { definition, ...id ? { id } : {} }
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
listSemanticTasks() {
|
|
425
|
-
return this.request({ method: "GET", path: "/v1/semantic-tasks" });
|
|
426
|
-
}
|
|
427
|
-
deleteSemanticTask(taskId, options = {}) {
|
|
428
|
-
required(taskId, "SDK_SEMANTIC_TASK_ID_REQUIRED");
|
|
429
|
-
return this.request({ method: "DELETE", path: `/v1/semantic-tasks/${encodeURIComponent(taskId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:semantic-task:${taskId}` });
|
|
430
|
-
}
|
|
431
|
-
createSemanticTask(task, options = {}) {
|
|
432
|
-
required(task?.taskKey, "SDK_SEMANTIC_TASK_KEY_REQUIRED");
|
|
433
|
-
return this.request({ method: "POST", path: "/v1/semantic-tasks", body: { ...task, ...options.id ? { id: options.id } : {} } }, { idempotencyKey: options.idempotencyKey ?? `sdk:semantic-task:${task.taskKey}` });
|
|
434
|
-
}
|
|
435
|
-
listBehaviorPoints() {
|
|
436
|
-
return this.request({ method: "GET", path: "/v1/behavior-points" });
|
|
437
|
-
}
|
|
438
|
-
deleteBehaviorPoint(pointId, options = {}) {
|
|
439
|
-
required(pointId, "SDK_BEHAVIOR_POINT_ID_REQUIRED");
|
|
440
|
-
return this.request({ method: "DELETE", path: `/v1/behavior-points/${encodeURIComponent(pointId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:behavior-point:${pointId}` });
|
|
441
|
-
}
|
|
442
|
-
createBehaviorPoint(point, options = {}) {
|
|
443
|
-
required(point?.key, "SDK_BEHAVIOR_POINT_KEY_REQUIRED");
|
|
444
|
-
if (!point.taskId && !point.taskIds?.length) throw new Error("SDK_BEHAVIOR_POINT_TASK_REQUIRED");
|
|
445
|
-
return this.request({ method: "POST", path: "/v1/behavior-points", body: { ...point, ...options.id ? { id: options.id } : {} } }, { idempotencyKey: options.idempotencyKey ?? `sdk:behavior-point:${point.key}` });
|
|
446
|
-
}
|
|
447
|
-
createBehaviorStrategyVariant(strategyId, variant, options = {}) {
|
|
448
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
449
|
-
if (!variant || typeof variant !== "object")
|
|
450
|
-
throw new Error("SDK_STRATEGY_VARIANT_REQUIRED");
|
|
451
|
-
return this.request(
|
|
452
|
-
{
|
|
453
|
-
method: "POST",
|
|
454
|
-
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/variants`,
|
|
455
|
-
body: variant
|
|
456
|
-
},
|
|
457
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
validateBehaviorStrategy(strategyId) {
|
|
461
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
462
|
-
return this.request({
|
|
463
|
-
method: "POST",
|
|
464
|
-
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/validate`,
|
|
465
|
-
body: {}
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
activateBehaviorStrategy(strategyId) {
|
|
469
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
470
|
-
return this.request({ method: "POST", path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/activate`, body: {} });
|
|
471
|
-
}
|
|
472
|
-
simulateBehaviorStrategy(strategyId, initialState = {}) {
|
|
473
|
-
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
474
|
-
return this.request({
|
|
475
|
-
method: "POST",
|
|
476
|
-
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/simulate`,
|
|
477
|
-
body: { initialState }
|
|
478
|
-
});
|
|
479
|
-
}
|
|
480
|
-
startStrategyExecution(input) {
|
|
481
|
-
required(input.artifactDigest, "SDK_ARTIFACT_DIGEST_REQUIRED");
|
|
482
|
-
for (const value of [
|
|
483
|
-
input.organizationId,
|
|
484
|
-
input.projectId,
|
|
485
|
-
input.environmentId,
|
|
486
|
-
input.revisionId,
|
|
487
|
-
input.idempotencyKey,
|
|
488
|
-
input.nonce,
|
|
489
|
-
input.decisionPlanId,
|
|
490
|
-
input.policy
|
|
491
|
-
])
|
|
492
|
-
required(value, "SDK_STRATEGY_START_FIELD_REQUIRED");
|
|
493
|
-
if (input.behaviorPoint !== void 0)
|
|
494
|
-
required(input.behaviorPoint, "SDK_BEHAVIOR_POINT_REQUIRED");
|
|
495
|
-
if (!Array.isArray(input.candidates) || input.candidates.length === 0)
|
|
496
|
-
throw new Error("SDK_STRATEGY_CANDIDATES_REQUIRED");
|
|
497
|
-
if (!Number.isFinite(input.expiresAt))
|
|
498
|
-
throw new Error("SDK_EXPIRY_REQUIRED");
|
|
499
|
-
const body = {
|
|
500
|
-
...buildRuntimeRequest(input),
|
|
501
|
-
artifactDigest: input.artifactDigest,
|
|
502
|
-
...input.behaviorPoint ? { behaviorPoint: input.behaviorPoint } : {},
|
|
503
|
-
...input.executionBoundary ? { executionBoundary: input.executionBoundary } : {},
|
|
504
|
-
...input.traceId ? { traceId: input.traceId } : {},
|
|
505
|
-
...input.executionId ? { executionId: input.executionId } : {},
|
|
506
|
-
...input.state ? { state: input.state } : {}
|
|
507
|
-
};
|
|
508
|
-
return this.request(
|
|
509
|
-
{ method: "POST", path: "/v1/runtime/strategy-executions", body },
|
|
510
|
-
{ idempotencyKey: input.idempotencyKey }
|
|
511
|
-
);
|
|
512
|
-
}
|
|
513
|
-
getStrategyExecution(executionId) {
|
|
514
|
-
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
515
|
-
return this.request({
|
|
516
|
-
method: "GET",
|
|
517
|
-
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}`
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
getStrategyExecutionTrace(executionId) {
|
|
521
|
-
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
522
|
-
return this.request({
|
|
523
|
-
method: "GET",
|
|
524
|
-
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/trace`
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
continueStrategyExecution(executionId, continuation) {
|
|
528
|
-
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
529
|
-
const input = typeof continuation === "string" ? { authorityDigest: continuation } : continuation ?? {};
|
|
530
|
-
return this.request({
|
|
531
|
-
method: "POST",
|
|
532
|
-
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/continue`,
|
|
533
|
-
body: {
|
|
534
|
-
...input.authorityDigest ? { authorityDigest: input.authorityDigest } : {},
|
|
535
|
-
...input.state ? { state: input.state } : {},
|
|
536
|
-
...input.references ? { references: input.references } : {}
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
cancelStrategyExecution(executionId) {
|
|
541
|
-
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
542
|
-
return this.request({
|
|
543
|
-
method: "POST",
|
|
544
|
-
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/cancel`,
|
|
545
|
-
body: {}
|
|
546
|
-
});
|
|
547
|
-
}
|
|
548
|
-
listBehaviorPrograms() {
|
|
549
|
-
return this.request({
|
|
550
|
-
method: "GET",
|
|
551
|
-
path: "/v1/behavior-programs"
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
listCapabilities(options = {}) {
|
|
555
|
-
const query = options.surface ? `?surface=${encodeURIComponent(options.surface)}` : "";
|
|
556
|
-
return this.request({ method: "GET", path: `/v1/capabilities${query}` });
|
|
557
|
-
}
|
|
558
|
-
getBehaviorProgram(programId) {
|
|
559
|
-
required(programId, "SDK_PROGRAM_ID_REQUIRED");
|
|
560
|
-
return this.request({
|
|
561
|
-
method: "GET",
|
|
562
|
-
path: `/v1/behavior-programs/${encodeURIComponent(programId)}`
|
|
563
|
-
});
|
|
564
|
-
}
|
|
565
|
-
createBehaviorProgram(program, options = {}) {
|
|
566
|
-
if (!program || typeof program !== "object")
|
|
567
|
-
throw new Error("SDK_PROGRAM_REQUIRED");
|
|
568
|
-
return this.request(
|
|
569
|
-
{ method: "POST", path: "/v1/behavior-programs", body: program },
|
|
570
|
-
{
|
|
571
|
-
idempotencyKey: options.idempotencyKey ?? `sdk:program:${String(program.id ?? "")}`
|
|
572
|
-
}
|
|
573
|
-
);
|
|
574
|
-
}
|
|
575
|
-
attestCapabilities(attestation, options = {}) {
|
|
576
|
-
return this.request(
|
|
577
|
-
{
|
|
578
|
-
method: "POST",
|
|
579
|
-
path: "/v1/runtime/capabilities/attest",
|
|
580
|
-
body: attestation
|
|
581
|
-
},
|
|
582
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
583
|
-
);
|
|
584
|
-
}
|
|
585
|
-
resolveCapabilities(attestationId, request, options = {}) {
|
|
586
|
-
required(attestationId, "SDK_ATTESTATION_ID_REQUIRED");
|
|
587
|
-
return this.request(
|
|
588
|
-
{
|
|
589
|
-
method: "POST",
|
|
590
|
-
path: "/v1/runtime/capabilities/resolve",
|
|
591
|
-
body: { attestationId, request }
|
|
592
|
-
},
|
|
593
|
-
{ idempotencyKey: options.idempotencyKey, enforcing: true }
|
|
594
|
-
);
|
|
595
|
-
}
|
|
596
|
-
recordCapabilityTrace(decisionDigest, trace, options = {}) {
|
|
597
|
-
required(decisionDigest, "SDK_DECISION_DIGEST_REQUIRED");
|
|
598
|
-
return this.request(
|
|
599
|
-
{
|
|
600
|
-
method: "POST",
|
|
601
|
-
path: "/v1/runtime/capabilities/traces",
|
|
602
|
-
body: { decisionDigest, trace }
|
|
603
|
-
},
|
|
604
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
605
|
-
);
|
|
606
|
-
}
|
|
607
|
-
resolveModelServing(request, options = {}) {
|
|
608
|
-
return this.request(
|
|
609
|
-
{ method: "POST", path: "/v1/model-serving/resolve", body: request },
|
|
610
|
-
{ idempotencyKey: options.idempotencyKey, enforcing: true }
|
|
611
|
-
);
|
|
612
|
-
}
|
|
613
|
-
recordModelServingTrace(trace, selection, options = {}) {
|
|
614
|
-
return this.request(
|
|
615
|
-
{
|
|
616
|
-
method: "POST",
|
|
617
|
-
path: "/v1/model-serving/traces",
|
|
618
|
-
body: { trace, selection }
|
|
619
|
-
},
|
|
620
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
621
|
-
);
|
|
622
|
-
}
|
|
623
|
-
submitTeachEvidence(evidence, options = {}) {
|
|
624
|
-
return this.request(
|
|
625
|
-
{ method: "POST", path: "/v1/teach/evidence", body: evidence },
|
|
626
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
627
|
-
);
|
|
628
|
-
}
|
|
629
|
-
createTeachProposal(input, options = {}) {
|
|
630
|
-
return this.request({ method: "POST", path: "/v1/teach/proposals", body: input }, { idempotencyKey: options.idempotencyKey });
|
|
631
|
-
}
|
|
632
|
-
transitionTeachProposal(proposalId, transition, input = {}, options = {}) {
|
|
633
|
-
required(proposalId, "SDK_TEACH_PROPOSAL_ID_REQUIRED");
|
|
634
|
-
return this.request({ method: "POST", path: `/v1/teach/proposals/${encodeURIComponent(proposalId)}/${transition}`, body: input }, { idempotencyKey: options.idempotencyKey ?? `sdk:teach:${transition}:${proposalId}` });
|
|
635
|
-
}
|
|
636
|
-
startEvaluation(input, options = {}) {
|
|
637
|
-
return this.request(
|
|
638
|
-
{ method: "POST", path: "/v1/evaluations", body: input },
|
|
639
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
640
|
-
);
|
|
641
|
-
}
|
|
642
|
-
evaluateQuality(input, options = {}) {
|
|
643
|
-
return this.request(
|
|
644
|
-
{ method: "POST", path: "/v1/quality/evaluate", body: input },
|
|
645
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
646
|
-
);
|
|
647
|
-
}
|
|
648
|
-
getEvaluation(evaluationId) {
|
|
649
|
-
required(evaluationId, "SDK_EVALUATION_ID_REQUIRED");
|
|
650
|
-
return this.request({
|
|
651
|
-
method: "GET",
|
|
652
|
-
path: `/v1/evaluations/${encodeURIComponent(evaluationId)}`
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
createRelease(input, options = {}) {
|
|
656
|
-
return this.request(
|
|
657
|
-
{ method: "POST", path: "/v1/releases", body: input },
|
|
658
|
-
{ idempotencyKey: options.idempotencyKey }
|
|
659
|
-
);
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
return this.
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
...typeof
|
|
754
|
-
...
|
|
755
|
-
...
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
1
|
+
// ../../sdks/js-client.ts
|
|
2
|
+
var ViraApiError = class extends Error {
|
|
3
|
+
status;
|
|
4
|
+
body;
|
|
5
|
+
requestId;
|
|
6
|
+
constructor(message, status, body, requestId) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "ViraApiError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.body = body;
|
|
11
|
+
this.requestId = requestId;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var required = (value, code) => {
|
|
15
|
+
if (typeof value !== "string" || !value.trim()) throw new Error(code);
|
|
16
|
+
return value;
|
|
17
|
+
};
|
|
18
|
+
var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
var ViraClient = class {
|
|
20
|
+
baseUrl;
|
|
21
|
+
apiKey;
|
|
22
|
+
fetcher;
|
|
23
|
+
timeoutMs;
|
|
24
|
+
retries;
|
|
25
|
+
failureMode;
|
|
26
|
+
defaultHeaders;
|
|
27
|
+
constructor(options) {
|
|
28
|
+
this.baseUrl = required(options.baseUrl, "SDK_BASE_URL_REQUIRED").replace(
|
|
29
|
+
/\/$/,
|
|
30
|
+
""
|
|
31
|
+
);
|
|
32
|
+
this.apiKey = options.apiKey;
|
|
33
|
+
this.fetcher = options.fetcher ?? fetch;
|
|
34
|
+
this.timeoutMs = Math.max(
|
|
35
|
+
1e3,
|
|
36
|
+
Math.min(options.timeoutMs ?? 15e3, 12e4)
|
|
37
|
+
);
|
|
38
|
+
this.retries = Math.max(0, Math.min(options.retries ?? 2, 5));
|
|
39
|
+
this.failureMode = options.failureMode ?? "throw";
|
|
40
|
+
this.defaultHeaders = options.defaultHeaders ?? {};
|
|
41
|
+
}
|
|
42
|
+
async request(request, options = {}) {
|
|
43
|
+
const url = new URL(this.baseUrl + request.path);
|
|
44
|
+
for (const [key, value] of Object.entries(request.query ?? {}))
|
|
45
|
+
url.searchParams.set(key, value);
|
|
46
|
+
const headers = {
|
|
47
|
+
accept: "application/json",
|
|
48
|
+
...this.defaultHeaders
|
|
49
|
+
};
|
|
50
|
+
if (request.body) headers["content-type"] = "application/json";
|
|
51
|
+
if (this.apiKey) headers.authorization = `Bearer ${this.apiKey}`;
|
|
52
|
+
if (options.idempotencyKey)
|
|
53
|
+
headers["idempotency-key"] = options.idempotencyKey;
|
|
54
|
+
const retryable = request.method === "GET" || Boolean(options.idempotencyKey);
|
|
55
|
+
const attempts = retryable ? this.retries + 1 : 1;
|
|
56
|
+
let lastError;
|
|
57
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
60
|
+
try {
|
|
61
|
+
const response = await this.fetcher(url, {
|
|
62
|
+
method: request.method,
|
|
63
|
+
headers,
|
|
64
|
+
body: request.body ? JSON.stringify(request.body) : void 0,
|
|
65
|
+
signal: controller.signal
|
|
66
|
+
});
|
|
67
|
+
const raw = await response.text();
|
|
68
|
+
let body = null;
|
|
69
|
+
try {
|
|
70
|
+
body = raw ? JSON.parse(raw) : null;
|
|
71
|
+
} catch {
|
|
72
|
+
body = raw;
|
|
73
|
+
}
|
|
74
|
+
const requestId = response.headers.get("x-request-id") ?? "";
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
const error = new ViraApiError(
|
|
77
|
+
`VIRA_API_${response.status}`,
|
|
78
|
+
response.status,
|
|
79
|
+
body,
|
|
80
|
+
requestId
|
|
81
|
+
);
|
|
82
|
+
if ((response.status >= 500 || response.status === 429) && attempt + 1 < attempts) {
|
|
83
|
+
const retryAfter = Number(response.headers.get("retry-after") ?? "");
|
|
84
|
+
const serverDelay = Number.isFinite(retryAfter) && retryAfter >= 0 ? Math.min(retryAfter * 1e3, 12e4) : 0;
|
|
85
|
+
const jitter = Math.floor(Math.random() * 25);
|
|
86
|
+
await wait(Math.max(serverDelay, 25 * 2 ** attempt) + jitter);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (this.failureMode === "open" && !options.enforcing && response.status >= 500)
|
|
90
|
+
return null;
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
return { status: response.status, body, requestId };
|
|
94
|
+
} catch (error) {
|
|
95
|
+
lastError = error;
|
|
96
|
+
if (error instanceof ViraApiError) throw error;
|
|
97
|
+
if (attempt + 1 < attempts) {
|
|
98
|
+
await wait(25 * 2 ** attempt);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (this.failureMode === "open" && !options.enforcing) return null;
|
|
102
|
+
throw error;
|
|
103
|
+
} finally {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
throw lastError;
|
|
108
|
+
}
|
|
109
|
+
connectSystem(input) {
|
|
110
|
+
const request = buildAISystemRequest(input);
|
|
111
|
+
return this.request(request, {
|
|
112
|
+
idempotencyKey: input.idempotencyKey
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
listAISystems() {
|
|
116
|
+
return this.request({ method: "GET", path: "/v1/ai-systems" });
|
|
117
|
+
}
|
|
118
|
+
deleteAISystem(systemId, options = {}) {
|
|
119
|
+
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
120
|
+
return this.request({ method: "DELETE", path: `/v1/ai-systems/${encodeURIComponent(systemId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:ai-system:${systemId}` });
|
|
121
|
+
}
|
|
122
|
+
getAISystem(systemId) {
|
|
123
|
+
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
124
|
+
return this.request({ method: "GET", path: `/v1/ai-systems/${encodeURIComponent(systemId)}` });
|
|
125
|
+
}
|
|
126
|
+
connectAdapter(systemId, input) {
|
|
127
|
+
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
128
|
+
const body = {
|
|
129
|
+
...input,
|
|
130
|
+
capabilities: [...input.capabilities ?? []]
|
|
131
|
+
};
|
|
132
|
+
delete body.idempotencyKey;
|
|
133
|
+
return this.request(
|
|
134
|
+
{
|
|
135
|
+
method: "POST",
|
|
136
|
+
path: `/v1/ai-systems/${encodeURIComponent(systemId)}/connections`,
|
|
137
|
+
body
|
|
138
|
+
},
|
|
139
|
+
{ idempotencyKey: input.idempotencyKey }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
async connectOnce(input) {
|
|
143
|
+
const systemResult = await this.connectSystem(input);
|
|
144
|
+
if (!systemResult) return null;
|
|
145
|
+
const systemId = String(systemResult.body.system.id ?? "");
|
|
146
|
+
required(systemId, "SDK_SYSTEM_ID_MISSING");
|
|
147
|
+
const connectionResult = await this.connectAdapter(systemId, {
|
|
148
|
+
adapterType: input.adapterType,
|
|
149
|
+
adapterVersion: input.adapterVersion,
|
|
150
|
+
capabilities: input.capabilities,
|
|
151
|
+
config: input.config,
|
|
152
|
+
idempotencyKey: input.idempotencyKey
|
|
153
|
+
});
|
|
154
|
+
if (!connectionResult) return null;
|
|
155
|
+
return {
|
|
156
|
+
status: connectionResult.status,
|
|
157
|
+
requestId: connectionResult.requestId,
|
|
158
|
+
body: {
|
|
159
|
+
system: systemResult.body.system,
|
|
160
|
+
connection: connectionResult.body.connection,
|
|
161
|
+
handshake: connectionResult.body.handshake
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
observe(event, options = {}) {
|
|
166
|
+
return this.request(buildObservationRequest(event), {
|
|
167
|
+
idempotencyKey: options.idempotencyKey ?? event.eventId
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
trace(event, options = {}) {
|
|
171
|
+
return this.observe({ ...event, eventType: "model.response" }, options);
|
|
172
|
+
}
|
|
173
|
+
outcome(event, options = {}) {
|
|
174
|
+
return this.observe({ ...event, eventType: "business.outcome" }, options);
|
|
175
|
+
}
|
|
176
|
+
feedback(event, options = {}) {
|
|
177
|
+
return this.observe({ ...event, eventType: "feedback.user" }, options);
|
|
178
|
+
}
|
|
179
|
+
observeBatch(events, options = {}) {
|
|
180
|
+
if (!Array.isArray(events) || events.length < 1 || events.length > 100)
|
|
181
|
+
throw new Error("SDK_OBSERVATION_BATCH_INVALID");
|
|
182
|
+
return this.request(
|
|
183
|
+
{
|
|
184
|
+
method: "POST",
|
|
185
|
+
path: "/v1/observations/batch",
|
|
186
|
+
body: { events: [...events] }
|
|
187
|
+
},
|
|
188
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
observeSdk(delivery, options = {}) {
|
|
192
|
+
return this.request(
|
|
193
|
+
{ method: "POST", path: "/v1/observations/sdk", body: delivery },
|
|
194
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
discover(reviewState) {
|
|
198
|
+
return this.request({
|
|
199
|
+
method: "GET",
|
|
200
|
+
path: "/v1/discovery/candidates",
|
|
201
|
+
query: reviewState ? { reviewState } : void 0
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
createSignalDiscoveryProposal(input, options = {}) {
|
|
205
|
+
return this.request({ method: "POST", path: "/v1/signals/discovery-proposals", body: input }, { idempotencyKey: options.idempotencyKey ?? String(input.signalStableKey ?? "") });
|
|
206
|
+
}
|
|
207
|
+
reviewSignalDiscoveryProposal(proposalId, decision, options = {}) {
|
|
208
|
+
required(proposalId, "SDK_SIGNAL_PROPOSAL_ID_REQUIRED");
|
|
209
|
+
return this.request({ method: "POST", path: `/v1/signals/discovery-proposals/${encodeURIComponent(proposalId)}/review`, body: { decision } }, { idempotencyKey: options.idempotencyKey ?? `signal-review:${proposalId}` });
|
|
210
|
+
}
|
|
211
|
+
materializeSignalDiscovery(proposalId, input = {}, options = {}) {
|
|
212
|
+
required(proposalId, "SDK_SIGNAL_PROPOSAL_ID_REQUIRED");
|
|
213
|
+
return this.request({ method: "POST", path: `/v1/signals/discovery-proposals/${encodeURIComponent(proposalId)}/materialize`, body: input }, { idempotencyKey: options.idempotencyKey ?? `signal-materialize:${proposalId}` });
|
|
214
|
+
}
|
|
215
|
+
createSignalMapping(input, options = {}) {
|
|
216
|
+
return this.request({ method: "POST", path: "/v1/signals/mappings", body: input }, { idempotencyKey: options.idempotencyKey ?? String(input.signalStableKey ?? "") });
|
|
217
|
+
}
|
|
218
|
+
getSignalMapping(mappingId) {
|
|
219
|
+
required(mappingId, "SDK_SIGNAL_MAPPING_ID_REQUIRED");
|
|
220
|
+
return this.request({ method: "GET", path: `/v1/signals/mappings/${encodeURIComponent(mappingId)}` });
|
|
221
|
+
}
|
|
222
|
+
reviewSignalMapping(mappingId, input = {}, options = {}) {
|
|
223
|
+
required(mappingId, "SDK_SIGNAL_MAPPING_ID_REQUIRED");
|
|
224
|
+
return this.request({ method: "POST", path: `/v1/signals/mappings/${encodeURIComponent(mappingId)}/review`, body: input }, { idempotencyKey: options.idempotencyKey ?? `signal-mapping-review:${mappingId}` });
|
|
225
|
+
}
|
|
226
|
+
connectionHealth(connectionId) {
|
|
227
|
+
required(connectionId, "SDK_CONNECTION_ID_REQUIRED");
|
|
228
|
+
return this.request({
|
|
229
|
+
method: "GET",
|
|
230
|
+
path: `/v1/connections/${encodeURIComponent(connectionId)}/health`
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
inventory(systemId) {
|
|
234
|
+
required(systemId, "SDK_SYSTEM_ID_REQUIRED");
|
|
235
|
+
return this.request({
|
|
236
|
+
method: "GET",
|
|
237
|
+
path: `/v1/ai-systems/${encodeURIComponent(systemId)}/inventory`
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
listPacks() {
|
|
241
|
+
return this.request({
|
|
242
|
+
method: "GET",
|
|
243
|
+
path: "/v1/packs"
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
importPack(input) {
|
|
247
|
+
if (!input?.manifest && !input?.archiveBase64)
|
|
248
|
+
throw new Error("SDK_PACK_MANIFEST_OR_ARCHIVE_REQUIRED");
|
|
249
|
+
if (input.archiveBase64 && !input.fileName)
|
|
250
|
+
throw new Error("SDK_PACK_FILE_NAME_REQUIRED");
|
|
251
|
+
return this.request(
|
|
252
|
+
{
|
|
253
|
+
method: "POST",
|
|
254
|
+
path: "/v1/packs/import",
|
|
255
|
+
body: {
|
|
256
|
+
...input.manifest ? { manifest: input.manifest } : {},
|
|
257
|
+
objects: input.objects ?? {},
|
|
258
|
+
...input.archiveBase64 ? { archiveBase64: input.archiveBase64, fileName: input.fileName } : {}
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
{ idempotencyKey: input.idempotencyKey ?? `sdk:pack:${Date.now()}` }
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
installPack(packId, variables = {}) {
|
|
265
|
+
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
266
|
+
return this.request(
|
|
267
|
+
{
|
|
268
|
+
method: "POST",
|
|
269
|
+
path: `/v1/packs/${encodeURIComponent(packId)}/install`,
|
|
270
|
+
body: { variables }
|
|
271
|
+
},
|
|
272
|
+
{ idempotencyKey: `sdk:pack:install:${packId}` }
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
evaluatePack(packId, input) {
|
|
276
|
+
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
277
|
+
return this.request(
|
|
278
|
+
{
|
|
279
|
+
method: "POST",
|
|
280
|
+
path: `/v1/packs/${encodeURIComponent(packId)}/evaluate`,
|
|
281
|
+
body: { ...input }
|
|
282
|
+
},
|
|
283
|
+
{ idempotencyKey: `sdk:pack:evaluate:${packId}` }
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
approvePack(packId) {
|
|
287
|
+
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
288
|
+
return this.request(
|
|
289
|
+
{
|
|
290
|
+
method: "POST",
|
|
291
|
+
path: `/v1/packs/${encodeURIComponent(packId)}/approve`,
|
|
292
|
+
body: {}
|
|
293
|
+
},
|
|
294
|
+
{ idempotencyKey: `sdk:pack:approve:${packId}` }
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
releasePack(packId) {
|
|
298
|
+
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
299
|
+
return this.request(
|
|
300
|
+
{
|
|
301
|
+
method: "POST",
|
|
302
|
+
path: `/v1/packs/${encodeURIComponent(packId)}/release`,
|
|
303
|
+
body: {}
|
|
304
|
+
},
|
|
305
|
+
{ idempotencyKey: `sdk:pack:release:${packId}` }
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
previewPackUpgrade(packId, incoming) {
|
|
309
|
+
required(packId, "SDK_PACK_ID_REQUIRED");
|
|
310
|
+
if (!incoming || typeof incoming !== "object")
|
|
311
|
+
throw new Error("SDK_PACK_UPGRADE_INPUT_REQUIRED");
|
|
312
|
+
return this.request(
|
|
313
|
+
{
|
|
314
|
+
method: "POST",
|
|
315
|
+
path: `/v1/packs/${encodeURIComponent(packId)}/upgrade-preview`,
|
|
316
|
+
body: { incoming }
|
|
317
|
+
},
|
|
318
|
+
{ idempotencyKey: `sdk:pack:upgrade-preview:${packId}` }
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
executeRuntime(input) {
|
|
322
|
+
return this.request(
|
|
323
|
+
{
|
|
324
|
+
method: "POST",
|
|
325
|
+
path: "/v1/runtime/execute",
|
|
326
|
+
body: buildRuntimeRequest(
|
|
327
|
+
input
|
|
328
|
+
)
|
|
329
|
+
},
|
|
330
|
+
{ idempotencyKey: input.idempotencyKey, enforcing: true }
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
resolveRuntime(input) {
|
|
334
|
+
if (!input.behaviorPoint && !input.executionBoundary && !input.semanticContext)
|
|
335
|
+
throw new Error("SDK_RUNTIME_ROUTE_REQUIRED");
|
|
336
|
+
return this.request(
|
|
337
|
+
{
|
|
338
|
+
method: "POST",
|
|
339
|
+
path: "/v1/runtime/resolve",
|
|
340
|
+
body: {
|
|
341
|
+
...input.behaviorPoint ? { behaviorPoint: input.behaviorPoint } : {},
|
|
342
|
+
...input.executionBoundary ? { executionBoundary: input.executionBoundary } : {},
|
|
343
|
+
...input.semanticContext ? { semanticContext: input.semanticContext } : {}
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
idempotencyKey: input.idempotencyKey ?? `sdk:resolve:${input.behaviorPoint ?? "boundary"}`,
|
|
348
|
+
enforcing: true
|
|
349
|
+
}
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
/** Resolve a runtime route from a governed execution boundary and semantic context. */
|
|
353
|
+
resolveBoundary(input) {
|
|
354
|
+
if (!input.executionBoundary || typeof input.executionBoundary !== "object")
|
|
355
|
+
throw new Error("SDK_EXECUTION_BOUNDARY_REQUIRED");
|
|
356
|
+
return this.resolveRuntime({
|
|
357
|
+
executionBoundary: input.executionBoundary,
|
|
358
|
+
...input.semanticContext ? { semanticContext: input.semanticContext } : {},
|
|
359
|
+
idempotencyKey: input.idempotencyKey
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
resolveSemantic(input) {
|
|
363
|
+
required(input.input, "SDK_SEMANTIC_INPUT_REQUIRED");
|
|
364
|
+
const { idempotencyKey, requestId, ...body } = input;
|
|
365
|
+
const resolvedRequestId = requestId ?? `sdk-semantic-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
366
|
+
return this.request(
|
|
367
|
+
{
|
|
368
|
+
method: "POST",
|
|
369
|
+
path: "/v1/semantic/resolve",
|
|
370
|
+
body: { ...body, requestId: resolvedRequestId }
|
|
371
|
+
},
|
|
372
|
+
{ idempotencyKey: idempotencyKey ?? `sdk:semantic:${resolvedRequestId}` }
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
async resolveSemanticRuntime(input) {
|
|
376
|
+
const semantic = await this.resolveSemantic(input);
|
|
377
|
+
if (!semantic) return null;
|
|
378
|
+
const body = semantic.body;
|
|
379
|
+
const receipt = body.receipt && typeof body.receipt === "object" ? body.receipt : {};
|
|
380
|
+
if (receipt.outcome !== "select" || typeof receipt.taskKey !== "string")
|
|
381
|
+
return { semantic, runtime: null };
|
|
382
|
+
const capsules = Array.isArray(body.capsules) ? body.capsules : [];
|
|
383
|
+
const firstCapsule = capsules[0] && typeof capsules[0] === "object" ? capsules[0] : {};
|
|
384
|
+
const behaviorPoint = typeof firstCapsule.behaviorKey === "string" && firstCapsule.behaviorKey.trim() ? firstCapsule.behaviorKey : receipt.taskKey;
|
|
385
|
+
const runtime = input.executionBoundary ? await this.resolveBoundary({
|
|
386
|
+
executionBoundary: input.executionBoundary,
|
|
387
|
+
semanticContext: {
|
|
388
|
+
taskKey: receipt.taskKey,
|
|
389
|
+
behaviorPoint,
|
|
390
|
+
capsule: firstCapsule
|
|
391
|
+
},
|
|
392
|
+
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
393
|
+
}) : await this.resolveRuntime({
|
|
394
|
+
semanticContext: { taskKey: receipt.taskKey, behaviorPoint, capsule: firstCapsule },
|
|
395
|
+
idempotencyKey: input.idempotencyKey ? `sdk:runtime:${input.idempotencyKey}` : void 0
|
|
396
|
+
});
|
|
397
|
+
return { semantic, runtime };
|
|
398
|
+
}
|
|
399
|
+
listBehaviorStrategies() {
|
|
400
|
+
return this.request({
|
|
401
|
+
method: "GET",
|
|
402
|
+
path: "/v1/behavior-strategies"
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
deleteBehaviorStrategy(strategyId, options = {}) {
|
|
406
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
407
|
+
return this.request({ method: "DELETE", path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:strategy:${strategyId}` });
|
|
408
|
+
}
|
|
409
|
+
getBehaviorStrategy(strategyId) {
|
|
410
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
411
|
+
return this.request({
|
|
412
|
+
method: "GET",
|
|
413
|
+
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}`
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
createBehaviorStrategy(definition, id) {
|
|
417
|
+
if (!definition) throw new Error("SDK_STRATEGY_DEFINITION_REQUIRED");
|
|
418
|
+
return this.request({
|
|
419
|
+
method: "POST",
|
|
420
|
+
path: "/v1/behavior-strategies",
|
|
421
|
+
body: { definition, ...id ? { id } : {} }
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
listSemanticTasks() {
|
|
425
|
+
return this.request({ method: "GET", path: "/v1/semantic-tasks" });
|
|
426
|
+
}
|
|
427
|
+
deleteSemanticTask(taskId, options = {}) {
|
|
428
|
+
required(taskId, "SDK_SEMANTIC_TASK_ID_REQUIRED");
|
|
429
|
+
return this.request({ method: "DELETE", path: `/v1/semantic-tasks/${encodeURIComponent(taskId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:semantic-task:${taskId}` });
|
|
430
|
+
}
|
|
431
|
+
createSemanticTask(task, options = {}) {
|
|
432
|
+
required(task?.taskKey, "SDK_SEMANTIC_TASK_KEY_REQUIRED");
|
|
433
|
+
return this.request({ method: "POST", path: "/v1/semantic-tasks", body: { ...task, ...options.id ? { id: options.id } : {} } }, { idempotencyKey: options.idempotencyKey ?? `sdk:semantic-task:${task.taskKey}` });
|
|
434
|
+
}
|
|
435
|
+
listBehaviorPoints() {
|
|
436
|
+
return this.request({ method: "GET", path: "/v1/behavior-points" });
|
|
437
|
+
}
|
|
438
|
+
deleteBehaviorPoint(pointId, options = {}) {
|
|
439
|
+
required(pointId, "SDK_BEHAVIOR_POINT_ID_REQUIRED");
|
|
440
|
+
return this.request({ method: "DELETE", path: `/v1/behavior-points/${encodeURIComponent(pointId)}`, body: { confirm: options.confirm === true } }, { idempotencyKey: options.idempotencyKey ?? `sdk:delete:behavior-point:${pointId}` });
|
|
441
|
+
}
|
|
442
|
+
createBehaviorPoint(point, options = {}) {
|
|
443
|
+
required(point?.key, "SDK_BEHAVIOR_POINT_KEY_REQUIRED");
|
|
444
|
+
if (!point.taskId && !point.taskIds?.length) throw new Error("SDK_BEHAVIOR_POINT_TASK_REQUIRED");
|
|
445
|
+
return this.request({ method: "POST", path: "/v1/behavior-points", body: { ...point, ...options.id ? { id: options.id } : {} } }, { idempotencyKey: options.idempotencyKey ?? `sdk:behavior-point:${point.key}` });
|
|
446
|
+
}
|
|
447
|
+
createBehaviorStrategyVariant(strategyId, variant, options = {}) {
|
|
448
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
449
|
+
if (!variant || typeof variant !== "object")
|
|
450
|
+
throw new Error("SDK_STRATEGY_VARIANT_REQUIRED");
|
|
451
|
+
return this.request(
|
|
452
|
+
{
|
|
453
|
+
method: "POST",
|
|
454
|
+
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/variants`,
|
|
455
|
+
body: variant
|
|
456
|
+
},
|
|
457
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
validateBehaviorStrategy(strategyId) {
|
|
461
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
462
|
+
return this.request({
|
|
463
|
+
method: "POST",
|
|
464
|
+
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/validate`,
|
|
465
|
+
body: {}
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
activateBehaviorStrategy(strategyId) {
|
|
469
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
470
|
+
return this.request({ method: "POST", path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/activate`, body: {} });
|
|
471
|
+
}
|
|
472
|
+
simulateBehaviorStrategy(strategyId, initialState = {}) {
|
|
473
|
+
required(strategyId, "SDK_STRATEGY_ID_REQUIRED");
|
|
474
|
+
return this.request({
|
|
475
|
+
method: "POST",
|
|
476
|
+
path: `/v1/behavior-strategies/${encodeURIComponent(strategyId)}/simulate`,
|
|
477
|
+
body: { initialState }
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
startStrategyExecution(input) {
|
|
481
|
+
required(input.artifactDigest, "SDK_ARTIFACT_DIGEST_REQUIRED");
|
|
482
|
+
for (const value of [
|
|
483
|
+
input.organizationId,
|
|
484
|
+
input.projectId,
|
|
485
|
+
input.environmentId,
|
|
486
|
+
input.revisionId,
|
|
487
|
+
input.idempotencyKey,
|
|
488
|
+
input.nonce,
|
|
489
|
+
input.decisionPlanId,
|
|
490
|
+
input.policy
|
|
491
|
+
])
|
|
492
|
+
required(value, "SDK_STRATEGY_START_FIELD_REQUIRED");
|
|
493
|
+
if (input.behaviorPoint !== void 0)
|
|
494
|
+
required(input.behaviorPoint, "SDK_BEHAVIOR_POINT_REQUIRED");
|
|
495
|
+
if (!Array.isArray(input.candidates) || input.candidates.length === 0)
|
|
496
|
+
throw new Error("SDK_STRATEGY_CANDIDATES_REQUIRED");
|
|
497
|
+
if (!Number.isFinite(input.expiresAt))
|
|
498
|
+
throw new Error("SDK_EXPIRY_REQUIRED");
|
|
499
|
+
const body = {
|
|
500
|
+
...buildRuntimeRequest(input),
|
|
501
|
+
artifactDigest: input.artifactDigest,
|
|
502
|
+
...input.behaviorPoint ? { behaviorPoint: input.behaviorPoint } : {},
|
|
503
|
+
...input.executionBoundary ? { executionBoundary: input.executionBoundary } : {},
|
|
504
|
+
...input.traceId ? { traceId: input.traceId } : {},
|
|
505
|
+
...input.executionId ? { executionId: input.executionId } : {},
|
|
506
|
+
...input.state ? { state: input.state } : {}
|
|
507
|
+
};
|
|
508
|
+
return this.request(
|
|
509
|
+
{ method: "POST", path: "/v1/runtime/strategy-executions", body },
|
|
510
|
+
{ idempotencyKey: input.idempotencyKey }
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
getStrategyExecution(executionId) {
|
|
514
|
+
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
515
|
+
return this.request({
|
|
516
|
+
method: "GET",
|
|
517
|
+
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}`
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
getStrategyExecutionTrace(executionId) {
|
|
521
|
+
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
522
|
+
return this.request({
|
|
523
|
+
method: "GET",
|
|
524
|
+
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/trace`
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
continueStrategyExecution(executionId, continuation) {
|
|
528
|
+
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
529
|
+
const input = typeof continuation === "string" ? { authorityDigest: continuation } : continuation ?? {};
|
|
530
|
+
return this.request({
|
|
531
|
+
method: "POST",
|
|
532
|
+
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/continue`,
|
|
533
|
+
body: {
|
|
534
|
+
...input.authorityDigest ? { authorityDigest: input.authorityDigest } : {},
|
|
535
|
+
...input.state ? { state: input.state } : {},
|
|
536
|
+
...input.references ? { references: input.references } : {}
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
cancelStrategyExecution(executionId) {
|
|
541
|
+
required(executionId, "SDK_EXECUTION_ID_REQUIRED");
|
|
542
|
+
return this.request({
|
|
543
|
+
method: "POST",
|
|
544
|
+
path: `/v1/runtime/strategy-executions/${encodeURIComponent(executionId)}/cancel`,
|
|
545
|
+
body: {}
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
listBehaviorPrograms() {
|
|
549
|
+
return this.request({
|
|
550
|
+
method: "GET",
|
|
551
|
+
path: "/v1/behavior-programs"
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
listCapabilities(options = {}) {
|
|
555
|
+
const query = options.surface ? `?surface=${encodeURIComponent(options.surface)}` : "";
|
|
556
|
+
return this.request({ method: "GET", path: `/v1/capabilities${query}` });
|
|
557
|
+
}
|
|
558
|
+
getBehaviorProgram(programId) {
|
|
559
|
+
required(programId, "SDK_PROGRAM_ID_REQUIRED");
|
|
560
|
+
return this.request({
|
|
561
|
+
method: "GET",
|
|
562
|
+
path: `/v1/behavior-programs/${encodeURIComponent(programId)}`
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
createBehaviorProgram(program, options = {}) {
|
|
566
|
+
if (!program || typeof program !== "object")
|
|
567
|
+
throw new Error("SDK_PROGRAM_REQUIRED");
|
|
568
|
+
return this.request(
|
|
569
|
+
{ method: "POST", path: "/v1/behavior-programs", body: program },
|
|
570
|
+
{
|
|
571
|
+
idempotencyKey: options.idempotencyKey ?? `sdk:program:${String(program.id ?? "")}`
|
|
572
|
+
}
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
attestCapabilities(attestation, options = {}) {
|
|
576
|
+
return this.request(
|
|
577
|
+
{
|
|
578
|
+
method: "POST",
|
|
579
|
+
path: "/v1/runtime/capabilities/attest",
|
|
580
|
+
body: attestation
|
|
581
|
+
},
|
|
582
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
resolveCapabilities(attestationId, request, options = {}) {
|
|
586
|
+
required(attestationId, "SDK_ATTESTATION_ID_REQUIRED");
|
|
587
|
+
return this.request(
|
|
588
|
+
{
|
|
589
|
+
method: "POST",
|
|
590
|
+
path: "/v1/runtime/capabilities/resolve",
|
|
591
|
+
body: { attestationId, request }
|
|
592
|
+
},
|
|
593
|
+
{ idempotencyKey: options.idempotencyKey, enforcing: true }
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
recordCapabilityTrace(decisionDigest, trace, options = {}) {
|
|
597
|
+
required(decisionDigest, "SDK_DECISION_DIGEST_REQUIRED");
|
|
598
|
+
return this.request(
|
|
599
|
+
{
|
|
600
|
+
method: "POST",
|
|
601
|
+
path: "/v1/runtime/capabilities/traces",
|
|
602
|
+
body: { decisionDigest, trace }
|
|
603
|
+
},
|
|
604
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
resolveModelServing(request, options = {}) {
|
|
608
|
+
return this.request(
|
|
609
|
+
{ method: "POST", path: "/v1/model-serving/resolve", body: request },
|
|
610
|
+
{ idempotencyKey: options.idempotencyKey, enforcing: true }
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
recordModelServingTrace(trace, selection, options = {}) {
|
|
614
|
+
return this.request(
|
|
615
|
+
{
|
|
616
|
+
method: "POST",
|
|
617
|
+
path: "/v1/model-serving/traces",
|
|
618
|
+
body: { trace, selection }
|
|
619
|
+
},
|
|
620
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
submitTeachEvidence(evidence, options = {}) {
|
|
624
|
+
return this.request(
|
|
625
|
+
{ method: "POST", path: "/v1/teach/evidence", body: evidence },
|
|
626
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
createTeachProposal(input, options = {}) {
|
|
630
|
+
return this.request({ method: "POST", path: "/v1/teach/proposals", body: input }, { idempotencyKey: options.idempotencyKey });
|
|
631
|
+
}
|
|
632
|
+
transitionTeachProposal(proposalId, transition, input = {}, options = {}) {
|
|
633
|
+
required(proposalId, "SDK_TEACH_PROPOSAL_ID_REQUIRED");
|
|
634
|
+
return this.request({ method: "POST", path: `/v1/teach/proposals/${encodeURIComponent(proposalId)}/${transition}`, body: input }, { idempotencyKey: options.idempotencyKey ?? `sdk:teach:${transition}:${proposalId}` });
|
|
635
|
+
}
|
|
636
|
+
startEvaluation(input, options = {}) {
|
|
637
|
+
return this.request(
|
|
638
|
+
{ method: "POST", path: "/v1/evaluations", body: input },
|
|
639
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
evaluateQuality(input, options = {}) {
|
|
643
|
+
return this.request(
|
|
644
|
+
{ method: "POST", path: "/v1/quality/evaluate", body: input },
|
|
645
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
getEvaluation(evaluationId) {
|
|
649
|
+
required(evaluationId, "SDK_EVALUATION_ID_REQUIRED");
|
|
650
|
+
return this.request({
|
|
651
|
+
method: "GET",
|
|
652
|
+
path: `/v1/evaluations/${encodeURIComponent(evaluationId)}`
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
createRelease(input, options = {}) {
|
|
656
|
+
return this.request(
|
|
657
|
+
{ method: "POST", path: "/v1/releases", body: input },
|
|
658
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
createManualRelease(input, options = {}) {
|
|
662
|
+
if (!input || typeof input !== "object" || !input.manualApproval) throw new Error("SDK_MANUAL_RELEASE_APPROVAL_REQUIRED");
|
|
663
|
+
return this.createRelease(input, { idempotencyKey: options.idempotencyKey });
|
|
664
|
+
}
|
|
665
|
+
recordDeploymentComparison(deploymentId, comparison, options = {}) {
|
|
666
|
+
required(deploymentId, "SDK_DEPLOYMENT_ID_REQUIRED");
|
|
667
|
+
return this.request(
|
|
668
|
+
{
|
|
669
|
+
method: "POST",
|
|
670
|
+
path: `/v1/deployments/${encodeURIComponent(deploymentId)}/comparisons`,
|
|
671
|
+
body: comparison
|
|
672
|
+
},
|
|
673
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
recordDeploymentHealth(deploymentId, health, options = {}) {
|
|
677
|
+
required(deploymentId, "SDK_DEPLOYMENT_ID_REQUIRED");
|
|
678
|
+
return this.request(
|
|
679
|
+
{
|
|
680
|
+
method: "POST",
|
|
681
|
+
path: `/v1/deployments/${encodeURIComponent(deploymentId)}/authoritative-health`,
|
|
682
|
+
body: health
|
|
683
|
+
},
|
|
684
|
+
{ idempotencyKey: options.idempotencyKey }
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
compileBehaviorCapsule(input) {
|
|
688
|
+
required(input.programId, "SDK_PROGRAM_ID_REQUIRED");
|
|
689
|
+
required(input.input, "SDK_RUNTIME_INPUT_REQUIRED");
|
|
690
|
+
return this.request(
|
|
691
|
+
{
|
|
692
|
+
method: "POST",
|
|
693
|
+
path: "/v1/runtime/behavior-capsule",
|
|
694
|
+
body: { ...input }
|
|
695
|
+
},
|
|
696
|
+
{
|
|
697
|
+
idempotencyKey: input.idempotencyKey ?? `sdk:capsule:${input.programId}`
|
|
698
|
+
}
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
serveBehaviorProjection(input) {
|
|
702
|
+
required(input.input, "SDK_RUNTIME_INPUT_REQUIRED");
|
|
703
|
+
required(input.provider, "SDK_PROVIDER_REQUIRED");
|
|
704
|
+
return this.request(
|
|
705
|
+
{
|
|
706
|
+
method: "POST",
|
|
707
|
+
path: "/v1/runtime/behavior-projection",
|
|
708
|
+
body: { ...input }
|
|
709
|
+
},
|
|
710
|
+
{
|
|
711
|
+
idempotencyKey: input.idempotencyKey ?? `sdk:projection:${input.programId ?? "active"}`
|
|
712
|
+
}
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
async testConnection(connectionId) {
|
|
716
|
+
required(connectionId, "SDK_CONNECTION_ID_REQUIRED");
|
|
717
|
+
return this.request({
|
|
718
|
+
method: "POST",
|
|
719
|
+
path: `/v1/connections/${encodeURIComponent(connectionId)}/test`,
|
|
720
|
+
body: {}
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
function buildRuntimeRequest(input) {
|
|
725
|
+
const {
|
|
726
|
+
organizationId,
|
|
727
|
+
projectId,
|
|
728
|
+
environmentId,
|
|
729
|
+
revisionId,
|
|
730
|
+
idempotencyKey,
|
|
731
|
+
nonce,
|
|
732
|
+
expiresAt,
|
|
733
|
+
decisionPlanId,
|
|
734
|
+
calibrationId,
|
|
735
|
+
candidates,
|
|
736
|
+
allowedIds,
|
|
737
|
+
policy,
|
|
738
|
+
postModelJudge,
|
|
739
|
+
shadow
|
|
740
|
+
} = input;
|
|
741
|
+
for (const value of [
|
|
742
|
+
organizationId,
|
|
743
|
+
projectId,
|
|
744
|
+
environmentId,
|
|
745
|
+
revisionId,
|
|
746
|
+
idempotencyKey,
|
|
747
|
+
nonce
|
|
748
|
+
])
|
|
749
|
+
if (typeof value !== "string" || !value.trim())
|
|
750
|
+
throw new Error("SDK_REQUEST_FIELD_REQUIRED");
|
|
751
|
+
if (!Number.isFinite(expiresAt)) throw new Error("SDK_EXPIRY_REQUIRED");
|
|
752
|
+
const boundary = {
|
|
753
|
+
...typeof decisionPlanId === "string" ? { decisionPlanId } : {},
|
|
754
|
+
...typeof calibrationId === "string" ? { calibrationId } : {},
|
|
755
|
+
...Array.isArray(candidates) ? { candidates } : {},
|
|
756
|
+
...Array.isArray(allowedIds) ? { allowedIds } : {},
|
|
757
|
+
...typeof policy === "string" ? { policy } : {},
|
|
758
|
+
...postModelJudge !== void 0 ? { postModelJudge } : {},
|
|
759
|
+
...shadow !== void 0 ? { shadow } : {}
|
|
760
|
+
};
|
|
761
|
+
return Object.freeze({
|
|
762
|
+
organizationId,
|
|
763
|
+
projectId,
|
|
764
|
+
environmentId,
|
|
765
|
+
revisionId,
|
|
766
|
+
idempotencyKey,
|
|
767
|
+
nonce,
|
|
768
|
+
expiresAt,
|
|
769
|
+
...boundary
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
function buildPublicContentRequest(input) {
|
|
773
|
+
const { slug, version = "v1" } = input;
|
|
774
|
+
if (typeof slug !== "string" || !slug.trim())
|
|
775
|
+
throw new Error("SDK_SLUG_REQUIRED");
|
|
776
|
+
if (version !== "v1") throw new Error("SDK_VERSION_UNSUPPORTED");
|
|
777
|
+
return Object.freeze({
|
|
778
|
+
method: "GET",
|
|
779
|
+
path: "/v1/content",
|
|
780
|
+
query: Object.freeze({ slug })
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
function buildDocsQueryRequest(input) {
|
|
784
|
+
const { query, operation = "search" } = input;
|
|
785
|
+
if (typeof query !== "string" || !query.trim())
|
|
786
|
+
throw new Error("SDK_QUERY_REQUIRED");
|
|
787
|
+
if (!["search", "ask"].includes(operation))
|
|
788
|
+
throw new Error("SDK_OPERATION_UNSUPPORTED");
|
|
789
|
+
return Object.freeze({
|
|
790
|
+
method: "GET",
|
|
791
|
+
path: `/docs/${operation}`,
|
|
792
|
+
query: Object.freeze({ query })
|
|
793
|
+
});
|
|
794
|
+
}
|
|
795
|
+
function buildAISystemRequest(input) {
|
|
796
|
+
const { name, systemType, organizationId, projectId, environmentId } = input;
|
|
797
|
+
for (const value of [
|
|
798
|
+
name,
|
|
799
|
+
systemType,
|
|
800
|
+
organizationId,
|
|
801
|
+
projectId,
|
|
802
|
+
environmentId
|
|
803
|
+
])
|
|
804
|
+
if (typeof value !== "string" || !value.trim())
|
|
805
|
+
throw new Error("SDK_AI_SYSTEM_FIELD_REQUIRED");
|
|
806
|
+
return Object.freeze({
|
|
807
|
+
method: "POST",
|
|
808
|
+
path: "/v1/ai-systems",
|
|
809
|
+
body: Object.freeze({
|
|
810
|
+
name,
|
|
811
|
+
systemType,
|
|
812
|
+
organizationId,
|
|
813
|
+
projectId,
|
|
814
|
+
environmentId
|
|
815
|
+
})
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
function buildObservationRequest(event) {
|
|
819
|
+
if (!event?.eventId || !event.eventType || !event.aiSystemId || !event.connectionId)
|
|
820
|
+
throw new Error("SDK_OBSERVATION_FIELD_REQUIRED");
|
|
821
|
+
return Object.freeze({
|
|
822
|
+
method: "POST",
|
|
823
|
+
path: "/v1/observations",
|
|
824
|
+
body: Object.freeze({ ...event })
|
|
825
|
+
});
|
|
826
|
+
}
|
|
827
|
+
export {
|
|
828
|
+
ViraApiError,
|
|
829
|
+
ViraClient,
|
|
830
|
+
buildAISystemRequest,
|
|
831
|
+
buildDocsQueryRequest,
|
|
832
|
+
buildObservationRequest,
|
|
833
|
+
buildPublicContentRequest,
|
|
834
|
+
buildRuntimeRequest
|
|
835
|
+
};
|