Skip to content

armgit / pibot

Public Imported from GitHub
0

Open-source, local-first AI agent team for your desktop: multi-agent group chats, computer use, browser, MCP, routines and approvals. macOS, Windows, Linux + Android. Built on pi.

6.8 KB
Raw Blame
// Deliberately bounded JSON Schema subset. Unknown assertion keywords must not
// silently disappear before approval; unsupported schemas are never dispatched.
export class ConnectorSchemaError extends Error {
  constructor(readonly code: "connector_schema_unsupported" | "connector_args_invalid") {
    super(code);
  }
}
type Schema = boolean | Record<string, unknown>;
const annotations = new Set(["title", "description", "default", "examples", "$comment", "$schema"]);
const keywords = new Set([
  "type",
  "properties",
  "required",
  "additionalProperties",
  "items",
  "enum",
  "const",
  "minLength",
  "maxLength",
  "minimum",
  "maximum",
  "exclusiveMinimum",
  "exclusiveMaximum",
  "minItems",
  "maxItems",
  "minProperties",
  "maxProperties",
  "allOf",
  "anyOf",
  "oneOf",
  "not",
]);
const types = new Set(["object", "array", "string", "number", "integer", "boolean", "null"]);
function unsupported(): never {
  throw new ConnectorSchemaError("connector_schema_unsupported");
}
function object(value: unknown): value is Record<string, unknown> {
  return value !== null && typeof value === "object" && !Array.isArray(value);
}

export function validateConnectorArgs(schema: unknown, args: Record<string, unknown>): void {
  let nodes = 0;
  function check(value: unknown, depth = 0): asserts value is Schema {
    if (++nodes > 2000 || depth > 24) unsupported();
    if (typeof value === "boolean") return;
    if (!object(value)) unsupported();
    for (const [key, entry] of Object.entries(value)) {
      // Vendor extensions (`x-…`, e.g. GitHub's `x-mcp-header`) are annotations by convention and
      // never constrain a value; rejecting them refused every call to GitHub's search tools and
      // broke the owner's routine connection trigger (2026-11-01).
      if (key.startsWith("x-")) continue;
      if (annotations.has(key)) {
        if (
          key === "$schema" &&
          ![
            "http://json-schema.org/draft-07/schema#",
            "https://json-schema.org/draft/2020-12/schema",
          ].includes(String(entry))
        )
          unsupported();
        continue;
      }
      if (!keywords.has(key)) unsupported();
      if (key === "type") {
        const list = Array.isArray(entry) ? entry : [entry];
        if (!list.length || list.some((kind) => typeof kind !== "string" || !types.has(kind)))
          unsupported();
      } else if (key === "properties") {
        if (!object(entry)) unsupported();
        for (const child of Object.values(entry)) check(child, depth + 1);
      } else if (["items", "additionalProperties", "not"].includes(key)) check(entry, depth + 1);
      else if (["allOf", "anyOf", "oneOf"].includes(key)) {
        if (!Array.isArray(entry) || !entry.length || entry.length > 100) unsupported();
        for (const child of entry) check(child, depth + 1);
      } else if (key === "required") {
        if (!Array.isArray(entry) || entry.some((name) => typeof name !== "string")) unsupported();
      } else if (key === "enum") {
        if (!Array.isArray(entry) || !entry.length) unsupported();
      } else if (key !== "const") {
        if (typeof entry !== "number" || !Number.isFinite(entry)) unsupported();
        if (
          (key.startsWith("min") || key.startsWith("max")) &&
          !["minimum", "maximum"].includes(key) &&
          (!Number.isSafeInteger(entry) || entry < 0)
        )
          unsupported();
      }
    }
  }
  check(schema);
  let steps = 0;
  function equal(a: unknown, b: unknown): boolean {
    if (a === b) return true;
    if (Array.isArray(a) && Array.isArray(b))
      return a.length === b.length && a.every((entry, index) => equal(entry, b[index]));
    if (object(a) && object(b)) {
      const keys = Object.keys(a);
      return (
        keys.length === Object.keys(b).length &&
        keys.every((key) => Object.hasOwn(b, key) && equal(a[key], b[key]))
      );
    }
    return false;
  }
  function matches(s: Schema, value: unknown, depth = 0): boolean {
    if (++steps > 20_000 || depth > 40) throw new ConnectorSchemaError("connector_args_invalid");
    if (typeof s === "boolean") return s;
    const isType = (type: string) => {
      if (type === "null") return value === null;
      if (type === "object") return object(value);
      if (type === "array") return Array.isArray(value);
      if (type === "integer") return typeof value === "number" && Number.isInteger(value);
      return typeof value === type;
    };
    if (
      s.type !== undefined &&
      !(Array.isArray(s.type) ? s.type : [s.type]).some((t) => isType(String(t)))
    )
      return false;
    if (s.enum && !(s.enum as unknown[]).some((entry) => equal(entry, value))) return false;
    if (Object.hasOwn(s, "const") && !equal(s.const, value)) return false;
    const test = (child: Schema) => matches(child, value, depth + 1);
    if (s.allOf && !(s.allOf as Schema[]).every(test)) return false;
    if (s.anyOf && !(s.anyOf as Schema[]).some(test)) return false;
    if (s.oneOf && (s.oneOf as Schema[]).filter(test).length !== 1) return false;
    if (s.not !== undefined && test(s.not as Schema)) return false;
    if (typeof value === "string") {
      const length = [...value].length;
      if (s.minLength !== undefined && length < Number(s.minLength)) return false;
      if (s.maxLength !== undefined && length > Number(s.maxLength)) return false;
    }
    if (typeof value === "number") {
      if (!Number.isFinite(value)) return false;
      if (s.minimum !== undefined && value < Number(s.minimum)) return false;
      if (s.maximum !== undefined && value > Number(s.maximum)) return false;
      if (s.exclusiveMinimum !== undefined && value <= Number(s.exclusiveMinimum)) return false;
      if (s.exclusiveMaximum !== undefined && value >= Number(s.exclusiveMaximum)) return false;
    }
    if (Array.isArray(value)) {
      if (s.minItems !== undefined && value.length < Number(s.minItems)) return false;
      if (s.maxItems !== undefined && value.length > Number(s.maxItems)) return false;
      if (s.items !== undefined && !value.every((v) => matches(s.items as Schema, v, depth + 1)))
        return false;
    }
    if (object(value)) {
      const keys = Object.keys(value);
      if (s.minProperties !== undefined && keys.length < Number(s.minProperties)) return false;
      if (s.maxProperties !== undefined && keys.length > Number(s.maxProperties)) return false;
      if (s.required && !(s.required as string[]).every((key) => Object.hasOwn(value, key)))
        return false;
      const properties = (s.properties ?? {}) as Record<string, Schema>;
      for (const key of keys) {
        const child = Object.hasOwn(properties, key) ? properties[key] : s.additionalProperties;
        if (child !== undefined && !matches(child as Schema, value[key], depth + 1)) return false;
      }
    }
    return true;
  }
  if (!matches(schema, args)) throw new ConnectorSchemaError("connector_args_invalid");
}