1import {
2 ChangeDetectionStrategy,
3 Component,
4 computed,
5 inject,
6 Injector,
7 signal,
8} from "@angular/core";
9import { ActivatedRoute } from "@angular/router";
10import { connectAgentContext, CopilotKit } from "@copilotkit/angular";
11
12import { agentIdForCurrentIntegration } from "../../feature-agent";
13import { FeatureHeaderComponent } from "../feature-header.component";
14import { ShowcaseChatHostComponent } from "../showcase-chat-host.component";
15import {
16 AgentConfigCardComponent,
17 AuthCardComponent,
18 DEFAULT_AGENT_CONFIG,
19} from "./app-settings-cards";
20import type {
21 AgentConfig,
22 Expertise,
23 ResponseLength,
24 Tone,
25} from "./app-settings-cards";
26
27const DEMO_AUTH_HEADERS: Readonly<Record<string, string>> = {
28 Authorization: "Bearer demo-token-123",
29};
30
31@Component({
32 selector: "showcase-app-settings-feature",
33 imports: [
34 AgentConfigCardComponent,
35 AuthCardComponent,
36 FeatureHeaderComponent,
37 ShowcaseChatHostComponent,
38 ],
39 changeDetection: ChangeDetectionStrategy.OnPush,
40 host: { class: "feature-page" },
41 template: `
42 <showcase-feature-header />
43 @if (feature === "auth" && !signedIn()) {
44 <main class="sign-in-page">
45 <showcase-auth-card [authenticated]="false" (signIn)="signIn()" />
46 </main>
47 } @else {
48 <main class="settings-page">
49 <section class="settings-panel">
50 @if (feature === "auth") {
51 <showcase-auth-card [authenticated]="true" (signOut)="signOut()" />
52 } @else {
53 <showcase-agent-config-card
54 [config]="config()"
55 (toneChange)="setTone($event)"
56 (expertiseChange)="setExpertise($event)"
57 (responseLengthChange)="setResponseLength($event)"
58 />
59 }
60 </section>
61 <section class="chat-surface" aria-label="CopilotKit assistant">
62 <showcase-chat-host
63 [agentId]="agentId"
64 [headers]="feature === 'auth' ? authHeaders : undefined"
65 />
66 </section>
67 </main>
68 }
69 `,
70 styles: `
71 .sign-in-page {
72 display: grid;
73 min-height: 0;
74 place-items: center;
75 padding: 1.5rem;
76 background: radial-gradient(
77 circle at 50% 20%,
78 light-dark(#eef2ff, rgb(30 27 75 / 40%)),
79 light-dark(#eef3f7, #0a0a0a) 55%
80 );
81 }
82 .settings-page {
83 display: grid;
84 min-height: 0;
85 grid-template-rows: auto minmax(0, 1fr);
86 gap: 1rem;
87 padding: 1rem;
88 background: light-dark(#eef3f7, #0a0a0a);
89 }
90 .settings-panel {
91 min-width: 0;
92 }
93 .chat-surface {
94 min-height: 0;
95 overflow: hidden;
96 border: 1px solid light-dark(#d8e0ea, #303136);
97 border-radius: 1rem;
98 background: light-dark(#fff, #191a1e);
99 }
100 `,
101})
102export class AppSettingsFeatureComponent {
103 private readonly route = inject(ActivatedRoute);
104 private readonly injector = inject(Injector);
105 protected readonly feature =
106 (this.route.snapshot.data["feature"] as string | undefined) ??
107 "agent-config";
108 protected readonly signedIn = signal(false);
109 protected readonly config = signal<AgentConfig>({ ...DEFAULT_AGENT_CONFIG });
110 protected readonly authHeaders = DEMO_AUTH_HEADERS;
111 protected readonly agentId = agentIdForCurrentIntegration(this.feature);
112 private readonly configContext = computed(() => ({
113 description:
114 "Agent response preferences. Apply tone, expertise level, and response length to every reply.",
115 value: JSON.stringify(this.config()),
116 }));
117
118 constructor() {
119 if (this.feature === "agent-config") {
120 connectAgentContext(this.configContext);
121 }
122 }
123
124 protected signIn(): void {
125 this.signedIn.set(true);
126 }
127
128 protected signOut(): void {
129 this.injector.get(CopilotKit).updateRuntime({ headers: {} });
130 this.signedIn.set(false);
131 }
132
133 protected setTone(tone: Tone): void {
134 this.updateConfig({ tone });
135 }
136
137 protected setExpertise(expertise: Expertise): void {
138 this.updateConfig({ expertise });
139 }
140
141 protected setResponseLength(responseLength: ResponseLength): void {
142 this.updateConfig({ responseLength });
143 }
144
145 private updateConfig(patch: Partial<AgentConfig>): void {
146 this.config.update((current) => ({ ...current, ...patch }));
147 }
148}
149