@opendatalabs/vana-sdk 3.20.1 → 3.22.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.
- package/README.md +64 -0
- package/dist/direct/access-request-client.cjs +100 -2
- package/dist/direct/access-request-client.cjs.map +1 -1
- package/dist/direct/access-request-client.d.ts +23 -1
- package/dist/direct/access-request-client.js +98 -1
- package/dist/direct/access-request-client.js.map +1 -1
- package/dist/direct/controller.cjs +4 -0
- package/dist/direct/controller.cjs.map +1 -1
- package/dist/direct/controller.d.ts +10 -1
- package/dist/direct/controller.js +6 -1
- package/dist/direct/controller.js.map +1 -1
- package/dist/direct/types.cjs.map +1 -1
- package/dist/direct/types.d.ts +45 -0
- package/dist/direct/types.js.map +1 -1
- package/dist/index.browser.d.ts +2 -1
- package/dist/index.browser.js +186 -0
- package/dist/index.browser.js.map +4 -4
- package/dist/index.node.cjs +196 -0
- package/dist/index.node.cjs.map +4 -4
- package/dist/index.node.d.ts +2 -1
- package/dist/index.node.js +186 -0
- package/dist/index.node.js.map +4 -4
- package/dist/protocol/derivative-questions.cjs +22 -0
- package/dist/protocol/derivative-questions.cjs.map +1 -1
- package/dist/protocol/derivative-questions.d.ts +53 -0
- package/dist/protocol/derivative-questions.js +19 -0
- package/dist/protocol/derivative-questions.js.map +1 -1
- package/dist/protocol/derivative-status.cjs +209 -0
- package/dist/protocol/derivative-status.cjs.map +1 -0
- package/dist/protocol/derivative-status.d.ts +196 -0
- package/dist/protocol/derivative-status.js +190 -0
- package/dist/protocol/derivative-status.js.map +1 -0
- package/dist/protocol/derivative-status.test.d.ts +1 -0
- package/dist/server.cjs +4 -2
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +4 -2
- package/dist/server.js.map +1 -1
- package/dist/tests/mock-personal-server.d.ts +9 -0
- package/package.json +1 -1
|
@@ -1 +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 a\n * 401 the session is responsible for (the Personal Server keeps sessions in\n * memory and forgets them when it restarts; a 401 about the PROOF is\n * surfaced as it is, since a new session would not change it).\n *\n * Two rules govern the proof on these routes, and both are the server's\n * (`personal-server-ts` d91124d and later):\n *\n * - the signed `uri` claim covers the query string, not just the path,\n * because `?derivedScope=` is what the list route authorizes against;\n * - every call carries a fresh `nonce` claim, which becomes the server's\n * replay key. Without one the whole proof is the key, so two identical\n * polls signed inside the same second are refused as a replay.\n *\n * @category Protocol\n */\n\nimport { z } from \"zod\";\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport {\n DerivativeComputeUnavailableError,\n DerivativeCycleError,\n DerivativeDerivedScopeRequiredError,\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 {\n readPersonalServerErrorBody,\n type PersonalServerErrorBody,\n} 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 freshProofNonce,\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/**\n * The 202 answer of a recompute request: the same registration view every\n * other question route answers, so a client needs one schema.\n *\n * @remarks\n * Older Personal Servers answered only\n * `{ questionId, derivedScope, status }` here. The full view is a superset\n * of those three fields, so code reading them is unaffected, but the answer\n * of a server before `personal-server-ts` d91124d no longer parses.\n */\nexport const QuestionRecomputeResultSchema = DerivativeQuestionSchema;\n\n/** @see {@link QuestionRecomputeResultSchema} */\nexport type QuestionRecomputeResult = DerivativeQuestion;\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/**\n * The 401s a re-handshake cannot fix. They are failures of the per-request\n * PROOF, not of the session, so the same call signed under a brand new\n * session fails exactly the same way: replaying it would burn a second\n * proof, add a pointless handshake, and report the wrong problem. Every\n * other 401 is treated as the session the Personal Server forgot.\n */\nconst PROOF_FAILURE_CODES = new Set([\n \"WRITE_ATTRIBUTION_REQUIRED\",\n \"WRITE_ATTRIBUTION_INVALID\",\n \"WRITE_ATTRIBUTION_SIGNER_MISMATCH\",\n \"WRITE_ATTRIBUTION_GRANT_MISMATCH\",\n \"WRITE_ATTRIBUTION_REPLAY\",\n]);\n\nfunction isStaleSession(errorCode: string | null): boolean {\n return errorCode === null || !PROOF_FAILURE_CODES.has(errorCode);\n}\n\n/**\n * Map a non-2xx question answer onto the SDK's typed errors. `body` is the\n * already-read error body, for the caller that had to peek at it (a response\n * body can only be read once).\n */\nasync function questionErrorFromResponse(\n response: Response,\n body?: PersonalServerErrorBody,\n): Promise<PersonalServerWriteError> {\n const { errorCode, message, details } =\n body ?? (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 case \"DERIVATIVE_DERIVED_SCOPE_REQUIRED\":\n return new DerivativeDerivedScopeRequiredError(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 /**\n * The whole request target, path AND query string. The Personal Server\n * verifies the proof against it (the query decides the authorization on\n * the list route), so it is built once and used for both the signed `uri`\n * claim and the `fetch` URL, where the two cannot drift apart.\n */\n target: 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.target,\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 proof commits to the whole request target, query included:\n // `?derivedScope=` is what the list route authorizes against, and a\n // proof that did not cover it would authorize any other scope.\n uri: spec.target,\n method: spec.method,\n body: bodyBytes,\n grantId: session.grantId,\n // Fresh per attempt, so a retry after a thrown `fetch` is never the\n // proof the server may already have consumed, and so two identical\n // polls inside one second stay distinct.\n nonce: freshProofNonce(),\n iat,\n }),\n );\n return {\n url: `${resolved.baseUrl}${spec.target}`,\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 // Read once, kept for the throw below: a body cannot be read twice.\n let errorBody: PersonalServerErrorBody | undefined;\n if (response.status === 401) {\n errorBody = await readPersonalServerErrorBody(response);\n if (isStaleSession(errorBody.errorCode)) {\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 errorBody = undefined;\n }\n }\n\n if (!response.ok) {\n throw await questionErrorFromResponse(response, errorBody);\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\n/** The request target for one question id: no query, so the bare path. */\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 target: 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 target = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"GET\", target, 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, which\n * is exactly why the signed proof commits to the query string as well as the\n * path. The target is built once and used for both, so the signature and the\n * request can never name different scopes.\n *\n * @returns The registrations, newest state included.\n * @throws {DerivativeDerivedScopeRequiredError} 400\n * `DERIVATIVE_DERIVED_SCOPE_REQUIRED` when the server saw no\n * `?derivedScope=` (the SDK refuses an empty one before sending).\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 target = `${DERIVATIVE_QUESTIONS_PATH}?derivedScope=${encodeURIComponent(params.derivedScope)}`;\n const { questions } = await sendQuestionRequest(\n params,\n {\n method: \"GET\",\n target,\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 The full registration view, with the status the question was put\n * into (`pending` when it had never computed, else `stale`). Servers\n * before `personal-server-ts` d91124d answered only\n * `{ questionId, derivedScope, status }` here, which no longer parses.\n */\nexport async function recomputeQuestion(\n params: RecomputeQuestionParams,\n): Promise<QuestionRecomputeResult> {\n const target = `${assertQuestionId(params.questionId)}/recompute`;\n return sendQuestionRequest(\n params,\n { method: \"POST\", target, 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 target = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"DELETE\", target, 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":"AA2CA,SAAS,SAAS;AAClB,SAAS,6BAA6B;AACtC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP,SAAS,gCAAgC;AACzC;AAAA,EACE;AAAA,OAEK;AACP;AAAA,EACE;AAAA,OAEK;AAEP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,OAGK;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,EAAE,KAAK,iBAAiB;AAMrD,MAAM,6BAA6B,EAAE,MAAM;AAAA,EAChD,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,OAAO,EAAE,CAAC;AAAA,EACrC,EAAE,OAAO;AAAA,IACP,MAAM,EAAE,QAAQ,SAAS;AAAA,IACzB,SAAS,EAAE,OAAO;AAAA,IAClB,SAAS,EAAE,OAAO;AAAA,EACpB,CAAC;AACH,CAAC;AAOD,MAAM,iBAAiB,EACpB,OAAO,EACP,QAAQ,EACR,UAAU,CAAC,UAAU,SAAS,IAAI;AAM9B,MAAM,2BAA2B,EAAE,OAAO;AAAA,EAC/C,YAAY,EAAE,OAAO,EAAE,IAAI,CAAC;AAAA,EAC5B,cAAc,EAAE,OAAO,EAA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AAA,IACpB,GAAI,OAAO,aAAa,SAAY,CAAC,IAAI,EAAE,UAAU,OAAO,SAAS;AAAA,IACrE,GAAI,OAAO,YAAY,SAAY,CAAC,IAAI,EAAE,SAAS,OAAO,QAAQ;AAAA,IAClE,GAAI,OAAO,UAAU,SAAY,CAAC,IAAI,EAAE,OAAO,OAAO,MAAM;AAAA,EAC9D;AACA,QAAM,SAAS,MAAM,uBAAuB,UAAU;AACtD,SAAO,EAAE,cAAc,OAAO;AAChC;","names":[]}
|
|
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 a\n * 401 the session is responsible for (the Personal Server keeps sessions in\n * memory and forgets them when it restarts; a 401 about the PROOF is\n * surfaced as it is, since a new session would not change it).\n *\n * Two rules govern the proof on these routes, and both are the server's\n * (`personal-server-ts` d91124d and later):\n *\n * - the signed `uri` claim covers the query string, not just the path,\n * because `?derivedScope=` is what the list route authorizes against;\n * - every call carries a fresh `nonce` claim, which becomes the server's\n * replay key. Without one the whole proof is the key, so two identical\n * polls signed inside the same second are refused as a replay.\n *\n * @category Protocol\n */\n\nimport { z } from \"zod\";\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport {\n DerivativeComputeUnavailableError,\n DerivativeCycleError,\n DerivativeDerivedScopeRequiredError,\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 {\n readPersonalServerErrorBody,\n type PersonalServerErrorBody,\n} 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 freshProofNonce,\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 */\n/**\n * Every failure class a question can carry. A closed vocabulary, because the\n * status route serves it to a reader of the derived scope: it never carries a\n * scope name, the question or provider detail.\n *\n * - `inference_unavailable` — the provider or relay failed. The only\n * transient class: the Personal Server retries it on its own.\n * - `source_missing` — a source scope is deleted or holds no local data.\n * - `grant_invalid` — the registering builder's grant no longer covers what\n * the question reads.\n * - `internal` — anything else, including a permanent provider 4xx.\n */\nexport const DERIVATIVE_ERROR_CODES = [\n \"inference_unavailable\",\n \"source_missing\",\n \"grant_invalid\",\n \"internal\",\n] as const;\n\n/** @see {@link DERIVATIVE_ERROR_CODES} */\nexport const DerivativeErrorCodeSchema = z.enum(DERIVATIVE_ERROR_CODES);\n\n/** @see {@link DERIVATIVE_ERROR_CODES} */\nexport type DerivativeErrorCode = z.infer<typeof DerivativeErrorCodeSchema>;\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 /**\n * The coarse failure class behind `error`, set only while `status` is\n * `failed`. `null` from a Personal Server that predates the class\n * (`personal-server-ts` before the status route).\n */\n errorCode: DerivativeErrorCodeSchema.nullish().transform(\n (value) => value ?? null,\n ),\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/**\n * The 202 answer of a recompute request: the same registration view every\n * other question route answers, so a client needs one schema.\n *\n * @remarks\n * Older Personal Servers answered only\n * `{ questionId, derivedScope, status }` here. The full view is a superset\n * of those three fields, so code reading them is unaffected, but the answer\n * of a server before `personal-server-ts` d91124d no longer parses.\n */\nexport const QuestionRecomputeResultSchema = DerivativeQuestionSchema;\n\n/** @see {@link QuestionRecomputeResultSchema} */\nexport type QuestionRecomputeResult = DerivativeQuestion;\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/**\n * The 401s a re-handshake cannot fix. They are failures of the per-request\n * PROOF, not of the session, so the same call signed under a brand new\n * session fails exactly the same way: replaying it would burn a second\n * proof, add a pointless handshake, and report the wrong problem. Every\n * other 401 is treated as the session the Personal Server forgot.\n */\nconst PROOF_FAILURE_CODES = new Set([\n \"WRITE_ATTRIBUTION_REQUIRED\",\n \"WRITE_ATTRIBUTION_INVALID\",\n \"WRITE_ATTRIBUTION_SIGNER_MISMATCH\",\n \"WRITE_ATTRIBUTION_GRANT_MISMATCH\",\n \"WRITE_ATTRIBUTION_REPLAY\",\n]);\n\nfunction isStaleSession(errorCode: string | null): boolean {\n return errorCode === null || !PROOF_FAILURE_CODES.has(errorCode);\n}\n\n/**\n * Map a non-2xx question answer onto the SDK's typed errors. `body` is the\n * already-read error body, for the caller that had to peek at it (a response\n * body can only be read once).\n */\nasync function questionErrorFromResponse(\n response: Response,\n body?: PersonalServerErrorBody,\n): Promise<PersonalServerWriteError> {\n const { errorCode, message, details } =\n body ?? (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 case \"DERIVATIVE_DERIVED_SCOPE_REQUIRED\":\n return new DerivativeDerivedScopeRequiredError(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\n/**\n * The typed error for a non-2xx answer of any `/v1/derivatives` route.\n *\n * @remarks\n * Shared with the reader-facing status client so both surfaces map the same\n * `errorCode` to the same error class. Not part of the package's public API.\n *\n * @internal\n */\nexport const personalServerErrorFromQuestionResponse =\n questionErrorFromResponse;\n\ninterface QuestionRequestSpec {\n method: \"GET\" | \"POST\" | \"DELETE\";\n /**\n * The whole request target, path AND query string. The Personal Server\n * verifies the proof against it (the query decides the authorization on\n * the list route), so it is built once and used for both the signed `uri`\n * claim and the `fetch` URL, where the two cannot drift apart.\n */\n target: 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.target,\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 proof commits to the whole request target, query included:\n // `?derivedScope=` is what the list route authorizes against, and a\n // proof that did not cover it would authorize any other scope.\n uri: spec.target,\n method: spec.method,\n body: bodyBytes,\n grantId: session.grantId,\n // Fresh per attempt, so a retry after a thrown `fetch` is never the\n // proof the server may already have consumed, and so two identical\n // polls inside one second stay distinct.\n nonce: freshProofNonce(),\n iat,\n }),\n );\n return {\n url: `${resolved.baseUrl}${spec.target}`,\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 // Read once, kept for the throw below: a body cannot be read twice.\n let errorBody: PersonalServerErrorBody | undefined;\n if (response.status === 401) {\n errorBody = await readPersonalServerErrorBody(response);\n if (isStaleSession(errorBody.errorCode)) {\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 errorBody = undefined;\n }\n }\n\n if (!response.ok) {\n throw await questionErrorFromResponse(response, errorBody);\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\n/** The request target for one question id: no query, so the bare path. */\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 target: 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 target = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"GET\", target, 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, which\n * is exactly why the signed proof commits to the query string as well as the\n * path. The target is built once and used for both, so the signature and the\n * request can never name different scopes.\n *\n * @returns The registrations, newest state included.\n * @throws {DerivativeDerivedScopeRequiredError} 400\n * `DERIVATIVE_DERIVED_SCOPE_REQUIRED` when the server saw no\n * `?derivedScope=` (the SDK refuses an empty one before sending).\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 target = `${DERIVATIVE_QUESTIONS_PATH}?derivedScope=${encodeURIComponent(params.derivedScope)}`;\n const { questions } = await sendQuestionRequest(\n params,\n {\n method: \"GET\",\n target,\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 The full registration view, with the status the question was put\n * into (`pending` when it had never computed, else `stale`). Servers\n * before `personal-server-ts` d91124d answered only\n * `{ questionId, derivedScope, status }` here, which no longer parses.\n */\nexport async function recomputeQuestion(\n params: RecomputeQuestionParams,\n): Promise<QuestionRecomputeResult> {\n const target = `${assertQuestionId(params.questionId)}/recompute`;\n return sendQuestionRequest(\n params,\n { method: \"POST\", target, 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 target = assertQuestionId(params.questionId);\n return sendQuestionRequest(\n params,\n { method: \"DELETE\", target, 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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B,OAAO,QAAQ;AAAA,IAC/C,SAAS,OAAO;AAAA,EAClB,CAAC;AACD,QAAM,aAA2C;AAAA,IAC/C,mBAAmB,iBAAiB,OAAO,iBAAiB;AAAA,IAC5D,OAAO,OAAO;AAAA,IACd,SAAS,OAAO;AAAA,IAChB,aAAa,OAAO;AAAA,IACpB,GAAI,OAAO,aAAa,SAAY,CAAC,IAAI,EAAE,UAAU,OAAO,SAAS;AAAA,IACrE,GAAI,OAAO,YAAY,SAAY,CAAC,IAAI,EAAE,SAAS,OAAO,QAAQ;AAAA,IAClE,GAAI,OAAO,UAAU,SAAY,CAAC,IAAI,EAAE,OAAO,OAAO,MAAM;AAAA,EAC9D;AACA,QAAM,SAAS,MAAM,uBAAuB,UAAU;AACtD,SAAO,EAAE,cAAc,OAAO;AAChC;","names":[]}
|
|
@@ -0,0 +1,209 @@
|
|
|
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_status_exports = {};
|
|
20
|
+
__export(derivative_status_exports, {
|
|
21
|
+
DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS: () => DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS,
|
|
22
|
+
DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS: () => DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS,
|
|
23
|
+
DERIVATIVE_ERROR_CODES: () => import_derivative_questions2.DERIVATIVE_ERROR_CODES,
|
|
24
|
+
DERIVATIVE_STATUS_PATH: () => DERIVATIVE_STATUS_PATH,
|
|
25
|
+
DerivativeErrorCodeSchema: () => import_derivative_questions2.DerivativeErrorCodeSchema,
|
|
26
|
+
DerivativeStatusSchema: () => DerivativeStatusSchema,
|
|
27
|
+
derivativeStatusTarget: () => derivativeStatusTarget,
|
|
28
|
+
getDerivativeStatus: () => getDerivativeStatus,
|
|
29
|
+
isDerivativeStatusSettled: () => isDerivativeStatusSettled,
|
|
30
|
+
waitForDerivativeStatus: () => waitForDerivativeStatus
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(derivative_status_exports);
|
|
33
|
+
var import_zod = require("zod");
|
|
34
|
+
var import_web3_signed_builder = require("../auth/web3-signed-builder");
|
|
35
|
+
var import_errors = require("../errors");
|
|
36
|
+
var import_derivative_questions = require("./derivative-questions");
|
|
37
|
+
var import_write_signer = require("./write-signer");
|
|
38
|
+
var import_derivative_questions2 = require("./derivative-questions");
|
|
39
|
+
const DERIVATIVE_STATUS_PATH = "/v1/derivatives/status";
|
|
40
|
+
const DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS = 12e4;
|
|
41
|
+
const DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS = 2e3;
|
|
42
|
+
const nullable = (schema) => schema.nullish().transform((value) => value ?? null);
|
|
43
|
+
const DerivativeStatusSchema = import_zod.z.object({
|
|
44
|
+
derivedScope: import_zod.z.string().min(1),
|
|
45
|
+
status: import_derivative_questions.QuestionStatusSchema,
|
|
46
|
+
/** When the last compute finished, or `null` if none ever has. */
|
|
47
|
+
lastComputedAt: nullable(import_zod.z.string()),
|
|
48
|
+
/** Local version of the derived record the last compute wrote. */
|
|
49
|
+
derivedVersion: nullable(import_zod.z.number()),
|
|
50
|
+
derivedCollectedAt: nullable(import_zod.z.string()),
|
|
51
|
+
/** The failure class; `null` unless `status` is `failed`. */
|
|
52
|
+
errorCode: nullable(import_derivative_questions.DerivativeErrorCodeSchema),
|
|
53
|
+
/**
|
|
54
|
+
* Seconds until the Personal Server's next automatic retry, or `null` when
|
|
55
|
+
* none is pending or running — the terminal signature. Poll on this cadence
|
|
56
|
+
* rather than guessing one.
|
|
57
|
+
*/
|
|
58
|
+
retryAfterSeconds: nullable(import_zod.z.number())
|
|
59
|
+
});
|
|
60
|
+
function derivativeStatusTarget(derivedScope) {
|
|
61
|
+
return `${DERIVATIVE_STATUS_PATH}?derivedScope=${encodeURIComponent(derivedScope)}`;
|
|
62
|
+
}
|
|
63
|
+
function normalizeBaseUrl(url) {
|
|
64
|
+
return url.replace(/\/+$/, "");
|
|
65
|
+
}
|
|
66
|
+
function resolveFetch(fetchFn) {
|
|
67
|
+
const resolved = fetchFn ?? globalThis.fetch;
|
|
68
|
+
if (resolved === void 0) {
|
|
69
|
+
throw new import_errors.WriteRequestError("No fetch implementation available");
|
|
70
|
+
}
|
|
71
|
+
return resolved;
|
|
72
|
+
}
|
|
73
|
+
function requireDerivedScope(derivedScope) {
|
|
74
|
+
if (typeof derivedScope !== "string" || derivedScope.length === 0) {
|
|
75
|
+
throw new import_errors.WriteRequestError("derivedScope is required");
|
|
76
|
+
}
|
|
77
|
+
return derivedScope;
|
|
78
|
+
}
|
|
79
|
+
function sleep(ms, signal) {
|
|
80
|
+
if (ms <= 0) return Promise.resolve();
|
|
81
|
+
return new Promise((resolve, reject) => {
|
|
82
|
+
const timer = setTimeout(() => {
|
|
83
|
+
signal?.removeEventListener("abort", onAbort);
|
|
84
|
+
resolve();
|
|
85
|
+
}, ms);
|
|
86
|
+
const onAbort = () => {
|
|
87
|
+
clearTimeout(timer);
|
|
88
|
+
reject(abortError(signal));
|
|
89
|
+
};
|
|
90
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function timeoutError(latest, timeoutMs) {
|
|
94
|
+
return new import_errors.DerivativeQuestionTimeoutError(
|
|
95
|
+
`Derived scope ${latest.derivedScope} was still ${latest.status} after ${timeoutMs}ms`,
|
|
96
|
+
{
|
|
97
|
+
derivedScope: latest.derivedScope,
|
|
98
|
+
status: latest.status,
|
|
99
|
+
errorCode: latest.errorCode,
|
|
100
|
+
retryAfterSeconds: latest.retryAfterSeconds,
|
|
101
|
+
timeoutMs
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
function abortError(signal) {
|
|
106
|
+
const reason = signal?.reason;
|
|
107
|
+
return reason instanceof Error ? reason : new import_errors.WriteRequestError("Derivative status wait was aborted");
|
|
108
|
+
}
|
|
109
|
+
function isDerivativeStatusSettled(status) {
|
|
110
|
+
if (status.status === "ready") return true;
|
|
111
|
+
return status.status === "failed" && status.retryAfterSeconds === null;
|
|
112
|
+
}
|
|
113
|
+
async function getDerivativeStatus(params) {
|
|
114
|
+
const derivedScope = requireDerivedScope(params.derivedScope);
|
|
115
|
+
const fetchFn = resolveFetch(params.fetch);
|
|
116
|
+
const baseUrl = normalizeBaseUrl(params.personalServerUrl);
|
|
117
|
+
const signer = (0, import_write_signer.resolveWriteSigner)(params.signer, { account: params.account });
|
|
118
|
+
const headers = new Headers(params.headers);
|
|
119
|
+
headers.set(
|
|
120
|
+
"Authorization",
|
|
121
|
+
await (0, import_web3_signed_builder.buildWeb3SignedHeader)({
|
|
122
|
+
signMessage: signer.signMessage,
|
|
123
|
+
aud: params.audience ?? baseUrl,
|
|
124
|
+
method: "GET",
|
|
125
|
+
uri: DERIVATIVE_STATUS_PATH,
|
|
126
|
+
grantId: params.grantId
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
let response;
|
|
130
|
+
try {
|
|
131
|
+
response = await fetchFn(
|
|
132
|
+
`${baseUrl}${derivativeStatusTarget(derivedScope)}`,
|
|
133
|
+
{
|
|
134
|
+
method: "GET",
|
|
135
|
+
headers,
|
|
136
|
+
...params.signal ? { signal: params.signal } : {}
|
|
137
|
+
}
|
|
138
|
+
);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
throw new import_errors.WriteTransportError(
|
|
141
|
+
`Derivative status read failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
142
|
+
1,
|
|
143
|
+
err
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (!response.ok) {
|
|
147
|
+
throw await (0, import_derivative_questions.personalServerErrorFromQuestionResponse)(
|
|
148
|
+
response
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
let body;
|
|
152
|
+
try {
|
|
153
|
+
body = await response.json();
|
|
154
|
+
} catch (err) {
|
|
155
|
+
throw new import_errors.DerivativeQuestionRejectedError(
|
|
156
|
+
"Derivative status response is not JSON",
|
|
157
|
+
response.status,
|
|
158
|
+
null,
|
|
159
|
+
{ cause: err instanceof Error ? err.message : String(err) }
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
const parsed = DerivativeStatusSchema.safeParse(body);
|
|
163
|
+
if (!parsed.success) {
|
|
164
|
+
throw new import_errors.DerivativeQuestionRejectedError(
|
|
165
|
+
"Derivative status response is not a status view",
|
|
166
|
+
response.status,
|
|
167
|
+
null,
|
|
168
|
+
{ issues: parsed.error.issues }
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return parsed.data;
|
|
172
|
+
}
|
|
173
|
+
async function waitForDerivativeStatus(params) {
|
|
174
|
+
const timeoutMs = Math.max(
|
|
175
|
+
0,
|
|
176
|
+
params.timeoutMs ?? DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS
|
|
177
|
+
);
|
|
178
|
+
const pollIntervalMs = Math.max(
|
|
179
|
+
0,
|
|
180
|
+
params.pollIntervalMs ?? DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS
|
|
181
|
+
);
|
|
182
|
+
const deadline = Date.now() + timeoutMs;
|
|
183
|
+
for (; ; ) {
|
|
184
|
+
if (params.signal?.aborted) throw abortError(params.signal);
|
|
185
|
+
const latest = await getDerivativeStatus(params);
|
|
186
|
+
if (isDerivativeStatusSettled(latest)) return latest;
|
|
187
|
+
const remaining = deadline - Date.now();
|
|
188
|
+
if (remaining <= 0) throw timeoutError(latest, timeoutMs);
|
|
189
|
+
const waitMs = latest.retryAfterSeconds === null ? pollIntervalMs : latest.retryAfterSeconds * 1e3;
|
|
190
|
+
if (waitMs > remaining) {
|
|
191
|
+
throw timeoutError(latest, timeoutMs);
|
|
192
|
+
}
|
|
193
|
+
await sleep(waitMs, params.signal);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
197
|
+
0 && (module.exports = {
|
|
198
|
+
DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS,
|
|
199
|
+
DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS,
|
|
200
|
+
DERIVATIVE_ERROR_CODES,
|
|
201
|
+
DERIVATIVE_STATUS_PATH,
|
|
202
|
+
DerivativeErrorCodeSchema,
|
|
203
|
+
DerivativeStatusSchema,
|
|
204
|
+
derivativeStatusTarget,
|
|
205
|
+
getDerivativeStatus,
|
|
206
|
+
isDerivativeStatusSettled,
|
|
207
|
+
waitForDerivativeStatus
|
|
208
|
+
});
|
|
209
|
+
//# sourceMappingURL=derivative-status.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/protocol/derivative-status.ts"],"sourcesContent":["/**\n * Derivative status: the lifecycle of the question behind a derived scope,\n * as the party that READS the answer sees it.\n *\n * @remarks\n * A builder registers a question with {@link registerQuestion} and follows it\n * with {@link waitForQuestion}, both of which need a write session. The app\n * that only consumes the answer holds no write entry at all — a consent flow\n * grants it a bare read on the derived scope — so it cannot open one, and\n * `GET /v1/data/<derivedScope>` answers 404 for all three of \"computing right\n * now\", \"failed but retrying\" and \"failed for good\".\n *\n * `GET /v1/derivatives/status?derivedScope=<scope>` is that reader's view.\n * Authorization is the data read's (a live grant covering the derived scope,\n * or the owner), nothing is served and nothing is charged, and the view is\n * deliberately narrow: lifecycle, a coarse {@link DerivativeErrorCode} and\n * the next retry. The question text, the source scopes, the question id, the\n * registrar and the server's raw error string stay owner-only.\n *\n * Requires `personal-server-ts` with the status route; an older Personal\n * Server answers 404 for the route itself.\n *\n * @category Protocol\n */\n\nimport { z } from \"zod\";\nimport { buildWeb3SignedHeader } from \"../auth/web3-signed-builder\";\nimport {\n DerivativeQuestionRejectedError,\n DerivativeQuestionTimeoutError,\n WriteRequestError,\n WriteTransportError,\n type PersonalServerWriteError,\n} from \"../errors\";\nimport {\n DerivativeErrorCodeSchema,\n personalServerErrorFromQuestionResponse,\n QuestionStatusSchema,\n type QuestionStatus,\n} from \"./derivative-questions\";\nimport {\n resolveWriteSigner,\n type ResolveWriteSignerOptions,\n type WriteSignerSource,\n} from \"./write-signer\";\n\nexport {\n DERIVATIVE_ERROR_CODES,\n DerivativeErrorCodeSchema,\n type DerivativeErrorCode,\n} from \"./derivative-questions\";\n\n/** The reader-facing status route. */\nexport const DERIVATIVE_STATUS_PATH = \"/v1/derivatives/status\";\n\n/** How long {@link waitForDerivativeStatus} polls before giving up. */\nexport const DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS = 120_000;\n\n/**\n * How long {@link waitForDerivativeStatus} waits between polls when the\n * server names no retry time of its own.\n */\nexport const DEFAULT_DERIVATIVE_STATUS_POLL_INTERVAL_MS = 2_000;\n\nconst nullable = <T extends z.ZodTypeAny>(schema: T) =>\n schema\n .nullish()\n .transform((value: z.infer<T> | null | undefined) => value ?? null);\n\n/**\n * The status of the derived scope, not of one registration: when several\n * questions write the same scope, the server reports the most optimistic\n * true state, because serving data is registration-agnostic.\n */\nexport const DerivativeStatusSchema = z.object({\n derivedScope: z.string().min(1),\n status: QuestionStatusSchema,\n /** When the last compute finished, or `null` if none ever has. */\n lastComputedAt: nullable(z.string()),\n /** Local version of the derived record the last compute wrote. */\n derivedVersion: nullable(z.number()),\n derivedCollectedAt: nullable(z.string()),\n /** The failure class; `null` unless `status` is `failed`. */\n errorCode: nullable(DerivativeErrorCodeSchema),\n /**\n * Seconds until the Personal Server's next automatic retry, or `null` when\n * none is pending or running — the terminal signature. Poll on this cadence\n * rather than guessing one.\n */\n retryAfterSeconds: nullable(z.number()),\n});\n\n/** @see {@link DerivativeStatusSchema} */\nexport type DerivativeStatus = z.infer<typeof DerivativeStatusSchema>;\n\n/** What {@link getDerivativeStatus} needs to sign and send one read. */\nexport interface GetDerivativeStatusParams extends ResolveWriteSignerOptions {\n /** Personal Server origin, e.g. `https://ps.example.com`. */\n personalServerUrl: string;\n /** The derived scope whose question to observe. */\n derivedScope: string;\n /**\n * A grant covering the derived scope, sent as the signed `grantId` claim.\n * Omit only when the signer is the Personal Server's owner, who is\n * authorized without one.\n */\n grantId?: string;\n /** Reader key: a viem `LocalAccount`, `WalletClient`, or `{ signMessage }`. */\n signer: WriteSignerSource;\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 /** Aborts the request in flight. */\n signal?: AbortSignal;\n}\n\n/** What {@link waitForDerivativeStatus} polls with. */\nexport interface WaitForDerivativeStatusParams extends GetDerivativeStatusParams {\n /** Give up after this long (default 120s). */\n timeoutMs?: number;\n /**\n * Wait between polls when the server names no retry time (default 2s).\n * A `retryAfterSeconds` from the server replaces this outright, longer or\n * shorter: it is when the next compute actually happens.\n */\n pollIntervalMs?: number;\n /**\n * Aborts the wait, and the request in flight with it: the signal is passed\n * to every poll, so an abort during a stalled request does not sit until\n * the transport gives up.\n */\n signal?: AbortSignal;\n}\n\n/**\n * The request target for a status read: the query carries the derived scope,\n * the signed `uri` does not.\n *\n * @remarks\n * Like every Web3Signed read (data, lineage), the Personal Server verifies\n * the signature over the PATH; per-scope authorization is enforced live\n * against the caller's grant on each request, so the query needs no\n * signature to be safe. Only the write path signs path AND query, where a\n * parameter decides what is written.\n */\nexport function derivativeStatusTarget(derivedScope: string): string {\n return `${DERIVATIVE_STATUS_PATH}?derivedScope=${encodeURIComponent(derivedScope)}`;\n}\n\nfunction normalizeBaseUrl(url: string): string {\n return url.replace(/\\/+$/, \"\");\n}\n\nfunction resolveFetch(fetchFn: typeof fetch | undefined): typeof fetch {\n const resolved = fetchFn ?? globalThis.fetch;\n if (resolved === undefined) {\n throw new WriteRequestError(\"No fetch implementation available\");\n }\n return resolved;\n}\n\nfunction requireDerivedScope(derivedScope: string): string {\n if (typeof derivedScope !== \"string\" || derivedScope.length === 0) {\n // The server answers 400 DERIVATIVE_DERIVED_SCOPE_REQUIRED; refuse before\n // signing rather than spend a signature on a request that cannot pass.\n throw new WriteRequestError(\"derivedScope is required\");\n }\n return derivedScope;\n}\n\nfunction sleep(ms: number, signal?: AbortSignal): Promise<void> {\n if (ms <= 0) return Promise.resolve();\n return new Promise((resolve, reject) => {\n const timer = setTimeout(() => {\n signal?.removeEventListener(\"abort\", onAbort);\n resolve();\n }, ms);\n const onAbort = () => {\n clearTimeout(timer);\n reject(abortError(signal));\n };\n signal?.addEventListener(\"abort\", onAbort, { once: true });\n });\n}\n\nfunction timeoutError(\n latest: DerivativeStatus,\n timeoutMs: number,\n): DerivativeQuestionTimeoutError {\n return new DerivativeQuestionTimeoutError(\n `Derived scope ${latest.derivedScope} was still ${latest.status} after ${timeoutMs}ms`,\n {\n derivedScope: latest.derivedScope,\n status: latest.status,\n errorCode: latest.errorCode,\n retryAfterSeconds: latest.retryAfterSeconds,\n timeoutMs,\n },\n );\n}\n\nfunction abortError(signal?: AbortSignal): Error {\n const reason = signal?.reason;\n return reason instanceof Error\n ? reason\n : new WriteRequestError(\"Derivative status wait was aborted\");\n}\n\n/**\n * Is this a state the reader can act on?\n *\n * @remarks\n * `ready` means the derived scope has an answer to read. A `failed` status\n * is settled only when no retry is pending: with `retryAfterSeconds` set the\n * Personal Server will compute again on its own, so the answer may still\n * arrive. `pending` and `stale` are always in flight.\n */\nexport function isDerivativeStatusSettled(status: DerivativeStatus): boolean {\n if (status.status === \"ready\") return true;\n return status.status === \"failed\" && status.retryAfterSeconds === null;\n}\n\n/**\n * Read the lifecycle of the question behind a derived scope.\n *\n * @remarks\n * Sends `GET /v1/derivatives/status?derivedScope=<scope>` with a Web3Signed\n * `Authorization` header carrying `grantId`, the same authentication a data\n * read uses. Nothing is charged: the route authorizes, it does not serve\n * data, so a priced grant raises no 402 here.\n *\n * @returns The status of the derived scope. When several registrations write\n * it, the most optimistic true state answers — `ready`, then `stale`, then\n * `pending`, then `failed` — because a duplicate that never wrote anything\n * must not report away an answer the scope has.\n * @throws {DerivativeQuestionNotFoundError} 404: the caller may read the\n * scope but no question stands behind it (and, on an older Personal\n * Server, the route itself is unknown).\n * @throws {WriteForbiddenError} 403: the grant does not cover the derived\n * scope. The check runs before any store lookup, so a caller cannot probe\n * which scopes have questions.\n * @throws {DerivativeQuestionRejectedError} On any other non-2xx answer or\n * an unparseable body.\n * @throws {WriteTransportError} When `fetch` itself failed.\n * @throws {WriteRequestError} On a missing `derivedScope` or no `fetch`.\n *\n * @example\n * ```typescript\n * const status = await getDerivativeStatus({\n * personalServerUrl: \"https://ps.example.com\",\n * derivedScope: \"coach.weekly\",\n * grantId,\n * signer,\n * });\n * if (status.status === \"ready\") {\n * const record = await readPersonalServerData({ ... });\n * } else if (status.retryAfterSeconds !== null) {\n * // Computing or retrying: come back then.\n * }\n * ```\n */\nexport async function getDerivativeStatus(\n params: GetDerivativeStatusParams,\n): Promise<DerivativeStatus> {\n const derivedScope = requireDerivedScope(params.derivedScope);\n const fetchFn = resolveFetch(params.fetch);\n const baseUrl = normalizeBaseUrl(params.personalServerUrl);\n const signer = resolveWriteSigner(params.signer, { account: params.account });\n const headers = new Headers(params.headers);\n headers.set(\n \"Authorization\",\n await buildWeb3SignedHeader({\n signMessage: signer.signMessage,\n aud: params.audience ?? baseUrl,\n method: \"GET\",\n uri: DERIVATIVE_STATUS_PATH,\n grantId: params.grantId,\n }),\n );\n\n let response: Response;\n try {\n response = await fetchFn(\n `${baseUrl}${derivativeStatusTarget(derivedScope)}`,\n {\n method: \"GET\",\n headers,\n ...(params.signal ? { signal: params.signal } : {}),\n },\n );\n } catch (err) {\n throw new WriteTransportError(\n `Derivative status read failed: ${err instanceof Error ? err.message : String(err)}`,\n 1,\n err,\n );\n }\n if (!response.ok) {\n throw (await personalServerErrorFromQuestionResponse(\n response,\n )) as PersonalServerWriteError;\n }\n let body: unknown;\n try {\n body = await response.json();\n } catch (err) {\n throw new DerivativeQuestionRejectedError(\n \"Derivative status response is not JSON\",\n response.status,\n null,\n { cause: err instanceof Error ? err.message : String(err) },\n );\n }\n const parsed = DerivativeStatusSchema.safeParse(body);\n if (!parsed.success) {\n throw new DerivativeQuestionRejectedError(\n \"Derivative status response is not a status view\",\n response.status,\n null,\n { issues: parsed.error.issues },\n );\n }\n return parsed.data;\n}\n\n/**\n * Poll {@link getDerivativeStatus} until the derived scope has an answer or\n * has stopped trying to get one.\n *\n * @remarks\n * Returns as soon as {@link isDerivativeStatusSettled} holds: `ready`, or\n * `failed` with no retry pending. A failure the server will retry is not a\n * result, so the wait continues through it — on the server's own cadence,\n * because `retryAfterSeconds` is when the next compute actually happens and\n * polling faster only spends requests. A failed status is returned, not\n * thrown: the reader branches on `errorCode`.\n *\n * @returns The settled status.\n * @throws {DerivativeQuestionTimeoutError} When the budget ran out first.\n * The question keeps computing on the server; call again.\n *\n * @example\n * ```typescript\n * const status = await waitForDerivativeStatus({\n * personalServerUrl,\n * derivedScope: \"coach.weekly\",\n * grantId,\n * signer,\n * timeoutMs: 60_000,\n * });\n * if (status.status !== \"ready\") console.log(status.errorCode);\n * ```\n */\nexport async function waitForDerivativeStatus(\n params: WaitForDerivativeStatusParams,\n): Promise<DerivativeStatus> {\n const timeoutMs = Math.max(\n 0,\n params.timeoutMs ?? DEFAULT_DERIVATIVE_STATUS_TIMEOUT_MS,\n );\n const pollIntervalMs = Math.max(\n 0,\n params.pollIntervalMs ?? DEFAULT_DERIVATIVE_STATUS_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 getDerivativeStatus(params);\n if (isDerivativeStatusSettled(latest)) return latest;\n const remaining = deadline - Date.now();\n if (remaining <= 0) throw timeoutError(latest, timeoutMs);\n // The server's scheduled retry decides the cadence outright, in both\n // directions: a caller's longer pollIntervalMs would sit past a retry\n // that has already produced an answer, and a shorter one would ask\n // before anything can have changed. pollIntervalMs is the cadence for\n // the case where the server named none.\n const waitMs =\n latest.retryAfterSeconds === null\n ? pollIntervalMs\n : latest.retryAfterSeconds * 1000;\n if (waitMs > remaining) {\n // The budget cannot cover the next cadence, so the poll after this\n // sleep would land before anything could have changed. Give up now\n // instead of spending a request to say the same thing.\n throw timeoutError(latest, timeoutMs);\n }\n await sleep(waitMs, params.signal);\n }\n}\n\n/** Statuses that mean a compute is in flight. @see {@link QuestionStatus} */\nexport type PendingDerivativeStatus = Extract<\n QuestionStatus,\n \"pending\" | \"stale\"\n>;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAyBA,iBAAkB;AAClB,iCAAsC;AACtC,oBAMO;AACP,kCAKO;AACP,0BAIO;AAEP,IAAAA,+BAIO;AAGA,MAAM,yBAAyB;AAG/B,MAAM,uCAAuC;AAM7C,MAAM,6CAA6C;AAE1D,MAAM,WAAW,CAAyB,WACxC,OACG,QAAQ,EACR,UAAU,CAAC,UAAyC,SAAS,IAAI;AAO/D,MAAM,yBAAyB,aAAE,OAAO;AAAA,EAC7C,cAAc,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