Define better interface

This commit is contained in:
Paulus Schoutsen 2025-07-12 07:16:52 +00:00
parent 5ee2055a72
commit ceef53581b
2 changed files with 52 additions and 38 deletions

View File

@ -21,6 +21,11 @@ declare global {
} }
} }
export interface SuggestWithAIGenerateTask {
type: "data";
task: GenDataTask;
}
@customElement("ha-suggest-with-ai-button") @customElement("ha-suggest-with-ai-button")
export class HaSuggestWithAIButton extends LitElement { export class HaSuggestWithAIButton extends LitElement {
@property({ attribute: false }) @property({ attribute: false })
@ -30,7 +35,7 @@ export class HaSuggestWithAIButton extends LitElement {
public taskType!: "data"; public taskType!: "data";
@property({ attribute: false }) @property({ attribute: false })
generateTask!: () => GenDataTask; generateTask!: () => SuggestWithAIGenerateTask;
@state() @state()
private _aiPrefs?: AITaskPreferences; private _aiPrefs?: AITaskPreferences;
@ -71,8 +76,13 @@ export class HaSuggestWithAIButton extends LitElement {
} }
try { try {
this._suggesting = true; this._suggesting = true;
const task = await this.generateTask(); const info = await this.generateTask();
const result = await generateDataAITask(this.hass, task); let result: GenDataTaskResult;
if (info.type === "data") {
result = await generateDataAITask(this.hass, info.task);
} else {
throw new Error("Unsupported task type");
}
fireEvent(this, "suggestion", result); fireEvent(this, "suggestion", result);
} finally { } finally {
this._suggesting = false; this._suggesting = false;

View File

@ -13,6 +13,7 @@ import "../../../../components/ha-textarea";
import "../../../../components/ha-textfield"; import "../../../../components/ha-textfield";
import "../../../../components/ha-labels-picker"; import "../../../../components/ha-labels-picker";
import "../../../../components/ha-suggest-with-ai-button"; import "../../../../components/ha-suggest-with-ai-button";
import type { SuggestWithAIGenerateTask } from "../../../../components/ha-suggest-with-ai-button";
import "../../category/ha-category-picker"; import "../../category/ha-category-picker";
import "../../../../components/ha-expansion-panel"; import "../../../../components/ha-expansion-panel";
import "../../../../components/chips/ha-chip-set"; import "../../../../components/chips/ha-chip-set";
@ -27,7 +28,7 @@ import type {
SaveDialogParams, SaveDialogParams,
} from "./show-dialog-automation-save"; } from "./show-dialog-automation-save";
import { supportsMarkdownHelper } from "../../../../common/translations/markdown_support"; import { supportsMarkdownHelper } from "../../../../common/translations/markdown_support";
import type { GenDataTask, GenDataTaskResult } from "../../../../data/ai_task"; import type { GenDataTaskResult } from "../../../../data/ai_task";
import { computeStateDomain } from "../../../../common/entity/compute_state_domain"; import { computeStateDomain } from "../../../../common/entity/compute_state_domain";
import { subscribeOne } from "../../../../common/util/subscribe-one"; import { subscribeOne } from "../../../../common/util/subscribe-one";
import { subscribeLabelRegistry } from "../../../../data/label_registry"; import { subscribeLabelRegistry } from "../../../../data/label_registry";
@ -342,7 +343,7 @@ class DialogAutomationSave extends LitElement implements HassDialog {
]); ]);
} }
private _generateTask = async (): Promise<GenDataTask> => { private _generateTask = async (): Promise<SuggestWithAIGenerateTask> => {
const [labels, entities, categories] = await this._getSuggestData(); const [labels, entities, categories] = await this._getSuggestData();
const automationInspiration: string[] = []; const automationInspiration: string[] = [];
@ -374,8 +375,10 @@ class DialogAutomationSave extends LitElement implements HassDialog {
} }
return { return {
task_name: "frontend:automation:save", type: "data",
instructions: `Suggest in language "${this.hass.language}" a name, description, category and labels for the following Home Assistant automation. task: {
task_name: "frontend:automation:save",
instructions: `Suggest in language "${this.hass.language}" a name, description, category and labels for the following Home Assistant automation.
The name should be relevant to the automation's purpose. The name should be relevant to the automation's purpose.
${ ${
@ -394,39 +397,40 @@ The automation configuration is as follows:
${dump(this._params.config)} ${dump(this._params.config)}
`, `,
structure: { structure: {
name: { name: {
description: "The name of the automation", description: "The name of the automation",
required: true, required: true,
selector: { selector: {
text: {}, text: {},
},
},
description: {
description: "A short description of the automation",
required: false,
selector: {
text: {},
},
},
labels: {
description: "Labels for the automation",
required: false,
selector: {
text: {
multiple: true,
}, },
}, },
}, description: {
category: { description: "A short description of the automation",
description: "The category of the automation", required: false,
required: false, selector: {
selector: { text: {},
select: { },
options: Object.entries(categories).map(([id, name]) => ({ },
value: id, labels: {
label: name, description: "Labels for the automation",
})), required: false,
selector: {
text: {
multiple: true,
},
},
},
category: {
description: "The category of the automation",
required: false,
selector: {
select: {
options: Object.entries(categories).map(([id, name]) => ({
value: id,
label: name,
})),
},
}, },
}, },
}, },