@agent-os-sdk/client 0.9.14 → 0.9.15
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 +8 -4
- package/dist/client/AgentOsClient.d.ts.map +1 -1
- package/dist/client/AgentOsClient.js +12 -6
- package/dist/errors/factory.d.ts +2 -0
- package/dist/errors/factory.d.ts.map +1 -1
- package/dist/errors/factory.js +5 -1
- package/dist/generated/openapi.d.ts +544 -744
- package/dist/generated/openapi.d.ts.map +1 -1
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -2
- package/dist/modules/agents.d.ts +25 -7
- package/dist/modules/agents.d.ts.map +1 -1
- package/dist/modules/agents.js +36 -7
- 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/evaluation.d.ts +64 -0
- package/dist/modules/evaluation.d.ts.map +1 -1
- package/dist/modules/evaluation.js +12 -0
- package/dist/modules/improvements.d.ts +52 -0
- package/dist/modules/improvements.d.ts.map +1 -0
- package/dist/modules/improvements.js +27 -0
- package/dist/modules/metaAgent.d.ts +62 -0
- package/dist/modules/metaAgent.d.ts.map +1 -0
- package/dist/modules/metaAgent.js +25 -0
- package/dist/modules/presets.d.ts.map +1 -1
- package/dist/modules/presets.js +113 -44
- package/dist/modules/runs.d.ts +9 -0
- package/dist/modules/runs.d.ts.map +1 -1
- package/dist/modules/runs.js +14 -1
- package/dist/modules/templates.d.ts +28 -0
- package/dist/modules/templates.d.ts.map +1 -0
- package/dist/modules/templates.js +19 -0
- package/package.json +2 -2
- package/src/client/AgentOsClient.ts +12 -6
- package/src/errors/factory.ts +7 -2
- package/src/generated/openapi.ts +544 -744
- package/src/generated/swagger.json +551 -924
- package/src/index.ts +22 -2
- package/src/modules/agents.ts +62 -8
- package/src/modules/contracts.ts +80 -0
- package/src/modules/evaluation.ts +80 -0
- package/src/modules/improvements.ts +71 -0
- package/src/modules/metaAgent.ts +87 -0
- package/src/modules/presets.ts +118 -50
- package/src/modules/runs.ts +20 -1
- package/src/modules/templates.ts +40 -0
- package/src/modules/builder.ts +0 -456
- package/src/modules/graphs.ts +0 -188
package/src/modules/runs.ts
CHANGED
|
@@ -82,6 +82,9 @@ export interface RunEventDto {
|
|
|
82
82
|
timestamp: string;
|
|
83
83
|
attempt_id: string;
|
|
84
84
|
payload: Record<string, unknown> | null;
|
|
85
|
+
operation_id: string;
|
|
86
|
+
parent_operation_id?: string | null;
|
|
87
|
+
root_operation_id: string;
|
|
85
88
|
}
|
|
86
89
|
|
|
87
90
|
export class RunsModule {
|
|
@@ -645,12 +648,15 @@ export interface FollowEvent {
|
|
|
645
648
|
seq: number;
|
|
646
649
|
timestamp: string;
|
|
647
650
|
payload?: Record<string, unknown> | null;
|
|
651
|
+
operation_id?: string;
|
|
652
|
+
parent_operation_id?: string | null;
|
|
653
|
+
root_operation_id?: string;
|
|
648
654
|
/** For run_event: the node that emitted this event */
|
|
649
655
|
node?: string;
|
|
650
656
|
}
|
|
651
657
|
|
|
652
658
|
/** Narrow raw SSE event to FollowEvent */
|
|
653
|
-
function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
659
|
+
export function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
654
660
|
const eventType = raw.event ?? "message";
|
|
655
661
|
const validTypes = ["run_event", "heartbeat", "close", "error"];
|
|
656
662
|
|
|
@@ -668,6 +674,13 @@ function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
|
668
674
|
const innerPayload = (typeof data.payload === "object" && data.payload !== null)
|
|
669
675
|
? (data.payload as Record<string, unknown>)
|
|
670
676
|
: null;
|
|
677
|
+
const operationId = typeof data.operation_id === "string" ? data.operation_id : undefined;
|
|
678
|
+
const parentOperationId = typeof data.parent_operation_id === "string"
|
|
679
|
+
? data.parent_operation_id
|
|
680
|
+
: data.parent_operation_id === null
|
|
681
|
+
? null
|
|
682
|
+
: undefined;
|
|
683
|
+
const rootOperationId = typeof data.root_operation_id === "string" ? data.root_operation_id : undefined;
|
|
671
684
|
|
|
672
685
|
return {
|
|
673
686
|
type: "run_event",
|
|
@@ -677,7 +690,13 @@ function narrowFollowEvent(raw: SSEEvent<unknown>): FollowEvent | null {
|
|
|
677
690
|
...(innerPayload ?? {}),
|
|
678
691
|
type: typeof data.type === "string" ? data.type : "UNKNOWN",
|
|
679
692
|
attempt_id: typeof data.attempt_id === "string" ? data.attempt_id : undefined,
|
|
693
|
+
operation_id: operationId,
|
|
694
|
+
parent_operation_id: parentOperationId,
|
|
695
|
+
root_operation_id: rootOperationId,
|
|
680
696
|
},
|
|
697
|
+
operation_id: operationId,
|
|
698
|
+
parent_operation_id: parentOperationId,
|
|
699
|
+
root_operation_id: rootOperationId,
|
|
681
700
|
node: typeof innerPayload?.node === "string" ? innerPayload.node : undefined,
|
|
682
701
|
};
|
|
683
702
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { APIResponse, RawClient } from "../client/raw.js";
|
|
2
|
+
|
|
3
|
+
export interface AuthoringTemplateSummary {
|
|
4
|
+
template_ref: string;
|
|
5
|
+
version: string;
|
|
6
|
+
display_name: string;
|
|
7
|
+
description?: string | null;
|
|
8
|
+
template_kind: string;
|
|
9
|
+
category: string;
|
|
10
|
+
editability_mode: string;
|
|
11
|
+
required_capability_refs: string[];
|
|
12
|
+
default_policy_refs?: Record<string, unknown> | null;
|
|
13
|
+
default_runtime_config_refs?: Record<string, unknown> | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface AuthoringTemplateDetail extends AuthoringTemplateSummary {
|
|
17
|
+
base_agent_ir: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface AuthoringTemplateListResponse {
|
|
21
|
+
items: AuthoringTemplateSummary[];
|
|
22
|
+
total: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class TemplatesModule {
|
|
26
|
+
constructor(private client: RawClient, private headers: () => Record<string, string>) {}
|
|
27
|
+
|
|
28
|
+
async list(): Promise<APIResponse<AuthoringTemplateListResponse>> {
|
|
29
|
+
return this.client.GET<AuthoringTemplateListResponse>("/v1/api/templates", {
|
|
30
|
+
headers: this.headers(),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async get(templateRef: string): Promise<APIResponse<AuthoringTemplateDetail>> {
|
|
35
|
+
return this.client.GET<AuthoringTemplateDetail>("/v1/api/templates/{templateRef}", {
|
|
36
|
+
params: { path: { templateRef } },
|
|
37
|
+
headers: this.headers(),
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/modules/builder.ts
DELETED
|
@@ -1,456 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Builder Module - Meta-Agent for Agent Building
|
|
3
|
-
*
|
|
4
|
-
* Connects to the Control Plane's builder endpoint which proxies to the
|
|
5
|
-
* Data Plane's meta-agent for AI-assisted agent creation.
|
|
6
|
-
* Uses SSE streaming for real-time responses and graph updates.
|
|
7
|
-
*
|
|
8
|
-
* Flow: Frontend → CP:5000/v1/api/builder/{agentId}/chat → DP:8001/v1/internal/builder/{agentId}/chat
|
|
9
|
-
*
|
|
10
|
-
* ALL HTTP goes through rawClient - no direct fetch calls.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const client = new AgentOsClient({ ... })
|
|
15
|
-
*
|
|
16
|
-
* // SSE streaming chat
|
|
17
|
-
* for await (const event of client.builder.chat(agentId, {
|
|
18
|
-
* message: "Adicione um nó de pesquisa web",
|
|
19
|
-
* current_graph_spec: { nodes: [...], edges: [...] }
|
|
20
|
-
* })) {
|
|
21
|
-
* if (event.type === 'message') console.log(event.data.text)
|
|
22
|
-
* if (event.type === 'graph_update') applyAction(event.data)
|
|
23
|
-
* }
|
|
24
|
-
*
|
|
25
|
-
* // Sync chat (no streaming)
|
|
26
|
-
* const result = await client.builder.chatSync(agentId, { message: "Mude as instruções" })
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
import type { RawClient, APIResponse } from "../client/raw.js";
|
|
31
|
-
import type { SSEOptions } from "../sse/client.js";
|
|
32
|
-
|
|
33
|
-
export type CatalogVersions = {
|
|
34
|
-
nodes: string;
|
|
35
|
-
tools: string;
|
|
36
|
-
triggers: string;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type ChatHistoryMessage = {
|
|
40
|
-
role: 'user' | 'assistant';
|
|
41
|
-
content: string;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Builder chat request.
|
|
46
|
-
*
|
|
47
|
-
* KERNEL-FIRST: Frontend sends INTENTION only.
|
|
48
|
-
* - Graph spec is loaded from artifact storage by CP
|
|
49
|
-
* - Catalogs are loaded from DB by CP
|
|
50
|
-
* - Frontend should NOT send graph_spec or catalogs
|
|
51
|
-
*/
|
|
52
|
-
export type BuilderChatRequest = {
|
|
53
|
-
/** Chat message/intention */
|
|
54
|
-
message: string;
|
|
55
|
-
/** Thread ID for conversation continuity */
|
|
56
|
-
thread_id?: string;
|
|
57
|
-
/** Chat history for context (optional, sent from frontend) */
|
|
58
|
-
history?: { role: 'user' | 'assistant'; content: string }[];
|
|
59
|
-
/** Optional ETag for concurrency control */
|
|
60
|
-
base_graph_etag?: string;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type HintTargetModel = {
|
|
64
|
-
node_id?: string;
|
|
65
|
-
edge_id?: string;
|
|
66
|
-
panel?: "credentials" | "triggers" | "logs" | "validation" | "deploy" | string;
|
|
67
|
-
action?: "create_credential" | "save_draft" | "publish_version" | string;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export type HintDataModel = {
|
|
71
|
-
// Credential hints
|
|
72
|
-
credential_slug?: string;
|
|
73
|
-
reason?: string;
|
|
74
|
-
|
|
75
|
-
// Config hints
|
|
76
|
-
fields?: string[];
|
|
77
|
-
|
|
78
|
-
// Error/validation hints
|
|
79
|
-
code?: string;
|
|
80
|
-
path?: string;
|
|
81
|
-
|
|
82
|
-
// Run/execution hints
|
|
83
|
-
run_id?: string;
|
|
84
|
-
error_code?: string;
|
|
85
|
-
|
|
86
|
-
// Cost/performance hints
|
|
87
|
-
tokens_in?: number;
|
|
88
|
-
tokens_out?: number;
|
|
89
|
-
usd_estimate?: number;
|
|
90
|
-
|
|
91
|
-
// Action hints
|
|
92
|
-
action_id?: string;
|
|
93
|
-
params?: Record<string, any>;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export type HintModel = {
|
|
97
|
-
id?: string;
|
|
98
|
-
scope: "graph" | "thread" | "run" | string;
|
|
99
|
-
type:
|
|
100
|
-
| "highlight_node"
|
|
101
|
-
| "needs_credential"
|
|
102
|
-
| "missing_config"
|
|
103
|
-
| "error_marker"
|
|
104
|
-
| "warning_marker"
|
|
105
|
-
| "toast"
|
|
106
|
-
| "run_started"
|
|
107
|
-
| "node_running"
|
|
108
|
-
| "node_failed"
|
|
109
|
-
| "rate_limited"
|
|
110
|
-
| "cost_estimate"
|
|
111
|
-
| "action_suggested"
|
|
112
|
-
| string;
|
|
113
|
-
severity: "info" | "warn" | "error" | "success";
|
|
114
|
-
target?: HintTargetModel;
|
|
115
|
-
message?: string;
|
|
116
|
-
ttl_ms?: number;
|
|
117
|
-
data?: HintDataModel;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export type GraphUpdateAction = {
|
|
121
|
-
graph_spec: Record<string, unknown>;
|
|
122
|
-
hints?: HintModel[];
|
|
123
|
-
has_errors?: boolean;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export type BuilderStreamEvent =
|
|
127
|
-
| { type: "message"; data: { text: string } }
|
|
128
|
-
| { type: "validation"; data: { valid: boolean; errors: any[] } }
|
|
129
|
-
| { type: "graph_update"; data: { graph_spec: Record<string, unknown>; hints: HintModel[]; has_errors?: boolean } }
|
|
130
|
-
| { type: "done"; data: { response: string; thread_id: string } }
|
|
131
|
-
| { type: "error"; data: { error: string } };
|
|
132
|
-
|
|
133
|
-
export type BuilderChatResponse = {
|
|
134
|
-
response: string;
|
|
135
|
-
graph_spec?: Record<string, unknown>;
|
|
136
|
-
valid: boolean;
|
|
137
|
-
errors: any[];
|
|
138
|
-
thread_id: string;
|
|
139
|
-
hints: HintModel[];
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
// ============================================
|
|
143
|
-
// PATCH-OPS ARCHITECTURE (Meta-Agent V2)
|
|
144
|
-
// ============================================
|
|
145
|
-
|
|
146
|
-
/** Supported patch operation types */
|
|
147
|
-
export type OpType =
|
|
148
|
-
| "add_node"
|
|
149
|
-
| "remove_node"
|
|
150
|
-
| "update_node"
|
|
151
|
-
| "add_edge"
|
|
152
|
-
| "remove_edge"
|
|
153
|
-
| "set_entrypoint"
|
|
154
|
-
| "rename_node";
|
|
155
|
-
|
|
156
|
-
/** A single graph modification operation */
|
|
157
|
-
export type PatchOp = {
|
|
158
|
-
op: OpType;
|
|
159
|
-
node_id?: string;
|
|
160
|
-
kind?: string;
|
|
161
|
-
label?: string;
|
|
162
|
-
data?: Record<string, unknown>;
|
|
163
|
-
set?: Record<string, unknown>;
|
|
164
|
-
from?: string;
|
|
165
|
-
to?: string;
|
|
166
|
-
entrypoint?: string;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
/** A collection of patch operations with deterministic ID */
|
|
170
|
-
export type GraphPatch = {
|
|
171
|
-
patch_id: string;
|
|
172
|
-
ops: PatchOp[];
|
|
173
|
-
description?: string;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/** Validation error from applying patch */
|
|
177
|
-
export type PatchValidationError = {
|
|
178
|
-
code: string;
|
|
179
|
-
message: string;
|
|
180
|
-
node_id?: string;
|
|
181
|
-
path?: string;
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
/** Meta-chat request */
|
|
185
|
-
export type MetaChatRequest = {
|
|
186
|
-
instruction: string;
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
/** Meta-chat stream events */
|
|
190
|
-
export type MetaChatStreamEvent =
|
|
191
|
-
| { type: "assistant.delta"; data: { text: string } }
|
|
192
|
-
| { type: "patch.validation"; data: { valid: boolean; errors: PatchValidationError[] } }
|
|
193
|
-
| { type: "patch.proposed"; data: GraphPatch }
|
|
194
|
-
| { type: "final"; data: { ok: boolean; patch_id: string } }
|
|
195
|
-
| { type: "error"; data: { error: string } };
|
|
196
|
-
|
|
197
|
-
/** Meta-chat sync response */
|
|
198
|
-
export type MetaChatResponse = {
|
|
199
|
-
patch: GraphPatch | null;
|
|
200
|
-
applied_spec: Record<string, unknown> | null;
|
|
201
|
-
valid: boolean;
|
|
202
|
-
errors: PatchValidationError[];
|
|
203
|
-
message: string;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
export class BuilderModule {
|
|
208
|
-
constructor(private client: RawClient) { }
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Stream chat with meta-agent (SSE).
|
|
212
|
-
* Returns async generator of events.
|
|
213
|
-
*
|
|
214
|
-
* Uses rawClient.streamPost() - headers resolved asynchronously.
|
|
215
|
-
*/
|
|
216
|
-
async *chat(
|
|
217
|
-
agentId: string,
|
|
218
|
-
request: BuilderChatRequest,
|
|
219
|
-
options?: SSEOptions
|
|
220
|
-
): AsyncGenerator<BuilderStreamEvent> {
|
|
221
|
-
// Use rawClient.streamPost() - ALL auth headers resolved automatically
|
|
222
|
-
const response = await this.client.streamPost("/v1/api/builder/{agentId}/chat", {
|
|
223
|
-
params: { path: { agentId } },
|
|
224
|
-
body: request,
|
|
225
|
-
signal: options?.signal,
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
if (!response.ok) {
|
|
229
|
-
const errorText = await response.text();
|
|
230
|
-
throw new Error(`Builder chat failed: ${response.status} - ${errorText}`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (!response.body) {
|
|
234
|
-
throw new Error("No response body");
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
options?.onOpen?.();
|
|
238
|
-
|
|
239
|
-
const reader = response.body.getReader();
|
|
240
|
-
const decoder = new TextDecoder();
|
|
241
|
-
let buffer = "";
|
|
242
|
-
|
|
243
|
-
try {
|
|
244
|
-
while (true) {
|
|
245
|
-
const { done, value } = await reader.read();
|
|
246
|
-
if (done) break;
|
|
247
|
-
|
|
248
|
-
buffer += decoder.decode(value, { stream: true });
|
|
249
|
-
const lines = buffer.split("\n");
|
|
250
|
-
buffer = lines.pop() ?? "";
|
|
251
|
-
|
|
252
|
-
let eventType = "message";
|
|
253
|
-
let eventData = "";
|
|
254
|
-
|
|
255
|
-
for (const line of lines) {
|
|
256
|
-
if (line.startsWith("event:")) {
|
|
257
|
-
eventType = line.slice(6).trim();
|
|
258
|
-
} else if (line.startsWith("data:")) {
|
|
259
|
-
eventData = line.slice(5).trim();
|
|
260
|
-
} else if (line === "" && eventData) {
|
|
261
|
-
try {
|
|
262
|
-
const parsed = JSON.parse(eventData);
|
|
263
|
-
yield { type: eventType, data: parsed } as BuilderStreamEvent;
|
|
264
|
-
} catch {
|
|
265
|
-
// Skip invalid JSON
|
|
266
|
-
}
|
|
267
|
-
eventData = "";
|
|
268
|
-
eventType = "message";
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
} finally {
|
|
273
|
-
reader.releaseLock();
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* Sync chat with meta-agent (no streaming).
|
|
279
|
-
*
|
|
280
|
-
* Uses rawClient.POST() - headers resolved asynchronously.
|
|
281
|
-
*/
|
|
282
|
-
async chatSync(
|
|
283
|
-
agentId: string,
|
|
284
|
-
request: BuilderChatRequest
|
|
285
|
-
): Promise<BuilderChatResponse> {
|
|
286
|
-
const { data, error } = await this.client.POST<BuilderChatResponse>(
|
|
287
|
-
"/v1/api/builder/{agentId}/chat/sync",
|
|
288
|
-
{
|
|
289
|
-
params: { path: { agentId } },
|
|
290
|
-
body: request,
|
|
291
|
-
}
|
|
292
|
-
);
|
|
293
|
-
|
|
294
|
-
if (error) {
|
|
295
|
-
throw new Error(`Builder chat failed: ${error.code} - ${error.message}`);
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
return data!;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Convenience method: chat and collect all events.
|
|
303
|
-
*/
|
|
304
|
-
async chatCollect(
|
|
305
|
-
agentId: string,
|
|
306
|
-
request: BuilderChatRequest
|
|
307
|
-
): Promise<BuilderChatResponse> {
|
|
308
|
-
let fullResponse = "";
|
|
309
|
-
let lastGraphSpec: Record<string, unknown> | undefined;
|
|
310
|
-
let allHints: HintModel[] = [];
|
|
311
|
-
const allErrors: any[] = [];
|
|
312
|
-
let threadId = request.thread_id ?? "";
|
|
313
|
-
let isValid = true;
|
|
314
|
-
|
|
315
|
-
for await (const event of this.chat(agentId, request)) {
|
|
316
|
-
if (event.type === "message") {
|
|
317
|
-
fullResponse += event.data.text;
|
|
318
|
-
} else if (event.type === "graph_update") {
|
|
319
|
-
if ('graph_spec' in event.data) {
|
|
320
|
-
lastGraphSpec = event.data.graph_spec;
|
|
321
|
-
if (event.data.hints) {
|
|
322
|
-
allHints.push(...event.data.hints);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
} else if (event.type === "validation") {
|
|
326
|
-
isValid = event.data.valid;
|
|
327
|
-
if (event.data.errors) {
|
|
328
|
-
allErrors.push(...event.data.errors);
|
|
329
|
-
}
|
|
330
|
-
} else if (event.type === "done") {
|
|
331
|
-
threadId = event.data.thread_id;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
return {
|
|
336
|
-
response: fullResponse,
|
|
337
|
-
graph_spec: lastGraphSpec,
|
|
338
|
-
hints: allHints,
|
|
339
|
-
valid: isValid,
|
|
340
|
-
errors: allErrors,
|
|
341
|
-
thread_id: threadId,
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
// ============================================
|
|
346
|
-
// META-CHAT V2 (PATCH-OPS ARCHITECTURE)
|
|
347
|
-
// ============================================
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Stream meta-chat with patch-ops architecture (SSE).
|
|
351
|
-
* Returns GraphPatch proposals instead of full graph specs.
|
|
352
|
-
*
|
|
353
|
-
* @example
|
|
354
|
-
* ```ts
|
|
355
|
-
* for await (const event of client.builder.metaChat(agentId, {
|
|
356
|
-
* instruction: "Add a log node after the main agent"
|
|
357
|
-
* })) {
|
|
358
|
-
* if (event.type === 'patch.proposed') {
|
|
359
|
-
* const patch = event.data;
|
|
360
|
-
* console.log(`Proposed ${patch.ops.length} operations`);
|
|
361
|
-
* }
|
|
362
|
-
* }
|
|
363
|
-
* ```
|
|
364
|
-
*/
|
|
365
|
-
async *metaChat(
|
|
366
|
-
agentId: string,
|
|
367
|
-
request: MetaChatRequest,
|
|
368
|
-
options?: SSEOptions
|
|
369
|
-
): AsyncGenerator<MetaChatStreamEvent> {
|
|
370
|
-
const response = await this.client.streamPost("/v1/builder/{agentId}/meta-chat", {
|
|
371
|
-
params: { path: { agentId } },
|
|
372
|
-
body: request,
|
|
373
|
-
signal: options?.signal,
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
if (!response.ok) {
|
|
377
|
-
const errorText = await response.text();
|
|
378
|
-
throw new Error(`Meta-chat failed: ${response.status} - ${errorText}`);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
if (!response.body) {
|
|
382
|
-
throw new Error("No response body");
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
options?.onOpen?.();
|
|
386
|
-
|
|
387
|
-
const reader = response.body.getReader();
|
|
388
|
-
const decoder = new TextDecoder();
|
|
389
|
-
let buffer = "";
|
|
390
|
-
|
|
391
|
-
try {
|
|
392
|
-
while (true) {
|
|
393
|
-
const { done, value } = await reader.read();
|
|
394
|
-
if (done) break;
|
|
395
|
-
|
|
396
|
-
buffer += decoder.decode(value, { stream: true });
|
|
397
|
-
const lines = buffer.split("\n");
|
|
398
|
-
buffer = lines.pop() ?? "";
|
|
399
|
-
|
|
400
|
-
let eventType = "message";
|
|
401
|
-
let eventData = "";
|
|
402
|
-
|
|
403
|
-
for (const line of lines) {
|
|
404
|
-
if (line.startsWith("event:")) {
|
|
405
|
-
eventType = line.slice(6).trim();
|
|
406
|
-
} else if (line.startsWith("data:")) {
|
|
407
|
-
eventData = line.slice(5).trim();
|
|
408
|
-
} else if (line === "" && eventData) {
|
|
409
|
-
try {
|
|
410
|
-
const parsed = JSON.parse(eventData);
|
|
411
|
-
yield { type: eventType, data: parsed } as MetaChatStreamEvent;
|
|
412
|
-
} catch {
|
|
413
|
-
// Skip invalid JSON
|
|
414
|
-
}
|
|
415
|
-
eventData = "";
|
|
416
|
-
eventType = "message";
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
} finally {
|
|
421
|
-
reader.releaseLock();
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* Meta-chat and collect the proposed patch.
|
|
427
|
-
*/
|
|
428
|
-
async metaChatCollect(
|
|
429
|
-
agentId: string,
|
|
430
|
-
request: MetaChatRequest
|
|
431
|
-
): Promise<MetaChatResponse> {
|
|
432
|
-
let patch: GraphPatch | null = null;
|
|
433
|
-
let isValid = true;
|
|
434
|
-
const errors: PatchValidationError[] = [];
|
|
435
|
-
let message = "";
|
|
436
|
-
|
|
437
|
-
for await (const event of this.metaChat(agentId, request)) {
|
|
438
|
-
if (event.type === "assistant.delta") {
|
|
439
|
-
message += event.data.text;
|
|
440
|
-
} else if (event.type === "patch.proposed") {
|
|
441
|
-
patch = event.data;
|
|
442
|
-
} else if (event.type === "patch.validation") {
|
|
443
|
-
isValid = event.data.valid;
|
|
444
|
-
errors.push(...event.data.errors);
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return {
|
|
449
|
-
patch,
|
|
450
|
-
applied_spec: null, // Frontend should apply patch locally
|
|
451
|
-
valid: isValid,
|
|
452
|
-
errors,
|
|
453
|
-
message,
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
}
|