Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

2.9 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import { GrokCliExecutor } from "../../open-sse/executors/grok-cli.ts";
import type { ProviderCredentials } from "../../open-sse/executors/base.ts";

const testCredentials: ProviderCredentials = { accessToken: "tok" };

test("#7611: GrokCliExecutor sanitizes incomplete hex escapes in function_call_output", () => {
  const executor = new GrokCliExecutor();
  const body = {
    model: "grok-4.5",
    input: [
      {
        type: "function_call",
        call_id: "call_1",
        name: "read_file",
        arguments: "{}",
      },
      {
        type: "function_call_output",
        call_id: "call_1",
        // incomplete \u escape — the production failure mode
        output: '{"path":"foo","snippet":"bad \\u12"}',
      },
    ],
  };

  const transformed = executor.transformRequest("grok-4.5", body, true, testCredentials) as Record<
    string,
    unknown
  >;

  const input = transformed.input as Array<Record<string, unknown>>;
  const outputItem = input.find((item) => item.type === "function_call_output");
  assert.ok(outputItem, "function_call_output item should remain");
  assert.equal(typeof outputItem.output, "string");
  // Must be re-parseable as a JSON string payload after sanitization.
  assert.doesNotThrow(() => JSON.parse(JSON.stringify({ output: outputItem.output })));
  assert.doesNotMatch(String(outputItem.output), /\\u12(?![0-9A-Fa-f])/);
});

test("#7611: valid JSON tool outputs are re-serialized cleanly", () => {
  const executor = new GrokCliExecutor();
  const payload = { ok: true, data: ["a", "b"], note: "café" };
  const body = {
    model: "grok-4.5",
    input: [
      {
        type: "function_call_output",
        call_id: "call_2",
        output: JSON.stringify(payload),
      },
    ],
  };
  const transformed = executor.transformRequest("grok-4.5", body, false, testCredentials) as Record<
    string,
    unknown
  >;
  const input = transformed.input as Array<Record<string, unknown>>;
  const outputItem = input.find((item) => item.type === "function_call_output");
  assert.deepEqual(JSON.parse(String(outputItem?.output)), payload);
});

test("#7611: array content parts are flattened to text", () => {
  const executor = new GrokCliExecutor();
  const body = {
    model: "grok-4.5",
    input: [
      {
        type: "function_call_output",
        call_id: "call_3",
        output: [
          { type: "input_text", text: "hello" },
          { type: "input_text", text: "world" },
        ],
      },
    ],
  };
  const transformed = executor.transformRequest("grok-4.5", body, false, testCredentials) as Record<
    string,
    unknown
  >;
  const input = transformed.input as Array<Record<string, unknown>>;
  const outputItem = input.find((item) => item.type === "function_call_output");
  assert.equal(typeof outputItem?.output, "string");
  assert.match(String(outputItem?.output), /hello/);
  assert.match(String(outputItem?.output), /world/);
});