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,134 @@
1
+ /**
2
+ * Try It! bootstrap for the two picos demo. Install it on a new pico: it creates
3
+ * two children, Alice (com.example.two_picos.answerer) and Bob
4
+ * (com.example.two_picos.asker), and introduces Bob to Alice's well-known
5
+ * channel. Bob asks for a relationship, Alice accepts, and Bob asks his first
6
+ * question. tryIt() reports progress and links to both picos' pages.
7
+ */
8
+ ruleset com.example.two_picos.bootstrap {
9
+ meta {
10
+ name "Two picos talking"
11
+ description "Shows relationships: asking through a well-known channel, accepting by role, and one pico querying another over the relationship with subscription:queryOnSub."
12
+ author "PICOLABS"
13
+
14
+ use module io.picolabs.wrangler alias wrangler
15
+
16
+ shares tryIt
17
+ }
18
+
19
+ global {
20
+ answerer_rid = "com.example.two_picos.answerer"
21
+ asker_rid = "com.example.two_picos.asker"
22
+ xmlui_rid = "io.picolabs.xmlui"
23
+
24
+ host = function() {
25
+ meta:host.defaultsTo("").replace(re#/$#, "")
26
+ }
27
+
28
+ appLink = function(role, rid, label) {
29
+ child = ent:children{role}
30
+ channel = child => wrangler:picoQuery(child, "io.picolabs.wrangler", "channels",
31
+ {"tags": "two_picos," + role}).head() | null
32
+ channel => {"label": label, "url": host() + "/ui/" + channel{"id"} + "/" + rid + "/"} | null
33
+ }
34
+
35
+ /**
36
+ * Progress of this demo's setup, and the links to open when it is ready.
37
+ * @returns Map
38
+ */
39
+ tryIt = function() {
40
+ ready = ent:stage == "ready"
41
+ links = ready => [
42
+ appLink("asker", asker_rid, "Bob's conversation"),
43
+ appLink("answerer", answerer_rid, "What Alice knows")
44
+ ].filter(function(link) { link }) | []
45
+ status = {"ready": ready, "stage": ent:stage.defaultsTo("starting"), "links": links}
46
+ status
47
+ }
48
+ }
49
+
50
+ rule start {
51
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
52
+ always {
53
+ ent:stage := "creating picos"
54
+ raise wrangler event "new_channel_request" attributes {
55
+ "tags": ["try_it"],
56
+ "eventPolicy": {"allow": [], "deny": []},
57
+ "queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
58
+ }
59
+ raise wrangler event "new_child_request" attributes
60
+ {"name": "Alice", "two_picos_role": "answerer", "backgroundColor": "#ffe0b2"}
61
+ raise wrangler event "new_child_request" attributes
62
+ {"name": "Bob", "two_picos_role": "asker", "backgroundColor": "#bbdefb"}
63
+ }
64
+ }
65
+
66
+ // A child is set up: install io.picolabs.xmlui, then its role's ruleset.
67
+ rule set_up_child {
68
+ select when wrangler child_initialized where event:attr("two_picos_role")
69
+ pre {
70
+ role = event:attr("two_picos_role")
71
+ child = event:attr("eci")
72
+ rid = role == "answerer" => answerer_rid | asker_rid
73
+ }
74
+ every {
75
+ event:send({
76
+ "eci": child,
77
+ "domain": "wrangler",
78
+ "type": "install_ruleset_request",
79
+ "attrs": {"url": host() + "/krl/" + xmlui_rid + ".krl"}
80
+ })
81
+ event:send({
82
+ "eci": child,
83
+ "domain": "wrangler",
84
+ "type": "install_ruleset_request",
85
+ "attrs": {"absoluteURL": meta:rulesetURI, "rid": rid}
86
+ })
87
+ }
88
+ always {
89
+ ent:children{role} := child
90
+ }
91
+ }
92
+
93
+ // Both children have their rulesets: introduce Bob to Alice.
94
+ rule child_ready {
95
+ select when two_picos child_ready
96
+ role re#^(answerer|asker)$#
97
+ setting(role)
98
+ pre {
99
+ ready = ent:ready.defaultsTo({}).put(role, true)
100
+ both = (ready{"answerer"} && ready{"asker"}) => true | false
101
+ }
102
+ if both then noop()
103
+ fired {
104
+ raise two_picos event "both_ready"
105
+ }
106
+ finally {
107
+ ent:ready{role} := true
108
+ ent:stage := "installing rulesets"
109
+ }
110
+ }
111
+
112
+ rule introduce {
113
+ select when two_picos both_ready
114
+ pre {
115
+ well_known = wrangler:picoQuery(ent:children{"answerer"}, "io.picolabs.subscription", "wellKnown_Rx"){"id"}
116
+ }
117
+ event:send({
118
+ "eci": ent:children{"asker"},
119
+ "domain": "two_picos",
120
+ "type": "introduce",
121
+ "attrs": {"wellKnown_Tx": well_known}
122
+ })
123
+ always {
124
+ ent:stage := "introducing Bob to Alice"
125
+ }
126
+ }
127
+
128
+ rule connected {
129
+ select when two_picos connected
130
+ always {
131
+ ent:stage := "ready"
132
+ }
133
+ }
134
+ }
@@ -0,0 +1,5 @@
1
+ - Leave the **Try It!** tab and close the pico, so the developer UI shows the pico tree. The demo's child has two children, Alice and Bob.
2
+ - Open each one's **Relationships** tab. Each has one relationship: Bob is the `asker` and Alice is the `answerer`.
3
+ - On **Bob's conversation**, ask about `cat`, `hometown`, or `name`. Then ask about something Alice doesn't know yet.
4
+ - On **What Alice knows**, teach Alice that fact, then ask Bob to ask again.
5
+ - Bob never uses Alice's family channel or her page's channel. Look at `ask` in `com.example.two_picos.asker.krl`: every question goes over the relationship with `subscription:queryOnSub`.
@@ -0,0 +1,17 @@
1
+ # Pico views demos
2
+
3
+ Rulesets that serve XMLUI pages from a pico. The engine's view tests also use them.
4
+
5
+ - Overview: [`docs/cookbook/xmlui/README.md`](../../docs/cookbook/xmlui/README.md) (on the docs site: https://docs.picolabs.io/#/cookbook/xmlui/)
6
+ - Instructions: [`docs/cookbook/xmlui/instructions.md`](../../docs/cookbook/xmlui/instructions.md)
7
+
8
+ | File | Shows |
9
+ |------|-------|
10
+ | `com.example.hello.krl` | A `@view` and nothing else |
11
+ | `com.example.fav_color.krl` | A query and an event |
12
+ | `com.example.guestbook.krl` | An app channel created on install |
13
+ | `com.example.contacts.krl` | A list page and a detail page |
14
+ | `com.example.thermostat.krl` | Two channels, an app `@component`, and the shared components |
15
+ | `com.example.thermostat.bootstrap.krl` | Sets up the Thermostat demo on a new pico; the **Try It!** tab runs it |
16
+ | `temperature_sensors.krl` | Sample data for the next one |
17
+ | `com.example.temperature_sensors_view.krl` | A view over another pico's data |
@@ -0,0 +1,132 @@
1
+ ruleset com.example.contacts {
2
+ meta {
3
+ name "Contacts"
4
+ use module io.picolabs.xmlui alias ui
5
+ shares contacts, contact, contactsPage, detail
6
+ }
7
+ global {
8
+ /**
9
+ * All contacts, sorted by name
10
+ * @returns Array
11
+ */
12
+ contacts = function() {
13
+ ent:contacts.defaultsTo({}).values()
14
+ .sort(function(a, b) { a{"name"} cmp b{"name"} })
15
+ }
16
+
17
+ /**
18
+ * One contact
19
+ * @param id Contact id
20
+ * @returns Map
21
+ */
22
+ contact = function(id) {
23
+ ent:contacts{id}
24
+ }
25
+
26
+ /**
27
+ * Everyone, linking to each person's page
28
+ * @view "Contacts" /list
29
+ */
30
+ contactsPage = function() {
31
+ <<<Component name="ContactsPage">
32
+ #{ui:query("contacts", "contacts", {"rid": meta:rid})}
33
+ #{ui:event("add", "contacts", "contact_added", {"rid": meta:rid, "refresh": ["contacts"]})}
34
+ <Form onSubmit="(data) => add.execute(data)">
35
+ <FormItem bindTo="name" label="Name" required />
36
+ <FormItem bindTo="email" label="Email" />
37
+ </Form>
38
+ <Table data="{contacts}">
39
+ <property name="noDataTemplate">
40
+ <Text value="No contacts" variant="strong" />
41
+ </property>
42
+ <Column header="Name">
43
+ <Link to="/contact/{$item.id}">{$item.name}</Link>
44
+ </Column>
45
+ <Column bindTo="email" header="Email" />
46
+ </Table>
47
+ </Component>
48
+ >>
49
+ }
50
+
51
+ /**
52
+ * One person's details and notes
53
+ * @view "Contact" /contact/:id
54
+ */
55
+ detail = function() {
56
+ <<<Component name="Detail">
57
+ #{ui:query("person", "contact", {"rid": meta:rid, "args": {"id": "{$routeParams.id}"}})}
58
+ #{ui:event("note", "contacts", "note_added", {"rid": meta:rid, "refresh": ["person"]})}
59
+ <Link to="/list">Back to contacts</Link>
60
+ <H2>{person.value.name}</H2>
61
+ <Text>{person.value.email}</Text>
62
+ <List data="{person.value.notes}">
63
+ <Text>{$item}</Text>
64
+ </List>
65
+ <Form onSubmit="(toSave) => note.execute({id: $routeParams.id, text: toSave.text})">
66
+ <FormItem bindTo="text" label="Add a note" type="textarea" />
67
+ </Form>
68
+ </Component>
69
+ >>
70
+ }
71
+
72
+ channel_tags = ["contacts", "app"]
73
+ }
74
+
75
+ rule create_app_channel {
76
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
77
+ ui:create_app_channel(channel_tags, ["contacts"], null, meta:rid)
78
+ }
79
+
80
+ rule seed_demo_contacts {
81
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
82
+ pre {
83
+ empty = ent:contacts.defaultsTo({}).isEmpty()
84
+ }
85
+ if empty then noop()
86
+ fired {
87
+ raise contacts event "contact_added" attributes {
88
+ "name": "Ada Lovelace",
89
+ "email": "ada@example.com"
90
+ }
91
+ raise contacts event "contact_added" attributes {
92
+ "name": "Grace Hopper",
93
+ "email": "grace@example.com"
94
+ }
95
+ }
96
+ }
97
+
98
+ /**
99
+ * Add a contact
100
+ * @attr name Full name
101
+ * @attr email Email address
102
+ */
103
+ rule add_contact {
104
+ select when contacts contact_added
105
+ name re#(.+)# setting(name)
106
+ pre {
107
+ id = random:uuid()
108
+ }
109
+ fired {
110
+ ent:contacts{id} := {"id": id, "name": name, "email": event:attr("email"), "notes": []}
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Add a note to a contact
116
+ * @attr id Contact id
117
+ * @attr text The note
118
+ */
119
+ rule add_note {
120
+ select when contacts note_added
121
+ id re#(.+)#
122
+ text re#(.+)#
123
+ setting(id, text)
124
+ pre {
125
+ notes = ent:contacts{[id, "notes"]}
126
+ }
127
+ if notes then noop()
128
+ fired {
129
+ ent:contacts{[id, "notes"]} := notes.append(text)
130
+ }
131
+ }
132
+ }
@@ -0,0 +1,39 @@
1
+ ruleset com.example.fav_color {
2
+ meta {
3
+ name "Favorite color"
4
+ use module io.picolabs.xmlui alias ui
5
+ shares favColor, colorView
6
+ }
7
+ global {
8
+ /** Current favorite color */
9
+ favColor = function() {
10
+ {"name": ent:colorname, "code": ent:colorcode}
11
+ }
12
+
13
+ /**
14
+ * Pick a favorite color
15
+ * @view "Favorite color"
16
+ */
17
+ colorView = function() {
18
+ <<<Component name="ColorView">
19
+ #{ui:query("fav", "favColor", {"rid": meta:rid})}
20
+ <Text when="{!fav.value.name}">You have not yet selected a favorite color.</Text>
21
+ <Card when="{fav.value.name}" title="Your favorite color: {fav.value.name}" />
22
+ #{ui:event("pick", "fav_color", "fav_color_selected", {"rid": meta:rid, "refresh": ["fav"]})}
23
+ <Form onSubmit="(toSave) => pick.execute(toSave)">
24
+ <FormItem bindTo="fav_color" label="Color (#rrggbb, hex digits a–f or A–F)" />
25
+ </Form>
26
+ </Component>
27
+ >>
28
+ }
29
+ }
30
+
31
+ rule recordFavColor {
32
+ select when fav_color fav_color_selected
33
+ fav_color re#^(\#[a-fA-F0-9]{6})$# setting(fav_color)
34
+ fired {
35
+ ent:colorcode := fav_color
36
+ ent:colorname := fav_color
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,61 @@
1
+ ruleset com.example.guestbook {
2
+ meta {
3
+ name "Guestbook"
4
+ use module io.picolabs.xmlui alias ui
5
+ shares entries, book
6
+ }
7
+ global {
8
+ /**
9
+ * Guestbook entries, newest first
10
+ * @returns Array
11
+ */
12
+ entries = function() {
13
+ ent:entries.defaultsTo([]).reverse()
14
+ }
15
+
16
+ /**
17
+ * Sign and read the guestbook
18
+ * @view "Guestbook"
19
+ */
20
+ book = function() {
21
+ <<<Component name="Book">
22
+ #{ui:query("entries", "entries", {"rid": meta:rid})}
23
+ #{ui:event("sign", "guestbook", "signed", {"rid": meta:rid, "refresh": ["entries"]})}
24
+ <Form onSubmit="(toSave) => sign.execute(toSave)">
25
+ <FormItem bindTo="name" label="Name" />
26
+ <FormItem bindTo="message" label="Message" type="textarea" />
27
+ </Form>
28
+ <Table data="{entries}">
29
+ <Column bindTo="name" header="Name" />
30
+ <Column bindTo="message" header="Message" />
31
+ <Column bindTo="time" header="Signed" />
32
+ </Table>
33
+ </Component>
34
+ >>
35
+ }
36
+
37
+ channel_tags = ["guestbook", "app"]
38
+ }
39
+
40
+ /**
41
+ * Add an entry
42
+ * @attr name Who is signing
43
+ * @attr message What they wrote
44
+ */
45
+ rule sign {
46
+ select when guestbook signed
47
+ name re#(.+)# setting(name)
48
+ pre {
49
+ message = event:attr("message") || ""
50
+ }
51
+ fired {
52
+ ent:entries := ent:entries.defaultsTo([])
53
+ .append({"name": name, "message": message, "time": time:now()})
54
+ }
55
+ }
56
+
57
+ rule create_app_channel {
58
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
59
+ ui:create_app_channel(channel_tags, ["guestbook"], null, meta:rid)
60
+ }
61
+ }
@@ -0,0 +1,19 @@
1
+ ruleset com.example.hello {
2
+ meta {
3
+ name "Hello"
4
+ shares hello
5
+ }
6
+ global {
7
+ /**
8
+ * A first view
9
+ * @view "Hello"
10
+ */
11
+ hello = function() {
12
+ <<<Component name="Hello">
13
+ <H1>Hello</H1>
14
+ <Text>This view is a shared function in #{meta:rid}.</Text>
15
+ </Component>
16
+ >>
17
+ }
18
+ }
19
+ }
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Demo XMLUI view: shows readings from `com.example.temperature_sensors` on **another** pico.
3
+ *
4
+ * Install `demos/xmlui/temperature_sensors.krl` on the **data** pico and this ruleset on the **view** pico.
5
+ *
6
+ * **DID / layer-2 relationships:** do **not** pass `Remote Tx DID` (`did:peer:…`) to `wrangler:picoQuery` —
7
+ * that is not a channel ECI. Use the **relationship id** (same `Id` on both picos in Developer UI → Relationships)
8
+ * and `io.picolabs.subscription:queryOnSub` (wired below).
9
+ *
10
+ * raise temperature_sensors_view event "link_data_pico" attributes { "relationship_id": "<Id>" }
11
+ *
12
+ * **Legacy ECI relationships:** the established bus has a real **Remote Tx ECI**; you could call
13
+ * `wrangler:picoQuery` on that ECI directly, but `queryOnSub` still works if you store `relationship_id`.
14
+ *
15
+ * (`latestReadingsForView` is empty until `ent:sensors_relationship_id` is set.)
16
+ *
17
+ * The data pico's `readingAt` is a UTC string and `timestamp` is epoch milliseconds.
18
+ * This ruleset ignores that string and formats `timestamp` as Mountain Standard Time
19
+ * (UTC−7) before the table renders it.
20
+ *
21
+ * Open: Developer UI → View, or `/ui/<view-app-eci>/com.example.temperature_sensors_view/`
22
+ */
23
+ ruleset com.example.temperature_sensors_view {
24
+ meta {
25
+ name "Temperature sensors (demo view)"
26
+ use module io.picolabs.subscription alias sub
27
+ use module io.picolabs.xmlui alias ui
28
+ shares dashboard, latestReadingsForView
29
+ }
30
+
31
+ global {
32
+ sensors_rid = "com.example.temperature_sensors"
33
+
34
+ /**
35
+ * Format a UTC epoch-millisecond timestamp as Mountain Standard Time.
36
+ * `time:new` returns an ISO-8601 UTC string. Subtracting the offset first
37
+ * makes the date and clock in that string the MST reading time.
38
+ * @param timestamp Number milliseconds since the Unix epoch
39
+ * @returns String "YYYY-MM-DD HH:MM:SS MST"
40
+ */
41
+ readingAtMst = function(timestamp) {
42
+ // Mountain Standard Time is a fixed seven hours behind UTC.
43
+ mst_offset_ms = 7 * 60 * 60 * 1000
44
+ iso = time:new(timestamp - mst_offset_ms)
45
+ date = iso.split("T")[0]
46
+ clock = iso.split("T")[1].split(".")[0]
47
+ date + " " + clock + " MST"
48
+ }
49
+
50
+ /**
51
+ * Readings from the data pico, with `readingAt` rewritten to MST.
52
+ * @returns Array
53
+ */
54
+ latestReadingsForView = function() {
55
+ readings = ent:sensors_relationship_id.isnull() => [] |
56
+ sub:queryOnSub(ent:sensors_relationship_id, sensors_rid, "latestReadings", {})
57
+ readings.isnull() => readings |
58
+ readings.typeof() == "Array" => readings.map(function(row) {
59
+ row.put("readingAt", readingAtMst(row{"timestamp"}))
60
+ }) | readings
61
+ }
62
+
63
+ /**
64
+ * @view "Dashboard"
65
+ */
66
+ dashboard = function() {
67
+ <<<Component name="Dashboard">
68
+ #{ui:query("readings", "latestReadingsForView", {"rid": meta:rid})}
69
+ <Card title="Latest temperatures" subtitle="UTC timestamps from the data pico, shown here as MST" />
70
+ <Table data="{readings}">
71
+ <Column bindTo="name" header="Sensor" />
72
+ <Column bindTo="lastTemperature" header="Temp (F)" />
73
+ <Column bindTo="readingAt" header="Reading at (MST)" />
74
+ <Column bindTo="timestamp" header="Timestamp (ms)" />
75
+ </Table>
76
+ </Component>
77
+ >>
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Store the relationship id (established subscription bus `Id`, same string on both picos).
83
+ * @attr relationship_id String e.g. from Relationships tab next to the relationship name
84
+ */
85
+ rule link_data_pico {
86
+ select when temperature_sensors_view link_data_pico
87
+ relationship_id re#(.+)# setting(relationship_id)
88
+ fired {
89
+ ent:sensors_relationship_id := relationship_id
90
+ }
91
+ }
92
+
93
+ rule create_app_channel {
94
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
95
+ // Pass null for queries so the channel allows every shared query on this RS
96
+ // (including `dashboard`, which @view needs for Main.xmlui). A narrow list that
97
+ // omits the view query leaves viewQueriesOnChannel empty → Main.xmlui fails.
98
+ ui:create_app_channel(
99
+ ["temperature_sensors_view", "app"],
100
+ ["temperature_sensors_view"],
101
+ null,
102
+ meta:rid
103
+ )
104
+ }
105
+ }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Try It! bootstrap for the Thermostat demo. Install it on a new pico: it
3
+ * installs io.picolabs.xmlui and com.example.thermostat, then seeds three
4
+ * readings. tryIt() reports progress and links to the dashboard on the
5
+ * thermostat's viewer and admin channels.
6
+ *
7
+ * This ruleset does not use io.picolabs.xmlui as a module, because it is the
8
+ * one that installs it.
9
+ */
10
+ ruleset com.example.thermostat.bootstrap {
11
+ meta {
12
+ name "Thermostat"
13
+ description "Shows pico views: a dashboard @view, an app @component, the shared settings and theme components, and two channels whose policies give viewers and admins different pages."
14
+ author "PICOLABS"
15
+
16
+ use module io.picolabs.wrangler alias wrangler
17
+
18
+ shares tryIt
19
+ }
20
+
21
+ global {
22
+ thermostat_rid = "com.example.thermostat"
23
+ xmlui_rid = "io.picolabs.xmlui"
24
+ seed_temperatures = ["70", "72", "75"]
25
+
26
+ host = function() {
27
+ meta:host.defaultsTo("").replace(re#/$#, "")
28
+ }
29
+
30
+ appUrl = function(channel) {
31
+ host() + "/ui/" + channel{"id"} + "/" + thermostat_rid + "/"
32
+ }
33
+
34
+ /**
35
+ * Progress of this demo's setup, and the links to open when it is ready.
36
+ * @returns Map
37
+ */
38
+ tryIt = function() {
39
+ viewer = wrangler:channels(["thermostat", "viewer"]).head()
40
+ admin = wrangler:channels(["thermostat", "admin"]).head()
41
+ ready = (ent:stage == "ready" && viewer && admin) => true | false
42
+ // A map on the line after `| false` would parse as false{...}.
43
+ status = {
44
+ "ready": ready,
45
+ "stage": ent:stage.defaultsTo("starting"),
46
+ "links": ready => [
47
+ {"label": "Dashboard (viewer channel)", "url": appUrl(viewer)},
48
+ {"label": "Dashboard and Settings (admin channel)", "url": appUrl(admin)}
49
+ ] | []
50
+ }
51
+ status
52
+ }
53
+ }
54
+
55
+ rule start {
56
+ select when wrangler ruleset_installed where event:attr("rids") >< meta:rid
57
+ pre {
58
+ has_xmlui = wrangler:installedRIDs() >< xmlui_rid
59
+ }
60
+ if not has_xmlui then noop()
61
+ fired {
62
+ raise wrangler event "install_ruleset_request" attributes
63
+ {"url": host() + "/krl/" + xmlui_rid + ".krl"}
64
+ }
65
+ else {
66
+ raise wrangler event "install_ruleset_request" attributes
67
+ {"absoluteURL": meta:rulesetURI, "rid": thermostat_rid}
68
+ }
69
+ finally {
70
+ ent:stage := "installing"
71
+ raise wrangler event "new_channel_request" attributes {
72
+ "tags": ["try_it"],
73
+ "eventPolicy": {"allow": [], "deny": []},
74
+ "queryPolicy": {"allow": [{"rid": meta:rid, "name": "tryIt"}], "deny": []}
75
+ }
76
+ }
77
+ }
78
+
79
+ rule install_thermostat {
80
+ select when wrangler ruleset_installed where event:attr("rids") >< xmlui_rid
81
+ always {
82
+ raise wrangler event "install_ruleset_request" attributes
83
+ {"absoluteURL": meta:rulesetURI, "rid": thermostat_rid}
84
+ }
85
+ }
86
+
87
+ rule seed_readings {
88
+ select when wrangler ruleset_installed where event:attr("rids") >< thermostat_rid
89
+ foreach seed_temperatures setting(temperature)
90
+ always {
91
+ raise thermostat event "new_reading" attributes {"temperature": temperature}
92
+ ent:stage := "ready" on final
93
+ }
94
+ }
95
+ }