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: 0 additions & 2 deletions packages/core/resources/presets/sydney.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
keywords:
- sydney
- chatgpt
- gpt

prompts:
- role: system
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/llm-core/chat/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,26 @@ async function autoSummarizeTitle(
`Assistant: ${aiContent}`

try {
const result = await wrapper.model.invoke([new HumanMessage(prompt)])
const result = await wrapper.model.invoke([new HumanMessage(prompt)], {
configurable: {
id: conversationId
},
id: conversationId,
variables_hide: {
built: {
conversationId
}
}
})
const title = getMessageContent(result.content).trim().slice(0, 20)

if (!title) {
return
}

await chatluna.conversation.touchConversation(conversationId, {
title
title,
autoTitle: false
Comment thread
coderabbitai[bot] marked this conversation as resolved.
})
} catch (error) {
logger.error(error)
Expand Down
64 changes: 51 additions & 13 deletions packages/core/src/llm-core/chat/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import { emptyEmbeddings } from 'koishi-plugin-chatluna/llm-core/model/in_memory
import {
PlatformEmbeddingsClient,
PlatformModelAndEmbeddingsClient,
PlatformModelClient
PlatformModelClient,
PlatformModelEmbeddingsAndRerankerClient
} from 'koishi-plugin-chatluna/llm-core/platform/client'
import { ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model'
import {
ChatLunaBaseEmbeddings,
ChatLunaChatModel
} from 'koishi-plugin-chatluna/llm-core/platform/model'
import { PlatformService } from 'koishi-plugin-chatluna/llm-core/platform/service'
import {
ModelCapabilities,
ModelInfo
ModelInfo,
ModelType
} from 'koishi-plugin-chatluna/llm-core/platform/types'
import { parseRawModelName } from 'koishi-plugin-chatluna/llm-core/utils/count_tokens'
import {
Expand Down Expand Up @@ -48,37 +53,70 @@ export async function initEmbeddings(
return computed(() => emptyEmbeddings)
}

if (platform == null || modelName == null) {
logger.warn(
`Embeddings model ${model} is invalid, falling back to empty embeddings`
)
return computed(() => emptyEmbeddings)
}

const clientRef = await service.getClient(platform)
const info = service.findModel(platform, modelName)

return computed<Embeddings>(() => {
const client = clientRef.value

logger.info(`Init embeddings for %c`, model)

if (client == null || client instanceof PlatformModelClient) {
if (info.value == null) {
logger.warn(
`Platform ${platform} is not supported, falling back to fake embeddings`
`Embeddings model ${modelName} not found, falling back to empty embeddings`
)
return emptyEmbeddings
}

if (client instanceof PlatformEmbeddingsClient) {
return client.createModel(modelName)
if (info.value.type !== ModelType.embeddings) {
logger.warn(
`Model ${modelName} is not an embeddings model, falling back to empty embeddings`
)
return emptyEmbeddings
}

if (client instanceof PlatformModelAndEmbeddingsClient) {
const ref = client.createModel(modelName)
if (client == null || client instanceof PlatformModelClient) {
logger.warn(
`Platform ${platform} is not supported, falling back to empty embeddings`
)
return emptyEmbeddings
}

if (
client instanceof PlatformEmbeddingsClient ||
client instanceof PlatformModelAndEmbeddingsClient ||
client instanceof PlatformModelEmbeddingsAndRerankerClient
) {
try {
const ref = client.createModel(modelName)

if (ref instanceof ChatLunaBaseEmbeddings) {
return ref
}

if (ref instanceof ChatLunaChatModel) {
logger.warn(
`Model ${modelName} is not an embeddings model, falling back to fake embeddings`
`Model ${modelName} is not an embeddings model, falling back to empty embeddings`
)
return emptyEmbeddings
} catch (error) {
logger.warn(
`Embeddings model ${modelName} not found, falling back to empty embeddings`,
error
)
return emptyEmbeddings
}

return ref
}

logger.warn(
`Platform ${platform} is not supported, falling back to empty embeddings`
)
return emptyEmbeddings
})
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ chatluna:
broken: 'broken'
messages:
unavailable: 'The model {0} for this conversation is unavailable. Please contact an administrator to configure the API.'
preset_unavailable: 'The preset {0} for this conversation is unavailable. Please contact an administrator to configure the preset.'
new_success: 'Created conversation: {0}'
new_forbidden: 'Creating new conversations is disabled in the current route.'
admin_required: 'Conversation management requires administrator permission in the current route.'
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ chatluna:
broken: '已损坏'
messages:
unavailable: '当前会话对应的模型 {0} 不可用,请联系管理员配置 API。'
preset_unavailable: '当前会话对应的预设 {0} 不可用,请联系管理员配置预设。'
new_success: '已创建会话:{0}'
new_forbidden: '当前路由已禁止创建新会话。'
admin_required: '当前路由要求管理员权限才能管理会话。'
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/middlewares/model/resolve_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) {
ctx.chatluna.preset.getPreset(presetName, false).value !=
null

if (!presetExists) {
await context.send(
session.text(
'chatluna.conversation.messages.preset_unavailable',
[presetName]
)
)
return ChainMiddlewareRunStatus.STOP
}

if (
!presetExists ||
modelName.trim().length < 1 ||
modelName === '无' ||
modelName === 'empty'
Expand Down
35 changes: 29 additions & 6 deletions packages/core/src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class ChatLunaService extends Service<Config> {
this.ctx,
{
chatMode: conversation.chatMode,
autoTitle: conversation.autoTitle ?? true,
autoTitle: conversation.autoTitle === true,
botName: config.botNames[0],
preset: this.preset.getPreset(conversation.preset),
model: conversation.model,
Expand Down Expand Up @@ -333,15 +333,37 @@ export class ChatLunaService extends Service<Config> {
;[platformName, modelName] = parseRawModelName(platformName)
}

if (
platformName == null ||
modelName == null ||
modelName.length < 1 ||
modelName === '无'
) {
return computed(() => emptyEmbeddings)
}

const client = await service.getClient(platformName)
const info = service.findModel(platformName, modelName)

return computed(() => {
if (info.value == null) {
this.ctx.logger.warn(
`The embeddings model ${modelName} not found, return empty embeddings`
)
return emptyEmbeddings
}

if (info.value.type !== ModelType.embeddings) {
this.ctx.logger.warn(
`The model ${modelName} is not embeddings, return empty embeddings`
)
return emptyEmbeddings
}

if (client.value == null) {
if (platformName !== '无') {
this.ctx.logger.warn(
`The platform ${platformName} no available`
)
}
this.ctx.logger.warn(
`The platform ${platformName} no available, return empty embeddings`
)
return emptyEmbeddings
}

Expand All @@ -353,6 +375,7 @@ export class ChatLunaService extends Service<Config> {
}
} catch (error) {
this.ctx.logger.warn(`The model ${modelName} not found`, error)
return emptyEmbeddings
}

this.ctx.logger.warn(
Expand Down
2 changes: 1 addition & 1 deletion packages/extension-agent/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-chatluna-agent",
"description": "Agent framework for ChatLuna",
"version": "1.0.25",
"version": "1.0.26",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/extension-agent/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ export function createDefaultSubAgentConfig(): SubAgentConfig {
},
presetAgents: {},
defaults: {
skills: createPermissionRule('allow'),
mcp: createPermissionRule('inherit'),
tools: createPermissionRule('inherit'),
skills: createPermissionRule('deny'),
mcp: createPermissionRule('deny'),
tools: createPermissionRule('deny'),
Comment thread
dingyi222666 marked this conversation as resolved.
Comment thread
dingyi222666 marked this conversation as resolved.
computer: createPermissionRule('allow')
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/extension-agent/src/service/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ export class ChatLunaAgentMcpService {
const serverCfg = this.config.mcp.mcpServers[serverName]
if (!serverCfg) return

this.ctx.chatluna_agent?.permission.invalidateCache()
this._disposeTools(serverName)

const names = new Set<string>()
Expand Down Expand Up @@ -483,6 +484,8 @@ export class ChatLunaAgentMcpService {
delete this._tools[toolName]
}
}

this.ctx.chatluna_agent?.permission.invalidateCache()
}

private async _drop(name: string, clearTools = true) {
Expand All @@ -506,6 +509,8 @@ export class ChatLunaAgentMcpService {
}
}
}

this.ctx.chatluna_agent?.permission.invalidateCache()
}

private async _remove(name: string) {
Expand Down
15 changes: 6 additions & 9 deletions packages/extension-agent/src/service/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,12 @@ export class ChatLunaAgentPermissionService {
return false
}

if (
!matchRule(
name,
this.mergeRule(
info.permissions.tools,
this.config.subAgent.defaults.tools
)
)
) {
const rule = this.mergeRule(
info.permissions.tools,
this.config.subAgent.defaults.tools
)

if (!matchRule(name, rule)) {
return false
}

Expand Down
6 changes: 3 additions & 3 deletions packages/extension-agent/src/sub-agent/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ function createTools(
permission: ChatLunaAgentPermissionService,
info: SubAgentInfo
): ComputedRef<ChatLunaTool[]> {
const tools = ctx.chatluna.platform.getTools()

return computed(() =>
permission
.listTools()
.map((item) => item.name)
tools.value
.filter((name) => permission.canUseTool(info, name))
.map((name) => ctx.chatluna.platform.getTool(name))
Comment thread
dingyi222666 marked this conversation as resolved.
)
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"@types/turndown": "^5.0.6",
"atsc": "^2.1.0",
"koishi": "^4.18.9",
"koishi-plugin-chatluna-agent": "^1.0.25",
"koishi-plugin-adapter-onebot": "^6.8.0"
"koishi-plugin-adapter-onebot": "^6.8.0",
"koishi-plugin-chatluna-agent": "^1.0.25"
},
"peerDependencies": {
"koishi": "^4.18.9",
Expand Down