@kairotic/app 0.0.0-stage → 0.2.1

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,108 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · code boxes
3
+ ;; The base of any programmable component: a box whose data is a source text in some
4
+ ;; language. The text is part of the work (it travels through the history and the rooms); each tab
5
+ ;; compiles it locally with the compiler registered for its language, and keeps the result in
6
+ ;; a cache keyed by source. Faust and ClojureScript are the first two languages; others (JavaScript,
7
+ ;; GLSL, a rule language…) only have to register their compiler and their component.
8
+
9
+ (ns kairotic.code
10
+ (:refer-clojure :exclude [empty])
11
+ (:require [clojure.string :as str]
12
+ [kairotic.components :as comp]))
13
+
14
+ ;; ───────── data (with history) ─────────
15
+
16
+ (def empty {:source ""})
17
+ ;; a code box may hold a whole package (a component box with every component of @kairotic/analysis is
18
+ ;; about 26 000 characters): longer sources are cut, and cut code does not read
19
+ (def max-source 100000)
20
+
21
+ (defn step [d [op & args]]
22
+ (let [d (or d empty)]
23
+ (case op
24
+ :source (let [[s] args] (if (string? s) (assoc d :source (subs s 0 (min (count s) max-source))) d))
25
+ :load (let [[x] args] (if (and (map? x) (string? (:source x))) {:source (subs (:source x) 0 (min (count (:source x)) max-source))} d))
26
+ d)))
27
+
28
+ (defn ->code [id d]
29
+ (when (seq (str/trim (str (:source d))))
30
+ [(str "(code " id " " (pr-str (:source d)) ")")]))
31
+
32
+ (def model {:empty empty :step step :load (fn [x] (step empty [:load x])) :code ->code})
33
+
34
+ ;; ───────── compilation ─────────
35
+ ;; compilers: language → (fn [source] promise of {:value x :info {…}})
36
+
37
+ (defonce compilers (atom {}))
38
+ (defonce cache (atom {})) ; [language source] → {:status :compiling|:ok|:error :error :value :info :n}
39
+ (defonce listeners (atom {})) ; key → (fn []) to notify when a compilation finishes
40
+ (defonce counter (atom 0))
41
+
42
+ (defn notify! [] (doseq [[_ f] @listeners] (try (f) (catch :default e (js/console.error e)))))
43
+
44
+ (defn register-language! [language f] (swap! compilers assoc language f) language)
45
+
46
+ (defn prune! []
47
+ (when (> (count @cache) 60)
48
+ (swap! cache (fn [c] (into {} (take-last 40 (sort-by (fn [[_ v]] (:n v)) c)))))))
49
+
50
+ (defn compiled
51
+ "Compilation status of a source; if it has not been requested yet, requests it (and notifies when done)."
52
+ [language source]
53
+ (let [source (str source) k [language source]]
54
+ (cond
55
+ (str/blank? source) {:status :empty}
56
+ (get @cache k) (get @cache k)
57
+ :else
58
+ (if-let [f (get @compilers language)]
59
+ (let [n (swap! counter inc)]
60
+ (swap! cache assoc k {:status :compiling :n n})
61
+ (prune!)
62
+ (-> (js/Promise.resolve nil)
63
+ (.then (fn [_] (f source)))
64
+ (.then (fn [r] (swap! cache assoc k (assoc r :status :ok :n n)) (notify!)))
65
+ (.catch (fn [e] (swap! cache assoc k {:status :error :n n :error (or (.-message e) (str e))}) (notify!))))
66
+ (get @cache k))
67
+ {:status :error :error (str "No compiler for " language)}))))
68
+
69
+ (defn forget-language! [language]
70
+ (swap! cache (fn [c] (into {} (remove (fn [[[l _] _]] (= l language)) c)))))
71
+
72
+ ;; ───────── script: ClojureScript inside the patch ─────────
73
+ ;; The source is a function (fn [m] …) with the same contract as any component: it receives
74
+ ;; {:inlet :value :state :args :at} and returns {:state :out [[o v]] :later … :effects …}.
75
+ ;; A script runs its writer's code inside this page, so in a shared room
76
+ ;; it is not evaluated until this tab allows it: (trust-scripts true).
77
+
78
+ (defonce trust (atom false))
79
+ ;; sources this page brought itself from the official library (Study 22): they run without asking
80
+ (defonce trusted (atom #{}))
81
+ (defn allowed? [source] (or @trust (contains? @trusted source)))
82
+
83
+ (register-language! "clojurescript"
84
+ (fn [source]
85
+ (js/Promise.
86
+ (fn [res rej]
87
+ (if-not (allowed? source)
88
+ (rej (js/Error. "not run: this work is shared; (trust-scripts true) to evaluate its scripts"))
89
+ (try
90
+ (let [f (js/scittle.core.eval_string (str "(do " source "\n)"))]
91
+ (if (fn? f) (res {:value f :info {:summary "ready"}}) (rej (js/Error. "a script is a function (fn [m] …)"))))
92
+ (catch :default e (rej e))))))))
93
+
94
+ (defn whole [x d] (if (number? x) (max 0 (js/Math.round x)) d))
95
+
96
+ (comp/define! "script"
97
+ {:doc "ClojureScript code box: its source is a function (fn [m] …) that receives and returns messages like any component."
98
+ :usage "script [inlets] [outlets]"
99
+ :display :code :language "clojurescript" :model model
100
+ :io (fn [c] (let [[i o] (:args c)] [(max 1 (whole i 1)) (whole o 1)]))
101
+ :template "(fn [{:keys [inlet value state]}]\n ;; return {:out [[outlet value]] :state …}\n {:out [[0 value]]})"
102
+ :receive (fn [m]
103
+ (let [r (compiled "clojurescript" (get-in m [:data :source]))]
104
+ (if (= :ok (:status r))
105
+ (try
106
+ (let [x ((:value r) m)] (if (map? x) x {}))
107
+ (catch :default e {:effects [[:print (str "script: " (or (.-message e) e))]]}))
108
+ {})))})