All files / api/src/connectors/libsql logs.ts

90.9% Statements 90/99
68.75% Branches 11/16
100% Functions 4/4
90.9% Lines 90/99

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    1x         1x 1x 1x 1x 1x             1x 1x 5x 5x 5x 5x 5x   5x 55x 1x 1x 1x 55x   5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 1x 1x 5x 1x 1x 1x 5x 1x 1x 1x   5x 5x 3x 3x 5x   5x       5x               5x 5x 5x   1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x   1x 1x 1x 1x 1x 1x 1x  
import type { LogsStorageConnector } from '@api/types/connector';
import type { AppContext } from '@api/types/hono';
import {
  Log,
  type LogCreateParams,
  type LogsQueryParams,
} from '@shared/types/data/log';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
import { getLibsqlClient } from './client';
import { insertInto, parseRows } from './query';
import { asColumns, toJsonColumn } from './rows';
 
/**
 * Reads go through `logs_with_eval_scores`, which carries the computed
 * `avg_eval_score` and `eval_run_count`, exactly as the Supabase connector
 * does. Writes go to the `logs` table itself.
 */
export const libsqlLogsStorageConnector: LogsStorageConnector = {
  getLogs: async (
    c: AppContext,
    queryParams: LogsQueryParams,
  ): Promise<Log[]> => {
    const conditions: string[] = [];
    const args: (string | number)[] = [];
 
    const eq = (column: string, value: string | number | undefined) => {
      if (value !== undefined) {
        conditions.push(`${column} = ?`);
        args.push(value);
      }
    };
 
    eq('agent_id', queryParams.agent_id);
    eq('skill_id', queryParams.skill_id);
    eq('cluster_id', queryParams.cluster_id);
    eq('arm_id', queryParams.arm_id);
    eq('app_id', queryParams.app_id);
    eq('id', queryParams.id);
    eq('method', queryParams.method);
    eq('endpoint', queryParams.endpoint);
    eq('function_name', queryParams.function_name);
    eq('status', queryParams.status);
    eq('cache_status', queryParams.cache_status);
 
    if (queryParams.embedding_not_null) {
      conditions.push('embedding IS NOT NULL');
    }
    if (queryParams.after !== undefined) {
      conditions.push('start_time >= ?');
      args.push(queryParams.after);
    }
    if (queryParams.before !== undefined) {
      conditions.push('start_time <= ?');
      args.push(queryParams.before);
    }
 
    let sql = 'SELECT * FROM logs_with_eval_scores';
    if (conditions.length > 0) {
      sql += ` WHERE ${conditions.join(' AND ')}`;
    }
    sql += ' ORDER BY start_time DESC';
 
    if (queryParams.limit !== undefined) {
      sql += ' LIMIT ?';
      args.push(queryParams.limit);
    }
    if (queryParams.offset !== undefined) {
      if (queryParams.limit === undefined) {
        sql += ' LIMIT -1';
      }
      sql += ' OFFSET ?';
      args.push(queryParams.offset);
    }
 
    const result = await getLibsqlClient(c).execute({ sql, args });
    return parseRows('logs_with_eval_scores', result.rows, z.array(Log));
  },
 
  createLog: async (
    c: AppContext,
    createParams: LogCreateParams,
  ): Promise<Log> => {
    const {
      base_sa_config,
      ai_provider_request_log,
      hook_logs,
      metadata,
      embedding,
      user_metadata,
      ...rest
    } = createParams as LogCreateParams & Record<string, unknown>;
 
    const rows = await insertInto(
      getLibsqlClient(c),
      'logs',
      {
        ...asColumns(rest),
        id: uuidv4(),
        base_sa_config: toJsonColumn(base_sa_config),
        ai_provider_request_log: toJsonColumn(ai_provider_request_log),
        hook_logs: toJsonColumn(hook_logs ?? []),
        metadata: toJsonColumn(metadata ?? {}),
        embedding:
          embedding === undefined ? undefined : toJsonColumn(embedding),
        user_metadata:
          user_metadata === undefined ? undefined : toJsonColumn(user_metadata),
      },
      z.array(Log),
    );
 
    return rows[0];
  },
 
  deleteLog: async (c: AppContext, id: string): Promise<void> => {
    await getLibsqlClient(c).execute({
      sql: 'DELETE FROM logs WHERE id = ?',
      args: [id],
    });
  },
};