mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-25 08:48:08 +02:00
The MCP sql_execution/sl_query tools and the `ktx sql` CLI threw a plain Error naming no valid connection ids when an agent passed an unconfigured connectionId (or omitted it with multiple connections). The message reached the agent verbatim but gave it nothing to correct with, so it re-guessed for days, and each correct caller-driven rejection filed in PostHog Error Tracking as a ktx fault (issue 019eb10c, 8 occurrences on one install). Add a shared resolver (resolveConfiguredConnection / resolveRequiredConnectionId) that throws KtxExpectedError listing the configured connections, and route the three SQL-execution call sites through it. Expected-error classification keeps these out of Error Tracking while the actionable message lets agents self-correct.
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import { KtxExpectedError } from '../../errors.js';
|
|
import type { KtxProjectConfig, KtxProjectConnectionConfig } from '../project/config.js';
|
|
|
|
function configuredConnectionIds(config: KtxProjectConfig): string[] {
|
|
return Object.keys(config.connections).sort();
|
|
}
|
|
|
|
function availableConnectionsHint(config: KtxProjectConfig): string {
|
|
const ids = configuredConnectionIds(config);
|
|
return ids.length === 0
|
|
? 'No connections are configured in ktx.yaml.'
|
|
: `Configured connections: ${ids.join(', ')}.`;
|
|
}
|
|
|
|
/**
|
|
* Look up a connection by id, throwing an expected (caller-driven) error that
|
|
* names the configured connections so an agent or CLI user can self-correct.
|
|
*/
|
|
export function resolveConfiguredConnection(
|
|
config: KtxProjectConfig,
|
|
connectionId: string,
|
|
): KtxProjectConnectionConfig {
|
|
const connection = config.connections[connectionId];
|
|
if (!connection) {
|
|
throw new KtxExpectedError(
|
|
`Connection "${connectionId}" is not configured in ktx.yaml. ${availableConnectionsHint(config)}`,
|
|
);
|
|
}
|
|
return connection;
|
|
}
|
|
|
|
/**
|
|
* Resolve the connection id to run against: validate a requested id against the
|
|
* configured connections, or default to the sole connection when none is given.
|
|
* Throws an expected error that lists the configured connections otherwise.
|
|
*/
|
|
export function resolveRequiredConnectionId(
|
|
config: KtxProjectConfig,
|
|
requested: string | undefined,
|
|
): string {
|
|
if (requested !== undefined) {
|
|
resolveConfiguredConnection(config, requested);
|
|
return requested;
|
|
}
|
|
const ids = configuredConnectionIds(config);
|
|
if (ids.length === 1) {
|
|
return ids[0];
|
|
}
|
|
throw new KtxExpectedError(`connectionId is required. ${availableConnectionsHint(config)}`);
|
|
}
|