@zhin.js/gateway 0.0.1-bootstrap.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/lib/config/ai-config-loader.d.ts +22 -0
- package/lib/config/ai-config-loader.js +96 -0
- package/lib/config/configuration-chat-policy.d.ts +57 -0
- package/lib/config/configuration-chat-policy.js +423 -0
- package/lib/config/configuration-patch.d.ts +16 -0
- package/lib/config/configuration-patch.js +105 -0
- package/lib/config/configuration-path.d.ts +6 -0
- package/lib/config/configuration-path.js +40 -0
- package/lib/config/configuration-projection.d.ts +6 -0
- package/lib/config/configuration-projection.js +28 -0
- package/lib/config/configuration-redaction.d.ts +24 -0
- package/lib/config/configuration-redaction.js +128 -0
- package/lib/config/configuration.d.ts +81 -0
- package/lib/config/configuration.js +686 -0
- package/lib/config/console-schema.d.ts +11 -0
- package/lib/config/console-schema.js +222 -0
- package/lib/config/environment-files.d.ts +2 -0
- package/lib/config/environment-files.js +21 -0
- package/lib/config/plugin-package-map.d.ts +2 -0
- package/lib/config/plugin-package-map.js +14 -0
- package/lib/config/plugin-schema-catalog.d.ts +22 -0
- package/lib/config/plugin-schema-catalog.js +262 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +12 -0
- package/lib/database/config.d.ts +22 -0
- package/lib/database/config.js +159 -0
- package/lib/database/index.d.ts +2 -0
- package/lib/database/index.js +2 -0
- package/lib/database/project-database.d.ts +31 -0
- package/lib/database/project-database.js +130 -0
- package/lib/database.d.ts +2 -0
- package/lib/database.js +2 -0
- package/lib/http/console-rpc.d.ts +19 -0
- package/lib/http/console-rpc.js +51 -0
- package/lib/http/gateway-console.d.ts +63 -0
- package/lib/http/gateway-console.js +309 -0
- package/lib/http/gateway-events.d.ts +35 -0
- package/lib/http/gateway-events.js +172 -0
- package/lib/http/gateway-http-server.d.ts +95 -0
- package/lib/http/gateway-http-server.js +518 -0
- package/lib/http/local-principal.d.ts +2 -0
- package/lib/http/local-principal.js +16 -0
- package/lib/http/run-gateway.d.ts +43 -0
- package/lib/http/run-gateway.js +268 -0
- package/lib/http/start-gateway.d.ts +114 -0
- package/lib/http/start-gateway.js +220 -0
- package/lib/http-response.d.ts +6 -0
- package/lib/http-response.js +25 -0
- package/lib/http.d.ts +6 -0
- package/lib/http.js +6 -0
- package/lib/iam/crypto.d.ts +12 -0
- package/lib/iam/crypto.js +58 -0
- package/lib/iam/index.d.ts +8 -0
- package/lib/iam/index.js +8 -0
- package/lib/iam/install.d.ts +36 -0
- package/lib/iam/install.js +74 -0
- package/lib/iam/local-admin.d.ts +27 -0
- package/lib/iam/local-admin.js +102 -0
- package/lib/iam/rate-limit.d.ts +15 -0
- package/lib/iam/rate-limit.js +52 -0
- package/lib/iam/routes.d.ts +23 -0
- package/lib/iam/routes.js +157 -0
- package/lib/iam/rpc.d.ts +14 -0
- package/lib/iam/rpc.js +100 -0
- package/lib/iam/service.d.ts +203 -0
- package/lib/iam/service.js +784 -0
- package/lib/iam/setup-service.d.ts +83 -0
- package/lib/iam/setup-service.js +206 -0
- package/lib/iam/tables.d.ts +20 -0
- package/lib/iam/tables.js +82 -0
- package/lib/iam.d.ts +2 -0
- package/lib/iam.js +2 -0
- package/lib/index.d.ts +22 -0
- package/lib/index.js +22 -0
- package/lib/local/client.d.ts +71 -0
- package/lib/local/client.js +296 -0
- package/lib/local/handler.d.ts +30 -0
- package/lib/local/handler.js +86 -0
- package/lib/local/local-socket.d.ts +55 -0
- package/lib/local/local-socket.js +232 -0
- package/lib/local/paths.d.ts +34 -0
- package/lib/local/paths.js +58 -0
- package/lib/local/protocol.d.ts +140 -0
- package/lib/local/protocol.js +90 -0
- package/lib/local/state-file.d.ts +8 -0
- package/lib/local/state-file.js +45 -0
- package/lib/local.d.ts +10 -0
- package/lib/local.js +10 -0
- package/lib/onboarding/allowlist.d.ts +54 -0
- package/lib/onboarding/allowlist.js +106 -0
- package/lib/onboarding/errors.d.ts +7 -0
- package/lib/onboarding/errors.js +17 -0
- package/lib/onboarding/index.d.ts +6 -0
- package/lib/onboarding/index.js +6 -0
- package/lib/onboarding/install.d.ts +99 -0
- package/lib/onboarding/install.js +132 -0
- package/lib/onboarding/package-manager.d.ts +47 -0
- package/lib/onboarding/package-manager.js +179 -0
- package/lib/onboarding/service.d.ts +53 -0
- package/lib/onboarding/service.js +144 -0
- package/lib/onboarding/state-store.d.ts +26 -0
- package/lib/onboarding/state-store.js +97 -0
- package/lib/onboarding.d.ts +2 -0
- package/lib/onboarding.js +2 -0
- package/lib/packages/authorization.d.ts +23 -0
- package/lib/packages/authorization.js +109 -0
- package/lib/packages/errors.d.ts +11 -0
- package/lib/packages/errors.js +26 -0
- package/lib/packages/index.d.ts +5 -0
- package/lib/packages/index.js +5 -0
- package/lib/packages/rpc.d.ts +30 -0
- package/lib/packages/rpc.js +155 -0
- package/lib/packages/service.d.ts +121 -0
- package/lib/packages/service.js +441 -0
- package/lib/packages/snapshot.d.ts +14 -0
- package/lib/packages/snapshot.js +73 -0
- package/lib/packages.d.ts +2 -0
- package/lib/packages.js +2 -0
- package/lib/plugin-management/activity.d.ts +6 -0
- package/lib/plugin-management/activity.js +14 -0
- package/lib/plugin-management/config-explanation.d.ts +6 -0
- package/lib/plugin-management/config-explanation.js +113 -0
- package/lib/plugin-management/use-cases.d.ts +15 -0
- package/lib/plugin-management/use-cases.js +120 -0
- package/lib/plugin-management/workflow.d.ts +92 -0
- package/lib/plugin-management/workflow.js +772 -0
- package/lib/plugin-management.d.ts +5 -0
- package/lib/plugin-management.js +5 -0
- package/lib/project/config-file.d.ts +6 -0
- package/lib/project/config-file.js +58 -0
- package/lib/project/environment.d.ts +13 -0
- package/lib/project/environment.js +52 -0
- package/lib/project/instance-declarations.d.ts +55 -0
- package/lib/project/instance-declarations.js +542 -0
- package/lib/project/legacy-config.d.ts +25 -0
- package/lib/project/legacy-config.js +70 -0
- package/lib/project/node-requirements.d.ts +7 -0
- package/lib/project/node-requirements.js +20 -0
- package/lib/project/tsx-module-loader.d.ts +7 -0
- package/lib/project/tsx-module-loader.js +29 -0
- package/lib/project.d.ts +7 -0
- package/lib/project.js +7 -0
- package/lib/server.d.ts +11 -0
- package/lib/server.js +11 -0
- package/lib/supervisor/instance-logs.d.ts +39 -0
- package/lib/supervisor/instance-logs.js +152 -0
- package/lib/supervisor/instance-supervisor.d.ts +140 -0
- package/lib/supervisor/instance-supervisor.js +408 -0
- package/lib/supervisor/run-instance.d.ts +35 -0
- package/lib/supervisor/run-instance.js +161 -0
- package/lib/supervisor/terminal-state.d.ts +30 -0
- package/lib/supervisor/terminal-state.js +57 -0
- package/lib/supervisor.d.ts +4 -0
- package/lib/supervisor.js +4 -0
- package/lib/terminal/backends.d.ts +38 -0
- package/lib/terminal/backends.js +193 -0
- package/lib/terminal/config.d.ts +63 -0
- package/lib/terminal/config.js +237 -0
- package/lib/terminal/host.d.ts +26 -0
- package/lib/terminal/host.js +76 -0
- package/lib/terminal/index.d.ts +60 -0
- package/lib/terminal/index.js +135 -0
- package/lib/terminal/modules.d.ts +87 -0
- package/lib/terminal/modules.js +44 -0
- package/lib/terminal/service.d.ts +101 -0
- package/lib/terminal/service.js +455 -0
- package/lib/terminal/tickets.d.ts +46 -0
- package/lib/terminal/tickets.js +76 -0
- package/lib/terminal.d.ts +3 -0
- package/lib/terminal.js +3 -0
- package/lib/usage/aggregate.d.ts +87 -0
- package/lib/usage/aggregate.js +212 -0
- package/lib/usage.d.ts +2 -0
- package/lib/usage.js +2 -0
- package/package.json +117 -4
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** `@zhin.js/gateway/plugin-management` — project-level package transactions (install / uninstall / update). */
|
|
2
|
+
export * from './plugin-management/activity.js';
|
|
3
|
+
export * from './plugin-management/config-explanation.js';
|
|
4
|
+
export * from './plugin-management/use-cases.js';
|
|
5
|
+
export * from './plugin-management/workflow.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** `@zhin.js/gateway/plugin-management` — project-level package transactions (install / uninstall / update). */
|
|
2
|
+
export * from './plugin-management/activity.js';
|
|
3
|
+
export * from './plugin-management/config-explanation.js';
|
|
4
|
+
export * from './plugin-management/use-cases.js';
|
|
5
|
+
export * from './plugin-management/workflow.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type PreparedConfigDocument } from '@zhin.js/plugin-runtime';
|
|
2
|
+
export declare function findConfigFile(cwd: string): string | null;
|
|
3
|
+
export declare function readConfig(filePath: string): Promise<Record<string, unknown>>;
|
|
4
|
+
/** Validates an editable candidate and prepares a write against its original source. */
|
|
5
|
+
export declare function prepareConfigSave(filePath: string, config: Record<string, unknown>, baseConfig?: Record<string, unknown>): Promise<PreparedConfigDocument | undefined>;
|
|
6
|
+
export declare function saveConfig(filePath: string, config: Record<string, unknown>, baseConfig?: Record<string, unknown>): Promise<void>;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
4
|
+
import { ConfigDocumentConflictError, createConfigDocument, readApplicationPluginDeclaration } from '@zhin.js/runtime';
|
|
5
|
+
import { ROOT_CONFIG_FILE_NAMES, selectRootConfigFile, } from '@zhin.js/plugin-runtime';
|
|
6
|
+
const loadedConfigSource = Symbol('zhin.cli.loaded-config-source');
|
|
7
|
+
function rememberSource(config, file, snapshot) {
|
|
8
|
+
Object.defineProperty(config, loadedConfigSource, {
|
|
9
|
+
value: { file: path.resolve(file), snapshot }, configurable: true, enumerable: false,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export function findConfigFile(cwd) {
|
|
13
|
+
const existing = ROOT_CONFIG_FILE_NAMES
|
|
14
|
+
.filter((name) => fs.existsSync(path.join(cwd, name)));
|
|
15
|
+
const selected = selectRootConfigFile(existing);
|
|
16
|
+
return selected ?? null;
|
|
17
|
+
}
|
|
18
|
+
export async function readConfig(filePath) {
|
|
19
|
+
const snapshot = await createConfigDocument(filePath).read();
|
|
20
|
+
readApplicationPluginDeclaration(snapshot.document);
|
|
21
|
+
const config = structuredClone(snapshot.document);
|
|
22
|
+
rememberSource(config, filePath, snapshot);
|
|
23
|
+
return config;
|
|
24
|
+
}
|
|
25
|
+
/** Validates an editable candidate and prepares a write against its original source. */
|
|
26
|
+
export async function prepareConfigSave(filePath, config, baseConfig = config) {
|
|
27
|
+
readApplicationPluginDeclaration(config);
|
|
28
|
+
const document = createConfigDocument(filePath);
|
|
29
|
+
const source = baseConfig[loadedConfigSource];
|
|
30
|
+
if (source && source.file !== path.resolve(filePath))
|
|
31
|
+
throw new Error('Loaded configuration belongs to a different file');
|
|
32
|
+
const current = source?.snapshot ?? await document.read();
|
|
33
|
+
// Validate even a no-op before a caller writes related dependencies or env.
|
|
34
|
+
if (source && (await document.read()).revision !== current.revision)
|
|
35
|
+
throw new ConfigDocumentConflictError(filePath);
|
|
36
|
+
const patches = [];
|
|
37
|
+
for (const key of new Set([...Object.keys(current.document), ...Object.keys(config)])) {
|
|
38
|
+
if (isDeepStrictEqual(current.document[key], config[key]))
|
|
39
|
+
continue;
|
|
40
|
+
if (key === 'plugins' && current.document.plugins && config.plugins) {
|
|
41
|
+
const previous = current.document.plugins;
|
|
42
|
+
const next = config.plugins;
|
|
43
|
+
for (const alias of new Set([...Object.keys(previous), ...Object.keys(next)])) {
|
|
44
|
+
if (isDeepStrictEqual(previous[alias], next[alias]))
|
|
45
|
+
continue;
|
|
46
|
+
patches.push(Object.hasOwn(next, alias) ? { op: 'set', path: ['plugins', alias], value: next[alias] } : { op: 'remove', path: ['plugins', alias] });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
patches.push(Object.hasOwn(config, key) ? { op: 'set', path: [key], value: config[key] } : { op: 'remove', path: [key] });
|
|
51
|
+
}
|
|
52
|
+
return patches.length ? document.prepare(current, patches) : undefined;
|
|
53
|
+
}
|
|
54
|
+
export async function saveConfig(filePath, config, baseConfig = config) {
|
|
55
|
+
const prepared = await prepareConfigSave(filePath, config, baseConfig);
|
|
56
|
+
if (prepared)
|
|
57
|
+
rememberSource(config, filePath, await prepared.commit());
|
|
58
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { EnvironmentLayers, EnvironmentLayersPort } from '@zhin.js/runtime';
|
|
2
|
+
/**
|
|
3
|
+
* Read project dotenv files into Runtime EnvironmentLayers without changing
|
|
4
|
+
* the CLI process. `.env.<environment>` deliberately overrides `.env`; the
|
|
5
|
+
* Runtime applies that overlay after inherited process variables.
|
|
6
|
+
*/
|
|
7
|
+
export declare class ProjectEnvironmentFileSource implements EnvironmentLayersPort {
|
|
8
|
+
#private;
|
|
9
|
+
readonly sources: readonly string[];
|
|
10
|
+
constructor(root: string, environment: string);
|
|
11
|
+
read(): Promise<Readonly<EnvironmentLayers>>;
|
|
12
|
+
}
|
|
13
|
+
export declare function loadRuntimeEnvironmentLayers(root: string, environment: string): Promise<Readonly<EnvironmentLayers>>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { parse as parseDotenv } from 'dotenv';
|
|
4
|
+
function processEnvironmentSource() {
|
|
5
|
+
const result = {};
|
|
6
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
7
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(key))
|
|
8
|
+
continue;
|
|
9
|
+
result[key] = value;
|
|
10
|
+
}
|
|
11
|
+
return Object.freeze(result);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Read project dotenv files into Runtime EnvironmentLayers without changing
|
|
15
|
+
* the CLI process. `.env.<environment>` deliberately overrides `.env`; the
|
|
16
|
+
* Runtime applies that overlay after inherited process variables.
|
|
17
|
+
*/
|
|
18
|
+
export class ProjectEnvironmentFileSource {
|
|
19
|
+
sources;
|
|
20
|
+
#environment;
|
|
21
|
+
#base;
|
|
22
|
+
constructor(root, environment) {
|
|
23
|
+
this.#environment = environment;
|
|
24
|
+
this.#base = processEnvironmentSource();
|
|
25
|
+
this.sources = Object.freeze([
|
|
26
|
+
resolve(root, '.env'),
|
|
27
|
+
resolve(root, `.env.${environment}`),
|
|
28
|
+
]);
|
|
29
|
+
}
|
|
30
|
+
async read() {
|
|
31
|
+
const overlay = {};
|
|
32
|
+
for (const file of this.sources) {
|
|
33
|
+
try {
|
|
34
|
+
Object.assign(overlay, parseDotenv(await readFile(file, 'utf8')));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT')
|
|
38
|
+
continue;
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
base: this.#base,
|
|
44
|
+
environments: Object.freeze({
|
|
45
|
+
[this.#environment]: Object.freeze(overlay),
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function loadRuntimeEnvironmentLayers(root, environment) {
|
|
51
|
+
return new ProjectEnvironmentFileSource(root, environment).read();
|
|
52
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
type SourceState = Readonly<{
|
|
2
|
+
exists: boolean;
|
|
3
|
+
source: string;
|
|
4
|
+
digest: string;
|
|
5
|
+
mode: number;
|
|
6
|
+
}>;
|
|
7
|
+
export interface InstanceMigrationFile {
|
|
8
|
+
readonly path: string;
|
|
9
|
+
readonly before: SourceState;
|
|
10
|
+
readonly after: SourceState;
|
|
11
|
+
}
|
|
12
|
+
export interface InstanceDeclarationMigrationPlan {
|
|
13
|
+
readonly root: string;
|
|
14
|
+
readonly files: readonly InstanceMigrationFile[];
|
|
15
|
+
/** Package manifests consulted during static resolution. No plugin entry is imported. */
|
|
16
|
+
readonly guards: readonly Readonly<{
|
|
17
|
+
path: string;
|
|
18
|
+
digest: string;
|
|
19
|
+
}>[];
|
|
20
|
+
readonly instances: readonly Readonly<{
|
|
21
|
+
instanceKey: string;
|
|
22
|
+
package: string;
|
|
23
|
+
pluginId: string;
|
|
24
|
+
enabled: boolean;
|
|
25
|
+
}>[];
|
|
26
|
+
readonly pendingChecks: readonly string[];
|
|
27
|
+
}
|
|
28
|
+
export interface InstanceMigrationApplyOptions {
|
|
29
|
+
/** Fault-injection seam: invoked after each durable file replacement. */
|
|
30
|
+
readonly afterWrite?: (index: number) => void | Promise<void>;
|
|
31
|
+
/** Clock seam for the human-readable backup directory name. */
|
|
32
|
+
readonly now?: () => Date;
|
|
33
|
+
}
|
|
34
|
+
export interface InstanceMigrationApplyResult {
|
|
35
|
+
/** Timestamped copy of every original file, independent of the recovery journal. */
|
|
36
|
+
readonly backupDirectory: string;
|
|
37
|
+
}
|
|
38
|
+
/** Static, offline and side-effect-free. Does not load schemas from executable entries. */
|
|
39
|
+
export declare function planInstanceDeclarationMigration(projectRoot: string, options?: {
|
|
40
|
+
readonly lifecycleFile?: string;
|
|
41
|
+
}): Promise<InstanceDeclarationMigrationPlan>;
|
|
42
|
+
export declare function assertNoPendingInstanceMigration(root: string): Promise<void>;
|
|
43
|
+
export declare function applyInstanceDeclarationMigration(plan: InstanceDeclarationMigrationPlan, options?: InstanceMigrationApplyOptions): Promise<InstanceMigrationApplyResult>;
|
|
44
|
+
/**
|
|
45
|
+
* Human-readable, value-free preview of a migration plan. Only key names, package
|
|
46
|
+
* specifiers and instance state are shown; configuration values (including
|
|
47
|
+
* environment references) never appear, so the output is safe to paste in issues.
|
|
48
|
+
*/
|
|
49
|
+
export declare function formatInstanceMigrationPreview(plan: InstanceDeclarationMigrationPlan, options?: {
|
|
50
|
+
readonly mode?: 'dry-run' | 'apply';
|
|
51
|
+
}): string;
|
|
52
|
+
export declare function recoverInstanceDeclarationMigration(rootPath: string, action: 'continue' | 'rollback', options?: InstanceMigrationApplyOptions): Promise<void>;
|
|
53
|
+
export declare function assertStopped(root: string): Promise<void>;
|
|
54
|
+
export declare function withProjectLock<T>(root: string, operation: () => Promise<T>, recovery?: boolean): Promise<T>;
|
|
55
|
+
export {};
|