Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/adapter-azure-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"resolutions": {
"@langchain/core": "0.3.62",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-claude/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"resolutions": {
"@langchain/core": "0.3.62",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-deepseek/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"category": "ai",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-dify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-doubao/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-gemini/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53",
"koishi-plugin-chatluna": "^1.3.0-alpha.54",
"koishi-plugin-chatluna-storage-service": "^0.0.9"
},
"peerDependenciesMeta": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-hunyuan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-ollama/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"resolutions": {
"@langchain/core": "0.3.62",
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-openai-like/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-qwen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-rwkv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-spark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-wenxin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-zhipu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"koishi": {
"description": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-chatluna",
"description": "chatluna for koishi",
"version": "1.3.0-alpha.53",
"version": "1.3.0-alpha.54",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand Down
20 changes: 19 additions & 1 deletion packages/core/src/chains/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,11 @@ class DefaultChatChainSender {
): Promise<void> {
if (!messages?.length) return

if (this.config.isForwardMsg) {
if (
this.config.isForwardMsg &&
this.getMessageText(messages).length >
this.config.forwardMsgMinLength
) {
await this.sendAsForward(session, messages)
return
}
Expand Down Expand Up @@ -821,6 +825,20 @@ class DefaultChatChainSender {
}
return [message]
}

private getMessageText(message: (h[] | h | string)[]) {
return message
.map((element) => {
if (typeof element === 'string') {
return element
}
if (Array.isArray(element)) {
return h.select(element, 'text').toString()
}
return element.toString()
})
.join(' ')
}
Comment thread
dingyi222666 marked this conversation as resolved.
}

export interface ChainMiddlewareContext {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Config {
isNickNameWithContent: boolean
allowPrivate: boolean
isForwardMsg: boolean
forwardMsgMinLength: number
allowChatWithRoomName: boolean
msgCooldown: number
randomReplyFrequency: Computed<Awaitable<number>>
Expand Down Expand Up @@ -65,6 +66,7 @@ export const Config: Schema<Config> = Schema.intersect([
allowQuoteReply: Schema.boolean().default(false),
isReplyWithAt: Schema.boolean().default(false),
isForwardMsg: Schema.boolean().default(false),
forwardMsgMinLength: Schema.number().min(0).max(400).step(1).default(0),
privateChatWithoutCommand: Schema.boolean().default(true),
allowChatWithRoomName: Schema.boolean().default(false),
includeQuoteReply: Schema.boolean().default(true),
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/locales/en-US.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ $inner:
privateChatWithoutCommand: Enable direct conversation in private chats without commands.
allowChatWithRoomName: 'Enable room name prefix triggering. Note: May impact performance significantly. Recommended for use with filters in specific groups only.'
randomReplyFrequency: Set random reply frequency (0-100, where 0 means never and 100 means always).
forwardMsgMinLength: Set minimum length of forwarded messages (characters).
includeQuoteReply: Include quoted message content in replies.

- $desc: Response Options
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/locales/zh-CN.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ $inner:
allowAtReply: 是否允许通过 @ bot 来触发对话。
allowQuoteReply: 是否允许通过引用 bot 的消息来触发对话。
isReplyWithAt: 当 bot 回复时,是否引用原消息。
isForwardMsg: 是否以转发消息的形式发送 bot 的回复。
isForwardMsg: 是否以合并消息的形式发送 bot 的回复。
forwardMsgMinLength: 合并消息的最小应用长度。为 0 则总是应用。设置大于 0 时,只有发送的文本大于此长度才会转为合并消息。
privateChatWithoutCommand: 在私聊中是否允许无需命令直接与 bot 对话。
allowChatWithRoomName: 是否允许使用房间名前缀触发对话。注意:启用此选项可能会显著影响 ChatLuna 的性能,建议配合过滤器仅在特定群组中启用。
randomReplyFrequency: 设置随机回复的频率。
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-long-memory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53"
"koishi-plugin-chatluna": "^1.3.0-alpha.54"
},
"resolutions": {
"@langchain/core": "0.3.62",
Expand Down
7 changes: 3 additions & 4 deletions packages/extension-mcp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-chatluna-mcp-client",
"description": "MCP Client for ChatLuna",
"version": "1.3.0-alpha.5",
"version": "1.3.0-alpha.6",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -49,8 +49,7 @@
],
"dependencies": {
"@langchain/core": "0.3.62",
"@modelcontextprotocol/sdk": "^1.18.2",
"json-schema-to-zod": "^2.6.1",
"@modelcontextprotocol/sdk": "^1.19.1",
"zod": "3.25.76"
},
"devDependencies": {
Expand All @@ -60,7 +59,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53",
"koishi-plugin-chatluna": "^1.3.0-alpha.54",
"koishi-plugin-chatluna-storage-service": "^0.0.9"
},
"peerDependenciesMeta": {
Expand Down
9 changes: 9 additions & 0 deletions packages/extension-mcp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Context, Logger, Schema } from 'koishi'
import { createLogger } from 'koishi-plugin-chatluna/utils/logger'
import { ChatLunaMCPClientService } from './service'
import * as command from './command'
import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'

export let logger: Logger
export let plugin: ChatLunaPlugin

export function apply(ctx: Context, config: Config) {
logger = createLogger(ctx, 'chatluna-mcp-client')
Expand All @@ -14,6 +16,13 @@ export function apply(ctx: Context, config: Config) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
ctx.i18n.define('en-US', require('./locales/en-US'))

plugin = new ChatLunaPlugin(
ctx,
config as unknown as ChatLunaPlugin.Config,
Comment thread
dingyi222666 marked this conversation as resolved.
'mcp-client',
false
)

ctx.plugin(ChatLunaMCPClientService, config)

ctx.plugin(command, config)
Expand Down
35 changes: 20 additions & 15 deletions packages/extension-mcp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import { Context, Service } from 'koishi'
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
import { Config, logger } from '.'
import { Config, logger, plugin } from '.'
import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
import { tool } from '@langchain/core/tools'
import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
import { JsonSchema7Type } from 'zod-to-json-schema'
import { callTool } from './utils'

export class ChatLunaMCPClientService extends Service {
private _client: Client

private _plugin: ChatLunaPlugin
private _globalTools: Record<
string,
{
Expand All @@ -25,6 +23,8 @@ export class ChatLunaMCPClientService extends Service {
}
> = {}

private _plugin: ChatLunaPlugin

constructor(
ctx: Context,
public config: Config
Expand All @@ -33,29 +33,30 @@ export class ChatLunaMCPClientService extends Service {

this._client = new Client({
name: 'ChatLuna',
version: '1.0.0'
version: '1.0.0',
title: 'ChatLuna ModelContext Protocol Client',
description: 'A client for the ChatLuna ModelContext Protocol'
})

this._plugin = new ChatLunaPlugin(
ctx,
config as unknown as ChatLunaPlugin.Config,
'mcp-client',
false
)
this._plugin = plugin

ctx.on('ready', async () => {
logger.info('Preparing MCP client...')
logger.info('Initializing MCP client service')
const prepared = await this.prepareClient()

if (!prepared) {
logger.warn('MCP client not prepared, skipping registration')
logger.warn(
'Failed to initialize MCP client, skipping tool integration'
)
return
}

await this.registerClientToolsToSchema()

const toolLength = await this.registerClientTools()
logger.info(`MCP client found ${toolLength} tools`)
logger.info(
`MCP client initialized successfully with ${toolLength} tool(s) available`
)
})
}

Expand Down Expand Up @@ -87,6 +88,7 @@ export class ChatLunaMCPClientService extends Service {
return false
}

let availableServers = 0
for (const serverConfig of serverConfigs) {
const { command, args, env, cwd, url, type, headers } = serverConfig

Expand Down Expand Up @@ -133,6 +135,7 @@ export class ChatLunaMCPClientService extends Service {
try {
await this._client.connect(transport)
logger.debug('MCP client connected at', serverConfig)
availableServers++
} catch (error) {
logger.error(
`Failed to connect to server at ${JSON.stringify(
Expand All @@ -143,7 +146,7 @@ export class ChatLunaMCPClientService extends Service {
}
}

return true
return availableServers > 0
}

async registerClientToolsToSchema() {
Expand Down Expand Up @@ -208,7 +211,9 @@ export class ChatLunaMCPClientService extends Service {
name: mcpTool.name,
description: mcpTool.description,
responseFormat: 'content_and_artifact',
schema: mcpTool.inputSchema as JsonSchema7Type
schema: mcpTool.inputSchema as Parameters<
typeof tool
>[1]['schema']
}
)

Expand Down
4 changes: 2 additions & 2 deletions packages/extension-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-chatluna-plugin-common",
"description": "plugin service for agent mode of chatluna",
"version": "1.3.0-alpha.15",
"version": "1.3.0-alpha.16",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -71,7 +71,7 @@
},
"peerDependencies": {
"koishi": "^4.18.9",
"koishi-plugin-chatluna": "^1.3.0-alpha.53",
"koishi-plugin-chatluna": "^1.3.0-alpha.54",
"koishi-plugin-chatluna-knowledge-chat": "^1.0.23",
"koishi-plugin-chatluna-storage-service": "^0.0.9"
},
Expand Down
Loading