lodestar-ai-sdk 0.1.0 → 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 +25 -2
- package/dist/index.d.ts +208 -2
- package/dist/index.js +200 -66
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,14 +17,14 @@ const lodestar = new Lodestar({ apiKey: process.env.LODESTAR_API_KEY! });
|
|
|
17
17
|
|
|
18
18
|
// 1. Standalone System-One probe (~20ms, $0.03/1M input, $0 output)
|
|
19
19
|
const check = await lodestar.probes.eval({
|
|
20
|
-
model: "
|
|
20
|
+
model: "lodestar-s1",
|
|
21
21
|
input: "Who designed the Kessington Bridge in Cleveland?",
|
|
22
22
|
probes: ["hallucination_risk", "prompt_injection"],
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
// 2. Glass-Box Chat Completion with Token-0 abort_if + declarative onAbort remediation
|
|
26
26
|
const res = await lodestar.chat.create({
|
|
27
|
-
model: "
|
|
27
|
+
model: "lodestar-s1",
|
|
28
28
|
messages: [{ role: "user", content: "Return JSON with keys service, status." }],
|
|
29
29
|
probes: ["hallucination_risk", "prompt_injection"],
|
|
30
30
|
abortIf: { hallucination_risk: 0.75, prompt_injection: 0.7 },
|
|
@@ -41,4 +41,27 @@ const res = await lodestar.chat.create({
|
|
|
41
41
|
},
|
|
42
42
|
patches: ["strict-json"],
|
|
43
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);
|
|
44
67
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
export declare const DEFAULT_BASE_URL = "https://api.lodestar.so/v1";
|
|
2
|
+
export declare const DEFAULT_MODEL = "lodestar-s1";
|
|
2
3
|
export interface LodestarOptions {
|
|
3
4
|
apiKey: string;
|
|
4
5
|
baseUrl?: string;
|
|
5
6
|
fetch?: typeof globalThis.fetch;
|
|
7
|
+
/** Enable Zero-Data-Retention mode (`X-Lodestar-ZDR: true`) on all requests. */
|
|
8
|
+
zdr?: boolean;
|
|
6
9
|
}
|
|
7
10
|
export interface ChatMessage {
|
|
8
11
|
role: "system" | "user" | "assistant" | string;
|
|
@@ -14,6 +17,7 @@ export interface ProbeEvalParams {
|
|
|
14
17
|
input?: string;
|
|
15
18
|
messages?: ChatMessage[];
|
|
16
19
|
system?: string;
|
|
20
|
+
zdr?: boolean;
|
|
17
21
|
}
|
|
18
22
|
export interface ProbeEvalResponse {
|
|
19
23
|
id: string;
|
|
@@ -43,6 +47,7 @@ export interface ProbeCreateParams {
|
|
|
43
47
|
}>;
|
|
44
48
|
positiveLabel?: string;
|
|
45
49
|
negativeLabel?: string;
|
|
50
|
+
zdr?: boolean;
|
|
46
51
|
}
|
|
47
52
|
export interface ProbeCreateResponse {
|
|
48
53
|
id: string;
|
|
@@ -61,7 +66,7 @@ export interface OnAbortRule {
|
|
|
61
66
|
action: OnAbortAction;
|
|
62
67
|
/** Required when `action` is `"retry_with_patch"`. */
|
|
63
68
|
patches?: string[];
|
|
64
|
-
/** Required when `action` is `"fallback_model"` (e.g. `"
|
|
69
|
+
/** Required when `action` is `"fallback_model"` (e.g. `"lodestar-s1-precise"` or BYOK `"gpt-4o"` / `"claude-sonnet-4-20250514"`). */
|
|
65
70
|
model?: string;
|
|
66
71
|
/** Optional provider override for BYOK fallback: `"lodestar" | "openai" | "anthropic" | "gemini" | "custom"`. */
|
|
67
72
|
provider?: "lodestar" | "openai" | "anthropic" | "gemini" | "custom";
|
|
@@ -96,20 +101,114 @@ export interface ChatCreateParams {
|
|
|
96
101
|
abortIf?: Record<string, number | string>;
|
|
97
102
|
onAbort?: OnAbortConfig;
|
|
98
103
|
patches?: string[];
|
|
104
|
+
memoryModel?: string;
|
|
105
|
+
zdr?: boolean;
|
|
99
106
|
max_tokens?: number;
|
|
100
107
|
temperature?: number;
|
|
101
108
|
[extra: string]: unknown;
|
|
102
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
|
+
}
|
|
103
119
|
export interface LodestarMetadata {
|
|
120
|
+
trace_id?: string;
|
|
104
121
|
probes: Record<string, number>;
|
|
105
122
|
aborted: boolean;
|
|
106
123
|
abort_reason: string | null;
|
|
107
124
|
patches_applied: string[];
|
|
108
125
|
probe_ms: number;
|
|
126
|
+
zdr?: boolean;
|
|
109
127
|
early_exit_layer?: number | null;
|
|
110
128
|
vllm_model?: string;
|
|
129
|
+
model_version?: number;
|
|
130
|
+
lessons_evaluated?: number;
|
|
131
|
+
lessons_applied?: AppliedLessonTelemetry[];
|
|
111
132
|
remediation?: LodestarRemediationMetadata;
|
|
112
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
|
+
}
|
|
113
212
|
export interface ChatCompletionResponse {
|
|
114
213
|
id: string;
|
|
115
214
|
object: "chat.completion";
|
|
@@ -127,6 +226,68 @@ export interface ChatCompletionResponse {
|
|
|
127
226
|
};
|
|
128
227
|
lodestar: LodestarMetadata;
|
|
129
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
|
+
}
|
|
130
291
|
export declare class LodestarAPIError extends Error {
|
|
131
292
|
statusCode: number;
|
|
132
293
|
body: string;
|
|
@@ -134,10 +295,12 @@ export declare class LodestarAPIError extends Error {
|
|
|
134
295
|
}
|
|
135
296
|
export declare class Lodestar {
|
|
136
297
|
readonly baseUrl: string;
|
|
298
|
+
readonly zdr: boolean;
|
|
137
299
|
private readonly apiKey;
|
|
138
300
|
private readonly _fetch;
|
|
139
301
|
constructor(opts: LodestarOptions);
|
|
140
302
|
private headers;
|
|
303
|
+
private requestText;
|
|
141
304
|
private request;
|
|
142
305
|
readonly probes: {
|
|
143
306
|
list: (model?: string) => Promise<unknown[]>;
|
|
@@ -145,10 +308,53 @@ export declare class Lodestar {
|
|
|
145
308
|
create: (params: ProbeCreateParams) => Promise<ProbeCreateResponse>;
|
|
146
309
|
export: (probeId: string) => Promise<Record<string, unknown>>;
|
|
147
310
|
};
|
|
311
|
+
private _chatCreate;
|
|
312
|
+
private _chatStream;
|
|
148
313
|
readonly chat: {
|
|
149
314
|
create: (params: ChatCreateParams) => Promise<ChatCompletionResponse>;
|
|
150
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>>;
|
|
151
358
|
};
|
|
152
|
-
models: () => Promise<unknown[]>;
|
|
153
359
|
usage: (limit?: number) => Promise<Record<string, unknown>>;
|
|
154
360
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const DEFAULT_BASE_URL = "https://api.lodestar.so/v1";
|
|
2
|
+
export const DEFAULT_MODEL = "lodestar-s1";
|
|
2
3
|
export class LodestarAPIError extends Error {
|
|
3
4
|
statusCode;
|
|
4
5
|
body;
|
|
@@ -11,6 +12,7 @@ export class LodestarAPIError extends Error {
|
|
|
11
12
|
}
|
|
12
13
|
export class Lodestar {
|
|
13
14
|
baseUrl;
|
|
15
|
+
zdr;
|
|
14
16
|
apiKey;
|
|
15
17
|
_fetch;
|
|
16
18
|
constructor(opts) {
|
|
@@ -20,14 +22,18 @@ export class Lodestar {
|
|
|
20
22
|
this.apiKey = key;
|
|
21
23
|
this.baseUrl = (opts.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
22
24
|
this._fetch = opts.fetch || globalThis.fetch;
|
|
25
|
+
this.zdr = Boolean(opts.zdr);
|
|
23
26
|
}
|
|
24
27
|
headers() {
|
|
25
|
-
|
|
28
|
+
const hdrs = {
|
|
26
29
|
Authorization: `Bearer ${this.apiKey}`,
|
|
27
30
|
"Content-Type": "application/json",
|
|
28
31
|
};
|
|
32
|
+
if (this.zdr)
|
|
33
|
+
hdrs["X-Lodestar-ZDR"] = "true";
|
|
34
|
+
return hdrs;
|
|
29
35
|
}
|
|
30
|
-
async
|
|
36
|
+
async requestText(method, path, body) {
|
|
31
37
|
const res = await this._fetch(`${this.baseUrl}${path}`, {
|
|
32
38
|
method,
|
|
33
39
|
headers: this.headers(),
|
|
@@ -46,7 +52,10 @@ export class Lodestar {
|
|
|
46
52
|
}
|
|
47
53
|
throw new LodestarAPIError(res.status, msg, text);
|
|
48
54
|
}
|
|
49
|
-
return
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
async request(method, path, body) {
|
|
58
|
+
return JSON.parse(await this.requestText(method, path, body));
|
|
50
59
|
}
|
|
51
60
|
probes = {
|
|
52
61
|
list: async (model) => {
|
|
@@ -55,88 +64,213 @@ export class Lodestar {
|
|
|
55
64
|
return out.data;
|
|
56
65
|
},
|
|
57
66
|
eval: (params) => this.request("POST", "/probes/eval", {
|
|
58
|
-
model: params.model ??
|
|
67
|
+
model: params.model ?? DEFAULT_MODEL,
|
|
59
68
|
probes: params.probes,
|
|
60
69
|
input: params.input,
|
|
61
70
|
messages: params.messages,
|
|
62
71
|
system: params.system,
|
|
72
|
+
...(params.zdr !== undefined ? { zdr: params.zdr } : {}),
|
|
63
73
|
}),
|
|
64
74
|
create: (params) => this.request("POST", "/probes/create", {
|
|
65
|
-
model: params.model ??
|
|
75
|
+
model: params.model ?? DEFAULT_MODEL,
|
|
66
76
|
name: params.name,
|
|
67
77
|
positive: params.positive,
|
|
68
78
|
negative: params.negative,
|
|
69
79
|
positive_label: params.positiveLabel ?? "Positive",
|
|
70
80
|
negative_label: params.negativeLabel ?? "Negative",
|
|
81
|
+
...(params.zdr !== undefined ? { zdr: params.zdr } : {}),
|
|
71
82
|
}),
|
|
72
83
|
export: (probeId) => this.request("GET", `/probes/${encodeURIComponent(probeId)}/export`),
|
|
73
84
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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({
|
|
87
126
|
...rest,
|
|
88
|
-
model: model ??
|
|
89
|
-
stream:
|
|
127
|
+
model: model ?? DEFAULT_MODEL,
|
|
128
|
+
stream: true,
|
|
90
129
|
...(Object.keys(lodestar).length ? { lodestar } : {}),
|
|
91
|
-
})
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const txt = await res.text();
|
|
116
|
-
throw new LodestarAPIError(res.status, txt.slice(0, 300), txt);
|
|
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);
|
|
117
154
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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);
|
|
121
200
|
while (true) {
|
|
122
|
-
const
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
for (const raw of lines) {
|
|
129
|
-
const line = raw.trim();
|
|
130
|
-
if (!line.startsWith("data:"))
|
|
131
|
-
continue;
|
|
132
|
-
const payload = line.slice(5).trim();
|
|
133
|
-
if (!payload || payload === "[DONE]")
|
|
134
|
-
continue;
|
|
135
|
-
yield JSON.parse(payload);
|
|
136
|
-
}
|
|
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));
|
|
137
207
|
}
|
|
138
|
-
}
|
|
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`),
|
|
139
226
|
};
|
|
140
|
-
models = async () => (await this.request("GET", "/models")).data
|
|
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
|
+
});
|
|
141
275
|
usage = (limit = 25) => this.request("GET", `/usage?limit=${limit}`);
|
|
142
276
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lodestar-ai-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Zero-dependency TypeScript/JavaScript SDK for Lodestar Glass-Box Inference & Open System-One Probes (api.lodestar.so/v1)",
|
|
5
5
|
"homepage": "https://lodestar.so/docs",
|
|
6
6
|
"repository": {
|