@@ -32,9 +32,13 @@ pub struct DemoChat {
32
32
33
33
impl Widget for DemoChat {
34
34
fn handle_event ( & mut self , cx : & mut Cx , event : & Event , scope : & mut Scope ) {
35
+ let selector = self . bot_selector ( id ! ( selector) ) ;
36
+ let chat = self . chat ( id ! ( chat) ) ;
35
37
self . ui_runner ( ) . handle ( cx, event, scope, self ) ;
36
38
37
- self . chat ( id ! ( chat) ) . read_with ( |chat| {
39
+ // Let's override what happens when a user copies a message, for demonstration purposes.
40
+ // Hooking is only possible before `handle_event` is called on the child widget.
41
+ chat. read_with ( |chat| {
38
42
chat. hook ( event) . write_with ( |hook| {
39
43
let mut abort = false ;
40
44
@@ -59,95 +63,16 @@ impl Widget for DemoChat {
59
63
60
64
self . deref . handle_event ( cx, event, scope) ;
61
65
62
- let selector = self . bot_selector ( id ! ( selector) ) ;
63
- let chat = self . chat ( id ! ( chat) ) ;
64
-
65
- if let Event :: Startup = event {
66
- // TODO: Ensure syncrhonization on updates.
67
- let client = {
68
- let moly = MolyClient :: new ( "http://localhost:8085" . into ( ) ) ;
69
- let ollama = MolyClient :: new ( "http://localhost:11434" . into ( ) ) ;
70
-
71
- let openai_url = OPENAI_API_URL . unwrap_or ( "https://api.openai.com" ) ;
72
- let mut openai = MolyClient :: new ( openai_url. into ( ) ) ;
73
- openai. set_key ( OPEN_AI_KEY . unwrap_or ( "" ) ) ;
74
-
75
- let mut client = MultiClient :: new ( ) ;
76
- client. add_client ( Box :: new ( moly) ) ;
77
- client. add_client ( Box :: new ( ollama) ) ;
78
- client. add_client ( Box :: new ( openai) ) ;
79
- client
80
- } ;
81
-
82
- let mut repo: BotRepo = client. into ( ) ;
83
-
84
- chat. borrow_mut ( ) . unwrap ( ) . bot_repo = Some ( repo. clone ( ) ) ;
85
-
86
- let ui = self . ui_runner ( ) ;
87
- spawn ( async move {
88
- repo. load ( ) . await . expect ( "TODO: Handle loading better" ) ;
89
- ui. defer_with_redraw ( move |me, _cx, _scope| {
90
- let chat = me. chat ( id ! ( chat) ) ;
91
-
92
- let bots = repo
93
- . bots ( )
94
- . into_iter ( )
95
- . filter ( |b| {
96
- let openai_whitelist = [
97
- "gpt-4o" ,
98
- "gpt-4o-mini" ,
99
- "o1" ,
100
- "o1-preview" ,
101
- "o1-mini" ,
102
- "o3-mini" ,
103
- "o3-mini-high" ,
104
- ] ;
105
-
106
- let openrouter_whitelist = [
107
- "openai/gpt-4o" ,
108
- "openai/gpt-4o-mini" ,
109
- "openai/o1" ,
110
- "openai/o1-preview" ,
111
- "openai/o1-mini" ,
112
- "openai/o3-mini" ,
113
- "openai/o3-mini-high" ,
114
- "perplexity/sonar" ,
115
- "perplexity/sonar-reasoning" ,
116
- "perplexity/r1-1776" ,
117
- "openrouter/auto" ,
118
- "google/gemini-2.0-flash-001" ,
119
- "anthropic/claude-3.5-sonnet" ,
120
- "deepseek/deepseek-r1" ,
121
- ] ;
122
-
123
- let ollama_whitelist = [
124
- "deepseek-r1:1.5b" ,
125
- "deepseek-r1:8b" ,
126
- "llama3.1:8b" ,
127
- "llama3.2:latest" ,
128
- ] ;
129
-
130
- let siliconflow_whitelist = [
131
- "Pro/Qwen/Qwen2-1.5B-Instruct" ,
132
- "Pro/deepseek-ai/DeepSeek-R1" ,
133
- ] ;
134
-
135
- openai_whitelist
136
- . iter ( )
137
- . chain ( openrouter_whitelist. iter ( ) )
138
- . chain ( ollama_whitelist. iter ( ) )
139
- . chain ( siliconflow_whitelist. iter ( ) )
140
- . any ( |s| * s == b. id . as_str ( ) )
141
- } )
142
- . collect :: < Vec < _ > > ( ) ;
143
-
144
- chat. borrow_mut ( ) . unwrap ( ) . bot_id = Some ( bots. first ( ) . unwrap ( ) . id . clone ( ) ) ;
145
- me. bot_selector ( id ! ( selector) ) . set_bots ( bots) ;
146
-
147
- chat. borrow_mut ( ) . unwrap ( ) . visible = true ;
148
- } ) ;
66
+ // Let's log message updates for demonstration purposes.
67
+ // This will be something that already happened since we are doing this
68
+ // after `handle_event`.
69
+ chat. read_with ( |chat| {
70
+ chat. tasks ( event) . read_with ( |task| {
71
+ if let ChatTask :: UpdateMessage ( index, body) = task {
72
+ log ! ( "Message updated at index {}: {}" , index, body) ;
73
+ }
149
74
} ) ;
150
- }
75
+ } ) ;
151
76
152
77
let Event :: Actions ( actions) = event else {
153
78
return ;
@@ -164,4 +89,93 @@ impl Widget for DemoChat {
164
89
}
165
90
}
166
91
167
- impl LiveHook for DemoChat { }
92
+ impl LiveHook for DemoChat {
93
+ fn after_new_from_doc ( & mut self , _cx : & mut Cx ) {
94
+ let client = {
95
+ let moly = MolyClient :: new ( "http://localhost:8085" . into ( ) ) ;
96
+ let ollama = MolyClient :: new ( "http://localhost:11434" . into ( ) ) ;
97
+
98
+ let openai_url = OPENAI_API_URL . unwrap_or ( "https://api.openai.com" ) ;
99
+ let mut openai = MolyClient :: new ( openai_url. into ( ) ) ;
100
+ openai. set_key ( OPEN_AI_KEY . unwrap_or ( "" ) ) ;
101
+
102
+ let mut client = MultiClient :: new ( ) ;
103
+ client. add_client ( Box :: new ( moly) ) ;
104
+ client. add_client ( Box :: new ( ollama) ) ;
105
+ client. add_client ( Box :: new ( openai) ) ;
106
+ client
107
+ } ;
108
+
109
+ let mut repo: BotRepo = client. into ( ) ;
110
+ self . chat ( id ! ( chat) ) . write ( ) . bot_repo = Some ( repo. clone ( ) ) ;
111
+
112
+ let ui = self . ui_runner ( ) ;
113
+ spawn ( async move {
114
+ repo. load ( ) . await . expect ( "TODO: Handle loading better" ) ;
115
+ ui. defer_with_redraw ( move |me, _cx, _scope| {
116
+ me. fill_selector ( repo. bots ( ) ) ;
117
+ me. chat ( id ! ( chat) ) . write ( ) . visible = true ;
118
+ } ) ;
119
+ } ) ;
120
+ }
121
+ }
122
+
123
+ impl DemoChat {
124
+ fn fill_selector ( & mut self , bots : Vec < Bot > ) {
125
+ let chat = self . chat ( id ! ( chat) ) ;
126
+
127
+ let bots = bots
128
+ . into_iter ( )
129
+ . filter ( |b| {
130
+ let openai_whitelist = [
131
+ "gpt-4o" ,
132
+ "gpt-4o-mini" ,
133
+ "o1" ,
134
+ "o1-preview" ,
135
+ "o1-mini" ,
136
+ "o3-mini" ,
137
+ "o3-mini-high" ,
138
+ ] ;
139
+
140
+ let openrouter_whitelist = [
141
+ "openai/gpt-4o" ,
142
+ "openai/gpt-4o-mini" ,
143
+ "openai/o1" ,
144
+ "openai/o1-preview" ,
145
+ "openai/o1-mini" ,
146
+ "openai/o3-mini" ,
147
+ "openai/o3-mini-high" ,
148
+ "perplexity/sonar" ,
149
+ "perplexity/sonar-reasoning" ,
150
+ "perplexity/r1-1776" ,
151
+ "openrouter/auto" ,
152
+ "google/gemini-2.0-flash-001" ,
153
+ "anthropic/claude-3.5-sonnet" ,
154
+ "deepseek/deepseek-r1" ,
155
+ ] ;
156
+
157
+ let ollama_whitelist = [
158
+ "deepseek-r1:1.5b" ,
159
+ "deepseek-r1:8b" ,
160
+ "llama3.1:8b" ,
161
+ "llama3.2:latest" ,
162
+ ] ;
163
+
164
+ let siliconflow_whitelist = [
165
+ "Pro/Qwen/Qwen2-1.5B-Instruct" ,
166
+ "Pro/deepseek-ai/DeepSeek-R1" ,
167
+ ] ;
168
+
169
+ openai_whitelist
170
+ . iter ( )
171
+ . chain ( openrouter_whitelist. iter ( ) )
172
+ . chain ( ollama_whitelist. iter ( ) )
173
+ . chain ( siliconflow_whitelist. iter ( ) )
174
+ . any ( |s| * s == b. id . as_str ( ) )
175
+ } )
176
+ . collect :: < Vec < _ > > ( ) ;
177
+
178
+ chat. borrow_mut ( ) . unwrap ( ) . bot_id = Some ( bots. first ( ) . unwrap ( ) . id . clone ( ) ) ;
179
+ self . bot_selector ( id ! ( selector) ) . set_bots ( bots) ;
180
+ }
181
+ }
0 commit comments