@agent-os-sdk/client 0.9.26 → 0.9.27
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/dist/client/AgentOsClient.d.ts +141 -0
- package/dist/client/AgentOsClient.d.ts.map +1 -0
- package/{src/client/AgentOsClient.ts → dist/client/AgentOsClient.js} +61 -105
- package/dist/client/HttpRequestBuilder.d.ts +50 -0
- package/dist/client/HttpRequestBuilder.d.ts.map +1 -0
- package/{src/client/HttpRequestBuilder.ts → dist/client/HttpRequestBuilder.js} +9 -41
- package/{src/client/OperationContext.ts → dist/client/OperationContext.d.ts} +6 -9
- package/dist/client/OperationContext.d.ts.map +1 -0
- package/dist/client/OperationContext.js +13 -0
- package/dist/client/OperationContextProvider.d.ts +54 -0
- package/dist/client/OperationContextProvider.d.ts.map +1 -0
- package/{src/client/OperationContextProvider.ts → dist/client/OperationContextProvider.js} +17 -35
- package/{src/client/auth.ts → dist/client/auth.d.ts} +29 -65
- package/dist/client/auth.d.ts.map +1 -0
- package/dist/client/auth.js +38 -0
- package/dist/client/config.d.ts +49 -0
- package/dist/client/config.d.ts.map +1 -0
- package/{src/client/config.ts → dist/client/config.js} +5 -43
- package/dist/client/helpers.d.ts +63 -0
- package/dist/client/helpers.d.ts.map +1 -0
- package/{src/client/helpers.ts → dist/client/helpers.js} +9 -52
- package/dist/client/pagination.d.ts +105 -0
- package/dist/client/pagination.d.ts.map +1 -0
- package/{src/client/pagination.ts → dist/client/pagination.js} +15 -116
- package/dist/client/raw.d.ts +277 -0
- package/dist/client/raw.d.ts.map +1 -0
- package/dist/client/raw.js +337 -0
- package/dist/client/retry.d.ts +37 -0
- package/dist/client/retry.d.ts.map +1 -0
- package/{src/client/retry.ts → dist/client/retry.js} +13 -55
- package/dist/client/sanitize.d.ts +19 -0
- package/dist/client/sanitize.d.ts.map +1 -0
- package/{src/client/sanitize.ts → dist/client/sanitize.js} +4 -7
- package/dist/client/timeout.d.ts +26 -0
- package/dist/client/timeout.d.ts.map +1 -0
- package/{src/client/timeout.ts → dist/client/timeout.js} +10 -18
- package/dist/errors/factory.d.ts +22 -0
- package/dist/errors/factory.d.ts.map +1 -0
- package/{src/errors/factory.ts → dist/errors/factory.js} +18 -57
- package/dist/errors/index.d.ts +210 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/{src/errors/index.ts → dist/errors/index.js} +74 -156
- package/{src/generated/client.ts → dist/generated/client.d.ts} +5 -17
- package/dist/generated/client.d.ts.map +1 -0
- package/dist/generated/client.js +15 -0
- package/{src/generated/index.ts → dist/generated/index.d.ts} +1 -1
- package/dist/generated/index.d.ts.map +1 -0
- package/dist/generated/index.js +1 -0
- package/{src/generated/openapi.ts → dist/generated/openapi.d.ts} +1 -1
- package/dist/generated/openapi.d.ts.map +1 -0
- package/dist/generated/openapi.js +5 -0
- package/dist/index.d.ts +69 -0
- package/dist/index.d.ts.map +1 -0
- package/{src/index.ts → dist/index.js} +13 -63
- package/dist/modules/a2a.d.ts +48 -0
- package/dist/modules/a2a.d.ts.map +1 -0
- package/dist/modules/a2a.js +37 -0
- package/dist/modules/agents.d.ts +346 -0
- package/dist/modules/agents.d.ts.map +1 -0
- package/dist/modules/agents.js +296 -0
- package/dist/modules/apiTokens.d.ts +62 -0
- package/dist/modules/apiTokens.d.ts.map +1 -0
- package/dist/modules/apiTokens.js +56 -0
- package/dist/modules/approvals.d.ts +75 -0
- package/dist/modules/approvals.d.ts.map +1 -0
- package/dist/modules/approvals.js +91 -0
- package/dist/modules/audit.d.ts +78 -0
- package/dist/modules/audit.d.ts.map +1 -0
- package/{src/modules/audit.ts → dist/modules/audit.js} +23 -74
- package/{src/modules/auth.ts → dist/modules/auth.d.ts} +6 -11
- package/dist/modules/auth.d.ts.map +1 -0
- package/dist/modules/auth.js +22 -0
- package/dist/modules/builder.d.ts +242 -0
- package/dist/modules/builder.d.ts.map +1 -0
- package/dist/modules/builder.js +248 -0
- package/dist/modules/catalog.d.ts +142 -0
- package/dist/modules/catalog.d.ts.map +1 -0
- package/dist/modules/catalog.js +136 -0
- package/dist/modules/chatwoot.d.ts +59 -0
- package/dist/modules/chatwoot.d.ts.map +1 -0
- package/dist/modules/chatwoot.js +200 -0
- package/dist/modules/checkpoints.d.ts +62 -0
- package/dist/modules/checkpoints.d.ts.map +1 -0
- package/dist/modules/checkpoints.js +48 -0
- package/dist/modules/contracts.d.ts +48 -0
- package/dist/modules/contracts.d.ts.map +1 -0
- package/dist/modules/contracts.js +25 -0
- package/dist/modules/credentials.d.ts +105 -0
- package/dist/modules/credentials.d.ts.map +1 -0
- package/dist/modules/credentials.js +135 -0
- package/dist/modules/crons.d.ts +73 -0
- package/dist/modules/crons.d.ts.map +1 -0
- package/dist/modules/crons.js +67 -0
- package/dist/modules/datasets.d.ts +80 -0
- package/dist/modules/datasets.d.ts.map +1 -0
- package/dist/modules/datasets.js +91 -0
- package/{src/modules/evaluation.ts → dist/modules/evaluation.d.ts} +23 -109
- package/dist/modules/evaluation.d.ts.map +1 -0
- package/dist/modules/evaluation.js +108 -0
- package/dist/modules/files.d.ts +89 -0
- package/dist/modules/files.d.ts.map +1 -0
- package/dist/modules/files.js +133 -0
- package/dist/modules/graphs.d.ts +120 -0
- package/dist/modules/graphs.d.ts.map +1 -0
- package/dist/modules/graphs.js +69 -0
- package/{src/modules/improvements.ts → dist/modules/improvements.d.ts} +8 -27
- package/dist/modules/improvements.d.ts.map +1 -0
- package/dist/modules/improvements.js +27 -0
- package/dist/modules/info.d.ts +76 -0
- package/dist/modules/info.d.ts.map +1 -0
- package/{src/modules/info.ts → dist/modules/info.js} +16 -66
- package/dist/modules/knowledge.d.ts +91 -0
- package/dist/modules/knowledge.d.ts.map +1 -0
- package/dist/modules/knowledge.js +80 -0
- package/{src/modules/me.ts → dist/modules/me.d.ts} +7 -23
- package/dist/modules/me.d.ts.map +1 -0
- package/dist/modules/me.js +28 -0
- package/dist/modules/members.d.ts +124 -0
- package/dist/modules/members.d.ts.map +1 -0
- package/dist/modules/members.js +142 -0
- package/dist/modules/memberships.d.ts +25 -0
- package/dist/modules/memberships.d.ts.map +1 -0
- package/dist/modules/memberships.js +20 -0
- package/{src/modules/metaAgent.ts → dist/modules/metaAgent.d.ts} +8 -44
- package/dist/modules/metaAgent.d.ts.map +1 -0
- package/dist/modules/metaAgent.js +32 -0
- package/dist/modules/metrics.d.ts +20 -0
- package/dist/modules/metrics.d.ts.map +1 -0
- package/{src/modules/metrics.ts → dist/modules/metrics.js} +7 -14
- package/dist/modules/observability.d.ts +19 -0
- package/dist/modules/observability.d.ts.map +1 -0
- package/dist/modules/observability.js +14 -0
- package/dist/modules/playground.d.ts +42 -0
- package/dist/modules/playground.d.ts.map +1 -0
- package/dist/modules/playground.js +48 -0
- package/dist/modules/presets.d.ts +32 -0
- package/dist/modules/presets.d.ts.map +1 -0
- package/dist/modules/presets.js +199 -0
- package/dist/modules/prompts.d.ts +87 -0
- package/dist/modules/prompts.d.ts.map +1 -0
- package/dist/modules/prompts.js +87 -0
- package/dist/modules/roles.d.ts +64 -0
- package/dist/modules/roles.d.ts.map +1 -0
- package/dist/modules/roles.js +79 -0
- package/dist/modules/runs.d.ts +451 -0
- package/dist/modules/runs.d.ts.map +1 -0
- package/{src/modules/runs.ts → dist/modules/runs.js} +96 -438
- package/dist/modules/store.d.ts +37 -0
- package/dist/modules/store.d.ts.map +1 -0
- package/dist/modules/store.js +48 -0
- package/{src/modules/templates.ts → dist/modules/templates.d.ts} +8 -32
- package/dist/modules/templates.d.ts.map +1 -0
- package/dist/modules/templates.js +26 -0
- package/dist/modules/tenants.d.ts +49 -0
- package/dist/modules/tenants.d.ts.map +1 -0
- package/dist/modules/tenants.js +56 -0
- package/dist/modules/threads.d.ts +205 -0
- package/dist/modules/threads.d.ts.map +1 -0
- package/dist/modules/threads.js +225 -0
- package/dist/modules/tools.d.ts +62 -0
- package/dist/modules/tools.d.ts.map +1 -0
- package/dist/modules/tools.js +45 -0
- package/dist/modules/traces.d.ts +89 -0
- package/dist/modules/traces.d.ts.map +1 -0
- package/dist/modules/traces.js +65 -0
- package/{src/modules/triggers.ts → dist/modules/triggers.d.ts} +24 -153
- package/dist/modules/triggers.d.ts.map +1 -0
- package/dist/modules/triggers.js +176 -0
- package/{src/modules/usage.ts → dist/modules/usage.d.ts} +14 -46
- package/dist/modules/usage.d.ts.map +1 -0
- package/dist/modules/usage.js +43 -0
- package/dist/modules/vectorStores.d.ts +100 -0
- package/dist/modules/vectorStores.d.ts.map +1 -0
- package/dist/modules/vectorStores.js +159 -0
- package/dist/modules/workspaces.d.ts +138 -0
- package/dist/modules/workspaces.d.ts.map +1 -0
- package/dist/modules/workspaces.js +121 -0
- package/dist/sse/client.d.ts +140 -0
- package/dist/sse/client.d.ts.map +1 -0
- package/{src/sse/client.ts → dist/sse/client.js} +24 -100
- package/package.json +8 -15
- package/src/client/raw.ts +0 -609
- package/src/generated/swagger.json +0 -17014
- package/src/modules/a2a.ts +0 -64
- package/src/modules/agents.ts +0 -604
- package/src/modules/apiTokens.ts +0 -101
- package/src/modules/approvals.ts +0 -151
- package/src/modules/catalog.ts +0 -241
- package/src/modules/chatwoot.ts +0 -242
- package/src/modules/checkpoints.ts +0 -87
- package/src/modules/contracts.ts +0 -80
- package/src/modules/credentials.ts +0 -216
- package/src/modules/crons.ts +0 -115
- package/src/modules/datasets.ts +0 -142
- package/src/modules/files.ts +0 -208
- package/src/modules/members.ts +0 -199
- package/src/modules/memberships.ts +0 -42
- package/src/modules/observability.ts +0 -28
- package/src/modules/playground.ts +0 -68
- package/src/modules/presets.ts +0 -246
- package/src/modules/prompts.ts +0 -147
- package/src/modules/roles.ts +0 -112
- package/src/modules/store.ts +0 -65
- package/src/modules/tenants.ts +0 -79
- package/src/modules/threads.ts +0 -343
- package/src/modules/tools.ts +0 -91
- package/src/modules/traces.ts +0 -133
- package/src/modules/vectorStores.ts +0 -257
- package/src/modules/workspaces.ts +0 -216
|
@@ -1,182 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Runs Module - Core execution API (Fully Typed)
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* Naming conventions:
|
|
5
5
|
* - get* for singular items
|
|
6
|
-
* - list* for collections
|
|
6
|
+
* - list* for collections
|
|
7
7
|
* - create*, update*, delete* for mutations
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
import type { RawClient, APIResponse, components } from "../client/raw.js";
|
|
11
|
-
import type { PaginationParams, PaginatedResponse } from "../client/helpers.js";
|
|
12
|
-
import { parseSSE, type SSEEvent } from "../sse/client.js";
|
|
13
|
-
|
|
14
|
-
// Type aliases from OpenAPI
|
|
15
|
-
type WaitRunResponse = components["schemas"]["WaitRunResponse"];
|
|
16
|
-
type BatchRunResponse = components["schemas"]["BatchRunResponse"];
|
|
17
|
-
type CancelRunResponse = components["schemas"]["CancelRunResponse"];
|
|
18
|
-
type CheckpointIndexResponse = components["schemas"]["CheckpointIndexResponse"];
|
|
19
|
-
type RunReplayResponse = components["schemas"]["RunReplayResponse"];
|
|
20
|
-
|
|
21
|
-
// Response types
|
|
22
|
-
export interface Run {
|
|
23
|
-
run_id: string;
|
|
24
|
-
status: RunStatus;
|
|
25
|
-
thread_id?: string;
|
|
26
|
-
agent_id: string;
|
|
27
|
-
bundle_id?: string;
|
|
28
|
-
agent_version_id?: string;
|
|
29
|
-
tenant_id: string;
|
|
30
|
-
workspace_id: string;
|
|
31
|
-
input?: unknown;
|
|
32
|
-
output?: unknown;
|
|
33
|
-
error?: unknown;
|
|
34
|
-
current_attempt_id?: string;
|
|
35
|
-
current_attempt_no?: number;
|
|
36
|
-
latest_seq?: number;
|
|
37
|
-
created_at: string;
|
|
38
|
-
started_at?: string;
|
|
39
|
-
completed_at?: string;
|
|
40
|
-
duration_ms?: number;
|
|
41
|
-
reused?: boolean;
|
|
42
|
-
// HITL (Human-in-the-Loop) fields
|
|
43
|
-
human_prompt?: string;
|
|
44
|
-
checkpoint_id?: string;
|
|
45
|
-
// Replay fields
|
|
46
|
-
replayed_from_run_id?: string;
|
|
47
|
-
replayed_from_checkpoint_id?: string;
|
|
48
|
-
replay_mode?: "best_effort" | "strict";
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Run status values
|
|
52
|
-
export type RunStatus =
|
|
53
|
-
| "pending"
|
|
54
|
-
| "queued"
|
|
55
|
-
| "running"
|
|
56
|
-
| "completed"
|
|
57
|
-
| "failed"
|
|
58
|
-
| "cancelled"
|
|
59
|
-
| "waiting_for_human"
|
|
60
|
-
| "resumed";
|
|
61
|
-
|
|
62
|
-
export interface CreateRunResponse {
|
|
63
|
-
run_id: string;
|
|
64
|
-
status: string;
|
|
65
|
-
reused?: boolean;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export type RunListResponse = PaginatedResponse<Run>;
|
|
69
|
-
|
|
70
|
-
/** Wave 2.3: Seq-based polling response for execution events */
|
|
71
|
-
export interface RunEventsPollResponse {
|
|
72
|
-
events: RunEventDto[];
|
|
73
|
-
latest_seq: number;
|
|
74
|
-
next_after_seq: number | null;
|
|
75
|
-
has_more: boolean;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** Single event from seq-based polling */
|
|
79
|
-
export interface RunEventDto {
|
|
80
|
-
id: string;
|
|
81
|
-
seq: number;
|
|
82
|
-
type: string;
|
|
83
|
-
timestamp: string;
|
|
84
|
-
attempt_id: string;
|
|
85
|
-
payload: Record<string, unknown> | null;
|
|
86
|
-
operation_id: string;
|
|
87
|
-
parent_operation_id?: string | null;
|
|
88
|
-
root_operation_id: string;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface RunInspectionResponse {
|
|
92
|
-
run: Record<string, unknown>;
|
|
93
|
-
replay: Record<string, unknown>;
|
|
94
|
-
failure: Record<string, unknown>;
|
|
95
|
-
attempts: Array<Record<string, unknown>>;
|
|
96
|
-
node_executions: Array<Record<string, unknown>>;
|
|
97
|
-
events: Array<Record<string, unknown>>;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface RunFailureRetentionItem {
|
|
101
|
-
failure_id: string;
|
|
102
|
-
failure_scope: string;
|
|
103
|
-
run_id: string;
|
|
104
|
-
attempt_id: string;
|
|
105
|
-
attempt_no: number;
|
|
106
|
-
agent_id?: string | null;
|
|
107
|
-
thread_id: string;
|
|
108
|
-
node_execution_id?: string | null;
|
|
109
|
-
operation_id?: string | null;
|
|
110
|
-
capability_ref?: string | null;
|
|
111
|
-
capability_version?: string | null;
|
|
112
|
-
execution_binding?: string | null;
|
|
113
|
-
source_kind: string;
|
|
114
|
-
ancestry_kind: string;
|
|
115
|
-
status: string;
|
|
116
|
-
error_code: string;
|
|
117
|
-
error_category: string;
|
|
118
|
-
is_retryable: boolean;
|
|
119
|
-
error_source: string;
|
|
120
|
-
provider_error_code?: string | null;
|
|
121
|
-
error_summary: string;
|
|
122
|
-
retried_from_node_execution_id?: string | null;
|
|
123
|
-
replayed_from_node_execution_id?: string | null;
|
|
124
|
-
can_retry: boolean;
|
|
125
|
-
retry_from_node_execution_id?: string | null;
|
|
126
|
-
can_replay: boolean;
|
|
127
|
-
replay_from_node_execution_id?: string | null;
|
|
128
|
-
failed_at: string;
|
|
129
|
-
created_at: string;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface RunFailureRetentionListResponse {
|
|
133
|
-
items: RunFailureRetentionItem[];
|
|
134
|
-
count: number;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export interface RunFailureRetentionDetailResponse {
|
|
138
|
-
failure: RunFailureRetentionItem;
|
|
139
|
-
payload?: unknown;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export interface RunObservabilitySummaryResponse {
|
|
143
|
-
workspace_id: string;
|
|
144
|
-
agent_id?: string | null;
|
|
145
|
-
from?: string | null;
|
|
146
|
-
to?: string | null;
|
|
147
|
-
runs_total: number;
|
|
148
|
-
runs_completed: number;
|
|
149
|
-
runs_failed: number;
|
|
150
|
-
node_executions_total: number;
|
|
151
|
-
node_executions_failed: number;
|
|
152
|
-
retry_count: number;
|
|
153
|
-
replay_count: number;
|
|
154
|
-
handoff_count: number;
|
|
155
|
-
avg_run_latency_ms: number;
|
|
156
|
-
p95_run_latency_ms: number;
|
|
157
|
-
avg_node_latency_ms: number;
|
|
158
|
-
failure_rate: number;
|
|
159
|
-
top_error_categories: Array<{
|
|
160
|
-
error_category: string;
|
|
161
|
-
count: number;
|
|
162
|
-
}>;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export interface RunObservabilityCapabilityListResponse {
|
|
166
|
-
items: Array<Record<string, unknown>>;
|
|
167
|
-
count: number;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
export interface RunObservabilityBindingListResponse {
|
|
171
|
-
items: Array<Record<string, unknown>>;
|
|
172
|
-
count: number;
|
|
173
|
-
}
|
|
174
|
-
|
|
9
|
+
import { parseSSE } from "../sse/client.js";
|
|
175
10
|
export class RunsModule {
|
|
176
|
-
|
|
177
|
-
|
|
11
|
+
client;
|
|
12
|
+
constructor(client) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
}
|
|
178
15
|
// ======================== CRUD ========================
|
|
179
|
-
|
|
180
16
|
/**
|
|
181
17
|
* Create a new run.
|
|
182
18
|
* @example
|
|
@@ -185,7 +21,7 @@ export class RunsModule {
|
|
|
185
21
|
* agent_id: "agent-uuid",
|
|
186
22
|
* input: { message: "Hello" }
|
|
187
23
|
* });
|
|
188
|
-
*
|
|
24
|
+
*
|
|
189
25
|
* // With idempotency (safe to retry)
|
|
190
26
|
* const { data } = await client.runs.create({
|
|
191
27
|
* agent_id: "agent-uuid",
|
|
@@ -194,252 +30,174 @@ export class RunsModule {
|
|
|
194
30
|
* });
|
|
195
31
|
* ```
|
|
196
32
|
*/
|
|
197
|
-
async create(body
|
|
198
|
-
agent_id: string;
|
|
199
|
-
thread?: { thread_id?: string } | { new_thread: true };
|
|
200
|
-
input?: unknown;
|
|
201
|
-
/** Optional bundle pinning (draft/published). When omitted, backend resolves live bundle. */
|
|
202
|
-
bundle_id?: string;
|
|
203
|
-
/** Idempotency key for safe retries. When set, duplicate requests with the same key return the original response. */
|
|
204
|
-
idempotency_key?: string;
|
|
205
|
-
}): Promise<APIResponse<CreateRunResponse>> {
|
|
33
|
+
async create(body) {
|
|
206
34
|
// Send canonical X-Idempotency-Key header + body idempotency_key for backend contract parity.
|
|
207
|
-
const headers
|
|
35
|
+
const headers = {};
|
|
208
36
|
if (body.idempotency_key) {
|
|
209
37
|
headers["X-Idempotency-Key"] = body.idempotency_key;
|
|
210
38
|
}
|
|
211
|
-
|
|
212
|
-
return this.client.POST<CreateRunResponse>("/v1/api/runs", {
|
|
39
|
+
return this.client.POST("/v1/api/runs", {
|
|
213
40
|
body,
|
|
214
41
|
headers,
|
|
215
42
|
});
|
|
216
43
|
}
|
|
217
|
-
|
|
218
44
|
/**
|
|
219
45
|
* Get a run by ID.
|
|
220
46
|
*/
|
|
221
|
-
async get(runId
|
|
222
|
-
return this.client.GET
|
|
47
|
+
async get(runId) {
|
|
48
|
+
return this.client.GET("/v1/api/runs/{runId}", {
|
|
223
49
|
params: { path: { runId } },
|
|
224
50
|
});
|
|
225
51
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
return this.client.GET<RunInspectionResponse>("/v1/api/runs/{runId}/inspection", {
|
|
52
|
+
async getInspection(runId) {
|
|
53
|
+
return this.client.GET("/v1/api/runs/{runId}/inspection", {
|
|
229
54
|
params: { path: { runId } },
|
|
230
55
|
});
|
|
231
56
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
agent_id?: string;
|
|
235
|
-
capability_ref?: string;
|
|
236
|
-
error_category?: string;
|
|
237
|
-
retryable?: boolean;
|
|
238
|
-
from?: string;
|
|
239
|
-
to?: string;
|
|
240
|
-
limit?: number;
|
|
241
|
-
offset?: number;
|
|
242
|
-
}): Promise<APIResponse<RunFailureRetentionListResponse>> {
|
|
243
|
-
return this.client.GET<RunFailureRetentionListResponse>("/v1/api/runs/failures", {
|
|
57
|
+
async listFailures(params) {
|
|
58
|
+
return this.client.GET("/v1/api/runs/failures", {
|
|
244
59
|
params: { query: params },
|
|
245
60
|
});
|
|
246
61
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return this.client.GET<RunFailureRetentionDetailResponse>("/v1/api/runs/failures/{failureId}", {
|
|
62
|
+
async getFailure(failureId) {
|
|
63
|
+
return this.client.GET("/v1/api/runs/failures/{failureId}", {
|
|
250
64
|
params: { path: { failureId } },
|
|
251
65
|
});
|
|
252
66
|
}
|
|
253
|
-
|
|
254
67
|
/**
|
|
255
68
|
* List runs with optional filters.
|
|
256
69
|
*/
|
|
257
|
-
async list(params
|
|
258
|
-
|
|
259
|
-
agent_id?: string;
|
|
260
|
-
status?: string;
|
|
261
|
-
}): Promise<APIResponse<RunListResponse>> {
|
|
262
|
-
return this.client.GET<RunListResponse>("/v1/api/runs", {
|
|
70
|
+
async list(params) {
|
|
71
|
+
return this.client.GET("/v1/api/runs", {
|
|
263
72
|
params: { query: params },
|
|
264
73
|
});
|
|
265
74
|
}
|
|
266
|
-
|
|
267
75
|
/**
|
|
268
76
|
* Iterate through all runs with automatic pagination.
|
|
269
|
-
*
|
|
77
|
+
*
|
|
270
78
|
* @example
|
|
271
79
|
* ```ts
|
|
272
80
|
* // Stream all completed runs
|
|
273
81
|
* for await (const run of client.runs.iterate({ status: "completed" })) {
|
|
274
82
|
* console.log(run.run_id, run.status);
|
|
275
83
|
* }
|
|
276
|
-
*
|
|
84
|
+
*
|
|
277
85
|
* // With limit
|
|
278
86
|
* for await (const run of client.runs.iterate({ agent_id: "..." }, { maxItems: 100 })) {
|
|
279
87
|
* processRun(run);
|
|
280
88
|
* }
|
|
281
89
|
* ```
|
|
282
90
|
*/
|
|
283
|
-
async *iterate(
|
|
284
|
-
filters?: {
|
|
285
|
-
thread_id?: string;
|
|
286
|
-
agent_id?: string;
|
|
287
|
-
status?: string;
|
|
288
|
-
},
|
|
289
|
-
options?: { pageSize?: number; maxItems?: number; signal?: AbortSignal }
|
|
290
|
-
): AsyncGenerator<Run, void, unknown> {
|
|
91
|
+
async *iterate(filters, options) {
|
|
291
92
|
const pageSize = options?.pageSize ?? 100;
|
|
292
93
|
const maxItems = options?.maxItems ?? Infinity;
|
|
293
94
|
let offset = 0;
|
|
294
95
|
let yielded = 0;
|
|
295
96
|
let hasMore = true;
|
|
296
|
-
|
|
297
97
|
while (hasMore && yielded < maxItems) {
|
|
298
|
-
if (options?.signal?.aborted)
|
|
299
|
-
|
|
98
|
+
if (options?.signal?.aborted)
|
|
99
|
+
return;
|
|
300
100
|
const response = await this.list({
|
|
301
101
|
...filters,
|
|
302
102
|
limit: Math.min(pageSize, maxItems - yielded),
|
|
303
103
|
offset,
|
|
304
104
|
});
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
const data = response.data
|
|
308
|
-
|
|
105
|
+
if (response.error)
|
|
106
|
+
throw response.error;
|
|
107
|
+
const data = response.data;
|
|
309
108
|
for (const run of data.items) {
|
|
310
|
-
if (yielded >= maxItems)
|
|
109
|
+
if (yielded >= maxItems)
|
|
110
|
+
return;
|
|
311
111
|
yield run;
|
|
312
112
|
yielded++;
|
|
313
113
|
}
|
|
314
|
-
|
|
315
114
|
offset += data.items.length;
|
|
316
115
|
hasMore = offset < data.total && data.items.length > 0;
|
|
317
116
|
}
|
|
318
117
|
}
|
|
319
|
-
|
|
320
118
|
// ======================== Execution ========================
|
|
321
|
-
|
|
322
119
|
/**
|
|
323
120
|
* Wait for run completion synchronously.
|
|
324
121
|
*/
|
|
325
|
-
async wait(body
|
|
326
|
-
|
|
327
|
-
thread?: { thread_id?: string } | { new_thread: true };
|
|
328
|
-
input?: unknown;
|
|
329
|
-
timeout_seconds?: number;
|
|
330
|
-
}): Promise<APIResponse<WaitRunResponse>> {
|
|
331
|
-
return this.client.POST<WaitRunResponse>("/v1/api/runs/wait", {
|
|
122
|
+
async wait(body) {
|
|
123
|
+
return this.client.POST("/v1/api/runs/wait", {
|
|
332
124
|
body,
|
|
333
125
|
});
|
|
334
126
|
}
|
|
335
|
-
|
|
336
127
|
/**
|
|
337
128
|
* Create batch runs.
|
|
338
129
|
*/
|
|
339
|
-
async batch(body
|
|
340
|
-
|
|
341
|
-
inputs: Array<{ input: unknown; idempotency_key?: string }>;
|
|
342
|
-
}): Promise<APIResponse<BatchRunResponse>> {
|
|
343
|
-
return this.client.POST<BatchRunResponse>("/v1/api/runs/batch", {
|
|
130
|
+
async batch(body) {
|
|
131
|
+
return this.client.POST("/v1/api/runs/batch", {
|
|
344
132
|
body,
|
|
345
133
|
});
|
|
346
134
|
}
|
|
347
|
-
|
|
348
135
|
/**
|
|
349
136
|
* Cancel a running run.
|
|
350
137
|
*/
|
|
351
|
-
async cancel(runId
|
|
352
|
-
return this.client.POST
|
|
138
|
+
async cancel(runId, reason) {
|
|
139
|
+
return this.client.POST("/v1/api/runs/{runId}/cancel", {
|
|
353
140
|
params: { path: { runId } },
|
|
354
141
|
body: { reason },
|
|
355
142
|
});
|
|
356
143
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
agent_id?: string;
|
|
360
|
-
from?: string;
|
|
361
|
-
to?: string;
|
|
362
|
-
}): Promise<APIResponse<RunObservabilitySummaryResponse>> {
|
|
363
|
-
return this.client.GET<RunObservabilitySummaryResponse>("/v1/api/runs/observability/summary", {
|
|
144
|
+
async getObservabilitySummary(params) {
|
|
145
|
+
return this.client.GET("/v1/api/runs/observability/summary", {
|
|
364
146
|
params: { query: params },
|
|
365
147
|
});
|
|
366
148
|
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
agent_id?: string;
|
|
370
|
-
from?: string;
|
|
371
|
-
to?: string;
|
|
372
|
-
limit?: number;
|
|
373
|
-
offset?: number;
|
|
374
|
-
}): Promise<APIResponse<RunObservabilityCapabilityListResponse>> {
|
|
375
|
-
return this.client.GET<RunObservabilityCapabilityListResponse>("/v1/api/runs/observability/capabilities", {
|
|
149
|
+
async getObservabilityCapabilities(params) {
|
|
150
|
+
return this.client.GET("/v1/api/runs/observability/capabilities", {
|
|
376
151
|
params: { query: params },
|
|
377
152
|
});
|
|
378
153
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
agent_id?: string;
|
|
382
|
-
from?: string;
|
|
383
|
-
to?: string;
|
|
384
|
-
limit?: number;
|
|
385
|
-
offset?: number;
|
|
386
|
-
}): Promise<APIResponse<RunObservabilityBindingListResponse>> {
|
|
387
|
-
return this.client.GET<RunObservabilityBindingListResponse>("/v1/api/runs/observability/bindings", {
|
|
154
|
+
async getObservabilityBindings(params) {
|
|
155
|
+
return this.client.GET("/v1/api/runs/observability/bindings", {
|
|
388
156
|
params: { query: params },
|
|
389
157
|
});
|
|
390
158
|
}
|
|
391
|
-
|
|
392
159
|
/**
|
|
393
160
|
* Resume a run waiting for human input (HITL).
|
|
394
161
|
* After the run reaches status 'waiting_for_human', use this to provide
|
|
395
162
|
* the human response and continue execution.
|
|
396
|
-
*
|
|
163
|
+
*
|
|
397
164
|
* @param runId - The run ID to resume
|
|
398
165
|
* @param body - The human input to provide
|
|
399
166
|
* @returns Updated run (status will change to 'resumed' then 'queued')
|
|
400
|
-
*
|
|
167
|
+
*
|
|
401
168
|
* @example
|
|
402
169
|
* ```ts
|
|
403
170
|
* // List runs waiting for human input
|
|
404
171
|
* const waiting = await client.runs.list({ status: 'waiting_for_human' })
|
|
405
|
-
*
|
|
172
|
+
*
|
|
406
173
|
* // Get the prompt
|
|
407
174
|
* const run = await client.runs.get(runId)
|
|
408
175
|
* console.log(run.human_prompt) // "Deseja continuar com a compra?"
|
|
409
|
-
*
|
|
176
|
+
*
|
|
410
177
|
* // Respond
|
|
411
178
|
* await client.runs.resume(runId, { input: { answer: "sim" } })
|
|
412
179
|
* ```
|
|
413
180
|
*/
|
|
414
|
-
async resume(runId
|
|
415
|
-
return this.client.POST
|
|
181
|
+
async resume(runId, body) {
|
|
182
|
+
return this.client.POST("/v1/api/runs/{runId}/resume", {
|
|
416
183
|
params: { path: { runId } },
|
|
417
184
|
body,
|
|
418
185
|
});
|
|
419
186
|
}
|
|
420
|
-
|
|
421
187
|
/**
|
|
422
188
|
* Rerun from start (stateless).
|
|
423
189
|
*/
|
|
424
|
-
async rerun(runId
|
|
425
|
-
return this.client.POST
|
|
190
|
+
async rerun(runId) {
|
|
191
|
+
return this.client.POST("/v1/api/runs/{runId}/rerun", {
|
|
426
192
|
params: { path: { runId } },
|
|
427
193
|
});
|
|
428
194
|
}
|
|
429
|
-
|
|
430
195
|
/**
|
|
431
196
|
* Replay from checkpoint (stateful).
|
|
432
197
|
* Creates a NEW run that starts from the specified checkpoint.
|
|
433
198
|
*/
|
|
434
|
-
async replay(
|
|
435
|
-
runId
|
|
436
|
-
checkpointId: string,
|
|
437
|
-
options?: {
|
|
438
|
-
mode?: "best_effort" | "strict";
|
|
439
|
-
reason?: string;
|
|
440
|
-
}
|
|
441
|
-
): Promise<APIResponse<RunReplayResponse>> {
|
|
442
|
-
return this.client.POST<RunReplayResponse>("/v1/api/runs/{runId}/checkpoints/{checkpointId}/replay", {
|
|
199
|
+
async replay(runId, checkpointId, options) {
|
|
200
|
+
return this.client.POST("/v1/api/runs/{runId}/checkpoints/{checkpointId}/replay", {
|
|
443
201
|
params: { path: { runId, checkpointId } },
|
|
444
202
|
body: {
|
|
445
203
|
mode: options?.mode,
|
|
@@ -447,18 +205,12 @@ export class RunsModule {
|
|
|
447
205
|
},
|
|
448
206
|
});
|
|
449
207
|
}
|
|
450
|
-
|
|
451
208
|
// ======================== Events ========================
|
|
452
|
-
|
|
453
209
|
/**
|
|
454
210
|
* Get canonical run events from SSOT ledger (polling by seq).
|
|
455
211
|
*/
|
|
456
|
-
async getEvents(runId
|
|
457
|
-
|
|
458
|
-
afterSeq?: number;
|
|
459
|
-
limit?: number;
|
|
460
|
-
}): Promise<APIResponse<RunEventsPollResponse>> {
|
|
461
|
-
return this.client.GET<RunEventsPollResponse>("/v1/api/runs/{runId}/events", {
|
|
212
|
+
async getEvents(runId, params) {
|
|
213
|
+
return this.client.GET("/v1/api/runs/{runId}/events", {
|
|
462
214
|
params: {
|
|
463
215
|
path: { runId },
|
|
464
216
|
query: {
|
|
@@ -469,19 +221,16 @@ export class RunsModule {
|
|
|
469
221
|
},
|
|
470
222
|
});
|
|
471
223
|
}
|
|
472
|
-
|
|
473
224
|
/** Alias: runs.events() -> runs.getEvents() */
|
|
474
|
-
events = (runId
|
|
475
|
-
this.getEvents(runId, params);
|
|
476
|
-
|
|
225
|
+
events = (runId, params) => this.getEvents(runId, params);
|
|
477
226
|
/**
|
|
478
227
|
* Wave 2.3: Seq-based event polling for execution trace.
|
|
479
228
|
* Use this for incremental polling with reconnection support.
|
|
480
|
-
*
|
|
229
|
+
*
|
|
481
230
|
* @param runId - Run ID to poll events for
|
|
482
231
|
* @param params - Polling options
|
|
483
232
|
* @returns Events with seq cursors for pagination
|
|
484
|
-
*
|
|
233
|
+
*
|
|
485
234
|
* @example
|
|
486
235
|
* ```ts
|
|
487
236
|
* let afterSeq = 0;
|
|
@@ -496,12 +245,8 @@ export class RunsModule {
|
|
|
496
245
|
* }
|
|
497
246
|
* ```
|
|
498
247
|
*/
|
|
499
|
-
async pollEvents(runId
|
|
500
|
-
|
|
501
|
-
afterSeq?: number;
|
|
502
|
-
limit?: number;
|
|
503
|
-
}): Promise<APIResponse<RunEventsPollResponse>> {
|
|
504
|
-
return this.client.GET<RunEventsPollResponse>("/v1/api/runs/{runId}/events", {
|
|
248
|
+
async pollEvents(runId, params) {
|
|
249
|
+
return this.client.GET("/v1/api/runs/{runId}/events", {
|
|
505
250
|
params: {
|
|
506
251
|
path: { runId },
|
|
507
252
|
query: {
|
|
@@ -512,23 +257,18 @@ export class RunsModule {
|
|
|
512
257
|
},
|
|
513
258
|
});
|
|
514
259
|
}
|
|
515
|
-
|
|
516
260
|
// ======================== Checkpoints ========================
|
|
517
|
-
|
|
518
261
|
/**
|
|
519
262
|
* Get checkpoints for a run.
|
|
520
263
|
*/
|
|
521
|
-
async getCheckpoints(runId
|
|
522
|
-
return this.client.GET
|
|
264
|
+
async getCheckpoints(runId) {
|
|
265
|
+
return this.client.GET("/v1/api/runs/{runId}/checkpoints/index", {
|
|
523
266
|
params: { path: { runId } },
|
|
524
267
|
});
|
|
525
268
|
}
|
|
526
|
-
|
|
527
269
|
/** Alias: runs.checkpoints() -> runs.getCheckpoints() */
|
|
528
|
-
checkpoints = (runId
|
|
529
|
-
|
|
270
|
+
checkpoints = (runId) => this.getCheckpoints(runId);
|
|
530
271
|
// ======================== FOLLOW (SSOT SSE) ========================
|
|
531
|
-
|
|
532
272
|
/**
|
|
533
273
|
* Follow a run attempt's canonical event stream with automatic reconnection.
|
|
534
274
|
*
|
|
@@ -536,7 +276,7 @@ export class RunsModule {
|
|
|
536
276
|
* - Stream is keyed by (run_id, attempt_id, seq)
|
|
537
277
|
* - Resume is driven by `afterSeq` (monotonic)
|
|
538
278
|
* - No implicit attempt inference
|
|
539
|
-
*
|
|
279
|
+
*
|
|
540
280
|
* @example
|
|
541
281
|
* ```ts
|
|
542
282
|
* for await (const event of client.runs.follow(runId, attemptId)) {
|
|
@@ -548,33 +288,28 @@ export class RunsModule {
|
|
|
548
288
|
* }
|
|
549
289
|
* ```
|
|
550
290
|
*/
|
|
551
|
-
async *follow(runId
|
|
291
|
+
async *follow(runId, attemptId, options) {
|
|
552
292
|
if (!attemptId) {
|
|
553
293
|
throw new Error("attemptId is required for run event streaming");
|
|
554
294
|
}
|
|
555
|
-
|
|
556
295
|
const signal = options?.signal;
|
|
557
296
|
const maxReconnects = options?.maxReconnects ?? 10;
|
|
558
297
|
const baseDelayMs = options?.baseDelayMs ?? 1000;
|
|
559
298
|
const maxDelayMs = options?.maxDelayMs ?? 30000;
|
|
560
|
-
|
|
561
299
|
// nextSeq = next seq we expect to receive (or 0 at start)
|
|
562
300
|
let nextSeq = options?.startSeq ?? 0;
|
|
563
301
|
let reconnectCount = 0;
|
|
564
302
|
let receivedTerminal = false;
|
|
565
|
-
|
|
566
303
|
options?.onConnect?.();
|
|
567
|
-
|
|
568
304
|
while (!receivedTerminal && reconnectCount <= maxReconnects) {
|
|
569
|
-
if (signal?.aborted)
|
|
570
|
-
|
|
305
|
+
if (signal?.aborted)
|
|
306
|
+
return;
|
|
571
307
|
try {
|
|
572
308
|
// Build headers - Last-Event-ID is the LAST seq we received (nextSeq - 1)
|
|
573
|
-
const headers
|
|
309
|
+
const headers = {};
|
|
574
310
|
if (nextSeq > 0) {
|
|
575
311
|
headers["Last-Event-ID"] = String(nextSeq - 1);
|
|
576
312
|
}
|
|
577
|
-
|
|
578
313
|
const response = await this.client.streamGet("/v1/api/runs/{runId}/events/stream", {
|
|
579
314
|
params: {
|
|
580
315
|
path: { runId },
|
|
@@ -585,10 +320,9 @@ export class RunsModule {
|
|
|
585
320
|
},
|
|
586
321
|
headers,
|
|
587
322
|
});
|
|
588
|
-
|
|
589
323
|
// Check for auth errors - don't reconnect on these
|
|
590
324
|
if (response.status === 401 || response.status === 403) {
|
|
591
|
-
const errorEvent
|
|
325
|
+
const errorEvent = {
|
|
592
326
|
type: "error",
|
|
593
327
|
payload: { code: `HTTP_${response.status}`, message: response.statusText },
|
|
594
328
|
seq: -1,
|
|
@@ -597,39 +331,33 @@ export class RunsModule {
|
|
|
597
331
|
yield errorEvent;
|
|
598
332
|
return; // Don't reconnect on auth errors
|
|
599
333
|
}
|
|
600
|
-
|
|
601
334
|
if (!response.ok) {
|
|
602
335
|
throw new Error(`SSE connection failed: ${response.status}`);
|
|
603
336
|
}
|
|
604
|
-
|
|
605
337
|
// Reset reconnect count on successful connection
|
|
606
338
|
if (reconnectCount > 0) {
|
|
607
339
|
options?.onReconnect?.(reconnectCount);
|
|
608
340
|
}
|
|
609
341
|
reconnectCount = 0;
|
|
610
|
-
|
|
611
342
|
// Parse SSE stream
|
|
612
|
-
for await (const rawEvent of parseSSE
|
|
613
|
-
if (signal?.aborted)
|
|
614
|
-
|
|
343
|
+
for await (const rawEvent of parseSSE(response)) {
|
|
344
|
+
if (signal?.aborted)
|
|
345
|
+
return;
|
|
615
346
|
// Narrow to known event types
|
|
616
347
|
const event = narrowFollowEvent(rawEvent);
|
|
617
|
-
if (!event)
|
|
618
|
-
|
|
348
|
+
if (!event)
|
|
349
|
+
continue; // Skip unknown event types
|
|
619
350
|
// Update seq tracking if event has seq
|
|
620
351
|
if (typeof event.seq === "number" && event.seq >= 0) {
|
|
621
352
|
nextSeq = event.seq + 1;
|
|
622
353
|
}
|
|
623
|
-
|
|
624
354
|
if (event.type === "error") {
|
|
625
355
|
// Error event from server - yield but continue (might reconnect)
|
|
626
356
|
yield event;
|
|
627
357
|
// If it's a fatal error, break and try reconnect
|
|
628
358
|
break;
|
|
629
359
|
}
|
|
630
|
-
|
|
631
360
|
yield event;
|
|
632
|
-
|
|
633
361
|
if (event.type === "run_event" && isTerminalRunEvent(event)) {
|
|
634
362
|
receivedTerminal = true;
|
|
635
363
|
yield {
|
|
@@ -641,7 +369,6 @@ export class RunsModule {
|
|
|
641
369
|
return;
|
|
642
370
|
}
|
|
643
371
|
}
|
|
644
|
-
|
|
645
372
|
// Stream ended without close event - this is unexpected EOF
|
|
646
373
|
// Reconnect unless we already received terminal
|
|
647
374
|
if (!receivedTerminal && !signal?.aborted) {
|
|
@@ -651,16 +378,16 @@ export class RunsModule {
|
|
|
651
378
|
await sleep(calculateBackoff(reconnectCount, baseDelayMs, maxDelayMs));
|
|
652
379
|
}
|
|
653
380
|
}
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
if (signal?.aborted)
|
|
657
|
-
|
|
381
|
+
}
|
|
382
|
+
catch (err) {
|
|
383
|
+
if (signal?.aborted)
|
|
384
|
+
return;
|
|
658
385
|
options?.onDisconnect?.("error");
|
|
659
386
|
reconnectCount++;
|
|
660
|
-
|
|
661
387
|
if (reconnectCount <= maxReconnects) {
|
|
662
388
|
await sleep(calculateBackoff(reconnectCount, baseDelayMs, maxDelayMs));
|
|
663
|
-
}
|
|
389
|
+
}
|
|
390
|
+
else {
|
|
664
391
|
// Max reconnects exceeded - yield error and stop
|
|
665
392
|
yield {
|
|
666
393
|
type: "error",
|
|
@@ -673,65 +400,44 @@ export class RunsModule {
|
|
|
673
400
|
}
|
|
674
401
|
}
|
|
675
402
|
}
|
|
676
|
-
|
|
677
403
|
/**
|
|
678
404
|
* Create run and follow canonical SSOT stream for the current attempt.
|
|
679
405
|
*/
|
|
680
|
-
async *createAndStream(
|
|
681
|
-
body: {
|
|
682
|
-
agent_id: string;
|
|
683
|
-
thread?: { thread_id?: string } | { new_thread: true };
|
|
684
|
-
input?: unknown;
|
|
685
|
-
/** Idempotency key for safe retries. */
|
|
686
|
-
idempotency_key?: string;
|
|
687
|
-
},
|
|
688
|
-
options?: FollowOptions
|
|
689
|
-
): AsyncGenerator<FollowEvent, void, unknown> {
|
|
406
|
+
async *createAndStream(body, options) {
|
|
690
407
|
const { data, error } = await this.create(body);
|
|
691
408
|
if (error || !data) {
|
|
692
409
|
throw new Error(`Failed to create run: ${JSON.stringify(error)}`);
|
|
693
410
|
}
|
|
694
|
-
|
|
695
411
|
const runId = data.run_id;
|
|
696
412
|
const runResponse = await this.get(runId);
|
|
697
413
|
if (runResponse.error || !runResponse.data?.current_attempt_id) {
|
|
698
414
|
throw new Error("Run created without current_attempt_id; cannot stream SSOT events");
|
|
699
415
|
}
|
|
700
|
-
|
|
701
416
|
yield* this.follow(runId, runResponse.data.current_attempt_id, options);
|
|
702
417
|
}
|
|
703
|
-
|
|
704
418
|
/**
|
|
705
419
|
* Wait for a specific event that matches a predicate.
|
|
706
|
-
*
|
|
420
|
+
*
|
|
707
421
|
* @example
|
|
708
422
|
* ```ts
|
|
709
423
|
* // Wait for run completion
|
|
710
424
|
* const event = await client.runs.waitFor(runId, attemptId, (e) => e.type === "close", {
|
|
711
425
|
* timeoutMs: 60000
|
|
712
426
|
* });
|
|
713
|
-
*
|
|
427
|
+
*
|
|
714
428
|
* // Wait for specific node execution
|
|
715
|
-
* const nodeEvent = await client.runs.waitFor(runId, attemptId, (e) =>
|
|
429
|
+
* const nodeEvent = await client.runs.waitFor(runId, attemptId, (e) =>
|
|
716
430
|
* e.type === "run_event" && e.payload?.node === "my_node"
|
|
717
431
|
* );
|
|
718
432
|
* ```
|
|
719
433
|
*/
|
|
720
|
-
async waitFor(
|
|
721
|
-
runId: string,
|
|
722
|
-
attemptId: string,
|
|
723
|
-
predicate: (event: FollowEvent) => boolean,
|
|
724
|
-
options?: { timeoutMs?: number; signal?: AbortSignal; startSeq?: number }
|
|
725
|
-
): Promise<FollowEvent> {
|
|
434
|
+
async waitFor(runId, attemptId, predicate, options) {
|
|
726
435
|
const timeoutMs = options?.timeoutMs ?? 300_000; // 5 min default
|
|
727
436
|
const controller = new AbortController();
|
|
728
|
-
|
|
729
437
|
// Combine parent signal with our timeout
|
|
730
438
|
const onAbort = () => controller.abort();
|
|
731
439
|
options?.signal?.addEventListener("abort", onAbort, { once: true });
|
|
732
|
-
|
|
733
440
|
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
734
|
-
|
|
735
441
|
try {
|
|
736
442
|
for await (const event of this.follow(runId, attemptId, {
|
|
737
443
|
signal: controller.signal,
|
|
@@ -740,85 +446,43 @@ export class RunsModule {
|
|
|
740
446
|
if (predicate(event)) {
|
|
741
447
|
return event;
|
|
742
448
|
}
|
|
743
|
-
|
|
744
449
|
// If we got close without matching predicate, error
|
|
745
450
|
if (event.type === "close") {
|
|
746
451
|
throw new Error("Run completed without matching event");
|
|
747
452
|
}
|
|
748
453
|
}
|
|
749
|
-
|
|
750
454
|
// Stream ended without match or terminal
|
|
751
455
|
throw new Error("Stream ended without matching event");
|
|
752
|
-
|
|
753
|
-
|
|
456
|
+
}
|
|
457
|
+
catch (err) {
|
|
754
458
|
if (controller.signal.aborted && !options?.signal?.aborted) {
|
|
755
459
|
// Our timeout triggered the abort
|
|
756
460
|
const { TimeoutError } = await import("../errors/index.js");
|
|
757
461
|
throw new TimeoutError(timeoutMs);
|
|
758
462
|
}
|
|
759
463
|
throw err;
|
|
760
|
-
}
|
|
464
|
+
}
|
|
465
|
+
finally {
|
|
761
466
|
clearTimeout(timeoutId);
|
|
762
467
|
options?.signal?.removeEventListener("abort", onAbort);
|
|
763
468
|
}
|
|
764
469
|
}
|
|
765
470
|
}
|
|
766
|
-
|
|
767
|
-
// ============================================================================
|
|
768
|
-
// Follow Types & Helpers
|
|
769
|
-
// ============================================================================
|
|
770
|
-
|
|
771
|
-
/** Options for runs.follow() */
|
|
772
|
-
export interface FollowOptions {
|
|
773
|
-
/** Starting sequence number (default: 0 = from beginning) */
|
|
774
|
-
startSeq?: number;
|
|
775
|
-
/** AbortSignal for cancellation */
|
|
776
|
-
signal?: AbortSignal;
|
|
777
|
-
/** Max reconnection attempts (default: 10) */
|
|
778
|
-
maxReconnects?: number;
|
|
779
|
-
/** Base delay for backoff in ms (default: 1000) */
|
|
780
|
-
baseDelayMs?: number;
|
|
781
|
-
/** Max delay for backoff in ms (default: 30000) */
|
|
782
|
-
maxDelayMs?: number;
|
|
783
|
-
/** Called when initially connected */
|
|
784
|
-
onConnect?: () => void;
|
|
785
|
-
/** Called when reconnecting (with attempt number) */
|
|
786
|
-
onReconnect?: (attempt: number) => void;
|
|
787
|
-
/** Called when disconnected (with reason) */
|
|
788
|
-
onDisconnect?: (reason: "eof" | "error") => void;
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
/** Event emitted by runs.follow() */
|
|
792
|
-
export interface FollowEvent {
|
|
793
|
-
type: "run_event" | "heartbeat" | "close" | "error";
|
|
794
|
-
seq: number;
|
|
795
|
-
timestamp: string;
|
|
796
|
-
payload?: Record<string, unknown> | null;
|
|
797
|
-
operation_id?: string;
|
|
798
|
-
parent_operation_id?: string | null;
|
|
799
|
-
root_operation_id?: string;
|
|
800
|
-
/** For run_event: the node that emitted this event */
|
|
801
|
-
node?: string;
|
|
802
|
-
}
|
|
803
|
-
|
|
804
471
|
/** Narrow raw SSE event to FollowEvent */
|
|
805
|
-
export function narrowFollowEvent(raw
|
|
472
|
+
export function narrowFollowEvent(raw) {
|
|
806
473
|
const eventType = raw.event ?? "message";
|
|
807
474
|
const validTypes = ["run_event", "heartbeat", "close", "error"];
|
|
808
|
-
|
|
809
475
|
if (!validTypes.includes(eventType)) {
|
|
810
476
|
return null;
|
|
811
477
|
}
|
|
812
|
-
|
|
813
|
-
const data = raw.data as Record<string, unknown> | null;
|
|
814
|
-
|
|
478
|
+
const data = raw.data;
|
|
815
479
|
// Canonical payload from backend SSE is RunEventDto:
|
|
816
480
|
// { id, seq, type, timestamp, attempt_id, payload }
|
|
817
481
|
if (eventType === "run_event" && data) {
|
|
818
482
|
const seq = typeof data.seq === "number" ? data.seq : (raw.id ? parseInt(raw.id, 10) : -1);
|
|
819
483
|
const timestamp = typeof data.timestamp === "string" ? data.timestamp : new Date().toISOString();
|
|
820
484
|
const innerPayload = (typeof data.payload === "object" && data.payload !== null)
|
|
821
|
-
?
|
|
485
|
+
? data.payload
|
|
822
486
|
: null;
|
|
823
487
|
const operationId = typeof data.operation_id === "string" ? data.operation_id : undefined;
|
|
824
488
|
const parentOperationId = typeof data.parent_operation_id === "string"
|
|
@@ -827,7 +491,6 @@ export function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
|
827
491
|
? null
|
|
828
492
|
: undefined;
|
|
829
493
|
const rootOperationId = typeof data.root_operation_id === "string" ? data.root_operation_id : undefined;
|
|
830
|
-
|
|
831
494
|
return {
|
|
832
495
|
type: "run_event",
|
|
833
496
|
seq,
|
|
@@ -846,33 +509,28 @@ export function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
|
846
509
|
node: typeof innerPayload?.node === "string" ? innerPayload.node : undefined,
|
|
847
510
|
};
|
|
848
511
|
}
|
|
849
|
-
|
|
850
512
|
return {
|
|
851
|
-
type: eventType
|
|
513
|
+
type: eventType,
|
|
852
514
|
seq: typeof data?.seq === "number" ? data.seq : (raw.id ? parseInt(raw.id, 10) : -1),
|
|
853
515
|
timestamp: typeof data?.timestamp === "string" ? data.timestamp : new Date().toISOString(),
|
|
854
516
|
payload: data,
|
|
855
517
|
node: typeof data?.node === "string" ? data.node : undefined,
|
|
856
518
|
};
|
|
857
519
|
}
|
|
858
|
-
|
|
859
|
-
function isTerminalRunEvent(event: FollowEvent): boolean {
|
|
520
|
+
function isTerminalRunEvent(event) {
|
|
860
521
|
const type = typeof event.payload?.type === "string"
|
|
861
522
|
? event.payload.type.toUpperCase()
|
|
862
523
|
: "";
|
|
863
|
-
|
|
864
524
|
return type === "RUN_FINISHED" || type === "RUN_FAILED" || type === "RUN_CANCELLED";
|
|
865
525
|
}
|
|
866
|
-
|
|
867
526
|
/** Calculate exponential backoff with jitter */
|
|
868
|
-
function calculateBackoff(attempt
|
|
527
|
+
function calculateBackoff(attempt, baseMs, maxMs) {
|
|
869
528
|
const exponential = baseMs * Math.pow(2, attempt - 1);
|
|
870
529
|
const capped = Math.min(exponential, maxMs);
|
|
871
530
|
const jitter = capped * 0.2 * Math.random();
|
|
872
531
|
return Math.floor(capped + jitter);
|
|
873
532
|
}
|
|
874
|
-
|
|
875
533
|
/** Sleep utility */
|
|
876
|
-
function sleep(ms
|
|
534
|
+
function sleep(ms) {
|
|
877
535
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
878
536
|
}
|