Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

7.7 KB
Raw Blame
import { z } from "zod";

export const MODEL_PRESETS = ["openrouter", "openai", "gemini", "groq", "ollama", "lmstudio", "custom"] as const;
export type ModelPreset = (typeof MODEL_PRESETS)[number];

export const LOCAL_TRANSCRIPTION_MODELS = ["english-fast", "multilingual"] as const;
export type LocalTranscriptionModel = (typeof LOCAL_TRANSCRIPTION_MODELS)[number];

/** Hosted transcription services that take only a key (and optionally a model). */
export const KEYED_TRANSCRIPTION_KINDS = ["deepgram", "assemblyai", "elevenlabs", "gemini", "revai"] as const;

/** Every value `transcription.kind` and `CLIPY_TRANSCRIBE` accept. */
export const TRANSCRIPTION_KINDS = [
  "none",
  "local",
  "openai",
  "groq",
  "openai-compatible",
  ...KEYED_TRANSCRIPTION_KINDS,
] as const;
export type StoredTranscriptionKind = (typeof TRANSCRIPTION_KINDS)[number];

/** The model provider as saved in the file. */
export interface StoredModel {
  /** Which provider. `custom` is any other OpenAI-compatible endpoint. */
  preset: ModelPreset;
  /** Required for openrouter, openai, gemini and groq. */
  apiKey?: string;
  /** Defaults per preset; required for ollama, lmstudio and custom. */
  model?: string;
  /** Required for custom. For a preset, replaces its URL (for example Ollama on another host). */
  baseUrl?: string;
}

/**
 * Transcription as saved in the file. `openai` and `groq` are shorthands for
 * their OpenAI-compatible endpoints and default models.
 */
export type StoredTranscription = {
  /** ISO 639-1 code for the spoken language. Omit to use the engine's default. */
  language?: string;
} & (
  | { kind: "none" }
  | { kind: "local"; model?: LocalTranscriptionModel }
  | { kind: "openai" | "groq"; apiKey?: string; model?: string }
  | { kind: "openai-compatible"; baseUrl: string; apiKey?: string; model: string }
  | { kind: (typeof KEYED_TRANSCRIPTION_KINDS)[number]; apiKey?: string; model?: string }
);

/**
 * The settings file (`config.json`, mode 0600). Every field is optional; a
 * missing file means all defaults. Example:
 *
 * ```json
 * {
 *   "version": 1,
 *   "model": { "preset": "openrouter", "apiKey": "sk-or-..." },
 *   "transcription": { "kind": "local", "model": "english-fast" }
 * }
 * ```
 *
 * - `model`: the provider that reads the transcript for spoken cues. Absent or
 *   null means rules only, and nothing leaves the machine.
 * - `transcription`: how recordings are transcribed. Defaults to the local
 *   "English, fast" model.
 *
 * Unknown top-level keys are kept, so a newer version's settings survive a
 * save from an older one. Inside `model` and `transcription` unknown keys are
 * errors, which catches typos such as `apikey`.
 *
 * Editors can check the file against `config.schema.json`, generated from
 * `storedConfigSchema` with `pnpm --filter @clipy/local-config run schema`.
 * Add a top-level `"$schema"` key with its URL,
 * `https://raw.githubusercontent.com/clipy-recorder/clipy/main/packages/config/config.schema.json`,
 * or a path to a local copy; VS Code and other JSON Schema aware editors then
 * suggest values and flag typos. The key is kept like any other unknown
 * top-level key. The schema describes the file as written: Clipy also trims
 * strings and checks that endpoint URLs use http or https when it reads it.
 */
export interface StoredConfig {
  version?: 1;
  model?: StoredModel | null;
  transcription?: StoredTranscription;
}

const HTTP_URL_MESSAGE = "must be an http:// or https:// URL";

export function isHttpUrl(value: string): boolean {
  try {
    const url = new URL(value);
    return url.protocol === "http:" || url.protocol === "https:";
  } catch {
    return false;
  }
}

const nonEmpty = z.string().trim().min(1, "must not be empty");
const httpUrl = z.string().trim().refine(isHttpUrl, HTTP_URL_MESSAGE).describe("An HTTP or HTTPS endpoint URL.");

// Assigned alpha-2 codes from https://www.loc.gov/standards/iso639-2/php/code_list.php
const ISO_639_1 = new Set(`aa ab ae af ak am an ar as av ay az ba be bg bi bm bn bo br bs ca ce ch co cr cs cu cv cy
da de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gn gu gv ha he hi ho hr ht hu hy hz ia id ie ig ii ik
io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky la lb lg li ln lo lt lu lv mg mh mi mk ml mn mr ms mt
my na nb nd ne ng nl nn no nr nv ny oc oj om or os pa pi pl ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sm sn so
sq sr ss st su sv sw ta te tg th ti tk tl tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo za zh zu`.split(/\s+/));

export const transcriptionLanguageSchema = z.string().trim()
  .refine((s) => /^[a-z]{2}$/i.test(s) && ISO_639_1.has(s.toLowerCase()), 'must be an ISO 639-1 language code such as "en" or "es"')
  .toLowerCase().optional();

const storedModelSchema = z.strictObject({
  preset: z.enum(MODEL_PRESETS),
  apiKey: nonEmpty.optional(),
  model: nonEmpty.optional(),
  baseUrl: httpUrl.optional(),
});

const storedTranscriptionSchema = z.discriminatedUnion("kind", [
  z.strictObject({ kind: z.literal("none"), language: transcriptionLanguageSchema }),
  z.strictObject({ kind: z.literal("local"), model: z.enum(LOCAL_TRANSCRIPTION_MODELS).optional(), language: transcriptionLanguageSchema }),
  z.strictObject({ kind: z.enum(["openai", "groq"]), apiKey: nonEmpty.optional(), model: nonEmpty.optional(), language: transcriptionLanguageSchema }),
  z.strictObject({ kind: z.literal("openai-compatible"), baseUrl: httpUrl, apiKey: nonEmpty.optional(), model: nonEmpty, language: transcriptionLanguageSchema }),
  z.strictObject({ kind: z.enum(KEYED_TRANSCRIPTION_KINDS), apiKey: nonEmpty.optional(), model: nonEmpty.optional(), language: transcriptionLanguageSchema }),
]);

export const storedConfigSchema = z.looseObject({
  version: z.literal(1).optional(),
  model: storedModelSchema.nullable().optional().describe("Model provider for spoken cues. Omit or set to null to use rules only."),
  transcription: storedTranscriptionSchema.optional().describe("How recordings are transcribed. Defaults to the english-fast local model."),
}).meta({
  $id: "https://raw.githubusercontent.com/clipy-recorder/clipy/main/packages/config/config.schema.json",
  title: "Clipy Local settings",
  description: "Model provider and transcription settings for Clipy Local recordings.",
});

/** Describe config.json before Clipy trims strings, checks URLs and resolves defaults. */
export function configSchemaJson(): string {
  const schema = z.toJSONSchema(storedConfigSchema, {
    target: "draft-2020-12",
    io: "input",
    unrepresentable: "any",
  });
  return `${JSON.stringify(schema, null, 2)}\n`;
}
/** How many problems `describeIssues` spells out before summarising the rest. */
const MAX_LISTED_ISSUES = 5;

/**
 * One line per problem, naming the field. Zod issues here never carry the
 * input value, so a key in the file cannot end up in the message. Past
 * `MAX_LISTED_ISSUES` it says how many more there are, so fixing the listed
 * ones is not mistaken for fixing the file.
 */
export function describeIssues(error: z.ZodError): string {
  const hidden = error.issues.length - MAX_LISTED_ISSUES;
  const listed = error.issues
    .slice(0, MAX_LISTED_ISSUES)
    .map((issue) => {
      const path = issue.path.map(String);
      let message = issue.message;
      if (issue.code === "invalid_union" && "discriminator" in issue && typeof issue.discriminator === "string") {
        if (path[path.length - 1] !== issue.discriminator) path.push(issue.discriminator);
        message = `must be one of ${TRANSCRIPTION_KINDS.join(", ")}`;
      }
      const field = path.length ? `"${path.join(".")}"` : "the file";
      return path.length ? `${field}: ${message}` : `${field} must contain a JSON object`;
    })
    .join("; ");
  if (hidden <= 0) return listed;
  return `${listed}; and ${hidden} more ${hidden === 1 ? "problem" : "problems"}`;
}