Allow changing LLM Task preferences

pull/25779/head
Paulus Schoutsen 2025-06-13 11:31:22 -04:00
parent 67c7a3931f
commit 464a77cc98
5 changed files with 215 additions and 0 deletions

39
src/data/ai_task.ts Normal file
View File

@ -0,0 +1,39 @@
import type { HomeAssistant } from "../types";
export interface AITaskPreferences {
gen_text_summary_entity_id: string | null;
gen_text_generate_entity_id: string | null;
}
export interface GenTextTaskResult {
conversation_id: string;
result: string;
}
export const fetchAITaskPreferences = (hass: HomeAssistant) =>
hass.callWS<AITaskPreferences>({
type: "ai_task/preferences/get",
});
export const saveAITaskPreferences = (
hass: HomeAssistant,
preferences: Partial<AITaskPreferences>
) =>
hass.callWS<AITaskPreferences>({
type: "ai_task/preferences/set",
...preferences,
});
export const generateTextAITask = (
hass: HomeAssistant,
task: {
task_name: string;
entity_id?: string;
task_type: "summary" | "generate";
prompt: string;
}
) =>
hass.callWS<GenTextTaskResult>({
type: "ai_task/generate_text",
...task,
});

View File

@ -37,6 +37,7 @@ import {
mdiRoomService,
mdiScriptText,
mdiSpeakerMessage,
mdiStarFourPoints,
mdiThermostat,
mdiTimerOutline,
mdiToggleSwitch,
@ -66,6 +67,7 @@ export const DEFAULT_DOMAIN_ICON = mdiBookmark;
/** Fallback icons for each domain */
export const FALLBACK_DOMAIN_ICONS = {
ai_task: mdiStarFourPoints,
air_quality: mdiAirFilter,
alert: mdiAlert,
automation: mdiRobot,

View File

@ -0,0 +1,162 @@
import "@material/mwc-button";
import { mdiHelpCircle } from "@mdi/js";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import "../../../components/ha-card";
import "../../../components/ha-settings-row";
import "../../../components/entity/ha-entity-picker";
import type { HaEntityPicker } from "../../../components/entity/ha-entity-picker";
import type { HomeAssistant } from "../../../types";
import { brandsUrl } from "../../../util/brands-url";
import {
fetchAITaskPreferences,
saveAITaskPreferences,
type AITaskPreferences,
} from "../../../data/ai_task";
import { documentationUrl } from "../../../util/documentation-url";
@customElement("ai-task-pref")
export class AITaskPref extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;
@state() private _prefs?: AITaskPreferences;
protected firstUpdated(changedProps) {
super.firstUpdated(changedProps);
fetchAITaskPreferences(this.hass).then((prefs) => {
this._prefs = prefs;
});
}
protected render() {
if (!this._prefs) {
return nothing;
}
return html`
<ha-card outlined>
<h1 class="card-header">
<img
alt=""
src=${brandsUrl({
domain: "ai_task",
type: "icon",
darkOptimized: this.hass.themes?.darkMode,
})}
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>${this.hass.localize(
"ui.panel.config.voice_assistants.ai_task.header"
)}
</h1>
<div class="header-actions">
<a
href=${documentationUrl(this.hass, "/integrations/ai_task/")}
target="_blank"
rel="noreferrer"
class="icon-link"
>
<ha-icon-button
.label=${this.hass.localize(
"ui.panel.config.cloud.account.alexa.link_learn_how_it_works"
)}
.path=${mdiHelpCircle}
></ha-icon-button>
</a>
</div>
<div class="card-content">
<p>
${this.hass!.localize(
"ui.panel.config.voice_assistants.ai_task.description"
)}
</p>
<ha-settings-row narrow>
<span slot="heading">
${this.hass!.localize(
"ui.panel.config.voice_assistants.ai_task.gen_text_summary_header"
)}
</span>
<span slot="description">
${this.hass!.localize(
"ui.panel.config.voice_assistants.ai_task.gen_text_summary_description"
)}
</span>
<ha-entity-picker
data-name="gen_text_summary_entity_id"
.hass=${this.hass}
.value=${this._prefs.gen_text_summary_entity_id}
.includeDomains=${["ai_task"]}
@change=${this._handlePrefChange}
></ha-entity-picker>
</ha-settings-row>
<ha-settings-row narrow>
<span slot="heading">
${this.hass!.localize(
"ui.panel.config.voice_assistants.ai_task.gen_text_generate_header"
)}
</span>
<span slot="description">
${this.hass!.localize(
"ui.panel.config.voice_assistants.ai_task.gen_text_generate_description"
)}
</span>
<ha-entity-picker
data-name="gen_text_generate_entity_id"
.hass=${this.hass}
.value=${this._prefs.gen_text_generate_entity_id}
.includeDomains=${["ai_task"]}
@change=${this._handlePrefChange}
></ha-entity-picker>
</ha-settings-row>
</div>
</ha-card>
`;
}
private async _handlePrefChange(ev: CustomEvent) {
const input = ev.target as HaEntityPicker;
const key = input.getAttribute("data-name") as keyof AITaskPreferences;
const entityId = input.value || null;
const oldPrefs = this._prefs;
this._prefs = { ...this._prefs!, [key]: entityId };
try {
this._prefs = await saveAITaskPreferences(this.hass, {
[key]: entityId,
});
} catch (_err: any) {
this._prefs = oldPrefs;
}
}
static styles = css`
a {
color: var(--primary-color);
}
ha-settings-row {
padding: 0;
}
.header-actions {
position: absolute;
right: 0px;
inset-inline-end: 0px;
inset-inline-start: initial;
top: 24px;
display: flex;
flex-direction: row;
}
.header-actions .icon-link {
margin-top: -16px;
margin-right: 8px;
margin-inline-end: 8px;
margin-inline-start: initial;
direction: var(--direction);
color: var(--secondary-text-color);
}
`;
}
declare global {
interface HTMLElementTagNameMap {
"ai-task-pref": AITaskPref;
}
}

View File

@ -8,6 +8,7 @@ import "../../../layouts/hass-loading-screen";
import "../../../layouts/hass-tabs-subpage";
import type { HomeAssistant, Route } from "../../../types";
import "./assist-pref";
import "./ai-task-pref";
import "./cloud-alexa-pref";
import "./cloud-discover";
import "./cloud-google-pref";
@ -53,6 +54,9 @@ export class HaConfigVoiceAssistantsAssistants extends LitElement {
></assist-pref>
`
: nothing}
${isComponentLoaded(this.hass, "ai_task")
? html`<ai-task-pref .hass=${this.hass}></ai-task-pref>`
: nothing}
${this.cloudStatus?.logged_in
? html`
<cloud-alexa-pref

View File

@ -3433,6 +3433,14 @@
"sign_in": "Sign in"
}
},
"ai_task": {
"header": "AI Suggestions",
"description": "Home Assistant can use generative AI to help you with tasks like writing automations, creating scripts, and more.",
"gen_text_summary_header": "Summary tasks",
"gen_text_summary_description": "Used to summarize automations and generate names or labels.",
"gen_text_generate_header": "Generate tasks",
"gen_text_generate_description": "Used to generate automations or dashboards."
},
"debug": {
"header": "Debug assistant",
"no_runs_found": "No runs found",