Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript
2.4 KB
Raw Blame
import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
import { listDecisionModels, runDecision, setDecisionModelEnabled, DecisionError } from '../services/decision.js';

// Dashboard: the Models → Decision page. Models come from the catalog's
// decisionModels registry; this lists them with key and usage counts, lets the
// operator switch one off, and runs a test decision from the page.
export const decisionsRouter = Router();

decisionsRouter.get('/models', (_req: Request, res: Response) => {
  res.json(listDecisionModels());
});

const patchSchema = z.object({ enabled: z.boolean() });

decisionsRouter.patch('/models/:id', (req: Request, res: Response) => {
  const id = Number(req.params.id);
  if (!Number.isInteger(id) || id <= 0) {
    res.status(400).json({ error: { message: 'Invalid model id' } });
    return;
  }
  const parsed = patchSchema.safeParse(req.body);
  if (!parsed.success) {
    res.status(400).json({ error: { message: 'Provide enabled' } });
    return;
  }
  if (!setDecisionModelEnabled(id, parsed.data.enabled)) {
    res.status(404).json({ error: { message: 'Decision model not found' } });
    return;
  }
  res.json(listDecisionModels().find(m => m.id === id));
});

// "Try it" on the page: one choice question over the text the user typed.
const trySchema = z.object({
  model: z.string().min(1).optional(),
  state: z.string().min(1).max(20_000),
  question: z.string().min(1).max(2_000),
  options: z.array(z.string().min(1).max(200)).min(2).max(20),
});

decisionsRouter.post('/try', async (req: Request, res: Response) => {
  const parsed = trySchema.safeParse(req.body);
  if (!parsed.success) {
    res.status(400).json({ error: { message: 'Provide text, a question and at least two options' } });
    return;
  }
  const { model, state, question, options } = parsed.data;
  try {
    const result = await runDecision(
      {
        state,
        questions: {
          answer: { type: 'choice', instructions: question, criteria: Object.fromEntries(options.map(o => [o, null])) },
        },
      },
      { model, clientAgent: 'dashboard' },
    );
    res.json(result);
  } catch (err: any) {
    const status = err instanceof DecisionError ? err.status : 502;
    res.status(status).json({ error: { message: err?.message ?? 'Decision failed', ...(err?.code ? { code: err.code } : {}) } });
  }
});