@k9o/extension-manifest 0.0.0-stage → 1.6.0
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 +201 -0
- package/NOTICE +4 -0
- package/README.md +134 -2
- package/package.json +21 -4
- package/src/builder.ts +376 -0
- package/src/console-origin.ts +129 -0
- package/src/host-theme.ts +310 -0
- package/src/iframe-bridge.ts +270 -0
- package/src/index.ts +226 -0
- package/src/init-protocol.ts +76 -0
- package/src/manifest.ts +1643 -0
- package/src/permissions.ts +356 -0
package/src/builder.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manifest authoring API
|
|
3
|
+
*
|
|
4
|
+
* `defineManifest` is the typed entry point for extension authors. It accepts
|
|
5
|
+
* a camelCase author input shape (SDK casing rules) and emits
|
|
6
|
+
* the snake_case wire shape the validator + platform consume:
|
|
7
|
+
*
|
|
8
|
+
* - `surfaces.runtimeApi` → `surfaces.runtime_api` (no `prefix`: the mount is
|
|
9
|
+
* fixed at `…/extensions/<slug>/api/`)
|
|
10
|
+
* - `surfaces.backgroundLogic` → `surfaces.background_logic`
|
|
11
|
+
* - `hooks.<event>` accepts a bare URL string (e.g. `"POST /hooks/enable"`)
|
|
12
|
+
* and the builder wraps it into the wire-shape `{ url }`
|
|
13
|
+
* - `manifest_version: 1` is set automatically (v2-launch enum-of-one)
|
|
14
|
+
* - `publisher` carries `{ name, url }` only: "verified" is derived by the platform from the
|
|
15
|
+
* signed catalogue release, never authored
|
|
16
|
+
*
|
|
17
|
+
* The builder does no semantic validation; that's the manifest validator's
|
|
18
|
+
* job (`@k9o/extension-platform.validateManifest`). The builder gives
|
|
19
|
+
* authors typed ergonomics; validation happens once, at the platform
|
|
20
|
+
* boundary, against the binding + rules.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
MANIFEST_VERSION_V2_LAUNCH,
|
|
25
|
+
type AttributionDeclaration,
|
|
26
|
+
type BackgroundLogicSurface,
|
|
27
|
+
type Compatibility,
|
|
28
|
+
type ConsoleSurface,
|
|
29
|
+
type PublicSurface,
|
|
30
|
+
type CredentialRequirement,
|
|
31
|
+
type DependenciesDeclaration,
|
|
32
|
+
type ExternalEndpointAuthKind,
|
|
33
|
+
type ExternalEndpointDeclaration,
|
|
34
|
+
type ScheduleDeclaration,
|
|
35
|
+
type ExternalEndpointMethod,
|
|
36
|
+
type ExternalEndpointMinRole,
|
|
37
|
+
type ExternalEndpointPublicCapability,
|
|
38
|
+
type FeatureFlagDeclaration,
|
|
39
|
+
type HookEvent,
|
|
40
|
+
type HooksDeclaration,
|
|
41
|
+
type ListingDeclaration,
|
|
42
|
+
type LlmDeclaration,
|
|
43
|
+
type Manifest,
|
|
44
|
+
type Permission,
|
|
45
|
+
type PricingDeclaration,
|
|
46
|
+
type RevealRequestDeclaration,
|
|
47
|
+
type RuntimeApiSurface,
|
|
48
|
+
type SchemaDeclaration,
|
|
49
|
+
type Surfaces,
|
|
50
|
+
type WebhookIngressDeclaration,
|
|
51
|
+
type WebhookIngressScheme,
|
|
52
|
+
type AnalyticsReadSurface,
|
|
53
|
+
type AnalyticsWriteSurface,
|
|
54
|
+
type EventBridgeSurface,
|
|
55
|
+
type PersonalDataDeclaration,
|
|
56
|
+
type DependsOnEntry,
|
|
57
|
+
type InstallConfigEntry,
|
|
58
|
+
type WorkflowTemplateEntry,
|
|
59
|
+
type EventSubscriptionEntry,
|
|
60
|
+
type ErasureHookDeclaration,
|
|
61
|
+
} from "./manifest.ts";
|
|
62
|
+
|
|
63
|
+
/** runtime_api author input — there is no `prefix`; the mount is fixed. */
|
|
64
|
+
export type RuntimeApiAuthor = {
|
|
65
|
+
/** Required only when `externalEndpoints` is non-empty. */
|
|
66
|
+
spec?: string;
|
|
67
|
+
/** Name of the `backgroundLogic.functions[]` entry hosting `k9o.bff.dispatch`; see `RuntimeApiSurface.function`. */
|
|
68
|
+
function?: string;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Console surface author shape. `iframe_origin` was retired and hard-dropped (2026-05-29):
|
|
73
|
+
* iframes load from the CDN class subdomain derived from `static_files` (default
|
|
74
|
+
* `"static"`) + slug — no per-extension origin field. Console authors supply
|
|
75
|
+
* only `routes` + `bundle`.
|
|
76
|
+
*/
|
|
77
|
+
export type ConsoleSurfaceAuthor = ConsoleSurface;
|
|
78
|
+
|
|
79
|
+
export type SurfacesAuthor = {
|
|
80
|
+
/** Bridge surfaces: wire-shaped already, passed through. */
|
|
81
|
+
event_bridge?: EventBridgeSurface | null;
|
|
82
|
+
analytics_write?: AnalyticsWriteSurface | null;
|
|
83
|
+
analytics_read?: AnalyticsReadSurface | null;
|
|
84
|
+
console?: ConsoleSurfaceAuthor | null;
|
|
85
|
+
runtimeApi?: RuntimeApiAuthor | null;
|
|
86
|
+
backgroundLogic?: BackgroundLogicSurface | null;
|
|
87
|
+
/** Public-page surface (P2); wire-identical, direct passthrough. */
|
|
88
|
+
public?: PublicSurface;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/** Hook events map directly to a URL string; the builder wraps to `{ url }`. */
|
|
92
|
+
export type HooksAuthor = Partial<Record<HookEvent, string>>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Author shape for `external_endpoints[]` entries.
|
|
96
|
+
* Mirrors the wire shape with camelCase `versionAttribute`; the builder maps to
|
|
97
|
+
* wire `version_attribute`.
|
|
98
|
+
*/
|
|
99
|
+
export type ExternalEndpointAuthor = {
|
|
100
|
+
path: string;
|
|
101
|
+
method: ExternalEndpointMethod;
|
|
102
|
+
auth: ExternalEndpointAuthKind;
|
|
103
|
+
versionAttribute?: number;
|
|
104
|
+
/** Required on `operator-jwt` endpoints. */
|
|
105
|
+
minRole?: ExternalEndpointMinRole;
|
|
106
|
+
/** Required on (and only valid on) `anonymous` endpoints. */
|
|
107
|
+
publicCapability?: ExternalEndpointPublicCapability;
|
|
108
|
+
/** Only valid on `extension` endpoints: the extension slugs admitted as callers. */
|
|
109
|
+
allowedCallers?: readonly string[];
|
|
110
|
+
description: string;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/** Author shape for `webhookIngress[]` entries: identical to the wire shape. */
|
|
114
|
+
export type WebhookIngressAuthor = WebhookIngressDeclaration;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Compatibility author shape. camelCase author keys
|
|
118
|
+
* map to wire snake_case (`tokensetVersionMin` → `tokenset_version_min`;
|
|
119
|
+
* `shadcnVersionMin` → `shadcn_version_min`).
|
|
120
|
+
*/
|
|
121
|
+
export type CompatibilityAuthor = {
|
|
122
|
+
tokensetVersionMin?: string;
|
|
123
|
+
shadcnVersionMin?: string;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export type ManifestAuthor = {
|
|
127
|
+
slug: string;
|
|
128
|
+
name: string;
|
|
129
|
+
version: string;
|
|
130
|
+
publisher: { name: string; url: string };
|
|
131
|
+
description: string;
|
|
132
|
+
permissions: readonly Permission[];
|
|
133
|
+
surfaces: SurfacesAuthor;
|
|
134
|
+
/**
|
|
135
|
+
* Static-files directory name (default `"static"`).
|
|
136
|
+
* Platform CDN-publish reads `<extension>/<staticFiles>/` and uploads to
|
|
137
|
+
* `<orgSlug>.cdn.k9o.io/@<projectSlug>/extensions/<slug>/...`. Iframe
|
|
138
|
+
* bundles convention: `static/iframe/<surface>/index.html`. Builder
|
|
139
|
+
* passes through to wire `static_files`.
|
|
140
|
+
*/
|
|
141
|
+
staticFiles?: string;
|
|
142
|
+
/** Vendor credentials this extension needs: role and auth shape only. */
|
|
143
|
+
requires?: readonly CredentialRequirement[];
|
|
144
|
+
schema?: SchemaDeclaration;
|
|
145
|
+
hooks?: HooksAuthor;
|
|
146
|
+
/**
|
|
147
|
+
* The 6 additive declarations. The builder accepts these inline so authors
|
|
148
|
+
* don't need a post-build spread to attach them. All optional; the validator
|
|
149
|
+
* enforces required-when-present rules.
|
|
150
|
+
*/
|
|
151
|
+
llm?: LlmDeclaration;
|
|
152
|
+
pricing?: PricingDeclaration;
|
|
153
|
+
attribution?: AttributionDeclaration;
|
|
154
|
+
listing?: ListingDeclaration;
|
|
155
|
+
revealRequest?: RevealRequestDeclaration;
|
|
156
|
+
dependencies?: DependenciesDeclaration;
|
|
157
|
+
/**
|
|
158
|
+
* BFF endpoints. The builder accepts them inline so authors don't need a
|
|
159
|
+
* post-build spread. Validator enforces shape + cap (`EXTERNAL_ENDPOINTS_MAX_PER_EXTENSION`)
|
|
160
|
+
* + path-prefix rules.
|
|
161
|
+
*/
|
|
162
|
+
externalEndpoints?: readonly ExternalEndpointAuthor[];
|
|
163
|
+
/** Schedules the platform runs; same shape on the wire. Each names an `auth: "schedule"` endpoint. */
|
|
164
|
+
schedules?: readonly ScheduleDeclaration[];
|
|
165
|
+
/**
|
|
166
|
+
* Webhook ingress declarations. Builder accepts inline; maps camelCase
|
|
167
|
+
* `secretRef` → wire snake_case `secret_ref`. Retires the older
|
|
168
|
+
* `ManifestWithWebhookIngress` intersection-cast bridge pattern.
|
|
169
|
+
*/
|
|
170
|
+
webhookIngress?: readonly WebhookIngressAuthor[];
|
|
171
|
+
/**
|
|
172
|
+
* Feature-flag declarations. Builder accepts inline; wire shape is camelCase-already-
|
|
173
|
+
* matching-snake_case (single-word `key + default + description + scope`).
|
|
174
|
+
*/
|
|
175
|
+
featureFlags?: readonly FeatureFlagDeclaration[];
|
|
176
|
+
/**
|
|
177
|
+
* Collections the extension writes only by proposal. Author `checkerRole` maps to wire
|
|
178
|
+
* `checker_role`: the project IAM role, by name, that must approve a proposal.
|
|
179
|
+
*/
|
|
180
|
+
proposeOnly?: readonly { collection: string; checkerRole: string }[];
|
|
181
|
+
/** Personal-data declarations; the wire shape is the same snake_case shape, so entries pass through. */
|
|
182
|
+
personalData?: readonly PersonalDataDeclaration[];
|
|
183
|
+
/** Install settings; the wire shape is the same snake_case shape, so entries pass through (copied). */
|
|
184
|
+
/** Extensions this one needs installed and enabled; wire-shaped entries `{ slug, version_range, reason }`. */
|
|
185
|
+
dependsOn?: readonly DependsOnEntry[];
|
|
186
|
+
/** Ready-made workflows a workflow-template extension ships; wire-shaped entries, copied through verbatim (inner keys stay camelCase). */
|
|
187
|
+
workflowTemplates?: readonly WorkflowTemplateEntry[];
|
|
188
|
+
installConfig?: readonly InstallConfigEntry[];
|
|
189
|
+
/** Event subscriptions; same wire shape, entries pass through (copied). Each `invokes` names an `auth: "event"` endpoint. */
|
|
190
|
+
eventSubscriptions?: readonly EventSubscriptionEntry[];
|
|
191
|
+
/** The subject-erasure hook; same wire shape. `invokes` names an `auth: "erasure"` endpoint. */
|
|
192
|
+
erasureHook?: ErasureHookDeclaration;
|
|
193
|
+
/**
|
|
194
|
+
* Compatibility declarations (tokenset versioning + shadcn version pin).
|
|
195
|
+
* Builder maps camelCase
|
|
196
|
+
* `tokensetVersionMin` → wire `tokenset_version_min`; `shadcnVersionMin`
|
|
197
|
+
* → `shadcn_version_min`.
|
|
198
|
+
*/
|
|
199
|
+
compatibility?: CompatibilityAuthor;
|
|
200
|
+
/**
|
|
201
|
+
* Extension-owned custom CSS-variable tokens. Keys MUST be
|
|
202
|
+
* `--ext-<own-slug>-*` form (the validator regex enforces). Wire shape is camelCase-already-matching-snake_case (single-
|
|
203
|
+
* word `customTokens` → `custom_tokens`); direct passthrough of the
|
|
204
|
+
* `Record<string, string>` value.
|
|
205
|
+
*/
|
|
206
|
+
customTokens?: Readonly<Record<string, string>>;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
export function defineManifest(input: ManifestAuthor): Manifest {
|
|
210
|
+
const runtimeApi: RuntimeApiSurface | null =
|
|
211
|
+
input.surfaces.runtimeApi
|
|
212
|
+
? {
|
|
213
|
+
...(input.surfaces.runtimeApi.spec !== undefined
|
|
214
|
+
? { spec: input.surfaces.runtimeApi.spec }
|
|
215
|
+
: {}),
|
|
216
|
+
...(input.surfaces.runtimeApi.function !== undefined
|
|
217
|
+
? { function: input.surfaces.runtimeApi.function }
|
|
218
|
+
: {}),
|
|
219
|
+
}
|
|
220
|
+
: null;
|
|
221
|
+
|
|
222
|
+
// Console wire surface carries routes + bundle. `iframe_origin` hard-dropped
|
|
223
|
+
// (2026-05-29) — iframes load from CDN class subdomain derived
|
|
224
|
+
// from `static_files` (default `"static"`) + slug.
|
|
225
|
+
const consoleSurface: ConsoleSurface | null = input.surfaces.console ?? null;
|
|
226
|
+
|
|
227
|
+
const surfaces: Surfaces = {
|
|
228
|
+
console: consoleSurface,
|
|
229
|
+
runtime_api: runtimeApi,
|
|
230
|
+
background_logic: input.surfaces.backgroundLogic ?? null,
|
|
231
|
+
...(input.surfaces.public !== undefined
|
|
232
|
+
? { public: { ...input.surfaces.public } }
|
|
233
|
+
: {}),
|
|
234
|
+
...(input.surfaces.event_bridge != null ? { event_bridge: { ...input.surfaces.event_bridge } } : {}),
|
|
235
|
+
...(input.surfaces.analytics_write != null ? { analytics_write: { ...input.surfaces.analytics_write } } : {}),
|
|
236
|
+
...(input.surfaces.analytics_read != null ? { analytics_read: { ...input.surfaces.analytics_read } } : {}),
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
let hooks: HooksDeclaration | undefined;
|
|
240
|
+
if (input.hooks !== undefined) {
|
|
241
|
+
const out: { -readonly [K in HookEvent]?: { url: string } } = {};
|
|
242
|
+
for (const [event, url] of Object.entries(input.hooks)) {
|
|
243
|
+
if (typeof url === "string") {
|
|
244
|
+
out[event as HookEvent] = { url };
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
hooks = out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
manifest_version: MANIFEST_VERSION_V2_LAUNCH,
|
|
252
|
+
slug: input.slug,
|
|
253
|
+
name: input.name,
|
|
254
|
+
version: input.version,
|
|
255
|
+
publisher: {
|
|
256
|
+
name: input.publisher.name,
|
|
257
|
+
url: input.publisher.url,
|
|
258
|
+
},
|
|
259
|
+
description: input.description,
|
|
260
|
+
permissions: input.permissions,
|
|
261
|
+
surfaces,
|
|
262
|
+
...(input.staticFiles !== undefined
|
|
263
|
+
? { static_files: input.staticFiles }
|
|
264
|
+
: {}),
|
|
265
|
+
...(input.requires !== undefined ? { requires: input.requires } : {}),
|
|
266
|
+
...(input.schema !== undefined ? { schema: input.schema } : {}),
|
|
267
|
+
...(hooks !== undefined ? { hooks } : {}),
|
|
268
|
+
...(input.llm !== undefined ? { llm: input.llm } : {}),
|
|
269
|
+
...(input.pricing !== undefined ? { pricing: input.pricing } : {}),
|
|
270
|
+
...(input.attribution !== undefined ? { attribution: input.attribution } : {}),
|
|
271
|
+
...(input.listing !== undefined ? { listing: input.listing } : {}),
|
|
272
|
+
...(input.revealRequest !== undefined
|
|
273
|
+
? { reveal_request: input.revealRequest }
|
|
274
|
+
: {}),
|
|
275
|
+
...(input.dependencies !== undefined
|
|
276
|
+
? { dependencies: input.dependencies }
|
|
277
|
+
: {}),
|
|
278
|
+
// BFF endpoints. Author
|
|
279
|
+
// camelCase `externalEndpoints[].{versionAttribute,minRole,publicCapability,allowedCallers}` map to wire snake_case
|
|
280
|
+
// `external_endpoints[].{version_attribute,min_role,public_capability,allowed_callers}`.
|
|
281
|
+
...(input.externalEndpoints !== undefined
|
|
282
|
+
? {
|
|
283
|
+
external_endpoints: input.externalEndpoints.map(
|
|
284
|
+
(e): ExternalEndpointDeclaration => ({
|
|
285
|
+
path: e.path,
|
|
286
|
+
method: e.method,
|
|
287
|
+
auth: e.auth,
|
|
288
|
+
...(e.versionAttribute !== undefined
|
|
289
|
+
? { version_attribute: e.versionAttribute }
|
|
290
|
+
: {}),
|
|
291
|
+
...(e.minRole !== undefined ? { min_role: e.minRole } : {}),
|
|
292
|
+
...(e.publicCapability !== undefined
|
|
293
|
+
? { public_capability: e.publicCapability }
|
|
294
|
+
: {}),
|
|
295
|
+
...(e.allowedCallers !== undefined ? { allowed_callers: [...e.allowedCallers] } : {}),
|
|
296
|
+
description: e.description,
|
|
297
|
+
}),
|
|
298
|
+
),
|
|
299
|
+
}
|
|
300
|
+
: {}),
|
|
301
|
+
...(input.schedules !== undefined ? { schedules: input.schedules.map((s) => ({ ...s })) } : {}),
|
|
302
|
+
// Webhook ingress: author and wire shapes are the same camelCase shape.
|
|
303
|
+
...(input.webhookIngress !== undefined
|
|
304
|
+
? { webhookIngress: input.webhookIngress.map((w) => ({ ...w })) }
|
|
305
|
+
: {}),
|
|
306
|
+
// Feature-flag declarations. Wire shape is camelCase-already-matching-snake_case (all
|
|
307
|
+
// single-word fields); direct passthrough.
|
|
308
|
+
...(input.featureFlags !== undefined
|
|
309
|
+
? { feature_flags: input.featureFlags }
|
|
310
|
+
: {}),
|
|
311
|
+
...(input.proposeOnly !== undefined
|
|
312
|
+
? { propose_only: input.proposeOnly.map((d) => ({ collection: d.collection, checker_role: d.checkerRole })) }
|
|
313
|
+
: {}),
|
|
314
|
+
...(input.personalData !== undefined
|
|
315
|
+
? { personal_data: input.personalData.map((d) => ({ ...d, ...(d.columns ? { columns: d.columns.map((c) => ({ ...c })) } : {}) })) }
|
|
316
|
+
: {}),
|
|
317
|
+
...(input.dependsOn !== undefined ? { depends_on: input.dependsOn.map((d) => structuredClone(d)) } : {}),
|
|
318
|
+
...(input.workflowTemplates !== undefined ? { workflow_templates: input.workflowTemplates.map((t) => structuredClone(t)) } : {}),
|
|
319
|
+
...(input.installConfig !== undefined ? { install_config: input.installConfig.map((d) => structuredClone(d)) } : {}),
|
|
320
|
+
...(input.eventSubscriptions !== undefined ? { event_subscriptions: input.eventSubscriptions.map((d) => structuredClone(d)) } : {}),
|
|
321
|
+
...(input.erasureHook !== undefined ? { erasure_hook: { ...input.erasureHook } } : {}),
|
|
322
|
+
// Design-system declarations. Author camelCase `compatibility.tokensetVersionMin` / `shadcnVersionMin`
|
|
323
|
+
// map to wire snake_case `compatibility.tokenset_version_min` /
|
|
324
|
+
// `shadcn_version_min`. `customTokens` is single-word camelCase-already-
|
|
325
|
+
// matching-snake_case; direct passthrough.
|
|
326
|
+
...(input.compatibility !== undefined
|
|
327
|
+
? {
|
|
328
|
+
compatibility: ((): Compatibility => {
|
|
329
|
+
const c: { -readonly [K in keyof Compatibility]?: Compatibility[K] } = {};
|
|
330
|
+
if (input.compatibility.tokensetVersionMin !== undefined) {
|
|
331
|
+
c.tokenset_version_min = input.compatibility.tokensetVersionMin;
|
|
332
|
+
}
|
|
333
|
+
if (input.compatibility.shadcnVersionMin !== undefined) {
|
|
334
|
+
c.shadcn_version_min = input.compatibility.shadcnVersionMin;
|
|
335
|
+
}
|
|
336
|
+
return c;
|
|
337
|
+
})(),
|
|
338
|
+
}
|
|
339
|
+
: {}),
|
|
340
|
+
...(input.customTokens !== undefined
|
|
341
|
+
? { custom_tokens: input.customTokens }
|
|
342
|
+
: {}),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Ergonomic helper for declaring `feature_flags[]`.
|
|
348
|
+
*
|
|
349
|
+
* The const-assertion (`as const`) preserves the exact tuple type so
|
|
350
|
+
* downstream consumers can do type-level extraction of declared flag keys
|
|
351
|
+
* (e.g. `(typeof flags)[number]["key"]`), enabling typed
|
|
352
|
+
* `config.featureFlags.<exact-key>` access surfaces in future SDK runtime
|
|
353
|
+
* helpers.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* import { defineFeatureFlags, defineManifest } from "@k9o/extension-manifest";
|
|
357
|
+
*
|
|
358
|
+
* const flags = defineFeatureFlags([
|
|
359
|
+
* { key: "enable_streaming", default: true, description: "Enable streaming responses", scope: "project" },
|
|
360
|
+
* { key: "experimental_inference_v2", default: false, description: "Experimental v2 inference path", scope: "extension", v2xForwardBound: true },
|
|
361
|
+
* ]);
|
|
362
|
+
*
|
|
363
|
+
* export default defineManifest({
|
|
364
|
+
* // ...
|
|
365
|
+
* featureFlags: flags,
|
|
366
|
+
* });
|
|
367
|
+
*
|
|
368
|
+
* // Type-level extraction (forward-compatible with v2.x typed config surface):
|
|
369
|
+
* type FlagKeys = (typeof flags)[number]["key"];
|
|
370
|
+
* // ^? "enable_streaming" | "experimental_inference_v2"
|
|
371
|
+
*/
|
|
372
|
+
export function defineFeatureFlags<
|
|
373
|
+
const T extends readonly FeatureFlagDeclaration[],
|
|
374
|
+
>(flags: T): T {
|
|
375
|
+
return flags;
|
|
376
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Console-origin resolution for extension iframes.
|
|
3
|
+
*
|
|
4
|
+
* An extension bundle is built once and served into whichever Console embeds it
|
|
5
|
+
* (any deployment domain: SaaS, an air-gapped internal apex, localhost). It
|
|
6
|
+
* therefore cannot bake a Console domain in, and this module carries NO list or
|
|
7
|
+
* pattern of domain names.
|
|
8
|
+
*
|
|
9
|
+
* Why that is safe: the gateway serves each extension iframe with the CSP
|
|
10
|
+
* `frame-ancestors <the install's own org Console origin>`, so the browser
|
|
11
|
+
* refuses to render the iframe in any other parent. The iframe can trust the
|
|
12
|
+
* origin of its actual parent window.
|
|
13
|
+
*
|
|
14
|
+
* Rule (when the caller passes no explicit `consoleOrigin`):
|
|
15
|
+
* 1. `location.ancestorOrigins[0]` (the embedding window) pins the origin.
|
|
16
|
+
* 2. Otherwise the origin of the first `message` event whose
|
|
17
|
+
* `event.source === window.parent` and which carries the host handshake
|
|
18
|
+
* (`k9o:host:context`) pins it.
|
|
19
|
+
* 3. Until one of those happens nothing is sent (never a `"*"` target) and
|
|
20
|
+
* nothing is accepted.
|
|
21
|
+
* Once pinned the origin never changes. Messages are accepted only from the
|
|
22
|
+
* pinned origin and from `window.parent`.
|
|
23
|
+
*
|
|
24
|
+
* Scheme rule: `https:` for every origin; `http:` only for loopback hosts
|
|
25
|
+
* (`localhost`, `127.0.0.1`, `[::1]`) for local development.
|
|
26
|
+
*
|
|
27
|
+
* An explicit `consoleOrigin` keeps exact-equality behaviour.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** Host handshake message type; the first one from `window.parent` may pin the origin. */
|
|
31
|
+
export const HOST_CONTEXT_TYPE = "k9o:host:context";
|
|
32
|
+
|
|
33
|
+
const LOOPBACK_HOSTS: ReadonlySet<string> = new Set(["localhost", "127.0.0.1", "[::1]"]);
|
|
34
|
+
|
|
35
|
+
export type ConsoleOriginEnv = {
|
|
36
|
+
/** Mirrors `location.ancestorOrigins`; injected by tests, read from `globalThis.location` otherwise. */
|
|
37
|
+
ancestorOrigins?: readonly string[] | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* The embedding window (`window.parent`). Injected by tests; read from `globalThis.parent`
|
|
40
|
+
* otherwise (undefined when not framed). Handshake pinning requires it.
|
|
41
|
+
*/
|
|
42
|
+
parent?: unknown;
|
|
43
|
+
/** Message types that count as the host handshake. Defaults to `[HOST_CONTEXT_TYPE]`. */
|
|
44
|
+
handshakeTypes?: readonly string[] | undefined;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type ConsoleOriginEvent = {
|
|
48
|
+
origin: string;
|
|
49
|
+
data?: unknown;
|
|
50
|
+
/** `MessageEvent.source`. */
|
|
51
|
+
source?: unknown;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export type ConsoleOriginPin = {
|
|
55
|
+
/**
|
|
56
|
+
* True when the event comes from the pinned Console origin (and from `window.parent` when it is
|
|
57
|
+
* known). While unpinned, a handshake message from `window.parent` with an allowed origin pins it.
|
|
58
|
+
*/
|
|
59
|
+
accept(event: ConsoleOriginEvent): boolean;
|
|
60
|
+
/** The pinned origin, or `null` before resolution. Use as the `postMessage` target. */
|
|
61
|
+
target(): string | null;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* True for a bare origin (no path, no trailing slash) that is `https:`, or `http:` on a loopback
|
|
66
|
+
* host. No domain-name matching.
|
|
67
|
+
*/
|
|
68
|
+
export function isTrustedConsoleOrigin(origin: string): boolean {
|
|
69
|
+
let u: URL;
|
|
70
|
+
try {
|
|
71
|
+
u = new URL(origin);
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
if (u.origin !== origin) return false;
|
|
76
|
+
if (u.protocol === "https:") return true;
|
|
77
|
+
return u.protocol === "http:" && LOOPBACK_HOSTS.has(u.hostname);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function readAncestorOrigins(env: ConsoleOriginEnv): readonly string[] {
|
|
81
|
+
if (env.ancestorOrigins !== undefined) return env.ancestorOrigins;
|
|
82
|
+
const loc = (globalThis as { location?: { ancestorOrigins?: ArrayLike<string> } }).location;
|
|
83
|
+
const list = loc?.ancestorOrigins;
|
|
84
|
+
return list ? Array.from(list) : [];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readParent(env: ConsoleOriginEnv): unknown {
|
|
88
|
+
if ("parent" in env) return env.parent ?? undefined;
|
|
89
|
+
const g = globalThis as { parent?: unknown };
|
|
90
|
+
return g.parent !== undefined && g.parent !== globalThis ? g.parent : undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isHandshake(data: unknown, types: readonly string[]): boolean {
|
|
94
|
+
if (typeof data !== "object" || data === null) return false;
|
|
95
|
+
const t = (data as { type?: unknown }).type;
|
|
96
|
+
return typeof t === "string" && types.includes(t);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function createConsoleOriginPin(
|
|
100
|
+
configured: string | undefined,
|
|
101
|
+
env: ConsoleOriginEnv = {},
|
|
102
|
+
): ConsoleOriginPin {
|
|
103
|
+
const parent = readParent(env);
|
|
104
|
+
const handshakeTypes = env.handshakeTypes ?? [HOST_CONTEXT_TYPE];
|
|
105
|
+
const fromParentOk = (e: ConsoleOriginEvent): boolean => parent === undefined || e.source === parent;
|
|
106
|
+
|
|
107
|
+
if (configured !== undefined) {
|
|
108
|
+
return { accept: (e) => e.origin === configured && fromParentOk(e), target: () => configured };
|
|
109
|
+
}
|
|
110
|
+
const first = readAncestorOrigins(env)[0];
|
|
111
|
+
let pinned: string | null = first !== undefined && isTrustedConsoleOrigin(first) ? first : null;
|
|
112
|
+
return {
|
|
113
|
+
accept(e) {
|
|
114
|
+
if (pinned !== null) return e.origin === pinned && fromParentOk(e);
|
|
115
|
+
// Handshake pinning needs the real parent window to compare against.
|
|
116
|
+
if (parent === undefined || e.source !== parent) return false;
|
|
117
|
+
if (!isHandshake(e.data, handshakeTypes)) return false;
|
|
118
|
+
if (!isTrustedConsoleOrigin(e.origin)) return false;
|
|
119
|
+
pinned = e.origin;
|
|
120
|
+
return true;
|
|
121
|
+
},
|
|
122
|
+
target: () => pinned,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Gate API base for a pinned Console origin: the Console proxies the gateway under `/api`. */
|
|
127
|
+
export function deriveApiBase(consoleOrigin: string): string {
|
|
128
|
+
return `${consoleOrigin}/api`;
|
|
129
|
+
}
|