|
2 | 2 | import { |
3 | 3 | Cell, |
4 | 4 | computed, |
5 | | - Default, |
6 | 5 | derive, |
7 | 6 | generateObject, |
8 | | - ifElse, |
9 | | - NAME, |
10 | 7 | pattern, |
11 | 8 | patternTool, |
12 | 9 | toSchema, |
13 | 10 | UI, |
| 11 | + type WishState, |
14 | 12 | } from "commontools"; |
15 | 13 | import { fetchAndRunPattern, listPatternIndex } from "./common-tools.tsx"; |
16 | 14 |
|
17 | | -export const Suggestion = pattern( |
18 | | - ( |
19 | | - { situation, context }: { |
20 | | - situation: string; |
21 | | - context: { [id: string]: any }; |
| 15 | +export default pattern< |
| 16 | + { situation: string; context: { [id: string]: any } }, |
| 17 | + WishState<Cell<any>> |
| 18 | +>(({ situation, context }) => { |
| 19 | + const suggestion = generateObject({ |
| 20 | + system: "Find a useful pattern, run it, pass link to final result", |
| 21 | + prompt: situation, |
| 22 | + context, |
| 23 | + tools: { |
| 24 | + fetchAndRunPattern: patternTool(fetchAndRunPattern), |
| 25 | + listPatternIndex: patternTool(listPatternIndex), |
22 | 26 | }, |
23 | | - ) => { |
24 | | - const suggestion = generateObject({ |
25 | | - system: "Find a useful pattern, run it, pass link to final result", |
26 | | - prompt: situation, |
27 | | - context, |
28 | | - tools: { |
29 | | - fetchAndRunPattern: patternTool(fetchAndRunPattern), |
30 | | - listPatternIndex: patternTool(listPatternIndex), |
31 | | - }, |
32 | | - model: "anthropic:claude-haiku-4-5", |
33 | | - schema: toSchema<{ cell: Cell<any> }>(), |
34 | | - }); |
| 27 | + model: "anthropic:claude-haiku-4-5", |
| 28 | + schema: toSchema<{ cell: Cell<any> }>(), |
| 29 | + }); |
35 | 30 |
|
36 | | - return ifElse( |
37 | | - computed(() => suggestion.pending && !suggestion.result), |
38 | | - undefined, |
39 | | - suggestion.result, |
40 | | - ); |
41 | | - }, |
42 | | -); |
| 31 | + const result = computed(() => suggestion.result?.cell); |
43 | 32 |
|
44 | | -export default pattern<{ title: Default<string, "Suggestion Tester"> }>( |
45 | | - ({ title }) => { |
46 | | - const suggestion = Suggestion({ |
47 | | - situation: "gimme counter plz", |
48 | | - context: {}, |
49 | | - }); |
50 | | - |
51 | | - const suggestion2 = Suggestion({ |
52 | | - situation: "gimme note with the attached content", |
53 | | - context: { |
54 | | - content: "This is the expected content", |
55 | | - value: Cell.of(0), |
56 | | - }, |
57 | | - }); |
58 | | - |
59 | | - return { |
60 | | - [NAME]: title, |
61 | | - [UI]: ( |
62 | | - <div> |
63 | | - <h1>Suggestion Tester</h1> |
64 | | - <h2>Counter</h2> |
65 | | - <ct-cell-context $cell={suggestion} label="Counter Suggestion"> |
66 | | - {derive(suggestion, (s) => { |
67 | | - return s?.cell ?? "waiting..."; |
68 | | - })} |
69 | | - </ct-cell-context> |
70 | | - |
71 | | - <h2>Note</h2> |
72 | | - <ct-cell-context $cell={suggestion2} label="Note Suggestion"> |
73 | | - {derive(suggestion2, (s) => { |
74 | | - return s?.cell ?? "waiting.."; |
75 | | - })} |
76 | | - </ct-cell-context> |
77 | | - </div> |
78 | | - ), |
79 | | - suggestion, |
80 | | - }; |
81 | | - }, |
82 | | -); |
| 33 | + return { |
| 34 | + result, |
| 35 | + [UI]: ( |
| 36 | + <ct-cell-context $cell={result}> |
| 37 | + {derive(result, (r) => r ?? "Searching...")} |
| 38 | + </ct-cell-context> |
| 39 | + ), |
| 40 | + }; |
| 41 | +}); |
0 commit comments