diff options
author | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2024-10-29 16:25:30 -0400 |
---|---|---|
committer | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2024-10-29 16:25:30 -0400 |
commit | 58e97fd9a8c85c7e4df75aed17d914498680c190 (patch) | |
tree | dc3fcb51ea351000c894ae0e36f6de06e6d660d5 | |
parent | 691fc130de706c691a42f7e87688c14102016ccc (diff) |
no compile errors but probably 100 logical ones
5 files changed, 61 insertions, 94 deletions
diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/DocCreatorMenu.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/DocCreatorMenu.tsx index 36264f886..46266883f 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/DocCreatorMenu.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/DocCreatorMenu.tsx @@ -59,7 +59,7 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { @observable _templateDocs: Doc[] = []; @observable _selectedTemplate: Doc | undefined = undefined; - @observable _columns: Col[] = []; + @observable _userCreatedColumns: Col[] = []; @observable _selectedCols: { title: string; type: string; desc: string }[] | undefined = []; @observable _layout: { type: LayoutType; yMargin: number; xMargin: number; columns?: number; repeat: number } = { type: LayoutType.GRID, yMargin: 0, xMargin: 0, repeat: 0 }; @@ -176,7 +176,7 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { return col; }) - .concat(this._columns); + .concat(this._userCreatedColumns); } @computed get canMakeDocs() { @@ -486,15 +486,15 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { }; @action addField = () => { - const newFields: Col[] = this._columns.concat([{ title: '', type: TemplateFieldType.UNSET, desc: '', sizes: [] }]); - this._columns = newFields; + const newFields: Col[] = this._userCreatedColumns.concat([{ title: '', type: TemplateFieldType.UNSET, desc: '', sizes: [] }]); + this._userCreatedColumns = newFields; }; @action removeField = (field: { title: string; type: string; desc: string }) => { if (this._dataViz?.axes.includes(field.title)) { this._dataViz.selectAxes(this._dataViz.axes.filter(col => col !== field.title)); } else { - const toRemove = this._columns.filter(f => f === field); + const toRemove = this._userCreatedColumns.filter(f => f === field); if (!toRemove) return; if (toRemove.length > 1) { @@ -503,10 +503,10 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { } } - if (this._columns.length === 1) { - this._columns = []; + if (this._userCreatedColumns.length === 1) { + this._userCreatedColumns = []; } else { - this._columns.splice(this._columns.indexOf(toRemove[0]), 1); + this._userCreatedColumns.splice(this._userCreatedColumns.indexOf(toRemove[0]), 1); } } }; @@ -573,7 +573,7 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { * @param assignments a list of template field numbers (from top to bottom) and their assigned columns from the linked dataviz * @returns a doc containing the fully rendered template */ - fillPresetTemplate = async (template: Template, assignments: { [field: string]: Col }): Promise<Doc> => { + applyGPTContentToTemplate = async (template: Template, assignments: { [field: string]: Col }): Promise<Template> => { const wordLimit = (size: TemplateFieldSize) => { switch (size) { case TemplateFieldSize.TINY: @@ -654,12 +654,8 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { return rendered; }; - const fields: Field[] = []; - - const autoLoadedFields = Object.entries(assignments).filter(([f, col]) => this._columns.includes(col)); - const userCreatedFields: [string, Col][] = Object.entries(assignments).filter(a => !autoLoadedFields.includes(a)); - const GPTTextCalls = autoLoadedFields.filter(([str, col]) => col.type === TemplateFieldType.TEXT); - const GPTIMGCalls = autoLoadedFields.filter(([str, col]) => col.type === TemplateFieldType.VISUAL); + const GPTTextCalls = Object.entries(assignments).filter(([str, col]) => col.type === TemplateFieldType.TEXT); + const GPTIMGCalls = Object.entries(assignments).filter(([str, col]) => col.type === TemplateFieldType.VISUAL); const stringifyGPTInfo = (calls: [string, Col][]): string => { let string: string = '*** COLUMN INFO:'; @@ -671,84 +667,24 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { const GPTTextAssignment = stringifyGPTInfo(GPTTextCalls); - let fieldContent: string = ''; - - Object.entries(userCreatedFields).forEach(([fieldID, strAndCol]) => { - const field: Field = template.getFieldByID(Number(fieldID)); - const col = strAndCol[1]; - - const doc = (col.type === TemplateFieldType.VISUAL ? FieldUtils.ImageField : FieldUtils.TextField)( - { - tl: field.tl, - br: field.br, - }, - template.height, - template.width, - col.title, - col.defaultContent ?? '', - field.opts - ); - - fieldContent += `--- Field #${fieldID} (title: ${col.title}): ${col.defaultContent ?? ''} ---`; - - fields.push(doc); - }); - - template.decorations.forEach(dec => { - console.log(dec); - const doc = FieldUtils.TextField( - { - tl: dec.tl, - br: dec.br, - }, - template.height, - template.width, - '', - '', - dec.opts - ); - - fields.push(doc); - }); - - const createMainDoc = (): Doc => { - const main = Docs.Create.FreeformDocument(fields, { - _height: template.height, - _width: template.width, - title: template.title, - backgroundColor: template.opts.backgroundColor, - _layout_borderRounding: `${template.opts.cornerRounding}px` ?? '0px', - borderWidth: template.opts.borderWidth, - borderColor: template.opts.borderColor, - x: 40000, - y: 40000, - }); - - const mainCollection = this._dataViz?.DocumentView?.().containerViewPath?.().lastElement()?.ComponentView as CollectionFreeFormView; - mainCollection.addDocument(main); - - return main; - }; + let fieldContent: string = this.templateManager.getContentSummary(template); const textCalls = await renderTextCalls(); const imageCalls = await renderImageCalls(); - textCalls.forEach(doc => { - fields.push(doc); - }); - imageCalls.forEach(doc => { - fields.push(doc); - }); - - return createMainDoc(); + return template; }; + renderTemplates = (templates: Template[]) => { + + } + compileFieldDescriptions = (templates: Template[]): string => { let descriptions: string = ''; templates.forEach(template => { - descriptions += `---------- NEW TEMPLATE TO INCLUDE: Description of template ${template.title}'s fields: `; - template.fields.forEach((field, index) => { - descriptions += `{Field #${index}: ${field.description}} `; + descriptions += `---------- NEW TEMPLATE TO INCLUDE: Description of template ${template.mainField.getTitle()}'s fields: `; + template.allFields.forEach(field => { + descriptions += `{Field #${field.getID}: ${field.getDescription}} `; }); }); @@ -767,7 +703,7 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { }; @action - assignColsToFields = async (templates: TemplateDocInfos[], cols: Col[]): Promise<[TemplateDocInfos, { [field: number]: Col }][]> => { + assignColsToFields = async (templates: Template[], cols: Col[]): Promise<[Template, { [field: number]: Col }][]> => { const fieldDescriptions: string = this.compileFieldDescriptions(templates); const colDescriptions: string = this.compileColDescriptions(cols); @@ -784,15 +720,22 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { const res = await gptAPICall(prompt, GPTCallType.TEMPLATE); if (res && this._callCount === origCount) { - const assignments: { [templateTitle: string]: { [field: string]: string } } = JSON.parse(res); - const brokenDownAssignments: [TemplateDocInfos, { [field: number]: Col }][] = []; + const assignments: { [templateTitle: string]: { [fieldID: string]: string } } = JSON.parse(res); + const brokenDownAssignments: [Template, { [fieldID: number]: Col }][] = []; Object.entries(assignments).forEach(([tempTitle, assignment]) => { - const template = TemplateLayouts.getTemplateByTitle(tempTitle); + const template = templates.filter(template => template.mainField.getTitle() === tempTitle)[0]; if (!template) return; const toObj = Object.entries(assignment).reduce( - (a, [fieldNum, colTitle]) => { - a[Number(fieldNum)] = this.getColByTitle(colTitle); + (a, [fieldID, colTitle]) => { + const col = this.getColByTitle(colTitle); + if (!this._userCreatedColumns.includes(col)){ // do the following for any fields not added by the user; will change in the future, for now only GPT content works with user-added fields + const field = template.getFieldByID(Number(fieldID)); + field.setContent(col.defaultContent ?? '', col.type === TemplateFieldType.VISUAL ? FieldContentType.IMAGE : FieldContentType.STRING); + field.setTitle(col.title); + } else { + a[Number(fieldID)] = this.getColByTitle(colTitle); + } return a; }, {} as { [field: number]: Col } @@ -814,14 +757,14 @@ export class DocCreatorMenu extends ObservableReactComponent<FieldViewProps> { const cols = this.fieldsInfos; const templates = this.templateManager.getValidTemplates(cols); - const assignments: [TemplateDocInfos, { [field: number]: Col }][] = await this.assignColsToFields(templates, cols); + const assignments: [Template, { [field: number]: Col }][] = await this.assignColsToFields(templates, cols); - const renderedTemplatePromises: Promise<Doc>[] = assignments.map(([template, assignments]) => this.fillPresetTemplate(template, assignments)); + const renderedTemplatePromises: Promise<Template>[] = assignments.map(([template, assignments]) => this.applyGPTContentToTemplate(template, assignments)); - const renderedTemplates: Doc[] = await Promise.all(renderedTemplatePromises); + const renderedTemplates: Template[] = await Promise.all(renderedTemplatePromises); setTimeout(() => { - this.setGSuggestedTemplates(renderedTemplates); + this.setGSuggestedTemplates(renderedTemplates.map(template => template.mainField.renderedDoc())); this._GPTLoading = false; }); }; diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/DynamicField.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/DynamicField.tsx index a55f44589..220d938ff 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/DynamicField.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/DynamicField.tsx @@ -21,6 +21,7 @@ export class DynamicField implements Field { this.type = type; this.id = id; this.settings = settings; + if (settings.title) { this.title = settings.title }; if (!parent) { this.parent = this; this.dimensions = {width: this.settings.br[0] - this.settings.tl[0], height: this.settings.br[1] - this.settings.tl[1], coord: {x: this.settings.tl[0], y: this.settings.tl[1]}}; @@ -51,6 +52,10 @@ export class DynamicField implements Field { get getID() { return this.id }; get getViewType() { return this.type }; + get getDescription(): string { + return this.settings.description ?? ''; + } + matches = (cols: Col[]): Array<number> => { return []; } diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/Field.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/Field.tsx index aa1d1de33..feba6a8ef 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/Field.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/Field.tsx @@ -10,6 +10,7 @@ export interface Field { getAllSubfields: Field[]; getID: number; getViewType: ViewType; + getDescription: string; getTitle: () => string; setTitle: (title: string) => void; setupSubfields: () => Field[]; @@ -22,6 +23,7 @@ export type FieldSettings = { br: [number, number]; opts: FieldOpts; viewType: ViewType; + title?: string; subfields?: FieldSettings[]; types?: TemplateFieldType[]; sizes?: TemplateFieldSize[]; diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/StaticField.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/StaticField.tsx index cd1008429..809b40520 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/StaticField.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/FieldTypes/StaticField.tsx @@ -23,6 +23,7 @@ export class StaticField { constructor(settings: FieldSettings, parent: Field, id: number) { this.settings = settings; + if (settings.title) { this.title = settings.title }; this.id = id; this.parent = parent; this.dimensions = FieldUtils.getLocalDimensions({tl: settings.tl, br: settings.br}, this.parent.getDimensions); @@ -45,6 +46,10 @@ export class StaticField { get getID() { return this.id }; get getViewType() { return ViewType.STATIC }; + get getDescription(): string { + return this.settings.description ?? ''; + } + setContent = (newContent: string, type: FieldContentType) => { this.content = newContent; this.contentType = type; diff --git a/src/client/views/nodes/DataVizBox/DocCreatorMenu/TemplateManager.tsx b/src/client/views/nodes/DataVizBox/DocCreatorMenu/TemplateManager.tsx index 890bc6f73..d47ae802c 100644 --- a/src/client/views/nodes/DataVizBox/DocCreatorMenu/TemplateManager.tsx +++ b/src/client/views/nodes/DataVizBox/DocCreatorMenu/TemplateManager.tsx @@ -17,6 +17,18 @@ export class TemplateManager { return initializedTemplates; } + /** + * Provides a text summary of content currently filled into a template, formatted for GPT analysis. + * @param template the template to be summarized + */ + getContentSummary = (template: Template) => { + let summary: string = ''; + template.contentFields.forEach(field => { + summary += `--- Field #${field.getID} (title: ${field.getTitle()}): ${field.getContent() ?? ''} ---`; + }); + return summary; + } + getValidTemplates = (cols: Col[]): Template[] => { return this.templates.filter(template => template.isValidTemplate(cols)); } |