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,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 观察者客户端那扇窗:有没有出来、改标题。
|
|
3
|
+
*
|
|
4
|
+
* 走 Windows PowerShell + user32(见 client-window.ps1),不引第三方原生依赖。
|
|
5
|
+
* 认哪扇窗一律按 `ownerPid`:同一台机器上还有人自己玩的那份客户端,
|
|
6
|
+
* 除了进程没有第二个凭据分得开她的窗口和别人的窗口。标题改成账号名是给 OBS 认的。
|
|
7
|
+
*/
|
|
8
|
+
import { spawn } from 'node:child_process';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import type { Logger } from '../../core/types.ts';
|
|
11
|
+
|
|
12
|
+
const SCRIPT = fileURLToPath(new URL('./client-window.ps1', import.meta.url));
|
|
13
|
+
|
|
14
|
+
interface ScriptOutput {
|
|
15
|
+
ok?: boolean;
|
|
16
|
+
error?: string;
|
|
17
|
+
title?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function runScript(args: string[], timeoutMs: number): Promise<string> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const proc = spawn(
|
|
23
|
+
'powershell.exe',
|
|
24
|
+
['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass', '-File', SCRIPT, ...args],
|
|
25
|
+
{ windowsHide: true },
|
|
26
|
+
);
|
|
27
|
+
let out = '';
|
|
28
|
+
let err = '';
|
|
29
|
+
const timer = setTimeout(() => {
|
|
30
|
+
try { proc.kill(); } catch { /* 已退出 */ }
|
|
31
|
+
reject(new Error(`窗口探测超时 ${timeoutMs}ms`));
|
|
32
|
+
}, timeoutMs);
|
|
33
|
+
proc.stdout.on('data', (c: Buffer) => { out += c.toString(); });
|
|
34
|
+
proc.stderr.on('data', (c: Buffer) => { err += c.toString(); });
|
|
35
|
+
proc.on('error', (e) => { clearTimeout(timer); reject(e); });
|
|
36
|
+
proc.on('close', (code) => {
|
|
37
|
+
clearTimeout(timer);
|
|
38
|
+
if (code !== 0 && !out.trim()) reject(new Error(`powershell code=${code} ${err.slice(-300)}`));
|
|
39
|
+
else resolve(out);
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseLast(raw: string): ScriptOutput | null {
|
|
45
|
+
const line = raw.trim().split(/\r?\n/).filter(Boolean).pop() ?? '';
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(line) as ScriptOutput;
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 客户端就绪判定:进程名下出现了一扇可见窗口。非 Windows 一律 false。 */
|
|
54
|
+
export async function findWindow(opts: { ownerPid: number; log?: Logger }): Promise<boolean> {
|
|
55
|
+
if (process.platform !== 'win32') return false;
|
|
56
|
+
let raw: string;
|
|
57
|
+
try {
|
|
58
|
+
raw = await runScript(['-OwnerPid', String(opts.ownerPid)], 15_000);
|
|
59
|
+
} catch (err) {
|
|
60
|
+
opts.log?.warn(`窗口探测失败: ${(err as Error).message}`);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
const parsed = parseLast(raw);
|
|
64
|
+
if (!parsed) {
|
|
65
|
+
opts.log?.warn(`窗口探测输出看不懂: ${raw.trim().slice(-200)}`);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
if (!parsed.ok && parsed.error && parsed.error !== 'no-window') opts.log?.warn(`窗口探测: ${parsed.error}`);
|
|
69
|
+
return parsed.ok === true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 按进程改窗口标题。给 OBS 按标题区分两份客户端;失败不抛。 */
|
|
73
|
+
export async function setWindowTitle(opts: {
|
|
74
|
+
ownerPid: number;
|
|
75
|
+
title: string;
|
|
76
|
+
log?: Logger;
|
|
77
|
+
}): Promise<boolean> {
|
|
78
|
+
if (process.platform !== 'win32' || !opts.title) return false;
|
|
79
|
+
let raw: string;
|
|
80
|
+
try {
|
|
81
|
+
raw = await runScript(['-OwnerPid', String(opts.ownerPid), '-SetTitle', opts.title], 8_000);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
opts.log?.warn(`窗口标题未改成 ${opts.title}: ${(err as Error).message}`);
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const parsed = parseLast(raw);
|
|
87
|
+
if (!parsed) {
|
|
88
|
+
opts.log?.warn(`窗口标题输出看不懂: ${raw.trim().slice(-200)}`);
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
if (parsed.ok) return true;
|
|
92
|
+
if (parsed.error && parsed.error !== 'no-window') {
|
|
93
|
+
opts.log?.warn(`窗口标题未改成 ${opts.title}: ${parsed.error}`);
|
|
94
|
+
}
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 按维度与坐标持久化已完成成果,跨任务保留。
|
|
3
|
+
* 寻路禁止在登记格头顶垫脚;显式挖掘与放置仍执行,并在回执中报告涉及的成果数量。
|
|
4
|
+
*/
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
7
|
+
|
|
8
|
+
/** 登记的三类。`blueprint` 是完工蓝图占的格,`farmland` 是锄成的耕地,`crop` 是种下的作物 */
|
|
9
|
+
type WorkKind = 'blueprint' | 'farmland' | 'crop';
|
|
10
|
+
|
|
11
|
+
const WORK_KIND_ZH: Record<WorkKind, string> = {
|
|
12
|
+
blueprint: '建成的蓝图', farmland: '耕地', crop: '作物',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
interface WorkCell {
|
|
16
|
+
kind: WorkKind;
|
|
17
|
+
/** 登记那一刻这一格是什么(蓝图记材料名、耕地记 farmland、作物记作物方块名) */
|
|
18
|
+
block: string;
|
|
19
|
+
/** 蓝图记图名,别的为 null */
|
|
20
|
+
site: string | null;
|
|
21
|
+
/** 登记时刻(epoch ms) */
|
|
22
|
+
at: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 落盘形状:维度 → "x,y,z" → 一格 */
|
|
26
|
+
type WorksFile = Record<string, Record<string, WorkCell>>;
|
|
27
|
+
|
|
28
|
+
const WORK_KINDS = new Set<string>(['blueprint', 'farmland', 'crop']);
|
|
29
|
+
|
|
30
|
+
export function cellKey(x: number, y: number, z: number): string {
|
|
31
|
+
return `${x},${y},${z}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function parseFile(raw: string): WorksFile {
|
|
35
|
+
let data: unknown;
|
|
36
|
+
try {
|
|
37
|
+
data = JSON.parse(raw);
|
|
38
|
+
} catch {
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
if (typeof data !== 'object' || data === null) return {};
|
|
42
|
+
const out: WorksFile = {};
|
|
43
|
+
for (const [dim, cells] of Object.entries(data as Record<string, unknown>)) {
|
|
44
|
+
if (typeof cells !== 'object' || cells === null) continue;
|
|
45
|
+
const kept: Record<string, WorkCell> = {};
|
|
46
|
+
for (const [key, v] of Object.entries(cells as Record<string, unknown>)) {
|
|
47
|
+
const c = v as Partial<WorkCell>;
|
|
48
|
+
if (!WORK_KINDS.has(String(c.kind)) || typeof c.block !== 'string') continue;
|
|
49
|
+
kept[key] = {
|
|
50
|
+
kind: c.kind as WorkKind,
|
|
51
|
+
block: c.block,
|
|
52
|
+
site: typeof c.site === 'string' ? c.site : null,
|
|
53
|
+
at: typeof c.at === 'number' ? c.at : 0,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (Object.keys(kept).length > 0) out[dim] = kept;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function loadWorks(file: string | null): WorksFile {
|
|
62
|
+
if (!file || !existsSync(file)) return {};
|
|
63
|
+
try {
|
|
64
|
+
const raw = readFileSync(file, 'utf8').trim();
|
|
65
|
+
return raw ? parseFile(raw) : {};
|
|
66
|
+
} catch {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface WorkHit extends WorkCell {
|
|
72
|
+
x: number;
|
|
73
|
+
y: number;
|
|
74
|
+
z: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class WorksBook {
|
|
78
|
+
private works: WorksFile;
|
|
79
|
+
|
|
80
|
+
constructor(private readonly file: string | null) {
|
|
81
|
+
this.works = loadWorks(file);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 登记一格。同一格重登(耕地被踩回去又锄一遍)覆盖旧的,时刻跟着刷新 */
|
|
85
|
+
note(dimension: string, x: number, y: number, z: number, cell: Omit<WorkCell, 'at'>, now = Date.now()): void {
|
|
86
|
+
const dim = (this.works[dimension] ??= {});
|
|
87
|
+
dim[cellKey(x, y, z)] = { ...cell, at: now };
|
|
88
|
+
this.save();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 一批一起登记(蓝图完工):只落一次盘 */
|
|
92
|
+
noteMany(
|
|
93
|
+
dimension: string,
|
|
94
|
+
cells: ReadonlyArray<{ x: number; y: number; z: number } & Omit<WorkCell, 'at'>>,
|
|
95
|
+
now = Date.now(),
|
|
96
|
+
): void {
|
|
97
|
+
if (cells.length === 0) return;
|
|
98
|
+
const dim = (this.works[dimension] ??= {});
|
|
99
|
+
for (const c of cells) {
|
|
100
|
+
dim[cellKey(c.x, c.y, c.z)] = { kind: c.kind, block: c.block, site: c.site, at: now };
|
|
101
|
+
}
|
|
102
|
+
this.save();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
at(dimension: string, x: number, y: number, z: number): WorkCell | null {
|
|
106
|
+
return this.works[dimension]?.[cellKey(x, y, z)] ?? null;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
has(dimension: string, x: number, y: number, z: number): boolean {
|
|
110
|
+
return this.at(dimension, x, y, z) !== null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 这一批格子里哪些在登记上;按传入顺序返回 */
|
|
114
|
+
inCells(
|
|
115
|
+
dimension: string,
|
|
116
|
+
cells: ReadonlyArray<{ x: number; y: number; z: number }>,
|
|
117
|
+
): WorkHit[] {
|
|
118
|
+
const dim = this.works[dimension];
|
|
119
|
+
if (!dim) return [];
|
|
120
|
+
const out: WorkHit[] = [];
|
|
121
|
+
for (const c of cells) {
|
|
122
|
+
const hit = dim[cellKey(c.x, c.y, c.z)];
|
|
123
|
+
if (hit) out.push({ ...hit, x: c.x, y: c.y, z: c.z });
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** 撤销一格的登记:她把它拆了/挖了,登记不该留着骗下一次 */
|
|
129
|
+
forget(dimension: string, x: number, y: number, z: number): void {
|
|
130
|
+
const dim = this.works[dimension];
|
|
131
|
+
if (!dim?.[cellKey(x, y, z)]) return;
|
|
132
|
+
delete dim[cellKey(x, y, z)];
|
|
133
|
+
if (Object.keys(dim).length === 0) delete this.works[dimension];
|
|
134
|
+
this.save();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
count(dimension?: string): number {
|
|
138
|
+
if (dimension !== undefined) return Object.keys(this.works[dimension] ?? {}).length;
|
|
139
|
+
return Object.values(this.works).reduce((n, d) => n + Object.keys(d).length, 0);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
stat(): string {
|
|
143
|
+
const n = this.count();
|
|
144
|
+
if (!this.file || !existsSync(this.file)) return n === 0 ? '(无文件)' : `${n} 格(未落盘)`;
|
|
145
|
+
const kb = (statSync(this.file).size / 1024).toFixed(1);
|
|
146
|
+
return `${n} 格 / ${kb}KB`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
clear(): string {
|
|
150
|
+
const n = this.count();
|
|
151
|
+
this.works = {};
|
|
152
|
+
if (this.file) {
|
|
153
|
+
mkdirSync(dirname(this.file), { recursive: true });
|
|
154
|
+
writeFileSync(this.file, '{}\n', 'utf8');
|
|
155
|
+
}
|
|
156
|
+
return n === 0 ? '成果登记是空的' : `成果登记已清空(${n} 格)`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private save(): void {
|
|
160
|
+
if (!this.file) return;
|
|
161
|
+
mkdirSync(dirname(this.file), { recursive: true });
|
|
162
|
+
writeFileSync(this.file, `${JSON.stringify(this.works)}\n`, 'utf8');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 「这一框里有你 N 格登记的耕地/作物」那句。按类别合并计数,各报最早那一笔的时刻;
|
|
168
|
+
* 一格都没有返回 null(不出这一行)。
|
|
169
|
+
*/
|
|
170
|
+
export function worksNote(hits: readonly WorkHit[], clock: (ms: number) => string): string | null {
|
|
171
|
+
if (hits.length === 0) return null;
|
|
172
|
+
const byKind = new Map<WorkKind, { n: number; at: number }>();
|
|
173
|
+
for (const h of hits) {
|
|
174
|
+
const cur = byKind.get(h.kind);
|
|
175
|
+
if (cur) { cur.n++; cur.at = Math.min(cur.at, h.at); } else byKind.set(h.kind, { n: 1, at: h.at });
|
|
176
|
+
}
|
|
177
|
+
const bits = [...byKind].map(([kind, v]) => `${v.n} 格${WORK_KIND_ZH[kind]}(记于 ${clock(v.at)})`);
|
|
178
|
+
return `这一框里有你${bits.join('、')}`;
|
|
179
|
+
}
|