Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

12.5 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { ARGUMENT_REFUSAL, argumentRefusalNote, exampleCall, fitArguments, fittedArgumentsNote } from "./connector-arguments.ts";

/** The shapes Composio's own tools list, a definition behind a local $ref included. */
const SEARCH = {
  type: "object",
  properties: { queries: { type: "array", items: { $ref: "#/$defs/Query" } }, session: { type: "object", properties: { id: { type: "string" } } } },
  required: ["queries"],
  $defs: { Query: { type: "object", properties: { use_case: { type: "string" }, known_fields: { type: "string" } }, required: ["use_case"] } },
};
const WORKBENCH = {
  type: "object",
  properties: { code_to_execute: { type: "string" }, thought: { type: "string" }, timeout: { type: "number" } },
  required: ["code_to_execute"],
};
const MULTI = {
  type: "object",
  properties: {
    tools: { type: "array", items: { type: "object", properties: { tool_slug: { type: "string" }, arguments: { type: "object" } }, required: ["tool_slug", "arguments"] } },
    sync_response_to_workbench: { type: "boolean" },
    session_id: { type: "string" },
  },
  required: ["tools"],
};

describe("fitting a call's arguments to the tool's schema", () => {
  it("fits each argument mistake bots made in a Team trial", () => {
    expect(fitArguments({ job: "generate an image from a text description" }, SEARCH)).toEqual({
      args: { queries: [{ use_case: "generate an image from a text description" }] },
      changed: ["job as queries", 'queries as [{"use_case": …}]'],
    });
    expect(fitArguments({ code: "print(1)" }, WORKBENCH)).toEqual({ args: { code_to_execute: "print(1)" }, changed: ["code as code_to_execute"] });
    expect(fitArguments({ code_to_execute: "print(1)", timeout: "120" }, WORKBENCH)).toEqual({ args: { code_to_execute: "print(1)", timeout: 120 }, changed: ["timeout as a number"] });
    const listed = JSON.stringify([{ tool_slug: "GOOGLE_ANALYTICS_BATCH_RUN_REPORTS", arguments: { property: "properties/1" } }]);
    expect(fitArguments({ session_id: "s", sync_response_to_workbench: false, tools: listed }, MULTI)).toEqual({
      args: { session_id: "s", sync_response_to_workbench: false, tools: [{ tool_slug: "GOOGLE_ANALYTICS_BATCH_RUN_REPORTS", arguments: { property: "properties/1" } }] },
      changed: ["tools from text to a list"],
    });
  });

  it("reads a bare string or a list of strings where a list of one-field objects goes, arguments sent as text, and nested text", () => {
    expect(fitArguments({ queries: "send an email" }, SEARCH).args).toEqual({ queries: [{ use_case: "send an email" }] });
    expect(fitArguments({ queries: ["send an email", "read my calendar"] }, SEARCH).args).toEqual({ queries: [{ use_case: "send an email" }, { use_case: "read my calendar" }] });
    expect(fitArguments(JSON.stringify({ queries: [{ use_case: "x" }] }), SEARCH)).toEqual({ args: { queries: [{ use_case: "x" }] }, changed: ["the arguments from text to an object"] });
    expect(fitArguments({ tools: [{ tool_slug: "GMAIL_SEND_EMAIL", arguments: '{"to":"[email protected]"}' }] }, MULTI)).toEqual({
      args: { tools: [{ tool_slug: "GMAIL_SEND_EMAIL", arguments: { to: "[email protected]" } }] },
      changed: ["tools[0].arguments from text to an object"],
    });
  });

  it.each(["number", "integer"])("preserves unsafe numeric text for a %s argument", (type) => {
    const schema = { type: "object", properties: { value: { type } } };
    for (const value of ["9007199254740993", "-9007199254740993", "9".repeat(400)]) {
      const args = { value };
      const fitted = fitArguments(args, schema);
      expect(fitted).toEqual({ args, changed: [] });
      expect(fitted.args).toBe(args);
      expect(JSON.stringify(fitted.args)).toBe(JSON.stringify(args));
    }
  });

  it("fits only integral text to an integer schema", () => {
    const schema = { type: "object", properties: { value: { type: "integer" } } };
    expect(fitArguments({ value: "1.5" }, schema)).toEqual({ args: { value: "1.5" }, changed: [] });
    for (const value of [" 42 ", "-12", "9007199254740991", "42.0"]) {
      expect(fitArguments({ value }, schema)).toEqual({ args: { value: Number(value) }, changed: ["value as a number"] });
    }
    expect(fitArguments({ value: "1.5" }, { type: "object", properties: { value: { type: "number" } } })).toEqual({
      args: { value: 1.5 }, changed: ["value as a number"],
    });
  });

  it("leaves a well-formed call as it is, and never guesses which of two sent fields was meant", () => {
    const fine = { queries: [{ use_case: "x" }], session: { id: "s" } };
    expect(fitArguments(fine, SEARCH)).toEqual({ args: fine, changed: [] });
    expect(fitArguments(fine, SEARCH).args).toBe(fine);
    expect(fitArguments({ script: "a", notes: "b" }, WORKBENCH)).toEqual({ args: { script: "a", notes: "b" }, changed: [] });
    // Of two text fields, the one sharing a word with the missing name is the one meant.
    expect(fitArguments({ code: "a", note: "b" }, WORKBENCH).args).toEqual({ code_to_execute: "a", note: "b" });
    expect(fitArguments({ timeout: "soon", code_to_execute: "x" }, WORKBENCH).changed).toEqual([]);
    expect(fitArguments("not json", SEARCH)).toEqual({ args: "not json", changed: [] });
    expect(fitArguments({ anything: 1 }, undefined)).toEqual({ args: { anything: 1 }, changed: [] });
  });
});

describe("what a bot is told", () => {
  it("shows a call each schema accepts", () => {
    expect(exampleCall(SEARCH)).toBe('{"queries":[{"use_case":"…"}]}');
    expect(exampleCall(WORKBENCH)).toBe('{"code_to_execute":"…"}');
    expect(exampleCall(MULTI)).toBe('{"tools":[{"tool_slug":"…","arguments":{}}]}');
    expect(exampleCall({ type: "object", properties: {} })).toBeNull();
  });

  it("says how to call again, and that a refusal over arguments says nothing about which tools exist", () => {
    const note = argumentRefusalNote("COMPOSIO_SEARCH_TOOLS", SEARCH, ["gemini", "gmail"]);
    expect(note).toContain('Call it again with the arguments its schema names, for example {"queries":[{"use_case":"…"}]} with your own values.');
    expect(note).toContain("it says nothing about which apps or tools exist, so do not tell the person a tool is missing. Connected apps you can use: gemini, gmail.");
    expect(argumentRefusalNote("GMAIL_SEND_EMAIL", undefined, [])).toContain("Read its arguments with COMPOSIO_GET_TOOL_SCHEMAS");
    expect(fittedArgumentsNote("COMPOSIO_SEARCH_TOOLS", ["job as queries"])).toBe("OpenMausBot note: COMPOSIO_SEARCH_TOOLS's arguments were read with job as queries. Send them in that shape next time.");
  });

  it("knows Composio's refusal of a call's arguments", () => {
    expect(ARGUMENT_REFUSAL.test('Invalid request for COMPOSIO_SEARCH_TOOLS: Validation error: Required at "queries"')).toBe(true);
    expect(ARGUMENT_REFUSAL.test('Invalid request for COMPOSIO_REMOTE_WORKBENCH: Validation error: Expected number, received string at "timeout"')).toBe(true);
    expect(ARGUMENT_REFUSAL.test("The calendar has no events that day.")).toBe(false);
  });
});

describe("numeric text preservation", () => {
  it.each(["number", "integer"])("keeps nonzero decimal text that underflows for %s", (type) => {
    const schema = { type: "object", properties: { value: { type } } };
    const tiny = "0." + "0".repeat(323) + "1";
    for (const value of [tiny, "-" + tiny]) {
      const args = { value };
      expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
      expect(fitArguments(args, schema).args).toBe(args);
    }
  });

  it("keeps fractional text that rounds to an integer", () => {
    const schema = { type: "object", properties: { value: { type: "integer" } } };
    for (const value of ["1.0000000000000001", "-1.0000000000000001", "9007199254740991.1"]) {
      const args = { value };
      expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
    }
  });

  it("keeps decimal text whose significant digits would be rounded", () => {
    const schema = { type: "object", properties: { value: { type: "number" } } };
    for (const value of ["0.10000000000000001", "1.234567890123456789", "-1.234567890123456789"]) {
      const args = { value };
      expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
    }
  });

  it.each([
    "9007199254740993", "-9007199254740993", "1e400", "-1e400",
    "1e-400", "-1e-400", "1.0000000000000001", "9007199254740993e-1",
  ])("keeps root JSON text containing the unsafe numeric token %s", (token) => {
    const args = '{"value":' + token + "}";
    const schema = { type: "object", properties: { value: { type: "number" } } };
    expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
  });

  it.each([
    { type: "object", properties: { value: { type: "number" } } },
    { type: "object" },
  ])("keeps unsafe nested object text for schema %j", (objectSchema) => {
    const args = { payload: '{"value":9007199254740993}' };
    const schema = { type: "object", properties: { payload: objectSchema } };
    expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
  });

  it.each([
    { name: "numeric items", items: { type: "number" }, value: "[9007199254740993]" },
    { name: "string items", items: { type: "string" }, value: "[9007199254740993]" },
    { name: "one-field objects", items: { type: "object", properties: { use_case: { type: "string" } }, required: ["use_case"] }, value: '[{"use_case":"x","limit":9007199254740993}]' },
  ])("keeps unsafe array text without a fallback wrapper for $name", ({ items, value }) => {
    const args = { values: value };
    const schema = { type: "object", properties: { values: { type: "array", items } } };
    expect(fitArguments(args, schema)).toEqual({ args, changed: [] });
    expect(fitArguments(args, schema).args).toBe(args);
  });

  it("keeps unsafe nested text when a separate safe field is fitted", () => {
    const payload = '{"limit":1e400}';
    const schema = { type: "object", properties: { payload: { type: "object" }, timeout: { type: "number" } } };
    expect(fitArguments({ payload, timeout: "42" }, schema)).toEqual({
      args: { payload, timeout: 42 },
      changed: ["timeout as a number"],
    });
  });

  it("still fits exact numeric text including zero-only fractional digits", () => {
    for (const type of ["number", "integer"]) {
      const schema = { type: "object", properties: { value: { type } } };
      for (const value of [" 42 ", "-12", "42.000", "00042.000", "-0.000", "9007199254740991"]) {
        expect(fitArguments({ value }, schema)).toEqual({ args: { value: Number(value) }, changed: ["value as a number"] });
      }
    }
  });

  it("still parses equivalent decimal and exponent JSON spellings", () => {
    const schema = { type: "object", properties: { value: { type: "number" } } };
    for (const token of ["0.1", "1.50", "4.2e1", "1e-7", "0e400", "-0.000e-400", "9007199254740991.0"]) {
      const args = '{"value":' + token + "}";
      expect(fitArguments(args, schema)).toEqual({
        args: { value: Number(token) },
        changed: ["the arguments from text to an object"],
      });
    }
  });

  it("does not interpret numeric-looking quoted strings as numeric tokens", () => {
    const value = '9007199254740993 and 1e400, with "quoted" text';
    const schema = { type: "object", properties: { value: { type: "string" }, enabled: { type: "boolean" }, extra: {} } };
    const args = { value, enabled: false, extra: null };
    expect(fitArguments(JSON.stringify(args), schema)).toEqual({
      args,
      changed: ["the arguments from text to an object"],
    });
  });

  it("keeps ordinary text fallback and malformed JSON behavior", () => {
    expect(fitArguments({ queries: "read my calendar" }, SEARCH)).toEqual({
      args: { queries: [{ use_case: "read my calendar" }] },
      changed: ['queries as [{"use_case": …}]'],
    });
    const args = '{"value": 12,}';
    expect(fitArguments(args, { type: "object", properties: { value: { type: "number" } } })).toEqual({ args, changed: [] });
  });

  it.each(["string items", "one-field objects"])("keeps numeric-looking plain text fallback for %s", (kind) => {
    const items = kind === "string items"
      ? { type: "string" }
      : { type: "object", properties: { use_case: { type: "string" } }, required: ["use_case"] };
    const schema = { type: "object", properties: { values: { type: "array", items } } };
    for (const value of ["9007199254740993", "1e400", "0.10000000000000001", '{"value":9007199254740993}', '{"value":1e400}']) {
      const expected = kind === "string items" ? [value] : [{ use_case: value }];
      const fitted = fitArguments({ values: value }, schema);
      expect(fitted.args).toEqual({ values: expected });
      expect(fitted.changed).toHaveLength(1);
    }
  });

});