@buildwithtrace/sdk 0.1.0 → 0.1.1
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 +19 -3
- package/dist/index.d.mts +170 -3
- package/dist/index.d.ts +170 -3
- package/dist/index.js +614 -42
- package/dist/index.mjs +601 -48
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -9,7 +9,13 @@ npm install @buildwithtrace/sdk
|
|
|
9
9
|
```ts
|
|
10
10
|
import { Trace } from '@buildwithtrace/sdk';
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
// Sign in once via the browser (opens the Trace login page, captcha solved there);
|
|
13
|
+
// credentials persist so a bare `new Trace()` reuses them. `Trace.logout()` clears them.
|
|
14
|
+
const trace = await Trace.login();
|
|
15
|
+
|
|
16
|
+
// Or construct directly — token precedence: apiKey > TRACE_API_KEY env > stored credentials:
|
|
17
|
+
// const trace = new Trace({ apiKey: process.env.TRACE_API_KEY });
|
|
18
|
+
// const trace = new Trace(); // uses credentials stored by Trace.login()/the CLI
|
|
13
19
|
|
|
14
20
|
const sym = await trace.generateSymbol('LM7805 5V voltage regulator');
|
|
15
21
|
sym.save('./symbols/'); // writes LM7805.kicad_sym
|
|
@@ -29,7 +35,12 @@ const res = await trace.chat('Add a 100nF decoupling cap on U1', {
|
|
|
29
35
|
});
|
|
30
36
|
```
|
|
31
37
|
|
|
32
|
-
Get an API key: `buildwithtrace auth token` (
|
|
38
|
+
Get an API key (CI): mint a long-lived Personal Access Token with `buildwithtrace auth token --create --name ci-bot` (`trace_pat_...`, shown once), or in the dashboard (Settings → Developer).
|
|
39
|
+
|
|
40
|
+
Env overrides: `TRACE_API_KEY` (token), `TRACE_BASE_URL` (API base), `TRACE_FRONTEND_URL`
|
|
41
|
+
(login page origin), `TRACE_CONFIG_DIR` (credential store), `TRACE_LLM_PROVIDER` /
|
|
42
|
+
`TRACE_LLM_API_KEY` / `TRACE_LLM_MODEL` (BYOK), `TRACE_NO_ANALYTICS` / `DO_NOT_TRACK`
|
|
43
|
+
(opt out of anonymous usage analytics; also auto-off in CI).
|
|
33
44
|
|
|
34
45
|
## Notes
|
|
35
46
|
|
|
@@ -39,4 +50,9 @@ Get an API key: `buildwithtrace auth token` (CLI) or buildwithtrace.com/dashboar
|
|
|
39
50
|
are not yet ported — those throw `TraceToolExecutionError`; use the `buildwithtrace` CLI for them.
|
|
40
51
|
- `.trace_*` writes succeed, but the matching `.kicad_*` isn't regenerated client-side yet
|
|
41
52
|
(the converter isn't bundled in the Node SDK) — the result says so.
|
|
42
|
-
- BYOK
|
|
53
|
+
- BYOK via per-call `chat(..., { llmProvider, llmApiKey, llmModelId })`, the constructor
|
|
54
|
+
(`new Trace({ llmProvider, llmApiKey, llmModelId })`), or the
|
|
55
|
+
`TRACE_LLM_PROVIDER`/`TRACE_LLM_API_KEY`/`TRACE_LLM_MODEL` env (precedence: per-call >
|
|
56
|
+
constructor > env). BYOK skips Trace's cost cap but NOT the plan gate — `agent`/`plan`
|
|
57
|
+
still require a trial/paid plan even with your own key (`ask` is free); a free account gets
|
|
58
|
+
a `TracePlanRestrictedError`.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser "deeplink" login + file-based credential storage for the Trace SDK.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the Python SDK (`buildwithtrace_sdk`) so the two tools interoperate:
|
|
5
|
+
* the loopback callback contract is identical and credentials persist to the
|
|
6
|
+
* same `credentials.json` filename under an OS config dir.
|
|
7
|
+
*
|
|
8
|
+
* Login flow:
|
|
9
|
+
* 1. Start a loopback HTTP server on 127.0.0.1 on a free port.
|
|
10
|
+
* 2. Open `{FRONTEND}/login?callback=http://localhost:PORT/callback` in the
|
|
11
|
+
* user's browser. The user authenticates in-browser (captcha solved there).
|
|
12
|
+
* 3. The browser redirects to
|
|
13
|
+
* `http://localhost:PORT/callback?token=<ACCESS_JWT>&refresh_token=<RT>&user=<urlencoded JSON>`.
|
|
14
|
+
* The access-token query param is named `token` (NOT `access_token`).
|
|
15
|
+
* 4. We parse the tokens, respond with a "you can close this window" page,
|
|
16
|
+
* close the server, and resolve.
|
|
17
|
+
*
|
|
18
|
+
* Uses only Node built-ins (http, child_process, os, fs, path) — no runtime deps.
|
|
19
|
+
*/
|
|
20
|
+
/** Subset of the Supabase user object the callback URL-encodes into `user`. */
|
|
21
|
+
interface TraceUser {
|
|
22
|
+
id?: string;
|
|
23
|
+
email?: string;
|
|
24
|
+
full_name?: string;
|
|
25
|
+
avatar_url?: string;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
/** Shape persisted to credentials.json (field names mirror the Python SDK). */
|
|
29
|
+
interface StoredCredentials {
|
|
30
|
+
access_token: string;
|
|
31
|
+
refresh_token: string;
|
|
32
|
+
user_data?: TraceUser | null;
|
|
33
|
+
}
|
|
34
|
+
/** Result of a successful browser login. */
|
|
35
|
+
interface BrowserLoginResult {
|
|
36
|
+
accessToken: string;
|
|
37
|
+
refreshToken: string;
|
|
38
|
+
user: TraceUser | null;
|
|
39
|
+
}
|
|
40
|
+
interface BrowserLoginOptions {
|
|
41
|
+
/** API base URL — used to derive the FRONTEND login host. */
|
|
42
|
+
baseUrl?: string;
|
|
43
|
+
/** Explicit frontend origin (e.g. `https://buildwithtrace.com`). Overrides derivation. */
|
|
44
|
+
frontendUrl?: string;
|
|
45
|
+
/** How long to wait for the browser round-trip before rejecting. Default 120s. */
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
/** Inject a custom browser-opener (used in tests). Defaults to the OS opener. */
|
|
48
|
+
open?: (url: string) => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The login page lives on the FRONTEND host, not the API host. Derive it:
|
|
52
|
+
* - `TRACE_FRONTEND_URL` env always wins.
|
|
53
|
+
* - `api.<domain>` -> `https://<domain>` (e.g. api.buildwithtrace.com -> buildwithtrace.com)
|
|
54
|
+
* - localhost / 127.0.0.1 -> `http://localhost:3000` (dev frontend default)
|
|
55
|
+
* - anything else (staging IPs, unknown hosts) -> the production frontend.
|
|
56
|
+
*/
|
|
57
|
+
declare function deriveFrontendUrl(baseUrl?: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* OS config directory for Trace, best-effort matching the Python SDK's
|
|
60
|
+
* `platformdirs.user_config_dir("trace", "buildwithtrace")`:
|
|
61
|
+
* - `TRACE_CONFIG_DIR` env always wins.
|
|
62
|
+
* - macOS: ~/Library/Application Support/trace
|
|
63
|
+
* - Windows: %APPDATA%\buildwithtrace\trace
|
|
64
|
+
* - Linux: $XDG_CONFIG_HOME/trace (else ~/.config/trace)
|
|
65
|
+
*/
|
|
66
|
+
declare function getConfigDir(): string;
|
|
67
|
+
/** Persist tokens to a 0600 credentials.json. Creates the config dir if needed. */
|
|
68
|
+
declare function storeCredentials(creds: StoredCredentials): string;
|
|
69
|
+
/** Read stored credentials, or null if absent/corrupt. */
|
|
70
|
+
declare function readCredentials(): StoredCredentials | null;
|
|
71
|
+
/** The stored access token (a Supabase JWT), or null if not logged in. */
|
|
72
|
+
declare function getStoredAccessToken(): string | null;
|
|
73
|
+
/** Remove stored credentials. No-op if there are none. */
|
|
74
|
+
declare function clearCredentials(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Open a URL in the user's default browser. Cross-platform via the OS opener
|
|
77
|
+
* (macOS `open`, Windows `start`, Linux `xdg-open`). Best-effort: if the spawn
|
|
78
|
+
* fails the caller still prints the URL as a manual fallback.
|
|
79
|
+
*/
|
|
80
|
+
declare function openBrowser(url: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* Run the browser deeplink login. Starts a loopback server, opens the frontend
|
|
83
|
+
* login page, and resolves with the tokens parsed from the callback redirect.
|
|
84
|
+
* Rejects cleanly on timeout. Does NOT persist anything — see `Trace.login`.
|
|
85
|
+
*/
|
|
86
|
+
declare function browserLogin(opts?: BrowserLoginOptions): Promise<BrowserLoginResult>;
|
|
87
|
+
|
|
1
88
|
/**
|
|
2
89
|
* @buildwithtrace/sdk — TypeScript SDK for Trace AI-powered EDA tools.
|
|
3
90
|
*
|
|
@@ -19,6 +106,7 @@
|
|
|
19
106
|
* const answer = await trace.ask('What ERC violations exist?', { projectDir: './my-board/' });
|
|
20
107
|
* ```
|
|
21
108
|
*/
|
|
109
|
+
|
|
22
110
|
/** Base error for SDK-surfaced backend failures (e.g. an SSE `error` event). */
|
|
23
111
|
declare class TraceError extends Error {
|
|
24
112
|
constructor(message: string);
|
|
@@ -35,9 +123,48 @@ declare class TraceError extends Error {
|
|
|
35
123
|
declare class TraceToolExecutionError extends TraceError {
|
|
36
124
|
constructor(message: string);
|
|
37
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Thrown when the backend rejects a mode the user's plan doesn't allow (HTTP
|
|
128
|
+
* 403). Free users can only use `ask`; `agent`/`plan` require a paid plan (BYOK
|
|
129
|
+
* does NOT bypass this gate). The backend body is
|
|
130
|
+
* `{"detail": {"error": "plan_restricted", "message": ..., "current_plan_id": ...}}`
|
|
131
|
+
* (see backend `app/auth/quota.py::PlanRestricted`). Callers (the CLI) can catch
|
|
132
|
+
* this and print a clean upgrade CTA instead of a raw error.
|
|
133
|
+
*/
|
|
134
|
+
declare class TracePlanRestrictedError extends TraceError {
|
|
135
|
+
/** Always true — lets callers branch on a flag, mirroring the backend `code`. */
|
|
136
|
+
readonly planRestricted = true;
|
|
137
|
+
/** The chat mode that was rejected (`"agent"`/`"plan"`), if known. */
|
|
138
|
+
readonly mode?: string;
|
|
139
|
+
/** The user's current plan id (`"free"`), if the backend reported it. */
|
|
140
|
+
readonly plan?: string;
|
|
141
|
+
constructor(message: string, opts?: {
|
|
142
|
+
mode?: string;
|
|
143
|
+
plan?: string;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
38
146
|
interface TraceConfig {
|
|
39
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Bearer token for the API. Optional: if omitted, the SDK falls back to the
|
|
149
|
+
* `TRACE_API_KEY` env var, then to credentials stored by `Trace.login()`.
|
|
150
|
+
*/
|
|
151
|
+
apiKey?: string;
|
|
152
|
+
baseUrl?: string;
|
|
153
|
+
timeout?: number;
|
|
154
|
+
/**
|
|
155
|
+
* BYOK defaults for every call on this client (route turns to your own
|
|
156
|
+
* provider; you pay the provider directly, so Trace's cost cap is skipped).
|
|
157
|
+
* Per-call `chat(...)` options override these; both override the env vars.
|
|
158
|
+
*/
|
|
159
|
+
llmProvider?: string;
|
|
160
|
+
llmApiKey?: string;
|
|
161
|
+
llmModelId?: string;
|
|
162
|
+
}
|
|
163
|
+
/** Options for the browser deeplink login (`Trace.login`). */
|
|
164
|
+
interface LoginOptions extends BrowserLoginOptions {
|
|
165
|
+
/** API base URL the returned `Trace` instance will use (also seeds frontend derivation). */
|
|
40
166
|
baseUrl?: string;
|
|
167
|
+
/** Request timeout (ms) for the returned `Trace` instance. */
|
|
41
168
|
timeout?: number;
|
|
42
169
|
}
|
|
43
170
|
interface GenerateResult {
|
|
@@ -97,7 +224,37 @@ declare class Trace {
|
|
|
97
224
|
private apiKey;
|
|
98
225
|
private baseUrl;
|
|
99
226
|
private timeout;
|
|
100
|
-
|
|
227
|
+
/** Constructor-level BYOK defaults (overridden per-call; both override env). */
|
|
228
|
+
private byokProvider?;
|
|
229
|
+
private byokApiKey?;
|
|
230
|
+
private byokModelId?;
|
|
231
|
+
/** Whether this instance holds a token (always true post-construction; sent as an analytics super-prop). */
|
|
232
|
+
private authed;
|
|
233
|
+
constructor(config?: TraceConfig);
|
|
234
|
+
/**
|
|
235
|
+
* Wrap a public method call: emit `sdk_call` (with timing + outcome) on every
|
|
236
|
+
* call and `sdk_error` (with the error class) on failure. Analytics is
|
|
237
|
+
* fire-and-forget; it must never change behavior, so the underlying result /
|
|
238
|
+
* thrown error always propagates unchanged.
|
|
239
|
+
*/
|
|
240
|
+
private _instrument;
|
|
241
|
+
/**
|
|
242
|
+
* Sign in via the browser deeplink flow, persist the returned tokens to a
|
|
243
|
+
* 0600 `credentials.json`, and return a ready-to-use `Trace` instance.
|
|
244
|
+
*
|
|
245
|
+
* Opens `{FRONTEND}/login?callback=http://localhost:PORT/callback` in the
|
|
246
|
+
* browser; the user authenticates there (captcha included) and the page
|
|
247
|
+
* redirects back to the loopback server with the final Supabase JWTs.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const trace = await Trace.login();
|
|
252
|
+
* const answer = await trace.ask('What ERC violations exist?');
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
static login(opts?: LoginOptions): Promise<Trace>;
|
|
256
|
+
/** Clear stored credentials (sign out). Subsequent `new Trace()` will need a token again. */
|
|
257
|
+
static logout(): void;
|
|
101
258
|
generateSymbol(description: string, options?: {
|
|
102
259
|
datasheetUrl?: string;
|
|
103
260
|
additionalInstructions?: string;
|
|
@@ -140,6 +297,16 @@ declare class Trace {
|
|
|
140
297
|
* backend ChatRequest contract so the SDK stays in lockstep with the
|
|
141
298
|
* CLI/desktop instead of sending a stale minimal payload.
|
|
142
299
|
*/
|
|
300
|
+
/**
|
|
301
|
+
* Resolve BYOK routing with precedence: per-call options > constructor config >
|
|
302
|
+
* env (`TRACE_LLM_PROVIDER` / `TRACE_LLM_API_KEY` / `TRACE_LLM_MODEL`). Provider
|
|
303
|
+
* and key are resolved as a UNIT from the first source supplying BOTH (we never
|
|
304
|
+
* mix a per-call provider with an env key); the model id is layered (a more
|
|
305
|
+
* specific source's model wins). Node has no keyring `byok set` store, so env is
|
|
306
|
+
* the "persisted" mechanism. Returns `{}` when no complete config is found, so
|
|
307
|
+
* nothing is attached and the backend uses Trace-hosted Bedrock.
|
|
308
|
+
*/
|
|
309
|
+
private _resolveByok;
|
|
143
310
|
private _buildChatBody;
|
|
144
311
|
/**
|
|
145
312
|
* Stream /chat/stream and drive a client-side tool loop.
|
|
@@ -168,4 +335,4 @@ declare class Trace {
|
|
|
168
335
|
private _makeGenerateResult;
|
|
169
336
|
}
|
|
170
337
|
|
|
171
|
-
export { type AskOptions, type ChatOptions, type GenerateResult, type SearchOptions, type SearchResult, type TextResult, Trace, type TraceConfig, TraceError, TraceToolExecutionError, Trace as default };
|
|
338
|
+
export { type AskOptions, type BrowserLoginOptions, type BrowserLoginResult, type ChatOptions, type GenerateResult, type LoginOptions, type SearchOptions, type SearchResult, type StoredCredentials, type TextResult, Trace, type TraceConfig, TraceError, TracePlanRestrictedError, TraceToolExecutionError, type TraceUser, browserLogin, clearCredentials, Trace as default, deriveFrontendUrl, getConfigDir, getStoredAccessToken, openBrowser, readCredentials, storeCredentials };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser "deeplink" login + file-based credential storage for the Trace SDK.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors the Python SDK (`buildwithtrace_sdk`) so the two tools interoperate:
|
|
5
|
+
* the loopback callback contract is identical and credentials persist to the
|
|
6
|
+
* same `credentials.json` filename under an OS config dir.
|
|
7
|
+
*
|
|
8
|
+
* Login flow:
|
|
9
|
+
* 1. Start a loopback HTTP server on 127.0.0.1 on a free port.
|
|
10
|
+
* 2. Open `{FRONTEND}/login?callback=http://localhost:PORT/callback` in the
|
|
11
|
+
* user's browser. The user authenticates in-browser (captcha solved there).
|
|
12
|
+
* 3. The browser redirects to
|
|
13
|
+
* `http://localhost:PORT/callback?token=<ACCESS_JWT>&refresh_token=<RT>&user=<urlencoded JSON>`.
|
|
14
|
+
* The access-token query param is named `token` (NOT `access_token`).
|
|
15
|
+
* 4. We parse the tokens, respond with a "you can close this window" page,
|
|
16
|
+
* close the server, and resolve.
|
|
17
|
+
*
|
|
18
|
+
* Uses only Node built-ins (http, child_process, os, fs, path) — no runtime deps.
|
|
19
|
+
*/
|
|
20
|
+
/** Subset of the Supabase user object the callback URL-encodes into `user`. */
|
|
21
|
+
interface TraceUser {
|
|
22
|
+
id?: string;
|
|
23
|
+
email?: string;
|
|
24
|
+
full_name?: string;
|
|
25
|
+
avatar_url?: string;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
/** Shape persisted to credentials.json (field names mirror the Python SDK). */
|
|
29
|
+
interface StoredCredentials {
|
|
30
|
+
access_token: string;
|
|
31
|
+
refresh_token: string;
|
|
32
|
+
user_data?: TraceUser | null;
|
|
33
|
+
}
|
|
34
|
+
/** Result of a successful browser login. */
|
|
35
|
+
interface BrowserLoginResult {
|
|
36
|
+
accessToken: string;
|
|
37
|
+
refreshToken: string;
|
|
38
|
+
user: TraceUser | null;
|
|
39
|
+
}
|
|
40
|
+
interface BrowserLoginOptions {
|
|
41
|
+
/** API base URL — used to derive the FRONTEND login host. */
|
|
42
|
+
baseUrl?: string;
|
|
43
|
+
/** Explicit frontend origin (e.g. `https://buildwithtrace.com`). Overrides derivation. */
|
|
44
|
+
frontendUrl?: string;
|
|
45
|
+
/** How long to wait for the browser round-trip before rejecting. Default 120s. */
|
|
46
|
+
timeoutMs?: number;
|
|
47
|
+
/** Inject a custom browser-opener (used in tests). Defaults to the OS opener. */
|
|
48
|
+
open?: (url: string) => void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The login page lives on the FRONTEND host, not the API host. Derive it:
|
|
52
|
+
* - `TRACE_FRONTEND_URL` env always wins.
|
|
53
|
+
* - `api.<domain>` -> `https://<domain>` (e.g. api.buildwithtrace.com -> buildwithtrace.com)
|
|
54
|
+
* - localhost / 127.0.0.1 -> `http://localhost:3000` (dev frontend default)
|
|
55
|
+
* - anything else (staging IPs, unknown hosts) -> the production frontend.
|
|
56
|
+
*/
|
|
57
|
+
declare function deriveFrontendUrl(baseUrl?: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* OS config directory for Trace, best-effort matching the Python SDK's
|
|
60
|
+
* `platformdirs.user_config_dir("trace", "buildwithtrace")`:
|
|
61
|
+
* - `TRACE_CONFIG_DIR` env always wins.
|
|
62
|
+
* - macOS: ~/Library/Application Support/trace
|
|
63
|
+
* - Windows: %APPDATA%\buildwithtrace\trace
|
|
64
|
+
* - Linux: $XDG_CONFIG_HOME/trace (else ~/.config/trace)
|
|
65
|
+
*/
|
|
66
|
+
declare function getConfigDir(): string;
|
|
67
|
+
/** Persist tokens to a 0600 credentials.json. Creates the config dir if needed. */
|
|
68
|
+
declare function storeCredentials(creds: StoredCredentials): string;
|
|
69
|
+
/** Read stored credentials, or null if absent/corrupt. */
|
|
70
|
+
declare function readCredentials(): StoredCredentials | null;
|
|
71
|
+
/** The stored access token (a Supabase JWT), or null if not logged in. */
|
|
72
|
+
declare function getStoredAccessToken(): string | null;
|
|
73
|
+
/** Remove stored credentials. No-op if there are none. */
|
|
74
|
+
declare function clearCredentials(): void;
|
|
75
|
+
/**
|
|
76
|
+
* Open a URL in the user's default browser. Cross-platform via the OS opener
|
|
77
|
+
* (macOS `open`, Windows `start`, Linux `xdg-open`). Best-effort: if the spawn
|
|
78
|
+
* fails the caller still prints the URL as a manual fallback.
|
|
79
|
+
*/
|
|
80
|
+
declare function openBrowser(url: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* Run the browser deeplink login. Starts a loopback server, opens the frontend
|
|
83
|
+
* login page, and resolves with the tokens parsed from the callback redirect.
|
|
84
|
+
* Rejects cleanly on timeout. Does NOT persist anything — see `Trace.login`.
|
|
85
|
+
*/
|
|
86
|
+
declare function browserLogin(opts?: BrowserLoginOptions): Promise<BrowserLoginResult>;
|
|
87
|
+
|
|
1
88
|
/**
|
|
2
89
|
* @buildwithtrace/sdk — TypeScript SDK for Trace AI-powered EDA tools.
|
|
3
90
|
*
|
|
@@ -19,6 +106,7 @@
|
|
|
19
106
|
* const answer = await trace.ask('What ERC violations exist?', { projectDir: './my-board/' });
|
|
20
107
|
* ```
|
|
21
108
|
*/
|
|
109
|
+
|
|
22
110
|
/** Base error for SDK-surfaced backend failures (e.g. an SSE `error` event). */
|
|
23
111
|
declare class TraceError extends Error {
|
|
24
112
|
constructor(message: string);
|
|
@@ -35,9 +123,48 @@ declare class TraceError extends Error {
|
|
|
35
123
|
declare class TraceToolExecutionError extends TraceError {
|
|
36
124
|
constructor(message: string);
|
|
37
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Thrown when the backend rejects a mode the user's plan doesn't allow (HTTP
|
|
128
|
+
* 403). Free users can only use `ask`; `agent`/`plan` require a paid plan (BYOK
|
|
129
|
+
* does NOT bypass this gate). The backend body is
|
|
130
|
+
* `{"detail": {"error": "plan_restricted", "message": ..., "current_plan_id": ...}}`
|
|
131
|
+
* (see backend `app/auth/quota.py::PlanRestricted`). Callers (the CLI) can catch
|
|
132
|
+
* this and print a clean upgrade CTA instead of a raw error.
|
|
133
|
+
*/
|
|
134
|
+
declare class TracePlanRestrictedError extends TraceError {
|
|
135
|
+
/** Always true — lets callers branch on a flag, mirroring the backend `code`. */
|
|
136
|
+
readonly planRestricted = true;
|
|
137
|
+
/** The chat mode that was rejected (`"agent"`/`"plan"`), if known. */
|
|
138
|
+
readonly mode?: string;
|
|
139
|
+
/** The user's current plan id (`"free"`), if the backend reported it. */
|
|
140
|
+
readonly plan?: string;
|
|
141
|
+
constructor(message: string, opts?: {
|
|
142
|
+
mode?: string;
|
|
143
|
+
plan?: string;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
38
146
|
interface TraceConfig {
|
|
39
|
-
|
|
147
|
+
/**
|
|
148
|
+
* Bearer token for the API. Optional: if omitted, the SDK falls back to the
|
|
149
|
+
* `TRACE_API_KEY` env var, then to credentials stored by `Trace.login()`.
|
|
150
|
+
*/
|
|
151
|
+
apiKey?: string;
|
|
152
|
+
baseUrl?: string;
|
|
153
|
+
timeout?: number;
|
|
154
|
+
/**
|
|
155
|
+
* BYOK defaults for every call on this client (route turns to your own
|
|
156
|
+
* provider; you pay the provider directly, so Trace's cost cap is skipped).
|
|
157
|
+
* Per-call `chat(...)` options override these; both override the env vars.
|
|
158
|
+
*/
|
|
159
|
+
llmProvider?: string;
|
|
160
|
+
llmApiKey?: string;
|
|
161
|
+
llmModelId?: string;
|
|
162
|
+
}
|
|
163
|
+
/** Options for the browser deeplink login (`Trace.login`). */
|
|
164
|
+
interface LoginOptions extends BrowserLoginOptions {
|
|
165
|
+
/** API base URL the returned `Trace` instance will use (also seeds frontend derivation). */
|
|
40
166
|
baseUrl?: string;
|
|
167
|
+
/** Request timeout (ms) for the returned `Trace` instance. */
|
|
41
168
|
timeout?: number;
|
|
42
169
|
}
|
|
43
170
|
interface GenerateResult {
|
|
@@ -97,7 +224,37 @@ declare class Trace {
|
|
|
97
224
|
private apiKey;
|
|
98
225
|
private baseUrl;
|
|
99
226
|
private timeout;
|
|
100
|
-
|
|
227
|
+
/** Constructor-level BYOK defaults (overridden per-call; both override env). */
|
|
228
|
+
private byokProvider?;
|
|
229
|
+
private byokApiKey?;
|
|
230
|
+
private byokModelId?;
|
|
231
|
+
/** Whether this instance holds a token (always true post-construction; sent as an analytics super-prop). */
|
|
232
|
+
private authed;
|
|
233
|
+
constructor(config?: TraceConfig);
|
|
234
|
+
/**
|
|
235
|
+
* Wrap a public method call: emit `sdk_call` (with timing + outcome) on every
|
|
236
|
+
* call and `sdk_error` (with the error class) on failure. Analytics is
|
|
237
|
+
* fire-and-forget; it must never change behavior, so the underlying result /
|
|
238
|
+
* thrown error always propagates unchanged.
|
|
239
|
+
*/
|
|
240
|
+
private _instrument;
|
|
241
|
+
/**
|
|
242
|
+
* Sign in via the browser deeplink flow, persist the returned tokens to a
|
|
243
|
+
* 0600 `credentials.json`, and return a ready-to-use `Trace` instance.
|
|
244
|
+
*
|
|
245
|
+
* Opens `{FRONTEND}/login?callback=http://localhost:PORT/callback` in the
|
|
246
|
+
* browser; the user authenticates there (captcha included) and the page
|
|
247
|
+
* redirects back to the loopback server with the final Supabase JWTs.
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* ```typescript
|
|
251
|
+
* const trace = await Trace.login();
|
|
252
|
+
* const answer = await trace.ask('What ERC violations exist?');
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
static login(opts?: LoginOptions): Promise<Trace>;
|
|
256
|
+
/** Clear stored credentials (sign out). Subsequent `new Trace()` will need a token again. */
|
|
257
|
+
static logout(): void;
|
|
101
258
|
generateSymbol(description: string, options?: {
|
|
102
259
|
datasheetUrl?: string;
|
|
103
260
|
additionalInstructions?: string;
|
|
@@ -140,6 +297,16 @@ declare class Trace {
|
|
|
140
297
|
* backend ChatRequest contract so the SDK stays in lockstep with the
|
|
141
298
|
* CLI/desktop instead of sending a stale minimal payload.
|
|
142
299
|
*/
|
|
300
|
+
/**
|
|
301
|
+
* Resolve BYOK routing with precedence: per-call options > constructor config >
|
|
302
|
+
* env (`TRACE_LLM_PROVIDER` / `TRACE_LLM_API_KEY` / `TRACE_LLM_MODEL`). Provider
|
|
303
|
+
* and key are resolved as a UNIT from the first source supplying BOTH (we never
|
|
304
|
+
* mix a per-call provider with an env key); the model id is layered (a more
|
|
305
|
+
* specific source's model wins). Node has no keyring `byok set` store, so env is
|
|
306
|
+
* the "persisted" mechanism. Returns `{}` when no complete config is found, so
|
|
307
|
+
* nothing is attached and the backend uses Trace-hosted Bedrock.
|
|
308
|
+
*/
|
|
309
|
+
private _resolveByok;
|
|
143
310
|
private _buildChatBody;
|
|
144
311
|
/**
|
|
145
312
|
* Stream /chat/stream and drive a client-side tool loop.
|
|
@@ -168,4 +335,4 @@ declare class Trace {
|
|
|
168
335
|
private _makeGenerateResult;
|
|
169
336
|
}
|
|
170
337
|
|
|
171
|
-
export { type AskOptions, type ChatOptions, type GenerateResult, type SearchOptions, type SearchResult, type TextResult, Trace, type TraceConfig, TraceError, TraceToolExecutionError, Trace as default };
|
|
338
|
+
export { type AskOptions, type BrowserLoginOptions, type BrowserLoginResult, type ChatOptions, type GenerateResult, type LoginOptions, type SearchOptions, type SearchResult, type StoredCredentials, type TextResult, Trace, type TraceConfig, TraceError, TracePlanRestrictedError, TraceToolExecutionError, type TraceUser, browserLogin, clearCredentials, Trace as default, deriveFrontendUrl, getConfigDir, getStoredAccessToken, openBrowser, readCredentials, storeCredentials };
|