Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat: bot support custom markdown render and context injection (#13294)

Rdmclin2dd192eda3e2d
diff --git a/packages/context-engine/src/engine/messages/MessagesEngine.ts b/packages/context-engine/src/engine/messages/MessagesEngine.ts
index eeea84ee1b0..54fa23947a5 100644
--- a/packages/context-engine/src/engine/messages/MessagesEngine.ts
+++ b/packages/context-engine/src/engine/messages/MessagesEngine.ts
@@ -25,6 +25,7 @@ import {
AgentBuilderContextInjector,
AgentDocumentInjector,
AgentManagementContextInjector,
+ BotPlatformContextInjector,
DiscordContextProvider,
EvalContextSystemInjector,
ForceFinishSummaryInjector,
@@ -140,6 +141,7 @@ export class MessagesEngine {
fileContext,
messages,
agentBuilderContext,
+ botPlatformContext,
discordContext,
evalContext,
agentManagementContext,
@@ -207,6 +209,12 @@ export class MessagesEngine {
// 2. Eval context injection (appends envPrompt to system message)
new EvalContextSystemInjector({ enabled: !!evalContext?.envPrompt, evalContext }),
+ // 2.5. Bot platform context injection (appends formatting instructions for non-Markdown platforms)
+ new BotPlatformContextInjector({
+ context: botPlatformContext,
+ enabled: !!botPlatformContext,
+ }),
+
// 3. System date injection (appends current date to system message)
new SystemDateProvider({ enabled: isSystemDateEnabled, timezone }),
diff --git a/packages/context-engine/src/engine/messages/types.ts b/packages/context-engine/src/engine/messages/types.ts
index 0b9e01b0400..665faef9beb 100644
--- a/packages/context-engine/src/engine/messages/types.ts
+++ b/packages/context-engine/src/engine/messages/types.ts
@@ -12,6 +12,7 @@ import type { AgentInfo } from '../../processors/GroupRoleTransform';
import type { AgentBuilderContext } from '../../providers/AgentBuilderContextInjector';
import type { AgentContextDocument } from '../../providers/AgentDocumentInjector';
import type { AgentManagementContext } from '../../providers/AgentManagementContextInjector';
+import type { BotPlatformContext } from '../../providers/BotPlatformContextInjector';
import type { DiscordContext } from '../../providers/DiscordContextProvider';
import type { EvalContext } from '../../providers/EvalContextSystemInjector';
import type { GroupAgentBuilderContext } from '../../providers/GroupAgentBuilderContextInjector';
@@ -266,6 +267,8 @@ export interface MessagesEngineParams {
// ========== Extended contexts (both frontend and backend) ==========
/** Agent Builder context */
agentBuilderContext?: AgentBuilderContext;
+ /** Bot platform context for injecting platform capabilities (e.g. markdown support) */
+ botPlatformContext?: BotPlatformContext;
/** Discord context for injecting channel/guild info into system injection message */
discordContext?: DiscordContext;
/** Eval context for injecting environment prompts into system message */
@@ -331,6 +334,7 @@ export interface MessagesEngineResult {
export { type AgentInfo } from '../../processors/GroupRoleTransform';
export { type AgentBuilderContext } from '../../providers/AgentBuilderContextInjector';
export { type AgentManagementContext } from '../../providers/AgentManagementContextInjector';
+export { type BotPlatformContext } from '../../providers/BotPlatformContextInjector';
export { type DiscordContext } from '../../providers/DiscordContextProvider';
export { type EvalContext } from '../../providers/EvalContextSystemInjector';
export { type GroupAgentBuilderContext } from '../../providers/GroupAgentBuilderContextInjector';
diff --git a/packages/context-engine/src/providers/BotPlatformContextInjector.ts b/packages/context-engine/src/providers/BotPlatformContextInjector.ts
new file mode 100644
index 00000000000..d39b8562e16
--- /dev/null
+++ b/packages/context-engine/src/providers/BotPlatformContextInjector.ts
@@ -0,0 +1,76 @@
+import type { BotPlatformInfo } from '@lobechat/prompts';
+import { formatBotPlatformContext } from '@lobechat/prompts';
+import debug from 'debug';
+
+import { BaseProvider } from '../base/BaseProvider';
+import type { PipelineContext, ProcessorOptions } from '../types';
+
+const log = debug('context-engine:provider:BotPlatformContextInjector');
+
+export interface BotPlatformContext {
+ platformName: string;
+ supportsMarkdown: boolean;
+}
+
+export interface BotPlatformContextInjectorConfig {
+ context?: BotPlatformContext;
+ enabled?: boolean;
+}
+
+/**
+ * Bot Platform Context Injector
+ *
+ * Appends platform-specific formatting instructions to the system message.
+ * For platforms that don't support Markdown (e.g. WeChat, QQ), instructs
+ * the AI to respond in plain text only.
+ *
+ * Should run after SystemRoleInjector in the pipeline.
+ */
+export class BotPlatformContextInjector extends BaseProvider {
+ readonly name = 'BotPlatformContextInjector';
+
+ constructor(
+ private config: BotPlatformContextInjectorConfig,
+ options: ProcessorOptions = {},
+ ) {
+ super(options);
+ }
+
+ protected async doProcess(context: PipelineContext): Promise<PipelineContext> {
+ if (!this.config.enabled || !this.config.context) {
+ log('Disabled or no context, skipping injection');
+ return this.markAsExecuted(context);
+ }
+
+ const info: BotPlatformInfo = this.config.context;
+ const prompt = formatBotPlatformContext(info);
+
+ if (!prompt) {
+ log('Platform supports markdown, no injection needed');
+ return this.markAsExecuted(context);
+ }
+
+ const clonedContext = this.cloneContext(context);
+ const systemMsgIndex = clonedContext.messages.findIndex((m) => m.role === 'system');
+
+ if (systemMsgIndex >= 0) {
+ const original = clonedContext.messages[systemMsgIndex];
+ clonedContext.messages[systemMsgIndex] = {
+ ...original,
+ content: [original.content, prompt].filter(Boolean).join('\n\n'),
+ };
+ log('Appended bot platform context to existing system message');
+ } else {
+ clonedContext.messages.unshift({
+ content: prompt,
+ createdAt: Date.now(),
+ id: `bot-platform-context-${Date.now()}`,
+ role: 'system' as const,
+ updatedAt: Date.now(),
+ });
+ log('Created new system message with bot platform context');
+ }
+
+ return this.markAsExecuted(clonedContext);
+ }
+}
diff --git a/packages/context-engine/src/providers/index.ts b/packages/context-engine/src/providers/index.ts
index a29421ac5f2..711e9348795 100644
--- a/packages/context-engine/src/providers/index.ts
+++ b/packages/context-engine/src/providers/index.ts
@@ -2,6 +2,7 @@
export { AgentBuilderContextInjector } from './AgentBuilderContextInjector';
export { AGENT_DOCUMENT_INJECTION_POSITIONS, AgentDocumentInjector } from './AgentDocumentInjector';
export { AgentManagementContextInjector } from './AgentManagementContextInjector';
+export { BotPlatformContextInjector } from './BotPlatformContextInjector';
export { DiscordContextProvider } from './DiscordContextProvider';
export { EvalContextSystemInjector } from './EvalContextSystemInjector';
export { ForceFinishSummaryInjector } from './ForceFinishSummaryInjector';
@@ -42,6 +43,10 @@ export type {
AvailablePluginInfo,
AvailableProviderInfo,
} from './AgentManagementContextInjector';
+export type {
+ BotPlatformContext,
+ BotPlatformContextInjectorConfig,
+} from './BotPlatformContextInjector';
export type { DiscordContext, DiscordContextProviderConfig } from './DiscordContextProvider';
export type { EvalContext, EvalContextSystemInjectorConfig } from './EvalContextSystemInjector';
export type { ForceFinishSummaryInjectorConfig } from './ForceFinishSummaryInjector';
diff --git a/packages/prompts/src/prompts/botPlatformContext/index.ts b/packages/prompts/src/prompts/botPlatformContext/index.ts
new file mode 100644
index 00000000000..611d77cbeb0
--- /dev/null
+++ b/packages/prompts/src/prompts/botPlatformContext/index.ts
@@ -0,0 +1,30 @@
+export interface BotPlatformInfo {
+ platformName: string;
+ supportsMarkdown: boolean;
+}
+
+/**
+ * Format bot platform context into a system-level instruction.
+ *
+ * When the platform does not support Markdown, instructs the AI to use plain text only.
+ */
+export const formatBotPlatformContext = ({
+ platformName,
+ supportsMarkdown,
+}: BotPlatformInfo): string | null => {
+ if (supportsMarkdown) return null;
+
+ return [
+ `<bot_platform_context platform="${platformName}">`,
+ 'The current IM platform does NOT support Markdown rendering.',
+ 'You MUST NOT use any Markdown formatting in your response, including:',
+ '- **bold**, *italic*, ~~strikethrough~~',
+ '- `inline code` or ```code blocks```',
+ '- # Headings',
+ '- [links](url)',
+ '- Tables, blockquotes, or HTML tags',
+ '',
+ 'Use plain text only. Use line breaks, indentation, dashes, and numbering to structure your response for readability.',
+ '</bot_platform_context>',
+ ].join('\n');
+};
diff --git a/packages/prompts/src/prompts/index.ts b/packages/prompts/src/prompts/index.ts
index 947aa6c49ae..22df46da147 100644
--- a/packages/prompts/src/prompts/index.ts
+++ b/packages/prompts/src/prompts/index.ts
@@ -1,5 +1,6 @@
export * from './agentBuilder';
export * from './agentGroup';
+export * from './botPlatformContext';
export * from './chatMessages';
export * from './compressContext';
export * from './discordContext';
diff --git a/scripts/devStartupSequence.mts b/scripts/devStartupSequence.mts
index 099e7d3d925..cde2c470651 100644
--- a/scripts/devStartupSequence.mts
+++ b/scripts/devStartupSequence.mts
@@ -1,25 +1,17 @@
import { type ChildProcess, spawn } from 'node:child_process';
-import { readFileSync } from 'node:fs';
+import dotenv from 'dotenv';
import net from 'node:net';
-import { resolve } from 'node:path';
+
+dotenv.config();
const NEXT_HOST = 'localhost';
/**
- * Parse the Next.js dev port from the `dev:next` script in the nearest package.json.
- * Supports both `--port <n>` and `-p <n>` flags. Falls back to 3010.
+ * Resolve the Next.js dev port.
+ * Respects the PORT environment variable, falls back to 3010.
*/
const resolveNextPort = (): number => {
- try {
- const pkg = JSON.parse(readFileSync(resolve(process.cwd(), 'package.json'), 'utf-8'));
- const devNext: string | undefined = pkg?.scripts?.['dev:next'];
- if (devNext) {
- const match = devNext.match(/(?:--port|-p)\s+(\d+)/);
- if (match) return Number(match[1]);
- }
- } catch {
- /* fallback */
- }
+ if (process.env.PORT) return Number(process.env.PORT);
return 3010;
};
@@ -120,7 +112,11 @@ const main = async () => {
process.once('SIGINT', () => shutdownAll('SIGINT'));
process.once('SIGTERM', () => shutdownAll('SIGTERM'));
- nextProcess = runNpmScript('dev:next');
+ nextProcess = spawn('npx', ['next', 'dev', '-p', String(NEXT_PORT)], {
+ env: process.env,
+ stdio: 'inherit',
+ shell: process.platform === 'win32',
+ });
watchChildExit(nextProcess, 'next');
viteProcess = runNpmScript('dev:spa');
diff --git a/src/envs/app.ts b/src/envs/app.ts
index 670bed4f276..de5a7f6afff 100644
--- a/src/envs/app.ts
+++ b/src/envs/app.ts
@@ -22,8 +22,8 @@ const APP_URL = process.env.APP_URL
: isInVercel
? getVercelUrl()
: process.env.NODE_ENV === 'development'
- ? 'http://localhost:3010'
- : 'http://localhost:3210';
+ ? `http://localhost:${process.env.PORT || 3010}`
+ : `http://localhost:${process.env.PORT || 3210}`;
// INTERNAL_APP_URL is used for server-to-server calls to bypass CDN/proxy
// Falls back to APP_URL if not set
diff --git a/src/server/modules/AgentRuntime/RuntimeExecutors.ts b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
index edb71e7252b..29e72d96db1 100644
--- a/src/server/modules/AgentRuntime/RuntimeExecutors.ts
+++ b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
@@ -92,6 +92,7 @@ const formatErrorEventData = (error: unknown, phase: string) => {
export interface RuntimeExecutorContext {
agentConfig?: any;
+ botPlatformContext?: any;
discordContext?: any;
evalContext?: EvalContext;
fileService?: any;
@@ -276,6 +277,7 @@ export const createRuntimeExecutors = (
return info?.abilities?.vision ?? true;
},
},
+ botPlatformContext: ctx.botPlatformContext,
discordContext: ctx.discordContext,
enableHistoryCount: agentConfig.chatConfig?.enableHistoryCount ?? undefined,
evalContext: ctx.evalContext,
diff --git a/src/server/modules/Mecha/ContextEngineering/index.ts b/src/server/modules/Mecha/ContextEngineering/index.ts
index c334c0a78d5..c32838c5509 100644
--- a/src/server/modules/Mecha/ContextEngineering/index.ts
+++ b/src/server/modules/Mecha/ContextEngineering/index.ts
@@ -64,6 +64,7 @@ export const serverMessagesEngine = async ({
capabilities,
userMemory,
agentBuilderContext,
+ botPlatformContext,
discordContext,
evalContext,
agentManagementContext,
@@ -146,6 +147,7 @@ export const serverMessagesEngine = async ({
// Extended contexts
...(agentBuilderContext && { agentBuilderContext }),
+ ...(botPlatformContext && { botPlatformContext }),
...(discordContext && { discordContext }),
...(evalContext && { evalContext }),
...(agentManagementContext && { agentManagementContext }),
@@ -158,6 +160,7 @@ export const serverMessagesEngine = async ({
// Re-export types
export type {
+ BotPlatformContext,
EvalContext,
ServerKnowledgeConfig,
ServerMessagesEngineParams,
diff --git a/src/server/modules/Mecha/ContextEngineering/types.ts b/src/server/modules/Mecha/ContextEngineering/types.ts
index fb23ba675b5..8bc85a8cde2 100644
--- a/src/server/modules/Mecha/ContextEngineering/types.ts
+++ b/src/server/modules/Mecha/ContextEngineering/types.ts
@@ -2,6 +2,7 @@
import type {
AgentBuilderContext,
AgentManagementContext,
+ BotPlatformContext,
DiscordContext,
EvalContext,
FileContent,
@@ -75,6 +76,8 @@ export interface ServerMessagesEngineParams {
// ========== Capability injection ==========
/** Model capability checkers */
capabilities?: ServerModelCapabilities;
+ /** Bot platform context for injecting platform capabilities (e.g. markdown support) */
+ botPlatformContext?: BotPlatformContext;
/** Discord context for injecting channel/guild info */
discordContext?: DiscordContext;
// ========== Eval context ==========
@@ -134,6 +137,7 @@ export interface ServerMessagesEngineParams {
export {
type AgentBuilderContext,
type AgentManagementContext,
+ type BotPlatformContext,
type DiscordContext,
type EvalContext,
type FileContent,
diff --git a/src/server/services/agentRuntime/AgentRuntimeService.ts b/src/server/services/agentRuntime/AgentRuntimeService.ts
index 6333286616c..6bdc53a019a 100644
--- a/src/server/services/agentRuntime/AgentRuntimeService.ts
+++ b/src/server/services/agentRuntime/AgentRuntimeService.ts
@@ -274,6 +274,7 @@ export class AgentRuntimeService {
completionWebhook,
stepWebhook,
webhookDelivery,
+ botPlatformContext,
discordContext,
evalContext,
maxSteps,
@@ -318,6 +319,7 @@ export class AgentRuntimeService {
metadata: {
activeDeviceId,
agentConfig,
+ botPlatformContext,
completionWebhook,
deviceSystemInfo,
discordContext,
@@ -1502,6 +1504,7 @@ export class AgentRuntimeService {
// Create streaming executor context
const executorContext: RuntimeExecutorContext = {
agentConfig: metadata?.agentConfig,
+ botPlatformContext: metadata?.botPlatformContext,
discordContext: metadata?.discordContext,
userTimezone: metadata?.userTimezone,
evalContext: metadata?.evalContext,
diff --git a/src/server/services/agentRuntime/types.ts b/src/server/services/agentRuntime/types.ts
index 9e72dc39a67..50aac0f27c7 100644
--- a/src/server/services/agentRuntime/types.ts
+++ b/src/server/services/agentRuntime/types.ts
@@ -140,6 +140,8 @@ export interface OperationCreationParams {
trigger?: string;
};
autoStart?: boolean;
+ /** Bot platform context for injecting platform capabilities (e.g. markdown support) */
+ botPlatformContext?: any;
/**
* Completion webhook configuration
* When set, an HTTP POST will be fired when the operation completes (success or error).
diff --git a/src/server/services/aiAgent/index.ts b/src/server/services/aiAgent/index.ts
index 9aca5cb0218..b9df48f2fa4 100644
--- a/src/server/services/aiAgent/index.ts
+++ b/src/server/services/aiAgent/index.ts
@@ -84,6 +84,8 @@ function formatErrorForMetadata(error: unknown): Record<string, any> | undefined
interface InternalExecAgentParams extends ExecAgentParams {
/** Bot context for topic metadata (platform, applicationId, platformThreadId) */
botContext?: ChatTopicBotContext;
+ /** Bot platform context for injecting platform capabilities (e.g. markdown support) */
+ botPlatformContext?: any;
/**
* Completion webhook configuration
* Persisted in Redis state, triggered via HTTP POST when the operation completes.
@@ -207,6 +209,7 @@ export class AiAgentService {
appContext,
autoStart = true,
botContext,
+ botPlatformContext,
discordContext,
existingMessageIds = [],
files,
@@ -866,6 +869,7 @@ export class AiAgentService {
trigger,
},
autoStart,
+ botPlatformContext,
completionWebhook,
discordContext,
evalContext,
diff --git a/src/server/services/bot/AgentBridgeService.ts b/src/server/services/bot/AgentBridgeService.ts
index 31470c1f504..450780fda82 100644
--- a/src/server/services/bot/AgentBridgeService.ts
+++ b/src/server/services/bot/AgentBridgeService.ts
@@ -549,10 +549,24 @@ export class AgentBridgeService {
trigger?: string;
},
): Promise<{ reply: string; topicId: string }> {
+ // Resolve bot platform context from platform registry
+ let botPlatformContext: { platformName: string; supportsMarkdown: boolean } | undefined;
+ if (opts.botContext?.platform) {
+ const platformDef = platformRegistry.getPlatform(opts.botContext.platform);
+ if (platformDef) {
+ botPlatformContext = {
+ platformName: platformDef.name,
+ supportsMarkdown: platformDef.supportsMarkdown !== false,
+ };
+ }
+ }
+
+ const optsWithPlatform = { ...opts, botPlatformContext };
+
if (isQueueAgentRuntimeEnabled()) {
- return this.executeWithWebhooks(thread, userMessage, opts);
+ return this.executeWithWebhooks(thread, userMessage, optsWithPlatform);
}
- return this.executeWithInMemoryCallbacks(thread, userMessage, opts);
+ return this.executeWithInMemoryCallbacks(thread, userMessage, optsWithPlatform);
}
/**
@@ -566,13 +580,15 @@ export class AgentBridgeService {
opts: {
agentId: string;
botContext?: ChatTopicBotContext;
+ botPlatformContext?: { platformName: string; supportsMarkdown: boolean };
channelContext?: DiscordChannelContext;
client?: PlatformClient;
topicId?: string;
trigger?: string;
},
): Promise<{ reply: string; topicId: string }> {
- const { agentId, botContext, channelContext, client, topicId, trigger } = opts;
+ const { agentId, botContext, botPlatformContext, channelContext, client, topicId, trigger } =
+ opts;
const aiAgentService = new AiAgentService(this.db, this.userId);
const timezone = await this.loadTimezone();
@@ -634,6 +650,7 @@ export class AgentBridgeService {
appContext: topicId ? { topicId } : undefined,
autoStart: true,
botContext,
+ botPlatformContext,
completionWebhook: { body: webhookBody, url: callbackUrl },
discordContext: channelContext
? { channel: channelContext.channel, guild: channelContext.guild }
@@ -706,6 +723,7 @@ export class AgentBridgeService {
opts: {
agentId: string;
botContext?: ChatTopicBotContext;
+ botPlatformContext?: { platformName: string; supportsMarkdown: boolean };
channelContext?: DiscordChannelContext;
charLimit?: number;
client?: PlatformClient;
@@ -713,7 +731,16 @@ export class AgentBridgeService {
trigger?: string;
},
): Promise<{ reply: string; topicId: string }> {
- const { agentId, botContext, channelContext, charLimit, client, topicId, trigger } = opts;
+ const {
+ agentId,
+ botContext,
+ botPlatformContext,
+ channelContext,
+ charLimit,
+ client,
+ topicId,
+ trigger,
+ } = opts;
const aiAgentService = new AiAgentService(this.db, this.userId);
const timezone = await this.loadTimezone();
@@ -759,6 +786,7 @@ export class AgentBridgeService {
appContext: topicId ? { topicId } : undefined,
autoStart: true,
botContext,
+ botPlatformContext,
discordContext: channelContext
? { channel: channelContext.channel, guild: channelContext.guild }
: undefined,
@@ -786,7 +814,8 @@ export class AgentBridgeService {
totalCost: stepData.totalCost ?? 0,
totalTokens: stepData.totalTokens ?? 0,
};
- const progressText = client?.formatReply?.(msgBody, stats) ?? msgBody;
+ const formatted = client?.formatMarkdown?.(msgBody) ?? msgBody;
+ const progressText = client?.formatReply?.(formatted, stats) ?? formatted;
if (content) lastLLMContent = content;
if (toolsCalling) lastToolsCalling = toolsCalling;
@@ -849,7 +878,9 @@ export class AgentBridgeService {
totalCost: finalState.cost?.total ?? 0,
totalTokens: finalState.usage?.llm?.tokens?.total ?? 0,
};
- const finalText = client?.formatReply?.(replyBody, replyStats) ?? replyBody;
+ const formattedBody = client?.formatMarkdown?.(replyBody) ?? replyBody;
+ const finalText =
+ client?.formatReply?.(formattedBody, replyStats) ?? formattedBody;
const chunks = splitMessage(finalText, charLimit);
diff --git a/src/server/services/bot/BotCallbackService.ts b/src/server/services/bot/BotCallbackService.ts
index b8fa3fd6b74..44bdad542f5 100644
--- a/src/server/services/bot/BotCallbackService.ts
+++ b/src/server/services/bot/BotCallbackService.ts
@@ -184,7 +184,8 @@ export class BotCallbackService {
totalTokens: body.totalTokens ?? 0,
};
- const progressText = client.formatReply?.(msgBody, stats) ?? msgBody;
+ const formatted = client.formatMarkdown?.(msgBody) ?? msgBody;
+ const progressText = client.formatReply?.(formatted, stats) ?? formatted;
const isLlmFinalResponse =
body.stepType === 'call_llm' && !body.toolsCalling?.length && body.content;
@@ -248,7 +249,8 @@ export class BotCallbackService {
totalTokens: body.totalTokens ?? 0,
};
- const finalText = client.formatReply?.(msgBody, stats) ?? msgBody;
+ const formattedBody = client.formatMarkdown?.(msgBody) ?? msgBody;
+ const finalText = client.formatReply?.(formattedBody, stats) ?? formattedBody;
const chunks = splitMessage(finalText, charLimit);
try {
diff --git a/src/server/services/bot/platforms/feishu/client.ts b/src/server/services/bot/platforms/feishu/client.ts
index d176080966a..c298b9a3301 100644
--- a/src/server/services/bot/platforms/feishu/client.ts
+++ b/src/server/services/bot/platforms/feishu/client.ts
@@ -7,6 +7,7 @@ import {
updateBotRuntimeStatus,
} from '@/server/services/gateway/runtimeStatus';
+import { stripMarkdown } from '../stripMarkdown';
import {
type BotPlatformRuntimeContext,
type BotProviderConfig,
@@ -122,6 +123,10 @@ class FeishuWebhookClient implements PlatformClient {
return extractChatId(platformThreadId);
}
+ formatMarkdown(markdown: string): string {
+ return stripMarkdown(markdown);
+ }
+
formatReply(body: string, stats?: UsageStats): string {
if (!stats || !this.config.settings?.showUsageStats) return body;
return `${body}\n\n${formatUsageStats(stats)}`;
diff --git a/src/server/services/bot/platforms/feishu/definitions/feishu.ts b/src/server/services/bot/platforms/feishu/definitions/feishu.ts
index e6bab2fe15f..f1fd90c1f02 100644
--- a/src/server/services/bot/platforms/feishu/definitions/feishu.ts
+++ b/src/server/services/bot/platforms/feishu/definitions/feishu.ts
@@ -12,5 +12,6 @@ export const feishu: PlatformDefinition = {
},
schema: sharedSchema,
showWebhookUrl: true,
+ supportsMarkdown: false,
clientFactory: sharedClientFactory,
};
diff --git a/src/server/services/bot/platforms/feishu/definitions/lark.ts b/src/server/services/bot/platforms/feishu/definitions/lark.ts
index 49d501ea922..e798e3ae1af 100644
--- a/src/server/services/bot/platforms/feishu/definitions/lark.ts
+++ b/src/server/services/bot/platforms/feishu/definitions/lark.ts
@@ -12,5 +12,6 @@ export const lark: PlatformDefinition = {
},
schema: sharedSchema,
showWebhookUrl: true,
+ supportsMarkdown: false,
clientFactory: sharedClientFactory,
};
diff --git a/src/server/services/bot/platforms/qq/client.ts b/src/server/services/bot/platforms/qq/client.ts
index 0a16c3bce5a..91a2b569d23 100644
--- a/src/server/services/bot/platforms/qq/client.ts
+++ b/src/server/services/bot/platforms/qq/client.ts
@@ -7,6 +7,7 @@ import {
updateBotRuntimeStatus,
} from '@/server/services/gateway/runtimeStatus';
+import { stripMarkdown } from '../stripMarkdown';
import {
type BotPlatformRuntimeContext,
type BotProviderConfig,
@@ -141,6 +142,10 @@ class QQWebhookClient implements PlatformClient {
return extractChatId(platformThreadId);
}
+ formatMarkdown(markdown: string): string {
+ return stripMarkdown(markdown);
+ }
+
formatReply(body: string, stats?: UsageStats): string {
if (!stats || !this.config.settings?.showUsageStats) return body;
return `${body}\n\n${formatUsageStats(stats)}`;
diff --git a/src/server/services/bot/platforms/qq/definition.ts b/src/server/services/bot/platforms/qq/definition.ts
index 5958250dd47..5db9dadd2f2 100644
--- a/src/server/services/bot/platforms/qq/definition.ts
+++ b/src/server/services/bot/platforms/qq/definition.ts
@@ -12,6 +12,7 @@ export const qq: PlatformDefinition = {
},
schema,
showWebhookUrl: true,
+ supportsMarkdown: false,
supportsMessageEdit: false,
clientFactory: new QQClientFactory(),
};
diff --git a/src/server/services/bot/platforms/slack/client.ts b/src/server/services/bot/platforms/slack/client.ts
index ff4b8013aea..42febffc080 100644
--- a/src/server/services/bot/platforms/slack/client.ts
+++ b/src/server/services/bot/platforms/slack/client.ts
@@ -18,6 +18,7 @@ import {
} from '../types';
import { formatUsageStats } from '../utils';
import { SLACK_API_BASE, SlackApi } from './api';
+import { markdownToSlackMrkdwn } from './markdownToMrkdwn';
const log = debug('bot-platform:slack:bot');
@@ -114,6 +115,10 @@ class SlackWebhookClient implements PlatformClient {
return extractChannelId(platformThreadId);
}
+ formatMarkdown(markdown: string): string {
+ return markdownToSlackMrkdwn(markdown);
+ }
+
formatReply(body: string, stats?: UsageStats): string {
if (!stats || !this.config.settings?.showUsageStats) return body;
return `${body}\n\n${formatUsageStats(stats)}`;
diff --git a/src/server/services/bot/platforms/slack/markdownToMrkdwn.test.ts b/src/server/services/bot/platforms/slack/markdownToMrkdwn.test.ts
new file mode 100644
index 00000000000..97461d31b16
--- /dev/null
+++ b/src/server/services/bot/platforms/slack/markdownToMrkdwn.test.ts
@@ -0,0 +1,79 @@
+import { describe, expect, it } from 'vitest';
+
+import { markdownToSlackMrkdwn } from './markdownToMrkdwn';
+
+describe('markdownToSlackMrkdwn', () => {
+ it('should convert bold', () => {
+ expect(markdownToSlackMrkdwn('**bold text**')).toBe('*bold text*');
+ });
+
+ it('should convert bold+italic', () => {
+ expect(markdownToSlackMrkdwn('***bold italic***')).toBe('*_bold italic_*');
+ });
+
+ it('should convert strikethrough', () => {
+ expect(markdownToSlackMrkdwn('~~deleted~~')).toBe('~deleted~');
+ });
+
+ it('should preserve inline code', () => {
+ expect(markdownToSlackMrkdwn('run `npm install` now')).toBe('run `npm install` now');
+ });
+
+ it('should convert fenced code blocks (strip language)', () => {
+ const input = '```typescript\nconst x = 1;\n```';
+ expect(markdownToSlackMrkdwn(input)).toBe('```const x = 1;```');
+ });
+
+ it('should convert links', () => {
+ expect(markdownToSlackMrkdwn('[Click](https://example.com)')).toBe(
+ '<https://example.com|Click>',
+ );
+ });
+
+ it('should convert images to links', () => {
+ expect(markdownToSlackMrkdwn('![logo](https://img.png)')).toBe('<https://img.png|logo>');
+ });
+
+ it('should convert headings to bold', () => {
+ expect(markdownToSlackMrkdwn('# Title')).toBe('*Title*');
+ expect(markdownToSlackMrkdwn('## Subtitle')).toBe('*Subtitle*');
+ });
+
+ it('should preserve blockquotes', () => {
+ expect(markdownToSlackMrkdwn('> quoted text')).toBe('> quoted text');
+ });
+
+ it('should not convert markdown inside code blocks', () => {
+ const input = '```\n**not bold** and *not italic*\n```';
+ expect(markdownToSlackMrkdwn(input)).toBe('```**not bold** and *not italic*```');
+ });
+
+ it('should not convert markdown inside inline code', () => {
+ expect(markdownToSlackMrkdwn('use `**bold**` syntax')).toBe('use `**bold**` syntax');
+ });
+
+ it('should handle a complex document', () => {
+ const input = [
+ '# Hello',
+ '',
+ 'This is **bold** and ~~deleted~~.',
+ '',
+ '```js',
+ 'const x = 1;',
+ '```',
+ '',
+ '[Link](https://example.com)',
+ ].join('\n');
+
+ const result = markdownToSlackMrkdwn(input);
+ expect(result).toContain('*Hello*');
+ expect(result).toContain('*bold*');
+ expect(result).toContain('~deleted~');
+ expect(result).toContain('```const x = 1;```');
+ expect(result).toContain('<https://example.com|Link>');
+ });
+
+ it('should pass through plain text unchanged', () => {
+ expect(markdownToSlackMrkdwn('Hello world')).toBe('Hello world');
+ });
+});
diff --git a/src/server/services/bot/platforms/slack/markdownToMrkdwn.ts b/src/server/services/bot/platforms/slack/markdownToMrkdwn.ts
new file mode 100644
index 00000000000..d908e78ad07
--- /dev/null
+++ b/src/server/services/bot/platforms/slack/markdownToMrkdwn.ts
@@ -0,0 +1,94 @@
+/**
+ * Convert standard Markdown to Slack mrkdwn format.
+ *
+ * Slack mrkdwn differences from standard Markdown:
+ * - Bold: *text* (not **text**)
+ * - Italic: _text_ (same, but conflicts with bold are resolved)
+ * - Strikethrough: ~text~ (not ~~text~~)
+ * - Links: <url|text> (not [text](url))
+ * - Code: `text` (same)
+ * - Code block: ```text``` (same, but no language highlighting)
+ * - Blockquote: > text (same)
+ * - No headings β€” converted to bold
+ *
+ * Strategy:
+ * 1. Extract fenced code blocks (protect from conversion)
+ * 2. Extract inline code (protect from conversion)
+ * 3. Convert block-level elements
+ * 4. Convert inline elements
+ * 5. Re-insert protected content
+ */
+export function markdownToSlackMrkdwn(md: string): string {
+ // 1. Extract fenced code blocks
+ const codeBlocks: string[] = [];
+ let text = md.replaceAll(/^```\w*\n([\s\S]*?)^```/gm, (_match, code: string) => {
+ codeBlocks.push('```' + code.replace(/\n$/, '') + '```');
+ return `\x00CODEBLOCK_${codeBlocks.length - 1}\x00`;
+ });
+
+ // 2. Extract inline code
+ const inlineCodes: string[] = [];
+ text = text.replaceAll(/`([^`\n]+)`/g, (_match, code: string) => {
+ inlineCodes.push('`' + code + '`');
+ return `\x00INLINECODE_${inlineCodes.length - 1}\x00`;
+ });
+
+ // 3. Block-level transforms
+
+ // Headings β†’ bold
+ text = text.replaceAll(/^#{1,6}\s+(.+)/gm, '*$1*');
+
+ // 4. Inline transforms (order matters)
+
+ // Images: ![alt](url) β†’ <url|alt>
+ text = text.replaceAll(/!\[([^\]]*)\]\(([^)]+)\)/g, '<$2|$1>');
+
+ // Links: [text](url) β†’ <url|text>
+ text = text.replaceAll(/\[([^\]]+)\]\(([^)]+)\)/g, '<$2|$1>');
+
+ // Bold + italic: ***text*** β†’ *_text_*
+ text = text.replaceAll(/\*{3}(.+?)\*{3}/g, '*_$1_*');
+ text = text.replaceAll(/_{3}(.+?)_{3}/g, '*_$1_*');
+
+ // Bold: **text** β†’ *text*
+ text = text.replaceAll(/\*{2}(.+?)\*{2}/g, '*$1*');
+
+ // Note: *text* is already italic in mrkdwn β†’ _text_
+ // But single * in markdown is italic too, so we need to convert
+ // markdown italic *text* β†’ slack italic _text_
+ // This is tricky because after bold conversion, *text* is now bold in slack.
+ // The bold conversion above already handles **text** β†’ *text*.
+ // Remaining single *text* from markdown should become _text_ in slack.
+ // But we can't distinguish remaining markdown *italic* from converted *bold*.
+ // Solution: use __text__ for underscore bold (already converted),
+ // and handle *italic* before bold... Actually let's rethink.
+
+ // Actually the approach is simpler: convert in specific order.
+ // After ***β†’*_ and **β†’*, any remaining *text* pairs are markdown italic.
+ // We need to NOT convert these since they'd conflict with the bold we just created.
+ // Instead, markdown italic *text* is already valid as slack bold *text*,
+ // which isn't ideal. Let's use a different approach:
+
+ // Reset - use placeholder approach for bold
+ // Re-do: extract bold first, then handle italic
+ // Actually the current approach works because:
+ // - ***text*** β†’ *_text_* (bold+italic)
+ // - **text** β†’ *text* (bold in slack)
+ // - Remaining *text* from original markdown was italic β†’ becomes bold in slack
+ // This is acceptable because slack has no way to distinguish, and bold is close enough.
+
+ // Strikethrough: ~~text~~ β†’ ~text~
+ text = text.replaceAll(/~~(.+?)~~/g, '~$1~');
+
+ // 5. Re-insert inline code
+ text = text.replaceAll(/\0INLINECODE_(\d+)\0/g, (_match, idx: string) => {
+ return inlineCodes[Number.parseInt(idx)];
+ });
+
+ // 6. Re-insert code blocks
+ text = text.replaceAll(/\0CODEBLOCK_(\d+)\0/g, (_match, idx: string) => {
+ return codeBlocks[Number.parseInt(idx)];
+ });
+
+ return text;
+}
diff --git a/src/server/services/bot/platforms/stripMarkdown.test.ts b/src/server/services/bot/platforms/stripMarkdown.test.ts
new file mode 100644
index 00000000000..14e7e90aee4
--- /dev/null
+++ b/src/server/services/bot/platforms/stripMarkdown.test.ts
@@ -0,0 +1,90 @@
+import { describe, expect, it } from 'vitest';
+
+import { stripMarkdown } from './stripMarkdown';
+
+describe('stripMarkdown', () => {
+ it('should remove heading markers', () => {
+ expect(stripMarkdown('# Title')).toBe('Title');
+ expect(stripMarkdown('## Subtitle')).toBe('Subtitle');
+ expect(stripMarkdown('### H3')).toBe('H3');
+ });
+
+ it('should remove bold formatting', () => {
+ expect(stripMarkdown('**bold text**')).toBe('bold text');
+ expect(stripMarkdown('__bold text__')).toBe('bold text');
+ });
+
+ it('should remove italic formatting', () => {
+ expect(stripMarkdown('*italic text*')).toBe('italic text');
+ expect(stripMarkdown('some _italic_ here')).toBe('some italic here');
+ });
+
+ it('should remove bold+italic formatting', () => {
+ expect(stripMarkdown('***bold italic***')).toBe('bold italic');
+ });
+
+ it('should remove strikethrough', () => {
+ expect(stripMarkdown('~~deleted~~')).toBe('deleted');
+ });
+
+ it('should remove inline code backticks', () => {
+ expect(stripMarkdown('run `npm install` now')).toBe('run npm install now');
+ });
+
+ it('should remove fenced code block markers but keep content', () => {
+ const input = '```typescript\nconst x = 1;\n```';
+ expect(stripMarkdown(input)).toBe('const x = 1;\n');
+ });
+
+ it('should convert links to text (url) format', () => {
+ expect(stripMarkdown('[Click here](https://example.com)')).toBe(
+ 'Click here (https://example.com)',
+ );
+ });
+
+ it('should convert images to alt text', () => {
+ expect(stripMarkdown('![alt text](https://img.png)')).toBe('alt text');
+ });
+
+ it('should convert blockquotes to vertical bar', () => {
+ expect(stripMarkdown('> quoted text')).toBe('| quoted text');
+ });
+
+ it('should handle tables by converting to bullet list', () => {
+ const input = '| Name | Age |\n|------|-----|\n| Alice | 30 |\n| Bob | 25 |';
+ const result = stripMarkdown(input);
+ expect(result).toContain('- Name: Alice, Age: 30');
+ expect(result).toContain('- Name: Bob, Age: 25');
+ });
+
+ it('should handle a complex mixed markdown document', () => {
+ const input = [
+ '# Hello World',
+ '',
+ 'This is **bold** and *italic* text.',
+ '',
+ '- item 1',
+ '- item 2',
+ '',
+ '```js',
+ 'console.log("hi");',
+ '```',
+ '',
+ '[Link](https://example.com)',
+ ].join('\n');
+
+ const result = stripMarkdown(input);
+ expect(result).not.toContain('**');
+ expect(result).not.toContain('```');
+ expect(result).not.toContain('# ');
+ expect(result).toContain('Hello World');
+ expect(result).toContain('bold');
+ expect(result).toContain('italic');
+ expect(result).toContain('console.log("hi");');
+ expect(result).toContain('Link (https://example.com)');
+ });
+
+ it('should pass through plain text unchanged', () => {
+ expect(stripMarkdown('Hello world')).toBe('Hello world');
+ });
+});
diff --git a/src/server/services/bot/platforms/stripMarkdown.ts b/src/server/services/bot/platforms/stripMarkdown.ts
new file mode 100644
index 00000000000..1c02a78a63c
--- /dev/null
+++ b/src/server/services/bot/platforms/stripMarkdown.ts
@@ -0,0 +1,81 @@
+/**
+ * Convert Markdown to readable plain text for platforms that don't support Markdown rendering.
+ *
+ * Design goals:
+ * - Preserve readability and structure (line breaks, indentation, lists)
+ * - Remove syntactic noise (**, `, #, []() etc.)
+ * - Keep code block content intact (just remove the fences)
+ * - Convert links to "text (url)" format so URLs are still accessible
+ * - Convert tables to aligned plain-text representation
+ */
+export function stripMarkdown(md: string): string {
+ let text = md;
+
+ // --- Block-level transforms (order matters) ---
+
+ // Fenced code blocks: remove fences, keep content
+ text = text.replaceAll(/^```[\w-]*\n([\s\S]*?)^```/gm, '$1');
+
+ // Tables: convert to bullet-style rows
+ text = text.replaceAll(
+ /^(\|.+\|)\n\|[-\s|:]+\|\n((?:\|.+\|\n?)*)/gm,
+ (_match, headerRow: string, bodyRows: string) => {
+ const parseRow = (row: string) =>
+ row
+ .split('|')
+ .slice(1, -1)
+ .map((c: string) => c.trim());
+
+ const headers = parseRow(headerRow);
+ const rows = bodyRows
+ .trim()
+ .split('\n')
+ .filter(Boolean)
+ .map((r: string) => parseRow(r));
+
+ return rows
+ .map((cells: string[]) =>
+ cells.map((cell: string, i: number) => `${headers[i]}: ${cell}`).join(', '),
+ )
+ .map((line: string) => `- ${line}`)
+ .join('\n');
+ },
+ );
+
+ // Headings: remove # prefix
+ text = text.replaceAll(/^#{1,6}\s+(.+)/gm, '$1');
+
+ // Blockquotes: replace > with vertical bar
+ text = text.replaceAll(/^>\s?/gm, '| ');
+
+ // Horizontal rules
+ text = text.replaceAll(/^[-*_]{3,}\s*$/gm, '---');
+
+ // --- Inline transforms ---
+
+ // Images: ![alt](url) β†’ alt
+ text = text.replaceAll(/!\[([^\]]*)\]\([^)]+\)/g, '$1');
+
+ // Links: [text](url) β†’ text (url)
+ text = text.replaceAll(/\[([^\]]+)\]\(([^)]+)\)/g, '$1 ($2)');
+
+ // Bold + italic: ***text*** or ___text___
+ text = text.replaceAll(/\*{3}(.+?)\*{3}/g, '$1');
+ text = text.replaceAll(/_{3}(.+?)_{3}/g, '$1');
+
+ // Bold: **text** or __text__
+ text = text.replaceAll(/\*{2}(.+?)\*{2}/g, '$1');
+ text = text.replaceAll(/_{2}(.+?)_{2}/g, '$1');
+
+ // Italic: *text* or _text_
+ text = text.replaceAll(/\*(.+?)\*/g, '$1');
+ text = text.replaceAll(/(^|[\s(])_(.+?)_([\s).,:;!?]|$)/g, '$1$2$3');
+
+ // Strikethrough: ~~text~~
+ text = text.replaceAll(/~~(.+?)~~/g, '$1');
+
+ // Inline code: `text`
+ text = text.replaceAll(/`([^`]+)`/g, '$1');
+
+ return text;
+}
diff --git a/src/server/services/bot/platforms/telegram/api.ts b/src/server/services/bot/platforms/telegram/api.ts
index 74a2a3956b6..110afb6b96c 100644
--- a/src/server/services/bot/platforms/telegram/api.ts
+++ b/src/server/services/bot/platforms/telegram/api.ts
@@ -19,6 +19,7 @@ export class TelegramApi {
log('sendMessage: chatId=%s', chatId);
const data = await this.call('sendMessage', {
chat_id: chatId,
+ parse_mode: 'HTML',
text: this.truncateText(text),
});
return { message_id: data.result.message_id };
@@ -30,6 +31,7 @@ export class TelegramApi {
await this.call('editMessageText', {
chat_id: chatId,
message_id: messageId,
+ parse_mode: 'HTML',
text: this.truncateText(text),
});
} catch (error: any) {
diff --git a/src/server/services/bot/platforms/telegram/client.ts b/src/server/services/bot/platforms/telegram/client.ts
index d4e27329fd7..a413fd8c20c 100644
--- a/src/server/services/bot/platforms/telegram/client.ts
+++ b/src/server/services/bot/platforms/telegram/client.ts
@@ -19,6 +19,7 @@ import {
import { formatUsageStats } from '../utils';
import { TELEGRAM_API_BASE, TelegramApi } from './api';
import { extractBotId, setTelegramWebhook } from './helpers';
+import { markdownToTelegramHTML } from './markdownToHTML';
const log = debug('bot-platform:telegram:bot');
@@ -142,6 +143,10 @@ class TelegramWebhookClient implements PlatformClient {
log('TelegramBot appId=%s registered %d commands', this.applicationId, commands.length);
}
+ formatMarkdown(markdown: string): string {
+ return markdownToTelegramHTML(markdown);
+ }
+
formatReply(body: string, stats?: UsageStats): string {
if (!stats || !this.config.settings?.showUsageStats) return body;
return `${body}\n\n${formatUsageStats(stats)}`;
diff --git a/src/server/services/bot/platforms/telegram/markdownToHTML.test.ts b/src/server/services/bot/platforms/telegram/markdownToHTML.test.ts
new file mode 100644
index 00000000000..8e2669b8d05
--- /dev/null
+++ b/src/server/services/bot/platforms/telegram/markdownToHTML.test.ts
@@ -0,0 +1,99 @@
+import { describe, expect, it } from 'vitest';
+
+import { markdownToTelegramHTML } from './markdownToHTML';
+
+describe('markdownToTelegramHTML', () => {
+ it('should convert bold', () => {
+ expect(markdownToTelegramHTML('**bold text**')).toBe('<b>bold text</b>');
+ });
+
+ it('should convert italic', () => {
+ expect(markdownToTelegramHTML('*italic text*')).toBe('<i>italic text</i>');
+ });
+
+ it('should convert bold + italic', () => {
+ expect(markdownToTelegramHTML('***bold italic***')).toBe('<b><i>bold italic</i></b>');
+ });
+
+ it('should convert strikethrough', () => {
+ expect(markdownToTelegramHTML('~~deleted~~')).toBe('<s>deleted</s>');
+ });
+
+ it('should convert inline code', () => {
+ expect(markdownToTelegramHTML('run `npm install` now')).toBe(
+ 'run <code>npm install</code> now',
+ );
+ });
+
+ it('should convert fenced code blocks', () => {
+ const input = '```typescript\nconst x = 1;\n```';
+ expect(markdownToTelegramHTML(input)).toBe(
+ '<pre><code class="language-typescript">const x = 1;</code></pre>',
+ );
+ });
+
+ it('should convert fenced code blocks without language', () => {
+ const input = '```\nplain code\n```';
+ expect(markdownToTelegramHTML(input)).toBe('<pre>plain code</pre>');
+ });
+
+ it('should convert links', () => {
+ expect(markdownToTelegramHTML('[Click](https://example.com)')).toBe(
+ '<a href="https://example.com">Click</a>',
+ );
+ });
+
+ it('should convert headings to bold', () => {
+ expect(markdownToTelegramHTML('# Title')).toBe('<b>Title</b>');
+ expect(markdownToTelegramHTML('## Subtitle')).toBe('<b>Subtitle</b>');
+ });
+
+ it('should convert blockquotes', () => {
+ expect(markdownToTelegramHTML('> quoted text')).toBe('<blockquote>quoted text</blockquote>');
+ });
+
+ it('should merge consecutive blockquotes', () => {
+ const input = '> line 1\n> line 2';
+ expect(markdownToTelegramHTML(input)).toBe('<blockquote>line 1\nline 2</blockquote>');
+ });
+
+ it('should escape HTML entities in text', () => {
+ expect(markdownToTelegramHTML('a < b > c & d')).toBe('a &lt; b &gt; c &amp; d');
+ });
+
+ it('should escape HTML entities inside code blocks', () => {
+ const input = '```\n<div>hello</div>\n```';
+ expect(markdownToTelegramHTML(input)).toBe('<pre>&lt;div&gt;hello&lt;/div&gt;</pre>');
+ });
+
+ it('should escape HTML entities in inline code', () => {
+ expect(markdownToTelegramHTML('use `<b>tag</b>`')).toBe(
+ 'use <code>&lt;b&gt;tag&lt;/b&gt;</code>',
+ );
+ });
+
+ it('should handle a complex document', () => {
+ const input = [
+ '# Hello',
+ '',
+ 'This is **bold** and *italic*.',
+ '',
+ '```js',
+ 'const x = 1 < 2;',
+ '```',
+ '',
+ '[Link](https://example.com)',
+ ].join('\n');
+
+ const result = markdownToTelegramHTML(input);
+ expect(result).toContain('<b>Hello</b>');
+ expect(result).toContain('<b>bold</b>');
+ expect(result).toContain('<i>italic</i>');
+ expect(result).toContain('<pre><code class="language-js">const x = 1 &lt; 2;</code></pre>');
+ expect(result).toContain('<a href="https://example.com">Link</a>');
+ });
+
+ it('should pass through plain text (with HTML escaping)', () => {
+ expect(markdownToTelegramHTML('Hello world')).toBe('Hello world');
+ });
+});
diff --git a/src/server/services/bot/platforms/telegram/markdownToHTML.ts b/src/server/services/bot/platforms/telegram/markdownToHTML.ts
new file mode 100644
index 00000000000..859b5e71105
--- /dev/null
+++ b/src/server/services/bot/platforms/telegram/markdownToHTML.ts
@@ -0,0 +1,87 @@
+/**
+ * Convert standard Markdown to Telegram-compatible HTML.
+ *
+ * Telegram HTML mode supports: <b>, <i>, <s>, <u>, <code>, <pre>, <a href="">,
+ * <blockquote>, <tg-spoiler>.
+ *
+ * Characters `<`, `>`, `&` must be escaped outside of tags.
+ *
+ * Strategy:
+ * 1. Extract fenced code blocks first (protect from further processing)
+ * 2. Escape HTML entities in remaining text
+ * 3. Convert inline markdown to HTML tags
+ * 4. Convert block-level markdown to HTML tags
+ * 5. Re-insert code blocks
+ */
+export function markdownToTelegramHTML(md: string): string {
+ // 1. Extract fenced code blocks and replace with placeholders
+ const codeBlocks: string[] = [];
+ let text = md.replaceAll(/^```(\w*)\n([\s\S]*?)^```/gm, (_match, lang: string, code: string) => {
+ const escaped = escapeHTML(code.replace(/\n$/, ''));
+ const block = lang
+ ? `<pre><code class="language-${lang}">${escaped}</code></pre>`
+ : `<pre>${escaped}</pre>`;
+ codeBlocks.push(block);
+ return `\x00CODEBLOCK_${codeBlocks.length - 1}\x00`;
+ });
+
+ // 2. Extract inline code and replace with placeholders
+ const inlineCodes: string[] = [];
+ text = text.replaceAll(/`([^`\n]+)`/g, (_match, code: string) => {
+ inlineCodes.push(`<code>${escapeHTML(code)}</code>`);
+ return `\x00INLINECODE_${inlineCodes.length - 1}\x00`;
+ });
+
+ // 3. Escape HTML entities in remaining text
+ text = escapeHTML(text);
+
+ // 4. Block-level transforms
+
+ // Headings β†’ bold
+ text = text.replaceAll(/^#{1,6}\s+(.+)/gm, '<b>$1</b>');
+
+ // Blockquotes
+ text = text.replaceAll(/^&gt;\s?(.*)/gm, '<blockquote>$1</blockquote>');
+ // Merge consecutive blockquote tags
+ text = text.replaceAll('</blockquote>\n<blockquote>', '\n');
+
+ // 5. Inline transforms (order matters: bold+italic first)
+
+ // Bold + italic: ***text*** or ___text___
+ text = text.replaceAll(/\*{3}(.+?)\*{3}/g, '<b><i>$1</i></b>');
+ text = text.replaceAll(/_{3}(.+?)_{3}/g, '<b><i>$1</i></b>');
+
+ // Bold: **text** or __text__
+ text = text.replaceAll(/\*{2}(.+?)\*{2}/g, '<b>$1</b>');
+ text = text.replaceAll(/_{2}(.+?)_{2}/g, '<b>$1</b>');
+
+ // Italic: *text* or _text_
+ text = text.replaceAll(/\*(.+?)\*/g, '<i>$1</i>');
+ text = text.replaceAll(/(^|[\s(])_(.+?)_([\s).,:;!?]|$)/g, '$1<i>$2</i>$3');
+
+ // Strikethrough: ~~text~~
+ text = text.replaceAll(/~~(.+?)~~/g, '<s>$1</s>');
+
+ // Links: [text](url) β€” already escaped, so &gt; etc. won't appear in url typically
+ // Need to handle the escaped brackets: after escapeHTML, [ and ] are unchanged, ( and ) too
+ text = text.replaceAll(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
+
+ // Images: ![alt](url) β†’ just show alt as link
+ text = text.replaceAll(/!\[([^\]]*)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
+
+ // 6. Re-insert inline code
+ text = text.replaceAll(/\0INLINECODE_(\d+)\0/g, (_match, idx: string) => {
+ return inlineCodes[Number.parseInt(idx)];
+ });
+
+ // 7. Re-insert code blocks
+ text = text.replaceAll(/\0CODEBLOCK_(\d+)\0/g, (_match, idx: string) => {
+ return codeBlocks[Number.parseInt(idx)];
+ });
+
+ return text;
+}
+
+function escapeHTML(text: string): string {
+ return text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');
+}
diff --git a/src/server/services/bot/platforms/types.ts b/src/server/services/bot/platforms/types.ts
index d7a3061df81..1d63c577e92 100644
--- a/src/server/services/bot/platforms/types.ts
+++ b/src/server/services/bot/platforms/types.ts
@@ -99,6 +99,16 @@ export interface PlatformClient {
/** Extract the chat/channel ID from a composite platformThreadId. */
extractChatId: (platformThreadId: string) => string;
+ /**
+ * Transform outbound Markdown content into a format the platform can render.
+ * Called before `formatReply` and `splitMessage`.
+ *
+ * Platforms that don't support Markdown (e.g. WeChat, QQ) should strip
+ * formatting to plain text. Platforms with native Markdown support can
+ * omit this method β€” the content is passed through as-is.
+ */
+ formatMarkdown?: (markdown: string) => string;
+
/**
* Format the final outbound reply from body content and optional usage stats.
* Each platform decides whether to render the stats and how to format them
@@ -277,6 +287,14 @@ export interface PlatformDefinition {
/** Whether to show webhook URL for manual configuration. When true, the UI displays the webhook endpoint for the user to copy. */
showWebhookUrl?: boolean;
+ /**
+ * Whether the platform supports rendering Markdown in messages.
+ * When false, outbound markdown is converted to plain text before sending,
+ * and the AI is instructed to avoid markdown formatting.
+ * Defaults to true.
+ */
+ supportsMarkdown?: boolean;
+
/**
* Whether the platform supports editing sent messages.
* When false, step progress updates are skipped and only the final reply is sent.
diff --git a/src/server/services/bot/platforms/wechat/client.ts b/src/server/services/bot/platforms/wechat/client.ts
index 64cb55ae290..58fbe4845a6 100644
--- a/src/server/services/bot/platforms/wechat/client.ts
+++ b/src/server/services/bot/platforms/wechat/client.ts
@@ -13,6 +13,7 @@ import {
updateBotRuntimeStatus,
} from '@/server/services/gateway/runtimeStatus';
+import { stripMarkdown } from '../stripMarkdown';
import {
type BotPlatformRuntimeContext,
type BotProviderConfig,
@@ -370,6 +371,10 @@ class WechatGatewayClient implements PlatformClient {
return extractChatId(platformThreadId);
}
+ formatMarkdown(markdown: string): string {
+ return stripMarkdown(markdown);
+ }
+
formatReply(body: string, stats?: UsageStats): string {
if (!stats || !this.config.settings?.showUsageStats) return body;
return `${body}\n\n${formatUsageStats(stats)}`;
diff --git a/src/server/services/bot/platforms/wechat/definition.ts b/src/server/services/bot/platforms/wechat/definition.ts
index cfd61decbc7..8e58e67064e 100644
--- a/src/server/services/bot/platforms/wechat/definition.ts
+++ b/src/server/services/bot/platforms/wechat/definition.ts
@@ -12,6 +12,7 @@ export const wechat: PlatformDefinition = {
setupGuideUrl: 'https://lobehub.com/docs/usage/channels/wechat',
},
schema,
+ supportsMarkdown: false,
supportsMessageEdit: false,
clientFactory: new WechatClientFactory(),
};
diff --git a/vite.config.ts b/vite.config.ts
index 64eb5615e7f..687d71ebd4a 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -108,10 +108,10 @@ export default defineConfig({
port: 9876,
host: true,
proxy: {
- '/api': 'http://localhost:3010',
- '/oidc': 'http://localhost:3010',
- '/trpc': 'http://localhost:3010',
- '/webapi': 'http://localhost:3010',
+ '/api': `http://localhost:${process.env.PORT || 3010}`,
+ '/oidc': `http://localhost:${process.env.PORT || 3010}`,
+ '/trpc': `http://localhost:${process.env.PORT || 3010}`,
+ '/webapi': `http://localhost:${process.env.PORT || 3010}`,
},
warmup: {
clientFiles: [