-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.cirru
613 lines (612 loc) · 31.5 KB
/
compact.cirru
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
{} (:package |app)
:configs $ {} (:init-fn |app.main/main!) (:reload-fn |app.main/reload!) (:version |0.0.1)
:modules $ [] |respo.calcit/ |lilac/ |memof/ |respo-ui.calcit/ |reel.calcit/ |respo-markdown.calcit/ |alerts.calcit/
:entries $ {}
:files $ {}
|app.comp.container $ %{} :FileEntry
:defs $ {}
|*abort-control $ %{} :CodeEntry (:doc |)
:code $ quote (defatom *abort-control nil)
|*gen-ai $ %{} :CodeEntry (:doc |)
:code $ quote (defatom *gen-ai nil)
|call-anthropic-msg! $ %{} :CodeEntry (:doc |)
:code $ quote
defn call-anthropic-msg! (cursor state prompt-text model thinking? d!) (hint-fn async)
if-let
abort $ deref *abort-control
do (js/console.warn "\"Aborting prev") (.!abort abort)
d! $ :: :states cursor
-> state (assoc :answer nil) (assoc :loading? true)
d! $ :: :change-model
let
selected $ js-await (get-selected)
content $ .replace prompt-text "\"{{selected}}" (or selected "\"<未找到内容>")
result $ js-await
.!post axios (str "\"https://sa.chenyong.life/v1/messages")
js-object
:model $ get-env "\"claude-model" (or model "\"claude-3-5-sonnet-latest")
:max_tokens 1024
:stream true
:thinking $ if thinking?
js-object (:type "\"enabled") (:budget_tokens 2000)
, js/undefined
:messages $ js-array
js-object (:role "\"user") (:content content)
js-object
:params $ js-object
:headers $ js-object (; :Accept "\"text/event-stream") (; :Content-Type "\"application/json")
"\"x-api-key" $ get-anthropic-key!
"\"anthropic-version" "\"2023-06-01"
"\"anthropic-dangerous-direct-browser-access" true
:responseType "\"stream"
:adapter "\"fetch"
:signal $ let
abort $ new js/AbortController
reset! *abort-control abort
.-signal abort
stream $ .-data result
reader $ ->
.!pipeThrough stream $ new js/TextDecoderStream
.!getReader
*text $ atom (str "\"Claude AI:" &newline &newline)
; reading $ js-await (.!read reader)
; answer $ -> result .-data .-candidates .-0 .-content .-parts .-0 .-text
; d! $ :: :states cursor
-> state
assoc :answer $ w-log answer
assoc :loading? false
apply-args () $ fn () (hint-fn async)
let
info $ js-await (.!read reader)
value $ wo-js-log (.-value info)
done? $ .-done info
; js/console.log "\"VALUE" info
if (wo-log done?) (:: :unit)
do (; println "\"processing")
let
events $ -> value .split-lines
filter $ fn (s) (.starts-with? s "\"data: ")
map $ fn (s)
-> (.strip-prefix s "\"data: ") js/JSON.parse to-calcit-data
apply-args (events)
fn (xs)
list-match xs
() $ ;nil println "\"no thing to handle in this Loop"
(x0 xss)
let
stop? $ = (get x0 "\"type") "\"message_stop"
wo-js-log x0
if stop?
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? true)
let
content $ get-in x0 ([] "\"delta" "\"text")
if (nil? content)
do
;nil d! $ :: :states cursor
-> state
assoc :answer $ str @*text &newline "\"[STOPPED: " (.-finishReason candidate0) "\"]"
assoc :loading? false
assoc :done? true
println "\"content is nil"
recur xss
let () (swap! *text str content)
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? false)
recur xss
recur
|call-deepinfra-msg! $ %{} :CodeEntry (:doc |)
:code $ quote
defn call-deepinfra-msg! (cursor state prompt-text d! *text) (hint-fn async)
if-let
abort $ deref *abort-control
do (js/console.warn "\"Aborting prev") (.!abort abort)
d! $ :: :states cursor
-> state (assoc :answer nil) (assoc :loading? true)
let
selected $ js-await (get-selected)
content $ .replace prompt-text "\"{{selected}}" (or selected "\"<未找到内容>")
result $ js-await
.!post axios "\"https://api.deepinfra.com/v1/openai/chat/completions"
js-object (:model "\"nvidia/Llama-3.1-Nemotron-70B-Instruct") (:stream true)
:messages $ js-array
js-object (:role "\"user") (:content content)
js-object
:params $ js-object
:headers $ js-object (:Content-Type "\"application/json")
:Authorization $ str-spaced "\"Bearer" (get-deepinfra-key!)
:responseType "\"stream"
:adapter "\"fetch"
:signal $ let
abort $ new js/AbortController
reset! *abort-control abort
.-signal abort
stream $ .-data result
reader $ ->
.!pipeThrough stream $ new js/TextDecoderStream
.!getReader
; reading $ js-await (.!read reader)
; answer $ -> result .-data .-candidates .-0 .-content .-parts .-0 .-text
reset! *text $ str "\"Nemotron:" &newline &newline
; d! $ :: :states cursor
-> state
assoc :answer $ w-log answer
assoc :loading? false
apply-args () $ fn () (hint-fn async)
let
info $ js-await (.!read reader)
value $ .-value info
done? $ .-done info
if done?
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? true)
if (.starts-with? value "\": ping") (recur)
if
or
.ends-with? (trim value) "\"[DONE]"
nil? content
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? true)
let
lines $ -> (.split-lines value)
filter $ fn (x)
not $ empty? x
&doseq (line lines)
let
candidate0 $ -> (.!slice line 6) (first-line) (js/JSON.parse) .-choices .-0
content $ or (-> candidate0 .-delta .-content) "\""
swap! *text str content
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? false)
recur
|call-gemini-msg! $ %{} :CodeEntry (:doc "|switching to Google's generative-ai-js sdk")
:code $ quote
defn call-gemini-msg! (variant cursor state prompt-text d!) (hint-fn async)
if (nil? @*gen-ai)
reset! *gen-ai $ new GoogleGenerativeAI (get-gemini-key!)
if-let
abort $ deref *abort-control
do (js/console.warn "\"Aborting prev") (.!abort abort)
d! $ :: :states cursor
-> state (assoc :answer nil) (assoc :loading? true)
let
selected $ js-await (get-selected)
gen-ai $ let
ai @*gen-ai
js/console.log ai
, ai
model-instance $ .!getGenerativeModel gen-ai
js-object $ :model (pick-model variant)
js-object (:baseUrl "\"https://sf.chenyong.life")
:signal $ let
abort $ new js/AbortController
reset! *abort-control abort
.-signal abort
content $ .!replace prompt-text "\"{{selected}}" (or selected "\"<未找到选中内容>")
json? $ or (.!includes prompt-text "\"{{json}}") (.!includes prompt-text "\"{{JSON}}")
sdk-result $ js-await
.!generateContentStream model-instance $ js-object
:contents $ js-array
js-object (:role "\"user")
:parts $ js-array
js-object $ :text content
:generationConfig $ if json?
js-object $ "\"responseMimeType" "\"application/json"
, js/undefined
*text $ atom "\""
js-await $ for-await-stream (.-stream sdk-result)
fn (? chunk)
if (some? chunk)
do
swap! *text str $ .!text chunk
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? false)
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? false)
d! $ :: :states cursor
-> state (assoc :answer @*text) (assoc :loading? false) (assoc :done? true)
|comp-abort $ %{} :CodeEntry (:doc |)
:code $ quote
defn comp-abort (t)
span
{}
:class-name $ str-spaced css/font-fancy css/row-middle style-more
:style $ {} (:cursor :pointer)
:on-click $ fn (e d!)
if-let
abort $ deref *abort-control
do (js/console.warn "\"Aborting prev") (.!abort abort)
<> t
=< 8 nil
<> "\"✕" style-abort-close
|comp-container $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-container (reel)
let
store $ :store reel
states $ :states store
cursor $ or (:cursor states) ([])
state $ or (:data states)
{} (:answer nil) (:loading? false) (:done? false)
model $ either (:model state) :gemini
model-plugin $ use-modal-menu (>> states :model)
{} (; :title "|Select model")
:style $ {} (:width 300)
:backdrop-style $ {}
; :card-class style-card
; :backdrop-class style-backdrop
; :confirm-class style-confirm
:items $ [] (:: :item :gemini-flash "|Gemini Flash") (:: :item :gemini-flash-lite "|Gemini Flash Lite") (:: :item :gemini-pro "|Gemini Pro") (:: :item :gemini-flash-thinking "|Gemini Flash thinking") (:: :item :gemini-thinking "|Gemini thinking") (:: :item :gemini-learnlm "|Gemini LearnLM") (:: :item :claude "\"Claude 3.5") (:: :item :claude-3.7 "\"Claude 3.7") (:: :item :claude-3.7-thinking "\"Claude 3.7 Thinking") (:: :item :deepinfra "\"Deepinfra")
:on-result $ fn (result d!)
d! cursor $ assoc state :model (nth result 1)
div
{} $ :class-name (str-spaced css/preset css/global css/column css/fullscreen css/gap8 style-app-global)
div
{} $ :class-name (str-spaced css/expand style-message-area)
div
{} $ :class-name (str-spaced style-message-list)
if (:loading? state)
div ({})
comp-abort $ str (turn-str model) "\" loading..."
if
not $ blank? (:answer state)
div ({})
if
json-pattern? $ :answer state
pre $ {} (:class-name style-code-content)
:inner-text $ :answer state
comp-md-block
-> (:answer state) (either "\"")
.!replace pattern-spaced-code $ str &newline "\"```"
{} $ :class-name style-md-content
div
{} $ :class-name css/row-parted
div
{} $ :class-name (str-spaced css/row-middle css/gap8)
if (:done? state)
a $ {}
:inner-text $ turn-str model
:class-name $ str-spaced style-a-toggler css/font-fancy
:style $ {}
:opacity $ if (= model :anthropic) 1 0.3
:on-click $ fn (e d!)
; d! $ :: :change-model
.show model-plugin d!
div ({})
comp-abort $ str (turn-str model) "\" streaming..."
if (:done? state)
div
{} $ :class-name (str-spaced css/row-middle)
comp-copy $ :answer state
=< nil 200
comp-message-box (>> states :message-box)
fn (text d!) (submit-message! cursor state text model d!)
.render model-plugin
if dev? $ comp-reel (>> states :reel) reel ({})
if dev? $ comp-inspect "\"Store" store nil
|comp-message-box $ %{} :CodeEntry (:doc |)
:code $ quote
defcomp comp-message-box (states on-submit)
let
cursor $ :cursor states
state $ either (:data states)
{} $ :content "\""
[] (effect-focus)
div
{} $ :class-name (str-spaced css/center style-message-box)
textarea $ {}
:value $ :content state
:placeholder "\"Content"
:id "\"message"
:class-name $ str-spaced css/textarea css/font-code! style-textbox
:on-input $ fn (e d!)
d! cursor $ assoc state :content (:value e)
:on-keydown $ fn (e d!)
if
and
= 13 $ :keycode e
:meta? e
on-submit (:content state) d!
button $ {}
:class-name $ str-spaced css/button style-submit
:inner-text "\"Generate"
:on-click $ fn (e d!)
; println $ :content state
on-submit (:content state) d!
if
not $ blank? (:content state)
comp-close $ {} (:class-name style-clear)
:on-click $ fn (e d!)
d! cursor $ assoc state :content "\""
-> (js/document.querySelector "\"#message") (.!focus)
|effect-focus $ %{} :CodeEntry (:doc |)
:code $ quote
defeffect effect-focus () (action el at?)
when (= action :mount)
.!select $ .!querySelector el "\"textarea"
|first-line $ %{} :CodeEntry (:doc "|last message from error contains a line starts with \"data: \" and an extra error message. In order that JSON is parsed correctly, only first line is used now.")
:code $ quote
defn first-line (tt)
let
lines $ -> tt (.!split &newline)
.!filter $ fn (line idx _a)
not $ blank? line
if
> (.-length lines) 1
js/console.warn "\"Droping some unexpected lines:" $ .!slice lines 1
.-0 lines
|for-await-stream $ %{} :CodeEntry (:doc |)
:code $ quote
defn for-await-stream (stream f) (hint-fn async) (&raw-code "\"for await (let item of stream) {\n f(item)\n}\n\nreturn undefined")
|get-anthropic-key! $ %{} :CodeEntry (:doc |)
:code $ quote
defn get-anthropic-key! () $ let
key $ js/localStorage.getItem "\"claude-key"
if (blank? key)
let
v $ js/prompt "\"Required claude-key in localStorage"
if (blank? v)
raise $ new js/Error "\"key is empty"
js/localStorage.setItem "\"claude-key" v
, v
, key
|get-deepinfra-key! $ %{} :CodeEntry (:doc |)
:code $ quote
defn get-deepinfra-key! () $ let
key $ js/localStorage.getItem "\"deepinfra-key"
if (blank? key)
let
v $ js/prompt "\"Required deepinfra-key in localStorage"
if (blank? v)
raise $ new js/Error "\"key is empty"
js/localStorage.setItem "\"deepinfra-key" v
, v
, key
|get-gemini-key! $ %{} :CodeEntry (:doc |)
:code $ quote
defn get-gemini-key! () $ let
key $ js/localStorage.getItem "\"gemini-key"
if (blank? key)
let
v $ js/prompt "\"Required gemini-key in localStorage"
if (blank? v)
raise $ new js/Error "\"key is empty"
js/localStorage.setItem "\"gemini-key" v
, v
, key
|json-pattern? $ %{} :CodeEntry (:doc |)
:code $ quote
defn json-pattern? (text)
or (.!startsWith text "\"{") (.!startsWith text "\"[")
|pattern-spaced-code $ %{} :CodeEntry (:doc |)
:code $ quote
def pattern-spaced-code $ noted "\"temp fix of nested code block" (&raw-code "\"/\\n\\s+```/g")
|pick-model $ %{} :CodeEntry (:doc |)
:code $ quote
defn pick-model (variant)
case-default variant "\"gemini-2.0-flash-exp" (:gemini-thinking "\"gemini-2.0-flash-thinking-exp-1219") (:gemini-pro "\"gemini-2.5-pro-exp-03-25") (:gemini-flash-lite "\"gemini-2.0-flash-lite-preview-02-05") (:gemini-learnlm "\"learnlm-1.5-pro-experimental") (:gemini-flash-thinking "\"gemini-2.0-flash-thinking-exp-01-21")
|style-a-toggler $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-a-toggler $ {}
"\"&" $ {} (:cursor :pointer)
|style-abort-close $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-abort-close $ {}
"\"&" $ {} (:vertical-align :top) (:font-size 10)
|style-app-global $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-app-global $ {}
str "\"& ." style-code-block
{} $ :max-width "\"90vw"
"\"&" $ {} (:color "\"#999") (:transition-duration "\"300ms")
:background-color $ hsl 0 0 98
"\"&:hover" $ {} (:color "\"#777")
:background-color $ hsl 0 0 100
|style-clear $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-clear $ {}
"\"&" $ {} (:position :absolute) (:left 12) (:bottom 12) (:opacity 0.4) (:padding "\"4px 8px") (:display :inline-block) (:height "\"24px")
|style-code-content $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-code-content $ {}
"\"&" $ {} (:line-height "\"1.5") (:font-size 13)
|style-md-content $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-md-content $ {}
"\"& .md-p" $ {} (:margin "\"16px 0") (:line-height "\"1.6")
|style-message-area $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-message-area $ {}
"\"&" $ {} (:flex 2) (:overflow :scroll)
|style-message-box $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-message-box $ {}
"\"&" $ {} (:position :absolute) (:bottom 0) (:opacity 0.9) (:max-width 1200) (:width "\"100%") (:right "\"50%") (:padding "\"8px") (:margin :auto) (:transition-duration "\"300ms") (:transform "\"translate(50%,0)")
"\"&:focus-within" $ {} (:opacity 1) (:transform "\"translate(50%,0)")
|style-message-list $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-message-list $ {}
"\"&" $ {} (:flex 2) (:padding "\"40px 16px 200px 16px") (:width "\"100%") (:max-width 1200) (:margin :auto) (:position :relative)
|style-more $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-more $ {}
"\"&" $ {} (:text-align :center) (:min-width 80)
:background-color $ hsl 0 0 94
:border-radius 16
:padding "\"4px 12px"
:margin "\"8px 0"
:white-space :nowrap
:display :inline-flex
"\"&:hover" $ {}
:box-shadow $ str "\"1px 1px 4px " (hsl 0 0 0 0.2)
|style-submit $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-submit $ {}
"\"&" $ {} (:position :absolute) (:bottom 20) (:right 20)
|style-textbox $ %{} :CodeEntry (:doc |)
:code $ quote
defstyle style-textbox $ {}
"\"&" $ {} (:border-radius 12) (:height "\"160px") (:width "\"100%") (:transition-duration "\"320ms")
"\"&:focus-within" $ {} (:height "\"260px")
|submit-message! $ %{} :CodeEntry (:doc |)
:code $ quote
defn submit-message! (cursor state prompt-text model d!) (hint-fn async)
let
*text $ atom "\""
try
case-default (:model state)
js-await $ call-gemini-msg! (:model state) cursor state prompt-text d!
:gemini-pro $ js-await
call-gemini-msg! (:model state) cursor state prompt-text d!
:gemini-thinking $ js-await
call-gemini-msg! (:model state) cursor state prompt-text d!
:gemini-flash-thinking $ js-await
call-gemini-msg! (:model state) cursor state prompt-text d!
:gemini-flash-lite $ js-await
call-gemini-msg! (:model state) cursor state prompt-text d!
:gemini-learnlm $ js-await
call-gemini-msg! (:model state) cursor state prompt-text d!
:claude $ js-await (call-anthropic-msg! cursor state prompt-text "\"claude-3-5-sonnet-20241022" false d!)
:claude-3.7 $ js-await (call-anthropic-msg! cursor state prompt-text "\"claude-3-7-sonnet-20250219" false d!)
:claude-3.7-thinking $ js-await (call-anthropic-msg! cursor state prompt-text "\"claude-3-7-sonnet-20250219" true d!)
:deepinfra $ js-await (call-deepinfra-msg! cursor state prompt-text d! *text)
fn (e)
d! cursor $ -> state
assoc :answer $ str @*text &newline &newline (str "\"Failed to load: " e)
assoc :loading? false
assoc :done? true
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.comp.container $ :require (respo-ui.css :as css)
respo.css :refer $ defstyle
respo.util.format :refer $ hsl
respo.core :refer $ defcomp defeffect <> >> div button textarea span input a pre
respo.comp.space :refer $ =<
respo.comp.inspect :refer $ comp-inspect
reel.comp.reel :refer $ comp-reel
app.config :refer $ dev? chrome-extension?
"\"axios" :default axios
respo-md.comp.md :refer $ comp-md-block style-code-block
respo-ui.comp :refer $ comp-copy comp-close
respo-alerts.core :refer $ use-modal-menu
"\"../extension/get-selected" :refer $ get-selected
"\"@google/generative-ai" :refer $ GoogleGenerativeAI
|app.config $ %{} :FileEntry
:defs $ {}
|chrome-extension? $ %{} :CodeEntry (:doc |)
:code $ quote
def chrome-extension? $ and (some? js/window.chrome) (some? js/window.chrome.runtime) (some? js/window.chrome.runtime.id)
|dev? $ %{} :CodeEntry (:doc |)
:code $ quote
def dev? $ = "\"dev" (get-env "\"mode" "\"release")
|site $ %{} :CodeEntry (:doc |)
:code $ quote
def site $ {} (:storage-key "\"msg-buffer")
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.config)
|app.main $ %{} :FileEntry
:defs $ {}
|*reel $ %{} :CodeEntry (:doc |)
:code $ quote
defatom *reel $ -> reel-schema/reel (assoc :base schema/store) (assoc :store schema/store)
|dispatch! $ %{} :CodeEntry (:doc |)
:code $ quote
defn dispatch! (op)
when
and config/dev? $ not= op :states
js/console.log "\"Dispatch:" op
reset! *reel $ reel-updater updater @*reel op
|listen-extension! $ %{} :CodeEntry (:doc |)
:code $ quote
defn listen-extension! ()
js/chrome.runtime.onMessage.addListener $ fn (message sender respond!)
if
= "\"menu-trigger" $ .-action message
let
content $ str "\"你扮演一个专业的工程师, 对以下内容做一下讲解, 用中文, 注意要简略, 内容注意分块.\n\n" &newline &newline (.-content message)
store $ :store @*reel
cursor $ []
state0 $ get-in store ([] :states :data)
model $ either (:model store) :gemini
submit-message! cursor state0 content model dispatch!
js/chrome.runtime.connect $ js-object (:name |mySidepanel)
|main! $ %{} :CodeEntry (:doc |)
:code $ quote
defn main! ()
println "\"Running mode:" $ if config/dev? "\"dev" "\"release"
if config/dev? $ load-console-formatter!
render-app!
add-watch *reel :changes $ fn (reel prev) (render-app!)
listen-devtools! |k dispatch!
js/window.addEventListener |beforeunload $ fn (event) (persist-storage!)
js/window.addEventListener |visibilitychange $ fn (event)
if (= "\"hidden" js/document.visibilityState) (persist-storage!)
; flipped js/setInterval 60000 persist-storage!
let
raw $ js/localStorage.getItem (:storage-key config/site)
when (some? raw)
dispatch! $ :: :hydrate-storage (parse-cirru-edn raw)
if config/chrome-extension? $ listen-extension!
println "|App started."
|mount-target $ %{} :CodeEntry (:doc |)
:code $ quote
def mount-target $ js/document.querySelector |.app
|persist-storage! $ %{} :CodeEntry (:doc |)
:code $ quote
defn persist-storage! ()
println "\"Saved at" $ .!toISOString (new js/Date)
js/localStorage.setItem (:storage-key config/site)
format-cirru-edn $ :store @*reel
|reload! $ %{} :CodeEntry (:doc |)
:code $ quote
defn reload! () $ if (nil? build-errors)
do (remove-watch *reel :changes) (clear-cache!)
add-watch *reel :changes $ fn (reel prev) (render-app!)
reset! *reel $ refresh-reel @*reel schema/store updater
hud! "\"ok~" "\"Ok"
hud! "\"error" build-errors
|render-app! $ %{} :CodeEntry (:doc |)
:code $ quote
defn render-app! () $ render! mount-target (comp-container @*reel) dispatch!
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.main $ :require
respo.core :refer $ render! clear-cache!
app.comp.container :refer $ comp-container submit-message!
app.updater :refer $ updater
app.schema :as schema
reel.util :refer $ listen-devtools!
reel.core :refer $ reel-updater refresh-reel
reel.schema :as reel-schema
app.config :as config
"\"./calcit.build-errors" :default build-errors
"\"bottom-tip" :default hud!
|app.schema $ %{} :FileEntry
:defs $ {}
|store $ %{} :CodeEntry (:doc |)
:code $ quote
def store $ {}
:states $ {}
:cursor $ []
:model nil
:ns $ %{} :CodeEntry (:doc |)
:code $ quote (ns app.schema)
|app.updater $ %{} :FileEntry
:defs $ {}
|updater $ %{} :CodeEntry (:doc |)
:code $ quote
defn updater (store op op-id op-time)
tag-match op
:states cursor s
update-states store cursor s
(:hydrate-storage data) data
(:change-model)
if
= (:model store) :anthropic
assoc store :model :gemini
assoc store :model :anthropic
_ $ do (eprintln "\"unknown op:" op) store
:ns $ %{} :CodeEntry (:doc |)
:code $ quote
ns app.updater $ :require
respo.cursor :refer $ update-states