kimaki 0.4.48 → 0.4.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -17,6 +17,15 @@ import http from 'node:http';
17
17
  import { setDataDir, getDataDir, getLockPort, setDefaultVerbosity } from './config.js';
18
18
  import { sanitizeAgentName } from './commands/agent.js';
19
19
  const cliLogger = createLogger(LogPrefix.CLI);
20
+ // Strip bracketed paste escape sequences from terminal input.
21
+ // iTerm2 and other terminals wrap pasted content with \x1b[200~ and \x1b[201~
22
+ // which can cause validation to fail on macOS. See: https://github.com/remorses/kimaki/issues/18
23
+ function stripBracketedPaste(value) {
24
+ if (!value) {
25
+ return '';
26
+ }
27
+ return value.replace(/\x1b\[200~/g, '').replace(/\x1b\[201~/g, '').trim();
28
+ }
20
29
  // Spawn caffeinate on macOS to prevent system sleep while bot is running.
21
30
  // Not detached, so it dies automatically with the parent process.
22
31
  function startCaffeinate() {
@@ -290,7 +299,7 @@ async function registerCommands({ token, appId, userCommands = [], agents = [],
290
299
  .setName('level')
291
300
  .setDescription('Verbosity level')
292
301
  .setRequired(true)
293
- .addChoices({ name: 'tools-and-text (default)', value: 'tools-and-text' }, { name: 'text-only', value: 'text-only' });
302
+ .addChoices({ name: 'tools-and-text (default)', value: 'tools-and-text' }, { name: 'text-and-essential-tools', value: 'text-and-essential-tools' }, { name: 'text-only', value: 'text-only' });
294
303
  return option;
295
304
  })
296
305
  .toJSON(),
@@ -499,17 +508,20 @@ async function run({ restart, addChannels, useWorktrees }) {
499
508
  message: 'Enter your Discord Application ID:',
500
509
  placeholder: 'e.g., 1234567890123456789',
501
510
  validate(value) {
502
- if (!value)
511
+ const cleaned = stripBracketedPaste(value);
512
+ if (!cleaned) {
503
513
  return 'Application ID is required';
504
- if (!/^\d{17,20}$/.test(value))
514
+ }
515
+ if (!/^\d{17,20}$/.test(cleaned)) {
505
516
  return 'Invalid Application ID format (should be 17-20 digits)';
517
+ }
506
518
  },
507
519
  });
508
520
  if (isCancel(appIdInput)) {
509
521
  cancel('Setup cancelled');
510
522
  process.exit(0);
511
523
  }
512
- appId = appIdInput;
524
+ appId = stripBracketedPaste(appIdInput);
513
525
  note('1. Go to the "Bot" section in the left sidebar\n' +
514
526
  '2. Scroll down to "Privileged Gateway Intents"\n' +
515
527
  '3. Enable these intents by toggling them ON:\n' +
@@ -530,33 +542,39 @@ async function run({ restart, addChannels, useWorktrees }) {
530
542
  const tokenInput = await password({
531
543
  message: 'Enter your Discord Bot Token (from "Bot" section - click "Reset Token" if needed):',
532
544
  validate(value) {
533
- if (!value)
545
+ const cleaned = stripBracketedPaste(value);
546
+ if (!cleaned) {
534
547
  return 'Bot token is required';
535
- if (value.length < 50)
548
+ }
549
+ if (cleaned.length < 50) {
536
550
  return 'Invalid token format (too short)';
551
+ }
537
552
  },
538
553
  });
539
554
  if (isCancel(tokenInput)) {
540
555
  cancel('Setup cancelled');
541
556
  process.exit(0);
542
557
  }
543
- token = tokenInput;
558
+ token = stripBracketedPaste(tokenInput);
544
559
  note(`You can get a Gemini api Key at https://aistudio.google.com/apikey`, `Gemini API Key`);
545
- const geminiApiKey = await password({
560
+ const geminiApiKeyInput = await password({
546
561
  message: 'Enter your Gemini API Key for voice channels and audio transcription (optional, press Enter to skip):',
547
562
  validate(value) {
548
- if (value && value.length < 10)
563
+ const cleaned = stripBracketedPaste(value);
564
+ if (cleaned && cleaned.length < 10) {
549
565
  return 'Invalid API key format';
566
+ }
550
567
  return undefined;
551
568
  },
552
569
  });
553
- if (isCancel(geminiApiKey)) {
570
+ if (isCancel(geminiApiKeyInput)) {
554
571
  cancel('Setup cancelled');
555
572
  process.exit(0);
556
573
  }
574
+ const geminiApiKey = stripBracketedPaste(geminiApiKeyInput) || null;
557
575
  // Store API key in database
558
576
  if (geminiApiKey) {
559
- db.prepare('INSERT OR REPLACE INTO bot_api_keys (app_id, gemini_api_key) VALUES (?, ?)').run(appId, geminiApiKey || null);
577
+ db.prepare('INSERT OR REPLACE INTO bot_api_keys (app_id, gemini_api_key) VALUES (?, ?)').run(appId, geminiApiKey);
560
578
  note('API key saved successfully', 'API Key Stored');
561
579
  }
562
580
  note(`Bot install URL:\n${generateBotInstallUrl({ clientId: appId })}\n\nYou MUST install the bot in your Discord server before continuing.`, 'Step 4: Install Bot to Server');
@@ -805,7 +823,7 @@ cli
805
823
  .option('--data-dir <path>', 'Data directory for config and database (default: ~/.kimaki)')
806
824
  .option('--install-url', 'Print the bot install URL and exit')
807
825
  .option('--use-worktrees', 'Create git worktrees for all new sessions started from channel messages')
808
- .option('--verbosity <level>', 'Default verbosity for all channels (tools-and-text or text-only)')
826
+ .option('--verbosity <level>', 'Default verbosity for all channels (tools-and-text, text-and-essential-tools, or text-only)')
809
827
  .action(async (options) => {
810
828
  try {
811
829
  // Set data directory early, before any database access
@@ -814,8 +832,9 @@ cli
814
832
  cliLogger.log(`Using data directory: ${getDataDir()}`);
815
833
  }
816
834
  if (options.verbosity) {
817
- if (options.verbosity !== 'tools-and-text' && options.verbosity !== 'text-only') {
818
- cliLogger.error(`Invalid verbosity level: ${options.verbosity}. Use "tools-and-text" or "text-only".`);
835
+ const validLevels = ['tools-and-text', 'text-and-essential-tools', 'text-only'];
836
+ if (!validLevels.includes(options.verbosity)) {
837
+ cliLogger.error(`Invalid verbosity level: ${options.verbosity}. Use one of: ${validLevels.join(', ')}`);
819
838
  process.exit(EXIT_NO_RESTART);
820
839
  }
821
840
  setDefaultVerbosity(options.verbosity);
@@ -43,9 +43,15 @@ export async function handleVerbosityCommand({ command, appId, }) {
43
43
  }
44
44
  setChannelVerbosity(channelId, level);
45
45
  verbosityLogger.log(`[VERBOSITY] Set channel ${channelId} to ${level}`);
46
- const description = level === 'text-only'
47
- ? 'Only text responses will be shown. Tool executions, status messages, and thinking will be hidden.'
48
- : 'All output will be shown, including tool executions and status messages.';
46
+ const description = (() => {
47
+ if (level === 'text-only') {
48
+ return 'Only text responses will be shown. Tool executions, status messages, and thinking will be hidden.';
49
+ }
50
+ if (level === 'text-and-essential-tools') {
51
+ return 'Text responses and essential tools (edits, custom MCP tools) will be shown. Read, search, and navigation tools will be hidden.';
52
+ }
53
+ return 'All output will be shown, including tool executions and status messages.';
54
+ })();
49
55
  await command.reply({
50
56
  content: `Verbosity set to **${level}** for this channel.\n${description}\nThis is a per-channel setting and applies immediately, including any active sessions.`,
51
57
  ephemeral: true,
@@ -0,0 +1,107 @@
1
+ // Image processing utilities for Discord attachments.
2
+ // Uses sharp (optional) to resize large images and heic-convert (optional) for HEIC support.
3
+ // Falls back gracefully if dependencies are not available.
4
+ import { createLogger, LogPrefix } from './logger.js';
5
+ const logger = createLogger(LogPrefix.FORMATTING);
6
+ const MAX_DIMENSION = 1500;
7
+ const HEIC_MIME_TYPES = ['image/heic', 'image/heif', 'image/heic-sequence', 'image/heif-sequence'];
8
+ let sharpModule = undefined;
9
+ let heicConvertModule = undefined;
10
+ async function tryLoadSharp() {
11
+ if (sharpModule !== undefined) {
12
+ return sharpModule;
13
+ }
14
+ try {
15
+ sharpModule = (await import('sharp')).default;
16
+ logger.log('sharp loaded successfully');
17
+ return sharpModule;
18
+ }
19
+ catch {
20
+ logger.log('sharp not available, images will be sent at original size');
21
+ sharpModule = null;
22
+ return null;
23
+ }
24
+ }
25
+ async function tryLoadHeicConvert() {
26
+ if (heicConvertModule !== undefined) {
27
+ return heicConvertModule;
28
+ }
29
+ try {
30
+ const mod = await import('heic-convert');
31
+ heicConvertModule = mod.default;
32
+ logger.log('heic-convert loaded successfully');
33
+ return heicConvertModule;
34
+ }
35
+ catch {
36
+ logger.log('heic-convert not available, HEIC images will be sent as-is');
37
+ heicConvertModule = null;
38
+ return null;
39
+ }
40
+ }
41
+ function isHeicMime(mime) {
42
+ return HEIC_MIME_TYPES.includes(mime.toLowerCase());
43
+ }
44
+ export async function processImage(buffer, mime) {
45
+ // Skip non-images (PDFs, etc.)
46
+ if (!mime.startsWith('image/')) {
47
+ return { buffer, mime };
48
+ }
49
+ let workingBuffer = buffer;
50
+ let workingMime = mime;
51
+ // Handle HEIC conversion first (before sharp, since sharp doesn't support HEIC)
52
+ if (isHeicMime(mime)) {
53
+ const heicConvert = await tryLoadHeicConvert();
54
+ if (heicConvert) {
55
+ try {
56
+ const outputArrayBuffer = await heicConvert({
57
+ buffer: workingBuffer.buffer.slice(workingBuffer.byteOffset, workingBuffer.byteOffset + workingBuffer.byteLength),
58
+ format: 'JPEG',
59
+ quality: 0.85,
60
+ });
61
+ workingBuffer = Buffer.from(outputArrayBuffer);
62
+ workingMime = 'image/jpeg';
63
+ logger.log(`Converted HEIC to JPEG (${buffer.length} → ${workingBuffer.length} bytes)`);
64
+ }
65
+ catch (error) {
66
+ logger.error('Failed to convert HEIC, sending original:', error);
67
+ return { buffer, mime };
68
+ }
69
+ }
70
+ else {
71
+ // No heic-convert available, return original (LLM might not support it)
72
+ logger.log('HEIC image detected but heic-convert not available, sending as-is');
73
+ return { buffer, mime };
74
+ }
75
+ }
76
+ // Now process with sharp (resize + ensure JPEG output)
77
+ const sharp = await tryLoadSharp();
78
+ if (!sharp) {
79
+ return { buffer: workingBuffer, mime: workingMime };
80
+ }
81
+ try {
82
+ const image = sharp(workingBuffer);
83
+ const metadata = await image.metadata();
84
+ const { width, height } = metadata;
85
+ const needsResize = width && height && (width > MAX_DIMENSION || height > MAX_DIMENSION);
86
+ if (!needsResize) {
87
+ // Still convert to JPEG for consistency (unless already JPEG from HEIC conversion)
88
+ const outputBuffer = await image.jpeg({ quality: 85 }).toBuffer();
89
+ logger.log(`Converted image to JPEG: ${width}x${height} (${outputBuffer.length} bytes)`);
90
+ return { buffer: outputBuffer, mime: 'image/jpeg' };
91
+ }
92
+ // Resize and convert to JPEG
93
+ const outputBuffer = await image
94
+ .resize(MAX_DIMENSION, MAX_DIMENSION, {
95
+ fit: 'inside',
96
+ withoutEnlargement: true,
97
+ })
98
+ .jpeg({ quality: 85 })
99
+ .toBuffer();
100
+ logger.log(`Resized image: ${width}x${height} → max ${MAX_DIMENSION}px (${outputBuffer.length} bytes)`);
101
+ return { buffer: outputBuffer, mime: 'image/jpeg' };
102
+ }
103
+ catch (error) {
104
+ logger.error('Failed to process image with sharp, using working buffer:', error);
105
+ return { buffer: workingBuffer, mime: workingMime };
106
+ }
107
+ }
@@ -1,12 +1,10 @@
1
1
  // OpenCode message part formatting for Discord.
2
2
  // Converts SDK message parts (text, tools, reasoning) to Discord-friendly format,
3
3
  // handles file attachments, and provides tool summary generation.
4
- import fs from 'node:fs';
5
- import path from 'node:path';
6
4
  import * as errore from 'errore';
7
5
  import { createLogger, LogPrefix } from './logger.js';
8
6
  import { FetchError } from './errors.js';
9
- const ATTACHMENTS_DIR = path.join(process.cwd(), 'tmp', 'discord-attachments');
7
+ import { processImage } from './image-utils.js';
10
8
  const logger = createLogger(LogPrefix.FORMATTING);
11
9
  /**
12
10
  * Escapes Discord inline markdown characters so dynamic content
@@ -148,10 +146,6 @@ export async function getFileAttachments(message) {
148
146
  if (fileAttachments.length === 0) {
149
147
  return [];
150
148
  }
151
- // ensure tmp directory exists
152
- if (!fs.existsSync(ATTACHMENTS_DIR)) {
153
- fs.mkdirSync(ATTACHMENTS_DIR, { recursive: true });
154
- }
155
149
  const results = await Promise.all(fileAttachments.map(async (attachment) => {
156
150
  const response = await errore.tryAsync({
157
151
  try: () => fetch(attachment.url),
@@ -165,15 +159,18 @@ export async function getFileAttachments(message) {
165
159
  logger.error(`Failed to fetch attachment ${attachment.name}: ${response.status}`);
166
160
  return null;
167
161
  }
168
- const buffer = Buffer.from(await response.arrayBuffer());
169
- const localPath = path.join(ATTACHMENTS_DIR, `${message.id}-${attachment.name}`);
170
- fs.writeFileSync(localPath, buffer);
171
- logger.log(`Downloaded attachment to ${localPath}`);
162
+ const rawBuffer = Buffer.from(await response.arrayBuffer());
163
+ const originalMime = attachment.contentType || 'application/octet-stream';
164
+ // Process image (resize if needed, convert to JPEG)
165
+ const { buffer, mime } = await processImage(rawBuffer, originalMime);
166
+ const base64 = buffer.toString('base64');
167
+ const dataUrl = `data:${mime};base64,${base64}`;
168
+ logger.log(`Attachment ${attachment.name}: ${rawBuffer.length} → ${buffer.length} bytes, ${mime}`);
172
169
  return {
173
170
  type: 'file',
174
- mime: attachment.contentType || 'application/octet-stream',
171
+ mime,
175
172
  filename: attachment.name,
176
- url: localPath,
173
+ url: dataUrl,
177
174
  };
178
175
  }));
179
176
  return results.filter((r) => r !== null);
@@ -16,6 +16,21 @@ const sessionLogger = createLogger(LogPrefix.SESSION);
16
16
  const voiceLogger = createLogger(LogPrefix.VOICE);
17
17
  const discordLogger = createLogger(LogPrefix.DISCORD);
18
18
  export const abortControllers = new Map();
19
+ // Built-in tools that are hidden in text-and-essential-tools verbosity mode.
20
+ // Essential tools (edits, bash, todos, tasks, custom MCP tools) are shown; these navigation/read tools are hidden.
21
+ const NON_ESSENTIAL_TOOLS = new Set([
22
+ 'read',
23
+ 'list',
24
+ 'glob',
25
+ 'grep',
26
+ 'todoread',
27
+ 'skill',
28
+ 'question',
29
+ 'webfetch',
30
+ ]);
31
+ function isEssentialTool(toolName) {
32
+ return !NON_ESSENTIAL_TOOLS.has(toolName);
33
+ }
19
34
  // Track multiple pending permissions per thread (keyed by permission ID)
20
35
  // OpenCode handles blocking/sequencing - we just need to track all pending permissions
21
36
  // to avoid duplicates and properly clean up on auto-reject
@@ -326,10 +341,23 @@ export async function handleOpencodeSession({ prompt, thread, projectDirectory,
326
341
  return getChannelVerbosity(verbosityChannelId);
327
342
  };
328
343
  const sendPartMessage = async (part) => {
344
+ const verbosity = getVerbosity();
329
345
  // In text-only mode, only send text parts (the ⬥ diamond messages)
330
- if (getVerbosity() === 'text-only' && part.type !== 'text') {
346
+ if (verbosity === 'text-only' && part.type !== 'text') {
331
347
  return;
332
348
  }
349
+ // In text-and-essential-tools mode, show text + essential tools (edits, custom MCP tools)
350
+ if (verbosity === 'text-and-essential-tools') {
351
+ if (part.type === 'text') {
352
+ // text is always shown
353
+ }
354
+ else if (part.type === 'tool' && isEssentialTool(part.tool)) {
355
+ // essential tools are shown
356
+ }
357
+ else {
358
+ return;
359
+ }
360
+ }
333
361
  const content = formatPart(part) + '\n\n';
334
362
  if (!content.trim() || content.length === 0) {
335
363
  // discordLogger.log(`SKIP: Part ${part.id} has no content`)
@@ -487,7 +515,7 @@ export async function handleOpencodeSession({ prompt, thread, projectDirectory,
487
515
  agentSpawnCounts[agent] = (agentSpawnCounts[agent] || 0) + 1;
488
516
  const label = `${agent}-${agentSpawnCounts[agent]}`;
489
517
  subtaskSessions.set(childSessionId, { label, assistantMessageId: undefined });
490
- // Skip task messages in text-only mode
518
+ // Show task messages in tools-and-text and text-and-essential-tools modes
491
519
  if (getVerbosity() !== 'text-only') {
492
520
  const taskDisplay = `┣ task **${label}** _${description}_`;
493
521
  await sendThreadMessage(thread, taskDisplay + '\n\n');
@@ -497,24 +525,37 @@ export async function handleOpencodeSession({ prompt, thread, projectDirectory,
497
525
  }
498
526
  return;
499
527
  }
500
- if (part.type === 'tool' && part.state.status === 'completed' && getVerbosity() !== 'text-only') {
501
- const output = part.state.output || '';
502
- const outputTokens = Math.ceil(output.length / 4);
503
- const largeOutputThreshold = 3000;
504
- if (outputTokens >= largeOutputThreshold) {
505
- const formattedTokens = outputTokens >= 1000 ? `${(outputTokens / 1000).toFixed(1)}k` : String(outputTokens);
506
- const percentageSuffix = (() => {
507
- if (!modelContextLimit) {
508
- return '';
509
- }
510
- const pct = (outputTokens / modelContextLimit) * 100;
511
- if (pct < 1) {
512
- return '';
513
- }
514
- return ` (${pct.toFixed(1)}%)`;
515
- })();
516
- const chunk = `⬦ ${part.tool} returned ${formattedTokens} tokens${percentageSuffix}`;
517
- await thread.send({ content: chunk, flags: SILENT_MESSAGE_FLAGS });
528
+ // Show large output notifications for tools that are visible in current verbosity mode
529
+ if (part.type === 'tool' && part.state.status === 'completed') {
530
+ const showLargeOutput = (() => {
531
+ const verbosity = getVerbosity();
532
+ if (verbosity === 'text-only') {
533
+ return false;
534
+ }
535
+ if (verbosity === 'text-and-essential-tools') {
536
+ return isEssentialTool(part.tool);
537
+ }
538
+ return true;
539
+ })();
540
+ if (showLargeOutput) {
541
+ const output = part.state.output || '';
542
+ const outputTokens = Math.ceil(output.length / 4);
543
+ const largeOutputThreshold = 3000;
544
+ if (outputTokens >= largeOutputThreshold) {
545
+ const formattedTokens = outputTokens >= 1000 ? `${(outputTokens / 1000).toFixed(1)}k` : String(outputTokens);
546
+ const percentageSuffix = (() => {
547
+ if (!modelContextLimit) {
548
+ return '';
549
+ }
550
+ const pct = (outputTokens / modelContextLimit) * 100;
551
+ if (pct < 1) {
552
+ return '';
553
+ }
554
+ return ` (${pct.toFixed(1)}%)`;
555
+ })();
556
+ const chunk = `⬦ ${part.tool} returned ${formattedTokens} tokens${percentageSuffix}`;
557
+ await thread.send({ content: chunk, flags: SILENT_MESSAGE_FLAGS });
558
+ }
518
559
  }
519
560
  }
520
561
  if (part.type === 'reasoning') {
@@ -542,10 +583,17 @@ export async function handleOpencodeSession({ prompt, thread, projectDirectory,
542
583
  }
543
584
  };
544
585
  const handleSubtaskPart = async (part, subtaskInfo) => {
586
+ const verbosity = getVerbosity();
545
587
  // In text-only mode, skip all subtask output (they're tool-related)
546
- if (getVerbosity() === 'text-only') {
588
+ if (verbosity === 'text-only') {
547
589
  return;
548
590
  }
591
+ // In text-and-essential-tools mode, only show essential tools from subtasks
592
+ if (verbosity === 'text-and-essential-tools') {
593
+ if (part.type !== 'tool' || !isEssentialTool(part.tool)) {
594
+ return;
595
+ }
596
+ }
549
597
  if (part.type === 'step-start' || part.type === 'step-finish') {
550
598
  return;
551
599
  }
@@ -943,10 +991,11 @@ export async function handleOpencodeSession({ prompt, thread, projectDirectory,
943
991
  sessionLogger.log(`[PROMPT] Sending ${images.length} image(s):`, images.map((img) => ({
944
992
  mime: img.mime,
945
993
  filename: img.filename,
946
- url: img.url.slice(0, 100),
994
+ urlPreview: img.url.slice(0, 50) + '...',
947
995
  })));
948
- const imagePathsList = images.map((img) => `- ${img.filename}: ${img.url}`).join('\n');
949
- return `${prompt}\n\n**attached images:**\n${imagePathsList}`;
996
+ // Just list filenames, not the full base64 URLs (images are passed as separate parts)
997
+ const imageList = images.map((img) => `- ${img.filename}`).join('\n');
998
+ return `${prompt}\n\n**attached images:**\n${imageList}`;
950
999
  })();
951
1000
  const parts = [{ type: 'text', text: promptWithImagePaths }, ...images];
952
1001
  sessionLogger.log(`[PROMPT] Parts to send:`, parts.length);
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "kimaki",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.4.48",
5
+ "version": "0.4.50",
6
6
  "repository": "https://github.com/remorses/kimaki",
7
7
  "bin": "bin.js",
8
8
  "files": [
@@ -14,6 +14,7 @@
14
14
  "@opencode-ai/plugin": "^1.1.12",
15
15
  "@types/better-sqlite3": "^7.6.13",
16
16
  "@types/bun": "latest",
17
+ "@types/heic-convert": "^2.1.0",
17
18
  "@types/js-yaml": "^4.0.9",
18
19
  "@types/ms": "^2.1.0",
19
20
  "@types/node": "^24.3.0",
@@ -45,7 +46,9 @@
45
46
  },
46
47
  "optionalDependencies": {
47
48
  "@discordjs/opus": "^0.10.0",
48
- "prism-media": "^1.3.5"
49
+ "heic-convert": "^2.1.0",
50
+ "prism-media": "^1.3.5",
51
+ "sharp": "^0.34.5"
49
52
  },
50
53
  "scripts": {
51
54
  "dev": "tsx --env-file .env src/cli.ts",
package/src/cli.ts CHANGED
@@ -52,6 +52,16 @@ import { sanitizeAgentName } from './commands/agent.js'
52
52
 
53
53
  const cliLogger = createLogger(LogPrefix.CLI)
54
54
 
55
+ // Strip bracketed paste escape sequences from terminal input.
56
+ // iTerm2 and other terminals wrap pasted content with \x1b[200~ and \x1b[201~
57
+ // which can cause validation to fail on macOS. See: https://github.com/remorses/kimaki/issues/18
58
+ function stripBracketedPaste(value: string | undefined): string {
59
+ if (!value) {
60
+ return ''
61
+ }
62
+ return value.replace(/\x1b\[200~/g, '').replace(/\x1b\[201~/g, '').trim()
63
+ }
64
+
55
65
  // Spawn caffeinate on macOS to prevent system sleep while bot is running.
56
66
  // Not detached, so it dies automatically with the parent process.
57
67
  function startCaffeinate() {
@@ -380,6 +390,7 @@ async function registerCommands({
380
390
  .setRequired(true)
381
391
  .addChoices(
382
392
  { name: 'tools-and-text (default)', value: 'tools-and-text' },
393
+ { name: 'text-and-essential-tools', value: 'text-and-essential-tools' },
383
394
  { name: 'text-only', value: 'text-only' },
384
395
  )
385
396
  return option
@@ -696,9 +707,13 @@ async function run({ restart, addChannels, useWorktrees }: CliOptions) {
696
707
  message: 'Enter your Discord Application ID:',
697
708
  placeholder: 'e.g., 1234567890123456789',
698
709
  validate(value) {
699
- if (!value) return 'Application ID is required'
700
- if (!/^\d{17,20}$/.test(value))
710
+ const cleaned = stripBracketedPaste(value)
711
+ if (!cleaned) {
712
+ return 'Application ID is required'
713
+ }
714
+ if (!/^\d{17,20}$/.test(cleaned)) {
701
715
  return 'Invalid Application ID format (should be 17-20 digits)'
716
+ }
702
717
  },
703
718
  })
704
719
 
@@ -706,7 +721,7 @@ async function run({ restart, addChannels, useWorktrees }: CliOptions) {
706
721
  cancel('Setup cancelled')
707
722
  process.exit(0)
708
723
  }
709
- appId = appIdInput
724
+ appId = stripBracketedPaste(appIdInput)
710
725
 
711
726
  note(
712
727
  '1. Go to the "Bot" section in the left sidebar\n' +
@@ -737,8 +752,13 @@ async function run({ restart, addChannels, useWorktrees }: CliOptions) {
737
752
  const tokenInput = await password({
738
753
  message: 'Enter your Discord Bot Token (from "Bot" section - click "Reset Token" if needed):',
739
754
  validate(value) {
740
- if (!value) return 'Bot token is required'
741
- if (value.length < 50) return 'Invalid token format (too short)'
755
+ const cleaned = stripBracketedPaste(value)
756
+ if (!cleaned) {
757
+ return 'Bot token is required'
758
+ }
759
+ if (cleaned.length < 50) {
760
+ return 'Invalid token format (too short)'
761
+ }
742
762
  },
743
763
  })
744
764
 
@@ -746,29 +766,34 @@ async function run({ restart, addChannels, useWorktrees }: CliOptions) {
746
766
  cancel('Setup cancelled')
747
767
  process.exit(0)
748
768
  }
749
- token = tokenInput
769
+ token = stripBracketedPaste(tokenInput)
750
770
 
751
771
  note(`You can get a Gemini api Key at https://aistudio.google.com/apikey`, `Gemini API Key`)
752
772
 
753
- const geminiApiKey = await password({
773
+ const geminiApiKeyInput = await password({
754
774
  message:
755
775
  'Enter your Gemini API Key for voice channels and audio transcription (optional, press Enter to skip):',
756
776
  validate(value) {
757
- if (value && value.length < 10) return 'Invalid API key format'
777
+ const cleaned = stripBracketedPaste(value)
778
+ if (cleaned && cleaned.length < 10) {
779
+ return 'Invalid API key format'
780
+ }
758
781
  return undefined
759
782
  },
760
783
  })
761
784
 
762
- if (isCancel(geminiApiKey)) {
785
+ if (isCancel(geminiApiKeyInput)) {
763
786
  cancel('Setup cancelled')
764
787
  process.exit(0)
765
788
  }
766
789
 
790
+ const geminiApiKey = stripBracketedPaste(geminiApiKeyInput) || null
791
+
767
792
  // Store API key in database
768
793
  if (geminiApiKey) {
769
794
  db.prepare('INSERT OR REPLACE INTO bot_api_keys (app_id, gemini_api_key) VALUES (?, ?)').run(
770
795
  appId,
771
- geminiApiKey || null,
796
+ geminiApiKey,
772
797
  )
773
798
  note('API key saved successfully', 'API Key Stored')
774
799
  }
@@ -1098,7 +1123,7 @@ cli
1098
1123
  .option('--data-dir <path>', 'Data directory for config and database (default: ~/.kimaki)')
1099
1124
  .option('--install-url', 'Print the bot install URL and exit')
1100
1125
  .option('--use-worktrees', 'Create git worktrees for all new sessions started from channel messages')
1101
- .option('--verbosity <level>', 'Default verbosity for all channels (tools-and-text or text-only)')
1126
+ .option('--verbosity <level>', 'Default verbosity for all channels (tools-and-text, text-and-essential-tools, or text-only)')
1102
1127
  .action(
1103
1128
  async (options: {
1104
1129
  restart?: boolean
@@ -1116,11 +1141,12 @@ cli
1116
1141
  }
1117
1142
 
1118
1143
  if (options.verbosity) {
1119
- if (options.verbosity !== 'tools-and-text' && options.verbosity !== 'text-only') {
1120
- cliLogger.error(`Invalid verbosity level: ${options.verbosity}. Use "tools-and-text" or "text-only".`)
1144
+ const validLevels = ['tools-and-text', 'text-and-essential-tools', 'text-only']
1145
+ if (!validLevels.includes(options.verbosity)) {
1146
+ cliLogger.error(`Invalid verbosity level: ${options.verbosity}. Use one of: ${validLevels.join(', ')}`)
1121
1147
  process.exit(EXIT_NO_RESTART)
1122
1148
  }
1123
- setDefaultVerbosity(options.verbosity)
1149
+ setDefaultVerbosity(options.verbosity as 'tools-and-text' | 'text-and-essential-tools' | 'text-only')
1124
1150
  cliLogger.log(`Default verbosity: ${options.verbosity}`)
1125
1151
  }
1126
1152
 
@@ -60,9 +60,15 @@ export async function handleVerbosityCommand({
60
60
  setChannelVerbosity(channelId, level)
61
61
  verbosityLogger.log(`[VERBOSITY] Set channel ${channelId} to ${level}`)
62
62
 
63
- const description = level === 'text-only'
64
- ? 'Only text responses will be shown. Tool executions, status messages, and thinking will be hidden.'
65
- : 'All output will be shown, including tool executions and status messages.'
63
+ const description = (() => {
64
+ if (level === 'text-only') {
65
+ return 'Only text responses will be shown. Tool executions, status messages, and thinking will be hidden.'
66
+ }
67
+ if (level === 'text-and-essential-tools') {
68
+ return 'Text responses and essential tools (edits, custom MCP tools) will be shown. Read, search, and navigation tools will be hidden.'
69
+ }
70
+ return 'All output will be shown, including tool executions and status messages.'
71
+ })()
66
72
 
67
73
  await command.reply({
68
74
  content: `Verbosity set to **${level}** for this channel.\n${description}\nThis is a per-channel setting and applies immediately, including any active sessions.`,
package/src/database.ts CHANGED
@@ -363,7 +363,10 @@ export function runWorktreeSettingsMigrations(database?: Database.Database): voi
363
363
  }
364
364
 
365
365
  // Verbosity levels for controlling output detail
366
- export type VerbosityLevel = 'tools-and-text' | 'text-only'
366
+ // - tools-and-text: shows all output including tool executions
367
+ // - text-and-essential-tools: shows text + edits + custom MCP tools, hides read/search/navigation tools
368
+ // - text-only: only shows text responses (⬥ diamond parts)
369
+ export type VerbosityLevel = 'tools-and-text' | 'text-and-essential-tools' | 'text-only'
367
370
 
368
371
  export function runVerbosityMigrations(database?: Database.Database): void {
369
372
  const targetDb = database || getDatabase()
@@ -0,0 +1,132 @@
1
+ // Image processing utilities for Discord attachments.
2
+ // Uses sharp (optional) to resize large images and heic-convert (optional) for HEIC support.
3
+ // Falls back gracefully if dependencies are not available.
4
+
5
+ import { createLogger, LogPrefix } from './logger.js'
6
+
7
+ const logger = createLogger(LogPrefix.FORMATTING)
8
+
9
+ const MAX_DIMENSION = 1500
10
+ const HEIC_MIME_TYPES = ['image/heic', 'image/heif', 'image/heic-sequence', 'image/heif-sequence']
11
+
12
+ type SharpModule = typeof import('sharp')
13
+ type HeicConvertFn = (options: {
14
+ buffer: ArrayBufferLike
15
+ format: 'JPEG' | 'PNG'
16
+ quality?: number
17
+ }) => Promise<ArrayBuffer>
18
+
19
+ let sharpModule: SharpModule | null | undefined = undefined
20
+ let heicConvertModule: HeicConvertFn | null | undefined = undefined
21
+
22
+ async function tryLoadSharp(): Promise<SharpModule | null> {
23
+ if (sharpModule !== undefined) {
24
+ return sharpModule
25
+ }
26
+ try {
27
+ sharpModule = (await import('sharp')).default as unknown as SharpModule
28
+ logger.log('sharp loaded successfully')
29
+ return sharpModule
30
+ } catch {
31
+ logger.log('sharp not available, images will be sent at original size')
32
+ sharpModule = null
33
+ return null
34
+ }
35
+ }
36
+
37
+ async function tryLoadHeicConvert(): Promise<HeicConvertFn | null> {
38
+ if (heicConvertModule !== undefined) {
39
+ return heicConvertModule
40
+ }
41
+ try {
42
+ const mod = await import('heic-convert')
43
+ heicConvertModule = mod.default as HeicConvertFn
44
+ logger.log('heic-convert loaded successfully')
45
+ return heicConvertModule
46
+ } catch {
47
+ logger.log('heic-convert not available, HEIC images will be sent as-is')
48
+ heicConvertModule = null
49
+ return null
50
+ }
51
+ }
52
+
53
+ function isHeicMime(mime: string): boolean {
54
+ return HEIC_MIME_TYPES.includes(mime.toLowerCase())
55
+ }
56
+
57
+ export async function processImage(
58
+ buffer: Buffer,
59
+ mime: string,
60
+ ): Promise<{ buffer: Buffer; mime: string }> {
61
+ // Skip non-images (PDFs, etc.)
62
+ if (!mime.startsWith('image/')) {
63
+ return { buffer, mime }
64
+ }
65
+
66
+ let workingBuffer = buffer
67
+ let workingMime = mime
68
+
69
+ // Handle HEIC conversion first (before sharp, since sharp doesn't support HEIC)
70
+ if (isHeicMime(mime)) {
71
+ const heicConvert = await tryLoadHeicConvert()
72
+ if (heicConvert) {
73
+ try {
74
+ const outputArrayBuffer = await heicConvert({
75
+ buffer: workingBuffer.buffer.slice(
76
+ workingBuffer.byteOffset,
77
+ workingBuffer.byteOffset + workingBuffer.byteLength,
78
+ ),
79
+ format: 'JPEG',
80
+ quality: 0.85,
81
+ })
82
+ workingBuffer = Buffer.from(outputArrayBuffer)
83
+ workingMime = 'image/jpeg'
84
+ logger.log(`Converted HEIC to JPEG (${buffer.length} → ${workingBuffer.length} bytes)`)
85
+ } catch (error) {
86
+ logger.error('Failed to convert HEIC, sending original:', error)
87
+ return { buffer, mime }
88
+ }
89
+ } else {
90
+ // No heic-convert available, return original (LLM might not support it)
91
+ logger.log('HEIC image detected but heic-convert not available, sending as-is')
92
+ return { buffer, mime }
93
+ }
94
+ }
95
+
96
+ // Now process with sharp (resize + ensure JPEG output)
97
+ const sharp = await tryLoadSharp()
98
+ if (!sharp) {
99
+ return { buffer: workingBuffer, mime: workingMime }
100
+ }
101
+
102
+ try {
103
+ const image = sharp(workingBuffer)
104
+ const metadata = await image.metadata()
105
+ const { width, height } = metadata
106
+
107
+ const needsResize = width && height && (width > MAX_DIMENSION || height > MAX_DIMENSION)
108
+
109
+ if (!needsResize) {
110
+ // Still convert to JPEG for consistency (unless already JPEG from HEIC conversion)
111
+ const outputBuffer = await image.jpeg({ quality: 85 }).toBuffer()
112
+ logger.log(`Converted image to JPEG: ${width}x${height} (${outputBuffer.length} bytes)`)
113
+ return { buffer: outputBuffer, mime: 'image/jpeg' }
114
+ }
115
+
116
+ // Resize and convert to JPEG
117
+ const outputBuffer = await image
118
+ .resize(MAX_DIMENSION, MAX_DIMENSION, {
119
+ fit: 'inside',
120
+ withoutEnlargement: true,
121
+ })
122
+ .jpeg({ quality: 85 })
123
+ .toBuffer()
124
+
125
+ logger.log(`Resized image: ${width}x${height} → max ${MAX_DIMENSION}px (${outputBuffer.length} bytes)`)
126
+
127
+ return { buffer: outputBuffer, mime: 'image/jpeg' }
128
+ } catch (error) {
129
+ logger.error('Failed to process image with sharp, using working buffer:', error)
130
+ return { buffer: workingBuffer, mime: workingMime }
131
+ }
132
+ }
@@ -5,11 +5,10 @@
5
5
  import type { Part } from '@opencode-ai/sdk/v2'
6
6
  import type { FilePartInput } from '@opencode-ai/sdk'
7
7
  import type { Message } from 'discord.js'
8
- import fs from 'node:fs'
9
- import path from 'node:path'
10
8
  import * as errore from 'errore'
11
9
  import { createLogger, LogPrefix } from './logger.js'
12
10
  import { FetchError } from './errors.js'
11
+ import { processImage } from './image-utils.js'
13
12
 
14
13
  // Generic message type compatible with both v1 and v2 SDK
15
14
  type GenericSessionMessage = {
@@ -17,8 +16,6 @@ type GenericSessionMessage = {
17
16
  parts: Part[]
18
17
  }
19
18
 
20
- const ATTACHMENTS_DIR = path.join(process.cwd(), 'tmp', 'discord-attachments')
21
-
22
19
  const logger = createLogger(LogPrefix.FORMATTING)
23
20
 
24
21
  /**
@@ -192,11 +189,6 @@ export async function getFileAttachments(message: Message): Promise<FilePartInpu
192
189
  return []
193
190
  }
194
191
 
195
- // ensure tmp directory exists
196
- if (!fs.existsSync(ATTACHMENTS_DIR)) {
197
- fs.mkdirSync(ATTACHMENTS_DIR, { recursive: true })
198
- }
199
-
200
192
  const results = await Promise.all(
201
193
  fileAttachments.map(async (attachment) => {
202
194
  const response = await errore.tryAsync({
@@ -212,17 +204,22 @@ export async function getFileAttachments(message: Message): Promise<FilePartInpu
212
204
  return null
213
205
  }
214
206
 
215
- const buffer = Buffer.from(await response.arrayBuffer())
216
- const localPath = path.join(ATTACHMENTS_DIR, `${message.id}-${attachment.name}`)
217
- fs.writeFileSync(localPath, buffer)
207
+ const rawBuffer = Buffer.from(await response.arrayBuffer())
208
+ const originalMime = attachment.contentType || 'application/octet-stream'
209
+
210
+ // Process image (resize if needed, convert to JPEG)
211
+ const { buffer, mime } = await processImage(rawBuffer, originalMime)
212
+
213
+ const base64 = buffer.toString('base64')
214
+ const dataUrl = `data:${mime};base64,${base64}`
218
215
 
219
- logger.log(`Downloaded attachment to ${localPath}`)
216
+ logger.log(`Attachment ${attachment.name}: ${rawBuffer.length} ${buffer.length} bytes, ${mime}`)
220
217
 
221
218
  return {
222
219
  type: 'file' as const,
223
- mime: attachment.contentType || 'application/octet-stream',
220
+ mime,
224
221
  filename: attachment.name,
225
- url: localPath,
222
+ url: dataUrl,
226
223
  }
227
224
  }),
228
225
  )
@@ -44,6 +44,23 @@ const discordLogger = createLogger(LogPrefix.DISCORD)
44
44
 
45
45
  export const abortControllers = new Map<string, AbortController>()
46
46
 
47
+ // Built-in tools that are hidden in text-and-essential-tools verbosity mode.
48
+ // Essential tools (edits, bash, todos, tasks, custom MCP tools) are shown; these navigation/read tools are hidden.
49
+ const NON_ESSENTIAL_TOOLS = new Set([
50
+ 'read',
51
+ 'list',
52
+ 'glob',
53
+ 'grep',
54
+ 'todoread',
55
+ 'skill',
56
+ 'question',
57
+ 'webfetch',
58
+ ])
59
+
60
+ function isEssentialTool(toolName: string): boolean {
61
+ return !NON_ESSENTIAL_TOOLS.has(toolName)
62
+ }
63
+
47
64
  // Track multiple pending permissions per thread (keyed by permission ID)
48
65
  // OpenCode handles blocking/sequencing - we just need to track all pending permissions
49
66
  // to avoid duplicates and properly clean up on auto-reject
@@ -484,10 +501,21 @@ export async function handleOpencodeSession({
484
501
  }
485
502
 
486
503
  const sendPartMessage = async (part: Part) => {
504
+ const verbosity = getVerbosity()
487
505
  // In text-only mode, only send text parts (the ⬥ diamond messages)
488
- if (getVerbosity() === 'text-only' && part.type !== 'text') {
506
+ if (verbosity === 'text-only' && part.type !== 'text') {
489
507
  return
490
508
  }
509
+ // In text-and-essential-tools mode, show text + essential tools (edits, custom MCP tools)
510
+ if (verbosity === 'text-and-essential-tools') {
511
+ if (part.type === 'text') {
512
+ // text is always shown
513
+ } else if (part.type === 'tool' && isEssentialTool(part.tool)) {
514
+ // essential tools are shown
515
+ } else {
516
+ return
517
+ }
518
+ }
491
519
 
492
520
  const content = formatPart(part) + '\n\n'
493
521
  if (!content.trim() || content.length === 0) {
@@ -697,7 +725,7 @@ export async function handleOpencodeSession({
697
725
  agentSpawnCounts[agent] = (agentSpawnCounts[agent] || 0) + 1
698
726
  const label = `${agent}-${agentSpawnCounts[agent]}`
699
727
  subtaskSessions.set(childSessionId, { label, assistantMessageId: undefined })
700
- // Skip task messages in text-only mode
728
+ // Show task messages in tools-and-text and text-and-essential-tools modes
701
729
  if (getVerbosity() !== 'text-only') {
702
730
  const taskDisplay = `┣ task **${label}** _${description}_`
703
731
  await sendThreadMessage(thread, taskDisplay + '\n\n')
@@ -708,25 +736,38 @@ export async function handleOpencodeSession({
708
736
  return
709
737
  }
710
738
 
711
- if (part.type === 'tool' && part.state.status === 'completed' && getVerbosity() !== 'text-only') {
712
- const output = part.state.output || ''
713
- const outputTokens = Math.ceil(output.length / 4)
714
- const largeOutputThreshold = 3000
715
- if (outputTokens >= largeOutputThreshold) {
716
- const formattedTokens =
717
- outputTokens >= 1000 ? `${(outputTokens / 1000).toFixed(1)}k` : String(outputTokens)
718
- const percentageSuffix = (() => {
719
- if (!modelContextLimit) {
720
- return ''
721
- }
722
- const pct = (outputTokens / modelContextLimit) * 100
723
- if (pct < 1) {
724
- return ''
725
- }
726
- return ` (${pct.toFixed(1)}%)`
727
- })()
728
- const chunk = `⬦ ${part.tool} returned ${formattedTokens} tokens${percentageSuffix}`
729
- await thread.send({ content: chunk, flags: SILENT_MESSAGE_FLAGS })
739
+ // Show large output notifications for tools that are visible in current verbosity mode
740
+ if (part.type === 'tool' && part.state.status === 'completed') {
741
+ const showLargeOutput = (() => {
742
+ const verbosity = getVerbosity()
743
+ if (verbosity === 'text-only') {
744
+ return false
745
+ }
746
+ if (verbosity === 'text-and-essential-tools') {
747
+ return isEssentialTool(part.tool)
748
+ }
749
+ return true
750
+ })()
751
+ if (showLargeOutput) {
752
+ const output = part.state.output || ''
753
+ const outputTokens = Math.ceil(output.length / 4)
754
+ const largeOutputThreshold = 3000
755
+ if (outputTokens >= largeOutputThreshold) {
756
+ const formattedTokens =
757
+ outputTokens >= 1000 ? `${(outputTokens / 1000).toFixed(1)}k` : String(outputTokens)
758
+ const percentageSuffix = (() => {
759
+ if (!modelContextLimit) {
760
+ return ''
761
+ }
762
+ const pct = (outputTokens / modelContextLimit) * 100
763
+ if (pct < 1) {
764
+ return ''
765
+ }
766
+ return ` (${pct.toFixed(1)}%)`
767
+ })()
768
+ const chunk = `⬦ ${part.tool} returned ${formattedTokens} tokens${percentageSuffix}`
769
+ await thread.send({ content: chunk, flags: SILENT_MESSAGE_FLAGS })
770
+ }
730
771
  }
731
772
  }
732
773
 
@@ -761,10 +802,17 @@ export async function handleOpencodeSession({
761
802
  part: Part,
762
803
  subtaskInfo: { label: string; assistantMessageId?: string },
763
804
  ) => {
805
+ const verbosity = getVerbosity()
764
806
  // In text-only mode, skip all subtask output (they're tool-related)
765
- if (getVerbosity() === 'text-only') {
807
+ if (verbosity === 'text-only') {
766
808
  return
767
809
  }
810
+ // In text-and-essential-tools mode, only show essential tools from subtasks
811
+ if (verbosity === 'text-and-essential-tools') {
812
+ if (part.type !== 'tool' || !isEssentialTool(part.tool)) {
813
+ return
814
+ }
815
+ }
768
816
  if (part.type === 'step-start' || part.type === 'step-finish') {
769
817
  return
770
818
  }
@@ -1275,11 +1323,12 @@ export async function handleOpencodeSession({
1275
1323
  images.map((img) => ({
1276
1324
  mime: img.mime,
1277
1325
  filename: img.filename,
1278
- url: img.url.slice(0, 100),
1326
+ urlPreview: img.url.slice(0, 50) + '...',
1279
1327
  })),
1280
1328
  )
1281
- const imagePathsList = images.map((img) => `- ${img.filename}: ${img.url}`).join('\n')
1282
- return `${prompt}\n\n**attached images:**\n${imagePathsList}`
1329
+ // Just list filenames, not the full base64 URLs (images are passed as separate parts)
1330
+ const imageList = images.map((img) => `- ${img.filename}`).join('\n')
1331
+ return `${prompt}\n\n**attached images:**\n${imageList}`
1283
1332
  })()
1284
1333
 
1285
1334
  const parts = [{ type: 'text' as const, text: promptWithImagePaths }, ...images]