Skip to content

Commit f4fb95a

Browse files
authored
Fix spawnPattern data handling [CT-910] (commontoolsinc#1796)
* Fix `spawnPattern` data handling * Clean up Charm types
1 parent 7d2c48e commit f4fb95a

File tree

2 files changed

+87
-45
lines changed

2 files changed

+87
-45
lines changed

packages/patterns/chatbot-note.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import {
2626

2727
import Chat from "./chatbot.tsx";
2828

29-
type Charm = {
29+
export type MentionableCharm = {
3030
[NAME]: string;
3131
content?: string;
32-
mentioned?: Charm[];
32+
mentioned?: MentionableCharm[];
3333
};
3434

3535
type NoteResult = {
@@ -38,13 +38,13 @@ type NoteResult = {
3838

3939
export type NoteInput = {
4040
content: Default<string, "">;
41-
allCharms: Cell<Charm[]>;
41+
allCharms: Cell<MentionableCharm[]>;
4242
};
4343

4444
const handleCharmLinkClick = handler<
4545
{
4646
detail: {
47-
charm: Cell<Charm>;
47+
charm: Cell<MentionableCharm>;
4848
};
4949
},
5050
Record<string, never>
@@ -72,7 +72,7 @@ export const Note = recipe<NoteInput>(
7272
);
7373

7474
const handleCharmLinkClicked = handler(
75-
(_: any, { charm }: { charm: Cell<Charm> }) => {
75+
(_: any, { charm }: { charm: Cell<MentionableCharm> }) => {
7676
return navigateTo(charm);
7777
},
7878
);
@@ -82,13 +82,13 @@ type LLMTestInput = {
8282
messages: Default<Array<BuiltInLLMMessage>, []>;
8383
expandChat: Default<boolean, false>;
8484
content: Default<string, "">;
85-
allCharms: Cell<Charm[]>;
85+
allCharms: Cell<MentionableCharm[]>;
8686
};
8787

8888
type LLMTestResult = {
8989
messages: Default<Array<BuiltInLLMMessage>, []>;
90-
mentioned: Default<Array<Charm>, []>;
91-
backlinks: Default<Array<Charm>, []>;
90+
mentioned: Default<Array<MentionableCharm>, []>;
91+
backlinks: Default<Array<MentionableCharm>, []>;
9292
content: Default<string, "">;
9393
};
9494

@@ -163,7 +163,7 @@ export default recipe<LLMTestInput, LLMTestResult>(
163163
const chat = Chat({ messages, tools });
164164
const { addMessage, cancelGeneration, pending } = chat;
165165

166-
const mentioned = cell<Charm[]>([]);
166+
const mentioned = cell<MentionableCharm[]>([]);
167167

168168
// Must use JSONSchema here, CTS doesn't work correctly. See CT-901
169169
const computeBacklinks = lift(
@@ -179,7 +179,7 @@ export default recipe<LLMTestInput, LLMTestResult>(
179179
items: { type: "object" },
180180
} as JSONSchema,
181181
({ allCharms, content }) => {
182-
const cs: Charm[] = allCharms.get();
182+
const cs: MentionableCharm[] = allCharms.get();
183183
if (!cs) return [];
184184

185185
const self = cs.find((c) => c.content === content.get());
@@ -194,7 +194,7 @@ export default recipe<LLMTestInput, LLMTestResult>(
194194
},
195195
);
196196

197-
const backlinks: OpaqueRef<Charm[]> = computeBacklinks({
197+
const backlinks: OpaqueRef<MentionableCharm[]> = computeBacklinks({
198198
allCharms,
199199
content,
200200
});
@@ -230,7 +230,7 @@ export default recipe<LLMTestInput, LLMTestResult>(
230230
<details>
231231
<summary>Mentioned Charms</summary>
232232
<ct-vstack>
233-
{mentioned.map((charm: Charm) => (
233+
{mentioned.map((charm: MentionableCharm) => (
234234
<ct-button onClick={handleCharmLinkClicked({ charm })}>
235235
{charm[NAME]}
236236
</ct-button>
@@ -240,7 +240,7 @@ export default recipe<LLMTestInput, LLMTestResult>(
240240
<details>
241241
<summary>Backlinks</summary>
242242
<ct-vstack>
243-
{backlinks.map((charm: Charm) => (
243+
{backlinks.map((charm: MentionableCharm) => (
244244
<ct-button onClick={handleCharmLinkClicked({ charm })}>
245245
{charm[NAME]}
246246
</ct-button>

packages/patterns/default-app.tsx

Lines changed: 74 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
handler,
88
NAME,
99
navigateTo,
10+
OpaqueRef,
1011
recipe,
1112
str,
1213
UI,
@@ -16,8 +17,11 @@ import {
1617
import Chatbot from "./chatbot.tsx";
1718
import ChatbotTools from "./chatbot-tools.tsx";
1819
import ChatbotOutliner from "./chatbot-outliner.tsx";
19-
import ChatbotNote from "./chatbot-note.tsx";
20-
import Note from "./note.tsx";
20+
import {
21+
default as ChatbotNote,
22+
type MentionableCharm,
23+
} from "./chatbot-note.tsx";
24+
import { default as Note } from "./note.tsx";
2125

2226
export type Charm = {
2327
[NAME]?: string;
@@ -61,11 +65,64 @@ const removeCharm = handler<
6165
}
6266
});
6367

64-
const spawnPattern = (recipe: any, params: any) =>
65-
handler<Record<string, never>, Record<string, never>>((event, state) => {
66-
const charm = recipe(params);
67-
return navigateTo(charm);
68-
});
68+
const spawnChatbot = handler<
69+
Record<string, never>,
70+
Record<string, never>
71+
>((_, state) => {
72+
return navigateTo(Chatbot({
73+
messages: [],
74+
tools: undefined,
75+
}));
76+
});
77+
78+
const spawnChatbotTools = handler<
79+
Record<string, never>,
80+
Record<string, never>
81+
>((_, state) => {
82+
return navigateTo(ChatbotTools({
83+
title: "Chatbot Tools",
84+
messages: [],
85+
list: [],
86+
}));
87+
});
88+
89+
const spawnChatbotOutliner = handler<
90+
Record<string, never>,
91+
{ allCharms: Cell<Charm[]> }
92+
>((_, state) => {
93+
return navigateTo(ChatbotOutliner({
94+
title: "Chatbot Outliner",
95+
expandChat: false,
96+
messages: [],
97+
outline: {
98+
root: { body: "", children: [], attachments: [] },
99+
},
100+
allCharms: state.allCharms,
101+
}));
102+
});
103+
104+
const spawnChatbotNote = handler<
105+
Record<string, never>,
106+
{ allCharms: Cell<MentionableCharm[]> }
107+
>((_, state) => {
108+
return navigateTo(ChatbotNote({
109+
title: "New Note",
110+
content: "",
111+
expandChat: false,
112+
messages: [],
113+
allCharms: state.allCharms,
114+
}));
115+
});
116+
117+
const spawnNote = handler<
118+
Record<string, never>,
119+
Record<string, never>
120+
>((_, state) => {
121+
return navigateTo(Note({
122+
title: "New Note",
123+
content: "",
124+
}));
125+
});
69126

70127
export default recipe<CharmsListInput, CharmsListOutput>(
71128
"DefaultCharmList",
@@ -79,47 +136,32 @@ export default recipe<CharmsListInput, CharmsListOutput>(
79136
<ct-hstack gap="2" align="center">
80137
<h3>Quicklaunch:</h3>
81138
<ct-button
82-
onClick={spawnPattern(Chatbot, {
83-
messages: [],
84-
tools: undefined,
85-
})({})}
139+
onClick={spawnChatbot({})}
86140
>
87141
💬 Chatbot
88142
</ct-button>
89143
<ct-button
90-
onClick={spawnPattern(ChatbotTools, {
91-
title: "Chatbot Tools",
92-
chat: [],
93-
list: [],
94-
})({})}
144+
onClick={spawnChatbotTools({})}
95145
>
96146
🔧 Chatbot Tools
97147
</ct-button>
98148
<ct-button
99-
onClick={spawnPattern(ChatbotOutliner, {
100-
outline: {
101-
root: { body: "", children: [], attachments: [] },
102-
},
103-
allCharms,
104-
})({})}
149+
onClick={spawnChatbotOutliner({ allCharms })}
105150
>
106151
📝 Chatbot Outliner
107152
</ct-button>
108153
<ct-button
109-
onClick={spawnPattern(ChatbotNote, {
110-
title: "New Note",
111-
content: "",
112-
allCharms,
113-
})({})}
154+
onClick={spawnChatbotNote({
155+
// slight disagreement between Charm types but they are compatible
156+
allCharms: allCharms as unknown as OpaqueRef<
157+
MentionableCharm[]
158+
>,
159+
})}
114160
>
115161
🤖 Chatbot Note
116162
</ct-button>
117163
<ct-button
118-
onClick={spawnPattern(Note, {
119-
title: "New Note",
120-
content: "",
121-
allCharms,
122-
})({})}
164+
onClick={spawnNote({})}
123165
>
124166
📄 Note
125167
</ct-button>

0 commit comments

Comments
 (0)