feat(ai): default and last-used templates per db type
This commit is contained in:
1 parent
d92e119464
commit
91f309f6c7
20 files changed
+647
-12
No files matched your search
@@ -0,0 +1,88 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { capTemplateIdsToCharLimit, resolveAutoTemplateIds, resolveDefaultTemplateIds } from "@/lib/ai/promptTemplateDefaults";
|
||||
import type { PromptTemplate } from "@/types/promptTemplate";
|
||||
|
||||
function template(id: string, content: string): PromptTemplate {
|
||||
return { id, name: id, content, createdAt: "2026-01-01", updatedAt: "2026-01-01" };
|
||||
}
|
||||
|
||||
describe("resolveDefaultTemplateIds", () => {
|
||||
it("returns the configured defaults for the db_type", () => {
|
||||
expect(resolveDefaultTemplateIds("postgresql", { postgresql: ["tpl-pg"] })).toEqual(["tpl-pg"]);
|
||||
});
|
||||
|
||||
it("returns empty when no defaults are configured", () => {
|
||||
// Namespace-switch contract: with no defaults the switch clears the
|
||||
// selection; last-used is unreachable from this resolution by design.
|
||||
expect(resolveDefaultTemplateIds("postgresql", {})).toEqual([]);
|
||||
expect(resolveDefaultTemplateIds("postgresql", { mysql: ["tpl-my"] })).toEqual([]);
|
||||
});
|
||||
|
||||
it("resolves nothing without a connection db_type", () => {
|
||||
expect(resolveDefaultTemplateIds(undefined, { mysql: ["tpl"] })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveAutoTemplateIds", () => {
|
||||
it("returns explicit defaults for the connection db_type", () => {
|
||||
expect(
|
||||
resolveAutoTemplateIds({
|
||||
dbType: "postgresql",
|
||||
defaultTemplatesByDbType: { postgresql: ["tpl-pg"] },
|
||||
lastUsedTemplatesByDbType: { postgresql: ["tpl-last"] },
|
||||
}),
|
||||
).toEqual(["tpl-pg"]);
|
||||
});
|
||||
|
||||
it("falls back to the db_type's last-used templates only when no defaults are configured", () => {
|
||||
expect(
|
||||
resolveAutoTemplateIds({
|
||||
dbType: "mysql",
|
||||
defaultTemplatesByDbType: { postgresql: ["tpl-pg"] },
|
||||
lastUsedTemplatesByDbType: { mysql: ["tpl-last"] },
|
||||
}),
|
||||
).toEqual(["tpl-last"]);
|
||||
});
|
||||
|
||||
it("never leaks another db_type's defaults or last-used list", () => {
|
||||
expect(
|
||||
resolveAutoTemplateIds({
|
||||
dbType: "mysql",
|
||||
defaultTemplatesByDbType: { postgresql: ["tpl-pg"] },
|
||||
lastUsedTemplatesByDbType: { postgresql: ["tpl-last"] },
|
||||
}),
|
||||
).toEqual([]);
|
||||
});
|
||||
|
||||
it("treats an empty defaults list as unconfigured and still applies last-used", () => {
|
||||
expect(
|
||||
resolveAutoTemplateIds({
|
||||
dbType: "mysql",
|
||||
defaultTemplatesByDbType: { mysql: [] },
|
||||
lastUsedTemplatesByDbType: { mysql: ["tpl-last"] },
|
||||
}),
|
||||
).toEqual(["tpl-last"]);
|
||||
});
|
||||
|
||||
it("resolves nothing without a connection db_type", () => {
|
||||
expect(resolveAutoTemplateIds({ defaultTemplatesByDbType: { mysql: ["tpl"] }, lastUsedTemplatesByDbType: {} })).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("capTemplateIdsToCharLimit", () => {
|
||||
it("drops ids whose templates no longer exist", () => {
|
||||
const templates = [template("kept", "abc")];
|
||||
expect(capTemplateIdsToCharLimit(["missing", "kept"], templates, 100)).toEqual(["kept"]);
|
||||
});
|
||||
|
||||
it("skips a template that alone exceeds the remaining budget but keeps later smaller ones", () => {
|
||||
const templates = [template("small-a", "abc"), template("huge", "x".repeat(10)), template("small-b", "de")];
|
||||
expect(capTemplateIdsToCharLimit(["small-a", "huge", "small-b"], templates, 8)).toEqual(["small-a", "small-b"]);
|
||||
});
|
||||
|
||||
it("deduplicates repeated ids without double counting their content", () => {
|
||||
const templates = [template("a", "abc")];
|
||||
expect(capTemplateIdsToCharLimit(["a", "a"], templates, 5)).toEqual(["a"]);
|
||||
});
|
||||
});
|
||||
@@ -716,7 +716,7 @@ async function loadCandidateSchemas(tab: QueryTab, connection: ConnectionConfig)
|
||||
return [database];
|
||||
}
|
||||
|
||||
function aiDatabaseTypeForConnection(connection: ConnectionConfig): DatabaseType {
|
||||
export function aiDatabaseTypeForConnection(connection: ConnectionConfig): DatabaseType {
|
||||
return effectiveDatabaseTypeForConnection(connection) ?? connection.db_type;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { PromptTemplate } from "@/types/promptTemplate";
|
||||
import { promptTemplateCharacterCount } from "@/types/promptTemplate";
|
||||
|
||||
export interface AutoTemplateResolutionInput {
|
||||
dbType?: string;
|
||||
defaultTemplatesByDbType: Record<string, string[]>;
|
||||
lastUsedTemplatesByDbType: Record<string, string[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template ids explicitly configured as defaults for the db_type, or empty
|
||||
* when none are configured. Namespace-switch resolution must stop here:
|
||||
* falling back to last-used would resurrect selections the user just cleared
|
||||
* on every connection/database/schema change.
|
||||
*/
|
||||
export function resolveDefaultTemplateIds(dbType: string | undefined, defaultTemplatesByDbType: Record<string, string[]>): string[] {
|
||||
if (!dbType) return [];
|
||||
const defaults = defaultTemplatesByDbType[dbType];
|
||||
return defaults && defaults.length > 0 ? [...defaults] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Template ids to auto-select when a panel opens, keyed by the connection's
|
||||
* db_type: explicit defaults win over the last-used fallback. Both records are
|
||||
* scoped per db_type so a template tuned for one database never leaks into a
|
||||
* namespace of another database type.
|
||||
*/
|
||||
export function resolveAutoTemplateIds({ dbType, defaultTemplatesByDbType, lastUsedTemplatesByDbType }: AutoTemplateResolutionInput): string[] {
|
||||
const defaults = resolveDefaultTemplateIds(dbType, defaultTemplatesByDbType);
|
||||
if (defaults.length > 0) return defaults;
|
||||
if (!dbType) return [];
|
||||
const lastUsed = lastUsedTemplatesByDbType[dbType];
|
||||
return lastUsed && lastUsed.length > 0 ? [...lastUsed] : [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep only ids that still exist among templates, deduplicated, dropping any
|
||||
* single template whose content would push the combined total past maxTotal
|
||||
* (same budget the manual multi-select enforces). A later smaller template is
|
||||
* still applied so one oversized default cannot shadow the remaining defaults.
|
||||
*/
|
||||
export function capTemplateIdsToCharLimit(ids: string[], templates: PromptTemplate[], maxTotal: number): string[] {
|
||||
const byId = new Map(templates.map((template) => [template.id, template]));
|
||||
const kept: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
let total = 0;
|
||||
for (const id of ids) {
|
||||
if (seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
const template = byId.get(id);
|
||||
if (!template) continue;
|
||||
const size = promptTemplateCharacterCount(template.content);
|
||||
if (total + size > maxTotal) continue;
|
||||
total += size;
|
||||
kept.push(id);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
Reference in new issue
Block a user