diff --git a/packages/pro b/packages/pro index 703247c76a..0e940ad427 160000 --- a/packages/pro +++ b/packages/pro @@ -1 +1 @@ -Subproject commit 703247c76a6d008ce425318b9c038a1509c71df4 +Subproject commit 0e940ad427ef13ff48bc0911b18743734c34ccce diff --git a/packages/server/src/api/controllers/ai.ts b/packages/server/src/api/controllers/ai.ts index 86218392dd..1dff73ef91 100644 --- a/packages/server/src/api/controllers/ai.ts +++ b/packages/server/src/api/controllers/ai.ts @@ -3,13 +3,11 @@ import { FieldType, GenerateTablesRequest, GenerateTablesResponse, - RelationshipFieldMetadata, SourceName, - Table, + TableSourceType, UserCtx, } from "@budibase/types" import { getLLM } from "packages/pro/src/ai" -import * as tableController from "./public/tables" import { context, utils } from "@budibase/backend-core" import sdk from "../../sdk" import fs from "fs" @@ -44,51 +42,41 @@ export async function generateTables( config: {}, }) - console.warn(response?.message) - if (!useCached) { const dir = path.join(process.env.PWD!, `../../llm-output/${cacheKey}`) if (!fs.existsSync(dir)) { fs.mkdirSync(dir) } fs.writeFileSync(path.join(dir, "prompt.txt"), prompt) - fs.writeFileSync(path.join(dir, "latest.json"), response?.message || "") + fs.writeFileSync(path.join(dir, "latest.json"), JSON.stringify(response)) fs.writeFileSync( path.join(dir, `${Date.now()}.json`), - response?.message || "" + JSON.stringify(response) ) } - const json = JSON.parse(response!.message || "") as { - tables: Table[] - data: Record - } const createdTables: GenerateTablesResponse["createdTables"] = [] - for (const table of Object.values(json.tables)) { - const createTableCtx = { - request: { - body: { - ...table, - sourceId: dsId, - schema: {}, - primaryDisplay: undefined, - }, - }, - user: ctx.user, - throw: ctx.throw, - } as any - await tableController.create(createTableCtx, async () => {}) - const createdTable = createTableCtx.body - createdTables.push({ id: createdTable._id, name: createdTable.name }) - table._id = createdTable._id + for (const table of response!.tables) { + const { _id, ...structure } = table.structure + const createdTable = await sdk.tables.create({ + ...structure, + sourceId: dsId, + schema: {}, + primaryDisplay: undefined, + sourceType: TableSourceType.INTERNAL, + type: "table", + }) + + createdTables.push({ id: createdTable._id!, name: createdTable.name }) + table.structure._id = createdTable._id! } - for (const table of Object.values(json.tables)) { - for (const fieldKey of Object.keys(table.schema).filter( - f => table.schema[f].type === FieldType.LINK + for (const table of Object.values(response!.tables)) { + for (const field of table.structure.schema.filter( + f => f.type === FieldType.LINK )) { - const field = table.schema[fieldKey] as RelationshipFieldMetadata + // const field = table.schema[fieldKey] as RelationshipFieldMetadata const linkedTable = createdTables.find(t => t.name === field.tableId) if (!linkedTable) { throw `Table ${field.tableId} not found in the json response.` @@ -97,106 +85,106 @@ export async function generateTables( } } - for (const table of Object.values(json.tables)) { - const readTableCtx = { - params: { tableId: table._id }, - user: ctx.user, - throw: ctx.throw, - } as any + // for (const table of Object.values(json.tables)) { + // const readTableCtx = { + // params: { tableId: table._id }, + // user: ctx.user, + // throw: ctx.throw, + // } as any - await tableController.read(readTableCtx, async () => {}) + // await tableController.read(readTableCtx, async () => {}) - const updateTableCtx = { - request: { - body: { - ...readTableCtx.body, - schema: { - ...readTableCtx.body.schema, - ...table.schema, - }, - primaryDisplay: table.primaryDisplay, - }, - }, - params: { tableId: table._id }, - user: ctx.user, - throw: ctx.throw, - } as any - await tableController.update(updateTableCtx, async () => {}) - } + // const updateTableCtx = { + // request: { + // body: { + // ...readTableCtx.body, + // schema: { + // ...readTableCtx.body.schema, + // ...table.schema, + // }, + // primaryDisplay: table.primaryDisplay, + // }, + // }, + // params: { tableId: table._id }, + // user: ctx.user, + // throw: ctx.throw, + // } as any + // await tableController.update(updateTableCtx, async () => {}) + // } - if (addData) { - const createdData: Record> = {} - const toUpdateLinks: { - tableId: string - rowId: string - data: Record - }[] = [] - for (const table of Object.values(json.tables)) { - const dataToAdd = json.data?.[table.name] + // if (addData) { + // const createdData: Record> = {} + // const toUpdateLinks: { + // tableId: string + // rowId: string + // data: Record + // }[] = [] + // for (const table of Object.values(json.tables)) { + // const dataToAdd = json.data?.[table.name] - const linksOverride: Record = {} - for (const fieldKey of Object.keys(table.schema).filter( - f => table.schema[f].type === FieldType.LINK - )) { - linksOverride[fieldKey] = null - } + // const linksOverride: Record = {} + // for (const fieldKey of Object.keys(table.schema).filter( + // f => table.schema[f].type === FieldType.LINK + // )) { + // linksOverride[fieldKey] = null + // } - for (const entry of dataToAdd || []) { - const createdRow = await sdk.rows.save( - table._id!, - { - ...entry, - ...linksOverride, - _id: undefined, - }, - ctx.user._id - ) + // for (const entry of dataToAdd || []) { + // const createdRow = await sdk.rows.save( + // table._id!, + // { + // ...entry, + // ...linksOverride, + // _id: undefined, + // }, + // ctx.user._id + // ) - createdData[table._id!] ??= {} - createdData[table._id!][entry._id] = createdRow.row._id! + // createdData[table._id!] ??= {} + // createdData[table._id!][entry._id] = createdRow.row._id! - const overridenLinks = Object.keys(linksOverride).reduce< - Record - >((acc, l) => { - if (entry[l]) { - acc[l] = { - tableId: (table.schema[l] as RelationshipFieldMetadata).tableId, - rowId: entry[l], - } - } - return acc - }, {}) + // const overridenLinks = Object.keys(linksOverride).reduce< + // Record + // >((acc, l) => { + // if (entry[l]) { + // acc[l] = { + // tableId: (table.schema[l] as RelationshipFieldMetadata).tableId, + // rowId: entry[l], + // } + // } + // return acc + // }, {}) - if (Object.keys(overridenLinks)) { - toUpdateLinks.push({ - tableId: createdRow.table._id!, - rowId: createdRow.row._id!, - data: overridenLinks, - }) - } - } - } + // if (Object.keys(overridenLinks)) { + // toUpdateLinks.push({ + // tableId: createdRow.table._id!, + // rowId: createdRow.row._id!, + // data: overridenLinks, + // }) + // } + // } + // } - for (const data of toUpdateLinks.filter(d => Object.keys(d.data).length)) { - const persistedRow = await sdk.rows.find(data.tableId, data.rowId) + // for (const data of toUpdateLinks.filter(d => Object.keys(d.data).length)) { + // const persistedRow = await sdk.rows.find(data.tableId, data.rowId) - const updatedLinks = Object.keys(data.data).reduce< - Record - >((acc, d) => { - acc[d] = createdData[data.data[d].tableId][data.data[d].rowId] - return acc - }, {}) + // const updatedLinks = Object.keys(data.data).reduce< + // Record + // >((acc, d) => { + // acc[d] = createdData[data.data[d].tableId][data.data[d].rowId] + // return acc + // }, {}) - await sdk.rows.save( - data.tableId, - { - ...persistedRow, - ...updatedLinks, - }, - ctx.user._id - ) - } - } + // await sdk.rows.save( + // data.tableId, + // { + // ...persistedRow, + // ...updatedLinks, + // }, + // ctx.user._id + // ) + // } + // } ctx.body = { createdTables,