@zhin.js/gateway 0.0.1-bootstrap.0 → 1.1.2

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.
Files changed (181) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +61 -0
  3. package/lib/config/ai-config-loader.d.ts +22 -0
  4. package/lib/config/ai-config-loader.js +96 -0
  5. package/lib/config/configuration-chat-policy.d.ts +57 -0
  6. package/lib/config/configuration-chat-policy.js +423 -0
  7. package/lib/config/configuration-patch.d.ts +16 -0
  8. package/lib/config/configuration-patch.js +105 -0
  9. package/lib/config/configuration-path.d.ts +6 -0
  10. package/lib/config/configuration-path.js +40 -0
  11. package/lib/config/configuration-projection.d.ts +6 -0
  12. package/lib/config/configuration-projection.js +28 -0
  13. package/lib/config/configuration-redaction.d.ts +18 -0
  14. package/lib/config/configuration-redaction.js +75 -0
  15. package/lib/config/configuration.d.ts +81 -0
  16. package/lib/config/configuration.js +686 -0
  17. package/lib/config/console-schema.d.ts +11 -0
  18. package/lib/config/console-schema.js +223 -0
  19. package/lib/config/environment-files.d.ts +3 -0
  20. package/lib/config/environment-files.js +14 -0
  21. package/lib/config/literal-secrets.d.ts +34 -0
  22. package/lib/config/literal-secrets.js +136 -0
  23. package/lib/config/plugin-package-map.d.ts +2 -0
  24. package/lib/config/plugin-package-map.js +14 -0
  25. package/lib/config/plugin-schema-catalog.d.ts +22 -0
  26. package/lib/config/plugin-schema-catalog.js +262 -0
  27. package/lib/config.d.ts +13 -0
  28. package/lib/config.js +13 -0
  29. package/lib/database/config.d.ts +22 -0
  30. package/lib/database/config.js +159 -0
  31. package/lib/database/index.d.ts +2 -0
  32. package/lib/database/index.js +2 -0
  33. package/lib/database/project-database.d.ts +31 -0
  34. package/lib/database/project-database.js +130 -0
  35. package/lib/database.d.ts +2 -0
  36. package/lib/database.js +2 -0
  37. package/lib/http/console-rpc.d.ts +27 -0
  38. package/lib/http/console-rpc.js +51 -0
  39. package/lib/http/gateway-console.d.ts +63 -0
  40. package/lib/http/gateway-console.js +309 -0
  41. package/lib/http/gateway-events.d.ts +40 -0
  42. package/lib/http/gateway-events.js +185 -0
  43. package/lib/http/gateway-http-server.d.ts +95 -0
  44. package/lib/http/gateway-http-server.js +522 -0
  45. package/lib/http/local-principal.d.ts +2 -0
  46. package/lib/http/local-principal.js +16 -0
  47. package/lib/http/plugin-switch-rpc.d.ts +19 -0
  48. package/lib/http/plugin-switch-rpc.js +214 -0
  49. package/lib/http/run-gateway.d.ts +43 -0
  50. package/lib/http/run-gateway.js +268 -0
  51. package/lib/http/start-gateway.d.ts +114 -0
  52. package/lib/http/start-gateway.js +226 -0
  53. package/lib/http-response.d.ts +6 -0
  54. package/lib/http-response.js +25 -0
  55. package/lib/http.d.ts +6 -0
  56. package/lib/http.js +6 -0
  57. package/lib/iam/crypto.d.ts +12 -0
  58. package/lib/iam/crypto.js +58 -0
  59. package/lib/iam/index.d.ts +8 -0
  60. package/lib/iam/index.js +8 -0
  61. package/lib/iam/install.d.ts +36 -0
  62. package/lib/iam/install.js +74 -0
  63. package/lib/iam/local-admin.d.ts +27 -0
  64. package/lib/iam/local-admin.js +102 -0
  65. package/lib/iam/rate-limit.d.ts +15 -0
  66. package/lib/iam/rate-limit.js +52 -0
  67. package/lib/iam/routes.d.ts +23 -0
  68. package/lib/iam/routes.js +157 -0
  69. package/lib/iam/rpc.d.ts +14 -0
  70. package/lib/iam/rpc.js +100 -0
  71. package/lib/iam/service.d.ts +203 -0
  72. package/lib/iam/service.js +784 -0
  73. package/lib/iam/setup-service.d.ts +83 -0
  74. package/lib/iam/setup-service.js +206 -0
  75. package/lib/iam/tables.d.ts +20 -0
  76. package/lib/iam/tables.js +82 -0
  77. package/lib/iam.d.ts +2 -0
  78. package/lib/iam.js +2 -0
  79. package/lib/index.d.ts +22 -0
  80. package/lib/index.js +22 -0
  81. package/lib/local/client.d.ts +71 -0
  82. package/lib/local/client.js +296 -0
  83. package/lib/local/handler.d.ts +30 -0
  84. package/lib/local/handler.js +86 -0
  85. package/lib/local/local-socket.d.ts +55 -0
  86. package/lib/local/local-socket.js +232 -0
  87. package/lib/local/paths.d.ts +34 -0
  88. package/lib/local/paths.js +58 -0
  89. package/lib/local/protocol.d.ts +140 -0
  90. package/lib/local/protocol.js +90 -0
  91. package/lib/local/state-file.d.ts +8 -0
  92. package/lib/local/state-file.js +45 -0
  93. package/lib/local.d.ts +10 -0
  94. package/lib/local.js +10 -0
  95. package/lib/onboarding/allowlist.d.ts +54 -0
  96. package/lib/onboarding/allowlist.js +106 -0
  97. package/lib/onboarding/errors.d.ts +7 -0
  98. package/lib/onboarding/errors.js +17 -0
  99. package/lib/onboarding/index.d.ts +6 -0
  100. package/lib/onboarding/index.js +6 -0
  101. package/lib/onboarding/install.d.ts +99 -0
  102. package/lib/onboarding/install.js +132 -0
  103. package/lib/onboarding/package-manager.d.ts +47 -0
  104. package/lib/onboarding/package-manager.js +179 -0
  105. package/lib/onboarding/service.d.ts +53 -0
  106. package/lib/onboarding/service.js +144 -0
  107. package/lib/onboarding/state-store.d.ts +26 -0
  108. package/lib/onboarding/state-store.js +97 -0
  109. package/lib/onboarding.d.ts +2 -0
  110. package/lib/onboarding.js +2 -0
  111. package/lib/packages/authorization.d.ts +24 -0
  112. package/lib/packages/authorization.js +128 -0
  113. package/lib/packages/errors.d.ts +11 -0
  114. package/lib/packages/errors.js +26 -0
  115. package/lib/packages/index.d.ts +5 -0
  116. package/lib/packages/index.js +5 -0
  117. package/lib/packages/rpc.d.ts +30 -0
  118. package/lib/packages/rpc.js +160 -0
  119. package/lib/packages/service.d.ts +139 -0
  120. package/lib/packages/service.js +504 -0
  121. package/lib/packages/snapshot.d.ts +14 -0
  122. package/lib/packages/snapshot.js +73 -0
  123. package/lib/packages.d.ts +2 -0
  124. package/lib/packages.js +2 -0
  125. package/lib/plugin-management/activity.d.ts +6 -0
  126. package/lib/plugin-management/activity.js +14 -0
  127. package/lib/plugin-management/config-explanation.d.ts +6 -0
  128. package/lib/plugin-management/config-explanation.js +113 -0
  129. package/lib/plugin-management/use-cases.d.ts +15 -0
  130. package/lib/plugin-management/use-cases.js +120 -0
  131. package/lib/plugin-management/workflow.d.ts +136 -0
  132. package/lib/plugin-management/workflow.js +878 -0
  133. package/lib/plugin-management.d.ts +5 -0
  134. package/lib/plugin-management.js +5 -0
  135. package/lib/project/config-file.d.ts +6 -0
  136. package/lib/project/config-file.js +58 -0
  137. package/lib/project/environment.d.ts +13 -0
  138. package/lib/project/environment.js +52 -0
  139. package/lib/project/instance-declarations.d.ts +55 -0
  140. package/lib/project/instance-declarations.js +542 -0
  141. package/lib/project/legacy-config.d.ts +25 -0
  142. package/lib/project/legacy-config.js +70 -0
  143. package/lib/project/node-requirements.d.ts +7 -0
  144. package/lib/project/node-requirements.js +20 -0
  145. package/lib/project/tsx-module-loader.d.ts +7 -0
  146. package/lib/project/tsx-module-loader.js +29 -0
  147. package/lib/project.d.ts +7 -0
  148. package/lib/project.js +7 -0
  149. package/lib/server.d.ts +11 -0
  150. package/lib/server.js +11 -0
  151. package/lib/supervisor/instance-logs.d.ts +39 -0
  152. package/lib/supervisor/instance-logs.js +152 -0
  153. package/lib/supervisor/instance-supervisor.d.ts +140 -0
  154. package/lib/supervisor/instance-supervisor.js +408 -0
  155. package/lib/supervisor/run-instance.d.ts +35 -0
  156. package/lib/supervisor/run-instance.js +161 -0
  157. package/lib/supervisor/terminal-state.d.ts +30 -0
  158. package/lib/supervisor/terminal-state.js +57 -0
  159. package/lib/supervisor.d.ts +4 -0
  160. package/lib/supervisor.js +4 -0
  161. package/lib/terminal/backends.d.ts +38 -0
  162. package/lib/terminal/backends.js +193 -0
  163. package/lib/terminal/config.d.ts +63 -0
  164. package/lib/terminal/config.js +237 -0
  165. package/lib/terminal/host.d.ts +26 -0
  166. package/lib/terminal/host.js +76 -0
  167. package/lib/terminal/index.d.ts +60 -0
  168. package/lib/terminal/index.js +135 -0
  169. package/lib/terminal/modules.d.ts +87 -0
  170. package/lib/terminal/modules.js +44 -0
  171. package/lib/terminal/service.d.ts +101 -0
  172. package/lib/terminal/service.js +455 -0
  173. package/lib/terminal/tickets.d.ts +46 -0
  174. package/lib/terminal/tickets.js +76 -0
  175. package/lib/terminal.d.ts +3 -0
  176. package/lib/terminal.js +3 -0
  177. package/lib/usage/aggregate.d.ts +87 -0
  178. package/lib/usage/aggregate.js +212 -0
  179. package/lib/usage.d.ts +2 -0
  180. package/lib/usage.js +2 -0
  181. package/package.json +117 -4
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Masks stored configuration values before they leave the store in a response that the
3
+ * caller did not ask to read (e.g. a trusted project patch conflict). Conservative: a
4
+ * schema secret/writeOnly/password/hidden annotation, a secret-like key, or an unexpanded
5
+ * environment reference (`${…}`, `env://…`) is replaced as a whole and never echoed.
6
+ */
7
+ import { UNRESOLVED_CONFIG_SCHEMA, configSchemaChildren, expandConfigSchema, isConfigEnvironmentReference, isSecretConfigKey, isSecretConfigSchema, } from '@zhin.js/runtime';
8
+ const unsupportedApplicators = Object.freeze([
9
+ 'if', 'then', 'else', 'not', 'contains', 'dependencies', 'dependentSchemas',
10
+ '$dynamicRef', '$recursiveRef', '$dynamicAnchor', 'unevaluatedProperties', 'unevaluatedItems',
11
+ ]);
12
+ /** Shared proof limit: these branches/resources need a resolver we do not provide. */
13
+ export function isUnknownConfigurationSchemaBoundary(schema, root) {
14
+ return schema !== root && Object.hasOwn(schema, '$id')
15
+ || unsupportedApplicators.some(keyword => Object.hasOwn(schema, keyword))
16
+ || schema.$ref !== undefined && (typeof schema.$ref !== 'string' || !schema.$ref.startsWith('#/') || /~(?![01])/u.test(schema.$ref));
17
+ }
18
+ /** Credential names, excluding ordinary budgets such as maxTokens and tokenLimit (the ADR 0057 predicate). */
19
+ export const sensitiveConfigKey = isSecretConfigKey;
20
+ export const REDACTED_CONFIG_VALUE = '[redacted]';
21
+ const MAX_DEPTH = 32;
22
+ // Retain uncertainty without changing the non-strict masker's best-effort behavior.
23
+ const unresolvedSchema = UNRESOLVED_CONFIG_SCHEMA;
24
+ const expand = expandConfigSchema;
25
+ const children = configSchemaChildren;
26
+ function record(value) {
27
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
28
+ }
29
+ function sensitiveSchema(schema) {
30
+ const metadata = schema['x-zhin-config'];
31
+ // `hidden` mirrors the configuration explanation contract: hidden fields are never echoed.
32
+ return isSecretConfigSchema(schema) || schema.hidden === true
33
+ || record(metadata) && metadata.hidden === true;
34
+ }
35
+ function maskTree(value, nodes, root, depth, hidden) {
36
+ if (nodes.some(hidden))
37
+ return REDACTED_CONFIG_VALUE;
38
+ if (typeof value === 'string')
39
+ return isConfigEnvironmentReference(value) ? REDACTED_CONFIG_VALUE : value;
40
+ if (value === null || typeof value !== 'object')
41
+ return value;
42
+ if (depth >= MAX_DEPTH)
43
+ return REDACTED_CONFIG_VALUE;
44
+ if (Array.isArray(value)) {
45
+ return Object.freeze(value.map((item, index) => maskTree(item, children(nodes, String(index), root), root, depth + 1, hidden)));
46
+ }
47
+ return Object.freeze(Object.fromEntries(Object.entries(value).map(([key, item]) => [key,
48
+ sensitiveConfigKey(key) ? REDACTED_CONFIG_VALUE : maskTree(item, children(nodes, key, root), root, depth + 1, hidden)])));
49
+ }
50
+ /**
51
+ * Masks `value`, stored at `path` (first segment is the section) under `sectionSchema`.
52
+ * A missing schema still applies key and environment-reference masking.
53
+ * `unknownSchema: 'redact'` (project reads and conflict evidence) also replaces any value under a schema
54
+ * boundary this masker cannot prove (`if`/`not`/`contains`/…, nested `$id`, non-local
55
+ * `$ref`; the chat policy's {@link isUnknownConfigurationSchemaBoundary}), since a secret
56
+ * annotation could hide behind it.
57
+ */
58
+ export function maskConfigValue(sectionSchema, path, value, options = {}) {
59
+ const root = record(sectionSchema) ? sectionSchema : {};
60
+ const hidden = options.unknownSchema === 'redact'
61
+ ? node => node === unresolvedSchema || sensitiveSchema(node) || isUnknownConfigurationSchemaBoundary(node, root) : sensitiveSchema;
62
+ let nodes = record(sectionSchema) ? expand(sectionSchema, root) : [];
63
+ if (path.length > 0 && sensitiveConfigKey(path[0]))
64
+ return REDACTED_CONFIG_VALUE;
65
+ if (nodes.some(hidden))
66
+ return REDACTED_CONFIG_VALUE;
67
+ for (const key of path.slice(1)) {
68
+ if (sensitiveConfigKey(key))
69
+ return REDACTED_CONFIG_VALUE;
70
+ nodes = children(nodes, key, root);
71
+ if (nodes.some(hidden))
72
+ return REDACTED_CONFIG_VALUE;
73
+ }
74
+ return maskTree(value, nodes, root, 0, hidden);
75
+ }
@@ -0,0 +1,81 @@
1
+ import { type ConsoleConfigApplication, type ConsoleConfigApplicationStatus, type ConsoleConfigApplyResult, type ConsoleConfigPatchRequest, type ConsoleConfigPatchResult, type ConsoleConfigProjectPatchResult, type ConsoleConfigProjectRead, type ConsoleConfigProjectReadRequest, type ConsoleConfigSource } from '@zhin.js/console-protocol';
2
+ import type { PluginConfigValidation } from '@zhin.js/host-http';
3
+ import { type RuntimeConfigDocument } from '@zhin.js/plugin-runtime';
4
+ import { type ConfigFileDocument, type EnvStore } from '@zhin.js/runtime';
5
+ export interface ChatConfigurationField {
6
+ readonly path: readonly string[];
7
+ readonly type: readonly string[];
8
+ }
9
+ export interface ChatConfigurationRead {
10
+ readonly revision: string;
11
+ /** `get`: the single leaf value (already proven non-sensitive). */
12
+ readonly value?: unknown;
13
+ /** `fields`: chat-manageable leaves under the scope; never values. */
14
+ readonly fields?: readonly ChatConfigurationField[];
15
+ }
16
+ /** A Host-classified rejection before this operation attempted any commit. */
17
+ export declare class ConfigChatRequestRejectedError extends Error {
18
+ readonly code = "configuration_request_rejected";
19
+ constructor();
20
+ }
21
+ /** Owns one project's Console configuration projection over the Root config authority. */
22
+ export declare class ConsoleConfigurationStore {
23
+ #private;
24
+ constructor(options: {
25
+ readonly projectRoot: string;
26
+ readonly document: ConfigFileDocument;
27
+ readonly configDeclarations?: () => ReadonlyMap<string, Readonly<Record<string, unknown>>> | Promise<ReadonlyMap<string, Readonly<Record<string, unknown>>>>;
28
+ readonly environmentName?: string | (() => string);
29
+ readonly environmentForPlugin?: (pluginName: string) => Pick<EnvStore, 'has'> | undefined;
30
+ readonly applyConfiguration?: (expectedRevision?: string) => Promise<ConsoleConfigApplication>;
31
+ readonly readApplication?: () => ConsoleConfigApplication | undefined;
32
+ readonly validateConfiguration?: (document: RuntimeConfigDocument) => Promise<void>;
33
+ });
34
+ readSource(): Promise<ConsoleConfigSource>;
35
+ readDocument(): Promise<Record<string, unknown>>;
36
+ replaceSource(source: string, expectedRevision: string): Promise<{
37
+ readonly revision: string;
38
+ readonly restartRequired: boolean;
39
+ } & ConsoleConfigApplicationStatus>;
40
+ setKey(pluginName: string, data: unknown): Promise<{
41
+ restartRequired: boolean;
42
+ } & ConsoleConfigApplicationStatus>;
43
+ removeKey(pluginName: string): Promise<{
44
+ restartRequired: boolean;
45
+ } & ConsoleConfigApplicationStatus>;
46
+ /**
47
+ * Scoped, revision-checked path writes (`config:patch`). Paths outside the caller's
48
+ * allowlist are rejected before reading values; a stale revision returns the latest
49
+ * values instead of overwriting; every touched section is validated by its schema.
50
+ */
51
+ patch(request: ConsoleConfigPatchRequest): Promise<ConsoleConfigPatchResult>;
52
+ /**
53
+ * Trusted project administration (`config:patch-project`, full scope only). Same
54
+ * single-CAS core as {@link patch}, without the page allowlist; it does not widen
55
+ * `config:patch` / `HOST_CONFIG_PATCH_PATHS`. Declaration writes are rejected as
56
+ * `invalid`, conflict values are masked, and a thrown commit is `unconfirmed`.
57
+ */
58
+ patchProject(request: Omit<ConsoleConfigPatchRequest, 'instanceKey'>): Promise<ConsoleConfigProjectPatchResult>;
59
+ /** Host-authenticated owner chat. Policy checks share the mutation's CAS boundary. */
60
+ patchChat(request: Omit<ConsoleConfigPatchRequest, 'instanceKey'>): Promise<ConsoleConfigPatchResult>;
61
+ /**
62
+ * Trusted project read (`config:get-project`, full scope only). Values are masked by
63
+ * section schema, secret-like keys and unexpanded environment references unless the
64
+ * caller explicitly asks to `reveal`; a section whose schema cannot be read is masked whole.
65
+ */
66
+ readProject(request?: ConsoleConfigProjectReadRequest): Promise<ConsoleConfigProjectRead>;
67
+ /**
68
+ * Host-authenticated owner chat reads (C1). `get` returns one proven-safe leaf;
69
+ * `fields` lists manageable leaves and their types without values. Denials carry
70
+ * only a reason code.
71
+ */
72
+ readChat(path: readonly string[], mode: 'get' | 'fields'): Promise<ChatConfigurationRead>;
73
+ apply(expectedRevision: string): Promise<ConsoleConfigApplyResult>;
74
+ readEnvironmentFile(filename: string): Promise<string>;
75
+ writeEnvironmentFile(filename: string, content: string): Promise<void>;
76
+ readSchema(pluginName?: string): Promise<unknown>;
77
+ validatePluginConfig(pluginName: string, data: unknown, capturedDocument?: RuntimeConfigDocument): Promise<PluginConfigValidation>;
78
+ readAllSchemas(): Promise<Record<string, unknown>>;
79
+ listKeys(): Promise<string[]>;
80
+ }
81
+ export { configKeyPatch, configKeyRemovePatch, flattenConfigDocument } from './configuration-projection.js';