Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix(nc-gui): handle deleted column properly from ai prompt

Ramesh Manefaee5523568c
diff --git a/packages/nc-gui/components/smartsheet/column/ButtonOptions/index.vue b/packages/nc-gui/components/smartsheet/column/ButtonOptions/index.vue
index 8d69f2d517..60b89a9006 100644
--- a/packages/nc-gui/components/smartsheet/column/ButtonOptions/index.vue
+++ b/packages/nc-gui/components/smartsheet/column/ButtonOptions/index.vue
@@ -6,6 +6,7 @@ import {
UITypes,
isHiddenCol,
substituteColumnIdWithAliasInFormula,
+ substituteColumnIdWithAliasInPrompt,
validateFormulaAndExtractTreeWithType,
} from 'nocodb-sdk'
import { searchIcons } from '~/utils/iconUtils'
@@ -315,6 +316,13 @@ if (vModel.value?.type === ButtonActionsType.Url || (column.value?.colOptions as
} else {
vModel.value.formula_raw = ''
}
+} else if (vModel.value?.type === ButtonActionsType.Ai || (column.value?.colOptions as any)?.type === ButtonActionsType.Ai) {
+ vModel.value.formula_raw =
+ substituteColumnIdWithAliasInPrompt(
+ (column.value?.colOptions as ButtonType)?.formula ?? '',
+ meta?.value?.columns as ColumnType[],
+ (column.value?.colOptions as any)?.formula_raw,
+ ).substituted || ''
}
const colorClass = {
diff --git a/packages/nc-gui/components/smartsheet/column/LongTextOptions.vue b/packages/nc-gui/components/smartsheet/column/LongTextOptions.vue
index a8137cb58d..7ae0f8ce28 100644
--- a/packages/nc-gui/components/smartsheet/column/LongTextOptions.vue
+++ b/packages/nc-gui/components/smartsheet/column/LongTextOptions.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import { UITypes, UITypesName, isAIPromptCol } from 'nocodb-sdk'
+import { UITypes, UITypesName, isAIPromptCol, substituteColumnIdWithAliasInPrompt, type ColumnType } from 'nocodb-sdk'
const props = defineProps<{
modelValue: any
@@ -128,7 +128,12 @@ const isPromptEnabled = computed(() => {
onMounted(() => {
// set default value
- vModel.value.prompt_raw = (column?.value?.colOptions as Record<string, any>)?.prompt_raw || ''
+ vModel.value.prompt_raw =
+ substituteColumnIdWithAliasInPrompt(
+ (column.value?.colOptions as Record<string, any>)?.prompt ?? '',
+ meta?.value?.columns as ColumnType[],
+ (column.value?.colOptions as Record<string, any>)?.prompt_raw,
+ ).substituted || ''
})
const validators = {
diff --git a/packages/nc-gui/utils/columnUtils.ts b/packages/nc-gui/utils/columnUtils.ts
index 9e5ad01e82..bfbb72f0e0 100644
--- a/packages/nc-gui/utils/columnUtils.ts
+++ b/packages/nc-gui/utils/columnUtils.ts
@@ -318,7 +318,7 @@ const isColumnInvalid = (
break
}
case UITypes.LongText: {
- if (parseProp(col.meta)[LongTextAiMetaProp]) {
+ if (isAIPromptCol(col)) {
const colOptions = col.colOptions as ButtonType
result.isInvalid =
diff --git a/packages/nocodb-sdk/src/lib/ai/index.ts b/packages/nocodb-sdk/src/lib/ai/index.ts
index b313823ee1..89853a1ddb 100644
--- a/packages/nocodb-sdk/src/lib/ai/index.ts
+++ b/packages/nocodb-sdk/src/lib/ai/index.ts
@@ -1,3 +1,5 @@
+import { ColumnType } from '../Api';
+
export interface SerializedAiTableType {
tables?: {
title?: string;
@@ -53,3 +55,46 @@ export interface PredictNextFormulasType {
formula: string;
description?: string | null;
}
+
+export function substituteColumnIdWithAliasInPrompt(
+ prompt: string,
+ columns: ColumnType[],
+ rawPrompt?: string
+): { substituted: string; missingIds: { id: string; title: string }[] } {
+ const columnIdMap = columns.reduce((acc, col) => {
+ acc[col.id] = col;
+ acc[col.column_name] = col;
+ acc[col.title] = col;
+ return acc;
+ }, {} as Record<string, ColumnType>);
+
+ // Keep flexible regex: matches anything inside {}
+ const regex = /{(.*?)}/g;
+
+ const rawMatches = rawPrompt ? [...rawPrompt.matchAll(regex)] : [];
+ let matchIndex = 0;
+ const missingIds: { id: string; title: string }[] = [];
+
+ const substituted = (prompt || '').replace(regex, (fullMatch, key) => {
+ const idx = matchIndex++; // always advance
+
+ const column = columnIdMap[key];
+ if (column) {
+ return `{${column.title}}`;
+ }
+
+ const oldName = rawMatches[idx]?.[1];
+ if (oldName) {
+ return `{${oldName}}`;
+ }
+
+ // Nothing found β†’ keep original and log it
+ missingIds.push({
+ id: key,
+ title: oldName,
+ });
+ return fullMatch;
+ });
+
+ return { substituted, missingIds };
+}