LCOV - code coverage report
Current view: top level - dashql-app/src/agent - agent_prompt.ts Coverage Total Hit
Test: DashQL 6e74948 Lines: 90.0 % 30 27
Test Date: 2026-11-06 17:46:50

            Line data    Source code
       1              : import { ConnectorType } from '../connections/connector_info.js';
       2              : import { renderScriptReference } from './script_references.js';
       3              : 
       4              : /// Describe the SQL dialect of a connector for the agent
       5            1 : export function describeSqlDialect(connectorType: ConnectorType): string {
       6            1 :     switch (connectorType) {
       7            1 :         case ConnectorType.HYPER:
       8            1 :             return 'Tableau Hyper (PostgreSQL-compatible)';
       9            0 :         case ConnectorType.SALESFORCE_DATA_CLOUD:
      10            0 :         case ConnectorType.SIDB:
      11            0 :             return 'Salesforce Data Cloud SQL (PostgreSQL-compatible)';
      12            1 :         case ConnectorType.TRINO:
      13            1 :             return 'Trino';
      14            1 :     }
      15            1 : }
      16              : 
      17              : /// A question that the agent answers in the scripts of a session
      18              : export interface AgentPromptInput {
      19              :     /// The question in natural language
      20              :     question: string;
      21              :     /// The SQL dialect of the connection
      22              :     dialect: string;
      23              :     /// The catalog objects that the question mentions, e.g. "orders (table)"
      24              :     mentionedIdentifiers: string[];
      25              :     /// The scripts that the question references, by their display names
      26              :     referencedScripts: string[];
      27              : }
      28              : 
      29              : /// Build the prompt of a question for the agent.
      30              : /// The agent reads the schema and writes the SQL itself through the dashql command, it also picks the script.
      31            1 : export function buildAgentPrompt(input: AgentPromptInput): string {
      32            1 :     const lines = [
      33            1 :         'Answer the question below with a SQL query in a DashQL script.',
      34            1 :         `The connection speaks ${input.dialect}.`,
      35            1 :         'Look up the scripts with `dashql agent scripts` and the schema with `dashql agent catalog`.',
      36            1 :         'Write the query with `dashql agent edit <script>`, into a new script with a short snake_case name'
      37              :             + ' unless the question is about an existing script.',
      38            1 :         'Check it with `dashql agent lint <script>` and run it with `dashql agent run <script>`.',
      39            1 :         'Prefer read-only queries.',
      40            1 :         'The user watches the scripts change and run, do not recap your steps, the queries or the results.',
      41            1 :         'End with one plain sentence without markdown that answers the question, or with nothing if the results'
      42              :             + ' already answer it.',
      43            1 :     ];
      44            1 :     if (input.referencedScripts.length > 0) {
      45            1 :         lines.push(`The question targets these scripts, referenced as ${renderScriptReference('<script>')}: ${input.referencedScripts.join(', ')}.`
      46            1 :             + ' Work on them instead of writing a new script.');
      47              :     }
      48            1 :     if (input.mentionedIdentifiers.length > 0) {
      49            1 :         lines.push(`The question mentions these catalog objects: ${input.mentionedIdentifiers.join(', ')}.`);
      50              :     }
      51              :     // Harnesses reject NUL characters
      52            1 :     lines.push('', 'Question:', input.question.replace(/\0/g, ''));
      53            1 :     return lines.join('\n');
      54            1 : }
        

Generated by: LCOV version 2.0-1