All files / src/storage schema.ts

100% Statements 130/130
100% Branches 2/2
100% Functions 1/1
100% Lines 130/130

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 75x 675x 675x 75x 75x 75x 75x 75x 75x 75x 75x  
/**
 * SQLite Schema Definition
 *
 * Defines the database schema for RelayPlane storage.
 *
 * @packageDocumentation
 */
 
/**
 * SQL statements for creating the database schema.
 */
export const SCHEMA_SQL = `
-- Runs table: stores all LLM invocations
CREATE TABLE IF NOT EXISTS runs (
  id TEXT PRIMARY KEY,
  prompt TEXT NOT NULL,
  system_prompt TEXT,
  task_type TEXT NOT NULL,
  model TEXT NOT NULL,
  success INTEGER NOT NULL,
  output TEXT,
  error TEXT,
  duration_ms INTEGER NOT NULL,
  tokens_in INTEGER,
  tokens_out INTEGER,
  metadata TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now'))
);
 
-- Index for task type queries
CREATE INDEX IF NOT EXISTS idx_runs_task_type ON runs(task_type);
 
-- Index for model queries
CREATE INDEX IF NOT EXISTS idx_runs_model ON runs(model);
 
-- Index for time-based queries
CREATE INDEX IF NOT EXISTS idx_runs_created_at ON runs(created_at);
 
-- Outcomes table: stores user feedback on runs
CREATE TABLE IF NOT EXISTS outcomes (
  id TEXT PRIMARY KEY,
  run_id TEXT NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
  success INTEGER NOT NULL,
  quality TEXT,
  latency_satisfactory INTEGER,
  cost_satisfactory INTEGER,
  feedback TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  UNIQUE(run_id)
);
 
-- Index for run lookups
CREATE INDEX IF NOT EXISTS idx_outcomes_run_id ON outcomes(run_id);
 
-- Routing rules table: stores routing preferences
CREATE TABLE IF NOT EXISTS routing_rules (
  id TEXT PRIMARY KEY,
  task_type TEXT NOT NULL UNIQUE,
  preferred_model TEXT NOT NULL,
  source TEXT NOT NULL DEFAULT 'default',
  confidence REAL,
  sample_count INTEGER,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);
 
-- Index for task type lookups
CREATE INDEX IF NOT EXISTS idx_routing_rules_task_type ON routing_rules(task_type);
 
-- Suggestions table: stores routing improvement suggestions
CREATE TABLE IF NOT EXISTS suggestions (
  id TEXT PRIMARY KEY,
  task_type TEXT NOT NULL,
  current_model TEXT NOT NULL,
  suggested_model TEXT NOT NULL,
  reason TEXT NOT NULL,
  confidence REAL NOT NULL,
  expected_improvement TEXT NOT NULL,
  sample_count INTEGER NOT NULL,
  accepted INTEGER,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  accepted_at TEXT
);
 
-- Index for task type lookups
CREATE INDEX IF NOT EXISTS idx_suggestions_task_type ON suggestions(task_type);
 
-- Index for pending suggestions
CREATE INDEX IF NOT EXISTS idx_suggestions_accepted ON suggestions(accepted);
 
-- Schema version table for migrations
CREATE TABLE IF NOT EXISTS schema_version (
  version INTEGER PRIMARY KEY,
  applied_at TEXT NOT NULL DEFAULT (datetime('now'))
);
 
-- Insert initial schema version
INSERT OR IGNORE INTO schema_version (version) VALUES (1);
`;
 
/**
 * Default routing rules to seed the database.
 */
export const DEFAULT_ROUTING_RULES = [
  { taskType: 'code_generation', preferredModel: 'anthropic:claude-3-5-sonnet-20241022' },
  { taskType: 'code_review', preferredModel: 'anthropic:claude-3-5-sonnet-20241022' },
  { taskType: 'summarization', preferredModel: 'anthropic:claude-3-haiku-20240307' },
  { taskType: 'analysis', preferredModel: 'openai:gpt-4o' },
  { taskType: 'creative_writing', preferredModel: 'anthropic:claude-3-opus-20240229' },
  { taskType: 'data_extraction', preferredModel: 'openai:gpt-4o-mini' },
  { taskType: 'translation', preferredModel: 'openai:gpt-4o-mini' },
  { taskType: 'question_answering', preferredModel: 'anthropic:claude-3-haiku-20240307' },
  { taskType: 'general', preferredModel: 'anthropic:claude-3-5-sonnet-20241022' },
] as const;
 
/**
 * SQL for seeding default routing rules.
 */
export function generateSeedSQL(): string {
  const values = DEFAULT_ROUTING_RULES.map((rule, index) => {
    const id = `default-${rule.taskType}`;
    return `('${id}', '${rule.taskType}', '${rule.preferredModel}', 'default', NULL, NULL, datetime('now'), datetime('now'))`;
  }).join(',\n  ');
 
  return `
INSERT OR IGNORE INTO routing_rules (id, task_type, preferred_model, source, confidence, sample_count, created_at, updated_at)
VALUES
  ${values};
`;
}