@wu529778790/open-im 0.3.6 → 0.3.8

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.
@@ -3,3 +3,8 @@ export declare function splitLongContent(text: string, maxLen: number): string[]
3
3
  export declare function formatToolStats(toolStats: Record<string, number>, numTurns: number): string;
4
4
  export declare function formatToolCallNotification(toolName: string, toolInput?: Record<string, unknown>): string;
5
5
  export declare function getContextWarning(totalTurns: number): string | null;
6
+ /**
7
+ * 预处理 Markdown 内容,将其转换为 Telegram 友好的格式
8
+ * Telegram Markdown 不支持标题(#),需要转换为粗体
9
+ */
10
+ export declare function preprocessMarkdownForTelegram(content: string): string;
@@ -79,3 +79,18 @@ export function getContextWarning(totalTurns) {
79
79
  const tipIndex = (totalTurns - 2) % regularTips.length;
80
80
  return regularTips[tipIndex];
81
81
  }
82
+ /**
83
+ * 预处理 Markdown 内容,将其转换为 Telegram 友好的格式
84
+ * Telegram Markdown 不支持标题(#),需要转换为粗体
85
+ */
86
+ export function preprocessMarkdownForTelegram(content) {
87
+ return content
88
+ // 转换 Markdown 标题为粗体(支持 1-6 级标题)
89
+ .replace(/^#{1,6}\s+(.+)$/gm, '**$1**')
90
+ // 转换加粗(如果使用 __ 形式)
91
+ .replace(/__(.+?)__/g, '**$1**')
92
+ // 转换斜体(如果使用 _ 形式且不是 __)
93
+ .replace(/(?<!_)_(?!_)(.+?)(?!_)_(?!_)/g, '*$1*')
94
+ // 确保 ``` 代码块正确
95
+ .replace(/```(\w*)\n([\s\S]+?)```/g, '```$1\n$2```');
96
+ }
@@ -2,7 +2,7 @@ import { getBot } from './client.js';
2
2
  import { createReadStream } from 'node:fs';
3
3
  import { basename } from 'node:path';
4
4
  import { createLogger } from '../logger.js';
5
- import { splitLongContent, truncateText } from '../shared/utils.js';
5
+ import { splitLongContent, truncateText, preprocessMarkdownForTelegram } from '../shared/utils.js';
6
6
  import { MAX_TELEGRAM_MESSAGE_LENGTH } from '../constants.js';
7
7
  import { listDirectories, buildDirectoryKeyboard } from '../commands/handler.js';
8
8
  const log = createLogger('TgSender');
@@ -32,12 +32,17 @@ const RESERVED_LENGTH = 150;
32
32
  function formatMessage(content, status, note, toolId = 'claude') {
33
33
  const icon = STATUS_ICONS[status];
34
34
  const title = getToolTitle(toolId, status);
35
+ // 在应用 Markdown 格式时,预处理内容以兼容 Telegram
36
+ let processedContent = content;
37
+ if (status === 'done' || status === 'error') {
38
+ processedContent = preprocessMarkdownForTelegram(content);
39
+ }
35
40
  // 计算可用内容长度(预留 header 和 note 空间)
36
41
  const headerLength = `${icon} ${title}\n\n`.length;
37
42
  const noteLength = note ? `\n\n─────────\n${note}`.length : 0;
38
43
  const maxContentLength = TG_MAX_LENGTH - headerLength - noteLength - RESERVED_LENGTH;
39
44
  // 确保内容长度不超过限制
40
- const text = truncateText(content, Math.max(100, maxContentLength));
45
+ const text = truncateText(processedContent, Math.max(100, maxContentLength));
41
46
  let out = `${icon} ${title}\n\n${text}`;
42
47
  if (note)
43
48
  out += `\n\n─────────\n${note}`;
@@ -105,11 +110,12 @@ export async function updateMessage(chatId, messageId, content, status, note, to
105
110
  // 完成或错误时,显式移除停止按钮(使用空的 inline_keyboard)
106
111
  opts.reply_markup = { inline_keyboard: [] };
107
112
  // 添加日志,帮助诊断
108
- log.info(`Updating message to ${status} status (removing stop button) for ${chatId}:${messageId}`);
113
+ log.info(`Updating message to ${status} status (removing stop button) for ${chatId}:${messageId}, content length: ${content.length}`);
109
114
  }
110
115
  // 流式输出时使用纯文本,避免 Markdown 解析导致内容减少
111
- // 只在完成时应用 Markdown 格式
112
- const shouldParseMarkdown = status === 'done' || status === 'error';
116
+ // 完成时也暂时使用纯文本,避免 Markdown 解析错误
117
+ // TODO: 等待 Markdown 预处理稳定后再启用 Markdown
118
+ const shouldParseMarkdown = false; // 暂时禁用 Markdown 解析
113
119
  let retries = 0;
114
120
  const maxRetries = 2; // 减少重试次数,但增加等待时间
115
121
  while (retries <= maxRetries) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wu529778790/open-im",
3
- "version": "0.3.6",
3
+ "version": "0.3.8",
4
4
  "description": "Multi-platform IM bridge for AI CLI tools (Claude, Codex, Cursor)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",