@daex-dev/sdk 0.7.1 → 0.8.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/LICENSE +21 -21
- package/README.md +479 -417
- package/dist/{client-C0KQKP8O.d.cts → client-DVZHOtg6.d.cts} +18 -4
- package/dist/{client-C0KQKP8O.d.ts → client-DVZHOtg6.d.ts} +18 -4
- package/dist/connect/browser.cjs +209 -0
- package/dist/connect/browser.d.cts +81 -0
- package/dist/connect/browser.d.ts +81 -0
- package/dist/connect/browser.js +183 -0
- package/dist/connect/index.cjs +1954 -0
- package/dist/connect/index.d.cts +433 -0
- package/dist/connect/index.d.ts +433 -0
- package/dist/connect/index.js +1888 -0
- package/dist/electron/index.d.cts +2 -2
- package/dist/electron/index.d.ts +2 -2
- package/dist/index.cjs +130 -80
- package/dist/index.d.cts +11 -8
- package/dist/index.d.ts +11 -8
- package/dist/index.js +130 -80
- package/dist/server.cjs +1 -1
- package/dist/server.d.cts +1 -1
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/types-DUGv-9QF.d.cts +166 -0
- package/dist/types-DUGv-9QF.d.ts +166 -0
- package/package.json +111 -93
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { C as ConnectProviderInfo, b as ConnectProviderId, h as CursorCredential, i as ConnectCredential, a as ConnectSignInStatus, j as CodexCredential, k as ConnectRequest, e as ConnectEvent, c as ConnectModel } from '../types-DUGv-9QF.js';
|
|
2
|
+
export { d as ConnectBrowserRequest, l as ConnectDoneEvent, f as ConnectMessage, g as ConnectResponseFormat } from '../types-DUGv-9QF.js';
|
|
3
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Errors thrown by `@daex-dev/sdk/connect`. Every one carries a stable code an
|
|
7
|
+
* app can switch on, and never a token: messages are safe to log and show.
|
|
8
|
+
*/
|
|
9
|
+
declare const CONNECT_ERROR_CODES: readonly ["not_signed_in", "sign_in_required", "sign_in_failed", "sign_in_cancelled", "plan_not_eligible", "plan_usage_disabled", "usage_limit", "provider_unavailable", "unsupported_request", "provider_error", "provider_sdk_missing", "invalid_credential", "invalid_request", "cancelled"];
|
|
10
|
+
/**
|
|
11
|
+
* - `not_signed_in`: the user never connected this provider.
|
|
12
|
+
* - `sign_in_required`: the saved sign-in stopped working (revoked, expired,
|
|
13
|
+
* refresh refused). Ask the user to sign in again.
|
|
14
|
+
* - `sign_in_failed` / `sign_in_cancelled`: a sign-in attempt did not finish.
|
|
15
|
+
* - `plan_not_eligible`: the user's plan cannot be used here (e.g. ChatGPT Free).
|
|
16
|
+
* - `plan_usage_disabled`: the user signed in but did not allow plan usage.
|
|
17
|
+
* - `usage_limit`: the user's plan limit is used up; `manageUrl` says where to look.
|
|
18
|
+
* - `provider_unavailable`: the provider is down or busy; retry later.
|
|
19
|
+
* - `unsupported_request`: the provider refused something in the request.
|
|
20
|
+
* - `provider_error`: any other provider failure.
|
|
21
|
+
* - `provider_sdk_missing`: an optional provider package is not installed.
|
|
22
|
+
* - `invalid_credential`: a stored or imported credential is malformed.
|
|
23
|
+
* - `invalid_request`: the caller passed something this module cannot use.
|
|
24
|
+
* - `cancelled`: the caller's signal stopped the request.
|
|
25
|
+
*/
|
|
26
|
+
type ConnectErrorCode = (typeof CONNECT_ERROR_CODES)[number];
|
|
27
|
+
type ConnectErrorDetails = {
|
|
28
|
+
provider?: string;
|
|
29
|
+
/** True when the same call may succeed later without user action. */
|
|
30
|
+
retryable?: boolean;
|
|
31
|
+
/** Where the user manages the plan or usage this error is about. */
|
|
32
|
+
manageUrl?: string;
|
|
33
|
+
httpStatus?: number;
|
|
34
|
+
/** The provider's own error code, verbatim. */
|
|
35
|
+
providerCode?: string;
|
|
36
|
+
requestId?: string;
|
|
37
|
+
cause?: unknown;
|
|
38
|
+
};
|
|
39
|
+
declare class ConnectError extends Error {
|
|
40
|
+
readonly code: ConnectErrorCode;
|
|
41
|
+
readonly provider?: string;
|
|
42
|
+
readonly retryable: boolean;
|
|
43
|
+
readonly manageUrl?: string;
|
|
44
|
+
readonly httpStatus?: number;
|
|
45
|
+
readonly providerCode?: string;
|
|
46
|
+
readonly requestId?: string;
|
|
47
|
+
constructor(code: ConnectErrorCode, message: string, details?: ConnectErrorDetails);
|
|
48
|
+
/** A plain, token-free object for an HTTP response body. */
|
|
49
|
+
toJSON(): Record<string, unknown>;
|
|
50
|
+
}
|
|
51
|
+
/** The error every module throws when the caller's signal stopped a request. */
|
|
52
|
+
declare function cancelledError(provider: string): ConnectError;
|
|
53
|
+
declare function isConnectError(value: unknown): value is ConnectError;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The providers `@daex-dev/sdk/connect` can sign a user in to, and the check on
|
|
57
|
+
* a request's conversation. A new provider starts here: the provider ID list,
|
|
58
|
+
* routes and messages all derive from `CONNECT_PROVIDERS`. Credential checks
|
|
59
|
+
* are in `credentials.ts`.
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
declare const CONNECT_PROVIDERS: readonly ConnectProviderInfo[];
|
|
63
|
+
declare const CONNECT_PROVIDER_IDS: readonly ConnectProviderId[];
|
|
64
|
+
declare function isConnectProviderId(value: unknown): value is ConnectProviderId;
|
|
65
|
+
declare function connectProvider(id: ConnectProviderId): ConnectProviderInfo;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Checks on a stored or uploaded credential: shape, usability and the status
|
|
69
|
+
* an app may show. Never returns a secret in a status.
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/** Validate a credential that crossed a boundary (storage, an upload). */
|
|
73
|
+
declare function parseConnectCredential(value: unknown): ConnectCredential;
|
|
74
|
+
/** A Cursor key works until it expires; there is no renewal. */
|
|
75
|
+
declare function isCursorUsable(credential: CursorCredential, now?: number): boolean;
|
|
76
|
+
declare function signInStatus(credential: ConnectCredential | null, provider: ConnectProviderId, now?: number): ConnectSignInStatus;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The server's copy of each user's sign-ins: one sealed record per user and
|
|
80
|
+
* provider, in whatever store the app already has.
|
|
81
|
+
*
|
|
82
|
+
* - Sealed with AES-256-GCM under a key derived from the app's secret. The
|
|
83
|
+
* storage key is bound in as associated data, so a record copied to another
|
|
84
|
+
* user's key fails to open.
|
|
85
|
+
* - Storage keys hold a SHA-256 of the user ID, never the ID or an email.
|
|
86
|
+
* - The vault is the only refresher. A ChatGPT refresh token rotates on use, so
|
|
87
|
+
* two refreshers would sign each other out. Refreshes, saves and sign-outs of
|
|
88
|
+
* one sign-in run one at a time in this process, and across processes when
|
|
89
|
+
* the storage has `lock`.
|
|
90
|
+
* - A refresh OpenAI refuses for good leaves a signed-out record that keeps the
|
|
91
|
+
* issued client ID, so the user's next sign-in reuses their registration.
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
/** Plug in any key-value store: a database table, Redis, KV, files. */
|
|
95
|
+
interface ConnectStorage {
|
|
96
|
+
get(key: string): Promise<string | null>;
|
|
97
|
+
set(key: string, value: string, options?: {
|
|
98
|
+
ttlMs?: number;
|
|
99
|
+
}): Promise<void>;
|
|
100
|
+
delete(key: string): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Optional: run `fn` while holding `key` across every server instance. Add it
|
|
103
|
+
* when more than one process serves the same users; without it, refreshes are
|
|
104
|
+
* serialized only inside one process.
|
|
105
|
+
*/
|
|
106
|
+
lock?<T>(key: string, fn: () => Promise<T>): Promise<T>;
|
|
107
|
+
}
|
|
108
|
+
/** An in-memory store, for development and tests. Lost on restart. */
|
|
109
|
+
declare function memoryStorage(): ConnectStorage;
|
|
110
|
+
type ConnectVaultOptions = {
|
|
111
|
+
storage: ConnectStorage;
|
|
112
|
+
/** At least 32 bytes, kept in the server's secrets. Changing it makes stored sign-ins unreadable. */
|
|
113
|
+
secret: string | Uint8Array;
|
|
114
|
+
/** Refresh a ChatGPT token this long before it expires. Default 5 minutes. */
|
|
115
|
+
refreshAheadMs?: number;
|
|
116
|
+
fetch?: typeof fetch;
|
|
117
|
+
};
|
|
118
|
+
type ConnectVault = {
|
|
119
|
+
save(userId: string, credential: ConnectCredential): Promise<void>;
|
|
120
|
+
load(userId: string, provider: ConnectProviderId): Promise<ConnectCredential | null>;
|
|
121
|
+
/**
|
|
122
|
+
* Forget a sign-in. ChatGPT sign-ins are also revoked at OpenAI unless
|
|
123
|
+
* `revoke: false`; `revoked` says whether OpenAI confirmed it (null when not
|
|
124
|
+
* asked or nothing to revoke).
|
|
125
|
+
*/
|
|
126
|
+
remove(userId: string, provider: ConnectProviderId, options?: {
|
|
127
|
+
revoke?: boolean;
|
|
128
|
+
}): Promise<{
|
|
129
|
+
revoked: boolean | null;
|
|
130
|
+
}>;
|
|
131
|
+
/** What the app may show, for every provider. Never contains a secret. */
|
|
132
|
+
status(userId: string): Promise<ConnectSignInStatus[]>;
|
|
133
|
+
/**
|
|
134
|
+
* A credential that is safe to use right now: refreshed first when close to
|
|
135
|
+
* expiry (or when `forceRefresh`), and saved before it is returned.
|
|
136
|
+
*/
|
|
137
|
+
fresh(userId: string, provider: "codex", options?: {
|
|
138
|
+
forceRefresh?: boolean;
|
|
139
|
+
}): Promise<CodexCredential>;
|
|
140
|
+
fresh(userId: string, provider: "cursor", options?: {
|
|
141
|
+
forceRefresh?: boolean;
|
|
142
|
+
}): Promise<CursorCredential>;
|
|
143
|
+
fresh(userId: string, provider: ConnectProviderId, options?: {
|
|
144
|
+
forceRefresh?: boolean;
|
|
145
|
+
}): Promise<ConnectCredential>;
|
|
146
|
+
/** The saved registration for a returning ChatGPT user, for `startCodexSignIn({ reauth })`. */
|
|
147
|
+
codexReauth(userId: string): Promise<{
|
|
148
|
+
clientId: string;
|
|
149
|
+
idTokenHint?: string;
|
|
150
|
+
loginHint?: string;
|
|
151
|
+
} | undefined>;
|
|
152
|
+
/** Short-lived sealed values (pending sign-ins). */
|
|
153
|
+
putTemporary(id: string, value: unknown, ttlMs: number): Promise<void>;
|
|
154
|
+
readTemporary<T>(id: string): Promise<T | null>;
|
|
155
|
+
deleteTemporary(id: string): Promise<void>;
|
|
156
|
+
};
|
|
157
|
+
declare function createConnectVault(options: ConnectVaultOptions): ConnectVault;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* AI calls on a user's own plan, for any provider in the vault:
|
|
161
|
+
*
|
|
162
|
+
* const ai = createConnectAI({ vault });
|
|
163
|
+
* const reply = ai.respond(userId, { provider: "codex", prompt: "Summarize this note: …" });
|
|
164
|
+
* for await (const event of reply) if (event.type === "text") write(event.delta);
|
|
165
|
+
* // or: const text = await reply.text();
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
type ConnectResponse = AsyncIterable<ConnectEvent> & {
|
|
169
|
+
/** The whole reply text, once it finished. */
|
|
170
|
+
text(): Promise<string>;
|
|
171
|
+
};
|
|
172
|
+
type ConnectAI = {
|
|
173
|
+
respond(userId: string, request: ConnectRequest): ConnectResponse;
|
|
174
|
+
models(userId: string, provider: ConnectProviderId): Promise<ConnectModel[]>;
|
|
175
|
+
};
|
|
176
|
+
type ConnectAIOptions = {
|
|
177
|
+
vault: ConnectVault;
|
|
178
|
+
fetch?: typeof fetch;
|
|
179
|
+
};
|
|
180
|
+
declare function createConnectAI(options: ConnectAIOptions): ConnectAI;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Drop-in HTTP routes for "Sign in with <provider>" and AI calls, as a Fetch
|
|
184
|
+
* API handler (Request in, Response out). Works with Next.js route handlers,
|
|
185
|
+
* Hono, Bun, Deno and workers directly, and with Node's http/Express through
|
|
186
|
+
* `toNodeListener`.
|
|
187
|
+
*
|
|
188
|
+
* Routes, under `basePath` (default `/connect`):
|
|
189
|
+
*
|
|
190
|
+
* GET /providers providers + this user's sign-in status
|
|
191
|
+
* POST /codex/start { signInId, url } open `url` in the browser
|
|
192
|
+
* POST /codex/complete { signInId, callbackUrl } the address the browser ended on
|
|
193
|
+
* POST /codex/import { credential } a sign-in made on the user's device
|
|
194
|
+
* POST /cursor/start { signInId, url } open `url`; the server waits
|
|
195
|
+
* GET /cursor/status?signInId= { state: "pending" | "done" | "failed" }
|
|
196
|
+
* POST /{provider}/sign-out
|
|
197
|
+
* GET /models?provider=
|
|
198
|
+
* POST /respond a `ConnectBrowserRequest`
|
|
199
|
+
* → text/event-stream (`text`, `raw`, `done`, `error`), or JSON
|
|
200
|
+
* ({ text, model, usage, response }) for `Accept: application/json`
|
|
201
|
+
*
|
|
202
|
+
* Every route needs `getUserId` to name the signed-in user of the app, and
|
|
203
|
+
* every POST must send `Content-Type: application/json` (a cross-site form
|
|
204
|
+
* cannot, so it cannot act for a signed-in user).
|
|
205
|
+
*/
|
|
206
|
+
|
|
207
|
+
type ConnectHandlerOptions = {
|
|
208
|
+
vault: ConnectVault;
|
|
209
|
+
/** Reuse an existing `createConnectAI`; one is made from the vault otherwise. */
|
|
210
|
+
ai?: ConnectAI;
|
|
211
|
+
/** Shown on the provider's consent screen and as the Cursor key's name. */
|
|
212
|
+
appName: string;
|
|
213
|
+
/** This server's stable host ID (`newCodexHostId()`, generated once and kept). */
|
|
214
|
+
hostId: string;
|
|
215
|
+
/** The app's own signed-in user for a request, or null when nobody is signed in. */
|
|
216
|
+
getUserId(request: Request): string | null | Promise<string | null>;
|
|
217
|
+
basePath?: string;
|
|
218
|
+
/** Override for the ChatGPT redirect (`http://127.0.0.1:<port>/auth/callback`). */
|
|
219
|
+
codexRedirectUri?: string;
|
|
220
|
+
/**
|
|
221
|
+
* Shape every `/respond` call before it runs: set the model, effort, format,
|
|
222
|
+
* raw `codex` fields, tools or Cursor agent options yourself, add your own
|
|
223
|
+
* instructions, or throw a `ConnectError` to refuse. Gets the request built
|
|
224
|
+
* from the body (a `ConnectBrowserRequest`: no `codex`, `cursor.agent` or
|
|
225
|
+
* `cursor.send`) and the raw body, so it can pass any of those on.
|
|
226
|
+
*/
|
|
227
|
+
prepareRequest?(request: ConnectRequest, context: {
|
|
228
|
+
userId: string;
|
|
229
|
+
body: Record<string, unknown>;
|
|
230
|
+
httpRequest: Request;
|
|
231
|
+
}): ConnectRequest | Promise<ConnectRequest>;
|
|
232
|
+
/**
|
|
233
|
+
* Send raw provider events and the provider's final `response` object to the
|
|
234
|
+
* browser. Off by default: they echo the request, including instructions and
|
|
235
|
+
* tools that `prepareRequest` added on the server. Text, model and usage are
|
|
236
|
+
* always sent.
|
|
237
|
+
*/
|
|
238
|
+
exposeProviderDetails?: boolean;
|
|
239
|
+
fetch?: typeof fetch;
|
|
240
|
+
};
|
|
241
|
+
/** Returns null for a path outside `basePath`, so the app can route it elsewhere. */
|
|
242
|
+
type ConnectHandler = ((request: Request) => Promise<Response | null>) & {
|
|
243
|
+
/** Where the routes live; `toNodeListener` skips other paths without reading their body. */
|
|
244
|
+
readonly basePath: string;
|
|
245
|
+
};
|
|
246
|
+
declare function createConnectHandler(options: ConnectHandlerOptions): ConnectHandler;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Mount a `ConnectHandler` on Node's `http` server or Express:
|
|
250
|
+
*
|
|
251
|
+
* http.createServer(toNodeListener(handler))
|
|
252
|
+
* app.use(toNodeListener(handler)) // other paths go to next(), body untouched
|
|
253
|
+
* app.use("/connect", toNodeListener(handler)) // mounted at its own path
|
|
254
|
+
* app.use("/api", toNodeListener(handler)) // under a prefix: routes at /api/connect/*
|
|
255
|
+
*
|
|
256
|
+
* A request outside the handler's `basePath` is passed on before its body is
|
|
257
|
+
* read. A body a parser such as `express.json()` already read is reused. The
|
|
258
|
+
* reply is written as fast as the client takes it, and stops when it leaves.
|
|
259
|
+
*/
|
|
260
|
+
|
|
261
|
+
type NodeListenerOptions = {
|
|
262
|
+
/** Largest request body accepted, in bytes. Default 1 MiB. */
|
|
263
|
+
maxBodyBytes?: number;
|
|
264
|
+
};
|
|
265
|
+
type ExpressRequest = IncomingMessage & {
|
|
266
|
+
originalUrl?: string;
|
|
267
|
+
body?: unknown;
|
|
268
|
+
};
|
|
269
|
+
declare function toNodeListener(handler: ConnectHandler, options?: NodeListenerOptions): (req: ExpressRequest, res: ServerResponse, next?: (error?: unknown) => void) => Promise<void>;
|
|
270
|
+
|
|
271
|
+
type CodexCallOptions = {
|
|
272
|
+
fetch?: typeof fetch;
|
|
273
|
+
signal?: AbortSignal;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* ChatGPT plan usage through OpenAI's Sign in with ChatGPT ("token sharing").
|
|
278
|
+
*
|
|
279
|
+
* Protocol: https://developers.openai.com/siwc/token-sharing-open-source
|
|
280
|
+
* - Authorization Code + PKCE (S256), public client, no secret. The first sign-in
|
|
281
|
+
* registers with `client_id=dynamic_agent_client`; the callback returns the
|
|
282
|
+
* issued client ID (`oaiapp_…`), which every later call uses.
|
|
283
|
+
* - The redirect is a loopback URL on `127.0.0.1` (never `localhost`). The port
|
|
284
|
+
* may change between sign-ins; scheme, host and path may not.
|
|
285
|
+
* - Access tokens last 1 hour; refresh tokens last 30 days and rotate on every
|
|
286
|
+
* refresh, so exactly one holder may refresh a sign-in (the vault serializes it).
|
|
287
|
+
* - Inference is `POST /v1/responses` only, with `store: false` and `stream: true`;
|
|
288
|
+
* a stream that ends without `response.completed` failed.
|
|
289
|
+
*
|
|
290
|
+
* Every function here takes and returns plain values, so a server can keep a
|
|
291
|
+
* pending sign-in between "start" and "complete" in any store it likes.
|
|
292
|
+
* Inference is in `codexApi.ts`.
|
|
293
|
+
*/
|
|
294
|
+
|
|
295
|
+
declare const CODEX_DEFAULT_REDIRECT_URI = "http://127.0.0.1:1455/auth/callback";
|
|
296
|
+
/** A pending sign-in older than this is refused. */
|
|
297
|
+
declare const CODEX_SIGN_IN_TTL_MS: number;
|
|
298
|
+
/** Everything needed to finish a sign-in. Secret: keep it server-side or on the device. */
|
|
299
|
+
type CodexPendingSignIn = {
|
|
300
|
+
provider: "codex";
|
|
301
|
+
state: string;
|
|
302
|
+
nonce: string;
|
|
303
|
+
verifier: string;
|
|
304
|
+
redirectUri: string;
|
|
305
|
+
hostId: string;
|
|
306
|
+
/** Set on a returning sign-in: the client ID issued at registration. */
|
|
307
|
+
clientId?: string;
|
|
308
|
+
createdAt: number;
|
|
309
|
+
};
|
|
310
|
+
type StartCodexSignInOptions = {
|
|
311
|
+
/** Shown to the user on OpenAI's consent screen. */
|
|
312
|
+
appName: string;
|
|
313
|
+
/**
|
|
314
|
+
* A stable ID for the host that will hold this sign-in (`newCodexHostId()`).
|
|
315
|
+
* Keep one per server (or per device) and reuse it for every user.
|
|
316
|
+
*/
|
|
317
|
+
hostId: string;
|
|
318
|
+
/** `http://127.0.0.1:<port>/auth/callback`. Defaults to port 1455. */
|
|
319
|
+
redirectUri?: string;
|
|
320
|
+
/** A returning user: reuse their issued client ID (from a signed-out credential). */
|
|
321
|
+
reauth?: {
|
|
322
|
+
clientId: string;
|
|
323
|
+
idTokenHint?: string;
|
|
324
|
+
loginHint?: string;
|
|
325
|
+
};
|
|
326
|
+
now?: number;
|
|
327
|
+
};
|
|
328
|
+
/** A new stable host ID, `urn:uuid:<uuid>`. Generate once per host and store it. */
|
|
329
|
+
declare function newCodexHostId(): string;
|
|
330
|
+
/** Build the URL to open in the user's browser, and the state to finish with. */
|
|
331
|
+
declare function startCodexSignIn(options: StartCodexSignInOptions): Promise<{
|
|
332
|
+
url: string;
|
|
333
|
+
pending: CodexPendingSignIn;
|
|
334
|
+
}>;
|
|
335
|
+
/**
|
|
336
|
+
* Finish a sign-in from the browser's callback. Refuses a mismatched state, a
|
|
337
|
+
* denied consent, a stale pending sign-in and a client ID that changed.
|
|
338
|
+
*/
|
|
339
|
+
declare function completeCodexSignIn(pending: CodexPendingSignIn, callback: string | URL | URLSearchParams, options?: CodexCallOptions & {
|
|
340
|
+
now?: number;
|
|
341
|
+
}): Promise<CodexCredential>;
|
|
342
|
+
/**
|
|
343
|
+
* Exchange the refresh token for a new token set. The old refresh token stops
|
|
344
|
+
* working, so save the result before using it, and never refresh one sign-in
|
|
345
|
+
* from two places (`createConnectVault` serializes this for you).
|
|
346
|
+
*/
|
|
347
|
+
declare function refreshCodexCredential(credential: CodexCredential, options?: CodexCallOptions): Promise<CodexCredential>;
|
|
348
|
+
/**
|
|
349
|
+
* Sign out at OpenAI (revokes the refresh token). Resolves `true` once OpenAI
|
|
350
|
+
* confirmed (an already-dead token counts), `false` when it could not confirm.
|
|
351
|
+
* The discovery document is fetched once; the revocation request is retried
|
|
352
|
+
* twice on a network failure or a 5xx. Each request gives up after 10 s, and
|
|
353
|
+
* the caller's signal stops it all. Never throws.
|
|
354
|
+
*/
|
|
355
|
+
declare function revokeCodexCredential(credential: CodexCredential, options?: CodexCallOptions): Promise<boolean>;
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* ChatGPT plan usage once signed in: the model list and `POST /v1/responses`
|
|
359
|
+
* as a stream. Plan usage takes `store: false` and `stream: true`; a stream that
|
|
360
|
+
* ends without `response.completed` failed. Sign-in is in `codexAuth.ts`.
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
/** The models this user's plan can use, in OpenAI's order. */
|
|
364
|
+
declare function listCodexModels(credential: CodexCredential, options?: CodexCallOptions): Promise<ConnectModel[]>;
|
|
365
|
+
/**
|
|
366
|
+
* Stream a reply on the user's ChatGPT plan. `model` must be set (see
|
|
367
|
+
* `listCodexModels`); `createConnectAI` picks one when the caller does not.
|
|
368
|
+
*/
|
|
369
|
+
declare function streamCodexResponse(credential: CodexCredential, request: ConnectRequest & {
|
|
370
|
+
model: string;
|
|
371
|
+
}, options?: CodexCallOptions): AsyncGenerator<ConnectEvent>;
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Sign in with ChatGPT on the machine the user is sitting at (a desktop app, a
|
|
375
|
+
* CLI, an Electron main process). Listens on `127.0.0.1` for OpenAI's redirect,
|
|
376
|
+
* so the user never copies anything. Node only.
|
|
377
|
+
*
|
|
378
|
+
* The result is a credential this device holds. To use it from a server, send
|
|
379
|
+
* it there once (`POST …/codex/import` on `createConnectHandler`) and drop the
|
|
380
|
+
* local copy: refresh tokens rotate, so only one holder can keep it alive.
|
|
381
|
+
*/
|
|
382
|
+
|
|
383
|
+
type CodexDeviceSignInOptions = Omit<StartCodexSignInOptions, "redirectUri"> & {
|
|
384
|
+
/** Opens the sign-in page. Without it, use `onUrl` to show the link yourself. */
|
|
385
|
+
openBrowser?: (url: string) => void | Promise<void>;
|
|
386
|
+
/** Always called with the sign-in link before waiting. */
|
|
387
|
+
onUrl?: (url: string) => void;
|
|
388
|
+
/** Preferred port; another free one is used when it is taken. Default 1455. */
|
|
389
|
+
port?: number;
|
|
390
|
+
/** Default 10 minutes. */
|
|
391
|
+
timeoutMs?: number;
|
|
392
|
+
signal?: AbortSignal;
|
|
393
|
+
fetch?: typeof fetch;
|
|
394
|
+
};
|
|
395
|
+
declare function signInWithCodexOnThisDevice(options: CodexDeviceSignInOptions): Promise<CodexCredential>;
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* Cursor through Cursor's own SDK (`@cursor/sdk`, an optional peer dependency).
|
|
399
|
+
*
|
|
400
|
+
* - Sign-in: `Cursor.auth.login()` shows a link, waits while the user signs in
|
|
401
|
+
* in any browser, then mints a named user API key (90 days) that "bills to
|
|
402
|
+
* that user's plan". No loopback, so it runs on a server for a web sign-in:
|
|
403
|
+
* send the link to the page, keep waiting on the server.
|
|
404
|
+
* - Use: a local Cursor agent created with that key, on an in-memory file tree
|
|
405
|
+
* (`tempFs()`), so nothing on the host (files, shell, git) reaches it. Its
|
|
406
|
+
* streamed assistant text is the reply.
|
|
407
|
+
*/
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Hand this module `@cursor/sdk` yourself: `provideCursorSdk(() => import("@cursor/sdk"))`.
|
|
411
|
+
* Needed when a bundler (Next.js, a serverless build) must see the import to
|
|
412
|
+
* ship it, or when `@cursor/sdk` is not resolvable from this package's folder.
|
|
413
|
+
* Without it, the module is imported by name on first use.
|
|
414
|
+
*/
|
|
415
|
+
declare function provideCursorSdk(loader: () => Promise<unknown>): void;
|
|
416
|
+
type CursorSignInOptions = {
|
|
417
|
+
/** The name of the minted key on the user's Cursor API-keys page. */
|
|
418
|
+
appName: string;
|
|
419
|
+
/** Called with the link the user must open. Always called before waiting. */
|
|
420
|
+
onLoginUrl: (url: string) => void;
|
|
421
|
+
/** Opens a browser on this machine. Leave unset on a server. */
|
|
422
|
+
openBrowser?: (url: string) => void | Promise<void>;
|
|
423
|
+
/** Key lifetime in ms. Cursor's default is 90 days. */
|
|
424
|
+
keyTtlMs?: number;
|
|
425
|
+
signal?: AbortSignal;
|
|
426
|
+
};
|
|
427
|
+
/** Run Cursor's sign-in. Resolves once the user finished in the browser. */
|
|
428
|
+
declare function signInWithCursor(options: CursorSignInOptions): Promise<CursorCredential>;
|
|
429
|
+
declare function listCursorModels(credential: CursorCredential): Promise<ConnectModel[]>;
|
|
430
|
+
/** Stream a reply from a Cursor agent on the user's plan. */
|
|
431
|
+
declare function streamCursorResponse(credential: CursorCredential, request: ConnectRequest): AsyncGenerator<ConnectEvent>;
|
|
432
|
+
|
|
433
|
+
export { CODEX_DEFAULT_REDIRECT_URI, CODEX_SIGN_IN_TTL_MS, CONNECT_ERROR_CODES, CONNECT_PROVIDERS, CONNECT_PROVIDER_IDS, type CodexCallOptions, CodexCredential, type CodexDeviceSignInOptions, type CodexPendingSignIn, type ConnectAI, type ConnectAIOptions, ConnectCredential, ConnectError, type ConnectErrorCode, type ConnectErrorDetails, ConnectEvent, type ConnectHandler, type ConnectHandlerOptions, ConnectModel, ConnectProviderId, ConnectProviderInfo, ConnectRequest, type ConnectResponse, ConnectSignInStatus, type ConnectStorage, type ConnectVault, type ConnectVaultOptions, CursorCredential, type CursorSignInOptions, type NodeListenerOptions, type StartCodexSignInOptions, cancelledError, completeCodexSignIn, connectProvider, createConnectAI, createConnectHandler, createConnectVault, isConnectError, isConnectProviderId, isCursorUsable, listCodexModels, listCursorModels, memoryStorage, newCodexHostId, parseConnectCredential, provideCursorSdk, refreshCodexCredential, revokeCodexCredential, signInStatus, signInWithCodexOnThisDevice, signInWithCursor, startCodexSignIn, streamCodexResponse, streamCursorResponse, toNodeListener };
|