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,286 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
3
|
+
import type { AddressInfo } from 'node:net';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import type { Logger } from '../../../core/types.ts';
|
|
6
|
+
import type { AgentAnnouncementState, OverlayAudienceEvent } from './types.ts';
|
|
7
|
+
import { OverlayAssetStore } from './assets.ts';
|
|
8
|
+
|
|
9
|
+
const STATIC_DIR = fileURLToPath(new URL('./web/', import.meta.url));
|
|
10
|
+
const STATIC_FILES: Record<string, { file: string; type: string }> = {
|
|
11
|
+
'/overlay': { file: 'overlay.html', type: 'text/html; charset=utf-8' },
|
|
12
|
+
'/overlay/app.js': { file: 'app.js', type: 'text/javascript; charset=utf-8' },
|
|
13
|
+
'/overlay/styles.css': { file: 'styles.css', type: 'text/css; charset=utf-8' },
|
|
14
|
+
'/editor': { file: 'editor.html', type: 'text/html; charset=utf-8' },
|
|
15
|
+
'/editor/editor.js': { file: 'editor.js', type: 'text/javascript; charset=utf-8' },
|
|
16
|
+
'/editor/editor.css': { file: 'editor.css', type: 'text/css; charset=utf-8' },
|
|
17
|
+
};
|
|
18
|
+
const KEEPALIVE_MS = 15_000;
|
|
19
|
+
const SSE_PAD = `: ${' '.repeat(2048)}\n\n`;
|
|
20
|
+
const MAX_EDITOR_BODY_BYTES = 12 * 1024 * 1024;
|
|
21
|
+
const HOST = '127.0.0.1';
|
|
22
|
+
|
|
23
|
+
export interface BilibiliOverlayEditorActions {
|
|
24
|
+
state(): Record<string, unknown>;
|
|
25
|
+
saveDesign(value: unknown, baseRevision: unknown): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
26
|
+
importAsset(value: unknown): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
27
|
+
deleteAsset(value: unknown): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
28
|
+
setAgentAnnouncement(value: unknown, expectedRevision: unknown): Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class OverlayEditorConflictError extends Error {}
|
|
32
|
+
|
|
33
|
+
interface BilibiliOverlayServerOptions {
|
|
34
|
+
preferredPort: number;
|
|
35
|
+
assets: OverlayAssetStore;
|
|
36
|
+
snapshot: () => Record<string, unknown>;
|
|
37
|
+
editor: BilibiliOverlayEditorActions;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export class BilibiliOverlayServer {
|
|
41
|
+
private http: Server | null = null;
|
|
42
|
+
private boundPort: number | null = null;
|
|
43
|
+
private keepalive: ReturnType<typeof setInterval> | null = null;
|
|
44
|
+
private readonly subscribers = new Set<ServerResponse>();
|
|
45
|
+
private seq = 0;
|
|
46
|
+
|
|
47
|
+
constructor(private readonly options: BilibiliOverlayServerOptions) {}
|
|
48
|
+
|
|
49
|
+
get port(): number {
|
|
50
|
+
return this.boundPort ?? this.options.preferredPort;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
get overlayUrl(): string {
|
|
54
|
+
return `http://${HOST}:${this.port}/overlay`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get editorUrl(): string {
|
|
58
|
+
return `http://${HOST}:${this.port}/editor`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get baseUrl(): string {
|
|
62
|
+
return `http://${HOST}:${this.port}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
get subscriberCount(): number {
|
|
66
|
+
return this.subscribers.size;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get running(): boolean {
|
|
70
|
+
return this.http !== null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async start(log: Logger): Promise<void> {
|
|
74
|
+
if (this.http) return;
|
|
75
|
+
const preferred = this.options.preferredPort;
|
|
76
|
+
const attempts = preferred === 0 ? 1 : Math.min(5, 65536 - preferred);
|
|
77
|
+
let lastError: unknown;
|
|
78
|
+
for (let index = 0; index < attempts; index += 1) {
|
|
79
|
+
const port = preferred === 0 ? 0 : preferred + index;
|
|
80
|
+
const server = createServer((request, response) => {
|
|
81
|
+
void this.onRequest(request, response).catch((error: unknown) => {
|
|
82
|
+
if (response.headersSent) {
|
|
83
|
+
response.destroy();
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this.json(response, 500, { error: error instanceof Error ? error.message : String(error) });
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
try {
|
|
90
|
+
await new Promise<void>((resolve, reject) => {
|
|
91
|
+
server.once('error', reject);
|
|
92
|
+
server.listen(port, HOST, () => {
|
|
93
|
+
server.removeListener('error', reject);
|
|
94
|
+
resolve();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
this.http = server;
|
|
98
|
+
this.boundPort = (server.address() as AddressInfo).port;
|
|
99
|
+
if (index > 0) {
|
|
100
|
+
log.warn(
|
|
101
|
+
`B站 Overlay 端口 ${preferred} 被占用,改用 ${this.boundPort}`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
lastError = error;
|
|
107
|
+
server.close();
|
|
108
|
+
if ((error as NodeJS.ErrnoException)?.code !== 'EADDRINUSE' || preferred === 0) throw error;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (!this.http) throw lastError instanceof Error ? lastError : new Error('B站 Overlay 没有可用端口');
|
|
112
|
+
this.keepalive = setInterval(() => {
|
|
113
|
+
for (const response of this.subscribers) {
|
|
114
|
+
try {
|
|
115
|
+
response.write(':hb\n\n');
|
|
116
|
+
} catch {
|
|
117
|
+
this.subscribers.delete(response);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}, KEEPALIVE_MS);
|
|
121
|
+
this.keepalive.unref?.();
|
|
122
|
+
log.info(`B站 Overlay 已启动 ${this.overlayUrl}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async stop(): Promise<void> {
|
|
126
|
+
if (this.keepalive) clearInterval(this.keepalive);
|
|
127
|
+
this.keepalive = null;
|
|
128
|
+
for (const response of this.subscribers) response.end();
|
|
129
|
+
this.subscribers.clear();
|
|
130
|
+
const server = this.http;
|
|
131
|
+
this.http = null;
|
|
132
|
+
this.boundPort = null;
|
|
133
|
+
if (server) await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
emitAudience(event: OverlayAudienceEvent): void {
|
|
137
|
+
this.emit('audience', { event });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
emitState(): void {
|
|
141
|
+
this.emit('state', this.options.snapshot());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
emitAnnouncement(agentAnnouncement: AgentAnnouncementState): void {
|
|
145
|
+
this.emit('announcement', { agentAnnouncement });
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private emit(type: string, data: Record<string, unknown>): void {
|
|
149
|
+
if (!this.subscribers.size) return;
|
|
150
|
+
const payload = `id: ${++this.seq}\ndata: ${JSON.stringify({ type, ...data })}\n\n`;
|
|
151
|
+
for (const response of this.subscribers) {
|
|
152
|
+
if (type === 'audience' && response.writableNeedDrain) continue;
|
|
153
|
+
try {
|
|
154
|
+
response.write(payload);
|
|
155
|
+
} catch {
|
|
156
|
+
this.subscribers.delete(response);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private async onRequest(request: IncomingMessage, response: ServerResponse): Promise<void> {
|
|
162
|
+
const url = new URL(request.url ?? '/', this.baseUrl);
|
|
163
|
+
const path = url.pathname === '/' ? '/editor' : url.pathname.replace(/\/$/, '') || '/';
|
|
164
|
+
if (path === '/api/editor/state' && request.method === 'GET') {
|
|
165
|
+
this.json(response, 200, this.options.editor.state());
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (path.startsWith('/api/editor/')) {
|
|
169
|
+
await this.onEditorRequest(request, response, path);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const staticFile = STATIC_FILES[path];
|
|
173
|
+
if (staticFile && request.method === 'GET') {
|
|
174
|
+
try {
|
|
175
|
+
const body = readFileSync(STATIC_DIR + staticFile.file);
|
|
176
|
+
response.writeHead(200, {
|
|
177
|
+
'Content-Type': staticFile.type,
|
|
178
|
+
'Cache-Control': 'no-cache, no-store',
|
|
179
|
+
'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' https: http: data: blob:; connect-src 'self'",
|
|
180
|
+
'X-Content-Type-Options': 'nosniff',
|
|
181
|
+
'X-Frame-Options': 'SAMEORIGIN',
|
|
182
|
+
}).end(body);
|
|
183
|
+
} catch {
|
|
184
|
+
response.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' }).end('overlay asset unreadable');
|
|
185
|
+
}
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
if (path.startsWith('/assets/') && request.method === 'GET') {
|
|
189
|
+
const id = path.slice('/assets/'.length);
|
|
190
|
+
const asset = this.options.assets.path(id);
|
|
191
|
+
if (!asset) {
|
|
192
|
+
response.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }).end('asset not found');
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
response.writeHead(200, {
|
|
196
|
+
'Content-Type': asset.mime,
|
|
197
|
+
'Cache-Control': 'public, max-age=31536000, immutable',
|
|
198
|
+
'X-Content-Type-Options': 'nosniff',
|
|
199
|
+
}).end(readFileSync(asset.path));
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (path !== '/stream' || request.method !== 'GET') {
|
|
203
|
+
response.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' }).end('GET /editor | /overlay | /stream');
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
response.writeHead(200, {
|
|
207
|
+
'Content-Type': 'text/event-stream; charset=utf-8',
|
|
208
|
+
'Cache-Control': 'no-cache, no-store, no-transform',
|
|
209
|
+
Connection: 'keep-alive',
|
|
210
|
+
'X-Accel-Buffering': 'no',
|
|
211
|
+
});
|
|
212
|
+
response.flushHeaders();
|
|
213
|
+
response.write(SSE_PAD);
|
|
214
|
+
response.write(`data: ${JSON.stringify({ type: 'snapshot', ...this.options.snapshot() })}\n\n`);
|
|
215
|
+
this.subscribers.add(response);
|
|
216
|
+
response.on('close', () => this.subscribers.delete(response));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private async onEditorRequest(request: IncomingMessage, response: ServerResponse, path: string): Promise<void> {
|
|
220
|
+
if (!this.sameOrigin(request)) {
|
|
221
|
+
this.json(response, 403, { error: '编辑器写入只接受同源请求' });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
if (path === '/api/editor/design' && request.method === 'PUT') {
|
|
226
|
+
const body = await this.jsonBody(request);
|
|
227
|
+
this.json(response, 200, await this.options.editor.saveDesign(body.design, body.baseRevision));
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (path === '/api/editor/assets' && request.method === 'POST') {
|
|
231
|
+
const body = await this.jsonBody(request);
|
|
232
|
+
this.json(response, 200, await this.options.editor.importAsset(body.base64));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (path.startsWith('/api/editor/assets/') && request.method === 'DELETE') {
|
|
236
|
+
this.json(response, 200, await this.options.editor.deleteAsset(decodeURIComponent(path.slice('/api/editor/assets/'.length))));
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (path === '/api/editor/announcement' && request.method === 'PUT') {
|
|
240
|
+
const body = await this.jsonBody(request);
|
|
241
|
+
this.json(response, 200, await this.options.editor.setAgentAnnouncement(body.text, body.expectedRevision));
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this.json(response, 404, { error: '编辑器接口不存在' });
|
|
245
|
+
} catch (error) {
|
|
246
|
+
this.json(response, error instanceof OverlayEditorConflictError ? 409 : 400, {
|
|
247
|
+
error: error instanceof Error ? error.message : String(error),
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private sameOrigin(request: IncomingMessage): boolean {
|
|
253
|
+
const origin = request.headers.origin;
|
|
254
|
+
if (!origin) return false;
|
|
255
|
+
try {
|
|
256
|
+
return new URL(origin).origin === this.baseUrl;
|
|
257
|
+
} catch {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private async jsonBody(request: IncomingMessage): Promise<Record<string, unknown>> {
|
|
263
|
+
if (!String(request.headers['content-type'] ?? '').toLowerCase().startsWith('application/json')) {
|
|
264
|
+
throw new Error('请求必须使用 application/json');
|
|
265
|
+
}
|
|
266
|
+
const chunks: Buffer[] = [];
|
|
267
|
+
let size = 0;
|
|
268
|
+
for await (const chunk of request) {
|
|
269
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
270
|
+
size += bytes.length;
|
|
271
|
+
if (size > MAX_EDITOR_BODY_BYTES) throw new Error('编辑器请求不能超过 12 MiB');
|
|
272
|
+
chunks.push(bytes);
|
|
273
|
+
}
|
|
274
|
+
const parsed: unknown = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
275
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw new Error('请求正文必须是 JSON 对象');
|
|
276
|
+
return parsed as Record<string, unknown>;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private json(response: ServerResponse, status: number, body: Record<string, unknown>): void {
|
|
280
|
+
response.writeHead(status, {
|
|
281
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
282
|
+
'Cache-Control': 'no-cache, no-store',
|
|
283
|
+
'X-Content-Type-Options': 'nosniff',
|
|
284
|
+
}).end(JSON.stringify(body));
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
export const OVERLAY_SCHEMA_VERSION = 4 as const;
|
|
2
|
+
|
|
3
|
+
type OverlayAxis = 'horizontal' | 'vertical';
|
|
4
|
+
type DanmakuAdmission = 'danmaku' | 'gift' | 'all';
|
|
5
|
+
type OverlayEventKind = 'danmaku' | 'gift';
|
|
6
|
+
type OverlayTitlePosition = 'top' | 'right' | 'bottom' | 'left';
|
|
7
|
+
type OverlayTitleAlign = 'left' | 'center' | 'right';
|
|
8
|
+
|
|
9
|
+
export interface OverlayTextStyle {
|
|
10
|
+
fontFamily: string;
|
|
11
|
+
fontSize: number;
|
|
12
|
+
fontWeight: number;
|
|
13
|
+
color: string;
|
|
14
|
+
strokeColor: string;
|
|
15
|
+
strokeWidth: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type OverlayTextStylePatch = Partial<OverlayTextStyle>;
|
|
19
|
+
|
|
20
|
+
export interface OverlayComponentTitle {
|
|
21
|
+
text: string;
|
|
22
|
+
position: OverlayTitlePosition;
|
|
23
|
+
align: OverlayTitleAlign;
|
|
24
|
+
style: OverlayTextStyle;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface OverlayInsets {
|
|
28
|
+
top: number;
|
|
29
|
+
right: number;
|
|
30
|
+
bottom: number;
|
|
31
|
+
left: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type NineSliceRepeat = 'stretch' | 'repeat' | 'round';
|
|
35
|
+
|
|
36
|
+
interface NineSliceStyle {
|
|
37
|
+
assetId: string;
|
|
38
|
+
/** Source-image cut lines, in source pixels. */
|
|
39
|
+
slice: OverlayInsets;
|
|
40
|
+
/** Destination border widths, in canvas pixels. */
|
|
41
|
+
width: OverlayInsets;
|
|
42
|
+
fill: boolean;
|
|
43
|
+
repeat: NineSliceRepeat;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface OverlayStyle {
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
background: string;
|
|
50
|
+
borderColor: string;
|
|
51
|
+
borderWidth: number;
|
|
52
|
+
radius: number;
|
|
53
|
+
padding: number;
|
|
54
|
+
username: OverlayTextStyle;
|
|
55
|
+
body: OverlayTextStyle;
|
|
56
|
+
nineSlice?: NineSliceStyle;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type AudienceField =
|
|
60
|
+
| 'uid'
|
|
61
|
+
| 'guardLevel'
|
|
62
|
+
| 'medalLevel'
|
|
63
|
+
| 'medalName'
|
|
64
|
+
| 'medalAnchorName'
|
|
65
|
+
| 'medalRoomId'
|
|
66
|
+
| 'medalColor'
|
|
67
|
+
| 'isAdmin'
|
|
68
|
+
| 'vip'
|
|
69
|
+
| 'svip'
|
|
70
|
+
| 'rank'
|
|
71
|
+
| 'nameColor'
|
|
72
|
+
| 'userLevel'
|
|
73
|
+
| 'eventKind';
|
|
74
|
+
|
|
75
|
+
export type AudienceCompare = 'eq' | 'gte' | 'lte' | 'exists' | 'contains';
|
|
76
|
+
|
|
77
|
+
export type AudienceRule =
|
|
78
|
+
| { op: 'all'; rules: AudienceRule[] }
|
|
79
|
+
| { op: 'any'; rules: AudienceRule[] }
|
|
80
|
+
| { op: 'leaf'; field: AudienceField; compare: AudienceCompare; value?: string | number | boolean };
|
|
81
|
+
|
|
82
|
+
export interface AudienceGroup {
|
|
83
|
+
id: string;
|
|
84
|
+
name: string;
|
|
85
|
+
enabled: boolean;
|
|
86
|
+
priority: number;
|
|
87
|
+
rule: AudienceRule;
|
|
88
|
+
username: OverlayTextStylePatch;
|
|
89
|
+
body: OverlayTextStylePatch;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface OverlayComponentBase {
|
|
93
|
+
id: string;
|
|
94
|
+
name: string;
|
|
95
|
+
styleId: string;
|
|
96
|
+
title?: OverlayComponentTitle;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
interface DanmakuComponent extends OverlayComponentBase {
|
|
100
|
+
kind: 'danmaku';
|
|
101
|
+
axis: OverlayAxis;
|
|
102
|
+
admission: DanmakuAdmission;
|
|
103
|
+
showAvatar: boolean;
|
|
104
|
+
speed: number;
|
|
105
|
+
gap: number;
|
|
106
|
+
maxItems: number;
|
|
107
|
+
usernameMaxChars: number;
|
|
108
|
+
bodyMaxChars: number;
|
|
109
|
+
edgeFadePx: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
interface ScrollNoticeComponent extends OverlayComponentBase {
|
|
113
|
+
kind: 'scroll-notice';
|
|
114
|
+
axis: OverlayAxis;
|
|
115
|
+
text: string;
|
|
116
|
+
speed: number;
|
|
117
|
+
gap: number;
|
|
118
|
+
lineHoldMs: number;
|
|
119
|
+
lineTransitionMs: number;
|
|
120
|
+
edgeFadePx: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface FixedNoticeComponent extends OverlayComponentBase {
|
|
124
|
+
kind: 'fixed-notice';
|
|
125
|
+
text: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
interface AgentNoticeComponent extends OverlayComponentBase {
|
|
129
|
+
kind: 'agent-notice';
|
|
130
|
+
emptyText: string;
|
|
131
|
+
hideWhenEmpty: boolean;
|
|
132
|
+
typingMs: number;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface ImageComponent extends OverlayComponentBase {
|
|
136
|
+
kind: 'image';
|
|
137
|
+
source: 'external' | 'upload';
|
|
138
|
+
url: string;
|
|
139
|
+
assetId: string;
|
|
140
|
+
fit: 'contain' | 'cover' | 'fill';
|
|
141
|
+
opacity: number;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type OverlayComponent =
|
|
145
|
+
| DanmakuComponent
|
|
146
|
+
| ScrollNoticeComponent
|
|
147
|
+
| FixedNoticeComponent
|
|
148
|
+
| AgentNoticeComponent
|
|
149
|
+
| ImageComponent;
|
|
150
|
+
|
|
151
|
+
export interface OverlayPlacement {
|
|
152
|
+
id: string;
|
|
153
|
+
componentId: string;
|
|
154
|
+
x: number;
|
|
155
|
+
y: number;
|
|
156
|
+
width: number;
|
|
157
|
+
height: number;
|
|
158
|
+
z: number;
|
|
159
|
+
visible: boolean;
|
|
160
|
+
locked: boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export interface BilibiliOverlayDesign {
|
|
164
|
+
schemaVersion: typeof OVERLAY_SCHEMA_VERSION;
|
|
165
|
+
canvas: { width: number; height: number };
|
|
166
|
+
styles: OverlayStyle[];
|
|
167
|
+
groups: AudienceGroup[];
|
|
168
|
+
components: OverlayComponent[];
|
|
169
|
+
placements: OverlayPlacement[];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface BilibiliOverlayConfig {
|
|
173
|
+
enabled: boolean;
|
|
174
|
+
port: number;
|
|
175
|
+
agentNoticeMaxChars: number;
|
|
176
|
+
design: BilibiliOverlayDesign;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface OverlayAudienceFacts {
|
|
180
|
+
uid?: string;
|
|
181
|
+
guardLevel?: number;
|
|
182
|
+
medalLevel?: number;
|
|
183
|
+
medalName?: string;
|
|
184
|
+
medalAnchorName?: string;
|
|
185
|
+
medalRoomId?: number;
|
|
186
|
+
medalColor?: number;
|
|
187
|
+
isAdmin?: boolean;
|
|
188
|
+
vip?: boolean;
|
|
189
|
+
svip?: boolean;
|
|
190
|
+
rank?: number;
|
|
191
|
+
nameColor?: string;
|
|
192
|
+
userLevel?: number;
|
|
193
|
+
eventKind: OverlayEventKind;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface OverlayAudienceEvent {
|
|
197
|
+
eventKind: OverlayEventKind;
|
|
198
|
+
username: string;
|
|
199
|
+
body: string;
|
|
200
|
+
avatarUrl: string;
|
|
201
|
+
facts: OverlayAudienceFacts;
|
|
202
|
+
groupId?: string;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface AgentAnnouncementState {
|
|
206
|
+
schemaVersion: 1;
|
|
207
|
+
text: string;
|
|
208
|
+
revision: number;
|
|
209
|
+
updatedAt: string | null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface OverlayAssetInfo {
|
|
213
|
+
id: string;
|
|
214
|
+
mime: string;
|
|
215
|
+
size: number;
|
|
216
|
+
url: string;
|
|
217
|
+
}
|