miii-agent 0.1.8 → 0.1.9

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 (3) hide show
  1. package/README.md +45 -3
  2. package/dist/cli.js +1755 -1427
  3. package/package.json +3 -1
package/dist/cli.js CHANGED
@@ -1,34 +1,26 @@
1
1
  #!/usr/bin/env node
2
-
3
- // src/cli.tsx
4
- import { render } from "ink";
5
- import { createElement } from "react";
6
-
7
- // src/ui/App.tsx
8
- import { useState as useState4, useEffect as useEffect3 } from "react";
9
- import { Box as Box10, Text as Text10, useApp } from "ink";
10
- import { homedir as homedir4 } from "os";
11
- import { sep as sep2 } from "path";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
+ };
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
12
11
 
13
12
  // src/ollama/client.ts
14
13
  import { Ollama } from "ollama";
15
14
  import { execFileSync } from "child_process";
16
- var ollama = new Ollama({
17
- host: process.env.OLLAMA_HOST ?? "http://localhost:11434"
18
- });
19
- var OLLAMA_NOT_INSTALLED = "Ollama is not installed. Install it with: npm i -g ollama\nOr download from https://ollama.com/download";
20
- var OLLAMA_NOT_RUNNING = "Ollama is not running. Start it with: ollama serve";
21
15
  function ollamaInstalled() {
22
16
  try {
23
- const cmd = process.platform === "win32" ? "where" : "which";
24
- execFileSync(cmd, ["ollama"], { stdio: "ignore" });
17
+ const cmd2 = process.platform === "win32" ? "where" : "which";
18
+ execFileSync(cmd2, ["ollama"], { stdio: "ignore" });
25
19
  return true;
26
20
  } catch {
27
21
  return false;
28
22
  }
29
23
  }
30
- var HARMONY_RE = /<\|?\/?(?:channel|message|start|end|return|constrain|assistant|user|system|developer|tool|tool_call|tool_response|final|analysis|commentary)\|?>/gi;
31
- var CHANNEL_LABEL_RE = /^(?:analysis|commentary|final)\s*(?=\w)/i;
32
24
  function stripHarmony(s) {
33
25
  if (s == null) return s;
34
26
  let out = s.replace(HARMONY_RE, "");
@@ -138,1532 +130,1862 @@ async function* chat(model, messages, tools, opts) {
138
130
  if (opts?.signal) opts.signal.removeEventListener("abort", onAbort);
139
131
  }
140
132
  }
133
+ var ollama, OLLAMA_NOT_INSTALLED, OLLAMA_NOT_RUNNING, HARMONY_RE, CHANNEL_LABEL_RE;
134
+ var init_client = __esm({
135
+ "src/ollama/client.ts"() {
136
+ "use strict";
137
+ ollama = new Ollama({
138
+ host: process.env.OLLAMA_HOST ?? "http://localhost:11434"
139
+ });
140
+ OLLAMA_NOT_INSTALLED = "Ollama is not installed. Install it with: npm i -g ollama\nOr download from https://ollama.com/download";
141
+ OLLAMA_NOT_RUNNING = "Ollama is not running. Start it with: ollama serve";
142
+ HARMONY_RE = /<\|?\/?(?:channel|message|start|end|return|constrain|assistant|user|system|developer|tool|tool_call|tool_response|final|analysis|commentary)\|?>/gi;
143
+ CHANNEL_LABEL_RE = /^(?:analysis|commentary|final)\s*(?=\w)/i;
144
+ }
145
+ });
141
146
 
142
- // src/config.ts
143
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
144
- import { join } from "path";
145
- import { homedir } from "os";
146
- var CONFIG_DIR = join(homedir(), ".miii");
147
- var CONFIG_PATH = join(CONFIG_DIR, "config.json");
148
- function loadConfig() {
149
- if (!existsSync(CONFIG_PATH)) return {};
150
- try {
151
- return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
152
- } catch {
153
- return {};
147
+ // src/tools/paths.ts
148
+ import { resolve, relative as relative2, isAbsolute, sep } from "path";
149
+ function confinePath(p) {
150
+ if (typeof p !== "string" || p.length === 0) {
151
+ throw new Error("Path is required.");
154
152
  }
153
+ const root = process.cwd();
154
+ const abs = resolve(root, p);
155
+ const rel = relative2(root, abs);
156
+ if (rel === ".." || rel.startsWith(".." + sep) || isAbsolute(rel)) {
157
+ throw new Error(`Path "${p}" is outside the working directory (${root}). Access denied.`);
158
+ }
159
+ return abs;
155
160
  }
156
- function saveConfig(config) {
157
- mkdirSync(CONFIG_DIR, { recursive: true });
158
- writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
159
- }
160
- function setModel(model) {
161
- saveConfig({ ...loadConfig(), model });
162
- }
163
- function setEffort(effort) {
164
- saveConfig({ ...loadConfig(), effort });
161
+ var init_paths = __esm({
162
+ "src/tools/paths.ts"() {
163
+ "use strict";
164
+ }
165
+ });
166
+
167
+ // src/tools/edit_file.ts
168
+ import { readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
169
+ var edit_file;
170
+ var init_edit_file = __esm({
171
+ "src/tools/edit_file.ts"() {
172
+ "use strict";
173
+ init_paths();
174
+ edit_file = {
175
+ name: "edit_file",
176
+ description: "Replace an exact string in a file. old_str must be unique.",
177
+ input_schema: {
178
+ type: "object",
179
+ properties: {
180
+ path: { type: "string", description: "File path" },
181
+ old_str: { type: "string", description: "Exact text to replace" },
182
+ new_str: { type: "string", description: "Replacement text" }
183
+ },
184
+ required: ["path", "old_str", "new_str"]
185
+ },
186
+ handler: ({ path, old_str, new_str }) => {
187
+ try {
188
+ const abs = confinePath(path);
189
+ const src = readFileSync3(abs, "utf-8");
190
+ const first = src.indexOf(old_str);
191
+ if (first === -1) {
192
+ return { content: `old_str not found in ${path}`, is_error: true };
193
+ }
194
+ if (src.indexOf(old_str, first + 1) !== -1) {
195
+ return { content: `old_str not unique in ${path}`, is_error: true };
196
+ }
197
+ writeFileSync3(abs, src.slice(0, first) + new_str + src.slice(first + old_str.length), "utf-8");
198
+ return { content: `Edited ${path}` };
199
+ } catch (err) {
200
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
201
+ }
202
+ }
203
+ };
204
+ }
205
+ });
206
+
207
+ // src/tools/read_file.ts
208
+ import { readFileSync as readFileSync4 } from "fs";
209
+ var read_file;
210
+ var init_read_file = __esm({
211
+ "src/tools/read_file.ts"() {
212
+ "use strict";
213
+ init_paths();
214
+ read_file = {
215
+ name: "read_file",
216
+ description: "Read entire file contents as UTF-8 text.",
217
+ input_schema: {
218
+ type: "object",
219
+ properties: {
220
+ path: { type: "string", description: "File path" }
221
+ },
222
+ required: ["path"]
223
+ },
224
+ handler: ({ path }) => {
225
+ try {
226
+ const MAX = 2e5;
227
+ const raw = readFileSync4(confinePath(path), "utf-8");
228
+ const truncated = raw.length > MAX;
229
+ const body = truncated ? raw.slice(0, MAX) + `
230
+ [truncated: ${raw.length - MAX} more chars]` : raw;
231
+ return { content: body };
232
+ } catch (err) {
233
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
234
+ }
235
+ }
236
+ };
237
+ }
238
+ });
239
+
240
+ // src/tools/write_file.ts
241
+ import { writeFileSync as writeFileSync4, mkdirSync as mkdirSync3 } from "fs";
242
+ import { dirname } from "path";
243
+ var write_file;
244
+ var init_write_file = __esm({
245
+ "src/tools/write_file.ts"() {
246
+ "use strict";
247
+ init_paths();
248
+ write_file = {
249
+ name: "write_file",
250
+ description: "Create or overwrite a file with the given content. Parent dirs auto-created.",
251
+ input_schema: {
252
+ type: "object",
253
+ properties: {
254
+ path: { type: "string", description: "File path" },
255
+ content: { type: "string", description: "Full file content" }
256
+ },
257
+ required: ["path", "content"]
258
+ },
259
+ handler: ({ path, content }) => {
260
+ try {
261
+ const abs = confinePath(path);
262
+ mkdirSync3(dirname(abs), { recursive: true });
263
+ writeFileSync4(abs, content, "utf-8");
264
+ return { content: `Wrote ${path} (${content.length} bytes)` };
265
+ } catch (err) {
266
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
267
+ }
268
+ }
269
+ };
270
+ }
271
+ });
272
+
273
+ // src/tools/run_bash.ts
274
+ import { execa } from "execa";
275
+ var run_bash;
276
+ var init_run_bash = __esm({
277
+ "src/tools/run_bash.ts"() {
278
+ "use strict";
279
+ run_bash = {
280
+ name: "run_bash",
281
+ description: "Execute a shell command (bash on Unix, cmd on Windows). Returns stdout+stderr. Non-interactive only.",
282
+ input_schema: {
283
+ type: "object",
284
+ properties: {
285
+ command: { type: "string", description: "Shell command to run" },
286
+ timeout_ms: { type: "number", description: "Timeout in ms (default 30000)" }
287
+ },
288
+ required: ["command"]
289
+ },
290
+ handler: async ({ command, timeout_ms }) => {
291
+ try {
292
+ const isWin = process.platform === "win32";
293
+ const shell = isWin ? "cmd" : "bash";
294
+ const shellArgs = isWin ? ["/c", command] : ["-c", command];
295
+ const { stdout, stderr, exitCode } = await execa(shell, shellArgs, {
296
+ timeout: timeout_ms ?? 3e4,
297
+ reject: false,
298
+ all: false
299
+ });
300
+ const out = [stdout, stderr].filter(Boolean).join("\n");
301
+ const is_error = exitCode !== 0;
302
+ const body = out || (is_error ? `(no output)` : "");
303
+ const content = is_error ? `${body}
304
+ [exit ${exitCode}]` : body;
305
+ return {
306
+ content: content.slice(0, 32e3),
307
+ is_error
308
+ };
309
+ } catch (err) {
310
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
311
+ }
312
+ }
313
+ };
314
+ }
315
+ });
316
+
317
+ // src/tools/grep.ts
318
+ import { execa as execa2 } from "execa";
319
+ var grep;
320
+ var init_grep = __esm({
321
+ "src/tools/grep.ts"() {
322
+ "use strict";
323
+ grep = {
324
+ name: "grep",
325
+ description: "Search file contents for a regex pattern. Uses ripgrep if available, falls back to grep -R.",
326
+ input_schema: {
327
+ type: "object",
328
+ properties: {
329
+ pattern: { type: "string", description: "Regex pattern" },
330
+ path: { type: "string", description: "Root path to search (default cwd)" },
331
+ glob: { type: "string", description: 'File glob filter, e.g. "*.ts"' },
332
+ case_insensitive: { type: "string", description: 'Set "true" for case-insensitive' },
333
+ max_results: { type: "number", description: "Max matching lines (default 200)" }
334
+ },
335
+ required: ["pattern"]
336
+ },
337
+ handler: async ({ pattern, path, glob: glob2, case_insensitive, max_results }) => {
338
+ const root = path ?? process.cwd();
339
+ const limit = max_results ?? 200;
340
+ const ci = case_insensitive === true || String(case_insensitive) === "true";
341
+ const tryRg = async () => {
342
+ const args = ["--line-number", "--no-heading", "--color=never", "-m", String(limit)];
343
+ if (ci) args.push("-i");
344
+ if (glob2) args.push("--glob", glob2);
345
+ args.push("--", pattern, root);
346
+ return execa2("rg", args, { reject: false, timeout: 2e4 });
347
+ };
348
+ const tryGrep = async () => {
349
+ const args = ["-R", "-n", "--color=never"];
350
+ if (ci) args.push("-i");
351
+ if (glob2) args.push("--include", glob2);
352
+ args.push("--", pattern, root);
353
+ return execa2("grep", args, { reject: false, timeout: 2e4 });
354
+ };
355
+ try {
356
+ let res;
357
+ try {
358
+ res = await tryRg();
359
+ if (res.exitCode === 127 || (res.stderr ?? "").includes("command not found")) {
360
+ res = await tryGrep();
361
+ }
362
+ } catch {
363
+ res = await tryGrep();
364
+ }
365
+ const lines = (res.stdout ?? "").split("\n").slice(0, limit);
366
+ const out = lines.join("\n");
367
+ const code = res.exitCode ?? 0;
368
+ if (!out && code === 1) return { content: "No matches." };
369
+ return { content: out || res.stderr || "No matches.", is_error: code > 1 };
370
+ } catch (err) {
371
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
372
+ }
373
+ }
374
+ };
375
+ }
376
+ });
377
+
378
+ // src/tools/glob.ts
379
+ import { execa as execa3 } from "execa";
380
+ function globToFindName(glob2) {
381
+ return glob2;
165
382
  }
383
+ var glob;
384
+ var init_glob = __esm({
385
+ "src/tools/glob.ts"() {
386
+ "use strict";
387
+ glob = {
388
+ name: "glob",
389
+ description: 'List files matching a glob pattern (e.g. "**/*.ts"). Uses ripgrep --files if available.',
390
+ input_schema: {
391
+ type: "object",
392
+ properties: {
393
+ pattern: { type: "string", description: 'Glob pattern, e.g. "**/*.ts"' },
394
+ path: { type: "string", description: "Root path (default cwd)" },
395
+ max_results: { type: "number", description: "Max paths returned (default 500)" }
396
+ },
397
+ required: ["pattern"]
398
+ },
399
+ handler: async ({ pattern, path, max_results }) => {
400
+ const root = path ?? process.cwd();
401
+ const limit = max_results ?? 500;
402
+ const tryRg = () => execa3("rg", ["--files", "--hidden", "--glob", pattern, root], {
403
+ reject: false,
404
+ timeout: 2e4
405
+ });
406
+ const tryFind = () => {
407
+ const name = globToFindName(pattern.replace(/^\*\*\//, ""));
408
+ return execa3("find", [root, "-type", "f", "-name", name], {
409
+ reject: false,
410
+ timeout: 2e4
411
+ });
412
+ };
413
+ try {
414
+ let res;
415
+ try {
416
+ res = await tryRg();
417
+ if (res.exitCode === 127 || (res.stderr ?? "").includes("command not found")) {
418
+ res = await tryFind();
419
+ }
420
+ } catch {
421
+ res = await tryFind();
422
+ }
423
+ const lines = (res.stdout ?? "").split("\n").filter(Boolean).slice(0, limit);
424
+ if (lines.length === 0) return { content: "No files matched." };
425
+ return { content: lines.join("\n") };
426
+ } catch (err) {
427
+ return { content: err instanceof Error ? err.message : String(err), is_error: true };
428
+ }
429
+ }
430
+ };
431
+ }
432
+ });
166
433
 
167
- // src/ui/WelcomeBlock.tsx
168
- import { Box, Text } from "ink";
169
- import { jsx, jsxs } from "react/jsx-runtime";
170
- function WelcomeBlock({ model, activeCtx, effort, cwd }) {
171
- const ctxLabel = activeCtx != null ? `${Math.round(activeCtx / 1024)}k ctx` : "\u2014 ctx";
172
- return /* @__PURE__ */ jsxs(
173
- Box,
174
- {
175
- flexDirection: "column",
176
- borderStyle: "round",
177
- borderColor: "gray",
178
- paddingX: 2,
179
- marginBottom: 1,
180
- children: [
181
- /* @__PURE__ */ jsxs(Box, { gap: 2, children: [
182
- /* @__PURE__ */ jsx(Text, { bold: true, color: "blue", children: "MIII CLI" }),
183
- /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
184
- /* @__PURE__ */ jsx(Text, { children: model ?? "/models" }),
185
- /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
186
- /* @__PURE__ */ jsx(Text, { children: ctxLabel }),
187
- /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
188
- /* @__PURE__ */ jsxs(Text, { children: [
189
- effort,
190
- " effort"
191
- ] })
192
- ] }),
193
- /* @__PURE__ */ jsx(Text, { dimColor: true, children: cwd })
194
- ]
434
+ // src/tools/registry.ts
435
+ function getTool(name) {
436
+ return TOOLS.find((t) => t.name === name);
437
+ }
438
+ function toOllamaTools(tools = TOOLS) {
439
+ return tools.map((t) => ({
440
+ type: "function",
441
+ function: {
442
+ name: t.name,
443
+ description: t.description,
444
+ parameters: {
445
+ type: "object",
446
+ properties: t.input_schema.properties,
447
+ required: t.input_schema.required
448
+ }
195
449
  }
196
- );
450
+ }));
197
451
  }
452
+ var TOOLS;
453
+ var init_registry = __esm({
454
+ "src/tools/registry.ts"() {
455
+ "use strict";
456
+ init_edit_file();
457
+ init_read_file();
458
+ init_write_file();
459
+ init_run_bash();
460
+ init_grep();
461
+ init_glob();
462
+ TOOLS = [
463
+ edit_file,
464
+ read_file,
465
+ write_file,
466
+ run_bash,
467
+ grep,
468
+ glob
469
+ ];
470
+ }
471
+ });
198
472
 
199
- // src/ui/ModelList.tsx
200
- import { Box as Box2, Text as Text2 } from "ink";
201
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
202
- function ModelList({ models, cursor, activeModel, showActive }) {
203
- if (models.length === 0) {
204
- return /* @__PURE__ */ jsxs2(Text2, { dimColor: true, children: [
205
- "no models found. run: ollama pull ",
206
- "<model>"
207
- ] });
473
+ // src/tools/validate.ts
474
+ import { z } from "zod";
475
+ function propSchema(spec) {
476
+ if (spec.enum && spec.enum.length) return z.enum(spec.enum);
477
+ switch (spec.type) {
478
+ case "string":
479
+ return z.string();
480
+ case "number":
481
+ return z.number();
482
+ case "integer":
483
+ return z.number().int();
484
+ case "boolean":
485
+ return z.boolean();
486
+ case "array":
487
+ return z.array(z.unknown());
488
+ case "object":
489
+ return z.record(z.unknown());
490
+ default:
491
+ return z.unknown();
208
492
  }
209
- return /* @__PURE__ */ jsx2(Box2, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: models.map((m, i) => /* @__PURE__ */ jsxs2(Text2, { color: i === cursor ? "blue" : void 0, dimColor: i !== cursor, children: [
210
- i === cursor ? "\u276F " : " ",
211
- m,
212
- showActive && m === activeModel ? /* @__PURE__ */ jsx2(Text2, { dimColor: true, children: " (active)" }) : null
213
- ] }, m)) });
214
493
  }
215
-
216
- // src/ui/InputBar.tsx
217
- import { useEffect, useState } from "react";
218
- import { Box as Box3, Text as Text3 } from "ink";
219
- import { Fragment, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
220
- var SPIN = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
221
- function InputBar({ input, disabled, processingLabel }) {
222
- const [frame, setFrame] = useState(0);
223
- useEffect(() => {
224
- if (!disabled) return;
225
- const t = setInterval(() => setFrame((f) => (f + 1) % SPIN.length), 150);
226
- return () => clearInterval(t);
227
- }, [disabled]);
228
- return /* @__PURE__ */ jsx3(
229
- Box3,
230
- {
231
- borderStyle: "single",
232
- borderTop: true,
233
- borderBottom: true,
234
- borderLeft: false,
235
- borderRight: false,
236
- borderColor: disabled ? "yellow" : "white dim",
237
- paddingX: 1,
238
- children: disabled ? /* @__PURE__ */ jsxs3(Fragment, { children: [
239
- /* @__PURE__ */ jsx3(Text3, { color: "yellow", children: SPIN[frame] + " " }),
240
- /* @__PURE__ */ jsx3(Text3, { dimColor: true, italic: true, children: processingLabel ?? "processing\u2026" }),
241
- /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: " (esc to cancel)" })
242
- ] }) : /* @__PURE__ */ jsxs3(Fragment, { children: [
243
- /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: "> " }),
244
- /* @__PURE__ */ jsx3(Text3, { children: input }),
245
- /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: "\u258C" })
246
- ] })
247
- }
248
- );
494
+ function toZod(schema) {
495
+ const required = new Set(schema.required ?? []);
496
+ const shape = {};
497
+ for (const [key, spec] of Object.entries(schema.properties)) {
498
+ shape[key] = required.has(key) ? propSchema(spec) : z.unknown().optional();
499
+ }
500
+ return z.object(shape).passthrough();
501
+ }
502
+ function validateInput(schema, input) {
503
+ const result = toZod(schema).safeParse(input ?? {});
504
+ if (result.success) return null;
505
+ const issues = result.error.issues.map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
506
+ return `Invalid arguments: ${issues}`;
249
507
  }
508
+ var init_validate = __esm({
509
+ "src/tools/validate.ts"() {
510
+ "use strict";
511
+ }
512
+ });
250
513
 
251
- // src/ui/ModelsView.tsx
252
- import { Box as Box4, Text as Text4 } from "ink";
253
- import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
254
- function ModelsView({ models, cursor, model, ollamaHost, effort }) {
255
- return /* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", marginLeft: 2, children: [
256
- /* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", marginBottom: 1, children: [
257
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "config" }),
258
- /* @__PURE__ */ jsxs4(Box4, { marginTop: 1, flexDirection: "column", children: [
259
- /* @__PURE__ */ jsxs4(Text4, { children: [
260
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "model " }),
261
- /* @__PURE__ */ jsx4(Text4, { children: model ?? "\u2014" })
262
- ] }),
263
- /* @__PURE__ */ jsxs4(Text4, { children: [
264
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "host " }),
265
- /* @__PURE__ */ jsx4(Text4, { children: ollamaHost ?? "http://localhost:11434" })
266
- ] }),
267
- /* @__PURE__ */ jsxs4(Text4, { children: [
268
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "effort " }),
269
- /* @__PURE__ */ jsx4(Text4, { children: effort }),
270
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: " (\u2190 \u2192)" })
271
- ] })
272
- ] })
273
- ] }),
274
- /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "switch model" }),
275
- /* @__PURE__ */ jsx4(Box4, { marginTop: 1, children: /* @__PURE__ */ jsx4(ModelList, { models, cursor, activeModel: model, showActive: true }) }),
276
- /* @__PURE__ */ jsx4(Box4, { marginTop: 1, children: /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "\u2191\u2193 navigate enter switch \u2190\u2192 effort esc close" }) })
277
- ] });
278
- }
514
+ // src/prompt/system.ts
515
+ function buildSystemPrompt(tools, cwd) {
516
+ const toolLines = tools.map((t) => `- ${t.name}: ${t.description}`).join("\n");
517
+ return `You are miii, a senior software engineer running in a terminal.
279
518
 
280
- // src/ui/SessionsView.tsx
281
- import { Box as Box5, Text as Text5 } from "ink";
282
- import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
283
- function relativeTime(iso) {
284
- const diff = Date.now() - new Date(iso).getTime();
285
- const min = Math.floor(diff / 6e4);
286
- if (min < 1) return "just now";
287
- if (min < 60) return `${min}m ago`;
288
- const hr = Math.floor(min / 60);
289
- if (hr < 24) return `${hr}h ago`;
290
- const d = Math.floor(hr / 24);
291
- return `${d}d ago`;
292
- }
293
- function SessionsView({ sessions, cursor }) {
294
- return /* @__PURE__ */ jsxs5(Box5, { flexDirection: "column", marginLeft: 2, children: [
295
- /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "resume session" }),
296
- /* @__PURE__ */ jsx5(Box5, { marginTop: 1, flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: sessions.length === 0 ? /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "no saved sessions yet" }) : sessions.map((s, i) => {
297
- const active = i === cursor;
298
- const label = s.title;
299
- return /* @__PURE__ */ jsxs5(Box5, { gap: 1, children: [
300
- /* @__PURE__ */ jsxs5(Text5, { color: active ? "blue" : void 0, dimColor: !active, children: [
301
- active ? "\u276F " : " ",
302
- label
303
- ] }),
304
- /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: `\xB7 ${s.messageCount} msgs \xB7 ${relativeTime(s.updatedAt)}` })
305
- ] }, s.id);
306
- }) }),
307
- /* @__PURE__ */ jsx5(Box5, { marginTop: 1, children: /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "\u2191\u2193 navigate enter resume d delete esc cancel" }) })
308
- ] });
309
- }
519
+ Working directory: ${cwd}
310
520
 
311
- // src/ui/CommandPalette.tsx
312
- import { Box as Box6, Text as Text6 } from "ink";
313
- import { jsx as jsx6, jsxs as jsxs6 } from "react/jsx-runtime";
314
- var COMMANDS = [
315
- { name: "/models", description: "switch model or adjust effort" },
316
- { name: "/new", description: "save current session and start fresh" },
317
- { name: "/sessions", description: "list sessions and resume one" },
318
- { name: "/clear", description: "clear chat and reset context" },
319
- { name: "/exit", description: "quit miii" }
320
- ];
321
- function CommandPalette({ filter, cursor }) {
322
- const filtered = COMMANDS.filter((c) => c.name.startsWith(filter));
323
- if (filtered.length === 0) return null;
324
- const nameWidth = Math.max(...filtered.map((c) => c.name.length));
325
- return /* @__PURE__ */ jsxs6(
326
- Box6,
327
- {
328
- flexDirection: "column",
329
- borderStyle: "round",
330
- borderColor: "gray",
331
- marginX: 1,
332
- marginBottom: 0,
333
- paddingX: 1,
334
- children: [
335
- filtered.map((cmd, i) => {
336
- const active = i === cursor;
337
- return /* @__PURE__ */ jsxs6(Box6, { gap: 2, children: [
338
- /* @__PURE__ */ jsxs6(Text6, { bold: active, color: active ? "blue" : void 0, dimColor: !active, children: [
339
- active ? "\u276F " : " ",
340
- cmd.name.padEnd(nameWidth)
341
- ] }),
342
- /* @__PURE__ */ jsx6(Text6, { dimColor: true, children: cmd.description })
343
- ] }, cmd.name);
344
- }),
345
- /* @__PURE__ */ jsx6(Box6, { marginTop: 0, children: /* @__PURE__ */ jsx6(Text6, { dimColor: true, children: "\u2191\u2193 navigate tab/enter autocomplete esc dismiss" }) })
346
- ]
347
- }
348
- );
349
- }
350
- function filteredCommands(filter) {
351
- return COMMANDS.filter((c) => c.name.startsWith(filter));
521
+ # Goal Understanding (read this first, every turn)
522
+ Before acting on any request, extract and hold three things:
523
+ GOAL: what the user ultimately wants (outcome, not steps)
524
+ CRITERION: how you will know the goal is met
525
+ GAPS: anything unclear that would force you to guess
526
+
527
+ If GAPS is non-empty, ask the minimum questions needed to fill them \u2014 one message, numbered list \u2014 before touching any file or running any command. Do not guess. Do not act on assumptions.
528
+
529
+ Re-read GOAL before every tool call. If a tool call does not move toward GOAL, skip it.
530
+
531
+ # Attention: re-attend to goal at each step
532
+ After each tool result, answer silently: "Does this result move me toward GOAL?"
533
+ YES \u2192 continue
534
+ NO \u2192 stop, re-derive plan from GOAL, explain the correction in one line
535
+
536
+ This prevents drift. Each step attends to the original goal, not just the previous step.
537
+
538
+ # Output format
539
+ - Always reply in plain text. Never use Markdown syntax: no \`#\` headings, no \`**bold**\`, no \`-\` bullet lists, no fenced \`\`\` code blocks, no inline backticks.
540
+ - Quote code, paths, and identifiers inline as plain text. Do not wrap them.
541
+ - Keep prose terse.
542
+
543
+ # Engineering mindset
544
+ - Treat every request as one of: bug, feature, or fix. Name which one before you start.
545
+ - Apply first principles: decompose unclear tasks into smallest concrete sub-problems, solve each explicitly, compose the result.
546
+ - Never guess. If a fact (file path, function signature, current behavior) is unknown, read or search for it first.
547
+
548
+ # Clarifying questions \u2014 when to ask
549
+ Ask BEFORE acting when:
550
+ - The goal has more than one valid interpretation
551
+ - Success criterion is ambiguous (e.g. "make it better" \u2014 better how?)
552
+ - Required context is missing (which file? which behavior? which user?)
553
+ - Two reasonable approaches have different tradeoffs the user should choose
554
+
555
+ Do NOT ask when:
556
+ - The answer is findable by reading the codebase
557
+ - There is only one sensible interpretation
558
+ - The user has already answered this implicitly
559
+
560
+ Ask in a numbered list. One round of questions per turn. Then wait.
561
+
562
+ # Tool calls
563
+ - When you need a tool, emit the tool call directly. No preamble, no narration, no "I will use X".
564
+ - Never describe a tool call instead of emitting it. If you cannot emit the call, answer in plain text.
565
+ - After a tool result, move directly to the next tool call or the final answer. Do not restate what the previous tool did.
566
+
567
+ # Tools
568
+ You have access to the following tools. Call them via the function-calling interface.
569
+ ${toolLines}
570
+
571
+ # Loop semantics
572
+ - When you need to act on the filesystem or run a command, emit a tool call.
573
+ - After each tool result, decide: more tool calls, or a final plain-text answer.
574
+ - Stop emitting tool calls when GOAL is met. Reply with a concise plain-text final message confirming CRITERION is satisfied.
575
+
576
+ # Rules
577
+ - Always read a file before updating it. Never edit, overwrite, or create-over a file you have not read first this turn.
578
+ - Prefer editing existing files over creating new ones.
579
+ - For edit_file, ensure old_str is unique within the target file.
580
+ - Never invent file paths. Read, glob, or grep before editing.
581
+ - No filler, no pleasantries, no apologies.
582
+
583
+ # Testing and verification
584
+ - Always test the code after a change. Run the project's tests (e.g. npm test, pytest, go test) or the relevant script via run_bash before declaring a task done.
585
+ - If no test exists for the change, run the affected entry point via run_bash to verify it behaves correctly.
586
+ - Treat a green test run or a successful command as the completion signal. If it fails, fix and re-run.
587
+
588
+ # Permissions
589
+ - File tools are confined to the working directory; paths outside it are denied.
590
+ - Each tool call may prompt the user for approval. If they choose "don't ask again", the exact command or path is persisted to ~/.miii/permissions.json and the same call is auto-allowed thereafter.
591
+ `;
352
592
  }
593
+ var init_system = __esm({
594
+ "src/prompt/system.ts"() {
595
+ "use strict";
596
+ }
597
+ });
353
598
 
354
- // src/session/store.ts
355
- import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync2, existsSync as existsSync2, readdirSync, readFileSync as readFileSync2, rmSync } from "fs";
356
- import { join as join2 } from "path";
357
- import { homedir as homedir2 } from "os";
358
- import { randomUUID } from "crypto";
359
- function encodeProjectDir(cwd) {
360
- return cwd.replace(/[/\\]/g, "-").replace(/^-+/, "");
599
+ // src/permissions/policy.ts
600
+ import { readFileSync as readFileSync5, writeFileSync as writeFileSync5, mkdirSync as mkdirSync4, existsSync as existsSync3, renameSync } from "fs";
601
+ import { join as join4 } from "path";
602
+ import { homedir as homedir3 } from "os";
603
+ function loadRules() {
604
+ if (!existsSync3(RULES_PATH)) return [];
605
+ try {
606
+ const data = JSON.parse(readFileSync5(RULES_PATH, "utf-8"));
607
+ return Array.isArray(data.rules) ? data.rules : [];
608
+ } catch {
609
+ return [];
610
+ }
361
611
  }
362
- var SESSION_DIR = join2(homedir2(), ".miii", "projects", encodeProjectDir(process.cwd()), "session");
363
- function newSessionId() {
364
- return randomUUID();
612
+ function saveRules(rules) {
613
+ mkdirSync4(RULES_DIR, { recursive: true });
614
+ const tmp = RULES_PATH + ".tmp";
615
+ writeFileSync5(tmp, JSON.stringify({ rules }, null, 2), "utf-8");
616
+ renameSync(tmp, RULES_PATH);
365
617
  }
366
- function sessionPath(id) {
367
- return join2(SESSION_DIR, `${id}.jsonl`);
618
+ function addRule(tool, pattern) {
619
+ const rules = loadRules();
620
+ if (rules.some((r) => r.tool === tool && r.pattern === pattern)) return;
621
+ rules.push({ tool, pattern });
622
+ saveRules(rules);
368
623
  }
369
- function messageText(m) {
370
- if (typeof m.content === "string") return m.content;
371
- return m.content.map((b) => {
372
- if (b.type === "text") return b.text;
373
- if (b.type === "tool_use") return `[tool ${b.name}]`;
374
- if (b.type === "tool_result") return "[result]";
375
- return "";
376
- }).join(" ");
624
+ function subjectFor(toolName, input) {
625
+ const obj = input ?? {};
626
+ if (toolName === "run_bash") return typeof obj.command === "string" ? obj.command : "";
627
+ if (typeof obj.path === "string") return obj.path;
628
+ return "";
377
629
  }
378
- function firstUserText(messages) {
379
- const first = messages.find((m) => m.role === "user");
380
- if (!first) return "untitled";
381
- return messageText(first).trim().slice(0, 80) || "untitled";
630
+ function globToRegExp(glob2) {
631
+ const escaped = glob2.replace(/[.+^${}()|[\]\\]/g, "\\$&");
632
+ const pattern = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
633
+ return new RegExp(`^${pattern}$`);
382
634
  }
383
- function readMeta(id) {
635
+ function matches(rule, toolName, subject) {
636
+ if (rule.tool !== toolName) return false;
384
637
  try {
385
- const raw = readFileSync2(sessionPath(id), "utf-8");
386
- const firstLine = raw.slice(0, raw.indexOf("\n") === -1 ? raw.length : raw.indexOf("\n"));
387
- const parsed = JSON.parse(firstLine);
388
- if (parsed.type !== "meta") return null;
389
- const { type: _t, ...meta } = parsed;
390
- return meta;
638
+ return globToRegExp(rule.pattern).test(subject);
391
639
  } catch {
392
- return null;
393
- }
394
- }
395
- function persistSession(id, messages, title) {
396
- if (!messages.length) return;
397
- mkdirSync2(SESSION_DIR, { recursive: true });
398
- const existing = readMeta(id);
399
- const now = (/* @__PURE__ */ new Date()).toISOString();
400
- const meta = {
401
- id,
402
- createdAt: existing?.createdAt ?? now,
403
- updatedAt: now,
404
- title: title ?? existing?.title ?? firstUserText(messages),
405
- messageCount: messages.length
406
- };
407
- const lines = [JSON.stringify({ type: "meta", ...meta })];
408
- for (const message of messages) {
409
- lines.push(JSON.stringify({ type: "message", message }));
640
+ return false;
410
641
  }
411
- writeFileSync2(sessionPath(id), lines.join("\n") + "\n", "utf-8");
412
642
  }
413
- function listSessions() {
414
- if (!existsSync2(SESSION_DIR)) return [];
415
- const metas = [];
416
- for (const file of readdirSync(SESSION_DIR)) {
417
- if (!file.endsWith(".jsonl")) continue;
418
- const meta = readMeta(file.replace(/\.jsonl$/, ""));
419
- if (meta) metas.push(meta);
420
- }
421
- return metas.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
643
+ async function check(toolName, input, ctx) {
644
+ const subject = subjectFor(toolName, input);
645
+ const rules = loadRules();
646
+ if (rules.some((r) => matches(r, toolName, subject))) return "allow";
647
+ const answer = await ctx.ask(toolName, input);
648
+ if (answer === "no") return "deny";
649
+ if (answer === "always") addRule(toolName, subject);
650
+ return "allow";
422
651
  }
423
- function deleteSession(id) {
424
- try {
425
- rmSync(sessionPath(id), { force: true });
426
- } catch {
652
+ var RULES_DIR, RULES_PATH;
653
+ var init_policy = __esm({
654
+ "src/permissions/policy.ts"() {
655
+ "use strict";
656
+ RULES_DIR = join4(homedir3(), ".miii");
657
+ RULES_PATH = join4(RULES_DIR, "permissions.json");
427
658
  }
659
+ });
660
+
661
+ // src/agent/adapter.ts
662
+ function mintToolUseId() {
663
+ const rand = Math.random().toString(36).slice(2, 14);
664
+ return `toolu_${rand}`;
428
665
  }
429
- function loadSession(id) {
430
- try {
431
- const raw = readFileSync2(sessionPath(id), "utf-8");
432
- const messages = [];
433
- for (const line of raw.split("\n")) {
434
- if (!line.trim()) continue;
435
- const parsed = JSON.parse(line);
436
- if (parsed.type === "message") messages.push(parsed.message);
666
+ function toOllamaMessages(history, system) {
667
+ const out = [{ role: "system", content: system }];
668
+ for (const msg of history) {
669
+ if (typeof msg.content === "string") {
670
+ out.push({ role: msg.role === "system" ? "system" : msg.role, content: msg.content });
671
+ continue;
672
+ }
673
+ if (msg.role === "assistant") {
674
+ const text = msg.content.filter((b) => b.type === "text").map((b) => b.text).join("");
675
+ const tool_uses = msg.content.filter((b) => b.type === "tool_use");
676
+ const ollamaMsg = { role: "assistant", content: text };
677
+ if (tool_uses.length > 0) {
678
+ ollamaMsg.tool_calls = tool_uses.map((u) => ({
679
+ function: { name: u.name, arguments: u.input }
680
+ }));
681
+ }
682
+ out.push(ollamaMsg);
683
+ continue;
684
+ }
685
+ if (msg.role === "user") {
686
+ const tool_results = msg.content.filter((b) => b.type === "tool_result");
687
+ const texts = msg.content.filter((b) => b.type === "text");
688
+ for (const tr of tool_results) {
689
+ out.push({ role: "tool", content: tr.content });
690
+ }
691
+ if (texts.length > 0) {
692
+ out.push({ role: "user", content: texts.map((t) => t.text).join("") });
693
+ }
437
694
  }
438
- return messages;
439
- } catch {
440
- return [];
441
695
  }
696
+ return out;
442
697
  }
443
- function toDisplayMessages(history) {
444
- const out = [];
445
- for (const m of history) {
446
- if (m.role === "system") continue;
447
- const blocks = Array.isArray(m.content) ? m.content : [{ type: "text", text: m.content }];
448
- if (m.role === "user") {
449
- const text = blocks.filter((b) => b.type === "text").map((b) => b.text).join("");
450
- const results = blocks.filter((b) => b.type === "tool_result");
451
- if (results.length && out.length) {
452
- const last = out[out.length - 1];
453
- last.tool_results = [
454
- ...last.tool_results ?? [],
455
- ...results.map((r) => ({
456
- tool_use_id: r.tool_use_id,
457
- content: r.content,
458
- is_error: r.is_error
459
- }))
460
- ];
698
+ function parseTextToolCalls(text, knownToolNames) {
699
+ if (!text) return { calls: [], cleanedText: text };
700
+ const calls = [];
701
+ let cleaned = text;
702
+ const tagRe = /<\|?tool_call\|?>\s*([\s\S]*?)\s*<\|?\/?tool_call\|?>/g;
703
+ cleaned = cleaned.replace(tagRe, (_m, body) => {
704
+ const c = tryParse(body, knownToolNames);
705
+ if (c) calls.push(c);
706
+ return "";
707
+ });
708
+ const fenceRe = /```(?:json|tool_call)?\s*([\s\S]*?)```/g;
709
+ cleaned = cleaned.replace(fenceRe, (_m, body) => {
710
+ const c = tryParse(body, knownToolNames);
711
+ if (c) {
712
+ calls.push(c);
713
+ return "";
714
+ }
715
+ return _m;
716
+ });
717
+ if (calls.length === 0) {
718
+ const candidate = extractFirstJsonObject(cleaned);
719
+ if (candidate) {
720
+ const c = tryParse(candidate.json, knownToolNames);
721
+ if (c) {
722
+ calls.push(c);
723
+ cleaned = (cleaned.slice(0, candidate.start) + cleaned.slice(candidate.end)).trim();
461
724
  }
462
- if (text.trim()) out.push({ role: "user", content: text });
463
- } else {
464
- const text = blocks.filter((b) => b.type === "text").map((b) => b.text).join("");
465
- const uses = blocks.filter((b) => b.type === "tool_use").map((b) => ({ id: b.id, name: b.name, input: b.input }));
466
- out.push({
467
- role: "assistant",
468
- content: text,
469
- tool_uses: uses.length ? uses : void 0
470
- });
471
725
  }
472
726
  }
473
- return out;
727
+ return { calls, cleanedText: cleaned.trim() };
474
728
  }
475
- async function summarizeMessage(model, text) {
476
- const fallback = text.trim().slice(0, 80) || "untitled";
477
- const prompt = `Summarize this user request as a short title, 3-6 words, no punctuation. Reply with the title only.
478
-
479
- Request:
480
- ${text.slice(0, 2e3)}`;
729
+ function tryParse(raw, knownToolNames) {
730
+ const s = raw.trim();
731
+ if (!s.startsWith("{")) return null;
481
732
  try {
482
- let out = "";
483
- for await (const chunk of chat(
484
- model,
485
- [{ role: "user", content: prompt }],
486
- void 0,
487
- { temperature: 0.2, num_predict: 32 }
488
- )) {
489
- if (chunk.content) out += chunk.content;
490
- }
491
- return out.trim().split("\n").filter(Boolean)[0]?.trim() || fallback;
733
+ const obj = JSON.parse(s);
734
+ const name = typeof obj.name === "string" ? obj.name : void 0;
735
+ const args = obj.arguments ?? obj.parameters ?? obj.input ?? {};
736
+ if (!name || !knownToolNames.includes(name)) return null;
737
+ return { function: { name, arguments: args } };
492
738
  } catch {
493
- return fallback;
739
+ return null;
494
740
  }
495
741
  }
496
-
497
- // src/ui/FilePicker.tsx
498
- import { Box as Box7, Text as Text7 } from "ink";
499
- import { readdirSync as readdirSync2 } from "fs";
500
- import { join as join3, relative } from "path";
501
- import { jsx as jsx7, jsxs as jsxs7 } from "react/jsx-runtime";
502
- var IGNORE = /* @__PURE__ */ new Set(["node_modules", ".git", "dist", "build", ".next", "coverage", ".miii"]);
503
- var MAX_RESULTS = 10;
504
- var MAX_SCAN = 2e3;
505
- var cache = null;
506
- function listFiles(cwd) {
507
- if (cache && cache.cwd === cwd) return cache.files;
508
- const out = [];
509
- const stack = [cwd];
510
- while (stack.length && out.length < MAX_SCAN) {
511
- const dir = stack.pop();
512
- let entries;
513
- try {
514
- entries = readdirSync2(dir, { withFileTypes: true });
515
- } catch {
742
+ function extractFirstJsonObject(s) {
743
+ const start = s.indexOf("{");
744
+ if (start === -1) return null;
745
+ let depth = 0;
746
+ let inStr = false;
747
+ let esc = false;
748
+ for (let i = start; i < s.length; i++) {
749
+ const ch = s[i];
750
+ if (inStr) {
751
+ if (esc) esc = false;
752
+ else if (ch === "\\") esc = true;
753
+ else if (ch === '"') inStr = false;
516
754
  continue;
517
755
  }
518
- for (const e of entries) {
519
- if (IGNORE.has(e.name) || e.name.startsWith(".")) continue;
520
- const full = join3(dir, e.name);
521
- if (e.isDirectory()) stack.push(full);
522
- else if (e.isFile()) out.push(relative(cwd, full));
523
- if (out.length >= MAX_SCAN) break;
756
+ if (ch === '"') {
757
+ inStr = true;
758
+ continue;
759
+ }
760
+ if (ch === "{") depth++;
761
+ else if (ch === "}") {
762
+ depth--;
763
+ if (depth === 0) return { json: s.slice(start, i + 1), start, end: i + 1 };
524
764
  }
525
765
  }
526
- cache = { cwd, files: out };
527
- return out;
528
- }
529
- function parseMention(input) {
530
- const m = input.match(/(?:^|\s)@([^\s]*)$/);
531
- if (!m) return null;
532
- return { query: m[1], start: input.length - m[1].length - 1 };
766
+ return null;
533
767
  }
534
- function searchFiles(cwd, query) {
535
- const files = listFiles(cwd);
536
- const q = query.toLowerCase();
537
- if (!q) return files.slice(0, MAX_RESULTS);
538
- const scored = [];
539
- for (const f of files) {
540
- const lf = f.toLowerCase();
541
- const idx = lf.indexOf(q);
542
- if (idx === -1) continue;
543
- const base = lf.split("/").pop() ?? lf;
544
- const baseIdx = base.indexOf(q);
545
- const score = baseIdx === 0 ? 0 : baseIdx > -1 ? 1 : 2 + idx;
546
- scored.push([score, f]);
547
- if (scored.length > 500) break;
768
+ function blocksFromOllama(text, tool_calls, knownToolNames = []) {
769
+ const blocks = [];
770
+ let finalText = text;
771
+ let finalCalls = tool_calls ?? [];
772
+ if (finalCalls.length === 0 && knownToolNames.length > 0) {
773
+ const parsed = parseTextToolCalls(text, knownToolNames);
774
+ if (parsed.calls.length > 0) {
775
+ finalCalls = parsed.calls;
776
+ finalText = parsed.cleanedText;
777
+ }
548
778
  }
549
- scored.sort((a, b) => a[0] - b[0] || a[1].length - b[1].length);
550
- return scored.slice(0, MAX_RESULTS).map(([, f]) => f);
779
+ if (finalText) blocks.push({ type: "text", text: finalText });
780
+ for (const tc of finalCalls) {
781
+ blocks.push({
782
+ type: "tool_use",
783
+ id: mintToolUseId(),
784
+ name: tc.function.name,
785
+ input: tc.function.arguments ?? {}
786
+ });
787
+ }
788
+ return blocks;
551
789
  }
552
- function FilePicker({ matches: matches2, cursor }) {
553
- if (matches2.length === 0) return null;
554
- return /* @__PURE__ */ jsxs7(
555
- Box7,
556
- {
557
- flexDirection: "column",
558
- borderStyle: "round",
559
- borderColor: "gray",
560
- marginX: 1,
561
- marginBottom: 0,
562
- paddingX: 1,
563
- children: [
564
- matches2.map((f, i) => {
565
- const active = i === cursor;
566
- return /* @__PURE__ */ jsx7(Box7, { children: /* @__PURE__ */ jsxs7(Text7, { bold: active, color: active ? "blue" : void 0, dimColor: !active, children: [
567
- active ? "\u276F " : " ",
568
- f
569
- ] }) }, f);
570
- }),
571
- /* @__PURE__ */ jsx7(Box7, { marginTop: 0, children: /* @__PURE__ */ jsx7(Text7, { dimColor: true, children: "\u2191\u2193 navigate tab insert esc dismiss" }) })
572
- ]
573
- }
574
- );
790
+ var init_adapter = __esm({
791
+ "src/agent/adapter.ts"() {
792
+ "use strict";
793
+ }
794
+ });
795
+
796
+ // src/agent/loop.ts
797
+ async function* runAgent(opts) {
798
+ const { model, cwd, permissions, hooks, signal, num_ctx } = opts;
799
+ const startTime = Date.now();
800
+ const system = buildSystemPrompt(TOOLS, cwd);
801
+ const ollamaTools = toOllamaTools(TOOLS);
802
+ const history = [
803
+ ...opts.history,
804
+ { role: "user", content: opts.userText }
805
+ ];
806
+ let promptTokens = 0;
807
+ let evalTokens = 0;
808
+ let lastAssistantSig = "";
809
+ let repeatCount = 0;
810
+ for (let turn = 0; turn < MAX_TURNS; turn++) {
811
+ let text = "";
812
+ let tool_calls;
813
+ let lastTail = "";
814
+ let tailRepeats = 0;
815
+ let streamLooped = false;
816
+ const ac = new AbortController();
817
+ const composedSignal = signal ? AbortSignal.any ? AbortSignal.any([signal, ac.signal]) : ac.signal : ac.signal;
818
+ if (signal) signal.addEventListener("abort", () => ac.abort(), { once: true });
819
+ try {
820
+ for await (const chunk of chat(model, toOllamaMessages(history, system), ollamaTools, { signal: composedSignal, num_ctx, num_predict: NUM_PREDICT })) {
821
+ if (signal?.aborted) break;
822
+ if (chunk.content) {
823
+ text += chunk.content;
824
+ yield { type: "text-delta", text: chunk.content };
825
+ if (text.length >= REPEAT_TAIL) {
826
+ const tail = text.slice(-REPEAT_TAIL);
827
+ if (tail === lastTail) {
828
+ tailRepeats++;
829
+ if (tailRepeats >= REPEAT_KILL) {
830
+ streamLooped = true;
831
+ ac.abort();
832
+ break;
833
+ }
834
+ } else {
835
+ tailRepeats = 0;
836
+ lastTail = tail;
837
+ }
838
+ }
839
+ }
840
+ if (chunk.thinking) {
841
+ yield { type: "thinking-delta", text: chunk.thinking };
842
+ }
843
+ if (chunk.tool_calls && chunk.tool_calls.length > 0) {
844
+ tool_calls = chunk.tool_calls;
845
+ }
846
+ if (chunk.done) {
847
+ promptTokens += chunk.prompt_eval_count ?? 0;
848
+ evalTokens += chunk.eval_count ?? 0;
849
+ }
850
+ }
851
+ } catch (err) {
852
+ if (streamLooped) {
853
+ yield { type: "error", message: "Model stuck in repetition. Aborted stream. Try a different model or shorten context." };
854
+ return history;
855
+ }
856
+ yield { type: "error", message: err instanceof Error ? err.message : String(err) };
857
+ return history;
858
+ }
859
+ if (streamLooped) {
860
+ yield { type: "error", message: "Model stuck in repetition. Aborted stream. Try a different model or shorten context." };
861
+ return history;
862
+ }
863
+ if (signal?.aborted) {
864
+ yield {
865
+ type: "aborted",
866
+ prompt_tokens: promptTokens,
867
+ eval_tokens: evalTokens,
868
+ duration_ms: Date.now() - startTime
869
+ };
870
+ return history;
871
+ }
872
+ const blocks = blocksFromOllama(text, tool_calls, TOOLS.map((t) => t.name));
873
+ const tool_uses = blocks.filter((b) => b.type === "tool_use");
874
+ history.push({ role: "assistant", content: blocks });
875
+ if (tool_uses.length === 0) {
876
+ yield { type: "turn-end", stop_reason: "end_turn" };
877
+ break;
878
+ }
879
+ const sig = JSON.stringify(
880
+ blocks.map(
881
+ (b) => b.type === "tool_use" ? { t: "u", n: b.name, i: b.input } : b.type === "text" ? { t: "t", x: b.text.trim() } : b
882
+ )
883
+ );
884
+ if (sig === lastAssistantSig) {
885
+ repeatCount++;
886
+ if (repeatCount >= 2) {
887
+ yield { type: "error", message: "Agent loop detected: assistant produced identical output 3 turns in a row" };
888
+ return history;
889
+ }
890
+ } else {
891
+ repeatCount = 0;
892
+ lastAssistantSig = sig;
893
+ }
894
+ for (const u of tool_uses) yield { type: "tool-use", block: u };
895
+ const results = [];
896
+ for (const use of tool_uses) {
897
+ const tool = getTool(use.name);
898
+ if (!tool) {
899
+ const r2 = {
900
+ type: "tool_result",
901
+ tool_use_id: use.id,
902
+ content: `Unknown tool: ${use.name}`,
903
+ is_error: true
904
+ };
905
+ results.push(r2);
906
+ yield { type: "tool-result", block: r2 };
907
+ continue;
908
+ }
909
+ const invalid = validateInput(tool.input_schema, use.input);
910
+ if (invalid) {
911
+ const r2 = {
912
+ type: "tool_result",
913
+ tool_use_id: use.id,
914
+ content: `${invalid} for ${use.name}.`,
915
+ is_error: true
916
+ };
917
+ results.push(r2);
918
+ yield { type: "tool-result", block: r2 };
919
+ continue;
920
+ }
921
+ const decision = await check(use.name, use.input, permissions);
922
+ if (decision === "deny") {
923
+ const r2 = {
924
+ type: "tool_result",
925
+ tool_use_id: use.id,
926
+ content: `Permission denied for ${use.name}.`,
927
+ is_error: true
928
+ };
929
+ results.push(r2);
930
+ yield { type: "permission-denied", toolName: use.name, tool_use_id: use.id };
931
+ yield { type: "tool-result", block: r2 };
932
+ continue;
933
+ }
934
+ try {
935
+ await hooks?.firePre(use);
936
+ } catch {
937
+ }
938
+ let r;
939
+ try {
940
+ const out = await tool.handler(use.input);
941
+ r = {
942
+ type: "tool_result",
943
+ tool_use_id: use.id,
944
+ content: out.content,
945
+ is_error: out.is_error
946
+ };
947
+ } catch (err) {
948
+ r = {
949
+ type: "tool_result",
950
+ tool_use_id: use.id,
951
+ content: err instanceof Error ? err.message : String(err),
952
+ is_error: true
953
+ };
954
+ }
955
+ try {
956
+ await hooks?.firePost(use, r);
957
+ } catch {
958
+ }
959
+ results.push(r);
960
+ yield { type: "tool-result", block: r };
961
+ }
962
+ history.push({ role: "user", content: results });
963
+ yield { type: "turn-end", stop_reason: "tool_use" };
964
+ }
965
+ yield { type: "done", prompt_tokens: promptTokens, eval_tokens: evalTokens };
966
+ return history;
575
967
  }
968
+ var MAX_TURNS, NUM_PREDICT, REPEAT_TAIL, REPEAT_KILL;
969
+ var init_loop = __esm({
970
+ "src/agent/loop.ts"() {
971
+ "use strict";
972
+ init_client();
973
+ init_registry();
974
+ init_validate();
975
+ init_system();
976
+ init_policy();
977
+ init_adapter();
978
+ MAX_TURNS = 25;
979
+ NUM_PREDICT = 4096;
980
+ REPEAT_TAIL = 120;
981
+ REPEAT_KILL = 4;
982
+ }
983
+ });
576
984
 
577
- // src/ui/ChatView.tsx
578
- import { Box as Box9, Text as Text9 } from "ink";
985
+ // eval/runner.ts
986
+ import { mkdtempSync, writeFileSync as writeFileSync6, mkdirSync as mkdirSync5, rmSync as rmSync2 } from "fs";
987
+ import { dirname as dirname2, join as join5 } from "path";
988
+ import { tmpdir } from "os";
989
+ async function runScenario(model, s) {
990
+ const dir = mkdtempSync(join5(tmpdir(), "miii-eval-"));
991
+ const prevCwd = process.cwd();
992
+ for (const [rel, content] of Object.entries(s.files ?? {})) {
993
+ const abs = join5(dir, rel);
994
+ mkdirSync5(dirname2(abs), { recursive: true });
995
+ writeFileSync6(abs, content, "utf-8");
996
+ }
997
+ const r = {
998
+ name: s.name,
999
+ pass: false,
1000
+ toolCalls: 0,
1001
+ promptTokens: 0,
1002
+ evalTokens: 0,
1003
+ durationMs: 0
1004
+ };
1005
+ const start = Date.now();
1006
+ let finalText = "";
1007
+ try {
1008
+ process.chdir(dir);
1009
+ const gen = runAgent({
1010
+ model,
1011
+ cwd: dir,
1012
+ history: [],
1013
+ userText: s.prompt,
1014
+ permissions: autoYes
1015
+ });
1016
+ for await (const ev of gen) {
1017
+ if (ev.type === "tool-use") r.toolCalls++;
1018
+ else if (ev.type === "text-delta") finalText += ev.text;
1019
+ else if (ev.type === "turn-end" && ev.stop_reason === "tool_use") finalText = "";
1020
+ else if (ev.type === "done") {
1021
+ r.promptTokens = ev.prompt_tokens;
1022
+ r.evalTokens = ev.eval_tokens;
1023
+ } else if (ev.type === "error") r.error = ev.message;
1024
+ }
1025
+ } catch (err) {
1026
+ r.error = err instanceof Error ? err.message : String(err);
1027
+ } finally {
1028
+ process.chdir(prevCwd);
1029
+ }
1030
+ r.durationMs = Date.now() - start;
1031
+ if (r.error) {
1032
+ r.reason = `loop error: ${r.error}`;
1033
+ rmSync2(dir, { recursive: true, force: true });
1034
+ return r;
1035
+ }
1036
+ try {
1037
+ const verdict2 = await s.check(dir, finalText.trim());
1038
+ if (verdict2 === true) r.pass = true;
1039
+ else r.reason = typeof verdict2 === "string" ? verdict2 : "check returned false";
1040
+ } catch (err) {
1041
+ r.reason = `check threw: ${err instanceof Error ? err.message : String(err)}`;
1042
+ }
1043
+ rmSync2(dir, { recursive: true, force: true });
1044
+ return r;
1045
+ }
1046
+ var autoYes;
1047
+ var init_runner = __esm({
1048
+ "eval/runner.ts"() {
1049
+ "use strict";
1050
+ init_loop();
1051
+ autoYes = { ask: async () => "yes" };
1052
+ }
1053
+ });
579
1054
 
580
- // src/ui/ThinkingBlock.tsx
581
- import { useState as useState2, useEffect as useEffect2 } from "react";
582
- import { Box as Box8, Text as Text8 } from "ink";
583
- import { jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
584
- var FRAMES = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
585
- var globalThinkingVisible = false;
586
- var listeners = /* @__PURE__ */ new Set();
587
- function toggleThinkingVisible() {
588
- globalThinkingVisible = !globalThinkingVisible;
589
- listeners.forEach((fn) => fn());
590
- }
591
- function useThinkingVisible() {
592
- const [visible, setVisible] = useState2(globalThinkingVisible);
593
- useEffect2(() => {
594
- const handler = () => setVisible(globalThinkingVisible);
595
- listeners.add(handler);
596
- return () => {
597
- listeners.delete(handler);
598
- };
599
- }, []);
600
- return visible;
601
- }
602
- function ThinkingBlock({ content }) {
603
- const [frame, setFrame] = useState2(0);
604
- const visible = useThinkingVisible();
605
- useEffect2(() => {
606
- const t = setInterval(() => setFrame((f) => (f + 1) % FRAMES.length), 80);
607
- return () => clearInterval(t);
608
- }, []);
609
- return /* @__PURE__ */ jsxs8(Box8, { flexDirection: "column", marginLeft: 2, marginBottom: 1, children: [
610
- /* @__PURE__ */ jsxs8(Box8, { children: [
611
- /* @__PURE__ */ jsxs8(Text8, { color: "blue", children: [
612
- FRAMES[frame],
613
- " "
614
- ] }),
615
- /* @__PURE__ */ jsx8(Text8, { dimColor: true, italic: true, children: "thinking\u2026" }),
616
- /* @__PURE__ */ jsxs8(Text8, { dimColor: true, children: [
617
- " \xB7 ctrl+t to ",
618
- visible ? "hide" : "show",
619
- " thoughts"
620
- ] })
621
- ] }),
622
- visible && content ? /* @__PURE__ */ jsx8(Box8, { marginLeft: 2, children: /* @__PURE__ */ jsx8(Text8, { dimColor: true, italic: true, children: content }) }) : null
623
- ] });
624
- }
1055
+ // eval/scenarios.ts
1056
+ import { readFileSync as readFileSync6, existsSync as existsSync4 } from "fs";
1057
+ import { join as join6 } from "path";
1058
+ var read, scenarios;
1059
+ var init_scenarios = __esm({
1060
+ "eval/scenarios.ts"() {
1061
+ "use strict";
1062
+ read = (dir, f) => existsSync4(join6(dir, f)) ? readFileSync6(join6(dir, f), "utf-8") : null;
1063
+ scenarios = [
1064
+ {
1065
+ name: "edit-exact-string",
1066
+ prompt: "In config.js, change the port from 3000 to 8080. Change nothing else.",
1067
+ files: { "config.js": 'export const port = 3000\nexport const host = "localhost"\n' },
1068
+ check: (dir) => {
1069
+ const out = read(dir, "config.js");
1070
+ if (out == null) return "config.js missing";
1071
+ if (!out.includes("8080")) return "port not changed to 8080";
1072
+ if (out.includes("3000")) return "old port 3000 still present";
1073
+ if (!out.includes('host = "localhost"')) return "unrelated line damaged";
1074
+ return true;
1075
+ }
1076
+ },
1077
+ {
1078
+ name: "read-then-answer",
1079
+ prompt: "What is the value of the MAX_RETRIES constant in limits.js? Reply with just the number.",
1080
+ files: { "limits.js": "export const MAX_RETRIES = 7\n" },
1081
+ check: (dir, finalText) => {
1082
+ if (read(dir, "limits.js")?.includes("MAX_RETRIES = 7") !== true)
1083
+ return "agent mutated a read-only task";
1084
+ if (!/\b7\b/.test(finalText)) return `answer missing "7": ${JSON.stringify(finalText)}`;
1085
+ return true;
1086
+ }
1087
+ },
1088
+ {
1089
+ name: "create-new-file",
1090
+ prompt: "Create a file named greeting.txt containing exactly the text: hello world",
1091
+ check: (dir) => {
1092
+ const out = read(dir, "greeting.txt");
1093
+ if (out == null) return "greeting.txt not created";
1094
+ if (out.trim() !== "hello world") return `wrong content: ${JSON.stringify(out)}`;
1095
+ return true;
1096
+ }
1097
+ },
1098
+ {
1099
+ name: "grep-locate",
1100
+ prompt: "Which file defines a function called computeTax? Reply with just the filename.",
1101
+ files: {
1102
+ "a.js": "export function formatDate() {}\n",
1103
+ "b.js": "export function computeTax(x) { return x * 0.1 }\n",
1104
+ "c.js": "export function parseArgs() {}\n"
1105
+ },
1106
+ check: (dir, finalText) => {
1107
+ if (read(dir, "b.js")?.includes("computeTax") !== true) return "b.js damaged";
1108
+ if (!/\bb\.js\b/.test(finalText)) return `answer missing "b.js": ${JSON.stringify(finalText)}`;
1109
+ return true;
1110
+ }
1111
+ }
1112
+ ];
1113
+ }
1114
+ });
625
1115
 
626
- // src/ui/constants.ts
627
- var EMPTY_STATE_HINTS = [
628
- "\u2022 explain @file \u2014 reference a file with @",
629
- "\u2022 /models \u2014 switch model or effort",
630
- "\u2022 /new \u2014 start a new chat",
631
- "\u2022 /sessions \u2014 view saved chats",
632
- "\u2022 ctrl+t \u2014 toggle thinking"
633
- ];
634
- var EMPTY_STATE_TITLE = "Ask anything, or try:";
1116
+ // eval/run.ts
1117
+ var run_exports = {};
1118
+ __export(run_exports, {
1119
+ runEval: () => runEval
1120
+ });
1121
+ function pad(s, n) {
1122
+ return s.length >= n ? s.slice(0, n) : s + " ".repeat(n - s.length);
1123
+ }
1124
+ async function resolveModels(modelsArg) {
1125
+ if (modelsArg !== "all") return modelsArg.split(",").map((m) => m.trim()).filter(Boolean);
1126
+ return (await listModels()).filter((m) => !m.includes("cloud"));
1127
+ }
1128
+ function verdict(passed, total) {
1129
+ const ratio = total === 0 ? 0 : passed / total;
1130
+ if (ratio === 1) return "ready";
1131
+ if (ratio >= 0.5) return "marginal \u2014 some tasks fail";
1132
+ return "not recommended \u2014 weak tool-calling";
1133
+ }
1134
+ async function runModel(model, picked) {
1135
+ console.log(`
1136
+ === ${model} ===`);
1137
+ const results = [];
1138
+ for (const s of picked) {
1139
+ const r = await runScenario(model, s);
1140
+ results.push(r);
1141
+ const mark = r.pass ? "PASS" : "FAIL";
1142
+ const detail = r.pass ? "" : ` ${r.reason ?? r.error ?? ""}`;
1143
+ console.log(
1144
+ `${mark} ${pad(r.name, 22)} ${pad(`${r.toolCalls} calls`, 9)} ${pad(`${r.evalTokens} tok`, 11)} ${pad(`${r.durationMs}ms`, 8)}${detail}`
1145
+ );
1146
+ }
1147
+ const passed = results.filter((r) => r.pass).length;
1148
+ console.log(` \u2192 ${model}: ${passed}/${picked.length} \u2014 ${verdict(passed, picked.length)}`);
1149
+ return results;
1150
+ }
1151
+ function printMatrix(models, picked, grid) {
1152
+ const w = Math.max(...picked.map((s) => s.name.length), 3) + 1;
1153
+ const modelW = Math.max(...models.map((m) => m.length), 5) + 1;
1154
+ console.log("\nMatrix\n");
1155
+ let header = pad("", modelW);
1156
+ for (const s of picked) header += pad(s.name.slice(0, w - 1), w);
1157
+ console.log(header + " score");
1158
+ for (const m of models) {
1159
+ let row = pad(m, modelW);
1160
+ const rs = grid.get(m) ?? [];
1161
+ let passed = 0;
1162
+ for (const s of picked) {
1163
+ const r = rs.find((x) => x.name === s.name);
1164
+ const cell = !r ? "?" : r.pass ? "+" : ".";
1165
+ if (r?.pass) passed++;
1166
+ row += pad(cell, w);
1167
+ }
1168
+ row += ` ${passed}/${picked.length}`;
1169
+ console.log(row);
1170
+ }
1171
+ console.log("\n + pass . fail ? not run");
1172
+ }
1173
+ async function runEval(args) {
1174
+ const strip = (s) => (s ?? "").replace(/^-+/, "");
1175
+ const modelsArg = strip(args[0]) || process.env.MIII_EVAL_MODEL || "all";
1176
+ const filter = strip(args[1]);
1177
+ const picked = filter ? scenarios.filter((s) => s.name.includes(filter)) : scenarios;
1178
+ if (picked.length === 0) {
1179
+ console.error(`No scenarios match "${filter}"`);
1180
+ return 1;
1181
+ }
1182
+ const models = await resolveModels(modelsArg);
1183
+ if (models.length === 0) {
1184
+ console.error("No models to run.");
1185
+ return 1;
1186
+ }
1187
+ console.log(`models: ${models.length} scenarios: ${picked.length}`);
1188
+ const grid = /* @__PURE__ */ new Map();
1189
+ for (const model of models) grid.set(model, await runModel(model, picked));
1190
+ if (models.length > 1) printMatrix(models, picked, grid);
1191
+ const allPass = [...grid.values()].every((rs) => rs.every((r) => r.pass));
1192
+ return allPass ? 0 : 1;
1193
+ }
1194
+ var init_run = __esm({
1195
+ "eval/run.ts"() {
1196
+ "use strict";
1197
+ init_runner();
1198
+ init_scenarios();
1199
+ init_client();
1200
+ }
1201
+ });
635
1202
 
636
- // src/ui/ChatView.tsx
637
- import { jsx as jsx9, jsxs as jsxs9 } from "react/jsx-runtime";
638
- function formatTokens(n) {
639
- if (n >= 1e3) return (n / 1e3).toFixed(n >= 1e4 ? 0 : 1) + "k";
640
- return String(n);
641
- }
642
- function formatDuration(ms) {
643
- const totalSec = ms / 1e3;
644
- if (totalSec < 60) return `${totalSec.toFixed(1)}s`;
645
- const m = Math.floor(totalSec / 60);
646
- const s = Math.round(totalSec - m * 60);
647
- return `${m}m ${s}s`;
1203
+ // src/cli.tsx
1204
+ import { render } from "ink";
1205
+ import { createElement } from "react";
1206
+
1207
+ // src/ui/App.tsx
1208
+ init_client();
1209
+ import { useState as useState4, useEffect as useEffect3 } from "react";
1210
+ import { Box as Box10, Text as Text10, useApp } from "ink";
1211
+ import { homedir as homedir4 } from "os";
1212
+ import { sep as sep2 } from "path";
1213
+
1214
+ // src/config.ts
1215
+ import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
1216
+ import { join } from "path";
1217
+ import { homedir } from "os";
1218
+ var CONFIG_DIR = join(homedir(), ".miii");
1219
+ var CONFIG_PATH = join(CONFIG_DIR, "config.json");
1220
+ function loadConfig() {
1221
+ if (!existsSync(CONFIG_PATH)) return {};
1222
+ try {
1223
+ return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
1224
+ } catch {
1225
+ return {};
1226
+ }
648
1227
  }
649
- function countLines(s) {
650
- if (!s) return 0;
651
- return s.split("\n").length;
1228
+ function saveConfig(config) {
1229
+ mkdirSync(CONFIG_DIR, { recursive: true });
1230
+ writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
652
1231
  }
653
- function FileEditBlock({
654
- label,
655
- path,
656
- added,
657
- removed,
658
- previewLines
659
- }) {
660
- const MAX = 16;
661
- const shown = previewLines.slice(0, MAX);
662
- const extra = previewLines.length - shown.length;
663
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
664
- /* @__PURE__ */ jsxs9(Box9, { children: [
665
- /* @__PURE__ */ jsx9(Text9, { color: "yellow", children: "\u25CF " }),
666
- /* @__PURE__ */ jsxs9(Text9, { color: "yellow", children: [
667
- label,
668
- " "
669
- ] }),
670
- /* @__PURE__ */ jsx9(Text9, { children: "(" }),
671
- /* @__PURE__ */ jsx9(Text9, { bold: true, children: path }),
672
- /* @__PURE__ */ jsx9(Text9, { children: ")" })
673
- ] }),
674
- /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
675
- "\u23BF ",
676
- removed > 0 ? `Added ${added} lines, removed ${removed} lines` : `Added ${added} lines`
677
- ] }) }),
678
- shown.map((ln, i) => /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { color: ln.sign === "+" ? "green" : ln.sign === "-" ? "red" : void 0, dimColor: ln.sign === " ", children: [
679
- ln.sign,
680
- " ",
681
- ln.text
682
- ] }) }, i)),
683
- extra > 0 && /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
684
- "\u2026 ",
685
- extra,
686
- " more lines"
687
- ] }) })
688
- ] });
1232
+ function setModel(model) {
1233
+ saveConfig({ ...loadConfig(), model });
689
1234
  }
690
- var TOOL_LABEL = {
691
- write_file: "Write",
692
- edit_file: "Update",
693
- read_file: "Read",
694
- run_bash: "Bash",
695
- glob: "Glob",
696
- grep: "Grep"
697
- };
698
- function truncate(s, max) {
699
- if (s.length <= max) return s;
700
- return s.slice(0, max - 1) + "\u2026";
1235
+ function setEffort(effort) {
1236
+ saveConfig({ ...loadConfig(), effort });
701
1237
  }
702
- function toolHeader(use) {
703
- const label = TOOL_LABEL[use.name] ?? use.name;
704
- const input = use.input ?? {};
705
- let arg = "";
706
- switch (use.name) {
707
- case "write_file":
708
- case "edit_file":
709
- case "read_file":
710
- arg = String(input.path ?? input.file_path ?? "");
711
- break;
712
- case "run_bash": {
713
- const cmd = String(input.command ?? "").replace(/\s+/g, " ");
714
- arg = truncate(cmd, 120);
715
- break;
716
- }
717
- case "glob":
718
- case "grep":
719
- arg = truncate(String(input.pattern ?? ""), 120);
720
- break;
721
- default: {
722
- arg = truncate(JSON.stringify(input), 80);
1238
+
1239
+ // src/ui/WelcomeBlock.tsx
1240
+ import { Box, Text } from "ink";
1241
+ import { jsx, jsxs } from "react/jsx-runtime";
1242
+ function WelcomeBlock({ model, activeCtx, effort, cwd }) {
1243
+ const ctxLabel = activeCtx != null ? `${Math.round(activeCtx / 1024)}k ctx` : "\u2014 ctx";
1244
+ return /* @__PURE__ */ jsxs(
1245
+ Box,
1246
+ {
1247
+ flexDirection: "column",
1248
+ borderStyle: "round",
1249
+ borderColor: "gray",
1250
+ paddingX: 2,
1251
+ marginBottom: 1,
1252
+ children: [
1253
+ /* @__PURE__ */ jsxs(Box, { gap: 2, children: [
1254
+ /* @__PURE__ */ jsx(Text, { bold: true, color: "blue", children: "MIII CLI" }),
1255
+ /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
1256
+ /* @__PURE__ */ jsx(Text, { children: model ?? "/models" }),
1257
+ /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
1258
+ /* @__PURE__ */ jsx(Text, { children: ctxLabel }),
1259
+ /* @__PURE__ */ jsx(Text, { dimColor: true, children: "\xB7" }),
1260
+ /* @__PURE__ */ jsxs(Text, { children: [
1261
+ effort,
1262
+ " effort"
1263
+ ] })
1264
+ ] }),
1265
+ /* @__PURE__ */ jsx(Text, { dimColor: true, children: cwd })
1266
+ ]
723
1267
  }
724
- }
725
- return { label, arg };
1268
+ );
726
1269
  }
727
- function summarizeResult(res, toolName) {
728
- const content = res.content ?? "";
729
- const lines = content.split("\n");
730
- if (!res.is_error) {
731
- if (toolName === "read_file") {
732
- const total = lines.length;
733
- return `Read ${total} line${total === 1 ? "" : "s"}`;
734
- }
735
- if (toolName === "grep") {
736
- if (content === "No matches.") return "No matches";
737
- const n = lines.filter(Boolean).length;
738
- return `${n} match${n === 1 ? "" : "es"}`;
739
- }
740
- if (toolName === "glob") {
741
- if (content === "No files matched.") return "No files";
742
- const n = lines.filter(Boolean).length;
743
- return `${n} file${n === 1 ? "" : "s"}`;
744
- }
1270
+
1271
+ // src/ui/ModelList.tsx
1272
+ import { Box as Box2, Text as Text2 } from "ink";
1273
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
1274
+ function ModelList({ models, cursor, activeModel, showActive }) {
1275
+ if (models.length === 0) {
1276
+ return /* @__PURE__ */ jsxs2(Text2, { dimColor: true, children: [
1277
+ "no models found. run: ollama pull ",
1278
+ "<model>"
1279
+ ] });
745
1280
  }
746
- const firstNonEmpty = lines.find((l) => l.trim().length > 0) ?? "";
747
- const extra = lines.length - 1;
748
- const head = firstNonEmpty.length > 100 ? firstNonEmpty.slice(0, 97) + "..." : firstNonEmpty;
749
- return extra > 0 ? `${head} (+${extra} lines)` : head;
1281
+ return /* @__PURE__ */ jsx2(Box2, { flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: models.map((m, i) => /* @__PURE__ */ jsxs2(Text2, { color: i === cursor ? "blue" : void 0, dimColor: i !== cursor, children: [
1282
+ i === cursor ? "\u276F " : " ",
1283
+ m,
1284
+ showActive && m === activeModel ? /* @__PURE__ */ jsx2(Text2, { dimColor: true, children: " (active)" }) : null
1285
+ ] }, m)) });
750
1286
  }
751
- function ToolResultBlock({ result, toolName }) {
752
- const content = result.content ?? "";
753
- const lines = content.split("\n");
754
- const showMulti = (toolName === "run_bash" || toolName === "grep" || toolName === "glob" || result.is_error) && lines.length > 1;
755
- if (!showMulti) {
756
- return /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { color: result.is_error ? "red" : void 0, dimColor: !result.is_error, children: [
757
- "\u23BF ",
758
- summarizeResult(result, toolName)
759
- ] }) });
760
- }
761
- const MAX_LINES = 10;
762
- const MAX_LINE_WIDTH = 200;
763
- const shown = lines.slice(0, MAX_LINES).map((l) => truncate(l, MAX_LINE_WIDTH));
764
- const extra = lines.length - shown.length;
765
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
766
- /* @__PURE__ */ jsxs9(Text9, { color: result.is_error ? "red" : void 0, dimColor: !result.is_error, children: [
767
- "\u23BF ",
768
- summarizeResult(result, toolName)
769
- ] }),
770
- shown.map((ln, i) => /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsx9(Text9, { color: result.is_error ? "red" : void 0, dimColor: true, children: ln || " " }) }, i)),
771
- extra > 0 && /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
772
- "\u2026 ",
773
- extra,
774
- " more lines"
775
- ] }) })
776
- ] });
1287
+
1288
+ // src/ui/InputBar.tsx
1289
+ import { useEffect, useState } from "react";
1290
+ import { Box as Box3, Text as Text3 } from "ink";
1291
+ import { Fragment, jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
1292
+ var SPIN = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
1293
+ function InputBar({ input, disabled, processingLabel }) {
1294
+ const [frame, setFrame] = useState(0);
1295
+ useEffect(() => {
1296
+ if (!disabled) return;
1297
+ const t = setInterval(() => setFrame((f) => (f + 1) % SPIN.length), 150);
1298
+ return () => clearInterval(t);
1299
+ }, [disabled]);
1300
+ return /* @__PURE__ */ jsx3(
1301
+ Box3,
1302
+ {
1303
+ borderStyle: "single",
1304
+ borderTop: true,
1305
+ borderBottom: true,
1306
+ borderLeft: false,
1307
+ borderRight: false,
1308
+ borderColor: disabled ? "yellow" : "white dim",
1309
+ paddingX: 1,
1310
+ children: disabled ? /* @__PURE__ */ jsxs3(Fragment, { children: [
1311
+ /* @__PURE__ */ jsx3(Text3, { color: "yellow", children: SPIN[frame] + " " }),
1312
+ /* @__PURE__ */ jsx3(Text3, { dimColor: true, italic: true, children: processingLabel ?? "processing\u2026" }),
1313
+ /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: " (esc to cancel)" })
1314
+ ] }) : /* @__PURE__ */ jsxs3(Fragment, { children: [
1315
+ /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: "> " }),
1316
+ /* @__PURE__ */ jsx3(Text3, { children: input }),
1317
+ /* @__PURE__ */ jsx3(Text3, { dimColor: true, children: "\u258C" })
1318
+ ] })
1319
+ }
1320
+ );
777
1321
  }
778
- function ToolUseLine({ use, result }) {
779
- if (use.name === "write_file" && !result?.is_error) {
780
- const input = use.input;
781
- const content = input.content ?? "";
782
- const added = countLines(content);
783
- const preview = content.split("\n").map((t) => ({ sign: "+", text: t }));
784
- return /* @__PURE__ */ jsx9(FileEditBlock, { label: "Write", path: input.path ?? "", added, removed: 0, previewLines: preview });
785
- }
786
- if (use.name === "edit_file" && !result?.is_error) {
787
- const input = use.input;
788
- const oldS = input.old_str ?? "";
789
- const newS = input.new_str ?? "";
790
- const added = countLines(newS);
791
- const removed = countLines(oldS);
792
- const preview = [
793
- ...oldS.split("\n").map((t) => ({ sign: "-", text: t })),
794
- ...newS.split("\n").map((t) => ({ sign: "+", text: t }))
795
- ];
796
- return /* @__PURE__ */ jsx9(FileEditBlock, { label: "Update", path: input.path ?? "", added, removed, previewLines: preview });
797
- }
798
- const { label, arg } = toolHeader(use);
799
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
800
- /* @__PURE__ */ jsxs9(Box9, { children: [
801
- /* @__PURE__ */ jsx9(Text9, { color: "yellow", children: "\u25CF " }),
802
- /* @__PURE__ */ jsxs9(Text9, { color: "yellow", children: [
803
- label,
804
- " "
805
- ] }),
806
- /* @__PURE__ */ jsx9(Text9, { children: "(" }),
807
- /* @__PURE__ */ jsx9(Text9, { bold: true, children: arg }),
808
- /* @__PURE__ */ jsx9(Text9, { children: ")" })
1322
+
1323
+ // src/ui/ModelsView.tsx
1324
+ import { Box as Box4, Text as Text4 } from "ink";
1325
+ import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
1326
+ function ModelsView({ models, cursor, model, ollamaHost, effort }) {
1327
+ return /* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", marginLeft: 2, children: [
1328
+ /* @__PURE__ */ jsxs4(Box4, { flexDirection: "column", marginBottom: 1, children: [
1329
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "config" }),
1330
+ /* @__PURE__ */ jsxs4(Box4, { marginTop: 1, flexDirection: "column", children: [
1331
+ /* @__PURE__ */ jsxs4(Text4, { children: [
1332
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "model " }),
1333
+ /* @__PURE__ */ jsx4(Text4, { children: model ?? "\u2014" })
1334
+ ] }),
1335
+ /* @__PURE__ */ jsxs4(Text4, { children: [
1336
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "host " }),
1337
+ /* @__PURE__ */ jsx4(Text4, { children: ollamaHost ?? "http://localhost:11434" })
1338
+ ] }),
1339
+ /* @__PURE__ */ jsxs4(Text4, { children: [
1340
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "effort " }),
1341
+ /* @__PURE__ */ jsx4(Text4, { children: effort }),
1342
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: " (\u2190 \u2192)" })
1343
+ ] })
1344
+ ] })
809
1345
  ] }),
810
- result && /* @__PURE__ */ jsx9(ToolResultBlock, { result, toolName: use.name })
1346
+ /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "switch model" }),
1347
+ /* @__PURE__ */ jsx4(Box4, { marginTop: 1, children: /* @__PURE__ */ jsx4(ModelList, { models, cursor, activeModel: model, showActive: true }) }),
1348
+ /* @__PURE__ */ jsx4(Box4, { marginTop: 1, children: /* @__PURE__ */ jsx4(Text4, { dimColor: true, children: "\u2191\u2193 navigate enter switch \u2190\u2192 effort esc close" }) })
811
1349
  ] });
812
1350
  }
813
- function AssistantMessage({ msg }) {
814
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, children: [
815
- msg.content && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", children: [
816
- /* @__PURE__ */ jsx9(Text9, { color: "white", children: "\u25CF " }),
817
- /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: msg.content }) })
818
- ] }),
819
- msg.tool_uses?.map((u) => {
820
- const r = msg.tool_results?.find((x) => x.tool_use_id === u.id);
821
- return /* @__PURE__ */ jsx9(ToolUseLine, { use: u, result: r }, u.id);
822
- }),
823
- msg.tokens && /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
824
- `\u21B3 Completed \xB7 ${formatTokens(msg.tokens.prompt_eval + msg.tokens.eval)} tokens`,
825
- msg.duration != null ? ` \xB7 ${formatDuration(msg.duration)}` : ""
826
- ] }) })
1351
+
1352
+ // src/ui/SessionsView.tsx
1353
+ import { Box as Box5, Text as Text5 } from "ink";
1354
+ import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
1355
+ function relativeTime(iso) {
1356
+ const diff = Date.now() - new Date(iso).getTime();
1357
+ const min = Math.floor(diff / 6e4);
1358
+ if (min < 1) return "just now";
1359
+ if (min < 60) return `${min}m ago`;
1360
+ const hr = Math.floor(min / 60);
1361
+ if (hr < 24) return `${hr}h ago`;
1362
+ const d = Math.floor(hr / 24);
1363
+ return `${d}d ago`;
1364
+ }
1365
+ function SessionsView({ sessions, cursor }) {
1366
+ return /* @__PURE__ */ jsxs5(Box5, { flexDirection: "column", marginLeft: 2, children: [
1367
+ /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "resume session" }),
1368
+ /* @__PURE__ */ jsx5(Box5, { marginTop: 1, flexDirection: "column", borderStyle: "round", borderColor: "gray", paddingX: 1, children: sessions.length === 0 ? /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "no saved sessions yet" }) : sessions.map((s, i) => {
1369
+ const active = i === cursor;
1370
+ const label = s.title;
1371
+ return /* @__PURE__ */ jsxs5(Box5, { gap: 1, children: [
1372
+ /* @__PURE__ */ jsxs5(Text5, { color: active ? "blue" : void 0, dimColor: !active, children: [
1373
+ active ? "\u276F " : " ",
1374
+ label
1375
+ ] }),
1376
+ /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: `\xB7 ${s.messageCount} msgs \xB7 ${relativeTime(s.updatedAt)}` })
1377
+ ] }, s.id);
1378
+ }) }),
1379
+ /* @__PURE__ */ jsx5(Box5, { marginTop: 1, children: /* @__PURE__ */ jsx5(Text5, { dimColor: true, children: "\u2191\u2193 navigate enter resume d delete esc cancel" }) })
827
1380
  ] });
828
1381
  }
829
- function summarizeInput(input) {
830
- if (!input || typeof input !== "object") return "";
831
- const obj = input;
832
- const priority = ["path", "file_path", "command", "pattern", "query"];
833
- for (const k of priority) {
834
- const v = obj[k];
835
- if (typeof v === "string" && v.length > 0) return `${k}: ${v}`;
836
- }
837
- const first = Object.entries(obj).find(([, v]) => typeof v === "string");
838
- if (first) {
839
- const [k, v] = first;
840
- const trimmed = v.length > 80 ? v.slice(0, 80) + "\u2026" : v;
841
- return `${k}: ${trimmed}`;
842
- }
843
- return "";
1382
+
1383
+ // src/ui/CommandPalette.tsx
1384
+ import { Box as Box6, Text as Text6 } from "ink";
1385
+ import { jsx as jsx6, jsxs as jsxs6 } from "react/jsx-runtime";
1386
+ var COMMANDS = [
1387
+ { name: "/models", description: "switch model or adjust effort" },
1388
+ { name: "/new", description: "save current session and start fresh" },
1389
+ { name: "/sessions", description: "list sessions and resume one" },
1390
+ { name: "/clear", description: "clear chat and reset context" },
1391
+ { name: "/exit", description: "quit miii" }
1392
+ ];
1393
+ function CommandPalette({ filter, cursor }) {
1394
+ const filtered = COMMANDS.filter((c) => c.name.startsWith(filter));
1395
+ if (filtered.length === 0) return null;
1396
+ const nameWidth = Math.max(...filtered.map((c) => c.name.length));
1397
+ return /* @__PURE__ */ jsxs6(
1398
+ Box6,
1399
+ {
1400
+ flexDirection: "column",
1401
+ borderStyle: "round",
1402
+ borderColor: "gray",
1403
+ marginX: 1,
1404
+ marginBottom: 0,
1405
+ paddingX: 1,
1406
+ children: [
1407
+ filtered.map((cmd2, i) => {
1408
+ const active = i === cursor;
1409
+ return /* @__PURE__ */ jsxs6(Box6, { gap: 2, children: [
1410
+ /* @__PURE__ */ jsxs6(Text6, { bold: active, color: active ? "blue" : void 0, dimColor: !active, children: [
1411
+ active ? "\u276F " : " ",
1412
+ cmd2.name.padEnd(nameWidth)
1413
+ ] }),
1414
+ /* @__PURE__ */ jsx6(Text6, { dimColor: true, children: cmd2.description })
1415
+ ] }, cmd2.name);
1416
+ }),
1417
+ /* @__PURE__ */ jsx6(Box6, { marginTop: 0, children: /* @__PURE__ */ jsx6(Text6, { dimColor: true, children: "\u2191\u2193 navigate tab/enter autocomplete esc dismiss" }) })
1418
+ ]
1419
+ }
1420
+ );
844
1421
  }
845
- function PermissionPrompt({ req, cursor }) {
846
- const label = TOOL_LABEL[req.toolName] ?? req.toolName;
847
- const options = [
848
- { label: "Yes", key: "yes" },
849
- { label: "Yes, don't ask again for this", key: "always" },
850
- { label: "No", key: "no" }
851
- ];
852
- const summary = summarizeInput(req.input);
853
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, borderStyle: "round", borderColor: "blue", paddingX: 1, children: [
854
- /* @__PURE__ */ jsx9(Text9, { color: "blue", bold: true, children: "Tool use" }),
855
- /* @__PURE__ */ jsx9(Box9, { marginTop: 1, children: /* @__PURE__ */ jsxs9(Text9, { children: [
856
- "Allow ",
857
- /* @__PURE__ */ jsx9(Text9, { bold: true, children: label }),
858
- "?"
859
- ] }) }),
860
- summary && /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsx9(Text9, { dimColor: true, children: summary }) }),
861
- /* @__PURE__ */ jsx9(Box9, { flexDirection: "column", marginTop: 1, children: options.map((opt, i) => /* @__PURE__ */ jsxs9(Text9, { color: i === cursor ? "blue" : void 0, children: [
862
- i === cursor ? "\u276F " : " ",
863
- i + 1,
864
- ". ",
865
- opt.label
866
- ] }, opt.key)) })
867
- ] });
1422
+ function filteredCommands(filter) {
1423
+ return COMMANDS.filter((c) => c.name.startsWith(filter));
868
1424
  }
869
- function ChatView({
870
- messages,
871
- streaming,
872
- streamingContent,
873
- thinking,
874
- thinkingContent,
875
- error,
876
- pendingPermission,
877
- permissionCursor = 0,
878
- activeToolUses,
879
- activeToolResults
880
- }) {
881
- const empty = messages.length === 0 && !streaming && !thinking && !pendingPermission && !error;
882
- return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 1, marginBottom: 1, children: [
883
- empty && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, children: [
884
- /* @__PURE__ */ jsx9(Text9, { dimColor: true, children: EMPTY_STATE_TITLE }),
885
- EMPTY_STATE_HINTS.map((h, i) => /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
886
- " ",
887
- h
888
- ] }, i))
889
- ] }),
890
- messages.map(
891
- (msg, i) => msg.role === "user" ? /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
892
- /* @__PURE__ */ jsx9(Text9, { color: "blue", children: "\u25CF " }),
893
- /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: msg.content }) })
894
- ] }, i) : /* @__PURE__ */ jsx9(AssistantMessage, { msg }, i)
895
- ),
896
- thinking && /* @__PURE__ */ jsx9(ThinkingBlock, { content: thinkingContent }),
897
- streaming && streamingContent && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
898
- /* @__PURE__ */ jsx9(Text9, { color: "white", children: "\u25CF " }),
899
- /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: streamingContent }) })
900
- ] }),
901
- activeToolUses?.map((u) => {
902
- const r = activeToolResults?.find((x) => x.tool_use_id === u.id);
903
- return /* @__PURE__ */ jsx9(ToolUseLine, { use: u, result: r }, u.id);
904
- }),
905
- pendingPermission && /* @__PURE__ */ jsx9(PermissionPrompt, { req: pendingPermission, cursor: permissionCursor }),
906
- error && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
907
- /* @__PURE__ */ jsx9(Text9, { color: "red", children: "\u25CF " }),
908
- /* @__PURE__ */ jsx9(Text9, { color: "red", children: error })
909
- ] })
910
- ] });
1425
+
1426
+ // src/session/store.ts
1427
+ init_client();
1428
+ import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync2, existsSync as existsSync2, readdirSync, readFileSync as readFileSync2, rmSync } from "fs";
1429
+ import { join as join2 } from "path";
1430
+ import { homedir as homedir2 } from "os";
1431
+ import { randomUUID } from "crypto";
1432
+ function encodeProjectDir(cwd) {
1433
+ return cwd.replace(/[/\\]/g, "-").replace(/^-+/, "");
1434
+ }
1435
+ var SESSION_DIR = join2(homedir2(), ".miii", "projects", encodeProjectDir(process.cwd()), "session");
1436
+ function newSessionId() {
1437
+ return randomUUID();
1438
+ }
1439
+ function sessionPath(id) {
1440
+ return join2(SESSION_DIR, `${id}.jsonl`);
1441
+ }
1442
+ function messageText(m) {
1443
+ if (typeof m.content === "string") return m.content;
1444
+ return m.content.map((b) => {
1445
+ if (b.type === "text") return b.text;
1446
+ if (b.type === "tool_use") return `[tool ${b.name}]`;
1447
+ if (b.type === "tool_result") return "[result]";
1448
+ return "";
1449
+ }).join(" ");
911
1450
  }
912
-
913
- // src/ui/hooks/useAgentRunner.ts
914
- import { useState as useState3, useRef } from "react";
915
-
916
- // src/tools/edit_file.ts
917
- import { readFileSync as readFileSync3, writeFileSync as writeFileSync3 } from "fs";
918
-
919
- // src/tools/paths.ts
920
- import { resolve, relative as relative2, isAbsolute, sep } from "path";
921
- function confinePath(p) {
922
- if (typeof p !== "string" || p.length === 0) {
923
- throw new Error("Path is required.");
1451
+ function firstUserText(messages) {
1452
+ const first = messages.find((m) => m.role === "user");
1453
+ if (!first) return "untitled";
1454
+ return messageText(first).trim().slice(0, 80) || "untitled";
1455
+ }
1456
+ function readMeta(id) {
1457
+ try {
1458
+ const raw = readFileSync2(sessionPath(id), "utf-8");
1459
+ const firstLine = raw.slice(0, raw.indexOf("\n") === -1 ? raw.length : raw.indexOf("\n"));
1460
+ const parsed = JSON.parse(firstLine);
1461
+ if (parsed.type !== "meta") return null;
1462
+ const { type: _t, ...meta } = parsed;
1463
+ return meta;
1464
+ } catch {
1465
+ return null;
924
1466
  }
925
- const root = process.cwd();
926
- const abs = resolve(root, p);
927
- const rel = relative2(root, abs);
928
- if (rel === ".." || rel.startsWith(".." + sep) || isAbsolute(rel)) {
929
- throw new Error(`Path "${p}" is outside the working directory (${root}). Access denied.`);
1467
+ }
1468
+ function persistSession(id, messages, title) {
1469
+ if (!messages.length) return;
1470
+ mkdirSync2(SESSION_DIR, { recursive: true });
1471
+ const existing = readMeta(id);
1472
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1473
+ const meta = {
1474
+ id,
1475
+ createdAt: existing?.createdAt ?? now,
1476
+ updatedAt: now,
1477
+ title: title ?? existing?.title ?? firstUserText(messages),
1478
+ messageCount: messages.length
1479
+ };
1480
+ const lines = [JSON.stringify({ type: "meta", ...meta })];
1481
+ for (const message of messages) {
1482
+ lines.push(JSON.stringify({ type: "message", message }));
930
1483
  }
931
- return abs;
1484
+ writeFileSync2(sessionPath(id), lines.join("\n") + "\n", "utf-8");
932
1485
  }
933
-
934
- // src/tools/edit_file.ts
935
- var edit_file = {
936
- name: "edit_file",
937
- description: "Replace an exact string in a file. old_str must be unique.",
938
- input_schema: {
939
- type: "object",
940
- properties: {
941
- path: { type: "string", description: "File path" },
942
- old_str: { type: "string", description: "Exact text to replace" },
943
- new_str: { type: "string", description: "Replacement text" }
944
- },
945
- required: ["path", "old_str", "new_str"]
946
- },
947
- handler: ({ path, old_str, new_str }) => {
948
- try {
949
- const abs = confinePath(path);
950
- const src = readFileSync3(abs, "utf-8");
951
- const first = src.indexOf(old_str);
952
- if (first === -1) {
953
- return { content: `old_str not found in ${path}`, is_error: true };
954
- }
955
- if (src.indexOf(old_str, first + 1) !== -1) {
956
- return { content: `old_str not unique in ${path}`, is_error: true };
957
- }
958
- writeFileSync3(abs, src.slice(0, first) + new_str + src.slice(first + old_str.length), "utf-8");
959
- return { content: `Edited ${path}` };
960
- } catch (err) {
961
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
962
- }
1486
+ function listSessions() {
1487
+ if (!existsSync2(SESSION_DIR)) return [];
1488
+ const metas = [];
1489
+ for (const file of readdirSync(SESSION_DIR)) {
1490
+ if (!file.endsWith(".jsonl")) continue;
1491
+ const meta = readMeta(file.replace(/\.jsonl$/, ""));
1492
+ if (meta) metas.push(meta);
963
1493
  }
964
- };
965
-
966
- // src/tools/read_file.ts
967
- import { readFileSync as readFileSync4 } from "fs";
968
- var read_file = {
969
- name: "read_file",
970
- description: "Read entire file contents as UTF-8 text.",
971
- input_schema: {
972
- type: "object",
973
- properties: {
974
- path: { type: "string", description: "File path" }
975
- },
976
- required: ["path"]
977
- },
978
- handler: ({ path }) => {
979
- try {
980
- const MAX = 2e5;
981
- const raw = readFileSync4(confinePath(path), "utf-8");
982
- const truncated = raw.length > MAX;
983
- const body = truncated ? raw.slice(0, MAX) + `
984
- [truncated: ${raw.length - MAX} more chars]` : raw;
985
- return { content: body };
986
- } catch (err) {
987
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
988
- }
1494
+ return metas.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
1495
+ }
1496
+ function deleteSession(id) {
1497
+ try {
1498
+ rmSync(sessionPath(id), { force: true });
1499
+ } catch {
989
1500
  }
990
- };
991
-
992
- // src/tools/write_file.ts
993
- import { writeFileSync as writeFileSync4, mkdirSync as mkdirSync3 } from "fs";
994
- import { dirname } from "path";
995
- var write_file = {
996
- name: "write_file",
997
- description: "Create or overwrite a file with the given content. Parent dirs auto-created.",
998
- input_schema: {
999
- type: "object",
1000
- properties: {
1001
- path: { type: "string", description: "File path" },
1002
- content: { type: "string", description: "Full file content" }
1003
- },
1004
- required: ["path", "content"]
1005
- },
1006
- handler: ({ path, content }) => {
1007
- try {
1008
- const abs = confinePath(path);
1009
- mkdirSync3(dirname(abs), { recursive: true });
1010
- writeFileSync4(abs, content, "utf-8");
1011
- return { content: `Wrote ${path} (${content.length} bytes)` };
1012
- } catch (err) {
1013
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
1501
+ }
1502
+ function loadSession(id) {
1503
+ try {
1504
+ const raw = readFileSync2(sessionPath(id), "utf-8");
1505
+ const messages = [];
1506
+ for (const line of raw.split("\n")) {
1507
+ if (!line.trim()) continue;
1508
+ const parsed = JSON.parse(line);
1509
+ if (parsed.type === "message") messages.push(parsed.message);
1014
1510
  }
1511
+ return messages;
1512
+ } catch {
1513
+ return [];
1015
1514
  }
1016
- };
1017
-
1018
- // src/tools/run_bash.ts
1019
- import { execa } from "execa";
1020
- var run_bash = {
1021
- name: "run_bash",
1022
- description: "Execute a shell command (bash on Unix, cmd on Windows). Returns stdout+stderr. Non-interactive only.",
1023
- input_schema: {
1024
- type: "object",
1025
- properties: {
1026
- command: { type: "string", description: "Shell command to run" },
1027
- timeout_ms: { type: "number", description: "Timeout in ms (default 30000)" }
1028
- },
1029
- required: ["command"]
1030
- },
1031
- handler: async ({ command, timeout_ms }) => {
1032
- try {
1033
- const isWin = process.platform === "win32";
1034
- const shell = isWin ? "cmd" : "bash";
1035
- const shellArgs = isWin ? ["/c", command] : ["-c", command];
1036
- const { stdout, stderr, exitCode } = await execa(shell, shellArgs, {
1037
- timeout: timeout_ms ?? 3e4,
1038
- reject: false,
1039
- all: false
1515
+ }
1516
+ function toDisplayMessages(history) {
1517
+ const out = [];
1518
+ for (const m of history) {
1519
+ if (m.role === "system") continue;
1520
+ const blocks = Array.isArray(m.content) ? m.content : [{ type: "text", text: m.content }];
1521
+ if (m.role === "user") {
1522
+ const text = blocks.filter((b) => b.type === "text").map((b) => b.text).join("");
1523
+ const results = blocks.filter((b) => b.type === "tool_result");
1524
+ if (results.length && out.length) {
1525
+ const last = out[out.length - 1];
1526
+ last.tool_results = [
1527
+ ...last.tool_results ?? [],
1528
+ ...results.map((r) => ({
1529
+ tool_use_id: r.tool_use_id,
1530
+ content: r.content,
1531
+ is_error: r.is_error
1532
+ }))
1533
+ ];
1534
+ }
1535
+ if (text.trim()) out.push({ role: "user", content: text });
1536
+ } else {
1537
+ const text = blocks.filter((b) => b.type === "text").map((b) => b.text).join("");
1538
+ const uses = blocks.filter((b) => b.type === "tool_use").map((b) => ({ id: b.id, name: b.name, input: b.input }));
1539
+ out.push({
1540
+ role: "assistant",
1541
+ content: text,
1542
+ tool_uses: uses.length ? uses : void 0
1040
1543
  });
1041
- const out = [stdout, stderr].filter(Boolean).join("\n");
1042
- const is_error = exitCode !== 0;
1043
- const body = out || (is_error ? `(no output)` : "");
1044
- const content = is_error ? `${body}
1045
- [exit ${exitCode}]` : body;
1046
- return {
1047
- content: content.slice(0, 32e3),
1048
- is_error
1049
- };
1050
- } catch (err) {
1051
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
1052
1544
  }
1053
1545
  }
1054
- };
1546
+ return out;
1547
+ }
1548
+ async function summarizeMessage(model, text) {
1549
+ const fallback = text.trim().slice(0, 80) || "untitled";
1550
+ const prompt = `Summarize this user request as a short title, 3-6 words, no punctuation. Reply with the title only.
1055
1551
 
1056
- // src/tools/grep.ts
1057
- import { execa as execa2 } from "execa";
1058
- var grep = {
1059
- name: "grep",
1060
- description: "Search file contents for a regex pattern. Uses ripgrep if available, falls back to grep -R.",
1061
- input_schema: {
1062
- type: "object",
1063
- properties: {
1064
- pattern: { type: "string", description: "Regex pattern" },
1065
- path: { type: "string", description: "Root path to search (default cwd)" },
1066
- glob: { type: "string", description: 'File glob filter, e.g. "*.ts"' },
1067
- case_insensitive: { type: "string", description: 'Set "true" for case-insensitive' },
1068
- max_results: { type: "number", description: "Max matching lines (default 200)" }
1069
- },
1070
- required: ["pattern"]
1071
- },
1072
- handler: async ({ pattern, path, glob: glob2, case_insensitive, max_results }) => {
1073
- const root = path ?? process.cwd();
1074
- const limit = max_results ?? 200;
1075
- const ci = case_insensitive === true || String(case_insensitive) === "true";
1076
- const tryRg = async () => {
1077
- const args = ["--line-number", "--no-heading", "--color=never", "-m", String(limit)];
1078
- if (ci) args.push("-i");
1079
- if (glob2) args.push("--glob", glob2);
1080
- args.push("--", pattern, root);
1081
- return execa2("rg", args, { reject: false, timeout: 2e4 });
1082
- };
1083
- const tryGrep = async () => {
1084
- const args = ["-R", "-n", "--color=never"];
1085
- if (ci) args.push("-i");
1086
- if (glob2) args.push("--include", glob2);
1087
- args.push("--", pattern, root);
1088
- return execa2("grep", args, { reject: false, timeout: 2e4 });
1089
- };
1090
- try {
1091
- let res;
1092
- try {
1093
- res = await tryRg();
1094
- if (res.exitCode === 127 || (res.stderr ?? "").includes("command not found")) {
1095
- res = await tryGrep();
1096
- }
1097
- } catch {
1098
- res = await tryGrep();
1099
- }
1100
- const lines = (res.stdout ?? "").split("\n").slice(0, limit);
1101
- const out = lines.join("\n");
1102
- const code = res.exitCode ?? 0;
1103
- if (!out && code === 1) return { content: "No matches." };
1104
- return { content: out || res.stderr || "No matches.", is_error: code > 1 };
1105
- } catch (err) {
1106
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
1552
+ Request:
1553
+ ${text.slice(0, 2e3)}`;
1554
+ try {
1555
+ let out = "";
1556
+ for await (const chunk of chat(
1557
+ model,
1558
+ [{ role: "user", content: prompt }],
1559
+ void 0,
1560
+ { temperature: 0.2, num_predict: 32 }
1561
+ )) {
1562
+ if (chunk.content) out += chunk.content;
1107
1563
  }
1564
+ return out.trim().split("\n").filter(Boolean)[0]?.trim() || fallback;
1565
+ } catch {
1566
+ return fallback;
1108
1567
  }
1109
- };
1110
-
1111
- // src/tools/glob.ts
1112
- import { execa as execa3 } from "execa";
1113
- function globToFindName(glob2) {
1114
- return glob2;
1115
1568
  }
1116
- var glob = {
1117
- name: "glob",
1118
- description: 'List files matching a glob pattern (e.g. "**/*.ts"). Uses ripgrep --files if available.',
1119
- input_schema: {
1120
- type: "object",
1121
- properties: {
1122
- pattern: { type: "string", description: 'Glob pattern, e.g. "**/*.ts"' },
1123
- path: { type: "string", description: "Root path (default cwd)" },
1124
- max_results: { type: "number", description: "Max paths returned (default 500)" }
1125
- },
1126
- required: ["pattern"]
1127
- },
1128
- handler: async ({ pattern, path, max_results }) => {
1129
- const root = path ?? process.cwd();
1130
- const limit = max_results ?? 500;
1131
- const tryRg = () => execa3("rg", ["--files", "--hidden", "--glob", pattern, root], {
1132
- reject: false,
1133
- timeout: 2e4
1134
- });
1135
- const tryFind = () => {
1136
- const name = globToFindName(pattern.replace(/^\*\*\//, ""));
1137
- return execa3("find", [root, "-type", "f", "-name", name], {
1138
- reject: false,
1139
- timeout: 2e4
1140
- });
1141
- };
1569
+
1570
+ // src/ui/FilePicker.tsx
1571
+ import { Box as Box7, Text as Text7 } from "ink";
1572
+ import { readdirSync as readdirSync2 } from "fs";
1573
+ import { join as join3, relative } from "path";
1574
+ import { jsx as jsx7, jsxs as jsxs7 } from "react/jsx-runtime";
1575
+ var IGNORE = /* @__PURE__ */ new Set(["node_modules", ".git", "dist", "build", ".next", "coverage", ".miii"]);
1576
+ var MAX_RESULTS = 10;
1577
+ var MAX_SCAN = 2e3;
1578
+ var cache = null;
1579
+ function listFiles(cwd) {
1580
+ if (cache && cache.cwd === cwd) return cache.files;
1581
+ const out = [];
1582
+ const stack = [cwd];
1583
+ while (stack.length && out.length < MAX_SCAN) {
1584
+ const dir = stack.pop();
1585
+ let entries;
1142
1586
  try {
1143
- let res;
1144
- try {
1145
- res = await tryRg();
1146
- if (res.exitCode === 127 || (res.stderr ?? "").includes("command not found")) {
1147
- res = await tryFind();
1148
- }
1149
- } catch {
1150
- res = await tryFind();
1151
- }
1152
- const lines = (res.stdout ?? "").split("\n").filter(Boolean).slice(0, limit);
1153
- if (lines.length === 0) return { content: "No files matched." };
1154
- return { content: lines.join("\n") };
1155
- } catch (err) {
1156
- return { content: err instanceof Error ? err.message : String(err), is_error: true };
1587
+ entries = readdirSync2(dir, { withFileTypes: true });
1588
+ } catch {
1589
+ continue;
1157
1590
  }
1158
- }
1159
- };
1160
-
1161
- // src/tools/registry.ts
1162
- var TOOLS = [
1163
- edit_file,
1164
- read_file,
1165
- write_file,
1166
- run_bash,
1167
- grep,
1168
- glob
1169
- ];
1170
- function getTool(name) {
1171
- return TOOLS.find((t) => t.name === name);
1172
- }
1173
- function toOllamaTools(tools = TOOLS) {
1174
- return tools.map((t) => ({
1175
- type: "function",
1176
- function: {
1177
- name: t.name,
1178
- description: t.description,
1179
- parameters: {
1180
- type: "object",
1181
- properties: t.input_schema.properties,
1182
- required: t.input_schema.required
1183
- }
1591
+ for (const e of entries) {
1592
+ if (IGNORE.has(e.name) || e.name.startsWith(".")) continue;
1593
+ const full = join3(dir, e.name);
1594
+ if (e.isDirectory()) stack.push(full);
1595
+ else if (e.isFile()) out.push(relative(cwd, full));
1596
+ if (out.length >= MAX_SCAN) break;
1184
1597
  }
1185
- }));
1186
- }
1187
-
1188
- // src/tools/validate.ts
1189
- import { z } from "zod";
1190
- function propSchema(spec) {
1191
- if (spec.enum && spec.enum.length) return z.enum(spec.enum);
1192
- switch (spec.type) {
1193
- case "string":
1194
- return z.string();
1195
- case "number":
1196
- return z.number();
1197
- case "integer":
1198
- return z.number().int();
1199
- case "boolean":
1200
- return z.boolean();
1201
- case "array":
1202
- return z.array(z.unknown());
1203
- case "object":
1204
- return z.record(z.unknown());
1205
- default:
1206
- return z.unknown();
1207
1598
  }
1599
+ cache = { cwd, files: out };
1600
+ return out;
1601
+ }
1602
+ function parseMention(input) {
1603
+ const m = input.match(/(?:^|\s)@([^\s]*)$/);
1604
+ if (!m) return null;
1605
+ return { query: m[1], start: input.length - m[1].length - 1 };
1208
1606
  }
1209
- function toZod(schema) {
1210
- const required = new Set(schema.required ?? []);
1211
- const shape = {};
1212
- for (const [key, spec] of Object.entries(schema.properties)) {
1213
- shape[key] = required.has(key) ? propSchema(spec) : z.unknown().optional();
1607
+ function searchFiles(cwd, query) {
1608
+ const files = listFiles(cwd);
1609
+ const q = query.toLowerCase();
1610
+ if (!q) return files.slice(0, MAX_RESULTS);
1611
+ const scored = [];
1612
+ for (const f of files) {
1613
+ const lf = f.toLowerCase();
1614
+ const idx = lf.indexOf(q);
1615
+ if (idx === -1) continue;
1616
+ const base = lf.split("/").pop() ?? lf;
1617
+ const baseIdx = base.indexOf(q);
1618
+ const score = baseIdx === 0 ? 0 : baseIdx > -1 ? 1 : 2 + idx;
1619
+ scored.push([score, f]);
1620
+ if (scored.length > 500) break;
1214
1621
  }
1215
- return z.object(shape).passthrough();
1622
+ scored.sort((a, b) => a[0] - b[0] || a[1].length - b[1].length);
1623
+ return scored.slice(0, MAX_RESULTS).map(([, f]) => f);
1216
1624
  }
1217
- function validateInput(schema, input) {
1218
- const result = toZod(schema).safeParse(input ?? {});
1219
- if (result.success) return null;
1220
- const issues = result.error.issues.map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`).join("; ");
1221
- return `Invalid arguments: ${issues}`;
1625
+ function FilePicker({ matches: matches2, cursor }) {
1626
+ if (matches2.length === 0) return null;
1627
+ return /* @__PURE__ */ jsxs7(
1628
+ Box7,
1629
+ {
1630
+ flexDirection: "column",
1631
+ borderStyle: "round",
1632
+ borderColor: "gray",
1633
+ marginX: 1,
1634
+ marginBottom: 0,
1635
+ paddingX: 1,
1636
+ children: [
1637
+ matches2.map((f, i) => {
1638
+ const active = i === cursor;
1639
+ return /* @__PURE__ */ jsx7(Box7, { children: /* @__PURE__ */ jsxs7(Text7, { bold: active, color: active ? "blue" : void 0, dimColor: !active, children: [
1640
+ active ? "\u276F " : " ",
1641
+ f
1642
+ ] }) }, f);
1643
+ }),
1644
+ /* @__PURE__ */ jsx7(Box7, { marginTop: 0, children: /* @__PURE__ */ jsx7(Text7, { dimColor: true, children: "\u2191\u2193 navigate tab insert esc dismiss" }) })
1645
+ ]
1646
+ }
1647
+ );
1222
1648
  }
1223
1649
 
1224
- // src/prompt/system.ts
1225
- function buildSystemPrompt(tools, cwd) {
1226
- const toolLines = tools.map((t) => `- ${t.name}: ${t.description}`).join("\n");
1227
- return `You are miii, a senior software engineer running in a terminal.
1228
-
1229
- Working directory: ${cwd}
1230
-
1231
- # Goal Understanding (read this first, every turn)
1232
- Before acting on any request, extract and hold three things:
1233
- GOAL: what the user ultimately wants (outcome, not steps)
1234
- CRITERION: how you will know the goal is met
1235
- GAPS: anything unclear that would force you to guess
1236
-
1237
- If GAPS is non-empty, ask the minimum questions needed to fill them \u2014 one message, numbered list \u2014 before touching any file or running any command. Do not guess. Do not act on assumptions.
1238
-
1239
- Re-read GOAL before every tool call. If a tool call does not move toward GOAL, skip it.
1240
-
1241
- # Attention: re-attend to goal at each step
1242
- After each tool result, answer silently: "Does this result move me toward GOAL?"
1243
- YES \u2192 continue
1244
- NO \u2192 stop, re-derive plan from GOAL, explain the correction in one line
1245
-
1246
- This prevents drift. Each step attends to the original goal, not just the previous step.
1247
-
1248
- # Output format
1249
- - Always reply in plain text. Never use Markdown syntax: no \`#\` headings, no \`**bold**\`, no \`-\` bullet lists, no fenced \`\`\` code blocks, no inline backticks.
1250
- - Quote code, paths, and identifiers inline as plain text. Do not wrap them.
1251
- - Keep prose terse.
1252
-
1253
- # Engineering mindset
1254
- - Treat every request as one of: bug, feature, or fix. Name which one before you start.
1255
- - Apply first principles: decompose unclear tasks into smallest concrete sub-problems, solve each explicitly, compose the result.
1256
- - Never guess. If a fact (file path, function signature, current behavior) is unknown, read or search for it first.
1257
-
1258
- # Clarifying questions \u2014 when to ask
1259
- Ask BEFORE acting when:
1260
- - The goal has more than one valid interpretation
1261
- - Success criterion is ambiguous (e.g. "make it better" \u2014 better how?)
1262
- - Required context is missing (which file? which behavior? which user?)
1263
- - Two reasonable approaches have different tradeoffs the user should choose
1264
-
1265
- Do NOT ask when:
1266
- - The answer is findable by reading the codebase
1267
- - There is only one sensible interpretation
1268
- - The user has already answered this implicitly
1269
-
1270
- Ask in a numbered list. One round of questions per turn. Then wait.
1271
-
1272
- # Tool calls
1273
- - When you need a tool, emit the tool call directly. No preamble, no narration, no "I will use X".
1274
- - Never describe a tool call instead of emitting it. If you cannot emit the call, answer in plain text.
1275
- - After a tool result, move directly to the next tool call or the final answer. Do not restate what the previous tool did.
1276
-
1277
- # Tools
1278
- You have access to the following tools. Call them via the function-calling interface.
1279
- ${toolLines}
1280
-
1281
- # Loop semantics
1282
- - When you need to act on the filesystem or run a command, emit a tool call.
1283
- - After each tool result, decide: more tool calls, or a final plain-text answer.
1284
- - Stop emitting tool calls when GOAL is met. Reply with a concise plain-text final message confirming CRITERION is satisfied.
1285
-
1286
- # Rules
1287
- - Prefer editing existing files over creating new ones.
1288
- - For edit_file, ensure old_str is unique within the target file.
1289
- - Never invent file paths. Read, glob, or grep before editing.
1290
- - No filler, no pleasantries, no apologies.
1291
-
1292
- # Testing and verification
1293
- - Always test the code after a change. Run the project's tests (e.g. npm test, pytest, go test) or the relevant script via run_bash before declaring a task done.
1294
- - If no test exists for the change, run the affected entry point via run_bash to verify it behaves correctly.
1295
- - Treat a green test run or a successful command as the completion signal. If it fails, fix and re-run.
1296
-
1297
- # Permissions
1298
- - File tools are confined to the working directory; paths outside it are denied.
1299
- - Each tool call may prompt the user for approval. If they choose "don't ask again", the exact command or path is persisted to ~/.miii/permissions.json and the same call is auto-allowed thereafter.
1300
- `;
1301
- }
1650
+ // src/ui/ChatView.tsx
1651
+ import { Box as Box9, Text as Text9 } from "ink";
1302
1652
 
1303
- // src/permissions/policy.ts
1304
- import { readFileSync as readFileSync5, writeFileSync as writeFileSync5, mkdirSync as mkdirSync4, existsSync as existsSync3, renameSync } from "fs";
1305
- import { join as join4 } from "path";
1306
- import { homedir as homedir3 } from "os";
1307
- var RULES_DIR = join4(homedir3(), ".miii");
1308
- var RULES_PATH = join4(RULES_DIR, "permissions.json");
1309
- function loadRules() {
1310
- if (!existsSync3(RULES_PATH)) return [];
1311
- try {
1312
- const data = JSON.parse(readFileSync5(RULES_PATH, "utf-8"));
1313
- return Array.isArray(data.rules) ? data.rules : [];
1314
- } catch {
1315
- return [];
1316
- }
1653
+ // src/ui/ThinkingBlock.tsx
1654
+ import { useState as useState2, useEffect as useEffect2 } from "react";
1655
+ import { Box as Box8, Text as Text8 } from "ink";
1656
+ import { jsx as jsx8, jsxs as jsxs8 } from "react/jsx-runtime";
1657
+ var FRAMES = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
1658
+ var globalThinkingVisible = false;
1659
+ var listeners = /* @__PURE__ */ new Set();
1660
+ function toggleThinkingVisible() {
1661
+ globalThinkingVisible = !globalThinkingVisible;
1662
+ listeners.forEach((fn) => fn());
1317
1663
  }
1318
- function saveRules(rules) {
1319
- mkdirSync4(RULES_DIR, { recursive: true });
1320
- const tmp = RULES_PATH + ".tmp";
1321
- writeFileSync5(tmp, JSON.stringify({ rules }, null, 2), "utf-8");
1322
- renameSync(tmp, RULES_PATH);
1664
+ function useThinkingVisible() {
1665
+ const [visible, setVisible] = useState2(globalThinkingVisible);
1666
+ useEffect2(() => {
1667
+ const handler = () => setVisible(globalThinkingVisible);
1668
+ listeners.add(handler);
1669
+ return () => {
1670
+ listeners.delete(handler);
1671
+ };
1672
+ }, []);
1673
+ return visible;
1323
1674
  }
1324
- function addRule(tool, pattern) {
1325
- const rules = loadRules();
1326
- if (rules.some((r) => r.tool === tool && r.pattern === pattern)) return;
1327
- rules.push({ tool, pattern });
1328
- saveRules(rules);
1675
+ function ThinkingBlock({ content }) {
1676
+ const [frame, setFrame] = useState2(0);
1677
+ const visible = useThinkingVisible();
1678
+ useEffect2(() => {
1679
+ const t = setInterval(() => setFrame((f) => (f + 1) % FRAMES.length), 80);
1680
+ return () => clearInterval(t);
1681
+ }, []);
1682
+ return /* @__PURE__ */ jsxs8(Box8, { flexDirection: "column", marginLeft: 2, marginBottom: 1, children: [
1683
+ /* @__PURE__ */ jsxs8(Box8, { children: [
1684
+ /* @__PURE__ */ jsxs8(Text8, { color: "blue", children: [
1685
+ FRAMES[frame],
1686
+ " "
1687
+ ] }),
1688
+ /* @__PURE__ */ jsx8(Text8, { dimColor: true, italic: true, children: "thinking\u2026" }),
1689
+ /* @__PURE__ */ jsxs8(Text8, { dimColor: true, children: [
1690
+ " \xB7 ctrl+t to ",
1691
+ visible ? "hide" : "show",
1692
+ " thoughts"
1693
+ ] })
1694
+ ] }),
1695
+ visible && content ? /* @__PURE__ */ jsx8(Box8, { marginLeft: 2, children: /* @__PURE__ */ jsx8(Text8, { dimColor: true, italic: true, children: content }) }) : null
1696
+ ] });
1329
1697
  }
1330
- function subjectFor(toolName, input) {
1331
- const obj = input ?? {};
1332
- if (toolName === "run_bash") return typeof obj.command === "string" ? obj.command : "";
1333
- if (typeof obj.path === "string") return obj.path;
1334
- return "";
1698
+
1699
+ // src/ui/constants.ts
1700
+ var EMPTY_STATE_HINTS = [
1701
+ "\u2022 explain @file \u2014 reference a file with @",
1702
+ "\u2022 /models \u2014 switch model or effort",
1703
+ "\u2022 /new \u2014 start a new chat",
1704
+ "\u2022 /sessions \u2014 view saved chats",
1705
+ "\u2022 ctrl+t \u2014 toggle thinking"
1706
+ ];
1707
+ var EMPTY_STATE_TITLE = "Ask anything, or try:";
1708
+
1709
+ // src/ui/ChatView.tsx
1710
+ import { jsx as jsx9, jsxs as jsxs9 } from "react/jsx-runtime";
1711
+ function formatTokens(n) {
1712
+ if (n >= 1e3) return (n / 1e3).toFixed(n >= 1e4 ? 0 : 1) + "k";
1713
+ return String(n);
1335
1714
  }
1336
- function globToRegExp(glob2) {
1337
- const escaped = glob2.replace(/[.+^${}()|[\]\\]/g, "\\$&");
1338
- const pattern = escaped.replace(/\*/g, ".*").replace(/\?/g, ".");
1339
- return new RegExp(`^${pattern}$`);
1715
+ function formatDuration(ms) {
1716
+ const totalSec = ms / 1e3;
1717
+ if (totalSec < 60) return `${totalSec.toFixed(1)}s`;
1718
+ const m = Math.floor(totalSec / 60);
1719
+ const s = Math.round(totalSec - m * 60);
1720
+ return `${m}m ${s}s`;
1340
1721
  }
1341
- function matches(rule, toolName, subject) {
1342
- if (rule.tool !== toolName) return false;
1343
- try {
1344
- return globToRegExp(rule.pattern).test(subject);
1345
- } catch {
1346
- return false;
1347
- }
1722
+ function countLines(s) {
1723
+ if (!s) return 0;
1724
+ return s.split("\n").length;
1348
1725
  }
1349
- async function check(toolName, input, ctx) {
1350
- const subject = subjectFor(toolName, input);
1351
- const rules = loadRules();
1352
- if (rules.some((r) => matches(r, toolName, subject))) return "allow";
1353
- const answer = await ctx.ask(toolName, input);
1354
- if (answer === "no") return "deny";
1355
- if (answer === "always") addRule(toolName, subject);
1356
- return "allow";
1726
+ function FileEditBlock({
1727
+ label,
1728
+ path,
1729
+ added,
1730
+ removed,
1731
+ previewLines
1732
+ }) {
1733
+ const MAX = 16;
1734
+ const shown = previewLines.slice(0, MAX);
1735
+ const extra = previewLines.length - shown.length;
1736
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
1737
+ /* @__PURE__ */ jsxs9(Box9, { children: [
1738
+ /* @__PURE__ */ jsx9(Text9, { color: "yellow", children: "\u25CF " }),
1739
+ /* @__PURE__ */ jsxs9(Text9, { color: "yellow", children: [
1740
+ label,
1741
+ " "
1742
+ ] }),
1743
+ /* @__PURE__ */ jsx9(Text9, { children: "(" }),
1744
+ /* @__PURE__ */ jsx9(Text9, { bold: true, children: path }),
1745
+ /* @__PURE__ */ jsx9(Text9, { children: ")" })
1746
+ ] }),
1747
+ /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
1748
+ "\u23BF ",
1749
+ removed > 0 ? `Added ${added} lines, removed ${removed} lines` : `Added ${added} lines`
1750
+ ] }) }),
1751
+ shown.map((ln, i) => /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { color: ln.sign === "+" ? "green" : ln.sign === "-" ? "red" : void 0, dimColor: ln.sign === " ", children: [
1752
+ ln.sign,
1753
+ " ",
1754
+ ln.text
1755
+ ] }) }, i)),
1756
+ extra > 0 && /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
1757
+ "\u2026 ",
1758
+ extra,
1759
+ " more lines"
1760
+ ] }) })
1761
+ ] });
1357
1762
  }
1358
-
1359
- // src/agent/adapter.ts
1360
- function mintToolUseId() {
1361
- const rand = Math.random().toString(36).slice(2, 14);
1362
- return `toolu_${rand}`;
1763
+ var TOOL_LABEL = {
1764
+ write_file: "Write",
1765
+ edit_file: "Update",
1766
+ read_file: "Read",
1767
+ run_bash: "Bash",
1768
+ glob: "Glob",
1769
+ grep: "Grep"
1770
+ };
1771
+ function truncate(s, max) {
1772
+ if (s.length <= max) return s;
1773
+ return s.slice(0, max - 1) + "\u2026";
1363
1774
  }
1364
- function toOllamaMessages(history, system) {
1365
- const out = [{ role: "system", content: system }];
1366
- for (const msg of history) {
1367
- if (typeof msg.content === "string") {
1368
- out.push({ role: msg.role === "system" ? "system" : msg.role, content: msg.content });
1369
- continue;
1370
- }
1371
- if (msg.role === "assistant") {
1372
- const text = msg.content.filter((b) => b.type === "text").map((b) => b.text).join("");
1373
- const tool_uses = msg.content.filter((b) => b.type === "tool_use");
1374
- const ollamaMsg = { role: "assistant", content: text };
1375
- if (tool_uses.length > 0) {
1376
- ollamaMsg.tool_calls = tool_uses.map((u) => ({
1377
- function: { name: u.name, arguments: u.input }
1378
- }));
1379
- }
1380
- out.push(ollamaMsg);
1381
- continue;
1775
+ function toolHeader(use) {
1776
+ const label = TOOL_LABEL[use.name] ?? use.name;
1777
+ const input = use.input ?? {};
1778
+ let arg = "";
1779
+ switch (use.name) {
1780
+ case "write_file":
1781
+ case "edit_file":
1782
+ case "read_file":
1783
+ arg = String(input.path ?? input.file_path ?? "");
1784
+ break;
1785
+ case "run_bash": {
1786
+ const cmd2 = String(input.command ?? "").replace(/\s+/g, " ");
1787
+ arg = truncate(cmd2, 120);
1788
+ break;
1382
1789
  }
1383
- if (msg.role === "user") {
1384
- const tool_results = msg.content.filter((b) => b.type === "tool_result");
1385
- const texts = msg.content.filter((b) => b.type === "text");
1386
- for (const tr of tool_results) {
1387
- out.push({ role: "tool", content: tr.content });
1388
- }
1389
- if (texts.length > 0) {
1390
- out.push({ role: "user", content: texts.map((t) => t.text).join("") });
1391
- }
1790
+ case "glob":
1791
+ case "grep":
1792
+ arg = truncate(String(input.pattern ?? ""), 120);
1793
+ break;
1794
+ default: {
1795
+ arg = truncate(JSON.stringify(input), 80);
1392
1796
  }
1393
1797
  }
1394
- return out;
1798
+ return { label, arg };
1395
1799
  }
1396
- function parseTextToolCalls(text, knownToolNames) {
1397
- if (!text) return { calls: [], cleanedText: text };
1398
- const calls = [];
1399
- let cleaned = text;
1400
- const tagRe = /<\|?tool_call\|?>\s*([\s\S]*?)\s*<\|?\/?tool_call\|?>/g;
1401
- cleaned = cleaned.replace(tagRe, (_m, body) => {
1402
- const c = tryParse(body, knownToolNames);
1403
- if (c) calls.push(c);
1404
- return "";
1405
- });
1406
- const fenceRe = /```(?:json|tool_call)?\s*([\s\S]*?)```/g;
1407
- cleaned = cleaned.replace(fenceRe, (_m, body) => {
1408
- const c = tryParse(body, knownToolNames);
1409
- if (c) {
1410
- calls.push(c);
1411
- return "";
1800
+ function summarizeResult(res, toolName) {
1801
+ const content = res.content ?? "";
1802
+ const lines = content.split("\n");
1803
+ if (!res.is_error) {
1804
+ if (toolName === "read_file") {
1805
+ const total = lines.length;
1806
+ return `Read ${total} line${total === 1 ? "" : "s"}`;
1412
1807
  }
1413
- return _m;
1414
- });
1415
- if (calls.length === 0) {
1416
- const candidate = extractFirstJsonObject(cleaned);
1417
- if (candidate) {
1418
- const c = tryParse(candidate.json, knownToolNames);
1419
- if (c) {
1420
- calls.push(c);
1421
- cleaned = (cleaned.slice(0, candidate.start) + cleaned.slice(candidate.end)).trim();
1422
- }
1808
+ if (toolName === "grep") {
1809
+ if (content === "No matches.") return "No matches";
1810
+ const n = lines.filter(Boolean).length;
1811
+ return `${n} match${n === 1 ? "" : "es"}`;
1812
+ }
1813
+ if (toolName === "glob") {
1814
+ if (content === "No files matched.") return "No files";
1815
+ const n = lines.filter(Boolean).length;
1816
+ return `${n} file${n === 1 ? "" : "s"}`;
1423
1817
  }
1424
1818
  }
1425
- return { calls, cleanedText: cleaned.trim() };
1819
+ const firstNonEmpty = lines.find((l) => l.trim().length > 0) ?? "";
1820
+ const extra = lines.length - 1;
1821
+ const head = firstNonEmpty.length > 100 ? firstNonEmpty.slice(0, 97) + "..." : firstNonEmpty;
1822
+ return extra > 0 ? `${head} (+${extra} lines)` : head;
1426
1823
  }
1427
- function tryParse(raw, knownToolNames) {
1428
- const s = raw.trim();
1429
- if (!s.startsWith("{")) return null;
1430
- try {
1431
- const obj = JSON.parse(s);
1432
- const name = typeof obj.name === "string" ? obj.name : void 0;
1433
- const args = obj.arguments ?? obj.parameters ?? obj.input ?? {};
1434
- if (!name || !knownToolNames.includes(name)) return null;
1435
- return { function: { name, arguments: args } };
1436
- } catch {
1437
- return null;
1824
+ function ToolResultBlock({ result, toolName }) {
1825
+ const content = result.content ?? "";
1826
+ const lines = content.split("\n");
1827
+ const showMulti = (toolName === "run_bash" || toolName === "grep" || toolName === "glob" || result.is_error) && lines.length > 1;
1828
+ if (!showMulti) {
1829
+ return /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { color: result.is_error ? "red" : void 0, dimColor: !result.is_error, children: [
1830
+ "\u23BF ",
1831
+ summarizeResult(result, toolName)
1832
+ ] }) });
1438
1833
  }
1834
+ const MAX_LINES = 10;
1835
+ const MAX_LINE_WIDTH = 200;
1836
+ const shown = lines.slice(0, MAX_LINES).map((l) => truncate(l, MAX_LINE_WIDTH));
1837
+ const extra = lines.length - shown.length;
1838
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
1839
+ /* @__PURE__ */ jsxs9(Text9, { color: result.is_error ? "red" : void 0, dimColor: !result.is_error, children: [
1840
+ "\u23BF ",
1841
+ summarizeResult(result, toolName)
1842
+ ] }),
1843
+ shown.map((ln, i) => /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsx9(Text9, { color: result.is_error ? "red" : void 0, dimColor: true, children: ln || " " }) }, i)),
1844
+ extra > 0 && /* @__PURE__ */ jsx9(Box9, { marginLeft: 4, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
1845
+ "\u2026 ",
1846
+ extra,
1847
+ " more lines"
1848
+ ] }) })
1849
+ ] });
1439
1850
  }
1440
- function extractFirstJsonObject(s) {
1441
- const start = s.indexOf("{");
1442
- if (start === -1) return null;
1443
- let depth = 0;
1444
- let inStr = false;
1445
- let esc = false;
1446
- for (let i = start; i < s.length; i++) {
1447
- const ch = s[i];
1448
- if (inStr) {
1449
- if (esc) esc = false;
1450
- else if (ch === "\\") esc = true;
1451
- else if (ch === '"') inStr = false;
1452
- continue;
1453
- }
1454
- if (ch === '"') {
1455
- inStr = true;
1456
- continue;
1457
- }
1458
- if (ch === "{") depth++;
1459
- else if (ch === "}") {
1460
- depth--;
1461
- if (depth === 0) return { json: s.slice(start, i + 1), start, end: i + 1 };
1462
- }
1851
+ function ToolUseLine({ use, result }) {
1852
+ if (use.name === "write_file" && !result?.is_error) {
1853
+ const input = use.input;
1854
+ const content = input.content ?? "";
1855
+ const added = countLines(content);
1856
+ const preview = content.split("\n").map((t) => ({ sign: "+", text: t }));
1857
+ return /* @__PURE__ */ jsx9(FileEditBlock, { label: "Write", path: input.path ?? "", added, removed: 0, previewLines: preview });
1463
1858
  }
1464
- return null;
1859
+ if (use.name === "edit_file" && !result?.is_error) {
1860
+ const input = use.input;
1861
+ const oldS = input.old_str ?? "";
1862
+ const newS = input.new_str ?? "";
1863
+ const added = countLines(newS);
1864
+ const removed = countLines(oldS);
1865
+ const preview = [
1866
+ ...oldS.split("\n").map((t) => ({ sign: "-", text: t })),
1867
+ ...newS.split("\n").map((t) => ({ sign: "+", text: t }))
1868
+ ];
1869
+ return /* @__PURE__ */ jsx9(FileEditBlock, { label: "Update", path: input.path ?? "", added, removed, previewLines: preview });
1870
+ }
1871
+ const { label, arg } = toolHeader(use);
1872
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 2, children: [
1873
+ /* @__PURE__ */ jsxs9(Box9, { children: [
1874
+ /* @__PURE__ */ jsx9(Text9, { color: "yellow", children: "\u25CF " }),
1875
+ /* @__PURE__ */ jsxs9(Text9, { color: "yellow", children: [
1876
+ label,
1877
+ " "
1878
+ ] }),
1879
+ /* @__PURE__ */ jsx9(Text9, { children: "(" }),
1880
+ /* @__PURE__ */ jsx9(Text9, { bold: true, children: arg }),
1881
+ /* @__PURE__ */ jsx9(Text9, { children: ")" })
1882
+ ] }),
1883
+ result && /* @__PURE__ */ jsx9(ToolResultBlock, { result, toolName: use.name })
1884
+ ] });
1465
1885
  }
1466
- function blocksFromOllama(text, tool_calls, knownToolNames = []) {
1467
- const blocks = [];
1468
- let finalText = text;
1469
- let finalCalls = tool_calls ?? [];
1470
- if (finalCalls.length === 0 && knownToolNames.length > 0) {
1471
- const parsed = parseTextToolCalls(text, knownToolNames);
1472
- if (parsed.calls.length > 0) {
1473
- finalCalls = parsed.calls;
1474
- finalText = parsed.cleanedText;
1475
- }
1886
+ function AssistantMessage({ msg }) {
1887
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, children: [
1888
+ msg.content && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", children: [
1889
+ /* @__PURE__ */ jsx9(Text9, { color: "white", children: "\u25CF " }),
1890
+ /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: msg.content }) })
1891
+ ] }),
1892
+ msg.tool_uses?.map((u) => {
1893
+ const r = msg.tool_results?.find((x) => x.tool_use_id === u.id);
1894
+ return /* @__PURE__ */ jsx9(ToolUseLine, { use: u, result: r }, u.id);
1895
+ }),
1896
+ msg.tokens && /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
1897
+ `\u21B3 Completed \xB7 ${formatTokens(msg.tokens.prompt_eval + msg.tokens.eval)} tokens`,
1898
+ msg.duration != null ? ` \xB7 ${formatDuration(msg.duration)}` : ""
1899
+ ] }) })
1900
+ ] });
1901
+ }
1902
+ function summarizeInput(input) {
1903
+ if (!input || typeof input !== "object") return "";
1904
+ const obj = input;
1905
+ const priority = ["path", "file_path", "command", "pattern", "query"];
1906
+ for (const k of priority) {
1907
+ const v = obj[k];
1908
+ if (typeof v === "string" && v.length > 0) return `${k}: ${v}`;
1476
1909
  }
1477
- if (finalText) blocks.push({ type: "text", text: finalText });
1478
- for (const tc of finalCalls) {
1479
- blocks.push({
1480
- type: "tool_use",
1481
- id: mintToolUseId(),
1482
- name: tc.function.name,
1483
- input: tc.function.arguments ?? {}
1484
- });
1910
+ const first = Object.entries(obj).find(([, v]) => typeof v === "string");
1911
+ if (first) {
1912
+ const [k, v] = first;
1913
+ const trimmed = v.length > 80 ? v.slice(0, 80) + "\u2026" : v;
1914
+ return `${k}: ${trimmed}`;
1485
1915
  }
1486
- return blocks;
1916
+ return "";
1487
1917
  }
1488
-
1489
- // src/agent/loop.ts
1490
- var MAX_TURNS = 25;
1491
- var NUM_PREDICT = 4096;
1492
- var REPEAT_TAIL = 120;
1493
- var REPEAT_KILL = 4;
1494
- async function* runAgent(opts) {
1495
- const { model, cwd, permissions, hooks, signal, num_ctx } = opts;
1496
- const startTime = Date.now();
1497
- const system = buildSystemPrompt(TOOLS, cwd);
1498
- const ollamaTools = toOllamaTools(TOOLS);
1499
- const history = [
1500
- ...opts.history,
1501
- { role: "user", content: opts.userText }
1918
+ function PermissionPrompt({ req, cursor }) {
1919
+ const label = TOOL_LABEL[req.toolName] ?? req.toolName;
1920
+ const options = [
1921
+ { label: "Yes", key: "yes" },
1922
+ { label: "Yes, don't ask again for this", key: "always" },
1923
+ { label: "No", key: "no" }
1502
1924
  ];
1503
- let promptTokens = 0;
1504
- let evalTokens = 0;
1505
- let lastAssistantSig = "";
1506
- let repeatCount = 0;
1507
- for (let turn = 0; turn < MAX_TURNS; turn++) {
1508
- let text = "";
1509
- let tool_calls;
1510
- let lastTail = "";
1511
- let tailRepeats = 0;
1512
- let streamLooped = false;
1513
- const ac = new AbortController();
1514
- const composedSignal = signal ? AbortSignal.any ? AbortSignal.any([signal, ac.signal]) : ac.signal : ac.signal;
1515
- if (signal) signal.addEventListener("abort", () => ac.abort(), { once: true });
1516
- try {
1517
- for await (const chunk of chat(model, toOllamaMessages(history, system), ollamaTools, { signal: composedSignal, num_ctx, num_predict: NUM_PREDICT })) {
1518
- if (signal?.aborted) break;
1519
- if (chunk.content) {
1520
- text += chunk.content;
1521
- yield { type: "text-delta", text: chunk.content };
1522
- if (text.length >= REPEAT_TAIL) {
1523
- const tail = text.slice(-REPEAT_TAIL);
1524
- if (tail === lastTail) {
1525
- tailRepeats++;
1526
- if (tailRepeats >= REPEAT_KILL) {
1527
- streamLooped = true;
1528
- ac.abort();
1529
- break;
1530
- }
1531
- } else {
1532
- tailRepeats = 0;
1533
- lastTail = tail;
1534
- }
1535
- }
1536
- }
1537
- if (chunk.thinking) {
1538
- yield { type: "thinking-delta", text: chunk.thinking };
1539
- }
1540
- if (chunk.tool_calls && chunk.tool_calls.length > 0) {
1541
- tool_calls = chunk.tool_calls;
1542
- }
1543
- if (chunk.done) {
1544
- promptTokens += chunk.prompt_eval_count ?? 0;
1545
- evalTokens += chunk.eval_count ?? 0;
1546
- }
1547
- }
1548
- } catch (err) {
1549
- if (streamLooped) {
1550
- yield { type: "error", message: "Model stuck in repetition. Aborted stream. Try a different model or shorten context." };
1551
- return history;
1552
- }
1553
- yield { type: "error", message: err instanceof Error ? err.message : String(err) };
1554
- return history;
1555
- }
1556
- if (streamLooped) {
1557
- yield { type: "error", message: "Model stuck in repetition. Aborted stream. Try a different model or shorten context." };
1558
- return history;
1559
- }
1560
- if (signal?.aborted) {
1561
- yield {
1562
- type: "aborted",
1563
- prompt_tokens: promptTokens,
1564
- eval_tokens: evalTokens,
1565
- duration_ms: Date.now() - startTime
1566
- };
1567
- return history;
1568
- }
1569
- const blocks = blocksFromOllama(text, tool_calls, TOOLS.map((t) => t.name));
1570
- const tool_uses = blocks.filter((b) => b.type === "tool_use");
1571
- history.push({ role: "assistant", content: blocks });
1572
- if (tool_uses.length === 0) {
1573
- yield { type: "turn-end", stop_reason: "end_turn" };
1574
- break;
1575
- }
1576
- const sig = JSON.stringify(
1577
- blocks.map(
1578
- (b) => b.type === "tool_use" ? { t: "u", n: b.name, i: b.input } : b.type === "text" ? { t: "t", x: b.text.trim() } : b
1579
- )
1580
- );
1581
- if (sig === lastAssistantSig) {
1582
- repeatCount++;
1583
- if (repeatCount >= 2) {
1584
- yield { type: "error", message: "Agent loop detected: assistant produced identical output 3 turns in a row" };
1585
- return history;
1586
- }
1587
- } else {
1588
- repeatCount = 0;
1589
- lastAssistantSig = sig;
1590
- }
1591
- for (const u of tool_uses) yield { type: "tool-use", block: u };
1592
- const results = [];
1593
- for (const use of tool_uses) {
1594
- const tool = getTool(use.name);
1595
- if (!tool) {
1596
- const r2 = {
1597
- type: "tool_result",
1598
- tool_use_id: use.id,
1599
- content: `Unknown tool: ${use.name}`,
1600
- is_error: true
1601
- };
1602
- results.push(r2);
1603
- yield { type: "tool-result", block: r2 };
1604
- continue;
1605
- }
1606
- const invalid = validateInput(tool.input_schema, use.input);
1607
- if (invalid) {
1608
- const r2 = {
1609
- type: "tool_result",
1610
- tool_use_id: use.id,
1611
- content: `${invalid} for ${use.name}.`,
1612
- is_error: true
1613
- };
1614
- results.push(r2);
1615
- yield { type: "tool-result", block: r2 };
1616
- continue;
1617
- }
1618
- const decision = await check(use.name, use.input, permissions);
1619
- if (decision === "deny") {
1620
- const r2 = {
1621
- type: "tool_result",
1622
- tool_use_id: use.id,
1623
- content: `Permission denied for ${use.name}.`,
1624
- is_error: true
1625
- };
1626
- results.push(r2);
1627
- yield { type: "permission-denied", toolName: use.name, tool_use_id: use.id };
1628
- yield { type: "tool-result", block: r2 };
1629
- continue;
1630
- }
1631
- try {
1632
- await hooks?.firePre(use);
1633
- } catch {
1634
- }
1635
- let r;
1636
- try {
1637
- const out = await tool.handler(use.input);
1638
- r = {
1639
- type: "tool_result",
1640
- tool_use_id: use.id,
1641
- content: out.content,
1642
- is_error: out.is_error
1643
- };
1644
- } catch (err) {
1645
- r = {
1646
- type: "tool_result",
1647
- tool_use_id: use.id,
1648
- content: err instanceof Error ? err.message : String(err),
1649
- is_error: true
1650
- };
1651
- }
1652
- try {
1653
- await hooks?.firePost(use, r);
1654
- } catch {
1655
- }
1656
- results.push(r);
1657
- yield { type: "tool-result", block: r };
1658
- }
1659
- history.push({ role: "user", content: results });
1660
- yield { type: "turn-end", stop_reason: "tool_use" };
1661
- }
1662
- yield { type: "done", prompt_tokens: promptTokens, eval_tokens: evalTokens };
1663
- return history;
1925
+ const summary = summarizeInput(req.input);
1926
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, borderStyle: "round", borderColor: "blue", paddingX: 1, children: [
1927
+ /* @__PURE__ */ jsx9(Text9, { color: "blue", bold: true, children: "Tool use" }),
1928
+ /* @__PURE__ */ jsx9(Box9, { marginTop: 1, children: /* @__PURE__ */ jsxs9(Text9, { children: [
1929
+ "Allow ",
1930
+ /* @__PURE__ */ jsx9(Text9, { bold: true, children: label }),
1931
+ "?"
1932
+ ] }) }),
1933
+ summary && /* @__PURE__ */ jsx9(Box9, { marginLeft: 2, children: /* @__PURE__ */ jsx9(Text9, { dimColor: true, children: summary }) }),
1934
+ /* @__PURE__ */ jsx9(Box9, { flexDirection: "column", marginTop: 1, children: options.map((opt, i) => /* @__PURE__ */ jsxs9(Text9, { color: i === cursor ? "blue" : void 0, children: [
1935
+ i === cursor ? "\u276F " : " ",
1936
+ i + 1,
1937
+ ". ",
1938
+ opt.label
1939
+ ] }, opt.key)) })
1940
+ ] });
1941
+ }
1942
+ function ChatView({
1943
+ messages,
1944
+ streaming,
1945
+ streamingContent,
1946
+ thinking,
1947
+ thinkingContent,
1948
+ error,
1949
+ pendingPermission,
1950
+ permissionCursor = 0,
1951
+ activeToolUses,
1952
+ activeToolResults
1953
+ }) {
1954
+ const empty = messages.length === 0 && !streaming && !thinking && !pendingPermission && !error;
1955
+ return /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginLeft: 1, marginBottom: 1, children: [
1956
+ empty && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "column", marginBottom: 1, children: [
1957
+ /* @__PURE__ */ jsx9(Text9, { dimColor: true, children: EMPTY_STATE_TITLE }),
1958
+ EMPTY_STATE_HINTS.map((h, i) => /* @__PURE__ */ jsxs9(Text9, { dimColor: true, children: [
1959
+ " ",
1960
+ h
1961
+ ] }, i))
1962
+ ] }),
1963
+ messages.map(
1964
+ (msg, i) => msg.role === "user" ? /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
1965
+ /* @__PURE__ */ jsx9(Text9, { color: "blue", children: "\u25CF " }),
1966
+ /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: msg.content }) })
1967
+ ] }, i) : /* @__PURE__ */ jsx9(AssistantMessage, { msg }, i)
1968
+ ),
1969
+ thinking && /* @__PURE__ */ jsx9(ThinkingBlock, { content: thinkingContent }),
1970
+ streaming && streamingContent && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
1971
+ /* @__PURE__ */ jsx9(Text9, { color: "white", children: "\u25CF " }),
1972
+ /* @__PURE__ */ jsx9(Box9, { flexGrow: 1, children: /* @__PURE__ */ jsx9(Text9, { children: streamingContent }) })
1973
+ ] }),
1974
+ activeToolUses?.map((u) => {
1975
+ const r = activeToolResults?.find((x) => x.tool_use_id === u.id);
1976
+ return /* @__PURE__ */ jsx9(ToolUseLine, { use: u, result: r }, u.id);
1977
+ }),
1978
+ pendingPermission && /* @__PURE__ */ jsx9(PermissionPrompt, { req: pendingPermission, cursor: permissionCursor }),
1979
+ error && /* @__PURE__ */ jsxs9(Box9, { flexDirection: "row", marginBottom: 1, children: [
1980
+ /* @__PURE__ */ jsx9(Text9, { color: "red", children: "\u25CF " }),
1981
+ /* @__PURE__ */ jsx9(Text9, { color: "red", children: error })
1982
+ ] })
1983
+ ] });
1664
1984
  }
1665
1985
 
1666
1986
  // src/ui/hooks/useAgentRunner.ts
1987
+ init_loop();
1988
+ import { useState as useState3, useRef } from "react";
1667
1989
  var FLUSH_MS = 100;
1668
1990
  function useAgentRunner(model, activeCtx) {
1669
1991
  const [messages, setMessages] = useState3([]);
@@ -2311,4 +2633,10 @@ function App() {
2311
2633
  }
2312
2634
 
2313
2635
  // src/cli.tsx
2314
- render(createElement(App));
2636
+ var [, , cmd, ...rest] = process.argv;
2637
+ if (cmd === "doctor" || cmd === "eval") {
2638
+ const { runEval: runEval2 } = await Promise.resolve().then(() => (init_run(), run_exports));
2639
+ process.exit(await runEval2(rest));
2640
+ } else {
2641
+ render(createElement(App));
2642
+ }