modkh 0.0.0-stage → 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/README.md +24 -2
- package/dist/agent/bundled-skills.d.ts +13 -0
- package/dist/agent/bundled-skills.js +95 -0
- package/dist/agent/images.d.ts +10 -0
- package/dist/agent/images.js +33 -0
- package/dist/agent/loop.d.ts +56 -0
- package/dist/agent/loop.js +155 -0
- package/dist/agent/skills.d.ts +20 -0
- package/dist/agent/skills.js +169 -0
- package/dist/agent/types.d.ts +132 -0
- package/dist/agent/types.js +12 -0
- package/dist/cli/agents.d.ts +8 -0
- package/dist/cli/agents.js +12 -0
- package/dist/cli/approval.d.ts +27 -0
- package/dist/cli/approval.js +62 -0
- package/dist/cli/catalog.d.ts +39 -0
- package/dist/cli/catalog.js +76 -0
- package/dist/cli/chat.d.ts +18 -0
- package/dist/cli/chat.js +733 -0
- package/dist/cli/clipboard.d.ts +9 -0
- package/dist/cli/clipboard.js +54 -0
- package/dist/cli/color.d.ts +42 -0
- package/dist/cli/color.js +176 -0
- package/dist/cli/config.d.ts +41 -0
- package/dist/cli/config.js +188 -0
- package/dist/cli/editor.d.ts +16 -0
- package/dist/cli/editor.js +77 -0
- package/dist/cli/keys.d.ts +18 -0
- package/dist/cli/keys.js +82 -0
- package/dist/cli/language.d.ts +1 -0
- package/dist/cli/language.js +105 -0
- package/dist/cli/limits.d.ts +13 -0
- package/dist/cli/limits.js +19 -0
- package/dist/cli/lines.d.ts +18 -0
- package/dist/cli/lines.js +80 -0
- package/dist/cli/models.d.ts +4 -0
- package/dist/cli/models.js +69 -0
- package/dist/cli/permissions.d.ts +25 -0
- package/dist/cli/permissions.js +30 -0
- package/dist/cli/plugins.d.ts +1 -0
- package/dist/cli/plugins.js +53 -0
- package/dist/cli/providers.d.ts +46 -0
- package/dist/cli/providers.js +76 -0
- package/dist/cli/terminal.d.ts +143 -0
- package/dist/cli/terminal.js +742 -0
- package/dist/cli/usage.d.ts +29 -0
- package/dist/cli/usage.js +75 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +164 -0
- package/dist/language/catalog.d.ts +44 -0
- package/dist/language/catalog.js +100 -0
- package/dist/language/commonlisp-bundled.d.ts +11 -0
- package/dist/language/commonlisp-bundled.js +22 -0
- package/dist/language/commonlisp-dependencies.d.ts +16 -0
- package/dist/language/commonlisp-dependencies.js +68 -0
- package/dist/language/commonlisp-install.d.ts +14 -0
- package/dist/language/commonlisp-install.js +213 -0
- package/dist/language/commonlisp-reader.d.ts +17 -0
- package/dist/language/commonlisp-reader.js +47 -0
- package/dist/language/commonlisp-systems.d.ts +3 -0
- package/dist/language/commonlisp-systems.js +32 -0
- package/dist/language/commonlisp-tls.d.ts +10 -0
- package/dist/language/commonlisp-tls.js +51 -0
- package/dist/language/detect.d.ts +29 -0
- package/dist/language/detect.js +103 -0
- package/dist/language/grammars/LICENSE +24 -0
- package/dist/language/grammars/manifest.json +76 -0
- package/dist/language/grammars/tree-sitter-c.wasm +0 -0
- package/dist/language/grammars/tree-sitter-cpp.wasm +0 -0
- package/dist/language/grammars/tree-sitter-css.wasm +0 -0
- package/dist/language/grammars/tree-sitter-html.wasm +0 -0
- package/dist/language/grammars/tree-sitter-javascript.wasm +0 -0
- package/dist/language/grammars/tree-sitter-php.wasm +0 -0
- package/dist/language/grammars/tree-sitter-python.wasm +0 -0
- package/dist/language/grammars/tree-sitter-ruby.wasm +0 -0
- package/dist/language/grammars/tree-sitter-rust.wasm +0 -0
- package/dist/language/grammars/tree-sitter-swift.wasm +0 -0
- package/dist/language/grammars/tree-sitter-tsx.wasm +0 -0
- package/dist/language/grammars/tree-sitter-typescript.wasm +0 -0
- package/dist/language/install.d.ts +50 -0
- package/dist/language/install.js +293 -0
- package/dist/language/lsp/client.d.ts +97 -0
- package/dist/language/lsp/client.js +337 -0
- package/dist/language/lsp/pool.d.ts +67 -0
- package/dist/language/lsp/pool.js +122 -0
- package/dist/language/lsp/service.d.ts +46 -0
- package/dist/language/lsp/service.js +138 -0
- package/dist/language/native.d.ts +8 -0
- package/dist/language/native.js +100 -0
- package/dist/language/platform.d.ts +14 -0
- package/dist/language/platform.js +20 -0
- package/dist/language/positions.d.ts +20 -0
- package/dist/language/positions.js +46 -0
- package/dist/language/syntax/assets.d.ts +9 -0
- package/dist/language/syntax/assets.js +73 -0
- package/dist/language/syntax/parser.d.ts +24 -0
- package/dist/language/syntax/parser.js +144 -0
- package/dist/language/typescript-capability.d.ts +6 -0
- package/dist/language/typescript-capability.js +42 -0
- package/dist/lisp/config.d.ts +16 -0
- package/dist/lisp/config.js +159 -0
- package/dist/lisp/routing.d.ts +5 -0
- package/dist/lisp/routing.js +24 -0
- package/dist/lisp/runtime.d.ts +21 -0
- package/dist/lisp/runtime.js +83 -0
- package/dist/memory/kiokuko.d.ts +32 -0
- package/dist/memory/kiokuko.js +102 -0
- package/dist/memory/mcp.d.ts +38 -0
- package/dist/memory/mcp.js +211 -0
- package/dist/model/ai-sdk.d.ts +27 -0
- package/dist/model/ai-sdk.js +180 -0
- package/dist/model/chatgpt/login.d.ts +42 -0
- package/dist/model/chatgpt/login.js +180 -0
- package/dist/model/chatgpt/responses.d.ts +41 -0
- package/dist/model/chatgpt/responses.js +202 -0
- package/dist/model/chatgpt/tokens.d.ts +11 -0
- package/dist/model/chatgpt/tokens.js +50 -0
- package/dist/model/connection.d.ts +6 -0
- package/dist/model/connection.js +65 -0
- package/dist/model/context.d.ts +9 -0
- package/dist/model/context.js +69 -0
- package/dist/model/factory.d.ts +25 -0
- package/dist/model/factory.js +88 -0
- package/dist/model/fixed.d.ts +17 -0
- package/dist/model/fixed.js +41 -0
- package/dist/model/reasoning.d.ts +16 -0
- package/dist/model/reasoning.js +62 -0
- package/dist/plugins/api.d.ts +60 -0
- package/dist/plugins/api.js +1 -0
- package/dist/plugins/manager.d.ts +34 -0
- package/dist/plugins/manager.js +261 -0
- package/dist/plugins/package.d.ts +13 -0
- package/dist/plugins/package.js +99 -0
- package/dist/plugins/registry.d.ts +8 -0
- package/dist/plugins/registry.js +14 -0
- package/dist/session/store.d.ts +82 -0
- package/dist/session/store.js +285 -0
- package/dist/session/workspace.d.ts +19 -0
- package/dist/session/workspace.js +73 -0
- package/dist/subagents/processes.d.ts +18 -0
- package/dist/subagents/processes.js +73 -0
- package/dist/subagents/request.d.ts +4 -0
- package/dist/subagents/request.js +21 -0
- package/dist/subagents/service.d.ts +39 -0
- package/dist/subagents/service.js +352 -0
- package/dist/subagents/settings.d.ts +10 -0
- package/dist/subagents/settings.js +32 -0
- package/dist/subagents/types.d.ts +43 -0
- package/dist/subagents/types.js +1 -0
- package/dist/subagents/verification.d.ts +31 -0
- package/dist/subagents/verification.js +178 -0
- package/dist/tools/diff.d.ts +6 -0
- package/dist/tools/diff.js +82 -0
- package/dist/tools/edit.d.ts +3 -0
- package/dist/tools/edit.js +152 -0
- package/dist/tools/files.d.ts +6 -0
- package/dist/tools/files.js +96 -0
- package/dist/tools/formal.d.ts +8 -0
- package/dist/tools/formal.js +77 -0
- package/dist/tools/guard.d.ts +7 -0
- package/dist/tools/guard.js +48 -0
- package/dist/tools/job-tools.d.ts +19 -0
- package/dist/tools/job-tools.js +148 -0
- package/dist/tools/jobs.d.ts +77 -0
- package/dist/tools/jobs.js +181 -0
- package/dist/tools/language.d.ts +6 -0
- package/dist/tools/language.js +174 -0
- package/dist/tools/registry.d.ts +28 -0
- package/dist/tools/registry.js +105 -0
- package/dist/tools/run.d.ts +13 -0
- package/dist/tools/run.js +159 -0
- package/dist/tools/search.d.ts +2 -0
- package/dist/tools/search.js +156 -0
- package/dist/tools/shell-process.d.ts +115 -0
- package/dist/tools/shell-process.js +286 -0
- package/dist/tools/skills.d.ts +2 -0
- package/dist/tools/skills.js +46 -0
- package/dist/tools/web.d.ts +24 -0
- package/dist/tools/web.js +386 -0
- package/docs/ci-repair-20261116.md +30 -0
- package/docs/commonlisp-provider-migration.md +22 -0
- package/docs/commonlisp-qlot-recovery.md +28 -0
- package/docs/commonlisp-tls-recovery.md +28 -0
- package/docs/context-efficiency.md +100 -0
- package/docs/input.md +66 -0
- package/docs/languages.md +117 -0
- package/docs/models.md +85 -0
- package/docs/modkh-evidence.json +641 -0
- package/docs/modkh-failure-comparison.json +675 -0
- package/docs/modkh-namespace.md +39 -0
- package/docs/modkh-qlot-evidence.json +37 -0
- package/docs/modkh-verification.md +173 -0
- package/docs/modkh.md +124 -0
- package/docs/presets.md +19 -0
- package/docs/setup.md +112 -0
- package/docs/subagents.md +134 -0
- package/docs/web.md +57 -0
- package/experiments/presets/common-lisp.lisp +38 -0
- package/experiments/presets/css.lisp +38 -0
- package/experiments/presets/formal.lisp +7 -0
- package/experiments/presets/html.lisp +38 -0
- package/experiments/presets/js.lisp +38 -0
- package/experiments/presets/php.lisp +38 -0
- package/experiments/presets/svelte.lisp +38 -0
- package/experiments/presets/tailwindcss.lisp +24 -0
- package/experiments/presets/ts.lisp +38 -0
- package/package.json +49 -4
- package/plugins/modkh-explorer/index.mjs +3 -0
- package/plugins/modkh-explorer/plugin.json +1 -0
- package/plugins/modkh-worker/index.mjs +3 -0
- package/plugins/modkh-worker/plugin.json +1 -0
- package/skills/kiokuko-dsh/LICENSE +21 -0
- package/skills/kiokuko-dsh/coding-ideal-routine-skill/SKILL.md +106 -0
- package/skills/kiokuko-dsh/japanese-translation-for-oss-models/SKILL.md +65 -0
- package/skills/kiokuko-dsh/kiokuko-architecture/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-architecture/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-benchmark/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-benchmark/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-investigate/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-investigate/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-lisp/SKILL.md +390 -0
- package/skills/kiokuko-dsh/kiokuko-review/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-review/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-simple-work/SKILL.md +70 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/SKILL.md +79 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/boundaries-and-ownership.md +52 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/domain-and-types.md +48 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/effects-and-data.md +43 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/kiokuko-patterns.md +314 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/problem-shaping-and-language.md +90 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/protocols-and-idempotency.md +42 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/review-checklist.md +92 -0
- package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/verification.md +45 -0
- package/skills/kiokuko-dsh/kiokuko-skill-authoring/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-skill-authoring/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-soul/SKILL.md +25 -0
- package/skills/kiokuko-dsh/kiokuko-technical-writing/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-technical-writing/references/cases.md +11 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/SKILL.md +64 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/accessibility-and-navigation.md +31 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/async-recovery.md +49 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/forms-and-controls.md +35 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/interaction-feedback.md +34 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/responsive-and-platform.md +38 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/safety-and-review.md +43 -0
- package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/ui-checklist.md +75 -0
- package/skills/kiokuko-dsh/kiokuko-verification/SKILL.md +20 -0
- package/skills/kiokuko-dsh/kiokuko-verification/references/cases.md +11 -0
- package/skills/kiokuko-dsh/manifest.json +168 -0
- package/skills/kiokuko-dsh/memory-reasoning/SKILL.md +44 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/SKILL.md +113 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/boundaries-and-lifecycle.md +70 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/discovery-and-scope.md +71 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/failure-recovery.md +43 -0
- package/skills/kiokuko-dsh/one-shot-software-completion/references/verification-and-completion.md +73 -0
- package/skills/kiokuko-dsh/veteran-programmer-skill/SKILL.md +66 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ImageAttachment } from '../agent/images.ts';
|
|
2
|
+
/** Read only on an explicit Ctrl+V. No clipboard text, files, credentials or network access. */
|
|
3
|
+
export declare const MAC_IMAGE_SCRIPT = "\nObjC.import('AppKit');\nObjC.import('Foundation');\nconst board = $.NSPasteboard.generalPasteboard;\nconst revision = Number(board.changeCount);\nlet data = board.dataForType('public.png');\nif (data.isNil()) {\n const tiff = board.dataForType('public.tiff');\n if (tiff.isNil()) throw new Error('\u30AF\u30EA\u30C3\u30D7\u30DC\u30FC\u30C9\u306B\u753B\u50CF\u304C\u3042\u308A\u307E\u305B\u3093');\n if (Number(tiff.length) > 40 * 1024 * 1024) throw new Error('\u753B\u50CF\u304C\u5927\u304D\u3059\u304E\u307E\u3059');\n const bitmap = $.NSBitmapImageRep.imageRepWithData(tiff);\n if (bitmap.isNil()) throw new Error('\u753B\u50CF\u3092\u8AAD\u307F\u53D6\u308C\u307E\u305B\u3093');\n if (Number(bitmap.pixelsWide) * Number(bitmap.pixelsHigh) > 40000000) throw new Error('\u753B\u50CF\u306F4,000\u4E07\u753B\u7D20\u4EE5\u5185\u306B\u3057\u3066\u304F\u3060\u3055\u3044');\n data = bitmap.representationUsingTypeProperties($.NSBitmapImageFileTypePNG, $.NSDictionary.dictionary);\n}\nif (data.isNil()) throw new Error('PNG\u3078\u306E\u5909\u63DB\u306B\u5931\u6557\u3057\u307E\u3057\u305F');\nif (Number(data.length) > 5242880) throw new Error('\u753B\u50CF\u306F1\u679A5 MiB\u4EE5\u5185\u306B\u3057\u3066\u304F\u3060\u3055\u3044');\nif (Number(board.changeCount) !== revision) throw new Error('\u30AF\u30EA\u30C3\u30D7\u30DC\u30FC\u30C9\u304C\u5909\u66F4\u3055\u308C\u307E\u3057\u305F\u3002\u3082\u3046\u4E00\u5EA6Ctrl+V\u3057\u3066\u304F\u3060\u3055\u3044');\nJSON.stringify({mediaType:'image/png', data:ObjC.unwrap(data.base64EncodedStringWithOptions(0))});\n";
|
|
4
|
+
export type ClipboardReader = (signal: AbortSignal) => Promise<ImageAttachment>;
|
|
5
|
+
export type ClipboardCommand = (signal: AbortSignal) => Promise<string>;
|
|
6
|
+
export declare function readClipboardImage(signal: AbortSignal, options?: {
|
|
7
|
+
platform?: string;
|
|
8
|
+
command?: ClipboardCommand;
|
|
9
|
+
}): Promise<ImageAttachment>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { MAX_IMAGE_BYTES, parseImage } from "../agent/images.js";
|
|
3
|
+
/** Read only on an explicit Ctrl+V. No clipboard text, files, credentials or network access. */
|
|
4
|
+
export const MAC_IMAGE_SCRIPT = `
|
|
5
|
+
ObjC.import('AppKit');
|
|
6
|
+
ObjC.import('Foundation');
|
|
7
|
+
const board = $.NSPasteboard.generalPasteboard;
|
|
8
|
+
const revision = Number(board.changeCount);
|
|
9
|
+
let data = board.dataForType('public.png');
|
|
10
|
+
if (data.isNil()) {
|
|
11
|
+
const tiff = board.dataForType('public.tiff');
|
|
12
|
+
if (tiff.isNil()) throw new Error('クリップボードに画像がありません');
|
|
13
|
+
if (Number(tiff.length) > 40 * 1024 * 1024) throw new Error('画像が大きすぎます');
|
|
14
|
+
const bitmap = $.NSBitmapImageRep.imageRepWithData(tiff);
|
|
15
|
+
if (bitmap.isNil()) throw new Error('画像を読み取れません');
|
|
16
|
+
if (Number(bitmap.pixelsWide) * Number(bitmap.pixelsHigh) > 40000000) throw new Error('画像は4,000万画素以内にしてください');
|
|
17
|
+
data = bitmap.representationUsingTypeProperties($.NSBitmapImageFileTypePNG, $.NSDictionary.dictionary);
|
|
18
|
+
}
|
|
19
|
+
if (data.isNil()) throw new Error('PNGへの変換に失敗しました');
|
|
20
|
+
if (Number(data.length) > ${MAX_IMAGE_BYTES}) throw new Error('画像は1枚5 MiB以内にしてください');
|
|
21
|
+
if (Number(board.changeCount) !== revision) throw new Error('クリップボードが変更されました。もう一度Ctrl+Vしてください');
|
|
22
|
+
JSON.stringify({mediaType:'image/png', data:ObjC.unwrap(data.base64EncodedStringWithOptions(0))});
|
|
23
|
+
`;
|
|
24
|
+
function macClipboardCommand(signal) {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
execFile('/usr/bin/osascript', ['-l', 'JavaScript', '-e', MAC_IMAGE_SCRIPT], {
|
|
27
|
+
signal, timeout: 5000, killSignal: 'SIGKILL', encoding: 'utf8',
|
|
28
|
+
maxBuffer: Math.ceil(MAX_IMAGE_BYTES / 3) * 4 + 4096,
|
|
29
|
+
env: { PATH: '/usr/bin:/bin', HOME: process.env.HOME, TMPDIR: process.env.TMPDIR, LANG: process.env.LANG },
|
|
30
|
+
}, (error, stdout, stderr) => {
|
|
31
|
+
if (error)
|
|
32
|
+
reject(new Error(signal.aborted ? '画像の読み取りを中止しました' : `画像を貼り付けられません: ${stderr.trim().slice(0, 240) || error.message}`));
|
|
33
|
+
else
|
|
34
|
+
resolve(stdout);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export async function readClipboardImage(signal, options = {}) {
|
|
39
|
+
signal.throwIfAborted();
|
|
40
|
+
if ((options.platform ?? process.platform) !== 'darwin')
|
|
41
|
+
throw new Error('Ctrl+Vの画像貼り付けは現在macOSのみ対応しています');
|
|
42
|
+
const output = await (options.command ?? macClipboardCommand)(signal);
|
|
43
|
+
signal.throwIfAborted();
|
|
44
|
+
if (output.length > Math.ceil(MAX_IMAGE_BYTES / 3) * 4 + 1024)
|
|
45
|
+
throw new Error('画像が大きすぎます');
|
|
46
|
+
let value;
|
|
47
|
+
try {
|
|
48
|
+
value = JSON.parse(output);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error('クリップボードの画像を読み取れませんでした');
|
|
52
|
+
}
|
|
53
|
+
return parseImage(value);
|
|
54
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export type Background = 'dark' | 'light';
|
|
2
|
+
export interface Palette {
|
|
3
|
+
reasoning: string;
|
|
4
|
+
user: string;
|
|
5
|
+
tool: string;
|
|
6
|
+
result: string;
|
|
7
|
+
system: string;
|
|
8
|
+
heading: string;
|
|
9
|
+
accent: string;
|
|
10
|
+
diffAdd: string;
|
|
11
|
+
diffDel: string;
|
|
12
|
+
diffMeta: string;
|
|
13
|
+
codeComment: string;
|
|
14
|
+
codeString: string;
|
|
15
|
+
codeNumber: string;
|
|
16
|
+
codeKeyword: string;
|
|
17
|
+
}
|
|
18
|
+
/** ダーク背景は明るい標準色、ライト背景は256色パレットの暗い色。 */
|
|
19
|
+
export declare function paletteFor(background: Background): Palette;
|
|
20
|
+
/** `rgb:rrrr/gggg/bbbb` または `color:0/9` 形式の OSC 11 応答を解析する。 */
|
|
21
|
+
export declare function parseOscBackground(text: string): string | null;
|
|
22
|
+
/** 0〜65535 の16進3成分から輝度を0..1で出す。 */
|
|
23
|
+
export declare function backgroundLuminance(osc: string): number | null;
|
|
24
|
+
/** OSC応答または COLORFGBG 環境変数から背景を分類する。根拠がなければ null。 */
|
|
25
|
+
export declare function classifyBackground(oscReply: string | null, env?: NodeJS.ProcessEnv): 'dark' | 'light' | null;
|
|
26
|
+
/** コード fenced block 内の1行を簡易ハイライトする。 */
|
|
27
|
+
export declare function highlightCodeLine(line: string, palette: Palette): string;
|
|
28
|
+
export type ColorMode = 'text' | 'reasoning' | 'code';
|
|
29
|
+
export interface ColorizerState {
|
|
30
|
+
mode: ColorMode;
|
|
31
|
+
inDiff: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** 連続する行に状態(推論・コードブロック・diff)を引き継いで色を付ける。 */
|
|
34
|
+
export declare class TranscriptColorizer {
|
|
35
|
+
private mode;
|
|
36
|
+
private inDiff;
|
|
37
|
+
/** 完成行までの状態を保存しておき、未完了行(ライブ表示)の色分けに複製する。 */
|
|
38
|
+
state(): ColorizerState;
|
|
39
|
+
restore(state: ColorizerState): void;
|
|
40
|
+
line(raw: string, palette: Palette): string;
|
|
41
|
+
private diffLine;
|
|
42
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* トランスクリプトの行単位の色分け。外部依存なし。
|
|
3
|
+
* - 推論ブロック((推論)から次の構造マーカーまで)は薄い色
|
|
4
|
+
* - diff(+/-/@@/---/+++)は追加=緑・削除=赤
|
|
5
|
+
* - ``` コードブロック内は簡易トークナイザー(文字列・コメント・キーワード・数値)
|
|
6
|
+
* ライト/ダーク両方で読みやすいよう背景判定ごとにパレットを替える。
|
|
7
|
+
*/
|
|
8
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
9
|
+
/** ダーク背景は明るい標準色、ライト背景は256色パレットの暗い色。 */
|
|
10
|
+
export function paletteFor(background) {
|
|
11
|
+
return background === 'dark'
|
|
12
|
+
? {
|
|
13
|
+
reasoning: '90', result: '90', system: '90',
|
|
14
|
+
user: '1', heading: '1', accent: '36', tool: '94',
|
|
15
|
+
diffAdd: '92', diffDel: '91', diffMeta: '96',
|
|
16
|
+
codeComment: '90', codeString: '33',
|
|
17
|
+
codeNumber: '95', codeKeyword: '94',
|
|
18
|
+
}
|
|
19
|
+
: {
|
|
20
|
+
reasoning: '90', result: '90', system: '90',
|
|
21
|
+
user: '1', heading: '1', accent: '36', tool: '38;5;26',
|
|
22
|
+
diffAdd: '38;5;28', diffDel: '38;5;160', diffMeta: '38;5;30',
|
|
23
|
+
codeComment: '38;5;242', codeString: '38;5;130',
|
|
24
|
+
codeNumber: '38;5;52', codeKeyword: '38;5;19',
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** `rgb:rrrr/gggg/bbbb` または `color:0/9` 形式の OSC 11 応答を解析する。 */
|
|
28
|
+
export function parseOscBackground(text) {
|
|
29
|
+
const match = /\x1b]11;([^\x07\x1b]*)/.exec(text);
|
|
30
|
+
if (match === null)
|
|
31
|
+
return null;
|
|
32
|
+
return stripVTControlCharacters(match[1]).trim() || null;
|
|
33
|
+
}
|
|
34
|
+
/** 0〜65535 の16進3成分から輝度を0..1で出す。 */
|
|
35
|
+
export function backgroundLuminance(osc) {
|
|
36
|
+
const rgb = /rgb:([0-9a-fA-F]+)\/([0-9a-fA-F]+)\/([0-9a-fA-F]+)/.exec(osc)
|
|
37
|
+
?? /rgba?:([0-9a-fA-F]+)\/([0-9a-fA-F]+)\/([0-9a-fA-F]+)/.exec(osc);
|
|
38
|
+
if (rgb !== null) {
|
|
39
|
+
const components = [rgb[1], rgb[2], rgb[3]].map((c) => parseInt(c, 16) / (16 ** c.length - 1));
|
|
40
|
+
if (components.some((n) => !Number.isFinite(n)))
|
|
41
|
+
return null;
|
|
42
|
+
return 0.2126 * components[0] + 0.7152 * components[1] + 0.0722 * components[2];
|
|
43
|
+
}
|
|
44
|
+
const gray = /^(\d+)$/.exec(osc.trim());
|
|
45
|
+
if (gray !== null) {
|
|
46
|
+
const n = Number(gray[1]);
|
|
47
|
+
return Number.isFinite(n) ? n / 255 : null;
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
/** OSC応答または COLORFGBG 環境変数から背景を分類する。根拠がなければ null。 */
|
|
52
|
+
export function classifyBackground(oscReply, env) {
|
|
53
|
+
if (oscReply !== null) {
|
|
54
|
+
const l = backgroundLuminance(oscReply);
|
|
55
|
+
if (l !== null)
|
|
56
|
+
return l < 0.5 ? 'dark' : 'light';
|
|
57
|
+
}
|
|
58
|
+
const fgbg = env?.['COLORFGBG'];
|
|
59
|
+
if (typeof fgbg === 'string' && /;/.test(fgbg)) {
|
|
60
|
+
const bg = Number(fgbg.split(';').at(-1));
|
|
61
|
+
// 0-6 暗い色、7/8-15 明るい色(xterm系の慣習)。15以上は256色拡張。
|
|
62
|
+
if (Number.isFinite(bg)) {
|
|
63
|
+
if (bg <= 6)
|
|
64
|
+
return 'dark';
|
|
65
|
+
if (bg >= 8 && bg <= 15)
|
|
66
|
+
return 'light';
|
|
67
|
+
if (bg >= 16)
|
|
68
|
+
return bg >= 238 ? 'dark' : 'light';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** 汎用キーワード群(C/JS/TS/Python/Rust/Go/Ruby/Shell など)。誤着色より無着色を避ける単純リスト。 */
|
|
74
|
+
const KEYWORDS = [
|
|
75
|
+
'function', 'const', 'let', 'var', 'return', 'if', 'else', 'elif', 'for', 'while', 'class', 'import', 'export',
|
|
76
|
+
'from', 'def', 'async', 'await', 'type', 'interface', 'extends', 'implements', 'new', 'try', 'catch', 'finally',
|
|
77
|
+
'throw', 'switch', 'case', 'break', 'continue', 'do', 'in', 'of', 'null', 'true', 'false', 'void', 'static',
|
|
78
|
+
'public', 'private', 'protected', 'this', 'super', 'yield', 'match', 'struct', 'impl', 'trait', 'fn', 'pub',
|
|
79
|
+
'use', 'mod', 'self', 'None', 'Some', 'end', 'local', 'require', 'lambda', 'package', 'func', 'range',
|
|
80
|
+
'select', 'defer', 'go', 'with', 'unless', 'until', 'nil', 'then', 'fi', 'esac', 'echo', 'done', 'set',
|
|
81
|
+
];
|
|
82
|
+
const TOKEN_PATTERN = new RegExp([
|
|
83
|
+
'(\\/\\/[^\\n]*|--[^\\n]*)', // 行コメント(/ / --)
|
|
84
|
+
'(#[^\\n]*)', // # コメント(shell/ruby/python風)
|
|
85
|
+
'("(?:[^"\\\\]|\\\\.)*"|\'(?:[^\'\\\\]|\\\\.)*\'|`(?:[^`\\\\]|\\\\.)*`)',
|
|
86
|
+
'\\b(\\d+(?:\\.\\d+)*)\\b',
|
|
87
|
+
`\\b(${KEYWORDS.join('|')})\\b`,
|
|
88
|
+
].join('|'), 'g');
|
|
89
|
+
/** コード fenced block 内の1行を簡易ハイライトする。 */
|
|
90
|
+
export function highlightCodeLine(line, palette) {
|
|
91
|
+
return line.replace(TOKEN_PATTERN, (match, lineComment, hashComment, str, num, keyword) => {
|
|
92
|
+
const code = lineComment !== undefined || hashComment !== undefined ? palette.codeComment
|
|
93
|
+
: str !== undefined ? palette.codeString
|
|
94
|
+
: num !== undefined ? palette.codeNumber
|
|
95
|
+
: palette.codeKeyword;
|
|
96
|
+
return `\x1b[${code}m${match}\x1b[0m`;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/** 連続する行に状態(推論・コードブロック・diff)を引き継いで色を付ける。 */
|
|
100
|
+
export class TranscriptColorizer {
|
|
101
|
+
mode = 'text';
|
|
102
|
+
inDiff = false;
|
|
103
|
+
/** 完成行までの状態を保存しておき、未完了行(ライブ表示)の色分けに複製する。 */
|
|
104
|
+
state() { return { mode: this.mode, inDiff: this.inDiff }; }
|
|
105
|
+
restore(state) { this.mode = state.mode; this.inDiff = state.inDiff; }
|
|
106
|
+
line(raw, palette) {
|
|
107
|
+
const trimmed = raw.trim();
|
|
108
|
+
const blank = trimmed.length === 0;
|
|
109
|
+
if (this.mode === 'reasoning') {
|
|
110
|
+
if (/^(▶ |#{1,3} |--- 承認要求|\(推論\)|```)/.test(raw) || /^ {0,2}(結果|エラー)/.test(trimmed)) {
|
|
111
|
+
this.mode = 'text';
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return blank ? raw : `\x1b[${palette.reasoning}m${raw}\x1b[0m`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else if (this.mode === 'code') {
|
|
118
|
+
if (/^(\s*)(```|~~~)/.test(raw)) {
|
|
119
|
+
this.mode = 'text';
|
|
120
|
+
return `\x1b[${palette.reasoning}m${raw}\x1b[0m`;
|
|
121
|
+
}
|
|
122
|
+
return highlightCodeLine(raw, palette);
|
|
123
|
+
}
|
|
124
|
+
if (blank)
|
|
125
|
+
return raw;
|
|
126
|
+
if (trimmed === '(推論)' || /^(推論)$/.test(trimmed)) {
|
|
127
|
+
this.mode = 'reasoning';
|
|
128
|
+
return `\x1b[${palette.reasoning}m${raw}\x1b[0m`;
|
|
129
|
+
}
|
|
130
|
+
if (/^(\s*)(```|~~~)/.test(raw)) {
|
|
131
|
+
this.mode = 'code';
|
|
132
|
+
return `\x1b[${palette.reasoning}m${raw}\x1b[0m`;
|
|
133
|
+
}
|
|
134
|
+
const diff = this.diffLine(trimmed, raw, palette);
|
|
135
|
+
if (diff !== null)
|
|
136
|
+
return diff;
|
|
137
|
+
if (/^#{1,6} /.test(trimmed))
|
|
138
|
+
return `\x1b[${palette.heading}m${raw}\x1b[0m`;
|
|
139
|
+
if (/^▶ You:/.test(raw))
|
|
140
|
+
return `\x1b[${palette.user}m${raw}\x1b[0m`;
|
|
141
|
+
if (/^▶ /.test(raw))
|
|
142
|
+
return `\x1b[${palette.tool}m${raw}\x1b[0m`;
|
|
143
|
+
if (/^\(自動承認:|^\(承認\)|^\(拒否\)|^\(中断|^\(接続|^\(モデル|^\(推論/.test(trimmed)
|
|
144
|
+
|| /^ {0,2}\(結果\./.test(trimmed) || /^ {0,2}\(結果:/.test(raw) || /^ {0,2}\(エラー/.test(trimmed)) {
|
|
145
|
+
this.inDiff = false;
|
|
146
|
+
return `\x1b[${palette.result}m${raw}\x1b[0m`;
|
|
147
|
+
}
|
|
148
|
+
if (/^\[exit code/.test(trimmed))
|
|
149
|
+
return `\x1b[${palette.result}m${raw}\x1b[0m`;
|
|
150
|
+
return raw;
|
|
151
|
+
}
|
|
152
|
+
diffLine(trimmed, raw, palette) {
|
|
153
|
+
if (/^--- 承認要求/.test(raw)) {
|
|
154
|
+
this.inDiff = false;
|
|
155
|
+
return `\x1b[${palette.system}m${raw}\x1b[0m`;
|
|
156
|
+
}
|
|
157
|
+
if (/^--- /.test(raw) && !this.inDiff) {
|
|
158
|
+
this.inDiff = true;
|
|
159
|
+
return `\x1b[${palette.diffMeta}m${raw}\x1b[0m`;
|
|
160
|
+
}
|
|
161
|
+
if (!this.inDiff)
|
|
162
|
+
return null;
|
|
163
|
+
if (/^\+\+\+ /.test(raw))
|
|
164
|
+
return `\x1b[${palette.diffMeta}m${raw}\x1b[0m`;
|
|
165
|
+
if (/^@@/.test(raw))
|
|
166
|
+
return `\x1b[${palette.diffMeta}m${raw}\x1b[0m`;
|
|
167
|
+
if (/^\+ /.test(raw))
|
|
168
|
+
return `\x1b[${palette.diffAdd}m${raw}\x1b[0m`;
|
|
169
|
+
if (/^- /.test(raw))
|
|
170
|
+
return `\x1b[${palette.diffDel}m${raw}\x1b[0m`;
|
|
171
|
+
if (/^ {2}/.test(raw))
|
|
172
|
+
return raw; // 文脈行
|
|
173
|
+
this.inDiff = false;
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export interface MemoryConfig {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
command: string;
|
|
4
|
+
args: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface LiteConfig {
|
|
7
|
+
memory: MemoryConfig;
|
|
8
|
+
/** Validated per plugin by the subagent boundary; malformed entries fail closed. */
|
|
9
|
+
subagents?: unknown;
|
|
10
|
+
/** Validated by parseLanguageSettings; malformed values fall back to defaults. */
|
|
11
|
+
language?: unknown;
|
|
12
|
+
/** all | workspace | none. Validated by parsePermissionPolicy; malformed values fall back. */
|
|
13
|
+
permissions?: unknown;
|
|
14
|
+
/** Tool round-trip limit for one turn. Validated by parseMaxTurns; malformed values fall back. */
|
|
15
|
+
maxTurns?: unknown;
|
|
16
|
+
/** 前回接続したモデル(--base-url の情報は modelBaseUrl に残す)。起動時の復元に使う。 */
|
|
17
|
+
model?: unknown;
|
|
18
|
+
modelBaseUrl?: unknown;
|
|
19
|
+
/** 前回の /reasoning レベル。起動時に復元する。 */
|
|
20
|
+
reasoning?: unknown;
|
|
21
|
+
/** モデル応答の無通信タイムアウト(ms)。1〜上限で検証し、不正値は使わない。 */
|
|
22
|
+
timeoutMs?: unknown;
|
|
23
|
+
diagnostics?: string[];
|
|
24
|
+
}
|
|
25
|
+
export declare const DEFAULT_CONFIG: LiteConfig;
|
|
26
|
+
/** タイムアウトは 5秒〜30分の整数で受ける。それ以外は無効値として扱う。 */
|
|
27
|
+
export declare const TIMEOUT_MS_MIN = 5000;
|
|
28
|
+
export declare const TIMEOUT_MS_MAX = 1800000;
|
|
29
|
+
export declare function parseTimeoutMs(value: unknown): number | null;
|
|
30
|
+
export declare function configDir(): string;
|
|
31
|
+
export declare function configPath(): string;
|
|
32
|
+
/** 既定値は毎回コピーして返す(呼び出し元が書き換えても共有しない)。 */
|
|
33
|
+
export declare function defaultConfig(): LiteConfig;
|
|
34
|
+
/** 一時ファイルに書いてrenameする。中断されても config.json が途中まで書かれた状態を残さない。 */
|
|
35
|
+
export declare function saveConfig(config: LiteConfig): void;
|
|
36
|
+
export declare function loadConfig(): LiteConfig;
|
|
37
|
+
/**
|
|
38
|
+
* 起動時に読んだ設定を壊さず1フィールドだけ更新する。書き込み前に実ファイルから
|
|
39
|
+
* 読み直すので、並行する別セッションの保存を失わない。
|
|
40
|
+
*/
|
|
41
|
+
export declare function updateConfig(apply: (config: LiteConfig) => void): void;
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
export const DEFAULT_CONFIG = {
|
|
5
|
+
memory: {
|
|
6
|
+
enabled: false,
|
|
7
|
+
command: 'kiokuko',
|
|
8
|
+
args: ['mcp', '--profile', 'chatgpt-memory'],
|
|
9
|
+
},
|
|
10
|
+
};
|
|
11
|
+
/** タイムアウトは 5秒〜30分の整数で受ける。それ以外は無効値として扱う。 */
|
|
12
|
+
export const TIMEOUT_MS_MIN = 5000;
|
|
13
|
+
export const TIMEOUT_MS_MAX = 1_800_000;
|
|
14
|
+
export function parseTimeoutMs(value) {
|
|
15
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value))
|
|
16
|
+
return null;
|
|
17
|
+
if (value < TIMEOUT_MS_MIN || value > TIMEOUT_MS_MAX)
|
|
18
|
+
return null;
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
export function configDir() {
|
|
22
|
+
if (process.env['MODKH_HOME'])
|
|
23
|
+
return process.env['MODKH_HOME'];
|
|
24
|
+
const configBase = process.env['XDG_CONFIG_HOME'] ?? path.join(os.homedir(), '.config');
|
|
25
|
+
return path.join(configBase, 'modkh');
|
|
26
|
+
}
|
|
27
|
+
export function configPath() {
|
|
28
|
+
return path.join(configDir(), 'config.json');
|
|
29
|
+
}
|
|
30
|
+
/** 既定値は毎回コピーして返す(呼び出し元が書き換えても共有しない)。 */
|
|
31
|
+
export function defaultConfig() {
|
|
32
|
+
return { ...DEFAULT_CONFIG, memory: { ...DEFAULT_CONFIG.memory } };
|
|
33
|
+
}
|
|
34
|
+
/** 書き込み時に意味を持つ既知のキー。これ以外は利用者の手書き設定として保持する。 */
|
|
35
|
+
const KNOWN_KEYS = new Set(['memory', 'subagents', 'language', 'permissions', 'maxTurns', 'model', 'modelBaseUrl', 'reasoning', 'timeoutMs']);
|
|
36
|
+
function readRawConfig() {
|
|
37
|
+
try {
|
|
38
|
+
return fs.readFileSync(configPath(), 'utf8');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function parseRawObject(text) {
|
|
45
|
+
try {
|
|
46
|
+
const parsed = JSON.parse(text);
|
|
47
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed))
|
|
48
|
+
return undefined;
|
|
49
|
+
return parsed;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function toLiteConfig(parsed) {
|
|
56
|
+
const memory = parsed.memory;
|
|
57
|
+
const diagnostics = Array.isArray(parsed.diagnostics)
|
|
58
|
+
? parsed.diagnostics.filter((entry) => typeof entry === 'string')
|
|
59
|
+
: undefined;
|
|
60
|
+
return {
|
|
61
|
+
subagents: parsed.subagents,
|
|
62
|
+
language: parsed.language,
|
|
63
|
+
permissions: parsed.permissions,
|
|
64
|
+
maxTurns: parsed.maxTurns,
|
|
65
|
+
model: parsed.model,
|
|
66
|
+
modelBaseUrl: parsed.modelBaseUrl,
|
|
67
|
+
reasoning: parsed.reasoning,
|
|
68
|
+
timeoutMs: parsed.timeoutMs,
|
|
69
|
+
memory: {
|
|
70
|
+
enabled: memory?.enabled === true,
|
|
71
|
+
command: memory?.command ?? DEFAULT_CONFIG.memory.command,
|
|
72
|
+
args: memory?.args ?? DEFAULT_CONFIG.memory.args,
|
|
73
|
+
},
|
|
74
|
+
...(diagnostics ? { diagnostics } : {}),
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 壊れたファイルからでも、次回起動の復元に必要な値だけは拾う。
|
|
79
|
+
* ここで model を諦めると「一度決めたモデルが次回は未接続」になるため、文字列だけは救出する。
|
|
80
|
+
*/
|
|
81
|
+
function salvage(parsed, text) {
|
|
82
|
+
const stringField = (name) => {
|
|
83
|
+
try {
|
|
84
|
+
const match = new RegExp(`"${name}"\\s*:\\s*"((?:[^"\\\\]|\\\\.)*)"`).exec(text);
|
|
85
|
+
return match?.[1];
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
if (parsed.model === undefined)
|
|
92
|
+
parsed.model = stringField('model');
|
|
93
|
+
if (parsed.modelBaseUrl === undefined)
|
|
94
|
+
parsed.modelBaseUrl = stringField('modelBaseUrl');
|
|
95
|
+
if (parsed.reasoning === undefined)
|
|
96
|
+
parsed.reasoning = stringField('reasoning');
|
|
97
|
+
if (parsed.timeoutMs === undefined) {
|
|
98
|
+
const number = /"timeoutMs"\s*:\s*(\d+)/.exec(text)?.[1];
|
|
99
|
+
if (number !== undefined)
|
|
100
|
+
parsed.timeoutMs = Number(number);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** 壊れた内容を上書きで失わないよう、直近の1件だけ退避する。 */
|
|
104
|
+
function quarantineBrokenConfig(text) {
|
|
105
|
+
try {
|
|
106
|
+
const backup = `${configPath()}.broken`;
|
|
107
|
+
let previous;
|
|
108
|
+
try {
|
|
109
|
+
previous = fs.readFileSync(backup, 'utf8');
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
previous = undefined;
|
|
113
|
+
}
|
|
114
|
+
if (previous === text)
|
|
115
|
+
return;
|
|
116
|
+
fs.writeFileSync(backup, text, { encoding: 'utf8', mode: 0o600 });
|
|
117
|
+
}
|
|
118
|
+
catch { /* 退避できなくても読み込みは続ける */ }
|
|
119
|
+
}
|
|
120
|
+
/** 一時ファイルに書いてrenameする。中断されても config.json が途中まで書かれた状態を残さない。 */
|
|
121
|
+
export function saveConfig(config) {
|
|
122
|
+
fs.mkdirSync(configDir(), { recursive: true });
|
|
123
|
+
const target = configPath();
|
|
124
|
+
// 手書きの未知キーを落とさない(設定ファイル全体を書き直すため)。
|
|
125
|
+
const unknown = {};
|
|
126
|
+
const raw = readRawConfig();
|
|
127
|
+
const existing = raw === undefined ? undefined : parseRawObject(raw);
|
|
128
|
+
if (existing) {
|
|
129
|
+
for (const [key, value] of Object.entries(existing))
|
|
130
|
+
if (!KNOWN_KEYS.has(key))
|
|
131
|
+
unknown[key] = value;
|
|
132
|
+
}
|
|
133
|
+
const { diagnostics: _derived, ...known } = config;
|
|
134
|
+
const body = { ...unknown, ...known };
|
|
135
|
+
const temporary = `${target}.${process.pid}.${Date.now()}.tmp`;
|
|
136
|
+
// 既存ファイルの権限を引き継ぐ(利用者が絞っていれば広げない)。
|
|
137
|
+
let mode = 0o600;
|
|
138
|
+
try {
|
|
139
|
+
mode = fs.statSync(target).mode & 0o777;
|
|
140
|
+
}
|
|
141
|
+
catch { /* 新規作成時は 0600 */ }
|
|
142
|
+
const fd = fs.openSync(temporary, 'w', mode);
|
|
143
|
+
try {
|
|
144
|
+
fs.writeFileSync(fd, `${JSON.stringify(body, null, 2)}\n`, { encoding: 'utf8' });
|
|
145
|
+
fs.fsyncSync(fd); // 電源断・強制終了でもrename前の内容が欠けない
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
fs.closeSync(fd);
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
fs.renameSync(temporary, target);
|
|
152
|
+
}
|
|
153
|
+
catch (e) {
|
|
154
|
+
try {
|
|
155
|
+
fs.rmSync(temporary, { force: true });
|
|
156
|
+
}
|
|
157
|
+
catch { /* 後始末の失敗は元のエラーを隠さない */ }
|
|
158
|
+
throw e;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
fs.chmodSync(target, mode);
|
|
162
|
+
}
|
|
163
|
+
catch { /* 権限変更に失敗しても保存自体は成立させる */ }
|
|
164
|
+
}
|
|
165
|
+
export function loadConfig() {
|
|
166
|
+
const raw = readRawConfig();
|
|
167
|
+
if (raw === undefined)
|
|
168
|
+
return defaultConfig();
|
|
169
|
+
const parsed = parseRawObject(raw);
|
|
170
|
+
if (parsed)
|
|
171
|
+
return toLiteConfig(parsed);
|
|
172
|
+
// 壊れていても、復元に必要な model 等は救出して「次回も同じモデル」を保つ。
|
|
173
|
+
const recovered = defaultConfig();
|
|
174
|
+
salvage(recovered, raw);
|
|
175
|
+
quarantineBrokenConfig(raw);
|
|
176
|
+
return { ...recovered, subagents: null, diagnostics: ['設定を読めません。子の起動は無効: 設定のJSONが不正。壊れた内容は config.json.broken に退避した'] };
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 起動時に読んだ設定を壊さず1フィールドだけ更新する。書き込み前に実ファイルから
|
|
180
|
+
* 読み直すので、並行する別セッションの保存を失わない。
|
|
181
|
+
*/
|
|
182
|
+
export function updateConfig(apply) {
|
|
183
|
+
const current = loadConfig();
|
|
184
|
+
apply(current);
|
|
185
|
+
// 壊れたファイルを読んだ直後でも、救出した model を含めて書き戻す(値を失わない)。
|
|
186
|
+
delete current.diagnostics;
|
|
187
|
+
saveConfig(current);
|
|
188
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare function chars(text: string): string[];
|
|
2
|
+
export declare function clean(text: string): string;
|
|
3
|
+
export declare function textWidth(text: string): number;
|
|
4
|
+
export declare function fitText(text: string, width: number): string;
|
|
5
|
+
export interface CursorPosition {
|
|
6
|
+
row: number;
|
|
7
|
+
column: number;
|
|
8
|
+
}
|
|
9
|
+
/** Each grapheme boundary has a visual position; explicit newlines and soft wraps share one layout. */
|
|
10
|
+
export declare function editorLayout(text: string, width: number): {
|
|
11
|
+
lines: string[];
|
|
12
|
+
positions: CursorPosition[];
|
|
13
|
+
};
|
|
14
|
+
export declare function wrap(text: string, width: number): string[];
|
|
15
|
+
export declare function verticalCursor(text: string, cursor: number, width: number, delta: number): number;
|
|
16
|
+
export declare function lineEdge(text: string, cursor: number, edge: 'start' | 'end'): number;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
2
|
+
const segments = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
|
|
3
|
+
export function chars(text) { return [...segments.segment(text)].map(s => s.segment); }
|
|
4
|
+
export function clean(text) { return stripVTControlCharacters(text).replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g, ''); }
|
|
5
|
+
function cellWidth(char) {
|
|
6
|
+
if (/\p{Extended_Pictographic}/u.test(char))
|
|
7
|
+
return 2;
|
|
8
|
+
const cp = char.codePointAt(0);
|
|
9
|
+
return cp >= 0x1100 && (cp <= 0x115f || cp >= 0x2e80 && cp <= 0xa4cf || cp >= 0xac00 && cp <= 0xd7a3
|
|
10
|
+
|| cp >= 0xf900 && cp <= 0xfaff || cp >= 0xfe10 && cp <= 0xfe6f || cp >= 0xff00 && cp <= 0xff60 || cp >= 0x20000) ? 2 : 1;
|
|
11
|
+
}
|
|
12
|
+
export function textWidth(text) { return chars(clean(text)).reduce((sum, c) => sum + cellWidth(c), 0); }
|
|
13
|
+
export function fitText(text, width) {
|
|
14
|
+
let result = '', used = 0;
|
|
15
|
+
for (const c of chars(clean(text))) {
|
|
16
|
+
const n = cellWidth(c);
|
|
17
|
+
if (used + n > width)
|
|
18
|
+
break;
|
|
19
|
+
result += c;
|
|
20
|
+
used += n;
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
/** Each grapheme boundary has a visual position; explicit newlines and soft wraps share one layout. */
|
|
25
|
+
export function editorLayout(text, width) {
|
|
26
|
+
const parts = chars(text), lines = [], positions = [];
|
|
27
|
+
let line = '', column = 0;
|
|
28
|
+
for (let i = 0; i < parts.length; i++) {
|
|
29
|
+
const c = parts[i], n = c === '\n' ? 0 : cellWidth(c);
|
|
30
|
+
if (c !== '\n' && column + n > width) {
|
|
31
|
+
lines.push(line);
|
|
32
|
+
line = '';
|
|
33
|
+
column = 0;
|
|
34
|
+
}
|
|
35
|
+
positions[i] = { row: lines.length, column };
|
|
36
|
+
if (c === '\n') {
|
|
37
|
+
lines.push(line);
|
|
38
|
+
line = '';
|
|
39
|
+
column = 0;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
line += c;
|
|
43
|
+
column += n;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
positions[parts.length] = { row: lines.length, column };
|
|
47
|
+
lines.push(line);
|
|
48
|
+
return { lines, positions };
|
|
49
|
+
}
|
|
50
|
+
export function wrap(text, width) { return editorLayout(clean(text), width).lines; }
|
|
51
|
+
export function verticalCursor(text, cursor, width, delta) {
|
|
52
|
+
const layout = editorLayout(text, width), current = layout.positions[cursor];
|
|
53
|
+
const row = Math.max(0, Math.min(layout.lines.length - 1, current.row + delta));
|
|
54
|
+
if (row === current.row)
|
|
55
|
+
return cursor;
|
|
56
|
+
let best = cursor, distance = Infinity;
|
|
57
|
+
for (let i = 0; i < layout.positions.length; i++) {
|
|
58
|
+
const position = layout.positions[i];
|
|
59
|
+
if (position.row === row && Math.abs(position.column - current.column) < distance) {
|
|
60
|
+
best = i;
|
|
61
|
+
distance = Math.abs(position.column - current.column);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return best;
|
|
65
|
+
}
|
|
66
|
+
export function lineEdge(text, cursor, edge) {
|
|
67
|
+
const parts = chars(text);
|
|
68
|
+
if (edge === 'start') {
|
|
69
|
+
while (cursor > 0 && parts[cursor - 1] !== '\n')
|
|
70
|
+
cursor--;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
while (cursor < parts.length && parts[cursor] !== '\n')
|
|
74
|
+
cursor++;
|
|
75
|
+
}
|
|
76
|
+
return cursor;
|
|
77
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider API keys entered at /connect. Kept in the MODKH config directory with mode 0600 and
|
|
3
|
+
* never written to a session log or echoed back by a command. Environment variables still win,
|
|
4
|
+
* so an exported key is not silently overridden by a stale saved one.
|
|
5
|
+
*/
|
|
6
|
+
export type SavedKeys = Record<string, string>;
|
|
7
|
+
export declare function keysPath(): string;
|
|
8
|
+
/** A malformed or unreadable file is treated as empty rather than granting a partial identity. */
|
|
9
|
+
export declare function loadSavedKeys(): SavedKeys;
|
|
10
|
+
export declare function saveKey(provider: string, key: string): {
|
|
11
|
+
provider: string;
|
|
12
|
+
path: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function removeKey(provider: string): boolean;
|
|
15
|
+
/** Names only; callers must never print the secret itself. */
|
|
16
|
+
export declare function savedKeyProviders(): string[];
|
|
17
|
+
/** Environment first, then the saved key. An empty environment value is ignored. */
|
|
18
|
+
export declare function resolveApiKey(provider: string, env?: NodeJS.ProcessEnv, envName?: string | null): string | undefined;
|