@composable-os/cli 0.1.0-beta.10 → 0.1.0-beta.13
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/dist/{accounts-X4CKM3HL.js → accounts-GOCNY6WI.js} +17 -14
- package/dist/app-RPRZMLU5.js +43 -0
- package/dist/app-upload-YJVEKZC6.js +198 -0
- package/dist/{blocks-YWDHEBOK.js → blocks-DE4RRWPJ.js} +207 -25
- package/dist/{chunk-VY64DMQO.js → chunk-2B6YUYZV.js} +23576 -11674
- package/dist/{run-RT2HVWMK.js → chunk-2CCHB37H.js} +9 -146
- package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
- package/dist/chunk-5D7BDF4Y.js +141 -0
- package/dist/{chunk-WO6D3XP3.js → chunk-6CM3WZG5.js} +13 -2470
- package/dist/chunk-CIBJGB5H.js +962 -0
- package/dist/chunk-EPDTPGZJ.js +330 -0
- package/dist/{chunk-LLULPZAJ.js → chunk-HAYF4G5V.js} +66 -23
- package/dist/chunk-HCG6T53D.js +61904 -0
- package/dist/chunk-JX4IFWNT.js +330 -0
- package/dist/{chunk-A4NTSYWV.js → chunk-KFHKNXGH.js} +786 -386
- package/dist/chunk-KMFMD52Z.js +13276 -0
- package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
- package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
- package/dist/{chunk-LJFLENFC.js → chunk-Q6Y45DY6.js} +25 -2
- package/dist/{chunk-Y7JZFSTQ.js → chunk-QJZGYJVV.js} +823 -44
- package/dist/{chunk-6VXHPKGG.js → chunk-QQ3YSL5F.js} +172 -16
- package/dist/chunk-RGKJKC5T.js +876 -0
- package/dist/{chunk-IFG4FRW5.js → chunk-RSEWEFAV.js} +12 -2
- package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
- package/dist/{chunk-CP6BXI6F.js → chunk-U72NEGCT.js} +8818 -3829
- package/dist/{chunk-WC5LHBBA.js → chunk-Z4IKF23A.js} +11 -7
- package/dist/chunk-Z7OVQ6BC.js +713 -0
- package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
- package/dist/{cli-2DFENR62.js → cli-OKQHHSOE.js} +17 -23
- package/dist/{cli-YIOVSKOD.js → cli-VYSD2S4Z.js} +231 -82
- package/dist/cli.js +5 -5
- package/dist/{client-repo-PRU3VCQQ.js → client-repo-NNUCHQ2Y.js} +7 -5
- package/dist/create.js +5 -5
- package/dist/{fields-2NC7LSYE.js → fields-GTHE6TTS.js} +27 -3
- package/dist/gateway-DPPEQZT4.js +25 -0
- package/dist/grants-6JY4XZO7.js +27 -0
- package/dist/invoke-MRONRZ5Q.js +33 -0
- package/dist/moves-UAUCMWL5.js +36 -0
- package/dist/{order-PUWD65BC.js → order-RKLGT74L.js} +2 -2
- package/dist/{person-WIQQ7ITU.js → person-YOZTFV4Q.js} +11 -11
- package/dist/plan-on-sale-H4F26YIN.js +26 -0
- package/dist/platform/migrations/0610_waitlists.sql +54 -0
- package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
- package/dist/platform/migrations/0650_person_links.sql +90 -0
- package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
- package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
- package/dist/platform/migrations/0730_practitioners.sql +79 -0
- package/dist/platform/migrations/0750_routine_mail.sql +98 -0
- package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
- package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
- package/dist/platform/migrations/0811_staff_time.sql +109 -0
- package/dist/platform/migrations/0812_staff_offers.sql +112 -0
- package/dist/platform/migrations/0813_pay_periods.sql +241 -0
- package/dist/platform/migrations/0814_staff_feed.sql +47 -0
- package/dist/platform/migrations/0815_checklists.sql +237 -0
- package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
- package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
- package/dist/platform/migrations/0850_agreements.sql +271 -0
- package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
- package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
- package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
- package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
- package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
- package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
- package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
- package/dist/platform/migrations/0910_routine_messages.sql +99 -0
- package/dist/platform/migrations/0911_texts.sql +60 -0
- package/dist/platform/migrations/0913_contact_consent.sql +93 -0
- package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
- package/dist/platform/migrations/0930_stock_movements.sql +95 -0
- package/dist/platform/migrations/0931_stock_low.sql +25 -0
- package/dist/platform/migrations/0932_order_delivery.sql +78 -0
- package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
- package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
- package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
- package/dist/platform/migrations/0953_discount_uses.sql +183 -0
- package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
- package/dist/platform/migrations/0970_published_text.sql +59 -0
- package/dist/platform/migrations/0971_publish_acks.sql +68 -0
- package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
- package/dist/platform/migrations/1010_intake_links.sql +148 -0
- package/dist/platform/migrations/1030_invoices.sql +747 -0
- package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
- package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
- package/dist/platform/migrations/1090_staff_hours.sql +43 -0
- package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
- package/dist/platform/migrations/1092_client_guard.sql +26 -0
- package/dist/platform/migrations/1095_thing_time.sql +70 -0
- package/dist/platform/migrations/1130_devices.sql +162 -0
- package/dist/platform/migrations/1135_person_codes.sql +88 -0
- package/dist/platform/migrations/1150_finance_periods.sql +316 -0
- package/dist/platform/migrations/1151_payouts.sql +202 -0
- package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
- package/dist/platform/migrations/1170_journeys.sql +318 -0
- package/dist/platform/migrations/1171_person_birthday.sql +52 -0
- package/dist/platform/migrations/1175_person_signals.sql +29 -0
- package/dist/platform/migrations/1190_restricted_values.sql +25 -0
- package/dist/platform/migrations/1230_note_levels.sql +113 -0
- package/dist/platform/migrations/1231_person_carers.sql +33 -0
- package/dist/platform/migrations/1232_note_kinds.sql +122 -0
- package/dist/platform/migrations/1270_time_entries.sql +145 -0
- package/dist/platform/migrations/1271_leave.sql +249 -0
- package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
- package/dist/platform/migrations/1330_accounting.sql +260 -0
- package/dist/platform/migrations/1350_customer_feed.sql +24 -0
- package/dist/platform/migrations/1390_locations.sql +77 -0
- package/dist/platform/migrations/1410_outside_copy.sql +213 -0
- package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
- package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
- package/dist/platform/sdk/acknowledge.ts +100 -0
- package/dist/platform/sdk/agreements.ts +185 -0
- package/dist/platform/sdk/allowance.ts +98 -8
- package/dist/platform/sdk/automation-check.ts +120 -9
- package/dist/platform/sdk/automation.ts +78 -10
- package/dist/platform/sdk/behaviour.ts +26 -6
- package/dist/platform/sdk/calendar-entry.ts +258 -0
- package/dist/platform/sdk/capacity.ts +103 -2
- package/dist/platform/sdk/checklists.ts +118 -0
- package/dist/platform/sdk/contract.ts +514 -27
- package/dist/platform/sdk/discount.ts +408 -0
- package/dist/platform/sdk/document.ts +4 -2
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/formula/compile.ts +10 -0
- package/dist/platform/sdk/index.ts +17 -0
- package/dist/platform/sdk/intake.ts +380 -0
- package/dist/platform/sdk/journey-signals.ts +215 -0
- package/dist/platform/sdk/manifest.ts +1313 -66
- package/dist/platform/sdk/mine.ts +161 -0
- package/dist/platform/sdk/occupies.ts +192 -0
- package/dist/platform/sdk/questions.ts +681 -0
- package/dist/platform/sdk/recurring.ts +90 -9
- package/dist/platform/sdk/restricted-fields.ts +129 -0
- package/dist/platform/sdk/rich-text.ts +112 -0
- package/dist/platform/sdk/routine-mail.ts +1272 -0
- package/dist/platform/sdk/runtime.ts +25 -12
- package/dist/platform/sdk/screens.ts +91 -0
- package/dist/platform/sdk/sellable.ts +595 -1
- package/dist/platform/sdk/slug.ts +7 -2
- package/dist/platform/sdk/staffed.ts +474 -0
- package/dist/platform/sdk/states.ts +220 -64
- package/dist/platform/sdk/stock.ts +332 -0
- package/dist/platform/sdk/team.ts +83 -0
- package/dist/platform/sdk/testing.ts +70 -6
- package/dist/platform/sdk/worked-on.ts +134 -0
- package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
- package/dist/run-7ID64RA6.js +65 -0
- package/dist/run-FH7OGGTV.js +131 -0
- package/dist/run-OYZIAP6M.js +3363 -0
- package/dist/{run-Y7T4TVOL.js → run-QOFRRSZV.js} +41 -60
- package/dist/{run-PZUCSDDL.js → run-TJAEWR5U.js} +21 -51
- package/dist/{src-XTOD7EXW.js → src-52VERUPK.js} +312 -4
- package/dist/src-PHRELWN5.js +40 -0
- package/dist/visibility-SZCINPP4.js +58 -0
- package/package.json +2 -2
- package/dist/chunk-6GDEDOV7.js +0 -157
- package/dist/chunk-6UPUF4ML.js +0 -29998
- package/dist/chunk-7CQGMVDO.js +0 -63
- package/dist/chunk-GYSGVT6R.js +0 -284
- package/dist/chunk-JJHHNV46.js +0 -227
- package/dist/chunk-K6T42YCH.js +0 -704
- package/dist/chunk-M7A6FR2Z.js +0 -2586
- package/dist/chunk-O2UIZHND.js +0 -249
- package/dist/chunk-Q4N55M2Z.js +0 -552
- package/dist/chunk-QGGJ2G6Z.js +0 -349
- package/dist/chunk-R4W5XAUD.js +0 -837
- package/dist/chunk-TQFGGSA4.js +0 -748
- package/dist/chunk-YJJHF65I.js +0 -759
- package/dist/chunk-Z3RMMB6S.js +0 -668
- package/dist/grants-QXMKYMGB.js +0 -32
- package/dist/invoke-I3NNNXVL.js +0 -39
- package/dist/plan-on-sale-NB774CQL.js +0 -26
- package/dist/src-GYS6NBQB.js +0 -44
- package/dist/visibility-DLGHU4YN.js +0 -33
package/dist/chunk-TQFGGSA4.js
DELETED
|
@@ -1,748 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
PROVIDERS,
|
|
4
|
-
parseRoleLayer
|
|
5
|
-
} from "./chunk-KGHA67HG.js";
|
|
6
|
-
import {
|
|
7
|
-
ACTORS,
|
|
8
|
-
ConflictError,
|
|
9
|
-
RISK_CLASSES,
|
|
10
|
-
RawNode,
|
|
11
|
-
ValidationError,
|
|
12
|
-
createQueryId,
|
|
13
|
-
freeze,
|
|
14
|
-
sql
|
|
15
|
-
} from "./chunk-A4NTSYWV.js";
|
|
16
|
-
import {
|
|
17
|
-
external_exports
|
|
18
|
-
} from "./chunk-CP6BXI6F.js";
|
|
19
|
-
|
|
20
|
-
// ../core/src/ids.ts
|
|
21
|
-
var ID_PREFIXES = {
|
|
22
|
-
person: "per",
|
|
23
|
-
organisation: "org",
|
|
24
|
-
affiliation: "aff",
|
|
25
|
-
activity: "act",
|
|
26
|
-
task: "tsk",
|
|
27
|
-
user: "usr",
|
|
28
|
-
view: "vw",
|
|
29
|
-
audit: "aud",
|
|
30
|
-
tag: "tag",
|
|
31
|
-
email: "eml",
|
|
32
|
-
phone: "phn",
|
|
33
|
-
domain: "dom",
|
|
34
|
-
address: "adr",
|
|
35
|
-
/** Records of extension objects (stage 3). */
|
|
36
|
-
record: "rec",
|
|
37
|
-
/** Extension run history rows. */
|
|
38
|
-
extensionRun: "xrn",
|
|
39
|
-
changeSet: "chs",
|
|
40
|
-
pendingAction: "pac",
|
|
41
|
-
connectedApp: "cap",
|
|
42
|
-
/** Integration connections (stage 6, ADR 0050). */
|
|
43
|
-
connection: "con",
|
|
44
|
-
integrationEvent: "iev",
|
|
45
|
-
jobChange: "jcs",
|
|
46
|
-
/** Stage history rows (ADR 0100). */
|
|
47
|
-
stageHistory: "sth",
|
|
48
|
-
dashboard: "dsh",
|
|
49
|
-
/** Automation runs (ADR 0130). */
|
|
50
|
-
automationRun: "arn",
|
|
51
|
-
/** Notifications (ADR 0132). */
|
|
52
|
-
notification: "ntf",
|
|
53
|
-
/** Automation events outbox rows (ADR 0171). */
|
|
54
|
-
outbox: "obx",
|
|
55
|
-
/** Admin notification channel routes (ADR 0172). */
|
|
56
|
-
notificationRoute: "nrt",
|
|
57
|
-
/** In-app assistant conversations (ADR 0200). */
|
|
58
|
-
assistantConversation: "asc",
|
|
59
|
-
/** A person's one assistant thread (ADR 0340). */
|
|
60
|
-
assistantThread: "ath",
|
|
61
|
-
/** A memory line (ADR 0342). */
|
|
62
|
-
memory: "mem",
|
|
63
|
-
/** A personal routine (ADR 0343). */
|
|
64
|
-
routine: "rtn",
|
|
65
|
-
/** Policy adjudicator verdicts (ADR 0204). */
|
|
66
|
-
adjudication: "adj",
|
|
67
|
-
/** Assignment rules (ADR 0246). */
|
|
68
|
-
assignmentRule: "asr",
|
|
69
|
-
/** An agent (ADR 0344). */
|
|
70
|
-
agentJob: "job",
|
|
71
|
-
/** One approval's claim on the actions it applies (ADR 0345). */
|
|
72
|
-
execution: "exe",
|
|
73
|
-
/** A customer account (stage 22, ADR 0402): a Person who can sign in. Never a core_user. */
|
|
74
|
-
customer: "cus",
|
|
75
|
-
/** A request to erase a Person, made when a customer deletes their account (stage 22). */
|
|
76
|
-
eraseRequest: "erq",
|
|
77
|
-
/** An order and one of its lines (stage 25, ADR 0426). */
|
|
78
|
-
order: "ord",
|
|
79
|
-
orderLine: "orl",
|
|
80
|
-
/** A refund of an order's payment (stage 25, ADR 0429). */
|
|
81
|
-
refund: "rfd",
|
|
82
|
-
/** A subscription the payment provider runs (stage 32a, ADR 0485). */
|
|
83
|
-
subscription: "sbs",
|
|
84
|
-
/** A continuation offer: free until a date for a subscriber moving from elsewhere (ADR 0491). */
|
|
85
|
-
subscriptionOffer: "sof",
|
|
86
|
-
/** A native app's signed-in session on the HTTP surface (stage 23, ADR 0419). */
|
|
87
|
-
deviceSession: "dvs",
|
|
88
|
-
/** Places claimed from a record's capacity (stage 26, ADR 0430). */
|
|
89
|
-
capacityClaim: "clm",
|
|
90
|
-
/** A use spent from an allowance holder by a spender (stage 32b, ADR 0493). */
|
|
91
|
-
allowanceUse: "alu",
|
|
92
|
-
/** A Person's one purchase of something sold once per Person (stage 32b, ADR 0512). */
|
|
93
|
-
sellableOnce: "sbo",
|
|
94
|
-
/** An invite for a staff account (ADR 0455). */
|
|
95
|
-
userInvite: "uin",
|
|
96
|
-
// Stage 28 (ADR 0444): a picture of the media library.
|
|
97
|
-
media: "med"
|
|
98
|
-
};
|
|
99
|
-
var ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
100
|
-
var lastTime = -1;
|
|
101
|
-
var lastRandom = [];
|
|
102
|
-
function randomDigits() {
|
|
103
|
-
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
|
104
|
-
return Array.from(bytes, (byte) => byte & 31);
|
|
105
|
-
}
|
|
106
|
-
function increment(digits) {
|
|
107
|
-
const next = [...digits];
|
|
108
|
-
for (let i = next.length - 1; i >= 0; i--) {
|
|
109
|
-
const digit = next[i] ?? 0;
|
|
110
|
-
if (digit < 31) {
|
|
111
|
-
next[i] = digit + 1;
|
|
112
|
-
return next;
|
|
113
|
-
}
|
|
114
|
-
next[i] = 0;
|
|
115
|
-
}
|
|
116
|
-
throw new Error("ULID random component overflowed within one millisecond");
|
|
117
|
-
}
|
|
118
|
-
function ulid(now = Date.now()) {
|
|
119
|
-
let random;
|
|
120
|
-
if (now <= lastTime) {
|
|
121
|
-
random = increment(lastRandom);
|
|
122
|
-
now = lastTime;
|
|
123
|
-
} else {
|
|
124
|
-
random = randomDigits();
|
|
125
|
-
lastTime = now;
|
|
126
|
-
}
|
|
127
|
-
lastRandom = random;
|
|
128
|
-
let time = "";
|
|
129
|
-
let t = now;
|
|
130
|
-
for (let i = 0; i < 10; i++) {
|
|
131
|
-
time = ALPHABET[t % 32] + time;
|
|
132
|
-
t = Math.floor(t / 32);
|
|
133
|
-
}
|
|
134
|
-
return time + random.map((digit) => ALPHABET[digit]).join("");
|
|
135
|
-
}
|
|
136
|
-
function newId(kind, now) {
|
|
137
|
-
return `${ID_PREFIXES[kind]}_${ulid(now)}`;
|
|
138
|
-
}
|
|
139
|
-
var PROVISIONAL_ID_RE = /^[a-z]{2,4}_pending_[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
140
|
-
var PROVISIONAL_ANYWHERE_RE = /[a-z]{2,4}_pending_[0-9A-HJKMNP-TV-Z]{26}/g;
|
|
141
|
-
var ANY_ID_RE = /[a-z]{2,4}_(?:pending_)?[0-9A-HJKMNP-TV-Z]{26}/g;
|
|
142
|
-
function toProvisionalId(id) {
|
|
143
|
-
const at = id.indexOf("_");
|
|
144
|
-
return at < 0 ? id : `${id.slice(0, at)}_pending_${id.slice(at + 1)}`;
|
|
145
|
-
}
|
|
146
|
-
function isProvisionalId(value) {
|
|
147
|
-
return typeof value === "string" && PROVISIONAL_ID_RE.test(value);
|
|
148
|
-
}
|
|
149
|
-
function findProvisionalIds(value) {
|
|
150
|
-
const text = JSON.stringify(value ?? null);
|
|
151
|
-
return [...new Set(text.match(PROVISIONAL_ANYWHERE_RE) ?? [])];
|
|
152
|
-
}
|
|
153
|
-
function findIds(value) {
|
|
154
|
-
const text = JSON.stringify(value ?? null);
|
|
155
|
-
return [...new Set(text.match(ANY_ID_RE) ?? [])];
|
|
156
|
-
}
|
|
157
|
-
function replaceIds(value, map) {
|
|
158
|
-
if (!map.size) return value;
|
|
159
|
-
const inString = (text) => text.replace(ANY_ID_RE, (id) => map.get(id) ?? id);
|
|
160
|
-
const walk = (v) => {
|
|
161
|
-
if (typeof v === "string") return inString(v);
|
|
162
|
-
if (Array.isArray(v)) return v.map(walk);
|
|
163
|
-
if (v && typeof v === "object") {
|
|
164
|
-
return Object.fromEntries(
|
|
165
|
-
Object.entries(v).map(([k, x]) => [inString(k), walk(x)])
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
return v;
|
|
169
|
-
};
|
|
170
|
-
return walk(value);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// ../core/src/gate.ts
|
|
174
|
-
var GATE_DECISIONS = ["auto", "review", "deny"];
|
|
175
|
-
var POLICY_PRESETS = ["cautious", "balanced", "autonomous"];
|
|
176
|
-
var approvalPolicySchema = external_exports.object({ preset: external_exports.enum(POLICY_PRESETS) });
|
|
177
|
-
var APPROVAL_POLICY_SETTING = "approval_policy";
|
|
178
|
-
var HUMAN_ALL_AUTO = {
|
|
179
|
-
read: "auto",
|
|
180
|
-
write: "auto",
|
|
181
|
-
"bulk-write": "auto",
|
|
182
|
-
destructive: "auto",
|
|
183
|
-
schema: "auto",
|
|
184
|
-
extension: "auto",
|
|
185
|
-
outbound: "auto"
|
|
186
|
-
};
|
|
187
|
-
var cautiousAi = {
|
|
188
|
-
read: "auto",
|
|
189
|
-
write: "review",
|
|
190
|
-
"bulk-write": "review",
|
|
191
|
-
destructive: "review",
|
|
192
|
-
schema: "review",
|
|
193
|
-
extension: "review",
|
|
194
|
-
outbound: "review"
|
|
195
|
-
};
|
|
196
|
-
var balancedAi = {
|
|
197
|
-
read: "auto",
|
|
198
|
-
write: "auto",
|
|
199
|
-
"bulk-write": { autoUpTo: 25, above: "review" },
|
|
200
|
-
destructive: "review",
|
|
201
|
-
schema: "review",
|
|
202
|
-
extension: "review",
|
|
203
|
-
outbound: "review"
|
|
204
|
-
};
|
|
205
|
-
var balancedMcp = { ...balancedAi, "bulk-write": "review" };
|
|
206
|
-
var autonomousAi = {
|
|
207
|
-
read: "auto",
|
|
208
|
-
write: "auto",
|
|
209
|
-
"bulk-write": "auto",
|
|
210
|
-
destructive: { autoUpTo: 10, above: "review" },
|
|
211
|
-
schema: "review",
|
|
212
|
-
extension: "review",
|
|
213
|
-
outbound: "auto"
|
|
214
|
-
};
|
|
215
|
-
var extensionRow = (bulk, destructive) => ({
|
|
216
|
-
read: "auto",
|
|
217
|
-
write: "auto",
|
|
218
|
-
"bulk-write": bulk,
|
|
219
|
-
destructive,
|
|
220
|
-
schema: "deny",
|
|
221
|
-
extension: "deny",
|
|
222
|
-
outbound: "review"
|
|
223
|
-
});
|
|
224
|
-
var PRESET_MATRICES = {
|
|
225
|
-
cautious: {
|
|
226
|
-
human: HUMAN_ALL_AUTO,
|
|
227
|
-
assistant: cautiousAi,
|
|
228
|
-
mcp: cautiousAi,
|
|
229
|
-
automation: {
|
|
230
|
-
read: "auto",
|
|
231
|
-
write: "auto",
|
|
232
|
-
"bulk-write": "review",
|
|
233
|
-
destructive: "review",
|
|
234
|
-
schema: "deny",
|
|
235
|
-
extension: "deny",
|
|
236
|
-
outbound: "review"
|
|
237
|
-
},
|
|
238
|
-
extension: extensionRow("review", "review")
|
|
239
|
-
},
|
|
240
|
-
balanced: {
|
|
241
|
-
human: HUMAN_ALL_AUTO,
|
|
242
|
-
assistant: balancedAi,
|
|
243
|
-
mcp: balancedMcp,
|
|
244
|
-
automation: {
|
|
245
|
-
read: "auto",
|
|
246
|
-
write: "auto",
|
|
247
|
-
"bulk-write": { autoUpTo: 100, above: "review" },
|
|
248
|
-
destructive: "review",
|
|
249
|
-
schema: "deny",
|
|
250
|
-
extension: "deny",
|
|
251
|
-
outbound: "review"
|
|
252
|
-
},
|
|
253
|
-
extension: extensionRow({ autoUpTo: 100, above: "review" }, "review")
|
|
254
|
-
},
|
|
255
|
-
autonomous: {
|
|
256
|
-
human: HUMAN_ALL_AUTO,
|
|
257
|
-
assistant: autonomousAi,
|
|
258
|
-
mcp: autonomousAi,
|
|
259
|
-
automation: { ...autonomousAi, schema: "deny", extension: "deny" },
|
|
260
|
-
extension: extensionRow("auto", { autoUpTo: 10, above: "review" })
|
|
261
|
-
}
|
|
262
|
-
};
|
|
263
|
-
var ruleSchema = external_exports.union([
|
|
264
|
-
external_exports.enum(GATE_DECISIONS),
|
|
265
|
-
external_exports.object({ autoUpTo: external_exports.number().int().min(0), above: external_exports.enum(["review", "deny"]) }).strict()
|
|
266
|
-
]);
|
|
267
|
-
var floorSchema = external_exports.partialRecord(
|
|
268
|
-
external_exports.enum([...ACTORS, "*"]),
|
|
269
|
-
external_exports.partialRecord(external_exports.enum([...RISK_CLASSES, "*"]), ruleSchema)
|
|
270
|
-
);
|
|
271
|
-
var STRICTNESS = { auto: 0, review: 1, deny: 2 };
|
|
272
|
-
function stricter(a, b) {
|
|
273
|
-
return STRICTNESS[a] >= STRICTNESS[b] ? a : b;
|
|
274
|
-
}
|
|
275
|
-
function applyRule(rule, bulkSize) {
|
|
276
|
-
if (typeof rule === "string") return rule;
|
|
277
|
-
return bulkSize <= rule.autoUpTo ? "auto" : rule.above;
|
|
278
|
-
}
|
|
279
|
-
function evaluateGate(request, policy, floor = {}) {
|
|
280
|
-
const bulkSize = Math.max(1, request.bulkSize ?? 1);
|
|
281
|
-
let policyDecision = applyRule(
|
|
282
|
-
PRESET_MATRICES[policy.preset][request.actor][request.risk],
|
|
283
|
-
bulkSize
|
|
284
|
-
);
|
|
285
|
-
if (request.risk === "outbound" && policyDecision === "review" && policy.preset === "balanced" && request.outbound?.autoApprovable && request.outbound.enabled) {
|
|
286
|
-
policyDecision = "auto";
|
|
287
|
-
}
|
|
288
|
-
const floorRules = [
|
|
289
|
-
floor[request.actor]?.[request.risk],
|
|
290
|
-
floor[request.actor]?.["*"],
|
|
291
|
-
floor["*"]?.[request.risk],
|
|
292
|
-
floor["*"]?.["*"]
|
|
293
|
-
].filter((rule) => rule !== void 0);
|
|
294
|
-
let decision = policyDecision;
|
|
295
|
-
let floorDecision;
|
|
296
|
-
for (const rule of floorRules) {
|
|
297
|
-
const fromFloor = applyRule(rule, bulkSize);
|
|
298
|
-
floorDecision = floorDecision ? stricter(floorDecision, fromFloor) : fromFloor;
|
|
299
|
-
decision = stricter(decision, fromFloor);
|
|
300
|
-
}
|
|
301
|
-
return {
|
|
302
|
-
decision,
|
|
303
|
-
policyDecision,
|
|
304
|
-
...floorDecision ? { floorDecision } : {},
|
|
305
|
-
preset: policy.preset
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
function evaluateOutsideGate(risk, floor = {}) {
|
|
309
|
-
if (risk !== "read" && risk !== "write") return "deny";
|
|
310
|
-
const rules = [floor["*"]?.[risk], floor["*"]?.["*"]].filter(
|
|
311
|
-
(rule) => rule !== void 0
|
|
312
|
-
);
|
|
313
|
-
return rules.some((rule) => applyRule(rule, 1) === "deny") ? "deny" : "auto";
|
|
314
|
-
}
|
|
315
|
-
function isLoosening(from, to) {
|
|
316
|
-
const a = PRESET_MATRICES[from];
|
|
317
|
-
const b = PRESET_MATRICES[to];
|
|
318
|
-
for (const actor of ACTORS) {
|
|
319
|
-
for (const risk of RISK_CLASSES) {
|
|
320
|
-
for (const size of [1, 10, 11, 25, 26, 100, 101, 1e4]) {
|
|
321
|
-
if (STRICTNESS[applyRule(b[actor][risk], size)] < STRICTNESS[applyRule(a[actor][risk], size)])
|
|
322
|
-
return true;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ../core/src/settings.ts
|
|
330
|
-
async function loadApprovalPolicy(db) {
|
|
331
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", APPROVAL_POLICY_SETTING).executeTakeFirst();
|
|
332
|
-
if (!row) return { preset: "cautious" };
|
|
333
|
-
try {
|
|
334
|
-
const parsed = approvalPolicySchema.safeParse(JSON.parse(row.value));
|
|
335
|
-
return parsed.success ? parsed.data : { preset: "cautious" };
|
|
336
|
-
} catch {
|
|
337
|
-
return { preset: "cautious" };
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
var BASE_CURRENCY_SETTING = "base_currency";
|
|
341
|
-
var ORG_NAME_SETTING = "org_name";
|
|
342
|
-
var ORG_LOGO_SETTING = "org_logo";
|
|
343
|
-
var ORG_TIMEZONE_SETTING = "org_timezone";
|
|
344
|
-
var ASSISTANT_NAME_SETTING = "assistant_name";
|
|
345
|
-
var DEFAULT_ASSISTANT_NAME = "Assistant";
|
|
346
|
-
function parseJson(value) {
|
|
347
|
-
if (value === void 0) return null;
|
|
348
|
-
try {
|
|
349
|
-
return JSON.parse(value);
|
|
350
|
-
} catch {
|
|
351
|
-
return null;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
function isTimezone(tz) {
|
|
355
|
-
try {
|
|
356
|
-
new Intl.DateTimeFormat("en", { timeZone: tz });
|
|
357
|
-
return true;
|
|
358
|
-
} catch {
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
async function loadGeneralSettings(db) {
|
|
363
|
-
const rows = await db.selectFrom("core_settings").select(["key", "value"]).where("key", "in", [
|
|
364
|
-
BASE_CURRENCY_SETTING,
|
|
365
|
-
ORG_NAME_SETTING,
|
|
366
|
-
ORG_LOGO_SETTING,
|
|
367
|
-
ORG_TIMEZONE_SETTING,
|
|
368
|
-
ASSISTANT_NAME_SETTING
|
|
369
|
-
]).execute();
|
|
370
|
-
const get = (key) => parseJson(rows.find((r) => r.key === key)?.value);
|
|
371
|
-
const currency = get(BASE_CURRENCY_SETTING);
|
|
372
|
-
const name = get(ORG_NAME_SETTING);
|
|
373
|
-
const logo = get(ORG_LOGO_SETTING);
|
|
374
|
-
const timezone = get(ORG_TIMEZONE_SETTING);
|
|
375
|
-
const assistantName = get(ASSISTANT_NAME_SETTING);
|
|
376
|
-
return {
|
|
377
|
-
baseCurrency: typeof currency === "string" && /^[A-Z]{3}$/.test(currency) ? currency : "USD",
|
|
378
|
-
orgName: typeof name === "string" && name.trim() ? name : null,
|
|
379
|
-
logo: typeof logo === "string" && logo.startsWith("data:image/") ? logo : null,
|
|
380
|
-
timezone: typeof timezone === "string" && isTimezone(timezone) ? timezone : "UTC",
|
|
381
|
-
assistantName: typeof assistantName === "string" && assistantName.trim() ? assistantName.trim() : DEFAULT_ASSISTANT_NAME
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
var AI_SETTINGS = "ai_settings";
|
|
385
|
-
async function loadAiSettings(db) {
|
|
386
|
-
const row = await db.selectFrom("core_settings").select("value").where("key", "=", AI_SETTINGS).executeTakeFirst();
|
|
387
|
-
if (!row) return { roles: {}, byok: [] };
|
|
388
|
-
try {
|
|
389
|
-
const value = JSON.parse(row.value);
|
|
390
|
-
return {
|
|
391
|
-
roles: parseRoleLayer(JSON.stringify(value.roles ?? {}), "Settings \u2192 AI (ai_settings)"),
|
|
392
|
-
byok: Array.isArray(value.byok) ? value.byok.filter((p) => PROVIDERS.includes(p)) : []
|
|
393
|
-
};
|
|
394
|
-
} catch {
|
|
395
|
-
return { roles: {}, byok: [] };
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// ../../node_modules/.pnpm/kysely@0.29.8/node_modules/kysely/dist/query-compiler/compiled-query.js
|
|
400
|
-
var CompiledQuery = freeze({
|
|
401
|
-
raw(sql2, parameters = []) {
|
|
402
|
-
return freeze({
|
|
403
|
-
sql: sql2,
|
|
404
|
-
query: RawNode.createWithSql(sql2),
|
|
405
|
-
parameters: freeze(parameters),
|
|
406
|
-
queryId: createQueryId()
|
|
407
|
-
});
|
|
408
|
-
}
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
// ../core/src/capacity/index.ts
|
|
412
|
-
function claimStatements(db, request, now) {
|
|
413
|
-
const quantity = request.quantity;
|
|
414
|
-
if (!Number.isInteger(quantity) || quantity < 1) {
|
|
415
|
-
throw new ValidationError("A claim is for a whole number of places, at least 1", {
|
|
416
|
-
rule: "capacity",
|
|
417
|
-
object: request.object,
|
|
418
|
-
record: request.record,
|
|
419
|
-
asked: quantity
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
const id = request.id ?? newId("capacityClaim");
|
|
423
|
-
const counter = () => db.selectFrom("core_capacity").where("object_type", "=", request.object).where("record_id", "=", request.record);
|
|
424
|
-
const statements = [
|
|
425
|
-
db.updateTable("core_capacity").set({ held: sql`held + ${quantity}`, updated_at: now }).where("object_type", "=", request.object).where("record_id", "=", request.record),
|
|
426
|
-
// The pool named on the record's counter, if any: no row matches when there is none.
|
|
427
|
-
db.updateTable("core_capacity").set({ held: sql`held + ${quantity}`, updated_at: now }).where("object_type", "=", counter().select("pool_object_type")).where("record_id", "=", counter().select("pool_record_id")),
|
|
428
|
-
db.insertInto("core_capacity_claim").values({
|
|
429
|
-
id,
|
|
430
|
-
object_type: request.object,
|
|
431
|
-
record_id: request.record,
|
|
432
|
-
pool_object_type: counter().select("pool_object_type"),
|
|
433
|
-
pool_record_id: counter().select("pool_record_id"),
|
|
434
|
-
quantity,
|
|
435
|
-
state: "held",
|
|
436
|
-
for_type: request.for.type,
|
|
437
|
-
for_id: request.for.id,
|
|
438
|
-
created_at: now,
|
|
439
|
-
updated_at: now
|
|
440
|
-
})
|
|
441
|
-
];
|
|
442
|
-
return { id, statements };
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// ../core/src/capacity/records.ts
|
|
446
|
-
async function readCounter(db, object, record) {
|
|
447
|
-
return db.selectFrom("core_capacity as c").leftJoin(
|
|
448
|
-
"core_capacity as p",
|
|
449
|
-
(join) => join.onRef("p.object_type", "=", "c.pool_object_type").onRef("p.record_id", "=", "c.pool_record_id")
|
|
450
|
-
).select([
|
|
451
|
-
"c.places",
|
|
452
|
-
"c.held",
|
|
453
|
-
"c.taken",
|
|
454
|
-
"c.pool_object_type",
|
|
455
|
-
"c.pool_record_id",
|
|
456
|
-
"p.places as pool_places",
|
|
457
|
-
"p.held as pool_held",
|
|
458
|
-
"p.taken as pool_taken"
|
|
459
|
-
]).where("c.object_type", "=", object).where("c.record_id", "=", record).executeTakeFirst();
|
|
460
|
-
}
|
|
461
|
-
function left(places, held, taken) {
|
|
462
|
-
return places === null ? null : Math.max(places - held - taken, 0);
|
|
463
|
-
}
|
|
464
|
-
var simulated = /* @__PURE__ */ new WeakMap();
|
|
465
|
-
async function queueClaim(ctx, request) {
|
|
466
|
-
const { id, statements } = claimStatements(ctx.db, request, ctx.now);
|
|
467
|
-
if (ctx.simulation) {
|
|
468
|
-
await simulateClaim(ctx, request);
|
|
469
|
-
return id;
|
|
470
|
-
}
|
|
471
|
-
ctx.write(...statements);
|
|
472
|
-
return id;
|
|
473
|
-
}
|
|
474
|
-
async function simulateClaim(ctx, request) {
|
|
475
|
-
const row = await readCounter(ctx.db, request.object, request.record);
|
|
476
|
-
if (!row) throw new ValidationError("A referenced record does not exist");
|
|
477
|
-
const seen = simulated.get(ctx) ?? /* @__PURE__ */ new Map();
|
|
478
|
-
simulated.set(ctx, seen);
|
|
479
|
-
const levels = [
|
|
480
|
-
{
|
|
481
|
-
object: request.object,
|
|
482
|
-
record: request.record,
|
|
483
|
-
left: left(row.places, row.held, row.taken)
|
|
484
|
-
},
|
|
485
|
-
...row.pool_object_type && row.pool_record_id ? [
|
|
486
|
-
{
|
|
487
|
-
object: row.pool_object_type,
|
|
488
|
-
record: row.pool_record_id,
|
|
489
|
-
left: left(row.pool_places, row.pool_held ?? 0, row.pool_taken ?? 0)
|
|
490
|
-
}
|
|
491
|
-
] : []
|
|
492
|
-
];
|
|
493
|
-
for (const level of levels) {
|
|
494
|
-
const key = `${level.object}|${level.record}`;
|
|
495
|
-
const already = seen.get(key) ?? 0;
|
|
496
|
-
const available = level.left === null ? null : Math.max(level.left - already, 0);
|
|
497
|
-
if (available !== null && request.quantity > available) {
|
|
498
|
-
throw new ValidationError(
|
|
499
|
-
`Not enough places: asked for ${request.quantity}, ${available} left`,
|
|
500
|
-
{
|
|
501
|
-
rule: "capacity",
|
|
502
|
-
object: level.object,
|
|
503
|
-
record: level.record,
|
|
504
|
-
asked: request.quantity,
|
|
505
|
-
available
|
|
506
|
-
}
|
|
507
|
-
);
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
for (const level of levels) {
|
|
511
|
-
const key = `${level.object}|${level.record}`;
|
|
512
|
-
seen.set(key, (seen.get(key) ?? 0) + request.quantity);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
function placesOf(object, capacity, value) {
|
|
516
|
-
if (value === null || value === void 0 || value === "") return null;
|
|
517
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) {
|
|
518
|
-
throw new ValidationError(
|
|
519
|
-
`${object.label}: places must be a whole number from 0, or empty for unlimited`,
|
|
520
|
-
{ field: capacity.places, rule: "capacity" }
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
return value;
|
|
524
|
-
}
|
|
525
|
-
async function counterWrite(ctx, object, id, after, changed) {
|
|
526
|
-
const capacity = object.capacity;
|
|
527
|
-
if (!capacity) return null;
|
|
528
|
-
const touches = changed === "create" || changed.includes(capacity.places) || capacity.pool !== void 0 && changed.includes(capacity.pool.field);
|
|
529
|
-
if (!touches) return null;
|
|
530
|
-
const places = placesOf(object, capacity, after[capacity.places]);
|
|
531
|
-
const poolId = capacity.pool ? after[capacity.pool.field] : null;
|
|
532
|
-
const pool = capacity.pool && typeof poolId === "string" && poolId ? { object: capacity.pool.object, record: poolId } : null;
|
|
533
|
-
if (changed !== "create" && ctx.simulation) {
|
|
534
|
-
const row = await readCounter(ctx.db, object.name, id);
|
|
535
|
-
if (row && places !== null && row.held + row.taken > places) {
|
|
536
|
-
throw new ValidationError(
|
|
537
|
-
`Places cannot be fewer than are claimed: ${row.held} held and ${row.taken} taken`,
|
|
538
|
-
{
|
|
539
|
-
rule: "capacity",
|
|
540
|
-
object: object.name,
|
|
541
|
-
record: id,
|
|
542
|
-
places,
|
|
543
|
-
held: row.held,
|
|
544
|
-
taken: row.taken
|
|
545
|
-
}
|
|
546
|
-
);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
return CompiledQuery.raw(
|
|
550
|
-
"INSERT INTO core_capacity (object_type, record_id, places, pool_object_type, pool_record_id, updated_at) VALUES (?, ?, ?, ?, ?, ?) ON CONFLICT (object_type, record_id) DO UPDATE SET places = excluded.places, pool_object_type = excluded.pool_object_type, pool_record_id = excluded.pool_record_id, updated_at = excluded.updated_at",
|
|
551
|
-
[object.name, id, places, pool?.object ?? null, pool?.record ?? null, ctx.now]
|
|
552
|
-
);
|
|
553
|
-
}
|
|
554
|
-
async function assertNoConfirmedClaims(ctx, object, id) {
|
|
555
|
-
if (!object.capacity) return;
|
|
556
|
-
const row = await ctx.db.selectFrom("core_capacity").select("taken").where("object_type", "=", object.name).where("record_id", "=", id).executeTakeFirst();
|
|
557
|
-
if (row && row.taken > 0) {
|
|
558
|
-
throw new ValidationError(
|
|
559
|
-
`${object.label} has ${row.taken} places taken by paid orders and cannot be deleted`,
|
|
560
|
-
{ rule: "capacity", object: object.name, record: id, taken: row.taken }
|
|
561
|
-
);
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
function dropCapacityStatements(object) {
|
|
565
|
-
return [
|
|
566
|
-
CompiledQuery.raw("DELETE FROM core_capacity_claim WHERE object_type = ?", [object]),
|
|
567
|
-
CompiledQuery.raw("DELETE FROM core_capacity WHERE object_type = ?", [object])
|
|
568
|
-
];
|
|
569
|
-
}
|
|
570
|
-
function backfillCounters(input) {
|
|
571
|
-
const places = `CASE WHEN typeof(t.${input.placesColumn}) IN ('integer', 'real') AND t.${input.placesColumn} >= 0 THEN CAST(t.${input.placesColumn} AS INTEGER) END`;
|
|
572
|
-
const poolRecord = input.pool ? `(SELECT p.record_id FROM core_capacity p WHERE p.object_type = ? AND p.record_id = t.${input.pool.column})` : "NULL";
|
|
573
|
-
const poolObject = input.pool ? `CASE WHEN ${poolRecord} IS NULL THEN NULL ELSE ? END` : "NULL";
|
|
574
|
-
const params = input.pool ? [input.object, input.pool.object, input.pool.object, input.pool.object, input.now] : [input.object, input.now];
|
|
575
|
-
return CompiledQuery.raw(
|
|
576
|
-
`INSERT INTO core_capacity (object_type, record_id, places, pool_object_type, pool_record_id, updated_at) SELECT ?, t.id, ${places}, ${poolObject}, ${poolRecord}, ? FROM ${input.table} t WHERE true ON CONFLICT (object_type, record_id) DO NOTHING`,
|
|
577
|
-
params
|
|
578
|
-
);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
// ../core/src/capacity/holds.ts
|
|
582
|
-
function targetWhere(target, alias) {
|
|
583
|
-
if ("claim" in target) return { sql: `${alias}.id = ?`, params: [target.claim] };
|
|
584
|
-
if ("forType" in target) return { sql: `${alias}.for_type = ?`, params: [target.forType] };
|
|
585
|
-
return {
|
|
586
|
-
sql: `${alias}.for_type = ? AND ${alias}.for_id = ?`,
|
|
587
|
-
params: [target.for.type, target.for.id]
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
function counterMoves(target, state, sets, now) {
|
|
591
|
-
const where = targetWhere(target, "c");
|
|
592
|
-
return ["record", "pool"].map((level) => {
|
|
593
|
-
const [object, record] = level === "record" ? ["object_type", "record_id"] : ["pool_object_type", "pool_record_id"];
|
|
594
|
-
const sum = `(SELECT coalesce(sum(c.quantity), 0) FROM core_capacity_claim c WHERE ${where.sql} AND c.state = '${state}' AND c.${object} = core_capacity.object_type AND c.${record} = core_capacity.record_id)`;
|
|
595
|
-
const assignments = [
|
|
596
|
-
`${sets.minus} = ${sets.minus} - ${sum}`,
|
|
597
|
-
...sets.plus ? [`${sets.plus} = ${sets.plus} + ${sum}`] : [],
|
|
598
|
-
"updated_at = ?"
|
|
599
|
-
];
|
|
600
|
-
const params = [...where.params, ...sets.plus ? where.params : [], now, ...where.params];
|
|
601
|
-
return CompiledQuery.raw(
|
|
602
|
-
`UPDATE core_capacity SET ${assignments.join(", ")} WHERE (object_type, record_id) IN (SELECT c.${object}, c.${record} FROM core_capacity_claim c WHERE ${where.sql} AND c.state = '${state}' AND c.${object} IS NOT NULL)`,
|
|
603
|
-
params
|
|
604
|
-
);
|
|
605
|
-
});
|
|
606
|
-
}
|
|
607
|
-
function claimMove(target, from, to, now) {
|
|
608
|
-
const where = targetWhere(target, "core_capacity_claim");
|
|
609
|
-
const stamp = to === "confirmed" ? "confirmed_at" : "released_at";
|
|
610
|
-
return CompiledQuery.raw(
|
|
611
|
-
`UPDATE core_capacity_claim SET state = '${to}', ${stamp} = ?, updated_at = ? WHERE ${where.sql} AND state = '${from}'`,
|
|
612
|
-
[now, now, ...where.params]
|
|
613
|
-
);
|
|
614
|
-
}
|
|
615
|
-
function confirmStatements(target, now) {
|
|
616
|
-
return [
|
|
617
|
-
...counterMoves(target, "held", { minus: "held", plus: "taken" }, now),
|
|
618
|
-
claimMove(target, "held", "confirmed", now)
|
|
619
|
-
];
|
|
620
|
-
}
|
|
621
|
-
function releaseStatements(target, now, options = {}) {
|
|
622
|
-
const from = options.confirmed ? "confirmed" : "held";
|
|
623
|
-
return [
|
|
624
|
-
...counterMoves(target, from, { minus: from === "held" ? "held" : "taken" }, now),
|
|
625
|
-
claimMove(target, from, "released", now)
|
|
626
|
-
];
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
// ../core/src/capacity/claimants.ts
|
|
630
|
-
function kindOf(claims, state) {
|
|
631
|
-
if (typeof state !== "string") return null;
|
|
632
|
-
if (claims.held.includes(state)) return "held";
|
|
633
|
-
if (claims.confirmed.includes(state)) return "confirmed";
|
|
634
|
-
return null;
|
|
635
|
-
}
|
|
636
|
-
function claimKind(object, values) {
|
|
637
|
-
if (!object.claims || !object.states || !values) return null;
|
|
638
|
-
return kindOf(object.claims, values[object.states.field]);
|
|
639
|
-
}
|
|
640
|
-
async function claimantMove(ctx, object, id, before, after) {
|
|
641
|
-
const claims = object.claims;
|
|
642
|
-
const field = object.states?.field;
|
|
643
|
-
if (!claims || !field) return;
|
|
644
|
-
const from = before === null ? null : kindOf(claims, before[field]);
|
|
645
|
-
const to = kindOf(claims, after[field]);
|
|
646
|
-
if (from === to) return;
|
|
647
|
-
const target = { for: { type: object.name, id } };
|
|
648
|
-
if (from === "held") {
|
|
649
|
-
ctx.write(
|
|
650
|
-
...to === "confirmed" ? confirmStatements(target, ctx.now) : releaseStatements(target, ctx.now)
|
|
651
|
-
);
|
|
652
|
-
return;
|
|
653
|
-
}
|
|
654
|
-
if (from === "confirmed") {
|
|
655
|
-
ctx.write(...releaseStatements(target, ctx.now, { confirmed: true }));
|
|
656
|
-
if (to === null) return;
|
|
657
|
-
}
|
|
658
|
-
const on = after[claims.on];
|
|
659
|
-
if (typeof on !== "string" || on === "") {
|
|
660
|
-
const label = object.fields.find((f) => f.name === claims.on)?.label ?? claims.on;
|
|
661
|
-
throw new ValidationError(`${object.label} needs ${label} to hold a place`, {
|
|
662
|
-
field: claims.on,
|
|
663
|
-
rule: "capacity"
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
const claim = await queueClaim(ctx, {
|
|
667
|
-
object: claims.object,
|
|
668
|
-
record: on,
|
|
669
|
-
quantity: 1,
|
|
670
|
-
for: { type: object.name, id }
|
|
671
|
-
});
|
|
672
|
-
if (to === "confirmed") ctx.write(...confirmStatements({ claim }, ctx.now));
|
|
673
|
-
}
|
|
674
|
-
function assertClaimRelationKept(object, before, written) {
|
|
675
|
-
const claims = object.claims;
|
|
676
|
-
if (!claims || before === null || !Object.hasOwn(written, claims.on)) return;
|
|
677
|
-
if ((written[claims.on] ?? null) === (before[claims.on] ?? null)) return;
|
|
678
|
-
if (claimKind(object, before) === null) return;
|
|
679
|
-
const label = object.fields.find((f) => f.name === claims.on)?.label ?? claims.on;
|
|
680
|
-
throw new ValidationError(
|
|
681
|
-
`${object.label} holds a place on its ${label.toLowerCase()}, so its ${label.toLowerCase()} cannot change`,
|
|
682
|
-
{ field: claims.on, rule: "claim_relation_kept" }
|
|
683
|
-
);
|
|
684
|
-
}
|
|
685
|
-
async function assertNoLiveClaim(ctx, object, id) {
|
|
686
|
-
if (!object.claims) return;
|
|
687
|
-
const row = await ctx.db.selectFrom("core_capacity_claim").select("state").where("for_type", "=", object.name).where("for_id", "=", id).where("state", "in", ["held", "confirmed"]).executeTakeFirst();
|
|
688
|
-
if (row) {
|
|
689
|
-
throw new ConflictError(
|
|
690
|
-
`${object.label} holds a place; move it to a state that gives the place back before deleting it`,
|
|
691
|
-
{ rule: "capacity", reason: "claim_live", object: object.name, record: id }
|
|
692
|
-
);
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
function releaseAllClaimsStatements(object, now) {
|
|
696
|
-
const target = { forType: object };
|
|
697
|
-
return [
|
|
698
|
-
...releaseStatements(target, now),
|
|
699
|
-
...releaseStatements(target, now, { confirmed: true })
|
|
700
|
-
];
|
|
701
|
-
}
|
|
702
|
-
function releaseAllClaimsOfStatements(object, id, now) {
|
|
703
|
-
const target = { for: { type: object, id } };
|
|
704
|
-
return [
|
|
705
|
-
...releaseStatements(target, now),
|
|
706
|
-
...releaseStatements(target, now, { confirmed: true })
|
|
707
|
-
];
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
export {
|
|
711
|
-
CompiledQuery,
|
|
712
|
-
ID_PREFIXES,
|
|
713
|
-
newId,
|
|
714
|
-
toProvisionalId,
|
|
715
|
-
isProvisionalId,
|
|
716
|
-
findProvisionalIds,
|
|
717
|
-
findIds,
|
|
718
|
-
replaceIds,
|
|
719
|
-
POLICY_PRESETS,
|
|
720
|
-
approvalPolicySchema,
|
|
721
|
-
APPROVAL_POLICY_SETTING,
|
|
722
|
-
PRESET_MATRICES,
|
|
723
|
-
evaluateGate,
|
|
724
|
-
evaluateOutsideGate,
|
|
725
|
-
isLoosening,
|
|
726
|
-
loadApprovalPolicy,
|
|
727
|
-
BASE_CURRENCY_SETTING,
|
|
728
|
-
ORG_NAME_SETTING,
|
|
729
|
-
ORG_LOGO_SETTING,
|
|
730
|
-
ORG_TIMEZONE_SETTING,
|
|
731
|
-
ASSISTANT_NAME_SETTING,
|
|
732
|
-
isTimezone,
|
|
733
|
-
loadGeneralSettings,
|
|
734
|
-
AI_SETTINGS,
|
|
735
|
-
loadAiSettings,
|
|
736
|
-
confirmStatements,
|
|
737
|
-
releaseStatements,
|
|
738
|
-
queueClaim,
|
|
739
|
-
counterWrite,
|
|
740
|
-
assertNoConfirmedClaims,
|
|
741
|
-
dropCapacityStatements,
|
|
742
|
-
backfillCounters,
|
|
743
|
-
claimantMove,
|
|
744
|
-
assertClaimRelationKept,
|
|
745
|
-
assertNoLiveClaim,
|
|
746
|
-
releaseAllClaimsStatements,
|
|
747
|
-
releaseAllClaimsOfStatements
|
|
748
|
-
};
|