104 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-07-08 12:53:27 +03:00
///
/// Copyright © 2016-2025 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Component } from '@angular/core';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { RuleNodeConfiguration, RuleNodeConfigurationComponent } from '@shared/models/rule-node.models';
import { EntityType } from '@shared/models/entity-type.models';
import { MatDialog } from '@angular/material/dialog';
import { AIModelDialogComponent, AIModelDialogData } from '@shared/components/ai-model/ai-model-dialog.component';
2025-07-09 20:05:06 +03:00
import { AiModel, AiRuleNodeResponseFormatTypeOnlyText, ResponseFormat } from '@shared/models/ai-model.models';
2025-07-08 12:53:27 +03:00
import { deepTrim } from '@core/utils';
@Component({
selector: 'tb-external-node-ai-config',
templateUrl: './ai-config.component.html',
styleUrls: []
})
export class AiConfigComponent extends RuleNodeConfigurationComponent {
aiConfigForm: UntypedFormGroup;
entityType = EntityType;
responseFormat = ResponseFormat;
constructor(private fb: UntypedFormBuilder,
private dialog: MatDialog) {
super();
}
protected configForm(): UntypedFormGroup {
return this.aiConfigForm;
}
protected onConfigurationSet(configuration: RuleNodeConfiguration) {
this.aiConfigForm = this.fb.group({
2025-07-09 20:05:06 +03:00
modelSettingsId: [configuration?.modelSettingsId ?? null, [Validators.required]],
systemPrompt: [configuration?.systemPrompt ?? '', [Validators.maxLength(10000), Validators.pattern(/.*\S.*/)]],
userPrompt: [configuration?.userPrompt ?? '', [Validators.required, Validators.maxLength(10000), Validators.pattern(/.*\S.*/)]],
2025-07-08 12:53:27 +03:00
responseFormat: this.fb.group({
2025-07-09 20:05:06 +03:00
type: [configuration?.responseFormat?.type ?? ResponseFormat.JSON, []],
schema: [configuration?.responseFormat?.schema ?? null, [Validators.required]],
2025-07-08 12:53:27 +03:00
}),
2025-07-09 20:05:06 +03:00
timeoutSeconds: [configuration?.timeoutSeconds ?? 60, []]
2025-07-08 12:53:27 +03:00
});
}
protected validatorTriggers(): string[] {
return ['responseFormat.type'];
}
protected updateValidators(emitEvent: boolean) {
2025-07-09 20:05:06 +03:00
if (this.aiConfigForm.get('responseFormat.type').value === ResponseFormat.JSON_SCHEMA) {
this.aiConfigForm.get('responseFormat.schema').enable({emitEvent: false});
2025-07-08 12:53:27 +03:00
} else {
2025-07-09 20:05:06 +03:00
this.aiConfigForm.get('responseFormat.schema').disable({emitEvent: false});
2025-07-08 12:53:27 +03:00
}
}
protected prepareOutputConfig(configuration: RuleNodeConfiguration): RuleNodeConfiguration {
return deepTrim(configuration);
}
onEntityChange($event: AiModel) {
if ($event) {
2025-07-09 20:05:06 +03:00
if (AiRuleNodeResponseFormatTypeOnlyText.includes($event.configuration.provider)) {
2025-07-08 12:53:27 +03:00
this.aiConfigForm.get('responseFormat.type').patchValue(ResponseFormat.TEXT, {emitEvent: false});
this.aiConfigForm.get('responseFormat.type').disable({emitEvent: false});
}
} else {
this.aiConfigForm.get('responseFormat.type').enable({emitEvent: false});
}
}
2025-07-09 20:05:06 +03:00
createModelAi(formControl: string) {
2025-07-08 12:53:27 +03:00
this.dialog.open<AIModelDialogComponent, AIModelDialogData, AiModel>(AIModelDialogComponent, {
disableClose: true,
panelClass: ['tb-dialog', 'tb-fullscreen-dialog'],
data: {
isAdd: true
}
}).afterClosed()
.subscribe((model) => {
if (model) {
this.aiConfigForm.get(formControl).patchValue(model.id);
this.aiConfigForm.get(formControl).markAsDirty();
}
});
}
}