From bd7b17d64b8becbaa6794fe2e4d5dc35e32cb13d Mon Sep 17 00:00:00 2001 From: meta-d Date: Wed, 7 Feb 2024 15:37:30 +0800 Subject: [PATCH] feat: copilot chat in formula --- .../app/@shared/model/formula.component.html | 97 ++++++++----------- .../app/@shared/model/formula.component.scss | 4 + .../app/@shared/model/formula.component.ts | 86 +++++++++++----- 3 files changed, 106 insertions(+), 81 deletions(-) diff --git a/apps/cloud/src/app/@shared/model/formula.component.html b/apps/cloud/src/app/@shared/model/formula.component.html index 18d3543cc..811cd6b3a 100644 --- a/apps/cloud/src/app/@shared/model/formula.component.html +++ b/apps/cloud/src/app/@shared/model/formula.component.html @@ -1,63 +1,46 @@
-
-
- drag_indicator - - {{ 'PAC.KEY_WORDS.CalculatedFormula' | translate: {Default: 'Calculated Formula'} }} - - ({{measure.caption}}) -
- -
- - - - - - - - - - - - -
- {{f.expression}} -
- -
-
-
- -
- -
-
- - - -
+
+
+ drag_indicator + + {{ 'PAC.KEY_WORDS.CalculatedFormula' | translate: {Default: 'Calculated Formula'} }} + + ({{measure.caption}}) +
+ +
+ +
+ +
+
+ + +
+
-
- - - - +
+ + + +
\ No newline at end of file diff --git a/apps/cloud/src/app/@shared/model/formula.component.scss b/apps/cloud/src/app/@shared/model/formula.component.scss index 4a486e1ee..684dad13e 100644 --- a/apps/cloud/src/app/@shared/model/formula.component.scss +++ b/apps/cloud/src/app/@shared/model/formula.component.scss @@ -4,4 +4,8 @@ flex: 1; max-width: 100%; overflow: hidden; +} + +.ngm-copilot-chat { + --ngm-copilot-bg-color: theme('colors.bluegray.700'); } \ No newline at end of file diff --git a/apps/cloud/src/app/@shared/model/formula.component.ts b/apps/cloud/src/app/@shared/model/formula.component.ts index f89ee2b2b..ac09487d1 100644 --- a/apps/cloud/src/app/@shared/model/formula.component.ts +++ b/apps/cloud/src/app/@shared/model/formula.component.ts @@ -1,19 +1,17 @@ import { CommonModule } from '@angular/common' -import { ChangeDetectionStrategy, Component, Inject, Input } from '@angular/core' +import { ChangeDetectionStrategy, Component, Inject, Input, inject } from '@angular/core' +import { toSignal } from '@angular/core/rxjs-interop' import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms' import { MAT_DIALOG_DATA } from '@angular/material/dialog' -import { NgmSearchComponent } from '@metad/ocap-angular/common' +import { CalculatedMeasureComponent } from '@metad/components/property' +import { NgmCopilotChatComponent, NgmCopilotEngineService, injectCopilotCommand, injectMakeCopilotActionable } from '@metad/ocap-angular/copilot' import { ButtonGroupDirective } from '@metad/ocap-angular/core' import { FormulaModule } from '@metad/ocap-angular/formula' import { DataSettings, EntityType, PropertyMeasure, Syntax } from '@metad/ocap-core' import { TranslateModule } from '@ngx-translate/core' -import { HighlightDirective } from '@metad/components/core' -import { MDXReference } from '@metad/components/mdx' -import { CalculatedMeasureComponent } from '@metad/components/property' -import { sortBy } from 'lodash-es' -import { MarkdownModule } from 'ngx-markdown' -import { map, of, startWith, switchMap } from 'rxjs' +import { Store } from '../../@core' import { MaterialModule } from '../material.module' +import { NGXLogger } from 'ngx-logger' @Component({ standalone: true, @@ -24,20 +22,21 @@ import { MaterialModule } from '../material.module' TranslateModule, MaterialModule, FormulaModule, - MarkdownModule, - HighlightDirective, CalculatedMeasureComponent, ButtonGroupDirective, - NgmSearchComponent + NgmCopilotChatComponent ], changeDetection: ChangeDetectionStrategy.OnPush, selector: 'pac-model-formula', templateUrl: 'formula.component.html', - styleUrls: ['formula.component.scss'] + styleUrls: ['formula.component.scss'], + providers: [ NgmCopilotEngineService ] }) export class ModelFormulaComponent { + readonly #store = inject(Store) + readonly #logger = inject(NGXLogger) + Syntax = Syntax - FUNCTIONS = sortBy(MDXReference.FUNCTIONS, 'label') @Input() dataSettings: DataSettings @Input() entityType: EntityType @@ -54,17 +53,56 @@ export class ModelFormulaComponent { formula = '' - public readonly functions$ = of(sortBy(MDXReference.FUNCTIONS, 'label')).pipe( - switchMap((functions) => - this.searchControl.valueChanges.pipe( - startWith(''), - map((text) => { - text = text?.trim().toLowerCase() - return text ? functions.filter((item) => item.label.toLowerCase().includes(text)) : functions - }) - ) - ) - ) + readonly themeName = toSignal(this.#store.primaryTheme$) + + #newFormula = injectMakeCopilotActionable({ + name: 'new_formula', + description: 'New formula for measure', + argumentAnnotations: [ + { + name: 'formula', + type: 'string', + description: 'The formula to be used for the measure', + required: true + } + ], + implementation: async (formula: string) => { + this.#logger.debug(`Copilot make formula is: ${formula}`) + this.formula = formula + return `The formula for the measure has been set to: ${formula}` + } + }) + + #formatFormula = injectCopilotCommand({ + name: 'format', + description: '', + systemPrompt: () => { + return `Format the MDX formula: +\`\`\` +${this.formula} +\`\`\` +` + }, + actions: [ + injectMakeCopilotActionable({ + name: 'format_formula', + description: 'Format the formula', + argumentAnnotations: [ + { + name: 'formula', + type: 'string', + description: 'The formula to be formatted', + required: true + } + ], + implementation: async (formula: string) => { + this.#logger.debug('Copilot format formula') + this.formula = formula + return `The formula has been formatted` + } + }) + ] + }) constructor( @Inject(MAT_DIALOG_DATA)