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';
|
2025-07-10 13:32:39 +03:00
|
|
|
import { AIModelDialogComponent, AIModelDialogData } from '@home/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';
|
2025-07-14 10:52:06 +03:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2025-08-08 10:57:56 +03:00
|
|
|
import { jsonRequired } from '@shared/components/json-object-edit.component';
|
2025-07-08 12:53:27 +03:00
|
|
|
|
|
|
|
|
@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,
|
2025-07-14 10:52:06 +03:00
|
|
|
private translate: TranslateService,
|
2025-07-08 12:53:27 +03:00
|
|
|
private dialog: MatDialog) {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected configForm(): UntypedFormGroup {
|
|
|
|
|
return this.aiConfigForm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected onConfigurationSet(configuration: RuleNodeConfiguration) {
|
|
|
|
|
this.aiConfigForm = this.fb.group({
|
2025-07-10 13:32:39 +03:00
|
|
|
modelId: [configuration?.modelId ?? null, [Validators.required]],
|
2025-08-13 18:26:53 +03:00
|
|
|
systemPrompt: [configuration?.systemPrompt ?? '', [Validators.maxLength(500_000), Validators.pattern(/.*\S.*/)]],
|
|
|
|
|
userPrompt: [configuration?.userPrompt ?? '', [Validators.required, Validators.maxLength(500_000), 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, []],
|
2025-08-08 10:57:56 +03:00
|
|
|
schema: [configuration?.responseFormat?.schema ?? null, [jsonRequired]],
|
2025-07-08 12:53:27 +03:00
|
|
|
}),
|
2025-07-10 15:58:45 +03:00
|
|
|
timeoutSeconds: [configuration?.timeoutSeconds ?? 60, []],
|
|
|
|
|
forceAck: [configuration?.forceAck ?? true, []]
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 14:36:13 +03:00
|
|
|
protected prepareOutputConfig(): RuleNodeConfiguration {
|
|
|
|
|
const config = this.configForm().getRawValue();
|
2025-07-14 10:52:06 +03:00
|
|
|
if (!this.aiConfigForm.get('systemPrompt').value) {
|
2025-08-20 14:36:13 +03:00
|
|
|
delete config.systemPrompt;
|
2025-07-14 10:52:06 +03:00
|
|
|
}
|
2025-08-20 14:36:13 +03:00
|
|
|
return deepTrim(config);
|
2025-07-08 12:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onEntityChange($event: AiModel) {
|
|
|
|
|
if ($event) {
|
2025-07-09 20:05:06 +03:00
|
|
|
if (AiRuleNodeResponseFormatTypeOnlyText.includes($event.configuration.provider)) {
|
2025-07-14 10:52:06 +03:00
|
|
|
if (this.aiConfigForm.get('responseFormat.type').value !== ResponseFormat.TEXT) {
|
|
|
|
|
this.aiConfigForm.get('responseFormat.type').patchValue(ResponseFormat.TEXT, {emitEvent: true});
|
|
|
|
|
}
|
2025-08-08 10:57:56 +03:00
|
|
|
this.aiConfigForm.get('responseFormat.type').disable();
|
2025-07-08 12:53:27 +03:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-08-08 10:57:56 +03:00
|
|
|
this.aiConfigForm.get('responseFormat.type').enable();
|
2025-07-08 12:53:27 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-14 10:52:06 +03:00
|
|
|
get getResponseFormatHint() {
|
|
|
|
|
return this.translate.instant(`rule-node-config.ai.response-format-hint-${this.aiConfigForm.get('responseFormat.type').value}`);
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|