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.
Files changed (256) hide show
  1. package/README.md +24 -2
  2. package/dist/agent/bundled-skills.d.ts +13 -0
  3. package/dist/agent/bundled-skills.js +95 -0
  4. package/dist/agent/images.d.ts +10 -0
  5. package/dist/agent/images.js +33 -0
  6. package/dist/agent/loop.d.ts +56 -0
  7. package/dist/agent/loop.js +155 -0
  8. package/dist/agent/skills.d.ts +20 -0
  9. package/dist/agent/skills.js +169 -0
  10. package/dist/agent/types.d.ts +132 -0
  11. package/dist/agent/types.js +12 -0
  12. package/dist/cli/agents.d.ts +8 -0
  13. package/dist/cli/agents.js +12 -0
  14. package/dist/cli/approval.d.ts +27 -0
  15. package/dist/cli/approval.js +62 -0
  16. package/dist/cli/catalog.d.ts +39 -0
  17. package/dist/cli/catalog.js +76 -0
  18. package/dist/cli/chat.d.ts +18 -0
  19. package/dist/cli/chat.js +733 -0
  20. package/dist/cli/clipboard.d.ts +9 -0
  21. package/dist/cli/clipboard.js +54 -0
  22. package/dist/cli/color.d.ts +42 -0
  23. package/dist/cli/color.js +176 -0
  24. package/dist/cli/config.d.ts +41 -0
  25. package/dist/cli/config.js +188 -0
  26. package/dist/cli/editor.d.ts +16 -0
  27. package/dist/cli/editor.js +77 -0
  28. package/dist/cli/keys.d.ts +18 -0
  29. package/dist/cli/keys.js +82 -0
  30. package/dist/cli/language.d.ts +1 -0
  31. package/dist/cli/language.js +105 -0
  32. package/dist/cli/limits.d.ts +13 -0
  33. package/dist/cli/limits.js +19 -0
  34. package/dist/cli/lines.d.ts +18 -0
  35. package/dist/cli/lines.js +80 -0
  36. package/dist/cli/models.d.ts +4 -0
  37. package/dist/cli/models.js +69 -0
  38. package/dist/cli/permissions.d.ts +25 -0
  39. package/dist/cli/permissions.js +30 -0
  40. package/dist/cli/plugins.d.ts +1 -0
  41. package/dist/cli/plugins.js +53 -0
  42. package/dist/cli/providers.d.ts +46 -0
  43. package/dist/cli/providers.js +76 -0
  44. package/dist/cli/terminal.d.ts +143 -0
  45. package/dist/cli/terminal.js +742 -0
  46. package/dist/cli/usage.d.ts +29 -0
  47. package/dist/cli/usage.js +75 -0
  48. package/dist/index.d.ts +11 -0
  49. package/dist/index.js +164 -0
  50. package/dist/language/catalog.d.ts +44 -0
  51. package/dist/language/catalog.js +100 -0
  52. package/dist/language/commonlisp-bundled.d.ts +11 -0
  53. package/dist/language/commonlisp-bundled.js +22 -0
  54. package/dist/language/commonlisp-dependencies.d.ts +16 -0
  55. package/dist/language/commonlisp-dependencies.js +68 -0
  56. package/dist/language/commonlisp-install.d.ts +14 -0
  57. package/dist/language/commonlisp-install.js +213 -0
  58. package/dist/language/commonlisp-reader.d.ts +17 -0
  59. package/dist/language/commonlisp-reader.js +47 -0
  60. package/dist/language/commonlisp-systems.d.ts +3 -0
  61. package/dist/language/commonlisp-systems.js +32 -0
  62. package/dist/language/commonlisp-tls.d.ts +10 -0
  63. package/dist/language/commonlisp-tls.js +51 -0
  64. package/dist/language/detect.d.ts +29 -0
  65. package/dist/language/detect.js +103 -0
  66. package/dist/language/grammars/LICENSE +24 -0
  67. package/dist/language/grammars/manifest.json +76 -0
  68. package/dist/language/grammars/tree-sitter-c.wasm +0 -0
  69. package/dist/language/grammars/tree-sitter-cpp.wasm +0 -0
  70. package/dist/language/grammars/tree-sitter-css.wasm +0 -0
  71. package/dist/language/grammars/tree-sitter-html.wasm +0 -0
  72. package/dist/language/grammars/tree-sitter-javascript.wasm +0 -0
  73. package/dist/language/grammars/tree-sitter-php.wasm +0 -0
  74. package/dist/language/grammars/tree-sitter-python.wasm +0 -0
  75. package/dist/language/grammars/tree-sitter-ruby.wasm +0 -0
  76. package/dist/language/grammars/tree-sitter-rust.wasm +0 -0
  77. package/dist/language/grammars/tree-sitter-swift.wasm +0 -0
  78. package/dist/language/grammars/tree-sitter-tsx.wasm +0 -0
  79. package/dist/language/grammars/tree-sitter-typescript.wasm +0 -0
  80. package/dist/language/install.d.ts +50 -0
  81. package/dist/language/install.js +293 -0
  82. package/dist/language/lsp/client.d.ts +97 -0
  83. package/dist/language/lsp/client.js +337 -0
  84. package/dist/language/lsp/pool.d.ts +67 -0
  85. package/dist/language/lsp/pool.js +122 -0
  86. package/dist/language/lsp/service.d.ts +46 -0
  87. package/dist/language/lsp/service.js +138 -0
  88. package/dist/language/native.d.ts +8 -0
  89. package/dist/language/native.js +100 -0
  90. package/dist/language/platform.d.ts +14 -0
  91. package/dist/language/platform.js +20 -0
  92. package/dist/language/positions.d.ts +20 -0
  93. package/dist/language/positions.js +46 -0
  94. package/dist/language/syntax/assets.d.ts +9 -0
  95. package/dist/language/syntax/assets.js +73 -0
  96. package/dist/language/syntax/parser.d.ts +24 -0
  97. package/dist/language/syntax/parser.js +144 -0
  98. package/dist/language/typescript-capability.d.ts +6 -0
  99. package/dist/language/typescript-capability.js +42 -0
  100. package/dist/lisp/config.d.ts +16 -0
  101. package/dist/lisp/config.js +159 -0
  102. package/dist/lisp/routing.d.ts +5 -0
  103. package/dist/lisp/routing.js +24 -0
  104. package/dist/lisp/runtime.d.ts +21 -0
  105. package/dist/lisp/runtime.js +83 -0
  106. package/dist/memory/kiokuko.d.ts +32 -0
  107. package/dist/memory/kiokuko.js +102 -0
  108. package/dist/memory/mcp.d.ts +38 -0
  109. package/dist/memory/mcp.js +211 -0
  110. package/dist/model/ai-sdk.d.ts +27 -0
  111. package/dist/model/ai-sdk.js +180 -0
  112. package/dist/model/chatgpt/login.d.ts +42 -0
  113. package/dist/model/chatgpt/login.js +180 -0
  114. package/dist/model/chatgpt/responses.d.ts +41 -0
  115. package/dist/model/chatgpt/responses.js +202 -0
  116. package/dist/model/chatgpt/tokens.d.ts +11 -0
  117. package/dist/model/chatgpt/tokens.js +50 -0
  118. package/dist/model/connection.d.ts +6 -0
  119. package/dist/model/connection.js +65 -0
  120. package/dist/model/context.d.ts +9 -0
  121. package/dist/model/context.js +69 -0
  122. package/dist/model/factory.d.ts +25 -0
  123. package/dist/model/factory.js +88 -0
  124. package/dist/model/fixed.d.ts +17 -0
  125. package/dist/model/fixed.js +41 -0
  126. package/dist/model/reasoning.d.ts +16 -0
  127. package/dist/model/reasoning.js +62 -0
  128. package/dist/plugins/api.d.ts +60 -0
  129. package/dist/plugins/api.js +1 -0
  130. package/dist/plugins/manager.d.ts +34 -0
  131. package/dist/plugins/manager.js +261 -0
  132. package/dist/plugins/package.d.ts +13 -0
  133. package/dist/plugins/package.js +99 -0
  134. package/dist/plugins/registry.d.ts +8 -0
  135. package/dist/plugins/registry.js +14 -0
  136. package/dist/session/store.d.ts +82 -0
  137. package/dist/session/store.js +285 -0
  138. package/dist/session/workspace.d.ts +19 -0
  139. package/dist/session/workspace.js +73 -0
  140. package/dist/subagents/processes.d.ts +18 -0
  141. package/dist/subagents/processes.js +73 -0
  142. package/dist/subagents/request.d.ts +4 -0
  143. package/dist/subagents/request.js +21 -0
  144. package/dist/subagents/service.d.ts +39 -0
  145. package/dist/subagents/service.js +352 -0
  146. package/dist/subagents/settings.d.ts +10 -0
  147. package/dist/subagents/settings.js +32 -0
  148. package/dist/subagents/types.d.ts +43 -0
  149. package/dist/subagents/types.js +1 -0
  150. package/dist/subagents/verification.d.ts +31 -0
  151. package/dist/subagents/verification.js +178 -0
  152. package/dist/tools/diff.d.ts +6 -0
  153. package/dist/tools/diff.js +82 -0
  154. package/dist/tools/edit.d.ts +3 -0
  155. package/dist/tools/edit.js +152 -0
  156. package/dist/tools/files.d.ts +6 -0
  157. package/dist/tools/files.js +96 -0
  158. package/dist/tools/formal.d.ts +8 -0
  159. package/dist/tools/formal.js +77 -0
  160. package/dist/tools/guard.d.ts +7 -0
  161. package/dist/tools/guard.js +48 -0
  162. package/dist/tools/job-tools.d.ts +19 -0
  163. package/dist/tools/job-tools.js +148 -0
  164. package/dist/tools/jobs.d.ts +77 -0
  165. package/dist/tools/jobs.js +181 -0
  166. package/dist/tools/language.d.ts +6 -0
  167. package/dist/tools/language.js +174 -0
  168. package/dist/tools/registry.d.ts +28 -0
  169. package/dist/tools/registry.js +105 -0
  170. package/dist/tools/run.d.ts +13 -0
  171. package/dist/tools/run.js +159 -0
  172. package/dist/tools/search.d.ts +2 -0
  173. package/dist/tools/search.js +156 -0
  174. package/dist/tools/shell-process.d.ts +115 -0
  175. package/dist/tools/shell-process.js +286 -0
  176. package/dist/tools/skills.d.ts +2 -0
  177. package/dist/tools/skills.js +46 -0
  178. package/dist/tools/web.d.ts +24 -0
  179. package/dist/tools/web.js +386 -0
  180. package/docs/ci-repair-20261116.md +30 -0
  181. package/docs/commonlisp-provider-migration.md +22 -0
  182. package/docs/commonlisp-qlot-recovery.md +28 -0
  183. package/docs/commonlisp-tls-recovery.md +28 -0
  184. package/docs/context-efficiency.md +100 -0
  185. package/docs/input.md +66 -0
  186. package/docs/languages.md +117 -0
  187. package/docs/models.md +85 -0
  188. package/docs/modkh-evidence.json +641 -0
  189. package/docs/modkh-failure-comparison.json +675 -0
  190. package/docs/modkh-namespace.md +39 -0
  191. package/docs/modkh-qlot-evidence.json +37 -0
  192. package/docs/modkh-verification.md +173 -0
  193. package/docs/modkh.md +124 -0
  194. package/docs/presets.md +19 -0
  195. package/docs/setup.md +112 -0
  196. package/docs/subagents.md +134 -0
  197. package/docs/web.md +57 -0
  198. package/experiments/presets/common-lisp.lisp +38 -0
  199. package/experiments/presets/css.lisp +38 -0
  200. package/experiments/presets/formal.lisp +7 -0
  201. package/experiments/presets/html.lisp +38 -0
  202. package/experiments/presets/js.lisp +38 -0
  203. package/experiments/presets/php.lisp +38 -0
  204. package/experiments/presets/svelte.lisp +38 -0
  205. package/experiments/presets/tailwindcss.lisp +24 -0
  206. package/experiments/presets/ts.lisp +38 -0
  207. package/package.json +49 -4
  208. package/plugins/modkh-explorer/index.mjs +3 -0
  209. package/plugins/modkh-explorer/plugin.json +1 -0
  210. package/plugins/modkh-worker/index.mjs +3 -0
  211. package/plugins/modkh-worker/plugin.json +1 -0
  212. package/skills/kiokuko-dsh/LICENSE +21 -0
  213. package/skills/kiokuko-dsh/coding-ideal-routine-skill/SKILL.md +106 -0
  214. package/skills/kiokuko-dsh/japanese-translation-for-oss-models/SKILL.md +65 -0
  215. package/skills/kiokuko-dsh/kiokuko-architecture/SKILL.md +20 -0
  216. package/skills/kiokuko-dsh/kiokuko-architecture/references/cases.md +11 -0
  217. package/skills/kiokuko-dsh/kiokuko-benchmark/SKILL.md +20 -0
  218. package/skills/kiokuko-dsh/kiokuko-benchmark/references/cases.md +11 -0
  219. package/skills/kiokuko-dsh/kiokuko-investigate/SKILL.md +20 -0
  220. package/skills/kiokuko-dsh/kiokuko-investigate/references/cases.md +11 -0
  221. package/skills/kiokuko-dsh/kiokuko-lisp/SKILL.md +390 -0
  222. package/skills/kiokuko-dsh/kiokuko-review/SKILL.md +20 -0
  223. package/skills/kiokuko-dsh/kiokuko-review/references/cases.md +11 -0
  224. package/skills/kiokuko-dsh/kiokuko-simple-work/SKILL.md +70 -0
  225. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/SKILL.md +79 -0
  226. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/boundaries-and-ownership.md +52 -0
  227. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/domain-and-types.md +48 -0
  228. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/effects-and-data.md +43 -0
  229. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/kiokuko-patterns.md +314 -0
  230. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/problem-shaping-and-language.md +90 -0
  231. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/protocols-and-idempotency.md +42 -0
  232. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/review-checklist.md +92 -0
  233. package/skills/kiokuko-dsh/kiokuko-single-purpose-functions/references/verification.md +45 -0
  234. package/skills/kiokuko-dsh/kiokuko-skill-authoring/SKILL.md +20 -0
  235. package/skills/kiokuko-dsh/kiokuko-skill-authoring/references/cases.md +11 -0
  236. package/skills/kiokuko-dsh/kiokuko-soul/SKILL.md +25 -0
  237. package/skills/kiokuko-dsh/kiokuko-technical-writing/SKILL.md +20 -0
  238. package/skills/kiokuko-dsh/kiokuko-technical-writing/references/cases.md +11 -0
  239. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/SKILL.md +64 -0
  240. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/accessibility-and-navigation.md +31 -0
  241. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/async-recovery.md +49 -0
  242. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/forms-and-controls.md +35 -0
  243. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/interaction-feedback.md +34 -0
  244. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/responsive-and-platform.md +38 -0
  245. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/safety-and-review.md +43 -0
  246. package/skills/kiokuko-dsh/kiokuko-ui-design-soul/references/ui-checklist.md +75 -0
  247. package/skills/kiokuko-dsh/kiokuko-verification/SKILL.md +20 -0
  248. package/skills/kiokuko-dsh/kiokuko-verification/references/cases.md +11 -0
  249. package/skills/kiokuko-dsh/manifest.json +168 -0
  250. package/skills/kiokuko-dsh/memory-reasoning/SKILL.md +44 -0
  251. package/skills/kiokuko-dsh/one-shot-software-completion/SKILL.md +113 -0
  252. package/skills/kiokuko-dsh/one-shot-software-completion/references/boundaries-and-lifecycle.md +70 -0
  253. package/skills/kiokuko-dsh/one-shot-software-completion/references/discovery-and-scope.md +71 -0
  254. package/skills/kiokuko-dsh/one-shot-software-completion/references/failure-recovery.md +43 -0
  255. package/skills/kiokuko-dsh/one-shot-software-completion/references/verification-and-completion.md +73 -0
  256. package/skills/kiokuko-dsh/veteran-programmer-skill/SKILL.md +66 -0
@@ -0,0 +1,286 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { existsSync } from 'node:fs';
3
+ import { StringDecoder } from 'node:string_decoder';
4
+ import { ToolError } from "../agent/types.js";
5
+ /** Keep both ends of oversized output: the head names what ran, the tail holds the failure. */
6
+ const HEAD_SHARE = 0.25;
7
+ function requireString(v, name) {
8
+ if (typeof v !== 'string' || v.length === 0) {
9
+ throw new ToolError('INVALID_ARGS', `${name} は空でない文字列で指定する`);
10
+ }
11
+ return v;
12
+ }
13
+ export const SHELLS = ['bash', 'zsh', 'sh'];
14
+ /** The shell that backs `sh`. Falls back through the usual install locations. */
15
+ export function resolveShell(name = 'sh') {
16
+ if (name === 'sh')
17
+ return { file: '/bin/sh', name };
18
+ const candidates = name === 'bash'
19
+ ? ['/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash']
20
+ : ['/bin/zsh', '/usr/bin/zsh'];
21
+ for (const file of candidates)
22
+ if (existsSync(file))
23
+ return { file, name };
24
+ throw new ToolError('INVALID_ARGS', `${name} が見つからない。/bin/sh を使う`);
25
+ }
26
+ /**
27
+ * A bounded text buffer with two views over the same stream.
28
+ *
29
+ * - `read(from)` serves a background job: it returns everything after an absolute position, so
30
+ * repeated polling reports only what is new. Old text is evicted, and a reader that fell
31
+ * behind is told how much it missed instead of silently receiving a gap.
32
+ * - `text()` serves a foreground result: it keeps the FIRST part and the LAST part, because a
33
+ * failing command's reason is at the end while the beginning says what ran.
34
+ *
35
+ * Keeping the head permanently is what makes both possible; a plain rolling buffer loses it.
36
+ */
37
+ export class TextSink {
38
+ chunks = [];
39
+ first = 0;
40
+ length = 0;
41
+ budget;
42
+ headBudget;
43
+ head = '';
44
+ /** Total characters ever appended, including text already evicted. */
45
+ produced = 0;
46
+ constructor(budget) {
47
+ this.budget = budget;
48
+ this.headBudget = Math.floor(budget * HEAD_SHARE);
49
+ }
50
+ push(text) {
51
+ if (text.length === 0)
52
+ return;
53
+ if (this.head.length < this.headBudget) {
54
+ const room = this.headBudget - this.head.length;
55
+ this.head += text.slice(0, room);
56
+ }
57
+ this.chunks.push(text);
58
+ this.length += text.length;
59
+ this.produced += text.length;
60
+ const tailBudget = this.budget - this.headBudget;
61
+ while (this.length > tailBudget && this.chunks.length > 1) {
62
+ const dropped = this.chunks.shift();
63
+ this.length -= dropped.length;
64
+ this.first += dropped.length;
65
+ }
66
+ }
67
+ get retainedLength() { return this.length; }
68
+ /** Absolute offset of the first character still available to `read`. */
69
+ get retainedFrom() { return this.first; }
70
+ /** The foreground view: head, an elision marker, then the retained tail. */
71
+ text() {
72
+ const tail = this.chunks.join('').slice(-(this.budget - this.headBudget));
73
+ const shown = this.head.length + tail.length;
74
+ if (this.produced <= shown)
75
+ return this.head + (this.produced > this.head.length ? tail.slice(this.head.length) : '');
76
+ return `${this.head}\n…(${this.produced - shown} 文字省略)…\n${tail}`;
77
+ }
78
+ /** Everything appended after absolute position `from` (background-job view). */
79
+ read(from) {
80
+ const all = this.chunks.join('');
81
+ if (from >= this.produced)
82
+ return { text: '', next: this.produced, dropped: 0 };
83
+ if (from < this.first)
84
+ return { text: all, next: this.produced, dropped: this.first - from };
85
+ return { text: all.slice(from - this.first), next: this.produced, dropped: 0 };
86
+ }
87
+ }
88
+ /**
89
+ * One shell invocation and its process group.
90
+ *
91
+ * `result` settles when the shell closed AND its group is gone, so a command cannot exit while
92
+ * silently abandoning children. A stop is always a reject: the caller cannot tell "the command
93
+ * failed" from "we killed it", and pretending otherwise would hide a cancelled effect.
94
+ */
95
+ export class ShellProcess {
96
+ shell;
97
+ startedAt = Date.now();
98
+ out;
99
+ err;
100
+ result;
101
+ child;
102
+ timeoutMs;
103
+ settle;
104
+ fail;
105
+ settled = false;
106
+ closed = false;
107
+ code = null;
108
+ exitSignal = null;
109
+ stopReason;
110
+ failure;
111
+ escalation;
112
+ cleanupDeadline;
113
+ poll;
114
+ timer;
115
+ promoteOnTimeout;
116
+ constructor(command, cwd, opts = {}) {
117
+ requireString(command, 'command');
118
+ this.shell = resolveShell(opts.shell).name;
119
+ const maxOutput = opts.maxOutputBytes ?? 32 * 1024;
120
+ this.out = new TextSink(Math.floor(maxOutput / 2));
121
+ this.err = new TextSink(Math.max(1, Math.ceil(maxOutput / 2)));
122
+ this.timeoutMs = opts.timeoutMs ?? 0;
123
+ this.promoteOnTimeout = opts.onTimeout !== undefined;
124
+ const file = resolveShell(opts.shell).file;
125
+ this.child = spawn(file, ['-c', command], {
126
+ cwd,
127
+ ...(opts.env === undefined ? {} : { env: opts.env }),
128
+ detached: true,
129
+ stdio: ['ignore', 'pipe', 'pipe'],
130
+ });
131
+ this.result = new Promise((resolve, reject) => {
132
+ this.settle = resolve;
133
+ this.fail = reject;
134
+ });
135
+ this.child.stdout.on('data', (data) => this.out.push(decode(data)));
136
+ this.child.stderr.on('data', (data) => this.err.push(decode(data)));
137
+ this.child.on('error', (e) => { this.failure = `起動に失敗した: ${e.message}`; });
138
+ this.child.on('exit', () => {
139
+ // Background commands are not a supported ownership transfer.
140
+ if (this.groupExists())
141
+ this.stop('descendants');
142
+ });
143
+ this.child.on('close', (exitCode, sig) => {
144
+ this.closed = true;
145
+ this.code = exitCode;
146
+ this.exitSignal = sig;
147
+ this.finish();
148
+ });
149
+ if (this.timeoutMs > 0) {
150
+ this.timer = setTimeout(() => {
151
+ if (opts.onTimeout) {
152
+ // Deadline reached, but the process keeps running: hand ownership over and let the
153
+ // new owner decide. The timer is spent, so this cannot fire twice.
154
+ this.timer = undefined;
155
+ opts.onTimeout(this);
156
+ return;
157
+ }
158
+ this.stop('timeout');
159
+ }, this.timeoutMs);
160
+ }
161
+ }
162
+ /** Detach from the result: the caller no longer awaits it, but the process keeps running. */
163
+ abandon() {
164
+ clearTimeout(this.timer);
165
+ this.timer = undefined;
166
+ // Keep the pipes open so output continues to fill the sinks for the new owner.
167
+ void this.result.catch(() => { });
168
+ }
169
+ get pid() { return this.child.pid; }
170
+ get isSettled() { return this.settled; }
171
+ get hasSettledResult() { return this.closed && !this.groupExists(); }
172
+ /** True once a stop was requested, so a failure can be reported as a kill. */
173
+ get stoppedByKill() { return this.stopReason === 'killed'; }
174
+ groupExists() {
175
+ if (this.child.pid === undefined)
176
+ return false;
177
+ try {
178
+ process.kill(-this.child.pid, 0);
179
+ return true;
180
+ }
181
+ catch (e) {
182
+ if (e.code === 'ESRCH')
183
+ return false;
184
+ this.failure = `子処理の状態を確認できない: ${e.message}`;
185
+ return true;
186
+ }
187
+ }
188
+ killGroup(sig) {
189
+ if (this.child.pid === undefined)
190
+ return;
191
+ try {
192
+ process.kill(-this.child.pid, sig);
193
+ }
194
+ catch (e) {
195
+ if (e.code !== 'ESRCH') {
196
+ this.failure = `子処理を停止できない: ${e.message}`;
197
+ }
198
+ }
199
+ }
200
+ finish(unconfirmed = false) {
201
+ if (this.settled || (!unconfirmed && (!this.closed || this.groupExists())))
202
+ return;
203
+ this.settled = true;
204
+ clearTimeout(this.timer);
205
+ clearTimeout(this.escalation);
206
+ clearTimeout(this.cleanupDeadline);
207
+ clearInterval(this.poll);
208
+ const result = {
209
+ shell: this.shell, code: this.code, signal: this.exitSignal,
210
+ stdout: this.out.text(), stderr: this.err.text(),
211
+ truncated: this.out.produced > this.out.retainedLength || this.err.produced > this.err.retainedLength,
212
+ timedOut: this.stopReason === 'timeout',
213
+ durationMs: Date.now() - this.startedAt,
214
+ };
215
+ if (unconfirmed) {
216
+ this.child.stdout?.destroy();
217
+ this.child.stderr?.destroy();
218
+ this.fail(new ToolError('RESULT_UNKNOWN', `子処理の停止を確認できない。再実行しない。${this.failure ?? ''}\n${describeShellResult(result)}`));
219
+ }
220
+ else if (this.stopReason) {
221
+ const reason = this.stopReason === 'timeout' ? `制限時間(${this.timeoutMs}ms)を超過した`
222
+ : this.stopReason === 'descendants' ? 'shell終了後に残った子処理を停止した'
223
+ : this.stopReason === 'killed' ? '停止を要求された' : 'コマンドを中断した';
224
+ // Partial output is evidence: keep it in the failure instead of reporting only the reason.
225
+ this.fail(new ToolError('ABORTED', `${reason}(code=${this.code ?? '不明'} signal=${this.exitSignal ?? 'なし'})\n${describeShellResult(result)}`));
226
+ }
227
+ else if (this.failure !== undefined || this.exitSignal !== null) {
228
+ // A signalled shell is an infrastructure stop, not an ordinary failure code.
229
+ this.fail(new ToolError('RUN_FAILED', `${this.failure ?? `signal=${this.exitSignal ?? 'なし'}`}`));
230
+ }
231
+ else {
232
+ this.settle(result);
233
+ }
234
+ }
235
+ /** Terminate the process group; the result rejects with the reason once the group is gone. */
236
+ stop(reason = 'killed') {
237
+ if (this.settled || this.stopReason)
238
+ return;
239
+ this.stopReason = reason;
240
+ this.killGroup('SIGTERM');
241
+ // Do not clear escalation merely because the shell closed its pipes.
242
+ this.escalation = setTimeout(() => { this.killGroup('SIGKILL'); this.finish(); }, 1000);
243
+ this.poll = setInterval(() => this.finish(), 20);
244
+ this.cleanupDeadline = setTimeout(() => this.finish(true), 3000);
245
+ }
246
+ }
247
+ function decode(data) {
248
+ return new StringDecoder('utf8').write(data);
249
+ }
250
+ /** Model-facing text: the exit code leads so the agent reads it before the body. */
251
+ export function describeShellResult(result) {
252
+ const parts = [`[exit code: ${result.code ?? '不明'}]${result.signal ? ` [signal: ${result.signal}]` : ''}`];
253
+ if (result.stdout)
254
+ parts.push(`--- stdout ---\n${result.stdout}`.trimEnd());
255
+ if (result.stderr)
256
+ parts.push(`--- stderr ---\n${result.stderr}`.trimEnd());
257
+ if (!result.stdout && !result.stderr)
258
+ parts.push('(出力なし)');
259
+ if (result.truncated)
260
+ parts.push('[出力が上限に達したため一部を省略した]');
261
+ return parts.join('\n');
262
+ }
263
+ /** Run one command to completion. See {@link ShellProcess} for the stop semantics. */
264
+ export function runShellProcess(command, cwd, signal, opts = {}) {
265
+ if (signal?.aborted)
266
+ return Promise.reject(new ToolError('NOT_EXECUTED', '中断済みのため実行しない'));
267
+ const process_ = new ShellProcess(command, cwd, opts);
268
+ if (signal) {
269
+ const onAbort = () => process_.stop('abort');
270
+ if (signal.aborted)
271
+ process_.stop('abort');
272
+ else
273
+ signal.addEventListener('abort', onAbort, { once: true });
274
+ // The listener is one-shot; a settled process has nothing left to stop.
275
+ void process_.result.finally(() => signal.removeEventListener('abort', onAbort)).catch(() => { });
276
+ }
277
+ return process_.result;
278
+ }
279
+ /** Compatibility wrapper: callers that treat a nonzero exit as a failure keep that contract. */
280
+ export async function runShell(command, cwd, signal, env) {
281
+ const result = await runShellProcess(command, cwd, signal, { env });
282
+ if (result.code !== 0) {
283
+ throw new ToolError('RUN_FAILED', `[exit code: ${result.code ?? '不明'}]\n${describeShellResult(result)}`);
284
+ }
285
+ return describeShellResult(result);
286
+ }
@@ -0,0 +1,2 @@
1
+ import type { Tool } from '../agent/types.ts';
2
+ export declare const skillTools: Tool[];
@@ -0,0 +1,46 @@
1
+ import { ToolError } from "../agent/types.js";
2
+ import { bundledSkills, MODKH_SKILL_BINDING, readBundledSkill } from "../agent/bundled-skills.js";
3
+ export const skillTools = [
4
+ {
5
+ name: 'list_skills', description: 'List bundled Kiokuko skills and their reference resources. Bodies are loaded only with read_skill.',
6
+ readOnly: true, parameters: { type: 'object', properties: {}, additionalProperties: false },
7
+ async execute() {
8
+ return JSON.stringify(bundledSkills().map(skill => ({ name: skill.name, description: skill.description, resources: Object.keys(skill.files) })));
9
+ },
10
+ },
11
+ {
12
+ name: 'read_skill', description: 'Read a bundled Skill runtime index or a risk-selected reference. Relative reference links belong to this skill, not the workspace. Follow nextLine for remaining text.',
13
+ readOnly: true,
14
+ parameters: { type: 'object', properties: {
15
+ name: { type: 'string' }, resource: { type: 'string', description: 'Exact resource from list_skills; defaults to SKILL.md' },
16
+ startLine: { type: 'integer', minimum: 1 },
17
+ }, required: ['name'], additionalProperties: false },
18
+ async execute(args) {
19
+ if (typeof args.name !== 'string' || (args.resource !== undefined && typeof args.resource !== 'string')
20
+ || (args.startLine !== undefined && (!Number.isSafeInteger(args.startLine) || args.startLine < 1)))
21
+ throw new ToolError('INVALID_ARGS', 'Invalid skill name/resource/startLine');
22
+ let body;
23
+ try {
24
+ body = readBundledSkill(args.name, args.resource);
25
+ }
26
+ catch {
27
+ throw new ToolError('SKILL_UNAVAILABLE', 'Unknown or invalid bundled Skill resource');
28
+ }
29
+ const lines = body.split('\n'), start = args.startLine ?? 1;
30
+ if (start > lines.length)
31
+ throw new ToolError('INVALID_ARGS', 'startLine exceeds resource length');
32
+ let output = MODKH_SKILL_BINDING + `\nSkill: ${args.name}, resource: ${args.resource ?? 'SKILL.md'}\n`;
33
+ let next = start;
34
+ while (next <= lines.length) {
35
+ const line = `${next}: ${lines[next - 1]}\n`;
36
+ if (Buffer.byteLength(output + line) > 14 * 1024)
37
+ break;
38
+ output += line;
39
+ next++;
40
+ }
41
+ if (next === start)
42
+ throw new ToolError('SKILL_UNAVAILABLE', 'Skill line exceeds read budget');
43
+ return output + (next <= lines.length ? `\n[nextLine: ${next}; continue read_skill with the same name/resource]` : '\n[end of resource]');
44
+ },
45
+ },
46
+ ];
@@ -0,0 +1,24 @@
1
+ import { type Tool } from '../agent/types.ts';
2
+ export interface WebOptions {
3
+ /** 検索APIのURL(テスト・社内プロキシ用)。既定は Brave。 */
4
+ searchEndpoint?: string;
5
+ /** 本文取得のバイト上限。 */
6
+ maxBytes?: number;
7
+ /** 取得の制限時間(ms)。 */
8
+ timeoutMs?: number;
9
+ /** 検索APIキーの解決。既定は環境変数→保存済みキー。 */
10
+ keyLookup?: (env: NodeJS.ProcessEnv) => string | undefined;
11
+ }
12
+ /** タグを落とし、ブロック境界だけ改行として残す。整形結果の見た目は保証しない。 */
13
+ export declare function htmlToText(html: string): string;
14
+ export declare function createFetchUrlTool(options?: WebOptions): Tool;
15
+ /** Brave の応答から必要な3項目だけを取り出す。形が違う場合は失敗として返す。 */
16
+ export declare function parseBraveResults(body: unknown, limit: number): {
17
+ title: string;
18
+ url: string;
19
+ description: string;
20
+ }[];
21
+ export declare function createWebSearchTool(options?: WebOptions): Tool;
22
+ export declare const fetchUrlTool: Tool;
23
+ export declare const webSearchTool: Tool;
24
+ export declare const webTools: Tool[];