Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

9.7 KB
Raw Blame
import * as Predicate from "effect/Predicate";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
import * as SchemaTransformation from "effect/SchemaTransformation";
import { TrimmedNonEmptyString } from "./baseSchemas.ts";
import { ProviderDriverKind } from "./providerInstance.ts";

export const ProviderOptionDescriptorType = Schema.Literals(["select", "boolean"]);

export type ProviderOptionDescriptorType = typeof ProviderOptionDescriptorType.Type;

export const ProviderOptionChoice = Schema.Struct({
  id: TrimmedNonEmptyString,
  label: TrimmedNonEmptyString,
  description: Schema.optional(TrimmedNonEmptyString),
  isDefault: Schema.optional(Schema.Boolean),
});

export type ProviderOptionChoice = typeof ProviderOptionChoice.Type;

const ProviderOptionDescriptorBase = {
  id: TrimmedNonEmptyString,
  label: TrimmedNonEmptyString,
  description: Schema.optional(TrimmedNonEmptyString),
} as const;

export const SelectProviderOptionDescriptor = Schema.Struct({
  ...ProviderOptionDescriptorBase,
  type: Schema.Literal("select"),
  options: Schema.Array(ProviderOptionChoice),
  currentValue: Schema.optional(TrimmedNonEmptyString),
  promptInjectedValues: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
});

export type SelectProviderOptionDescriptor = typeof SelectProviderOptionDescriptor.Type;

export const BooleanProviderOptionDescriptor = Schema.Struct({
  ...ProviderOptionDescriptorBase,
  type: Schema.Literal("boolean"),
  currentValue: Schema.optional(Schema.Boolean),
});

export type BooleanProviderOptionDescriptor = typeof BooleanProviderOptionDescriptor.Type;

export const ProviderOptionDescriptor = Schema.Union([
  SelectProviderOptionDescriptor,
  BooleanProviderOptionDescriptor,
]);

export type ProviderOptionDescriptor = typeof ProviderOptionDescriptor.Type;

export const ProviderOptionSelectionValue = Schema.Union([TrimmedNonEmptyString, Schema.Boolean]);

export type ProviderOptionSelectionValue = typeof ProviderOptionSelectionValue.Type;

export const ProviderOptionSelection = Schema.Struct({
  id: TrimmedNonEmptyString,
  value: ProviderOptionSelectionValue,
});

export type ProviderOptionSelection = typeof ProviderOptionSelection.Type;

/**
 * Legacy on-disk shape for provider option selections, kept readable by the
 * decoder so we can tolerate stored data written before the v3 array shape.
 *
 * Persisted historically as `{ effort: "max", fastMode: true, ... }` inside
 * `modelSelection.options`. Migration 026 rewrites stored rows to the
 * canonical array shape, but we still see the legacy form in:
 *   - `settings.json` files from older client builds,
 *   - SQLite databases that have not yet run migration 026,
 *   - any future regression that re-introduces the legacy shape.
 */
const LegacyProviderOptionSelectionsObject = Schema.Record(Schema.String, Schema.Unknown);

const ProviderOptionSelectionsFromLegacyObject = LegacyProviderOptionSelectionsObject.pipe(
  Schema.decodeTo(
    Schema.Array(ProviderOptionSelection),
    SchemaTransformation.transformOrFail({
      decode: (record) => Effect.succeed(coerceLegacyOptionsObjectToArray(record)),
      encode: (selections) => Effect.succeed(canonicalSelectionsToLegacyObject(selections)),
    }),
  ),
);

/**
 * Schema for the `options` field of every `ModelSelection` variant.
 *
 * Accepts both:
 *   - the canonical array shape `Array<{ id, value }>` (preferred), and
 *   - the legacy object shape `Record<string, string | boolean | …>` from
 *     pre-migration data.
 *
 * Always normalizes to the canonical array on decode and re-encodes as the
 * canonical array, so any legacy storage gets cleaned up the next time the
 * containing record is written back.
 */
export const ProviderOptionSelections = Schema.Union([
  Schema.Array(ProviderOptionSelection),
  ProviderOptionSelectionsFromLegacyObject,
]);

export type ProviderOptionSelections = typeof ProviderOptionSelections.Type;

function coerceLegacyOptionsObjectToArray(
  record: typeof LegacyProviderOptionSelectionsObject.Type,
): ReadonlyArray<ProviderOptionSelection> {
  const entries: Array<ProviderOptionSelection> = [];

  for (const [rawKey, rawValue] of Object.entries(record)) {
    const id = Predicate.isString(rawKey) ? rawKey.trim() : "";

    if (id.length === 0) continue;

    if (Predicate.isString(rawValue)) {
      const trimmed = rawValue.trim();

      if (trimmed.length > 0) entries.push({ id, value: trimmed });
    } else if (Predicate.isBoolean(rawValue)) {
      entries.push({ id, value: rawValue });
    }
    // Drop anything else (numbers, null, nested objects/arrays) to match the
    // permissive normalization performed by migration 026.
  }

  return entries;
}

function canonicalSelectionsToLegacyObject(selections: ReadonlyArray<ProviderOptionSelection>) {
  const out: Record<string, string | boolean> = {};

  for (const { id, value } of selections) {
    out[id] = value;
  }

  return out;
}

export const ModelCapabilities = Schema.Struct({
  optionDescriptors: Schema.optional(Schema.Array(ProviderOptionDescriptor)),
});

export type ModelCapabilities = typeof ModelCapabilities.Type;

const CODEX_DRIVER_KIND = ProviderDriverKind.make("codex");

const CLAUDE_DRIVER_KIND = ProviderDriverKind.make("claudeAgent");

const GROK_DRIVER_KIND = ProviderDriverKind.make("grok");

const KIMI_DRIVER_KIND = ProviderDriverKind.make("kimi");

const OPENCODE_DRIVER_KIND = ProviderDriverKind.make("opencode");

const OPENCODE_GO_DRIVER_KIND = ProviderDriverKind.make("opencodeGo");

const CUSTOM_OPENAI_DRIVER_KIND = ProviderDriverKind.make("customOpenai");

export const DEFAULT_MODEL = "gpt-6.1-sol";

/**
 * Codex default-model preference, most preferred first. The provider snapshot
 * marks the first of these present in the live `model/list` response as
 * default; when none are available, Codex's own `isDefault` flag wins.
 */
export const PREFERRED_DEFAULT_CODEX_MODELS: ReadonlyArray<string> = [
  "gpt-6.1-sol",
  "gpt-6-sol",
  "gpt-5.6-sol",
  "gpt-5.6-terra",
];

export const DEFAULT_TEXT_GENERATION_MODEL = "gpt-6-luna";

export const DEFAULT_TEXT_GENERATION_REASONING_EFFORT = "low";

export const DEFAULT_MODEL_BY_PROVIDER: Partial<Record<ProviderDriverKind, string>> = {
  [CODEX_DRIVER_KIND]: DEFAULT_MODEL,
  [CLAUDE_DRIVER_KIND]: "claude-opus-5-5",
  // Product slug, not an ACP model id. The Grok adapter treats it as "the session's current model".
  [GROK_DRIVER_KIND]: "grok-build",
  [KIMI_DRIVER_KIND]: "k3",
  [OPENCODE_DRIVER_KIND]: "openai/gpt-6.1-sol",
  [OPENCODE_GO_DRIVER_KIND]: "gpt-6-luna",
};

/** Per-provider text generation model defaults. */
export const DEFAULT_TEXT_GENERATION_MODEL_BY_PROVIDER: Partial<
  Record<ProviderDriverKind, string>
> = {
  [CODEX_DRIVER_KIND]: DEFAULT_TEXT_GENERATION_MODEL,
  [CLAUDE_DRIVER_KIND]: "claude-haiku-4-5",
  [OPENCODE_DRIVER_KIND]: "openai/gpt-6-luna",
};

export const MODEL_SLUG_ALIASES_BY_PROVIDER: Partial<
  Record<ProviderDriverKind, Record<string, string>>
> = {
  [CODEX_DRIVER_KIND]: {
    "gpt-5-codex": "gpt-5.4",
    "5.4": "gpt-5.4",
    "5.3": "gpt-5.3-codex",
    "gpt-5.3": "gpt-5.3-codex",
    "5.3-spark": "gpt-5.3-codex-spark",
    "gpt-5.3-spark": "gpt-5.3-codex-spark",
  },
  [CLAUDE_DRIVER_KIND]: {
    opus: "claude-opus-5-5",
    "opus-5.5": "claude-opus-5-5",
    "claude-opus-5.5": "claude-opus-5-5",
    fable: "claude-fable-5-1",
    "fable-5.1": "claude-fable-5-1",
    "claude-fable-5.1": "claude-fable-5-1",
    "opus-5": "claude-opus-5",
    "claude-opus-5.0": "claude-opus-5",
    "claude-opus-5-0": "claude-opus-5",
    "opus-4.8": "claude-opus-4-8",
    "claude-opus-4.8": "claude-opus-4-8",
    "opus-4.7": "claude-opus-4-7",
    "claude-opus-4.7": "claude-opus-4-7",
    "opus-4.6": "claude-opus-4-6",
    "claude-opus-4.6": "claude-opus-4-6",
    "claude-opus-4-6-20251117": "claude-opus-4-6",
    sonnet: "claude-sonnet-5-5",
    "sonnet-5.5": "claude-sonnet-5-5",
    "claude-sonnet-5.5": "claude-sonnet-5-5",
    "sonnet-5": "claude-sonnet-5",
    "claude-sonnet-5.0": "claude-sonnet-5",
    "claude-sonnet-5-0": "claude-sonnet-5",
    "sonnet-4.6": "claude-sonnet-4-6",
    "claude-sonnet-4.6": "claude-sonnet-4-6",
    "claude-sonnet-4-6-20251117": "claude-sonnet-4-6",
    haiku: "claude-haiku-4-5",
    "haiku-4.5": "claude-haiku-4-5",
    "claude-haiku-4.5": "claude-haiku-4-5",
    "claude-haiku-4-5-20251001": "claude-haiku-4-5",
  },
  [OPENCODE_DRIVER_KIND]: {},
  [OPENCODE_GO_DRIVER_KIND]: {},
};

// ── Provider display names ────────────────────────────────────────────

export const PROVIDER_DISPLAY_NAMES: Partial<Record<ProviderDriverKind, string>> = {
  [CODEX_DRIVER_KIND]: "Codex",
  [CLAUDE_DRIVER_KIND]: "Claude",
  [GROK_DRIVER_KIND]: "Grok",
  [KIMI_DRIVER_KIND]: "Kimi For Coding",
  [OPENCODE_DRIVER_KIND]: "OpenCode",
  [OPENCODE_GO_DRIVER_KIND]: "OpenCode Go",
  [CUSTOM_OPENAI_DRIVER_KIND]: "Custom API",
};

/**
 * The account a user signs in with for each built-in driver. Use in sign-in
 * and setup copy, where "Connect ChatGPT" means more than "Connect Codex".
 */
export const PROVIDER_ACCOUNT_NAMES: Partial<Record<ProviderDriverKind, string>> = {
  [CODEX_DRIVER_KIND]: "ChatGPT",
  [CLAUDE_DRIVER_KIND]: "Claude",
  [GROK_DRIVER_KIND]: "Grok",
  [KIMI_DRIVER_KIND]: "Kimi For Coding",
  [OPENCODE_DRIVER_KIND]: "OpenCode",
  [OPENCODE_GO_DRIVER_KIND]: "OpenCode Go",
};

/**
 * The account name for a built-in instance still using its stock name, so a
 * name the user chose ("Work Codex") wins over "ChatGPT". Undefined otherwise.
 */
export function stockProviderAccountName(
  driver: ProviderDriverKind,
  displayName: string | undefined,
): string | undefined {
  const name = displayName?.trim();

  return !name || name === PROVIDER_DISPLAY_NAMES[driver]
    ? PROVIDER_ACCOUNT_NAMES[driver]
    : undefined;
}