Skip to content

Commit a32f359

Browse files
committed
fix: agent stream returns actual text, --fields warns on no matches
- agent stream now outputs plain text (or structured JSON with --json) instead of JSON-stringified "ok" - --fields with no matching field names prints available fields warning - Addresses Codex autoresearch Exp 5 (stream scored 1/10 → 8/10)
1 parent a0edc75 commit a32f359

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

packages/cli/src/cli/main.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,17 @@ function printJson(data: unknown): void {
122122
/** Apply --fields filter to output. Reduces context window for AI agents. */
123123
function applyFieldsFilter(data: unknown, fields?: string): unknown {
124124
if (!fields) return data;
125-
const keys = fields.split(",").map((f) => f.trim());
126-
if (Array.isArray(data)) {
127-
return data.map((item) => pickFields(item, keys));
125+
const keys = fields.split(",").map((f) => f.trim()).filter(Boolean);
126+
if (Array.isArray(data) && data.length > 0) {
127+
const sample = data[0] as Record<string, unknown>;
128+
const available = Object.keys(sample);
129+
const invalid = keys.filter((k) => !available.includes(k));
130+
if (invalid.length > 0 && invalid.length === keys.length) {
131+
console.error(`Warning: No matching fields. Available: ${available.slice(0, 15).join(", ")}`);
132+
}
133+
return data.map((item) => pickFields(item as Record<string, unknown>, keys));
128134
}
129-
if (data && typeof data === "object") {
135+
if (data && typeof data === "object" && !Array.isArray(data)) {
130136
return pickFields(data as Record<string, unknown>, keys);
131137
}
132138
return data;
@@ -3587,14 +3593,23 @@ export function createProgram(): Command {
35873593
const body = loadJsonPayload(opts.body);
35883594
ensureLocalValidation("agent.stream", validateAgentStreamPayload(body));
35893595
const streamBody = body as import("@pixelml/agenticflow-sdk").StreamRequest;
3590-
if (token) {
3591-
const stream = await client.agents.stream(opts.agentId, streamBody);
3592-
const text = await stream.text();
3593-
await run(() => Promise.resolve(text));
3594-
} else {
3595-
const stream = await client.agents.streamAnonymous(opts.agentId, streamBody);
3596+
try {
3597+
const stream = token
3598+
? await client.agents.stream(opts.agentId, streamBody)
3599+
: await client.agents.streamAnonymous(opts.agentId, streamBody);
35963600
const text = await stream.text();
3597-
await run(() => Promise.resolve(text));
3601+
if (isJsonFlagEnabled()) {
3602+
printResult({
3603+
schema: "agenticflow.agent.stream.v1",
3604+
agent_id: opts.agentId,
3605+
response: text,
3606+
});
3607+
} else {
3608+
console.log(text);
3609+
}
3610+
} catch (err) {
3611+
const message = err instanceof Error ? err.message : String(err);
3612+
fail("stream_failed", message);
35983613
}
35993614
});
36003615

0 commit comments

Comments
 (0)