@tryvira/sdk 0.1.66 → 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/index.js CHANGED
@@ -48,6 +48,59 @@ var retryAfterMs = (value, now = Date.now()) => {
48
48
  const date = Date.parse(value);
49
49
  return Number.isFinite(date) && date > now ? Math.min(date - now, 12e4) : 0;
50
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
+ }
51
104
  var ViraClient = class {
52
105
  evaluations;
53
106
  teach;
@@ -1614,5 +1667,6 @@ export {
1614
1667
  createViraMiddleware,
1615
1668
  redactToolObservation,
1616
1669
  resolveFrameworkBoundary,
1617
- resolveHostToolBinding
1670
+ resolveHostToolBinding,
1671
+ viraHttpRequest
1618
1672
  };
@@ -184,6 +184,18 @@ type ClientObservation = ClientObservationScope & {
184
184
  sessionId?: string;
185
185
  [key: string]: unknown;
186
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>>;
187
199
  /** Small, authority-aware client over the existing versioned control API. */
188
200
  export declare class ViraClient {
189
201
  readonly evaluations: ViraLearningNamespaces['evaluations'];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryvira/sdk",
3
- "version": "0.1.66",
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"},