keysmash-ipsum 0.0.0-stage → 0.1.1
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/lib/cjs/adapters/claude-code.js +73 -0
- package/lib/cjs/adapters/codex.js +51 -0
- package/lib/cjs/adapters/index.js +16 -0
- package/lib/cjs/adapters/run.js +41 -0
- package/lib/cjs/bin.js +24 -0
- package/lib/cjs/cli-args.js +33 -0
- package/lib/cjs/cli.js +134 -0
- package/lib/cjs/en.js +45 -0
- package/lib/cjs/gen-cli.js +49 -0
- package/lib/cjs/generate.js +73 -0
- package/lib/cjs/index.js +21 -0
- package/lib/cjs/kana.js +193 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/prompt.js +79 -0
- package/lib/cjs/request.js +47 -0
- package/lib/cjs/restore.js +48 -0
- package/lib/cjs/rng.js +28 -0
- package/lib/cjs/words.js +42 -0
- package/lib/esm/adapters/claude-code.js +69 -0
- package/lib/esm/adapters/codex.js +47 -0
- package/lib/esm/adapters/index.js +12 -0
- package/lib/esm/adapters/run.js +37 -0
- package/lib/esm/bin.js +22 -0
- package/lib/esm/cli-args.js +27 -0
- package/lib/esm/cli.js +129 -0
- package/lib/esm/en.js +41 -0
- package/lib/esm/gen-cli.js +45 -0
- package/lib/esm/generate.js +68 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/kana.js +189 -0
- package/lib/esm/prompt.js +73 -0
- package/lib/esm/request.js +38 -0
- package/lib/esm/restore.js +42 -0
- package/lib/esm/rng.js +21 -0
- package/lib/esm/words.js +35 -0
- package/lib/types/adapters/claude-code.d.ts +7 -0
- package/lib/types/adapters/codex.d.ts +7 -0
- package/lib/types/adapters/index.d.ts +8 -0
- package/lib/types/adapters/run.d.ts +12 -0
- package/lib/types/bin.d.ts +2 -0
- package/lib/types/cli-args.d.ts +5 -0
- package/lib/types/cli.d.ts +15 -0
- package/lib/types/en.d.ts +11 -0
- package/lib/types/gen-cli.d.ts +1 -0
- package/lib/types/generate.d.ts +26 -0
- package/lib/types/index.d.ts +7 -0
- package/lib/types/kana.d.ts +10 -0
- package/lib/types/prompt.d.ts +12 -0
- package/lib/types/request.d.ts +31 -0
- package/lib/types/restore.d.ts +21 -0
- package/lib/types/rng.d.ts +10 -0
- package/lib/types/words.d.ts +16 -0
- package/license +9 -0
- package/package.json +61 -4
- package/readme.ja.md +149 -0
- package/readme.md +149 -0
- package/README.md +0 -3
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.claudeCodeComplete = exports.CLAUDE_DEFAULT_MODEL = void 0;
|
|
4
|
+
const node_os_1 = require("node:os");
|
|
5
|
+
const run_ts_1 = require("./run.js");
|
|
6
|
+
// haiku はランダムな英字の並びを安全分類器に誤判定されて断られることがある ([bio] を実測)。
|
|
7
|
+
// sonnet + effort low なら 100 語で 5〜10 秒・2.5k token 前後で、断られなかった (D-0012)
|
|
8
|
+
exports.CLAUDE_DEFAULT_MODEL = 'sonnet';
|
|
9
|
+
// 綴りの近い語を当てるだけなので深く考えさせる必要がない。思考の token と待ち時間を削る
|
|
10
|
+
const EFFORT = 'low';
|
|
11
|
+
/**
|
|
12
|
+
* 読み解き専用に隔離した `claude -p` の引数。プロンプトは stdin で渡す。
|
|
13
|
+
* --bare は OAuth (subscription) を読まなくなるので使えない。代わりに system prompt を差し替え、
|
|
14
|
+
* ツール・MCP・設定ファイル (CLAUDE.md や hooks) を切り、セッションも保存しない。
|
|
15
|
+
* --json-schema は引数でしか受け取れないが、中身はライブラリが作る固定の形 (キーと型だけ)
|
|
16
|
+
*/
|
|
17
|
+
const buildArgs = (model, system, schema) => [
|
|
18
|
+
'-p',
|
|
19
|
+
'--model',
|
|
20
|
+
model,
|
|
21
|
+
'--effort',
|
|
22
|
+
EFFORT,
|
|
23
|
+
'--output-format',
|
|
24
|
+
'json',
|
|
25
|
+
'--json-schema',
|
|
26
|
+
JSON.stringify(schema),
|
|
27
|
+
'--system-prompt',
|
|
28
|
+
system,
|
|
29
|
+
'--tools',
|
|
30
|
+
'',
|
|
31
|
+
'--strict-mcp-config',
|
|
32
|
+
'--setting-sources',
|
|
33
|
+
'',
|
|
34
|
+
'--no-session-persistence',
|
|
35
|
+
];
|
|
36
|
+
const REFUSAL = /can't help with this|usage policy|aup/i;
|
|
37
|
+
const parseJson = (stdout) => {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(stdout);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new Error(`claude -p の出力が JSON ではありません: ${stdout.trim().slice(0, 500)}`);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
const readStructured = (stdout) => {
|
|
46
|
+
const result = parseJson(stdout);
|
|
47
|
+
if (result.is_error === true) {
|
|
48
|
+
const message = result.result ?? '';
|
|
49
|
+
// ランダムな英字の長い並びは安全分類器に誤判定されることがある (haiku 5.5 で [bio] を実測)
|
|
50
|
+
const hint = REFUSAL.test(message)
|
|
51
|
+
? '\n安全分類器に断られました。語数を減らすか、--model sonnet などで試してください'
|
|
52
|
+
: '';
|
|
53
|
+
throw new Error(`claude -p がエラーを返しました: ${message}${hint}`);
|
|
54
|
+
}
|
|
55
|
+
if (result.structured_output === undefined) {
|
|
56
|
+
throw new Error('claude -p の出力に structured_output がありません');
|
|
57
|
+
}
|
|
58
|
+
return result.structured_output;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* 手元の Claude Code (`claude -p --json-schema`) を LLM として使う Complete。
|
|
62
|
+
* 認証は Claude Code のログイン (subscription) をそのまま使い、トークンには触れない
|
|
63
|
+
*/
|
|
64
|
+
const claudeCodeComplete = (model = exports.CLAUDE_DEFAULT_MODEL) => async ({ system, prompt, schema }) => {
|
|
65
|
+
// プロジェクトの CLAUDE.md を拾わないよう、cwd を一時ディレクトリにする
|
|
66
|
+
const stdout = await (0, run_ts_1.runCommand)('claude', buildArgs(model, system, schema), {
|
|
67
|
+
input: prompt,
|
|
68
|
+
cwd: (0, node_os_1.tmpdir)(),
|
|
69
|
+
shouldReadStdoutOnFailure: true,
|
|
70
|
+
});
|
|
71
|
+
return readStructured(stdout);
|
|
72
|
+
};
|
|
73
|
+
exports.claudeCodeComplete = claudeCodeComplete;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codexComplete = void 0;
|
|
4
|
+
const promises_1 = require("node:fs/promises");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const run_ts_1 = require("./run.js");
|
|
8
|
+
/**
|
|
9
|
+
* 読み解き専用に絞った `codex exec` の引数。プロンプトは stdin (`-`)、schema と出力はファイルで渡すので、
|
|
10
|
+
* 引数に入るのはフラグと一時ファイルのパスだけ。
|
|
11
|
+
* ~/.codex/AGENTS.md だけは止める手段が無い (CODEX_HOME の差し替えは認証の更新を壊しうるので使わない)。
|
|
12
|
+
* 出力は schema と parseRestored の検証で縛る
|
|
13
|
+
*/
|
|
14
|
+
const buildArgs = (dir, model) => [
|
|
15
|
+
'exec',
|
|
16
|
+
'--ignore-user-config',
|
|
17
|
+
'--ignore-rules',
|
|
18
|
+
'--ephemeral',
|
|
19
|
+
'--skip-git-repo-check',
|
|
20
|
+
'--sandbox',
|
|
21
|
+
'read-only',
|
|
22
|
+
'--cd',
|
|
23
|
+
dir,
|
|
24
|
+
'--output-schema',
|
|
25
|
+
(0, node_path_1.join)(dir, 'schema.json'),
|
|
26
|
+
'--output-last-message',
|
|
27
|
+
(0, node_path_1.join)(dir, 'out.json'),
|
|
28
|
+
...(model === undefined ? [] : ['--model', model]),
|
|
29
|
+
'-',
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* 手元の Codex CLI (`codex exec --output-schema`) を LLM として使う Complete。
|
|
33
|
+
* 認証は Codex のログイン (ChatGPT subscription) をそのまま使い、トークンには触れない。
|
|
34
|
+
* model 省略時は Codex の既定モデル
|
|
35
|
+
*/
|
|
36
|
+
const codexComplete = (model) => async ({ system, prompt, schema }) => {
|
|
37
|
+
const dir = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), 'keysmash-ipsum-'));
|
|
38
|
+
try {
|
|
39
|
+
await (0, promises_1.writeFile)((0, node_path_1.join)(dir, 'schema.json'), JSON.stringify(schema));
|
|
40
|
+
// codex exec には system prompt を差し替える引数が無いので、指示の先頭に置く
|
|
41
|
+
await (0, run_ts_1.runCommand)('codex', buildArgs(dir, model), {
|
|
42
|
+
input: `${system}\n\n${prompt}`,
|
|
43
|
+
cwd: dir,
|
|
44
|
+
});
|
|
45
|
+
return JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(dir, 'out.json'), 'utf8'));
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
await (0, promises_1.rm)(dir, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.codexComplete = codexComplete;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createComplete = exports.LLM_NAMES = void 0;
|
|
4
|
+
const claude_code_ts_1 = require("./claude-code.js");
|
|
5
|
+
const codex_ts_1 = require("./codex.js");
|
|
6
|
+
/**
|
|
7
|
+
* subscription でログイン済みの CLI を LLM として使う adapter。
|
|
8
|
+
* どれも structured output (JSON Schema) を CLI 側で受け付けるものだけを置く
|
|
9
|
+
*/
|
|
10
|
+
exports.LLM_NAMES = ['claude', 'codex'];
|
|
11
|
+
const ADAPTERS = {
|
|
12
|
+
claude: claude_code_ts_1.claudeCodeComplete,
|
|
13
|
+
codex: codex_ts_1.codexComplete,
|
|
14
|
+
};
|
|
15
|
+
const createComplete = (name, model) => ADAPTERS[name](model);
|
|
16
|
+
exports.createComplete = createComplete;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runCommand = void 0;
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
// claude / codex は 1 回 15〜60 秒かかる。固まった CLI を待ち続けないための上限
|
|
6
|
+
const TIMEOUT_MS = 5 * 60 * 1000;
|
|
7
|
+
/**
|
|
8
|
+
* コマンドを shell を通さずに実行し stdout を返す。
|
|
9
|
+
* 引数は配列のまま渡るので、`;` や `$()` は解釈されない。Windows の .cmd は shell が要るので対応しない
|
|
10
|
+
*/
|
|
11
|
+
const runCommand = (command, args, opts) => new Promise((resolve, reject) => {
|
|
12
|
+
const child = (0, node_child_process_1.spawn)(command, args, {
|
|
13
|
+
cwd: opts.cwd,
|
|
14
|
+
shell: false,
|
|
15
|
+
timeout: TIMEOUT_MS,
|
|
16
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
17
|
+
});
|
|
18
|
+
const stdout = [];
|
|
19
|
+
const stderr = [];
|
|
20
|
+
child.stdout.on('data', (chunk) => stdout.push(chunk));
|
|
21
|
+
child.stderr.on('data', (chunk) => stderr.push(chunk));
|
|
22
|
+
child.on('error', (error) => {
|
|
23
|
+
const reason = error.code === 'ENOENT'
|
|
24
|
+
? `${command} コマンドが見つかりません (インストールしてログインしてください)`
|
|
25
|
+
: `${command} を起動できませんでした: ${error.message}`;
|
|
26
|
+
reject(new Error(reason));
|
|
27
|
+
});
|
|
28
|
+
child.on('close', (code, signal) => {
|
|
29
|
+
if (code === 0 || opts.shouldReadStdoutOnFailure === true) {
|
|
30
|
+
resolve(Buffer.concat(stdout).toString('utf8'));
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
// claude --output-format json はエラーも stdout の JSON に出すので、stderr が空なら stdout を見せる
|
|
34
|
+
const detail = Buffer.concat(stderr).toString('utf8').trim() ||
|
|
35
|
+
Buffer.concat(stdout).toString('utf8').trim();
|
|
36
|
+
const status = signal === null ? `exit ${code}` : `signal ${signal}`;
|
|
37
|
+
reject(new Error(`${command} が失敗しました (${status}): ${detail}`));
|
|
38
|
+
});
|
|
39
|
+
child.stdin.end(opts.input);
|
|
40
|
+
});
|
|
41
|
+
exports.runCommand = runCommand;
|
package/lib/cjs/bin.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const cli_ts_1 = require("./cli.js");
|
|
5
|
+
// 端末のときだけ待ち中の 1 行を出し、終わったら消す (パイプ先の出力は汚さない)
|
|
6
|
+
const isTty = process.stderr.isTTY === true;
|
|
7
|
+
const CLEAR_LINE = '\r\x1b[K';
|
|
8
|
+
const notify = (message) => {
|
|
9
|
+
if (isTty)
|
|
10
|
+
process.stderr.write(message);
|
|
11
|
+
};
|
|
12
|
+
const clear = () => {
|
|
13
|
+
if (isTty)
|
|
14
|
+
process.stderr.write(CLEAR_LINE);
|
|
15
|
+
};
|
|
16
|
+
// top-level await は CJS ビルドで使えないので then でつなぐ
|
|
17
|
+
(0, cli_ts_1.runCli)(process.argv.slice(2), { notify }).then((out) => {
|
|
18
|
+
clear();
|
|
19
|
+
process.stdout.write(out);
|
|
20
|
+
}, (error) => {
|
|
21
|
+
clear();
|
|
22
|
+
console.error(error instanceof Error ? error.message : error);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.oneOf = exports.parseCount = exports.parseRange = void 0;
|
|
4
|
+
const parseRange = (raw, name) => {
|
|
5
|
+
if (raw === undefined)
|
|
6
|
+
return undefined;
|
|
7
|
+
const match = /^(\d+)(?:-(\d+))?$/.exec(raw);
|
|
8
|
+
if (match === null) {
|
|
9
|
+
throw new Error(`--${name} は "4" か "4-9" の形式で指定してください: ${raw}`);
|
|
10
|
+
}
|
|
11
|
+
const min = Number(match[1]);
|
|
12
|
+
return { min, max: match[2] === undefined ? min : Number(match[2]) };
|
|
13
|
+
};
|
|
14
|
+
exports.parseRange = parseRange;
|
|
15
|
+
const parseCount = (raw, name) => {
|
|
16
|
+
if (raw === undefined)
|
|
17
|
+
return undefined;
|
|
18
|
+
const count = Number(raw);
|
|
19
|
+
if (!Number.isInteger(count) || count < 1) {
|
|
20
|
+
throw new Error(`${name} は 1 以上の整数: ${raw}`);
|
|
21
|
+
}
|
|
22
|
+
return count;
|
|
23
|
+
};
|
|
24
|
+
exports.parseCount = parseCount;
|
|
25
|
+
const oneOf = (raw, allowed, name) => {
|
|
26
|
+
if (raw === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (!allowed.includes(raw)) {
|
|
29
|
+
throw new Error(`--${name} は ${allowed.join('|')}: ${raw}`);
|
|
30
|
+
}
|
|
31
|
+
return raw;
|
|
32
|
+
};
|
|
33
|
+
exports.oneOf = oneOf;
|
package/lib/cjs/cli.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runCli = exports.runGen = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* npm の `keysmash-ipsum` コマンド本体。引数の解釈と表示だけを持ち、生成・読み解きは words / restore に任せる。
|
|
6
|
+
* keysmash-ipsum [words|sentences] [<count>] [--lang en|ja] [--seed <s>] [--layout qwerty|uniform]
|
|
7
|
+
* [--llm claude|codex] [--model <name>] [--raw] [--json-detail] [-h]
|
|
8
|
+
* words (既定): count 個のバラバラな単語を空白区切りで出す
|
|
9
|
+
* sentences: count 個の文を 1 段落で出す
|
|
10
|
+
*/
|
|
11
|
+
const node_util_1 = require("node:util");
|
|
12
|
+
const claude_code_ts_1 = require("./adapters/claude-code.js");
|
|
13
|
+
const index_ts_1 = require("./adapters/index.js");
|
|
14
|
+
const cli_args_ts_1 = require("./cli-args.js");
|
|
15
|
+
const generate_ts_1 = require("./generate.js");
|
|
16
|
+
const restore_ts_1 = require("./restore.js");
|
|
17
|
+
const words_ts_1 = require("./words.js");
|
|
18
|
+
var gen_cli_ts_1 = require("./gen-cli.js");
|
|
19
|
+
Object.defineProperty(exports, "runGen", { enumerable: true, get: function () { return gen_cli_ts_1.runGen; } });
|
|
20
|
+
const MODES = ['words', 'sentences'];
|
|
21
|
+
/**
|
|
22
|
+
* 1 回の LLM 呼び出しに載せる上限。schema は語ごとに必須キーが増え、出力も 1 語あたり数 token 増えるので、
|
|
23
|
+
* 段落 2〜3 個ぶんで止める (多すぎると出力上限・時間切れ・安全分類器の誤判定に当たりやすい)。
|
|
24
|
+
* sentences は 1 文最大 9 語なので 20 文で 180 語
|
|
25
|
+
*/
|
|
26
|
+
const LIMITS = {
|
|
27
|
+
words: { default: 8, max: 200, unit: 'words' },
|
|
28
|
+
sentences: { default: 3, max: 20, unit: 'sentences' },
|
|
29
|
+
};
|
|
30
|
+
const HELP = `Usage: keysmash-ipsum [words|sentences] [count] [options]
|
|
31
|
+
|
|
32
|
+
Dummy text from keysmash: generates seeded random typos and restores them
|
|
33
|
+
with a CLI you are logged in to (structured output).
|
|
34
|
+
|
|
35
|
+
Commands:
|
|
36
|
+
words [n] n separate words (default; n default ${LIMITS.words.default}, max ${LIMITS.words.max})
|
|
37
|
+
sentences [n] n sentences as one paragraph (n default ${LIMITS.sentences.default}, max ${LIMITS.sentences.max})
|
|
38
|
+
|
|
39
|
+
Options:
|
|
40
|
+
--count <n> same as [n]
|
|
41
|
+
--lang en|ja language (default en)
|
|
42
|
+
--seed <s> same seed -> same keysmash (default: current time)
|
|
43
|
+
--layout qwerty|uniform keysmash style (default qwerty)
|
|
44
|
+
--llm claude|codex CLI used to restore (default claude)
|
|
45
|
+
--model <name> model passed to the CLI (claude: ${claude_code_ts_1.CLAUDE_DEFAULT_MODEL}, codex: its default)
|
|
46
|
+
--raw print the keysmash without calling the LLM
|
|
47
|
+
--json-detail print { seed, text, mid } as JSON (mid = keysmash)
|
|
48
|
+
-h, --help show this help
|
|
49
|
+
|
|
50
|
+
Examples:
|
|
51
|
+
keysmash-ipsum 6
|
|
52
|
+
keysmash-ipsum sentences 2 --lang ja
|
|
53
|
+
keysmash-ipsum words 12 --json-detail
|
|
54
|
+
keysmash-ipsum --raw --seed s1 30
|
|
55
|
+
`;
|
|
56
|
+
/** 先頭の位置引数がモード名ならモードとして取り、残りを語数・文数の候補として返す */
|
|
57
|
+
const splitPositionals = (positionals) => {
|
|
58
|
+
const [first, ...rest] = positionals;
|
|
59
|
+
const mode = MODES.find((name) => name === first);
|
|
60
|
+
return mode === undefined
|
|
61
|
+
? { mode: 'words', rest: positionals }
|
|
62
|
+
: { mode, rest };
|
|
63
|
+
};
|
|
64
|
+
const readCount = (mode, rest, option) => {
|
|
65
|
+
// `lang ja` のように -- を付け忘れた引数を黙って捨てないため、count 以外の位置引数は弾く
|
|
66
|
+
const extra = option === undefined ? rest.slice(1) : rest;
|
|
67
|
+
if (extra.length > 0) {
|
|
68
|
+
throw new Error(`不明な引数: ${extra.join(' ')} (オプションは --lang ja のように -- を付けてください。-h で使い方)`);
|
|
69
|
+
}
|
|
70
|
+
const limit = LIMITS[mode];
|
|
71
|
+
const count = (0, cli_args_ts_1.parseCount)(option ?? rest[0], 'count') ?? limit.default;
|
|
72
|
+
if (count > limit.max) {
|
|
73
|
+
throw new Error(`${mode} の count は ${limit.max} 以下: ${count}`);
|
|
74
|
+
}
|
|
75
|
+
return count;
|
|
76
|
+
};
|
|
77
|
+
/** モードごとに keysmash を作り、読み解き方を束ねる。ja の文は間に空白を入れない */
|
|
78
|
+
const prepare = (mode, count, seed, lang, layout) => {
|
|
79
|
+
if (mode === 'words') {
|
|
80
|
+
const words = (0, words_ts_1.generateWords)({ seed, count, lang, layout });
|
|
81
|
+
return {
|
|
82
|
+
mid: words.map((word) => word.text).join(' '),
|
|
83
|
+
restore: async (complete) => (await (0, restore_ts_1.restoreWords)(words, { lang, complete })).join(' '),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const sentences = (0, generate_ts_1.generate)({ seed, count, lang, layout });
|
|
87
|
+
const joint = lang === 'ja' ? '' : ' ';
|
|
88
|
+
return {
|
|
89
|
+
mid: sentences.map((sentence) => sentence.text).join(joint),
|
|
90
|
+
restore: async (complete) => (await (0, restore_ts_1.restore)(sentences, { lang, complete })).join(joint),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
const runCli = async (argv, deps = {}) => {
|
|
94
|
+
const { values, positionals } = (0, node_util_1.parseArgs)({
|
|
95
|
+
args: argv,
|
|
96
|
+
allowPositionals: true,
|
|
97
|
+
options: {
|
|
98
|
+
count: { type: 'string' },
|
|
99
|
+
lang: { type: 'string' },
|
|
100
|
+
seed: { type: 'string' },
|
|
101
|
+
layout: { type: 'string' },
|
|
102
|
+
llm: { type: 'string', default: 'claude' },
|
|
103
|
+
model: { type: 'string' },
|
|
104
|
+
raw: { type: 'boolean', default: false },
|
|
105
|
+
'json-detail': { type: 'boolean', default: false },
|
|
106
|
+
help: { type: 'boolean', short: 'h', default: false },
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
if (values.help)
|
|
110
|
+
return HELP;
|
|
111
|
+
const { mode, rest } = splitPositionals(positionals);
|
|
112
|
+
const count = readCount(mode, rest, values.count);
|
|
113
|
+
// seed 省略時の時刻 seed を --json-detail で出すため、先に確定させる
|
|
114
|
+
const { seed, lang } = (0, generate_ts_1.resolveOptions)({
|
|
115
|
+
seed: values.seed,
|
|
116
|
+
lang: (0, cli_args_ts_1.oneOf)(values.lang, ['en', 'ja'], 'lang'),
|
|
117
|
+
});
|
|
118
|
+
const layout = (0, cli_args_ts_1.oneOf)(values.layout, ['qwerty', 'uniform'], 'layout');
|
|
119
|
+
const keysmash = prepare(mode, count, seed, lang, layout);
|
|
120
|
+
const llm = (0, cli_args_ts_1.oneOf)(values.llm, index_ts_1.LLM_NAMES, 'llm') ?? 'claude';
|
|
121
|
+
if (!values.raw) {
|
|
122
|
+
const model = values.model === undefined ? '' : ` (${values.model})`;
|
|
123
|
+
deps.notify?.(`restoring ${count} ${LIMITS[mode].unit} with ${llm}${model}...`);
|
|
124
|
+
}
|
|
125
|
+
const pickComplete = deps.complete ?? index_ts_1.createComplete;
|
|
126
|
+
const text = values.raw
|
|
127
|
+
? keysmash.mid
|
|
128
|
+
: await keysmash.restore(pickComplete(llm, values.model));
|
|
129
|
+
if (!values['json-detail'])
|
|
130
|
+
return `${text}\n`;
|
|
131
|
+
const detail = { seed, text, mid: keysmash.mid };
|
|
132
|
+
return `${JSON.stringify(detail)}\n`;
|
|
133
|
+
};
|
|
134
|
+
exports.runCli = runCli;
|
package/lib/cjs/en.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.smashWordEn = void 0;
|
|
4
|
+
const rng_ts_1 = require("./rng.js");
|
|
5
|
+
const ALPHABET = 'abcdefghijklmnopqrstuvwxyz';
|
|
6
|
+
const QWERTY_ROWS = ['qwertyuiop', 'asdfghjkl', 'zxcvbnm'];
|
|
7
|
+
const HOME_ROW = 'asdfghjkl';
|
|
8
|
+
// 隣のキーへ流れる確率。1 に近いと「asdfjkl」のような手癖の塊になり、0 だと uniform と変わらない。
|
|
9
|
+
// 0.6 は推測の手がかり (それっぽさ) と意外性の釣り合いで選んだ
|
|
10
|
+
const ADJACENT_RATE = 0.6;
|
|
11
|
+
const neighborsOf = (ch) => {
|
|
12
|
+
const rowIndex = QWERTY_ROWS.findIndex((row) => row.includes(ch));
|
|
13
|
+
const row = QWERTY_ROWS[rowIndex];
|
|
14
|
+
if (row === undefined)
|
|
15
|
+
return ALPHABET;
|
|
16
|
+
const col = row.indexOf(ch);
|
|
17
|
+
const sameRow = [col - 1, col + 1].map((c) => row[c]);
|
|
18
|
+
const otherRows = [rowIndex - 1, rowIndex + 1].flatMap((r) => [col - 1, col, col + 1].map((c) => QWERTY_ROWS[r]?.[c]));
|
|
19
|
+
return [...sameRow, ...otherRows]
|
|
20
|
+
.filter((c) => c !== undefined)
|
|
21
|
+
.join('');
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* qwerty: ホーム列から始め、隣のキーへ流れやすいランダムウォーク (本物の keysmash の手癖)。
|
|
25
|
+
* trace は `·` = 隣のキーへ流れた、`|` = ランダムにジャンプした
|
|
26
|
+
*/
|
|
27
|
+
const smashWordEn = (rng, len, layout) => {
|
|
28
|
+
if (layout === 'uniform') {
|
|
29
|
+
const text = Array.from({ length: len }, () => (0, rng_ts_1.pickChar)(rng, ALPHABET)).join('');
|
|
30
|
+
return { text, trace: 'uniform' };
|
|
31
|
+
}
|
|
32
|
+
const first = (0, rng_ts_1.pickChar)(rng, HOME_ROW);
|
|
33
|
+
return Array.from({ length: len - 1 }).reduce((word) => {
|
|
34
|
+
const prev = word.text.at(-1) ?? first;
|
|
35
|
+
const isAdjacent = rng() < ADJACENT_RATE;
|
|
36
|
+
const next = isAdjacent
|
|
37
|
+
? (0, rng_ts_1.pickChar)(rng, neighborsOf(prev))
|
|
38
|
+
: (0, rng_ts_1.pickChar)(rng, ALPHABET);
|
|
39
|
+
return {
|
|
40
|
+
text: word.text + next,
|
|
41
|
+
trace: word.trace + (isAdjacent ? '·' : '|') + next,
|
|
42
|
+
};
|
|
43
|
+
}, { text: first, trace: first });
|
|
44
|
+
};
|
|
45
|
+
exports.smashWordEn = smashWordEn;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.runGen = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* skill の gen.ts 用。agent が自分で読み解くので LLM は呼ばず、ヘッダー付きで keysmash を出す
|
|
6
|
+
* gen.ts [--seed <s>] [--count <sentences>] [--words <min-max>] [--word-len <min-max>]
|
|
7
|
+
* [--layout qwerty|uniform] [--lang en|ja] [--seps <chars>] [--trace]
|
|
8
|
+
* 出力: 1 行目 `# seed=<s> lang=<lang>`、以降 1 行 1 文
|
|
9
|
+
*/
|
|
10
|
+
const node_util_1 = require("node:util");
|
|
11
|
+
const cli_args_ts_1 = require("./cli-args.js");
|
|
12
|
+
const generate_ts_1 = require("./generate.js");
|
|
13
|
+
// trace の桁揃え用。かなは端末で 2 桁ぶん取るので文字数ではなく表示幅で数える
|
|
14
|
+
const FULLWIDTH_FROM = 0x3000;
|
|
15
|
+
const displayWidth = (text) => [...text].reduce((sum, ch) => sum + ((ch.codePointAt(0) ?? 0) >= FULLWIDTH_FROM ? 2 : 1), 0);
|
|
16
|
+
const formatTrace = (sentence) => {
|
|
17
|
+
const width = Math.max(...sentence.words.map((word) => displayWidth(word.text)));
|
|
18
|
+
return sentence.words.map((word) => ` ${word.text}${' '.repeat(width - displayWidth(word.text))} ${word.trace}`);
|
|
19
|
+
};
|
|
20
|
+
const runGen = (argv) => {
|
|
21
|
+
const { values } = (0, node_util_1.parseArgs)({
|
|
22
|
+
args: argv,
|
|
23
|
+
options: {
|
|
24
|
+
seed: { type: 'string' },
|
|
25
|
+
count: { type: 'string' },
|
|
26
|
+
words: { type: 'string' },
|
|
27
|
+
'word-len': { type: 'string' },
|
|
28
|
+
layout: { type: 'string' },
|
|
29
|
+
lang: { type: 'string' },
|
|
30
|
+
seps: { type: 'string' },
|
|
31
|
+
trace: { type: 'boolean', default: false },
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
const opts = {
|
|
35
|
+
seed: values.seed,
|
|
36
|
+
count: (0, cli_args_ts_1.parseCount)(values.count, '--count'),
|
|
37
|
+
words: (0, cli_args_ts_1.parseRange)(values.words, 'words'),
|
|
38
|
+
wordLen: (0, cli_args_ts_1.parseRange)(values['word-len'], 'word-len'),
|
|
39
|
+
layout: (0, cli_args_ts_1.oneOf)(values.layout, ['qwerty', 'uniform'], 'layout'),
|
|
40
|
+
lang: (0, cli_args_ts_1.oneOf)(values.lang, ['en', 'ja'], 'lang'),
|
|
41
|
+
seps: values.seps,
|
|
42
|
+
};
|
|
43
|
+
// seed 省略時の時刻 seed をヘッダーにも出すため、先に確定させてから generate に渡す
|
|
44
|
+
const resolved = (0, generate_ts_1.resolveOptions)(opts);
|
|
45
|
+
const body = (0, generate_ts_1.generate)(resolved).flatMap((sentence) => values.trace ? [sentence.text, ...formatTrace(sentence)] : [sentence.text]);
|
|
46
|
+
const header = `# seed=${resolved.seed} lang=${resolved.lang}`;
|
|
47
|
+
return `${[header, ...body].join('\n')}\n`;
|
|
48
|
+
};
|
|
49
|
+
exports.runGen = runGen;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generate = exports.resolveOptions = void 0;
|
|
4
|
+
const en_ts_1 = require("./en.js");
|
|
5
|
+
const kana_ts_1 = require("./kana.js");
|
|
6
|
+
const rng_ts_1 = require("./rng.js");
|
|
7
|
+
// 区切りの先頭文字 (既定は空白) が選ばれる確率。残りを他の区切り文字で等分する
|
|
8
|
+
const PRIMARY_SEP_RATE = 0.85;
|
|
9
|
+
// 1 語の長さの既定。en は文字数、ja は助詞を除いたかなの数 (日本語の文節は 2〜5 かな程度が多い)
|
|
10
|
+
const DEFAULT_WORD_LEN = {
|
|
11
|
+
en: { min: 2, max: 8 },
|
|
12
|
+
ja: { min: 2, max: 5 },
|
|
13
|
+
};
|
|
14
|
+
const DEFAULT_WORDS = { min: 4, max: 9 };
|
|
15
|
+
const JA_PUNCT = { ',': '、', '.': '。' };
|
|
16
|
+
const assertRange = (range, name) => {
|
|
17
|
+
const isInt = Number.isInteger(range.min) && Number.isInteger(range.max);
|
|
18
|
+
if (!isInt || range.min < 1 || range.max < range.min) {
|
|
19
|
+
throw new Error(`${name} の範囲が不正です: ${range.min}-${range.max}`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const resolveOptions = (opts = {}) => {
|
|
23
|
+
const lang = opts.lang ?? 'en';
|
|
24
|
+
const resolved = {
|
|
25
|
+
seed: opts.seed ?? String(Date.now()),
|
|
26
|
+
count: opts.count ?? 1,
|
|
27
|
+
words: opts.words ?? DEFAULT_WORDS,
|
|
28
|
+
wordLen: opts.wordLen ?? DEFAULT_WORD_LEN[lang],
|
|
29
|
+
layout: opts.layout ?? 'qwerty',
|
|
30
|
+
lang,
|
|
31
|
+
seps: opts.seps ?? ' ,',
|
|
32
|
+
};
|
|
33
|
+
if (!Number.isInteger(resolved.count) || resolved.count < 1) {
|
|
34
|
+
throw new Error(`count は 1 以上の整数: ${resolved.count}`);
|
|
35
|
+
}
|
|
36
|
+
if (resolved.seps.length === 0)
|
|
37
|
+
throw new Error('seps は 1 文字以上');
|
|
38
|
+
assertRange(resolved.words, 'words');
|
|
39
|
+
assertRange(resolved.wordLen, 'wordLen');
|
|
40
|
+
return resolved;
|
|
41
|
+
};
|
|
42
|
+
exports.resolveOptions = resolveOptions;
|
|
43
|
+
const pickSep = (rng, seps) => {
|
|
44
|
+
const [primary = ' ', ...rest] = [...seps];
|
|
45
|
+
if (rest.length === 0 || rng() < PRIMARY_SEP_RATE)
|
|
46
|
+
return primary;
|
|
47
|
+
const sep = (0, rng_ts_1.pick)(rng, rest);
|
|
48
|
+
return sep === ' ' ? sep : `${sep} `;
|
|
49
|
+
};
|
|
50
|
+
const toJaPunct = (line) => [...line]
|
|
51
|
+
.map((ch) => JA_PUNCT[ch] ?? ch)
|
|
52
|
+
.join('')
|
|
53
|
+
.replace(/、 /g, '、');
|
|
54
|
+
const smashSentence = (rng, opts) => {
|
|
55
|
+
const wordCount = (0, rng_ts_1.between)(rng, opts.words);
|
|
56
|
+
const words = Array.from({ length: wordCount }, () => {
|
|
57
|
+
const len = (0, rng_ts_1.between)(rng, opts.wordLen);
|
|
58
|
+
return opts.lang === 'ja'
|
|
59
|
+
? (0, kana_ts_1.smashWordKana)(rng, len, opts.layout === 'uniform')
|
|
60
|
+
: (0, en_ts_1.smashWordEn)(rng, len, opts.layout);
|
|
61
|
+
});
|
|
62
|
+
// 1 語だけのときはラベルや仮の名前に使うので文末記号を付けない
|
|
63
|
+
const end = wordCount === 1 ? '' : '.';
|
|
64
|
+
const raw = words.reduce((acc, word, i) => i === 0 ? word.text : acc + pickSep(rng, opts.seps) + word.text, '') + end;
|
|
65
|
+
return { text: opts.lang === 'ja' ? toJaPunct(raw) : raw, words };
|
|
66
|
+
};
|
|
67
|
+
/** keysmash 文字列を seed から決定的に生成する。LLM は使わない */
|
|
68
|
+
const generate = (opts = {}) => {
|
|
69
|
+
const resolved = (0, exports.resolveOptions)(opts);
|
|
70
|
+
const rng = (0, rng_ts_1.createRng)(resolved.seed);
|
|
71
|
+
return Array.from({ length: resolved.count }, () => smashSentence(rng, resolved));
|
|
72
|
+
};
|
|
73
|
+
exports.generate = generate;
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseWords = exports.generateWords = exports.buildWordsSchema = exports.buildWordsRequest = exports.restoreWords = exports.restore = exports.keysmashIpsum = exports.RestoreMismatchError = exports.parseRestored = exports.buildSchema = exports.buildRequest = exports.resolveOptions = exports.generate = void 0;
|
|
4
|
+
var generate_ts_1 = require("./generate.js");
|
|
5
|
+
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_ts_1.generate; } });
|
|
6
|
+
Object.defineProperty(exports, "resolveOptions", { enumerable: true, get: function () { return generate_ts_1.resolveOptions; } });
|
|
7
|
+
var prompt_ts_1 = require("./prompt.js");
|
|
8
|
+
Object.defineProperty(exports, "buildRequest", { enumerable: true, get: function () { return prompt_ts_1.buildRequest; } });
|
|
9
|
+
Object.defineProperty(exports, "buildSchema", { enumerable: true, get: function () { return prompt_ts_1.buildSchema; } });
|
|
10
|
+
Object.defineProperty(exports, "parseRestored", { enumerable: true, get: function () { return prompt_ts_1.parseRestored; } });
|
|
11
|
+
var request_ts_1 = require("./request.js");
|
|
12
|
+
Object.defineProperty(exports, "RestoreMismatchError", { enumerable: true, get: function () { return request_ts_1.RestoreMismatchError; } });
|
|
13
|
+
var restore_ts_1 = require("./restore.js");
|
|
14
|
+
Object.defineProperty(exports, "keysmashIpsum", { enumerable: true, get: function () { return restore_ts_1.keysmashIpsum; } });
|
|
15
|
+
Object.defineProperty(exports, "restore", { enumerable: true, get: function () { return restore_ts_1.restore; } });
|
|
16
|
+
Object.defineProperty(exports, "restoreWords", { enumerable: true, get: function () { return restore_ts_1.restoreWords; } });
|
|
17
|
+
var words_ts_1 = require("./words.js");
|
|
18
|
+
Object.defineProperty(exports, "buildWordsRequest", { enumerable: true, get: function () { return words_ts_1.buildWordsRequest; } });
|
|
19
|
+
Object.defineProperty(exports, "buildWordsSchema", { enumerable: true, get: function () { return words_ts_1.buildWordsSchema; } });
|
|
20
|
+
Object.defineProperty(exports, "generateWords", { enumerable: true, get: function () { return words_ts_1.generateWords; } });
|
|
21
|
+
Object.defineProperty(exports, "parseWords", { enumerable: true, get: function () { return words_ts_1.parseWords; } });
|