Skip to content

The platform for continuously improving AI agents.

8.0 KB
Raw Blame
import * as z from "zod";

import { EvalRunsActivityListStatusEnum, FinetuningJobsListStatusEnum } from "@/openapi";

export const projectIdSearchSchema = z.object({
  // Reopens the create-project modal (`createProject=new`).
  createProject: z.enum(["new"]).optional(),
  experiment: z.string().uuid().optional(),
  message: z.string().optional(),
  projectId: z.string().optional(),
  workspace: z.string().uuid().optional(),
  workspaceTab: z.string().optional(),
});

/**
 * Named to match django-filter, so values stay strings and pass through to the
 * backend untouched. Every param must be declared even if only the client reads
 * it, or the router treats it as unknown and drops it.
 */
export const tracesSearchSchema = z.object({
  application_name__icontains: z.string().optional(),
  capability: z.string().optional(),
  detailExpanded: z.coerce.boolean().optional().default(false),
  error__icontains: z.string().optional(),
  /**
   * Inverted against its DRF-style name: "true" selects errored traces. The
   * route maps it to the API's `has_error`, which is the real lookup.
   */
  error__isnull: z.enum(["true", "false"]).optional(),
  /** "true" → only traces where some span reported a model (i.e. made an LLM call). */
  has_model: z.enum(["true", "false"]).optional(),
  iteration: z.string().optional(),
  job: z.string().optional(),
  max_duration_ms: z.string().optional(),
  min_duration_ms: z.string().optional(),
  /** Filter to traces whose LLM spans invoked this model id. */
  model: z.string().optional(),
  operation: z.string().optional(),
  operation__icontains: z.string().optional(),
  ordering: z.string().optional().default("-start_time_ns"),
  page: z.coerce.number().min(1).optional().default(1),
  page_size: z.coerce.number().min(5).max(100).optional().default(25),
  projectId: z.string().optional(),

  received_at__gte: z.string().optional(),
  received_at__lte: z.string().optional(),

  score__gte: z.string().optional(),
  score__lte: z.string().optional(),
  search: z.string().optional(),
  service_name: z.string().optional(),
  service_name__icontains: z.string().optional(),
  /** A Conversation UUID, not a span or trace id. */
  session: z.string().optional(),
  signal: z.string().optional(),
  signal_event: z.string().uuid().optional().catch(undefined),
  source: z.string().optional(),

  source__in: z.string().optional(),
  span_type: z.string().optional(),
  status_code: z.string().optional(),

  /** Drives `received_at__gte`/`__lte` unless set to "custom". */
  timeRange: z
    .enum(["all", "past15m", "past1h", "past24h", "past7d", "past30d", "custom"])
    .optional()
    .default("all"),
  timestamp: z.string().optional(),
  total_cost__gte: z.string().optional(),
  total_cost__lte: z.string().optional(),
  total_latency_ms__gte: z.string().optional(),
  total_latency_ms__lte: z.string().optional(),
  total_tokens__gte: z.string().optional(),
  total_tokens__lte: z.string().optional(),
  trace_group: z.string().optional(),
  trace_group__icontains: z.string().optional(),
  trace_id: z.string().optional(),
  unseen: z.boolean().optional().catch(undefined),
  view: z.enum(["roots", "signals"]).optional().catch("roots").default("roots"),
});

export const evaluationsSearchSchema = projectIdSearchSchema.extend({
  page: z.coerce.number().min(1).optional().default(1),
  page_size: z.coerce.number().min(5).max(100).optional().default(25),
  // "all" means no filter.
  run_capability: z.string().optional().default("all"),
  run_dataset: z.string().optional().default("all"),
  run_search: z.string().optional().default(""),
  run_status: z
    .enum(["all", ...Object.values(EvalRunsActivityListStatusEnum)])
    .optional()
    .default("all")
    .catch("all"),
  // `matrix` was the online tab's earlier name; old links still land on it.
  view: z
    .preprocess((v) => (v === "matrix" ? "online" : v), z.enum(["runs", "sets", "online"]))
    .optional()
    .default("runs")
    .catch("runs"),
});

export type EvaluationsSearch = z.infer<typeof evaluationsSearchSchema>;

// `ft_*` rather than the shared `run_*`: `ft_status` is a closed set, `run_status` is
// free-form, and a `search: (prev) => …` navigation carries every route's params.
export const trainingSearchSchema = projectIdSearchSchema.extend({
  capabilityId: z.string().optional(),
  /** `datasetId` preselects the train dataset; `evalDatasetId` the eval one. */
  datasetId: z.string().optional(),
  evalDatasetId: z.string().optional(),
  // Free-form ids: a dataset uuid and a base-model slug.
  ft_dataset: z.string().optional().default("all"),
  ft_model: z.string().optional().default("all"),
  ft_search: z.string().optional().default(""),
  // Forwarded to the API, so it must be one the select offers. `.catch` keeps a
  // hand-edited value from throwing a SearchParamError that would discard the
  // rest of the search — including the open run.
  ft_status: z
    .enum(["all", ...Object.values(FinetuningJobsListStatusEnum)])
    .optional()
    .default("all")
    .catch("all"),
  groupId: z.string().optional(),
  job: z.string().optional(),
  jobId: z.string().optional(),
  page: z.coerce.number().min(1).optional().default(1),
  page_size: z.coerce.number().min(5).max(100).optional().default(25),
  train: z.boolean().optional(),
});

export type TrainingSearch = z.infer<typeof trainingSearchSchema>;

/**
 * Matching is client-side (`filterDatasets`), so nothing is forwarded to the
 * API — but the closed sets still take `.catch` so a stale link falls back to
 * unfiltered instead of throwing a SearchParamError that would discard the rest
 * of the search, `projectId` included.
 */
export const datasetsSearchSchema = projectIdSearchSchema.extend({
  /** Opens the create-dataset dialog on land. The palette sends it as a string. */
  create: z
    .union([z.boolean(), z.enum(["true", "false"]).transform((v) => v === "true")])
    .optional(),
  /** A capability id, or the `NO_CAPABILITY` sentinel for datasets attached to none. */
  ds_capability: z.string().optional().default("all"),
  ds_role: z
    // Links saved before roles were intents still filter by the same use.
    .preprocess(
      (value) => ({ evaluation: "eval", training: "train" })[value as string] ?? value,
      z.enum(["all", "sft", "decision", "train", "eval", "pending"])
    )
    .optional()
    .default("all")
    .catch("all"),
  ds_search: z.string().optional().default(""),
});

// `inf_capability` is prefixed so it can't collide with the bare `capability` other routes
// carry through `search: (prev) => …`. The model detail route must share this schema:
// zod strips undeclared keys, and the detail page rebuilds the list URL from them.
export const inferenceSearchSchema = projectIdSearchSchema.extend({
  /** A capability id, the `NO_CAPABILITY` sentinel for deployments attached to none, or "all". */
  inf_capability: z.string().optional().default("all"),
  inf_deployment: z.enum(["all", "serverless", "dedicated"]).optional().default("all"),
  inf_ordering: z
    .enum([
      "-created_at",
      "recent_requests",
      "-recent_requests",
      "recent_success_rate",
      "-recent_success_rate",
      "recent_latency",
      "-recent_latency",
      "recent_speed",
      "-recent_speed",
    ])
    .optional()
    .default("-created_at"),
  /** DRF `?search=` — matches the model id, its base model, or the job's name. */
  inf_search: z.string().optional().default(""),
  page: z.coerce.number().min(1).optional().default(1),
  page_size: z.coerce.number().min(5).max(100).optional().default(25),
});

export type InferenceSearch = z.infer<typeof inferenceSearchSchema>;

export const projectsSearchSchema = z.object({
  createProject: z.enum(["new"]).optional(),
  message: z.string().optional(),
  sortBy: z
    .enum(["name", "organisationName", "memberCount", "createdAt"])
    .optional()
    .default("createdAt"),
  sortDirection: z.enum(["asc", "desc"]).optional().default("desc"),
});

export type ProjectsSearch = z.infer<typeof projectsSearchSchema>;

export const evaluationRunSearchSchema = projectIdSearchSchema.extend({
  format: z.enum(["generative", "decision"]).optional(),
});