-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.zod.ts
More file actions
165 lines (141 loc) · 5.44 KB
/
context.zod.ts
File metadata and controls
165 lines (141 loc) · 5.44 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
import { TenantQuotaSchema } from '../system/tenant.zod.js';
/**
* Runtime Mode Enum
* Defines the operating mode of the kernel
*/
export const RuntimeMode = z.enum([
'development', // Hot-reload, verbose logging
'production', // Optimized, strict security
'test', // Mocked interfaces
'provisioning', // Setup/Migration mode
'preview', // Demo/preview mode — bypass auth, simulate admin identity
]).describe('Kernel operating mode');
export type RuntimeMode = z.infer<typeof RuntimeMode>;
/**
* Preview Mode Configuration Schema
*
* Configures the kernel's preview/demo mode behaviour.
* When `mode` is set to `'preview'`, the platform skips authentication
* screens and optionally simulates an admin identity so that visitors
* (e.g. app-marketplace customers) can explore the system without
* registering or logging in.
*
* **Security note:** preview mode should NEVER be used in production.
* The runtime must enforce this constraint.
*
* @example
* ```ts
* const ctx = KernelContextSchema.parse({
* instanceId: '550e8400-e29b-41d4-a716-446655440000',
* mode: 'preview',
* version: '1.0.0',
* cwd: '/app',
* startTime: Date.now(),
* previewMode: {
* autoLogin: true,
* simulatedRole: 'admin',
* },
* });
* ```
*/
export const PreviewModeConfigSchema = z.object({
/**
* Automatically log in as a simulated user on startup.
* When enabled, the frontend skips login/registration screens entirely.
*/
autoLogin: z.boolean().default(true)
.describe('Auto-login as simulated user, skipping login/registration pages'),
/**
* Role of the simulated user.
* Determines the permission level of the auto-created preview session.
*/
simulatedRole: z.enum(['admin', 'user', 'viewer']).default('admin')
.describe('Permission role for the simulated preview user'),
/**
* Display name for the simulated user shown in the UI.
*/
simulatedUserName: z.string().default('Preview User')
.describe('Display name for the simulated preview user'),
/**
* Whether the preview session is read-only.
* When true, all write operations (create, update, delete) are blocked.
*/
readOnly: z.boolean().default(false)
.describe('Restrict the preview session to read-only operations'),
/**
* Session duration in seconds. After expiry the preview session ends.
* 0 means no expiration.
*/
expiresInSeconds: z.number().int().min(0).default(0)
.describe('Preview session duration in seconds (0 = no expiration)'),
/**
* Optional banner message shown in the UI to indicate preview mode.
* Useful for marketplace demos so visitors know they are in a sandbox.
*/
bannerMessage: z.string().optional()
.describe('Banner message displayed in the UI during preview mode'),
});
export type PreviewModeConfig = z.infer<typeof PreviewModeConfigSchema>;
/**
* Kernel Context Schema
* Defines the static environment information available to the Kernel at boot.
*/
export const KernelContextSchema = z.object({
/**
* Instance Identity
*/
instanceId: z.string().uuid().describe('Unique UUID for this running kernel process'),
/**
* Environment Metadata
*/
mode: RuntimeMode.default('production'),
version: z.string().describe('Kernel version'),
appName: z.string().optional().describe('Host application name'),
/**
* Paths
*/
cwd: z.string().describe('Current working directory'),
workspaceRoot: z.string().optional().describe('Workspace root if different from cwd'),
/**
* Telemetry
*/
startTime: z.number().int().describe('Boot timestamp (ms)'),
/**
* Feature Flags (Global)
*/
features: z.record(z.string(), z.boolean()).default({}).describe('Global feature toggles'),
/**
* Preview Mode Configuration.
* Only relevant when `mode` is `'preview'`. Configures auto-login,
* simulated identity, read-only restrictions, and UI banner.
*/
previewMode: PreviewModeConfigSchema.optional()
.describe('Preview/demo mode configuration (used when mode is "preview")'),
});
export type KernelContext = z.infer<typeof KernelContextSchema>;
// ==========================================================================
// Tenant Runtime Context
// ==========================================================================
/**
* Tenant Runtime Context Schema.
*
* Extends the base KernelContext with tenant-specific information.
* Constructed per-request from: session → org → tenant lookup.
* Provides the tenant identity, plan, region, and database URL to all
* downstream services during request processing.
*/
export const TenantRuntimeContextSchema = KernelContextSchema.extend({
/** Unique tenant identifier resolved from the current session */
tenantId: z.string().min(1).describe('Resolved tenant identifier'),
/** Tenant subscription plan */
tenantPlan: z.enum(['free', 'pro', 'enterprise']).describe('Tenant subscription plan'),
/** Tenant deployment region */
tenantRegion: z.string().optional().describe('Tenant deployment region'),
/** Tenant database connection URL */
tenantDbUrl: z.string().min(1).describe('Tenant database connection URL'),
/** Optional tenant quotas for the current plan */
tenantQuotas: TenantQuotaSchema.optional().describe('Tenant resource quotas'),
}).describe('Tenant-aware kernel runtime context');
export type TenantRuntimeContext = z.infer<typeof TenantRuntimeContextSchema>;