Skip to content

Commit 6944710

Browse files
authored
Fix model picker display in ct-prompt-input (commontoolsinc#2064)
* Fix model picker display in `ct-prompt-input` * Format * Fix `generateObject` 400 error * Handle `undefined` case
1 parent 97e6aea commit 6944710

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

packages/patterns/chatbot.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,20 @@ type ChatOutput = {
152152
export const TitleGenerator = recipe<
153153
{ model?: string; messages: Array<BuiltInLLMMessage> }
154154
>("Title Generator", ({ model, messages }) => {
155-
const titleMessages = computed(() => {
155+
const previewMessage = computed(() => {
156156
if (!messages || messages.length === 0) return "";
157157

158-
const messageCount = 2;
159-
const selectedMessages = messages.slice(0, messageCount).filter(Boolean);
158+
const firstMessage = messages[0];
160159

161-
if (selectedMessages.length === 0) return "";
160+
if (!firstMessage) return "";
162161

163-
return selectedMessages;
162+
return JSON.stringify(firstMessage);
164163
});
165164

166165
const { result } = generateObject({
167166
system:
168167
"Generate at most a 3-word title based on the following content, respond with NOTHING but the literal title text.",
169-
messages: titleMessages,
168+
prompt: previewMessage,
170169
model,
171170
schema: {
172171
type: "object",

packages/ui/src/v2/components/ct-prompt-input/ct-prompt-input.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ export class CTPromptInput extends BaseElement {
343343
declare theme?: CTTheme;
344344

345345
private _textareaElement?: HTMLElement;
346+
private _modelSelectElement?: HTMLSelectElement;
346347

347348
// Attachment management
348349
private attachments: Map<string, PromptAttachment> = new Map();
@@ -415,7 +416,11 @@ export class CTPromptInput extends BaseElement {
415416
this._textareaElement = this.shadowRoot?.querySelector(
416417
"textarea",
417418
) as HTMLTextAreaElement;
419+
this._modelSelectElement = this.shadowRoot?.querySelector(
420+
".model-select",
421+
) as HTMLSelectElement;
418422
this._updateThemeProperties();
423+
this._applyModelValueToDom();
419424
}
420425

421426
override updated(
@@ -438,6 +443,12 @@ export class CTPromptInput extends BaseElement {
438443
if (changedProperties.has("model") && this.model != null) {
439444
this._modelController.bind(this.model);
440445
}
446+
if (
447+
changedProperties.has("model") ||
448+
changedProperties.has("modelItems")
449+
) {
450+
this._applyModelValueToDom();
451+
}
441452

442453
// Manage mentions overlay based on controller state
443454
// The MentionController will trigger requestUpdate when state changes
@@ -737,7 +748,6 @@ export class CTPromptInput extends BaseElement {
737748
? html`
738749
<select
739750
class="model-select"
740-
.value="${this._modelController.getValue() || ""}"
741751
@change="${this._handleModelChange}"
742752
?disabled="${this.disabled || this.pending}"
743753
title="Select model"
@@ -905,6 +915,26 @@ export class CTPromptInput extends BaseElement {
905915
}
906916
}
907917

918+
/**
919+
* Apply the current model value to the DOM select element
920+
* This ensures the select element shows the correct selected option
921+
*/
922+
private _applyModelValueToDom() {
923+
// Re-query if we don't have a reference (e.g., model picker appeared after first render)
924+
if (!this._modelSelectElement) {
925+
this._modelSelectElement = this.shadowRoot?.querySelector(
926+
".model-select",
927+
) as HTMLSelectElement;
928+
}
929+
930+
if (!this._modelSelectElement) return;
931+
932+
const currentValue = this._modelController.getValue();
933+
if (currentValue != null) {
934+
this._modelSelectElement.value = String(currentValue);
935+
}
936+
}
937+
908938
/**
909939
* Mount the mentions overlay in the document body
910940
*/

0 commit comments

Comments
 (0)