@proagentstore/cli 0.4.159 → 0.4.160
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.
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { stripAnsi } from "./transcript-lines.js";
|
|
2
|
+
import { createGrokEngineAdapter, grokOutputFormat } from "./grok-adapter.js";
|
|
2
3
|
export function engineInvocationModeFromAdapter(mode) {
|
|
3
4
|
return mode === "stream-json" ? "structured" : "raw";
|
|
4
5
|
}
|
|
5
6
|
export function structuredCapableEngine(clientType) {
|
|
6
|
-
return clientType === "claude" || clientType === "codex";
|
|
7
|
+
return clientType === "claude" || clientType === "codex" || clientType === "grok";
|
|
7
8
|
}
|
|
8
9
|
export function engineInvocationWarning(clientType, mode) {
|
|
9
10
|
if (mode !== "raw" || !structuredCapableEngine(clientType))
|
|
@@ -234,5 +235,8 @@ export function engineAdapterFor(clientType, userArgs = []) {
|
|
|
234
235
|
return claudeEngineAdapter;
|
|
235
236
|
if (clientType === "codex" && userArgs[0] === "exec" && !["resume", "fork", "review", "help"].includes(userArgs[1] ?? ""))
|
|
236
237
|
return codexEngineAdapter;
|
|
238
|
+
// Grok's NDJSON stream (#1187) — unless the preset chose another output format on purpose, which stays raw.
|
|
239
|
+
if (clientType === "grok" && [null, "streaming-json"].includes(grokOutputFormat(userArgs)))
|
|
240
|
+
return createGrokEngineAdapter();
|
|
237
241
|
return genericRawEngineAdapter;
|
|
238
242
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
function record(v) {
|
|
2
|
+
return v && typeof v === "object" && !Array.isArray(v) ? v : null;
|
|
3
|
+
}
|
|
4
|
+
function hasFlag(args, flag) {
|
|
5
|
+
return args.some((a) => a === flag || a.startsWith(`${flag}=`));
|
|
6
|
+
}
|
|
7
|
+
/** The value a preset gave `--output-format`, or null. */
|
|
8
|
+
export function grokOutputFormat(args) {
|
|
9
|
+
for (let i = 0; i < args.length; i++) {
|
|
10
|
+
if (args[i] === "--output-format")
|
|
11
|
+
return args[i + 1] ?? null;
|
|
12
|
+
if (args[i].startsWith("--output-format="))
|
|
13
|
+
return args[i].slice("--output-format=".length);
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
/** The preset's argv with the structured-output flags in front — any order parses, and the trailing `-p` stays last. */
|
|
18
|
+
export function grokStructuredArgs(userArgs) {
|
|
19
|
+
const add = [];
|
|
20
|
+
if (!grokOutputFormat(userArgs))
|
|
21
|
+
add.push("--output-format", "streaming-json");
|
|
22
|
+
if (!hasFlag(userArgs, "--no-auto-update"))
|
|
23
|
+
add.push("--no-auto-update");
|
|
24
|
+
return [...add, ...userArgs];
|
|
25
|
+
}
|
|
26
|
+
/** The text a terminal call produced: its content blocks, else `output_for_prompt` without its `exit: N` header. */
|
|
27
|
+
function toolOutput(ev, raw) {
|
|
28
|
+
const texts = (Array.isArray(ev.content) ? ev.content : []).flatMap((c) => {
|
|
29
|
+
const inner = record(record(c)?.content);
|
|
30
|
+
return typeof inner?.text === "string" ? [inner.text] : [];
|
|
31
|
+
});
|
|
32
|
+
if (texts.some((t) => t.trim()))
|
|
33
|
+
return texts.join("");
|
|
34
|
+
const prompt = typeof raw?.output_for_prompt === "string" ? raw.output_for_prompt : "";
|
|
35
|
+
return prompt.replace(/^exit: -?\d+\n/, "");
|
|
36
|
+
}
|
|
37
|
+
/** A Grok call as the tool the rest of the runner knows: its terminal tool IS a Bash call. */
|
|
38
|
+
function asTool(ev) {
|
|
39
|
+
const rawInput = record(ev.rawInput) ?? {};
|
|
40
|
+
if (ev.kind === "execute" && typeof rawInput.command === "string")
|
|
41
|
+
return { name: "Bash", input: { command: rawInput.command } };
|
|
42
|
+
return { name: typeof ev.toolName === "string" && ev.toolName ? ev.toolName : String(ev.title ?? "tool"), input: rawInput };
|
|
43
|
+
}
|
|
44
|
+
/** One session's adapter: it holds the reply being streamed in deltas until something else happens. */
|
|
45
|
+
export function createGrokEngineAdapter() {
|
|
46
|
+
let text = "";
|
|
47
|
+
const flush = () => {
|
|
48
|
+
const t = text.trim();
|
|
49
|
+
text = "";
|
|
50
|
+
return t ? [{ kind: "assistant_text", text: t }] : [];
|
|
51
|
+
};
|
|
52
|
+
function parseLine(line) {
|
|
53
|
+
let ev;
|
|
54
|
+
try {
|
|
55
|
+
ev = record(JSON.parse(line));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
if (!ev)
|
|
61
|
+
return [];
|
|
62
|
+
switch (ev.type) {
|
|
63
|
+
case "text":
|
|
64
|
+
if (typeof ev.data === "string")
|
|
65
|
+
text += ev.data;
|
|
66
|
+
return [{ kind: "meta" }];
|
|
67
|
+
case "thought":
|
|
68
|
+
case "usage":
|
|
69
|
+
case "available_commands":
|
|
70
|
+
return [{ kind: "meta" }];
|
|
71
|
+
case "tool_call": {
|
|
72
|
+
const id = typeof ev.toolCallId === "string" ? ev.toolCallId : "";
|
|
73
|
+
const { name, input } = asTool(ev);
|
|
74
|
+
return [...flush(), { kind: "tool_use", block: { type: "tool_use", id, name, input }, id, name, input }];
|
|
75
|
+
}
|
|
76
|
+
case "tool_call_update": {
|
|
77
|
+
// Pending and in-progress updates are the same call filling in; only its END settles it.
|
|
78
|
+
if (ev.status !== "completed" && ev.status !== "failed")
|
|
79
|
+
return [{ kind: "meta" }];
|
|
80
|
+
const id = typeof ev.toolCallId === "string" ? ev.toolCallId : "";
|
|
81
|
+
const raw = record(ev.rawOutput);
|
|
82
|
+
const exitCode = typeof raw?.exit_code === "number" ? raw.exit_code : null;
|
|
83
|
+
const content = toolOutput(ev, raw);
|
|
84
|
+
const block = { type: "tool_result", tool_use_id: id, is_error: ev.status === "failed" || (exitCode !== null && exitCode !== 0), content };
|
|
85
|
+
return [{ kind: "tool_result", block, toolUseId: id, content }];
|
|
86
|
+
}
|
|
87
|
+
case "end": {
|
|
88
|
+
const stop = typeof ev.stopReason === "string" ? ev.stopReason : "";
|
|
89
|
+
const isError = stop !== "end_turn";
|
|
90
|
+
return [...flush(), { kind: "turn_end", raw: ev, isError, result: isError ? `stopped: ${stop || "unknown"}` : "" }];
|
|
91
|
+
}
|
|
92
|
+
case "error": {
|
|
93
|
+
const message = typeof ev.message === "string" ? ev.message : typeof record(ev.error)?.message === "string" ? record(ev.error)?.message : "";
|
|
94
|
+
return message.trim() ? [{ kind: "engine_error", text: message.trim() }] : [{ kind: "meta" }];
|
|
95
|
+
}
|
|
96
|
+
default:
|
|
97
|
+
return [];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
mode: "stream-json",
|
|
102
|
+
persistent: false,
|
|
103
|
+
prepareArgs: grokStructuredArgs,
|
|
104
|
+
buildLaunchArgs: (userArgs) => [...userArgs],
|
|
105
|
+
buildTurnArgs: (userArgs, turnText) => [...userArgs, turnText],
|
|
106
|
+
parseLine,
|
|
107
|
+
// An older Grok CLI that does not know a flag refuses before it runs the prompt, so one retry raw is safe.
|
|
108
|
+
rejectsStructuredOutput: (line) => /(?:--output-format|--no-auto-update|streaming-json)/.test(line) && /(?:unexpected argument|unknown (?:argument|option)|unrecognized|invalid value|isn't a valid value)/i.test(line),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
@@ -15,7 +15,7 @@ const HANDLERS = {
|
|
|
15
15
|
claude: { clientType: "claude", cliCommand: "claude --dangerously-skip-permissions", envVar: "ANTHROPIC_API_KEY" },
|
|
16
16
|
gemini: { clientType: "gemini", cliCommand: "gemini --approval-mode yolo --skip-trust --prompt", envVar: "GEMINI_API_KEY" },
|
|
17
17
|
codex: { clientType: "codex", cliCommand: "codex exec --json --sandbox danger-full-access", envVar: "OPENAI_API_KEY" },
|
|
18
|
-
grok: { clientType: "grok", cliCommand: "grok --permission-mode bypassPermissions -p", envVar: "XAI_API_KEY" },
|
|
18
|
+
grok: { clientType: "grok", cliCommand: "grok --permission-mode bypassPermissions --no-auto-update --output-format streaming-json -p", envVar: "XAI_API_KEY" },
|
|
19
19
|
generic: { clientType: "generic", cliCommand: "bash", envVar: "" },
|
|
20
20
|
};
|
|
21
21
|
export function handlerFor(clientType) {
|
|
@@ -601,11 +601,12 @@ export class HeadlessSession {
|
|
|
601
601
|
if (this.config.clientType === "grok" && this.grokContinuity && !this.grokConversation)
|
|
602
602
|
this.grokConversation = newGrokConversation();
|
|
603
603
|
const grokConversation = this.config.clientType === "grok" && this.grokContinuity ? this.grokConversation : null;
|
|
604
|
+
const presetArgs = this.adapter.prepareArgs?.(this.cmdArgs) ?? this.cmdArgs; // #1187: Grok's structured flags; gone with the adapter if refused
|
|
604
605
|
const turnArgs = resumedCodexThreadId
|
|
605
606
|
? buildCodexResumeArgs(this.cmdArgs, resumedCodexThreadId, text)
|
|
606
607
|
: grokConversation
|
|
607
|
-
? grokTurnArgs(
|
|
608
|
-
: this.adapter.buildTurnArgs(
|
|
608
|
+
? grokTurnArgs(presetArgs, text, grokConversation)
|
|
609
|
+
: this.adapter.buildTurnArgs(presetArgs, text);
|
|
609
610
|
const env = this.spawnEnv;
|
|
610
611
|
const target = spawnTarget(this.cmdPrefix, this.cmdBin, turnArgs);
|
|
611
612
|
const launch = resolveEngineSpawn(target.command, target.args, { env });
|