pico-engine 1.2.0 → 1.4.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.
@@ -0,0 +1,344 @@
1
+ ruleset io.picolabs.pds {
2
+ meta {
3
+ name "PDS"
4
+ description <<
5
+ Pico Data Services (PDS), a common data base structure for all rulesets to use.
6
+ >>
7
+ author "PICOLABS"
8
+
9
+ use module io.picolabs.wrangler alias wrangler
10
+
11
+ logging on
12
+
13
+ provides items, get_keys,
14
+ profile,
15
+ settings, get_config_value, get_setting_data_value, config_value, settings_names,
16
+ getChannelEci
17
+
18
+ shares items, get_keys,
19
+ profile,
20
+ settings, get_config_value, get_setting_data_value, config_value, settings_names,
21
+ getChannelEci
22
+
23
+ }
24
+
25
+
26
+ // --------------------------------------------
27
+ // PDS entity variables (io.picolabs.pds RS scope only)
28
+ //
29
+ // ent:profile — this pico's display identity
30
+ // {
31
+ // "name": "",
32
+ // "description": "",
33
+ // "photo": "<url or data: URI>",
34
+ // "_created": "20260609T120000+0000", // set on first update
35
+ // "_modified": "20260609T153000+0000", // set on every update
36
+ // "email": "", // optional
37
+ // "phone": "" // optional
38
+ // }
39
+ //
40
+ // ent:general — namespaced app/domain data
41
+ // "<namespace>": {
42
+ // "<key>": <value>,
43
+ // ...
44
+ // }
45
+ // Path: ent:general{[namespace, key]} or ent:general{namespace} for whole namespace map
46
+ //
47
+ // ent:settings — per-ruleset configuration, keyed by RID
48
+ // "<rulesetRID>": {
49
+ // "name": "<display name>",
50
+ // "rid": "<rulesetRID>",
51
+ // "data": { "<configKey>": <value>, ... },
52
+ // "schema": []
53
+ // }
54
+ // Path: ent:settings{[rulesetRID, "data", configKey]} for config_value()
55
+ //
56
+ // --------------------------------------------
57
+
58
+
59
+ global {
60
+
61
+ // Default avatar: embedded SVG (no external URL dependency)
62
+ defaultUnknownPhoto = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiBmaWxsPSIjZWNlZmYxIi8+PGNpcmNsZSBjeD0iMzIiIGN5PSIyMiIgcj0iMTEiIGZpbGw9IiM3ODkwOWMiLz48ZWxsaXBzZSBjeD0iMzIiIGN5PSI1NCIgcng9IjE4IiByeT0iMTQiIGZpbGw9IiM3ODkwOWMiLz48L3N2Zz4="
63
+
64
+ items = function (namespace, key){
65
+ item = function(namespace, keyvalue) {
66
+ ent:general{[namespace, keyvalue]}
67
+ };
68
+
69
+ multipleItems = function(namespace) {
70
+ ent:general{namespace}
71
+ };
72
+ payload = (key.isnull()) => (namespace.isnull() => ent:general | multipleItems(namespace)) | item(namespace, key);
73
+ status = namespace.isnull() => "failed" | "success";
74
+ {
75
+ "status" : status,
76
+ "general" : payload
77
+ };
78
+ }
79
+
80
+ get_keys = function(namespace) {
81
+ ent:general{namespace}.keys()
82
+ }
83
+
84
+ profile = function(key){
85
+ get_profile = function(k) {
86
+ ent:profile{k};
87
+ };
88
+ get_all_profile = function() {
89
+ ent:profile;
90
+ };
91
+ payload = (key.isnull()) => get_all_profile() | get_profile(key);
92
+ {
93
+ "status" : "success",
94
+ "profile" : payload
95
+ };
96
+ };
97
+
98
+ settings_names = function() {
99
+ ent:settings.keys().map(function(setRID) {
100
+ setName = ent:settings{[setRID, "name"]};
101
+ {
102
+ "setRID": setRID,
103
+ "setName": setName
104
+ }
105
+ })
106
+ };
107
+
108
+ settings = function(Rid, Key, detail){
109
+ get_setting_all = function() {
110
+ ent:settings
111
+ };
112
+ get_setting = function(rid) {
113
+ ent:settings{rid}
114
+ };
115
+ get_setting_value = function(rid, varible) {
116
+ ent:settings{[rid, varible]}
117
+ };
118
+ get_data_value = function(rid, dataKey) {
119
+ ent:settings{[rid, "data", dataKey]}
120
+ };
121
+ pick = function() {
122
+ Rid.isnull() => get_setting_all()
123
+ | Key.isnull() => get_setting(Rid)
124
+ | detail.isnull() => get_setting_value(Rid, Key)
125
+ | get_data_value(Rid, detail)
126
+ };
127
+ {
128
+ "status" : "success",
129
+ "settings" : pick()
130
+ };
131
+ }
132
+
133
+ setting_data = function(setRID) {
134
+ ent:settings{[setRID, "data"]}
135
+ };
136
+
137
+ setting_schema = function(setRID) {
138
+ ent:settings{[setRID, "schema"]}
139
+ };
140
+
141
+ setting_data_value = function(setRID, setKey) {
142
+ ent:settings{[setRID, "data", setKey]}
143
+ };
144
+
145
+ get_setting_data_value = function(setRID, setKey) {
146
+ setting_data_value(setRID, setKey)
147
+ }
148
+
149
+ config_value = function(setKey) {
150
+ setRID = meta:callingRID();
151
+ ent:settings{[setRID, "data", setKey]}
152
+ };
153
+
154
+ get_config_value = function(setKey) {
155
+ config_value(setKey)
156
+ }
157
+
158
+ getChannelEci = function() {
159
+ ent:pds_channel_eci
160
+ }
161
+
162
+ defaultProfile = {
163
+ "name": "",
164
+ "description": "",
165
+ "photo": defaultUnknownPhoto
166
+ };
167
+
168
+ }//End Global
169
+ // Rules
170
+
171
+ //------------------------------- ent: general
172
+
173
+ rule add_item {
174
+ select when pds new_data_available
175
+ pre {
176
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
177
+ keyvalue = event:attr("key").defaultsTo("", "no key");
178
+ value = event:attr("value").defaultsTo("", "no value");
179
+ }
180
+ noop()
181
+ always {
182
+ ent:general{[namespace, keyvalue]} := value;
183
+ raise pds event "data_added"
184
+ attributes {"namespace": namespace, "keyvalue": keyvalue}
185
+ }
186
+ }
187
+
188
+ rule update_item {
189
+ select when pds updated_data_available
190
+ foreach(event:attr("value") || {}) setting(avalue, akey)
191
+ pre {
192
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
193
+ keyvalue = event:attr("key").defaultsTo("", "no key");
194
+ }
195
+ noop()
196
+ always {
197
+ ent:general{[namespace, keyvalue, akey]} := avalue;
198
+ raise pds event "data_updated"
199
+ attributes {"namespace": namespace, "keyvalue": keyvalue};
200
+ }
201
+ }
202
+
203
+ rule remove_item {
204
+ select when pds remove_old_data
205
+ pre{
206
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
207
+ keyvalue = event:attr("key").defaultsTo("", "no key");
208
+ }
209
+ noop()
210
+ always {
211
+ clear ent:general{[namespace, keyvalue]};
212
+ raise pds event "data_deleted"
213
+ attributes {"namespace": namespace, "keyvalue": keyvalue}
214
+ }
215
+ }
216
+
217
+ rule remove_namespace {
218
+ select when pds remove_namespace
219
+ pre{
220
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
221
+ }
222
+ noop()
223
+ always {
224
+ clear ent:general{namespace};
225
+ raise pds event "namespace_deleted"
226
+ attributes {"namespace": namespace}
227
+ }
228
+ }
229
+
230
+ rule itemed_mapped {
231
+ select when pds map_item
232
+ pre{
233
+ namespace = event:attr("namespace").defaultsTo("", "no namespace");
234
+ mapvalues = event:attr("mapvalues").defaultsTo("", "no mapvalues").decode();
235
+ }
236
+ noop()
237
+ always {
238
+ ent:general{namespace} := mapvalues;
239
+ raise pds event "new_map_added"
240
+ attributes {"namespace": namespace, "mapvalues": mapvalues};
241
+ }
242
+ }
243
+
244
+ rule update_profile {
245
+ select when pds updated_profile
246
+ pre {
247
+ newProfile = event:attrs.defaultsTo({}, "no attrs");
248
+ created = function(){
249
+ time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"});
250
+ };
251
+
252
+ buildProfile = function(newProfile){
253
+ profile = ent:profile || defaultProfile;
254
+ keepOrSet = function(key) {
255
+ newProfile{key}.isnull() => profile{key} | newProfile{key}
256
+ };
257
+ newProfile
258
+ .put(["name"], keepOrSet("name"))
259
+ .put(["description"], keepOrSet("description"))
260
+ .put(["photo"], keepOrSet("photo"))
261
+ .put(["email"], keepOrSet("email"))
262
+ .put(["phone"], keepOrSet("phone"))
263
+ .put(["_created"], (profile{"_created"} || created()))
264
+ .put(["_modified"], time:strftime(time:now(), "%Y%m%dT%H%M%S%z", {"tz":"UTC"}))
265
+ };
266
+ newly_constructed_profile = (newProfile.keys().length() > 0) =>
267
+ buildProfile(newProfile) |
268
+ "nothing to update";
269
+
270
+ }
271
+ if newly_constructed_profile != "nothing to update" then
272
+ noop()
273
+ fired {
274
+ ent:profile := newly_constructed_profile;
275
+ raise pds event "profile_updated" attributes newProfile.put(["_status"], "success");
276
+ }
277
+ else {
278
+ raise pds event "profile_updated" attributes newProfile.put(["_status"], "failure");
279
+ }
280
+ }
281
+
282
+ rule settings_added {
283
+ select when pds add_settings
284
+ pre {
285
+ set_name = event:attr("name").defaultsTo(0, "no Name");
286
+ set_rid = event:attr("keyed_rid").defaultsTo(0, "no keyed_rid");
287
+ set_schema = event:attr("schema").defaultsTo(0, "no Schema");
288
+ set_attr = event:attr("data_key").defaultsTo(0, "no setAttr");
289
+ set_value = event:attr("value").defaultsTo(0, "no Value");
290
+
291
+ }
292
+ noop()
293
+ always {
294
+ ent:settings{[set_rid, "name"]} := set_name;
295
+ ent:settings{[set_rid, "rid"]} := set_rid;
296
+ ent:settings{[set_rid, "schema"]} := set_schema if set_schema;
297
+ ent:settings{[set_rid, "data", set_attr]} := set_value if set_attr;
298
+ raise pds event "settings_added" attributes event:attrs;
299
+ }
300
+ }
301
+
302
+ rule create_pds_channel {
303
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
304
+ pre {
305
+ channelName = "pds"
306
+ eventPolicy = {"allow": [{"domain": "pds", "name": "*"}], "deny": []}
307
+ queryPolicy = {"allow": [{"rid": "io.picolabs.pds", "name": "*"}], "deny": []}
308
+ existing_channels = wrangler:channels()
309
+ pds_channel = existing_channels.filter(function(chan) {
310
+ chan{"name"} == channelName
311
+ })
312
+ channel_exists = pds_channel.length() > 0
313
+ }
314
+ if not channel_exists then
315
+ wrangler:createChannel([channelName], eventPolicy, queryPolicy) setting(channel)
316
+ fired {
317
+ ent:pds_channel_eci := channel_exists => pds_channel.head(){"id"} | channel{"id"}
318
+ }
319
+ }
320
+
321
+ rule seed_profile_name {
322
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
323
+ pre {
324
+ wrangler_name = wrangler:myself(){"name"}
325
+ existing_name = ent:profile{"name"}
326
+ needs_seed = wrangler_name && (existing_name.isnull() || existing_name == "")
327
+ }
328
+ if needs_seed then noop()
329
+ fired {
330
+ raise pds event "updated_profile" attributes {"name": wrangler_name}
331
+ }
332
+ }
333
+
334
+ rule clearPDS {
335
+ select when pds clear_all_data
336
+ noop()
337
+ always {
338
+ clear ent:general;
339
+ clear ent:profile;
340
+ clear ent:settings;
341
+ }
342
+ }
343
+
344
+ }
@@ -12,9 +12,16 @@ ruleset io.picolabs.pico-engine-ui {
12
12
  .map(function(c){c["id"]})
13
13
  .head()
14
14
  }
15
- getOtherUiECI = function(eci){
16
- thisPico = ctx:channels.any(function(c){c{"id"}==eci})
17
- return (eci && not thisPico) => ctx:query(eci, ctx:rid, "uiECI") | null
15
+ // Cached UI channel ECIs for parent/child links. Populated at child creation
16
+ // and backfilled on engine started. box()/pico() read these maps only — no
17
+ // cross-pico ctx:query — to avoid deadlocks (#493).
18
+ uiEciForChild = function(familyEci){
19
+ ent:child_ui_ecis{familyEci}
20
+ }
21
+ childUiEcis = function(){
22
+ ctx:children
23
+ .map(uiEciForChild)
24
+ .filter(function(eci){eci})
18
25
  }
19
26
  testingECI = function(){
20
27
  return ent:testingECI
@@ -25,8 +32,8 @@ ruleset io.picolabs.pico-engine-ui {
25
32
  box = function(){
26
33
  return {
27
34
  "eci": uiECI(),
28
- "parent": getOtherUiECI(ctx:parent),
29
- "children": ctx:children.map(getOtherUiECI),
35
+ "parent": ent:parent_ui_eci,
36
+ "children": childUiEcis(),
30
37
  "name": name(),
31
38
  "backgroundColor": ent:backgroundColor || "#87cefa",
32
39
  "x": ent:x || 100,
@@ -38,8 +45,8 @@ ruleset io.picolabs.pico-engine-ui {
38
45
  pico = function(){
39
46
  return {
40
47
  "eci": uiECI(),
41
- "parent": getOtherUiECI(ctx:parent),
42
- "children": ctx:children.map(getOtherUiECI),
48
+ "parent": ent:parent_ui_eci,
49
+ "children": childUiEcis(),
43
50
  "channels": ctx:channels,
44
51
  "rulesets": ctx:rulesets
45
52
  }
@@ -88,10 +95,13 @@ ruleset io.picolabs.pico-engine-ui {
88
95
  };
89
96
  entryMap = function(a,e){
90
97
  // a.head() is array of entries; a[1] is whether last entry is eats
98
+ // a[2] is domain:name of the event
91
99
  eats = e{"msg"} == "event added to schedule"
92
- a[1] && eats
93
- => [a.head(),true] // omit an eats
94
- | [a.head().append(episode_line(e)),eats]
100
+ event_dt = eats => e{["event","domain"]} + ":" + e{["event","name"]}
101
+ | ""
102
+ a[1] && eats && a[2]==event_dt
103
+ => [a.head(),true,event_dt] // omit a duplicate eats
104
+ | [a.head().append(episode_line(e)),eats,event_dt]
95
105
  }
96
106
  episodes = ctx:logs()
97
107
  .collect(function(e){e.get("txnId")})
@@ -99,7 +109,7 @@ ruleset io.picolabs.pico-engine-ui {
99
109
  .map(function(k){
100
110
  episode = episodes.get(k)
101
111
  entries = episode
102
- .reduce(entryMap,[[],false]).head()
112
+ .reduce(entryMap,[[],false,""]).head()
103
113
  return {
104
114
  "txnId": k,
105
115
  "time": episode.head().get("time"),
@@ -108,8 +118,8 @@ ruleset io.picolabs.pico-engine-ui {
108
118
  }
109
119
  })
110
120
  .filter(function(g){
111
- g.header.match(re#^QUERY .*io.picolabs.pico-engine-ui/#) => false |
112
- g.header.match(re#^QUERY .*io.picolabs.subscription/established#) => false |
121
+ g{"header"}.match(re#^QUERY .*io.picolabs.pico-engine-ui/#) => false |
122
+ g{"header"}.match(re#^QUERY .*io.picolabs.subscription/established#) => false |
113
123
  true
114
124
  })
115
125
  .reverse()
@@ -173,6 +183,33 @@ ruleset io.picolabs.pico-engine-ui {
173
183
  ent:height := event:attrs{"height"}.as("Number") if event:attrs{"height"}
174
184
  ent:name := event:attrs{"name"}.as("String") if event:attrs{"name"}
175
185
  ent:backgroundColor := event:attrs{"backgroundColor"}.validateColor() if event:attrs{"backgroundColor"}
186
+ ent:parent_ui_eci := event:attrs{"parentUiEci"} if event:attrs{"parentUiEci"}
187
+ }
188
+ }
189
+ rule remember_child_ui_eci {
190
+ select when wrangler new_child_created
191
+ pre {
192
+ uiEci = event:attr("uiEci")
193
+ familyEci = event:attr("eci")
194
+ }
195
+ if uiEci && familyEci then noop()
196
+ always {
197
+ ent:child_ui_ecis{[familyEci]} := uiEci
198
+ }
199
+ }
200
+ rule backfill_child_ui_eci {
201
+ select when engine started
202
+ foreach ctx:children setting(familyEci)
203
+ if ent:child_ui_ecis{familyEci}.isnull() then noop()
204
+ always {
205
+ ent:child_ui_ecis{[familyEci]} := ctx:query(familyEci, ctx:rid, "uiECI")
206
+ }
207
+ }
208
+ rule backfill_parent_ui_eci {
209
+ select when engine started
210
+ if ctx:parent && ent:parent_ui_eci.isnull() then noop()
211
+ always {
212
+ ent:parent_ui_eci := ctx:query(ctx:parent, ctx:rid, "uiECI")
176
213
  }
177
214
  }
178
215
  rule new {
@@ -193,9 +230,8 @@ ruleset io.picolabs.pico-engine-ui {
193
230
  pre {
194
231
  delUiEci = event:attrs{"eci"}
195
232
  delEci = ctx:children
196
- .filter(function(eci){
197
- other = getOtherUiECI(eci)
198
- return other == delUiEci
233
+ .filter(function(familyEci){
234
+ ent:child_ui_ecis{familyEci} == delUiEci
199
235
  })
200
236
  .head()
201
237
  }