From 2aaef12a1ae174a23eff4b5e92d272089d7664b0 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 1 Oct 2024 16:36:37 +0100 Subject: [PATCH] better typing - discriminated unions --- .../DataTable/modals/CreateEditColumn.svelte | 2 +- .../common/AIFieldConfiguration.svelte | 15 +-- packages/pro | 2 +- .../src/api/controllers/table/bulkFormula.ts | 4 +- packages/types/src/documents/app/ai.ts | 105 ++++++++++++++++++ packages/types/src/documents/app/index.ts | 1 + .../types/src/documents/app/table/schema.ts | 5 +- 7 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 packages/types/src/documents/app/ai.ts diff --git a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte index da68b4182b..30472c4939 100644 --- a/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte +++ b/packages/builder/src/components/backend/DataTable/modals/CreateEditColumn.svelte @@ -417,13 +417,13 @@ FIELDS.BOOLEAN, FIELDS.DATETIME, FIELDS.LINK, + FIELDS.AI, FIELDS.LONGFORM, FIELDS.USER, FIELDS.USERS, FIELDS.ATTACHMENT_SINGLE, FIELDS.ATTACHMENTS, FIELDS.FORMULA, - FIELDS.AI, FIELDS.JSON, FIELDS.BARCODEQR, FIELDS.SIGNATURE_SINGLE, diff --git a/packages/builder/src/components/common/AIFieldConfiguration.svelte b/packages/builder/src/components/common/AIFieldConfiguration.svelte index 73e0562dbe..84a9deca95 100644 --- a/packages/builder/src/components/common/AIFieldConfiguration.svelte +++ b/packages/builder/src/components/common/AIFieldConfiguration.svelte @@ -1,5 +1,5 @@ {/if} diff --git a/packages/pro b/packages/pro index dcc9e50b80..d194be2339 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit dcc9e50b8064a2097d408771462ad80f48de7ff6 +Subproject commit d194be2339a1ee2e5b2ed1fe59d103249f2d8d2c diff --git a/packages/server/src/api/controllers/table/bulkFormula.ts b/packages/server/src/api/controllers/table/bulkFormula.ts index 6e47f30233..b2c9f6a27b 100644 --- a/packages/server/src/api/controllers/table/bulkFormula.ts +++ b/packages/server/src/api/controllers/table/bulkFormula.ts @@ -198,7 +198,7 @@ export async function runAIColumnChecks( table: Table, { oldTable }: { oldTable?: Table } ) { - // look to see if any formula values have changed + // look to see if any AI column values have changed const shouldUpdate = Object.values(table.schema).find( column => column.type === FieldType.AI && @@ -206,7 +206,7 @@ export async function runAIColumnChecks( !oldTable.schema[column.name] || !isEqual(oldTable.schema[column.name], column)) ) - // if a static formula column has updated, then need to run the update + // if an AI column has updated, then need to run the update if (shouldUpdate != null) { await updateAllAIColumnsInTable(table) } diff --git a/packages/types/src/documents/app/ai.ts b/packages/types/src/documents/app/ai.ts new file mode 100644 index 0000000000..6d3f164fa4 --- /dev/null +++ b/packages/types/src/documents/app/ai.ts @@ -0,0 +1,105 @@ + +export enum AIOperationEnum { + SUMMARISE_TEXT = "SUMMARISE_TEXT", + CLEAN_DATA = "CLEAN_DATA", + TRANSLATE = "TRANSLATE", + CATEGORISE_TEXT = "CATEGORISE_TEXT", + SENTIMENT_ANALYSIS = "SENTIMENT_ANALYSIS", + PROMPT = "PROMPT", + SEARCH_WEB = "SEARCH_WEB", +} + + +enum OperationFieldTypeEnum { + MULTI_COLUMN = "columns", + COLUMN = "column", + BINDABLE_TEXT = "prompt", +} + + +type OperationFieldsType = { + [AIOperationEnum.SUMMARISE_TEXT]: { + columns: OperationFieldTypeEnum.MULTI_COLUMN + } + [AIOperationEnum.CLEAN_DATA]: { + column: OperationFieldTypeEnum.COLUMN + } + [AIOperationEnum.TRANSLATE]: { + column: OperationFieldTypeEnum.COLUMN + language: OperationFieldTypeEnum.BINDABLE_TEXT + } + [AIOperationEnum.CATEGORISE_TEXT]: { + columns: OperationFieldTypeEnum.MULTI_COLUMN + categories: OperationFieldTypeEnum.BINDABLE_TEXT + } + [AIOperationEnum.SENTIMENT_ANALYSIS]: { + column: OperationFieldTypeEnum.COLUMN + } + [AIOperationEnum.PROMPT]: { + prompt: OperationFieldTypeEnum.BINDABLE_TEXT + } + [AIOperationEnum.SEARCH_WEB]: { + columns: OperationFieldTypeEnum.MULTI_COLUMN + } +} + +// Define the AI operations with just their labels (no need for separate 'value' property) +const AIOperations: { [key in AIOperationEnum]: string } = { + [AIOperationEnum.SUMMARISE_TEXT]: "Summarise Text", + [AIOperationEnum.CLEAN_DATA]: "Clean Data", + [AIOperationEnum.TRANSLATE]: "Translate", + [AIOperationEnum.CATEGORISE_TEXT]: "Categorise Text", + [AIOperationEnum.SENTIMENT_ANALYSIS]: "Sentiment Analysis", + [AIOperationEnum.PROMPT]: "Prompt", + [AIOperationEnum.SEARCH_WEB]: "Search Web", +} + +type BaseSchema = { + operation: AIOperationEnum +} + +type SummariseTextSchema = BaseSchema & { + operation: AIOperationEnum.SUMMARISE_TEXT + columns: string[] +} + +type CleanDataSchema = BaseSchema & { + operation: AIOperationEnum.CLEAN_DATA + column: string +} + +type TranslateSchema = BaseSchema & { + operation: AIOperationEnum.TRANSLATE + column: string + language: string +} + +type CategoriseTextSchema = BaseSchema & { + operation: AIOperationEnum.CATEGORISE_TEXT + columns: string[] + categories: string +} + +type SentimentAnalysisSchema = BaseSchema & { + operation: AIOperationEnum.SENTIMENT_ANALYSIS + column: string +} + +type PromptSchema = BaseSchema & { + operation: AIOperationEnum.PROMPT + prompt: string +} + +type SearchWebSchema = BaseSchema & { + operation: AIOperationEnum.SEARCH_WEB + columns: string[] +} + +export type AIColumnSchema = + | SummariseTextSchema + | CleanDataSchema + | TranslateSchema + | CategoriseTextSchema + | SentimentAnalysisSchema + | PromptSchema + | SearchWebSchema \ No newline at end of file diff --git a/packages/types/src/documents/app/index.ts b/packages/types/src/documents/app/index.ts index 2b13676ba1..0290e9ed2e 100644 --- a/packages/types/src/documents/app/index.ts +++ b/packages/types/src/documents/app/index.ts @@ -1,3 +1,4 @@ +export * from "./ai" export * from "./app" export * from "./automation" export * from "./datasource" diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index b0c78f9dc5..de1395cf31 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -9,6 +9,7 @@ import { JsonFieldSubType, RelationshipType, } from "./constants" +import { AIOperationEnum } from "../ai" export interface UIFieldMetadata { order?: number @@ -119,10 +120,10 @@ export interface FormulaFieldMetadata extends BaseFieldSchema { export interface AIFieldMetadata extends BaseFieldSchema { type: FieldType.AI formula: string - // TODO: needs better types - operation: string + operation: AIOperationEnum columns?: string[] column?: string + categories?: string prompt?: string language?: string }