@minca/core 0.1.18 → 0.3.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.
- package/README.md +1 -1
- package/dist/identity/abilities.d.ts +0 -2
- package/dist/identity/abilities.d.ts.map +1 -1
- package/dist/identity/abilities.js +2 -2
- package/dist/identity/abilities.js.map +1 -1
- package/dist/identity/account-vault.d.ts +0 -2
- package/dist/identity/account-vault.d.ts.map +1 -1
- package/dist/identity/account-vault.js +1 -1
- package/dist/identity/account-vault.js.map +1 -1
- package/dist/identity/crypto-p256.d.ts +1 -1
- package/dist/identity/crypto-p256.d.ts.map +1 -1
- package/dist/identity/ucan.d.ts +2 -1
- package/dist/identity/ucan.d.ts.map +1 -1
- package/dist/identity/ucan.js +1 -1
- package/dist/identity/ucan.js.map +1 -1
- package/dist/inbox/inbox.d.ts +0 -21
- package/dist/inbox/inbox.d.ts.map +1 -1
- package/dist/inbox/inbox.js +4 -4
- package/dist/inbox/inbox.js.map +1 -1
- package/dist/inbox/index.d.ts +4 -5
- package/dist/inbox/index.d.ts.map +1 -1
- package/dist/inbox/index.js +2 -3
- package/dist/inbox/index.js.map +1 -1
- package/dist/inbox/names.d.ts +0 -4
- package/dist/inbox/names.d.ts.map +1 -1
- package/dist/inbox/names.js +2 -2
- package/dist/inbox/names.js.map +1 -1
- package/dist/index.d.ts +45 -71
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -53
- package/dist/index.js.map +1 -1
- package/dist/network/mesh.d.ts.map +1 -1
- package/dist/network/mesh.js +10 -0
- package/dist/network/mesh.js.map +1 -1
- package/dist/network/rendezvous.d.ts +2 -0
- package/dist/network/rendezvous.d.ts.map +1 -1
- package/dist/network/rendezvous.js +5 -0
- package/dist/network/rendezvous.js.map +1 -1
- package/dist/network/rtc-transport.d.ts.map +1 -1
- package/dist/network/rtc-transport.js +8 -3
- package/dist/network/rtc-transport.js.map +1 -1
- package/dist/node/carrier.d.ts +1 -33
- package/dist/node/carrier.d.ts.map +1 -1
- package/dist/node/carrier.js +38 -68
- package/dist/node/carrier.js.map +1 -1
- package/dist/node/index.d.ts +7 -14
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +4 -8
- package/dist/node/index.js.map +1 -1
- package/dist/node/node-carriers.d.ts.map +1 -1
- package/dist/node/node-carriers.js +21 -52
- package/dist/node/node-carriers.js.map +1 -1
- package/dist/node/node-context.d.ts +0 -2
- package/dist/node/node-context.d.ts.map +1 -1
- package/dist/node/node-context.js.map +1 -1
- package/dist/node/node-hosting.d.ts.map +1 -1
- package/dist/node/node-hosting.js +13 -4
- package/dist/node/node-hosting.js.map +1 -1
- package/dist/node/node-records.js +1 -1
- package/dist/node/node-records.js.map +1 -1
- package/dist/node/node.d.ts.map +1 -1
- package/dist/node/node.js +134 -36
- package/dist/node/node.js.map +1 -1
- package/dist/node/space-checks.d.ts +3 -1
- package/dist/node/space-checks.d.ts.map +1 -1
- package/dist/node/space-checks.js +7 -3
- package/dist/node/space-checks.js.map +1 -1
- package/dist/node/space-holding.d.ts +5 -0
- package/dist/node/space-holding.d.ts.map +1 -1
- package/dist/node/space-holding.js +90 -5
- package/dist/node/space-holding.js.map +1 -1
- package/dist/node/space-lines.d.ts +3 -0
- package/dist/node/space-lines.d.ts.map +1 -1
- package/dist/node/space-lines.js +13 -1
- package/dist/node/space-lines.js.map +1 -1
- package/dist/node/space-membership.js +4 -4
- package/dist/node/space-membership.js.map +1 -1
- package/dist/node/space-peers.d.ts +5 -2
- package/dist/node/space-peers.d.ts.map +1 -1
- package/dist/node/space-peers.js +3 -2
- package/dist/node/space-peers.js.map +1 -1
- package/dist/node/space-runtime.d.ts +3 -1
- package/dist/node/space-runtime.d.ts.map +1 -1
- package/dist/node/space-runtime.js +29 -8
- package/dist/node/space-runtime.js.map +1 -1
- package/dist/node/space-standing.d.ts +1 -1
- package/dist/node/space-standing.d.ts.map +1 -1
- package/dist/node/space-standing.js +11 -4
- package/dist/node/space-standing.js.map +1 -1
- package/dist/node/space-writing.d.ts.map +1 -1
- package/dist/node/space-writing.js +9 -0
- package/dist/node/space-writing.js.map +1 -1
- package/dist/node/stores.d.ts +9 -1
- package/dist/node/stores.d.ts.map +1 -1
- package/dist/node/stores.js +4 -2
- package/dist/node/stores.js.map +1 -1
- package/dist/node/types.d.ts +16 -5
- package/dist/node/types.d.ts.map +1 -1
- package/dist/node/worker.d.ts +14 -6
- package/dist/node/worker.d.ts.map +1 -1
- package/dist/node/worker.js +59 -20
- package/dist/node/worker.js.map +1 -1
- package/dist/node/writer-keys.d.ts +2 -2
- package/dist/node/writer-keys.d.ts.map +1 -1
- package/dist/node/writer-keys.js +12 -14
- package/dist/node/writer-keys.js.map +1 -1
- package/dist/privacy/direct.d.ts +0 -7
- package/dist/privacy/direct.d.ts.map +1 -1
- package/dist/privacy/direct.js +2 -2
- package/dist/privacy/direct.js.map +1 -1
- package/dist/query/filter.d.ts +0 -2
- package/dist/query/filter.d.ts.map +1 -1
- package/dist/query/filter.js +1 -1
- package/dist/query/filter.js.map +1 -1
- package/dist/query/types.d.ts +2 -6
- package/dist/query/types.d.ts.map +1 -1
- package/dist/query/types.js.map +1 -1
- package/dist/records/key.d.ts +2 -0
- package/dist/records/key.d.ts.map +1 -1
- package/dist/records/key.js +4 -0
- package/dist/records/key.js.map +1 -1
- package/dist/records/links.js +1 -1
- package/dist/schemas/direct.d.ts +70 -0
- package/dist/schemas/direct.d.ts.map +1 -0
- package/dist/schemas/direct.js +39 -0
- package/dist/schemas/direct.js.map +1 -0
- package/dist/schemas/index.d.ts +10 -73
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +8 -66
- package/dist/schemas/index.js.map +1 -1
- package/dist/session/account-hint.d.ts +2 -2
- package/dist/session/account-hint.d.ts.map +1 -1
- package/dist/session/account-hint.js +3 -3
- package/dist/session/account-hint.js.map +1 -1
- package/dist/session/connect.d.ts +12 -5
- package/dist/session/connect.d.ts.map +1 -1
- package/dist/session/connect.js +25 -21
- package/dist/session/connect.js.map +1 -1
- package/dist/session/connection.d.ts +26 -2
- package/dist/session/connection.d.ts.map +1 -1
- package/dist/session/connection.js +151 -11
- package/dist/session/connection.js.map +1 -1
- package/dist/session/hosting.d.ts +1 -7
- package/dist/session/hosting.d.ts.map +1 -1
- package/dist/session/hosting.js +3 -3
- package/dist/session/hosting.js.map +1 -1
- package/dist/session/index.d.ts +9 -14
- package/dist/session/index.d.ts.map +1 -1
- package/dist/session/index.js +8 -11
- package/dist/session/index.js.map +1 -1
- package/dist/session/stay-signed-in.d.ts +0 -1
- package/dist/session/stay-signed-in.d.ts.map +1 -1
- package/dist/session/stay-signed-in.js +1 -1
- package/dist/session/stay-signed-in.js.map +1 -1
- package/dist/space/account-registry.d.ts +0 -3
- package/dist/space/account-registry.d.ts.map +1 -1
- package/dist/space/account-registry.js.map +1 -1
- package/dist/space/own-automations.d.ts +51 -95
- package/dist/space/own-automations.d.ts.map +1 -1
- package/dist/space/own-automations.js +100 -295
- package/dist/space/own-automations.js.map +1 -1
- package/dist/space/pass.d.ts +8 -3
- package/dist/space/pass.d.ts.map +1 -1
- package/dist/space/pass.js +4 -3
- package/dist/space/pass.js.map +1 -1
- package/dist/space/space-manager.d.ts +1 -3
- package/dist/space/space-manager.d.ts.map +1 -1
- package/dist/space/space-manager.js +5 -7
- package/dist/space/space-manager.js.map +1 -1
- package/dist/storage/body-cache.d.ts +26 -0
- package/dist/storage/body-cache.d.ts.map +1 -0
- package/dist/storage/body-cache.js +41 -0
- package/dist/storage/body-cache.js.map +1 -0
- package/dist/storage/encrypted-adapter.d.ts +0 -5
- package/dist/storage/encrypted-adapter.d.ts.map +1 -1
- package/dist/storage/encrypted-adapter.js +1 -1
- package/dist/storage/encrypted-adapter.js.map +1 -1
- package/dist/storage/folder-adapter.d.ts +8 -2
- package/dist/storage/folder-adapter.d.ts.map +1 -1
- package/dist/storage/folder-adapter.js +43 -26
- package/dist/storage/folder-adapter.js.map +1 -1
- package/dist/storage/indexeddb-adapter.js +27 -2
- package/dist/storage/indexeddb-adapter.js.map +1 -1
- package/dist/storage/sqlite-adapter.d.ts +57 -0
- package/dist/storage/sqlite-adapter.d.ts.map +1 -0
- package/dist/storage/sqlite-adapter.js +213 -0
- package/dist/storage/sqlite-adapter.js.map +1 -0
- package/dist/storage/storage-provider.d.ts +21 -2
- package/dist/storage/storage-provider.d.ts.map +1 -1
- package/dist/storage/storage-provider.js +149 -56
- package/dist/storage/storage-provider.js.map +1 -1
- package/dist/sync/sync-engine.d.ts +10 -4
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-engine.js +120 -38
- package/dist/sync/sync-engine.js.map +1 -1
- package/dist/sync/sync-messages.d.ts +4 -0
- package/dist/sync/sync-messages.d.ts.map +1 -1
- package/dist/sync/sync-messages.js.map +1 -1
- package/dist/types.d.ts +0 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/step.d.ts +8 -0
- package/dist/utils/step.d.ts.map +1 -0
- package/dist/utils/step.js +15 -0
- package/dist/utils/step.js.map +1 -0
- package/docs/README.md +1 -1
- package/docs/actions.md +41 -2
- package/docs/agents.md +13 -10
- package/docs/automations.md +16 -48
- package/docs/blueprints-and-guides.md +2 -2
- package/docs/bots.md +3 -3
- package/docs/building-an-app.md +16 -0
- package/docs/collections.md +2 -4
- package/docs/computed.md +1 -2
- package/docs/node.md +112 -39
- package/docs/proposing-collections.md +3 -3
- package/docs/sign-in.md +45 -52
- package/docs/spaces.md +0 -5
- package/docs/standard-library.md +20 -8
- package/docs/storage.md +78 -12
- package/docs/usernames.md +3 -3
- package/package.json +7 -13
- package/dist/elements/auth-styles.d.ts +0 -18
- package/dist/elements/auth-styles.d.ts.map +0 -1
- package/dist/elements/auth-styles.js +0 -206
- package/dist/elements/auth-styles.js.map +0 -1
- package/dist/elements/dom.d.ts +0 -29
- package/dist/elements/dom.d.ts.map +0 -1
- package/dist/elements/dom.js +0 -91
- package/dist/elements/dom.js.map +0 -1
- package/dist/elements/index.d.ts +0 -13
- package/dist/elements/index.d.ts.map +0 -1
- package/dist/elements/index.js +0 -11
- package/dist/elements/index.js.map +0 -1
- package/dist/elements/minca-auth.d.ts +0 -55
- package/dist/elements/minca-auth.d.ts.map +0 -1
- package/dist/elements/minca-auth.js +0 -768
- package/dist/elements/minca-auth.js.map +0 -1
- package/dist/node/actions.d.ts +0 -85
- package/dist/node/actions.d.ts.map +0 -1
- package/dist/node/actions.js +0 -921
- package/dist/node/actions.js.map +0 -1
- package/dist/node/host.d.ts +0 -187
- package/dist/node/host.d.ts.map +0 -1
- package/dist/node/host.js +0 -818
- package/dist/node/host.js.map +0 -1
- package/dist/node/usage.d.ts +0 -32
- package/dist/node/usage.d.ts.map +0 -1
- package/dist/node/usage.js +0 -217
- package/dist/node/usage.js.map +0 -1
- package/dist/node/watch-notifications.d.ts +0 -19
- package/dist/node/watch-notifications.d.ts.map +0 -1
- package/dist/node/watch-notifications.js +0 -128
- package/dist/node/watch-notifications.js.map +0 -1
- package/dist/schemas/automations.d.ts +0 -390
- package/dist/schemas/automations.d.ts.map +0 -1
- package/dist/schemas/automations.js +0 -619
- package/dist/schemas/automations.js.map +0 -1
- package/dist/schemas/blueprints.d.ts +0 -122
- package/dist/schemas/blueprints.d.ts.map +0 -1
- package/dist/schemas/blueprints.js +0 -337
- package/dist/schemas/blueprints.js.map +0 -1
- package/dist/schemas/choosing.d.ts +0 -19
- package/dist/schemas/choosing.d.ts.map +0 -1
- package/dist/schemas/choosing.js +0 -69
- package/dist/schemas/choosing.js.map +0 -1
- package/dist/schemas/connectors.d.ts +0 -202
- package/dist/schemas/connectors.d.ts.map +0 -1
- package/dist/schemas/connectors.js +0 -140
- package/dist/schemas/connectors.js.map +0 -1
- package/dist/schemas/cron.d.ts +0 -14
- package/dist/schemas/cron.d.ts.map +0 -1
- package/dist/schemas/cron.js +0 -75
- package/dist/schemas/cron.js.map +0 -1
- package/dist/schemas/geo.d.ts +0 -63
- package/dist/schemas/geo.d.ts.map +0 -1
- package/dist/schemas/geo.js +0 -131
- package/dist/schemas/geo.js.map +0 -1
- package/dist/schemas/guides.d.ts +0 -26
- package/dist/schemas/guides.d.ts.map +0 -1
- package/dist/schemas/guides.js +0 -33
- package/dist/schemas/guides.js.map +0 -1
- package/dist/schemas/library/annotations.d.ts +0 -616
- package/dist/schemas/library/annotations.d.ts.map +0 -1
- package/dist/schemas/library/annotations.js +0 -156
- package/dist/schemas/library/annotations.js.map +0 -1
- package/dist/schemas/library/collections.d.ts +0 -211
- package/dist/schemas/library/collections.d.ts.map +0 -1
- package/dist/schemas/library/collections.js +0 -57
- package/dist/schemas/library/collections.js.map +0 -1
- package/dist/schemas/library/community.d.ts +0 -1136
- package/dist/schemas/library/community.d.ts.map +0 -1
- package/dist/schemas/library/community.js +0 -315
- package/dist/schemas/library/community.js.map +0 -1
- package/dist/schemas/library/life.d.ts +0 -1440
- package/dist/schemas/library/life.d.ts.map +0 -1
- package/dist/schemas/library/life.js +0 -202
- package/dist/schemas/library/life.js.map +0 -1
- package/dist/schemas/library/media.d.ts +0 -1002
- package/dist/schemas/library/media.d.ts.map +0 -1
- package/dist/schemas/library/media.js +0 -110
- package/dist/schemas/library/media.js.map +0 -1
- package/dist/schemas/library/money.d.ts +0 -1221
- package/dist/schemas/library/money.d.ts.map +0 -1
- package/dist/schemas/library/money.js +0 -168
- package/dist/schemas/library/money.js.map +0 -1
- package/dist/schemas/library/planning.d.ts +0 -1107
- package/dist/schemas/library/planning.d.ts.map +0 -1
- package/dist/schemas/library/planning.js +0 -214
- package/dist/schemas/library/planning.js.map +0 -1
- package/dist/schemas/library/publishing.d.ts +0 -1499
- package/dist/schemas/library/publishing.d.ts.map +0 -1
- package/dist/schemas/library/publishing.js +0 -285
- package/dist/schemas/library/publishing.js.map +0 -1
- package/dist/schemas/library/social.d.ts +0 -1257
- package/dist/schemas/library/social.d.ts.map +0 -1
- package/dist/schemas/library/social.js +0 -147
- package/dist/schemas/library/social.js.map +0 -1
- package/dist/schemas/standard.d.ts +0 -21
- package/dist/schemas/standard.d.ts.map +0 -1
- package/dist/schemas/standard.js +0 -39
- package/dist/schemas/standard.js.map +0 -1
- package/dist/session/auth.d.ts +0 -411
- package/dist/session/auth.d.ts.map +0 -1
- package/dist/session/auth.js +0 -1529
- package/dist/session/auth.js.map +0 -1
- package/dist/session/credentials.d.ts +0 -30
- package/dist/session/credentials.d.ts.map +0 -1
- package/dist/session/credentials.js +0 -63
- package/dist/session/credentials.js.map +0 -1
- package/dist/session/password-strength.d.ts +0 -22
- package/dist/session/password-strength.d.ts.map +0 -1
- package/dist/session/password-strength.js +0 -130
- package/dist/session/password-strength.js.map +0 -1
|
@@ -1,619 +0,0 @@
|
|
|
1
|
-
import { nameOf, plainQuery, } from '../query/types.js';
|
|
2
|
-
import { checkFilter, checkQuery, matches } from '../query/filter.js';
|
|
3
|
-
import { causeOf, namedComputed } from '../query/computed.js';
|
|
4
|
-
import { quickAddBody } from '../schema/quick-add.js';
|
|
5
|
-
import { schemaProblem } from '../schema/collection-def.js';
|
|
6
|
-
import { isObject } from '../utils/guards.js';
|
|
7
|
-
import { checkCron, cronMatches } from './cron.js';
|
|
8
|
-
import { definedNames } from './defined.js';
|
|
9
|
-
import { about, define, one, person, text, typed, when as moment, words } from './fragments.js';
|
|
10
|
-
export const automation = typed()({
|
|
11
|
-
name: 'std.automation',
|
|
12
|
-
title: 'Automation',
|
|
13
|
-
description: 'When records in the space are a certain way, or at set times, do something.',
|
|
14
|
-
schema: {
|
|
15
|
-
type: 'object',
|
|
16
|
-
properties: {
|
|
17
|
-
name: words(120),
|
|
18
|
-
when: { type: 'object' },
|
|
19
|
-
every: text(100, 'Five cron fields, minute hour day month weekday, local time', 9),
|
|
20
|
-
then: { type: 'object' },
|
|
21
|
-
by: person('The bot that runs it'),
|
|
22
|
-
picked: { type: 'object' },
|
|
23
|
-
paused: { type: 'boolean' },
|
|
24
|
-
since: moment('Nothing that came to hold before this sets it off'),
|
|
25
|
-
spaces: { description: 'Every space of the account ("all"), or a list of their ids' },
|
|
26
|
-
open: words(2000),
|
|
27
|
-
app: {
|
|
28
|
-
type: 'object',
|
|
29
|
-
properties: { origin: words(200), name: words(80) },
|
|
30
|
-
required: ['origin'],
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
required: ['name', 'then', 'since'],
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
export const automationRun = define({
|
|
37
|
-
name: 'std.automation-run',
|
|
38
|
-
title: 'Automation run',
|
|
39
|
-
description: 'An automation acted on a record: once each, whichever device did it.',
|
|
40
|
-
schema: {
|
|
41
|
-
type: 'object',
|
|
42
|
-
properties: { did: words(500), ok: { type: 'boolean' }, at: moment(), made: words(256) },
|
|
43
|
-
required: ['did', 'ok', 'at'],
|
|
44
|
-
},
|
|
45
|
-
links: {
|
|
46
|
-
automation: one([automation.name], 'The automation that ran'),
|
|
47
|
-
about: about('The record it ran for'),
|
|
48
|
-
},
|
|
49
|
-
rules: { onePer: ['link:automation', 'link:about'] },
|
|
50
|
-
});
|
|
51
|
-
export const ACTIVITY_STATES = ['working', 'waiting', 'done', 'failed'];
|
|
52
|
-
/**
|
|
53
|
-
* Someone at work on a record, for apps to show while it lasts: "My bot is
|
|
54
|
-
* replying…" under a message, a spinner on a task. One per account per
|
|
55
|
-
* record, changed in place as the work goes on. Its writer's word, like a
|
|
56
|
-
* status, and only theirs to change.
|
|
57
|
-
*/
|
|
58
|
-
export const activity = define({
|
|
59
|
-
name: 'std.activity',
|
|
60
|
-
title: 'Activity',
|
|
61
|
-
description: 'Someone at work on a record: working, waiting, done or failed.',
|
|
62
|
-
schema: {
|
|
63
|
-
type: 'object',
|
|
64
|
-
properties: {
|
|
65
|
-
state: { type: 'string', enum: [...ACTIVITY_STATES] },
|
|
66
|
-
label: words(120),
|
|
67
|
-
at: moment('When it came to this state'),
|
|
68
|
-
},
|
|
69
|
-
required: ['state', 'at'],
|
|
70
|
-
},
|
|
71
|
-
links: { about: about('What the work is on') },
|
|
72
|
-
rules: { onePer: ['@author', 'link:about'] },
|
|
73
|
-
});
|
|
74
|
-
/**
|
|
75
|
-
* How long a `working` or `waiting` activity is believed without a change:
|
|
76
|
-
* one whose writer went away mid-way would otherwise show forever.
|
|
77
|
-
*/
|
|
78
|
-
export const ACTIVITY_STALE_SECONDS = 300;
|
|
79
|
-
/** Whether an activity says someone is at work on it now: working or waiting, and not gone stale */
|
|
80
|
-
export function activeNow(body, now = Date.now()) {
|
|
81
|
-
if (!body || (body.state !== 'working' && body.state !== 'waiting'))
|
|
82
|
-
return false;
|
|
83
|
-
const at = Date.parse(body.at);
|
|
84
|
-
return Number.isFinite(at) && now - at < ACTIVITY_STALE_SECONDS * 1000;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Says where this account's work on a record stands: its one `std.activity`
|
|
88
|
-
* about it, made or changed. Without a label, the one it had stays. Null
|
|
89
|
-
* where the space keeps no `std.activity`.
|
|
90
|
-
*/
|
|
91
|
-
export async function setActivity(node, space, on, state, label) {
|
|
92
|
-
if (!(await definedNames(node, space)).has(activity.name))
|
|
93
|
-
return null;
|
|
94
|
-
const mine = await node.records.linked(space, on, { rel: 'about', collection: activity.name });
|
|
95
|
-
const kept = label ?? mine.find((record) => record.root === node.did && !record.deleted)?.body?.label;
|
|
96
|
-
const body = {
|
|
97
|
-
state,
|
|
98
|
-
...(kept ? { label: kept.slice(0, 120) } : {}),
|
|
99
|
-
at: new Date().toISOString(),
|
|
100
|
-
};
|
|
101
|
-
// One per account per record: a put for one it already has is that record's next version.
|
|
102
|
-
return node.records.put(space, activity.name, body, { links: [{ rel: 'about', to: on }] });
|
|
103
|
-
}
|
|
104
|
-
/** In an action, the record the automation holds for */
|
|
105
|
-
export const IT = '$it';
|
|
106
|
-
/** In an automation's query or condition, whoever runs it: its maker, or the bot named in `by` */
|
|
107
|
-
export const ME = '$me';
|
|
108
|
-
/**
|
|
109
|
-
* The command a maker needs a grant of in a space for a bot there to run
|
|
110
|
-
* their rules: a library command, which no peer checks (docs/automations.md)
|
|
111
|
-
*/
|
|
112
|
-
export const INSTRUCT = `/${automation.name}/instruct`;
|
|
113
|
-
const said = { type: 'string', pattern: '\\S' };
|
|
114
|
-
const named = { type: 'string', minLength: 1 };
|
|
115
|
-
const saying = { type: 'object', properties: { text: { ...said, maxLength: 500 } }, required: ['text'] };
|
|
116
|
-
/** Each kind of action's shape, which the stored schema leaves open */
|
|
117
|
-
const ACTIONS = {
|
|
118
|
-
notify: saying,
|
|
119
|
-
ask: saying,
|
|
120
|
-
add: {
|
|
121
|
-
type: 'object',
|
|
122
|
-
properties: {
|
|
123
|
-
collection: named,
|
|
124
|
-
text: said,
|
|
125
|
-
links: {
|
|
126
|
-
type: 'array',
|
|
127
|
-
items: { type: 'object', properties: { rel: named, to: named }, required: ['rel', 'to'] },
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
required: ['collection', 'text'],
|
|
131
|
-
},
|
|
132
|
-
set: {
|
|
133
|
-
type: 'object',
|
|
134
|
-
properties: { field: named, value: { type: ['string', 'number', 'boolean'] } },
|
|
135
|
-
required: ['field', 'value'],
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
|
-
/** An automation's shape, with what sets it off spelled out */
|
|
139
|
-
const AUTOMATION = {
|
|
140
|
-
...automation.schema,
|
|
141
|
-
properties: {
|
|
142
|
-
...automation.schema.properties,
|
|
143
|
-
when: {
|
|
144
|
-
type: 'object',
|
|
145
|
-
properties: { from: { type: 'array', items: named }, others: { type: 'boolean' } },
|
|
146
|
-
},
|
|
147
|
-
spaces: {
|
|
148
|
-
anyOf: [{ const: 'all' }, { type: 'array', minItems: 1, maxItems: 256, items: { type: 'string' } }],
|
|
149
|
-
},
|
|
150
|
-
},
|
|
151
|
-
};
|
|
152
|
-
/** Why this can't be an automation, or null when it can */
|
|
153
|
-
export function checkAutomation(value) {
|
|
154
|
-
if (!isObject(value))
|
|
155
|
-
return 'An automation must be an object';
|
|
156
|
-
const shape = schemaProblem(AUTOMATION, value);
|
|
157
|
-
if (shape)
|
|
158
|
-
return shape;
|
|
159
|
-
if (typeof value.name !== 'string' || !value.name.trim())
|
|
160
|
-
return 'An automation needs a name';
|
|
161
|
-
if (value.when === undefined && value.every === undefined)
|
|
162
|
-
return 'An automation needs a when: { query, holds? }, an every, or both';
|
|
163
|
-
if (isObject(value.when)) {
|
|
164
|
-
const query = checkQuery(value.when.query);
|
|
165
|
-
if (query)
|
|
166
|
-
return `when.query: ${query}`;
|
|
167
|
-
const holds = value.when.holds === undefined ? null : checkFilter(value.when.holds, 'when.holds', { include: true });
|
|
168
|
-
if (holds)
|
|
169
|
-
return holds;
|
|
170
|
-
}
|
|
171
|
-
const every = typeof value.every === 'string' ? checkCron(value.every) : null;
|
|
172
|
-
if (every)
|
|
173
|
-
return `every: ${every}`;
|
|
174
|
-
const then = value.then;
|
|
175
|
-
const action = isObject(then) && typeof then.kind === 'string' ? ACTIONS[then.kind] : undefined;
|
|
176
|
-
if (!isObject(then) || !action)
|
|
177
|
-
return `then.kind must be one of ${Object.keys(ACTIONS).join(', ')}`;
|
|
178
|
-
const actionShape = schemaProblem(action, then, 'then');
|
|
179
|
-
if (actionShape)
|
|
180
|
-
return actionShape;
|
|
181
|
-
if (then.kind === 'set' && value.when === undefined)
|
|
182
|
-
return 'An automation that changes a record needs a when';
|
|
183
|
-
if (typeof value.since !== 'string' || !Number.isFinite(Date.parse(value.since)))
|
|
184
|
-
return 'since must be a date';
|
|
185
|
-
if (value.open !== undefined && !isWebAddress(value.open))
|
|
186
|
-
return 'open must be an https:// address';
|
|
187
|
-
if (isObject(value.app) && !isOrigin(value.app.origin))
|
|
188
|
-
return 'app.origin must be a web origin';
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
/** An https:// address, or http:// on this machine */
|
|
192
|
-
function isWebAddress(value) {
|
|
193
|
-
const url = parseUrl(value);
|
|
194
|
-
return url?.protocol === 'https:' || (url?.protocol === 'http:' && LOCAL_HOSTS.includes(url.hostname));
|
|
195
|
-
}
|
|
196
|
-
const LOCAL_HOSTS = ['localhost', '127.0.0.1'];
|
|
197
|
-
/** A browser extension's origin, which `URL` gives none */
|
|
198
|
-
const EXTENSION_ORIGIN = /^(chrome|moz|safari-web)-extension:\/\/[a-z0-9-]{1,64}$/i;
|
|
199
|
-
/** `https://chat.example`, nothing after it: what a browser reports as a message's origin */
|
|
200
|
-
function isOrigin(value) {
|
|
201
|
-
if (typeof value !== 'string')
|
|
202
|
-
return false;
|
|
203
|
-
return EXTENSION_ORIGIN.test(value) || parseUrl(value)?.origin === value;
|
|
204
|
-
}
|
|
205
|
-
function parseUrl(value) {
|
|
206
|
-
if (typeof value !== 'string')
|
|
207
|
-
return null;
|
|
208
|
-
try {
|
|
209
|
-
return new URL(value);
|
|
210
|
-
}
|
|
211
|
-
catch {
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
const isAutomation = (value) => checkAutomation(value) === null;
|
|
216
|
-
/** An automation record's body, when it is one */
|
|
217
|
-
export const automationOf = (record) => isAutomation(record.body) ? record.body : null;
|
|
218
|
-
/** `$me` wherever it is a value, as the account running the automation */
|
|
219
|
-
export function asRunner(value, me) {
|
|
220
|
-
// eslint-disable-next-line typescript/consistent-type-assertions -- the same shape, with one string swapped for another
|
|
221
|
-
return JSON.parse(JSON.stringify(value), (_key, found) => (found === ME ? me : found));
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* The records an automation holds for now, most recently changed first, for `me`,
|
|
225
|
-
* who runs it. Includes that count are run as lists and counted here, so the
|
|
226
|
-
* newest of them can tell when the automation came to hold.
|
|
227
|
-
*/
|
|
228
|
-
export async function matching(node, space, ruleWhen, me, limit = 200) {
|
|
229
|
-
const when = asRunner(ruleWhen, me);
|
|
230
|
-
const query = plainQuery(when.query);
|
|
231
|
-
const { include, counted } = countsAsLists(query.include ?? {});
|
|
232
|
-
const { records } = await node.records.query(space, {
|
|
233
|
-
...query,
|
|
234
|
-
include,
|
|
235
|
-
sort: query.sort ?? { '@updatedAt': 'desc' },
|
|
236
|
-
limit: query.limit ?? limit,
|
|
237
|
-
});
|
|
238
|
-
const holds = (subject) => when.holds === undefined || matches(subject, when.holds);
|
|
239
|
-
const causeIn = await causeFinder(node, space, query, when.holds, holds);
|
|
240
|
-
const found = [];
|
|
241
|
-
for (const record of records) {
|
|
242
|
-
const { included, newest } = readIncluded(record, counted);
|
|
243
|
-
if (!holds({ ...record, included }))
|
|
244
|
-
continue;
|
|
245
|
-
const cause = await causeIn(record, included);
|
|
246
|
-
// What made it hold can't be told: better quiet than set off by the wrong one.
|
|
247
|
-
if (!cause)
|
|
248
|
-
continue;
|
|
249
|
-
const moment = cause === record ? newest : Math.max(newest, Date.parse(cause.updatedAt));
|
|
250
|
-
found.push({ record, included, moment, cause });
|
|
251
|
-
}
|
|
252
|
-
return found;
|
|
253
|
-
}
|
|
254
|
-
/** A query's includes, with those that count turned into lists, and the names of those */
|
|
255
|
-
function countsAsLists(include) {
|
|
256
|
-
const counted = new Set();
|
|
257
|
-
const lists = Object.fromEntries(Object.entries(include).map(([name, found]) => {
|
|
258
|
-
if (!found.count)
|
|
259
|
-
return [name, found];
|
|
260
|
-
counted.add(name);
|
|
261
|
-
const { count: _count, limit: _limit, ...listed } = found;
|
|
262
|
-
return [name, listed];
|
|
263
|
-
}));
|
|
264
|
-
return { include: lists, counted };
|
|
265
|
-
}
|
|
266
|
-
/** What a record's includes found, counted ones as numbers, and the newest moment among it and them */
|
|
267
|
-
function readIncluded(record, counted) {
|
|
268
|
-
let newest = Date.parse(record.updatedAt);
|
|
269
|
-
const included = {};
|
|
270
|
-
for (const [name, value] of Object.entries(record.included)) {
|
|
271
|
-
if (typeof value === 'number') {
|
|
272
|
-
included[name] = value;
|
|
273
|
-
continue;
|
|
274
|
-
}
|
|
275
|
-
for (const one of value)
|
|
276
|
-
newest = Math.max(newest, Date.parse(one.createdAt));
|
|
277
|
-
included[name] = counted.has(name) ? value.length : value;
|
|
278
|
-
}
|
|
279
|
-
return { included, newest };
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* How to tell what made a record hold. A computed field the automation reads
|
|
283
|
-
* changes when what it reads is written: a 👍 can make a todo done. It is what
|
|
284
|
-
* made it hold, not what came last: a 😡 after the second 👍 sets nothing off.
|
|
285
|
-
*/
|
|
286
|
-
async function causeFinder(node, space, query, holdsFilter, holds) {
|
|
287
|
-
const name = nameOf(query.collection);
|
|
288
|
-
const fields = (await node.collections.list(space)).find((c) => c.name === name)?.computed ?? {};
|
|
289
|
-
const read = { ...namedComputed(fields, query.where), ...namedComputed(fields, holdsFilter) };
|
|
290
|
-
if (Object.keys(read).length === 0)
|
|
291
|
-
return async (record) => record;
|
|
292
|
-
const source = {
|
|
293
|
-
linked: (key, o) => node.records.linked(space, key, o),
|
|
294
|
-
};
|
|
295
|
-
return (record, included) => causeOf(source, record, read, (computed) => {
|
|
296
|
-
const subject = { ...record, included, computed };
|
|
297
|
-
return matches(subject, query.where ?? {}) && holds(subject);
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
/** What a record is called, for `{title}`: its first text among the usual names, or its key */
|
|
301
|
-
function titleOf(record) {
|
|
302
|
-
const body = isObject(record.body) ? record.body : {};
|
|
303
|
-
for (const name of ['title', 'name', 'question', 'text']) {
|
|
304
|
-
const value = body[name];
|
|
305
|
-
if (typeof value === 'string' && value.trim())
|
|
306
|
-
return value.length > 80 ? `${value.slice(0, 79)}…` : value;
|
|
307
|
-
}
|
|
308
|
-
return record.key;
|
|
309
|
-
}
|
|
310
|
-
/** `{title}`, and `{<include>}` as a count, filled in */
|
|
311
|
-
export function fillAutomationText(text, match, title = titleOf) {
|
|
312
|
-
return text.replace(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (whole, name) => {
|
|
313
|
-
if (!match)
|
|
314
|
-
return whole;
|
|
315
|
-
if (name === 'title')
|
|
316
|
-
return title(match.record);
|
|
317
|
-
const value = match.included[name];
|
|
318
|
-
if (typeof value === 'number')
|
|
319
|
-
return String(value);
|
|
320
|
-
if (Array.isArray(value))
|
|
321
|
-
return String(value.length);
|
|
322
|
-
return whole;
|
|
323
|
-
});
|
|
324
|
-
}
|
|
325
|
-
/** Does what an automation says, as whoever runs it; says what happened */
|
|
326
|
-
export async function act(node, trigger, options = {}) {
|
|
327
|
-
const { automation: active, match } = trigger;
|
|
328
|
-
const at = new Date().toISOString();
|
|
329
|
-
const fill = (template) => fillAutomationText(template, match, options.title);
|
|
330
|
-
const space = active.space;
|
|
331
|
-
const then = active.body.then;
|
|
332
|
-
try {
|
|
333
|
-
switch (then.kind) {
|
|
334
|
-
case 'notify': {
|
|
335
|
-
const text = fill(then.text);
|
|
336
|
-
const shown = options.notify?.(active.body.name, text, match?.record) ?? false;
|
|
337
|
-
return { did: shown ? `Notified: ${text}` : 'Nothing here could notify', ok: shown, at };
|
|
338
|
-
}
|
|
339
|
-
case 'ask':
|
|
340
|
-
if (!options.ask)
|
|
341
|
-
return { did: 'Nothing here can ask an agent', ok: false, at };
|
|
342
|
-
return { ...(await options.ask(trigger)), at };
|
|
343
|
-
case 'add': {
|
|
344
|
-
const collections = await node.collections.list(space);
|
|
345
|
-
const target = collections.find((c) => c.name === then.collection && c.version !== null);
|
|
346
|
-
const thing = (target?.title ?? then.collection.split('.').pop() ?? then.collection).toLowerCase();
|
|
347
|
-
const fresh = target ? quickAddBody(target.schema, fill(then.text)) : null;
|
|
348
|
-
if (!target || !fresh)
|
|
349
|
-
return { did: `This space can't add a ${thing} from a line of text`, ok: false, at };
|
|
350
|
-
// Without a record, as when the time set it off, a link to it has nothing to point at.
|
|
351
|
-
const links = (then.links ?? []).flatMap((l) => l.to !== IT ? [l] : match ? [{ rel: l.rel, to: match.record.key }] : []);
|
|
352
|
-
const made = await node.records.put(space, target.name, fresh, { links });
|
|
353
|
-
return { did: `Added a ${thing}: ${fill(then.text)}`, ok: true, at, made: made.key };
|
|
354
|
-
}
|
|
355
|
-
case 'set': {
|
|
356
|
-
const current = match ? await node.records.get(space, match.record.key) : null;
|
|
357
|
-
const was = isObject(current?.body) ? current.body : null;
|
|
358
|
-
if (!current || !was)
|
|
359
|
-
return { did: 'It is not there any more', ok: false, at };
|
|
360
|
-
if (!(await node.records.can(space, 'edit', current.key)))
|
|
361
|
-
return { did: 'Only whoever made it can change it', ok: false, at };
|
|
362
|
-
await node.records.update(space, current.key, { ...was, [then.field]: then.value });
|
|
363
|
-
return { did: `${fill('“{title}”')}: set ${then.field} to ${String(then.value)}`, ok: true, at };
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
catch (error) {
|
|
368
|
-
return { did: error instanceof Error ? error.message : String(error), ok: false, at };
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
/** Whether someone holds one of these roles; `member` is any place at all, and a founder holds every role's power */
|
|
372
|
-
const holdsOneOf = (access, did, names) => {
|
|
373
|
-
const member = access?.members.find((one) => one.did === did);
|
|
374
|
-
if (!member)
|
|
375
|
-
return false;
|
|
376
|
-
return member.founder || names.includes('member') || member.roles.some((role) => names.includes(role));
|
|
377
|
-
};
|
|
378
|
-
/** Whether an automation can run where `options` are: one that asks needs an agent */
|
|
379
|
-
const runnable = (active, options) => active.body.then.kind !== 'ask' || !!options.ask;
|
|
380
|
-
/**
|
|
381
|
-
* The automations in a space that `account` runs, none paused and none whose
|
|
382
|
-
* current version an agent wrote: those it made without `by`, and those
|
|
383
|
-
* naming it in `by` whose maker may instruct it here (`std.automation/instruct`).
|
|
384
|
-
*/
|
|
385
|
-
export async function automationsFor(node, space, account, access) {
|
|
386
|
-
const records = await node.records.list(space, { collection: automation.name });
|
|
387
|
-
const active = [];
|
|
388
|
-
for (const record of records) {
|
|
389
|
-
const maker = record.createdBy;
|
|
390
|
-
const body = automationOf(record);
|
|
391
|
-
// A key given records only, an agent's, may suggest an automation; it runs once the person saves it themselves.
|
|
392
|
-
if (!record.verified || record.recordsOnly || !maker || !body || body.paused)
|
|
393
|
-
continue;
|
|
394
|
-
if (await runsIt(node, space, account, maker, body.by, access))
|
|
395
|
-
active.push({ space, key: record.key, maker, body });
|
|
396
|
-
}
|
|
397
|
-
return active;
|
|
398
|
-
}
|
|
399
|
-
/** Whether `account` runs an automation: its own without `by`, or one naming it whose maker may instruct it */
|
|
400
|
-
async function runsIt(node, space, account, maker, by, access) {
|
|
401
|
-
if (by === undefined)
|
|
402
|
-
return maker === account;
|
|
403
|
-
if (by !== account)
|
|
404
|
-
return false;
|
|
405
|
-
if (maker === account)
|
|
406
|
-
return true;
|
|
407
|
-
const isMember = access?.members.some((member) => member.did === maker) ?? false;
|
|
408
|
-
return isMember && node.spaces.may(space, INSTRUCT, {}, maker);
|
|
409
|
-
}
|
|
410
|
-
/** How many automations in a space an agent wrote for `account`, waiting for the person to save them */
|
|
411
|
-
export async function suggestedAutomations(node, space, account) {
|
|
412
|
-
const records = await node.records.list(space, { collection: automation.name });
|
|
413
|
-
return records.filter((record) => record.createdBy === account && record.recordsOnly).length;
|
|
414
|
-
}
|
|
415
|
-
const linkTo = (record, rel) => record.links.find((l) => l.rel === rel)?.to;
|
|
416
|
-
/** The records each automation has run for, by automation key, and the records automations wrote */
|
|
417
|
-
async function runsIn(node, space) {
|
|
418
|
-
const by = new Map();
|
|
419
|
-
const made = new Set();
|
|
420
|
-
for (const run of await node.records.list(space, { collection: automationRun.name })) {
|
|
421
|
-
if (isObject(run.body) && typeof run.body.made === 'string')
|
|
422
|
-
made.add(run.body.made);
|
|
423
|
-
const ran = linkTo(run, 'automation');
|
|
424
|
-
const on = linkTo(run, 'about');
|
|
425
|
-
if (!ran || !on)
|
|
426
|
-
continue;
|
|
427
|
-
const seen = by.get(ran) ?? new Set();
|
|
428
|
-
by.set(ran, seen.add(on));
|
|
429
|
-
}
|
|
430
|
-
return { by, made };
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Whether this claim is the one that acts. Two devices of one account may both
|
|
434
|
-
* claim a record before either hears the other; after a wait, the earlier
|
|
435
|
-
* claim (by time, then key) acts and the later is taken back.
|
|
436
|
-
*/
|
|
437
|
-
async function actsFirst(node, space, mine, wait) {
|
|
438
|
-
if (wait <= 0)
|
|
439
|
-
return true;
|
|
440
|
-
await new Promise((resolve) => setTimeout(resolve, wait));
|
|
441
|
-
const ran = linkTo(mine, 'automation');
|
|
442
|
-
const on = linkTo(mine, 'about');
|
|
443
|
-
if (!ran || !on)
|
|
444
|
-
return true;
|
|
445
|
-
const runs = await node.records.linked(space, on, { rel: 'about', collection: automationRun.name });
|
|
446
|
-
const claims = runs.filter((run) => run.links.some((l) => l.rel === 'automation' && l.to === ran));
|
|
447
|
-
const earliest = claims.sort((a, b) => a.createdAt.localeCompare(b.createdAt) || a.key.localeCompare(b.key))[0];
|
|
448
|
-
if (!earliest || earliest.key === mine.key)
|
|
449
|
-
return true;
|
|
450
|
-
await node.records.delete(space, mine.key).catch(() => { });
|
|
451
|
-
return false;
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Runs, once, every automation `account` runs in a space: acts for each record
|
|
455
|
-
* an automation newly holds for, and leaves a run for it. `startAutomations`
|
|
456
|
-
* calls it when records change; a device can call it itself.
|
|
457
|
-
*/
|
|
458
|
-
export async function runAutomations(node, space, account, options = {}) {
|
|
459
|
-
const defined = await definedNames(node, space);
|
|
460
|
-
if (!defined.has(automation.name) || !defined.has(automationRun.name))
|
|
461
|
-
return;
|
|
462
|
-
const access = await node.spaces.access(space).catch(() => undefined);
|
|
463
|
-
const rules = (await automationsFor(node, space, account, access)).filter((r) => r.body.when && runnable(r, options));
|
|
464
|
-
if (rules.length === 0)
|
|
465
|
-
return;
|
|
466
|
-
const busy = options.busy ?? new Set();
|
|
467
|
-
const { by: runs, made } = await runsIn(node, space);
|
|
468
|
-
for (const active of rules) {
|
|
469
|
-
const when = active.body.when;
|
|
470
|
-
if (!when || !defined.has(nameOf(when.query.collection)))
|
|
471
|
-
continue;
|
|
472
|
-
const since = Date.parse(active.body.since);
|
|
473
|
-
for (const match of await matching(node, space, when, account)) {
|
|
474
|
-
const key = match.record.key;
|
|
475
|
-
const claim = `${active.key} ${key}`;
|
|
476
|
-
const handled = made.has(key) || runs.get(active.key)?.has(key) || busy.has(claim);
|
|
477
|
-
if (match.moment < since || handled || !setsOff(match, when, account, access, options.bot))
|
|
478
|
-
continue;
|
|
479
|
-
busy.add(claim);
|
|
480
|
-
try {
|
|
481
|
-
await claimAndAct(node, active, match, options, made);
|
|
482
|
-
}
|
|
483
|
-
finally {
|
|
484
|
-
busy.delete(claim);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
/** Whether whoever made a match hold may set the automation off for its runner */
|
|
490
|
-
function setsOff(match, when, account, access, bot) {
|
|
491
|
-
const cause = match.cause ?? match.record;
|
|
492
|
-
const by = cause.createdBy ?? cause.root;
|
|
493
|
-
// What the runner's agent, or the bot, wrote itself never sets an automation off: it would set itself off.
|
|
494
|
-
if (by === account && (cause.recordsOnly || bot))
|
|
495
|
-
return false;
|
|
496
|
-
if (when.from && !holdsOneOf(access, by ?? '', when.from))
|
|
497
|
-
return false;
|
|
498
|
-
return !(when.others && by === account);
|
|
499
|
-
}
|
|
500
|
-
/** Claims the record with a run first, so a second look leaves it alone, then acts and fills the run in */
|
|
501
|
-
async function claimAndAct(node, active, match, options, made) {
|
|
502
|
-
const space = active.space;
|
|
503
|
-
const links = [
|
|
504
|
-
{ rel: 'automation', to: active.key },
|
|
505
|
-
{ rel: 'about', to: match.record.key },
|
|
506
|
-
];
|
|
507
|
-
const claim = { did: 'Running…', ok: true, at: new Date().toISOString() };
|
|
508
|
-
const run = await node.records.put(space, automationRun.name, claim, { links });
|
|
509
|
-
if (!(await actsFirst(node, space, run, options.claimMs ?? 0)))
|
|
510
|
-
return;
|
|
511
|
-
const trigger = { automation: active, match };
|
|
512
|
-
const done = await act(node, trigger, options);
|
|
513
|
-
if (done.made)
|
|
514
|
-
made.add(done.made);
|
|
515
|
-
await node.records.update(space, run.key, done, { links });
|
|
516
|
-
options.onRun?.(trigger, done);
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Runs the automations `account` runs, in every space it follows, until stopped:
|
|
520
|
-
* once at the start, after each change in a space, and at the times automations
|
|
521
|
-
* name. The one loop every runner uses, an app, `minca agent` or a bot.
|
|
522
|
-
*/
|
|
523
|
-
export function startAutomations(node, options) {
|
|
524
|
-
const { account } = options;
|
|
525
|
-
const busy = options.busy ?? new Set();
|
|
526
|
-
const now = options.now ?? (() => new Date());
|
|
527
|
-
let stopped = false;
|
|
528
|
-
let spaces = [];
|
|
529
|
-
/** The automations it runs, by space */
|
|
530
|
-
const kept = new Map();
|
|
531
|
-
let shown = null;
|
|
532
|
-
let running = null;
|
|
533
|
-
/** Spaces with a change not yet looked at; null means the list of spaces changed */
|
|
534
|
-
const dirty = new Set([null]);
|
|
535
|
-
/** The minute each timed automation last ran, so it runs once in it */
|
|
536
|
-
const ranAt = new Map();
|
|
537
|
-
const allKept = () => [...kept.values()].flat();
|
|
538
|
-
async function run() {
|
|
539
|
-
while (dirty.size && !stopped) {
|
|
540
|
-
const changed = [...dirty];
|
|
541
|
-
dirty.clear();
|
|
542
|
-
try {
|
|
543
|
-
if (!(await look(changed)))
|
|
544
|
-
return;
|
|
545
|
-
announce();
|
|
546
|
-
}
|
|
547
|
-
catch (error) {
|
|
548
|
-
options.onError?.(error);
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
/** Runs the automations of the spaces that changed; false when stopped part way */
|
|
553
|
-
async function look(changed) {
|
|
554
|
-
const everySpace = changed.includes(null);
|
|
555
|
-
if (everySpace)
|
|
556
|
-
spaces = (await node.spaces.list()).map((space) => space.id);
|
|
557
|
-
for (const space of kept.keys())
|
|
558
|
-
if (!spaces.includes(space))
|
|
559
|
-
kept.delete(space);
|
|
560
|
-
const looking = everySpace ? spaces : changed.filter((s) => !!s && spaces.includes(s));
|
|
561
|
-
for (const space of looking) {
|
|
562
|
-
if (stopped)
|
|
563
|
-
return false;
|
|
564
|
-
const access = await node.spaces.access(space).catch(() => undefined);
|
|
565
|
-
const rules = await automationsFor(node, space, account, access);
|
|
566
|
-
kept.set(space, rules.filter((r) => runnable(r, options)));
|
|
567
|
-
await runAutomations(node, space, account, { ...options, busy });
|
|
568
|
-
}
|
|
569
|
-
return true;
|
|
570
|
-
}
|
|
571
|
-
/** Tells `onRules` the automations it runs, when they changed */
|
|
572
|
-
function announce() {
|
|
573
|
-
const all = allKept();
|
|
574
|
-
const names = all.map((r) => `${r.space}/${r.key}/${r.body.name}`).join();
|
|
575
|
-
if (names === shown)
|
|
576
|
-
return;
|
|
577
|
-
shown = names;
|
|
578
|
-
options.onRules?.(all);
|
|
579
|
-
}
|
|
580
|
-
function kick() {
|
|
581
|
-
running ??= run().finally(() => {
|
|
582
|
-
running = null;
|
|
583
|
-
if (dirty.size && !stopped)
|
|
584
|
-
kick();
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
/** Runs each timed automation that names this minute, once in it */
|
|
588
|
-
function tick() {
|
|
589
|
-
const at = now();
|
|
590
|
-
const minute = `${at.getFullYear()}-${at.getMonth()}-${at.getDate()} ${at.getHours()}:${at.getMinutes()}`;
|
|
591
|
-
for (const active of allKept()) {
|
|
592
|
-
const every = active.body.every;
|
|
593
|
-
const id = `${active.space}/${active.key}`;
|
|
594
|
-
if (!every || ranAt.get(id) === minute || !cronMatches(every, at))
|
|
595
|
-
continue;
|
|
596
|
-
ranAt.set(id, minute);
|
|
597
|
-
const trigger = { automation: active, at };
|
|
598
|
-
void act(node, trigger, options).then((done) => options.onRun?.(trigger, done), (error) => options.onError?.(error));
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
const unsubscribe = node.subscribe((event) => {
|
|
602
|
-
if (event.type === 'spaces' || event.type === 'account')
|
|
603
|
-
dirty.add(null);
|
|
604
|
-
else if (event.type === 'records')
|
|
605
|
-
dirty.add(spaces.includes(event.space) ? event.space : null);
|
|
606
|
-
else
|
|
607
|
-
return;
|
|
608
|
-
kick();
|
|
609
|
-
});
|
|
610
|
-
kick();
|
|
611
|
-
const timer = options.timed === false ? undefined : setInterval(tick, options.tickMs ?? 15_000);
|
|
612
|
-
return () => {
|
|
613
|
-
stopped = true;
|
|
614
|
-
if (timer)
|
|
615
|
-
clearInterval(timer);
|
|
616
|
-
unsubscribe();
|
|
617
|
-
};
|
|
618
|
-
}
|
|
619
|
-
//# sourceMappingURL=automations.js.map
|