-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmanifest.zod.ts
More file actions
565 lines (515 loc) · 22.4 KB
/
Copy pathmanifest.zod.ts
File metadata and controls
565 lines (515 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { PluginCapabilityManifestSchema } from './plugin-capability.zod';
import { PluginLoadingConfigSchema } from './plugin-loading.zod';
import { CORE_PLUGIN_TYPES } from './plugin.zod';
import { SeedSchema } from '../data/seed.zod';
import { NavigationContributionSchema } from '../ui/app.zod';
// ─────────────────────────────────────────────────────────────────────
// Plugin distribution (ADR-0025 §3.2) — authoritative shapes.
//
// These are the canonical schemas for a signed, permissioned plugin
// package. The cloud control plane mirrors them when it validates a
// published `.osplugin` manifest and persists the declared metadata onto
// `sys_package_version`; cloud swaps its local stopgap for these imports
// (see cloud docs/design/plugin-distribution-framework-tasks.md F1).
// ─────────────────────────────────────────────────────────────────────
/**
* Structured permission grants requested by a plugin (ADR-0025 §3.2).
* Each list scopes one capability surface the plugin may touch. The
* install-time consent flow (ADR §3.5 step 2) turns this declaration into
* the persisted `granted_permissions` set enforced at load by the
* PluginPermissionEnforcer.
*
* @example
* ```jsonc
* { "services": ["object", "http"], "hooks": ["record.beforeInsert"],
* "network": ["api.acme.com"], "fs": [] }
* ```
*/
export const PluginPermissionsSchema = z
.object({
services: z.array(z.string()).optional()
.describe('Platform services the plugin may resolve (e.g. "object", "http")'),
hooks: z.array(z.string()).optional()
.describe('Lifecycle hooks the plugin may register (e.g. "record.beforeInsert")'),
network: z.array(z.string()).optional()
.describe('Network hosts the plugin may reach (e.g. "api.acme.com")'),
fs: z.array(z.string()).optional()
.describe('Filesystem paths the plugin may access'),
})
.strict()
.describe('Structured plugin permission grants (ADR-0025 §3.2)');
export type PluginPermissions = z.infer<typeof PluginPermissionsSchema>;
/**
* Backward-compatible manifest `permissions` value: either the legacy flat
* list of permission strings (apps / older packages) or the structured
* plugin permission block above. New code should prefer the structured form.
*/
export const ManifestPermissionsSchema = z.union([
z.array(z.string()),
PluginPermissionsSchema,
]);
export type ManifestPermissions = z.infer<typeof ManifestPermissionsSchema>;
/**
* Compatibility ranges for a plugin (ADR-0025 §3.2, §3.10 #3).
* `protocol` (the metadata/runtime contract version) is checked first and
* takes precedence over `platform` (the engine release range), so a plugin
* keeps working across platform releases that preserve the protocol.
*/
export const PluginEnginesSchema = z
.object({
platform: z.string().optional()
.describe('ObjectStack platform release range (SemVer, e.g. ">=4.0 <5")'),
protocol: z.string().optional()
.describe('Runtime/metadata protocol range, checked first (ADR §3.10 #3)'),
})
.describe('Plugin compatibility ranges (ADR-0025 §3.2)');
export type PluginEngines = z.infer<typeof PluginEnginesSchema>;
/**
* Trust / isolation tier the plugin runs under (ADR-0025 §3.6):
* - `node` — in-process, full PluginContext (first-party / verified only)
* - `sandbox` — QuickJS-WASM, capability-gated surface
* - `worker` — out-of-process (reserved)
*/
export const PluginRuntimeSchema = z
.enum(['node', 'sandbox', 'worker'])
.describe('Plugin trust tier (ADR-0025 §3.6)');
export type PluginRuntime = z.infer<typeof PluginRuntimeSchema>;
/**
* Dependency packaging strategy (ADR-0025 §3.3):
* - `bundled` — deps pre-bundled into the artifact, no install-time npm
* - `manifest-deps`— deps resolved at install (`pnpm install`, opt-in)
*/
export const PluginPackagingSchema = z
.enum(['bundled', 'manifest-deps'])
.describe('Dependency packaging strategy (ADR-0025 §3.3)');
export type PluginPackaging = z.infer<typeof PluginPackagingSchema>;
/**
* Per-file content digests of the packaged artifact (ADR-0025 §3.2),
* mapping artifact-relative path → digest string (e.g. "sha256-<base64>").
* Re-verified by the runtime when it unpacks the `.osplugin` (ADR §3.5
* step 5).
*/
export const PluginIntegritySchema = z
.record(z.string(), z.string())
.describe('Per-file content digests of the plugin artifact (ADR-0025 §3.2)');
export type PluginIntegrity = z.infer<typeof PluginIntegritySchema>;
/**
* Schema for the ObjectStack Manifest.
* This defines the structure of a package configuration in the ObjectStack ecosystem.
* All packages (apps, plugins, drivers, modules) must conform to this schema.
*
* @example App Package
* ```yaml
* id: com.acme.crm
* version: 1.0.0
* type: app
* name: Acme CRM
* description: Customer Relationship Management system
* permissions:
* - system.user.read
* - system.object.create
* objects:
* - "./src/objects/*.object.yml"
* ```
*/
export const ManifestSchema = z.object({
/**
* Unique package identifier using reverse domain notation.
* Must be unique across the entire ecosystem.
*
* @example "com.steedos.crm"
* @example "org.apache.superset"
*/
id: z.string().describe('Unique package identifier (reverse domain style)'),
/**
* Short namespace identifier for metadata scoping AND the mandatory
* prefix of every object name in this package.
*
* **Authoring rule (single canonical style — no alternatives):**
* Every `object.name` in this stack MUST be `${namespace}_${shortName}`.
* AI and humans write the full prefixed name verbatim everywhere it
* appears (`*.object.ts`, view `data.object`, dashboard `object`,
* report `objectName`, flow / approval / hook references, app
* navigation `objectName`, seed dataset `externalId`, translation
* `objects.<name>` keys, permissions, sharing rules).
*
* Examples:
* namespace: 'todo' → object names: 'todo_task', 'todo_project'
* namespace: 'crm' → object names: 'crm_account', 'crm_contact'
*
* `defineStack()` enforces this with a validator that lists every
* violation and the expected fix. The platform deliberately does NOT
* provide a `ns('task') → 'todo_task'` helper or a generic factory
* (`defineObject<'todo'>(...)`) — two writing styles cause AI to
* guess wrong half the time. The full prefixed literal is the only
* supported form.
*
* Physical storage uses the full prefixed name as the table name, so
* multiple packages installed in the same database cannot collide.
*
* Rules:
* - 2-20 characters, lowercase letters, digits, and underscores only.
* - Must be unique within a running instance.
* - Platform-reserved namespaces: "base", "system", "sys".
* - Object names starting with `sys_` are reserved for the platform
* and exempt from the namespace-prefix check (apps may reference
* them but never define them).
*/
namespace: z.string()
.regex(/^[a-z][a-z0-9_]{1,19}$/, 'Namespace must be 2-20 chars, lowercase alphanumeric + underscore')
.optional()
.describe('Short namespace identifier; also the mandatory prefix of every object name (e.g. "todo" → object names "todo_task", "todo_project")'),
/**
* Default datasource for all objects in this package.
*
* When set, all objects defined in this package will use this datasource
* by default unless they explicitly override it with their own `datasource` field.
*
* This provides package-level datasource configuration without needing to
* specify it on every individual object.
*
* @example "memory" // Use in-memory driver for all package objects
* @example "turso" // Use Turso/LibSQL for all package objects
*/
defaultDatasource: z.string().optional().default('default')
.describe('Default datasource for all objects in this package'),
/**
* Package version following semantic versioning (major.minor.patch).
*
* @example "1.0.0"
* @example "2.1.0-beta.1"
*/
version: z.string().regex(/^\d+\.\d+\.\d+$/).describe('Package version (semantic versioning)'),
/**
* Type of the package in the ObjectStack ecosystem.
* - plugin: General-purpose functionality extension (Runtime: standard)
* - app: Business application package
* - driver: Connectivity adapter
* - server: Protocol gateway (Hono, GraphQL)
* - ui: Frontend package (Static/SPA)
* - theme: UI Theme
* - agent: AI Agent
* - module: Reusable code library/shared module
* - objectql: Core engine
* - adapter: Host adapter (Express, Fastify)
*/
type: z.enum([
'plugin',
...CORE_PLUGIN_TYPES,
'module',
'gateway', // Deprecated: use 'server'
'adapter'
]).describe('Type of package'),
/**
* Deployment scope of this package.
*
* - `cloud`: Control-plane exclusive (tenant management, credentials, package registry…).
* Never registered into a project kernel; accessible only via `/api/v1/cloud/*`.
* - `system`: Cross-project shared identity (user, org, role, i18n…).
* In a project kernel, objects are transparently proxied to the control-plane DB
* with an automatic `organization_id` filter for org-scoped tables.
* Packages with this scope should also set `defaultDatasource: 'cloud'`.
* - `project`: Per-project business objects (CRM, custom apps…).
* Registered normally into the project DB.
*/
scope: z.enum(['cloud', 'system', 'project']).default('project')
.describe('Deployment scope: cloud | system | project'),
/**
* Human-readable name of the package.
* Displayed in the UI for users.
*
* @example "Project Management"
*/
name: z.string().describe('Human-readable package name'),
/**
* Brief description of the package functionality.
* Displayed in the marketplace and plugin manager.
*/
description: z.string().optional().describe('Package description'),
/**
* Permissions the package requires — the "Scope" requested at installation.
*
* Accepts either the legacy flat list of permission strings, or the
* structured plugin permission block ({@link PluginPermissionsSchema},
* ADR-0025 §3.2) that maps to service / hook / network / fs capabilities.
*
* @example ["system.user.read", "system.data.write"]
* @example { "services": ["object", "http"], "hooks": ["record.beforeInsert"] }
*/
permissions: ManifestPermissionsSchema.optional()
.describe('Required permissions: legacy string[] or structured plugin block (ADR-0025 §3.2)'),
/**
* Glob patterns specifying ObjectQL schemas files.
* Matches `*.object.yml` or `*.object.ts` files to load business objects.
*
* @example ["./src/objects/*.object.yml"]
*/
objects: z.array(z.string()).optional().describe('Glob patterns for ObjectQL schemas files'),
/**
* Defines system level DataSources.
* Matches `*.datasource.yml` files.
*
* @example ["./src/datasources/*.datasource.mongo.yml"]
*/
datasources: z.array(z.string()).optional().describe('Glob patterns for Datasource definitions'),
/**
* Package Dependencies.
* Map of package IDs to version requirements.
*
* @example { "@steedos/plugin-auth": "^2.0.0" }
*/
dependencies: z.record(z.string(), z.string()).optional().describe('Package dependencies'),
/**
* Plugin Configuration Schema.
* Defines the settings this plugin exposes to the user via UI/ENV.
* Uses a simplified JSON Schema format.
*
* @example
* {
* "title": "Stripe Config",
* "properties": {
* "apiKey": { "type": "string", "secret": true },
* "currency": { "type": "string", "default": "USD" }
* }
* }
*/
configuration: z.object({
title: z.string().optional(),
properties: z.record(z.string(), z.object({
type: z.enum(['string', 'number', 'boolean', 'array', 'object']).describe('Data type of the setting'),
default: z.unknown().optional().describe('Default value'),
description: z.string().optional().describe('Tooltip description'),
required: z.boolean().optional().describe('Is this setting required?'),
secret: z.boolean().optional().describe('If true, value is encrypted/masked (e.g. API Keys)'),
enum: z.array(z.string()).optional().describe('Allowed values for select inputs'),
})).describe('Map of configuration keys to their definitions')
}).optional().describe('Plugin configuration settings'),
/**
* Contribution Points (VS Code Style).
* formalized way to extend the platform capabilities.
*/
contributes: z.object({
/**
* Register new Metadata Kinds (CRDs).
* Enables the system to parse and validate new file types.
* Example: Registering a BI plugin to handle *.report.ts
*/
kinds: z.array(z.object({
id: z.string().describe('The generic identifier of the kind (e.g., "sys.bi.report")'),
globs: z.array(z.string()).describe('File patterns to watch (e.g., ["**/*.report.ts"])'),
description: z.string().optional().describe('Description of what this kind represents'),
})).optional().describe('New Metadata Types to recognize'),
/**
* Register System Hooks.
* Declares that this plugin listens to specific system events.
*/
events: z.array(z.string()).optional().describe('Events this plugin listens to'),
/**
* Register UI Menus.
*/
menus: z.record(z.string(), z.array(z.object({
id: z.string(),
label: z.string(),
command: z.string().optional(),
}))).optional().describe('UI Menu contributions'),
/**
* Register Custom Themes.
*/
themes: z.array(z.object({
id: z.string(),
label: z.string(),
path: z.string(),
})).optional().describe('Theme contributions'),
/**
* Register Translations.
* Path to translation files (e.g. "locales/en.json").
*/
translations: z.array(z.object({
locale: z.string(),
path: z.string(),
})).optional().describe('Translation resources'),
/**
* Register Server Actions.
* Invocable functions exposed to Flows or API.
*/
actions: z.array(z.object({
name: z.string().describe('Unique action name'),
label: z.string().optional(),
description: z.string().optional(),
input: z.unknown().optional().describe('Input validation schema'),
output: z.unknown().optional().describe('Output schema'),
})).optional().describe('Exposed server actions'),
/**
* Register Storage Drivers.
* Enables connecting to new types of datasources.
*/
drivers: z.array(z.object({
id: z.string().describe('Driver unique identifier (e.g. "postgres", "mongo")'),
label: z.string().describe('Human readable name'),
description: z.string().optional(),
})).optional().describe('Driver contributions'),
/**
* Register Custom Field Types.
* Extends the data model with new widget types.
*/
fieldTypes: z.array(z.object({
name: z.string().describe('Unique field type name (e.g. "vector")'),
label: z.string().describe('Display label'),
description: z.string().optional(),
})).optional().describe('Field Type contributions'),
/**
* Register Custom Query Operators/Functions.
* Extends ObjectQL with new functions (e.g. distance()).
*/
functions: z.array(z.object({
name: z.string().describe('Function name (e.g. "distance")'),
description: z.string().optional(),
args: z.array(z.string()).optional().describe('Argument types'),
returnType: z.string().optional(),
})).optional().describe('Query Function contributions'),
/**
* Register API Route Namespaces.
* Declares the API endpoints this plugin provides to the HttpDispatcher.
* The kernel routes matching prefixes to this plugin's handler.
*
* @example
* routes: [
* { prefix: '/api/v1/ai', service: 'ai', methods: ['aiNlq', 'aiChat'] }
* ]
*/
routes: z.array(z.object({
/** URL path prefix (e.g. "/api/v1/ai") */
prefix: z.string().regex(/^\//).describe('API path prefix'),
/** Service name this plugin provides */
service: z.string().describe('Service name this plugin provides'),
/** Protocol method names implemented */
methods: z.array(z.string()).optional()
.describe('Protocol method names implemented (e.g. ["aiNlq", "aiChat"])'),
})).optional().describe('API route contributions to HttpDispatcher'),
/**
* Register CLI Commands.
* Allows plugins to extend the ObjectStack CLI with custom commands.
* Each command entry declares metadata; the actual Commander.js command
* is resolved at runtime by importing the plugin's module.
*
* The plugin package must export a `commands` array of Commander.js `Command` instances
* from its main entry point or from the path specified in `module`.
*
* @example
* ```yaml
* commands:
* - name: marketplace
* description: "Manage marketplace apps"
* module: "./cli" # optional, defaults to package main
* - name: deploy
* description: "Deploy to cloud"
* ```
*/
commands: z.array(z.object({
/** CLI command name (e.g., "marketplace", "deploy"). Must be a valid CLI identifier. */
name: z.string()
.regex(/^[a-z][a-z0-9-]*$/, 'Command name must be lowercase alphanumeric with hyphens')
.describe('CLI command name'),
/** Brief description shown in `os --help` */
description: z.string().optional().describe('Command description for help text'),
/**
* Optional module path (relative to package root) that exports the Commander.js commands.
* If omitted, the CLI will import from the package's main entry point.
* The module must export a `commands` array of Commander.js `Command` instances,
* or a single `Command` instance as default export.
*/
module: z.string().optional().describe('Module path exporting Commander.js commands'),
})).optional().describe('CLI command contributions'),
}).optional().describe('Platform contributions'),
/**
* Initial data seeding configuration.
* Defines default records to be inserted when the package is installed.
*
* Uses the standard SeedSchema which supports idempotent upsert via
* `externalId`, environment scoping via `env`, and multiple conflict
* resolution modes.
*
* @deprecated Prefer using the top-level `data` field on the Stack Definition
* (defineStack({ data: [...] })) for better visibility and metadata registration.
* This field is retained for backward compatibility with manifest-only packages.
*/
data: z.array(SeedSchema).optional().describe('Initial seed data (prefer top-level data field)'),
/**
* Plugin Capability Manifest.
* Declares protocols implemented, interfaces provided, dependencies, and extension points.
* This enables plugin interoperability and automatic discovery.
*/
capabilities: PluginCapabilityManifestSchema.optional()
.describe('Plugin capability declarations for interoperability'),
/**
* Extension points contributed by this package.
* Allows packages to extend UI components, add functionality, etc.
*/
extensions: z.record(z.string(), z.unknown()).optional().describe('Extension points and contributions'),
/**
* Navigation contributions (ADR-0029 D7).
*
* Lets this package inject navigation items into apps it does not own
* (e.g. a capability plugin adding its menu entries into the `setup` app).
* The runtime merges these into the target app's `navigation` tree by
* group id + priority. See {@link NavigationContributionSchema}.
*/
navigationContributions: z.array(NavigationContributionSchema).optional()
.describe('Navigation items this package contributes into apps owned by other packages'),
/**
* Plugin Loading Configuration.
* Configures how the plugin is loaded, initialized, and managed at runtime.
* Includes strategies for lazy loading, code splitting, caching, and hot reload.
*/
loading: PluginLoadingConfigSchema.optional()
.describe('Plugin loading and runtime behavior configuration'),
/**
* Platform Compatibility Requirements.
* Specifies the minimum ObjectStack platform version required to run this package.
* Used at install time to prevent incompatible packages from being installed.
*
* @example
* ```yaml
* engine:
* objectstack: ">=3.0.0"
* ```
*/
engine: z.object({
/** ObjectStack platform version requirement (SemVer range) */
objectstack: z.string()
.regex(/^[><=~^]*\d+\.\d+\.\d+/)
.describe('ObjectStack platform version requirement (SemVer range, e.g. ">=3.0.0")'),
}).optional().describe('Platform compatibility requirements (legacy; superseded by `engines`)'),
/**
* Compatibility ranges (ADR-0025 §3.2). Protocol-first: `engines.protocol`
* is checked before `engines.platform`. Supersedes the legacy single-field
* `engine`, which is retained for backward compatibility.
*/
engines: PluginEnginesSchema.optional()
.describe('Plugin compatibility ranges (ADR-0025 §3.2; supersedes `engine`)'),
/**
* Trust / isolation tier the plugin runs under (ADR-0025 §3.6).
* Unset implies a pure-metadata package (no executable code).
*/
runtime: PluginRuntimeSchema.optional()
.describe('Plugin trust tier (ADR-0025 §3.6)'),
/**
* Dependency packaging strategy for code-bearing plugins (ADR-0025 §3.3).
*/
packaging: PluginPackagingSchema.optional()
.describe('Dependency packaging strategy (ADR-0025 §3.3)'),
/**
* Per-file content digests of the packaged artifact (ADR-0025 §3.2),
* verified at install/load time when the runtime unpacks the `.osplugin`.
*/
integrity: PluginIntegritySchema.optional()
.describe('Per-file content digests of the plugin artifact (ADR-0025 §3.2)'),
});
/**
* TypeScript type inferred from the ManifestSchema.
* Use this type for type-safe manifest handling in TypeScript code.
*/
export type ObjectStackManifest = z.infer<typeof ManifestSchema>;
export type ObjectStackManifestInput = z.input<typeof ManifestSchema>;