@tryvira/sdk 0.1.96 → 0.1.98
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +46 -0
- package/dist/sdks/js-client.d.ts +28 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ Compound execution plans can be persisted with `saveCompoundTaskPlan(plan, {idem
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
npm install @tryvira/sdk@0.1.
|
|
18
|
+
npm install @tryvira/sdk@0.1.98
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Configure the organization, project, and environment for every request. Keep the API
|
package/dist/index.js
CHANGED
|
@@ -54,6 +54,17 @@ var retryAfterMs = (value, now = Date.now()) => {
|
|
|
54
54
|
const date = Date.parse(value);
|
|
55
55
|
return Number.isFinite(date) && date > now ? Math.min(date - now, 12e4) : 0;
|
|
56
56
|
};
|
|
57
|
+
function notifyTransport(observer, method, status, error, started, attempt) {
|
|
58
|
+
if (!observer) return;
|
|
59
|
+
try {
|
|
60
|
+
const known = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
61
|
+
const outcome = status === 429 ? "http_429" : status !== void 0 ? status >= 200 && status < 600 ? "http_" + Math.floor(status / 100) + "xx" : "http_other" : error instanceof Error && ["AbortError", "TimeoutError"].includes(error.name) ? "timeout" : "transport_error";
|
|
62
|
+
const event = Object.freeze({ method: known.includes(method) ? method : "OTHER", outcome, durationMs: Math.max(0, performance.now() - started), attempt: attempt + 1 });
|
|
63
|
+
void Promise.resolve(observer(event)).catch(() => {
|
|
64
|
+
});
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
57
68
|
async function viraHttpRequest(url, options) {
|
|
58
69
|
const timeoutMs = Math.max(1e3, Math.min(options.timeoutMs ?? 15e3, 12e4));
|
|
59
70
|
const retries = Math.max(0, Math.min(options.retries ?? 2, 5));
|
|
@@ -73,9 +84,13 @@ async function viraHttpRequest(url, options) {
|
|
|
73
84
|
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
74
85
|
const controller = new AbortController();
|
|
75
86
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
87
|
+
const started = options.transportObserver ? performance.now() : 0;
|
|
88
|
+
let reported = false;
|
|
76
89
|
try {
|
|
77
90
|
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 });
|
|
78
91
|
const raw = await response.text();
|
|
92
|
+
notifyTransport(options.transportObserver, options.method, response.status, void 0, started, attempt);
|
|
93
|
+
reported = true;
|
|
79
94
|
let body = null;
|
|
80
95
|
try {
|
|
81
96
|
body = raw ? JSON.parse(raw) : null;
|
|
@@ -94,6 +109,7 @@ async function viraHttpRequest(url, options) {
|
|
|
94
109
|
}
|
|
95
110
|
return { status: response.status, body, requestId };
|
|
96
111
|
} catch (error) {
|
|
112
|
+
if (!reported) notifyTransport(options.transportObserver, options.method, void 0, error, started, attempt);
|
|
97
113
|
lastError = error;
|
|
98
114
|
if (error instanceof ViraApiError) throw error;
|
|
99
115
|
if (attempt + 1 < attempts) {
|
|
@@ -116,6 +132,7 @@ var ViraClient = class {
|
|
|
116
132
|
apiCredentialProvider;
|
|
117
133
|
operatorDelegationTokenProvider;
|
|
118
134
|
fetcher;
|
|
135
|
+
transportObserver;
|
|
119
136
|
timeoutMs;
|
|
120
137
|
retries;
|
|
121
138
|
failureMode;
|
|
@@ -127,6 +144,7 @@ var ViraClient = class {
|
|
|
127
144
|
this.apiCredentialProvider = options.apiCredentialProvider ?? options.accessTokenProvider;
|
|
128
145
|
this.operatorDelegationTokenProvider = options.operatorDelegationTokenProvider;
|
|
129
146
|
this.fetcher = options.fetcher ?? fetch;
|
|
147
|
+
this.transportObserver = options.transportObserver;
|
|
130
148
|
this.timeoutMs = Math.max(
|
|
131
149
|
1e3,
|
|
132
150
|
Math.min(options.timeoutMs ?? 15e3, 12e4)
|
|
@@ -196,6 +214,11 @@ var ViraClient = class {
|
|
|
196
214
|
if (options.limit !== void 0 && !Number.isFinite(options.limit)) throw new Error("SDK_NOTIFICATION_LIMIT_INVALID");
|
|
197
215
|
return this.request({ method: "GET", path: "/v1/notifications/inbox", query: { ...limit === void 0 ? {} : { limit: String(limit) }, ...options.cursor ? { cursor: options.cursor } : {}, ...options.search ? { search: options.search } : {} } });
|
|
198
216
|
}
|
|
217
|
+
readNotificationUpdates(options = {}) {
|
|
218
|
+
const count = options.count ?? 32;
|
|
219
|
+
if (!Number.isSafeInteger(count) || count < 1 || count > 100) throw new Error("SDK_NOTIFICATION_UPDATES_COUNT_INVALID");
|
|
220
|
+
return this.request({ method: "GET", path: "/v1/notifications/updates", query: { afterId: options.afterId ?? "latest", count: String(count) } });
|
|
221
|
+
}
|
|
199
222
|
markInboxRead(notificationId, options = {}) {
|
|
200
223
|
if (!notificationId.trim()) throw new Error("SDK_NOTIFICATION_ID_REQUIRED");
|
|
201
224
|
return this.request({ method: "POST", path: `/v1/notifications/${encodeURIComponent(notificationId)}/read`, body: {} }, { idempotencyKey: options.idempotencyKey ?? `sdk:inbox-read:${notificationId}` });
|
|
@@ -278,6 +301,8 @@ var ViraClient = class {
|
|
|
278
301
|
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
279
302
|
const controller = new AbortController();
|
|
280
303
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
304
|
+
const started = this.transportObserver ? performance.now() : 0;
|
|
305
|
+
let reported = false;
|
|
281
306
|
try {
|
|
282
307
|
const response = await this.fetcher(url, {
|
|
283
308
|
method: request.method,
|
|
@@ -287,6 +312,8 @@ var ViraClient = class {
|
|
|
287
312
|
...operatorOperation ? { redirect: "error", credentials: "omit" } : {}
|
|
288
313
|
});
|
|
289
314
|
const raw = await response.text();
|
|
315
|
+
notifyTransport(this.transportObserver, request.method, response.status, void 0, started, attempt);
|
|
316
|
+
reported = true;
|
|
290
317
|
let body = null;
|
|
291
318
|
try {
|
|
292
319
|
body = raw ? JSON.parse(raw) : null;
|
|
@@ -313,6 +340,7 @@ var ViraClient = class {
|
|
|
313
340
|
}
|
|
314
341
|
return { status: response.status, body, requestId };
|
|
315
342
|
} catch (error) {
|
|
343
|
+
if (!reported) notifyTransport(this.transportObserver, request.method, void 0, error, started, attempt);
|
|
316
344
|
lastError = error;
|
|
317
345
|
if (error instanceof ViraApiError) throw error;
|
|
318
346
|
if (attempt + 1 < attempts) {
|
|
@@ -766,6 +794,24 @@ var ViraClient = class {
|
|
|
766
794
|
return response;
|
|
767
795
|
})();
|
|
768
796
|
}
|
|
797
|
+
/** Explicit opt-in upload through the existing scoped observation authority.
|
|
798
|
+
* Do not call automatically from a transport observer (recursive reporting).
|
|
799
|
+
* May be subject to observation quotas and negotiated latency capability.
|
|
800
|
+
*/
|
|
801
|
+
reportTransportLatency(input, options = {}) {
|
|
802
|
+
if (!Number.isFinite(input.durationMs) || input.durationMs < 0 || input.durationMs > 36e5) throw Error("SDK_TRANSPORT_LATENCY_INVALID");
|
|
803
|
+
return this.observe({
|
|
804
|
+
organizationId: input.organizationId,
|
|
805
|
+
projectId: input.projectId,
|
|
806
|
+
environmentId: input.environmentId,
|
|
807
|
+
...input.organizationUnitId ? { organizationUnitId: input.organizationUnitId } : {},
|
|
808
|
+
eventId: input.eventId,
|
|
809
|
+
eventType: "latency",
|
|
810
|
+
aiSystemId: input.aiSystemId,
|
|
811
|
+
connectionId: input.connectionId,
|
|
812
|
+
payload: { latencyMs: input.durationMs }
|
|
813
|
+
}, options);
|
|
814
|
+
}
|
|
769
815
|
listObservations(input = {}) {
|
|
770
816
|
const query = new URLSearchParams();
|
|
771
817
|
if (input.limit !== void 0) query.set("limit", String(input.limit));
|
package/dist/sdks/js-client.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface SdkRequest {
|
|
|
16
16
|
export type SdkFailureMode = "throw" | "open";
|
|
17
17
|
export type ViraApiCredentialProvider = () => string | null | undefined | Promise<string | null | undefined>;
|
|
18
18
|
export interface ViraClientOptions {
|
|
19
|
+
/** Optional local callback. The SDK does not upload these observations. */
|
|
20
|
+
transportObserver?: ViraTransportObserver;
|
|
19
21
|
baseUrl: string;
|
|
20
22
|
/** Explicit opt-in for plain HTTP on loopback during local development only. */
|
|
21
23
|
allowInsecureLocalhost?: boolean;
|
|
@@ -914,7 +916,15 @@ type ClientObservation = ClientObservationScope & {
|
|
|
914
916
|
sessionId?: string;
|
|
915
917
|
[key: string]: unknown;
|
|
916
918
|
};
|
|
919
|
+
export interface ViraTransportObservation {
|
|
920
|
+
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OTHER';
|
|
921
|
+
outcome: 'http_2xx' | 'http_3xx' | 'http_4xx' | 'http_429' | 'http_5xx' | 'http_other' | 'timeout' | 'transport_error';
|
|
922
|
+
durationMs: number;
|
|
923
|
+
attempt: number;
|
|
924
|
+
}
|
|
925
|
+
export type ViraTransportObserver = (event: Readonly<ViraTransportObservation>) => void | Promise<void>;
|
|
917
926
|
export interface ViraHttpRequestOptions {
|
|
927
|
+
transportObserver?: ViraTransportObserver;
|
|
918
928
|
apiKey?: string;
|
|
919
929
|
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
920
930
|
body?: unknown;
|
|
@@ -936,6 +946,7 @@ export declare class ViraClient {
|
|
|
936
946
|
private readonly apiCredentialProvider?;
|
|
937
947
|
private readonly operatorDelegationTokenProvider?;
|
|
938
948
|
private readonly fetcher;
|
|
949
|
+
private readonly transportObserver?;
|
|
939
950
|
private readonly timeoutMs;
|
|
940
951
|
private readonly retries;
|
|
941
952
|
private readonly failureMode;
|
|
@@ -977,6 +988,10 @@ export declare class ViraClient {
|
|
|
977
988
|
cursor?: string;
|
|
978
989
|
search?: string;
|
|
979
990
|
}): Promise<ViraResponse<ClientBody> | null>;
|
|
991
|
+
readNotificationUpdates(options?: {
|
|
992
|
+
afterId?: string;
|
|
993
|
+
count?: number;
|
|
994
|
+
}): Promise<ViraResponse<ClientBody> | null>;
|
|
980
995
|
markInboxRead(notificationId: string, options?: {
|
|
981
996
|
idempotencyKey?: string;
|
|
982
997
|
}): Promise<ViraResponse<ClientBody> | null>;
|
|
@@ -1201,6 +1216,19 @@ export declare class ViraClient {
|
|
|
1201
1216
|
observe(event: ClientObservation, options?: {
|
|
1202
1217
|
idempotencyKey?: string;
|
|
1203
1218
|
}): Promise<ViraResponse<ObservationAdmissionResult> | null>;
|
|
1219
|
+
/** Explicit opt-in upload through the existing scoped observation authority.
|
|
1220
|
+
* Do not call automatically from a transport observer (recursive reporting).
|
|
1221
|
+
* May be subject to observation quotas and negotiated latency capability.
|
|
1222
|
+
*/
|
|
1223
|
+
reportTransportLatency(input: ClientObservationScope & {
|
|
1224
|
+
eventId: string;
|
|
1225
|
+
aiSystemId: string;
|
|
1226
|
+
connectionId: string;
|
|
1227
|
+
organizationUnitId?: string;
|
|
1228
|
+
durationMs: number;
|
|
1229
|
+
}, options?: {
|
|
1230
|
+
idempotencyKey?: string;
|
|
1231
|
+
}): Promise<ViraResponse<ObservationAdmissionResult> | null>;
|
|
1204
1232
|
listObservations(input?: {
|
|
1205
1233
|
limit?: number;
|
|
1206
1234
|
cursor?: string;
|