@composable-os/cli 0.0.0-stage → 0.1.0-beta.8
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/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/blocks-DEELKGDC.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2S7375SC.js +802 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-E5JLTLRM.js +50 -0
- package/dist/chunk-G3ORKXVP.js +1178 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-KOKYDJ7Z.js +1423 -0
- package/dist/chunk-KTXGEXTY.js +358 -0
- package/dist/chunk-LTQDLCP7.js +16486 -0
- package/dist/chunk-MAXLUQKB.js +1287 -0
- package/dist/chunk-STJZJIRA.js +11378 -0
- package/dist/chunk-X6I2LWB3.js +24319 -0
- package/dist/chunk-XDDBLYKW.js +29154 -0
- package/dist/chunk-ZRWF6QZS.js +2357 -0
- package/dist/cli-BRSACHSG.js +31393 -0
- package/dist/cli-DTMGNDXN.js +121 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-ZVZ6KW5S.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-64BQJA62.js +48 -0
- package/dist/invoke-NHHOLLF6.js +26 -0
- package/dist/person-WMWRF2HV.js +58 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +112 -0
- package/dist/platform/sdk/contract.ts +766 -0
- package/dist/platform/sdk/document.ts +788 -0
- package/dist/platform/sdk/field-types.ts +106 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +33 -0
- package/dist/platform/sdk/manifest.ts +1484 -0
- package/dist/platform/sdk/media.ts +46 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +173 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +396 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +162 -0
- package/dist/platform/sdk/testing.ts +836 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-4JNFLYXV.js +847 -0
- package/dist/src-G6OF5SKW.js +31 -0
- package/dist/src-J27WXNLY.js +270 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/package.json +33 -4
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
APPROVAL_INTENT_CALIBRATED,
|
|
4
|
+
CALIBRATED_QUESTIONS,
|
|
5
|
+
GRANT_CALIBRATED,
|
|
6
|
+
INTENT_THRESHOLDS,
|
|
7
|
+
MEMORY_CONDITION_CALIBRATED,
|
|
8
|
+
MEMORY_WRITE_CALIBRATED,
|
|
9
|
+
OUTBOUND_CALIBRATED,
|
|
10
|
+
POLICY_CALIBRATED,
|
|
11
|
+
SHAPE_CALIBRATED,
|
|
12
|
+
STATEMENTS_CALIBRATED,
|
|
13
|
+
coreRegistry,
|
|
14
|
+
toolOperation
|
|
15
|
+
} from "./chunk-XDDBLYKW.js";
|
|
16
|
+
import {
|
|
17
|
+
AUDIENCES,
|
|
18
|
+
JOB_CHANGE_CALIBRATED,
|
|
19
|
+
ROUTINE_GATE_CALIBRATED,
|
|
20
|
+
audiencesOf,
|
|
21
|
+
choice,
|
|
22
|
+
forPacks
|
|
23
|
+
} from "./chunk-LTQDLCP7.js";
|
|
24
|
+
import {
|
|
25
|
+
external_exports,
|
|
26
|
+
toManifest
|
|
27
|
+
} from "./chunk-X6I2LWB3.js";
|
|
28
|
+
|
|
29
|
+
// ../core/src/judge/questions/orchestrator-suggest.ts
|
|
30
|
+
var ORCHESTRATOR_PLACEMENT = "orchestrator.suggest";
|
|
31
|
+
var ORCHESTRATOR_QUESTION = choice(
|
|
32
|
+
"`said` is what a person asked their CRM assistant. `selected`, when there is one, is how many records they have selected. How much work is the request?",
|
|
33
|
+
{
|
|
34
|
+
single_lookup: "One lookup, count, total or list that one query answers, one small change to one record, or a plain answer",
|
|
35
|
+
short_work: "A couple of tool calls: a few records at most, done in a few seconds",
|
|
36
|
+
long_work: "Work on many records one by one, research, or a bulk clean-up: anything that would take more than a few seconds or a couple of tool calls",
|
|
37
|
+
unclear: "`said` does not ask for any work, or does not say enough to tell"
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
var ORCHESTRATOR_THRESHOLD = 0.4;
|
|
41
|
+
var ORCHESTRATOR_CALIBRATED = {
|
|
42
|
+
[`${ORCHESTRATOR_PLACEMENT}/long_work`]: {
|
|
43
|
+
question: ORCHESTRATOR_QUESTION,
|
|
44
|
+
threshold: ORCHESTRATOR_THRESHOLD
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// ../core/src/http/contract.ts
|
|
49
|
+
var API_VERSION = "v1";
|
|
50
|
+
var SERVED_API_VERSIONS = Object.freeze([API_VERSION]);
|
|
51
|
+
function jsonSchema(schema, io) {
|
|
52
|
+
try {
|
|
53
|
+
const out = external_exports.toJSONSchema(schema, { unrepresentable: "any", io });
|
|
54
|
+
const { $schema: _drop, ...rest } = out;
|
|
55
|
+
return rest;
|
|
56
|
+
} catch {
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function staleGroup(name) {
|
|
61
|
+
const [first, second] = name.split(".");
|
|
62
|
+
if (first === "me" || first === "my") return "self";
|
|
63
|
+
if (first === "x") return `x.${second}`;
|
|
64
|
+
return first ?? name;
|
|
65
|
+
}
|
|
66
|
+
function buildContract(operations) {
|
|
67
|
+
const callable = operations.filter((op) => !op.toolOnly);
|
|
68
|
+
const queries = callable.filter((op) => op.risk === "read");
|
|
69
|
+
const entries = callable.map((op) => {
|
|
70
|
+
const kind = op.risk === "read" ? "query" : "mutation";
|
|
71
|
+
const group = staleGroup(op.name);
|
|
72
|
+
const reaches = (q) => staleGroup(q.name) === group || group.startsWith("x.") && staleGroup(q.name) === "self";
|
|
73
|
+
return {
|
|
74
|
+
name: op.name,
|
|
75
|
+
description: op.description,
|
|
76
|
+
risk: op.risk,
|
|
77
|
+
audiences: AUDIENCES.filter((a) => audiencesOf(op).includes(a)),
|
|
78
|
+
kind,
|
|
79
|
+
input: jsonSchema(op.input, "input"),
|
|
80
|
+
output: jsonSchema(op.output, "output"),
|
|
81
|
+
invalidates: kind === "mutation" ? queries.filter(reaches).map((q) => q.name).sort() : []
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
version: API_VERSION,
|
|
86
|
+
operations: entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ../core/src/http/handoff.ts
|
|
91
|
+
var SIGN_IN_CONFIRM_TTL_MS = 5 * 60 * 1e3;
|
|
92
|
+
|
|
93
|
+
// ../core/src/http/idempotency.ts
|
|
94
|
+
var IDEMPOTENCY_TTL_MS = 24 * 3600 * 1e3;
|
|
95
|
+
|
|
96
|
+
// ../core/src/judge/calibrated.ts
|
|
97
|
+
var CALIBRATED = {
|
|
98
|
+
...Object.fromEntries(
|
|
99
|
+
Object.entries(CALIBRATED_QUESTIONS).map(([key2, question]) => [
|
|
100
|
+
key2,
|
|
101
|
+
{ question, threshold: INTENT_THRESHOLDS[key2.split("/")[1]] }
|
|
102
|
+
])
|
|
103
|
+
),
|
|
104
|
+
...POLICY_CALIBRATED,
|
|
105
|
+
...STATEMENTS_CALIBRATED,
|
|
106
|
+
...OUTBOUND_CALIBRATED,
|
|
107
|
+
...SHAPE_CALIBRATED,
|
|
108
|
+
...JOB_CHANGE_CALIBRATED,
|
|
109
|
+
...MEMORY_WRITE_CALIBRATED,
|
|
110
|
+
...MEMORY_CONDITION_CALIBRATED,
|
|
111
|
+
...APPROVAL_INTENT_CALIBRATED,
|
|
112
|
+
...GRANT_CALIBRATED,
|
|
113
|
+
...ORCHESTRATOR_CALIBRATED,
|
|
114
|
+
...ROUTINE_GATE_CALIBRATED
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// ../core/src/judge/dev.ts
|
|
118
|
+
var ABOUT_SOMEONE = new RegExp("^(?!I\\b)\\p{Lu}\\p{L}+ (is|are|has|was|will|wants|prefers)\\b", "u");
|
|
119
|
+
var NOT_NAMING = new Set(
|
|
120
|
+
"a an and the to of for on in at my our it do go send yes ok approve this that".split(" ")
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// ../core/src/ai/fake.ts
|
|
124
|
+
var STOP = new Set(
|
|
125
|
+
"the a an and or of to in on for with at by is are was be it this that from as who what about we our their".split(
|
|
126
|
+
" "
|
|
127
|
+
)
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// ../client-gen/src/product.ts
|
|
131
|
+
function releaseContract(perAudience, packs, extensions) {
|
|
132
|
+
const merged = /* @__PURE__ */ new Map();
|
|
133
|
+
const own = new Set(extensions.map((e) => toManifest(e).id));
|
|
134
|
+
for (const contract of perAudience.map((c) => forPacks(c, packs))) {
|
|
135
|
+
for (const op of contract.operations) {
|
|
136
|
+
const seen = merged.get(op.name);
|
|
137
|
+
merged.set(
|
|
138
|
+
op.name,
|
|
139
|
+
seen ? {
|
|
140
|
+
...seen,
|
|
141
|
+
audiences: [.../* @__PURE__ */ new Set([...seen.audiences, ...op.audiences])].sort(),
|
|
142
|
+
invalidates: [.../* @__PURE__ */ new Set([...seen.invalidates, ...op.invalidates])].sort()
|
|
143
|
+
} : { ...op }
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
for (const name of [...merged.keys()]) {
|
|
148
|
+
const [first, id] = name.split(".");
|
|
149
|
+
if (first === "x" && id && own.has(id)) merged.delete(name);
|
|
150
|
+
}
|
|
151
|
+
const tools = extensions.flatMap((extension) => {
|
|
152
|
+
const manifest = toManifest(extension);
|
|
153
|
+
return Object.entries(manifest.tools).map(
|
|
154
|
+
([name, tool]) => toolOperation(manifest, name, tool, "shipped")
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
for (const op of buildContract(tools).operations) merged.set(op.name, op);
|
|
158
|
+
const version = perAudience[0]?.version ?? "v1";
|
|
159
|
+
return {
|
|
160
|
+
version,
|
|
161
|
+
operations: [...merged.values()].sort((a, b) => a.name.localeCompare(b.name))
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function productContract(extensions) {
|
|
165
|
+
const tools = extensions.flatMap((extension) => {
|
|
166
|
+
const manifest = toManifest(extension);
|
|
167
|
+
return Object.entries(manifest.tools).map(
|
|
168
|
+
([name, tool]) => toolOperation(manifest, name, tool, "shipped")
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
return buildContract([...coreRegistry.list(), ...tools]);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// ../client-gen/src/schema-ts.ts
|
|
175
|
+
var IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
176
|
+
function key(name) {
|
|
177
|
+
return IDENT.test(name) ? name : JSON.stringify(name);
|
|
178
|
+
}
|
|
179
|
+
function literal(value) {
|
|
180
|
+
return value === null || ["string", "number", "boolean"].includes(typeof value) ? JSON.stringify(value) : "unknown";
|
|
181
|
+
}
|
|
182
|
+
function union(parts) {
|
|
183
|
+
const unique = [...new Set(parts)];
|
|
184
|
+
if (unique.includes("unknown")) return "unknown";
|
|
185
|
+
return unique.length === 1 ? unique[0] : unique.map((p) => `(${p})`).join(" | ");
|
|
186
|
+
}
|
|
187
|
+
var MAX_DEPTH = 12;
|
|
188
|
+
function schemaToTs(schema, ctx = { defs: {}, depth: 0 }) {
|
|
189
|
+
if (schema === true || schema === void 0) return "unknown";
|
|
190
|
+
if (schema === false) return "never";
|
|
191
|
+
if (!schema || typeof schema !== "object") return "unknown";
|
|
192
|
+
if (ctx.depth > MAX_DEPTH) return "unknown";
|
|
193
|
+
const s = schema;
|
|
194
|
+
const next = { ...ctx, depth: ctx.depth + 1 };
|
|
195
|
+
const defs = s.$defs ?? void 0;
|
|
196
|
+
if (defs) next.defs = { ...ctx.defs, ...defs };
|
|
197
|
+
if (typeof s.$ref === "string") {
|
|
198
|
+
const match = /^#\/\$defs\/(.+)$/.exec(s.$ref);
|
|
199
|
+
const target = match ? next.defs[match[1]] : void 0;
|
|
200
|
+
return target ? schemaToTs(target, next) : "unknown";
|
|
201
|
+
}
|
|
202
|
+
if ("const" in s) return literal(s.const);
|
|
203
|
+
if (Array.isArray(s.enum)) return union(s.enum.map(literal));
|
|
204
|
+
if (Array.isArray(s.anyOf)) return union(s.anyOf.map((x) => schemaToTs(x, next)));
|
|
205
|
+
if (Array.isArray(s.oneOf)) return union(s.oneOf.map((x) => schemaToTs(x, next)));
|
|
206
|
+
if (Array.isArray(s.allOf)) {
|
|
207
|
+
const parts = s.allOf.map((x) => schemaToTs(x, next));
|
|
208
|
+
if (parts.includes("unknown"))
|
|
209
|
+
return parts.filter((p) => p !== "unknown").join(" & ") || "unknown";
|
|
210
|
+
return parts.map((p) => `(${p})`).join(" & ");
|
|
211
|
+
}
|
|
212
|
+
const type = s.type;
|
|
213
|
+
if (Array.isArray(type)) {
|
|
214
|
+
return union(type.map((t) => schemaToTs({ ...s, type: t }, ctx)));
|
|
215
|
+
}
|
|
216
|
+
switch (type) {
|
|
217
|
+
case "string":
|
|
218
|
+
return "string";
|
|
219
|
+
case "number":
|
|
220
|
+
case "integer":
|
|
221
|
+
return "number";
|
|
222
|
+
case "boolean":
|
|
223
|
+
return "boolean";
|
|
224
|
+
case "null":
|
|
225
|
+
return "null";
|
|
226
|
+
case "array": {
|
|
227
|
+
if (Array.isArray(s.prefixItems)) {
|
|
228
|
+
return `[${s.prefixItems.map((x) => schemaToTs(x, next)).join(", ")}]`;
|
|
229
|
+
}
|
|
230
|
+
const item = schemaToTs(s.items, next);
|
|
231
|
+
return `${/[|&]/.test(item) ? `(${item})` : item}[]`;
|
|
232
|
+
}
|
|
233
|
+
case "object":
|
|
234
|
+
return objectType(s, next);
|
|
235
|
+
default:
|
|
236
|
+
if (s.properties) return objectType(s, next);
|
|
237
|
+
return "unknown";
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function objectType(s, ctx) {
|
|
241
|
+
const properties = s.properties ?? {};
|
|
242
|
+
const required = new Set(s.required ?? []);
|
|
243
|
+
const fields = Object.entries(properties).map(([name, value]) => {
|
|
244
|
+
const optional = required.has(name) ? "" : "?";
|
|
245
|
+
return `${key(name)}${optional}: ${schemaToTs(value, ctx)}`;
|
|
246
|
+
});
|
|
247
|
+
const extra = s.additionalProperties;
|
|
248
|
+
if (Object.keys(properties).length === 0) {
|
|
249
|
+
if (extra === false) return "Record<string, never>";
|
|
250
|
+
return `Record<string, ${extra && typeof extra === "object" ? schemaToTs(extra, ctx) : "unknown"}>`;
|
|
251
|
+
}
|
|
252
|
+
if (extra && typeof extra === "object") {
|
|
253
|
+
return `{ ${fields.join("; ")} } & { [key: string]: unknown }`;
|
|
254
|
+
}
|
|
255
|
+
return `{ ${fields.join("; ")} }`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// ../client-gen/src/render.ts
|
|
259
|
+
var ENTRY_POINTS = ["customer", "staff"];
|
|
260
|
+
function pascal(name) {
|
|
261
|
+
return name.split(/[^A-Za-z0-9]+/).filter(Boolean).map((part) => part[0]?.toUpperCase() + part.slice(1)).join("");
|
|
262
|
+
}
|
|
263
|
+
function doc(op) {
|
|
264
|
+
const text = op.description.replace(/\*\//g, "* /").split("\n").join("\n * ");
|
|
265
|
+
return ` /**
|
|
266
|
+
* ${text}
|
|
267
|
+
*/`;
|
|
268
|
+
}
|
|
269
|
+
function operationsFor(contract, entry) {
|
|
270
|
+
const audiences = entry === "customer" ? ["customer", "public"] : ["staff"];
|
|
271
|
+
return contract.operations.filter((op) => op.audiences.some((a) => audiences.includes(a)));
|
|
272
|
+
}
|
|
273
|
+
function renderEntry(contract, entry, options) {
|
|
274
|
+
const ops = operationsFor(contract, entry);
|
|
275
|
+
const names = new Set(ops.map((op) => op.name));
|
|
276
|
+
const runtime = options.runtime ?? "@composable-os/client";
|
|
277
|
+
const factory = `create${pascal(entry)}Client`;
|
|
278
|
+
const lines = [
|
|
279
|
+
`// Generated by \`cos client generate\` for the ${options.product} product, API ${contract.version}: the`,
|
|
280
|
+
`// ${entry === "customer" ? "operations open to customers and the public" : "operations open to staff"}. Do not edit; generate again.`,
|
|
281
|
+
`import { type ClientOptions, createClient } from ${JSON.stringify(runtime)};`,
|
|
282
|
+
"",
|
|
283
|
+
"/** Every operation's input, output and kind. */",
|
|
284
|
+
// A type, not an interface: it must fit the runtime's Record constraint.
|
|
285
|
+
"export type Operations = {"
|
|
286
|
+
];
|
|
287
|
+
for (const op of ops) {
|
|
288
|
+
lines.push(doc(op));
|
|
289
|
+
lines.push(` ${JSON.stringify(op.name)}: {`);
|
|
290
|
+
lines.push(` kind: ${JSON.stringify(op.kind)};`);
|
|
291
|
+
lines.push(` input: ${schemaToTs(op.input)};`);
|
|
292
|
+
lines.push(` output: ${schemaToTs(op.output)};`);
|
|
293
|
+
lines.push(" };");
|
|
294
|
+
}
|
|
295
|
+
lines.push("};", "");
|
|
296
|
+
lines.push(
|
|
297
|
+
"/** What the runtime needs: each operation's kind, and what a mutation makes stale. */"
|
|
298
|
+
);
|
|
299
|
+
lines.push("export const operations = {");
|
|
300
|
+
for (const op of ops) {
|
|
301
|
+
const stale = op.invalidates.filter((n) => names.has(n));
|
|
302
|
+
lines.push(
|
|
303
|
+
` ${JSON.stringify(op.name)}: { kind: ${JSON.stringify(op.kind)}, invalidates: ${JSON.stringify(stale)} },`
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
lines.push("} as const;", "");
|
|
307
|
+
lines.push(`export const API_VERSION = ${JSON.stringify(contract.version)};`, "");
|
|
308
|
+
lines.push(`export function ${factory}(options: ClientOptions) {`);
|
|
309
|
+
lines.push(" return createClient<Operations>(operations, options);");
|
|
310
|
+
lines.push("}", "");
|
|
311
|
+
lines.push(`export type ${pascal(entry)}Client = ReturnType<typeof ${factory}>;`);
|
|
312
|
+
lines.push(`export type Input<N extends keyof Operations> = Operations[N]["input"];`);
|
|
313
|
+
lines.push(`export type Output<N extends keyof Operations> = Operations[N]["output"];`);
|
|
314
|
+
return `${lines.join("\n")}
|
|
315
|
+
`;
|
|
316
|
+
}
|
|
317
|
+
function renderRoleClient(staff, role, options) {
|
|
318
|
+
const allowed = new Set(role.operations);
|
|
319
|
+
const missing = role.operations.filter((n) => !staff.operations.some((op) => op.name === n));
|
|
320
|
+
if (missing.length) {
|
|
321
|
+
throw new Error(`The role ${role.name} names ${missing.join(", ")}, not in the staff contract`);
|
|
322
|
+
}
|
|
323
|
+
const operations = staff.operations.filter((op) => allowed.has(op.name)).map((op) => {
|
|
324
|
+
if (op.name !== "record.get" && op.name !== "record.list") return op;
|
|
325
|
+
const props = op.input.properties ?? {};
|
|
326
|
+
return {
|
|
327
|
+
...op,
|
|
328
|
+
input: {
|
|
329
|
+
...op.input,
|
|
330
|
+
properties: { ...props, object: { type: "string", enum: [...role.read].sort() } }
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
const text = renderEntry({ version: staff.version, operations }, "staff", options);
|
|
335
|
+
const factory = `create${pascal(role.name)}Client`;
|
|
336
|
+
return text.replace(
|
|
337
|
+
/^\/\/ Generated by .*\n\/\/ .*\n/,
|
|
338
|
+
`// Generated by \`cos client generate\` for the ${options.product} product, API ${staff.version}: the
|
|
339
|
+
// operations the ${role.name} role may call, and no other staff operation. Do not edit; generate again.
|
|
340
|
+
`
|
|
341
|
+
).replaceAll("createStaffClient", factory).replaceAll("StaffClient", `${pascal(role.name)}Client`);
|
|
342
|
+
}
|
|
343
|
+
function renderClient(contract, options) {
|
|
344
|
+
return Object.fromEntries(
|
|
345
|
+
ENTRY_POINTS.map((entry) => [`${entry}.ts`, renderEntry(contract, entry, options)])
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export {
|
|
350
|
+
releaseContract,
|
|
351
|
+
productContract,
|
|
352
|
+
schemaToTs,
|
|
353
|
+
ENTRY_POINTS,
|
|
354
|
+
operationsFor,
|
|
355
|
+
renderEntry,
|
|
356
|
+
renderRoleClient,
|
|
357
|
+
renderClient
|
|
358
|
+
};
|