@opendatalabs/vana-sdk 3.18.0 → 3.19.0

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.
Files changed (37) hide show
  1. package/README.md +129 -3
  2. package/dist/errors.cjs +56 -0
  3. package/dist/errors.cjs.map +1 -1
  4. package/dist/errors.d.ts +98 -2
  5. package/dist/errors.js +48 -0
  6. package/dist/errors.js.map +1 -1
  7. package/dist/index.browser.d.ts +1 -0
  8. package/dist/index.browser.js +542 -33
  9. package/dist/index.browser.js.map +4 -4
  10. package/dist/index.node.cjs +569 -33
  11. package/dist/index.node.cjs.map +4 -4
  12. package/dist/index.node.d.ts +1 -0
  13. package/dist/index.node.js +542 -33
  14. package/dist/index.node.js.map +4 -4
  15. package/dist/protocol/derivative-questions.cjs +501 -0
  16. package/dist/protocol/derivative-questions.cjs.map +1 -0
  17. package/dist/protocol/derivative-questions.d.ts +355 -0
  18. package/dist/protocol/derivative-questions.js +486 -0
  19. package/dist/protocol/derivative-questions.js.map +1 -0
  20. package/dist/protocol/derivative-questions.test.d.ts +1 -0
  21. package/dist/protocol/lineage.cjs +9 -4
  22. package/dist/protocol/lineage.cjs.map +1 -1
  23. package/dist/protocol/lineage.d.ts +21 -12
  24. package/dist/protocol/lineage.js +9 -4
  25. package/dist/protocol/lineage.js.map +1 -1
  26. package/dist/protocol/personal-server-write.cjs +12 -108
  27. package/dist/protocol/personal-server-write.cjs.map +1 -1
  28. package/dist/protocol/personal-server-write.d.ts +2 -17
  29. package/dist/protocol/personal-server-write.js +7 -98
  30. package/dist/protocol/personal-server-write.js.map +1 -1
  31. package/dist/protocol/write-request.cjs +142 -0
  32. package/dist/protocol/write-request.cjs.map +1 -0
  33. package/dist/protocol/write-request.d.ts +63 -0
  34. package/dist/protocol/write-request.js +111 -0
  35. package/dist/protocol/write-request.js.map +1 -0
  36. package/dist/tests/mock-personal-server.d.ts +51 -0
  37. package/package.json +1 -1
@@ -0,0 +1,501 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var derivative_questions_exports = {};
20
+ __export(derivative_questions_exports, {
21
+ DEFAULT_QUESTION_POLL_INTERVAL_MS: () => DEFAULT_QUESTION_POLL_INTERVAL_MS,
22
+ DEFAULT_QUESTION_TIMEOUT_MS: () => DEFAULT_QUESTION_TIMEOUT_MS,
23
+ DERIVATIVE_QUESTIONS_PATH: () => DERIVATIVE_QUESTIONS_PATH,
24
+ DerivativeQuestionSchema: () => DerivativeQuestionSchema,
25
+ MAX_QUESTION_CHARS: () => MAX_QUESTION_CHARS,
26
+ MAX_QUESTION_MODEL_CHARS: () => MAX_QUESTION_MODEL_CHARS,
27
+ MAX_QUESTION_SOURCE_SCOPES: () => MAX_QUESTION_SOURCE_SCOPES,
28
+ QUESTION_STATUSES: () => QUESTION_STATUSES,
29
+ QuestionDeleteResultSchema: () => QuestionDeleteResultSchema,
30
+ QuestionRecomputeResultSchema: () => QuestionRecomputeResultSchema,
31
+ QuestionRegisteredBySchema: () => QuestionRegisteredBySchema,
32
+ QuestionStatusSchema: () => QuestionStatusSchema,
33
+ askPersonalServer: () => askPersonalServer,
34
+ deleteQuestion: () => deleteQuestion,
35
+ getQuestion: () => getQuestion,
36
+ listQuestions: () => listQuestions,
37
+ recomputeQuestion: () => recomputeQuestion,
38
+ registerQuestion: () => registerQuestion,
39
+ waitForQuestion: () => waitForQuestion
40
+ });
41
+ module.exports = __toCommonJS(derivative_questions_exports);
42
+ var import_zod = require("zod");
43
+ var import_web3_signed_builder = require("../auth/web3-signed-builder");
44
+ var import_errors = require("../errors");
45
+ var import_lineage = require("./lineage");
46
+ var import_personal_server_error_body = require("./personal-server-error-body");
47
+ var import_personal_server_data = require("./personal-server-data");
48
+ var import_personal_server_write = require("./personal-server-write");
49
+ var import_write_request = require("./write-request");
50
+ var import_write_signer = require("./write-signer");
51
+ const DERIVATIVE_QUESTIONS_PATH = "/v1/derivatives/questions";
52
+ const MAX_QUESTION_SOURCE_SCOPES = 16;
53
+ const MAX_QUESTION_CHARS = 8e3;
54
+ const MAX_QUESTION_MODEL_CHARS = 128;
55
+ const MODEL_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;
56
+ const DEFAULT_QUESTION_TIMEOUT_MS = 12e4;
57
+ const DEFAULT_QUESTION_POLL_INTERVAL_MS = 2e3;
58
+ const SESSION_REFRESH_SKEW_MS = 3e4;
59
+ const QUESTION_STATUSES = [
60
+ "pending",
61
+ "ready",
62
+ "failed",
63
+ "stale"
64
+ ];
65
+ const QuestionStatusSchema = import_zod.z.enum(QUESTION_STATUSES);
66
+ const QuestionRegisteredBySchema = import_zod.z.union([
67
+ import_zod.z.object({ kind: import_zod.z.literal("owner") }),
68
+ import_zod.z.object({
69
+ kind: import_zod.z.literal("builder"),
70
+ builder: import_zod.z.string(),
71
+ grantId: import_zod.z.string()
72
+ })
73
+ ]);
74
+ const nullableString = import_zod.z.string().nullish().transform((value) => value ?? null);
75
+ const DerivativeQuestionSchema = import_zod.z.object({
76
+ questionId: import_zod.z.string().min(1),
77
+ derivedScope: import_zod.z.string().min(1),
78
+ sourceScopes: import_zod.z.array(import_zod.z.string()),
79
+ question: import_zod.z.string(),
80
+ /** The model override, or `null` for the server's default. */
81
+ model: nullableString,
82
+ registeredBy: QuestionRegisteredBySchema,
83
+ status: QuestionStatusSchema,
84
+ /** A short reason, set only while `status` is `failed`. */
85
+ error: nullableString,
86
+ createdAt: import_zod.z.string(),
87
+ updatedAt: nullableString,
88
+ /** When the last compute finished, or `null` while `pending`. */
89
+ lastComputedAt: nullableString,
90
+ /** Local version of the derived record the last compute wrote. */
91
+ derivedVersion: import_zod.z.number().nullish().transform((value) => value ?? null),
92
+ derivedCollectedAt: nullableString
93
+ });
94
+ const QuestionListSchema = import_zod.z.object({
95
+ questions: import_zod.z.array(DerivativeQuestionSchema)
96
+ });
97
+ const QuestionRecomputeResultSchema = import_zod.z.object({
98
+ questionId: import_zod.z.string().min(1),
99
+ derivedScope: import_zod.z.string().min(1),
100
+ /** `pending` when the question was never computed, else `stale`. */
101
+ status: QuestionStatusSchema
102
+ });
103
+ const QuestionDeleteResultSchema = import_zod.z.object({
104
+ questionId: import_zod.z.string().min(1),
105
+ deleted: import_zod.z.literal(true)
106
+ });
107
+ const sessionsBySigner = /* @__PURE__ */ new WeakMap();
108
+ function sessionCacheKey(personalServerUrl, audience, grantId, fetchFn) {
109
+ return JSON.stringify([personalServerUrl, audience, grantId]) + fetchIdOf(fetchFn);
110
+ }
111
+ const fetchIds = /* @__PURE__ */ new WeakMap();
112
+ let nextFetchId = 0;
113
+ function fetchIdOf(fetchFn) {
114
+ let id = fetchIds.get(fetchFn);
115
+ if (id === void 0) {
116
+ id = ++nextFetchId;
117
+ fetchIds.set(fetchFn, id);
118
+ }
119
+ return `#${id}`;
120
+ }
121
+ function resolveRequest(params) {
122
+ if (typeof params.personalServerUrl !== "string" || params.personalServerUrl.length === 0) {
123
+ throw new import_errors.WriteRequestError("personalServerUrl is required");
124
+ }
125
+ if (typeof params.grantId !== "string" || params.grantId.length === 0) {
126
+ throw new import_errors.WriteRequestError(
127
+ "grantId is required; a question call runs under the grant carrying write:<derivedScope>"
128
+ );
129
+ }
130
+ if (params.signer === null || typeof params.signer !== "object") {
131
+ throw new import_errors.WriteRequestError(
132
+ "signer must be a viem LocalAccount, a viem WalletClient, or a { signMessage } object"
133
+ );
134
+ }
135
+ const fetchFn = (0, import_write_request.resolveFetch)(params.fetch);
136
+ const baseUrl = (0, import_write_request.normalizeBaseUrl)(params.personalServerUrl);
137
+ const audience = params.audience ?? baseUrl;
138
+ return {
139
+ baseUrl,
140
+ audience,
141
+ fetchFn,
142
+ cacheKey: sessionCacheKey(baseUrl, audience, params.grantId, fetchFn),
143
+ signerKey: params.signer
144
+ };
145
+ }
146
+ async function resolveSession(params, resolved, force) {
147
+ let cache = sessionsBySigner.get(resolved.signerKey);
148
+ if (cache === void 0) {
149
+ cache = /* @__PURE__ */ new Map();
150
+ sessionsBySigner.set(resolved.signerKey, cache);
151
+ }
152
+ const cached = cache.get(resolved.cacheKey);
153
+ if (!force && cached !== void 0 && cached.expiresAt > Date.now() + SESSION_REFRESH_SKEW_MS) {
154
+ return cached;
155
+ }
156
+ if (force) cache.delete(resolved.cacheKey);
157
+ const session = await (0, import_personal_server_write.openWriteSession)({
158
+ personalServerUrl: resolved.baseUrl,
159
+ signer: params.signer,
160
+ grantId: params.grantId,
161
+ account: params.account,
162
+ audience: resolved.audience,
163
+ fetch: resolved.fetchFn,
164
+ headers: params.headers,
165
+ retry: params.retry
166
+ });
167
+ cache.set(resolved.cacheKey, session);
168
+ return session;
169
+ }
170
+ async function questionErrorFromResponse(response) {
171
+ const { errorCode, message, details } = await (0, import_personal_server_error_body.readPersonalServerErrorBody)(response);
172
+ const text = message ?? `Derivative question request failed: ${response.status} ${response.statusText}`;
173
+ switch (errorCode) {
174
+ case "DERIVATIVE_SOURCE_NOT_GRANTED":
175
+ return new import_errors.DerivativeSourceNotGrantedError(text, errorCode, details);
176
+ case "DERIVATIVE_CYCLE":
177
+ return new import_errors.DerivativeCycleError(text, errorCode, details);
178
+ case "DERIVATIVE_COMPUTE_UNAVAILABLE":
179
+ return new import_errors.DerivativeComputeUnavailableError(text, errorCode, details);
180
+ case "DERIVATIVE_QUESTION_INVALID":
181
+ case "LINEAGE_SCOPE_UNDER_SOURCE_PREFIX":
182
+ return new import_errors.DerivativeQuestionInvalidError(
183
+ text,
184
+ response.status,
185
+ errorCode,
186
+ details
187
+ );
188
+ case "DERIVATIVE_QUESTION_NOT_FOUND":
189
+ return new import_errors.DerivativeQuestionNotFoundError(text, errorCode, details);
190
+ default:
191
+ break;
192
+ }
193
+ switch (response.status) {
194
+ case 401:
195
+ return new import_errors.WriteUnauthorizedError(text, errorCode, details);
196
+ case 403:
197
+ return new import_errors.WriteForbiddenError(text, errorCode, details);
198
+ case 404:
199
+ return new import_errors.DerivativeQuestionNotFoundError(text, errorCode, details);
200
+ case 409:
201
+ return new import_errors.WriteConflictError(text, errorCode, details);
202
+ default:
203
+ return new import_errors.DerivativeQuestionRejectedError(
204
+ text,
205
+ response.status,
206
+ errorCode,
207
+ details
208
+ );
209
+ }
210
+ }
211
+ async function sendOnce(params, resolved, session, spec, bodyBytes) {
212
+ return (0, import_write_request.sendWithFreshProof)(
213
+ spec.label,
214
+ resolved.fetchFn,
215
+ params.retry,
216
+ (0, import_write_request.proofKeyFor)({
217
+ aud: session.audience,
218
+ method: spec.method,
219
+ uri: spec.path,
220
+ grantId: session.grantId,
221
+ signedBytes: bodyBytes
222
+ }),
223
+ async (iat) => {
224
+ const headers = new Headers(params.headers);
225
+ headers.set("Accept", "application/json");
226
+ headers.set("Authorization", `Bearer ${session.accessToken}`);
227
+ if (bodyBytes !== void 0) {
228
+ headers.set("Content-Type", "application/json");
229
+ }
230
+ headers.set(
231
+ import_personal_server_write.WRITE_SIGNATURE_HEADER,
232
+ await (0, import_web3_signed_builder.buildWeb3SignedHeader)({
233
+ signMessage: session.signer.signMessage,
234
+ aud: session.audience,
235
+ // The Personal Server verifies the proof against the request's
236
+ // path only, so the signed `uri` must not carry the query string.
237
+ uri: spec.path,
238
+ method: spec.method,
239
+ body: bodyBytes,
240
+ grantId: session.grantId,
241
+ iat
242
+ })
243
+ );
244
+ return {
245
+ url: `${resolved.baseUrl}${spec.path}${spec.query ?? ""}`,
246
+ init: {
247
+ method: spec.method,
248
+ headers,
249
+ ...bodyBytes === void 0 ? {} : { body: bodyBytes },
250
+ ...params.signal ? { signal: params.signal } : {}
251
+ }
252
+ };
253
+ }
254
+ );
255
+ }
256
+ async function sendQuestionRequest(params, spec, schema) {
257
+ const resolved = resolveRequest(params);
258
+ const bodyBytes = spec.body === void 0 ? void 0 : new TextEncoder().encode(JSON.stringify(spec.body));
259
+ let session = await resolveSession(params, resolved, false);
260
+ let response = await sendOnce(params, resolved, session, spec, bodyBytes);
261
+ if (response.status === 401) {
262
+ session = await resolveSession(params, resolved, true);
263
+ response = await sendOnce(params, resolved, session, spec, bodyBytes);
264
+ }
265
+ if (!response.ok) {
266
+ throw await questionErrorFromResponse(response);
267
+ }
268
+ let body;
269
+ try {
270
+ body = await response.json();
271
+ } catch (err) {
272
+ throw new import_errors.DerivativeQuestionRejectedError(
273
+ `${spec.label} response is not JSON`,
274
+ response.status,
275
+ null,
276
+ { cause: (0, import_write_request.errorMessage)(err) }
277
+ );
278
+ }
279
+ const parsed = schema.safeParse(body);
280
+ if (!parsed.success) {
281
+ throw new import_errors.DerivativeQuestionRejectedError(
282
+ `${spec.label} response is not a derivative question answer`,
283
+ response.status,
284
+ null,
285
+ { issues: parsed.error.issues }
286
+ );
287
+ }
288
+ return parsed.data;
289
+ }
290
+ function assertQuestionId(questionId) {
291
+ if (typeof questionId !== "string" || questionId.length === 0) {
292
+ throw new import_errors.WriteRequestError("questionId is required");
293
+ }
294
+ return `${DERIVATIVE_QUESTIONS_PATH}/${encodeURIComponent(questionId)}`;
295
+ }
296
+ function registrationBody(params) {
297
+ const { derivedScope, question } = params;
298
+ if (typeof derivedScope !== "string" || derivedScope.length === 0) {
299
+ throw new import_errors.WriteRequestError("derivedScope is required");
300
+ }
301
+ if (!Array.isArray(params.sourceScopes) || params.sourceScopes.length === 0) {
302
+ throw new import_errors.WriteRequestError(
303
+ "sourceScopes must be a non-empty array of scopes"
304
+ );
305
+ }
306
+ if (params.sourceScopes.length > MAX_QUESTION_SOURCE_SCOPES) {
307
+ throw new import_errors.WriteRequestError(
308
+ `sourceScopes lists ${params.sourceScopes.length} scopes; the maximum is ${MAX_QUESTION_SOURCE_SCOPES}`,
309
+ { max: MAX_QUESTION_SOURCE_SCOPES, count: params.sourceScopes.length }
310
+ );
311
+ }
312
+ const sourceScopes = [];
313
+ for (const scope of params.sourceScopes) {
314
+ if (typeof scope !== "string" || scope.length === 0) {
315
+ throw new import_errors.WriteRequestError("sourceScopes entries must be scope strings");
316
+ }
317
+ if (sourceScopes.includes(scope)) {
318
+ throw new import_errors.WriteRequestError("sourceScopes must not repeat a scope", {
319
+ duplicate: scope
320
+ });
321
+ }
322
+ if (scope === derivedScope) {
323
+ throw new import_errors.WriteRequestError(
324
+ "derivedScope cannot be one of its own sources",
325
+ { scope }
326
+ );
327
+ }
328
+ sourceScopes.push(scope);
329
+ }
330
+ if (typeof question !== "string" || question.trim() === "") {
331
+ throw new import_errors.WriteRequestError("question must be a non-empty string");
332
+ }
333
+ if (question.length > MAX_QUESTION_CHARS) {
334
+ throw new import_errors.WriteRequestError(
335
+ `question is ${question.length} characters; the maximum is ${MAX_QUESTION_CHARS}`,
336
+ { max: MAX_QUESTION_CHARS, length: question.length }
337
+ );
338
+ }
339
+ if (params.model !== void 0) {
340
+ if (typeof params.model !== "string" || params.model.length > MAX_QUESTION_MODEL_CHARS || !MODEL_ID_PATTERN.test(params.model)) {
341
+ throw new import_errors.WriteRequestError("model must be a provider model id", {
342
+ model: params.model
343
+ });
344
+ }
345
+ }
346
+ (0, import_lineage.assertDerivedScopeNaming)(derivedScope, sourceScopes);
347
+ return {
348
+ derivedScope,
349
+ sourceScopes,
350
+ question,
351
+ ...params.model === void 0 ? {} : { model: params.model }
352
+ };
353
+ }
354
+ async function registerQuestion(params) {
355
+ const body = registrationBody(params);
356
+ return sendQuestionRequest(
357
+ params,
358
+ {
359
+ method: "POST",
360
+ path: DERIVATIVE_QUESTIONS_PATH,
361
+ body,
362
+ label: "Register derivative question"
363
+ },
364
+ DerivativeQuestionSchema
365
+ );
366
+ }
367
+ async function getQuestion(params) {
368
+ const path = assertQuestionId(params.questionId);
369
+ return sendQuestionRequest(
370
+ params,
371
+ { method: "GET", path, label: "Read derivative question" },
372
+ DerivativeQuestionSchema
373
+ );
374
+ }
375
+ async function listQuestions(params) {
376
+ if (typeof params.derivedScope !== "string" || params.derivedScope.length === 0) {
377
+ throw new import_errors.WriteRequestError(
378
+ "derivedScope is required; a builder may only list its own questions on a scope it may write"
379
+ );
380
+ }
381
+ const { questions } = await sendQuestionRequest(
382
+ params,
383
+ {
384
+ method: "GET",
385
+ path: DERIVATIVE_QUESTIONS_PATH,
386
+ query: `?derivedScope=${encodeURIComponent(params.derivedScope)}`,
387
+ label: "List derivative questions"
388
+ },
389
+ QuestionListSchema
390
+ );
391
+ return questions;
392
+ }
393
+ async function recomputeQuestion(params) {
394
+ const path = `${assertQuestionId(params.questionId)}/recompute`;
395
+ return sendQuestionRequest(
396
+ params,
397
+ { method: "POST", path, label: "Recompute derivative question" },
398
+ QuestionRecomputeResultSchema
399
+ );
400
+ }
401
+ async function deleteQuestion(params) {
402
+ const path = assertQuestionId(params.questionId);
403
+ return sendQuestionRequest(
404
+ params,
405
+ { method: "DELETE", path, label: "Delete derivative question" },
406
+ QuestionDeleteResultSchema
407
+ );
408
+ }
409
+ function isSettled(status) {
410
+ return status === "ready" || status === "failed";
411
+ }
412
+ function abortError(signal) {
413
+ const reason = signal.reason;
414
+ if (reason instanceof Error) return reason;
415
+ const error = new Error("The operation was aborted");
416
+ error.name = "AbortError";
417
+ return error;
418
+ }
419
+ async function waitForQuestion(params) {
420
+ const timeoutMs = Math.max(
421
+ 0,
422
+ params.timeoutMs ?? DEFAULT_QUESTION_TIMEOUT_MS
423
+ );
424
+ const pollIntervalMs = Math.max(
425
+ 0,
426
+ params.pollIntervalMs ?? DEFAULT_QUESTION_POLL_INTERVAL_MS
427
+ );
428
+ const deadline = Date.now() + timeoutMs;
429
+ for (; ; ) {
430
+ if (params.signal?.aborted) throw abortError(params.signal);
431
+ const latest = await getQuestion(params);
432
+ if (isSettled(latest.status)) return latest;
433
+ const remaining = deadline - Date.now();
434
+ if (remaining <= 0) {
435
+ throw new import_errors.DerivativeQuestionTimeoutError(
436
+ `Derivative question ${latest.questionId} was still ${latest.status} after ${timeoutMs}ms`,
437
+ {
438
+ questionId: latest.questionId,
439
+ derivedScope: latest.derivedScope,
440
+ status: latest.status,
441
+ timeoutMs
442
+ }
443
+ );
444
+ }
445
+ await (0, import_write_request.sleep)(Math.min(pollIntervalMs, remaining));
446
+ }
447
+ }
448
+ async function askPersonalServer(params) {
449
+ const registered = await registerQuestion(params);
450
+ const registration = await waitForQuestion({
451
+ ...params,
452
+ questionId: registered.questionId
453
+ });
454
+ if (registration.status !== "ready") {
455
+ throw new import_errors.DerivativeQuestionFailedError(
456
+ `Derivative question ${registration.questionId} failed: ${registration.error ?? "no reason given"}`,
457
+ {
458
+ questionId: registration.questionId,
459
+ derivedScope: registration.derivedScope,
460
+ error: registration.error
461
+ }
462
+ );
463
+ }
464
+ const signer = (0, import_write_signer.resolveWriteSigner)(params.signer, {
465
+ account: params.account
466
+ });
467
+ const readParams = {
468
+ personalServerUrl: (0, import_write_request.normalizeBaseUrl)(params.personalServerUrl),
469
+ scope: params.derivedScope,
470
+ grantId: params.grantId,
471
+ signMessage: signer.signMessage,
472
+ ...params.audience === void 0 ? {} : { audience: params.audience },
473
+ ...params.headers === void 0 ? {} : { headers: params.headers },
474
+ ...params.fetch === void 0 ? {} : { fetch: params.fetch }
475
+ };
476
+ const record = await (0, import_personal_server_data.readPersonalServerData)(readParams);
477
+ return { registration, record };
478
+ }
479
+ // Annotate the CommonJS export names for ESM import in node:
480
+ 0 && (module.exports = {
481
+ DEFAULT_QUESTION_POLL_INTERVAL_MS,
482
+ DEFAULT_QUESTION_TIMEOUT_MS,
483
+ DERIVATIVE_QUESTIONS_PATH,
484
+ DerivativeQuestionSchema,
485
+ MAX_QUESTION_CHARS,
486
+ MAX_QUESTION_MODEL_CHARS,
487
+ MAX_QUESTION_SOURCE_SCOPES,
488
+ QUESTION_STATUSES,
489
+ QuestionDeleteResultSchema,
490
+ QuestionRecomputeResultSchema,
491
+ QuestionRegisteredBySchema,
492
+ QuestionStatusSchema,
493
+ askPersonalServer,
494
+ deleteQuestion,
495
+ getQuestion,
496
+ listQuestions,
497
+ recomputeQuestion,
498
+ registerQuestion,
499
+ waitForQuestion
500
+ });
501
+ //# sourceMappingURL=derivative-questions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/protocol/derivative-questions.ts"],"sourcesContent":["/**\n * Builder-side client for the Personal Server derivative question API.\n *\n * @remarks\n * A question is a standing prompt over the owner's source scopes. The\n * Personal Server answers it locally (the raw sources never leave the\n * machine except through its inference call) and writes the answer into the\n * derived scope as an ordinary derivative record, with lineage pointing at\n * the sources. The builder then reads the derived scope with its normal read\n * grant. Every source change re-runs the question, so a builder registers it\n * once and keeps reading a scope that stays up to date.\n *\n * One grant carries the whole pipeline, and it needs all three of:\n *\n * - a bare read entry for every source scope (the answer exposes them, so\n * the server refuses the registration otherwise:\n * `DERIVATIVE_SOURCE_NOT_GRANTED`),\n * - a bare read entry for the derived scope (to read the answer back),\n * - `write:<derivedScope>` (the credential the question routes authorize\n * against).\n *\n * Authentication is the Write API's, with no new credential: the write\n * session bearer from {@link openWriteSession} plus a fresh, single-use\n * `X-Vana-Write-Signature` Web3Signed proof over every request, carrying the\n * grant id as a signed claim. These helpers own that: they open one session\n * per `{ signer, Personal Server, grant }`, reuse it across calls, sign a new\n * proof per request, and re-open the session once when a call comes back 401\n * (the Personal Server keeps sessions in memory and forgets them when it\n * restarts).\n *\n * @category Protocol\n */\n\nimport { z } from \"zod\";\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport {\n DerivativeComputeUnavailableError,\n DerivativeCycleError,\n DerivativeQuestionFailedError,\n DerivativeQuestionInvalidError,\n DerivativeQuestionNotFoundError,\n DerivativeQuestionRejectedError,\n DerivativeQuestionTimeoutError,\n DerivativeSourceNotGrantedError,\n WriteConflictError,\n WriteForbiddenError,\n WriteRequestError,\n WriteUnauthorizedError,\n type PersonalServerWriteError,\n} from \"../errors\";\nimport { assertDerivedScopeNaming } from \"./lineage\";\nimport { readPersonalServerErrorBody } from \"./personal-server-error-body\";\nimport {\n readPersonalServerData,\n type ReadPersonalServerDataParams,\n} from \"./personal-server-data\";\nimport type { DataFileEnvelope } from \"./data-file\";\nimport {\n openWriteSession,\n WRITE_SIGNATURE_HEADER,\n type WriteSession,\n} from \"./personal-server-write\";\nimport {\n errorMessage,\n normalizeBaseUrl,\n proofKeyFor,\n resolveFetch,\n sendWithFreshProof,\n sleep,\n type WriteTransportRetryOptions,\n} from \"./write-request\";\nimport {\n resolveWriteSigner,\n type ResolveWriteSignerOptions,\n type WriteSignerSource,\n} from \"./write-signer\";\n\n/** Path the question routes are mounted at. */\nexport const DERIVATIVE_QUESTIONS_PATH = \"/v1/derivatives/questions\";\n/** The most source scopes one question may read. */\nexport const MAX_QUESTION_SOURCE_SCOPES = 16;\n/** The longest question text the Personal Server accepts. */\nexport const MAX_QUESTION_CHARS = 8_000;\n/** The longest model id the Personal Server accepts. */\nexport const MAX_QUESTION_MODEL_CHARS = 128;\n/** Model ids as providers spell them (`z-ai/glm-5.2`, `gpt-4o-mini`, ...). */\nconst MODEL_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/-]*$/;\n/** How long {@link waitForQuestion} polls before giving up. */\nexport const DEFAULT_QUESTION_TIMEOUT_MS = 120_000;\n/** How long {@link waitForQuestion} waits between polls. */\nexport const DEFAULT_QUESTION_POLL_INTERVAL_MS = 2_000;\n/** Re-open a session this long before its token expires. */\nconst SESSION_REFRESH_SKEW_MS = 30_000;\n\n/** Every state a question can be in. */\nexport const QUESTION_STATUSES = [\n \"pending\",\n \"ready\",\n \"failed\",\n \"stale\",\n] as const;\n\n/**\n * `pending` (never computed) -> `ready` | `failed`; a source change or an\n * explicit recompute puts a computed question back to `stale`, which\n * settles as `ready` or `failed` again.\n */\nexport const QuestionStatusSchema = z.enum(QUESTION_STATUSES);\n\n/** @see {@link QuestionStatusSchema} */\nexport type QuestionStatus = z.infer<typeof QuestionStatusSchema>;\n\n/** Who registered the question: the owner, or a builder under a grant. */\nexport const QuestionRegisteredBySchema = z.union([\n z.object({ kind: z.literal(\"owner\") }),\n z.object({\n kind: z.literal(\"builder\"),\n builder: z.string(),\n grantId: z.string(),\n }),\n]);\n\n/** @see {@link QuestionRegisteredBySchema} */\nexport type QuestionRegisteredBy = z.infer<typeof QuestionRegisteredBySchema>;\n\n// The server always sends these; `nullish` keeps a Personal Server that\n// omits one readable rather than failing the whole call on a missing field.\nconst nullableString = z\n .string()\n .nullish()\n .transform((value) => value ?? null);\n\n/**\n * A question registration as the Personal Server reports it (the answer of\n * register, get and list).\n */\nexport const DerivativeQuestionSchema = z.object({\n questionId: z.string().min(1),\n derivedScope: z.string().min(1),\n sourceScopes: z.array(z.string()),\n question: z.string(),\n /** The model override, or `null` for the server's default. */\n model: nullableString,\n registeredBy: QuestionRegisteredBySchema,\n status: QuestionStatusSchema,\n /** A short reason, set only while `status` is `failed`. */\n error: nullableString,\n createdAt: z.string(),\n updatedAt: nullableString,\n /** When the last compute finished, or `null` while `pending`. */\n lastComputedAt: nullableString,\n /** Local version of the derived record the last compute wrote. */\n derivedVersion: z\n .number()\n .nullish()\n .transform((value) => value ?? null),\n derivedCollectedAt: nullableString,\n});\n\n/** @see {@link DerivativeQuestionSchema} */\nexport type DerivativeQuestion = z.infer<typeof DerivativeQuestionSchema>;\n\nconst QuestionListSchema = z.object({\n questions: z.array(DerivativeQuestionSchema),\n});\n\n/** The 202 answer of a recompute request. */\nexport const QuestionRecomputeResultSchema = z.object({\n questionId: z.string().min(1),\n derivedScope: z.string().min(1),\n /** `pending` when the question was never computed, else `stale`. */\n status: QuestionStatusSchema,\n});\n\n/** @see {@link QuestionRecomputeResultSchema} */\nexport type QuestionRecomputeResult = z.infer<\n typeof QuestionRecomputeResultSchema\n>;\n\n/** The answer of a delete request. */\nexport const QuestionDeleteResultSchema = z.object({\n questionId: z.string().min(1),\n deleted: z.literal(true),\n});\n\n/** @see {@link QuestionDeleteResultSchema} */\nexport type QuestionDeleteResult = z.infer<typeof QuestionDeleteResultSchema>;\n\n/**\n * Connection, credential and transport shared by every question call.\n *\n * @remarks\n * The write session is opened on demand and reused for every later call\n * made with the same `signer` object, Personal Server, audience, grant and\n * `fetch`; a 401 re-opens it once and replays the call.\n */\nexport interface DerivativeQuestionAuthParams extends ResolveWriteSignerOptions {\n /** Personal Server origin, e.g. `https://ps.example.com`. */\n personalServerUrl: string;\n /** Builder key: a viem `LocalAccount`, `WalletClient`, or `{ signMessage }`. */\n signer: WriteSignerSource;\n /**\n * The grant the call runs under. It must carry `write:<derivedScope>`, a\n * bare read entry for the derived scope, and a bare read entry for every\n * source scope.\n */\n grantId: string;\n /** Web3Signed audience; defaults to `personalServerUrl`. */\n audience?: string;\n /** `fetch` to use; defaults to `globalThis.fetch`. */\n fetch?: typeof fetch;\n /** Extra request headers. */\n headers?: HeadersInit;\n retry?: WriteTransportRetryOptions;\n /** Aborts the request (and, for {@link waitForQuestion}, the polling). */\n signal?: AbortSignal;\n}\n\nexport interface RegisterQuestionParams extends DerivativeQuestionAuthParams {\n /**\n * The scope the answer is written into. Must not share its first\n * dot-segment with any source scope, so put derivatives in the app's own\n * namespace.\n */\n derivedScope: string;\n /**\n * The scopes the question reads: 1 to\n * {@link MAX_QUESTION_SOURCE_SCOPES} distinct scopes, none of them the\n * derived scope. They do not have to hold data yet: the question computes\n * once they do.\n */\n sourceScopes: readonly string[];\n /** The prompt, 1 to {@link MAX_QUESTION_CHARS} characters. */\n question: string;\n /** Model id override; omitted = the Personal Server's default model. */\n model?: string;\n}\n\nexport interface GetQuestionParams extends DerivativeQuestionAuthParams {\n questionId: string;\n}\n\nexport interface ListQuestionsParams extends DerivativeQuestionAuthParams {\n /**\n * The derived scope to list. A builder must name one (it may only see its\n * own questions on a scope it may write); the unfiltered list is the\n * owner's.\n */\n derivedScope: string;\n}\n\nexport interface RecomputeQuestionParams extends DerivativeQuestionAuthParams {\n questionId: string;\n}\n\nexport interface DeleteQuestionParams extends DerivativeQuestionAuthParams {\n questionId: string;\n}\n\nexport interface WaitForQuestionParams extends DerivativeQuestionAuthParams {\n questionId: string;\n /** Give up after this long (default {@link DEFAULT_QUESTION_TIMEOUT_MS}). */\n timeoutMs?: number;\n /** Wait between polls (default {@link DEFAULT_QUESTION_POLL_INTERVAL_MS}). */\n pollIntervalMs?: number;\n}\n\nexport interface AskPersonalServerParams extends RegisterQuestionParams {\n timeoutMs?: number;\n pollIntervalMs?: number;\n}\n\n/** {@link askPersonalServer}'s answer. */\nexport interface AskPersonalServerResult {\n /** The settled registration (`status` is `ready`). */\n registration: DerivativeQuestion;\n /** The derived record the Personal Server wrote and the builder just read. */\n record: DataFileEnvelope;\n}\n\n/**\n * Open write sessions, keyed by the signer object so a session is never\n * shared between builder keys and nothing is retained once the caller drops\n * its signer.\n */\nconst sessionsBySigner = new WeakMap<object, Map<string, WriteSession>>();\n\nfunction sessionCacheKey(\n personalServerUrl: string,\n audience: string,\n grantId: string,\n fetchFn: typeof fetch,\n): string {\n // The token is only valid on the server that minted it, and `fetch` is\n // what decides which server that is (a test double, a proxy, the global).\n return (\n JSON.stringify([personalServerUrl, audience, grantId]) + fetchIdOf(fetchFn)\n );\n}\n\nconst fetchIds = new WeakMap<object, number>();\nlet nextFetchId = 0;\n\nfunction fetchIdOf(fetchFn: typeof fetch): string {\n let id = fetchIds.get(fetchFn);\n if (id === undefined) {\n id = ++nextFetchId;\n fetchIds.set(fetchFn, id);\n }\n return `#${id}`;\n}\n\ninterface ResolvedQuestionRequest {\n baseUrl: string;\n audience: string;\n fetchFn: typeof fetch;\n cacheKey: string;\n signerKey: object;\n}\n\nfunction resolveRequest(\n params: DerivativeQuestionAuthParams,\n): ResolvedQuestionRequest {\n if (\n typeof params.personalServerUrl !== \"string\" ||\n params.personalServerUrl.length === 0\n ) {\n throw new WriteRequestError(\"personalServerUrl is required\");\n }\n // Checked before anything is signed or sent: without a grant the Personal\n // Server has nothing to authorize the call against.\n if (typeof params.grantId !== \"string\" || params.grantId.length === 0) {\n throw new WriteRequestError(\n \"grantId is required; a question call runs under the grant carrying write:<derivedScope>\",\n );\n }\n if (params.signer === null || typeof params.signer !== \"object\") {\n throw new WriteRequestError(\n \"signer must be a viem LocalAccount, a viem WalletClient, or a { signMessage } object\",\n );\n }\n const fetchFn = resolveFetch(params.fetch);\n const baseUrl = normalizeBaseUrl(params.personalServerUrl);\n const audience = params.audience ?? baseUrl;\n return {\n baseUrl,\n audience,\n fetchFn,\n cacheKey: sessionCacheKey(baseUrl, audience, params.grantId, fetchFn),\n signerKey: params.signer,\n };\n}\n\n/**\n * The session to use: the cached one while it is comfortably live, else a\n * fresh handshake. `force` drops the cached one first (the 401 path).\n */\nasync function resolveSession(\n params: DerivativeQuestionAuthParams,\n resolved: ResolvedQuestionRequest,\n force: boolean,\n): Promise<WriteSession> {\n let cache = sessionsBySigner.get(resolved.signerKey);\n if (cache === undefined) {\n cache = new Map();\n sessionsBySigner.set(resolved.signerKey, cache);\n }\n const cached = cache.get(resolved.cacheKey);\n if (\n !force &&\n cached !== undefined &&\n cached.expiresAt > Date.now() + SESSION_REFRESH_SKEW_MS\n ) {\n return cached;\n }\n if (force) cache.delete(resolved.cacheKey);\n const session = await openWriteSession({\n personalServerUrl: resolved.baseUrl,\n signer: params.signer,\n grantId: params.grantId,\n account: params.account,\n audience: resolved.audience,\n fetch: resolved.fetchFn,\n headers: params.headers,\n retry: params.retry,\n });\n cache.set(resolved.cacheKey, session);\n return session;\n}\n\n/** Map a non-2xx question answer onto the SDK's typed errors. */\nasync function questionErrorFromResponse(\n response: Response,\n): Promise<PersonalServerWriteError> {\n const { errorCode, message, details } =\n await readPersonalServerErrorBody(response);\n const text =\n message ??\n `Derivative question request failed: ${response.status} ${response.statusText}`;\n switch (errorCode) {\n case \"DERIVATIVE_SOURCE_NOT_GRANTED\":\n return new DerivativeSourceNotGrantedError(text, errorCode, details);\n case \"DERIVATIVE_CYCLE\":\n return new DerivativeCycleError(text, errorCode, details);\n case \"DERIVATIVE_COMPUTE_UNAVAILABLE\":\n return new DerivativeComputeUnavailableError(text, errorCode, details);\n case \"DERIVATIVE_QUESTION_INVALID\":\n case \"LINEAGE_SCOPE_UNDER_SOURCE_PREFIX\":\n return new DerivativeQuestionInvalidError(\n text,\n response.status,\n errorCode,\n details,\n );\n case \"DERIVATIVE_QUESTION_NOT_FOUND\":\n return new DerivativeQuestionNotFoundError(text, errorCode, details);\n default:\n break;\n }\n switch (response.status) {\n case 401:\n return new WriteUnauthorizedError(text, errorCode, details);\n case 403:\n return new WriteForbiddenError(text, errorCode, details);\n case 404:\n return new DerivativeQuestionNotFoundError(text, errorCode, details);\n case 409:\n return new WriteConflictError(text, errorCode, details);\n default:\n return new DerivativeQuestionRejectedError(\n text,\n response.status,\n errorCode,\n details,\n );\n }\n}\n\ninterface QuestionRequestSpec {\n method: \"GET\" | \"POST\" | \"DELETE\";\n /** Path without the query string: the proof only covers this. */\n path: string;\n /** Query string including the leading `?`, or `\"\"`. */\n query?: string;\n /** JSON body; sent (and signed) as compact JSON. */\n body?: Record<string, unknown>;\n label: string;\n}\n\nasync function sendOnce(\n params: DerivativeQuestionAuthParams,\n resolved: ResolvedQuestionRequest,\n session: WriteSession,\n spec: QuestionRequestSpec,\n bodyBytes: Uint8Array | undefined,\n): Promise<Response> {\n return sendWithFreshProof(\n spec.label,\n resolved.fetchFn,\n params.retry,\n proofKeyFor({\n aud: session.audience,\n method: spec.method,\n uri: spec.path,\n grantId: session.grantId,\n signedBytes: bodyBytes,\n }),\n async (iat) => {\n const headers = new Headers(params.headers);\n headers.set(\"Accept\", \"application/json\");\n headers.set(\"Authorization\", `Bearer ${session.accessToken}`);\n if (bodyBytes !== undefined) {\n headers.set(\"Content-Type\", \"application/json\");\n }\n headers.set(\n WRITE_SIGNATURE_HEADER,\n await buildWeb3SignedHeader({\n signMessage: session.signer.signMessage,\n aud: session.audience,\n // The Personal Server verifies the proof against the request's\n // path only, so the signed `uri` must not carry the query string.\n uri: spec.path,\n method: spec.method,\n body: bodyBytes,\n grantId: session.grantId,\n iat,\n }),\n );\n return {\n url: `${resolved.baseUrl}${spec.path}${spec.query ?? \"\"}`,\n init: {\n method: spec.method,\n headers,\n ...(bodyBytes === undefined\n ? {}\n : { body: bodyBytes as unknown as BodyInit }),\n ...(params.signal ? { signal: params.signal } : {}),\n },\n };\n },\n );\n}\n\n/**\n * Run one question call under a reused write session: fresh proof, and one\n * re-handshake when the Personal Server no longer knows the session.\n */\nasync function sendQuestionRequest<T>(\n params: DerivativeQuestionAuthParams,\n spec: QuestionRequestSpec,\n schema: z.ZodType<T>,\n): Promise<T> {\n const resolved = resolveRequest(params);\n // Compact JSON is the contract: the server re-serializes what it parsed\n // and refuses anything else with WRITE_BODY_NOT_CANONICAL.\n const bodyBytes =\n spec.body === undefined\n ? undefined\n : new TextEncoder().encode(JSON.stringify(spec.body));\n\n let session = await resolveSession(params, resolved, false);\n let response = await sendOnce(params, resolved, session, spec, bodyBytes);\n if (response.status === 401) {\n // The Personal Server keeps write sessions in memory: a restart (or an\n // expiry the client did not see) invalidates the bearer, not the grant.\n // Open a new session once and replay the call with a fresh proof.\n session = await resolveSession(params, resolved, true);\n response = await sendOnce(params, resolved, session, spec, bodyBytes);\n }\n\n if (!response.ok) {\n throw await questionErrorFromResponse(response);\n }\n let body: unknown;\n try {\n body = await response.json();\n } catch (err) {\n throw new DerivativeQuestionRejectedError(\n `${spec.label} response is not JSON`,\n response.status,\n null,\n { cause: errorMessage(err) },\n );\n }\n const parsed = schema.safeParse(body);\n if (!parsed.success) {\n throw new DerivativeQuestionRejectedError(\n `${spec.label} response is not a derivative question answer`,\n response.status,\n null,\n { issues: parsed.error.issues },\n );\n }\n return parsed.data;\n}\n\nfunction assertQuestionId(questionId: string): string {\n if (typeof questionId !== \"string\" || questionId.length === 0) {\n throw new WriteRequestError(\"questionId is required\");\n }\n return `${DERIVATIVE_QUESTIONS_PATH}/${encodeURIComponent(questionId)}`;\n}\n\n/**\n * Validate a registration the way the Personal Server does, so a builder\n * gets a typed error before a proof is signed rather than a 400 after.\n */\nfunction registrationBody(params: RegisterQuestionParams): {\n derivedScope: string;\n sourceScopes: string[];\n question: string;\n model?: string;\n} {\n const { derivedScope, question } = params;\n if (typeof derivedScope !== \"string\" || derivedScope.length === 0) {\n throw new WriteRequestError(\"derivedScope is required\");\n }\n if (!Array.isArray(params.sourceScopes) || params.sourceScopes.length === 0) {\n throw new WriteRequestError(\n \"sourceScopes must be a non-empty array of scopes\",\n );\n }\n if (params.sourceScopes.length > MAX_QUESTION_SOURCE_SCOPES) {\n throw new WriteRequestError(\n `sourceScopes lists ${params.sourceScopes.length} scopes; the maximum is ${MAX_QUESTION_SOURCE_SCOPES}`,\n { max: MAX_QUESTION_SOURCE_SCOPES, count: params.sourceScopes.length },\n );\n }\n const sourceScopes: string[] = [];\n for (const scope of params.sourceScopes) {\n if (typeof scope !== \"string\" || scope.length === 0) {\n throw new WriteRequestError(\"sourceScopes entries must be scope strings\");\n }\n if (sourceScopes.includes(scope)) {\n throw new WriteRequestError(\"sourceScopes must not repeat a scope\", {\n duplicate: scope,\n });\n }\n if (scope === derivedScope) {\n throw new WriteRequestError(\n \"derivedScope cannot be one of its own sources\",\n { scope },\n );\n }\n sourceScopes.push(scope);\n }\n if (typeof question !== \"string\" || question.trim() === \"\") {\n throw new WriteRequestError(\"question must be a non-empty string\");\n }\n if (question.length > MAX_QUESTION_CHARS) {\n throw new WriteRequestError(\n `question is ${question.length} characters; the maximum is ${MAX_QUESTION_CHARS}`,\n { max: MAX_QUESTION_CHARS, length: question.length },\n );\n }\n if (params.model !== undefined) {\n if (\n typeof params.model !== \"string\" ||\n params.model.length > MAX_QUESTION_MODEL_CHARS ||\n !MODEL_ID_PATTERN.test(params.model)\n ) {\n throw new WriteRequestError(\"model must be a provider model id\", {\n model: params.model,\n });\n }\n }\n // The lineage naming rule, applied before signing: the server would refuse\n // the registration with LINEAGE_SCOPE_UNDER_SOURCE_PREFIX.\n assertDerivedScopeNaming(derivedScope, sourceScopes);\n return {\n derivedScope,\n sourceScopes,\n question,\n ...(params.model === undefined ? {} : { model: params.model }),\n };\n}\n\n/**\n * Register a standing question over the owner's source scopes.\n *\n * @remarks\n * Sends `POST /v1/derivatives/questions`. The registration comes back\n * `pending` and the first compute is scheduled immediately; poll it with\n * {@link waitForQuestion}, then read `derivedScope`.\n *\n * @example\n * ```typescript\n * const registered = await registerQuestion({\n * personalServerUrl: \"https://ps.example.com\",\n * signer,\n * grantId,\n * derivedScope: \"coach.weekly\",\n * sourceScopes: [\"oura.sleep\", \"chatgpt.conversations\"],\n * question: \"How did my sleep relate to my mood this week?\",\n * });\n * ```\n * @returns The registration, `status: \"pending\"`.\n * @throws {WriteRequestError} Before sending: a missing grant, a bad scope\n * list, an over-long question, a derived scope under a source's namespace.\n * @throws {DerivativeSourceNotGrantedError} 403: a source scope is not\n * read-granted to the builder (`details.scopes`).\n * @throws {DerivativeCycleError} 409: the question would make the derived\n * scope a transitive source of itself.\n * @throws {DerivativeQuestionInvalidError} 400 from the server.\n * @throws {DerivativeComputeUnavailableError} 503: no compute layer.\n * @throws {WriteForbiddenError} 403: the grant does not authorize writing\n * the derived scope.\n */\nexport async function registerQuestion(\n params: RegisterQuestionParams,\n): Promise<DerivativeQuestion> {\n const body = registrationBody(params);\n return sendQuestionRequest(\n params,\n {\n method: \"POST\",\n path: DERIVATIVE_QUESTIONS_PATH,\n body,\n label: \"Register derivative question\",\n },\n DerivativeQuestionSchema,\n );\n}\n\n/**\n * Read one question's current state.\n *\n * @remarks\n * Sends `GET /v1/derivatives/questions/:id`. A builder only sees questions\n * it registered itself; anything else is a 404.\n *\n * @returns The registration, including `status`, `lastComputedAt`,\n * `derivedVersion` and (when it failed) `error`.\n * @throws {DerivativeQuestionNotFoundError} 404: unknown id, or not this\n * builder's question.\n */\nexport async function getQuestion(\n params: GetQuestionParams,\n): Promise<DerivativeQuestion> {\n const path = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"GET\", path, label: \"Read derivative question\" },\n DerivativeQuestionSchema,\n );\n}\n\n/**\n * List the questions this builder registered on a derived scope.\n *\n * @remarks\n * Sends `GET /v1/derivatives/questions?derivedScope=...`. The scope is\n * required for a builder: it is what the call is authorized against. Note\n * that the query string is outside the signed proof, which covers the path.\n *\n * @returns The registrations, newest state included.\n */\nexport async function listQuestions(\n params: ListQuestionsParams,\n): Promise<DerivativeQuestion[]> {\n if (\n typeof params.derivedScope !== \"string\" ||\n params.derivedScope.length === 0\n ) {\n throw new WriteRequestError(\n \"derivedScope is required; a builder may only list its own questions on a scope it may write\",\n );\n }\n const { questions } = await sendQuestionRequest(\n params,\n {\n method: \"GET\",\n path: DERIVATIVE_QUESTIONS_PATH,\n query: `?derivedScope=${encodeURIComponent(params.derivedScope)}`,\n label: \"List derivative questions\",\n },\n QuestionListSchema,\n );\n return questions;\n}\n\n/**\n * Ask the Personal Server to recompute a question now.\n *\n * @remarks\n * Sends `POST /v1/derivatives/questions/:id/recompute`, which answers 202\n * and schedules the compute immediately instead of after the usual quiet\n * period. Use it to retry a `failed` question; a source change recomputes on\n * its own.\n *\n * @returns `{ questionId, derivedScope, status }` with the status the\n * question was put into (`pending` when it had never computed, else\n * `stale`).\n */\nexport async function recomputeQuestion(\n params: RecomputeQuestionParams,\n): Promise<QuestionRecomputeResult> {\n const path = `${assertQuestionId(params.questionId)}/recompute`;\n return sendQuestionRequest(\n params,\n { method: \"POST\", path, label: \"Recompute derivative question\" },\n QuestionRecomputeResultSchema,\n );\n}\n\n/**\n * Delete a question registration.\n *\n * @remarks\n * Sends `DELETE /v1/derivatives/questions/:id`. The question stops\n * recomputing; the derived records it already wrote are left alone (delete\n * those through the data-point deletion path).\n *\n * @returns `{ questionId, deleted: true }`.\n */\nexport async function deleteQuestion(\n params: DeleteQuestionParams,\n): Promise<QuestionDeleteResult> {\n const path = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"DELETE\", path, label: \"Delete derivative question\" },\n QuestionDeleteResultSchema,\n );\n}\n\n/** `true` once the question has settled: nothing more to wait for. */\nfunction isSettled(status: QuestionStatus): boolean {\n return status === \"ready\" || status === \"failed\";\n}\n\nfunction abortError(signal: AbortSignal): Error {\n const reason: unknown = signal.reason;\n if (reason instanceof Error) return reason;\n const error = new Error(\"The operation was aborted\");\n error.name = \"AbortError\";\n return error;\n}\n\n/**\n * Poll a question until it settles.\n *\n * @remarks\n * Calls {@link getQuestion} every `pollIntervalMs` until `status` is `ready`\n * or `failed` and returns that state; a `failed` question is returned, not\n * thrown, so the caller can read `error` and decide whether to\n * {@link recomputeQuestion}. All polls share the one write session and each\n * signs its own proof.\n *\n * @example\n * ```typescript\n * const settled = await waitForQuestion({\n * personalServerUrl,\n * signer,\n * grantId,\n * questionId: registered.questionId,\n * timeoutMs: 60_000,\n * });\n * if (settled.status === \"ready\") {\n * // read derivedScope\n * }\n * ```\n * @returns The settled registration (`ready` or `failed`).\n * @throws {DerivativeQuestionTimeoutError} The question had not settled\n * within `timeoutMs`; it keeps computing on the server.\n * @throws Whatever {@link getQuestion} throws, and the `signal`'s abort\n * reason when the caller aborts.\n */\nexport async function waitForQuestion(\n params: WaitForQuestionParams,\n): Promise<DerivativeQuestion> {\n const timeoutMs = Math.max(\n 0,\n params.timeoutMs ?? DEFAULT_QUESTION_TIMEOUT_MS,\n );\n const pollIntervalMs = Math.max(\n 0,\n params.pollIntervalMs ?? DEFAULT_QUESTION_POLL_INTERVAL_MS,\n );\n const deadline = Date.now() + timeoutMs;\n for (;;) {\n if (params.signal?.aborted) throw abortError(params.signal);\n const latest = await getQuestion(params);\n if (isSettled(latest.status)) return latest;\n const remaining = deadline - Date.now();\n if (remaining <= 0) {\n throw new DerivativeQuestionTimeoutError(\n `Derivative question ${latest.questionId} was still ${latest.status} after ${timeoutMs}ms`,\n {\n questionId: latest.questionId,\n derivedScope: latest.derivedScope,\n status: latest.status,\n timeoutMs,\n },\n );\n }\n await sleep(Math.min(pollIntervalMs, remaining));\n }\n}\n\n/**\n * Register a question, wait for it, and read the answer: the whole builder\n * loop in one call.\n *\n * @remarks\n * {@link registerQuestion} + {@link waitForQuestion} +\n * {@link readPersonalServerData} on the derived scope, which is why the\n * grant needs a bare read entry for `derivedScope` on top of\n * `write:<derivedScope>` and the source reads. The read is the plain\n * Web3Signed one; when the grant is priced, settle the 402 yourself with the\n * escrow-aware read from `@opendatalabs/vana-sdk/server` and use\n * {@link registerQuestion} and {@link waitForQuestion} directly.\n *\n * A question registered this way keeps recomputing after the call returns:\n * every later change to a source scope refreshes the derived record, and the\n * builder can read it again without registering anything.\n *\n * @example\n * ```typescript\n * const { registration, record } = await askPersonalServer({\n * personalServerUrl: \"https://ps.example.com\",\n * signer,\n * grantId,\n * derivedScope: \"coach.weekly\",\n * sourceScopes: [\"oura.sleep\"],\n * question: \"How did my sleep trend this week?\",\n * });\n * console.log(record.data.answer, registration.questionId);\n * ```\n * @returns The settled registration and the derived record.\n * @throws {DerivativeQuestionFailedError} The question settled as `failed`\n * (`details.error` is the server's reason).\n * @throws Everything {@link registerQuestion}, {@link waitForQuestion} and\n * the read path throw.\n */\nexport async function askPersonalServer(\n params: AskPersonalServerParams,\n): Promise<AskPersonalServerResult> {\n const registered = await registerQuestion(params);\n const registration = await waitForQuestion({\n ...params,\n questionId: registered.questionId,\n });\n if (registration.status !== \"ready\") {\n throw new DerivativeQuestionFailedError(\n `Derivative question ${registration.questionId} failed: ${registration.error ?? \"no reason given\"}`,\n {\n questionId: registration.questionId,\n derivedScope: registration.derivedScope,\n error: registration.error,\n },\n );\n }\n const signer = resolveWriteSigner(params.signer, {\n account: params.account,\n });\n const readParams: ReadPersonalServerDataParams = {\n personalServerUrl: normalizeBaseUrl(params.personalServerUrl),\n scope: params.derivedScope,\n grantId: params.grantId,\n signMessage: signer.signMessage,\n ...(params.audience === undefined ? {} : { audience: params.audience }),\n ...(params.headers === undefined ? {} : { headers: params.headers }),\n ...(params.fetch === undefined ? {} : { fetch: params.fetch }),\n };\n const record = await readPersonalServerData(readParams);\n return { registration, record };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiCA,iBAAkB;AAClB,iCAAsC;AACtC,oBAcO;AACP,qBAAyC;AACzC,wCAA4C;AAC5C,kCAGO;AAEP,mCAIO;AACP,2BAQO;AACP,0BAIO;AAGA,MAAM,4BAA4B;AAElC,MAAM,6BAA6B;AAEnC,MAAM,qBAAqB;AAE3B,MAAM,2BAA2B;AAExC,MAAM,mBAAmB;AAElB,MAAM,8BAA8B;AAEpC,MAAM,oCAAoC;AAEjD,MAAM,0BAA0B;AAGzB,MAAM,oBAAoB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAOO,MAAM,uBAAuB,aAAE,KAAK,iBAAiB;AAMrD,MAAM,6BAA6B,aAAE,MAAM;AAAA,EAChD,aAAE,OAAO,EAAE,MAAM,aAAE,QAAQ,OAAO,EAAE,CAAC;AAAA,EACrC,aAAE,OAAO;AAAA,IACP,MAAM,aAAE,QAAQ,SAAS;AAAA,IACzB,SAAS,aAAE,OAAO;AAAA,IAClB,SAAS,aAAE,OAAO;AAAA,EACpB,CAAC;AACH,CAAC;AAOD,MAAM,iBAAiB,aACpB,OAAO,EACP,QAAQ,EACR,UAAU,CAAC,UAAU,SAAS,IAAI;AAM9B,MAAM,2BAA2B,aAAE,OAAO;AAAA,EAC/C,YAAY,aAAE,OAAO,EAAE,IAAI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