@kairotic/app 0.0.0-stage → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +9 -2
  2. package/index.html +1101 -0
  3. package/kairotic/api.cljs +362 -0
  4. package/kairotic/assistant.cljs +505 -0
  5. package/kairotic/backends/gun-backend.js +371 -0
  6. package/kairotic/canvas.cljs +751 -0
  7. package/kairotic/code.cljs +108 -0
  8. package/kairotic/components.cljs +838 -0
  9. package/kairotic/core.cljs +96 -0
  10. package/kairotic/corefns.cljs +473 -0
  11. package/kairotic/docs.mjs +2 -0
  12. package/kairotic/engine.cljs +482 -0
  13. package/kairotic/examples.mjs +96 -0
  14. package/kairotic/faust.cljs +225 -0
  15. package/kairotic/help.cljs +1036 -0
  16. package/kairotic/hiccup.cljs +566 -0
  17. package/kairotic/input.cljs +271 -0
  18. package/kairotic/kfile.mjs +138 -0
  19. package/kairotic/library.cljs +214 -0
  20. package/kairotic/links.cljs +41 -0
  21. package/kairotic/mt.cljs +1604 -0
  22. package/kairotic/mtboxes.cljs +276 -0
  23. package/kairotic/packages/analysis.mjs +64 -0
  24. package/kairotic/packages/controls.mjs +28 -0
  25. package/kairotic/packages/data.mjs +22 -0
  26. package/kairotic/packages/faust-units/cmaes.mjs +82 -0
  27. package/kairotic/packages/faust-units/graph.mjs +212 -0
  28. package/kairotic/packages/faust-units/listen.mjs +125 -0
  29. package/kairotic/packages/faust-units/notation.mjs +139 -0
  30. package/kairotic/packages/faust-units/parse.mjs +439 -0
  31. package/kairotic/packages/faust-units/recipe.mjs +195 -0
  32. package/kairotic/packages/faust-units/render.mjs +69 -0
  33. package/kairotic/packages/faust-units/units.mjs +130 -0
  34. package/kairotic/packages/faust-units/view.mjs +353 -0
  35. package/kairotic/packages/faust-units.mjs +9 -0
  36. package/kairotic/packages/io.mjs +58 -0
  37. package/kairotic/packages/net.mjs +28 -0
  38. package/kairotic/packages/patterns.mjs +34 -0
  39. package/kairotic/packages/spatial.mjs +22 -0
  40. package/kairotic/packages/spectral/shift.mjs +169 -0
  41. package/kairotic/packages/spectral.mjs +16 -0
  42. package/kairotic/packages/synth.mjs +28 -0
  43. package/kairotic/packages/theory/counterpoint.mjs +798 -0
  44. package/kairotic/packages/theory/mirror.mjs +221 -0
  45. package/kairotic/packages/theory.mjs +46 -0
  46. package/kairotic/patterns.cljs +538 -0
  47. package/kairotic/presets.cljs +227 -0
  48. package/kairotic/quantize.cljs +782 -0
  49. package/kairotic/record.cljs +169 -0
  50. package/kairotic/release.cljs +90 -0
  51. package/kairotic/ring.cljs +129 -0
  52. package/kairotic/ringaudio.cljs +232 -0
  53. package/kairotic/ringcore.mjs +270 -0
  54. package/kairotic/score.cljs +176 -0
  55. package/kairotic/shapes.cljs +283 -0
  56. package/kairotic/signal.cljs +327 -0
  57. package/kairotic/sounds.cljs +565 -0
  58. package/kairotic/sounds.mjs +108 -0
  59. package/kairotic/span.cljs +129 -0
  60. package/kairotic/spell.cljs +281 -0
  61. package/kairotic/structure.cljs +145 -0
  62. package/kairotic/talk.cljs +115 -0
  63. package/kairotic/tempo.cljs +226 -0
  64. package/kairotic/treecanvas.cljs +299 -0
  65. package/kairotic/tutorials.mjs +1283 -0
  66. package/kairotic/vendor/codemirror.mjs +23 -0
  67. package/package.json +23 -4
  68. package/page/00-work.cljs +862 -0
  69. package/page/01-commands.cljs +1219 -0
  70. package/page/02-patch-view.cljs +1051 -0
  71. package/page/03-history-view.cljs +481 -0
  72. package/page/04-panels.cljs +448 -0
  73. package/page/05-gestures.cljs +1247 -0
  74. package/page/06-connection.cljs +219 -0
  75. package/page/07-claude.cljs +430 -0
  76. package/page/08-sessions.cljs +423 -0
  77. package/page/09-learn.cljs +602 -0
  78. package/page/10-embed.cljs +469 -0
  79. package/page/11-startup.cljs +61 -0
  80. package/page/12-feedback.cljs +172 -0
  81. package/page/13-perform.cljs +325 -0
  82. package/page/14-editor.cljs +656 -0
  83. package/page/15-presets.cljs +230 -0
  84. package/page/16-release.cljs +242 -0
  85. package/page/17-sounds.cljs +147 -0
  86. package/page/18-lines.cljs +594 -0
  87. package/page/19-files.cljs +247 -0
  88. package/page/19-html.cljs +152 -0
  89. package/page/20-talk.cljs +527 -0
  90. package/page/21-net.cljs +110 -0
  91. package/page/22-learn.cljs +303 -0
  92. package/page/23-ids.cljs +233 -0
  93. package/page/24-clipboard.cljs +136 -0
  94. package/page/25-tidy.cljs +74 -0
  95. package/page/26-probes.cljs +111 -0
  96. package/sources.mjs +2 -0
@@ -0,0 +1,469 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 11 of 12: embed
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── embedding (Study 22) ─────────────────────────
8
+ ;; A link carries what it shows: a patch (its code, compressed), a tutorial step or a help patch. It
9
+ ;; opens as a local work (nothing is saved, and in ?embed / ?present nothing connects to a relay).
10
+ ;; The code of a link is read as data and only a few calls are let through, with literal arguments:
11
+ ;; a link cannot run code in this page. Its script and component boxes wait, as in a shared room,
12
+ ;; until whoever opened it runs them.
13
+ ;; Release/Perform (issue #50): the link carries a release of the patch (its presets boxes keeping their
14
+ ;; presets, as `(release)` makes it) and opens in Perform, so what whoever plays it changes is a take,
15
+ ;; never the work. Old ?present links open the same way.
16
+
17
+ (declare with-stored-presets perform)
18
+
19
+ (def link-calls links/calls)
20
+ (defn link-code
21
+ "The code of a link, read as data and written again with only the calls a patch is made of
22
+ (kairotic.links)."
23
+ [src] (links/read-code src))
24
+
25
+ (defn script-boxes
26
+ "The script, component and pattern boxes of the work that would run code nobody has allowed yet."
27
+ []
28
+ (count (filter (fn [b] (and (#{"script" "component" "pattern"} (klass (:text b)))
29
+ (not (cbox/allowed? (str (get-in b [:data :source]))))))
30
+ (vals (:boxes (eng/flat (root-patch)))))))
31
+
32
+ (defn trust-official!
33
+ "The component boxes of a link that are exactly an official package (as this page
34
+ brings it) run without asking."
35
+ []
36
+ (let [srcs (keep (fn [b] (when (= "component" (klass (:text b))) (str (get-in b [:data :source]))))
37
+ (vals (:boxes (eng/flat (root-patch)))))
38
+ pkgs (distinct (for [s srcs [_ p] (re-seq #":package \"([^\"]+)\"" s) :when (get lib/packages p)] p))
39
+ wanted (set srcs)]
40
+ (-> (js/Promise.all
41
+ (into-array
42
+ (for [p pkgs] (-> (lib/import-package p) (.then (fn [[m origin]] (lib/package-source m origin))) (.catch (fn [_] nil))))))
43
+ (.then (fn [arr]
44
+ (let [ok (filter wanted (array-seq arr))]
45
+ (when (seq ok)
46
+ (swap! cbox/trusted into ok)
47
+ (cbox/forget-language! "component")
48
+ (render-all!))))))))
49
+
50
+ (defn update-trust-bar! []
51
+ (when-let [el ($ "trust-bar")]
52
+ (let [n (if (and (= "link" (:id @work)) (not @cbox/trust)) (script-boxes) 0)]
53
+ (set! (.-hidden el) (zero? n))
54
+ (when (pos? n)
55
+ (set! (.-textContent ($ "trust-text"))
56
+ (if (es?)
57
+ (str "Este patch trae " n (if (= n 1) " script" " scripts") " (ClojureScript de quien hizo el enlace): no se ejecutan hasta que lo permitas.")
58
+ (str "This patch brings " n (if (= n 1) " script" " scripts") " (ClojureScript by whoever made the link): they do not run until you allow it.")))
59
+ (set! (.-textContent ($ "trust-run")) (if (es?) "Ejecutarlos" "Run them"))))))
60
+
61
+ (.addEventListener ($ "trust-run") "click" (fn [_] (trust-scripts true)))
62
+
63
+ (defn open-link-work!
64
+ "Opens a local work whose history starts with the patch of a link."
65
+ [nm code]
66
+ (let [p (build code)]
67
+ (eng/stop!)
68
+ (reset! lesson nil)
69
+ (reset! cbox/trust false)
70
+ (cbox/forget-language! "clojurescript")
71
+ (reset! work {:kind :local :id "link" :name nm})
72
+ (reset-work-state! :local)
73
+ (swap! mgr assoc :open false :confirm nil :invite nil)
74
+ (swap! hist assoc :mode :local :loaded true)
75
+ (let [id (new-node! nil 0 [[:load p]] nil)]
76
+ (swap! world assoc :pos [id 1]))
77
+ (ensure-classes!)
78
+ (trust-official!)
79
+ (repl-print! "out" (esc (str "; " (if (es?) "Desde un enlace" "From a link") ": «" nm "» (" (if (es?) "local: no se guarda" "local: not saved") ")")))
80
+ (render-all!)))
81
+
82
+ (defn site-url
83
+ "Where links point: this page on Kairotic Pages (inside claude.ai, the public site)."
84
+ []
85
+ (if (pages?)
86
+ (str (.-origin js/location) (.-pathname js/location))
87
+ "https://bonuoq.github.io/kairotic/"))
88
+
89
+ (defn release-code
90
+ "The code of the patch as a release carries it: its presets boxes keeping their presets (issue #50)."
91
+ []
92
+ (let [p (inline-lines (work-patch))]
93
+ (patch->code (try (with-stored-presets p) (catch :default _ p)))))
94
+
95
+ (defn link-mode
96
+ "The mode of a link: :present (Release/Perform; also asked as :release or :perform) or :embed."
97
+ [mode]
98
+ (if (#{:present :release :perform "present" "release" "perform"} mode) :present :embed))
99
+
100
+ (defn packed-code
101
+ "Promise of the patch as a link carries it: in Release/Perform, its release."
102
+ [mode]
103
+ (-> (js/Promise.resolve nil)
104
+ (.then (fn [_] ((.-kairoticPack js/window) (if (= mode :present) (release-code) (patch->code (inline-lines (work-patch)))))))))
105
+
106
+ (defn link-params
107
+ "Promise of the parameters that bring back what is shown now: the tutorial step, the help patch, or
108
+ the patch itself — or, if asked in the Share/Export panel, a description for Claude (Study 23). In
109
+ Release/Perform (:present), always the release of the patch (issue #50)."
110
+ ([] (link-params :embed))
111
+ ([mode]
112
+ (let [l @lesson
113
+ ask (when (some-> ($ "ep-ask") .-checked) (str/trim (str (.-value ($ "ep-ask-text")))))]
114
+ (cond
115
+ (seq ask) (js/Promise.resolve [["ask" ask]])
116
+ (= mode :present) (.then (packed-code :present) (fn [z] [["code" z]]))
117
+ (and l (:id l)) (js/Promise.resolve [["tutorial" (:id l)] ["step" (str (inc (:at l)))] ["lang" (name @lang)]])
118
+ (and l (:help l)) (js/Promise.resolve [["help" (:help l)] ["lang" (name @lang)]])
119
+ :else (.then (packed-code :embed) (fn [z] [["code" z]]))))))
120
+
121
+ (defn url-with
122
+ "A link to the site with these parameters, after # (issue #55: a long patch is not refused by the server)."
123
+ [ps mode]
124
+ (let [q (js/URLSearchParams.)]
125
+ (doseq [[k v] ps] (.set q k v))
126
+ (case mode :embed (.set q "embed" "") :present (.set q "present" "") nil)
127
+ (str (site-url) "#" (str/replace (.toString q) #"=(&|$)" "$1"))))
128
+
129
+ (defn iframe-of [url mode]
130
+ (str "<iframe src=\"" (esc url) "\" width=\"100%\" height=\"" (if (= mode :present) 560 480)
131
+ "\" style=\"border:0\" allow=\"fullscreen; autoplay\" allowfullscreen loading=\"lazy\"></iframe>"))
132
+
133
+ (defn copy-text! [text]
134
+ (-> (js/Promise.resolve)
135
+ (.then (fn [_] (.writeText (.-clipboard js/navigator) text)))
136
+ (.then (fn [_] true))
137
+ (.catch (fn [_] false))))
138
+
139
+ (defn embed-link
140
+ "A link (and an iframe) that shows what is open now in another page: the patch, or with :release
141
+ (also :perform or :present) a release of it, in Perform, only its presentation, fullscreen. The link is
142
+ copied. In Notion: /embed and paste it."
143
+ ([] (embed-link :embed))
144
+ ([mode]
145
+ (let [mode (link-mode mode)]
146
+ (-> (link-params mode)
147
+ (.then (fn [ps]
148
+ (let [u (url-with ps mode)]
149
+ (repl-print! "out" (esc (str "; " u)))
150
+ (repl-print! "out" (esc (str "; " (iframe-of u mode))))
151
+ (.then (copy-text! u) (fn [ok] (repl-print! "out" (if ok "; (link copied)" "; (copy it from here)")))))))
152
+ (.catch (fn [e] (repl-print! "err" (esc (str (or (.-message e) e)))))))
153
+ :making-link)))
154
+
155
+ ;; ───── the Share/Export panel (it was Embed) ─────
156
+
157
+ (defn ep-mode [] (if (.-checked (.querySelector js/document "input[name=ep-mode][value=present]")) :present :embed))
158
+
159
+ (defn render-embed-pop! []
160
+ (set! (.-hidden ($ "ep-ask-row")) (not (.-checked ($ "ep-ask"))))
161
+ (when (and (.-checked ($ "ep-ask")) (str/blank? (.-value ($ "ep-ask-text"))))
162
+ (set! (.-value ($ "ep-ask-text")) (str/trim (str (.-value ($ "ask-text"))))))
163
+ (let [mode (ep-mode) l @lesson]
164
+ (set! (.-textContent ($ "ep-what"))
165
+ (cond (= mode :present) "A release of this patch (its presets boxes keep their presets), opened in Perform: whatever is played or changed there is a take, never the work. Only the presented boxes, filling the frame, with a fullscreen button (without presented boxes, the whole patch)."
166
+ (and l (:id l)) (str "This tutorial, at step " (inc (:at l)) ", with its lesson bar.")
167
+ (and l (:help l)) (str "The help patch of " (:help l) ".")
168
+ :else "This patch, scaled to the frame, with ▶ ■ and a link to open it in Kairotic."))
169
+ (-> (link-params mode)
170
+ (.then (fn [ps]
171
+ (let [u (url-with ps mode)]
172
+ (set! (.-value ($ "ep-url")) u)
173
+ (set! (.-value ($ "ep-iframe")) (iframe-of u mode)))))
174
+ (.catch (fn [e] (set! (.-value ($ "ep-url")) (str "Error: " (or (.-message e) e))))))))
175
+
176
+ (.addEventListener ($ "embed-btn") "click"
177
+ (fn [_]
178
+ (let [el ($ "embed-pop")]
179
+ (set! (.-hidden el) (not (.-hidden el)))
180
+ (when-not (.-hidden el) (render-embed-pop!)))))
181
+
182
+ (.addEventListener ($ "embed-pop") "change" (fn [_] (render-embed-pop!)))
183
+
184
+ (.addEventListener ($ "embed-pop") "click"
185
+ (fn [e]
186
+ (let [t (.-target e)]
187
+ (cond
188
+ (.closest t "#ep-close") (set! (.-hidden ($ "embed-pop")) true)
189
+ (.closest t "[data-copy]")
190
+ (let [b (.closest t "[data-copy]") src ($ (.getAttribute b "data-copy"))]
191
+ (.select src)
192
+ (.then (copy-text! (.-value src))
193
+ (fn [ok] (set! (.-textContent b) (if ok "Copied" "Select + copy"))
194
+ (js/setTimeout (fn [] (set! (.-textContent b) "Copy")) 1500))))))))
195
+
196
+ ;; ───── inside the frame ─────
197
+
198
+ (defn mode-url
199
+ "This same address in another mode: nil (the whole editor), :embed or :present."
200
+ [mode]
201
+ (let [q (js/URLSearchParams. (.toString url-params))]
202
+ (.delete q "embed")
203
+ (.delete q "present")
204
+ (case mode :embed (.set q "embed" "") :present (.set q "present" "") nil)
205
+ (let [s (str/replace (.toString q) #"=(&|$)" "$1")]
206
+ (str (.-origin js/location) (.-pathname js/location) (when (seq s) (str "#" s))))))
207
+
208
+ (defn fullscreen!
209
+ "Fills the screen with the presentation (or leaves it). Where the frame does not allow it, the
210
+ presentation opens in a tab of its own."
211
+ []
212
+ (let [d js/document el (.-documentElement d)]
213
+ (cond
214
+ (.-fullscreenElement d) (.exitFullscreen d)
215
+ (and (.-fullscreenEnabled d) (.-requestFullscreen el)) (.catch (.requestFullscreen el) (fn [_] (js/window.open (mode-url :present) "_blank" "noopener")))
216
+ (and (.-webkitFullscreenEnabled d) (.-webkitRequestFullscreen el)) (.webkitRequestFullscreen el)
217
+ :else (js/window.open (mode-url :present) "_blank" "noopener"))))
218
+
219
+ (.addEventListener ($ "fs-btn") "click" (fn [_] (fullscreen!)))
220
+
221
+ (defonce resize-timer (atom nil))
222
+ (defonce first-touch (atom false))
223
+ (when embed-mode
224
+ (.add (.-classList js/document.body) "embed")
225
+ (when (= embed-mode :present) (.add (.-classList js/document.body) "present"))
226
+ (set! (.-href ($ "open-full")) (mode-url nil))
227
+ (doseq [ev ["resize" "fullscreenchange" "webkitfullscreenchange"]]
228
+ (.addEventListener (if (= ev "resize") js/window js/document) ev
229
+ (fn [_]
230
+ (js/clearTimeout @resize-timer)
231
+ (reset! resize-timer (js/setTimeout (fn [] (render-scene (current))) 80))))))
232
+
233
+ (defn perform-release!
234
+ "In ?present (Release/Perform, issue #50): the work opens in Perform, so what is played or changed there is
235
+ a take and the release stays as it came. Not in a lesson, which has steps of its own."
236
+ ([] (perform-release! 0))
237
+ ([tries]
238
+ (when (and (= embed-mode :present) (not @perf) (not @lesson))
239
+ (if (#{:connecting :none} (:mode @hist))
240
+ ;; the work is still opening: a moment later
241
+ (when (< tries 40) (js/setTimeout (fn [] (perform-release! (inc tries))) 150))
242
+ (try (perform)
243
+ (catch :default e (js/console.warn "Release/Perform: not in Perform" e)))))))
244
+
245
+ (defn present-what!
246
+ "In ?present (Release/Perform): in Perform, the presentation if the patch has presented boxes (otherwise
247
+ the whole patch); the first touch starts the sound."
248
+ []
249
+ (when (= embed-mode :present)
250
+ (perform-release!)
251
+ (presentation (boolean (some :present (vals (:boxes (work-patch))))))
252
+ (when-not @first-touch
253
+ (reset! first-touch true)
254
+ (.addEventListener ($ "canvas-wrap") "pointerdown"
255
+ (fn [_] (when-not (eng/running?) (play)))
256
+ #js {:once true :capture true}))))
257
+
258
+ ;; ───── a link that carries a description: ?ask=… (Study 23) ─────
259
+ ;; The link carries what to make, never a key: whoever opens it makes the patch with Claude on their own
260
+ ;; key (outside claude.ai, kept in their browser) or account. The result stays in their browser, so it is
261
+ ;; not made again on every visit, and "Copy the link of this result" gives a link that carries it.
262
+
263
+ (defonce link-ask (atom nil)) ; {:text :base :key :done :busy :hidden}
264
+ (defonce link-base (atom nil)) ; the code of the patch a ?code / ?src link brought, if any
265
+
266
+ (defn ask-store-key [text base] (str "kairotic.ask:" (hash [text base])))
267
+
268
+ (defn render-askbar! []
269
+ (let [a @link-ask el ($ "askbar")]
270
+ (set! (.-hidden el) (or (nil? a) (boolean (:hidden a))))
271
+ (when a
272
+ (let [s @sample-fn byok (some-> s .-byok) need-key (boolean (and s byok (not (.hasKey s))))]
273
+ (set! (.-textContent ($ "ab-text")) (str "«" (:text a) "»"))
274
+ (set! (.-hidden ($ "ab-keyrow")) (not need-key))
275
+ (set! (.-disabled ($ "ab-go")) (boolean (or (nil? s) need-key (:busy a))))
276
+ (set! (.-textContent ($ "ab-go")) (if (:done a) "Make it again" "Make it with Claude"))
277
+ (set! (.-hidden ($ "ab-fix")) (not (:done a)))
278
+ (set! (.-innerHTML ($ "ab-note"))
279
+ (cond
280
+ (nil? s) "Claude is not available here: open it in Kairotic ↗."
281
+ byok (str "Claude makes this patch from the description with <b>your own Anthropic API key</b>"
282
+ (when embed-mode " (saved only in this browser, for this frame)")
283
+ ", sent only to api.anthropic.com. The link carries the description, never a key.")
284
+ :else "Claude makes this patch from the description with your own Claude account."))))))
285
+
286
+ (defn ask-link! []
287
+ (when-let [a @link-ask]
288
+ (when-not (:busy a)
289
+ (swap! link-ask assoc :busy true)
290
+ (render-askbar!)
291
+ ;; from the patch the link brought (or an empty one), not from an earlier result
292
+ (dispatch! [:load (build (or (:base a) ""))])
293
+ (-> (ask-patch! (:text a) (boolean (:base a)))
294
+ (.then (fn [code]
295
+ (when code
296
+ (try (.setItem js/localStorage (:key a) code) (catch :default _ nil))
297
+ (swap! link-ask assoc :done true)
298
+ (present-what!))))
299
+ (.finally (fn [] (swap! link-ask assoc :busy false) (render-askbar!)))))))
300
+
301
+ (defn setup-ask!
302
+ "A link with ?ask: the bar with the description; what this browser made from it before, if anything."
303
+ [text base]
304
+ (let [k (ask-store-key text base)
305
+ made (try (.getItem js/localStorage k) (catch :default _ nil))]
306
+ (reset! link-ask {:text text :base base :key k})
307
+ (when (seq made)
308
+ (try (apply-patch-code! made)
309
+ (swap! link-ask assoc :done true)
310
+ (ask-log! "done" "made earlier in this browser")
311
+ (catch :default _ nil)))
312
+ (render-askbar!)))
313
+
314
+ (.addEventListener ($ "ab-go") "click" (fn [_] (ask-link!)))
315
+ (.addEventListener ($ "ab-hide") "click" (fn [_] (swap! link-ask assoc :hidden true) (render-askbar!)))
316
+ (.addEventListener ($ "ab-key-save") "click"
317
+ (fn [_] (when-let [s @sample-fn]
318
+ (let [v (str/trim (.-value ($ "ab-key")))]
319
+ (when (seq v) (.setKey s v) (set! (.-value ($ "ab-key")) "") (render-askbar!) (render-key!))))))
320
+ (.addEventListener ($ "ab-fix") "click"
321
+ (fn [_]
322
+ (-> (packed-code (or embed-mode :embed))
323
+ (.then (fn [z]
324
+ (let [u (url-with [["code" z]] (or embed-mode :embed))]
325
+ (.then (copy-text! u)
326
+ (fn [ok] (set! (.-textContent ($ "ab-status"))
327
+ (if ok "Link copied: put it in place of this one, and everyone sees this patch."
328
+ (str "Copy this link: " u)))))))))))
329
+
330
+ (defn ask-link
331
+ "A link that carries a description for Claude: (ask-link \"a sine at 440 Hz with an FM knob\"),
332
+ (ask-link \"…\" :present). Whoever opens it makes the patch with their own key or account."
333
+ ([text] (ask-link text :embed))
334
+ ([text mode]
335
+ (let [u (url-with [["ask" (str text)]] (link-mode mode))]
336
+ (repl-print! "out" (esc (str "; " u)))
337
+ (.then (copy-text! u) (fn [ok] (repl-print! "out" (if ok "; (link copied)" "; (copy it from here)"))))
338
+ :making-link)))
339
+
340
+
341
+ ;; ───── the log of requests to Claude: shown, loaded again, exported, shared ─────
342
+
343
+ (defn ask-play-url
344
+ "Promise of a link that plays what an entry made: its patch, or a patch with its component."
345
+ [e]
346
+ (let [code (if (= "component" (:kind e))
347
+ (str "(box :b1 \"component\" 20 20)\n(code :b1 " (pr-str (str (:code e))) ")")
348
+ (str (:code e)))]
349
+ (.then ((.-kairoticPack js/window) code) (fn [z] (url-with [["code" z]] nil)))))
350
+
351
+ (defn ask-markdown
352
+ "An entry as Markdown, to read or to share."
353
+ [e url max-code]
354
+ (let [code (str (:code e))]
355
+ (str "**Asked Claude** (" (:kind e) (when (:change e) ", changing the open one") " · " (:tier e) ") in Kairotic\n\n"
356
+ "> " (str/replace (str (:request e)) #"\n" "\n> ") "\n\n"
357
+ (when url (str "▶ [Open it in Kairotic](" url ")\n\n"))
358
+ (when (seq code)
359
+ (if (> (count code) max-code)
360
+ (str "_The code (" (count code) " characters) is in the link._\n\n")
361
+ (str "```clojure\n" code "\n```\n\n")))
362
+ "<details><summary>How it went: " (:status e) "</summary>\n\n"
363
+ (str/join "\n" (map #(str "- " %) (take 30 (:lines e))))
364
+ "\n</details>\n")))
365
+
366
+ (defn render-ask-history! []
367
+ (when-let [ol ($ "ask-hist-list")]
368
+ (let [es (reverse (ask-entries))]
369
+ (set! (.-textContent ($ "ask-hist-n")) (str "(" (count es) ")"))
370
+ (set! (.-innerHTML ol)
371
+ (apply str
372
+ (for [e es]
373
+ (str "<li data-ask=\"" (esc (:id e)) "\">"
374
+ "<div class=\"ah-top\">" (esc (:kind e)) " · " (esc (clojure.core/subs (str (:t e)) 0 16)) " · <span class=\"" (esc (:status e)) "\">" (esc (:status e)) "</span>"
375
+ (when (:work e) (str " · " (esc (:work e)))) "</div>"
376
+ "<div class=\"ah-q\">" (esc (ai/cut (:request e) 300)) "</div>"
377
+ "<div class=\"ah-acts\">"
378
+ (when (seq (:code e)) "<button class=\"act\" data-act=\"load\" title=\"Into the open work, as one event\">Load</button>")
379
+ "<button class=\"act\" data-act=\"copy\" title=\"As Markdown\">Copy</button>"
380
+ (when (seq (:code e)) "<button class=\"act\" data-act=\"share\" title=\"Opens a discussion already written in the Gallery of the Kairotic repository: you read it and publish it, under your own account\">Share on GitHub…</button>")
381
+ "<button class=\"act\" data-act=\"del\" title=\"Delete this entry\">✕</button>"
382
+ "</div></li>")))))))
383
+
384
+ (def share-repo "https://github.com/bonuoq/kairotic")
385
+
386
+ (.addEventListener ($ "ask-hist-list") "click"
387
+ (fn [ev]
388
+ (when-let [btn (.closest (.-target ev) "[data-act]")]
389
+ (let [id (.getAttribute (.closest btn "[data-ask]") "data-ask")
390
+ e (first (filter #(= id (:id %)) (ask-entries)))
391
+ act (.getAttribute btn "data-act")]
392
+ (when e
393
+ (case act
394
+ "load" (try
395
+ (if (= "component" (:kind e))
396
+ (let [src (str ";; written with Claude\n;; asked: " (str/replace (ai/cut (:request e) 300) #"\s+" " ") "\n" (:code e))]
397
+ (swap! cbox/trusted conj src)
398
+ (new-definition! src))
399
+ (apply-patch-code! (:code e)))
400
+ (catch :default x (repl-print! "err" (esc (str "; " (or (.-message x) x))))))
401
+ "copy" (-> (ask-play-url e)
402
+ (.then (fn [u] (copy-text! (ask-markdown e u 20000))))
403
+ (.then (fn [ok] (set! (.-textContent btn) (if ok "Copied" "Not copied")))))
404
+ "share" (let [w (js/window.open "about:blank" "_blank")]
405
+ ;; the window is opened at the click (a popup blocker allows it), then filled
406
+ (-> (ask-play-url e)
407
+ (.then (fn [u]
408
+ (let [q (js/URLSearchParams.)]
409
+ (.set q "category" "gallery")
410
+ (.set q "labels" "gallery")
411
+ (.set q "title" (ai/cut (str/replace (str (:request e)) #"\s+" " ") 90))
412
+ (.set q "body" (ask-markdown e u 3500))
413
+ (let [href (str share-repo "/discussions/new?" (.toString q))]
414
+ (if w (set! (.. w -location -href) href) (js/window.open href "_blank"))))))))
415
+ "del" (do (save-ask-entries! (vec (remove #(= id (:id %)) (ask-entries))))
416
+ (render-ask-history!))
417
+ nil))))))
418
+
419
+ (.addEventListener ($ "ask-hist-export") "click"
420
+ (fn [_]
421
+ (let [text (str/join "\n" (map #(js/JSON.stringify (clj->js %)) (ask-entries)))
422
+ a (js/document.createElement "a")]
423
+ (set! (.-href a) (js/URL.createObjectURL (js/Blob. #js [text] #js {:type "application/x-ndjson"})))
424
+ (set! (.-download a) "kairotic-claude-log.jsonl")
425
+ (.click a))))
426
+
427
+ (.addEventListener ($ "ask-hist-clear") "click"
428
+ (fn [_]
429
+ (let [b ($ "ask-hist-clear")]
430
+ (if (= "Sure?" (.-textContent b))
431
+ (do (save-ask-entries! []) (set! (.-textContent b) "Clear") (render-ask-history!))
432
+ (do (set! (.-textContent b) "Sure?") (js/setTimeout (fn [] (set! (.-textContent b) "Clear")) 3000))))))
433
+
434
+ (render-ask-history!)
435
+
436
+ (defn tutorials-ready []
437
+ (js/Promise.
438
+ (fn [res]
439
+ (load-tutorials!)
440
+ (if (sequential? @tutorials)
441
+ (res @tutorials)
442
+ (add-watch tutorials ::ready (fn [_ _ _ n] (when (sequential? n) (remove-watch tutorials ::ready) (res n))))))))
443
+
444
+ (defn start-from-url!
445
+ "Opens what the address asks for (?code, ?src, ?tutorial & ?step, ?help); in an embed, a local work if nothing."
446
+ []
447
+ (when-let [l (url-param "lang")] (when (#{"es" "en"} l) (reset! lang (keyword l)) (render-lesson!) (render-help!)))
448
+ (let [code (url-param "code") src (raw-param "src") ask (some-> (url-param "ask") str/trim not-empty) tut (url-param "tutorial") hc (url-param "help")
449
+ step (js/parseInt (or (url-param "step") "1") 10)
450
+ link-name (str (if (es?) "Enlace" "Link") (when (= embed-mode :present) " · release"))
451
+ fallback (fn [e]
452
+ (when e (repl-print! "err" (esc (str "; " (or (.-message e) e)))))
453
+ (when (and embed-mode (nil? (:kind @work))) (open-work! :local "local" "Local"))
454
+ (present-what!))]
455
+ (-> (cond
456
+ (seq code) (.then ((.-kairoticUnpack js/window) code)
457
+ (fn [src] (open-link-work! link-name (reset! link-base (link-code src)))))
458
+ ;; the code itself, readable (written by hand, or by Claude in a Notion page)
459
+ (seq src) (.then (js/Promise.resolve nil) (fn [_] (open-link-work! link-name (reset! link-base (link-code src)))))
460
+ (seq ask) (.then (js/Promise.resolve nil) (fn [_] (open-link-work! link-name "")))
461
+ (seq tut) (.then (tutorials-ready)
462
+ (fn [_] (or (open-tutorial! tut) (throw (js/Error. (str "no tutorial " (pr-str tut)))))))
463
+ (seq hc) (or (open-help-patch! hc) (js/Promise.reject (js/Error. (str "no help patch for " (pr-str hc)))))
464
+ :else (js/Promise.resolve nil))
465
+ (.then (fn [_]
466
+ (when (and @lesson (> step 1)) (lesson-go! (dec step)))
467
+ (when (and ask (= "link" (:id @work))) (setup-ask! ask @link-base))
468
+ (fallback nil)))
469
+ (.catch fallback))))
@@ -0,0 +1,61 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 12 of 12: startup
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── startup ─────────────────────────
8
+
9
+ (render-all!)
10
+ (render-comp!)
11
+ (render-controls!)
12
+ (render-manager!)
13
+ (repl-print! "out" (str "; Kairotic " version " — " (cond (.-claude js/window) "inside claude.ai: works, rooms and Claude come from claude.ai." (backend) "on its own: works and rooms in GUN (your solo works are encrypted in this browser), Claude with your own API key (Claude tab)." :else "without a database: local works only.")))
14
+ (repl-print! "out" "; New here? The Help tab has the tutorials and a page for every class (select a box and press ?). The Claude tab makes patches and components from a description. Share/Export gives a link to a patch, or puts it in another page.")
15
+
16
+ ;; on Pages, the address carries ?peer= while this page's relays are not the site's
17
+ (address-carries-relay!)
18
+
19
+ (let [h (.-hash js/location) m (re-matches #"#room-(.+)" (str h))]
20
+ (when m (swap! mgr assoc :invite (second m))))
21
+
22
+ (defn start-backend! []
23
+ (-> (js/Promise.all #js [(cuse "db") (cuse "user") (cuse "room")])
24
+ (.then (fn [arr]
25
+ (let [db (aget arr 0) user (aget arr 1) room (aget arr 2)]
26
+ (reset! userref user)
27
+ (reset! roomref room)
28
+ (when room
29
+ (.onPeers room
30
+ (fn [ch]
31
+ (reset! lobby-peers
32
+ (vec (for [p (array-seq (.-peers ch))]
33
+ {:me (.-isMe p) :room (let [s (.. p -presence -room)] (when (string? s) s))}))))
34
+ (fn [_] nil))
35
+ (lobby-presence!))
36
+ (if (nil? db)
37
+ (do (swap! hist assoc :mode :none :writable false) (render-manager!))
38
+ (-> (js/Promise.all #js [(if user (.id user) (js/Promise.resolve nil))
39
+ (if user (.can user "data.write") (js/Promise.resolve nil))])
40
+ (.then (fn [r]
41
+ (let [id (aget r 0) can (aget r 1)
42
+ writable (boolean (and id (not (false? can))))]
43
+ (swap! me assoc :id id)
44
+ (reset! dbref db)
45
+ (listen-library! db)
46
+ (swap! hist assoc :mode :none :writable writable)
47
+ (.onSnapshot (.collection db "rooms")
48
+ (fn [snap] (reset! rooms (into {} (for [d (array-seq (.-docs snap)) :when (.-exists d)] (let [s (meta-of d)] [(:id s) s])))))
49
+ db-failed!)
50
+ (when id
51
+ (.onSnapshot (.collection db (str "data/users/" id))
52
+ (fn [snap] (reset! solo-works (into {} (for [d (array-seq (.-docs snap)) :when (.-exists d)] (let [s (meta-of d)] [(:id s) s])))))
53
+ (fn [_] nil)))
54
+ (render-manager!)))))))))
55
+ (.catch (fn [_] (swap! hist assoc :mode :none) (render-manager!)))))
56
+
57
+ ;; an embed works on its own (no database, no relay); the whole editor connects first, then opens
58
+ ;; what the address asks for
59
+ (if embed-mode
60
+ (start-from-url!)
61
+ (.then (start-backend!) (fn [_] (start-from-url!))))