lodestar-ai-sdk 0.0.0-stage → 0.2.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 +66 -2
- package/dist/index.d.ts +360 -0
- package/dist/index.js +276 -0
- package/package.json +28 -4
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `lodestar-ai-sdk` (TypeScript / Edge / Node / Workers)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency TypeScript client for **Lodestar Glass-Box Inference (`api.lodestar.so/v1`)** built on native `fetch` — works in Node 18+, Next.js Edge, Cloudflare Workers, Deno, and Bun.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install lodestar-ai-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quickstart
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Lodestar } from "lodestar-ai-sdk";
|
|
15
|
+
|
|
16
|
+
const lodestar = new Lodestar({ apiKey: process.env.LODESTAR_API_KEY! });
|
|
17
|
+
|
|
18
|
+
// 1. Standalone System-One probe (~20ms, $0.03/1M input, $0 output)
|
|
19
|
+
const check = await lodestar.probes.eval({
|
|
20
|
+
model: "lodestar-s1",
|
|
21
|
+
input: "Who designed the Kessington Bridge in Cleveland?",
|
|
22
|
+
probes: ["hallucination_risk", "prompt_injection"],
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// 2. Glass-Box Chat Completion with Token-0 abort_if + declarative onAbort remediation
|
|
26
|
+
const res = await lodestar.chat.create({
|
|
27
|
+
model: "lodestar-s1",
|
|
28
|
+
messages: [{ role: "user", content: "Return JSON with keys service, status." }],
|
|
29
|
+
probes: ["hallucination_risk", "prompt_injection"],
|
|
30
|
+
abortIf: { hallucination_risk: 0.75, prompt_injection: 0.7 },
|
|
31
|
+
onAbort: {
|
|
32
|
+
hallucination_risk: {
|
|
33
|
+
action: "refuse",
|
|
34
|
+
message: "I don't have enough verified information to answer that.",
|
|
35
|
+
},
|
|
36
|
+
prompt_injection: {
|
|
37
|
+
action: "refuse",
|
|
38
|
+
message: "I cannot assist with untrusted instructions.",
|
|
39
|
+
webhook: "https://hooks.example.com/lodestar",
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
patches: ["strict-json"],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 3. Continual Learning (`teach()` / `revert()`) on `lodestar-s1` or OpenAI/Anthropic/Gemini
|
|
46
|
+
const agentName = "acme-agent";
|
|
47
|
+
const reply = await lodestar.chat.completions.create({
|
|
48
|
+
model: agentName,
|
|
49
|
+
messages: [{ role: "user", content: "Please refund my $120 order immediately." }],
|
|
50
|
+
});
|
|
51
|
+
const lesson = await lodestar.models.teach({
|
|
52
|
+
model: agentName,
|
|
53
|
+
traceId: reply.id,
|
|
54
|
+
correction: "Never promise refunds over $50 without manager approval.",
|
|
55
|
+
});
|
|
56
|
+
await lodestar.models.revert({ model: agentName, lessonId: lesson.id });
|
|
57
|
+
|
|
58
|
+
// 4. Open-Weight Model & LoRA Supply-Chain Security Audit (SARIF 2.1.0 + SVG badge)
|
|
59
|
+
const job = await lodestar.audit.lora({
|
|
60
|
+
lora: "my-org/qwen3-8b-vendor-lora",
|
|
61
|
+
mode: "weights-only",
|
|
62
|
+
profile: "strict-regulated",
|
|
63
|
+
});
|
|
64
|
+
const done = await lodestar.audit.wait(job.id);
|
|
65
|
+
const sarif = await lodestar.audit.sarif(done.id);
|
|
66
|
+
const badgeSvg = await lodestar.audit.badgeSvg(done.id);
|
|
67
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
export declare const DEFAULT_BASE_URL = "https://api.lodestar.so/v1";
|
|
2
|
+
export declare const DEFAULT_MODEL = "lodestar-s1";
|
|
3
|
+
export interface LodestarOptions {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
fetch?: typeof globalThis.fetch;
|
|
7
|
+
/** Enable Zero-Data-Retention mode (`X-Lodestar-ZDR: true`) on all requests. */
|
|
8
|
+
zdr?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface ChatMessage {
|
|
11
|
+
role: "system" | "user" | "assistant" | string;
|
|
12
|
+
content: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ProbeEvalParams {
|
|
15
|
+
model?: string;
|
|
16
|
+
probes: string[];
|
|
17
|
+
input?: string;
|
|
18
|
+
messages?: ChatMessage[];
|
|
19
|
+
system?: string;
|
|
20
|
+
zdr?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface ProbeEvalResponse {
|
|
23
|
+
id: string;
|
|
24
|
+
object: "lodestar.probe_eval";
|
|
25
|
+
model: string;
|
|
26
|
+
scores: Record<string, number>;
|
|
27
|
+
early_exit_layer?: number;
|
|
28
|
+
total_layers?: number;
|
|
29
|
+
probe_ms: number;
|
|
30
|
+
latency_ms: number;
|
|
31
|
+
usage: {
|
|
32
|
+
prompt_tokens: number;
|
|
33
|
+
completion_tokens: 0;
|
|
34
|
+
cost_usd: number;
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export interface ProbeCreateParams {
|
|
38
|
+
model?: string;
|
|
39
|
+
name: string;
|
|
40
|
+
positive: Array<string | {
|
|
41
|
+
text: string;
|
|
42
|
+
group?: string;
|
|
43
|
+
}>;
|
|
44
|
+
negative: Array<string | {
|
|
45
|
+
text: string;
|
|
46
|
+
group?: string;
|
|
47
|
+
}>;
|
|
48
|
+
positiveLabel?: string;
|
|
49
|
+
negativeLabel?: string;
|
|
50
|
+
zdr?: boolean;
|
|
51
|
+
}
|
|
52
|
+
export interface ProbeCreateResponse {
|
|
53
|
+
id: string;
|
|
54
|
+
object: "lodestar.probe";
|
|
55
|
+
name: string;
|
|
56
|
+
model: string;
|
|
57
|
+
layer: number;
|
|
58
|
+
d_model: number;
|
|
59
|
+
n_examples: number;
|
|
60
|
+
fit_ms: number;
|
|
61
|
+
metrics: Record<string, unknown>;
|
|
62
|
+
export_url: string;
|
|
63
|
+
}
|
|
64
|
+
export type OnAbortAction = "retry_with_patch" | "fallback_model" | "refuse";
|
|
65
|
+
export interface OnAbortRule {
|
|
66
|
+
action: OnAbortAction;
|
|
67
|
+
/** Required when `action` is `"retry_with_patch"`. */
|
|
68
|
+
patches?: string[];
|
|
69
|
+
/** Required when `action` is `"fallback_model"` (e.g. `"lodestar-s1-precise"` or BYOK `"gpt-4o"` / `"claude-sonnet-4-20250514"`). */
|
|
70
|
+
model?: string;
|
|
71
|
+
/** Optional provider override for BYOK fallback: `"lodestar" | "openai" | "anthropic" | "gemini" | "custom"`. */
|
|
72
|
+
provider?: "lodestar" | "openai" | "anthropic" | "gemini" | "custom";
|
|
73
|
+
/** Optional custom `https://` endpoint for BYOK fallback. */
|
|
74
|
+
upstream_url?: string;
|
|
75
|
+
/** Optional BYOK API key for frontier fallback (or pass `X-Lodestar-Fallback-API-Key` header). */
|
|
76
|
+
api_key?: string;
|
|
77
|
+
/** Custom refusal message when `action` is `"refuse"`. */
|
|
78
|
+
message?: string;
|
|
79
|
+
/** Optional `https://` webhook URL notified asynchronously when a guard trips. */
|
|
80
|
+
webhook?: string;
|
|
81
|
+
/** Optional extra HTTP headers sent with the webhook request. */
|
|
82
|
+
webhook_headers?: Record<string, string>;
|
|
83
|
+
}
|
|
84
|
+
/** Either a single remediation rule for all guards or a per-probe map (with optional `"default"` key). */
|
|
85
|
+
export type OnAbortConfig = OnAbortRule | Record<string, OnAbortRule>;
|
|
86
|
+
export interface LodestarRemediationMetadata {
|
|
87
|
+
triggered: boolean;
|
|
88
|
+
action: OnAbortAction;
|
|
89
|
+
trigger_reason: string;
|
|
90
|
+
patches_added?: string[];
|
|
91
|
+
original_model?: string;
|
|
92
|
+
fallback_model?: string;
|
|
93
|
+
provider?: string;
|
|
94
|
+
message?: string;
|
|
95
|
+
webhook_dispatched: boolean;
|
|
96
|
+
}
|
|
97
|
+
export interface ChatCreateParams {
|
|
98
|
+
model?: string;
|
|
99
|
+
messages: ChatMessage[];
|
|
100
|
+
probes?: string[];
|
|
101
|
+
abortIf?: Record<string, number | string>;
|
|
102
|
+
onAbort?: OnAbortConfig;
|
|
103
|
+
patches?: string[];
|
|
104
|
+
memoryModel?: string;
|
|
105
|
+
zdr?: boolean;
|
|
106
|
+
max_tokens?: number;
|
|
107
|
+
temperature?: number;
|
|
108
|
+
[extra: string]: unknown;
|
|
109
|
+
}
|
|
110
|
+
export interface AppliedLessonTelemetry {
|
|
111
|
+
id: string;
|
|
112
|
+
version: number;
|
|
113
|
+
kind: "guard" | "policy" | "skill" | string;
|
|
114
|
+
delivery_mode: "guard" | "episodic" | "hybrid" | "weight" | string;
|
|
115
|
+
score: number;
|
|
116
|
+
threshold: number;
|
|
117
|
+
view: "full_context" | "last_turn" | string;
|
|
118
|
+
}
|
|
119
|
+
export interface LodestarMetadata {
|
|
120
|
+
trace_id?: string;
|
|
121
|
+
probes: Record<string, number>;
|
|
122
|
+
aborted: boolean;
|
|
123
|
+
abort_reason: string | null;
|
|
124
|
+
patches_applied: string[];
|
|
125
|
+
probe_ms: number;
|
|
126
|
+
zdr?: boolean;
|
|
127
|
+
early_exit_layer?: number | null;
|
|
128
|
+
vllm_model?: string;
|
|
129
|
+
model_version?: number;
|
|
130
|
+
lessons_evaluated?: number;
|
|
131
|
+
lessons_applied?: AppliedLessonTelemetry[];
|
|
132
|
+
remediation?: LodestarRemediationMetadata;
|
|
133
|
+
}
|
|
134
|
+
export interface ContinualModelUpstream {
|
|
135
|
+
provider: "openai" | "anthropic" | "gemini" | "custom";
|
|
136
|
+
model: string;
|
|
137
|
+
upstream_url?: string;
|
|
138
|
+
}
|
|
139
|
+
export interface ContinualModelCreateParams {
|
|
140
|
+
name: string;
|
|
141
|
+
baseModel?: string;
|
|
142
|
+
upstream?: ContinualModelUpstream;
|
|
143
|
+
consolidationPolicy?: Record<string, unknown>;
|
|
144
|
+
}
|
|
145
|
+
export interface ContinualTeachParams {
|
|
146
|
+
model: string;
|
|
147
|
+
correction?: string;
|
|
148
|
+
traceId?: string;
|
|
149
|
+
messages?: ChatMessage[];
|
|
150
|
+
badOutput?: string;
|
|
151
|
+
goodOutput?: string;
|
|
152
|
+
when?: string;
|
|
153
|
+
examples?: Array<string | Record<string, unknown>>;
|
|
154
|
+
positive?: Array<string | {
|
|
155
|
+
text: string;
|
|
156
|
+
}>;
|
|
157
|
+
negative?: Array<string | {
|
|
158
|
+
text: string;
|
|
159
|
+
}>;
|
|
160
|
+
kind?: "auto" | "guard" | "policy" | "skill";
|
|
161
|
+
delivery?: "auto" | "guard" | "episodic" | "hybrid" | "weight";
|
|
162
|
+
rollout?: "active" | "canary" | "staging";
|
|
163
|
+
canaryPct?: number;
|
|
164
|
+
guardAction?: Record<string, unknown>;
|
|
165
|
+
tags?: string[];
|
|
166
|
+
metadata?: Record<string, unknown>;
|
|
167
|
+
idempotencyKey?: string;
|
|
168
|
+
upstream?: ContinualModelUpstream;
|
|
169
|
+
zdr?: boolean;
|
|
170
|
+
}
|
|
171
|
+
export interface ContinualLessonResponse {
|
|
172
|
+
id: string;
|
|
173
|
+
object: "lodestar.lesson";
|
|
174
|
+
model: string;
|
|
175
|
+
version: number;
|
|
176
|
+
status: string;
|
|
177
|
+
kind: string;
|
|
178
|
+
delivery_mode: string;
|
|
179
|
+
canary_pct: number;
|
|
180
|
+
consolidation_status: string;
|
|
181
|
+
superseded_by: string | null;
|
|
182
|
+
superseded_lesson_ids: string[];
|
|
183
|
+
correction: string;
|
|
184
|
+
scoped_rule: string;
|
|
185
|
+
trigger_summary: string;
|
|
186
|
+
tags: string[];
|
|
187
|
+
metadata: Record<string, unknown>;
|
|
188
|
+
probe: {
|
|
189
|
+
layer: number;
|
|
190
|
+
threshold: number;
|
|
191
|
+
};
|
|
192
|
+
trace_count: number;
|
|
193
|
+
trigger_count: number;
|
|
194
|
+
certification: Record<string, unknown> | null;
|
|
195
|
+
created_at: string;
|
|
196
|
+
updated_at: string;
|
|
197
|
+
}
|
|
198
|
+
export interface ContinualRevertParams {
|
|
199
|
+
model: string;
|
|
200
|
+
lessonId?: string;
|
|
201
|
+
toVersion?: number;
|
|
202
|
+
}
|
|
203
|
+
export interface ContinualRevertResponse {
|
|
204
|
+
object: "lodestar.revert";
|
|
205
|
+
model: string;
|
|
206
|
+
version: number;
|
|
207
|
+
reverted_lesson_ids: string[];
|
|
208
|
+
restored_lesson_ids: string[];
|
|
209
|
+
active_lessons: number;
|
|
210
|
+
refit_ms: number;
|
|
211
|
+
}
|
|
212
|
+
export interface ChatCompletionResponse {
|
|
213
|
+
id: string;
|
|
214
|
+
object: "chat.completion";
|
|
215
|
+
created: number;
|
|
216
|
+
model: string;
|
|
217
|
+
choices: Array<{
|
|
218
|
+
index: number;
|
|
219
|
+
message: ChatMessage;
|
|
220
|
+
finish_reason: string | null;
|
|
221
|
+
}>;
|
|
222
|
+
usage?: {
|
|
223
|
+
prompt_tokens: number;
|
|
224
|
+
completion_tokens: number;
|
|
225
|
+
total_tokens: number;
|
|
226
|
+
};
|
|
227
|
+
lodestar: LodestarMetadata;
|
|
228
|
+
}
|
|
229
|
+
export type AuditMode = "weights-only" | "standard" | "deep";
|
|
230
|
+
export type AuditProfile = "enterprise-default" | "strict-regulated" | "fine-tune-qa";
|
|
231
|
+
export interface CheckpointAuditParams {
|
|
232
|
+
base: string;
|
|
233
|
+
candidate: string;
|
|
234
|
+
mode?: AuditMode;
|
|
235
|
+
profile?: AuditProfile;
|
|
236
|
+
policy?: Record<string, unknown>;
|
|
237
|
+
policyYaml?: string;
|
|
238
|
+
strict?: boolean;
|
|
239
|
+
jlens?: "skip" | "cached" | "auto";
|
|
240
|
+
noHash?: boolean;
|
|
241
|
+
skipDirections?: boolean;
|
|
242
|
+
webhook?: string;
|
|
243
|
+
webhookHeaders?: Record<string, string>;
|
|
244
|
+
sync?: boolean;
|
|
245
|
+
}
|
|
246
|
+
export interface LoRAAuditParams {
|
|
247
|
+
lora: string;
|
|
248
|
+
base?: string;
|
|
249
|
+
mode?: AuditMode;
|
|
250
|
+
profile?: AuditProfile;
|
|
251
|
+
policy?: Record<string, unknown>;
|
|
252
|
+
policyYaml?: string;
|
|
253
|
+
strict?: boolean;
|
|
254
|
+
jlens?: "skip" | "cached" | "auto";
|
|
255
|
+
noHash?: boolean;
|
|
256
|
+
skipDirections?: boolean;
|
|
257
|
+
webhook?: string;
|
|
258
|
+
webhookHeaders?: Record<string, string>;
|
|
259
|
+
sync?: boolean;
|
|
260
|
+
}
|
|
261
|
+
export interface AuditJobResponse {
|
|
262
|
+
id: string;
|
|
263
|
+
object: "lodestar.audit_job";
|
|
264
|
+
kind: "checkpoint" | "lora";
|
|
265
|
+
status: "queued" | "running" | "completed" | "failed";
|
|
266
|
+
base: string | null;
|
|
267
|
+
candidate: string;
|
|
268
|
+
mode: AuditMode;
|
|
269
|
+
profile: string;
|
|
270
|
+
strict: boolean;
|
|
271
|
+
verdict: "PASS" | "WARN" | "FAIL" | null;
|
|
272
|
+
risk_score: number | null;
|
|
273
|
+
exit_code: number | null;
|
|
274
|
+
findings_count: number;
|
|
275
|
+
created_at: number;
|
|
276
|
+
started_at: number | null;
|
|
277
|
+
completed_at: number | null;
|
|
278
|
+
elapsed_sec: number | null;
|
|
279
|
+
progress: string[];
|
|
280
|
+
error: string | null;
|
|
281
|
+
webhook_dispatched: boolean;
|
|
282
|
+
links: {
|
|
283
|
+
self: string;
|
|
284
|
+
sarif: string;
|
|
285
|
+
markdown: string;
|
|
286
|
+
html: string;
|
|
287
|
+
badge: string;
|
|
288
|
+
};
|
|
289
|
+
result?: Record<string, unknown> | null;
|
|
290
|
+
}
|
|
291
|
+
export declare class LodestarAPIError extends Error {
|
|
292
|
+
statusCode: number;
|
|
293
|
+
body: string;
|
|
294
|
+
constructor(statusCode: number, message: string, body?: string);
|
|
295
|
+
}
|
|
296
|
+
export declare class Lodestar {
|
|
297
|
+
readonly baseUrl: string;
|
|
298
|
+
readonly zdr: boolean;
|
|
299
|
+
private readonly apiKey;
|
|
300
|
+
private readonly _fetch;
|
|
301
|
+
constructor(opts: LodestarOptions);
|
|
302
|
+
private headers;
|
|
303
|
+
private requestText;
|
|
304
|
+
private request;
|
|
305
|
+
readonly probes: {
|
|
306
|
+
list: (model?: string) => Promise<unknown[]>;
|
|
307
|
+
eval: (params: ProbeEvalParams) => Promise<ProbeEvalResponse>;
|
|
308
|
+
create: (params: ProbeCreateParams) => Promise<ProbeCreateResponse>;
|
|
309
|
+
export: (probeId: string) => Promise<Record<string, unknown>>;
|
|
310
|
+
};
|
|
311
|
+
private _chatCreate;
|
|
312
|
+
private _chatStream;
|
|
313
|
+
readonly chat: {
|
|
314
|
+
create: (params: ChatCreateParams) => Promise<ChatCompletionResponse>;
|
|
315
|
+
stream: (params: ChatCreateParams) => AsyncGenerator<Record<string, unknown>, void, void>;
|
|
316
|
+
completions: {
|
|
317
|
+
create: (params: ChatCreateParams) => Promise<ChatCompletionResponse>;
|
|
318
|
+
stream: (params: ChatCreateParams) => AsyncGenerator<Record<string, unknown>, void, void>;
|
|
319
|
+
};
|
|
320
|
+
};
|
|
321
|
+
readonly audit: {
|
|
322
|
+
checkpoint: (params: CheckpointAuditParams) => Promise<AuditJobResponse>;
|
|
323
|
+
lora: (params: LoRAAuditParams) => Promise<AuditJobResponse>;
|
|
324
|
+
get: (jobId: string) => Promise<AuditJobResponse>;
|
|
325
|
+
wait: (jobId: string, opts?: {
|
|
326
|
+
pollIntervalMs?: number;
|
|
327
|
+
timeoutMs?: number;
|
|
328
|
+
}) => Promise<AuditJobResponse>;
|
|
329
|
+
list: (opts?: {
|
|
330
|
+
status?: string;
|
|
331
|
+
verdict?: string;
|
|
332
|
+
limit?: number;
|
|
333
|
+
}) => Promise<AuditJobResponse[]>;
|
|
334
|
+
policies: () => Promise<unknown[]>;
|
|
335
|
+
sarif: (jobId: string) => Promise<Record<string, unknown>>;
|
|
336
|
+
markdown: (jobId: string) => Promise<string>;
|
|
337
|
+
html: (jobId: string) => Promise<string>;
|
|
338
|
+
badgeSvg: (jobId: string) => Promise<string>;
|
|
339
|
+
};
|
|
340
|
+
readonly models: (() => Promise<unknown[]>) & {
|
|
341
|
+
list: () => Promise<unknown[]>;
|
|
342
|
+
get: (model: string) => Promise<Record<string, unknown>>;
|
|
343
|
+
create: (params: ContinualModelCreateParams) => Promise<Record<string, unknown>>;
|
|
344
|
+
teach: (params: ContinualTeachParams) => Promise<ContinualLessonResponse>;
|
|
345
|
+
revert: (params: ContinualRevertParams) => Promise<ContinualRevertResponse>;
|
|
346
|
+
lessons: (model: string, opts?: {
|
|
347
|
+
status?: string;
|
|
348
|
+
limit?: number;
|
|
349
|
+
}) => Promise<ContinualLessonResponse[]>;
|
|
350
|
+
getLesson: (model: string, lessonId: string) => Promise<ContinualLessonResponse>;
|
|
351
|
+
updateLesson: (model: string, lessonId: string, updates: Record<string, unknown>) => Promise<ContinualLessonResponse>;
|
|
352
|
+
consolidate: (model: string, lessonId: string, options?: {
|
|
353
|
+
sync?: boolean;
|
|
354
|
+
rank?: number;
|
|
355
|
+
steps?: number;
|
|
356
|
+
lr?: number;
|
|
357
|
+
}) => Promise<Record<string, unknown>>;
|
|
358
|
+
};
|
|
359
|
+
usage: (limit?: number) => Promise<Record<string, unknown>>;
|
|
360
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
export const DEFAULT_BASE_URL = "https://api.lodestar.so/v1";
|
|
2
|
+
export const DEFAULT_MODEL = "lodestar-s1";
|
|
3
|
+
export class LodestarAPIError extends Error {
|
|
4
|
+
statusCode;
|
|
5
|
+
body;
|
|
6
|
+
constructor(statusCode, message, body = "") {
|
|
7
|
+
super(`Lodestar API ${statusCode}: ${message}`);
|
|
8
|
+
this.name = "LodestarAPIError";
|
|
9
|
+
this.statusCode = statusCode;
|
|
10
|
+
this.body = body;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export class Lodestar {
|
|
14
|
+
baseUrl;
|
|
15
|
+
zdr;
|
|
16
|
+
apiKey;
|
|
17
|
+
_fetch;
|
|
18
|
+
constructor(opts) {
|
|
19
|
+
const key = opts.apiKey?.trim();
|
|
20
|
+
if (!key)
|
|
21
|
+
throw new Error("Missing apiKey (expected 'ld_live_...').");
|
|
22
|
+
this.apiKey = key;
|
|
23
|
+
this.baseUrl = (opts.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
24
|
+
this._fetch = opts.fetch || globalThis.fetch;
|
|
25
|
+
this.zdr = Boolean(opts.zdr);
|
|
26
|
+
}
|
|
27
|
+
headers() {
|
|
28
|
+
const hdrs = {
|
|
29
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
30
|
+
"Content-Type": "application/json",
|
|
31
|
+
};
|
|
32
|
+
if (this.zdr)
|
|
33
|
+
hdrs["X-Lodestar-ZDR"] = "true";
|
|
34
|
+
return hdrs;
|
|
35
|
+
}
|
|
36
|
+
async requestText(method, path, body) {
|
|
37
|
+
const res = await this._fetch(`${this.baseUrl}${path}`, {
|
|
38
|
+
method,
|
|
39
|
+
headers: this.headers(),
|
|
40
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
41
|
+
});
|
|
42
|
+
const text = await res.text();
|
|
43
|
+
if (!res.ok) {
|
|
44
|
+
let msg = text;
|
|
45
|
+
try {
|
|
46
|
+
const parsed = JSON.parse(text);
|
|
47
|
+
if (parsed.detail)
|
|
48
|
+
msg = String(parsed.detail);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// raw text
|
|
52
|
+
}
|
|
53
|
+
throw new LodestarAPIError(res.status, msg, text);
|
|
54
|
+
}
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
async request(method, path, body) {
|
|
58
|
+
return JSON.parse(await this.requestText(method, path, body));
|
|
59
|
+
}
|
|
60
|
+
probes = {
|
|
61
|
+
list: async (model) => {
|
|
62
|
+
const q = model ? `?model=${encodeURIComponent(model)}` : "";
|
|
63
|
+
const out = await this.request("GET", `/probes${q}`);
|
|
64
|
+
return out.data;
|
|
65
|
+
},
|
|
66
|
+
eval: (params) => this.request("POST", "/probes/eval", {
|
|
67
|
+
model: params.model ?? DEFAULT_MODEL,
|
|
68
|
+
probes: params.probes,
|
|
69
|
+
input: params.input,
|
|
70
|
+
messages: params.messages,
|
|
71
|
+
system: params.system,
|
|
72
|
+
...(params.zdr !== undefined ? { zdr: params.zdr } : {}),
|
|
73
|
+
}),
|
|
74
|
+
create: (params) => this.request("POST", "/probes/create", {
|
|
75
|
+
model: params.model ?? DEFAULT_MODEL,
|
|
76
|
+
name: params.name,
|
|
77
|
+
positive: params.positive,
|
|
78
|
+
negative: params.negative,
|
|
79
|
+
positive_label: params.positiveLabel ?? "Positive",
|
|
80
|
+
negative_label: params.negativeLabel ?? "Negative",
|
|
81
|
+
...(params.zdr !== undefined ? { zdr: params.zdr } : {}),
|
|
82
|
+
}),
|
|
83
|
+
export: (probeId) => this.request("GET", `/probes/${encodeURIComponent(probeId)}/export`),
|
|
84
|
+
};
|
|
85
|
+
_chatCreate = (params) => {
|
|
86
|
+
const { probes, abortIf, onAbort, patches, memoryModel, zdr, model, ...rest } = params;
|
|
87
|
+
const lodestar = {};
|
|
88
|
+
if (probes?.length)
|
|
89
|
+
lodestar.probes = probes;
|
|
90
|
+
if (abortIf && Object.keys(abortIf).length)
|
|
91
|
+
lodestar.abort_if = abortIf;
|
|
92
|
+
if (onAbort && Object.keys(onAbort).length)
|
|
93
|
+
lodestar.on_abort = onAbort;
|
|
94
|
+
if (patches?.length)
|
|
95
|
+
lodestar.patches = patches;
|
|
96
|
+
if (memoryModel)
|
|
97
|
+
lodestar.memory_model = memoryModel;
|
|
98
|
+
if (zdr !== undefined)
|
|
99
|
+
lodestar.zdr = zdr;
|
|
100
|
+
return this.request("POST", "/chat/completions", {
|
|
101
|
+
...rest,
|
|
102
|
+
model: model ?? DEFAULT_MODEL,
|
|
103
|
+
stream: false,
|
|
104
|
+
...(Object.keys(lodestar).length ? { lodestar } : {}),
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
_chatStream = async function* (params) {
|
|
108
|
+
const { probes, abortIf, onAbort, patches, memoryModel, zdr, model, ...rest } = params;
|
|
109
|
+
const lodestar = {};
|
|
110
|
+
if (probes?.length)
|
|
111
|
+
lodestar.probes = probes;
|
|
112
|
+
if (abortIf && Object.keys(abortIf).length)
|
|
113
|
+
lodestar.abort_if = abortIf;
|
|
114
|
+
if (onAbort && Object.keys(onAbort).length)
|
|
115
|
+
lodestar.on_abort = onAbort;
|
|
116
|
+
if (patches?.length)
|
|
117
|
+
lodestar.patches = patches;
|
|
118
|
+
if (memoryModel)
|
|
119
|
+
lodestar.memory_model = memoryModel;
|
|
120
|
+
if (zdr !== undefined)
|
|
121
|
+
lodestar.zdr = zdr;
|
|
122
|
+
const res = await this._fetch(`${this.baseUrl}/chat/completions`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
headers: this.headers(),
|
|
125
|
+
body: JSON.stringify({
|
|
126
|
+
...rest,
|
|
127
|
+
model: model ?? DEFAULT_MODEL,
|
|
128
|
+
stream: true,
|
|
129
|
+
...(Object.keys(lodestar).length ? { lodestar } : {}),
|
|
130
|
+
}),
|
|
131
|
+
});
|
|
132
|
+
if (!res.ok || !res.body) {
|
|
133
|
+
const txt = await res.text();
|
|
134
|
+
throw new LodestarAPIError(res.status, txt.slice(0, 300), txt);
|
|
135
|
+
}
|
|
136
|
+
const reader = res.body.getReader();
|
|
137
|
+
const decoder = new TextDecoder();
|
|
138
|
+
let buf = "";
|
|
139
|
+
while (true) {
|
|
140
|
+
const { done, value } = await reader.read();
|
|
141
|
+
if (done)
|
|
142
|
+
break;
|
|
143
|
+
buf += decoder.decode(value, { stream: true });
|
|
144
|
+
const lines = buf.split("\n");
|
|
145
|
+
buf = lines.pop() || "";
|
|
146
|
+
for (const raw of lines) {
|
|
147
|
+
const line = raw.trim();
|
|
148
|
+
if (!line.startsWith("data:"))
|
|
149
|
+
continue;
|
|
150
|
+
const payload = line.slice(5).trim();
|
|
151
|
+
if (!payload || payload === "[DONE]")
|
|
152
|
+
continue;
|
|
153
|
+
yield JSON.parse(payload);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}.bind(this);
|
|
157
|
+
chat = {
|
|
158
|
+
create: (params) => this._chatCreate(params),
|
|
159
|
+
stream: (params) => this._chatStream(params),
|
|
160
|
+
completions: {
|
|
161
|
+
create: (params) => this._chatCreate(params),
|
|
162
|
+
stream: (params) => this._chatStream(params),
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
audit = {
|
|
166
|
+
checkpoint: (params) => this.request("POST", "/audit/checkpoint", {
|
|
167
|
+
base: params.base,
|
|
168
|
+
candidate: params.candidate,
|
|
169
|
+
mode: params.mode ?? "weights-only",
|
|
170
|
+
profile: params.profile ?? "enterprise-default",
|
|
171
|
+
policy: params.policy,
|
|
172
|
+
policy_yaml: params.policyYaml,
|
|
173
|
+
strict: params.strict ?? false,
|
|
174
|
+
jlens: params.jlens ?? "skip",
|
|
175
|
+
no_hash: params.noHash ?? false,
|
|
176
|
+
skip_directions: params.skipDirections ?? false,
|
|
177
|
+
webhook: params.webhook,
|
|
178
|
+
webhook_headers: params.webhookHeaders,
|
|
179
|
+
sync: params.sync ?? false,
|
|
180
|
+
}),
|
|
181
|
+
lora: (params) => this.request("POST", "/audit/lora", {
|
|
182
|
+
lora: params.lora,
|
|
183
|
+
base: params.base,
|
|
184
|
+
mode: params.mode ?? "weights-only",
|
|
185
|
+
profile: params.profile ?? "enterprise-default",
|
|
186
|
+
policy: params.policy,
|
|
187
|
+
policy_yaml: params.policyYaml,
|
|
188
|
+
strict: params.strict ?? false,
|
|
189
|
+
jlens: params.jlens ?? "skip",
|
|
190
|
+
no_hash: params.noHash ?? false,
|
|
191
|
+
skip_directions: params.skipDirections ?? false,
|
|
192
|
+
webhook: params.webhook,
|
|
193
|
+
webhook_headers: params.webhookHeaders,
|
|
194
|
+
sync: params.sync ?? false,
|
|
195
|
+
}),
|
|
196
|
+
get: (jobId) => this.request("GET", `/audit/jobs/${encodeURIComponent(jobId)}`),
|
|
197
|
+
wait: async (jobId, opts) => {
|
|
198
|
+
const interval = Math.max(50, opts?.pollIntervalMs ?? 500);
|
|
199
|
+
const deadline = Date.now() + (opts?.timeoutMs ?? 600_000);
|
|
200
|
+
while (true) {
|
|
201
|
+
const job = await this.audit.get(jobId);
|
|
202
|
+
if (job.status !== "queued" && job.status !== "running")
|
|
203
|
+
return job;
|
|
204
|
+
if (Date.now() >= deadline)
|
|
205
|
+
throw new Error(`Audit job ${jobId} timed out`);
|
|
206
|
+
await new Promise((r) => setTimeout(r, interval));
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
list: async (opts) => {
|
|
210
|
+
const qs = new URLSearchParams();
|
|
211
|
+
if (opts?.status)
|
|
212
|
+
qs.set("status", opts.status);
|
|
213
|
+
if (opts?.verdict)
|
|
214
|
+
qs.set("verdict", opts.verdict);
|
|
215
|
+
if (opts?.limit)
|
|
216
|
+
qs.set("limit", String(opts.limit));
|
|
217
|
+
const q = qs.toString();
|
|
218
|
+
const out = await this.request("GET", `/audit/jobs${q ? `?${q}` : ""}`);
|
|
219
|
+
return out.data;
|
|
220
|
+
},
|
|
221
|
+
policies: async () => (await this.request("GET", "/audit/policies")).data,
|
|
222
|
+
sarif: (jobId) => this.request("GET", `/audit/jobs/${encodeURIComponent(jobId)}/sarif`),
|
|
223
|
+
markdown: (jobId) => this.requestText("GET", `/audit/jobs/${encodeURIComponent(jobId)}/markdown`),
|
|
224
|
+
html: (jobId) => this.requestText("GET", `/audit/jobs/${encodeURIComponent(jobId)}/report.html`),
|
|
225
|
+
badgeSvg: (jobId) => this.requestText("GET", `/audit/jobs/${encodeURIComponent(jobId)}/badge.svg`),
|
|
226
|
+
};
|
|
227
|
+
models = Object.assign(async () => (await this.request("GET", "/models")).data, {
|
|
228
|
+
list: async () => (await this.request("GET", "/models")).data,
|
|
229
|
+
get: (model) => this.request("GET", `/models/${encodeURIComponent(model)}`),
|
|
230
|
+
create: (params) => this.request("POST", "/models", {
|
|
231
|
+
name: params.name,
|
|
232
|
+
base_model: params.baseModel ?? DEFAULT_MODEL,
|
|
233
|
+
...(params.upstream ? { upstream: params.upstream } : {}),
|
|
234
|
+
...(params.consolidationPolicy ? { consolidation_policy: params.consolidationPolicy } : {}),
|
|
235
|
+
}),
|
|
236
|
+
teach: (params) => this.request("POST", `/models/${encodeURIComponent(params.model)}/teach`, {
|
|
237
|
+
...(params.correction !== undefined ? { correction: params.correction } : {}),
|
|
238
|
+
...(params.traceId !== undefined ? { trace_id: params.traceId } : {}),
|
|
239
|
+
...(params.messages !== undefined ? { messages: params.messages } : {}),
|
|
240
|
+
...(params.badOutput !== undefined ? { bad_output: params.badOutput } : {}),
|
|
241
|
+
...(params.goodOutput !== undefined ? { good_output: params.goodOutput } : {}),
|
|
242
|
+
...(params.when !== undefined ? { when: params.when } : {}),
|
|
243
|
+
...(params.examples !== undefined ? { examples: params.examples } : {}),
|
|
244
|
+
...(params.positive !== undefined ? { positive: params.positive } : {}),
|
|
245
|
+
...(params.negative !== undefined ? { negative: params.negative } : {}),
|
|
246
|
+
kind: params.kind ?? "auto",
|
|
247
|
+
delivery: params.delivery ?? "auto",
|
|
248
|
+
rollout: params.rollout ?? "active",
|
|
249
|
+
...(params.canaryPct !== undefined ? { canary_pct: params.canaryPct } : {}),
|
|
250
|
+
...(params.guardAction !== undefined ? { guard_action: params.guardAction } : {}),
|
|
251
|
+
...(params.tags !== undefined ? { tags: params.tags } : {}),
|
|
252
|
+
...(params.metadata !== undefined ? { metadata: params.metadata } : {}),
|
|
253
|
+
...(params.idempotencyKey !== undefined ? { idempotency_key: params.idempotencyKey } : {}),
|
|
254
|
+
...(params.upstream !== undefined ? { upstream: params.upstream } : {}),
|
|
255
|
+
...(params.zdr !== undefined ? { zdr: params.zdr } : {}),
|
|
256
|
+
}),
|
|
257
|
+
revert: (params) => this.request("POST", `/models/${encodeURIComponent(params.model)}/revert`, {
|
|
258
|
+
...(params.lessonId !== undefined ? { lesson_id: params.lessonId } : {}),
|
|
259
|
+
...(params.toVersion !== undefined ? { to_version: params.toVersion } : {}),
|
|
260
|
+
}),
|
|
261
|
+
lessons: async (model, opts) => {
|
|
262
|
+
const qs = new URLSearchParams();
|
|
263
|
+
if (opts?.status)
|
|
264
|
+
qs.set("status", opts.status);
|
|
265
|
+
if (opts?.limit)
|
|
266
|
+
qs.set("limit", String(opts.limit));
|
|
267
|
+
const q = qs.toString();
|
|
268
|
+
const out = await this.request("GET", `/models/${encodeURIComponent(model)}/lessons${q ? `?${q}` : ""}`);
|
|
269
|
+
return out.data;
|
|
270
|
+
},
|
|
271
|
+
getLesson: (model, lessonId) => this.request("GET", `/models/${encodeURIComponent(model)}/lessons/${encodeURIComponent(lessonId)}`),
|
|
272
|
+
updateLesson: (model, lessonId, updates) => this.request("PATCH", `/models/${encodeURIComponent(model)}/lessons/${encodeURIComponent(lessonId)}`, updates),
|
|
273
|
+
consolidate: (model, lessonId, options = {}) => this.request("POST", `/models/${encodeURIComponent(model)}/lessons/${encodeURIComponent(lessonId)}/consolidate`, options),
|
|
274
|
+
});
|
|
275
|
+
usage = (limit = 25) => this.request("GET", `/usage?limit=${limit}`);
|
|
276
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lodestar-ai-sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Zero-dependency TypeScript/JavaScript SDK for Lodestar Glass-Box Inference & Open System-One Probes (api.lodestar.so/v1)",
|
|
5
|
+
"homepage": "https://lodestar.so/docs",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/faizancodes/lodestar.git",
|
|
9
|
+
"directory": "packages/lodestar-js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["lodestar", "ai", "llm", "guardrails", "interpretability", "probes", "openai", "anthropic"],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": ["dist", "README.md"],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"typecheck": "tsc --noEmit"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18.0.0"
|
|
28
|
+
},
|
|
29
|
+
"license": "MIT"
|
|
30
|
+
}
|