pico-engine 1.9.4 → 1.10.0

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 (186) hide show
  1. package/README.md +13 -5
  2. package/demos/bootstrap/README.md +12 -0
  3. package/demos/bootstrap/com.example.visitors.bootstrap.krl +101 -0
  4. package/demos/bootstrap/com.example.visitors.format.krl +25 -0
  5. package/demos/bootstrap/com.example.visitors.krl +228 -0
  6. package/demos/bootstrap/try-it.md +5 -0
  7. package/demos/child_tasks/README.md +12 -0
  8. package/demos/child_tasks/com.example.task_runner.bootstrap.krl +88 -0
  9. package/demos/child_tasks/com.example.task_runner.krl +139 -0
  10. package/demos/child_tasks/com.example.task_worker.krl +45 -0
  11. package/demos/child_tasks/try-it.md +5 -0
  12. package/demos/collection/README.md +13 -0
  13. package/demos/collection/com.example.collection.bootstrap.krl +148 -0
  14. package/demos/collection/com.example.collection.krl +95 -0
  15. package/demos/collection/com.example.collection.report.krl +93 -0
  16. package/demos/collection/com.example.collection.room.krl +101 -0
  17. package/demos/collection/try-it.md +6 -0
  18. package/demos/echo/README.md +11 -0
  19. package/demos/echo/com.example.echo.bootstrap.krl +84 -0
  20. package/demos/echo/com.example.echo.krl +73 -0
  21. package/demos/echo/try-it.md +5 -0
  22. package/demos/guard_rules/README.md +12 -0
  23. package/demos/guard_rules/com.example.archive.bootstrap.krl +98 -0
  24. package/demos/guard_rules/com.example.archive.krl +213 -0
  25. package/demos/guard_rules/com.example.archive_store.krl +36 -0
  26. package/demos/guard_rules/try-it.md +5 -0
  27. package/demos/json_query/README.md +11 -0
  28. package/demos/json_query/com.example.json_query.bootstrap.krl +84 -0
  29. package/demos/json_query/com.example.json_query.krl +162 -0
  30. package/demos/json_query/try-it.md +5 -0
  31. package/demos/kblog/README.md +14 -0
  32. package/demos/kblog/com.example.kblog.bootstrap.krl +135 -0
  33. package/demos/kblog/com.example.kblog.config.krl +50 -0
  34. package/demos/kblog/com.example.kblog.data.krl +75 -0
  35. package/demos/kblog/com.example.kblog.krl +184 -0
  36. package/demos/kblog/com.example.kblog.posting.krl +109 -0
  37. package/demos/kblog/try-it.md +6 -0
  38. package/demos/layered_rulesets/README.md +13 -0
  39. package/demos/layered_rulesets/com.example.invitations.bootstrap.krl +87 -0
  40. package/demos/layered_rulesets/com.example.invitations.krl +164 -0
  41. package/demos/layered_rulesets/com.example.invitations.short_links.krl +45 -0
  42. package/demos/layered_rulesets/com.example.invitations.welcome.krl +39 -0
  43. package/demos/layered_rulesets/try-it.md +5 -0
  44. package/demos/moving_picos/README.md +15 -0
  45. package/demos/moving_picos/io.picolabs.club_gear_demo.krl +650 -0
  46. package/demos/moving_picos/io.picolabs.ride_coordinator_demo.krl +331 -0
  47. package/demos/score_tracker/README.md +11 -0
  48. package/demos/score_tracker/com.example.score_tracker.bootstrap.krl +107 -0
  49. package/demos/score_tracker/com.example.score_tracker.krl +162 -0
  50. package/demos/score_tracker/try-it.md +5 -0
  51. package/demos/secrets/README.md +12 -0
  52. package/demos/secrets/com.example.hook_service.krl +120 -0
  53. package/demos/secrets/com.example.notifier.bootstrap.krl +130 -0
  54. package/demos/secrets/com.example.notifier.krl +138 -0
  55. package/demos/secrets/try-it.md +5 -0
  56. package/demos/try-it-order.json +1 -0
  57. package/demos/two_picos/README.md +12 -0
  58. package/demos/two_picos/com.example.two_picos.answerer.krl +125 -0
  59. package/demos/two_picos/com.example.two_picos.asker.krl +124 -0
  60. package/demos/two_picos/com.example.two_picos.bootstrap.krl +134 -0
  61. package/demos/two_picos/try-it.md +5 -0
  62. package/demos/xmlui/README.md +17 -0
  63. package/demos/xmlui/com.example.contacts.krl +132 -0
  64. package/demos/xmlui/com.example.fav_color.krl +39 -0
  65. package/demos/xmlui/com.example.guestbook.krl +61 -0
  66. package/demos/xmlui/com.example.hello.krl +19 -0
  67. package/demos/xmlui/com.example.temperature_sensors_view.krl +105 -0
  68. package/demos/xmlui/com.example.thermostat.bootstrap.krl +95 -0
  69. package/demos/xmlui/com.example.thermostat.krl +124 -0
  70. package/demos/xmlui/temperature_sensors.krl +56 -0
  71. package/demos/xmlui/try-it.md +5 -0
  72. package/dist/bundledKrl.js +97 -12
  73. package/dist/cli.js +7 -1
  74. package/dist/engineConfig.js +20 -1
  75. package/dist/identity/portableMove.js +5 -1
  76. package/dist/index.js +45 -12
  77. package/dist/internalAccess.js +110 -0
  78. package/dist/oauth/OAuthService.js +172 -10
  79. package/dist/oauth/acg.js +194 -24
  80. package/dist/oauth/cimd.js +381 -0
  81. package/dist/oauth/discovery.js +175 -0
  82. package/dist/runMcp.js +1 -1
  83. package/dist/server.js +210 -47
  84. package/dist/subtreeWrangler.js +8 -5
  85. package/dist/uiChannelPolicies.js +2 -0
  86. package/krl/io.picolabs.pds.krl +109 -21
  87. package/krl/io.picolabs.pico-engine-ui.krl +13 -7
  88. package/krl/io.picolabs.wrangler.krl +21 -9
  89. package/krl/io.picolabs.xmlui.krl +3 -2
  90. package/mcp/devResources.js +52 -0
  91. package/mcp/devTools.js +40 -9
  92. package/mcp/docs/CLAUDE.md +6 -4
  93. package/mcp/docs/cookbook/README.md +23 -0
  94. package/mcp/docs/cookbook/bootstrap/README.md +23 -0
  95. package/mcp/docs/cookbook/bootstrap/instructions.md +75 -0
  96. package/mcp/docs/cookbook/child_tasks/README.md +20 -0
  97. package/mcp/docs/cookbook/child_tasks/instructions.md +60 -0
  98. package/mcp/docs/cookbook/collection/README.md +21 -0
  99. package/mcp/docs/cookbook/collection/instructions.md +93 -0
  100. package/mcp/docs/cookbook/echo/README.md +19 -0
  101. package/mcp/docs/cookbook/echo/instructions.md +59 -0
  102. package/mcp/docs/cookbook/guard_rules/README.md +22 -0
  103. package/mcp/docs/cookbook/guard_rules/instructions.md +73 -0
  104. package/mcp/docs/cookbook/json_query/README.md +26 -0
  105. package/mcp/docs/cookbook/json_query/instructions.md +64 -0
  106. package/mcp/docs/cookbook/kblog/README.md +60 -0
  107. package/mcp/docs/cookbook/kblog/instructions.md +102 -0
  108. package/mcp/docs/cookbook/layered_rulesets/README.md +24 -0
  109. package/mcp/docs/cookbook/layered_rulesets/instructions.md +87 -0
  110. package/mcp/docs/cookbook/moving_picos/README.md +44 -0
  111. package/mcp/docs/cookbook/moving_picos/instructions.md +150 -0
  112. package/mcp/docs/cookbook/score_tracker/README.md +19 -0
  113. package/mcp/docs/cookbook/score_tracker/instructions.md +73 -0
  114. package/mcp/docs/cookbook/secrets/README.md +25 -0
  115. package/mcp/docs/cookbook/secrets/instructions.md +98 -0
  116. package/mcp/docs/cookbook/two_picos/README.md +20 -0
  117. package/mcp/docs/cookbook/two_picos/instructions.md +88 -0
  118. package/mcp/docs/cookbook/xmlui/README.md +23 -0
  119. package/mcp/docs/cookbook/xmlui/instructions.md +84 -0
  120. package/mcp/docs/developing/README.md +28 -0
  121. package/mcp/docs/developing/accepting-relationship-requests.md +65 -0
  122. package/mcp/docs/developing/arrays-and-maps.md +77 -0
  123. package/mcp/docs/developing/converting-time-formats.md +28 -0
  124. package/mcp/docs/developing/cookies-and-redirects.md +81 -0
  125. package/mcp/docs/developing/decoding-form-data.md +67 -0
  126. package/mcp/docs/developing/engine-log-files.md +34 -0
  127. package/mcp/docs/developing/event-loop.md +77 -0
  128. package/mcp/docs/developing/events-from-email.md +80 -0
  129. package/mcp/docs/developing/initializing-entity-variables.md +83 -0
  130. package/mcp/docs/developing/mcp-ai-tools.md +98 -0
  131. package/mcp/docs/developing/misconceptions.md +467 -0
  132. package/mcp/docs/developing/names-and-scope.md +44 -0
  133. package/mcp/docs/developing/postman.md +159 -0
  134. package/mcp/docs/developing/querying-another-pico.md +47 -0
  135. package/mcp/docs/developing/singleton-picos.md +36 -0
  136. package/mcp/docs/developing/tabular-data.md +67 -0
  137. package/mcp/docs/developing/tips.md +251 -0
  138. package/mcp/docs/developing/webhook-signatures.md +56 -0
  139. package/mcp/docs/developing/workflow.md +106 -0
  140. package/mcp/docs/exp/README.md +46 -0
  141. package/mcp/docs/exp/event.md +110 -0
  142. package/mcp/docs/exp/query.md +94 -0
  143. package/mcp/docs/lessons/README.md +59 -0
  144. package/mcp/docs/lessons/events-and-queries.md +247 -0
  145. package/mcp/docs/lessons/gossip.md +28 -0
  146. package/mcp/docs/lessons/modules-and-apis.md +270 -0
  147. package/mcp/docs/lessons/pico-based-systems.md +470 -0
  148. package/mcp/docs/lessons/pico-state.md +267 -0
  149. package/mcp/docs/lessons/pico-views.md +290 -0
  150. package/mcp/docs/lessons/quickstart.md +157 -0
  151. package/mcp/docs/lessons/relationships.md +530 -0
  152. package/mcp/docs/lessons/scatter-gather.md +362 -0
  153. package/mcp/docs/pico-engine.md +10 -5
  154. package/mcp/docs/pico-overview.md +3 -3
  155. package/mcp/docs/wrangler/README.md +47 -0
  156. package/mcp/docs/wrangler/channels.md +235 -0
  157. package/mcp/docs/wrangler/oauth.md +56 -0
  158. package/mcp/docs/wrangler/pds.md +423 -0
  159. package/mcp/docs/wrangler/portable-subtree.md +44 -0
  160. package/mcp/docs/wrangler/relationships.md +297 -0
  161. package/mcp/docs/wrangler/wrangler.md +253 -0
  162. package/mcp/docs/wrangler/xmlui.md +60 -0
  163. package/mcp/engineClient.js +57 -5
  164. package/mcp/mcpScopes.js +118 -0
  165. package/mcp/meshHttp.js +151 -45
  166. package/mcp/runStdio.js +2 -0
  167. package/mcp/subtreeTools.js +184 -0
  168. package/mcp/tryItTools.js +202 -0
  169. package/package.json +18 -9
  170. package/public/.DS_Store +0 -0
  171. package/public/pico-engine-ui.css +1 -1
  172. package/public/pico-engine-ui.js +26 -25
  173. package/src/bundledKrl.ts +118 -12
  174. package/src/cli.ts +8 -2
  175. package/src/engineConfig.ts +33 -2
  176. package/src/identity/portableMove.ts +6 -1
  177. package/src/index.ts +469 -422
  178. package/src/internalAccess.ts +127 -0
  179. package/src/oauth/OAuthService.ts +243 -11
  180. package/src/oauth/acg.ts +289 -25
  181. package/src/oauth/cimd.ts +508 -0
  182. package/src/oauth/discovery.ts +233 -0
  183. package/src/runMcp.ts +3 -1
  184. package/src/server.ts +266 -51
  185. package/src/subtreeWrangler.ts +17 -5
  186. package/src/uiChannelPolicies.ts +2 -0
@@ -0,0 +1,162 @@
1
+ /**
2
+ * Querying JSON: the query() operator finds values in a map or array with a
3
+ * JSONPath expression. The pico holds a fleet's vehicles, trips, and fill-ups
4
+ * (the kind of data Fuse kept), and the page runs canned queries against it, or
5
+ * one you type.
6
+ */
7
+ ruleset com.example.json_query {
8
+ meta {
9
+ name "Querying JSON"
10
+ description "Finds values in nested JSON with the query() operator and JSONPath."
11
+ author "PICOLABS"
12
+
13
+ use module io.picolabs.xmlui alias ui
14
+
15
+ shares queryPage, fleetData, cannedQueries, runQuery
16
+ }
17
+
18
+ global {
19
+ channel_tags = ["json_query", "app"]
20
+
21
+ fleet = {
22
+ "owner": {"name": "Ada Lovelace", "email": "ada@example.com"},
23
+ "vehicles": [
24
+ {
25
+ "name": "Work truck",
26
+ "make": "Ford", "model": "F-150", "year": 2022, "odometer": 48210,
27
+ "trips": [
28
+ {"date": "2026-11-02", "miles": 42.6, "category": "business", "cost": 9.8},
29
+ {"date": "2026-11-03", "miles": 3.1, "category": "personal", "cost": 0.71},
30
+ {"date": "2026-11-05", "miles": 118.4, "category": "business", "cost": 27.23}
31
+ ],
32
+ "fillups": [
33
+ {"date": "2026-10-28", "gallons": 22.1, "price": 3.39},
34
+ {"date": "2026-11-06", "gallons": 19.8, "price": 3.29}
35
+ ]
36
+ },
37
+ {
38
+ "name": "Family car",
39
+ "make": "Toyota", "model": "Prius", "year": 2019, "odometer": 91877,
40
+ "trips": [
41
+ {"date": "2026-11-01", "miles": 12.0, "category": "personal", "cost": 1.2},
42
+ {"date": "2026-11-04", "miles": 27.5, "category": "business", "cost": 2.75}
43
+ ],
44
+ "fillups": [
45
+ {"date": "2026-11-03", "gallons": 8.4, "price": 3.35}
46
+ ]
47
+ },
48
+ {
49
+ "name": "Commuter",
50
+ "make": "Chevrolet", "model": "Bolt", "year": 2023, "odometer": 15302,
51
+ "trips": [
52
+ {"date": "2026-11-02", "miles": 31.2, "category": "business", "cost": 1.4}
53
+ ],
54
+ "fillups": []
55
+ }
56
+ ]
57
+ }
58
+
59
+ /**
60
+ * The fleet data the queries run against.
61
+ * @returns Map
62
+ */
63
+ fleetData = function() {
64
+ fleet
65
+ }
66
+
67
+ /**
68
+ * Four queries to try, each showing a different part of JSONPath.
69
+ * @returns Array
70
+ */
71
+ cannedQueries = function() {
72
+ [
73
+ {
74
+ "label": "Every vehicle's name",
75
+ "path": "$.vehicles[*].name",
76
+ "explain": "Child selectors (.vehicles, .name) and the wildcard [*], which takes every element of the array."
77
+ },
78
+ {
79
+ "label": "All the miles, at any depth",
80
+ "path": "$..miles",
81
+ "explain": "The descendant selector .. finds a member wherever it is, without spelling out the path to it."
82
+ },
83
+ {
84
+ "label": "Business trips over 25 miles",
85
+ "path": "$.vehicles[*].trips[?@.category == 'business' && @.miles > 25]",
86
+ "explain": "A filter [?...] keeps the trips for which the test is true. @ is the trip being tested."
87
+ },
88
+ {
89
+ "label": "Each vehicle's latest fill-up",
90
+ "path": "$.vehicles[?count(@.fillups[*]) > 0].fillups[-1]",
91
+ "explain": "A filter that calls count() skips the electric car, and [-1] takes the last element of each fillups array."
92
+ }
93
+ ]
94
+ }
95
+
96
+ /**
97
+ * Run a JSONPath query against the fleet data.
98
+ * @param path String "a JSONPath expression, starting with $"
99
+ * @returns Map
100
+ */
101
+ runQuery = function(path) {
102
+ result = fleet.query(path)
103
+ answer = {"path": path, "count": result.length(), "result": result}
104
+ answer
105
+ }
106
+
107
+ /**
108
+ * Show the data, run a canned query, or type your own.
109
+ * @view "Querying JSON"
110
+ */
111
+ queryPage = function() {
112
+ data_url = ui:query_url("fleetData", meta:rid, meta:eci)
113
+ canned_url = ui:query_url("cannedQueries", meta:rid, meta:eci)
114
+ run_url = ui:query_url("runQuery", meta:rid, meta:eci)
115
+ <<
116
+ <Component name="QueryPage" var.showData="{false}" var.path="" var.label="" var.explain="">
117
+ <DataSource id="data" url="#{data_url}" headers="{appGlobals.auth}" />
118
+ <DataSource id="canned" url="#{canned_url}" headers="{appGlobals.auth}" />
119
+ <DataSource id="run" url="#{run_url}" headers="{appGlobals.auth}" queryParams="{{path: path}}" when="{path}" />
120
+ <PicoTheme>
121
+ <Card title="Querying JSON"
122
+ subtitle="This pico holds a fleet's vehicles, trips, and fill-ups. Each button runs fleet.query(path) on the pico and shows what it returns." />
123
+ <Button label="{showData ? 'Hide the JSON' : 'Show the JSON'}" onClick="showData = !showData" />
124
+ <FlowLayout>
125
+ <VStack width="{showData ? '50%' : '100%'}">
126
+ <H3>Try a query</H3>
127
+ <HStack wrapContent="true">
128
+ <Items data="{canned.value}">
129
+ <Button label="{$item.label}" variant="outlined"
130
+ onClick="path = $item.path; label = $item.label; explain = $item.explain" />
131
+ </Items>
132
+ </HStack>
133
+ <Text>Or type your own JSONPath:</Text>
134
+ <HStack>
135
+ <TextBox id="own" initialValue="$.owner.email" width="*" />
136
+ <Button label="Run it" onClick="path = own.value; label = 'Your query'; explain = ''" />
137
+ </HStack>
138
+ <Card when="{path}" title="{label}">
139
+ <Text variant="code">fleet.query("{path}")</Text>
140
+ <Text when="{explain}">{explain}</Text>
141
+ <Text when="{run.value.error}" variant="secondary">{run.value.error}</Text>
142
+ <Text when="{run.value.result}" variant="secondary">{run.value.count} {run.value.count == 1 ? 'value' : 'values'}:</Text>
143
+ <Text when="{run.value.result}" variant="mono" preserveLinebreaks="true" value="{JSON.stringify(run.value.result, null, 2)}" />
144
+ </Card>
145
+ </VStack>
146
+ <Card when="{showData}" width="50%" title="The fleet data">
147
+ <ScrollViewer height="520px">
148
+ <Text variant="mono" preserveLinebreaks="true" value="{JSON.stringify(data.value, null, 2)}" />
149
+ </ScrollViewer>
150
+ </Card>
151
+ </FlowLayout>
152
+ </PicoTheme>
153
+ </Component>
154
+ >>
155
+ }
156
+ }
157
+
158
+ rule create_app_channel {
159
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
160
+ ui:create_app_channel(channel_tags, [], ["queryPage", "fleetData", "cannedQueries", "runQuery"], meta:rid)
161
+ }
162
+ }
@@ -0,0 +1,5 @@
1
+ - Open **Query page** and click **Show the JSON**. The fleet data appears beside the queries, so you can check each answer against it.
2
+ - Click each of the four query buttons. Each shows the JSONPath it ran, what that path uses, and the values `query()` returned, always as an array.
3
+ - Type your own query and click **Run it**. Try `$.vehicles[?@.year > 2020].name`, `$..cost`, or `$.vehicles[0].trips[-1:]`.
4
+ - Type a broken path, such as `$.vehicles[?@.year >`. The pico answers with the parse error instead of a result.
5
+ - In the code, `runQuery` is one line of KRL: `fleet.query(path)`. Edit the `cannedQueries` list, click **Flush**, and reload the page to add your own buttons.
@@ -0,0 +1,14 @@
1
+ # KBlog demo
2
+
3
+ A blog written in KRL: four rulesets on one pico that work together by raising events, with a reader channel and an author channel. A new version of the [2011 KBlog](https://github.com/windley/KBlog).
4
+
5
+ - Overview: [`docs/cookbook/kblog/README.md`](../../docs/cookbook/kblog/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/kblog/)
6
+ - Instructions: [`docs/cookbook/kblog/instructions.md`](../../docs/cookbook/kblog/instructions.md)
7
+
8
+ | File | Shows |
9
+ |------|-------|
10
+ | `com.example.kblog.krl` | The pages (Home, an article page, Contact, Post) and the reader channel |
11
+ | `com.example.kblog.data.krl` | The articles: adds one on `kblog:new_article_available`, clears them, and provides them |
12
+ | `com.example.kblog.posting.krl` | Posting: checks each submitted post, provides the post form, and creates the author channel |
13
+ | `com.example.kblog.config.krl` | The title and the about and contact text, as a module |
14
+ | `com.example.kblog.bootstrap.krl` | Installs them in order on a new pico and submits three posts; the **Try It!** tab runs it |
@@ -0,0 +1,135 @@
1
+ /**
2
+ * Try It! bootstrap for KBlog. Install it on a new pico: it installs
3
+ * io.picolabs.xmlui and the four KBlog rulesets, one after another, each when
4
+ * the one it needs is in place. Then it submits three posts the way an author
5
+ * would. tryIt() reports progress and links to the blog on the reader and
6
+ * author channels.
7
+ *
8
+ * This ruleset does not use io.picolabs.xmlui as a module, because it is the
9
+ * one that installs it.
10
+ */
11
+ ruleset com.example.kblog.bootstrap {
12
+ meta {
13
+ name "KBlog"
14
+ description "Shows an application built from cooperating rulesets: an event hierarchy across rulesets, writes as events and reads as queries, multi-page views with routes, a component from another ruleset, and reader and author channels."
15
+ author "PICOLABS"
16
+
17
+ use module io.picolabs.wrangler alias wrangler
18
+
19
+ shares tryIt
20
+ }
21
+
22
+ global {
23
+ xmlui_rid = "io.picolabs.xmlui"
24
+ config_rid = "com.example.kblog.config"
25
+ data_rid = "com.example.kblog.data"
26
+ kblog_rid = "com.example.kblog"
27
+ posting_rid = "com.example.kblog.posting"
28
+ posts = [
29
+ {
30
+ "author": "Phil Windley",
31
+ "title": "Event hierarchies",
32
+ "body": "Events are nouns and rules are verbs. Each rule in KBlog does one thing and raises an event saying what is now true. Post something and watch the **event path**."
33
+ },
34
+ {
35
+ "author": "Phil Windley",
36
+ "title": "Rulesets in layers",
37
+ "body": "Posting is its own ruleset. Its install creates the author channel, the only channel that can submit a post."
38
+ },
39
+ {
40
+ "author": "Code Monkey",
41
+ "title": "Code Monkey was here",
42
+ "body": ":)"
43
+ }
44
+ ]
45
+
46
+ host = function() {
47
+ meta:host.defaultsTo("").replace(re#/$#, "")
48
+ }
49
+
50
+ link = function(tags, label, path) {
51
+ channel = wrangler:channels(tags).head()
52
+ channel => {"label": label, "url": host() + "/ui/" + channel{"id"} + "/" + kblog_rid + "/" + path} | null
53
+ }
54
+
55
+ /**
56
+ * Progress of this demo's setup, and the links to open when it is ready.
57
+ * @returns Map
58
+ */
59
+ tryIt = function() {
60
+ links = [
61
+ link(["kblog", "reader"], "Read the blog (reader channel)", ""),
62
+ link(["kblog", "author"], "Write a post (author channel)", "#/new")
63
+ ].filter(function(l) { l })
64
+ ready = (ent:stage == "ready" && links.length() == 2) => true | false
65
+ status = {"ready": ready, "stage": ent:stage.defaultsTo("starting"), "links": ready => links | []}
66
+ status
67
+ }
68
+ }
69
+
70
+ rule start {
71
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
72
+ pre {
73
+ has_xmlui = wrangler:installedRIDs() >< xmlui_rid
74
+ }
75
+ if not has_xmlui then noop()
76
+ fired {
77
+ raise wrangler event "install_ruleset_request" attributes
78
+ {"url": host() + "/krl/" + xmlui_rid + ".krl"}
79
+ }
80
+ else {
81
+ raise wrangler event "install_ruleset_request" attributes
82
+ {"absoluteURL": meta:rulesetURI, "rid": config_rid}
83
+ }
84
+ finally {
85
+ ent:stage := "installing"
86
+ raise wrangler event "new_channel_request" attributes {
87
+ "tags": ["try_it"],
88
+ "eventPolicy": {"allow": [], "deny": []},
89
+ "queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
90
+ }
91
+ }
92
+ }
93
+
94
+ rule install_config {
95
+ select when wrangler ruleset_installed where event:attr("rids") >< xmlui_rid
96
+ always {
97
+ raise wrangler event "install_ruleset_request" attributes
98
+ {"absoluteURL": meta:rulesetURI, "rid": config_rid}
99
+ }
100
+ }
101
+
102
+ rule install_data {
103
+ select when wrangler ruleset_installed where event:attr("rids") >< config_rid
104
+ always {
105
+ raise wrangler event "install_ruleset_request" attributes
106
+ {"absoluteURL": meta:rulesetURI, "rid": data_rid}
107
+ }
108
+ }
109
+
110
+ // The pages use the config and data rulesets as modules, so they come next.
111
+ rule install_pages {
112
+ select when wrangler ruleset_installed where event:attr("rids") >< data_rid
113
+ always {
114
+ raise wrangler event "install_ruleset_request" attributes
115
+ {"absoluteURL": meta:rulesetURI, "rid": kblog_rid}
116
+ }
117
+ }
118
+
119
+ rule install_posting {
120
+ select when wrangler ruleset_installed where event:attr("rids") >< kblog_rid
121
+ always {
122
+ raise wrangler event "install_ruleset_request" attributes
123
+ {"absoluteURL": meta:rulesetURI, "rid": posting_rid}
124
+ }
125
+ }
126
+
127
+ rule submit_posts {
128
+ select when wrangler ruleset_installed where event:attr("rids") >< posting_rid
129
+ foreach posts setting(post)
130
+ always {
131
+ raise kblog event "post_submitted" attributes post
132
+ ent:stage := "ready" on final
133
+ }
134
+ }
135
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * KBlog configuration: the blog's title and the text of its fixed pages, in one
3
+ * place. The other KBlog rulesets use it as a module.
4
+ */
5
+ ruleset com.example.kblog.config {
6
+ meta {
7
+ name "KBlog configuration"
8
+ description "The blog's title, about text, and contact text."
9
+ author "PICOLABS"
10
+
11
+ provides blogtitle, about_text, contact_text
12
+ }
13
+
14
+ global {
15
+ // Modules provide functions; a provided plain value can't be called.
16
+
17
+ /**
18
+ * The blog's title.
19
+ * @returns String
20
+ */
21
+ blogtitle = function() {
22
+ "KBlog"
23
+ }
24
+
25
+ /**
26
+ * The about box, in Markdown.
27
+ * @returns String
28
+ */
29
+ about_text = function() {
30
+ <<
31
+ KBlog is a blog written in KRL. Four rulesets on one pico run it: one for the
32
+ pages, one for the articles, one for posting, and one for this text. Rules raise
33
+ events for each other; none of them knows how the others work.
34
+
35
+ It is a new version of the [2011 KBlog](https://www.windley.com/archives/2011/04/event_hierarchies_a_blog_written_in_krl.shtml).
36
+ >>
37
+ }
38
+
39
+ /**
40
+ * The Contact page, in Markdown.
41
+ * @returns String
42
+ */
43
+ contact_text = function() {
44
+ <<
45
+ Pico Labs builds the pico engine and KRL. Find us at [picolabs.io](https://picolabs.io)
46
+ and the code at [github.com/Picolab](https://github.com/Picolab).
47
+ >>
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * KBlog data: keeps the articles. It adds an article when
3
+ * kblog:new_article_available arrives and raises kblog:new_article_added, and
4
+ * provides the articles to the other KBlog rulesets. Nothing else stores or
5
+ * changes them.
6
+ */
7
+ ruleset com.example.kblog.data {
8
+ meta {
9
+ name "KBlog data"
10
+ description "Stores KBlog's articles and provides them to the other rulesets."
11
+ author "PICOLABS"
12
+
13
+ provides articles, article
14
+ shares articles, article
15
+ }
16
+
17
+ global {
18
+ /**
19
+ * Every article, newest first.
20
+ * @returns Array of { id, author, title, body, time }
21
+ */
22
+ articles = function() {
23
+ ent:articles.defaultsTo({}).values()
24
+ .sort(function(a, b) { b{"id"} <=> a{"id"} })
25
+ }
26
+
27
+ /**
28
+ * One article.
29
+ * @param id String "the article's id"
30
+ * @returns Map
31
+ */
32
+ article = function(id) {
33
+ ent:articles.defaultsTo({}){id.as("String")}
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Store a new article. The posting ruleset raises this after it checks a
39
+ * submitted post.
40
+ * @attr author String "who wrote it"
41
+ * @attr title String "the title"
42
+ * @attr body String optional "the text, in Markdown"
43
+ */
44
+ rule add_article {
45
+ select when kblog new_article_available
46
+ pre {
47
+ id = ent:next_id.defaultsTo(1)
48
+ entry = {
49
+ "id": id,
50
+ "author": event:attr("author"),
51
+ "title": event:attr("title"),
52
+ "body": event:attr("body") || "",
53
+ "time": time:now()
54
+ }
55
+ }
56
+ send_directive("add_article", {"id": id})
57
+ fired {
58
+ ent:next_id := id + 1
59
+ ent:articles{id.as("String")} := entry
60
+ raise kblog event "new_article_added" attributes entry
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Delete every article.
66
+ */
67
+ rule clear_articles {
68
+ select when kblog clear_requested
69
+ send_directive("clear_articles")
70
+ fired {
71
+ ent:articles := {}
72
+ raise kblog event "articles_cleared"
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,184 @@
1
+ /**
2
+ * KBlog: the pages. Home lists the articles, each article has its own page,
3
+ * and Contact and Post round it out. The articles come from
4
+ * com.example.kblog.data and the text from com.example.kblog.config. The Post
5
+ * page shows the PostForm component, which com.example.kblog.posting provides;
6
+ * only the author channel that ruleset creates may open it.
7
+ *
8
+ * On install it creates the reader channel: every page but Post, and no events.
9
+ */
10
+ ruleset com.example.kblog {
11
+ meta {
12
+ name "KBlog"
13
+ description "The pages of a blog written in KRL."
14
+ author "PICOLABS"
15
+
16
+ use module com.example.kblog.config alias config
17
+ use module com.example.kblog.data alias data
18
+ use module io.picolabs.xmlui alias ui
19
+
20
+ shares articles, article, blogAbout, homePage, articlePage, contactPage, postPage
21
+ }
22
+
23
+ global {
24
+ reader_tags = ["kblog", "reader"]
25
+
26
+ /**
27
+ * Every article, newest first. The latest one is marked.
28
+ * @returns Array of { id, author, title, body, time, latest }
29
+ */
30
+ articles = function() {
31
+ data:articles().map(function(a) { a.put("latest", a{"id"} == ent:latest) })
32
+ }
33
+
34
+ /**
35
+ * One article.
36
+ * @param id String "the article's id"
37
+ * @returns Map
38
+ */
39
+ article = function(id) {
40
+ data:article(id)
41
+ }
42
+
43
+ /**
44
+ * The about box beside every page.
45
+ * @component
46
+ */
47
+ blogAbout = function() {
48
+ <<
49
+ <Component name="BlogAbout">
50
+ <Card title="About" width="{$props.width}">
51
+ <Markdown openLinkInNewTab="true"><![CDATA[#{config:about_text()}]]></Markdown>
52
+ </Card>
53
+ </Component>
54
+ >>
55
+ }
56
+
57
+ /**
58
+ * The latest articles.
59
+ * @view "Home" /
60
+ */
61
+ homePage = function() {
62
+ articles_url = ui:query_url("articles", meta:rid, meta:eci)
63
+ <<
64
+ <Component name="HomePage">
65
+ <DataSource id="posts" url="#{articles_url}" headers="{appGlobals.auth}" pollIntervalInSeconds="5" />
66
+ <PicoTheme>
67
+ <HStack verticalAlignment="start" wrapContent="true">
68
+ <VStack width="*" minWidth="300px">
69
+ <H2>#{ui:escape(config:blogtitle())}: latest from the blog</H2>
70
+ <Text when="{posts.value &amp;&amp; posts.value.length === 0}">No articles yet.</Text>
71
+ <Items data="{posts}">
72
+ <Card>
73
+ <HStack verticalAlignment="center">
74
+ <Link to="/post/{$item.id}"><H3>{$item.title}</H3></Link>
75
+ <Badge when="{$item.latest}" value="New" />
76
+ </HStack>
77
+ <Text variant="secondary">by {$item.author}, {$item.time.substring(0, 16).replace('T', ' ')} UTC</Text>
78
+ <Markdown content="{$item.body}" allowHtml="false" interpolateBindings="false" />
79
+ </Card>
80
+ </Items>
81
+ </VStack>
82
+ <BlogAbout width="300px" />
83
+ </HStack>
84
+ </PicoTheme>
85
+ </Component>
86
+ >>
87
+ }
88
+
89
+ /**
90
+ * One article, at its own address.
91
+ * @view "Article" /post/:id
92
+ */
93
+ articlePage = function() {
94
+ <<
95
+ <Component name="ArticlePage">
96
+ #{ui:query("post", "article", {"rid": meta:rid, "args": {"id": "{$routeParams.id}"}})}
97
+ <PicoTheme>
98
+ <HStack verticalAlignment="start" wrapContent="true">
99
+ <VStack width="*" minWidth="300px">
100
+ <Link to="/">Back to the blog</Link>
101
+ <Text when="{post.loaded &amp;&amp; !post.value}">There is no article {$routeParams.id}.</Text>
102
+ <Card when="{post.value}">
103
+ <H2>{post.value.title}</H2>
104
+ <Text variant="secondary">by {post.value.author}, {post.value.time.substring(0, 16).replace('T', ' ')} UTC</Text>
105
+ <Markdown content="{post.value.body}" allowHtml="false" interpolateBindings="false" />
106
+ </Card>
107
+ </VStack>
108
+ <BlogAbout width="300px" />
109
+ </HStack>
110
+ </PicoTheme>
111
+ </Component>
112
+ >>
113
+ }
114
+
115
+ /**
116
+ * How to reach us.
117
+ * @view "Contact" /contact
118
+ */
119
+ contactPage = function() {
120
+ <<
121
+ <Component name="ContactPage">
122
+ <PicoTheme>
123
+ <HStack verticalAlignment="start" wrapContent="true">
124
+ <Card title="Contact" width="*" minWidth="300px">
125
+ <Markdown openLinkInNewTab="true"><![CDATA[#{config:contact_text()}]]></Markdown>
126
+ </Card>
127
+ <BlogAbout width="300px" />
128
+ </HStack>
129
+ </PicoTheme>
130
+ </Component>
131
+ >>
132
+ }
133
+
134
+ /**
135
+ * Write a post. The form comes from com.example.kblog.posting.
136
+ * @view "Post" /new
137
+ */
138
+ postPage = function() {
139
+ <<
140
+ <Component name="PostPage">
141
+ <PicoTheme>
142
+ <HStack verticalAlignment="start" wrapContent="true">
143
+ <VStack width="*" minWidth="300px">
144
+ <H2>Post</H2>
145
+ <PostForm />
146
+ </VStack>
147
+ <BlogAbout width="300px" />
148
+ </HStack>
149
+ </PicoTheme>
150
+ </Component>
151
+ >>
152
+ }
153
+ }
154
+
155
+ /**
156
+ * A new article was added: mark it as the latest, and say the front page has
157
+ * it.
158
+ */
159
+ rule show_new_article {
160
+ select when kblog new_article_added
161
+ send_directive("show_new_article", {"id": event:attr("id")})
162
+ fired {
163
+ ent:latest := event:attr("id")
164
+ raise kblog event "front_page_updated" attributes event:attrs
165
+ }
166
+ }
167
+
168
+ /**
169
+ * The articles were deleted: nothing is the latest any more.
170
+ */
171
+ rule show_empty_blog {
172
+ select when kblog articles_cleared
173
+ send_directive("show_empty_blog")
174
+ fired {
175
+ ent:latest := null
176
+ raise kblog event "front_page_updated"
177
+ }
178
+ }
179
+
180
+ rule create_reader_channel {
181
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
182
+ ui:create_app_channel(reader_tags, [], ["articles", "article", "homePage", "articlePage", "contactPage"], meta:rid)
183
+ }
184
+ }