/// import { Cell, computed, Default, derive, generateText, handler, NAME, pattern, UI, wish, Writable, } from "commontools"; type Input = { title?: Default; }; const handleSend = handler< { detail: { message: string } }, { topic: Writable } >((event, { topic }) => { const userTopic = event.detail?.message?.trim(); if (userTopic) { topic.set(userTopic); } }); export default pattern(({ title }) => { const topic = Writable.of(""); const profile = wish>({ query: "#profile" }); const systemPrompt = computed(() => { const profileText = profile.result.get(); const profileSection = profileText ? `\n\n--- About the User ---\n${profileText}\n---\n` : ""; return `You are a helpful writing assistant.${profileSection} Write content personalized to the user when appropriate.`; }); const result = generateText({ system: systemPrompt, prompt: topic, }); return { [NAME]: title, [UI]: ( {title} Profile Context: {derive(topic, (t) => t ? ( Topic: {t} ) : null)} {derive( [result.pending, result.result], ([pending, r]) => pending ? ( {" "} Generating personalized content... ) : r ? ( Generated Text: {r} ) : null, )} ), topic, response: result.result, }; });
{t}