@gradiated/grad 0.0.0-stage → 0.2.0-alpha.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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/client.d.ts +71 -0
- package/dist/client.js +188 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +90 -0
- package/dist/core.d.ts +13 -0
- package/dist/core.js +1 -0
- package/dist/errors.d.ts +37 -0
- package/dist/errors.js +70 -0
- package/dist/generated/client.d.ts +532 -0
- package/dist/generated/client.js +596 -0
- package/dist/generated/index.d.ts +2 -0
- package/dist/generated/index.js +3 -0
- package/dist/generated/models.d.ts +998 -0
- package/dist/generated/models.js +9 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +16 -0
- package/dist/node/files.d.ts +26 -0
- package/dist/node/files.js +130 -0
- package/dist/pagination.d.ts +39 -0
- package/dist/pagination.js +73 -0
- package/dist/pending.d.ts +14 -0
- package/dist/pending.js +20 -0
- package/dist/polling.d.ts +25 -0
- package/dist/polling.js +63 -0
- package/dist/recipes.d.ts +220 -0
- package/dist/recipes.js +255 -0
- package/dist/resources/artifacts.d.ts +82 -0
- package/dist/resources/artifacts.js +122 -0
- package/dist/resources/datasets.d.ts +178 -0
- package/dist/resources/datasets.js +318 -0
- package/dist/resources/evals.d.ts +110 -0
- package/dist/resources/evals.js +258 -0
- package/dist/resources/misc.d.ts +31 -0
- package/dist/resources/misc.js +45 -0
- package/dist/resources/runs.d.ts +119 -0
- package/dist/resources/runs.js +245 -0
- package/dist/resources/sources.d.ts +118 -0
- package/dist/resources/sources.js +156 -0
- package/dist/rubrics.d.ts +87 -0
- package/dist/rubrics.js +167 -0
- package/dist/trajectory.d.ts +16 -0
- package/dist/trajectory.js +13 -0
- package/dist/transport.d.ts +229 -0
- package/dist/transport.js +678 -0
- package/package.json +53 -4
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ApiKey, ApiKeyScope, CreatedApiKey, TrainableModel, UsageRecord } from "../generated/models.js";
|
|
2
|
+
import type { RequestOptions } from "../transport.js";
|
|
3
|
+
import type { Core } from "../core.js";
|
|
4
|
+
import { Paginator } from "../pagination.js";
|
|
5
|
+
export declare class ModelsResource {
|
|
6
|
+
private readonly core;
|
|
7
|
+
constructor(core: Core);
|
|
8
|
+
/** Trainable base models with their adapters, recipes and prices. */
|
|
9
|
+
list(options?: RequestOptions): Promise<TrainableModel[]>;
|
|
10
|
+
}
|
|
11
|
+
export declare class KeysResource {
|
|
12
|
+
private readonly core;
|
|
13
|
+
constructor(core: Core);
|
|
14
|
+
/** Mints a scoped key; `secret` is shown only once. */
|
|
15
|
+
create(input: {
|
|
16
|
+
name: string;
|
|
17
|
+
scopes: ApiKeyScope[];
|
|
18
|
+
}, options?: RequestOptions): Promise<CreatedApiKey>;
|
|
19
|
+
list(options?: {
|
|
20
|
+
limit?: number;
|
|
21
|
+
} & RequestOptions): Paginator<ApiKey>;
|
|
22
|
+
revoke(id: string, options?: RequestOptions): Promise<ApiKey>;
|
|
23
|
+
}
|
|
24
|
+
export declare class UsageResource {
|
|
25
|
+
private readonly core;
|
|
26
|
+
constructor(core: Core);
|
|
27
|
+
/** Billed usage, newest first: one record per billed increment of a run or eval attempt. */
|
|
28
|
+
list(options?: {
|
|
29
|
+
limit?: number;
|
|
30
|
+
} & RequestOptions): Paginator<UsageRecord>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Paginator } from "../pagination.js";
|
|
2
|
+
export class ModelsResource {
|
|
3
|
+
core;
|
|
4
|
+
constructor(core) {
|
|
5
|
+
this.core = core;
|
|
6
|
+
}
|
|
7
|
+
/** Trainable base models with their adapters, recipes and prices. */
|
|
8
|
+
async list(options) {
|
|
9
|
+
return (await this.core.api.listBaseModels(options)).data;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class KeysResource {
|
|
13
|
+
core;
|
|
14
|
+
constructor(core) {
|
|
15
|
+
this.core = core;
|
|
16
|
+
}
|
|
17
|
+
/** Mints a scoped key; `secret` is shown only once. */
|
|
18
|
+
create(input, options) {
|
|
19
|
+
return this.core.api.createApiKey(input, options);
|
|
20
|
+
}
|
|
21
|
+
list(options = {}) {
|
|
22
|
+
const { limit, ...request } = options;
|
|
23
|
+
return new Paginator(async (cursor) => {
|
|
24
|
+
const page = await this.core.api.listApiKeys({ ...(limit !== undefined ? { limit } : {}), ...(cursor ? { cursor } : {}) }, request);
|
|
25
|
+
return { data: page.data, nextCursor: page.nextCursor };
|
|
26
|
+
}, request.signal);
|
|
27
|
+
}
|
|
28
|
+
revoke(id, options) {
|
|
29
|
+
return this.core.api.revokeApiKey(id, options);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export class UsageResource {
|
|
33
|
+
core;
|
|
34
|
+
constructor(core) {
|
|
35
|
+
this.core = core;
|
|
36
|
+
}
|
|
37
|
+
/** Billed usage, newest first: one record per billed increment of a run or eval attempt. */
|
|
38
|
+
list(options = {}) {
|
|
39
|
+
const { limit, ...request } = options;
|
|
40
|
+
return new Paginator(async (cursor) => {
|
|
41
|
+
const page = await this.core.api.listUsage({ ...(limit !== undefined ? { limit } : {}), ...(cursor ? { cursor } : {}) }, request);
|
|
42
|
+
return { data: page.data, nextCursor: page.nextCursor };
|
|
43
|
+
}, request.signal);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Artifact as ArtifactData, Checkpoint as CheckpointData, Deployment, MetricPoint, PlanRunResponse, RecipeSpec, ResolvedRunConfig, Run as RunData, RunStatus, SplitSpec } from "../generated/models.js";
|
|
2
|
+
import { type RequestOptions } from "../transport.js";
|
|
3
|
+
import type { Core } from "../core.js";
|
|
4
|
+
import { Paginator } from "../pagination.js";
|
|
5
|
+
import { Pending } from "../pending.js";
|
|
6
|
+
import { type WaitOptions } from "../polling.js";
|
|
7
|
+
import { type Recipe } from "../recipes.js";
|
|
8
|
+
import { Artifact, type DeployOptions, type DownloadOptions, type DownloadResult } from "./artifacts.js";
|
|
9
|
+
import { type DatasetRef } from "./datasets.js";
|
|
10
|
+
export declare const TERMINAL_RUN_STATUSES: readonly RunStatus[];
|
|
11
|
+
export interface RunParams {
|
|
12
|
+
dataset: DatasetRef;
|
|
13
|
+
name: string;
|
|
14
|
+
recipe: Recipe | RecipeSpec;
|
|
15
|
+
split?: SplitSpec;
|
|
16
|
+
/** Spend cap; the run stops with `stopped_budget` and keeps a checkpoint. */
|
|
17
|
+
maxCostUsd?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface RunWaitOptions extends WaitOptions {
|
|
20
|
+
/** Streams each metric point while waiting (uses the SSE metrics stream). */
|
|
21
|
+
onMetric?: (metric: MetricPoint) => void | Promise<void>;
|
|
22
|
+
/** Called with every status change. */
|
|
23
|
+
onStatus?: (status: RunStatus, run: Run) => void | Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
export interface MetricsOptions extends RequestOptions {
|
|
26
|
+
/** Keep the stream open until the run finishes (SSE with automatic reconnect). */
|
|
27
|
+
follow?: boolean;
|
|
28
|
+
/** Only points after this step (JSON pages). */
|
|
29
|
+
afterStep?: number;
|
|
30
|
+
}
|
|
31
|
+
/** A dry-run plan: resolved settings, warnings and cost before you spend anything. */
|
|
32
|
+
export interface RunPlan extends PlanRunResponse {
|
|
33
|
+
}
|
|
34
|
+
export declare class RunPlan {
|
|
35
|
+
constructor(data: PlanRunResponse);
|
|
36
|
+
/** Alias of `resolvedConfig` (the docs' name). */
|
|
37
|
+
get resolvedRecipe(): ResolvedRunConfig;
|
|
38
|
+
}
|
|
39
|
+
/** A checkpoint saved during a run; `promote()` turns it into a kept artifact. */
|
|
40
|
+
export interface Checkpoint extends CheckpointData {
|
|
41
|
+
}
|
|
42
|
+
export declare class Checkpoint {
|
|
43
|
+
#private;
|
|
44
|
+
constructor(core: Core, data: CheckpointData);
|
|
45
|
+
promote(options?: {
|
|
46
|
+
name?: string;
|
|
47
|
+
} & RequestOptions): Promise<Artifact>;
|
|
48
|
+
toJSON(): CheckpointData;
|
|
49
|
+
}
|
|
50
|
+
/** A training run. After `wait()`, `artifact` holds the produced artifact. */
|
|
51
|
+
export interface Run extends RunData {
|
|
52
|
+
}
|
|
53
|
+
export declare class Run {
|
|
54
|
+
#private;
|
|
55
|
+
/** The produced artifact, loaded by `wait()` when the run completes. */
|
|
56
|
+
artifact: Artifact | undefined;
|
|
57
|
+
constructor(core: Core, data: RunData);
|
|
58
|
+
private update;
|
|
59
|
+
refresh(options?: RequestOptions): Promise<this>;
|
|
60
|
+
/**
|
|
61
|
+
* Waits for a terminal status. Resolves for `completed` (with `artifact`
|
|
62
|
+
* loaded); throws RunFailedError for `failed`, `cancelled` and
|
|
63
|
+
* `stopped_budget` (resume with a raised cap) and `stopped_balance` (top up, then resume).
|
|
64
|
+
*/
|
|
65
|
+
wait(options?: RunWaitOptions): Promise<CompletedRun>;
|
|
66
|
+
private followUntilDone;
|
|
67
|
+
/**
|
|
68
|
+
* Metric points. With `follow: true` this streams live over SSE, reconnects
|
|
69
|
+
* with `Last-Event-ID` after drops, and ends when the run finishes.
|
|
70
|
+
*/
|
|
71
|
+
metrics(options?: MetricsOptions): AsyncGenerator<MetricPoint, void, undefined>;
|
|
72
|
+
cancel(options?: RequestOptions): Promise<this>;
|
|
73
|
+
/** Re-queues a stopped run from its last checkpoint (raise `maxCostUsd` after a budget stop). */
|
|
74
|
+
resume(options?: {
|
|
75
|
+
maxCostUsd?: number;
|
|
76
|
+
} & RequestOptions): Promise<this>;
|
|
77
|
+
checkpoints(options?: RequestOptions): Promise<Checkpoint[]>;
|
|
78
|
+
private requireArtifact;
|
|
79
|
+
/** Deploys the run's artifact (Cloud; self-hosted servers raise DeployUnavailableError). */
|
|
80
|
+
deploy(options?: DeployOptions): Promise<Deployment>;
|
|
81
|
+
/** Streams the run's artifact to disk with SHA-256 verification. */
|
|
82
|
+
download(dest: string, options?: DownloadOptions): Promise<DownloadResult>;
|
|
83
|
+
toJSON(): RunData & {
|
|
84
|
+
artifact?: ArtifactData;
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/** A run that finished `completed`, with its artifact loaded. */
|
|
88
|
+
export type CompletedRun = Run & {
|
|
89
|
+
artifact: Artifact;
|
|
90
|
+
artifactId: string;
|
|
91
|
+
};
|
|
92
|
+
export type PendingRun = Pending<Run, CompletedRun, RunWaitOptions>;
|
|
93
|
+
export declare class RunsResource {
|
|
94
|
+
private readonly core;
|
|
95
|
+
constructor(core: Core);
|
|
96
|
+
/** Validates the dataset, resolves defaults and estimates tokens and cost; nothing is billed. */
|
|
97
|
+
plan(params: RunParams, options?: RequestOptions): Promise<RunPlan>;
|
|
98
|
+
/** Queues a run: `await` it for the Run, or chain `.wait()`. */
|
|
99
|
+
create(params: RunParams, options?: RequestOptions): PendingRun;
|
|
100
|
+
get(id: string, options?: RequestOptions): Promise<Run>;
|
|
101
|
+
list(options?: {
|
|
102
|
+
limit?: number;
|
|
103
|
+
} & RequestOptions): Paginator<Run>;
|
|
104
|
+
wait(id: string, options?: RunWaitOptions): Promise<CompletedRun>;
|
|
105
|
+
cancel(id: string, options?: RequestOptions): Promise<Run>;
|
|
106
|
+
resume(id: string, options?: {
|
|
107
|
+
maxCostUsd?: number;
|
|
108
|
+
} & RequestOptions): Promise<Run>;
|
|
109
|
+
metrics(id: string, options?: MetricsOptions): AsyncGenerator<MetricPoint, void, undefined>;
|
|
110
|
+
checkpoints(id: string, options?: RequestOptions): Promise<Checkpoint[]>;
|
|
111
|
+
}
|
|
112
|
+
export declare class CheckpointsResource {
|
|
113
|
+
private readonly core;
|
|
114
|
+
constructor(core: Core);
|
|
115
|
+
/** Copies a checkpoint into a versioned artifact you keep. */
|
|
116
|
+
promote(id: string, options?: {
|
|
117
|
+
name?: string;
|
|
118
|
+
} & RequestOptions): Promise<Artifact>;
|
|
119
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { CODEC } from "../generated/client.js";
|
|
2
|
+
import { convertKeys, GradError } from "../transport.js";
|
|
3
|
+
import { GradUsageError, RunFailedError, WaitTimeoutError } from "../errors.js";
|
|
4
|
+
import { collect, pages, Paginator } from "../pagination.js";
|
|
5
|
+
import { Pending } from "../pending.js";
|
|
6
|
+
import { isTransient, pollUntil } from "../polling.js";
|
|
7
|
+
import { toRecipeSpec } from "../recipes.js";
|
|
8
|
+
import { Artifact, deployArtifact, downloadArtifact } from "./artifacts.js";
|
|
9
|
+
import { datasetName } from "./datasets.js";
|
|
10
|
+
export const TERMINAL_RUN_STATUSES = ["completed", "failed", "cancelled", "stopped_budget", "stopped_balance"];
|
|
11
|
+
const isTerminal = (status) => TERMINAL_RUN_STATUSES.includes(status);
|
|
12
|
+
export class RunPlan {
|
|
13
|
+
constructor(data) {
|
|
14
|
+
Object.assign(this, data);
|
|
15
|
+
}
|
|
16
|
+
/** Alias of `resolvedConfig` (the docs' name). */
|
|
17
|
+
get resolvedRecipe() {
|
|
18
|
+
return this.resolvedConfig;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const buildRunBody = async (params) => {
|
|
22
|
+
if (!params.name)
|
|
23
|
+
throw new GradUsageError("runs need a name.", "invalid_run");
|
|
24
|
+
return {
|
|
25
|
+
dataset: datasetName(params.dataset),
|
|
26
|
+
name: params.name,
|
|
27
|
+
recipe: await toRecipeSpec(params.recipe),
|
|
28
|
+
...(params.split !== undefined ? { split: params.split } : {}),
|
|
29
|
+
...(params.maxCostUsd !== undefined ? { maxCostUsd: params.maxCostUsd } : {}),
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
const decodeMetric = (data) => convertKeys(CODEC, "MetricPoint", JSON.parse(data), "decode");
|
|
33
|
+
const decodeRun = (data) => convertKeys(CODEC, "Run", JSON.parse(data), "decode");
|
|
34
|
+
export class Checkpoint {
|
|
35
|
+
#core;
|
|
36
|
+
constructor(core, data) {
|
|
37
|
+
this.#core = core;
|
|
38
|
+
Object.assign(this, data);
|
|
39
|
+
}
|
|
40
|
+
async promote(options = {}) {
|
|
41
|
+
const { name, ...request } = options;
|
|
42
|
+
return new Artifact(this.#core, await this.#core.api.promoteCheckpoint(this.id, name !== undefined ? { name } : {}, request));
|
|
43
|
+
}
|
|
44
|
+
toJSON() {
|
|
45
|
+
return { ...this };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class Run {
|
|
49
|
+
#core;
|
|
50
|
+
/** The produced artifact, loaded by `wait()` when the run completes. */
|
|
51
|
+
artifact;
|
|
52
|
+
constructor(core, data) {
|
|
53
|
+
this.#core = core;
|
|
54
|
+
this.artifact = undefined;
|
|
55
|
+
Object.assign(this, data);
|
|
56
|
+
}
|
|
57
|
+
update(data) {
|
|
58
|
+
Object.assign(this, data);
|
|
59
|
+
}
|
|
60
|
+
async refresh(options) {
|
|
61
|
+
this.update(await this.#core.api.getRun(this.id, options));
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Waits for a terminal status. Resolves for `completed` (with `artifact`
|
|
66
|
+
* loaded); throws RunFailedError for `failed`, `cancelled` and
|
|
67
|
+
* `stopped_budget` (resume with a raised cap) and `stopped_balance` (top up, then resume).
|
|
68
|
+
*/
|
|
69
|
+
async wait(options = {}) {
|
|
70
|
+
let lastStatus;
|
|
71
|
+
const notify = async () => {
|
|
72
|
+
if (this.status !== lastStatus) {
|
|
73
|
+
lastStatus = this.status;
|
|
74
|
+
await options.onStatus?.(this.status, this);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (options.onMetric) {
|
|
78
|
+
await this.followUntilDone(options, notify);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
await pollUntil(`run ${this.id}`, (signal) => this.#core.api.getRun(this.id, { signal }), (run) => isTerminal(run.status), options, this.#core.poll, async (run) => {
|
|
82
|
+
this.update(run);
|
|
83
|
+
await notify();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (this.status !== "completed")
|
|
87
|
+
throw new RunFailedError(this);
|
|
88
|
+
if (!this.artifactId) {
|
|
89
|
+
throw new GradError(`Run ${this.id} completed without an artifact; this is a server bug, please report it.`, { code: "missing_artifact" });
|
|
90
|
+
}
|
|
91
|
+
if (!this.artifact || this.artifact.id !== this.artifactId) {
|
|
92
|
+
this.artifact = new Artifact(this.#core, await this.#core.api.getArtifact(this.artifactId, { signal: options.signal }));
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
async followUntilDone(options, notify) {
|
|
97
|
+
const controller = new AbortController();
|
|
98
|
+
const onAbort = () => controller.abort(options.signal?.reason);
|
|
99
|
+
options.signal?.addEventListener("abort", onAbort, { once: true });
|
|
100
|
+
const timer = options.timeoutMs === undefined ? undefined : setTimeout(() => controller.abort(new WaitTimeoutError(`run ${this.id}`, options.timeoutMs ?? 0)), options.timeoutMs);
|
|
101
|
+
try {
|
|
102
|
+
for await (const metric of this.metrics({ follow: true, signal: controller.signal })) {
|
|
103
|
+
await options.onMetric?.(metric);
|
|
104
|
+
}
|
|
105
|
+
// metrics({ follow }) only returns after the final `status` event, which updated this run.
|
|
106
|
+
await notify();
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (controller.signal.reason instanceof WaitTimeoutError)
|
|
110
|
+
throw controller.signal.reason;
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
if (timer !== undefined)
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
options.signal?.removeEventListener("abort", onAbort);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Metric points. With `follow: true` this streams live over SSE, reconnects
|
|
121
|
+
* with `Last-Event-ID` after drops, and ends when the run finishes.
|
|
122
|
+
*/
|
|
123
|
+
async *metrics(options = {}) {
|
|
124
|
+
const { follow = false, afterStep, ...request } = options;
|
|
125
|
+
if (!follow) {
|
|
126
|
+
const fetchPage = (cursor) => this.#core.api.listRunMetrics(this.id, { ...(afterStep !== undefined ? { afterStep } : {}), ...(cursor ? { cursor } : {}) }, request);
|
|
127
|
+
for await (const page of pages(fetchPage, request.signal)) {
|
|
128
|
+
// The spec marks MetricPoint nullable, so page items are typed `| null`; the server never sends null.
|
|
129
|
+
for (const point of page.data)
|
|
130
|
+
if (point !== null)
|
|
131
|
+
yield point;
|
|
132
|
+
}
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
let lastEventId;
|
|
136
|
+
let failures = 0;
|
|
137
|
+
while (true) {
|
|
138
|
+
try {
|
|
139
|
+
for await (const event of this.#core.api.listRunMetricsStream(this.id, {}, { ...request, ...(lastEventId !== undefined ? { lastEventId } : {}) })) {
|
|
140
|
+
failures = 0;
|
|
141
|
+
if (event.event === "metric") {
|
|
142
|
+
if (event.id !== undefined)
|
|
143
|
+
lastEventId = event.id;
|
|
144
|
+
const point = decodeMetric(event.data);
|
|
145
|
+
if (afterStep === undefined || point.step > afterStep)
|
|
146
|
+
yield point;
|
|
147
|
+
}
|
|
148
|
+
else if (event.event === "status") {
|
|
149
|
+
this.update(decodeRun(event.data));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// The stream closed without a final status (proxy timeout, deploy): reconnect.
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
if (request.signal?.aborted || !isTransient(error))
|
|
157
|
+
throw error;
|
|
158
|
+
}
|
|
159
|
+
failures += 1;
|
|
160
|
+
await this.#core.poll.sleep(Math.min(500 * 2 ** Math.min(failures - 1, 4), 5_000), request.signal);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async cancel(options) {
|
|
164
|
+
this.update(await this.#core.api.cancelRun(this.id, options));
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
/** Re-queues a stopped run from its last checkpoint (raise `maxCostUsd` after a budget stop). */
|
|
168
|
+
async resume(options = {}) {
|
|
169
|
+
const { maxCostUsd, ...request } = options;
|
|
170
|
+
this.update(await this.#core.api.resumeRun(this.id, maxCostUsd !== undefined ? { maxCostUsd } : {}, request));
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
async checkpoints(options = {}) {
|
|
174
|
+
const fetchPage = (cursor) => this.#core.api.listRunCheckpoints(this.id, cursor ? { cursor } : {}, options);
|
|
175
|
+
return (await collect(pages(fetchPage, options.signal))).map((c) => new Checkpoint(this.#core, c));
|
|
176
|
+
}
|
|
177
|
+
requireArtifact(action) {
|
|
178
|
+
if (!this.artifactId) {
|
|
179
|
+
throw new GradUsageError(`Run ${this.id} has no artifact to ${action} (status ${this.status}); wait() for it to complete first.`, "no_artifact");
|
|
180
|
+
}
|
|
181
|
+
return this.artifactId;
|
|
182
|
+
}
|
|
183
|
+
/** Deploys the run's artifact (Cloud; self-hosted servers raise DeployUnavailableError). */
|
|
184
|
+
async deploy(options) {
|
|
185
|
+
return deployArtifact(this.#core, this.requireArtifact("deploy"), options);
|
|
186
|
+
}
|
|
187
|
+
/** Streams the run's artifact to disk with SHA-256 verification. */
|
|
188
|
+
async download(dest, options) {
|
|
189
|
+
return downloadArtifact(this.#core, this.requireArtifact("download"), dest, options);
|
|
190
|
+
}
|
|
191
|
+
toJSON() {
|
|
192
|
+
const { artifact, ...rest } = this;
|
|
193
|
+
return { ...rest, ...(artifact ? { artifact: artifact.toJSON() } : {}) };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
export class RunsResource {
|
|
197
|
+
core;
|
|
198
|
+
constructor(core) {
|
|
199
|
+
this.core = core;
|
|
200
|
+
}
|
|
201
|
+
/** Validates the dataset, resolves defaults and estimates tokens and cost; nothing is billed. */
|
|
202
|
+
async plan(params, options) {
|
|
203
|
+
return new RunPlan(await this.core.api.planRun(await buildRunBody(params), options));
|
|
204
|
+
}
|
|
205
|
+
/** Queues a run: `await` it for the Run, or chain `.wait()`. */
|
|
206
|
+
create(params, options) {
|
|
207
|
+
return new Pending((async () => new Run(this.core, await this.core.api.createRun(await buildRunBody(params), options)))());
|
|
208
|
+
}
|
|
209
|
+
async get(id, options) {
|
|
210
|
+
return new Run(this.core, await this.core.api.getRun(id, options));
|
|
211
|
+
}
|
|
212
|
+
list(options = {}) {
|
|
213
|
+
const { limit, ...request } = options;
|
|
214
|
+
return new Paginator(async (cursor) => {
|
|
215
|
+
const page = await this.core.api.listRuns({ ...(limit !== undefined ? { limit } : {}), ...(cursor ? { cursor } : {}) }, request);
|
|
216
|
+
return { data: page.data.map((r) => new Run(this.core, r)), nextCursor: page.nextCursor };
|
|
217
|
+
}, request.signal);
|
|
218
|
+
}
|
|
219
|
+
async wait(id, options) {
|
|
220
|
+
return (await this.get(id)).wait(options);
|
|
221
|
+
}
|
|
222
|
+
async cancel(id, options) {
|
|
223
|
+
return new Run(this.core, await this.core.api.cancelRun(id, options));
|
|
224
|
+
}
|
|
225
|
+
async resume(id, options = {}) {
|
|
226
|
+
const { maxCostUsd, ...request } = options;
|
|
227
|
+
return new Run(this.core, await this.core.api.resumeRun(id, maxCostUsd !== undefined ? { maxCostUsd } : {}, request));
|
|
228
|
+
}
|
|
229
|
+
metrics(id, options) {
|
|
230
|
+
return new Run(this.core, { id }).metrics(options);
|
|
231
|
+
}
|
|
232
|
+
checkpoints(id, options) {
|
|
233
|
+
return new Run(this.core, { id }).checkpoints(options);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
export class CheckpointsResource {
|
|
237
|
+
core;
|
|
238
|
+
constructor(core) {
|
|
239
|
+
this.core = core;
|
|
240
|
+
}
|
|
241
|
+
/** Copies a checkpoint into a versioned artifact you keep. */
|
|
242
|
+
promote(id, options = {}) {
|
|
243
|
+
return new Checkpoint(this.core, { id }).promote(options);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Secret as SecretData, Source as SourceData, SourceProvider as SourceProviderData } from "../generated/models.js";
|
|
2
|
+
import type { RequestOptions } from "../transport.js";
|
|
3
|
+
import type { Core } from "../core.js";
|
|
4
|
+
import { Paginator } from "../pagination.js";
|
|
5
|
+
import { type DatasetRef, type PendingImport } from "./datasets.js";
|
|
6
|
+
/** A workspace secret: its name and metadata, never its value. Pass it as `secret`. */
|
|
7
|
+
export interface SecretRef extends SecretData {
|
|
8
|
+
}
|
|
9
|
+
export declare class SecretRef {
|
|
10
|
+
constructor(data: SecretData);
|
|
11
|
+
toString(): string;
|
|
12
|
+
}
|
|
13
|
+
/** A secret reference (`await grad.secrets.get(name)`) or a secret name. */
|
|
14
|
+
export type SecretLike = SecretRef | {
|
|
15
|
+
name: string;
|
|
16
|
+
} | string;
|
|
17
|
+
export declare const secretName: (secret: SecretLike) => string;
|
|
18
|
+
export interface ProviderOptions {
|
|
19
|
+
host?: string;
|
|
20
|
+
secret?: SecretLike;
|
|
21
|
+
project?: string;
|
|
22
|
+
filters?: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
export interface GradiatedOptions {
|
|
25
|
+
models?: string[];
|
|
26
|
+
tasks?: string[];
|
|
27
|
+
versions?: string[];
|
|
28
|
+
filters?: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export type SourceProvider = SourceProviderData;
|
|
31
|
+
/**
|
|
32
|
+
* Observability providers a dataset can sync from:
|
|
33
|
+
* `dataset.addSource({ provider: sources.langfuse({ host, secret }), schedule: "*\/15 * * * *" })`.
|
|
34
|
+
*/
|
|
35
|
+
export declare const sources: {
|
|
36
|
+
/** Langfuse traces and scores; the secret is `PUBLIC_KEY:SECRET_KEY`. */
|
|
37
|
+
langfuse: (options?: ProviderOptions) => SourceProvider;
|
|
38
|
+
/** LangSmith runs and feedback, including corrections. */
|
|
39
|
+
langsmith: (options?: ProviderOptions) => SourceProvider;
|
|
40
|
+
/** Braintrust project logs, scores and `expected` answers (`project` is required). */
|
|
41
|
+
braintrust: (options?: ProviderOptions) => SourceProvider;
|
|
42
|
+
/** PostHog LLM events and survey feedback (`project` is the numeric project id). */
|
|
43
|
+
posthog: (options?: ProviderOptions) => SourceProvider;
|
|
44
|
+
/** Arize Phoenix spans and annotations (`host` is required). */
|
|
45
|
+
phoenix: (options?: ProviderOptions) => SourceProvider;
|
|
46
|
+
/** Gradiated Cloud gateway traces and feedback (Cloud only; no host or secret). */
|
|
47
|
+
gradiated: (options?: GradiatedOptions) => SourceProvider;
|
|
48
|
+
};
|
|
49
|
+
export interface AddSourceOptions extends RequestOptions {
|
|
50
|
+
provider: SourceProvider;
|
|
51
|
+
/** Five-field UTC cron (fires at most once a minute); leave it out to sync only on request. */
|
|
52
|
+
schedule?: string;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* An observability provider synced into a dataset; each sync adds a dataset version. A source
|
|
56
|
+
* feeds exactly one dataset; a dataset can have several sources, and deleting the dataset deletes
|
|
57
|
+
* them. `status` is `"paused"` after 10 failed syncs in a row (`pausedReason`, `lastError` say
|
|
58
|
+
* why); `sync()` or a new value for its secret resumes it.
|
|
59
|
+
*/
|
|
60
|
+
export interface Source extends SourceData {
|
|
61
|
+
}
|
|
62
|
+
export declare class Source {
|
|
63
|
+
#private;
|
|
64
|
+
constructor(core: Core, data: SourceData);
|
|
65
|
+
refresh(options?: RequestOptions): Promise<this>;
|
|
66
|
+
/**
|
|
67
|
+
* Syncs now; `await source.sync().wait()` blocks until the new version exists.
|
|
68
|
+
* `full: true` rebuilds the dataset from the provider. A sync in progress is returned as is
|
|
69
|
+
* (a full request then runs right after it); within a minute of the last sync's start, that
|
|
70
|
+
* sync is returned. Resumes a paused source.
|
|
71
|
+
*/
|
|
72
|
+
sync(options?: {
|
|
73
|
+
full?: boolean;
|
|
74
|
+
} & RequestOptions): PendingImport;
|
|
75
|
+
/** Disconnects the source; the dataset and its versions stay. */
|
|
76
|
+
remove(options?: RequestOptions): Promise<void>;
|
|
77
|
+
toJSON(): SourceData;
|
|
78
|
+
}
|
|
79
|
+
export declare const addSource: (core: Core, dataset: DatasetRef, options: AddSourceOptions) => Promise<Source>;
|
|
80
|
+
export declare class SecretsResource {
|
|
81
|
+
private readonly core;
|
|
82
|
+
constructor(core: Core);
|
|
83
|
+
/**
|
|
84
|
+
* Creates or replaces a secret. The value is encrypted at rest and never returned.
|
|
85
|
+
* `allowedHosts` are the only hosts the server sends it to (a source's host,
|
|
86
|
+
* huggingface.co for HF imports, a logger's host); leave it out for the public
|
|
87
|
+
* provider hosts. A secret for a self-hosted tool must name its host.
|
|
88
|
+
*/
|
|
89
|
+
set(name: string, value: string, options?: {
|
|
90
|
+
allowedHosts?: readonly string[];
|
|
91
|
+
} & RequestOptions): Promise<SecretRef>;
|
|
92
|
+
/** A reference to pass as `secret` (checks that it exists; never the value). */
|
|
93
|
+
get(name: string, options?: RequestOptions): Promise<SecretRef>;
|
|
94
|
+
list(options?: {
|
|
95
|
+
limit?: number;
|
|
96
|
+
} & RequestOptions): Paginator<SecretRef>;
|
|
97
|
+
/** Deletes a secret (409 ConflictError while a source uses it). */
|
|
98
|
+
delete(secret: SecretLike, options?: RequestOptions): Promise<void>;
|
|
99
|
+
}
|
|
100
|
+
export declare class SourcesResource {
|
|
101
|
+
private readonly core;
|
|
102
|
+
constructor(core: Core);
|
|
103
|
+
/**
|
|
104
|
+
* Connects `provider` to `dataset` (see also `dataset.addSource`). A source feeds one dataset;
|
|
105
|
+
* a dataset can have several sources.
|
|
106
|
+
*/
|
|
107
|
+
create(dataset: DatasetRef, options: AddSourceOptions): Promise<Source>;
|
|
108
|
+
get(id: string, options?: RequestOptions): Promise<Source>;
|
|
109
|
+
/** Sources, newest first; `dataset` keeps one dataset's. */
|
|
110
|
+
list(options?: {
|
|
111
|
+
dataset?: DatasetRef;
|
|
112
|
+
limit?: number;
|
|
113
|
+
} & RequestOptions): Paginator<Source>;
|
|
114
|
+
sync(id: string, options?: {
|
|
115
|
+
full?: boolean;
|
|
116
|
+
} & RequestOptions): PendingImport;
|
|
117
|
+
remove(id: string, options?: RequestOptions): Promise<void>;
|
|
118
|
+
}
|