返回 CodeWhale
index.d.ts
根目录 / npm / runtime-sdk / index.d.ts
1 import type { RuntimeEventEnvelope, DynamicToolCallParams, DynamicToolCallResult } from "./generated/runtime.js";
2 export type { FleetProfileDocument, FleetProfileDocumentKind, ThreadLoadedListParams, ThreadLoadedListResponse, ThreadSetNameParams, ThreadSetNameResponse, RuntimeImageInput, RuntimeEventEnvelope, RuntimeCapabilities, RuntimeExperimentalCapabilities, DynamicToolSpec, DynamicToolSpecOutput, DynamicToolItemStatus, DynamicToolCallParams, DynamicToolCallResult, DynamicToolCallContent, TurnEnvironmentParams } from "./generated/runtime.js";
3
4 export type FleetRunId = string;
5 export type FleetRunStatus =
6 | "pending"
7 | "queued"
8 | "running"
9 | "paused"
10 | "completed"
11 | "failed"
12 | "cancelled";
13
14 export type FleetRuntimeTarget = "this_computer" | "another_computer" | "cloud";
15 export type FleetWorkflowKind = "parallel";
16
17 export interface FleetWorkflowDescriptor {
18 id: string;
19 kind: FleetWorkflowKind;
20 }
21
22 export type FleetWorkerStatus =
23 | "unknown"
24 | "online"
25 | "busy"
26 | "offline"
27 | "unhealthy"
28 | "draining"
29 | "retired";
30
31 export type FleetArtifactKind =
32 | "log"
33 | "patch"
34 | "test_result"
35 | "report"
36 | "checkpoint"
37 | "receipt"
38 | string;
39
40 export interface FleetStatusSummary {
41 runs: number;
42 queued: number;
43 running: number;
44 completed: number;
45 partial: number;
46 failed: number;
47 restarted: number;
48 escalated: number;
49 transport_failed: number;
50 task_failed: number;
51 verifier_failed: number;
52 cancelled: number;
53 stale: number;
54 workers: Record<string, FleetWorkerStatus>;
55 }
56
57 export interface FleetTaskStatusSummary {
58 task_id: string;
59 status: "enqueued" | "leased" | "completed" | "failed" | "cancelled";
60 leased_to?: string | null;
61 attempts: number;
62 }
63
64 export interface FleetRunSummary {
65 id: string;
66 name: string;
67 lifecycle_status: FleetRunStatus;
68 status: FleetStatusSummary;
69 target?: FleetRuntimeTarget | null;
70 workflow?: FleetWorkflowDescriptor | null;
71 roles: string[];
72 task_count: number;
73 worker_count: number;
74 tasks: FleetTaskStatusSummary[];
75 labels: Record<string, string>;
76 created_at: string;
77 updated_at?: string | null;
78 completed_at?: string | null;
79 }
80
81 export interface FleetRunDetail extends FleetRunSummary {
82 task_specs: FleetTaskSpec[];
83 worker_specs: FleetWorkerSpec[];
84 }
85
86 export interface FleetRunsResponse {
87 status: FleetStatusSummary;
88 runs: FleetRunSummary[];
89 }
90
91 export interface FleetTaskSpec {
92 id: string;
93 name: string;
94 description?: string | null;
95 objective?: string | null;
96 instructions: string;
97 worker?: FleetTaskWorkerProfile | null;
98 workspace?: FleetWorkspaceRequirements | null;
99 input_files?: string[];
100 context?: string[];
101 budget?: FleetTaskBudget | null;
102 tags?: string[];
103 expected_artifacts?: FleetArtifactKind[];
104 scorer?: Record<string, unknown> | null;
105 retry_policy?: Record<string, unknown> | null;
106 alert_policy?: FleetAlertPolicy | null;
107 timeout_seconds?: number | null;
108 metadata?: Record<string, unknown>;
109 }
110
111 export interface FleetTaskWorkerProfile {
112 agent_profile?: string | null;
113 role?: string | null;
114 loadout?: string | null;
115 model_class?: string | null;
116 model?: string | null;
117 tool_profile?: string | null;
118 tools?: string[];
119 capabilities?: string[];
120 }
121
122 export interface FleetWorkspaceRequirements {
123 root?: string | null;
124 required_files?: string[];
125 writable_paths?: string[];
126 environment?: FleetEnvironmentRequirements | null;
127 }
128
129 export interface FleetEnvironmentRequirements {
130 required?: string[];
131 allowlist?: string[];
132 }
133
134 export interface FleetTaskBudget {
135 max_tokens?: number | null;
136 /** Maximum model turns. Omitted, null, or zero means unbounded. */
137 max_steps?: number | null;
138 /** Maximum admitted tool calls. Omitted, null, or zero means unbounded. */
139 max_tool_calls?: number | null;
140 max_seconds?: number | null;
141 }
142
143 export interface FleetAlertPolicy {
144 events?: string[];
145 channels?: Array<Record<string, unknown>>;
146 after_attempts?: number | null;
147 after_minutes_stale?: number | null;
148 }
149
150 export interface FleetWorkerSpec {
151 id: string;
152 name: string;
153 host: Record<string, unknown>;
154 labels?: Record<string, string>;
155 capabilities?: string[];
156 max_concurrent_tasks?: number | null;
157 }
158
159 export interface FleetArtifactRef {
160 kind: FleetArtifactKind;
161 path: string;
162 checksum?: string | null;
163 mime_type?: string | null;
164 size_bytes?: number | null;
165 }
166
167 export type FleetWorkerEventPayload =
168 | { state: "queued" }
169 | { state: "leased"; lease_expires_at?: string | null }
170 | { state: "starting" }
171 | { state: "running" }
172 | { state: "model_wait"; model?: string | null }
173 | { state: "running_tool"; tool: string; call_id?: string | null }
174 | { state: "heartbeat"; cpu_percent?: number | null; memory_mb?: number | null }
175 | ({ state: "artifact" } & FleetArtifactRef)
176 | { state: "completed"; exit_code?: number | null; summary?: string | null }
177 | { state: "failed"; reason: string; recoverable?: boolean }
178 | { state: "cancelled"; cancelled_by?: string | null }
179 | { state: "interrupted"; signal?: string | null }
180 | { state: "stale"; last_heartbeat_at?: string | null }
181 | { state: "restarted"; restart_count?: number }
182 | { state: "escalated"; channel: string; alert_id?: string | null };
183
184 export interface FleetWorkerEvent {
185 seq: number;
186 run_id: string;
187 worker_id: string;
188 task_id: string;
189 timestamp: string;
190 label?: string;
191 payload: FleetWorkerEventPayload;
192 extra?: Record<string, unknown>;
193 }
194
195 export interface FleetWorkerInspection {
196 worker_id: string;
197 status: FleetWorkerStatus;
198 run_id?: string | null;
199 task_id?: string | null;
200 objective?: string | null;
201 role?: string | null;
202 host?: Record<string, unknown> | null;
203 latest_heartbeat_at?: string | null;
204 latest_event?: FleetWorkerEvent | null;
205 artifacts: FleetArtifactRef[];
206 last_error?: string | null;
207 alert_state?: Record<string, unknown> | null;
208 }
209
210 export interface FleetWorkersResponse {
211 run_id: string;
212 workers: FleetWorkerInspection[];
213 }
214
215 export interface FleetWorkerActionResponse {
216 action: "interrupt" | "restart" | "stop";
217 worker: FleetWorkerInspection;
218 }
219
220 export interface StopFleetRunResponse {
221 action: "stop";
222 run_id: string;
223 stopped: number;
224 status: FleetStatusSummary;
225 }
226
227 export interface ManagedFleetRole {
228 name: string;
229 agent_profile?: string | null;
230 }
231
232 export interface ManagedFleetWorkflow extends FleetWorkflowDescriptor {
233 tasks: FleetTaskSpec[];
234 }
235
236 export interface FleetRunCreateSpec {
237 name?: string;
238 target: FleetRuntimeTarget;
239 roles: ManagedFleetRole[];
240 workflow: ManagedFleetWorkflow;
241 labels?: Record<string, string>;
242 max_workers?: number;
243 }
244
245 export interface CreateFleetRunResponse {
246 execution: "awaiting_start";
247 run: FleetRunDetail;
248 warnings: string[];
249 }
250
251 export interface StartFleetRunResponse {
252 action: "start";
253 execution: "scheduled";
254 run_id: string;
255 target: "this_computer";
256 /** Leasing begins only after the scheduled driver owns the run. */
257 leased: 0;
258 queued: number;
259 worker_ids: string[];
260 }
261
262 export interface FleetRuntimeEvent {
263 cursor: string;
264 event: string;
265 run_id: string;
266 worker_id?: string | null;
267 task_id?: string | null;
268 timestamp?: string | null;
269 worker_seq?: number | null;
270 payload: Record<string, unknown>;
271 }
272
273 export interface FleetStreamControlEvent {
274 event:
275 | "fleet.replay.truncated"
276 | "fleet.replay.cursor_unavailable"
277 | "fleet.stream.error";
278 run_id?: string;
279 cursor?: never;
280 reload_projection?: boolean;
281 retryable?: boolean;
282 }
283
284 export type FleetStreamEvent = FleetRuntimeEvent | FleetStreamControlEvent;
285
286 export interface FleetEventReplay {
287 run_id: string;
288 events: FleetRuntimeEvent[];
289 has_more: boolean;
290 history_truncated: boolean;
291 next_cursor?: string | null;
292 }
293
294 export interface FleetEventOptions {
295 after?: string;
296 limit?: number;
297 }
298
299 export interface RuntimeClientOptions {
300 baseUrl?: string;
301 token?: string;
302 fetch?: typeof fetch;
303 }
304
305 export interface ThreadRuntimeEvent extends Pick<RuntimeEventEnvelope, "seq" | "event" | "thread_id" | "timestamp"> {
306 schema_version?: number;
307 previous_seq?: number;
308 turn_id?: string | null;
309 item_id?: string | null;
310 payload: Record<string, unknown>;
311 }
312
313 /** Transport progress at the existing journal cursor; never a new event. */
314 export interface ThreadStreamProgress {
315 schema_version?: number;
316 event: "stream.progress";
317 kind?: "stream.progress";
318 thread_id: string;
319 seq: number;
320 state: "replaying" | "live";
321 }
322
323 /**
324 * The final frame whenever the Runtime ends a thread stream on purpose. It is
325 * not a journal event (no `seq`). Resume with `sinceSeq: last_seq` when
326 * `retryable`; stop and fall back to the thread snapshot otherwise. A stream
327 * that ends without this frame lost its connection; resume from your cursor.
328 */
329 export interface ThreadStreamEnd {
330 schema_version: number;
331 event: "stream.end";
332 kind: "stream.end";
333 thread_id: string;
334 reason: "replay_failed" | "catch_up_failed" | "runtime_shutdown" | (string & {});
335 last_seq: number;
336 retryable: boolean;
337 }
338
339 /** Narrow an item yielded by threadEvents; journal event names are open-ended. */
340 export function isThreadStreamEnd(
341 event: ThreadRuntimeEvent | ThreadStreamProgress | ThreadStreamEnd,
342 ): event is ThreadStreamEnd;
343
344 export interface ThreadEventOptions {
345 sinceSeq?: number;
346 replayLimit?: number;
347 signal?: AbortSignal;
348 }
349
350 export class RuntimeApiError extends Error {
351 status?: number;
352 method?: string;
353 path?: string;
354 body?: string;
355 }
356
357 export class RuntimeCapabilityError extends RuntimeApiError {
358 capability: string;
359 }
360
361 export class CodeWhaleRuntimeClient {
362 constructor(options?: RuntimeClientOptions);
363 deliverDynamicToolResult(
364 call: DynamicToolCallParams,
365 result: DynamicToolCallResult,
366 options?: { signal?: AbortSignal },
367 ): Promise<void>;
368 createFleetRun(spec: FleetRunCreateSpec): Promise<CreateFleetRunResponse>;
369 startFleetRun(runId: FleetRunId): Promise<StartFleetRunResponse>;
370 replayFleetEvents(
371 runId: FleetRunId,
372 options?: FleetEventOptions & { path?: string },
373 ): Promise<FleetEventReplay>;
374 listFleetRuns(): Promise<FleetRunsResponse>;
375 getFleetRun(runId: FleetRunId): Promise<FleetRunDetail>;
376 listFleetWorkers(runId: FleetRunId): Promise<FleetWorkersResponse>;
377 getFleetWorker(workerId: string): Promise<FleetWorkerInspection>;
378 interruptWorker(workerId: string): Promise<FleetWorkerActionResponse>;
379 stopWorker(workerId: string): Promise<FleetWorkerActionResponse>;
380 restartWorker(workerId: string): Promise<FleetWorkerActionResponse>;
381 stopFleetRun(runId: FleetRunId): Promise<StopFleetRunResponse>;
382 fleetEvents(
383 runId: FleetRunId,
384 options?: FleetEventOptions & { path?: string; signal?: AbortSignal },
385 ): AsyncIterable<FleetStreamEvent>;
386 threadEvents(threadId: string, options: ThreadEventOptions & { includeProgress: true }): AsyncIterable<ThreadRuntimeEvent | ThreadStreamProgress | ThreadStreamEnd>;
387 threadEvents(threadId: string, options?: ThreadEventOptions & { includeProgress?: false }): AsyncIterable<ThreadRuntimeEvent | ThreadStreamEnd>;
388 threadEvents(threadId: string, options: ThreadEventOptions & { includeProgress: boolean }): AsyncIterable<ThreadRuntimeEvent | ThreadStreamProgress | ThreadStreamEnd>;
389 }
390
391 export function createRuntimeClient(options?: RuntimeClientOptions): CodeWhaleRuntimeClient;
392
392 lines TYPESCRIPT