Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: request ai (#18800)

ecmadao968ba0c490c5
diff --git a/frontend/src/plugins/ai/components/DynamicSuggestions.vue b/frontend/src/plugins/ai/components/DynamicSuggestions.vue
index a46d46d8d1..3c3aad8dfa 100644
--- a/frontend/src/plugins/ai/components/DynamicSuggestions.vue
+++ b/frontend/src/plugins/ai/components/DynamicSuggestions.vue
@@ -53,7 +53,7 @@
<script lang="ts" setup>
import { RefreshCwIcon, XIcon } from "lucide-vue-next";
import { NButton } from "naive-ui";
-import { computed } from "vue";
+import { computed, onMounted } from "vue";
import { BBSpin } from "@/bbkit";
import { useCurrentUserV1 } from "@/store";
import { useDynamicLocalStorage } from "@/utils";
@@ -66,6 +66,13 @@ const emit = defineEmits<{
const dynamicSuggestions = useDynamicSuggestions();
const currentUser = useCurrentUserV1();
+onMounted(() => {
+ const suggestion = dynamicSuggestions.value;
+ if (suggestion && suggestion.suggestions.length === 0) {
+ suggestion.fetch();
+ }
+});
+
const ready = computed(() => dynamicSuggestions.value?.ready ?? false);
const state = computed(() => dynamicSuggestions.value?.state ?? "IDLE");
const suggestions = computed(() => dynamicSuggestions.value?.suggestions ?? []);
diff --git a/frontend/src/plugins/ai/logic/prompt.ts b/frontend/src/plugins/ai/logic/prompt.ts
index 9c67dfa8ff..6b985437a6 100644
--- a/frontend/src/plugins/ai/logic/prompt.ts
+++ b/frontend/src/plugins/ai/logic/prompt.ts
@@ -2,6 +2,11 @@ import type { Engine } from "@/types/proto-es/v1/common_pb";
import type { DatabaseMetadata } from "@/types/proto-es/v1/database_service_pb";
import { engineNameV1 } from "@/utils";
+// Max characters for schema text to stay within token limits.
+// ~4 chars per token, leaving room for system prompt and response.
+// Targeting ~10k tokens = ~40k chars for the schema portion.
+const MAX_SCHEMA_CHARS = 40000;
+
export const databaseMetadataToText = (
databaseMetadata: DatabaseMetadata | undefined,
engine?: Engine,
@@ -27,8 +32,10 @@ export const databaseMetadataToText = (
: databaseMetadata.schemas;
const schemaScoped = !!schema;
- schemas.forEach((schema) => {
- schema.tables.forEach((table) => {
+ let currentLength = prompts.join("\n").length;
+
+ for (const schema of schemas) {
+ for (const table of schema.tables) {
const tableNameParts = [table.name];
if (schema.name && !schemaScoped) {
tableNameParts.unshift(schema.name);
@@ -42,9 +49,18 @@ export const databaseMetadataToText = (
}
})
.join(", ");
- prompts.push(`# ${tableNameParts.join(".")}(${columns})`);
- });
- });
+ const tableLine = `# ${tableNameParts.join(".")}(${columns})`;
+
+ // Check if adding this table would exceed the limit
+ if (currentLength + tableLine.length + 1 > MAX_SCHEMA_CHARS) {
+ prompts.push("# ... (schema truncated due to size)");
+ return prompts.join("\n");
+ }
+
+ prompts.push(tableLine);
+ currentLength += tableLine.length + 1; // +1 for newline
+ }
+ }
}
return prompts.join("\n");
};
diff --git a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
index 0868c45dad..801a3dac6e 100644
--- a/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
+++ b/frontend/src/plugins/ai/logic/useDynamicSuggestions.ts
@@ -1,8 +1,10 @@
import { create as createProto } from "@bufbuild/protobuf";
+import { createContextValues } from "@connectrpc/connect";
import { head, uniq, values } from "lodash-es";
import { computed, reactive, ref } from "vue";
import { hashCode } from "@/bbkit/BBUtil";
import { sqlServiceClientConnect } from "@/connect";
+import { silentContextKey } from "@/connect/context-key";
import {
type AICompletionRequest_Message,
AICompletionRequest_MessageSchema,
@@ -49,7 +51,10 @@ export const useDynamicSuggestions = () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
try {
const request = createProto(AICompletionRequestSchema, { messages });
- const response = await sqlServiceClientConnect.aICompletion(request);
+ // Use silent mode to avoid showing error notifications for AI completion failures
+ const response = await sqlServiceClientConnect.aICompletion(request, {
+ contextValues: createContextValues().set(silentContextKey, true),
+ });
const text =
head(head(response.candidates)?.content?.parts)?.text?.trim() ?? "";
const card = JSON.parse(text) as Record<string, string>;
@@ -66,7 +71,9 @@ export const useDynamicSuggestions = () => {
suggestions: [],
index: 0,
state: "IDLE",
- ready: false,
+ // Mark as ready without auto-fetching to avoid API calls on tab open.
+ // We will trigger the fetch when DynamicSuggestions mounted.
+ ready: true,
used: new Set(),
current() {
const { suggestions } = suggestion;
@@ -143,12 +150,7 @@ export const useDynamicSuggestions = () => {
});
const stored = storage.load<string[]>(suggestion.key, []);
if (stored && stored.length > 0) {
- suggestion.ready = true;
suggestion.suggestions = stored;
- } else {
- suggestion.fetch().then(() => {
- suggestion.ready = true;
- });
}
cache.value.set(metadata, suggestion);
return suggestion;
diff --git a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
index cfadc00636..cbfc96b1a9 100644
--- a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/StandardPanel.vue
@@ -35,7 +35,10 @@
</Suspense>
</template>
<template #2>
- <div class="h-full overflow-hidden flex flex-col">
+ <div
+ v-if="showAIPanel"
+ class="h-full overflow-hidden flex flex-col"
+ >
<Suspense>
<AIChatToSQL key="ai-chat-to-sql" />
<template #fallback>