@yaks/openai 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +389 -2
- package/_dist/batches.d.ts +77 -0
- package/_dist/batches.d.ts.map +1 -0
- package/_dist/credential.d.ts +13 -0
- package/_dist/credential.d.ts.map +1 -0
- package/_dist/images.d.ts +15 -0
- package/_dist/images.d.ts.map +1 -0
- package/_dist/list.d.ts +8 -0
- package/_dist/list.d.ts.map +1 -0
- package/_dist/media.d.ts +11 -0
- package/_dist/media.d.ts.map +1 -0
- package/_dist/mod.d.ts +30 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/project-keys.d.ts +4 -0
- package/_dist/project-keys.d.ts.map +1 -0
- package/_dist/provider.d.ts +48 -0
- package/_dist/provider.d.ts.map +1 -0
- package/_dist/responses.d.ts +51 -0
- package/_dist/responses.d.ts.map +1 -0
- package/_dist/transport.d.ts +121 -0
- package/_dist/transport.d.ts.map +1 -0
- package/_dist/vocab.d.ts +10 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/batches.js +145 -0
- package/batches.js.map +1 -0
- package/batches.ts +194 -0
- package/credential.js +30 -0
- package/credential.js.map +1 -0
- package/credential.ts +40 -0
- package/deno.json +1 -0
- package/generations.js +288 -0
- package/generations.js.map +1 -0
- package/generations.ts +365 -0
- package/images.js +37 -0
- package/images.js.map +1 -0
- package/images.ts +63 -0
- package/info.js +55 -0
- package/info.js.map +1 -0
- package/info.ts +59 -0
- package/limits.js +50 -0
- package/limits.js.map +1 -0
- package/limits.ts +48 -0
- package/list.js +88 -0
- package/list.js.map +1 -0
- package/list.ts +127 -0
- package/mark.js +29 -0
- package/mark.js.map +1 -0
- package/mark.ts +50 -0
- package/media.js +60 -0
- package/media.js.map +1 -0
- package/media.ts +94 -0
- package/mod.js +28 -0
- package/mod.js.map +1 -0
- package/mod.ts +64 -0
- package/native.js +176 -0
- package/native.js.map +1 -0
- package/native.ts +221 -0
- package/package.json +36 -4
- package/project-keys.js +32 -0
- package/project-keys.js.map +1 -0
- package/project-keys.ts +43 -0
- package/provider.js +190 -0
- package/provider.js.map +1 -0
- package/provider.ts +292 -0
- package/reads.js +167 -0
- package/reads.js.map +1 -0
- package/reads.ts +169 -0
- package/responses.js +612 -0
- package/responses.js.map +1 -0
- package/responses.ts +706 -0
- package/transport.js +646 -0
- package/transport.js.map +1 -0
- package/transport.ts +972 -0
- package/vocab.js +23 -0
- package/vocab.js.map +1 -0
- package/vocab.json +25 -0
- package/vocab.ts +29 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** A bearer and the endpoint it is good for. */ export type Credential = {
|
|
2
|
+
token: string;
|
|
3
|
+
/** the ChatGPT account the token was issued to; the Codex backend wants it
|
|
4
|
+
* beside the bearer */ account?: string;
|
|
5
|
+
base: string;
|
|
6
|
+
};
|
|
7
|
+
export declare let OPENAI: string;
|
|
8
|
+
export declare let CODEX: string;
|
|
9
|
+
type Env = (name: string) => string | undefined;
|
|
10
|
+
/** An API key from the environment. */ export declare let fromEnv: (env: Env) => Credential | undefined;
|
|
11
|
+
/** The account claim in a ChatGPT OAuth bearer. The bearer never leaves the
|
|
12
|
+
* caller, and a malformed one fails before it can be sent to the backend. */ export declare let fromChatGPT: (token: string) => Credential;
|
|
13
|
+
//# sourceMappingURL=credential.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential.d.ts","sources":["../credential.ts"],"names":[],"mappings":"AAGA,8CAA8C,GAC9C,YAAY;EACV,OAAO,MAAM;EACb;uBACqB,GACrB,UAAU,MAAM;EAChB,MAAM,MAAM;;AAGd,OAAO,YAAI,eAAoC;AAC/C,OAAO,YAAI,cAA+C;KAErD,OAAO,MAAM,MAAM,KAAK,MAAM,GAAG,SAAS;AAE/C,qCAAqC,GACrC,OAAO,YAAI,UAAW,KAAK,QAAM,aAAa,SAAS,CAGtD;AAED;2EAC2E,GAC3E,OAAO,YAAI,cAAe,OAAO,MAAM,KAAG,WAczC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ArtifactStore } from "@yaks/blob";
|
|
2
|
+
export type ImageGeneration = {
|
|
3
|
+
type?: 'image_generation';
|
|
4
|
+
output_format?: 'png' | 'jpeg' | 'webp';
|
|
5
|
+
quality?: 'auto' | 'low' | 'medium' | 'high';
|
|
6
|
+
size?: 'auto' | '1024x1024' | '1536x1024' | '1024x1536';
|
|
7
|
+
background?: 'auto' | 'opaque' | 'transparent';
|
|
8
|
+
};
|
|
9
|
+
export type Images = {
|
|
10
|
+
/** @deprecated Ignored. Configured image tools are offered on every endpoint. */ auto?: boolean;
|
|
11
|
+
tool?: ImageGeneration;
|
|
12
|
+
store: ArtifactStore;
|
|
13
|
+
/** Maximum decoded image bytes, default 32 MiB. */ maxBytes?: number;
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=images.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"images.d.ts","sources":["../images.ts"],"names":[],"mappings":"AAEA,cAAc,aAAa,0BAA+B;AAG1D,YAAY;EACV,OAAO;EACP,gBAAgB,QAAQ,SAAS;EACjC,UAAU,SAAS,QAAQ,WAAW;EACtC,OAAO,SAAS,cAAc,cAAc;EAC5C,aAAa,SAAS,WAAW;;AAEnC,YAAY;EACV,+EAA+E,GAC/E,OAAO,OAAO;EACd,OAAO;EACP,OAAO;EACP,iDAAiD,GACjD,WAAW,MAAM"}
|
package/_dist/list.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type Listed } from "@yaks/model";
|
|
2
|
+
import { type Credential } from "./credential.js";
|
|
3
|
+
export type ListingOptions = {
|
|
4
|
+
retries?: number;
|
|
5
|
+
pause?: (ms: number) => Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
export declare let listing: (cred: Credential, fetcher?: typeof fetch, refresh?: (stale: Credential) => Credential | Promise<Credential>, headers?: Record<string, string>, opts?: ListingOptions) => Promise<Listed[]>;
|
|
8
|
+
//# sourceMappingURL=list.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"list.d.ts","sources":["../list.ts"],"names":[],"mappings":"AACA,SAAS,KAAK,MAAM,2BAA4C;AAChE,SAAgB,KAAK,UAAU,0BAAyB;AAGxD,YAAY;EACV,UAAU,MAAM;EAChB,SAAS,IAAI,MAAM,KAAK,QAAQ,IAAI;;AA2EtC,OAAO,YAAI,UACT,MAAM,YACN,iBAAgB,OAChB,WAAW,OAAO,eAAe,aAAa,QAAQ,aACtD,UAAS,OAAO,MAAM,EAAE,MAAM,GAC9B,OAAM,mBACL,QAAQ,UAsCV"}
|
package/_dist/media.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Decode provider media at the artifact boundary, before bytes enter a store. */ import { type ArtifactStore } from "@yaks/blob";
|
|
2
|
+
import { type Reply } from "@yaks/model";
|
|
3
|
+
export type MediaStore = {
|
|
4
|
+
store: ArtifactStore;
|
|
5
|
+
maxBytes?: number;
|
|
6
|
+
};
|
|
7
|
+
/** Refuses, as the store says, when it has no room for media: asked by a
|
|
8
|
+
* model that makes bytes before its paid call. */ export declare let mediaRoom: (options?: MediaStore) => Promise<void>;
|
|
9
|
+
/** Store one complete base64 media value, returning only its address and type. */ export declare let generatedMedia: (data: unknown, mediaType: string, call: string, options?: MediaStore) => Promise<NonNullable<Reply['artifacts']>[number]>;
|
|
10
|
+
/** Store bytes from a provider that answers with a binary body. */ export declare let generatedBytes: (bytes: Uint8Array, mediaType: string, call: string, options?: MediaStore) => Promise<NonNullable<Reply['artifacts']>[number]>;
|
|
11
|
+
//# sourceMappingURL=media.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.d.ts","sources":["../media.ts"],"names":[],"mappings":"AAAA,gFAAgF,GAChF,SAAS,KAAK,aAAa,0BAAiD;AAC5E,SAAgC,KAAK,KAAK,2BAAgC;AAU1E,YAAY;EACV,OAAO;EACP,WAAW,MAAM;;AAGnB;gDACgD,GAChD,OAAO,YAAI,YAAmB,UAAU,eAAa,QAAQ,IAAI,EAEhE;AAED,gFAAgF,GAChF,OAAO,YAAI,iBACT,MAAM,OAAO,EACb,WAAW,MAAM,EACjB,MAAM,MAAM,EACZ,UAAU,eACT,QAAQ,YAAY,MAAM,cAAc,MAAM,GA4BhD;AAED,iEAAiE,GACjE,OAAO,YAAI,iBACT,OAAO,YACP,WAAW,MAAM,EACjB,MAAM,MAAM,EACZ,UAAU,eACT,QAAQ,YAAY,MAAM,cAAc,MAAM,GA4BhD"}
|
package/_dist/mod.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @yaks/openai implements {@link https://jsr.io/@yaks/model | @yaks/model}'s
|
|
3
|
+
* `Model` interface over OpenAI's Responses API: one streamed exchange over
|
|
4
|
+
* `fetch`, a bearer an application supplies, and the endpoint it reaches. It
|
|
5
|
+
* opens no database and starts no process; a server that wants a model calls
|
|
6
|
+
* {@link responses} and gets back a `Model`.
|
|
7
|
+
*
|
|
8
|
+
* ```ts ignore
|
|
9
|
+
* import { fromEnv, responses } from '@yaks/openai'
|
|
10
|
+
* import { env } from '@yaks/process/system'
|
|
11
|
+
*
|
|
12
|
+
* let model = responses({
|
|
13
|
+
* credential: () => fromEnv(env.get)!,
|
|
14
|
+
* })
|
|
15
|
+
* let reply = await model({
|
|
16
|
+
* model: 'gpt-6.2-astra',
|
|
17
|
+
* items: [{ kind: 'user', text: 'hi' }],
|
|
18
|
+
* tools: [],
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @module
|
|
23
|
+
*/ export { CODEX, type Credential, fromChatGPT, fromEnv, OPENAI } from "./credential.js";
|
|
24
|
+
export { body, input, items, OPENAI_COMP, openaiDoc, type Options, responses } from "./responses.js";
|
|
25
|
+
export { CREDENTIAL_FAULT, type CredentialSource, frames, type RateLimits, ResponseError, type ResponseEvent, type ResponseFault, type ResponseItem, type ResponseOptions, type ResponseRequest, type ResponseResult, type ResponseUsage, type RunOptions, transport, type TransportCredential } from "./transport.js";
|
|
26
|
+
export type { ImageGeneration, Images } from "./images.js";
|
|
27
|
+
export { generatedBytes, generatedMedia, mediaRoom } from "./media.js";
|
|
28
|
+
export type { MediaStore } from "./media.js";
|
|
29
|
+
export { listing } from "./list.js";
|
|
30
|
+
//# sourceMappingURL=mod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;CAsBC,GAED,SACE,KAAK,EACL,KAAK,UAAU,EACf,WAAW,EACX,OAAO,EACP,MAAM,0BACgB;AACxB,SACE,IAAI,EACJ,KAAK,EACL,KAAK,EACL,WAAW,EACX,SAAS,EACT,KAAK,OAAO,EACZ,SAAS,yBACY;AAEvB,SACE,gBAAgB,EAChB,KAAK,gBAAgB,EACrB,MAAM,EACN,KAAK,UAAU,EACf,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,aAAa,EAClB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,aAAa,EAClB,KAAK,UAAU,EACf,SAAS,EACT,KAAK,mBAAmB,yBACH;AAEvB,cAAc,eAAe,EAAE,MAAM,sBAAqB;AAC1D,SAAS,cAAc,EAAE,cAAc,EAAE,SAAS,qBAAoB;AACtE,cAAc,UAAU,qBAAoB;AAE5C,SAAS,OAAO,oBAAmB"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare let projectId: (value: unknown) => value is string;
|
|
2
|
+
/** Issue a project credential through its own service account. OpenAI
|
|
3
|
+
* returns the key only on creation. Provider responses stay at this boundary. */ export declare let projectKey: (admin: string, project: string, name: string, send?: typeof fetch) => Promise<string>;
|
|
4
|
+
//# sourceMappingURL=project-keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"project-keys.d.ts","sources":["../project-keys.ts"],"names":[],"mappings":"AAEA,OAAO,YAAI,YAAa,OAAO,OAAO,KAAG,SAAS,MAAM,CACP;AAEjD;+EAC+E,GAC/E,OAAO,YAAI,aACT,OAAO,MAAM,EACb,SAAS,MAAM,EACf,MAAM,MAAM,EACZ,cAAa,UACZ,QAAQ,MAAM,EA8BhB"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type Lent, type Model, type Native, type Spending } from "@yaks/model";
|
|
2
|
+
import { type Credential } from "./credential.js";
|
|
3
|
+
import type { ImageGeneration, Images } from "./images.js";
|
|
4
|
+
import type { TransportCredential } from "./transport.js";
|
|
5
|
+
/** How the harness signs in to OpenAI over what it was lent. */ export type SignedIn = {
|
|
6
|
+
credential: () => Promise<Credential>;
|
|
7
|
+
refresh: (stale: TransportCredential) => Promise<Credential>;
|
|
8
|
+
};
|
|
9
|
+
type Key = () => string | undefined;
|
|
10
|
+
/** OpenAI's credential: `OPENAI_API_KEY`, else the person's ChatGPT grant,
|
|
11
|
+
* refreshed through their connection when the Codex backend refuses it. */ export declare let signedIn: (lent: Pick<Lent, 'env' | 'key' | 'refresh'>, own?: string | Key) => SignedIn;
|
|
12
|
+
/** The image tool's settings, or `false` for none. */ export type ImageOptions = {
|
|
13
|
+
tool?: ImageGeneration;
|
|
14
|
+
maxBytes?: number;
|
|
15
|
+
};
|
|
16
|
+
/** The plugin's options in a config. */ export type Options = {
|
|
17
|
+
/** generate images (default on, unless `HARNESS_IMAGES=0`) */ images?: ImageOptions | false;
|
|
18
|
+
/** offer hosted web search (default on, unless `HARNESS_WEB=0`) */ web?: boolean;
|
|
19
|
+
/** allow acknowledged Responses calls to be recovered after a disconnect */ background?: boolean;
|
|
20
|
+
/** an OpenAI-compatible endpoint to call instead of OpenAI's own, opened
|
|
21
|
+
* by `OPENAI_API_KEY`, else the lent `openai` key, else by nothing, which
|
|
22
|
+
* leaves the endpoint to authenticate the call (an AI Gateway's own key) */ base?: string;
|
|
23
|
+
/** headers every request carries */ headers?: Record<string, string>;
|
|
24
|
+
/** a Cloudflare AI Gateway to reach OpenAI through, where no `base` is
|
|
25
|
+
* given: its account and its id, and the token of a gateway that keeps
|
|
26
|
+
* OpenAI's key itself (sent as `cf-aig-authorization`) */ gateway?: {
|
|
27
|
+
account?: string;
|
|
28
|
+
id?: string;
|
|
29
|
+
token?: string;
|
|
30
|
+
};
|
|
31
|
+
/** the API key to call with, before `OPENAI_API_KEY` and the lent key */ key?: string;
|
|
32
|
+
/** the API key for conversational models when images use a separate key */ textKey?: string;
|
|
33
|
+
/** Whether a missing configured key can use an environment key or grant */ credentialFallback?: boolean;
|
|
34
|
+
/** names its own door answers (`native`), each the OpenAI model that
|
|
35
|
+
* answers it */ aliases?: Record<string, string>;
|
|
36
|
+
};
|
|
37
|
+
/** Where the models `providers` makes with `options` call, so a host may
|
|
38
|
+
* send a credential it lends them there and nowhere else. */ export declare let hosts: (options?: Options) => string[];
|
|
39
|
+
/** A single request only needs the key of the model it calls. Keep it
|
|
40
|
+
* under `key` so existing executors can read it. */ export declare let modelOptions: (options: Options, model: string) => Options;
|
|
41
|
+
/** Explicit options override the setting; otherwise images are on. */ export declare let configuredImages: (store: Lent['store'], options: ImageOptions | false | undefined, setting: string | undefined) => Images | undefined;
|
|
42
|
+
/** `openai`, served over what the host lends: a GPT Image model's name
|
|
43
|
+
* answers with an image kept in the lent store (generations.ts), any other
|
|
44
|
+
* with the Responses API. */ export declare let providers: (lent: Lent, options?: Options) => Record<string, Model>;
|
|
45
|
+
/** Its own door (native.ts): one Chat Completions call for each name
|
|
46
|
+
* `aliases` gives an OpenAI model, on the key and endpoint `providers` calls
|
|
47
|
+
* with, held to what `spending` affords. */ export declare let native: (lent: Lent, options: Options, spending: Spending) => Native;
|
|
48
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,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QpB;AAWD;;2BAE2B,GAC3B,OAAO,YAAI,YACT,MAAM,MACN,UAAS,YACR,OAAO,MAAM,EAAE,OAiGjB;AAED;;0CAE0C,GAC1C,OAAO,YAAI,SACT,MAAM,MACN,SAAS,SACT,UAAU,aACT,OAYF"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Hold, type Scope } from "@yaks/fp";
|
|
2
|
+
import { type Images } from "./images.js";
|
|
3
|
+
import { type Item, type MediaReceipts, type Model, type Request } from "@yaks/model";
|
|
4
|
+
import type { Credential } from "./credential.js";
|
|
5
|
+
import { openaiDoc } from "./vocab.js";
|
|
6
|
+
import { type ResponseOptions, type ResponseRequest, type RunOptions, type TransportCredential } from "./transport.js";
|
|
7
|
+
export { openaiDoc };
|
|
8
|
+
export declare let OPENAI_COMP: string;
|
|
9
|
+
/** How the model is reached. */ export type Options = Omit<ResponseOptions, 'credentials'> & RunOptions & {
|
|
10
|
+
credential: () => Credential | Promise<Credential>;
|
|
11
|
+
/** Provider component used for acknowledgements, marks and private receipts. */ provider?: string;
|
|
12
|
+
/** Retry a rejected credential once with a fresh bearer. */ refresh?: (stale: TransportCredential) => Credential | Promise<Credential>;
|
|
13
|
+
/** Configure native image storage and offer the tool on every ask. */ images?: Images;
|
|
14
|
+
/** Private provider results retained before artifact conversion or delivery. */ receipts?: MediaReceipts;
|
|
15
|
+
/** Offer provider-hosted web search, page opening, and find-in-page. */ web?: boolean;
|
|
16
|
+
/** Let an acknowledged generation finish after its stream disconnects. */ background?: boolean;
|
|
17
|
+
/** The account whose ChatGPT plan the Codex backend's limit readings are
|
|
18
|
+
* of (@yaks/model `accountEid`). */ account?: string;
|
|
19
|
+
/** who may share a read under way (@yaks/model `Lent.scope`) */ scope?: Scope;
|
|
20
|
+
/** keeps a cancel sent after its request alive (@yaks/model `Lent.hold`) */ hold?: Hold;
|
|
21
|
+
/** How long the endpoint keeps a cached prefix, in seconds: each reply's
|
|
22
|
+
* mark records the request's start plus this as its `cache_expires_at`.
|
|
23
|
+
* Unknown where absent. */ retention?: number | 'openai-default';
|
|
24
|
+
};
|
|
25
|
+
type Frame = Record<string, unknown>;
|
|
26
|
+
/** A request's items as Responses input items. */ export declare let input: (items: Item[]) => unknown[];
|
|
27
|
+
/** The whole request body. */ export declare let body: (req: Request, store?: boolean, images?: Images, web?: boolean) => ResponseRequest;
|
|
28
|
+
/** Completed output items as neutral items; reasoning ({@link thought}) and
|
|
29
|
+
* anything else the API adds are not part of the conversation and are
|
|
30
|
+
* dropped. */ export declare let items: (done: Frame[]) => Item[];
|
|
31
|
+
/**
|
|
32
|
+
* A {@link Model} over the Responses API.
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* import { responses } from '@yaks/openai'
|
|
36
|
+
*
|
|
37
|
+
* let model = responses({
|
|
38
|
+
* credential: () => ({ token: 'key', base: 'https://api.openai.com/v1' }),
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* A refusal the API named (a 4xx, a failed response), a missing credential, and
|
|
43
|
+
* a connection that was never established are all {@link ModelError}s; the
|
|
44
|
+
* server records those as errors and carries on. Anything else thrown is a bug.
|
|
45
|
+
*
|
|
46
|
+
* The model writes `openai{response_id}` on every reply it returns, and returns
|
|
47
|
+
* that id as an anchor only when `store` is on: the Codex backend stores
|
|
48
|
+
* nothing, so through it a caller replays the conversation and the id is only a
|
|
49
|
+
* record, not something to continue from.
|
|
50
|
+
*/ export declare let responses: (opts: Options) => Model;
|
|
51
|
+
//# sourceMappingURL=responses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"responses.d.ts","sources":["../responses.ts"],"names":[],"mappings":"AAAA,SAAS,KAAK,IAAI,EAAE,KAAK,KAAK,wBAA6C;AAI3E,SAA0B,KAAK,MAAM,sBAAqB;AAI1D,SAIE,KAAK,IAAI,EAIT,KAAK,aAAa,EAClB,KAAK,KAAK,EAKV,KAAK,OAAO,2BAIiB;AAC/B,cAAc,UAAU,0BAAyB;AACjD,SAAS,SAAS,qBAAoC;AAGtD,SAGE,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,UAAU,EAEf,KAAK,mBAAmB,yBACH;AAEvB,SAAS,SAAS,GAAE;AAEpB,OAAO,YAAI,oBAAsB;AAEjC,8BAA8B,GAC9B,YAAY,UAAU,KAAK,iBAAiB,iBAAiB;EAC3D,kBAAkB,aAAa,QAAQ;EACvC,8EAA8E,GAC9E,WAAW,MAAM;EACjB,0DAA0D,GAC1D,WAAW,OAAO,wBAAwB,aAAa,QAAQ;EAC/D,oEAAoE,GACpE,SAAS;EACT,8EAA8E,GAC9E,WAAW;EACX,sEAAsE,GACtE,MAAM,OAAO;EACb,wEAAwE,GACxE,aAAa,OAAO;EACpB;oCACkC,GAClC,UAAU,MAAM;EAChB,8DAA8D,GAC9D,QAAQ;EACR,0EAA0E,GAC1E,OAAO;EACP;;2BAEyB,GACzB,YAAY,MAAM,GAAG;;KAGlB,QAAQ,OAAO,MAAM,EAAE,OAAO;AA8DnC,gDAAgD,GAChD,OAAO,YAAI,QAAS,OAAO,WAAS,OAAO,GACP;AA0GpC,4BAA4B,GAC5B,OAAO,YAAI,OACT,KAAK,SACL,iBACA,SAAS,QACT,kBACC,gBA6BF;AAgCD;;YAEY,GACZ,OAAO,YAAI,QAAS,MAAM,YAAU,OAsBnC;AAED;;;;;;;;;;;;;;;;;;;CAmBC,GACD,OAAO,YAAI,YAAa,MAAM,YAAU,MAqCvC"}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { type Clock } from "@yaks/fp";
|
|
2
|
+
import { type Credential as ProviderCredential } from "./credential.js";
|
|
3
|
+
/** A bearer; omitted base uses the public API (or the transport's base). */ export type TransportCredential = Omit<ProviderCredential, 'base'> & {
|
|
4
|
+
base?: string;
|
|
5
|
+
};
|
|
6
|
+
export type CredentialSource = {
|
|
7
|
+
get: () => TransportCredential | Promise<TransportCredential>;
|
|
8
|
+
refresh?: (stale: TransportCredential) => TransportCredential | Promise<TransportCredential>;
|
|
9
|
+
/** A safe, caller-authored recovery instruction; never a loader exception. */ hint?: string;
|
|
10
|
+
};
|
|
11
|
+
/** Stable credential fault text for callers that schedule sign-in recovery. */ export declare let CREDENTIAL_FAULT: string;
|
|
12
|
+
export type ResponseRequest = {
|
|
13
|
+
model: string;
|
|
14
|
+
input: unknown;
|
|
15
|
+
[name: string]: unknown;
|
|
16
|
+
};
|
|
17
|
+
export type ResponseEvent = {
|
|
18
|
+
type: string;
|
|
19
|
+
[name: string]: unknown;
|
|
20
|
+
};
|
|
21
|
+
export type ResponseItem = {
|
|
22
|
+
type: string;
|
|
23
|
+
[name: string]: unknown;
|
|
24
|
+
};
|
|
25
|
+
export type ResponseUsage = {
|
|
26
|
+
input: number;
|
|
27
|
+
cached: number;
|
|
28
|
+
output: number;
|
|
29
|
+
reasoning: number;
|
|
30
|
+
raw: Record<string, unknown>;
|
|
31
|
+
};
|
|
32
|
+
export type RateLimits = Record<string, string>;
|
|
33
|
+
export type ResponseResult = {
|
|
34
|
+
model: string;
|
|
35
|
+
items: ResponseItem[];
|
|
36
|
+
unknown: ResponseEvent[];
|
|
37
|
+
unknownItems: ResponseItem[];
|
|
38
|
+
usage?: ResponseUsage;
|
|
39
|
+
response: Record<string, unknown>;
|
|
40
|
+
limits: RateLimits;
|
|
41
|
+
/** The response stopped at `max_output_tokens`: its last item is unfinished. */ cut?: boolean;
|
|
42
|
+
};
|
|
43
|
+
export type ResponseFault = Error & {
|
|
44
|
+
status?: number;
|
|
45
|
+
code?: string;
|
|
46
|
+
retry?: {
|
|
47
|
+
after: number;
|
|
48
|
+
};
|
|
49
|
+
/** how long the provider asked to wait, in milliseconds, when it answered */ wait?: number;
|
|
50
|
+
limits?: RateLimits;
|
|
51
|
+
evidence?: ResponseEvent[];
|
|
52
|
+
items?: ResponseItem[];
|
|
53
|
+
/** what the provider sent, redacted: an error status's body, or the event
|
|
54
|
+
* that ended the stream */ body?: string;
|
|
55
|
+
/** the usage the event that ended the stream reported, as OpenAI sent it */ usage?: unknown;
|
|
56
|
+
};
|
|
57
|
+
/** HTTP-edge policy. No environment, filesystem, or Tasks dependencies. */ export type ResponseOptions = {
|
|
58
|
+
/** Keep response ids usable as anchors on the public API. Default false. */ store?: boolean;
|
|
59
|
+
/** Scrub all credentials held by this client from frames and diagnostics.
|
|
60
|
+
* Default false; enable at a boundary that records provider output. */ redact?: boolean;
|
|
61
|
+
credentials: CredentialSource;
|
|
62
|
+
authentication?: 'required' | 'optional';
|
|
63
|
+
base?: string;
|
|
64
|
+
fetch?: typeof fetch;
|
|
65
|
+
headers?: Record<string, string>;
|
|
66
|
+
/** Additional attempts for credential loads and transient HTTP failures;
|
|
67
|
+
* default 2 (three total attempts). Wire backoff is 1s, 4s, then capped at
|
|
68
|
+
* 60s; Retry-After may extend it, up to 60s. */ retries?: number;
|
|
69
|
+
/** How long a transient HTTP failure may go on being retried once `retries`
|
|
70
|
+
* is spent — a backend outage fails every attempt the same way, and five
|
|
71
|
+
* seconds of patience is not an outage. 0 (the default) stops at `retries`;
|
|
72
|
+
* the wait is the same backoff, so the total is wall-clock, not attempts. */ patienceMs?: number;
|
|
73
|
+
/** How a backoff waits; default a sleep on `clock`. */ pause?: (ms: number) => Promise<void>;
|
|
74
|
+
/** The time the watchdog, backoff, Retry-After dates and `reach`'s timeout
|
|
75
|
+
* keep. Default the runtime's (@yaks/fp `system`). */ clock?: Clock;
|
|
76
|
+
id?: () => string;
|
|
77
|
+
/** Replace default body shaping for compatible providers. */ shape?: (request: ResponseRequest) => Record<string, unknown>;
|
|
78
|
+
/** Abort a connection, first frame, or inter-frame gap with no progress for
|
|
79
|
+
* this long, in milliseconds; default @yaks/model/stall `STALL_TIMEOUT`.
|
|
80
|
+
* 0 disables the watchdog. */ stallMs?: number;
|
|
81
|
+
};
|
|
82
|
+
/** Per-exchange cancellation and a hook for every parsed frame (after redaction). */ export type RunOptions = {
|
|
83
|
+
/** Await durable submission immediately before POST. Its presence forbids
|
|
84
|
+
* replay, including authentication refresh, after an uncertain outcome. */ submit?: (request: ResponseRequest) => unknown;
|
|
85
|
+
/** Do not replay once callers expose partial output. Checked after failure. */ noRetry?: boolean | (() => boolean);
|
|
86
|
+
/** Stable conversation affinity. Local Codex probes observed reuse with
|
|
87
|
+
* `session_id`; upstream emits `session-id`. Keep the measured form until
|
|
88
|
+
* an independently warmed comparison justifies changing it. Cache reuse
|
|
89
|
+
* remains the provider's decision, never guaranteed by this header. */ conversation?: string;
|
|
90
|
+
signal?: AbortSignal;
|
|
91
|
+
/** This exchange's own `ResponseOptions.stallMs`. */ stallMs?: number;
|
|
92
|
+
event?: (event: ResponseEvent) => unknown;
|
|
93
|
+
};
|
|
94
|
+
/** An HTTP or connection failure. `kind` is stable even when the provider
|
|
95
|
+
* supplies no code. */ export declare class ResponseError extends Error {
|
|
96
|
+
kind: string;
|
|
97
|
+
status?: number;
|
|
98
|
+
code?: string;
|
|
99
|
+
limits?: RateLimits;
|
|
100
|
+
evidence?: ResponseEvent[];
|
|
101
|
+
items?: ResponseItem[];
|
|
102
|
+
body?: string;
|
|
103
|
+
usage?: unknown;
|
|
104
|
+
constructor(kind: string, message: string);
|
|
105
|
+
/** How long the provider asked to wait, in milliseconds, when it answered. */ wait: number;
|
|
106
|
+
get retry(): {
|
|
107
|
+
after: number;
|
|
108
|
+
} | undefined;
|
|
109
|
+
}
|
|
110
|
+
/** Decode SSE across arbitrary byte chunks, including CRLF and a final frame
|
|
111
|
+
* without a blank line. The reader is released even if a consumer throws. */ export declare let frames: (body: ReadableStream<Uint8Array>) => AsyncGenerator<ResponseEvent>;
|
|
112
|
+
/** The provider-native transport. `run` reads through EOF; `reach` probes
|
|
113
|
+
* connectivity, not authorization. Neither keeps conversation state. */ export declare let transport: (options: ResponseOptions) => {
|
|
114
|
+
run: (value: ResponseRequest, run?: RunOptions) => Promise<ResponseResult>;
|
|
115
|
+
get: (response: string, signal?: AbortSignal) => Promise<Record<string, unknown> | undefined>;
|
|
116
|
+
cancel: (response: string) => Promise<void>;
|
|
117
|
+
/** The input tokens the endpoint counts a request at, its previous
|
|
118
|
+
* response and framing included: what it bills as that request's input. */ count: (value: ResponseRequest, signal?: AbortSignal) => Promise<number>;
|
|
119
|
+
reach: () => Promise<boolean>;
|
|
120
|
+
};
|
|
121
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sources":["../transport.ts"],"names":[],"mappings":"AAGA,SAAS,KAAK,KAAK,wBAA4C;AAC/D,SAAgB,KAAK,cAAc,kBAAkB,0BAAyB;AAI9E,0EAA0E,GAC1E,YAAY,sBAAsB,KAAK,oBAAoB;EACzD,OAAO,MAAM;;AAGf,YAAY;EACV,WAAW,sBAAsB,QAAQ;EACzC,WACE,OAAO,wBACJ,sBAAsB,QAAQ;EACnC,4EAA4E,GAC5E,OAAO,MAAM;;AAGf,6EAA6E,GAC7E,OAAO,YAAI,yBAAsD;AAEjE,YAAY;EACV,OAAO,MAAM;EACb,OAAO,OAAO;GACb,MAAM,MAAM,GAAG,OAAO;;AAGzB,YAAY;EACV,MAAM,MAAM;GACX,MAAM,MAAM,GAAG,OAAO;;AAGzB,YAAY;EACV,MAAM,MAAM;GACX,MAAM,MAAM,GAAG,OAAO;;AAGzB,YAAY;EACV,OAAO,MAAM;EACb,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,WAAW,MAAM;EACjB,KAAK,OAAO,MAAM,EAAE,OAAO;;AAG7B,YAAY,aAAa,OAAO,MAAM,EAAE,MAAM;AAE9C,YAAY;EACV,OAAO,MAAM;EACb,OAAO;EACP,SAAS;EACT,cAAc;EACd,QAAQ;EACR,UAAU,OAAO,MAAM,EAAE,OAAO;EAChC,QAAQ;EACR,8EAA8E,GAC9E,MAAM,OAAO;;AAGf,YAAY,gBAAgB;EAC1B,SAAS,MAAM;EACf,OAAO,MAAM;EACb;IAAU,OAAO,MAAM;;EACvB,2EAA2E,GAC3E,O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ACtE,OAAO,YAAI,YAAa,SAAS;EAC/B,MAAM,OAAO,iBAAiB,MAAM,eAAe,QAAQ;EAC3D,MACE,UAAU,MAAM,EAChB,SAAS,gBACN,QAAQ,OAAO,MAAM,EAAE,OAAO,IAAI,SAAS;EAChD,SAAS,UAAU,MAAM,KAAK,QAAQ,IAAI;EAC1C;2EACyE,GACzE,QAAQ,OAAO,iBAAiB,SAAS,gBAAgB,QAAQ,MAAM;EACvE,aAAa,QAAQ,OAAO;EAsU7B"}
|
package/_dist/vocab.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { VocabDoc } from "@yaks/vocab";
|
|
2
|
+
/** The one comp this provider stamps on an ask it answered:
|
|
3
|
+
* `openai{response_id}`. Load it beside the conversation's vocabulary. */ export declare let openaiDoc: VocabDoc;
|
|
4
|
+
/** The reply an ask's `openai{response_id}` (or `name`'s, for a provider
|
|
5
|
+
* stamping its own) names, which a later request continues from where the
|
|
6
|
+
* provider stored it. */ export declare let responseAnchor: (comps: Record<string, unknown>, name?: string) => string | undefined;
|
|
7
|
+
/** What this package is: its manifest's description, the one place it is
|
|
8
|
+
* written, which a host stamps on each document here. */ export declare let description: string;
|
|
9
|
+
/** Every document this plugin declares. */ export declare let docs: VocabDoc[];
|
|
10
|
+
//# sourceMappingURL=vocab.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.d.ts","sources":["../vocab.ts"],"names":[],"mappings":"AAIA,cAAc,QAAQ,2BAAgC;AAItD;wEACwE,GACxE,OAAO,YAAI,WAAW,SAAc;AAEpC;;uBAEuB,GACvB,OAAO,YAAI,iBACT,OAAO,OAAO,MAAM,EAAE,OAAO,GAC7B,kBACC,MAAM,GAAG,SAAS,CAGpB;AAED;uDACuD,GACvD,OAAO,YAAI,aAAa,MAAM,CAAuB;AAErD,yCAAyC,GACzC,OAAO,YAAI,MAAM,WAAwB"}
|
package/batches.js
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// OpenAI's Batch API as @yaks/model's `Batches`: many Responses requests
|
|
2
|
+
// uploaded as one JSON-lines file and answered within a day at half the
|
|
3
|
+
// price. OpenAI caches a prompt's prefix on its own; requests that name the
|
|
4
|
+
// same `prompt_cache_key` are routed to the same cache, so a prefix every
|
|
5
|
+
// request shares is read cheaply by all but the first few.
|
|
6
|
+
import { ModelError } from "@yaks/model";
|
|
7
|
+
import { OPENAI } from "./credential.js";
|
|
8
|
+
/** A Responses request as one line of a batch's file. */ export let lineOf = (a)=>JSON.stringify({
|
|
9
|
+
custom_id: a.id,
|
|
10
|
+
method: 'POST',
|
|
11
|
+
url: '/v1/responses',
|
|
12
|
+
body: {
|
|
13
|
+
model: a.model,
|
|
14
|
+
instructions: a.system,
|
|
15
|
+
input: a.text,
|
|
16
|
+
max_output_tokens: a.max,
|
|
17
|
+
store: false,
|
|
18
|
+
...a.cache ? {
|
|
19
|
+
prompt_cache_key: a.cache
|
|
20
|
+
} : {}
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* A line of a batch's output as an answer: the response's text and what it
|
|
25
|
+
* used, or why there is none.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { answerOf } from '@yaks/openai/batches'
|
|
29
|
+
* import { assertEquals } from '@std/assert'
|
|
30
|
+
*
|
|
31
|
+
* assertEquals(answerOf({
|
|
32
|
+
* custom_id: 'a',
|
|
33
|
+
* response: {
|
|
34
|
+
* status_code: 200,
|
|
35
|
+
* body: {
|
|
36
|
+
* status: 'completed',
|
|
37
|
+
* output: [{ type: 'message', content: [{ type: 'output_text', text: 'hi' }] }],
|
|
38
|
+
* usage: { input_tokens: 100, input_tokens_details: { cached_tokens: 90 }, output_tokens: 5 },
|
|
39
|
+
* },
|
|
40
|
+
* },
|
|
41
|
+
* }), { id: 'a', text: 'hi', usage: { input_tokens: 100, cached_tokens: 90, output_tokens: 5, reasoning_tokens: 0 } })
|
|
42
|
+
* assertEquals(answerOf({ custom_id: 'b', error: { code: 'x', message: 'no' } }), { id: 'b', error: 'x: no' })
|
|
43
|
+
* ```
|
|
44
|
+
*/ export let answerOf = (l)=>{
|
|
45
|
+
let body = l.response?.body;
|
|
46
|
+
if (l.error || !body || l.response.status_code != 200 || body.error) {
|
|
47
|
+
let e = l.error ?? body?.error ?? {};
|
|
48
|
+
let said = [
|
|
49
|
+
e.code ?? l.response?.status_code,
|
|
50
|
+
e.message
|
|
51
|
+
].filter((s)=>s != null && s !== '');
|
|
52
|
+
return {
|
|
53
|
+
id: l.custom_id,
|
|
54
|
+
error: said.join(': ') || 'no answer'
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
let text = (body.output ?? []).filter((o)=>o.type == 'message').flatMap((o)=>o.content ?? []).filter((c)=>c.type == 'output_text').map((c)=>c.text ?? '').join('');
|
|
58
|
+
let u = body.usage ?? {};
|
|
59
|
+
return {
|
|
60
|
+
id: l.custom_id,
|
|
61
|
+
text,
|
|
62
|
+
usage: {
|
|
63
|
+
input_tokens: u.input_tokens ?? 0,
|
|
64
|
+
cached_tokens: u.input_tokens_details?.cached_tokens ?? 0,
|
|
65
|
+
output_tokens: u.output_tokens ?? 0,
|
|
66
|
+
reasoning_tokens: u.output_tokens_details?.reasoning_tokens ?? 0
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
// How a batch stands once it will change no more.
|
|
71
|
+
let ENDED = [
|
|
72
|
+
'completed',
|
|
73
|
+
'failed',
|
|
74
|
+
'expired',
|
|
75
|
+
'cancelled'
|
|
76
|
+
];
|
|
77
|
+
/**
|
|
78
|
+
* The batches of one key.
|
|
79
|
+
*
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { batches } from '@yaks/openai/batches'
|
|
82
|
+
* import { assertEquals } from '@std/assert'
|
|
83
|
+
*
|
|
84
|
+
* let fetch = () => Promise.resolve(Response.json({ id: 'b1', status: 'finalizing' }))
|
|
85
|
+
* let api = batches({ fetch, key: () => 'k' })
|
|
86
|
+
* assertEquals(await api.ended('b1'), false)
|
|
87
|
+
* ```
|
|
88
|
+
*/ export let batches = (options)=>{
|
|
89
|
+
let base = (options.base ?? OPENAI).replace(/\/$/, '');
|
|
90
|
+
let call = async (path, init = {})=>{
|
|
91
|
+
let response = await (options.fetch ?? fetch)(base + path, {
|
|
92
|
+
...init,
|
|
93
|
+
headers: {
|
|
94
|
+
authorization: `Bearer ${await options.key()}`,
|
|
95
|
+
...typeof init.body == 'string' ? {
|
|
96
|
+
'content-type': 'application/json'
|
|
97
|
+
} : {}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
if (response.ok) return response;
|
|
101
|
+
let said = await response.json().catch(()=>({}));
|
|
102
|
+
throw new ModelError(response.status == 429 ? 'rate' : `http ${response.status}`, `OpenAI ${path.split('/')[1]}: ${said?.error?.message ?? response.statusText}`, response.status == 429 || response.status >= 500 ? {} : undefined);
|
|
103
|
+
};
|
|
104
|
+
let read = async (id)=>await (await call(`/batches/${encodeURIComponent(id)}`)).json();
|
|
105
|
+
let lines = async (file)=>file ? (await (await call(`/files/${encodeURIComponent(file)}/content`)).text()).split('\n').filter((l)=>l.trim()).map((l)=>JSON.parse(l)) : [];
|
|
106
|
+
return {
|
|
107
|
+
submit: async (asked)=>{
|
|
108
|
+
let form = new FormData();
|
|
109
|
+
form.append('purpose', 'batch');
|
|
110
|
+
form.append('file', new Blob([
|
|
111
|
+
asked.map(lineOf).join('\n') + '\n'
|
|
112
|
+
], {
|
|
113
|
+
type: 'application/jsonl'
|
|
114
|
+
}), 'batch.jsonl');
|
|
115
|
+
let file = await (await call('/files', {
|
|
116
|
+
method: 'POST',
|
|
117
|
+
body: form
|
|
118
|
+
})).json();
|
|
119
|
+
let batch = await (await call('/batches', {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
body: JSON.stringify({
|
|
122
|
+
input_file_id: file.id,
|
|
123
|
+
endpoint: '/v1/responses',
|
|
124
|
+
completion_window: '24h'
|
|
125
|
+
})
|
|
126
|
+
})).json();
|
|
127
|
+
return batch.id;
|
|
128
|
+
},
|
|
129
|
+
ended: async (id)=>ENDED.includes((await read(id)).status),
|
|
130
|
+
// Every request's line, from the output file and the error file. A batch
|
|
131
|
+
// refused whole (`failed`) has neither, and its errors say why.
|
|
132
|
+
results: async (id)=>{
|
|
133
|
+
let b = await read(id);
|
|
134
|
+
let all = [
|
|
135
|
+
...await lines(b.output_file_id),
|
|
136
|
+
...await lines(b.error_file_id)
|
|
137
|
+
];
|
|
138
|
+
if (!all.length && b.errors?.data?.length) {
|
|
139
|
+
throw new ModelError('batch failed', `OpenAI batch ${id} ${b.status}: ${b.errors.data.map((e)=>e.message ?? e.code).join('; ')}`);
|
|
140
|
+
}
|
|
141
|
+
return all.map(answerOf);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=batches.js.map
|
package/batches.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batches.js","sources":["./batches.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,wEAAwE;AACxE,4EAA4E;AAC5E,0EAA0E;AAC1E,2DAA2D;AAE3D,SAAS,UAAU,2BAAgC;AACnD,SAAS,MAAM,0BAAyB;AAUxC,uDAAuD,GACvD,OAAO,IAAI,SAAS,CAAC,IACnB,KAAK,SAAS,CAAC;IACb,WAAW,EAAE,EAAE;IACf,QAAQ;IACR,KAAK;IACL,MAAM;MACJ,OAAO,EAAE,KAAK;MACd,cAAc,EAAE,MAAM;MACtB,OAAO,EAAE,IAAI;MACb,mBAAmB,EAAE,GAAG;MACxB,OAAO;MACP,GAAG,EAAE,KAAK,GAAG;QAAE,kBAAkB,EAAE,KAAK;MAAC,IAAI,CAAC,CAAC;IACjD;EACF,GAAE;AAqBJ;;;;;;;;;;;;;;;;;;;;;CAqBC,GACD,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,OAAO,EAAE,QAAQ,EAAE;EACvB,IAA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