cortico 0.1.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 +21 -0
- package/README.md +47 -0
- package/package.json +43 -0
- package/src/boot.ts +46 -0
- package/src/bot.ts +1342 -0
- package/src/config-file.ts +41 -0
- package/src/core/README.md +92 -0
- package/src/core/billing.ts +26 -0
- package/src/core/blobs.ts +106 -0
- package/src/core/bus.ts +311 -0
- package/src/core/config-schema.ts +201 -0
- package/src/core/config.ts +262 -0
- package/src/core/core.ts +699 -0
- package/src/core/cost.ts +244 -0
- package/src/core/event-store.ts +304 -0
- package/src/core/fork.ts +113 -0
- package/src/core/generation.ts +118 -0
- package/src/core/instance-lock.ts +215 -0
- package/src/core/ipc-logger.ts +105 -0
- package/src/core/language.ts +62 -0
- package/src/core/log-context.ts +38 -0
- package/src/core/loop.ts +1858 -0
- package/src/core/markers.ts +48 -0
- package/src/core/prefix.ts +121 -0
- package/src/core/run.ts +95 -0
- package/src/core/secrets.ts +18 -0
- package/src/core/session.ts +56 -0
- package/src/core/sessions.ts +261 -0
- package/src/core/state.ts +104 -0
- package/src/core/template.ts +72 -0
- package/src/core/timers.ts +129 -0
- package/src/core/tool-log.ts +135 -0
- package/src/core/transcript.ts +92 -0
- package/src/core/truncate.ts +121 -0
- package/src/core/types.ts +1140 -0
- package/src/core/usage-log.ts +79 -0
- package/src/core/util.ts +386 -0
- package/src/deploy-listing.ts +160 -0
- package/src/deploy.ts +162 -0
- package/src/extensions/README.md +72 -0
- package/src/extensions/dry-mount.ts +390 -0
- package/src/extensions/manifest.ts +153 -0
- package/src/extensions/runtime.ts +42 -0
- package/src/extensions.ts +519 -0
- package/src/launcher.ts +285 -0
- package/src/paths.ts +155 -0
- package/src/protocol/open-responses/LICENSE +201 -0
- package/src/protocol/open-responses/README.md +14 -0
- package/src/protocol/open-responses/context-helpers.ts +27 -0
- package/src/protocol/open-responses/context-log.ts +59 -0
- package/src/protocol/open-responses/context.ts +89 -0
- package/src/protocol/open-responses/generated.ts +115 -0
- package/src/protocol/open-responses/index.ts +33 -0
- package/src/protocol/open-responses/openapi.json +4230 -0
- package/src/protocol/open-responses/stream.ts +146 -0
- package/src/protocol/open-responses/tokens.ts +31 -0
- package/src/providers/README.md +86 -0
- package/src/providers/base.ts +111 -0
- package/src/providers/configuration.ts +97 -0
- package/src/providers/console/config.ts +21 -0
- package/src/providers/console/settings.ts +443 -0
- package/src/providers/console/strings.ts +74 -0
- package/src/providers/console/types.ts +11 -0
- package/src/providers/llamacpp/archive.ts +148 -0
- package/src/providers/llamacpp/catalog.ts +247 -0
- package/src/providers/llamacpp/config.ts +36 -0
- package/src/providers/llamacpp/console/client.ts +6 -0
- package/src/providers/llamacpp/console/models-panel.ts +120 -0
- package/src/providers/llamacpp/console/runtime-panel.ts +173 -0
- package/src/providers/llamacpp/console/server.ts +153 -0
- package/src/providers/llamacpp/index.ts +95 -0
- package/src/providers/llamacpp/native.ts +51 -0
- package/src/providers/llamacpp/options.ts +117 -0
- package/src/providers/llamacpp/runtime-store.ts +139 -0
- package/src/providers/llamacpp/runtime.ts +214 -0
- package/src/providers/llamacpp/server.ts +286 -0
- package/src/providers/llamacpp/strings.ts +230 -0
- package/src/providers/openai-responses-compat/config.ts +20 -0
- package/src/providers/openai-responses-compat/index.ts +89 -0
- package/src/providers/openai-responses-compat/native.ts +127 -0
- package/src/providers/openai-responses-compat/strings.ts +19 -0
- package/src/providers/pricebook.ts +75 -0
- package/src/providers/registry.ts +137 -0
- package/src/providers/strings.ts +71 -0
- package/src/providers/transport/chat.ts +156 -0
- package/src/providers/transport/errors.ts +48 -0
- package/src/providers/transport/history.ts +68 -0
- package/src/providers/transport/native-input.ts +83 -0
- package/src/providers/transport/native-types.ts +13 -0
- package/src/providers/transport/response-assembly.ts +191 -0
- package/src/providers/transport/response-http.ts +196 -0
- package/src/providers/transport/response-meters.ts +27 -0
- package/src/providers/transport/responses-input.ts +56 -0
- package/src/web/README.md +84 -0
- package/src/web/client/console-pages/builtins/llm-settings/panel.ts +625 -0
- package/src/web/client/console-pages/builtins/llm-settings/pricing-panel.ts +168 -0
- package/src/web/client/console-pages/builtins/llm-settings/strings.ts +213 -0
- package/src/web/client/console-pages/builtins.ts +10 -0
- package/src/web/client/console-pages/context.ts +135 -0
- package/src/web/client/console-pages/host.ts +633 -0
- package/src/web/client/console-pages/loader.ts +132 -0
- package/src/web/client/console-pages/strings.ts +90 -0
- package/src/web/client/console-pages/tools/strings.ts +49 -0
- package/src/web/client/console-pages/tools/view.ts +215 -0
- package/src/web/client/core/api.ts +200 -0
- package/src/web/client/core/language.ts +56 -0
- package/src/web/client/core/lifecycle.ts +132 -0
- package/src/web/client/core/router.ts +199 -0
- package/src/web/client/core/stream.ts +157 -0
- package/src/web/client/core/websocket.ts +82 -0
- package/src/web/client/features/appearance/index.ts +333 -0
- package/src/web/client/features/appearance/strings.ts +87 -0
- package/src/web/client/features/config/index.ts +11 -0
- package/src/web/client/features/config/strings.ts +41 -0
- package/src/web/client/features/config/view.ts +428 -0
- package/src/web/client/features/core/events.ts +197 -0
- package/src/web/client/features/core/index.ts +254 -0
- package/src/web/client/features/core/run.ts +56 -0
- package/src/web/client/features/core/runlog.ts +214 -0
- package/src/web/client/features/core/sessions.ts +53 -0
- package/src/web/client/features/core/strings.ts +157 -0
- package/src/web/client/features/extensions/index.ts +422 -0
- package/src/web/client/features/extensions/strings.ts +164 -0
- package/src/web/client/features/feature.ts +93 -0
- package/src/web/client/features/live/context.ts +280 -0
- package/src/web/client/features/live/fork.ts +156 -0
- package/src/web/client/features/live/index.ts +392 -0
- package/src/web/client/features/live/onboarding.ts +113 -0
- package/src/web/client/features/live/protocol.ts +122 -0
- package/src/web/client/features/live/sessions.ts +71 -0
- package/src/web/client/features/live/status.ts +68 -0
- package/src/web/client/features/live/strings.ts +252 -0
- package/src/web/client/features/live/timeline.ts +590 -0
- package/src/web/client/features/prompts/editor.ts +292 -0
- package/src/web/client/features/prompts/index.ts +346 -0
- package/src/web/client/features/prompts/strings.ts +136 -0
- package/src/web/client/features/prompts/view.ts +256 -0
- package/src/web/client/features/providers/index.ts +100 -0
- package/src/web/client/features/providers/strings.ts +23 -0
- package/src/web/client/features/settings/general.ts +28 -0
- package/src/web/client/features/settings/index.ts +82 -0
- package/src/web/client/features/settings/strings.ts +33 -0
- package/src/web/client/features/storage/index.ts +1 -0
- package/src/web/client/features/storage/strings.ts +45 -0
- package/src/web/client/features/storage/view.ts +154 -0
- package/src/web/client/features/usage/chart.ts +486 -0
- package/src/web/client/features/usage/index.ts +407 -0
- package/src/web/client/features/usage/labels.ts +46 -0
- package/src/web/client/features/usage/range.ts +36 -0
- package/src/web/client/features/usage/state.ts +37 -0
- package/src/web/client/features/usage/strings.ts +191 -0
- package/src/web/client/features/usage/tooltip.ts +86 -0
- package/src/web/client/features/usage/types.ts +96 -0
- package/src/web/client/features/worlds/index.ts +424 -0
- package/src/web/client/features/worlds/strings.ts +121 -0
- package/src/web/client/main.ts +273 -0
- package/src/web/client/shell/avatar.ts +233 -0
- package/src/web/client/shell/index.ts +549 -0
- package/src/web/client/shell/strings.ts +89 -0
- package/src/web/client/theme/handoff.ts +43 -0
- package/src/web/client/theme/palette.ts +109 -0
- package/src/web/client/theme/registry.ts +130 -0
- package/src/web/client/theme/storage.ts +72 -0
- package/src/web/client/theme/strings.ts +123 -0
- package/src/web/client/theme/studio.ts +366 -0
- package/src/web/client/ui/actions.ts +109 -0
- package/src/web/client/ui/data.ts +185 -0
- package/src/web/client/ui/dom.ts +23 -0
- package/src/web/client/ui/fields.ts +191 -0
- package/src/web/client/ui/format.ts +75 -0
- package/src/web/client/ui/icons.ts +183 -0
- package/src/web/client/ui/images.ts +91 -0
- package/src/web/client/ui/index.ts +89 -0
- package/src/web/client/ui/lamp.ts +125 -0
- package/src/web/client/ui/log.ts +98 -0
- package/src/web/client/ui/overlay.ts +184 -0
- package/src/web/client/ui/page.ts +8 -0
- package/src/web/client/ui/prompt-input.tsx +311 -0
- package/src/web/client/ui/sheet.ts +131 -0
- package/src/web/client/ui/strings.ts +71 -0
- package/src/web/console-pages.ts +367 -0
- package/src/web/files.ts +107 -0
- package/src/web/path-picker.ts +296 -0
- package/src/web/public/index.html +17 -0
- package/src/web/public/styles.css +1715 -0
- package/src/web/server.ts +1883 -0
- package/src/web/shared/client-panel.ts +603 -0
- package/src/web/shared/console-protocol.ts +560 -0
- package/src/web/shared/css.d.ts +1 -0
- package/src/web/shared/path-picker.ts +23 -0
- package/src/web/shared/theme.ts +256 -0
- package/src/web/theme-store.ts +37 -0
- package/src/world.ts +449 -0
- package/src/worlds/bilibili/ENV_PROMPT.md +5 -0
- package/src/worlds/bilibili/README.md +176 -0
- package/src/worlds/bilibili/audience-admission.ts +995 -0
- package/src/worlds/bilibili/client.ts +528 -0
- package/src/worlds/bilibili/coalescing-buffer.ts +125 -0
- package/src/worlds/bilibili/config.ts +185 -0
- package/src/worlds/bilibili/console/client.ts +202 -0
- package/src/worlds/bilibili/definition.ts +35 -0
- package/src/worlds/bilibili/gift-frame.ts +131 -0
- package/src/worlds/bilibili/normalize.ts +452 -0
- package/src/worlds/bilibili/overlay/announcement.ts +76 -0
- package/src/worlds/bilibili/overlay/assets.ts +82 -0
- package/src/worlds/bilibili/overlay/model.ts +494 -0
- package/src/worlds/bilibili/overlay/project.ts +202 -0
- package/src/worlds/bilibili/overlay/server.ts +286 -0
- package/src/worlds/bilibili/overlay/types.ts +217 -0
- package/src/worlds/bilibili/overlay/web/app.js +690 -0
- package/src/worlds/bilibili/overlay/web/editor.css +297 -0
- package/src/worlds/bilibili/overlay/web/editor.html +102 -0
- package/src/worlds/bilibili/overlay/web/editor.js +2116 -0
- package/src/worlds/bilibili/overlay/web/overlay.html +13 -0
- package/src/worlds/bilibili/overlay/web/styles.css +258 -0
- package/src/worlds/bilibili/protobuf.ts +123 -0
- package/src/worlds/bilibili/wire.ts +94 -0
- package/src/worlds/bilibili/world.ts +1389 -0
- package/src/worlds/console-fixture/ENV_PROMPT.md +1 -0
- package/src/worlds/console-fixture/console/client.ts +72 -0
- package/src/worlds/console-fixture/world.ts +73 -0
- package/src/worlds/index.ts +15 -0
- package/src/worlds/minecraft/ENV_PROMPT.md +32 -0
- package/src/worlds/minecraft/ENV_PROMPT_CAMERA.md +2 -0
- package/src/worlds/minecraft/LICENSE-mineflayer-pathfinder.txt +21 -0
- package/src/worlds/minecraft/README.md +917 -0
- package/src/worlds/minecraft/blueprint-plan.ts +915 -0
- package/src/worlds/minecraft/blueprint-registry.ts +566 -0
- package/src/worlds/minecraft/blueprint-repair.ts +236 -0
- package/src/worlds/minecraft/blueprint-resource.ts +272 -0
- package/src/worlds/minecraft/blueprint.ts +492 -0
- package/src/worlds/minecraft/body-lease.ts +340 -0
- package/src/worlds/minecraft/bridge.ts +749 -0
- package/src/worlds/minecraft/check.ts +651 -0
- package/src/worlds/minecraft/chests.ts +292 -0
- package/src/worlds/minecraft/client-launch.ts +272 -0
- package/src/worlds/minecraft/client-options.ts +85 -0
- package/src/worlds/minecraft/client-skins.ts +164 -0
- package/src/worlds/minecraft/client-window.ps1 +91 -0
- package/src/worlds/minecraft/client.ts +423 -0
- package/src/worlds/minecraft/combat-context.ts +109 -0
- package/src/worlds/minecraft/combat.ts +1552 -0
- package/src/worlds/minecraft/config.ts +499 -0
- package/src/worlds/minecraft/console/access.ts +218 -0
- package/src/worlds/minecraft/console/client.ts +244 -0
- package/src/worlds/minecraft/console/log.ts +110 -0
- package/src/worlds/minecraft/console/mount.ts +420 -0
- package/src/worlds/minecraft/console/skin.ts +257 -0
- package/src/worlds/minecraft/console/style.css +154 -0
- package/src/worlds/minecraft/console/world.ts +449 -0
- package/src/worlds/minecraft/deaths.ts +135 -0
- package/src/worlds/minecraft/definition.ts +18 -0
- package/src/worlds/minecraft/engine-child.ts +274 -0
- package/src/worlds/minecraft/engine-ipc.ts +98 -0
- package/src/worlds/minecraft/entity-facts.ts +132 -0
- package/src/worlds/minecraft/escape.ts +407 -0
- package/src/worlds/minecraft/executor.ts +14494 -0
- package/src/worlds/minecraft/explored.ts +156 -0
- package/src/worlds/minecraft/geometry.ts +205 -0
- package/src/worlds/minecraft/goal-plan.ts +647 -0
- package/src/worlds/minecraft/item-break.ts +126 -0
- package/src/worlds/minecraft/item-facts.ts +146 -0
- package/src/worlds/minecraft/item-pick.ts +67 -0
- package/src/worlds/minecraft/level-dat.ts +176 -0
- package/src/worlds/minecraft/log.ts +132 -0
- package/src/worlds/minecraft/mineflayer-fixes.ts +1180 -0
- package/src/worlds/minecraft/names.ts +377 -0
- package/src/worlds/minecraft/pathfinder-lib.d.ts +13 -0
- package/src/worlds/minecraft/pathfinder-perf.ts +796 -0
- package/src/worlds/minecraft/piglin.ts +49 -0
- package/src/worlds/minecraft/policy.ts +547 -0
- package/src/worlds/minecraft/precheck.ts +655 -0
- package/src/worlds/minecraft/proxy.ts +445 -0
- package/src/worlds/minecraft/ranged.ts +635 -0
- package/src/worlds/minecraft/readouts.ts +102 -0
- package/src/worlds/minecraft/round.ts +26 -0
- package/src/worlds/minecraft/search-observation.ts +92 -0
- package/src/worlds/minecraft/server-config.ts +467 -0
- package/src/worlds/minecraft/server.ts +658 -0
- package/src/worlds/minecraft/show.ts +84 -0
- package/src/worlds/minecraft/skills.ts +1763 -0
- package/src/worlds/minecraft/terrain.ts +1735 -0
- package/src/worlds/minecraft/window.ts +96 -0
- package/src/worlds/minecraft/works.ts +179 -0
- package/src/worlds/minecraft/world.ts +6438 -0
- package/src/worlds/qq/ENV_PROMPT.md +5 -0
- package/src/worlds/qq/config.ts +108 -0
- package/src/worlds/qq/console/client.ts +126 -0
- package/src/worlds/qq/console/events.ts +155 -0
- package/src/worlds/qq/console/gate.ts +232 -0
- package/src/worlds/qq/console/roster.ts +203 -0
- package/src/worlds/qq/console/style.css +65 -0
- package/src/worlds/qq/conversation.ts +28 -0
- package/src/worlds/qq/definition.ts +62 -0
- package/src/worlds/qq/driver.ts +375 -0
- package/src/worlds/qq/history-tools.ts +209 -0
- package/src/worlds/qq/normalize.ts +323 -0
- package/src/worlds/qq/qface-map.ts +282 -0
- package/src/worlds/qq/vision-prompt.ts +27 -0
- package/src/worlds/qq/vision.ts +642 -0
- package/src/worlds/qq/vlm.ts +142 -0
- package/src/worlds/qq/world.ts +1503 -0
- package/src/worlds/terminal/ENV_PROMPT.md +11 -0
- package/src/worlds/terminal/config.ts +9 -0
- package/src/worlds/terminal/definition.ts +14 -0
- package/src/worlds/terminal/world.ts +700 -0
- package/src/worlds/websearch/ENV_PROMPT.md +1 -0
- package/src/worlds/websearch/brave-client.ts +261 -0
- package/src/worlds/websearch/config.ts +75 -0
- package/src/worlds/websearch/definition.ts +20 -0
- package/src/worlds/websearch/world.ts +171 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 解析 package.json 的 cortico 扩展声明,不访问文件系统;装载器与 check:extension 共用。
|
|
3
|
+
* kind 必须为 world、provider 或 bot;api 必须等于 EXTENSION_API_VERSION。
|
|
4
|
+
* WorldDefinition、ProviderModule、BotDefinition 及其可达接口或 ConsolePanelContext
|
|
5
|
+
* 发生不兼容变更时递增版本。
|
|
6
|
+
* consoleClient 和 consoleStyle 是预构建产物的包内相对路径;服务端分配 URL。
|
|
7
|
+
* 包必须使用 type=module,使扩展与框架通过同一 ESM 解析方式共享模块实例。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** 扩展接口发生不兼容变更时递增。 */
|
|
11
|
+
export const EXTENSION_API_VERSION = 4;
|
|
12
|
+
|
|
13
|
+
export type ExtensionKind = 'world' | 'provider' | 'bot';
|
|
14
|
+
export const EXTENSION_KINDS: readonly ExtensionKind[] = ['world', 'provider', 'bot'];
|
|
15
|
+
|
|
16
|
+
/** npm 上按类发现用的关键字。 */
|
|
17
|
+
export const EXTENSION_KEYWORDS: Readonly<Record<ExtensionKind, string>> = {
|
|
18
|
+
world: 'cortico-world',
|
|
19
|
+
provider: 'cortico-provider',
|
|
20
|
+
bot: 'cortico-bot',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** 框架导入前缀 cortico/ 对应 src/ 下的路径,由 runtime.ts 的模块钩子解析。 */
|
|
24
|
+
export const FRAMEWORK_SPECIFIER = 'cortico';
|
|
25
|
+
|
|
26
|
+
export interface ExtensionManifest {
|
|
27
|
+
kind: ExtensionKind;
|
|
28
|
+
api: number;
|
|
29
|
+
/** 包内相对路径,`.js` / `.mjs`。 */
|
|
30
|
+
consoleClient?: string;
|
|
31
|
+
/** 包内相对路径,`.css`。要有 `consoleClient` 才有意义。 */
|
|
32
|
+
consoleStyle?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** package.json 里我们会看的字段。 */
|
|
36
|
+
export interface ExtensionPackageJson {
|
|
37
|
+
name?: string;
|
|
38
|
+
version?: string;
|
|
39
|
+
description?: string;
|
|
40
|
+
type?: string;
|
|
41
|
+
main?: string;
|
|
42
|
+
module?: string;
|
|
43
|
+
exports?: unknown;
|
|
44
|
+
keywords?: string[];
|
|
45
|
+
dependencies?: Record<string, string>;
|
|
46
|
+
cortico?: unknown;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type ExtensionManifestResult =
|
|
50
|
+
| { ok: true; manifest: ExtensionManifest; warnings: string[] }
|
|
51
|
+
| { ok: false; reasons: string[]; warnings: string[] };
|
|
52
|
+
|
|
53
|
+
/** 包内相对路径仅使用 /;拒绝绝对路径、盘符、.. 段和开头的分隔符。 */
|
|
54
|
+
export function isSafeRelativeFile(p: unknown): p is string {
|
|
55
|
+
if (typeof p !== 'string' || p === '') return false;
|
|
56
|
+
if (p.includes('\\') || p.startsWith('/') || /^[A-Za-z]:/.test(p)) return false;
|
|
57
|
+
return !p.split('/').some((seg) => seg === '..' || seg === '');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function parseExtensionManifest(pkg: ExtensionPackageJson): ExtensionManifestResult {
|
|
61
|
+
const reasons: string[] = [];
|
|
62
|
+
const warnings: string[] = [];
|
|
63
|
+
|
|
64
|
+
if (pkg.type !== 'module') {
|
|
65
|
+
reasons.push("package.json 需要 \"type\": \"module\",以保持扩展与框架的 ESM 模块实例一致。");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const block = pkg.cortico;
|
|
69
|
+
if (!block || typeof block !== 'object' || Array.isArray(block)) {
|
|
70
|
+
reasons.push('package.json 缺少 cortico 块(至少要 kind 与 api)。');
|
|
71
|
+
return { ok: false, reasons, warnings };
|
|
72
|
+
}
|
|
73
|
+
const m = block as Record<string, unknown>;
|
|
74
|
+
|
|
75
|
+
const kind = m.kind;
|
|
76
|
+
const kindOk = typeof kind === 'string' && (EXTENSION_KINDS as readonly string[]).includes(kind);
|
|
77
|
+
if (!kindOk) {
|
|
78
|
+
reasons.push(`cortico.kind 必须是 ${EXTENSION_KINDS.map((k) => `"${k}"`).join(' 或 ')},现在是 ${JSON.stringify(kind)}。`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const api = m.api;
|
|
82
|
+
if (typeof api !== 'number' || !Number.isInteger(api) || api < 1) {
|
|
83
|
+
reasons.push(`cortico.api 必须是正整数(当前框架契约 v${EXTENSION_API_VERSION}),现在是 ${JSON.stringify(api)}。`);
|
|
84
|
+
} else if (api < EXTENSION_API_VERSION) {
|
|
85
|
+
reasons.push(`扩展按契约 v${api} 编写,本框架是 v${EXTENSION_API_VERSION}:扩展需要升级。`);
|
|
86
|
+
} else if (api > EXTENSION_API_VERSION) {
|
|
87
|
+
reasons.push(`扩展要求契约 v${api},本框架只到 v${EXTENSION_API_VERSION}:框架需要升级。`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const client = m.consoleClient;
|
|
91
|
+
if (client !== undefined) {
|
|
92
|
+
if (!isSafeRelativeFile(client)) {
|
|
93
|
+
reasons.push(`cortico.consoleClient 必须是包内相对路径(不含 ..、不以 / 开头、只用 /),现在是 ${JSON.stringify(client)}。`);
|
|
94
|
+
} else if (!/\.m?js$/.test(client)) {
|
|
95
|
+
reasons.push('cortico.consoleClient 必须指向预构建好的 .js / .mjs;浏览器不跑 TypeScript。');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const style = m.consoleStyle;
|
|
99
|
+
if (style !== undefined) {
|
|
100
|
+
if (client === undefined) {
|
|
101
|
+
reasons.push('cortico.consoleStyle 要与 consoleClient 一起给:样式随面板 bundle 注入。');
|
|
102
|
+
} else if (!isSafeRelativeFile(style)) {
|
|
103
|
+
reasons.push(`cortico.consoleStyle 必须是包内相对路径,现在是 ${JSON.stringify(style)}。`);
|
|
104
|
+
} else if (!/\.css$/.test(style)) {
|
|
105
|
+
reasons.push('cortico.consoleStyle 必须指向 .css。');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (kindOk) {
|
|
110
|
+
const keyword = EXTENSION_KEYWORDS[kind as ExtensionKind];
|
|
111
|
+
if (!(pkg.keywords ?? []).includes(keyword)) {
|
|
112
|
+
warnings.push(`keywords 里没有 "${keyword}":发布到 npm 后控制台的搜索找不到它(本地安装不受影响)。`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (reasons.length) return { ok: false, reasons, warnings };
|
|
117
|
+
return {
|
|
118
|
+
ok: true,
|
|
119
|
+
warnings,
|
|
120
|
+
manifest: {
|
|
121
|
+
kind: kind as ExtensionKind,
|
|
122
|
+
api: api as number,
|
|
123
|
+
...(typeof client === 'string' ? { consoleClient: client } : {}),
|
|
124
|
+
...(typeof style === 'string' ? { consoleStyle: style } : {}),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 浏览器端产物:服务端分配 URL
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* pageId 是控制台产物键,格式为 world:<id>、llm:<id> 或 persona:<id>。
|
|
133
|
+
* 版本写入 URL,使新版本使用新的 ESM 模块地址。服务端仅提供所列绝对文件路径。
|
|
134
|
+
*/
|
|
135
|
+
export interface ExtensionConsoleAsset {
|
|
136
|
+
pageId: string;
|
|
137
|
+
packageName: string;
|
|
138
|
+
version: string;
|
|
139
|
+
jsFile: string;
|
|
140
|
+
cssFile?: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** 进 URL 的一段:只留 `isSafeAssetUrl` 放行的字符,其余(`@` `/` `+` `~`…)换成 `-` / `__`。 */
|
|
144
|
+
export function extensionAssetSegment(raw: string): string {
|
|
145
|
+
return raw.replace(/^@/, '').replace(/\//g, '__').replace(/[^A-Za-z0-9._-]/g, '-');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export const EXTENSION_ASSET_PREFIX = '/assets/extensions/';
|
|
149
|
+
|
|
150
|
+
/** `/assets/extensions/<包>/<版本>/<文件名>`。 */
|
|
151
|
+
export function extensionAssetUrl(packageName: string, version: string, fileName: string): string {
|
|
152
|
+
return `${EXTENSION_ASSET_PREFIX}${extensionAssetSegment(packageName)}/${extensionAssetSegment(version)}/${extensionAssetSegment(fileName)}`;
|
|
153
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将扩展的 cortico/<路径> 导入映射到框架 src/<路径>。
|
|
3
|
+
* 使用同步 module.registerHooks 与 tsx 的解析链协作;映射后继续调用下一个解析器,
|
|
4
|
+
* 使最终 URL 与框架内部导入一致,保留共享模块实例。扩展须使用 type=module。
|
|
5
|
+
* childExecArgv 保留父进程加载器并通过 --import 注册本模块,供引擎子进程使用。
|
|
6
|
+
*/
|
|
7
|
+
import { registerHooks } from 'node:module';
|
|
8
|
+
import { FRAMEWORK_SPECIFIER } from './manifest.ts';
|
|
9
|
+
|
|
10
|
+
const PREFIX = `${FRAMEWORK_SPECIFIER}/`;
|
|
11
|
+
/** `src/` 的 URL:本文件在 `src/extensions/`。 */
|
|
12
|
+
const SRC_URL = new URL('../', import.meta.url);
|
|
13
|
+
const FLAG = Symbol.for('cortico.extensions.resolver');
|
|
14
|
+
|
|
15
|
+
/** 重复注册无效。 */
|
|
16
|
+
export function registerFrameworkResolver(): void {
|
|
17
|
+
const g = globalThis as unknown as Record<symbol, unknown>;
|
|
18
|
+
if (g[FLAG]) return;
|
|
19
|
+
g[FLAG] = true;
|
|
20
|
+
registerHooks({
|
|
21
|
+
resolve(specifier, context, next) {
|
|
22
|
+
if (specifier.startsWith(PREFIX)) {
|
|
23
|
+
return next(new URL(specifier.slice(PREFIX.length), SRC_URL).href, context);
|
|
24
|
+
}
|
|
25
|
+
return next(specifier, context);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 本文件的 URL:`--import` 它就等于调了一次 {@link registerFrameworkResolver}。 */
|
|
31
|
+
export const RESOLVER_URL = import.meta.url;
|
|
32
|
+
|
|
33
|
+
/** 保留父进程加载器参数;未配置时补充 --import tsx,最后导入本模块注册解析钩子。 */
|
|
34
|
+
export function childExecArgv(): string[] {
|
|
35
|
+
const inherited = process.execArgv.some((a) => /[\\/]tsx[\\/]|(^|\s)tsx$/.test(a))
|
|
36
|
+
? [...process.execArgv]
|
|
37
|
+
: ['--import', 'tsx'];
|
|
38
|
+
return [...inherited, '--import', RESOLVER_URL];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// --import 本模块时注册钩子。
|
|
42
|
+
registerFrameworkResolver();
|