diff --git a/package.json b/package.json index a5dc0294e..2434ff6d1 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "build": "yarn process-dynamic-import --lint && yarn fast-build shared-prompt-renderer && yarn fast-build core && yarn fast-build", "bump": "yakumo version", "dep": "yakumo upgrade", - "test": "cross-env TS_NODE_PROJECT=packages/core/tests/tsconfig.json yakumo mocha -r tsconfig-paths/register -r esbuild-register -r yml-register --exit", "pub": "yakumo publish --tag latest", "pub:next": "yakumo publish --tag next", "lint": "yarn eslint packages --cache --ext=ts", diff --git a/packages/adapter-claude/src/requester.ts b/packages/adapter-claude/src/requester.ts index 4acb8c86b..b95a3e835 100644 --- a/packages/adapter-claude/src/requester.ts +++ b/packages/adapter-claude/src/requester.ts @@ -142,7 +142,7 @@ export class ClaudeRequester extends ModelRequester { : undefined }) - const iterator = sseIterable(response) + const iterator = sseIterable(response, params) const reasoningState = new ReasoningState() for await (const event of iterator) { diff --git a/packages/adapter-deepseek/src/requester.ts b/packages/adapter-deepseek/src/requester.ts index 541613b77..bb6ad482c 100644 --- a/packages/adapter-deepseek/src/requester.ts +++ b/packages/adapter-deepseek/src/requester.ts @@ -18,6 +18,7 @@ import { completionStream, createEmbeddings, createRequestContext, + createRequestSignal, parseOpenAIModelNameWithReasoningEffort, processStreamResponse } from '@chatluna/v1-shared-adapter' @@ -87,17 +88,31 @@ export class DeepseekRequester delete request.top_p } + const requestSignal = createRequestSignal(params) + try { const response = await this.post('chat/completions', request, { - signal: params.signal + signal: requestSignal.signal }) + requestSignal.clearTimeout() - yield* processStreamResponse(requestContext, sseIterable(response)) + yield* processStreamResponse( + requestContext, + sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }) + ) } catch (e) { + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } if (e instanceof ChatLunaError) { throw e } throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } diff --git a/packages/adapter-dify/src/requester.ts b/packages/adapter-dify/src/requester.ts index dc6b75b18..14ef1d482 100644 --- a/packages/adapter-dify/src/requester.ts +++ b/packages/adapter-dify/src/requester.ts @@ -31,6 +31,7 @@ import { isMessageContentText } from 'koishi-plugin-chatluna/utils/string' import { File, FormData } from 'undici' +import { createRequestSignal } from '@chatluna/v1-shared-adapter' import { mapMimeToFileType, resolveFilePayload, @@ -162,73 +163,94 @@ export class DifyRequester extends ModelRequester { requestBody.files = files } - const response = await this._post( - '/chat-messages', - requestBody, - config.apiKey, - { - signal: params.signal - } - ) + const requestSignal = createRequestSignal(params) + + try { + const response = await this._post( + '/chat-messages', + requestBody, + config.apiKey, + { + signal: requestSignal.signal + } + ) + requestSignal.clearTimeout() - const iterator = sseIterable(response) - let updatedDifyConversationId: string | undefined + const iterator = sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }) + let updatedDifyConversationId: string | undefined - for await (const event of iterator) { - const chunk = event.data + for await (const event of iterator) { + const chunk = event.data - if (chunk == null) { - continue - } + if (chunk == null) { + continue + } - let data: AssistantStreamResponse + let data: AssistantStreamResponse - try { - data = JSON.parse(chunk) - } catch (err) { - this.ctx.logger.error( - 'error when parsing dify stream response, Result:' + chunk - ) - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_FAILED, - new Error( - 'error when calling dify completion, Result: ' + chunk + try { + data = JSON.parse(chunk) + } catch (err) { + this.ctx.logger.error( + 'error when parsing dify stream response, Result:' + + chunk ) - ) - } + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + 'error when calling dify completion, Result: ' + + chunk + ) + ) + } - if (data.event === 'error') { - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_FAILED, - new Error( - 'error when calling dify completion, Result:' + chunk + if (data.event === 'error') { + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + 'error when calling dify completion, Result:' + + chunk + ) ) - ) - } + } - const content = data.answer + const content = data.answer - if (content != null) { - const messageChunk = new AIMessageChunk({ content }) - const generationChunk = new ChatGenerationChunk({ - message: messageChunk, - text: content - }) + if (content != null) { + const messageChunk = new AIMessageChunk({ content }) + const generationChunk = new ChatGenerationChunk({ + message: messageChunk, + text: content + }) - yield generationChunk - } + yield generationChunk + } - updatedDifyConversationId = data.conversation_id + updatedDifyConversationId = data.conversation_id - if (data.event === 'message_end') { - await this.updateDifyConversationId( - conversationId, - config.workflowName, - updatedDifyConversationId, - difyUser - ) - break + if (data.event === 'message_end') { + await this.updateDifyConversationId( + conversationId, + config.workflowName, + updatedDifyConversationId, + difyUser + ) + break + } } + } catch (e) { + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } + if (e instanceof ChatLunaError) { + throw e + } + throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } @@ -267,60 +289,81 @@ export class DifyRequester extends ModelRequester { requestBody.files = files } - const response = await this._post( - '/workflows/run', - requestBody, - config.apiKey, - { - signal: params.signal - } - ) + const requestSignal = createRequestSignal(params) - const iterator = sseIterable(response) + try { + const response = await this._post( + '/workflows/run', + requestBody, + config.apiKey, + { + signal: requestSignal.signal + } + ) + requestSignal.clearTimeout() - for await (const event of iterator) { - const chunk = event.data + const iterator = sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }) - if (chunk == null) { - continue - } + for await (const event of iterator) { + const chunk = event.data + + if (chunk == null) { + continue + } - let data: AssistantStreamResponse + let data: AssistantStreamResponse - try { - data = JSON.parse(chunk) - } catch (err) { - this.ctx.logger.error( - 'error when parsing dify stream response, Result:' + chunk - ) - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_FAILED, - new Error( - 'error when calling dify completion, Result: ' + chunk + try { + data = JSON.parse(chunk) + } catch (err) { + this.ctx.logger.error( + 'error when parsing dify stream response, Result:' + + chunk ) - ) - } + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + 'error when calling dify completion, Result: ' + + chunk + ) + ) + } - if (data.event === 'error') { - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_FAILED, - new Error( - 'error when calling dify completion, Result:' + chunk + if (data.event === 'error') { + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + 'error when calling dify completion, Result:' + + chunk + ) ) - ) - } + } - const content = data.answer + const content = data.answer - if (content != null) { - const messageChunk = new AIMessageChunk({ content }) - const generationChunk = new ChatGenerationChunk({ - message: messageChunk, - text: content - }) + if (content != null) { + const messageChunk = new AIMessageChunk({ content }) + const generationChunk = new ChatGenerationChunk({ + message: messageChunk, + text: content + }) - yield generationChunk + yield generationChunk + } + } + } catch (e) { + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } + if (e instanceof ChatLunaError) { + throw e } + throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } diff --git a/packages/adapter-doubao/src/requester.ts b/packages/adapter-doubao/src/requester.ts index 22ffc2866..75c8ad44f 100644 --- a/packages/adapter-doubao/src/requester.ts +++ b/packages/adapter-doubao/src/requester.ts @@ -100,7 +100,7 @@ export class DoubaoRequester signal: params.signal }) - const iterator = sseIterable(response) + const iterator = sseIterable(response, params) yield* processStreamResponse(requestContext, iterator) } catch (e) { if (e instanceof ChatLunaError) { diff --git a/packages/adapter-gemini/src/requester.ts b/packages/adapter-gemini/src/requester.ts index b46b30319..b370640de 100644 --- a/packages/adapter-gemini/src/requester.ts +++ b/packages/adapter-gemini/src/requester.ts @@ -54,6 +54,7 @@ import { ToolCallChunk } from '@langchain/core/messages/tool' import { RunnableConfig } from '@langchain/core/runnables' import { trackLogToLocal } from 'koishi-plugin-chatluna/utils/logger' import { + createRequestSignal, createUsageMetadata, ReasoningState } from '@chatluna/v1-shared-adapter' @@ -103,18 +104,24 @@ export class GeminiRequester modelConfig, this._pluginConfig ) + const requestSignal = createRequestSignal(params) try { const response = await this._post( `models/${modelConfig.model}:streamGenerateContent?alt=sse`, chatGenerationParams, { - signal: params.signal + signal: requestSignal.signal } ) + requestSignal.clearTimeout() await checkResponse(response) - yield* this._processResponseStream(response) + yield* this._processResponseStream( + response, + params.timeout, + requestSignal.signal + ) } catch (e) { if (this.ctx.chatluna.currentConfig.isLog) { await trackLogToLocal( @@ -124,11 +131,16 @@ export class GeminiRequester 'warn' ) } + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } if (e instanceof ChatLunaError) { throw e } else { throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) } + } finally { + requestSignal.dispose() } } @@ -144,13 +156,14 @@ export class GeminiRequester modelConfig, this._pluginConfig ) + const requestSignal = createRequestSignal(params) try { const response = await this._post( `models/${modelConfig.model}:generateContent`, chatGenerationParams, { - signal: params.signal + signal: requestSignal.signal } ) @@ -177,6 +190,8 @@ export class GeminiRequester } else { throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) } + } finally { + requestSignal.dispose() } } @@ -184,15 +199,18 @@ export class GeminiRequester params: EmbeddingsRequestParams ): Promise { const input = this._prepareEmbeddingsInput(params.input) + const requestSignal = createRequestSignal(params) try { const response = await this._post( `models/${params.model}:batchEmbedContents`, - this._createEmbeddingsRequest(params.model, input) + this._createEmbeddingsRequest(params.model, input), + { signal: requestSignal.signal } ) return await this._processEmbeddingsResponse(response) } catch (e) { + if (e instanceof ChatLunaError) throw e const error = new Error( 'error when calling gemini embeddings, Error: ' + e.message ) @@ -200,6 +218,8 @@ export class GeminiRequester error.cause = e.cause logger.debug(e) throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, error) + } finally { + requestSignal.dispose() } } @@ -363,14 +383,20 @@ export class GeminiRequester return result } - private async *_processResponseStream(response: fetchType.Response) { + private async *_processResponseStream( + response: fetchType.Response, + timeout?: number, + signal?: AbortSignal + ) { const { groundingContent, currentGroundingIndex } = this._createStreamContext() const iterable = this._setupStreamTransform( response, groundingContent, - currentGroundingIndex + currentGroundingIndex, + timeout, + signal ) const reasoningState = new ReasoningState() @@ -409,7 +435,9 @@ export class GeminiRequester private _setupStreamTransform( response: fetchType.Response | ChatResponse, groundingContent: { value: string }, - currentGroundingIndex: { value: number } + currentGroundingIndex: { value: number }, + timeout?: number, + signal?: AbortSignal ) { const transformToChatPartStream = this._createTransformStream( groundingContent, @@ -424,7 +452,10 @@ export class GeminiRequester return } - for await (const chunk of sseIterable(response)) { + for await (const chunk of sseIterable(response, { + timeout, + signal + })) { controller.enqueue(chunk.data) } controller.close() diff --git a/packages/adapter-hunyuan/src/requester.ts b/packages/adapter-hunyuan/src/requester.ts index 5def3d26a..d8f3bd3ad 100644 --- a/packages/adapter-hunyuan/src/requester.ts +++ b/packages/adapter-hunyuan/src/requester.ts @@ -170,7 +170,7 @@ export class HunyuanRequester yield* processStreamResponse( requestContext, - validateHunyuanStream(sseIterable(response)) + validateHunyuanStream(sseIterable(response, params)) ) } catch (e) { if (e instanceof ChatLunaError) { diff --git a/packages/adapter-ollama/src/requester.ts b/packages/adapter-ollama/src/requester.ts index e37c7074d..59ca04eaa 100644 --- a/packages/adapter-ollama/src/requester.ts +++ b/packages/adapter-ollama/src/requester.ts @@ -140,7 +140,11 @@ export class OllamaRequester let buffer = '' - for await (const rawData of rawSeeAsIterable(response, 0)) { + for await (const rawData of rawSeeAsIterable( + response, + 0, + params.signal + )) { buffer += rawData const parts = buffer.split('\n') diff --git a/packages/adapter-qwen/src/requester.ts b/packages/adapter-qwen/src/requester.ts index 3954351a1..065671764 100644 --- a/packages/adapter-qwen/src/requester.ts +++ b/packages/adapter-qwen/src/requester.ts @@ -22,6 +22,7 @@ import { buildChatCompletionParams, createEmbeddings, createRequestContext, + createRequestSignal, processStreamResponse } from '@chatluna/v1-shared-adapter' @@ -85,23 +86,34 @@ export class QWenRequester baseRequest.enable_search = this._pluginConfig.enableSearch } + const requestSignal = createRequestSignal(params) + try { const response = await this.post('chat/completions', baseRequest, { - signal: params.signal + signal: requestSignal.signal }) + requestSignal.clearTimeout() - const iterator = sseIterable(response) + const iterator = sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }) const streamChunks = processStreamResponse(requestContext, iterator) for await (const chunk of streamChunks) { yield chunk } } catch (e) { + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } if (e instanceof ChatLunaError) { throw e } else { throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) } + } finally { + requestSignal.dispose() } } diff --git a/packages/adapter-spark/src/requester.ts b/packages/adapter-spark/src/requester.ts index d7341923d..b29bd2ad3 100644 --- a/packages/adapter-spark/src/requester.ts +++ b/packages/adapter-spark/src/requester.ts @@ -119,7 +119,7 @@ export class SparkRequester extends ModelRequester { yield* processStreamResponse( requestContext, - this._validateSparkStream(sseIterable(response)) + this._validateSparkStream(sseIterable(response, params)) ) } catch (e) { if (e instanceof ChatLunaError) { diff --git a/packages/adapter-wenxin/src/requester.ts b/packages/adapter-wenxin/src/requester.ts index 87f5c905a..b3708d86a 100644 --- a/packages/adapter-wenxin/src/requester.ts +++ b/packages/adapter-wenxin/src/requester.ts @@ -96,7 +96,10 @@ export class WenxinRequester signal: params.signal }) - yield* processStreamResponse(requestContext, sseIterable(response)) + yield* processStreamResponse( + requestContext, + sseIterable(response, params) + ) } catch (e) { if (e instanceof ChatLunaError) { throw e diff --git a/packages/adapter-zhipu/src/requester.ts b/packages/adapter-zhipu/src/requester.ts index b63b58758..a6eca4a50 100644 --- a/packages/adapter-zhipu/src/requester.ts +++ b/packages/adapter-zhipu/src/requester.ts @@ -100,7 +100,10 @@ export class ZhipuRequester signal: params.signal }) - yield* processStreamResponse(requestContext, sseIterable(response)) + yield* processStreamResponse( + requestContext, + sseIterable(response, params) + ) } catch (e) { if (e instanceof ChatLunaError) { throw e diff --git a/packages/core/src/llm-core/agent/agent.ts b/packages/core/src/llm-core/agent/agent.ts index 83a6cdae5..b21403cc7 100644 --- a/packages/core/src/llm-core/agent/agent.ts +++ b/packages/core/src/llm-core/agent/agent.ts @@ -51,19 +51,20 @@ export interface CreateAgentOptions { export interface AgentGenerateOptions { prompt: string | HumanMessage session?: Session - conversationId?: string + conversationId: string + requestId: string + source?: 'chatluna' | 'character' history?: BaseMessage[] // eslint-disable-next-line @typescript-eslint/no-explicit-any variables?: Record signal?: AbortSignal maxToken?: number + maxTokenLimit?: number messageQueue?: MessageQueue pauseGate?: (signal?: AbortSignal) => Promise toolMask?: ToolMask subagentContext?: SubagentContext - source?: 'chatluna' | 'character' onStep?: (event: AgentEvent) => Promise | void - requestId?: string onToken?: (token: string) => Promise | void onChunk?: (chunk: BaseMessageChunk) => Promise | void callbacks?: Callbacks @@ -125,39 +126,40 @@ export function createAgent(options: CreateAgentOptions): ChatLunaAgent { : input.prompt const text = getMessageContent(message.content) const history = input.history ?? [] - const vars = { - ...(input.variables ?? {}), - prompt: text, - built: { - conversationId: input.conversationId, - requestId: input.requestId, - userId: input.session?.userId, - guildId: input.session?.guildId, - channelId: input.session?.channelId, - chatPlatform: input.session?.platform - } - } - const toolMask = - input.subagentContext?.toolMask ?? - input.toolMask ?? - options.toolMask const ctx = { kind: input.subagentContext ? 'subagent' : 'main', agentId: id, agentName: name, conversationId: input.conversationId, - parentConversationId: - input.subagentContext?.parentConversationId, requestId: input.requestId, - source: input.source, + source: input.source ?? 'chatluna', userId: input.session?.userId, guildId: input.session?.guildId, channelId: input.session?.channelId, - toolMask, + toolMask: input.toolMask ?? options.toolMask, subagentContext: input.subagentContext } satisfies AgentRunContext + const maxTokens = + input.maxToken ?? + options.prompt.preset.value.config.maxOutputToken + const vars = { + ...(input.variables ?? {}), + prompt: text, + built: { + conversationId: ctx.conversationId, + requestId: ctx.requestId, + userId: ctx.userId, + guildId: ctx.guildId, + channelId: ctx.channelId, + chatPlatform: input.session?.platform + } + } - toolsRef.update(input.session, history.concat(message), toolMask) + toolsRef.update( + input.session, + history.concat(message), + ctx.toolMask + ) const bound = runner.value.withConfig({ configurable: { @@ -172,13 +174,12 @@ export function createAgent(options: CreateAgentOptions): ChatLunaAgent { { input: message, chat_history: [...history], + maxTokens, + maxTokenLimit: input.maxTokenLimit, variables: vars, variables_hide: vars, configurable: { session: input.session, - conversationId: input.conversationId, - toolMask, - subagentContext: input.subagentContext, agentContext: ctx } }, @@ -203,12 +204,7 @@ export function createAgent(options: CreateAgentOptions): ChatLunaAgent { configurable: { session: input.session, model: options.llm.value, - conversationId: input.conversationId, preset: name, - userId: input.session?.userId, - toolMask, - subagentContext: input.subagentContext, - source: input.source, agentContext: ctx } } as ChatLunaToolRunnable @@ -318,18 +314,38 @@ class AgentTool extends StructuredTool { _: unknown, runConfig?: ChatLunaToolRunnable ) { + const parent = runConfig!.configurable.agentContext + const runId = randomUUID() + const parentSub = parent.subagentContext + const depth = (parentSub?.depth ?? 0) + 1 + const maxDepth = parentSub?.maxDepth ?? 1 + if (parentSub?.disableHandoff || depth > maxDepth) { + throw new Error(`Maximum sub-agent depth ${maxDepth} reached`) + } + + const subagentContext: SubagentContext = { + parentConversationId: parent.conversationId, + depth, + maxDepth, + disableHandoff: depth >= maxDepth, + traceInfo: { + runId, + parentAgent: parent.agentName, + startedAt: Date.now(), + parentRequestId: parent.requestId + } + } + const result = await this._agent.generate({ prompt: input.prompt, session: runConfig?.configurable?.session, - conversationId: runConfig?.configurable?.conversationId, - toolMask: runConfig?.configurable?.toolMask, - subagentContext: - runConfig?.configurable?.agentContext?.subagentContext, + conversationId: `subagent:${runId}`, + requestId: runId, + source: parent.source, + toolMask: parent.toolMask, + subagentContext, signal: runConfig?.signal, - callbacks: runConfig?.callbacks, - source: ( - runConfig?.configurable as { source?: 'chatluna' | 'character' } - )?.source + callbacks: runConfig?.callbacks }) return result.output diff --git a/packages/core/src/llm-core/agent/executor.ts b/packages/core/src/llm-core/agent/executor.ts index 1296c90f6..549ea23b6 100644 --- a/packages/core/src/llm-core/agent/executor.ts +++ b/packages/core/src/llm-core/agent/executor.ts @@ -54,6 +54,15 @@ export class AgentRunner extends Runnable { ): AsyncGenerator { const configurable = (config?.configurable ?? {}) as AgentRuntimeConfigurable + const agentConfig = { + ...config, + ...(input['maxTokens'] == null + ? {} + : { maxTokens: input['maxTokens'] }), + ...(input['maxTokenLimit'] == null + ? {} + : { maxTokenLimit: input['maxTokenLimit'] }) + } for await (const event of runAgent({ agent: this.agent, @@ -63,7 +72,7 @@ export class AgentRunner extends Runnable { maxIterations: this.maxIterations, handleParsingErrors: this.handleParsingErrors, handleToolRuntimeErrors: this.handleToolRuntimeErrors, - config + config: agentConfig })) { if (event.type === 'tool-call') { for (const action of event.actions) { diff --git a/packages/core/src/llm-core/agent/legacy-executor.ts b/packages/core/src/llm-core/agent/legacy-executor.ts index f2ac1bd86..26fbac37c 100644 --- a/packages/core/src/llm-core/agent/legacy-executor.ts +++ b/packages/core/src/llm-core/agent/legacy-executor.ts @@ -22,6 +22,7 @@ import { BaseChain, ChainInputs } from 'koishi-plugin-chatluna/llm-core/chain/base' +import { isRequestFailure } from 'koishi-plugin-chatluna/utils/error' import { AgentAction, AgentCallbackEvent, @@ -118,9 +119,10 @@ async function executeTools( } as AgentStep } - const mask = - config?.configurable?.['toolMask'] ?? - config?.configurable?.['subagentContext']?.['toolMask'] + const context = ( + config?.configurable as AgentRuntimeConfigurable | undefined + )?.agentContext + const mask = context?.toolMask if (mask && !applyToolMask(action.tool, mask)) { const allowed = Object.values(toolMap) .map((item) => item.name) @@ -137,10 +139,7 @@ async function executeTools( } as AgentStep } - const callMask = - config?.configurable?.['toolMask']?.toolCallMask ?? - config?.configurable?.['subagentContext']?.['toolMask'] - ?.toolCallMask + const callMask = context?.toolMask?.toolCallMask if (callMask && !applyToolMask(action.tool, callMask)) { return { action, @@ -229,7 +228,8 @@ async function plan( input: ChainValues, steps: AgentStep[], scratchpad: ScratchpadEntry[], - config: RunnableConfig | undefined + config: RunnableConfig | undefined, + signal?: AbortSignal ) { const stream = await agent.stream( { @@ -243,6 +243,8 @@ async function plan( let result: AgentAction[] | AgentAction | AgentFinish | undefined for await (const chunk of stream) { + checkAborted(signal) + if (result !== undefined) { throw new Error('Multiple outputs from agent stream') } @@ -314,14 +316,37 @@ export async function* runAgent( options.input, steps, scratchpad, - config + config, + signal ) } catch (e) { - if (!(e instanceof OutputParserException)) { + checkAborted(signal) + + if (e instanceof OutputParserException) { + output = [toParsingErrorAction(handleParsingErrors, e)] + } else if ( + runtime.agentContext?.kind === 'subagent' && + !signal?.aborted && + isRequestFailure(e) + ) { + yield { + type: 'round-decision', + canContinue: false + } + + yield { + type: 'done', + output: + 'Subtask execution was interrupted; the workspace ' + + 'may have been modified.', + log: e instanceof Error ? e.message : String(e), + steps + } + + return + } else { throw e } - - output = [toParsingErrorAction(handleParsingErrors, e)] } checkAborted(signal) @@ -400,7 +425,8 @@ export async function* runAgent( scratchpad, options.input, model, - config?.configurable?.['conversationId'] ?? '', + (config?.configurable as AgentRuntimeConfigurable) + ?.agentContext?.conversationId ?? '', inputTokens, signal ) @@ -465,7 +491,7 @@ export async function* runAgent( /** * Compress scratchpad when input tokens approach context limit. - * Summarizes early scratchpad + chat_history, keeps recent entries. + * Summarizes the scratchpad prefix + chat_history and keeps the latest batch. */ async function compressScratchpad( scratchpad: ScratchpadEntry[], @@ -483,15 +509,23 @@ async function compressScratchpad( if (!limit || limit <= 0 || inputTokens < limit * 0.85) return + const keepIndex = scratchpad + .map( + (entry) => + !('messages' in entry) && + (entry.action.messageLog?.length ?? 0) > 0 + ) + .lastIndexOf(true) + const count = keepIndex < 0 ? scratchpad.length : keepIndex + if (count === 0) return + logger.info( - '[ScratchpadCompress] %d tokens exceed 85%% of %d, compressing', + '[ScratchpadCompress] %d provider input tokens reached usable limit %d, compressing', inputTokens, limit ) - const keepCount = Math.min(3, scratchpad.length) - const toCompress = scratchpad.slice(0, scratchpad.length - keepCount) - if (toCompress.length === 0) return + const toCompress = scratchpad.slice(0, count) const chatHistory = (input['chat_history'] ?? []) as BaseMessage[] const chatPart = chatHistory @@ -547,14 +581,15 @@ async function compressScratchpad( additional_kwargs: { source: 'scratchpad-compression' } }) ] - scratchpad.splice(0, scratchpad.length - keepCount) + scratchpad.splice(0, count) logger.info( '[ScratchpadCompress] Compressed %d entries, kept %d', - toCompress.length, - keepCount + count, + scratchpad.length ) } catch (e) { + checkAborted(signal) logger.error('[ScratchpadCompress] Failed:', e) } } diff --git a/packages/core/src/llm-core/agent/openai/output_parser.ts b/packages/core/src/llm-core/agent/openai/output_parser.ts index 6bd399898..cece2ba09 100644 --- a/packages/core/src/llm-core/agent/openai/output_parser.ts +++ b/packages/core/src/llm-core/agent/openai/output_parser.ts @@ -162,6 +162,16 @@ export class OpenAIToolsAgentOutputParser extends BaseOutputParser< log: content ?? '' } + if ( + (message instanceof AIMessageChunk || + message instanceof AIMessage) && + message.invalid_tool_calls?.length > 0 + ) { + throw new OutputParserException( + `Invalid or incomplete tool call arguments: ${JSON.stringify(message.invalid_tool_calls)}` + ) + } + if ( (message instanceof AIMessageChunk || message instanceof AIMessage) && diff --git a/packages/core/src/llm-core/agent/sub-agent/executor.ts b/packages/core/src/llm-core/agent/sub-agent/executor.ts index fc7569505..3f5c57d49 100644 --- a/packages/core/src/llm-core/agent/sub-agent/executor.ts +++ b/packages/core/src/llm-core/agent/sub-agent/executor.ts @@ -12,8 +12,13 @@ import type { ChatEvents } from 'koishi-plugin-chatluna/services/chat' import { getMessageContent } from 'koishi-plugin-chatluna/utils/string' import { observationToMessageContent } from '../legacy-executor' import { MessageQueue } from '../types' -import type { AgentEvent, AgentStep, SubagentContext, ToolMask } from '../types' -import type { ChatLunaToolRunnable } from '../../platform/types' +import type { + AgentEvent, + AgentRunContext, + AgentStep, + SubagentContext, + ToolMask +} from '../types' import type { ChatLunaAgent } from '../agent' import type { ActiveAgentTaskRun, @@ -45,30 +50,27 @@ export async function runAgentTask(options: { target: AgentTaskTarget prompt: string session: Session - conversationId: string - source: 'chatluna' | 'character' - parent?: SubagentContext + parentContext: AgentRunContext signal?: AbortSignal - runConfig?: ChatLunaToolRunnable }) { const runId = randomUUID() const toolMask: ToolMask = options.target.toolMask ?? - options.parent?.toolMask ?? - (options.runConfig?.configurable as { toolMask?: ToolMask }) - ?.toolMask ?? { mode: 'all' as const, allow: [], deny: [] } + options.parentContext.toolMask ?? { + mode: 'all' as const, + allow: [], + deny: [] + } const subCtx: SubagentContext = { - agentId: options.target.agent.id, - agentName: options.target.agent.name, parentConversationId: options.task.parentConversationId, depth: options.task.depth, maxDepth: options.task.maxDepth, - toolMask, disableHandoff: options.task.depth >= options.task.maxDepth, traceInfo: { runId, parentAgent: options.task.parentAgent, - startedAt: Date.now() + startedAt: Date.now(), + parentRequestId: options.parentContext.requestId } } @@ -101,8 +103,7 @@ export async function runAgentTask(options: { } : undefined - const requestId = - options.runConfig?.configurable?.agentContext?.requestId ?? runId + const requestId = runId // The sub-agent must not inherit the parent agent's callbacks, otherwise // its intermediate tokens and tool calls would bubble up and be streamed @@ -113,7 +114,7 @@ export async function runAgentTask(options: { : await options.session.app.chatluna.resolveCallbacks({ session: options.session, conversation: { - id: options.conversationId + id: options.task.conversationId } as ConversationRecord, message: { content: options.prompt } as Message, event: {} as ChatEvents, @@ -160,7 +161,10 @@ export async function runAgentTask(options: { prompt: options.prompt, session: options.session, conversationId: options.task.conversationId, - requestId, + requestId: runId, + source: options.parentContext.source, + toolMask, + subagentContext: subCtx, history: [...options.task.messages], signal, messageQueue: queue, @@ -180,9 +184,6 @@ export async function runAgentTask(options: { }) } }, - toolMask, - subagentContext: subCtx, - source: options.source, callbacks, onStep: async (event) => { await onTaskEvent(options.task, run, saveUser, event) @@ -260,7 +261,7 @@ async function notifyFinished( active: Map task: AgentTaskSession target: AgentTaskTarget - source: 'chatluna' | 'character' + parentContext: AgentRunContext }, run: AgentTaskRun, snapshot?: AgentTaskSessionSnapshot @@ -277,7 +278,7 @@ async function notifyFinished( const item = task?.activeRunId ? options.active.get(task.activeRunId) : undefined - if (item) { + if (item?.queue) { item.queue.push(message) return } @@ -292,7 +293,7 @@ async function notifyFinished( agentId: options.target.agent.id, agentName: options.target.agent.name, parentConversationId: parentId, - source: options.source, + source: options.parentContext.source, snapshot }) } catch (err) { @@ -370,7 +371,10 @@ async function onTaskEvent( } } - if (event.type === 'round-decision') { + if ( + event.type === 'round-decision' && + typeof event.canContinue === 'boolean' + ) { run.turnCount += 1 } @@ -393,13 +397,13 @@ export function createTaskSession( agent: ChatLunaAgent, parentConversationId: string, session: Session, - parent?: SubagentContext, + parent?: AgentRunContext, maxDepth = 1, promptContent?: string ): AgentTaskSession { const id = randomUUID() - const depth = (parent?.depth ?? 0) + 1 - const limit = parent?.maxDepth ?? maxDepth + const depth = (parent?.subagentContext?.depth ?? 0) + 1 + const limit = parent?.subagentContext?.maxDepth ?? maxDepth if (parent && depth > limit) { throw new Error(`Maximum sub-agent depth ${limit} reached`) } diff --git a/packages/core/src/llm-core/agent/sub-agent/runtime.ts b/packages/core/src/llm-core/agent/sub-agent/runtime.ts index e15c77f85..7ac516239 100644 --- a/packages/core/src/llm-core/agent/sub-agent/runtime.ts +++ b/packages/core/src/llm-core/agent/sub-agent/runtime.ts @@ -193,16 +193,9 @@ export function createTaskTool( }, async runTask(input, runConfig) { const action = input.action ?? 'run' - const parent = - runConfig?.configurable?.agentContext?.subagentContext + const parent = runConfig?.configurable?.agentContext const session = runConfig?.configurable?.session - const conversationId = runConfig?.configurable?.conversationId - const source = - ( - runConfig?.configurable as { - source?: 'chatluna' | 'character' - } - )?.source ?? 'chatluna' + const conversationId = parent?.conversationId if (action === 'list' || action === 'list_all') { if (!conversationId && action === 'list') @@ -280,11 +273,14 @@ export function createTaskTool( ].join('\n') } - if (parent && parent.depth >= parent.maxDepth) { - return `Cannot delegate: maximum nesting depth (${parent.maxDepth}) reached.` + if ( + parent?.subagentContext && + parent.subagentContext.depth >= parent.subagentContext.maxDepth + ) { + return `Cannot delegate: maximum nesting depth (${parent.subagentContext.maxDepth}) reached.` } - if (!session || !conversationId) + if (!session || !conversationId || !parent) return 'Task invocation is missing session context.' const raw = input.prompt?.trim() ?? '' @@ -292,9 +288,7 @@ export function createTaskTool( const agentName = name ?? task?.agentName const ctx = { session, - source, - conversationId, - parent, + agentContext: parent, runConfig } let target @@ -377,11 +371,8 @@ export function createTaskTool( target, prompt, session, - conversationId, - source, - parent, - signal: runConfig?.signal, - runConfig + parentContext: parent, + signal: runConfig?.signal }) } catch (err) { const run = getLatestTaskRun(runs, next.id) diff --git a/packages/core/src/llm-core/agent/sub-agent/types.ts b/packages/core/src/llm-core/agent/sub-agent/types.ts index c49104b4e..685525cba 100644 --- a/packages/core/src/llm-core/agent/sub-agent/types.ts +++ b/packages/core/src/llm-core/agent/sub-agent/types.ts @@ -3,7 +3,7 @@ import type { StructuredTool } from '@langchain/core/tools' import type { Awaitable, Session } from 'koishi' import type { ChatLunaAgent } from '../agent' import type { ChatLunaToolRunnable } from '../../platform/types' -import type { MessageQueue, SubagentContext, ToolMask } from '../types' +import type { AgentRunContext, MessageQueue, ToolMask } from '../types' export interface AgentTaskDescriptor { id: string @@ -110,12 +110,10 @@ export interface AgentTaskInput { export interface AgentTaskQueryContext { session?: Session - source?: 'chatluna' | 'character' + agentContext?: AgentRunContext } export interface AgentTaskResolveContext extends AgentTaskQueryContext { - conversationId?: string - parent?: SubagentContext runConfig?: ChatLunaToolRunnable } diff --git a/packages/core/src/llm-core/agent/tool-observation.ts b/packages/core/src/llm-core/agent/tool-observation.ts index a068a51c9..49f72fcc5 100644 --- a/packages/core/src/llm-core/agent/tool-observation.ts +++ b/packages/core/src/llm-core/agent/tool-observation.ts @@ -59,7 +59,7 @@ export function applyLoopGuidance( const tool = action.tool?.toLowerCase() ?? '' const text = toOutput(observation) - const failure = failed || isFailureObservation(text) + const failure = failed const hints: string[] = [] if (failure) { @@ -320,18 +320,6 @@ function appendObservation(observation: AgentObservation, text: string) { return observation } -function isFailureObservation(text: string) { - const value = text.slice(0, 800).toLowerCase() - return ( - value.startsWith('error') || - value.includes('"error"') || - value.includes('failed') || - value.includes('exception') || - value.includes('permission denied') || - value.includes('not found') - ) -} - function isNoProgressTool(tool: string) { return ( tool === 'grep' || diff --git a/packages/core/src/llm-core/agent/types.ts b/packages/core/src/llm-core/agent/types.ts index f74d0e1c2..da7f5634d 100644 --- a/packages/core/src/llm-core/agent/types.ts +++ b/packages/core/src/llm-core/agent/types.ts @@ -212,12 +212,11 @@ export interface ToolMask { export interface AgentRunContext { kind: 'main' | 'subagent' - agentId?: string - agentName?: string - conversationId?: string - parentConversationId?: string - requestId?: string - source?: 'chatluna' | 'character' + agentId: string + agentName: string + conversationId: string + requestId: string + source: 'chatluna' | 'character' userId?: string guildId?: string channelId?: string @@ -226,17 +225,15 @@ export interface AgentRunContext { } export interface SubagentContext { - agentId: string - agentName: string parentConversationId: string depth: number maxDepth: number - toolMask: ToolMask disableHandoff: boolean traceInfo: { runId: string parentAgent: string startedAt: number + parentRequestId?: string } } diff --git a/packages/core/src/llm-core/chain/agent_chat_chain.ts b/packages/core/src/llm-core/chain/agent_chat_chain.ts index 43c6e165a..4d243fb86 100644 --- a/packages/core/src/llm-core/chain/agent_chat_chain.ts +++ b/packages/core/src/llm-core/chain/agent_chat_chain.ts @@ -167,49 +167,34 @@ export class ChatLunaPluginChain }) } - async call({ - message, - signal, - session, - events, - conversationId, - variables, - maxToken, - messageQueue, - onAgentEvent, - toolMask: callToolMask, - subagentContext, - requestId, - callbacks - }: ChatLunaLLMCallArg): Promise { + async call(arg: ChatLunaLLMCallArg): Promise { + const ctx = { + kind: 'main', + agentId: arg.conversationId, + agentName: + this.preset.value.triggerKeyword[0] ?? arg.conversationId, + conversationId: arg.conversationId, + requestId: arg.requestId, + source: arg.source ?? 'chatluna', + userId: arg.session.userId, + guildId: arg.session.guildId, + channelId: arg.session.channelId, + toolMask: arg.toolMask + } satisfies AgentRunContext + const requests: ChainValues & { chat_history?: BaseMessage[] id?: string session?: Session } = { - input: message + input: arg.message } - const nextVars = Object.assign({}, variables ?? {}) - const toolMask = subagentContext?.toolMask ?? callToolMask + const nextVars = Object.assign({}, arg.variables ?? {}) + const toolMask = ctx.toolMask const chatHistory = this.historyMemory .chatHistory as KoishiChatMessageHistory const preset = this.preset.value - const ctx = { - kind: subagentContext ? 'subagent' : 'main', - agentId: conversationId, - agentName: preset.triggerKeyword[0] ?? conversationId, - conversationId, - parentConversationId: subagentContext?.parentConversationId, - requestId, - source: 'chatluna', - userId: session.userId, - guildId: session.guildId, - channelId: session.channelId, - toolMask, - subagentContext - } satisfies AgentRunContext - const messages = await chatHistory.getMessages() const history = this.agentMode === 'react' @@ -217,32 +202,34 @@ export class ChatLunaPluginChain : messages requests['chat_history'] = [...history] - requests['id'] = conversationId + requests['id'] = ctx.conversationId requests['variables'] = Object.assign(nextVars, { - prompt: getMessageContent(message.content) + prompt: getMessageContent(arg.message.content) }) requests['variables']['built'] = { - conversationId, - requestId, - userId: session.userId, - guildId: session.guildId, - channelId: session.channelId, - chatPlatform: session.platform + conversationId: ctx.conversationId, + requestId: ctx.requestId, + userId: ctx.userId, + guildId: ctx.guildId, + channelId: ctx.channelId, + chatPlatform: arg.session.platform } requests['variables_hide'] = requests['variables'] requests['configurable'] = { - session, - conversationId, - toolMask, + session: arg.session, agentContext: ctx } - this._toolsRef.update(session, messages.concat(message), toolMask) + this._toolsRef.update( + arg.session, + messages.concat(arg.message), + toolMask + ) const runner = this.runner.value.withConfig({ configurable: { - messageQueue, - onAgentEvent, + messageQueue: arg.messageQueue, + onAgentEvent: arg.onAgentEvent, agentContext: ctx } }) @@ -253,7 +240,8 @@ export class ChatLunaPluginChain const request = () => { const manager = - CallbackManager.configure(callbacks) ?? new CallbackManager() + CallbackManager.configure(arg.callbacks) ?? + new CallbackManager() manager.addHandler( CallbackManager.fromHandlers({ @@ -262,7 +250,7 @@ export class ChatLunaPluginChain out.llmOutput?.usage_metadata?.total_tokens ?? 0 }, async handleAgentAction(action: AgentAction) { - await events?.['llm-call-tool']?.( + await arg.events?.['llm-call-tool']?.( action.tool, action.toolInput, action.content, @@ -273,11 +261,11 @@ export class ChatLunaPluginChain logger.debug('Tool end:', sanitizeToolLogValue(out)) }, async handleLLMNewToken(token) { - await events?.['llm-new-token']?.(token) + await arg.events?.['llm-new-token']?.(token) }, async handleCustomEvent(name, data) { if (name === 'LLMNewChunk') { - await events?.['llm-new-chunk']?.(data) + await arg.events?.['llm-new-chunk']?.(data) } } }).handlers[0] @@ -286,19 +274,17 @@ export class ChatLunaPluginChain return runner.invoke( { ...requests, - maxTokens: maxToken + maxTokens: arg.maxToken, + maxTokenLimit: arg.maxTokenLimit }, { - signal, + signal: arg.signal, callbacks: manager, metadata: { chatlunaAgent: ctx }, configurable: { - session, + session: arg.session, model: this.llm, - conversationId, preset: preset.triggerKeyword[0], - userId: session.userId, - toolMask, agentContext: ctx } } @@ -306,9 +292,9 @@ export class ChatLunaPluginChain } for (let i = 0; i < 3; i++) { - if (signal?.aborted) { + if (arg.signal?.aborted) { throw ( - signal.reason ?? + arg.signal.reason ?? new ChatLunaError(ChatLunaErrorCode.ABORTED) ) } @@ -333,7 +319,7 @@ export class ChatLunaPluginChain } } - await events?.['llm-used-token-count']?.(usedToken) + await arg.events?.['llm-used-token-count']?.(usedToken) if (error != null && response == null) { if (error instanceof ChatLunaError) { diff --git a/packages/core/src/llm-core/chain/base.ts b/packages/core/src/llm-core/chain/base.ts index 4b05f63e8..b03fb1cf0 100644 --- a/packages/core/src/llm-core/chain/base.ts +++ b/packages/core/src/llm-core/chain/base.ts @@ -11,7 +11,7 @@ import { ChainValues } from '@langchain/core/utils/types' import { Session } from 'koishi' import { BufferMemory } from 'koishi-plugin-chatluna/llm-core/memory/langchain' import { ChatEvents } from '../../services/types' -import { SubagentContext, ToolMask } from '../agent' +import { ToolMask } from '../agent' import { ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model' import { ChatLunaError, @@ -43,15 +43,16 @@ export interface ChatLunaLLMCallArg { conversationId: string requestId: string session: Session + source?: 'chatluna' | 'character' // eslint-disable-next-line @typescript-eslint/no-explicit-any variables?: Record signal?: AbortSignal postHandler?: PostHandler maxToken?: number + maxTokenLimit?: number messageQueue?: MessageQueue onAgentEvent?: (event: AgentEvent) => Promise | void toolMask?: ToolMask - subagentContext?: SubagentContext callbacks?: Callbacks persist?: boolean } diff --git a/packages/core/src/llm-core/chain/chat_chain.ts b/packages/core/src/llm-core/chain/chat_chain.ts index 1455aed50..8ea1e82b9 100644 --- a/packages/core/src/llm-core/chain/chat_chain.ts +++ b/packages/core/src/llm-core/chain/chat_chain.ts @@ -18,6 +18,7 @@ import { } from 'koishi-plugin-chatluna/utils/error' import { ComputedRef } from '@vue/reactivity' import { getMessageContent } from 'koishi-plugin-chatluna/utils/string' +import type { AgentRunContext } from '../agent' export interface ChatLunaChatChainInput { botName: string @@ -103,9 +104,11 @@ export class ChatLunaChatChain session, conversationId, requestId, + source, variables, signal, maxToken, + maxTokenLimit, callbacks }: ChatLunaLLMCallArg): Promise { const requests: ChainValues = { @@ -127,9 +130,20 @@ export class ChatLunaChatChain chatPlatform: session.platform } requests['variables_hide'] = requests['variables'] + const agentContext = { + kind: 'main' as const, + agentId: conversationId, + agentName: this.botName, + conversationId, + requestId, + source: source ?? 'chatluna', + userId: session.userId, + guildId: session.guildId, + channelId: session.channelId + } satisfies AgentRunContext requests['configurable'] = { session, - conversationId + agentContext } requests['id'] = conversationId @@ -139,6 +153,7 @@ export class ChatLunaChatChain ...requests, stream, maxTokens: maxToken, + maxTokenLimit, signal }, events, diff --git a/packages/core/src/llm-core/chain/prompt.ts b/packages/core/src/llm-core/chain/prompt.ts index cb865f578..36ecb1fca 100644 --- a/packages/core/src/llm-core/chain/prompt.ts +++ b/packages/core/src/llm-core/chain/prompt.ts @@ -212,8 +212,7 @@ export class ChatLunaChatPrompt // Debug logging if (logger?.level === Logger.DEBUG) { - const name = - runtime.configurable?.subagentContext?.agentName || 'main' + const name = runtime.configurable?.agentContext?.agentName || 'main' if (runtime.usedTokens > runtime.sendTokenLimit) { logger?.debug( '[Agent %s] Used tokens: %c exceed limit: %c', diff --git a/packages/core/src/llm-core/chat/infinite_context.ts b/packages/core/src/llm-core/chat/infinite_context.ts index f4d2361b3..3acec1a7c 100644 --- a/packages/core/src/llm-core/chat/infinite_context.ts +++ b/packages/core/src/llm-core/chat/infinite_context.ts @@ -66,12 +66,13 @@ export async function compressIfNeeded( // Step 3: determine if compression is needed if (!force) { const invocation = model.invocationParams() - const maxTokenLimit = + const hardContext = model.getModelMaxContextSize() + const usableLimit = invocation.maxTokenLimit && invocation.maxTokenLimit > 0 ? invocation.maxTokenLimit - : model.getModelMaxContextSize() + : hardContext - if (!maxTokenLimit || maxTokenLimit <= 0) return noCompressResult() + if (!usableLimit || usableLimit <= 0) return noCompressResult() const presetMessages = Array.isArray(opts.preset?.value?.messages) ? (opts.preset.value.messages as BaseMessage[]) @@ -80,7 +81,9 @@ export async function compressIfNeeded( presetMessages, tokenCounter ) - const threshold = Math.floor(maxTokenLimit * (opts.threshold ?? 0.85)) + const threshold = Math.floor( + Math.min(usableLimit, hardContext * (opts.threshold ?? 0.85)) + ) if (inputTokens + presetTokens <= threshold) return noCompressResult() diff --git a/packages/core/src/llm-core/chat/runtime.ts b/packages/core/src/llm-core/chat/runtime.ts index d19053737..013ae9a91 100644 --- a/packages/core/src/llm-core/chat/runtime.ts +++ b/packages/core/src/llm-core/chat/runtime.ts @@ -4,7 +4,8 @@ import { parseRawModelName } from 'koishi-plugin-chatluna/llm-core/utils/count_t import { ModelType } from 'koishi-plugin-chatluna/llm-core/platform/types' import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + isAbortError } from 'koishi-plugin-chatluna/utils/error' import { transformMessageContentToElements } from 'koishi-plugin-chatluna/utils/koishi' import { buildVirtualSession } from 'koishi-plugin-chatluna/utils/virtual_session' @@ -355,7 +356,7 @@ function mapFail( let code = 'invoke_failed' let message = error.message - if (timedOut || controller.signal.aborted || error.name === 'AbortError') { + if (timedOut || controller.signal.aborted || isAbortError(error)) { code = timedOut ? 'timeout' : 'aborted' message = timedOut ? 'Chat invocation timed out.' diff --git a/packages/core/src/llm-core/platform/api.ts b/packages/core/src/llm-core/platform/api.ts index dadda0361..b093371d7 100644 --- a/packages/core/src/llm-core/platform/api.ts +++ b/packages/core/src/llm-core/platform/api.ts @@ -9,8 +9,9 @@ import { Context, Logger } from 'koishi' import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat' import * as fetchType from 'undici/types/fetch' import { - ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + isAbortError, + isErrorWithCode } from 'koishi-plugin-chatluna/utils/error' import type { EmbeddingsResult, @@ -152,13 +153,13 @@ export abstract class ModelRequester< ) } catch (e) { if ( - (e instanceof ChatLunaError && - (e.errorCode === ChatLunaErrorCode.NETWORK_ERROR || - e.errorCode === ChatLunaErrorCode.API_REQUEST_TIMEOUT || - e.errorCode === ChatLunaErrorCode.ABORTED || - e.errorCode === - ChatLunaErrorCode.API_UNSAFE_CONTENT)) || - e.name === 'AbortError' + isErrorWithCode(e, [ + ChatLunaErrorCode.NETWORK_ERROR, + ChatLunaErrorCode.API_REQUEST_TIMEOUT, + ChatLunaErrorCode.ABORTED, + ChatLunaErrorCode.API_UNSAFE_CONTENT + ]) || + isAbortError(e) ) { // Ignore network errors throw e diff --git a/packages/core/src/llm-core/platform/metrics.ts b/packages/core/src/llm-core/platform/metrics.ts index ba880f6f6..65bcbc225 100644 --- a/packages/core/src/llm-core/platform/metrics.ts +++ b/packages/core/src/llm-core/platform/metrics.ts @@ -15,6 +15,19 @@ export interface ChatLunaInvocationMetrics { } const chatlunaMetricsKey = 'chatluna_invocation_metrics' +/** + * Numeric metadata keys that must be pulled out of each streamed chunk before + * concat: AIMessageChunk.concat() would otherwise accumulate/array-ify these + * values across the aggregated message. They are collected here and merged back + * onto the trailing chunk by StreamMetricsTracker.attachTo. + */ +const STREAM_METADATA_SNAPSHOTS = [ + 'reasoning_time', + 'output_tokens', + 'total_tokens', + 'totalMs', + 'tps' +] as const type MetricsCarrier = { [chatlunaMetricsKey]?: ChatLunaInvocationMetrics } @@ -89,6 +102,29 @@ function hasResponseChunk(chunk: ChatGenerationChunk) { ) } +/** + * Pull the numeric snapshot keys out of a metadata object into `out`, returning + * a shallow copy without them. MUTATES nothing on `target` (it copies first), + * but the caller then replaces the message's original field with the returned + * copy. + */ +function takeMetaKeys( + target: Record | null | undefined, + out: Record +): Record | undefined { + if (target == null) return undefined + const keys = STREAM_METADATA_SNAPSHOTS.filter((key) => + Object.hasOwn(target, key) + ) + if (keys.length === 0) return undefined + const copy = { ...target } + for (const key of keys) { + out[key] = copy[key] + delete copy[key] + } + return copy +} + /** * Collects timing + usage across a streaming completion and attaches a single * ChatLunaInvocationMetrics payload to the trailing chunk. Encapsulates the @@ -98,18 +134,41 @@ export class StreamMetricsTracker { private readonly start = Date.now() private firstAt?: number private usage?: UsageMetadata + private readonly kwargs: Record = {} + private readonly metadata: Record = {} + private readonly info: Record = {} observe(chunk: ChatGenerationChunk): void { - const message = chunk.message as AIMessageChunk | undefined - if (message?.usage_metadata != null) { + const message = chunk.message as AIMessageChunk + if (message.usage_metadata != null) { this.usage = message.usage_metadata } + // These assignments MUTATE the incoming chunk: the snapshot keys are + // removed from its metadata fields and stashed here for attachTo. + const kwargs = takeMetaKeys(message.additional_kwargs, this.kwargs) + if (kwargs) message.additional_kwargs = kwargs + const metadata = takeMetaKeys(message.response_metadata, this.metadata) + if (metadata) message.response_metadata = metadata + const info = takeMetaKeys(chunk.generationInfo, this.info) + if (info) chunk.generationInfo = info if (this.firstAt == null && hasResponseChunk(chunk)) { this.firstAt = Date.now() } } attachTo(chunk: ChatGenerationChunk): ChatGenerationChunk { + chunk.message.additional_kwargs = { + ...chunk.message.additional_kwargs, + ...this.kwargs + } + chunk.message.response_metadata = { + ...chunk.message.response_metadata, + ...this.metadata + } + chunk.generationInfo = { + ...chunk.generationInfo, + ...this.info + } attachInvocationMetrics(chunk, { usageMetadata: this.usage, timing: createModelUsageTiming(this.start, this.firstAt, this.usage) diff --git a/packages/core/src/llm-core/platform/model.ts b/packages/core/src/llm-core/platform/model.ts index 6d1b48f47..921778698 100644 --- a/packages/core/src/llm-core/platform/model.ts +++ b/packages/core/src/llm-core/platform/model.ts @@ -8,6 +8,7 @@ import { AIMessage, AIMessageChunk, BaseMessage, + ToolMessage, type UsageMetadata } from '@langchain/core/messages' import { @@ -37,7 +38,10 @@ import { } from 'koishi-plugin-chatluna/llm-core/utils/count_tokens' import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + createTimeoutError, + isAbortError, + isErrorWithCode } from 'koishi-plugin-chatluna/utils/error' import { chunkArray } from 'koishi-plugin-chatluna/llm-core/utils/chunk' import { encodingForModel } from '../utils/tiktoken' @@ -232,20 +236,23 @@ export class ChatLunaChatModel extends BaseChatModel { messages: BaseMessage[], options: this['ParsedCallOptions'], runManager?: CallbackManagerForLLMRun, - reportUsage = true + reportUsage = true, + promptTokens = 0 ): AsyncGenerator { const maxAttempts = Math.max(1, (this._options.maxRetries ?? 0) + 1) - let promptTokens = 0 + const invocation = this.invocationParams(options) if (reportUsage) { ;[messages, promptTokens] = await this.cropMessages( messages, - options['tools'] + options['tools'], + 1, + invocation.maxTokenLimit ) } const streamParams = { - ...this.invocationParams(options), + ...invocation, input: messages } @@ -258,7 +265,7 @@ export class ChatLunaChatModel extends BaseChatModel { let response: ChatGenerationChunk | undefined try { - stream = await this._createStream(streamParams) + stream = this._createStream(streamParams) for await (const chunk of stream) { const hasTool = this._handleStreamChunk( @@ -283,6 +290,17 @@ export class ChatLunaChatModel extends BaseChatModel { ) } + const invalid = (response?.message as AIMessageChunk) + ?.invalid_tool_calls + if (invalid?.length > 0) { + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + `Invalid or incomplete tool call arguments: ${JSON.stringify(invalid)}` + ) + ) + } + this._finalizeStream( hasToolCallChunk, latestTokenUsage, @@ -300,8 +318,8 @@ export class ChatLunaChatModel extends BaseChatModel { } catch (error) { await this._closeStream(stream) - if (options.signal?.aborted) { - throw options.signal.reason ?? error + if (streamParams.signal?.aborted) { + throw streamParams.signal.reason ?? error } if ( @@ -319,11 +337,16 @@ export class ChatLunaChatModel extends BaseChatModel { ) } if (reportUsage) { + const metrics = readInvocationMetrics(response) await this._reportFailedUsage( options, promptTokens, - latestTokenUsage.output_tokens, - readInvocationMetrics(response).timing + response == null + ? 0 + : await this.countMessageTokens( + response.message + ), + metrics.timing ) } throw error @@ -438,15 +461,21 @@ export class ChatLunaChatModel extends BaseChatModel { ) { const metrics = readInvocationMetrics(response) usage = metrics.usageMetadata ?? usage + const outputTokens = response + ? await this.countMessageTokens(response.message) + : 0 - if (usage.total_tokens > 0) { - await this._reportUsage(usage, false, options, metrics.timing) + if (metrics.usageMetadata != null || usage.total_tokens > 0) { + await this._reportUsage({ + usage, + estimated: false, + options, + timing: metrics.timing, + local: { input: promptTokens, output: outputTokens } + }) return } - const outputTokens = response - ? await this.countMessageTokens(response.message) - : 0 let timing = metrics.timing if (timing != null && outputTokens > 0) { timing = { @@ -457,16 +486,17 @@ export class ChatLunaChatModel extends BaseChatModel { : (outputTokens * 1000) / timing.totalMs } } - await this._reportUsage( - { + await this._reportUsage({ + usage: { input_tokens: promptTokens, output_tokens: outputTokens, total_tokens: promptTokens + outputTokens }, - true, + estimated: true, options, - timing - ) + timing, + local: { input: promptTokens, output: outputTokens } + }) } private async _closeStream( @@ -495,19 +525,20 @@ export class ChatLunaChatModel extends BaseChatModel { return ( this._isNonRetryableError(error) || hasChunk || + (error instanceof ChatLunaError && + error.errorCode === ChatLunaErrorCode.API_REQUEST_TIMEOUT && + attempt >= 1) || attempt === maxAttempts - 1 ) } private _isNonRetryableError(error: unknown): boolean { - if (error instanceof ChatLunaError) { - return ( - error.errorCode === ChatLunaErrorCode.ABORTED || - error.errorCode === ChatLunaErrorCode.NOT_AVAILABLE_CONFIG - ) - } - - return (error as Error)?.name === 'AbortError' + return ( + isErrorWithCode(error, [ + ChatLunaErrorCode.ABORTED, + ChatLunaErrorCode.NOT_AVAILABLE_CONFIG + ]) || isAbortError(error) + ) } async _generate( @@ -518,59 +549,48 @@ export class ChatLunaChatModel extends BaseChatModel { let promptTokens: number ;[messages, promptTokens] = await this.cropMessages( messages, - options['tools'] + options['tools'], + 1, + this.invocationParams(options).maxTokenLimit ) - let response: ChatGeneration - try { - response = await this._generateWithRetry( - messages, - options, - runManager - ) - } catch (e) { - await this._reportFailedUsage(options, promptTokens) - throw e - } - - if (response == null) { - await this._reportFailedUsage(options, promptTokens) - throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED) - } + const response = await this._generateWithRetry( + messages, + options, + runManager, + promptTokens + ) const metrics = readInvocationMetrics(response) - let usageMetadata = metrics.usageMetadata + const providerUsage = metrics.usageMetadata + const completionTokens = await this.countMessageTokens(response.message) - const estimated = !usageMetadata?.total_tokens + const reportUsage = providerUsage ?? { + input_tokens: promptTokens, + output_tokens: completionTokens, + total_tokens: completionTokens + promptTokens + } - if (!usageMetadata?.total_tokens) { - const completionTokens = await this.countMessageTokens( - response.message - ) - usageMetadata = { - input_tokens: promptTokens, - output_tokens: completionTokens, - total_tokens: completionTokens + promptTokens - } - } else if (options.stream !== true) { - logger.debug(...formatUsageMetadata(usageMetadata)) + if (providerUsage != null && options.stream !== true) { + logger.debug(...formatUsageMetadata(providerUsage)) } - if (response.message.getType() === 'ai') { + if (response.message.getType() === 'ai' && providerUsage != null) { ;(response.message as AIMessage | AIMessageChunk).usage_metadata = - usageMetadata + providerUsage } - await this._reportUsage( - usageMetadata, - estimated, + await this._reportUsage({ + usage: reportUsage, + estimated: providerUsage == null, options, - metrics.timing - ) + timing: metrics.timing, + local: { input: promptTokens, output: completionTokens } + }) const llmOutput = { ...response.generationInfo, - usage_metadata: usageMetadata + usage_metadata: reportUsage } return { @@ -579,18 +599,27 @@ export class ChatLunaChatModel extends BaseChatModel { } } - private async _reportUsage( - usage: UsageMetadata, - estimated: boolean, - options: this['ParsedCallOptions'], + private async _reportUsage({ + usage, + estimated, + options, + timing, + local + }: { + usage: UsageMetadata + estimated: boolean + options: ChatLunaModelCallOptions timing?: ModelUsageTiming - ) { + local: { input?: number; output?: number } + }) { if (this._report == null) return try { await this._report({ callType: 'llm', usageMetadata: usage, + localInputTokens: local.input, + localOutputTokens: local.output, estimated, success: true, timing, @@ -617,7 +646,9 @@ export class ChatLunaChatModel extends BaseChatModel { output_tokens: outputTokens, total_tokens: promptTokens + outputTokens }, - estimated: promptTokens > 0 || outputTokens > 0, + localInputTokens: promptTokens, + localOutputTokens: outputTokens, + estimated: true, success: false, timing, context: usageContextFromOptions(options) @@ -630,21 +661,22 @@ export class ChatLunaChatModel extends BaseChatModel { private _generateWithRetry( messages: BaseMessage[], options: this['ParsedCallOptions'], - runManager?: CallbackManagerForLLMRun + runManager?: CallbackManagerForLLMRun, + promptTokens = 0 ): Promise { const maxAttempts = Math.max(1, (this._options.maxRetries ?? 0) + 1) const generateWithRetry = async () => { for (let attempt = 0; attempt < maxAttempts; attempt++) { + let response: ChatGeneration | undefined try { - let response: ChatGeneration - if (options.stream) { const stream = this._streamResponseChunks( messages, options, runManager, - false + false, + promptTokens ) let responseChunk: ChatGenerationChunk for await (const chunk of stream) { @@ -663,6 +695,7 @@ export class ChatLunaChatModel extends BaseChatModel { } if ( + response == null || !this._hasResponse( response.message as AIMessage | AIMessageChunk ) @@ -672,6 +705,17 @@ export class ChatLunaChatModel extends BaseChatModel { ) } + const invalid = (response.message as AIMessageChunk) + .invalid_tool_calls + if (invalid?.length > 0) { + throw new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_FAILED, + new Error( + `Invalid or incomplete tool call arguments: ${JSON.stringify(invalid)}` + ) + ) + } + return response } catch (error) { if (options.signal?.aborted) { @@ -683,6 +727,17 @@ export class ChatLunaChatModel extends BaseChatModel { this._isNonRetryableError(error) || attempt === maxAttempts - 1 ) { + const metrics = readInvocationMetrics(response) + await this._reportFailedUsage( + options, + promptTokens, + response == null + ? 0 + : await this.countMessageTokens( + response.message + ), + metrics.timing + ) throw error } @@ -696,62 +751,23 @@ export class ChatLunaChatModel extends BaseChatModel { return generateWithRetry() } - private async _withTimeout( - func: () => Promise, - timeout: number - ): Promise { - let timeoutError: Error | null = null - - try { - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_TIMEOUT, - null, - true - ) - } catch (e) { - timeoutError = e - } - - let timeoutId: NodeJS.Timeout - - // eslint-disable-next-line promise/param-names - const timeoutPromise = new Promise((_, reject) => { - timeoutId = setTimeout(() => { - reject(timeoutError) - }, timeout) - }) - - try { - return await Promise.race([func(), timeoutPromise]) - } finally { - clearTimeout(timeoutId) - } - } - private _createStream(params: ModelRequestParams) { - return this._withTimeout( - async () => this._requester.completionStream(params), - params.timeout - ) + return this._requester.completionStream(params) } /** @ignore */ private async _completion(params: ModelRequestParams) { - return this._withTimeout( - () => this._requester.completion(params), - params.timeout - ) + return this._requester.completion(params) } async cropMessages( messages: BaseMessage[], tools?: StructuredTool[], - systemMessageLength: number = 1 + systemMessageLength: number = 1, + maxTokenLimit = this.invocationParams().maxTokenLimit ): Promise<[BaseMessage[], number]> { messages = messages.concat([]) - const maxTokenLimit = this.invocationParams().maxTokenLimit - let totalTokens = 0 // If there are functions, add the function definitions as they count towards token usage @@ -833,26 +849,27 @@ export class ChatLunaChatModel extends BaseChatModel { // Find baseline: last AI message with usage_metadata let baselineRoundIdx = -1 + let baselineMessageIdx = -1 let baselineTokens = 0 if (hasLimit) { - let msgOffset = 0 for (let r = 0; r < conversationRounds.length; r++) { for (let j = 0; j < conversationRounds[r].length; j++) { - const msg = messages[msgOffset + j] + const msg = conversationRounds[r][j] if (msg.getType() === 'ai') { const usage = (msg as AIMessage).usage_metadata if (usage?.input_tokens > 0) { baselineRoundIdx = r + baselineMessageIdx = j baselineTokens = usage.input_tokens - totalTokens } } } - msgOffset += conversationRounds[r].length } - // Add tokens for messages after the baseline AI msg within its round + // Add tokens for the baseline AI msg and its remaining round tail if (baselineRoundIdx >= 0) { - // Count the tail of the baseline round (AI msg itself + tool msgs) - for (const msg of conversationRounds[baselineRoundIdx]) { + for (const msg of conversationRounds[baselineRoundIdx].slice( + baselineMessageIdx + )) { if (msg.getType() === 'ai' || msg.getType() === 'tool') { baselineTokens += await this.countMessageTokens(msg) } @@ -976,6 +993,20 @@ export class ChatLunaChatModel extends BaseChatModel { ) ) } + if (openAIMessage.getType() === 'ai') { + const toolCalls = (openAIMessage as AIMessage).tool_calls + const rawToolCalls = openAIMessage.additional_kwargs?.tool_calls + const payload = toolCalls?.length > 0 ? toolCalls : rawToolCalls + if (Array.isArray(payload) && payload.length > 0) { + count += await this.getNumTokens(JSON.stringify(payload)) + } + } + if (openAIMessage.getType() === 'tool') { + const toolCallId = (openAIMessage as ToolMessage).tool_call_id + if (toolCallId) { + count += await this.getNumTokens(toolCallId) + } + } totalCount += count @@ -1240,17 +1271,9 @@ export class ChatLunaEmbeddings extends ChatLunaBaseEmbeddings { private async _embeddingWithRetry(request: EmbeddingsRequestParams) { request.timeout = request.timeout ?? this.timeout - let timeoutError: Error | null = null - - try { - throw new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_TIMEOUT, - new Error(`timeout when calling ${this.modelName} embeddings`), - true - ) - } catch (e) { - timeoutError = e - } + const timeoutError = createTimeoutError( + new Error(`timeout when calling ${this.modelName} embeddings`) + ) const makeRequest = async () => { let timeoutId: NodeJS.Timeout @@ -1299,10 +1322,6 @@ type UsageSession = { type UsageConfig = { session?: UsageSession - conversationId?: string - requestId?: string - userId?: string - guildId?: string agentContext?: ModelUsageContext & { channelId?: string } } @@ -1325,22 +1344,13 @@ function usageContextFromOptions(options: ChatLunaModelCallOptions) { const context: ModelUsageContext = { chatPlatform: built?.chatPlatform ?? session?.platform, conversationId: + cfg?.agentContext?.conversationId ?? (typeof options.id === 'string' ? options.id : undefined) ?? - built?.conversationId ?? - cfg?.conversationId ?? - cfg?.agentContext?.conversationId, - requestId: - built?.requestId ?? cfg?.requestId ?? cfg?.agentContext?.requestId, - userId: - built?.userId ?? - cfg?.userId ?? - cfg?.agentContext?.userId ?? - session?.userId, + built?.conversationId, + requestId: cfg?.agentContext?.requestId ?? built?.requestId, + userId: cfg?.agentContext?.userId ?? built?.userId ?? session?.userId, guildId: - built?.guildId ?? - cfg?.guildId ?? - cfg?.agentContext?.guildId ?? - session?.guildId + cfg?.agentContext?.guildId ?? built?.guildId ?? session?.guildId } return context.chatPlatform != null || diff --git a/packages/core/src/llm-core/platform/rerank.ts b/packages/core/src/llm-core/platform/rerank.ts index 5c96ae420..2f0b72839 100644 --- a/packages/core/src/llm-core/platform/rerank.ts +++ b/packages/core/src/llm-core/platform/rerank.ts @@ -11,7 +11,8 @@ import { } from 'koishi-plugin-chatluna/llm-core/platform/api' import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + createTimeoutError } from 'koishi-plugin-chatluna/utils/error' import { logger } from 'koishi-plugin-chatluna' import type { ModelUsageReporter } from 'koishi-plugin-chatluna/llm-core/platform/usage' @@ -125,10 +126,8 @@ export class ChatLunaReranker extends BaseDocumentCompressor { private async _rerankWithRetry( request: RerankerRequestParams ): ReturnType { - const timeoutError = new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_TIMEOUT, - new Error(`timeout when calling ${request.model} reranker`), - true + const timeoutError = createTimeoutError( + new Error(`timeout when calling ${request.model} reranker`) ) const makeRequest = async () => { diff --git a/packages/core/src/llm-core/platform/types.ts b/packages/core/src/llm-core/platform/types.ts index 615e498ae..ad6fde458 100644 --- a/packages/core/src/llm-core/platform/types.ts +++ b/packages/core/src/llm-core/platform/types.ts @@ -8,7 +8,7 @@ import { PresetTemplate } from 'koishi-plugin-chatluna/llm-core/prompt' import { ChatLunaSaveableVectorStore } from 'koishi-plugin-chatluna/llm-core/vectorstores' import { BasePlatformClient } from 'koishi-plugin-chatluna/llm-core/platform/client' import { ComputedRef } from '@vue/reactivity' -import { AgentRunContext, ToolMask } from '../agent' +import { AgentRunContext } from '../agent' export interface ChatLunaChainInfo { name: string @@ -152,12 +152,8 @@ export type ChatLunaToolRunnable = ToolRunnableConfig & { configurable: { model: ChatLunaChatModel session: Session - conversationId?: string preset?: string - userId?: string - toolMask?: ToolMask - source?: 'chatluna' | 'character' - agentContext?: AgentRunContext + agentContext: AgentRunContext } } diff --git a/packages/core/src/llm-core/platform/usage.ts b/packages/core/src/llm-core/platform/usage.ts index 8257b183f..a166ca117 100644 --- a/packages/core/src/llm-core/platform/usage.ts +++ b/packages/core/src/llm-core/platform/usage.ts @@ -105,6 +105,8 @@ export interface ModelUsagePayload { platform: string model: string usageMetadata: UsageMetadata + localInputTokens?: number + localOutputTokens?: number estimated: boolean success: boolean createdAt: Date diff --git a/packages/core/src/llm-core/prompt/context_manager.ts b/packages/core/src/llm-core/prompt/context_manager.ts index b107e02ba..81d11fbf3 100644 --- a/packages/core/src/llm-core/prompt/context_manager.ts +++ b/packages/core/src/llm-core/prompt/context_manager.ts @@ -461,7 +461,7 @@ export class ChatLunaContextManagerService { afterUserMessage, currentMessages }: CollectPromptContextOptions): PromptContextInjectionCollection { - const conversationId = configurable?.conversationId + const conversationId = configurable?.agentContext?.conversationId // Step 1: stamp ids on any message that lacks one. for (const msg of currentMessages) { diff --git a/packages/core/src/middlewares/conversation/request_conversation.ts b/packages/core/src/middlewares/conversation/request_conversation.ts index 38ad5eca5..79152d23b 100644 --- a/packages/core/src/middlewares/conversation/request_conversation.ts +++ b/packages/core/src/middlewares/conversation/request_conversation.ts @@ -136,6 +136,10 @@ export function apply(ctx: Context, config: Config, chain: ChatChain) { postHandler, requestId: context.options.messageId, toolMask: invocation?.toolMask, + source: + invocation?.source.kind === 'character' + ? 'character' + : 'chatluna', signal: invocation?.signal, persist: invocation?.persist !== false } diff --git a/packages/core/src/migration/room_to_conversation.ts b/packages/core/src/migration/room_to_conversation.ts index 9f60bbe56..eef70c2cc 100644 --- a/packages/core/src/migration/room_to_conversation.ts +++ b/packages/core/src/migration/room_to_conversation.ts @@ -119,9 +119,7 @@ export async function runRoomToConversationMigration( // flags indicate it IS complete — so the two paths are mutually exclusive. export async function ensureMigrationValidated(ctx: Context, config: Config) { const hasSentinel = existsSync(getLegacySchemaSentinel(ctx.baseDir)) - const hasLegacyData = hasSentinel - ? await hasLegacyMigrationData(ctx) - : false + const hasLegacyData = await hasLegacyMigrationData(ctx) if (hasSentinel && hasLegacyData) { ctx.logger.warn( @@ -221,6 +219,16 @@ export async function ensureMigrationValidated(ctx: Context, config: Config) { roomDone !== true || messageDone !== true ) { + if (!hasLegacyData) { + ctx.logger.info('No legacy ChatHub data found; skipping migration.') + const validated = createPassedValidationResult() + await writeMetaValue(ctx, 'schema_version', BUILTIN_SCHEMA_VERSION) + await writeMetaValue(ctx, 'validation_result', validated) + await writeMigrationDone(ctx) + await writeMigrationFinished(ctx) + return validated + } + // Migration is genuinely incomplete; restart it. // runRoomToConversationMigration will NOT call back into ensureMigrationValidated // because it only does so when all done-flags are true, which they are not here. diff --git a/packages/core/src/services/conversation_runtime.ts b/packages/core/src/services/conversation_runtime.ts index ffba95a17..3cfb0ed6b 100644 --- a/packages/core/src/services/conversation_runtime.ts +++ b/packages/core/src/services/conversation_runtime.ts @@ -2,13 +2,19 @@ import type { Callbacks } from '@langchain/core/callbacks/manager' import { AIMessage, HumanMessage } from '@langchain/core/messages' import type { Session } from 'koishi' import { LRUCache } from 'lru-cache' -import { MessageQueue, type ToolMask } from '../llm-core/agent/types' +import { + AgentRunContext, + MessageQueue, + type ToolMask +} from '../llm-core/agent/types' import { RequestIdQueue } from 'koishi-plugin-chatluna/utils/queue' import { randomUUID } from 'crypto' import type { ChatLunaService } from './chat' import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + createAbortError, + createTimeoutError } from 'koishi-plugin-chatluna/utils/error' import type { ClientConfig } from '../llm-core/platform/config' import { markChatLunaUserMessage } from 'koishi-plugin-chatluna/utils/langchain' @@ -25,6 +31,7 @@ export interface ChatOptions { postHandler?: PostHandler requestId?: string toolMask?: ToolMask + source?: AgentRunContext['source'] callbacks?: Callbacks signal?: AbortSignal persist?: boolean @@ -119,13 +126,7 @@ export class ConversationRuntime { () => { if (abortController.signal.aborted) return if (Date.now() - lastActiveAt < config.timeout) return - abortController.abort( - new ChatLunaError( - ChatLunaErrorCode.API_REQUEST_TIMEOUT, - undefined, - true - ) - ) + abortController.abort(createTimeoutError()) }, Math.min(config.timeout, 30000) ) @@ -153,6 +154,7 @@ export class ConversationRuntime { stream, conversationId: conversation.id, requestId, + source: options.source ?? 'chatluna', session, variables, signal: abortController.signal, @@ -319,27 +321,13 @@ export class ConversationRuntime { if (signal != null) { if (signal.aborted) { - throw ( - signal.reason ?? - new ChatLunaError( - ChatLunaErrorCode.ABORTED, - undefined, - true - ) - ) + throw signal.reason ?? createAbortError() } await Promise.race([ waiting, new Promise((_resolve, reject) => { onAbort = () => - reject( - signal.reason ?? - new ChatLunaError( - ChatLunaErrorCode.ABORTED, - undefined, - true - ) - ) + reject(signal.reason ?? createAbortError()) signal.addEventListener('abort', onAbort, { once: true }) diff --git a/packages/core/src/services/prompt_renderer.ts b/packages/core/src/services/prompt_renderer.ts index 6218c0d99..a38b4aafc 100644 --- a/packages/core/src/services/prompt_renderer.ts +++ b/packages/core/src/services/prompt_renderer.ts @@ -99,7 +99,7 @@ export class ChatLunaPromptRenderService { return selectFromList( args.join(','), true, - cfg?.conversationId ?? '' + cfg?.agentContext?.conversationId ?? '' ) }) diff --git a/packages/core/src/services/types.ts b/packages/core/src/services/types.ts index 5e2acf7fb..ab6fcf041 100644 --- a/packages/core/src/services/types.ts +++ b/packages/core/src/services/types.ts @@ -21,7 +21,7 @@ import { } from '@langchain/core/messages' import { AgentAction, - SubagentContext, + AgentRunContext, ToolMask } from 'koishi-plugin-chatluna/llm-core/agent' import type { ChatInterface } from '../llm-core/chat/app' @@ -217,10 +217,7 @@ declare module 'koishi' { declare module '@chatluna/shared-prompt-renderer' { export interface RenderConfigurable { session?: Session - conversationId?: string - subagentContext?: SubagentContext - toolMask?: ToolMask - source?: string + agentContext?: AgentRunContext } } diff --git a/packages/core/src/utils/error.ts b/packages/core/src/utils/error.ts index a136e7a33..31c93f0c1 100644 --- a/packages/core/src/utils/error.ts +++ b/packages/core/src/utils/error.ts @@ -40,6 +40,41 @@ export class ChatLunaError extends Error { } } +export function isAbortError(e: unknown): boolean { + return e instanceof Error && e.name === 'AbortError' +} + +export function isErrorWithCode( + e: unknown, + codes: ChatLunaErrorCode[] +): boolean { + return e instanceof ChatLunaError && codes.includes(e.errorCode) +} + +export function isRequestFailure(e: unknown): boolean { + return ( + isErrorWithCode(e, [ + ChatLunaErrorCode.NETWORK_ERROR, + ChatLunaErrorCode.API_REQUEST_TIMEOUT, + ChatLunaErrorCode.API_REQUEST_FAILED, + ChatLunaErrorCode.API_REQUEST_TOKEN_LIMIT, + ChatLunaErrorCode.ABORTED + ]) || isAbortError(e) + ) +} + +export function createTimeoutError(originError?: Error): ChatLunaError { + return new ChatLunaError( + ChatLunaErrorCode.API_REQUEST_TIMEOUT, + originError, + true + ) +} + +export function createAbortError(): ChatLunaError { + return new ChatLunaError(ChatLunaErrorCode.ABORTED, undefined, true) +} + export enum ChatLunaErrorCode { NETWORK_ERROR = 1, UNSUPPORTED_PROXY_PROTOCOL = 2, @@ -56,6 +91,7 @@ export enum ChatLunaErrorCode { API_REQUEST_TIMEOUT = 102, API_REQUEST_FAILED = 103, API_UNSAFE_CONTENT = 104, + API_REQUEST_TOKEN_LIMIT = 105, MODEL_ADAPTER_NOT_FOUND = 300, MODEL_NOT_FOUND = 301, PRESET_NOT_FOUND = 302, diff --git a/packages/core/src/utils/queue.ts b/packages/core/src/utils/queue.ts index 9aace3072..21abffc54 100644 --- a/packages/core/src/utils/queue.ts +++ b/packages/core/src/utils/queue.ts @@ -1,6 +1,7 @@ import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + createAbortError } from 'koishi-plugin-chatluna/utils/error' import { ObjectLock } from 'koishi-plugin-chatluna/utils/lock' import { withResolver } from 'koishi-plugin-chatluna/utils/promise' @@ -146,13 +147,7 @@ export class RequestIdQueue { // Settle removed waiters so abort/cancel paths do not hang. if (removed != null && !removed.active) { - removed.notifyPromise.reject( - new ChatLunaError( - ChatLunaErrorCode.ABORTED, - undefined, - true - ) - ) + removed.notifyPromise.reject(createAbortError()) } items.forEach((item) => item.notifyPromise.resolve()) diff --git a/packages/core/src/utils/sse.ts b/packages/core/src/utils/sse.ts index 20d3474b5..fa04e9247 100644 --- a/packages/core/src/utils/sse.ts +++ b/packages/core/src/utils/sse.ts @@ -1,6 +1,8 @@ import { ChatLunaError, - ChatLunaErrorCode + ChatLunaErrorCode, + createAbortError, + createTimeoutError } from 'koishi-plugin-chatluna/utils/error' import * as fetchType from 'undici/types/fetch' @@ -155,42 +157,36 @@ export async function checkResponse( } // eslint-disable-next-line generator-star-spacing -async function* readSSE(reader: ReadableStreamDefaultReader) { +async function* readSSE( + reader: ReadableStreamDefaultReader, + signal?: AbortSignal +) { const decoder = new TextDecoder('utf-8') - + const abort = () => { + // eslint-disable-next-line no-void + void reader.cancel(signal?.reason ?? createAbortError()).catch(() => {}) + } + signal?.addEventListener('abort', abort, { once: true }) + if (signal?.aborted) abort() try { while (true) { const { value, done } = await reader.read() - - if (done) { - return - } - - const decodeValue = decoder.decode(value, { stream: true }) - - yield decodeValue + if (signal?.aborted) throw signal.reason ?? createAbortError() + if (done) return + yield decoder.decode(value, { stream: true }) } } finally { + signal?.removeEventListener('abort', abort) + await reader.cancel().catch(() => {}) reader.releaseLock() } } -export async function sse( - response: fetchType.Response | ReadableStreamDefaultReader, - onEvent: ( - rawData: string - ) => Promise = async () => {}, - cacheCount: number = 0 -) { - for await (const rawChunk of rawSeeAsIterable(response, cacheCount)) { - await onEvent(rawChunk) - } -} - // eslint-disable-next-line generator-star-spacing export async function* rawSeeAsIterable( response: fetchType.Response | ReadableStreamDefaultReader, - cacheCount: number = 0 + cacheCount: number = 0, + signal?: AbortSignal ) { await checkResponse(response) @@ -203,7 +199,7 @@ export async function* rawSeeAsIterable( let tempCount = 0 - for await (const rawChunk of readSSE(reader)) { + for await (const rawChunk of readSSE(reader, signal)) { bufferString += rawChunk tempCount++ @@ -222,14 +218,45 @@ export async function* rawSeeAsIterable( // eslint-disable-next-line generator-star-spacing export async function* sseIterable( - response: fetchType.Response | ReadableStreamDefaultReader + response: fetchType.Response | ReadableStreamDefaultReader, + params: { timeout?: number; signal?: AbortSignal } = {} ) { + const idleTimeout = params.timeout const parser = createParser() + const controller = new AbortController() + let timer: NodeJS.Timeout | undefined + const abort = () => controller.abort(params.signal?.reason) + + const reset = () => { + if (idleTimeout == null || controller.signal.aborted) return + if (timer) clearTimeout(timer) + timer = setTimeout( + () => controller.abort(createTimeoutError()), + idleTimeout + ) + } - for await (const rawChunk of rawSeeAsIterable(response)) { - for (const event of parser(rawChunk)) { - yield event + if (params.signal) { + params.signal.addEventListener('abort', abort, { once: true }) + if (params.signal.aborted) abort() + } + + reset() + + try { + for await (const rawChunk of rawSeeAsIterable( + response, + 0, + controller.signal + )) { + reset() + for (const event of parser(rawChunk)) { + yield event + } } + } finally { + if (timer) clearTimeout(timer) + params.signal?.removeEventListener('abort', abort) } return '[DONE]' diff --git a/packages/core/tests/conversation-archive.spec.ts b/packages/core/tests/conversation-archive.spec.ts deleted file mode 100644 index 7aad6a4ac..000000000 --- a/packages/core/tests/conversation-archive.spec.ts +++ /dev/null @@ -1,293 +0,0 @@ -/// - -import fs from 'node:fs/promises' -import os from 'node:os' -import path from 'node:path' -import { assert } from 'chai' -import { purgeArchivedConversation } from '../src/utils/archive' -import { gzipEncode } from '../src/utils/compression' -import type { - ArchiveRecord, - BindingRecord, - ConversationRecord -} from '../src/types' -import { - createConversation, - createMessage, - createService, - createSession, - expectRejected, - type TableRow -} from './helpers' - -it('ConversationService exports, archives, and restores conversations with legacy migration fields', async () => { - const tempDir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-archive-test-') - ) - const exportPath = path.join(tempDir, 'conversation-export.md') - - const conversation = createConversation({ - id: 'conversation-archive', - title: 'Archived Conversation', - latestMessageId: 'message-2', - legacyRoomId: 42, - legacyMeta: JSON.stringify({ roomName: 'legacy-room' }) - }) - const messageA = createMessage({ - id: 'message-1', - conversationId: 'conversation-archive', - text: 'hello' - }) - const messageB = createMessage({ - id: 'message-2', - conversationId: 'conversation-archive', - parentId: 'message-1', - role: 'ai', - text: 'world' - }) - - const { service, database, clearCacheCalls } = await createService({ - baseDir: tempDir, - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: 'shared:discord:bot:guild', - activeConversationId: 'conversation-archive', - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_message: [ - messageA as unknown as TableRow, - messageB as unknown as TableRow - ] - } - }) - - const exported = await service.exportConversation(createSession(), { - conversationId: 'conversation-archive', - outputPath: exportPath - }) - const exportMarkdown = await fs.readFile(exported.path, 'utf8') - - assert.match(exportMarkdown, /# Archived Conversation/) - assert.match(exportMarkdown, /hello/) - assert.match(exportMarkdown, /world/) - - const archived = await service.archiveConversation(createSession(), { - conversationId: 'conversation-archive' - }) - const archivedConversation = await service.getConversation( - 'conversation-archive' - ) - const archiveRecord = archived.archive as ArchiveRecord - const manifest = JSON.parse( - await fs.readFile(path.join(archived.path, 'manifest.json'), 'utf8') - ) - - assert.equal(archivedConversation.status, 'archived') - assert.equal(archivedConversation.archiveId, archiveRecord.id) - assert.equal(manifest.conversationId, 'conversation-archive') - assert.equal(database.tables.chatluna_message.length, 0) - assert.deepEqual(clearCacheCalls, ['conversation-archive']) - - const restored = await service.restoreConversation(createSession(), { - conversationId: 'conversation-archive' - }) - const restoredMessages = await service.listMessages('conversation-archive') - const restoredArchive = await service.getArchive(archiveRecord.id) - - assert.equal(restored.status, 'active') - assert.equal(restored.archiveId, null) - assert.equal(restored.legacyRoomId, 42) - assert.equal( - restored.legacyMeta, - JSON.stringify({ roomName: 'legacy-room' }) - ) - assert.equal(restoredMessages.length, 2) - assert.equal(restoredArchive.state, 'ready') - assert.notEqual(restoredArchive.restoredAt, null) -}) - -it('ConversationService rejects restoring archives from another conversation', async () => { - const conversation = createConversation({ - id: 'conversation-restore-owner' - }) - const foreignArchive: ArchiveRecord = { - id: 'archive-foreign', - conversationId: 'conversation-foreign', - path: path.join(os.tmpdir(), 'archive-foreign'), - formatVersion: 1, - messageCount: 0, - checksum: null, - size: 0, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } - - const { service } = await createService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_archive: [foreignArchive as unknown as TableRow] - } - }) - - await expectRejected( - service.restoreConversation(createSession(), { - conversationId: conversation.id, - archiveId: foreignArchive.id - }), - /419/ - ) -}) - -it('ConversationService rejects restoring a foreign conversation by exact id', async () => { - const dir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-foreign-restore-') - ) - const conversation = createConversation({ - id: 'conversation-foreign-restore', - bindingKey: 'shared:discord:bot:other-guild', - status: 'archived', - archiveId: 'archive-foreign-restore', - archivedAt: new Date('2026-03-22T00:00:00.000Z') - }) - const archivePath = path.join(dir, 'archive-foreign-restore.json.gz') - - await fs.writeFile( - archivePath, - await gzipEncode( - JSON.stringify({ - formatVersion: 1, - exportedAt: '2026-03-22T00:00:00.000Z', - conversation: { - ...conversation, - status: 'active', - archiveId: null, - archivedAt: null, - createdAt: conversation.createdAt.toISOString(), - updatedAt: conversation.updatedAt.toISOString(), - lastChatAt: conversation.lastChatAt?.toISOString() ?? null - }, - messages: [] - }) - ) - ) - - const { service } = await createService({ - baseDir: dir, - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_constraint: [ - { - id: 1, - name: 'allow-manage-current-route', - enabled: true, - priority: 10, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - guildId: 'guild', - manageMode: 'anyone' - } as unknown as TableRow - ], - chatluna_archive: [ - { - id: conversation.archiveId, - conversationId: conversation.id, - path: archivePath, - formatVersion: 1, - messageCount: 0, - checksum: null, - size: 1, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } as unknown as TableRow - ] - } - }) - - await expectRejected( - service.restoreConversation(createSession({ authority: 1 }), { - conversationId: conversation.id - }), - /Conversation does not belong to current route\./ - ) -}) - -it('purgeArchivedConversation removes archive directory and clears both binding pointers', async () => { - const dir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-purge-archive-') - ) - const archiveDir = path.join(dir, 'archive-dir') - await fs.mkdir(archiveDir, { recursive: true }) - await fs.writeFile(path.join(archiveDir, 'manifest.json'), '{}', 'utf8') - - const conversation = createConversation({ - id: 'conversation-purge', - status: 'archived', - archiveId: 'archive-purge' - }) - const { ctx, database } = await createService({ - baseDir: dir, - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: conversation.id, - updatedAt: new Date() - } - ], - chatluna_archive: [ - { - id: 'archive-purge', - conversationId: conversation.id, - path: archiveDir, - formatVersion: 1, - messageCount: 1, - checksum: null, - size: 1, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } - ], - chatluna_message: [ - createMessage({ - conversationId: conversation.id - }) as unknown as TableRow - ], - chatluna_acl: [ - { - conversationId: conversation.id, - principalType: 'user', - principalId: 'user', - permission: 'view' - } as unknown as TableRow - ] - } - }) - - await purgeArchivedConversation(ctx, conversation) - - await expectRejected(fs.access(archiveDir)) - assert.equal(database.tables.chatluna_conversation.length, 0) - assert.equal(database.tables.chatluna_archive.length, 0) - assert.equal(database.tables.chatluna_message.length, 0) - assert.equal(database.tables.chatluna_acl.length, 0) - assert.equal(database.tables.chatluna_binding[0].activeConversationId, null) - assert.equal(database.tables.chatluna_binding[0].lastConversationId, null) -}) diff --git a/packages/core/tests/conversation-e2e.spec.ts b/packages/core/tests/conversation-e2e.spec.ts deleted file mode 100644 index dd90112d9..000000000 --- a/packages/core/tests/conversation-e2e.spec.ts +++ /dev/null @@ -1,59 +0,0 @@ -/// - -import fs from 'node:fs/promises' -import os from 'node:os' -import path from 'node:path' -import { assert } from 'chai' -import { createMemoryService, createSession } from './helpers' - -it('ConversationService supports sampled end-to-end lifecycle flow', async () => { - const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chatluna-e2e-flow-')) - const { app, database, service } = await createMemoryService({ - baseDir: dir - }) - - try { - const session = createSession() - const created = await service.ensureActiveConversation(session, { - presetLane: 'helper' - }) - const listed = await service.listConversations(session, { - presetLane: 'helper' - }) - const renamed = await service.renameConversation(session, { - conversationId: created.conversation.id, - presetLane: 'helper', - title: 'Helper Session' - }) - const exported = await service.exportConversation(session, { - conversationId: created.conversation.id, - presetLane: 'helper' - }) - const archived = await service.archiveConversation(session, { - conversationId: created.conversation.id, - presetLane: 'helper' - }) - const restored = await service.restoreConversation(session, { - conversationId: created.conversation.id, - presetLane: 'helper' - }) - const removed = await service.deleteConversation(session, { - conversationId: created.conversation.id, - presetLane: 'helper' - }) - const stored = await database.get('chatluna_conversation', { - id: created.conversation.id - }) - - assert.equal(listed.length, 1) - assert.equal(renamed.title, 'Helper Session') - assert.equal(renamed.autoTitle, false) - assert.equal(path.extname(exported.path), '.md') - assert.equal(archived.conversation.status, 'archived') - assert.equal(restored.status, 'active') - assert.equal(removed.status, 'deleted') - assert.equal(stored[0].status, 'deleted') - } finally { - await app.stop() - } -}) diff --git a/packages/core/tests/conversation-migration.spec.ts b/packages/core/tests/conversation-migration.spec.ts deleted file mode 100644 index 8673d1391..000000000 --- a/packages/core/tests/conversation-migration.spec.ts +++ /dev/null @@ -1,396 +0,0 @@ -/// - -import fs from 'node:fs/promises' -import path from 'node:path' -import { assert } from 'chai' -import { - createLegacyBindingKey, - dropTableIfExists, - inferLegacyGroupRouteModes -} from '../src/migration/validators' -import { - getLegacySchemaSentinel, - getLegacySchemaSentinelDir -} from '../src/migration/legacy_tables' -import { - ensureMigrationValidated, - runRoomToConversationMigration -} from '../src/migration/room_to_conversation' -import { - createConfig, - createConversation, - createService, - type TableRow -} from './helpers' -import type { - BindingRecord, - ConversationRecord, - MessageRecord -} from '../src/types' - -it('runRoomToConversationMigration migrates legacy rooms, messages, bindings, and ACL', async () => { - const { ctx, database } = await createService({ - tables: { - chathub_room: [ - { - roomId: 1, - roomName: 'Legacy Room', - roomMasterId: 'owner', - conversationId: 'legacy-conversation', - preset: 'legacy-preset', - model: 'legacy/model', - chatMode: 'plugin', - visibility: 'private', - password: 'secret', - autoUpdate: true, - updatedTime: new Date('2026-03-21T00:00:00.000Z') - } as unknown as TableRow - ], - chathub_conversation: [ - { - id: 'legacy-conversation', - latestId: 'legacy-message-1', - additional_kwargs: '{"topic":"legacy"}', - updatedAt: new Date('2026-03-21T01:00:00.000Z') - } as unknown as TableRow - ], - chathub_message: [ - { - id: 'legacy-message-1', - conversation: 'legacy-conversation', - parent: null, - role: 'human', - text: 'hello from legacy room', - content: null, - name: 'owner', - tool_call_id: null, - tool_calls: null, - additional_kwargs: null, - additional_kwargs_binary: null, - rawId: null - } as unknown as TableRow - ], - chathub_room_member: [ - { - roomId: 1, - userId: 'owner', - roomPermission: 'owner', - mute: false - } as unknown as TableRow, - { - roomId: 1, - userId: 'guest', - roomPermission: 'member', - mute: false - } as unknown as TableRow, - { - roomId: 1, - userId: 'helper', - roomPermission: 'member', - mute: false - } as unknown as TableRow - ], - chathub_room_group_member: [ - { - roomId: 1, - groupId: 'guild', - roomVisibility: 'private' - } as unknown as TableRow, - { - roomId: 1, - groupId: 'guild-2', - roomVisibility: 'private' - } as unknown as TableRow - ], - chathub_user: [ - { - userId: 'owner', - groupId: 'guild', - defaultRoomId: 1 - } as unknown as TableRow - ] - } - }) - - await runRoomToConversationMigration(ctx, createConfig()) - - const conversation = database.tables - .chatluna_conversation[0] as ConversationRecord - const binding = database.tables.chatluna_binding[0] as BindingRecord - const message = database.tables.chatluna_message[0] as MessageRecord - const meta = database.tables.chatluna_meta.find( - (item) => item.key === 'validation_result' - ) as { value?: string | null } - - assert.equal(conversation.id, 'legacy-conversation') - assert.equal(conversation.bindingKey, 'custom:legacy:room:1') - assert.equal(conversation.latestMessageId, 'legacy-message-1') - assert.equal(conversation.legacyRoomId, 1) - assert.equal(binding.activeConversationId, 'legacy-conversation') - assert.equal(message.conversationId, 'legacy-conversation') - assert.equal(database.tables.chatluna_acl.length, 6) - assert.equal(JSON.parse(meta.value ?? '{}').passed, true) -}) - -it('ensureMigrationValidated treats sentinel plus incomplete migration metadata as fresh install', async () => { - const { ctx, database } = await createService({ - tables: { - chatluna_meta: [ - { - key: 'migration_started_at', - value: JSON.stringify('2026-04-06T05:59:16.380Z'), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow, - { - key: 'schema_version', - value: JSON.stringify(1), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow, - { - key: 'legacy_binding_route_mode', - value: JSON.stringify('shared'), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow, - { - key: 'legacy_table_retention', - value: JSON.stringify({ - state: 'migration-visible', - migrationTables: [ - 'chathub_room_member', - 'chathub_room_group_member', - 'chathub_user', - 'chathub_room', - 'chathub_message', - 'chathub_conversation' - ], - runtimeTables: [] - }), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow - ] - } - }) - - await fs.mkdir(getLegacySchemaSentinelDir(ctx.baseDir), { recursive: true }) - await fs.writeFile(getLegacySchemaSentinel(ctx.baseDir), '{}', 'utf8') - - await ensureMigrationValidated(ctx, createConfig()) - - const validation = database.tables.chatluna_meta.find( - (item) => item.key === 'validation_result' - ) as { value?: string | null } - const retention = database.tables.chatluna_meta.find( - (item) => item.key === 'legacy_table_retention' - ) as { value?: string | null } - const roomDone = database.tables.chatluna_meta.find( - (item) => item.key === 'room_migration_done' - ) as { value?: string | null } - const messageDone = database.tables.chatluna_meta.find( - (item) => item.key === 'message_migration_done' - ) as { value?: string | null } - - assert.equal(JSON.parse(validation.value ?? '{}').passed, true) - assert.equal(JSON.parse(retention.value ?? '{}').state, 'purged') - assert.equal(roomDone.value, 'true') - assert.equal(messageDone.value, 'true') - assert.equal(database.tables.chatluna_conversation.length, 0) -}) - -it('ensureMigrationValidated keeps existing chatluna data when sentinel already exists', async () => { - const { ctx, database } = await createService({ - tables: { - chatluna_conversation: [ - createConversation({ - id: 'existing-conversation' - }) as unknown as TableRow - ] - } - }) - - await fs.mkdir(getLegacySchemaSentinelDir(ctx.baseDir), { recursive: true }) - await fs.writeFile(getLegacySchemaSentinel(ctx.baseDir), '{}', 'utf8') - - await ensureMigrationValidated(ctx, createConfig()) - - const validation = database.tables.chatluna_meta.find( - (item) => item.key === 'validation_result' - ) as { value?: string | null } - - assert.equal(database.tables.chatluna_conversation.length, 1) - assert.equal( - (database.tables.chatluna_conversation[0] as ConversationRecord).id, - 'existing-conversation' - ) - assert.equal(JSON.parse(validation.value ?? '{}').passed, true) -}) - -it('ensureMigrationValidated ignores a stale sentinel when legacy data is still present', async () => { - const { ctx, database } = await createService({ - tables: { - chatluna_meta: [ - { - key: 'validation_result', - value: JSON.stringify({ - passed: true, - checkedAt: '2026-04-06T05:59:16.380Z' - }), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow, - { - key: 'schema_version', - value: JSON.stringify(1), - updatedAt: new Date('2026-04-06T13:59:16.000Z') - } as unknown as TableRow - ], - chathub_room: [ - { - roomId: 1, - roomName: 'Legacy Room', - roomMasterId: 'owner', - conversationId: 'legacy-conversation', - preset: 'legacy-preset', - model: 'legacy/model', - chatMode: 'plugin', - visibility: 'private', - password: null, - autoUpdate: false, - updatedTime: new Date('2026-03-21T00:00:00.000Z') - } as unknown as TableRow - ], - chathub_conversation: [ - { - id: 'legacy-conversation', - latestId: 'legacy-message-1', - additional_kwargs: null, - updatedAt: new Date('2026-03-21T01:00:00.000Z') - } as unknown as TableRow - ], - chathub_message: [ - { - id: 'legacy-message-1', - conversation: 'legacy-conversation', - parent: null, - role: 'human', - text: 'hello from legacy room', - content: null, - name: 'owner', - tool_call_id: null, - tool_calls: null, - additional_kwargs: null, - additional_kwargs_binary: null, - rawId: null - } as unknown as TableRow - ], - chathub_room_member: [ - { - roomId: 1, - userId: 'owner', - roomPermission: 'owner', - mute: false - } as unknown as TableRow - ], - chathub_room_group_member: [], - chathub_user: [] - } - }) - - await fs.mkdir(getLegacySchemaSentinelDir(ctx.baseDir), { recursive: true }) - await fs.writeFile(getLegacySchemaSentinel(ctx.baseDir), '{}', 'utf8') - - await ensureMigrationValidated(ctx, createConfig()) - - const validation = database.tables.chatluna_meta.find( - (item) => item.key === 'validation_result' - ) as { value?: string | null } - - assert.equal(database.tables.chatluna_conversation.length, 1) - assert.equal( - (database.tables.chatluna_conversation[0] as ConversationRecord).id, - 'legacy-conversation' - ) - assert.equal(database.tables.chatluna_message.length, 1) - assert.equal(JSON.parse(validation.value ?? '{}').passed, true) -}) - -it('runRoomToConversationMigration falls back to startup recovery when sentinel already exists', async () => { - const { ctx, database } = await createService() - - await fs.mkdir(getLegacySchemaSentinelDir(ctx.baseDir), { recursive: true }) - await fs.writeFile(getLegacySchemaSentinel(ctx.baseDir), '{}', 'utf8') - - await runRoomToConversationMigration(ctx, createConfig()) - - const validation = database.tables.chatluna_meta.find( - (item) => item.key === 'validation_result' - ) as { value?: string | null } - - assert.equal(JSON.parse(validation.value ?? '{}').passed, true) -}) - -it('dropTableIfExists treats cannot resolve table as a missing table', async () => { - const dropped = await dropTableIfExists( - { - database: { - drop: async () => { - throw new Error('cannot resolve table "chathub_room"') - } - }, - logger: { - warn: () => {} - } - } as never, - 'chathub_room' - ) - - assert.equal(dropped, false) -}) - -it('inferLegacyGroupRouteModes preserves per-group legacy routing semantics', () => { - const users = [ - { userId: 'a', groupId: 'g1', defaultRoomId: 1 }, - { userId: 'b', groupId: 'g1', defaultRoomId: 2 }, - { userId: 'c', groupId: 'g2', defaultRoomId: 3 } - ] - const rooms = [ - { roomId: 1, visibility: 'private' }, - { roomId: 2, visibility: 'private' }, - { roomId: 3, visibility: 'public' } - ] as never - const groups = [ - { roomId: 1, groupId: 'g1', roomVisibility: 'private' }, - { roomId: 2, groupId: 'g1', roomVisibility: 'private' }, - { roomId: 3, groupId: 'g2', roomVisibility: 'public' } - ] as never - - const modes = inferLegacyGroupRouteModes(users as never, rooms, groups) - - assert.equal( - createLegacyBindingKey(users[0] as never, modes), - 'personal:legacy:legacy:g1:a' - ) - assert.equal( - createLegacyBindingKey(users[1] as never, modes), - 'personal:legacy:legacy:g1:b' - ) - assert.equal( - createLegacyBindingKey(users[2] as never, modes), - 'shared:legacy:legacy:g2' - ) -}) - -it('getLegacySchemaSentinel resolves under baseDir', () => { - assert.equal( - getLegacySchemaSentinel('C:/chatluna-base'), - path.resolve( - 'C:/chatluna-base', - 'data/chatluna/temp/legacy-schema-disabled.json' - ) - ) -}) - -it('getLegacySchemaSentinelDir matches resolved sentinel parent', () => { - assert.equal( - getLegacySchemaSentinelDir('C:/chatluna-base'), - path.dirname(getLegacySchemaSentinel('C:/chatluna-base')) - ) -}) diff --git a/packages/core/tests/conversation-resolve.spec.ts b/packages/core/tests/conversation-resolve.spec.ts deleted file mode 100644 index 127caeb84..000000000 --- a/packages/core/tests/conversation-resolve.spec.ts +++ /dev/null @@ -1,1602 +0,0 @@ -/// - -import { createHash } from 'crypto' -import { assert } from 'chai' -import { ChainMiddlewareRunStatus, ChatChain } from '../src/chains/chain' -import { apply as applyRead } from '../src/middlewares/chat/read_chat_message' -import { apply as applyMessageDelay } from '../src/middlewares/chat/message_delay' -import { apply as applyTimeLimitSave } from '../src/middlewares/chat/chat_time_limit_save' -import { apply as applyResolve } from '../src/middlewares/conversation/resolve_conversation' -import { apply as applyRequest } from '../src/middlewares/conversation/request_conversation' -import { apply as applyManage } from '../src/middlewares/system/conversation_manage' -import { apply as applyLifecycle } from '../src/middlewares/system/lifecycle' -import { apply as applyCommands } from '../src/commands/conversation' -import { ChatLunaError, ChatLunaErrorCode } from '../src/utils/error' -import { - createConfig, - createConversation, - createMemoryService, - createSession -} from './helpers' - -it('resolve_conversation inherits active preset lane for explicit commands without target', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let useRoutePresetLane: boolean | undefined - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - useRoutePresetLane = opts.useRoutePresetLane - return { - bindingKey: 'shared:discord:bot:guild:preset:helper', - constraint: {}, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: null, - conversationId: null, - mode: 'context' - } - } - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'chat', - options: {} - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal(useRoutePresetLane, true) - } finally { - await app.stop() - } -}) - -it('request_conversation inherits active preset lane for explicit commands without target', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let useRoutePresetLane: boolean | undefined - - ctx.chatluna.conversation.ensureActiveConversation = async ( - _session, - opts - ) => { - useRoutePresetLane = opts.useRoutePresetLane - throw new Error('stop-after-ensure') - } - - applyRequest( - ctx as never, - { - streamResponse: false, - splitMessage: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - } - } - } - } as never - ) - - try { - await run!(session, { - command: 'voice', - options: { - inputMessage: { - content: 'hello' - } - } - }) - assert.fail('Expected middleware to stop after ensure.') - } catch (err) { - assert.match(String(err), /stop-after-ensure/) - } - - assert.equal(useRoutePresetLane, false) - } finally { - await app.stop() - } -}) - -it('request_conversation uses resolved conversation state instead of legacy top-level ids', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let opts: any - - ctx.chatluna.conversation.ensureActiveConversation = async ( - _session, - value - ) => { - opts = value - throw new Error('stop-after-ensure') - } - - applyRequest( - ctx as never, - { - streamResponse: false, - splitMessage: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - } - } - } - } as never - ) - - try { - await run!(session, { - command: 'chat', - options: { - conversationId: 'legacy-conversation', - conversation: { - conversationId: 'resolved-conversation', - bindingKey: 'shared:discord:bot:guild', - presetLane: 'helper' - }, - inputMessage: { - content: 'hello' - } - } - }) - assert.fail('Expected middleware to stop after ensure.') - } catch (err) { - assert.match(String(err), /stop-after-ensure/) - } - - assert.equal(opts.conversationId, 'resolved-conversation') - assert.equal(opts.bindingKey, 'shared:discord:bot:guild') - assert.equal(opts.presetLane, 'helper') - } finally { - await app.stop() - } -}) - -it('resolve_conversation restores target suggestions for mistyped explicit targets', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - session.text = (key) => key - session.suggest = async ({ actual, expect, suffix }) => - `${actual}|${expect.join(',')}|${suffix}` - ctx.chatluna.conversation.resolveConversation = async () => ({ - bindingKey: 'shared:discord:bot:guild', - constraint: {}, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: null, - conversationId: null, - mode: 'target' - }) - ctx.chatluna.conversation.listConversationEntries = async () => [ - { - displaySeq: 1, - conversation: { - id: 'conversation-1', - title: 'First Topic' - } - } - ] - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: '', - options: { - targetConversation: 'frist' - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - state.message, - 'frist|conversation-1,1,First Topic|commands.chatluna.chat.text.options.conversation' - ) - } finally { - await app.stop() - } -}) - -it('resolve_conversation restores target-specific ambiguous errors', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - session.text = (key) => key - ctx.chatluna.conversation.resolveConversation = async () => { - throw new ChatLunaError( - ChatLunaErrorCode.CONVERSATION_TARGET_AMBIGUOUS, - new Error('Conversation target is ambiguous.') - ) - } - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'conversation_switch', - options: { - conversation_manage: { - targetConversation: 'topic' - } - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - state.message, - 'chatluna.conversation.messages.target_ambiguous' - ) - } finally { - await app.stop() - } -}) - -it('resolve_conversation prefers pre-resolved conversation state over legacy top-level ids', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let opts: any - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - value - ) => { - opts = value - return { - bindingKey: 'shared:discord:bot:guild', - constraint: { - bindingKey: 'shared:discord:bot:guild' - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: createConversation({ - id: 'resolved-conversation' - }), - conversationId: 'resolved-conversation', - mode: 'target' - } - } - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'chat', - options: { - conversationId: 'stale-conversation', - conversation: { - conversationId: 'resolved-conversation' - } - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal(opts.mode, 'target') - assert.equal(opts.conversationId, 'resolved-conversation') - assert.equal( - state.options.conversation.conversationId, - 'resolved-conversation' - ) - } finally { - await app.stop() - } -}) - -it('resolve_conversation maps outside-route errors by code', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - session.text = (key) => key - ctx.chatluna.conversation.resolveConversation = async () => { - throw new ChatLunaError( - ChatLunaErrorCode.CONVERSATION_TARGET_OUTSIDE_ROUTE, - new Error('Conversation does not belong to current route.') - ) - } - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'conversation_restore', - options: { - conversation_manage: { - targetConversation: 'conversation-1' - } - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - state.message, - 'chatluna.conversation.messages.target_outside_route' - ) - } finally { - await app.stop() - } -}) - -it('transform_chat_message continues when conversation resolution stopped earlier', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let calls = 0 - - ctx.chatluna.messageTransformer.transform = async () => { - calls += 1 - return 'unexpected' - } - - applyRead( - ctx as never, - { - includeQuoteReply: false, - attachForwardMsgIdToContext: false - } as never, - { - middleware: (name, fn) => { - if (name === 'transform_chat_message') { - run = fn as never - } - - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - options: { - chatMessage: [] - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal(calls, 0) - } finally { - await app.stop() - } -}) - -it('resolve_conversation uses command-specific target suffix for conversation management commands', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - session.text = (key) => key - session.suggest = async ({ actual, expect, suffix }) => - `${actual}|${expect.join(',')}|${suffix}` - ctx.chatluna.conversation.resolveConversation = async () => ({ - bindingKey: 'shared:discord:bot:guild', - constraint: {}, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: null, - conversationId: null, - mode: 'target' - }) - ctx.chatluna.conversation.listConversationEntries = async () => [ - { - displaySeq: 1, - conversation: { - id: 'conversation-1', - title: 'First Topic' - } - } - ] - - applyResolve( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'conversation_restore', - options: { - conversation_manage: { - targetConversation: 'frist' - } - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - state.message, - 'frist|conversation-1,1,First Topic|commands.chatluna.restore.arguments.conversation' - ) - } finally { - await app.stop() - } -}) - -it('conversation_rule_share reports the effective route after reset', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - const runs = new Map< - string, - (session: any, context: any) => Promise - >() - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - ctx.chatluna.conversation.updateManagedConstraint = async () => ({ - routeMode: null - }) - ctx.chatluna.conversation.resolveConstraint = async () => ({ - routeMode: 'shared' - }) - - applyManage( - ctx as never, - {} as never, - { - middleware: (name, fn) => { - runs.set(name, fn as never) - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const run = runs.get('conversation_rule_share') - const state = { - command: 'conversation_rule_share', - options: { - conversation_rule: { - share: 'reset' - }, - conversation: { - constraint: { - routeMode: 'personal' - } - } - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - state.message, - 'chatluna.conversation.messages.rule_share_status:shared' - ) - } finally { - await app.stop() - } -}) - -it('conversation_switch accepts resolved direct conversation ids', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let conversationId: string | undefined - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - ctx.chatluna.conversation.switchConversation = async ( - _session, - opts - ) => { - conversationId = opts.conversationId - return { - id: 'conversation-1', - title: 'First Topic', - seq: 1 - } - } - - applyManage( - ctx as never, - {} as never, - { - middleware: (name, fn) => { - if (name === 'conversation_switch') { - run = fn as never - } - - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'conversation_switch', - options: { - conversation: { - conversationId: 'conversation-direct', - conversation: { - id: 'conversation-direct', - title: 'Direct Topic' - } - }, - conversation_manage: {} - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(conversationId, 'conversation-direct') - assert.equal( - state.message, - 'chatluna.conversation.messages.switch_success:First Topic,1,conversation-1' - ) - } finally { - await app.stop() - } -}) - -it('chatluna.delete removes range and list selectors', async () => { - const conversations = [ - createConversation({ - id: 'conversation-first', - title: 'First Topic', - seq: 1 - }), - createConversation({ - id: 'conversation-second', - title: 'Second Topic', - seq: 2 - }), - createConversation({ - id: 'conversation-third', - title: 'Third Topic', - seq: 3 - }) - ] - const actions = new Map Promise>() - const deleted: string[] = [] - const failures = new Set() - const messages: string[] = [] - const resolveTargets: (string | undefined)[] = [] - let listCalls = 0 - const entries = () => - conversations.map((conversation, idx) => ({ - conversation, - displaySeq: idx + 1 - })) - let resolve: - | ((session: any, context: any) => Promise) - | undefined - let run: - | ((session: any, context: any) => Promise) - | undefined - const ctx = { - command: (decl: string) => { - const cmd = { - alias: () => cmd, - option: () => cmd, - action: (fn: (...args: any[]) => Promise) => { - actions.set(decl, fn) - return cmd - } - } - return cmd - }, - chatluna: { - conversation: { - resolveConversation: async (_session, opts) => { - resolveTargets.push(opts.targetConversation) - const entry = entries().find( - (item) => - opts.targetConversation === item.conversation.id || - opts.targetConversation === - String(item.displaySeq) || - opts.targetConversation === item.conversation.title - ) - - return { - bindingKey: 'shared:discord:bot:guild', - constraint: {}, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: entry?.conversation ?? null, - conversationId: entry?.conversation.id ?? null, - mode: opts.mode - } - }, - listConversationEntries: async () => { - listCalls += 1 - return entries() - }, - deleteConversation: async (_session, opts) => { - if (failures.has(opts.conversationId)) { - throw new Error('delete failed') - } - deleted.push(opts.conversationId) - return conversations.find( - (item) => item.id === opts.conversationId - ) - } - } - } - } - const chain = { - middleware: (name, fn) => { - if (name === 'resolve_conversation') resolve = fn - if (name === 'conversation_delete') run = fn - return { - after() { - return this - }, - before() { - return this - } - } - } - } - - applyResolve(ctx as never, {} as never, chain as never) - applyManage(ctx as never, {} as never, chain as never) - applyCommands( - ctx as never, - {} as never, - { - receiveCommand: async (session, name, opts) => { - const state = { command: name, options: opts } - const status = await resolve!(session, state) - if (status !== ChainMiddlewareRunStatus.STOP) { - await run!(session, state) - } - } - } as never - ) - - const session = createSession() as any - session.text = (key, params) => { - const msg = params == null ? key : `${key}:${params.join(',')}` - messages.push(msg) - return msg - } - - const action = actions.get('chatluna.delete [conversation:text]')! - await action({ options: {}, session }, '1..2') - await action({ options: {}, session }, '1,3') - await action({ options: {}, session }, '1,3') - await action({ options: {}, session }, '1; 2;3') - await action({ options: {}, session }, '1 2') - await action({ options: {}, session }, '1..2,4') - const validCount = deleted.length - await action({ options: {}, session }, '1,4') - assert.equal(deleted.length, validCount) - - await action({ options: {}, session }, '1,1') - await action({ options: {}, session }, '3..1') - - const beforeInvalid = deleted.length - await action({ options: {}, session }, '4') - const beforeOverlap = listCalls - await action({ options: {}, session }, '1..50,25..75') - assert.equal(listCalls, beforeOverlap + 1) - await action({ options: {}, session }, '1..101') - await action( - { options: {}, session }, - '999999999999999999999..999999999999999999999' - ) - assert.equal(deleted.length, beforeInvalid) - - // Title with spaces must not be treated as batch selectors. - conversations.push( - createConversation({ - id: 'conversation-space-title', - title: 'Claude Code 黑子', - seq: 4 - }) - ) - await action({ options: {}, session }, 'Claude Code 黑子') - - conversations.push( - createConversation({ - id: 'conversation-range-title', - title: '1..101', - seq: 5 - }) - ) - await action({ options: {}, session }, '1..101') - - assert.deepEqual(deleted, [ - 'conversation-first', - 'conversation-second', - 'conversation-first', - 'conversation-third', - 'conversation-first', - 'conversation-third', - 'conversation-first', - 'conversation-second', - 'conversation-third', - 'conversation-first', - 'conversation-second', - 'conversation-first', - 'conversation-first', - 'conversation-second', - 'conversation-third', - 'conversation-space-title', - 'conversation-range-title' - ]) - assert.deepEqual(resolveTargets, [ - '1..2', - '1,3', - '1,3', - '1; 2;3', - '1 2', - '1..2,4', - '1,4', - '1,1', - '3..1', - '4', - '1..50,25..75', - '1..101', - '999999999999999999999..999999999999999999999', - 'Claude Code 黑子', - '1..101' - ]) - assert.deepEqual(messages, [ - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nSecond Topic (2)', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nThird Topic (3)', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nThird Topic (3)', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nSecond Topic (2)\nThird Topic (3)', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nSecond Topic (2)', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)', - 'chatluna.conversation.messages.delete_success_multi:First Topic (1)\nSecond Topic (2)\nThird Topic (3)', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.target_not_found:412', - 'chatluna.conversation.messages.delete_success:Claude Code 黑子,4,conversation-space-title', - 'chatluna.conversation.messages.delete_success:1..101,5,conversation-range-title' - ]) - - failures.add('conversation-second') - await action({ options: {}, session }, '1,2') - assert.equal( - messages.at(-1), - 'chatluna.conversation.messages.delete_partial:First Topic (1),Second Topic (2)' - ) - - failures.add('conversation-first') - await action({ options: {}, session }, '1,2') - assert.equal( - messages.at(-1), - 'chatluna.conversation.messages.delete_failed:First Topic (1)\nSecond Topic (2)' - ) -}) - -it('conversation_switch preserves explicit chain conversation through middleware order', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - const sent: string[] = [] - const chain = new ChatChain( - ctx, - createConfig({ - isForwardMsg: false, - isReplyWithAt: false, - forwardMsgMinLength: 99999 - }) - ) - const constraint = { - routeMode: 'shared', - bindingKey: 'shared:discord:bot:guild', - baseKey: 'shared:discord:bot:guild', - constraints: [], - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'admin' - } - let resolveOpts: any - let switchedId: string | undefined - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - session.sendQueued = async () => {} - chain.sender(async (_session, messages) => { - const msg = messages[0] - if (typeof msg === 'string') { - sent.push(msg) - } - }) - - chain.middleware('read_chat_message' as never, async () => 0) - chain.middleware('transform_chat_message' as never, async () => 0) - chain.middleware('resolve_model' as never, async () => 0) - - applyLifecycle(ctx as never, {} as never, chain) - applyResolve(ctx as never, {} as never, chain) - applyManage(ctx as never, {} as never, chain) - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - resolveOpts = opts - - return { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: createConversation({ - id: opts.conversationId, - title: 'Direct Topic' - }), - conversationId: opts.conversationId, - mode: opts.mode - } - } - ctx.chatluna.conversation.switchConversation = async ( - _session, - opts - ) => { - switchedId = opts.conversationId - - return { - id: 'conversation-1', - title: 'First Topic', - seq: 1 - } - } - - const handled = await chain.receiveCommand( - session, - 'conversation_switch', - { - conversation: { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: 'conversation-direct', - conversation: createConversation({ - id: 'conversation-direct', - title: 'Direct Topic' - }) - }, - conversation_manage: {} - } - ) - - assert.equal(handled, false) - assert.equal(resolveOpts.mode, 'target') - assert.equal(resolveOpts.conversationId, 'conversation-direct') - assert.equal(switchedId, 'conversation-direct') - assert.deepEqual(sent, [ - 'chatluna.conversation.messages.switch_success:First Topic,1,conversation-1' - ]) - } finally { - await app.stop() - } -}) - -it('conversation_switch prefers explicit target over preexisting chain conversation', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - const sent: string[] = [] - const chain = new ChatChain( - ctx, - createConfig({ - isForwardMsg: false, - isReplyWithAt: false, - forwardMsgMinLength: 99999 - }) - ) - const constraint = { - routeMode: 'shared', - bindingKey: 'shared:discord:bot:guild', - baseKey: 'shared:discord:bot:guild', - constraints: [], - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'admin' - } - let resolveOpts: any - let switchedId: string | undefined - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - session.sendQueued = async () => {} - chain.sender(async (_session, messages) => { - const msg = messages[0] - if (typeof msg === 'string') { - sent.push(msg) - } - }) - - chain.middleware('read_chat_message' as never, async () => 0) - chain.middleware('transform_chat_message' as never, async () => 0) - chain.middleware('resolve_model' as never, async () => 0) - - applyLifecycle(ctx as never, {} as never, chain) - applyResolve(ctx as never, {} as never, chain) - applyManage(ctx as never, {} as never, chain) - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - resolveOpts = opts - - return { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: createConversation({ - id: opts.targetConversation, - title: 'Target Topic' - }), - conversationId: opts.targetConversation, - mode: opts.mode - } - } - ctx.chatluna.conversation.switchConversation = async ( - _session, - opts - ) => { - switchedId = opts.conversationId - - return { - id: 'conversation-target', - title: 'Target Topic', - seq: 2 - } - } - - const handled = await chain.receiveCommand( - session, - 'conversation_switch', - { - conversation: { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: 'conversation-direct', - conversation: createConversation({ - id: 'conversation-direct', - title: 'Direct Topic' - }) - }, - conversation_manage: { - targetConversation: 'conversation-target' - } - } - ) - - assert.equal(handled, false) - assert.equal(resolveOpts.mode, 'target') - assert.equal(resolveOpts.conversationId, undefined) - assert.equal(resolveOpts.targetConversation, 'conversation-target') - assert.equal(switchedId, 'conversation-target') - assert.deepEqual(sent, [ - 'chatluna.conversation.messages.switch_success:Target Topic,2,conversation-target' - ]) - } finally { - await app.stop() - } -}) - -it('conversation_switch prefers explicit target over preexisting resolved conversation ids', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - const sent: string[] = [] - const chain = new ChatChain( - ctx, - createConfig({ - isForwardMsg: false, - isReplyWithAt: false, - forwardMsgMinLength: 99999 - }) - ) - const constraint = { - routeMode: 'shared', - bindingKey: 'shared:discord:bot:guild', - baseKey: 'shared:discord:bot:guild', - constraints: [], - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'admin' - } - let resolveOpts: any - let switchedId: string | undefined - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - session.sendQueued = async () => {} - chain.sender(async (_session, messages) => { - const msg = messages[0] - if (typeof msg === 'string') { - sent.push(msg) - } - }) - - chain.middleware('read_chat_message' as never, async () => 0) - chain.middleware('transform_chat_message' as never, async () => 0) - chain.middleware('resolve_model' as never, async () => 0) - - applyLifecycle(ctx as never, {} as never, chain) - applyResolve(ctx as never, {} as never, chain) - applyManage(ctx as never, {} as never, chain) - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - resolveOpts = opts - - return { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: createConversation({ - id: opts.targetConversation, - title: 'Target Topic' - }), - conversationId: opts.targetConversation, - mode: opts.mode - } - } - ctx.chatluna.conversation.switchConversation = async ( - _session, - opts - ) => { - switchedId = opts.conversationId - - return { - id: 'conversation-target', - title: 'Target Topic', - seq: 2 - } - } - - const handled = await chain.receiveCommand( - session, - 'conversation_switch', - { - conversation: { - conversationId: 'conversation-direct' - }, - conversation_manage: { - targetConversation: 'conversation-target' - } - } - ) - - assert.equal(handled, false) - assert.equal(resolveOpts.mode, 'target') - assert.equal(resolveOpts.conversationId, undefined) - assert.equal(resolveOpts.targetConversation, 'conversation-target') - assert.equal(switchedId, 'conversation-target') - assert.deepEqual(sent, [ - 'chatluna.conversation.messages.switch_success:Target Topic,2,conversation-target' - ]) - } finally { - await app.stop() - } -}) - -it('message_delay uses the resolved conversation only', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let appendedId: string | undefined - - ctx.chatluna.conversationRuntime.appendPendingMessage = async (id) => { - appendedId = id - return true - } - - applyMessageDelay( - ctx as never, - { - messageQueue: true, - messageQueueDelay: 0 - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - options: { - conversationId: 'legacy-conversation', - inputMessage: { - content: 'hello', - name: 'tester' - }, - conversation: { - conversation: createConversation({ - id: 'resolved-conversation' - }) - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(appendedId, 'resolved-conversation') - } finally { - await app.stop() - } -}) - -it('message_delay keeps collected messages in send order', async () => { - const { app, ctx } = await createMemoryService() - - try { - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - applyMessageDelay( - ctx as never, - { - messageQueue: true, - messageQueueDelay: 0.01 - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const conversation = createConversation({ - id: 'message-delay-order' - }) - const contexts = [1, 2, 3].map((num) => ({ - options: { - inputMessage: { - content: String(num), - name: 'tester' - }, - conversation: { - conversation - } - } - })) - - const p3 = run!( - { ...createSession(), timestamp: 3 } as any, - contexts[2] - ) - const p2 = run!( - { ...createSession(), timestamp: 2 } as any, - contexts[1] - ) - const p1 = run!( - { ...createSession(), timestamp: 1 } as any, - contexts[0] - ) - const statuses = await Promise.all([p1, p2, p3]) - const content = contexts[0].options.inputMessage.content as { - text?: string - }[] - - assert.deepEqual(statuses, [ - ChainMiddlewareRunStatus.CONTINUE, - ChainMiddlewareRunStatus.STOP, - ChainMiddlewareRunStatus.STOP - ]) - assert.equal(content.map((part) => part.text ?? '').join(''), '1\n2\n3') - ctx.emit('chatluna/after-chat', 'message-delay-order') - } finally { - await app.stop() - } -}) - -it('chat_time_limit_save uses the resolved conversation only', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let key: string | undefined - - applyTimeLimitSave( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - options: { - conversationId: 'legacy-conversation', - conversation: { - conversationId: 'resolved-conversation' - }, - chatLimit: { - count: 0 - }, - chatLimitCache: { - set: async (value, data) => { - key = value - assert.equal(data.count, 1) - } - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal( - key, - createHash('md5') - .update(`resolved-conversation-${session.userId}`) - .digest('hex') - ) - } finally { - await app.stop() - } -}) - -it('conversation_new keeps preset option out of preset lane resolution', async () => { - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - const sent: string[] = [] - const chain = new ChatChain( - ctx, - createConfig({ - isForwardMsg: false, - isReplyWithAt: false, - forwardMsgMinLength: 99999 - }) - ) - const constraint = { - routeMode: 'shared', - bindingKey: 'shared:discord:bot:guild', - baseKey: 'shared:discord:bot:guild', - constraints: [], - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'admin' - } - let resolveOpts: any - let createOpts: any - - session.text = (key, params) => - params == null ? key : `${key}:${params.join(',')}` - session.sendQueued = async () => {} - chain.sender(async (_session, messages) => { - const msg = messages[0] - if (typeof msg === 'string') { - sent.push(msg) - } - }) - - chain.middleware('read_chat_message' as never, async () => 0) - chain.middleware('transform_chat_message' as never, async () => 0) - chain.middleware('resolve_model' as never, async () => 0) - - applyLifecycle(ctx as never, {} as never, chain) - applyResolve(ctx as never, {} as never, chain) - applyManage(ctx as never, {} as never, chain) - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - resolveOpts = opts - - return { - bindingKey: 'shared:discord:bot:guild', - constraint, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversation: null, - conversationId: null, - mode: opts.mode - } - } - ctx.chatluna.conversation.createConversation = async ( - _session, - opts - ) => { - createOpts = opts - - return createConversation({ - id: 'conversation-new', - bindingKey: opts.bindingKey, - title: opts.title, - preset: opts.preset, - model: opts.model, - chatMode: opts.chatMode - }) - } - - const handled = await chain.receiveCommand( - session, - 'conversation_new', - { - conversation_create: { - preset: 'writer' - } - } - ) - - assert.equal(handled, false) - assert.equal(resolveOpts.mode, 'context') - assert.equal(resolveOpts.presetLane, undefined) - assert.equal(createOpts.preset, 'writer') - assert.equal(createOpts.bindingKey, 'shared:discord:bot:guild') - assert.deepEqual(sent, [ - 'chatluna.conversation.messages.new_success:chatluna.conversation.default_title,1,conversation-new' - ]) - } finally { - await app.stop() - } -}) diff --git a/packages/core/tests/conversation-runtime.spec.ts b/packages/core/tests/conversation-runtime.spec.ts deleted file mode 100644 index 18dcb8c56..000000000 --- a/packages/core/tests/conversation-runtime.spec.ts +++ /dev/null @@ -1,246 +0,0 @@ -/// - -import { HumanMessage } from '@langchain/core/messages' -import { assert } from 'chai' -import { ConversationRuntime } from '../src/services/conversation_runtime' -import { createConversation, createSession } from './helpers' - -it('ConversationRuntime registers, resolves, and stops active requests', () => { - const runtime = new ConversationRuntime({} as never) - const abortController = new AbortController() - const session = createSession({ sid: 'sid-1' }) - - runtime.registerRequest( - 'conversation-1', - 'request-1', - 'plugin', - 'platform', - abortController, - session - ) - - assert.equal(runtime.getRequestId(session, 'conversation-1'), 'request-1') - assert.equal(runtime.stopRequest('request-1'), true) - assert.equal(abortController.signal.aborted, true) - assert.equal(runtime.stopRequest('missing-request'), false) - - runtime.completeRequest('conversation-1', 'request-1') - assert.equal(runtime.getRequestId(session, 'conversation-1'), undefined) -}) - -it('ConversationRuntime chat preserves additional kwargs metadata', async () => { - const runtime = new ConversationRuntime({ - createChatInterface: async () => ({ - chat: async () => ({ - message: new HumanMessage('placeholder') - }) - }), - awaitLoadPlatform: async () => {}, - currentConfig: { - showThoughtMessage: true - }, - platform: { - resolveToolMask: async () => undefined, - getClient: async () => ({ - value: { - configPool: { - getConfig: () => ({ - value: { - concurrentMaxSize: 1 - } - }) - } - } - }) - }, - resolveCallbacks: async (input) => input.callbacks, - ctx: { - root: { - parallel: async () => {} - } - } - } as never) - - const conversation = createConversation({ - id: 'conversation-runtime-chat', - model: 'platform/model' - }) - const chatInterface = { - chat: async () => ({ - message: { - content: 'assistant reply', - additional_kwargs: { - provider: 'mock', - reasoning_content: 'thinking', - reasoning_time: 1000 - }, - usage_metadata: { - total_tokens: 12, - input_tokens: 5, - output_tokens: 7 - } - } - }) - } - runtime.interfaces.set(conversation.id, { - conversation, - chatInterface: chatInterface as never - }) - - const result = await runtime.chat( - createSession(), - conversation, - { - content: 'hello' - }, - {} as never - ) - - assert.deepEqual(result.additional_kwargs, { - provider: 'mock', - reasoning_content: 'thinking', - reasoning_time: 1000 - }) - assert.equal(result.additionalReplyMessages?.length, 2) -}) - -it('ConversationRuntime appendPendingMessage waits for plugin round decisions', async () => { - const runtime = new ConversationRuntime({} as never) - const activeRequest = runtime.registerRequest( - 'conversation-1', - 'request-1', - 'plugin', - 'platform', - new AbortController(), - createSession() - ) - - const pushed: HumanMessage[] = [] - const originalPush = activeRequest.messageQueue.push.bind( - activeRequest.messageQueue - ) - activeRequest.messageQueue.push = ((message: HumanMessage) => { - pushed.push(message) - return originalPush(message) - }) as typeof activeRequest.messageQueue.push - - const pending = runtime.appendPendingMessage( - 'conversation-1', - new HumanMessage('follow-up') - ) - - assert.equal(activeRequest.roundDecisionResolvers.length, 1) - activeRequest.roundDecisionResolvers[0](true) - assert.equal(await pending, true) - assert.equal(pushed.length, 1) - assert.equal(String(pushed[0].content), 'follow-up') - - activeRequest.lastDecision = false - assert.equal( - await runtime.appendPendingMessage( - 'conversation-1', - new HumanMessage('ignored'), - 'plugin' - ), - false - ) - assert.equal( - await runtime.appendPendingMessage( - 'conversation-1', - new HumanMessage('wrong-mode'), - 'chat' - ), - false - ) -}) - -it('ConversationRuntime clears cached interfaces and dispatches compression', async () => { - const cleared: string[] = [] - const compressed: boolean[] = [] - const runtime = new ConversationRuntime({ - createChatInterface: async () => ({ - clearChatHistory: async () => { - cleared.push('cleared') - }, - compressContext: async (force: boolean) => { - compressed.push(force) - return { - compressed: true, - inputTokens: 10, - outputTokens: 5, - reducedPercent: 50 - } - } - }), - awaitLoadPlatform: async () => {}, - platform: { - getClient: async () => ({ - value: { - configPool: { - getConfig: () => ({ - value: { - concurrentMaxSize: 1 - } - }) - } - } - }) - }, - ctx: { - root: { - parallel: async () => {} - } - } - } as never) - - const conversation = createConversation({ - id: 'conversation-runtime', - model: 'platform/model' - }) - - await runtime.ensureChatInterface(conversation) - assert.equal(runtime.getCachedConversations().length, 1) - - await runtime.clearConversationHistory(conversation) - assert.deepEqual(cleared, ['cleared']) - assert.equal(runtime.getCachedConversations().length, 0) - - const result = await runtime.compressConversation(conversation, true) - assert.equal(result.compressed, true) - assert.deepEqual(compressed, [true]) -}) - -it('ConversationRuntime dispose clears platform-scoped and global state', () => { - const runtime = new ConversationRuntime({} as never) - const session = createSession({ sid: 'sid-dispose' }) - const conversation = createConversation({ id: 'conversation-dispose' }) - - runtime.interfaces.set(conversation.id, { - conversation, - chatInterface: {} as never - }) - runtime.registerRequest( - conversation.id, - 'request-dispose', - 'plugin', - 'platform-a', - new AbortController(), - session - ) - - runtime.dispose('platform-a') - assert.equal(runtime.interfaces.has(conversation.id), false) - assert.equal(runtime.activeByConversation.has(conversation.id), false) - - runtime.registerRequest( - 'conversation-2', - 'request-2', - 'plugin', - 'platform-b', - new AbortController(), - createSession({ sid: 'sid-2' }) - ) - runtime.dispose() - assert.equal(runtime.activeByConversation.size, 0) - assert.equal(runtime.interfaces.size, 0) -}) diff --git a/packages/core/tests/conversation-service.spec.ts b/packages/core/tests/conversation-service.spec.ts deleted file mode 100644 index 99147190d..000000000 --- a/packages/core/tests/conversation-service.spec.ts +++ /dev/null @@ -1,1676 +0,0 @@ -/// - -import fs from 'node:fs/promises' -import os from 'node:os' -import path from 'node:path' -import { assert } from 'chai' -import type { - ACLRecord, - ArchiveRecord, - BindingRecord, - ConversationRecord, - ConstraintRecord -} from '../src/types' -import { ChatLunaError, ChatLunaErrorCode } from '../src/utils/error' -import { gzipEncode } from '../src/utils/compression' -import { - createConversation, - createMessage, - createService, - createSession, - expectRejected, - type TableRow -} from './helpers' - -it('ConversationService resolves routed constraints and preset lanes', async () => { - const highPriorityConstraint: ConstraintRecord = { - id: 2, - name: 'shared route', - enabled: true, - priority: 10, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - users: null, - excludeUsers: null, - guildId: 'guild', - routeMode: 'custom', - routeKey: 'team-alpha', - defaultModel: 'constraint/model', - fixedPreset: 'fixed-preset', - allowNew: false, - allowSwitch: true, - allowArchive: false, - allowExport: true, - manageMode: 'anyone' - } - const lowerPriorityConstraint: ConstraintRecord = { - id: 1, - name: 'fallback defaults', - enabled: true, - priority: 1, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - users: null, - excludeUsers: null, - defaultPreset: 'constraint-default-preset', - defaultChatMode: 'chat-mode-x', - fixedModel: null, - fixedChatMode: 'fixed-chat-mode' - } - - const { service } = await createService({ - tables: { - chatluna_constraint: [ - highPriorityConstraint as unknown as TableRow, - lowerPriorityConstraint as unknown as TableRow - ] - } - }) - - const resolved = await service.resolveConstraint(createSession(), { - presetLane: 'helper' - }) - - assert.equal(resolved.routeMode, 'custom') - assert.equal(resolved.baseKey, 'custom:team-alpha') - assert.equal(resolved.bindingKey, 'custom:team-alpha:preset:helper') - assert.equal(resolved.defaultModel, 'constraint/model') - assert.equal(resolved.defaultPreset, 'helper') - assert.equal(resolved.fixedPreset, 'fixed-preset') - assert.equal(resolved.fixedChatMode, 'fixed-chat-mode') - assert.equal(resolved.allowNew, false) - assert.equal(resolved.allowArchive, false) - assert.equal(resolved.manageMode, 'anyone') -}) - -it('ConversationService gives fixed preset precedence over preset lane', async () => { - const constraint: ConstraintRecord = { - id: 1, - name: 'fixed-preset', - enabled: true, - priority: 10, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - users: null, - excludeUsers: null, - fixedPreset: 'fixed-preset' - } - - const { service } = await createService({ - tables: { - chatluna_constraint: [constraint as unknown as TableRow] - } - }) - - const resolved = await service.resolveConversation(createSession(), { - presetLane: 'helper', - mode: 'context' - }) - - assert.equal(resolved.effectivePreset, 'fixed-preset') -}) - -it('ConversationService defaults resolveConversation mode to context and sets null conversationId', async () => { - const { service } = await createService() - - const resolved = await service.resolveConversation(createSession()) - - assert.equal(resolved.mode, 'context') - assert.equal(resolved.conversation, null) - assert.equal(resolved.conversationId, null) -}) - -it('ConversationService skips unavailable models before using config default', async () => { - const conversation = createConversation({ - model: 'missing-platform/old-model' - }) - const { service } = await createService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_constraint: [ - { - id: 1, - name: 'unavailable-models', - enabled: true, - priority: 10, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - users: null, - excludeUsers: null, - fixedModel: 'missing-platform/fixed-model', - defaultModel: 'missing-platform/default-model' - } as unknown as TableRow - ] - } - }) - - const resolved = await service.ensureActiveConversation(createSession()) - - assert.equal(resolved.effectiveModel, 'test-platform/test-model') - assert.equal(resolved.conversation.model, 'test-platform/test-model') -}) - -it('ConversationService resolveConversation uses explicit binding key constraints', async () => { - const remote = createConversation({ - id: 'conversation-remote-binding', - bindingKey: 'shared:discord:bot:other-guild' - }) - const { service } = await createService({ - tables: { - chatluna_conversation: [remote as unknown as TableRow], - chatluna_constraint: [ - { - id: 1, - name: 'managed:discord:bot:guild:guild', - enabled: true, - priority: 1000, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: null, - fixedPreset: null, - fixedChatMode: null, - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'admin' - } as unknown as TableRow, - { - id: 2, - name: 'managed:discord:bot:guild:other-guild', - enabled: true, - priority: 1000, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'other-guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: null, - fixedPreset: null, - fixedChatMode: null, - lockConversation: true, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'anyone' - } as unknown as TableRow - ] - } - }) - - const resolved = await service.resolveConversation( - createSession({ authority: 1 }), - { - conversationId: remote.id, - bindingKey: remote.bindingKey, - mode: 'context' - } - ) - - assert.equal(resolved.bindingKey, remote.bindingKey) - assert.equal(resolved.conversation?.id, remote.id) - assert.equal(resolved.conversationId, remote.id) - assert.equal(resolved.mode, 'context') - assert.equal(resolved.constraint.manageMode, 'anyone') - assert.equal(resolved.constraint.lockConversation, true) -}) - -it('ConversationService ensureActiveConversation creates conversation and binding on default route', async () => { - const { service, database } = await createService() - - const resolved = await service.ensureActiveConversation(createSession()) - const binding = database.tables.chatluna_binding[0] as BindingRecord - const conversation = database.tables.chatluna_conversation[0] - - assert.equal(resolved.bindingKey, 'shared:discord:bot:guild') - assert.equal(binding.activeConversationId, resolved.conversation.id) - assert.equal(conversation.id, resolved.conversation.id) - assert.equal(resolved.conversationId, resolved.conversation.id) - assert.equal(resolved.mode, 'active') - assert.equal(conversation.seq, 1) - assert.equal(conversation.legacyRoomId, null) - assert.equal(conversation.legacyMeta, null) - assert.equal(resolved.effectiveModel, 'test-platform/test-model') - assert.equal(resolved.effectivePreset, 'default-preset') - assert.equal(resolved.effectiveChatMode, 'plugin') -}) - -it('ConversationService returns null conversationId when explicit target conversation is missing', async () => { - const { service } = await createService() - - const resolved = await service.resolveConversation(createSession(), { - conversationId: 'missing-conversation', - mode: 'target' - }) - - assert.equal(resolved.conversation, null) - assert.equal(resolved.conversationId, null) - assert.equal(resolved.mode, 'target') -}) - -it('ConversationService restores archived current conversation automatically', async () => { - const archivedConversation = createConversation({ - id: 'conversation-archived', - status: 'archived', - archiveId: 'archive-1', - archivedAt: new Date('2026-03-22T00:00:00.000Z'), - latestMessageId: null - }) - const archivedPayload = { - formatVersion: 1, - exportedAt: '2026-03-22T00:00:00.000Z', - conversation: { - ...archivedConversation, - status: 'active', - archiveId: null, - archivedAt: null, - createdAt: archivedConversation.createdAt.toISOString(), - updatedAt: archivedConversation.updatedAt.toISOString(), - lastChatAt: archivedConversation.lastChatAt?.toISOString() ?? null - }, - messages: [] - } - const archiveDir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-restore-test-') - ) - const archivePath = path.join(archiveDir, 'archive.json.gz') - await fs.writeFile( - archivePath, - await gzipEncode(JSON.stringify(archivedPayload)) - ) - - const { service } = await createService({ - baseDir: archiveDir, - tables: { - chatluna_conversation: [ - archivedConversation as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: 'shared:discord:bot:guild', - activeConversationId: 'conversation-archived', - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_archive: [ - { - id: 'archive-1', - conversationId: 'conversation-archived', - path: archivePath, - formatVersion: 1, - messageCount: 0, - checksum: null, - size: 1, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } - ] - } - }) - - const resolved = await service.ensureActiveConversation(createSession()) - - assert.equal(resolved.conversation.id, 'conversation-archived') - assert.equal(resolved.conversation.status, 'active') - assert.equal(resolved.conversation.archiveId, null) -}) - -it('ConversationService does not auto-restore archived conversation without manage permission', async () => { - const archivedConversation = createConversation({ - id: 'conversation-archived-locked', - status: 'archived', - archiveId: 'archive-locked', - archivedAt: new Date('2026-03-22T00:00:00.000Z'), - latestMessageId: null - }) - const archiveDir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-restore-blocked-test-') - ) - const archivePath = path.join(archiveDir, 'archive.json.gz') - await fs.writeFile( - archivePath, - await gzipEncode( - JSON.stringify({ - formatVersion: 1, - exportedAt: '2026-03-22T00:00:00.000Z', - conversation: { - ...archivedConversation, - status: 'active', - archiveId: null, - archivedAt: null, - createdAt: archivedConversation.createdAt.toISOString(), - updatedAt: archivedConversation.updatedAt.toISOString(), - lastChatAt: - archivedConversation.lastChatAt?.toISOString() ?? null - }, - messages: [] - }) - ) - ) - - const { service } = await createService({ - baseDir: archiveDir, - tables: { - chatluna_conversation: [ - archivedConversation as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: 'shared:discord:bot:guild', - activeConversationId: archivedConversation.id, - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_archive: [ - { - id: 'archive-locked', - conversationId: archivedConversation.id, - path: archivePath, - formatVersion: 1, - messageCount: 0, - checksum: null, - size: 1, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } - ] - } - }) - - await expectRejected( - service.ensureActiveConversation(createSession({ authority: 1 })), - /administrator permission/ - ) -}) - -it('ConversationService clears archive data when deleting archived conversation', async () => { - const dir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-delete-archive-test-') - ) - const archiveDir = path.join(dir, 'archive-dir') - await fs.mkdir(archiveDir, { recursive: true }) - await fs.writeFile(path.join(archiveDir, 'manifest.json'), '{}', 'utf8') - - const conversation = createConversation({ - id: 'conversation-delete-archived', - status: 'archived', - archiveId: 'archive-delete', - archivedAt: new Date('2026-03-22T00:00:00.000Z') - }) - - const { service, database } = await createService({ - baseDir: dir, - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_archive: [ - { - id: 'archive-delete', - conversationId: conversation.id, - path: archiveDir, - formatVersion: 1, - messageCount: 0, - checksum: null, - size: 1, - state: 'ready', - createdAt: new Date(), - restoredAt: null - } as unknown as TableRow - ] - } - }) - - const deleted = await service.deleteConversation(createSession(), { - conversationId: conversation.id - }) - const stored = database.tables - .chatluna_conversation[0] as ConversationRecord - const binding = database.tables.chatluna_binding[0] as BindingRecord - - assert.equal(deleted.status, 'deleted') - assert.equal(deleted.archiveId, null) - assert.equal(stored.archiveId, null) - assert.equal(database.tables.chatluna_archive.length, 0) - assert.equal(binding.activeConversationId, null) - await expectRejected(fs.access(archiveDir)) -}) - -it('ConversationService ensureActiveConversation respects personal default group route mode', async () => { - const { service } = await createService({ - config: { - defaultGroupRouteMode: 'personal' - } - }) - - const resolved = await service.ensureActiveConversation(createSession()) - - assert.equal(resolved.bindingKey, 'personal:discord:bot:guild:user') - assert.equal(resolved.conversation.seq, 1) -}) - -it('ConversationService resolves numeric conversation targets by stable seq order', async () => { - const older = createConversation({ - id: 'conversation-old', - seq: 1, - title: 'Older', - lastChatAt: new Date('2026-03-20T00:00:00.000Z') - }) - const newer = createConversation({ - id: 'conversation-new', - seq: 2, - title: 'Newer Topic', - lastChatAt: new Date('2026-03-22T00:00:00.000Z') - }) - - const { service, database } = await createService({ - tables: { - chatluna_conversation: [ - older as unknown as TableRow, - newer as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: 'shared:discord:bot:guild', - activeConversationId: 'conversation-old', - lastConversationId: null, - updatedAt: new Date() - } - ] - } - }) - - const listed = await service.listConversations(createSession()) - assert.deepEqual( - listed.map((item) => item.id), - ['conversation-old', 'conversation-new'] - ) - - const bySeq = await service.switchConversation(createSession(), { - targetConversation: '2' - }) - const byId = await service.switchConversation(createSession(), { - targetConversation: 'conversation-old' - }) - const byTitle = await service.switchConversation(createSession(), { - targetConversation: 'newer topic' - }) - const byPartialTitle = await service.switchConversation(createSession(), { - targetConversation: 'Topic' - }) - const binding = database.tables.chatluna_binding[0] as BindingRecord - - assert.equal(bySeq.id, 'conversation-new') - assert.equal(byId.id, 'conversation-old') - assert.equal(byTitle.id, 'conversation-new') - assert.equal(byPartialTitle.id, 'conversation-new') - assert.equal(binding.activeConversationId, 'conversation-new') - assert.equal(binding.lastConversationId, 'conversation-old') -}) - -it('ConversationService lists and switches preset lanes across canonical and legacy routes', async () => { - const session = createSession({ - platform: 'onebot', - selfId: '1016049163', - guildId: '391122026', - channelId: '391122026' - }) - const canonicalBase = 'shared:onebot:1016049163:391122026' - const legacyBase = 'shared:legacy:legacy:391122026' - const legacy = createConversation({ - id: 'conversation-legacy', - bindingKey: legacyBase, - title: 'Legacy', - seq: 1, - lastChatAt: new Date('2026-03-21T00:00:00.000Z') - }) - const aqua = createConversation({ - id: 'conversation-aqua', - bindingKey: `${canonicalBase}:preset:Aqua`, - title: 'Aqua', - seq: 3, - lastChatAt: new Date('2026-03-22T00:00:00.000Z') - }) - const chatgpt = createConversation({ - id: 'conversation-chatgpt', - bindingKey: `${canonicalBase}:preset:chatgpt`, - title: 'ChatGPT', - seq: 2, - lastChatAt: new Date('2026-03-23T00:00:00.000Z') - }) - const sydney = createConversation({ - id: 'conversation-sydney', - bindingKey: `${canonicalBase}:preset:sydney`, - title: 'Sydney', - seq: 4, - lastChatAt: new Date('2026-03-24T00:00:00.000Z') - }) - - const { service, database } = await createService({ - tables: { - chatluna_conversation: [ - legacy as unknown as TableRow, - aqua as unknown as TableRow, - chatgpt as unknown as TableRow, - sydney as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: legacyBase, - activeConversationId: legacy.id, - lastConversationId: null, - updatedAt: new Date() - } - ] - } - }) - - const listed = await service.listConversations(session, { - allPresetLanes: true - }) - const entries = await service.listConversationEntries(session, { - allPresetLanes: true - }) - const switched = await service.switchConversation(session, { - targetConversation: '2', - allPresetLanes: true - }) - const managed = await service.getManagedConstraint(session) - const legacyBinding = database.tables.chatluna_binding.find( - (item) => item.bindingKey === legacyBase - ) as BindingRecord | undefined - const laneBinding = database.tables.chatluna_binding.find( - (item) => item.bindingKey === chatgpt.bindingKey - ) as BindingRecord | undefined - - assert.deepEqual( - listed.map((item) => item.id), - [ - 'conversation-legacy', - 'conversation-chatgpt', - 'conversation-aqua', - 'conversation-sydney' - ] - ) - assert.deepEqual( - entries.map((item) => [item.displaySeq, item.conversation.id]), - [ - [1, 'conversation-legacy'], - [2, 'conversation-chatgpt'], - [3, 'conversation-aqua'], - [4, 'conversation-sydney'] - ] - ) - assert.equal(switched.id, 'conversation-chatgpt') - assert.equal(legacyBinding?.activeConversationId, 'conversation-chatgpt') - assert.equal(laneBinding?.activeConversationId, 'conversation-chatgpt') - assert.equal(managed?.activePresetLane, 'chatgpt') -}) - -it('ConversationService allows exact id across preset lanes when allPresetLanes is enabled', async () => { - const laneA = createConversation({ - id: 'conversation-lane-a', - bindingKey: 'shared:discord:bot:guild:preset:A' - }) - const laneB = createConversation({ - id: 'conversation-lane-b', - bindingKey: 'shared:discord:bot:guild:preset:B' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - laneA as unknown as TableRow, - laneB as unknown as TableRow - ] - } - }) - - const resolved = await service.resolveConversation(createSession(), { - conversationId: laneB.id, - allPresetLanes: true, - permission: 'manage', - mode: 'target' - }) - - assert.equal(resolved.conversation?.id, laneB.id) - assert.equal(resolved.conversationId, laneB.id) -}) - -it('ConversationService resolves active preset lane conversation for untargeted command lookups', async () => { - const main = createConversation({ - id: 'conversation-main-lane-command' - }) - const helper = createConversation({ - id: 'conversation-helper-lane-command', - bindingKey: 'shared:discord:bot:guild:preset:helper' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - main as unknown as TableRow, - helper as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: main.bindingKey, - activeConversationId: main.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow, - { - bindingKey: helper.bindingKey, - activeConversationId: helper.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_constraint: [ - { - id: 1, - name: 'managed:discord:bot:guild:guild', - enabled: true, - priority: 1000, - createdBy: 'user', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - activePresetLane: 'helper', - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: null, - fixedPreset: null, - fixedChatMode: null, - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'anyone' - } as unknown as TableRow - ] - } - }) - - const resolved = ( - await service.resolveConversation(createSession(), { - permission: 'manage', - useRoutePresetLane: true, - mode: 'target' - }) - ).conversation - - assert.equal(resolved?.id, helper.id) -}) - -it('ConversationService allows exact id across legacy and canonical route family', async () => { - const session = createSession({ - platform: 'onebot', - selfId: '1016049163', - guildId: '391122026', - channelId: '391122026' - }) - const legacy = createConversation({ - id: 'conversation-legacy-route', - bindingKey: 'shared:legacy:legacy:391122026' - }) - const canonical = createConversation({ - id: 'conversation-canonical-route', - bindingKey: 'shared:onebot:1016049163:391122026:preset:chatgpt' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - legacy as unknown as TableRow, - canonical as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: legacy.bindingKey, - activeConversationId: legacy.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - const resolved = ( - await service.resolveConversation(session, { - conversationId: canonical.id, - allPresetLanes: true, - permission: 'manage', - mode: 'target' - }) - ).conversation - - assert.equal(resolved?.id, canonical.id) -}) - -it('ConversationService keeps current lane binding untouched and syncs route binding when switching across preset lanes', async () => { - const laneA = createConversation({ - id: 'conversation-switch-lane-a', - bindingKey: 'shared:discord:bot:guild:preset:A' - }) - const laneB = createConversation({ - id: 'conversation-switch-lane-b', - bindingKey: 'shared:discord:bot:guild:preset:B' - }) - - const { service, database } = await createService({ - tables: { - chatluna_conversation: [ - laneA as unknown as TableRow, - laneB as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: laneA.bindingKey, - activeConversationId: laneA.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - await service.switchConversation(createSession(), { - targetConversation: laneB.id, - allPresetLanes: true - }) - - const bindingA = database.tables.chatluna_binding.find( - (item) => item.bindingKey === laneA.bindingKey - ) as BindingRecord | undefined - const bindingB = database.tables.chatluna_binding.find( - (item) => item.bindingKey === laneB.bindingKey - ) as BindingRecord | undefined - const routeBinding = database.tables.chatluna_binding.find( - (item) => item.bindingKey === 'shared:discord:bot:guild' - ) as BindingRecord | undefined - - assert.equal(bindingA?.activeConversationId, laneA.id) - assert.equal(bindingB?.activeConversationId, laneB.id) - assert.equal(routeBinding?.activeConversationId, laneB.id) -}) - -it('ConversationService syncs managed preset lane when reopening archived route-family conversation', async () => { - const dir = await fs.mkdtemp( - path.join(os.tmpdir(), 'chatluna-reopen-lane-') - ) - const archivePath = path.join(dir, 'archive.json.gz') - const session = createSession({ - platform: 'onebot', - selfId: '1016049163', - guildId: '391122026', - channelId: '391122026' - }) - const legacy = createConversation({ - id: 'conversation-legacy-reopen', - bindingKey: 'shared:legacy:legacy:391122026' - }) - const archived = createConversation({ - id: 'conversation-archived-lane', - bindingKey: 'shared:onebot:1016049163:391122026:preset:chatgpt', - status: 'archived', - archiveId: 'archive-lane', - archivedAt: new Date('2026-03-25T00:00:00.000Z'), - latestMessageId: null - }) - await fs.writeFile( - archivePath, - await gzipEncode( - JSON.stringify({ - formatVersion: 1, - exportedAt: '2026-03-25T00:00:00.000Z', - conversation: { - ...archived, - status: 'active', - archiveId: null, - archivedAt: null, - createdAt: archived.createdAt.toISOString(), - updatedAt: archived.updatedAt.toISOString(), - lastChatAt: archived.lastChatAt?.toISOString() ?? null - }, - messages: [] - }) - ) - ) - - const { service } = await createService({ - baseDir: dir, - tables: { - chatluna_conversation: [ - legacy as unknown as TableRow, - archived as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: legacy.bindingKey, - activeConversationId: legacy.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_archive: [ - { - id: 'archive-lane', - conversationId: archived.id, - path: archivePath, - formatVersion: 1, - messageCount: 0, - checksum: null, - size: (await fs.stat(archivePath)).size, - state: 'ready', - createdAt: new Date('2026-03-25T00:00:00.000Z'), - restoredAt: null - } as unknown as TableRow - ] - } - }) - - const reopened = await service.reopenConversation(session, { - conversationId: archived.id, - allPresetLanes: true - }) - const managed = await service.getManagedConstraint(session) - - assert.equal(reopened.status, 'active') - assert.equal(managed?.activePresetLane, 'chatgpt') -}) - -it('ConversationService rejects ambiguous friendly conversation targets', async () => { - const alpha = createConversation({ - id: 'conversation-alpha', - seq: 1, - title: 'Project Alpha' - }) - const beta = createConversation({ - id: 'conversation-beta', - seq: 2, - title: 'Project Beta' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - alpha as unknown as TableRow, - beta as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: 'shared:discord:bot:guild', - activeConversationId: 'conversation-alpha', - lastConversationId: null, - updatedAt: new Date() - } - ] - } - }) - - await expectRejected( - service.switchConversation(createSession(), { - targetConversation: 'Project' - }), - /Conversation target is ambiguous\./ - ) -}) - -it('ConversationService prefers active local exact title matches over archived ones', async () => { - const active = createConversation({ - id: 'conversation-title-active', - title: 'Shared Topic' - }) - const archived = createConversation({ - id: 'conversation-title-archived', - title: 'Shared Topic', - status: 'archived', - archiveId: 'archive-title-exact', - archivedAt: new Date('2026-03-24T00:00:00.000Z') - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - active as unknown as TableRow, - archived as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: active.bindingKey, - activeConversationId: active.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - const resolved = ( - await service.resolveConversation(createSession(), { - targetConversation: 'Shared Topic', - permission: 'manage', - mode: 'target' - }) - ).conversation - - assert.equal(resolved?.id, active.id) -}) - -it('ConversationService prefers active local partial title matches over archived ones', async () => { - const active = createConversation({ - id: 'conversation-partial-active', - title: 'Project Mercury Active' - }) - const archived = createConversation({ - id: 'conversation-partial-archived', - title: 'Project Mercury Archived', - status: 'archived', - archiveId: 'archive-title-partial', - archivedAt: new Date('2026-03-24T00:00:00.000Z') - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - active as unknown as TableRow, - archived as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: active.bindingKey, - activeConversationId: active.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - const resolved = ( - await service.resolveConversation(createSession(), { - targetConversation: 'Mercury', - permission: 'manage', - mode: 'target' - }) - ).conversation - - assert.equal(resolved?.id, active.id) -}) - -it('ConversationService rejects ambiguous active exact title matches', async () => { - const first = createConversation({ - id: 'conversation-exact-active-a', - title: 'Shared Exact Topic' - }) - const second = createConversation({ - id: 'conversation-exact-active-b', - title: 'Shared Exact Topic' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - first as unknown as TableRow, - second as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: first.bindingKey, - activeConversationId: first.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - try { - await service.resolveConversation(createSession(), { - targetConversation: 'Shared Exact Topic', - permission: 'manage', - mode: 'target' - }) - assert.fail('Expected ambiguous target to throw.') - } catch (error) { - assert.instanceOf(error, ChatLunaError) - assert.equal( - (error as ChatLunaError).errorCode, - ChatLunaErrorCode.CONVERSATION_TARGET_AMBIGUOUS - ) - } -}) - -it('ConversationService resolves archived title targets when includeArchived is enabled', async () => { - const archived = createConversation({ - id: 'conversation-archived-explicit', - title: 'Archived Topic', - status: 'archived', - archiveId: 'archive-explicit', - archivedAt: new Date('2026-03-24T00:00:00.000Z') - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [archived as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: archived.bindingKey, - activeConversationId: null, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - const byId = ( - await service.resolveConversation(createSession(), { - conversationId: archived.id, - permission: 'manage', - mode: 'target' - }) - ).conversation - const byTitle = ( - await service.resolveConversation(createSession(), { - targetConversation: archived.title, - permission: 'manage', - includeArchived: true, - mode: 'target' - }) - ).conversation - - assert.equal(byId?.id, archived.id) - assert.equal(byTitle?.id, archived.id) -}) - -it('ConversationService rejects ambiguous archived-only exact title matches when includeArchived is enabled', async () => { - const first = createConversation({ - id: 'conversation-archived-a', - title: 'Archived Shared Topic', - status: 'archived', - archiveId: 'archive-archived-a', - archivedAt: new Date('2026-03-24T00:00:00.000Z') - }) - const second = createConversation({ - id: 'conversation-archived-b', - title: 'Archived Shared Topic', - status: 'archived', - archiveId: 'archive-archived-b', - archivedAt: new Date('2026-03-25T00:00:00.000Z') - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - first as unknown as TableRow, - second as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: first.bindingKey, - activeConversationId: null, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - try { - await service.resolveConversation(createSession(), { - targetConversation: 'Archived Shared Topic', - permission: 'manage', - includeArchived: true, - mode: 'target' - }) - assert.fail('Expected ambiguous target to throw.') - } catch (error) { - assert.instanceOf(error, ChatLunaError) - assert.equal( - (error as ChatLunaError).errorCode, - ChatLunaErrorCode.CONVERSATION_TARGET_AMBIGUOUS - ) - } -}) - -it('ConversationService keeps local archived matches ahead of global active matches when includeArchived is enabled', async () => { - const local = createConversation({ - id: 'conversation-local-archived-precedence', - title: 'Scoped Topic', - status: 'archived', - archiveId: 'archive-local-precedence', - archivedAt: new Date('2026-03-24T00:00:00.000Z') - }) - const remote = createConversation({ - id: 'conversation-remote-active-precedence', - bindingKey: 'shared:discord:bot:other-guild', - title: 'Scoped Topic' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - local as unknown as TableRow, - remote as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: local.bindingKey, - activeConversationId: null, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_acl: [ - { - conversationId: remote.id, - principalType: 'user', - principalId: 'user', - permission: 'manage' - } as unknown as TableRow - ] - } - }) - - const resolved = ( - await service.resolveConversation(createSession({ authority: 1 }), { - targetConversation: 'Scoped Topic', - permission: 'manage', - includeArchived: true, - mode: 'target' - }) - ).conversation - - assert.equal(resolved?.id, local.id) -}) - -it('ConversationService records compression metadata and use rejects fixed fields', async () => { - const conversation = createConversation() - const message = createMessage({ - id: 'summary', - conversationId: conversation.id, - text: 'compressed summary', - content: await gzipEncode(JSON.stringify('compressed summary')), - name: 'infinite_context' - }) - const { service } = await createService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_message: [message as unknown as TableRow], - chatluna_constraint: [ - { - id: 1, - name: 'managed:discord:bot:guild:guild', - enabled: true, - priority: 1000, - createdBy: 'admin', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: 'fixed-model', - fixedPreset: null, - fixedChatMode: null, - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'anyone' - } as unknown as TableRow - ] - } - }) - - const updated = await service.recordCompression(conversation.id, { - compressed: true, - inputTokens: 120, - outputTokens: 30, - reducedTokens: 90, - reducedPercent: 75, - originalMessageCount: 8, - remainingMessageCount: 1 - }) - const compression = JSON.parse(updated.compression ?? '{}') - - assert.equal(compression.count, 1) - assert.equal(compression.summary, 'compressed summary') - assert.equal(compression.outputTokens, 30) - assert.equal(compression.originalMessageCount, 8) - assert.equal(compression.remainingMessageCount, 1) - - await expectRejected( - service.updateConversationUsage(createSession(), { - model: 'other-model' - }), - /fixed to fixed-model/ - ) -}) - -it('ConversationService blocks raw id access outside route without ACL and allows manage ACL', async () => { - const local = createConversation({ - id: 'conversation-local', - bindingKey: 'shared:discord:bot:guild' - }) - const remote = createConversation({ - id: 'conversation-remote', - bindingKey: 'shared:discord:bot:other-guild' - }) - const acl: ACLRecord = { - conversationId: remote.id, - principalType: 'user', - principalId: 'user', - permission: 'manage' - } - - const { service, database } = await createService({ - tables: { - chatluna_conversation: [ - local as unknown as TableRow, - remote as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: local.bindingKey, - activeConversationId: local.id, - lastConversationId: null, - updatedAt: new Date() - } - ] - } - }) - - try { - await service.resolveConversation(createSession({ authority: 1 }), { - conversationId: remote.id, - permission: 'manage', - mode: 'target' - }) - assert.fail('Expected outside-route target to throw.') - } catch (error) { - assert.instanceOf(error, ChatLunaError) - assert.equal( - (error as ChatLunaError).errorCode, - ChatLunaErrorCode.CONVERSATION_TARGET_OUTSIDE_ROUTE - ) - assert.equal( - (error as ChatLunaError).originError?.message, - 'Conversation does not belong to current route.' - ) - } - - database.tables.chatluna_acl.push(acl as unknown as TableRow) - - const resolved = ( - await service.resolveConversation(createSession({ authority: 1 }), { - conversationId: remote.id, - permission: 'manage', - mode: 'target' - }) - ).conversation - - assert.equal(resolved.id, remote.id) -}) - -it('ConversationService resolves ACL-backed cross-route targetConversation', async () => { - const local = createConversation({ - id: 'conversation-local-2', - bindingKey: 'shared:discord:bot:guild' - }) - const remote = createConversation({ - id: 'conversation-remote-2', - bindingKey: 'shared:discord:bot:other-guild', - title: 'Remote Shared Topic', - seq: 7 - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - local as unknown as TableRow, - remote as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: local.bindingKey, - activeConversationId: local.id, - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_acl: [ - { - conversationId: remote.id, - principalType: 'user', - principalId: 'user', - permission: 'manage' - } as unknown as TableRow - ] - } - }) - - const byId = ( - await service.resolveConversation(createSession({ authority: 1 }), { - targetConversation: remote.id, - permission: 'manage', - mode: 'target' - }) - ).conversation - const byTitle = ( - await service.resolveConversation(createSession({ authority: 1 }), { - targetConversation: 'Remote Shared Topic', - permission: 'manage', - mode: 'target' - }) - ).conversation - - assert.equal(byId?.id, remote.id) - assert.equal(byTitle?.id, remote.id) -}) - -it('ConversationService rejects ambiguous global exact title matches', async () => { - const local = createConversation({ - id: 'conversation-local-title', - bindingKey: 'shared:discord:bot:guild' - }) - const remoteA = createConversation({ - id: 'conversation-remote-title-a', - bindingKey: 'shared:discord:bot:other-guild-a', - title: 'Shared Topic' - }) - const remoteB = createConversation({ - id: 'conversation-remote-title-b', - bindingKey: 'shared:discord:bot:other-guild-b', - title: 'Shared Topic' - }) - - const { service } = await createService({ - tables: { - chatluna_conversation: [ - local as unknown as TableRow, - remoteA as unknown as TableRow, - remoteB as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: local.bindingKey, - activeConversationId: local.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ], - chatluna_acl: [ - { - conversationId: remoteA.id, - principalType: 'user', - principalId: 'user', - permission: 'manage' - } as unknown as TableRow, - { - conversationId: remoteB.id, - principalType: 'user', - principalId: 'user', - permission: 'manage' - } as unknown as TableRow - ] - } - }) - - try { - await service.resolveConversation(createSession({ authority: 1 }), { - targetConversation: 'Shared Topic', - permission: 'manage', - mode: 'target' - }) - assert.fail('Expected ambiguous target to throw.') - } catch (error) { - assert.instanceOf(error, ChatLunaError) - assert.equal( - (error as ChatLunaError).errorCode, - ChatLunaErrorCode.CONVERSATION_TARGET_AMBIGUOUS - ) - assert.equal( - (error as ChatLunaError).originError?.message, - 'Conversation target is ambiguous.' - ) - } -}) - -it('ConversationService keeps local bindings untouched for cross-route switch and reopen', async () => { - const local = createConversation({ - id: 'conversation-local-switch', - bindingKey: 'shared:discord:bot:guild' - }) - const remote = createConversation({ - id: 'conversation-remote-switch', - bindingKey: 'shared:discord:bot:other-guild', - title: 'Remote Topic' - }) - - const { service, database } = await createService({ - tables: { - chatluna_conversation: [ - local as unknown as TableRow, - remote as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: local.bindingKey, - activeConversationId: local.id, - lastConversationId: null, - updatedAt: new Date() - } as unknown as TableRow - ] - } - }) - - await service.switchConversation(createSession(), { - targetConversation: remote.id - }) - await service.reopenConversation(createSession(), { - conversationId: remote.id - }) - - const localBinding = database.tables.chatluna_binding.find( - (item) => item.bindingKey === local.bindingKey - ) as BindingRecord | undefined - const remoteBinding = database.tables.chatluna_binding.find( - (item) => item.bindingKey === remote.bindingKey - ) as BindingRecord | undefined - - assert.equal(localBinding?.activeConversationId, local.id) - assert.equal(remoteBinding?.activeConversationId, remote.id) -}) - -it('ConversationService upserts and removes ACL records coherently', async () => { - const conversation = createConversation({ id: 'conversation-acl' }) - const { service } = await createService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow] - } - }) - - const created = await service.upsertAcl(conversation.id, [ - { - principalType: 'user', - principalId: 'alice', - permission: 'view' - }, - { - principalType: 'guild', - principalId: 'guild-x', - permission: 'manage' - } - ]) - - assert.equal(created.length, 2) - - const afterRemove = await service.removeAcl(conversation.id, [ - { - principalType: 'user', - principalId: 'alice' - } - ]) - - assert.deepEqual(afterRemove, [ - { - conversationId: conversation.id, - principalType: 'guild', - principalId: 'guild-x', - permission: 'manage' - } - ]) -}) - -it('ConversationService emits conversation lifecycle events for switch archive restore delete and compression', async () => { - const active = createConversation({ id: 'conversation-active', seq: 1 }) - const next = createConversation({ - id: 'conversation-next', - seq: 2, - title: 'Next Topic' - }) - const { service, events, syncCalls } = await createService({ - tables: { - chatluna_conversation: [ - active as unknown as TableRow, - next as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: active.bindingKey, - activeConversationId: active.id, - lastConversationId: null, - updatedAt: new Date() - } - ], - chatluna_message: [ - createMessage({ - id: 'summary-message', - conversationId: next.id, - content: await gzipEncode(JSON.stringify('summary')), - name: 'infinite_context', - text: 'summary' - }) as unknown as TableRow - ] - } - }) - - await service.switchConversation(createSession(), { - targetConversation: next.id - }) - await service.recordCompression(next.id, { - compressed: true, - inputTokens: 100, - outputTokens: 20, - reducedTokens: 80, - reducedPercent: 80, - originalMessageCount: 10, - remainingMessageCount: 2 - }) - - const archived = await service.archiveConversation(createSession(), { - conversationId: next.id - }) - await service.restoreConversation(createSession(), { - conversationId: next.id, - archiveId: archived.archive.id - }) - await service.deleteConversation(createSession(), { - conversationId: next.id - }) - - assert.deepEqual( - events.map((item) => item.name), - [ - 'chatluna/before-conversation-switch', - 'chatluna/after-binding-update', - 'chatluna/after-conversation-switch', - 'chatluna/conversation-compressed', - 'chatluna/before-conversation-archive', - 'chatluna/after-conversation-archive', - 'chatluna/before-conversation-restore', - 'chatluna/after-binding-update', - 'chatluna/after-conversation-restore', - 'chatluna/before-conversation-delete', - 'chatluna/after-conversation-delete' - ] - ) - assert.deepEqual(syncCalls, [next.id, next.id, next.id]) -}) diff --git a/packages/core/tests/conversation-source.spec.ts b/packages/core/tests/conversation-source.spec.ts deleted file mode 100644 index 278e961f6..000000000 --- a/packages/core/tests/conversation-source.spec.ts +++ /dev/null @@ -1,54 +0,0 @@ -/// - -import fs from 'node:fs/promises' -import path from 'node:path' -import { assert } from 'chai' -import { expectRejected } from './helpers' - -it('conversation-first runtime removes legacy room entry points from active source tree', async () => { - const coreSrc = path.resolve(__dirname, '../src') - - await Promise.all([ - expectRejected(fs.access(path.join(coreSrc, 'chains', 'rooms.ts'))), - expectRejected(fs.access(path.join(coreSrc, 'commands', 'room.ts'))), - expectRejected(fs.access(path.join(coreSrc, 'middlewares', 'room'))), - expectRejected( - fs.access( - path.join(coreSrc, 'middlewares', 'model', 'request_model.ts') - ) - ), - expectRejected(fs.access(path.join(coreSrc, 'legacy', 'types.ts'))) - ]) -}) - -it('conversation cleanup listeners in downstream packages use conversation lifecycle events', async () => { - const files = [ - path.resolve( - __dirname, - '../../extension-long-memory/src/service/memory.ts' - ), - path.resolve(__dirname, '../../extension-agent/src/service/skills.ts'), - path.resolve(__dirname, '../../extension-tools/src/plugins/todos.ts') - ] - - for (const file of files) { - const content = await fs.readFile(file, 'utf8') - assert.equal(content.includes('chatluna/clear-chat-history'), false) - assert.equal( - content.includes('chatluna/after-conversation-clear-history'), - true - ) - } -}) - -it('wipe source keeps legacy migration and runtime table cleanup wired in', async () => { - const source = await fs.readFile( - path.resolve(__dirname, '../src/middlewares/system/wipe.ts'), - 'utf8' - ) - - assert.match(source, /LEGACY_MIGRATION_TABLES/) - assert.match(source, /LEGACY_RUNTIME_TABLES/) - assert.match(source, /for \(const table of LEGACY_MIGRATION_TABLES\)/) - assert.match(source, /for \(const table of LEGACY_RUNTIME_TABLES\)/) -}) diff --git a/packages/core/tests/conversation-utils.spec.ts b/packages/core/tests/conversation-utils.spec.ts deleted file mode 100644 index 3a50591d6..000000000 --- a/packages/core/tests/conversation-utils.spec.ts +++ /dev/null @@ -1,271 +0,0 @@ -/// - -import path from 'node:path' -import { assert } from 'chai' -import { Pagination } from '../src/utils/pagination' -import { - bufferToArrayBuffer, - gzipDecode, - gzipEncode -} from '../src/utils/compression' -import { - getMessageContent, - parsePresetLaneInput -} from '../src/utils/message_content' -import { ChatLunaPromptRenderService } from '../src/services/prompt_renderer' -import { applyPresetLane, computeBaseBindingKey } from '../src/types' -import { usageSourceFromStack } from '../src/utils/usage_source' -import { selectFromList } from '../src/utils/string' -import { - createMessage, - type BindingSessionShape, - FakeDatabase -} from './helpers' -import { parseDeleteSeqs } from '../src/utils/conversation' - -it('parseDeleteSeqs supports multi-separators, ranges, and safety limits', () => { - assert.deepEqual(parseDeleteSeqs('1,3'), [1, 3]) - assert.deepEqual(parseDeleteSeqs('1,3'), [1, 3]) - assert.deepEqual(parseDeleteSeqs('1; 2;3'), [1, 2, 3]) - assert.deepEqual(parseDeleteSeqs('1 2'), [1, 2]) - assert.deepEqual(parseDeleteSeqs('1 2 3'), [1, 2, 3]) - assert.deepEqual(parseDeleteSeqs('1..2,4'), [1, 2, 4]) - assert.deepEqual(parseDeleteSeqs('1..2'), [1, 2]) - assert.deepEqual(parseDeleteSeqs('3..1'), [1, 2, 3]) - assert.deepEqual(parseDeleteSeqs('1,1'), [1]) - assert.deepEqual(parseDeleteSeqs('1..50,25..75'), [ - ...Array.from({ length: 75 }, (_, i) => i + 1) - ]) - - // Titles with spaces / non-selector text stay intact (not batch). - assert.equal(parseDeleteSeqs('Claude Code 黑子'), undefined) - assert.equal(parseDeleteSeqs('hello,world'), undefined) - assert.equal(parseDeleteSeqs('1 and 2'), undefined) - assert.equal(parseDeleteSeqs(undefined), undefined) - assert.equal(parseDeleteSeqs(''), undefined) - - // Invalid / over-limit pure selectors. - assert.equal(parseDeleteSeqs('1..101'), null) - assert.equal( - parseDeleteSeqs('999999999999999999999..999999999999999999999'), - null - ) - assert.equal(parseDeleteSeqs('0'), null) - assert.equal(parseDeleteSeqs('1..0'), null) - assert.equal(parseDeleteSeqs(`1..${'9'.repeat(510)}`), null) - assert.equal( - parseDeleteSeqs(Array.from({ length: 101 }, (_, i) => i + 1).join(',')), - null - ) -}) - -it('computeBaseBindingKey builds personal direct bindings', () => { - const bindingKey = computeBaseBindingKey( - { - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - userId: 'user', - isDirect: true - } as BindingSessionShape as never, - 'personal' - ) - - assert.equal(bindingKey, 'personal:discord:bot:direct:user') -}) - -it('computeBaseBindingKey builds shared guild bindings', () => { - const bindingKey = computeBaseBindingKey( - { - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - userId: 'user', - isDirect: false - } as BindingSessionShape as never, - 'shared' - ) - - assert.equal(bindingKey, 'shared:discord:bot:guild') -}) - -it('applyPresetLane appends preset lane when provided', () => { - assert.equal( - applyPresetLane('personal:discord:bot:guild:user', 'helper'), - 'personal:discord:bot:guild:user:preset:helper' - ) - assert.equal( - applyPresetLane('personal:discord:bot:guild:user', undefined), - 'personal:discord:bot:guild:user' - ) -}) - -it('parsePresetLaneInput normalizes alias prefixes and bare queries', () => { - assert.deepEqual( - parsePresetLaneInput('Sydney: hello', ['sydney', 'helper']), - { - preset: 'sydney', - content: 'hello', - queryOnly: false - } - ) - assert.deepEqual(parsePresetLaneInput('helper,', ['sydney', 'helper']), { - preset: 'helper', - content: '', - queryOnly: true - }) - assert.equal(parsePresetLaneInput('plain message', ['sydney']), null) -}) - -it('pagination normalizes page and limit bounds', async () => { - const pagination = new Pagination({ - page: 1, - limit: 2, - formatItem: (item) => `item:${item}`, - formatString: { - top: 'top', - bottom: 'bottom', - pages: 'page [page]/[total]' - } - }) - - await pagination.push([1, 2, 3], 'numbers') - - assert.deepEqual(await pagination.getPage(0, 0, 'numbers'), [1]) - assert.equal( - await pagination.getFormattedPage(2, 2, 'numbers'), - 'top\nitem:3\nbottom\npage 2/2' - ) -}) - -it('FakeDatabase.get applies sort and pagination modifiers', async () => { - const database = new FakeDatabase() - database.tables.chatluna_message = [ - createMessage({ - id: 'message-a', - createdAt: new Date('2026-03-21T00:00:03.000Z') - }), - createMessage({ - id: 'message-b', - createdAt: new Date('2026-03-21T00:00:01.000Z') - }), - createMessage({ - id: 'message-c', - createdAt: new Date('2026-03-21T00:00:02.000Z') - }) - ] - - const rows = await database.get( - 'chatluna_message', - {}, - { - sort: { - createdAt: 'asc' - }, - offset: 1, - limit: 1 - } - ) - - assert.deepEqual( - rows.map((row) => row.id), - ['message-c'] - ) -}) - -it('gzip helpers round-trip archived payload content', async () => { - const json = JSON.stringify({ text: 'hello', values: [1, 2, 3] }) - const compressed = await gzipEncode(json) - const base64 = await gzipEncode(json, 'base64') - const arrayBuffer = bufferToArrayBuffer(compressed) - - assert.equal(await gzipDecode(arrayBuffer), json) - assert.equal(await gzipDecode(base64), json) -}) - -it('usageSourceFromStack reads package names instead of folder names', () => { - const core = path.join( - process.cwd(), - 'packages/core/src/llm-core/platform/usage.ts' - ) - const adapter = path.join( - process.cwd(), - 'packages/adapter-openai/src/client.ts' - ) - const langchain = path.join( - process.cwd(), - 'node_modules/@langchain/core/dist/language_models/chat_models.cjs' - ) - - assert.equal( - usageSourceFromStack(` at report (${core}:16:20)`), - 'chatluna' - ) - assert.equal( - usageSourceFromStack( - [ - ` at report (${core}:16:20)`, - ` at stream (${langchain}:127:34)` - ].join('\n') - ), - 'chatluna' - ) - assert.equal( - usageSourceFromStack( - [ - ` at report (${core}:16:20)`, - ` at create (${adapter}:120:13)` - ].join('\n') - ), - 'chatluna-openai-adapter' - ) - assert.equal( - usageSourceFromStack( - ' at process.processTicksAndRejections ' + - '(node:internal/process/task_queues:105:5)' - ), - 'unknown' - ) -}) - -it('selectFromList keeps pick stable for a seed', () => { - assert.equal(selectFromList('red,green,blue', true, 'stable'), 'blue') - assert.equal(selectFromList('red,green,blue', true, 'stable'), 'blue') - assert.equal( - selectFromList('red,green,blue', true, 'conversation-b'), - 'green' - ) -}) - -it('prompt pick renders without configurable options', async () => { - const service = new ChatLunaPromptRenderService() - const result = await service.renderTemplate('{pick("red","green","blue")}') - - assert.equal(result.text, 'green') -}) - -it('prompt pick uses conversation id as stable seed', async () => { - const service = new ChatLunaPromptRenderService() - const result = await service.renderTemplate( - '{pick("red","green","blue")}', - {}, - { - configurable: { - conversationId: 'conversation-b' - } - } - ) - - assert.equal(result.text, 'green') -}) - -it('getMessageContent flattens structured text parts', () => { - assert.equal( - getMessageContent([ - { type: 'text', text: 'hello ' }, - { type: 'image_url', image_url: 'https://example.com/x.png' }, - { type: 'text', text: 'world' } - ] as never), - 'hello world' - ) -}) diff --git a/packages/core/tests/helpers.ts b/packages/core/tests/helpers.ts deleted file mode 100644 index 36a8d13fa..000000000 --- a/packages/core/tests/helpers.ts +++ /dev/null @@ -1,506 +0,0 @@ -import fs from 'node:fs/promises' -import os from 'node:os' -import path from 'node:path' -import { assert } from 'chai' -import memory from '@koishijs/plugin-database-memory' -import { Context } from 'koishi' -import type {} from '../src/services/types' -import { - type ArchiveRecord, - type ConversationRecord, - type MessageRecord -} from '../src/types' -import { ChatLunaService } from '../src/services/chat' -import { ConversationService } from '../src/services/conversation' -import { ModelType } from '../src/llm-core/platform/types' - -export async function expectRejected( - promise: Promise, - pattern?: RegExp -) { - try { - await promise - } catch (err) { - if (pattern != null) { - const text = - err instanceof Error - ? [ - err.message, - (err as { originError?: Error }).originError - ?.message ?? '' - ].join('\n') - : String(err) - assert.match(text, pattern) - } - return - } - - assert.fail('Expected promise to reject.') -} - -export type BindingSessionShape = { - platform?: string - selfId?: string - guildId?: string - userId?: string - channelId?: string - sid?: string - isDirect?: boolean - authority?: number -} - -export type TableRow = Record -export type Tables = Record - -type QueryOptions = { - offset?: number - limit?: number - sort?: Record | [string, 'asc' | 'desc'][] -} - -export class FakeDatabase { - tables: Tables = { - chatluna_meta: [], - chatluna_conversation: [], - chatluna_binding: [], - chatluna_archive: [], - chatluna_message: [], - chatluna_constraint: [], - chatluna_acl: [], - chathub_room_member: [], - chathub_room_group_member: [], - chathub_user: [], - chathub_room: [], - chathub_message: [], - chathub_conversation: [] - } - - extend(table: string) { - this.tables[table] ??= [] - } - - async get( - table: string, - query: Record, - modifier: QueryOptions | string[] = {} - ) { - const filters = { ...query } - const options = Array.isArray(modifier) ? {} : { ...modifier } - - if ('$offset' in filters) { - options.offset = Number(filters.$offset) - delete filters.$offset - } - - if ('$limit' in filters) { - options.limit = Number(filters.$limit) - delete filters.$limit - } - - if ('$sort' in filters) { - options.sort = filters.$sort as QueryOptions['sort'] - delete filters.$sort - } - - let rows = (this.tables[table] ?? []).filter((row) => - Object.entries(filters).every(([key, expected]) => { - const actual = row[key] - - if ( - expected != null && - typeof expected === 'object' && - '$in' in expected - ) { - return Array.isArray(expected.$in) - ? expected.$in.includes(actual) - : false - } - - if (Array.isArray(expected)) { - return expected.includes(actual) - } - - return actual === expected - }) - ) - - const sortEntries = Array.isArray(options.sort) - ? options.sort - : Object.entries(options.sort ?? {}) - const [sortKey, sortDir] = sortEntries[0] ?? [] - - if (sortKey != null && sortDir != null) { - rows = [...rows].sort((left, right) => { - const a = left[sortKey] - const b = right[sortKey] - - if (a === b) { - return 0 - } - - if (a == null) { - return sortDir === 'asc' ? -1 : 1 - } - - if (b == null) { - return sortDir === 'asc' ? 1 : -1 - } - - return (a < b ? -1 : 1) * (sortDir === 'asc' ? 1 : -1) - }) - } - - const offset = Number.isFinite(options.offset) - ? Math.max(0, Number(options.offset)) - : 0 - const limit = Number.isFinite(options.limit) - ? Math.max(0, Number(options.limit)) - : undefined - rows = - limit == null - ? rows.slice(offset) - : rows.slice(offset, offset + limit) - - if (Array.isArray(modifier)) { - return rows.map((row) => - Object.fromEntries(modifier.map((field) => [field, row[field]])) - ) - } - - return rows - } - - async create(table: string, row: TableRow) { - ;(this.tables[table] ??= []).push({ ...row }) - } - - async upsert(table: string, rows: TableRow[]) { - const target = (this.tables[table] ??= []) - - for (const row of rows) { - const idx = target.findIndex((current) => - this.samePrimary(table, current, row) - ) - if (idx >= 0) { - target[idx] = { ...target[idx], ...row } - } else { - target.push({ ...row }) - } - } - } - - async set( - table: string, - query: Record, - update: Record - ) { - const target = (this.tables[table] ??= []) - - for (let idx = 0; idx < target.length; idx += 1) { - if ( - Object.entries(query).every( - ([key, expected]) => target[idx][key] === expected - ) - ) { - target[idx] = { ...target[idx], ...update } - } - } - } - - async remove(table: string, query: Record) { - const target = (this.tables[table] ??= []) - this.tables[table] = target.filter( - (row) => - !Object.entries(query).every(([key, expected]) => { - const actual = row[key] - if (Array.isArray(expected)) { - return expected.includes(actual) - } - return actual === expected - }) - ) - } - - async drop(table: string) { - this.tables[table] = [] - } - - private samePrimary(table: string, left: TableRow, right: TableRow) { - if (table === 'chatluna_binding') { - return left.bindingKey === right.bindingKey - } - - if (table === 'chatluna_archive') { - return left.id === right.id - } - - if (table === 'chatluna_message') { - return left.id === right.id - } - - if (table === 'chatluna_constraint') { - return ( - (left.id != null && left.id === right.id) || - left.name === right.name - ) - } - - if (table === 'chatluna_meta') { - return left.key === right.key - } - - if (table === 'chatluna_acl') { - return ( - left.conversationId === right.conversationId && - left.principalType === right.principalType && - left.principalId === right.principalId && - left.permission === right.permission - ) - } - - return left.id === right.id - } -} - -export function createSession(overrides: Partial = {}) { - const authority = overrides.authority ?? 3 - - return { - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: 'channel', - userId: 'user', - sid: 'discord:channel:user', - isDirect: false, - authority, - app: { - permissions: { - test: async () => authority >= 3 - }, - logger: { - debug: () => {} - } - }, - getUser: async () => ({ - authority - }), - ...overrides - } as BindingSessionShape as never -} - -export function createConfig(overrides: Record = {}) { - return { - defaultModel: 'test-platform/test-model', - defaultPreset: 'default-preset', - defaultChatMode: 'plugin', - defaultGroupRouteMode: 'shared', - autoUpdateConversationModel: false, - enablePresetKeywordTrigger: true, - ...overrides - } as never -} - -export async function createService( - options: { - tables?: Partial - baseDir?: string - clearCache?: (conversation: ConversationRecord) => Promise - config?: Record - } = {} -) { - const database = new FakeDatabase() - const events: { name: string; args: unknown[] }[] = [] - const syncCalls: string[] = [] - - for (const [table, rows] of Object.entries(options.tables ?? {})) { - database.tables[table] = (rows ?? []).map((row) => ({ ...row })) - } - - const clearCacheCalls: string[] = [] - const clearConversation = async (conversation: ConversationRecord) => { - clearCacheCalls.push(conversation.id) - await options.clearCache?.(conversation) - return true - } - const ctx = { - database, - logger: { - info: () => {}, - error: () => {}, - warn: () => {}, - debug: () => {}, - success: () => {} - }, - baseDir: - options.baseDir ?? - (await fs.mkdtemp(path.join(os.tmpdir(), 'chatluna-core-test-'))), - root: { - parallel: async (name: string, ...args: unknown[]) => { - events.push({ name, args }) - } - }, - chatluna: { - platform: { - chatChains: { - value: [{ name: 'plugin' }] - }, - listPlatformModels: (platform: string) => ({ - value: - platform === 'test-platform' - ? [ - { - name: 'test-model', - type: ModelType.llm, - maxTokens: 4096, - capabilities: [] - } - ] - : [] - }) - }, - preset: { - getPreset: (name: string, _throwOnMissing = true) => ({ - value: { - triggerKeyword: [name], - rawText: '', - messages: [], - config: {} - } - }) - }, - conversation: { - getArchive: async (id: string) => - database.tables.chatluna_archive.find( - (item) => item.id === id - ) as ArchiveRecord | undefined - }, - conversationRuntime: { - withConversationLock: async ( - _id: string, - callback: () => Promise - ) => callback(), - withConversationSync: async ( - conversation: ConversationRecord, - callback: () => Promise - ) => { - syncCalls.push(conversation.id) - return callback() - }, - clearConversationInterfaceLocked: clearConversation, - clearConversationInterface: async ( - conversation: ConversationRecord - ) => clearConversation(conversation) - } - } - } as never - - const service = new ConversationService( - ctx, - createConfig(options.config), - ctx.chatluna.conversationRuntime, - ctx.chatluna.platform, - ctx.chatluna.preset - ) - - return { - service, - database, - ctx, - clearCacheCalls, - syncCalls, - events - } -} - -export async function createMemoryService( - options: { - tables?: Partial - config?: Record - baseDir?: string - } = {} -) { - const app = new Context() - app.baseDir = - options.baseDir ?? - (await fs.mkdtemp(path.join(os.tmpdir(), 'chatluna-core-test-'))) - app.plugin(memory) - app.plugin(ChatLunaService, createConfig(options.config)) - await app.start() - app.chatluna.platform.registerChatChain('plugin', {}, () => ({}) as never) - ;( - app.chatluna.platform as unknown as { - _models: Record - } - )._models['test-platform'] = [ - { - name: 'test-model', - type: ModelType.llm, - maxTokens: 4096, - capabilities: [] - } - ] - - for (const [table, rows] of Object.entries(options.tables ?? {})) { - for (const row of rows ?? []) { - await app.database.create(table as never, row as never) - } - } - - return { - app, - ctx: app, - database: app.database, - service: app.chatluna.conversation - } -} - -export function createConversation( - overrides: Partial = {} -): ConversationRecord { - const now = new Date('2026-03-21T00:00:00.000Z') - - return { - id: 'conversation-1', - seq: 1, - bindingKey: 'shared:discord:bot:guild', - title: 'Conversation 1', - model: 'test-platform/test-model', - preset: 'default-preset', - chatMode: 'plugin', - createdBy: 'user', - createdAt: now, - updatedAt: now, - lastChatAt: now, - status: 'active', - latestMessageId: 'message-2', - additional_kwargs: null, - compression: null, - archivedAt: null, - archiveId: null, - legacyRoomId: null, - legacyMeta: null, - ...overrides - } -} - -export function createMessage( - overrides: Partial = {} -): MessageRecord { - return { - id: 'message-1', - conversationId: 'conversation-1', - parentId: null, - role: 'human', - text: 'hello', - content: null, - name: 'user', - tool_call_id: null, - tool_calls: null, - additional_kwargs_binary: null, - response_metadata_binary: null, - rawId: null, - createdAt: new Date('2026-03-21T00:00:00.000Z'), - ...overrides - } -} diff --git a/packages/core/tests/preset-keyword-trigger.spec.ts b/packages/core/tests/preset-keyword-trigger.spec.ts deleted file mode 100644 index f01cf819e..000000000 --- a/packages/core/tests/preset-keyword-trigger.spec.ts +++ /dev/null @@ -1,47 +0,0 @@ -/// - -import { assert } from 'chai' -import { PresetService } from '../src/preset' -import { EMPTY_PRESET } from '../src/llm-core/prompt/preset_prompt_parse' -import { createConfig } from './helpers' - -function createPresetService(config: Record = {}) { - const ctx = { - scope: { isActive: true }, - on: () => {}, - schema: { set: () => {} } - } as never - return new PresetService(ctx, createConfig(config)) -} - -it('getKeywordTriggerAliases is empty when enablePresetKeywordTrigger is false', () => { - const preset = new PresetService( - { - scope: { isActive: true }, - on: () => {}, - schema: { set: () => {} } - } as never, - createConfig({ enablePresetKeywordTrigger: false }) - ) - preset.addPreset({ - ...EMPTY_PRESET, - triggerKeyword: ['catgirl', '猫娘'], - config: {} - }) - assert.deepEqual(preset.getKeywordTriggerAliases().value, []) -}) - -it('getKeywordTriggerAliases omits presets with config.enableKeywordTrigger false', () => { - const preset = createPresetService({ enablePresetKeywordTrigger: true }) - preset.addPreset({ - ...EMPTY_PRESET, - triggerKeyword: ['hidden'], - config: { enableKeywordTrigger: false } - }) - preset.addPreset({ - ...EMPTY_PRESET, - triggerKeyword: ['visible'], - config: {} - }) - assert.deepEqual(preset.getKeywordTriggerAliases().value, ['visible']) -}) \ No newline at end of file diff --git a/packages/core/tests/rollback-chat.spec.ts b/packages/core/tests/rollback-chat.spec.ts deleted file mode 100644 index 7d3d21713..000000000 --- a/packages/core/tests/rollback-chat.spec.ts +++ /dev/null @@ -1,957 +0,0 @@ -/// - -import { assert } from 'chai' -import { ChainMiddlewareRunStatus } from '../src/chains/chain' -import { apply } from '../src/middlewares/chat/rollback_chat' -import { apply as applyStop } from '../src/middlewares/chat/stop_chat' -import { - createConversation, - createMemoryService, - createMessage, - createSession, - expectRejected, - type TableRow -} from './helpers' - -it('rollback_chat keeps plain text rollback input as string and runs in sync lock', async () => { - const conversation = createConversation({ - id: 'conversation-rollback', - latestMessageId: 'message-rollback' - }) - const message = createMessage({ - id: 'message-rollback', - conversationId: conversation.id, - text: '123', - content: null - }) - const { app, ctx, database } = await createMemoryService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_message: [message as unknown as TableRow] - } - }) - - try { - const sent: string[] = [] - const syncCalls: string[] = [] - let resolveCalls = 0 - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - const withSync = - ctx.chatluna.conversationRuntime.withConversationSync.bind( - ctx.chatluna.conversationRuntime - ) - - ctx.chatluna.conversationRuntime.withConversationSync = async ( - current, - callback - ) => { - syncCalls.push(current.id) - return withSync(current, callback) - } - ctx.chatluna.conversation.resolveConversation = async () => { - resolveCalls += 1 - throw new Error('resolveConversation should not be called') - } - ctx.chatluna.messageTransformer.transform = async () => 'transformed' - session.text = (key, params) => - key === '.rollback_success' ? `${key}:${params?.[0]}` : key - session.send = async (msg) => { - sent.push(msg) - } - - apply( - ctx as never, - { - includeQuoteReply: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'rollback', - message: '', - options: { - rollback_round: 1, - conversation: { - bindingKey: conversation.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: conversation.id, - conversation, - mode: 'target' - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal(resolveCalls, 0) - assert.deepEqual(syncCalls, [conversation.id]) - assert.equal((await database.get('chatluna_message', {})).length, 0) - assert.equal( - ( - await database.get('chatluna_conversation', { - id: conversation.id - }) - )[0].latestMessageId, - null - ) - assert.deepEqual(sent, ['.rollback_success:1']) - } finally { - await app.stop() - } -}) - -it('stop_chat uses pre-resolved conversation without re-resolving', async () => { - const conversation = createConversation({ - id: 'conversation-stop-resolved' - }) - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let resolveCalls = 0 - let stoppedId: string | undefined - - ctx.chatluna.conversation.resolveConversation = async () => { - resolveCalls += 1 - throw new Error('resolveConversation should not be called') - } - ctx.chatluna.conversationRuntime.stopConversationRequest = (id) => { - stoppedId = id - return true - } - session.text = (key) => key - - applyStop( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'stop_chat', - options: { - conversation: { - bindingKey: conversation.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: conversation.id, - conversation, - mode: 'target' - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(resolveCalls, 0) - assert.equal(stoppedId, conversation.id) - } finally { - await app.stop() - } -}) - -it('stop_chat re-resolves when conversation state is partial', async () => { - const conversation = createConversation({ - id: 'conversation-stop-partial' - }) - const { app, ctx } = await createMemoryService() - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let resolveCalls = 0 - let resolveOpts: any - let stoppedId: string | undefined - - ctx.chatluna.conversation.resolveConversation = async ( - _session, - opts - ) => { - resolveCalls += 1 - resolveOpts = opts - return { - bindingKey: conversation.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: conversation.id, - conversation, - mode: 'target' - } - } - ctx.chatluna.conversationRuntime.stopConversationRequest = (id) => { - stoppedId = id - return true - } - session.text = (key) => key - - applyStop( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'stop_chat', - options: { - conversation: { - conversation - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(resolveCalls, 1) - assert.equal(resolveOpts.targetConversation, undefined) - assert.equal(resolveOpts.useRoutePresetLane, true) - assert.equal(stoppedId, conversation.id) - } finally { - await app.stop() - } -}) - -it('rollback_chat does not fall back to current conversation for an explicit missing target', async () => { - const conversation = createConversation({ - id: 'conversation-current', - latestMessageId: 'message-current' - }) - const message = createMessage({ - id: 'message-current', - conversationId: conversation.id, - text: 'hello current', - content: null - }) - const { app, database, ctx } = await createMemoryService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_binding: [ - { - bindingKey: conversation.bindingKey, - activeConversationId: conversation.id, - lastConversationId: null, - updatedAt: new Date() - } as TableRow - ], - chatluna_message: [message as unknown as TableRow] - } - }) - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - - session.text = (key) => key - session.send = async () => {} - - apply( - ctx as never, - { - includeQuoteReply: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const state = { - command: 'rollback', - message: '', - options: { - rollback_round: 1, - targetConversation: 'missing-conversation' - } - } - const status = await run!(session, state) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(state.message, '.conversation_not_exist') - assert.equal((await database.get('chatluna_message', {})).length, 1) - assert.equal( - ( - await database.get('chatluna_conversation', { - id: conversation.id - }) - )[0].latestMessageId, - 'message-current' - ) - } finally { - await app.stop() - } -}) - -it('rollback_chat resolves explicit target over pre-resolved conversation', async () => { - const current = createConversation({ - id: 'conversation-current-explicit', - latestMessageId: 'message-current-explicit' - }) - const target = createConversation({ - id: 'conversation-target-explicit', - seq: 2, - latestMessageId: 'message-target-explicit' - }) - const { app, database, ctx } = await createMemoryService({ - tables: { - chatluna_conversation: [ - current as unknown as TableRow, - target as unknown as TableRow - ], - chatluna_message: [ - createMessage({ - id: 'message-current-explicit', - conversationId: current.id, - text: 'current', - content: null - }) as unknown as TableRow, - createMessage({ - id: 'message-target-explicit', - conversationId: target.id, - text: 'target', - content: null - }) as unknown as TableRow - ] - } - }) - - try { - const session = createSession() as any - const resolve = ctx.chatluna.conversation.resolveConversation.bind( - ctx.chatluna.conversation - ) - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - const resolveOpts: unknown[] = [] - - ctx.chatluna.conversation.resolveConversation = async ( - currentSession, - opts - ) => { - resolveOpts.push(opts) - return resolve(currentSession, opts) - } - ctx.chatluna.messageTransformer.transform = async () => 'target' - session.text = (key, params) => - key === '.rollback_success' ? `${key}:${params?.[0]}` : key - session.send = async () => {} - - apply( - ctx as never, - { - includeQuoteReply: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'rollback', - message: '', - options: { - rollback_round: 1, - targetConversation: target.id, - conversation: { - bindingKey: current.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: current.id, - conversation: current, - mode: 'target' - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.equal( - resolveOpts.some( - (opts) => (opts as any).targetConversation === target.id - ), - true - ) - assert.equal( - ( - await database.get('chatluna_conversation', { - id: current.id - }) - )[0].latestMessageId, - 'message-current-explicit' - ) - assert.equal( - ( - await database.get('chatluna_conversation', { - id: target.id - }) - )[0].latestMessageId, - null - ) - } finally { - await app.stop() - } -}) - -it('rollback_chat keeps history untouched when rebuilding the input fails', async () => { - const conversation = createConversation({ - id: 'conversation-transform-failure', - latestMessageId: 'message-transform-failure' - }) - const message = createMessage({ - id: 'message-transform-failure', - conversationId: conversation.id, - text: 'fail me', - content: null - }) - const { app, database, ctx } = await createMemoryService({ - tables: { - chatluna_conversation: [conversation as unknown as TableRow], - chatluna_message: [message as unknown as TableRow] - } - }) - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let resolveCalls = 0 - - ctx.chatluna.conversation.resolveConversation = async () => { - resolveCalls += 1 - return { - bindingKey: conversation.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: conversation.id, - conversation, - mode: 'target' - } - } - ctx.chatluna.messageTransformer.transform = async () => { - throw new Error('transform failed') - } - session.text = (key) => key - session.send = async () => {} - - apply( - ctx as never, - { - includeQuoteReply: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - await expectRejected( - run!(session, { - command: 'rollback', - message: '', - options: { - rollback_round: 1, - conversation: { - conversation - } - } - }), - /transform failed/ - ) - - assert.equal(resolveCalls, 1) - assert.equal((await database.get('chatluna_message', {})).length, 1) - assert.equal( - ( - await database.get('chatluna_conversation', { - id: conversation.id - }) - )[0].latestMessageId, - 'message-transform-failure' - ) - } finally { - await app.stop() - } -}) - -it('rollback_chat falls back to the active preset lane conversation', async () => { - const main = createConversation({ - id: 'conversation-main-lane', - latestMessageId: 'message-main-lane' - }) - const helper = createConversation({ - id: 'conversation-helper-lane', - bindingKey: 'shared:discord:bot:guild:preset:helper', - latestMessageId: 'message-helper-lane' - }) - const mainMessage = createMessage({ - id: 'message-main-lane', - conversationId: main.id, - text: 'main lane', - content: null - }) - const helperMessage = createMessage({ - id: 'message-helper-lane', - conversationId: helper.id, - text: 'helper lane', - content: null - }) - const { app, ctx, database } = await createMemoryService({ - tables: { - chatluna_conversation: [ - main as unknown as TableRow, - helper as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: main.bindingKey, - activeConversationId: main.id, - lastConversationId: null, - updatedAt: new Date() - } as TableRow, - { - bindingKey: helper.bindingKey, - activeConversationId: helper.id, - lastConversationId: null, - updatedAt: new Date() - } as TableRow - ], - chatluna_message: [ - mainMessage as unknown as TableRow, - helperMessage as unknown as TableRow - ], - chatluna_constraint: [ - { - id: 1, - name: 'managed:discord:bot:guild:guild', - enabled: true, - priority: 1000, - createdBy: 'user', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - activePresetLane: 'helper', - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: null, - fixedPreset: null, - fixedChatMode: null, - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'anyone' - } as unknown as TableRow - ] - } - }) - - try { - const sent: string[] = [] - const syncCalls: string[] = [] - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - const withSync = - ctx.chatluna.conversationRuntime.withConversationSync.bind( - ctx.chatluna.conversationRuntime - ) - - ctx.chatluna.conversationRuntime.withConversationSync = async ( - current, - callback - ) => { - syncCalls.push(current.id) - return withSync(current, callback) - } - ctx.chatluna.messageTransformer.transform = async () => 'transformed' - session.text = (key, params) => - key === '.rollback_success' ? `${key}:${params?.[0]}` : key - session.send = async (msg) => { - sent.push(msg) - } - - apply( - ctx as never, - { - includeQuoteReply: false - } as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'rollback', - message: '', - options: { - rollback_round: 1 - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.CONTINUE) - assert.deepEqual(syncCalls, [helper.id]) - assert.equal( - ( - await database.get('chatluna_message', { - conversationId: helper.id - }) - ).length, - 0 - ) - assert.equal( - ( - await database.get('chatluna_message', { - conversationId: main.id - }) - ).length, - 1 - ) - assert.deepEqual(sent, ['.rollback_success:1']) - } finally { - await app.stop() - } -}) - -it('stop_chat resolves explicit target over pre-resolved conversation', async () => { - const current = createConversation({ - id: 'conversation-current-stop-explicit' - }) - const target = createConversation({ - id: 'conversation-target-stop-explicit', - seq: 2 - }) - const { app, ctx } = await createMemoryService({ - tables: { - chatluna_conversation: [ - current as unknown as TableRow, - target as unknown as TableRow - ] - } - }) - - try { - const session = createSession() as any - const resolve = ctx.chatluna.conversation.resolveConversation.bind( - ctx.chatluna.conversation - ) - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - const resolveOpts: unknown[] = [] - let stoppedId: string | undefined - - ctx.chatluna.conversation.resolveConversation = async ( - currentSession, - opts - ) => { - resolveOpts.push(opts) - return resolve(currentSession, opts) - } - ctx.chatluna.conversationRuntime.stopConversationRequest = (id) => { - stoppedId = id - return true - } - session.text = (key) => key - - applyStop( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'stop_chat', - options: { - targetConversation: target.id, - conversation: { - bindingKey: current.bindingKey, - constraint: { - manageMode: 'anyone', - lockConversation: false - }, - effectiveModel: 'test-platform/test-model', - effectivePreset: 'default-preset', - effectiveChatMode: 'plugin', - conversationId: current.id, - conversation: current, - mode: 'target' - } - } - }) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal( - resolveOpts.some( - (opts) => (opts as any).targetConversation === target.id - ), - true - ) - assert.equal(stoppedId, target.id) - } finally { - await app.stop() - } -}) - -it('stop_chat falls back to the active preset lane conversation', async () => { - const main = createConversation({ - id: 'conversation-main-stop' - }) - const helper = createConversation({ - id: 'conversation-helper-stop', - bindingKey: 'shared:discord:bot:guild:preset:helper' - }) - const { app, ctx } = await createMemoryService({ - tables: { - chatluna_conversation: [ - main as unknown as TableRow, - helper as unknown as TableRow - ], - chatluna_binding: [ - { - bindingKey: main.bindingKey, - activeConversationId: main.id, - lastConversationId: null, - updatedAt: new Date() - } as TableRow, - { - bindingKey: helper.bindingKey, - activeConversationId: helper.id, - lastConversationId: null, - updatedAt: new Date() - } as TableRow - ], - chatluna_constraint: [ - { - id: 1, - name: 'managed:discord:bot:guild:guild', - enabled: true, - priority: 1000, - createdBy: 'user', - createdAt: new Date(), - updatedAt: new Date(), - platform: 'discord', - selfId: 'bot', - guildId: 'guild', - channelId: null, - direct: false, - users: null, - excludeUsers: null, - routeMode: null, - routeKey: null, - activePresetLane: 'helper', - defaultModel: null, - defaultPreset: null, - defaultChatMode: null, - fixedModel: null, - fixedPreset: null, - fixedChatMode: null, - lockConversation: false, - allowNew: true, - allowSwitch: true, - allowArchive: true, - allowExport: true, - manageMode: 'anyone' - } as unknown as TableRow - ] - } - }) - - try { - const session = createSession() as any - let run: - | (( - session: any, - context: any - ) => Promise) - | undefined - let stoppedId: string | undefined - - ctx.chatluna.conversationRuntime.stopConversationRequest = (id) => { - stoppedId = id - return true - } - session.text = (key) => key - - applyStop( - ctx as never, - {} as never, - { - middleware: (_name, fn) => { - run = fn as never - return { - after() { - return this - }, - before() { - return this - } - } - } - } as never - ) - - const status = await run!(session, { - command: 'stop_chat', - options: {} - }) - - assert.equal(status, ChainMiddlewareRunStatus.STOP) - assert.equal(stoppedId, helper.id) - } finally { - await app.stop() - } -}) diff --git a/packages/core/tests/tsconfig.json b/packages/core/tests/tsconfig.json deleted file mode 100644 index 356ff1c23..000000000 --- a/packages/core/tests/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "extends": "../../../tsconfig.json", - "compilerOptions": { - "module": "commonjs", - "moduleResolution": "node", - "types": ["node", "mocha", "chai"] - }, - "include": ["./**/*.ts", "../src/**/*.ts"] -} diff --git a/packages/extension-agent/src/computer/backends/e2b.ts b/packages/extension-agent/src/computer/backends/e2b.ts index f4ddb9916..e5e3c01c2 100644 --- a/packages/extension-agent/src/computer/backends/e2b.ts +++ b/packages/extension-agent/src/computer/backends/e2b.ts @@ -35,6 +35,7 @@ import { TerminalHandle, TerminalOptions } from '../types' +import { replaceFileContent } from '../file_changes' interface SandboxWrapper { sandboxId: string @@ -262,13 +263,17 @@ export class E2BComputerSession implements ComputerSessionApi { const target = this.resolvePath(filePath) const sandbox = await this.ensureSandbox() if (typeof content === 'string') { + const before = (await sandbox.files.exists(target)) + ? await sandbox.files.read(target) + : '' await sandbox.files.write(target, content) - return + return { type: 'text' as const, before, after: content } } const data = new ArrayBuffer(content.byteLength) new Uint8Array(data).set(content) await sandbox.files.write(target, data) + return { type: 'binary' as const } } catch (err) { logger.error(err) throw new Error( @@ -313,48 +318,17 @@ export class E2BComputerSession implements ComputerSessionApi { newString: string, replaceCount?: number ) { - const content = await this.readFile(filePath) - if (!content.includes(oldString)) { - return { success: false, context: '', replacements: 0 } - } - - if (replaceCount === 1) { - const firstIdx = content.indexOf(oldString) - if (content.indexOf(oldString, firstIdx + 1) !== -1) { - throw new Error( - `Found multiple matches for oldString in ${filePath}. ` + - 'Provide more surrounding lines in oldString to identify the correct match, or set replaceAll to change every instance.' - ) - } - } - - let replacements = 0 - const next = content.replaceAll(oldString, (match) => { - if (replaceCount != null && replacements >= replaceCount) { - return match - } - - replacements += 1 - return newString - }) - - await this.writeFile(filePath, next) - const lines = next.split('\n') - const row = lines.findIndex((line) => line.includes(newString)) - const start = Math.max(0, row - 10) - const end = Math.min(lines.length, row + 11) + const result = replaceFileContent( + await this.readFile(filePath), + oldString, + newString, + replaceCount + ) + if (!result.success) return result + if (result.before === result.after) return result - return { - success: true, - replacements, - context: lines - .slice(start, end) - .map( - (line, idx) => - `${start + idx + 1 === row + 1 ? '>' : ' '} ${start + idx + 1}: ${line}` - ) - .join('\n') - } + await this.writeFile(filePath, result.after) + return result } async grep(pattern: string, searchPath?: string, include?: string) { @@ -396,11 +370,16 @@ export class E2BComputerSession implements ComputerSessionApi { const cwd = options.workdir ? this.resolvePath(options.workdir) : this._cwd - const result = await this.run(command, { - cwd, - timeoutMs: options.timeout, - envs: options.env - } as CommandStartOpts) + const result = await this.run( + command, + { + cwd, + timeoutMs: options.timeout, + envs: options.env + } as CommandStartOpts, + undefined, + options.signal + ) this._cwd = cwd return result } @@ -542,7 +521,7 @@ export class E2BComputerSession implements ComputerSessionApi { return undefined } - isInScope() { + async isInScope() { return true } @@ -573,7 +552,8 @@ export class E2BComputerSession implements ComputerSessionApi { private async run( command: string, options?: CommandStartOpts, - sandbox?: SandboxWrapper + sandbox?: SandboxWrapper, + signal?: AbortSignal ): Promise { const current = sandbox ?? (await this.ensureSandbox()) let handle: CommandHandle | undefined @@ -582,11 +562,36 @@ export class E2BComputerSession implements ComputerSessionApi { let timedOut = false try { + if (signal?.aborted) { + throw signal.reason ?? new Error('Aborted') + } handle = (await current.commands.run(command, { ...options, background: true })) as CommandHandle - result = await handle.wait() + + if (signal) { + let onAbort: (() => void) | undefined + const abortPromise = new Promise((_resolve, reject) => { + onAbort = () => + reject(signal.reason ?? new Error('Aborted')) + signal.addEventListener('abort', onAbort, { once: true }) + if (signal.aborted) onAbort() + }) + try { + result = await Promise.race([handle.wait(), abortPromise]) + } catch (err) { + if (signal.aborted) { + await handle.kill().catch(() => undefined) + throw signal.reason ?? err + } + throw err + } finally { + signal.removeEventListener('abort', onAbort) + } + } else { + result = await handle.wait() + } } catch (err) { if (err instanceof CommandExitError) { result = err diff --git a/packages/extension-agent/src/computer/backends/local/index.ts b/packages/extension-agent/src/computer/backends/local/index.ts index 6aa39a2a6..77275c0ae 100644 --- a/packages/extension-agent/src/computer/backends/local/index.ts +++ b/packages/extension-agent/src/computer/backends/local/index.ts @@ -19,6 +19,7 @@ import { buildPosixBackgroundCommand } from '../types' import { FileStore } from './store' import { LocalOutputCollector } from './output' import { + getPosixShellArgs, ResolvedShellCommand, resolveInteractiveShellCommand, resolveShellCommand @@ -46,15 +47,23 @@ export class LocalComputerSession implements ComputerSessionApi { private _connected = false private _cwd: string - private _store: FileStore + private _tmp = '' + private _store: FileStore | undefined + private _children = new Map, Promise>() constructor( private _cfg: LocalBackendConfig, id = randomUUID() ) { this.sessionId = id - this._cwd = _cfg.scopePath || process.cwd() - this._store = new FileStore(_cfg) + this._cwd = path.resolve(_cfg.scopePath || process.cwd()) + } + + private get store(): FileStore { + if (!this._connected || !this._store) { + throw new Error('Local computer session is disconnected') + } + return this._store } get cwd() { @@ -62,12 +71,26 @@ export class LocalComputerSession implements ComputerSessionApi { } async connect() { - await fs.mkdir(os.tmpdir(), { recursive: true }) + this._cwd = await fs.realpath(this._cwd) + this._tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'chatluna-agent-')) + await fs.chmod(this._tmp, 0o700) + this._store = new FileStore(this._cfg, this._tmp) this._connected = true } async disconnect() { this._connected = false + this._store = undefined + await Promise.allSettled( + Array.from(this._children, async ([child, settled]) => { + await killLocalChild(child) + await settled + }) + ) + if (this._tmp) { + await fs.rm(this._tmp, { recursive: true, force: true }) + this._tmp = '' + } } isConnected() { @@ -75,13 +98,11 @@ export class LocalComputerSession implements ComputerSessionApi { } async readFile(filePath: string, offset?: number, limit?: number) { - ensureLocalPathAccess(filePath, this._cfg, 'read') - return this._store.readFile(filePath, offset, limit) + return this.store.readFile(filePath, offset, limit) } async writeFile(filePath: string, content: FileContent) { - ensureLocalPathAccess(filePath, this._cfg, 'write') - await this._store.writeFile(filePath, content) + return await this.store.writeFile(filePath, content) } async editFile( @@ -90,42 +111,41 @@ export class LocalComputerSession implements ComputerSessionApi { newString: string, replaceCount?: number ) { - ensureLocalPathAccess(filePath, this._cfg, 'write') - return this._store.editFile( - filePath, - oldString, - newString, - replaceCount - ) + return this.store.editFile(filePath, oldString, newString, replaceCount) } async grep(pattern: string, searchPath?: string, include?: string) { - if (searchPath) { - ensureLocalPathAccess(searchPath, this._cfg, 'read') - } - return this._store.grep(pattern, searchPath, include) + return this.store.grep(pattern, searchPath, include) } async glob(pattern: string, searchPath?: string) { - if (searchPath) { - ensureLocalPathAccess(searchPath, this._cfg, 'read') - } - return this._store.glob(pattern, searchPath) + return this.store.glob(pattern, searchPath) } async execute(command: string, options: ExecuteOptions = {}) { + if (!this._connected) { + throw new Error('Local computer session is disconnected') + } + if (options.signal?.aborted) { + throw options.signal.reason ?? new Error('Aborted') + } + ensureCommandAllowed(command, this._cfg) - const tmp = os.tmpdir() - const workdir = options.workdir || this._cfg.scopePath || process.cwd() + const tmp = this._tmp + const workdir = await fs.realpath( + path.resolve( + options.workdir || this._cfg.scopePath || process.cwd() + ) + ) - ensureLocalCommandAccess(command, workdir, this._cfg) + await ensureLocalCommandAccess(command, workdir, this._cfg) await confirmHighRiskCommand(command, this._cfg, options.session) - await fs.mkdir(tmp, { recursive: true }) + await fs.mkdir(this._tmp, { recursive: true, mode: 0o700 }) const shell = await resolveShellCommand( - wrapCommandWithSandbox(command, workdir, this._cfg, tmp), + await wrapCommandWithSandbox(command, workdir, this._cfg, tmp), this._cfg ) @@ -133,8 +153,19 @@ export class LocalComputerSession implements ComputerSessionApi { return await runChildProcess( shell, workdir, - { ...shell.env, TMP: tmp, TEMP: tmp, TMPDIR: tmp, ...options.env }, - options.timeout ?? this._cfg.commandTimeoutMs + { + ...shell.env, + ...options.env, + PATH: options.env?.PATH ?? process.env.PATH, + TMP: this._tmp, + TEMP: this._tmp, + TMPDIR: this._tmp, + TMPPREFIX: path.join(this._tmp, 'zsh') + }, + options.timeout ?? this._cfg.commandTimeoutMs, + this._tmp, + options.signal, + this._children ) } @@ -143,18 +174,30 @@ export class LocalComputerSession implements ComputerSessionApi { marker: string, options: ExecuteOptions = {} ) { + if (!this._connected) { + throw new Error('Local computer session is disconnected') + } ensureCommandAllowed(command, this._cfg) - const tmp = os.tmpdir() - const workdir = options.workdir || this._cfg.scopePath || process.cwd() + const tmp = this._tmp + const workdir = await fs.realpath( + path.resolve( + options.workdir || this._cfg.scopePath || process.cwd() + ) + ) - ensureLocalCommandAccess(command, workdir, this._cfg) + await ensureLocalCommandAccess(command, workdir, this._cfg) await confirmHighRiskCommand(command, this._cfg, options.session) - await fs.mkdir(tmp, { recursive: true }) + await fs.mkdir(tmp, { recursive: true, mode: 0o700 }) const shell = await resolveInteractiveShellCommand(this._cfg) - const wrapped = wrapCommandWithSandbox(command, workdir, this._cfg, tmp) + const wrapped = await wrapCommandWithSandbox( + command, + workdir, + this._cfg, + tmp + ) this._cwd = path.resolve(workdir) if (process.platform !== 'win32') { @@ -188,12 +231,12 @@ export class LocalComputerSession implements ComputerSessionApi { } async readAsset(filePath: string) { - ensureLocalPathAccess(filePath, this._cfg, 'read') + await ensureLocalPathAccess(filePath, this._cfg, 'read', this._tmp) return (await fs.readFile(filePath)).toString('base64') } async openAsset(filePath: string) { - ensureLocalPathAccess(filePath, this._cfg, 'read') + await ensureLocalPathAccess(filePath, this._cfg, 'read', this._tmp) const info = await fs.stat(filePath) const mimeType = mimeTypes.lookup(filePath) return { @@ -204,30 +247,54 @@ export class LocalComputerSession implements ComputerSessionApi { } async getTempDir() { - return os.tmpdir() + return this._tmp } - isInScope(filePath: string) { - return this._store.isInScope(filePath) + async isInScope(filePath: string) { + return this.store.isInScope(filePath) } getScopePath() { - return this._store.scope + return this.store.scope } async createTerminal( options: { cwd?: string; cols?: number; rows?: number } = {} ) { - const tmp = os.tmpdir() - const cwd = options.cwd || this._cwd + const cwd = await fs.realpath(path.resolve(options.cwd || this._cwd)) + await ensureLocalCommandAccess('', cwd, this._cfg) + await fs.mkdir(this._tmp, { recursive: true, mode: 0o700 }) const shell = await resolveInteractiveShellCommand(this._cfg) - this._cwd = path.resolve(cwd) - return createLocalTerminal(shell, cwd, { - ...shell.env, - TMP: tmp, - TEMP: tmp, - TMPDIR: tmp - }) + const wrapped = + process.platform !== 'win32' && + !this._cfg.dangerouslySkipPermissions + ? await wrapCommandWithSandbox( + '', + cwd, + this._cfg, + this._tmp, + true + ) + : undefined + this._cwd = cwd + return createLocalTerminal( + wrapped + ? { + file: shell.file, + args: getPosixShellArgs(shell.file, `exec ${wrapped}`), + env: shell.env + } + : shell, + cwd, + { + ...shell.env, + PATH: process.env.PATH, + TMP: this._tmp, + TEMP: this._tmp, + TMPDIR: this._tmp, + TMPPREFIX: path.join(this._tmp, 'zsh') + } + ) } } @@ -235,10 +302,18 @@ async function runChildProcess( shell: ResolvedShellCommand, cwd: string, env: NodeJS.ProcessEnv, - timeout: number + timeout: number, + tmp: string, + signal: AbortSignal | undefined, + children: Map, Promise> ): Promise { return await new Promise((resolve, reject) => { - const output = new LocalOutputCollector('bash') + if (signal?.aborted) { + reject(signal.reason ?? new Error('Aborted')) + return + } + + const output = new LocalOutputCollector('bash', tmp) const child = spawn(shell.file, shell.args, { cwd, env, @@ -246,9 +321,26 @@ async function runChildProcess( windowsHide: true, detached: process.platform !== 'win32' }) + const closed = new Promise((resolve) => { + child.once('close', () => resolve()) + }) + let settle!: () => void + children.set( + child, + new Promise((resolve) => { + settle = resolve + }) + ) let done = false let timedOut = false + const timer = + timeout > 0 + ? setTimeout(() => { + timedOut = true + killLocalChild(child).catch(() => undefined) + }, timeout) + : undefined let hasOutput = false let hasStderr = false const stdout = { text: '', truncated: false } @@ -278,10 +370,20 @@ async function runChildProcess( if (done) return done = true - clearTimeout(timer) + if (timer) clearTimeout(timer) + signal?.removeEventListener('abort', abort) try { await Promise.all(pending) const value = await output.finish() + const hint = /operation not permitted|permission denied/i.test( + value.text + ) + ? '\nSandbox note: system temp paths are read-only. ' + + 'Use $TMPDIR for scratch files; it points to the ' + + 'session-scoped writable temporary directory.' + : '' + children.delete(child) + settle() resolve({ ...result, stdout: stdout.truncated @@ -292,28 +394,50 @@ async function runChildProcess( : stderr.text, output: { ...value, - text: value.text || '(no output)' + text: value.text + ? value.text + hint + : hint || '(no output)' } }) } catch (err) { - await output.dispose() + try { + await output.dispose() + } finally { + children.delete(child) + settle() + } reject(err) } } const fail = async (err: unknown) => { if (done) return done = true - clearTimeout(timer) - await output.dispose() + if (timer) clearTimeout(timer) + signal?.removeEventListener('abort', abort) + child.stdout.removeAllListeners('data') + child.stderr.removeAllListeners('data') + child.stdout.destroy() + child.stderr.destroy() + await closed + try { + await output.dispose() + } finally { + children.delete(child) + settle() + } reject(err) } - const timer = - timeout > 0 - ? setTimeout(() => { - timedOut = true - killLocalChild(child).catch(() => undefined) - }, timeout) - : undefined + const abort = () => { + // eslint-disable-next-line no-void + void killLocalChild(child).catch(() => undefined) + fail(signal?.reason ?? new Error('Aborted')).catch(reject) + } + + signal?.addEventListener('abort', abort, { once: true }) + if (signal?.aborted) { + abort() + return + } child.stdout.setEncoding('utf8') child.stderr.setEncoding('utf8') @@ -341,7 +465,7 @@ async function runChildProcess( child.on('close', (code, signal) => { finish({ - exitCode: timedOut ? 1 : (code ?? 0), + exitCode: timedOut ? 1 : (code ?? 1), stdout: stdout.text, stderr: stderr.text, signal: signal ?? undefined, diff --git a/packages/extension-agent/src/computer/backends/local/output.ts b/packages/extension-agent/src/computer/backends/local/output.ts index 2b2fc26d2..459f06484 100644 --- a/packages/extension-agent/src/computer/backends/local/output.ts +++ b/packages/extension-agent/src/computer/backends/local/output.ts @@ -7,6 +7,7 @@ import type { TextOutput } from '../../types' import { logger } from '../../..' const OUTPUT_FILE_PREFIX = '.tmp-chatluna-' +const SESSION_TMP_PREFIX = 'chatluna-agent-' export class LocalOutputCollector { private _text = '' @@ -27,6 +28,7 @@ export class LocalOutputCollector { constructor( private readonly _name: string, + private readonly _tmp: string, private readonly _limit = 8000 ) {} @@ -46,7 +48,7 @@ export class LocalOutputCollector { if (!this._stream) { this._path = path.join( - os.tmpdir(), + this._tmp, `${OUTPUT_FILE_PREFIX}${this._name}-${Date.now()}-${randomUUID()}.txt` ) this._stream = createWriteStream(this._path, { @@ -137,17 +139,20 @@ export async function cleanupExpiredOutputs(maxAgeMs: number) { const cutoff = Date.now() - maxAgeMs await Promise.all( entries - .filter((name) => name.startsWith(OUTPUT_FILE_PREFIX)) + .filter((name) => name.startsWith(SESSION_TMP_PREFIX)) .map(async (name) => { - const file = path.join(dir, name) + const sessionDir = path.join(dir, name) try { - const stat = await fs.stat(file) + const stat = await fs.stat(sessionDir) if (stat.mtimeMs < cutoff) { - await fs.rm(file, { force: true }) + await fs.rm(sessionDir, { + recursive: true, + force: true + }) } } catch (err) { logger.warn( - `Failed to clean up temporary output ${file}: ${err}` + `Failed to clean up session temp directory ${sessionDir}: ${err}` ) } }) diff --git a/packages/extension-agent/src/computer/backends/local/sandbox.ts b/packages/extension-agent/src/computer/backends/local/sandbox.ts index be451cf58..e5a98eb7b 100644 --- a/packages/extension-agent/src/computer/backends/local/sandbox.ts +++ b/packages/extension-agent/src/computer/backends/local/sandbox.ts @@ -1,13 +1,12 @@ /** @module computer/backends/local/sandbox */ -import { spawnSync } from 'node:child_process' -import path from 'path' -import which from 'which' +import fs from 'node:fs/promises' +import path from 'node:path' import { LocalBackendConfig } from '../../../types' -import { getPosixShell, getPosixShellArgs } from './shell' +import { buildDarwinSandbox } from './sandbox_darwin' +import { buildLinuxSandbox } from './sandbox_linux' const PROTECTED_NAMES = ['.git', '.chatluna', '.agents', '.codex', '.claude'] -const BWRAP_PROBE_CACHE = new Set() const WRITE_COMMAND_PATTERNS = [ />/, @@ -28,10 +27,11 @@ const WRITE_COMMAND_PATTERNS = [ /\bgit\s+clean\b/ ] -export function ensureLocalPathAccess( +export async function ensureLocalPathAccess( filePath: string, cfg: LocalBackendConfig, - mode: 'read' | 'write' + mode: 'read' | 'write', + tmp?: string ) { if (cfg.dangerouslySkipPermissions) { return @@ -39,8 +39,10 @@ export function ensureLocalPathAccess( const resolved = path.resolve(filePath) - if (cfg.denyRoots.some((root) => isInsideRoot(resolved, root))) { - throw new Error(`Path "${filePath}" is denied by configuration.`) + for (const root of cfg.denyRoots) { + if (await isInsideRootAsync(resolved, root)) { + throw new Error(`Path "${filePath}" is denied by configuration.`) + } } if ( @@ -59,15 +61,32 @@ export function ensureLocalPathAccess( throw new Error('Local backend is running in read-only mode.') } - if ( - mode === 'write' && - cfg.readOnlyRoots.some((root) => isInsideRoot(resolved, root)) - ) { - throw new Error(`Path "${filePath}" is read-only by configuration.`) + if (mode === 'write') { + for (const root of cfg.readOnlyRoots) { + if (await isInsideRootAsync(resolved, root)) { + throw new Error( + `Path "${filePath}" is read-only by configuration.` + ) + } + } + } + + const roots = [ + cfg.scopePath || process.cwd(), + ...(mode === 'read' ? cfg.readOnlyRoots : []), + ...(tmp ? [tmp] : []) + ] + const inside = ( + await Promise.all( + roots.map((root) => isInsideRootAsync(resolved, root)) + ) + ).some(Boolean) + if (!inside) { + throw new Error(`Path "${filePath}" is outside the local scope.`) } } -export function ensureLocalCommandAccess( +export async function ensureLocalCommandAccess( command: string, workdir: string, cfg: LocalBackendConfig @@ -92,99 +111,72 @@ export function ensureLocalCommandAccess( throw new Error('Command references a protected path.') } - if (cfg.denyRoots.some((root) => isInsideRoot(workdir, root))) { + for (const root of cfg.denyRoots) { + if (await isInsideRootAsync(workdir, root)) { + throw new Error( + `Working directory "${workdir}" is denied by configuration.` + ) + } + } + + if (!(await isInsideRootAsync(workdir, cfg.scopePath || process.cwd()))) { throw new Error( - `Working directory "${workdir}" is denied by configuration.` + `Working directory "${workdir}" is outside the local scope.` ) } } -export function wrapCommandWithSandbox( +export async function wrapCommandWithSandbox( command: string, workdir: string, cfg: LocalBackendConfig, - tmp: string + tmp: string, + interactive = false ) { - if (process.platform === 'win32' || cfg.dangerouslySkipPermissions) { - return command + if (!tmp) { + throw new Error('Local computer session is disconnected') } - const bwrap = which.sync('bwrap', { nothrow: true }) - if (!bwrap) { - throw new Error( - 'Local backend sandbox requires bubblewrap (`bwrap`), but it is not installed.' - ) + if (process.platform === 'win32' || cfg.dangerouslySkipPermissions) { + return command } - if (!BWRAP_PROBE_CACHE.has(bwrap)) { - const shell = getPosixShell() - const result = spawnSync( - bwrap, - [ - '--ro-bind', - '/', - '/', - '--bind', - tmp, - '/tmp', - '--dev', - '/dev', - '--proc', - '/proc', - shell, - ...getPosixShellArgs(shell, 'true') - ], - { encoding: 'utf8' } - ) - - if (result.status === 0) { - BWRAP_PROBE_CACHE.add(bwrap) - } else { - const err = - result.stderr?.trim() || - result.error?.message || - 'bubblewrap startup probe failed.' - throw new Error( - `Local backend sandbox requires bubblewrap, but the startup probe failed: ${err}` - ) - } + if (process.platform === 'darwin') { + return await buildDarwinSandbox(command, workdir, cfg, tmp, interactive) } - const shell = getPosixShell() - const args = [ - quote(bwrap), - '--ro-bind / /', - cfg.sandboxMode === 'workspace-write' - ? `--bind ${quote(cfg.scopePath || workdir)} ${quote(cfg.scopePath || workdir)}` - : undefined, - cfg.sandboxMode === 'workspace-write' && - path.resolve(workdir) !== path.resolve(cfg.scopePath || workdir) - ? `--bind ${quote(workdir)} ${quote(workdir)}` - : undefined, - cfg.sandboxMode === 'read-only' - ? `--ro-bind ${quote(tmp)} /tmp` - : `--bind ${quote(tmp)} /tmp`, - ...cfg.readOnlyRoots.map( - (root) => `--ro-bind ${quote(root)} ${quote(root)}` - ), - ...cfg.denyRoots.map((root) => `--tmpfs ${quote(root)}`), - '--dev /dev', - '--proc /proc', - '--die-with-parent', - ...(cfg.networkPolicy === 'block' ? ['--unshare-net'] : []), - quote(shell), - ...getPosixShellArgs(shell, command).map((arg) => quote(arg)) - ].filter((arg): arg is string => arg != null) - - return args.join(' ') + return buildLinuxSandbox(command, workdir, cfg, tmp, interactive) } -function isInsideRoot(target: string, root: string) { - const t = path.resolve(target) +export async function isInsideRootAsync(target: string, root: string) { + let t = path.resolve(target) const r = path.resolve(root) - return t === r || t.startsWith(r + path.sep) -} + try { + t = await fs.realpath(t) + } catch { + let current = t + while (current !== path.dirname(current)) { + try { + if ((await fs.lstat(current)).isSymbolicLink()) return false + } catch {} + current = path.dirname(current) + try { + t = path.join( + await fs.realpath(current), + path.relative(current, t) + ) + break + } catch {} + } + } -function quote(v: string) { - return `'${v.replaceAll("'", `'\\''`)}'` + let r2 = r + try { + r2 = await fs.realpath(r) + } catch { + try { + if ((await fs.lstat(r)).isSymbolicLink()) return false + } catch {} + } + return t === r2 || t.startsWith(r2 + path.sep) } diff --git a/packages/extension-agent/src/computer/backends/local/sandbox_darwin.ts b/packages/extension-agent/src/computer/backends/local/sandbox_darwin.ts new file mode 100644 index 000000000..41d328503 --- /dev/null +++ b/packages/extension-agent/src/computer/backends/local/sandbox_darwin.ts @@ -0,0 +1,143 @@ +/** @module computer/backends/local/sandbox_darwin */ + +import fs from 'node:fs/promises' +import path from 'node:path' +import which from 'which' +import { LocalBackendConfig } from '../../../types' +import { getPosixShell, getPosixShellArgs } from './shell' + +function quote(v: string) { + return `'${v.replaceAll("'", `'\\''`)}'` +} + +async function resolveReal(item: string) { + const value = path.resolve(item) + return await fs.realpath(value).catch(() => value) +} + +export async function buildDarwinSandbox( + command: string, + workdir: string, + cfg: LocalBackendConfig, + tmp: string, + interactive = false +) { + const sandboxExec = which.sync('sandbox-exec', { nothrow: true }) + if (!sandboxExec) { + throw new Error( + 'Local backend sandbox requires macOS sandbox-exec, but it is not available.' + ) + } + + const shell = getPosixShell() + const shellSelector = '/private/var/select' + const scope = await resolveReal(cfg.scopePath || workdir) + const readOnlyRoots = await Promise.all( + cfg.readOnlyRoots.map((item) => resolveReal(item)) + ) + const tmpRoot = await resolveReal(tmp) + const runtime = await Promise.all( + [shell, process.execPath].map((item) => resolveReal(item)) + ) + const runtimeRoots = Array.from( + new Set( + runtime.map((item) => { + const root = path.dirname(path.dirname(item)) + return root === path.parse(root).root + ? path.dirname(item) + : root + }) + ) + ) + const moduleRoots = new Set() + for (const start of [scope, process.cwd()]) { + let current = path.resolve(start) + while (true) { + const dir = await fs + .realpath(path.join(current, 'node_modules')) + .catch(() => undefined) + if (dir) moduleRoots.add(dir) + if (current === path.dirname(current)) break + current = path.dirname(current) + } + } + const readRoots = [ + scope, + ...readOnlyRoots, + tmpRoot, + ...runtimeRoots, + ...Array.from(moduleRoots), + shellSelector + ] + const readParams: [string, string][] = [ + ['SCOPE', scope], + ...readOnlyRoots.map( + (item, idx) => [`READ_ONLY_${idx}`, item] as [string, string] + ), + ['TMP', tmpRoot], + ...runtimeRoots.map( + (item, idx) => [`RUNTIME_${idx}`, item] as [string, string] + ), + ...Array.from(moduleRoots).map( + (item, idx) => [`MODULES_${idx}`, item] as [string, string] + ), + ['SHELL_SELECTOR', shellSelector] + ] + const parents = new Set() + for (const item of readRoots) { + let current = path.dirname(item) + while (current !== path.dirname(current)) { + parents.add(current) + current = path.dirname(current) + } + } + const denied = await Promise.all( + cfg.denyRoots.map((item) => resolveReal(item)) + ) + const params: [string, string][] = [ + ...readParams, + ...Array.from(parents).map( + (item, idx) => [`PARENT_${idx}`, item] as [string, string] + ), + ...denied.map((item, idx) => [`DENY_${idx}`, item] as [string, string]) + ] + const profile = [ + '(version 1)', + '(deny default)', + '(import "system.sb")', + '(allow process*)', + '(allow file-read* file-test-existence ' + + '(subpath "/bin") (subpath "/sbin") ' + + '(subpath "/usr/bin") (subpath "/usr/sbin") ' + + '(subpath "/usr/libexec") (subpath "/private/etc"))', + `(allow file-read* file-test-existence ${readParams.map(([key]) => `(subpath (param "${key}"))`).join(' ')})`, + `(allow file-map-executable ${readParams.map(([key]) => `(subpath (param "${key}"))`).join(' ')})`, + `(allow file-read-metadata file-test-existence ${Array.from(parents) + .map((_, idx) => `(literal (param "PARENT_${idx}"))`) + .join(' ')})`, + ...(cfg.sandboxMode === 'workspace-write' + ? [ + '(allow file-write* (subpath (param "SCOPE")) (subpath (param "TMP")))' + ] + : []), + ...readOnlyRoots.map( + (_, idx) => + `(deny file-write* (subpath (param "READ_ONLY_${idx}")))` + ), + ...denied.map( + (_, idx) => + `(deny file-read* file-write* (subpath (param "DENY_${idx}")))` + ), + cfg.networkPolicy === 'block' ? '(deny network*)' : '(allow network*)' + ].join(' ') + return [ + quote(sandboxExec), + ...params.flatMap(([key, value]) => ['-D', quote(`${key}=${value}`)]), + '-p', + quote(profile), + quote(shell), + ...getPosixShellArgs(shell, interactive ? undefined : command).map( + (arg) => quote(arg) + ) + ].join(' ') +} diff --git a/packages/extension-agent/src/computer/backends/local/sandbox_linux.ts b/packages/extension-agent/src/computer/backends/local/sandbox_linux.ts new file mode 100644 index 000000000..39f2028df --- /dev/null +++ b/packages/extension-agent/src/computer/backends/local/sandbox_linux.ts @@ -0,0 +1,99 @@ +/** @module computer/backends/local/sandbox_linux */ + +import { spawnSync } from 'node:child_process' +import path from 'node:path' +import which from 'which' +import { LocalBackendConfig } from '../../../types' +import { getPosixShell, getPosixShellArgs } from './shell' + +const BWRAP_PROBE_CACHE = new Set() + +function quote(v: string) { + return `'${v.replaceAll("'", `'\\''`)}'` +} + +export function buildLinuxSandbox( + command: string, + workdir: string, + cfg: LocalBackendConfig, + tmp: string, + interactive = false +) { + const bwrap = which.sync('bwrap', { nothrow: true }) + if (!bwrap) { + throw new Error( + 'Local backend sandbox requires bubblewrap (`bwrap`), but it is not installed.' + ) + } + + if (!BWRAP_PROBE_CACHE.has(bwrap)) { + const shell = getPosixShell() + const result = spawnSync( + bwrap, + [ + '--ro-bind', + '/', + '/', + '--bind', + tmp, + '/tmp', + '--dev', + '/dev', + '--proc', + '/proc', + shell, + ...getPosixShellArgs(shell, 'true') + ], + { encoding: 'utf8' } + ) + + if (result.status === 0) { + BWRAP_PROBE_CACHE.add(bwrap) + } else { + const err = + result.stderr?.trim() || + result.error?.message || + 'bubblewrap startup probe failed.' + throw new Error( + `Local backend sandbox requires bubblewrap, but the startup probe failed: ${err}` + ) + } + } + + const shell = getPosixShell() + const args = [ + quote(bwrap), + '--ro-bind / /', + cfg.sandboxMode === 'workspace-write' + ? `--bind ${quote(cfg.scopePath || workdir)} ${quote(cfg.scopePath || workdir)}` + : undefined, + cfg.sandboxMode === 'workspace-write' && + path.resolve(workdir) !== path.resolve(cfg.scopePath || workdir) + ? `--bind ${quote(workdir)} ${quote(workdir)}` + : undefined, + cfg.sandboxMode === 'read-only' + ? `--ro-bind ${quote(tmp)} /tmp` + : `--bind ${quote(tmp)} /tmp`, + '--setenv TMP /tmp', + '--setenv TEMP /tmp', + '--setenv TMPDIR /tmp', + '--setenv TMPPREFIX /tmp/zsh', + ...cfg.readOnlyRoots.map( + (root) => `--ro-bind ${quote(root)} ${quote(root)}` + ), + ...cfg.denyRoots.map((root) => `--tmpfs ${quote(root)}`), + '--dev /dev', + '--proc /proc', + '--unshare-pid', + '--unshare-ipc', + '--new-session', + '--die-with-parent', + ...(cfg.networkPolicy === 'block' ? ['--unshare-net'] : []), + quote(shell), + ...getPosixShellArgs(shell, interactive ? undefined : command).map( + (arg) => quote(arg) + ) + ].filter((arg): arg is string => arg != null) + + return args.join(' ') +} diff --git a/packages/extension-agent/src/computer/backends/local/shell.ts b/packages/extension-agent/src/computer/backends/local/shell.ts index f1745eb40..675378779 100644 --- a/packages/extension-agent/src/computer/backends/local/shell.ts +++ b/packages/extension-agent/src/computer/backends/local/shell.ts @@ -58,11 +58,11 @@ export function getPosixShellArgs(shell: string, command?: string) { } if (name.includes('fish')) { - return ['--interactive', '--command', command] + return ['--command', command] } if (name.includes('bash') || name.includes('zsh')) { - return ['-ic', command] + return ['-c', command] } return ['-c', command] @@ -139,7 +139,8 @@ export async function resolveShellCommand( const shell = getPosixShell() return { file: shell, - args: getPosixShellArgs(shell, command) + args: getPosixShellArgs(shell, command), + env: process.env } } diff --git a/packages/extension-agent/src/computer/backends/local/store.ts b/packages/extension-agent/src/computer/backends/local/store.ts index 2f394f8ea..7a239b85f 100644 --- a/packages/extension-agent/src/computer/backends/local/store.ts +++ b/packages/extension-agent/src/computer/backends/local/store.ts @@ -1,7 +1,7 @@ /** @module computer/backends/local/store */ import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process' -import { createReadStream } from 'node:fs' +import { createReadStream, realpathSync } from 'node:fs' import fs from 'fs/promises' import path from 'path' import { createInterface } from 'node:readline' @@ -9,22 +9,38 @@ import type { Readable } from 'node:stream' import micromatch from 'micromatch' import which from 'which' import { LocalBackendConfig } from '../../../types' -import type { FileContent, TextOutput } from '../../types' +import type { + EditResult, + FileContent, + TextOutput, + WriteResult +} from '../../types' +import { replaceFileContent } from '../../file_changes' import { LocalOutputCollector } from './output' import { logger } from '../../..' +import { ensureLocalPathAccess, isInsideRootAsync } from './sandbox' const rg = which.sync('rg', { nothrow: true }) const INTERNAL_IGNORES = ['**/.tmp-chatluna-*'] +function resolveReal(value: string) { + const resolved = path.resolve(value) + try { + return realpathSync.native(resolved) + } catch { + return resolved + } +} + export interface BaseFileStore { readFile(filePath: string, offset?: number, limit?: number): Promise - writeFile(writePath: string, contents: FileContent): Promise + writeFile(writePath: string, contents: FileContent): Promise editFile( filePath: string, oldString: string, newString: string, replaceCount?: number - ): Promise<{ success: boolean; context: string; replacements: number }> + ): Promise grep( pattern: string, searchPath?: string, @@ -32,34 +48,63 @@ export interface BaseFileStore { ): Promise glob(pattern: string, searchPath?: string): Promise readonly scope: string - isInScope(filePath: string): boolean + isInScope(filePath: string): Promise } export class FileStore implements BaseFileStore { - constructor(private _cfg: LocalBackendConfig) {} + readonly scope: string + private _allow: string[] + private _deny: string[] - get scope() { - return this._cfg.scopePath + constructor( + private _cfg: LocalBackendConfig, + private _tmp: string + ) { + const value = path.resolve(this._cfg.scopePath || process.cwd()) + try { + this.scope = realpathSync.native(value) + } catch { + this.scope = value + } + this._allow = [this.scope, ...this._cfg.readOnlyRoots, this._tmp].map( + (item) => resolveReal(item) + ) + this._deny = this._cfg.denyRoots.map((item) => resolveReal(item)) } - isInScope(filePath: string) { - return true + async isInScope(filePath: string) { + if (this._cfg.dangerouslySkipPermissions) return true + return isInsideRootAsync(filePath, this.scope) } async readFile(filePath: string, offset?: number, limit?: number) { + await ensureLocalPathAccess(filePath, this._cfg, 'read', this._tmp) const stat = await fs.stat(filePath) if (stat.isDirectory()) { - return (await fs.readdir(filePath, { withFileTypes: true })) - .filter( - (entry) => - !this._shouldIgnore(path.join(filePath, entry.name)) - ) - .map((entry) => - entry.isDirectory() - ? `${path.join(filePath, entry.name)}/` - : path.join(filePath, entry.name) - ) - .join('\n') + const result: string[] = [] + for (const entry of await fs.readdir(filePath, { + withFileTypes: true + })) { + const item = path.join(filePath, entry.name) + if (this._shouldIgnore(item)) continue + if (!this._isAllowed(item)) continue + + if (entry.isDirectory()) { + result.push(`${item}/`) + continue + } + + if (entry.isFile()) { + result.push(item) + continue + } + + if (!entry.isSymbolicLink()) continue + try { + if ((await fs.stat(item)).isFile()) result.push(item) + } catch {} + } + return result.join('\n') } const lines = (await fs.readFile(filePath, 'utf-8')).split('\n') @@ -80,9 +125,31 @@ export class FileStore implements BaseFileStore { return `${result}\n\n(Showing lines ${start + 1}-${end} of ${lines.length}. Use offset=${end + 1} to continue.)` } - async writeFile(writePath: string, contents: FileContent) { + async writeFile( + writePath: string, + contents: FileContent + ): Promise { + await ensureLocalPathAccess(writePath, this._cfg, 'write', this._tmp) + if (typeof contents === 'string') { + const before = await fs + .readFile(writePath, 'utf-8') + .catch((err) => { + if ((err as NodeJS.ErrnoException).code === 'ENOENT') + return '' + throw err + }) + if (before === contents) { + return { type: 'text', before, after: contents } + } + + await fs.mkdir(path.dirname(writePath), { recursive: true }) + await fs.writeFile(writePath, contents) + return { type: 'text', before, after: contents } + } + await fs.mkdir(path.dirname(writePath), { recursive: true }) await fs.writeFile(writePath, contents) + return { type: 'binary' } } async editFile( @@ -91,28 +158,24 @@ export class FileStore implements BaseFileStore { newString: string, replaceCount?: number ) { - const next = replaceSubstring( + await ensureLocalPathAccess(filePath, this._cfg, 'write', this._tmp) + const result = replaceFileContent( await fs.readFile(filePath, 'utf-8'), oldString, newString, replaceCount ) - if (next.count < 1) { - return { success: false, context: '', replacements: 0 } - } - - await fs.writeFile(filePath, next.result) + if (!result.success) return result + if (result.before === result.after) return result - return { - success: true, - context: buildEditContext(next.result, oldString, newString), - replacements: next.count - } + await fs.writeFile(filePath, result.after) + return result } async grep(pattern: string, searchPath?: string, include?: string) { - const dir = searchPath || this._cfg.scopePath || process.cwd() + const dir = searchPath || this.scope + await ensureLocalPathAccess(dir, this._cfg, 'read', this._tmp) const stat = await fs.stat(dir).catch(() => null) if (!stat) { @@ -125,7 +188,6 @@ export class FileStore implements BaseFileStore { '-n', '--hidden', '--no-ignore', - '--follow', '--color', 'never', '--engine', @@ -142,7 +204,7 @@ export class FileStore implements BaseFileStore { args.push('-e', pattern, '.') - const output = new LocalOutputCollector('grep') + const output = new LocalOutputCollector('grep', this._tmp) try { const found = await this._runRg(args, dir, async (stdout) => { for await (const line of createInterface({ @@ -159,6 +221,7 @@ export class FileStore implements BaseFileStore { if (this._shouldIgnore(file)) continue if (include && !this._matchPattern(file, include)) continue + if (!this._isAllowed(file)) continue const text = ( (item.data.lines.text as string | undefined) || '' @@ -213,7 +276,8 @@ export class FileStore implements BaseFileStore { } async glob(pattern: string, searchPath?: string) { - const dir = searchPath || this._cfg.scopePath || process.cwd() + const dir = searchPath || this.scope + await ensureLocalPathAccess(dir, this._cfg, 'read', this._tmp) const stat = await fs.stat(dir).catch(() => null) if (!stat) { @@ -225,13 +289,7 @@ export class FileStore implements BaseFileStore { } if (rg) { - const args = [ - '--files', - '--hidden', - '--no-ignore', - '--follow', - '-0' - ] + const args = ['--files', '--hidden', '--no-ignore', '-0'] args.push('-g', pattern) for (const item of [...this._cfg.ignores, ...INTERNAL_IGNORES]) { @@ -240,7 +298,7 @@ export class FileStore implements BaseFileStore { args.push('.') - const output = new LocalOutputCollector('glob') + const output = new LocalOutputCollector('glob', this._tmp) try { const found = await this._runRg(args, dir, async (stdout) => { let rest = '' @@ -251,6 +309,7 @@ export class FileStore implements BaseFileStore { const file = path.resolve(dir, raw) if (this._shouldIgnore(file)) continue if (!this._matchPattern(file, pattern)) continue + if (!this._isAllowed(file)) continue await output.appendLine(file) } } @@ -351,7 +410,7 @@ export class FileStore implements BaseFileStore { files: AsyncIterable | Iterable, collect: (output: LocalOutputCollector, file: string) => Promise ): Promise { - const output = new LocalOutputCollector(name) + const output = new LocalOutputCollector(name, this._tmp) try { for await (const file of files) { if (this._shouldIgnore(file)) continue @@ -390,12 +449,9 @@ export class FileStore implements BaseFileStore { try { const stat = await fs.stat(fullPath) - if (stat.isDirectory()) { - yield* this._walk(fullPath, seen) - continue - } - - if (stat.isFile()) yield fullPath + if (!stat.isFile()) continue + if (!this._isAllowed(fullPath)) continue + yield fullPath } catch (err) { if (process.env['CHATLUNA_AGENT_DEBUG']) { console.debug(err) @@ -409,10 +465,22 @@ export class FileStore implements BaseFileStore { } } + private _isAllowed(filePath: string) { + const resolved = path.resolve(filePath) + return ( + this._allow.some( + (root) => + resolved === root || resolved.startsWith(root + path.sep) + ) && + !this._deny.some( + (root) => + resolved === root || resolved.startsWith(root + path.sep) + ) + ) + } + private _matchPattern(filePath: string, pattern: string) { - const rel = path - .relative(this._cfg.scopePath || process.cwd(), filePath) - .replaceAll('\\', '/') + const rel = path.relative(this.scope, filePath).replaceAll('\\', '/') return micromatch.some( [rel, filePath.replaceAll('\\', '/'), path.basename(filePath)], pattern, @@ -422,68 +490,9 @@ export class FileStore implements BaseFileStore { private _shouldIgnore(filePath: string) { return micromatch.isMatch( - path - .relative(this._cfg.scopePath || process.cwd(), filePath) - .replace(/\\/g, '/'), + path.relative(this.scope, filePath).replace(/\\/g, '/'), [...this._cfg.ignores, ...INTERNAL_IGNORES], { dot: true } ) } } - -function replaceSubstring( - content: string, - oldString: string, - newString: string, - replaceCount?: number -) { - if (!content.includes(oldString)) { - return { result: content, count: 0 } - } - - if (replaceCount === 1) { - if ( - content.indexOf( - oldString, - content.indexOf(oldString) + oldString.length - ) !== -1 - ) { - throw new Error( - 'Found multiple matches for oldString. Provide more surrounding ' + - 'lines in oldString to identify the correct match, or set ' + - 'replaceAll to change every instance.' - ) - } - } - - let count = 0 - const result = content.replaceAll(oldString, (item) => { - if (replaceCount != null && count >= replaceCount) { - return item - } - - count += 1 - return newString - }) - - return { result, count } -} - -function buildEditContext( - content: string, - oldString: string, - newString: string -): string { - const lines = content.split('\n') - const row = lines.findIndex((line) => line.includes(newString || oldString)) - const start = Math.max(0, row - 10) - const end = Math.min(lines.length, row + 11) - - return lines - .slice(start, end) - .map( - (line, idx) => - `${start + idx + 1 === row + 1 ? '>' : ' '} ${start + idx + 1}: ${line}` - ) - .join('\n') -} diff --git a/packages/extension-agent/src/computer/backends/open_terminal.ts b/packages/extension-agent/src/computer/backends/open_terminal.ts index f2cafb2d8..256e5ae06 100644 --- a/packages/extension-agent/src/computer/backends/open_terminal.ts +++ b/packages/extension-agent/src/computer/backends/open_terminal.ts @@ -19,6 +19,7 @@ import { TerminalHandle, TerminalOptions } from '../types' +import { replaceFileContent } from '../file_changes' export class OpenTerminalComputerSession implements ComputerSessionApi { readonly backend = 'open-terminal' as const @@ -222,13 +223,32 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { clearTimeout(timer) } - return + return { type: 'binary' as const } } + const target = this.resolvePath(filePath) + const current = await this.ctx.http(this.url('/files/view'), { + method: 'GET', + proxyAgent: '', + headers: this.headers(), + params: { path: target }, + responseType: 'arraybuffer', + validateStatus: () => true + }) + if ( + current.status !== 404 && + (current.status < 200 || current.status >= 300) + ) { + throw new Error(`Failed to read before write: ${current.status}`) + } + const before = + current.status === 404 + ? '' + : Buffer.from(current.data).toString('utf-8') await this.ctx.http.post( this.url('/files/write'), { - path: this.resolvePath(filePath), + path: target, content }, { @@ -239,6 +259,7 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { } } ) + return { type: 'text' as const, before, after: content } } async hashFiles(paths: string[]) { @@ -285,29 +306,14 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { newString: string, replaceCount?: number ) { - const content = await this.readFile(filePath) - if (!content.includes(oldString)) { - return { success: false, context: '', replacements: 0 } - } - - if (replaceCount === 1) { - const firstIdx = content.indexOf(oldString) - if (content.indexOf(oldString, firstIdx + 1) !== -1) { - throw new Error( - `Found multiple matches for oldString in ${filePath}. ` + - 'Provide more surrounding lines in oldString to identify the correct match, or set replaceAll to change every instance.' - ) - } - } - - let replacements = 0 - const next = content.replaceAll(oldString, (match) => { - if (replaceCount != null && replacements >= replaceCount) { - return match - } - replacements += 1 - return newString - }) + const result = replaceFileContent( + await this.readFile(filePath), + oldString, + newString, + replaceCount + ) + if (!result.success) return result + if (result.before === result.after) return result await this.ctx.http.post( this.url('/files/replace'), @@ -330,21 +336,7 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { } ) - const lines = next.split('\n') - const row = lines.findIndex((line) => line.includes(newString)) - const start = Math.max(0, row - 10) - - return { - success: true, - replacements, - context: lines - .slice(start, Math.min(lines.length, row + 11)) - .map( - (line, idx) => - `${start + idx + 1 === row + 1 ? '>' : ' '} ${start + idx + 1}: ${line}` - ) - .join('\n') - } + return result } async grep(pattern: string, searchPath?: string, include?: string) { @@ -414,6 +406,9 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { } async execute(command: string, options: ExecuteOptions = {}) { + if (options.signal?.aborted) { + throw options.signal.reason ?? new Error('Aborted') + } const cwd = this.resolvePath(options.workdir || this._cwd) const timeout = options.timeout ?? 120000 const result: ExecuteResult = { @@ -423,29 +418,44 @@ export class OpenTerminalComputerSession implements ComputerSessionApi { timedOut: false } const start = Date.now() - let data = await this.postExecute(command, cwd, options, timeout) + let data = await this.postExecute( + command, + cwd, + options, + timeout, + options.signal + ) let offset = collectOpenTerminalOutput(data, result) let left = timeout <= 0 ? 300000 : timeout - (Date.now() - start) while (data.id && data.status === 'running' && left > 0) { - data = await this.getExecuteStatus(data.id, offset, left) + if (options.signal?.aborted) { + await this.forceDeleteExecute(data.id).catch(() => undefined) + throw options.signal.reason ?? new Error('Aborted') + } + try { + data = await this.getExecuteStatus( + data.id, + offset, + left, + options.signal + ) + } catch (e) { + if (options.signal?.aborted) { + await this.forceDeleteExecute(data.id).catch( + () => undefined + ) + throw options.signal.reason ?? e + } + throw e + } offset = collectOpenTerminalOutput(data, result) left = timeout <= 0 ? 300000 : timeout - (Date.now() - start) } if (data.id && data.status === 'running') { result.timedOut = true - await this.ctx.http - .delete( - this.url( - `/execute/${encodeURIComponent(data.id)}?force=true` - ), - { - proxyAgent: '', - headers: this.headers() - } - ) - .catch(() => undefined) + await this.forceDeleteExecute(data.id).catch(() => undefined) } result.exitCode = data.exit_code ?? (result.timedOut ? 1 : 0) @@ -591,7 +601,7 @@ exit return undefined } - isInScope() { + async isInScope() { return true } @@ -603,7 +613,8 @@ exit command: string, cwd: string, options: ExecuteOptions, - timeout: number + timeout: number, + signal?: AbortSignal ) { const query = new URLSearchParams({ wait: String(Math.min(Math.max(timeout, 0), 300000) / 1000) @@ -618,6 +629,7 @@ exit }, { proxyAgent: '', + signal, headers: { ...this.headers(), 'content-type': 'application/json' @@ -630,7 +642,8 @@ exit private async getExecuteStatus( id: string, offset: number, - timeout: number + timeout: number, + signal?: AbortSignal ) { const query = new URLSearchParams({ wait: String(Math.min(Math.max(timeout, 0), 300000) / 1000), @@ -644,12 +657,23 @@ exit { method: 'GET', proxyAgent: '', + signal, headers: this.headers() } ) ) } + private async forceDeleteExecute(id: string) { + await this.ctx.http.delete( + this.url(`/execute/${encodeURIComponent(id)}?force=true`), + { + proxyAgent: '', + headers: this.headers() + } + ) + } + private resolvePath(value: string) { if (value === '~') { return this._home diff --git a/packages/extension-agent/src/computer/file_changes.ts b/packages/extension-agent/src/computer/file_changes.ts new file mode 100644 index 000000000..82c2a9022 --- /dev/null +++ b/packages/extension-agent/src/computer/file_changes.ts @@ -0,0 +1,113 @@ +import type { EditResult } from './types' + +const MAX_DIFF_LINES = 40 +const MAX_DIFF_CHARS = 2000 +const MAX_DIFF_LINE_CHARS = 900 + +export function replaceFileContent( + content: string, + oldString: string, + newString: string, + count?: number +): EditResult { + if (!oldString) { + throw new Error('oldString must not be empty') + } + + if (!content.includes(oldString)) { + return { success: false, replacements: 0 } + } + + if ( + count === 1 && + content.indexOf( + oldString, + content.indexOf(oldString) + oldString.length + ) !== -1 + ) { + throw new Error( + 'Found multiple matches for oldString. Provide more surrounding ' + + 'lines in oldString to identify the correct match, or set ' + + 'replaceAll to change every instance.' + ) + } + + let replacements = 0 + const after = content.replaceAll(oldString, (value) => { + if (count != null && replacements >= count) return value + replacements += 1 + return newString + }) + + return { + success: true, + before: content, + after, + replacements + } +} + +export function formatFileDiff(before: string, after: string) { + if (before === after) return 'No changes.' + + const oldLines = before.split('\n') + const newLines = after.split('\n') + let start = 0 + while ( + start < oldLines.length && + start < newLines.length && + oldLines[start] === newLines[start] + ) { + start += 1 + } + + let oldEnd = oldLines.length + let newEnd = newLines.length + while ( + oldEnd > start && + newEnd > start && + oldLines[oldEnd - 1] === newLines[newEnd - 1] + ) { + oldEnd -= 1 + newEnd -= 1 + } + + const removed = oldLines.slice(start, oldEnd) + const added = newLines.slice(start, newEnd) + const oldStart = removed.length > 0 ? start + 1 : start + const newStart = added.length > 0 ? start + 1 : start + const changes: string[] = [] + for (let idx = 0; idx < Math.max(removed.length, added.length); idx++) { + if (idx < removed.length) changes.push(`-${removed[idx]}`) + if (idx < added.length) changes.push(`+${added[idx]}`) + } + + const context = [ + ...oldLines.slice(Math.max(0, start - 2), start), + ...oldLines.slice(oldEnd, Math.min(oldLines.length, oldEnd + 2)) + ].map((line) => ` ${line}`) + const lines = [ + `@@ -${oldStart},${removed.length} +${newStart},${added.length} @@` + ] + const marker = '[diff truncated]' + let chars = lines[0].length + let truncated = false + + for (const value of [...changes, ...context]) { + const line = value.slice(0, MAX_DIFF_LINE_CHARS) + if ( + lines.length >= MAX_DIFF_LINES - 1 || + chars + line.length + 1 > MAX_DIFF_CHARS - marker.length - 1 + ) { + truncated = true + break + } + + if (line.length < value.length) truncated = true + lines.push(line) + chars += line.length + 1 + } + + if (truncated) lines.push(marker) + return lines.join('\n') +} diff --git a/packages/extension-agent/src/computer/tools/bash.ts b/packages/extension-agent/src/computer/tools/bash.ts index a1bf5207b..78e94c6bd 100644 --- a/packages/extension-agent/src/computer/tools/bash.ts +++ b/packages/extension-agent/src/computer/tools/bash.ts @@ -191,8 +191,10 @@ When to use: const result = await computer.execute(command, { workdir: input.workdir, timeout, - session: toolConfig?.configurable?.session + session: toolConfig?.configurable?.session, + signal: toolConfig.signal }) + const status = `Exit code: ${result.exitCode}` if (result.timedOut) { const output = await this.formatLargeResult( @@ -202,7 +204,7 @@ When to use: ) return this.withBackend( computer, - `Command timed out after ${timeout}ms.\n${output}` + `${status}\nCommand timed out after ${timeout}ms.\n${output}` ) } @@ -215,21 +217,22 @@ When to use: if (result.signal) { return this.withBackend( computer, - `Command terminated by signal ${result.signal}:\n${output}` + `${status}\nCommand terminated by signal ${result.signal}:\n${output}` ) } if (result.exitCode !== 0) { - return this.withBackend( - computer, - `Command exited with code ${result.exitCode}:\n${output}` - ) + return this.withBackend(computer, `${status}\n${output}`) } this.log(computer, output) return this.withBackend(computer, output) } catch (err) { + if (toolConfig.signal?.aborted) { + throw toolConfig.signal.reason ?? err + } + return this.formatResult( false, `Command execution failed: ${getErrorMessage(err)}` diff --git a/packages/extension-agent/src/computer/tools/file_edit.ts b/packages/extension-agent/src/computer/tools/file_edit.ts index d61c94f57..738ac7c20 100644 --- a/packages/extension-agent/src/computer/tools/file_edit.ts +++ b/packages/extension-agent/src/computer/tools/file_edit.ts @@ -3,6 +3,7 @@ import type { ChatLunaToolRunnable } from 'koishi-plugin-chatluna/llm-core/platform/types' import z from 'zod' import { getErrorMessage } from '../../utils/shell' +import { formatFileDiff } from '../file_changes' import { ComputerToolBase } from './base' export class EditFileTool extends ComputerToolBase { @@ -59,7 +60,7 @@ Usage: ) return this.withBackend( computer, - `Replaced ${result.replacements} occurrence(s) in ${input.filePath}\n\nContext (> marks modified lines):\n${result.context}` + `Diff:\n${formatFileDiff(result.before, result.after)}\n\nReplaced ${result.replacements} occurrence(s) in ${input.filePath}` ) } catch (err) { return this.formatResult( diff --git a/packages/extension-agent/src/computer/tools/file_write.ts b/packages/extension-agent/src/computer/tools/file_write.ts index 3cb267522..b0847c80c 100644 --- a/packages/extension-agent/src/computer/tools/file_write.ts +++ b/packages/extension-agent/src/computer/tools/file_write.ts @@ -3,6 +3,7 @@ import type { ChatLunaToolRunnable } from 'koishi-plugin-chatluna/llm-core/platform/types' import z from 'zod' import { getErrorMessage } from '../../utils/shell' +import { formatFileDiff } from '../file_changes' import { ComputerToolBase } from './base' export class WriteFileTool extends ComputerToolBase { @@ -34,11 +35,18 @@ Usage: this.log(computer, `写入文件: ${input.filePath}`) try { - await computer.writeFile(input.filePath, input.content) + const result = await computer.writeFile( + input.filePath, + input.content + ) this.log(computer, `完成写入: ${input.filePath}`) return this.withBackend( computer, - this.formatResult(true, `Wrote ${input.filePath}`) + result.type === 'text' + ? result.before === result.after + ? `No changes. ${input.filePath} already has the requested content.` + : `Diff:\n${formatFileDiff(result.before, result.after)}\n\nWrote ${input.filePath}` + : `Wrote ${input.filePath}` ) } catch (err) { return this.formatResult( diff --git a/packages/extension-agent/src/computer/types.ts b/packages/extension-agent/src/computer/types.ts index 494b9f0b1..23f7081dd 100644 --- a/packages/extension-agent/src/computer/types.ts +++ b/packages/extension-agent/src/computer/types.ts @@ -22,7 +22,7 @@ export interface ComputerSessionApi { isConnected(): boolean readFile(path: string, offset?: number, limit?: number): Promise - writeFile(path: string, content: FileContent): Promise + writeFile(path: string, content: FileContent): Promise hashFiles?(paths: string[]): Promise> editFile( path: string, @@ -52,7 +52,7 @@ export interface ComputerSessionApi { desktopAction?(action: DesktopAction): Promise getDesktopStream?(): Promise - isInScope(path: string): boolean + isInScope(path: string): Promise getScopePath(): string } @@ -61,6 +61,7 @@ export interface ExecuteOptions { timeout?: number env?: Record session?: Session + signal?: AbortSignal } /** @module computer/types */ @@ -74,11 +75,17 @@ export interface ExecuteResult { output?: TextOutput } -export interface EditResult { - success: boolean - context: string - replacements: number -} +export type WriteResult = + { type: 'text'; before: string; after: string } | { type: 'binary' } + +export type EditResult = + | { + success: true + before: string + after: string + replacements: number + } + | { success: false; replacements: 0 } export interface TerminalOptions { cols?: number diff --git a/packages/extension-agent/src/service/computer.ts b/packages/extension-agent/src/service/computer.ts index 5209c5691..b9bc33d8b 100644 --- a/packages/extension-agent/src/service/computer.ts +++ b/packages/extension-agent/src/service/computer.ts @@ -1,14 +1,10 @@ /** @module service/computer */ import { randomUUID } from 'node:crypto' -import os from 'node:os' import path from 'node:path' import { SystemMessage } from '@langchain/core/messages' import which from 'which' -import type { - AgentRunContext, - ToolMask -} from 'koishi-plugin-chatluna/llm-core/agent' +import type { AgentRunContext } from 'koishi-plugin-chatluna/llm-core/agent' import type { ChatLunaToolRunnable } from 'koishi-plugin-chatluna/llm-core/platform/types' import { countMessageTokens, @@ -172,7 +168,12 @@ export class ChatLunaAgentComputerService { } if (type === 'local') { - return os.tmpdir() + if (!conversationId) return undefined + const session = await this.getOrCreateSession({ + backend: 'local', + conversationId + }) + return await session.getTempDir() } return '/tmp' @@ -946,12 +947,14 @@ export class ChatLunaAgentComputerService { runConfig?: ChatLunaToolRunnable, backend?: ComputerBackendType ) { - const sub = runConfig?.configurable?.agentContext?.subagentContext - const info = sub - ? this.ctx.chatluna_agent?.subAgent - .getCatalogSync() - .find((item) => item.id === sub.agentId) - : undefined + const context = runConfig!.configurable.agentContext + + const info = + context.kind === 'subagent' + ? this.ctx.chatluna_agent?.subAgent + .getCatalogSync() + .find((item) => item.id === context.agentId) + : undefined return { backend, allowedBackends: info @@ -960,13 +963,8 @@ export class ChatLunaAgentComputerService { COMPUTER_BACKENDS ) : undefined, - conversationId: - sub?.parentConversationId ?? - runConfig?.configurable?.agentContext?.conversationId ?? - runConfig?.configurable?.conversationId, - userId: - runConfig?.configurable?.userId ?? - runConfig?.configurable?.session?.userId + conversationId: context.conversationId, + userId: context.userId } } @@ -974,12 +972,12 @@ export class ChatLunaAgentComputerService { context: AgentRunContext, backend?: ComputerBackendType ) { - const sub = context.subagentContext - const info = sub - ? this.ctx.chatluna_agent?.subAgent - .getCatalogSync() - .find((item) => item.id === sub.agentId) - : undefined + const info = + context.kind === 'subagent' + ? this.ctx.chatluna_agent?.subAgent + .getCatalogSync() + .find((item) => item.id === context.agentId) + : undefined return { backend, @@ -989,7 +987,7 @@ export class ChatLunaAgentComputerService { COMPUTER_BACKENDS ) : undefined, - conversationId: sub?.parentConversationId ?? context.conversationId, + conversationId: context.conversationId, userId: context.userId } } @@ -1166,8 +1164,10 @@ export class ChatLunaAgentComputerService { this._promptDispose = this.ctx.chatluna.contextManager.pipeline( 'after_system_prompts', async (runtime: PromptContextRuntime, next) => { - const mask = (runtime.configurable as { toolMask?: ToolMask }) - ?.toolMask + const agentContext = runtime.configurable?.agentContext + if (!agentContext) return next() + + const mask = agentContext.toolMask const registry = this.ctx.chatluna.platform.getToolRegistry() const capabilities = ( mask != null @@ -1180,17 +1180,7 @@ export class ChatLunaAgentComputerService { return next() } - const agentContext = runtime.configurable?.agentContext as { - conversationId?: string - subagentContext?: { parentConversationId?: string } - } - const sub = - agentContext?.subagentContext ?? - runtime.configurable?.subagentContext - const conversationId = - sub?.parentConversationId ?? - agentContext?.conversationId ?? - runtime.configurable?.conversationId + const conversationId = agentContext.conversationId const type = this.resolveProvider() ?? this.config.computer.defaultProvider diff --git a/packages/extension-agent/src/service/skills.ts b/packages/extension-agent/src/service/skills.ts index b2b221f62..14a795386 100644 --- a/packages/extension-agent/src/service/skills.ts +++ b/packages/extension-agent/src/service/skills.ts @@ -3,7 +3,6 @@ import { writeFile } from 'fs/promises' import { SystemMessage } from '@langchain/core/messages' import type {} from 'koishi-plugin-chatluna/llm-core/chat/app' -import type { ToolMask } from 'koishi-plugin-chatluna/llm-core/agent' import { countMessageTokens, PromptContextRuntime @@ -262,20 +261,20 @@ export class ChatLunaAgentSkillsService implements SkillToolService { async activateSkill(name: string, runConfig?: ChatLunaToolRunnable) { const skill = this.getVisibleSkillByName(name) - const conversationId = runConfig?.configurable?.conversationId - const sub = runConfig?.configurable?.agentContext?.subagentContext + const agentContext = runConfig!.configurable.agentContext + + const conversationId = agentContext.conversationId + const sub = agentContext.kind === 'subagent' const session = runConfig?.configurable?.session - const source = - (runConfig?.configurable as { source?: 'chatluna' | 'character' }) - ?.source ?? 'chatluna' + const source = agentContext.source if (sub) { const agent = this.ctx.chatluna_agent?.subAgent .getCatalogSync() - .find((item) => item.id === sub.agentId) + .find((item) => item.id === agentContext.agentId) if (!agent) { - throw new Error(`Sub-agent not found: ${sub.agentId}`) + throw new Error(`Sub-agent not found: ${agentContext.agentId}`) } const names = this.permission @@ -489,26 +488,13 @@ export class ChatLunaAgentSkillsService implements SkillToolService { this._promptDispose = this.ctx.chatluna.contextManager.pipeline( 'after_system_prompts', async (runtime: PromptContextRuntime, next) => { - const conversationId = runtime.configurable?.conversationId - if (!conversationId) return next() - - const sub = - ( - runtime.configurable?.agentContext as { - subagentContext?: ChatLunaToolRunnable['configurable']['agentContext'] extends infer T - ? T extends { subagentContext?: infer U } - ? U - : never - : never - } - )?.subagentContext ?? runtime.configurable?.subagentContext + const agentContext = runtime.configurable?.agentContext + if (!agentContext) return next() + + const conversationId = agentContext.conversationId + const sub = agentContext.kind === 'subagent' const session = runtime.configurable?.session - const source = - ( - runtime.configurable as { - source?: 'chatluna' | 'character' - } - )?.source ?? 'chatluna' + const source = agentContext.source const cwd = this.ctx.chatluna_agent?.computer.getPromptWorkdir( conversationId @@ -516,8 +502,7 @@ export class ChatLunaAgentSkillsService implements SkillToolService { const status = this.ctx.chatluna_agent?.computer.getStatus() const remote = status != null && status.defaultProvider !== 'local' - const mask = (runtime.configurable as { toolMask?: ToolMask }) - ?.toolMask + const mask = agentContext.toolMask const hasTool = mask == null || this.ctx.chatluna.platform @@ -529,7 +514,7 @@ export class ChatLunaAgentSkillsService implements SkillToolService { const agent = sub ? this.ctx.chatluna_agent?.subAgent .getCatalogSync() - .find((item) => item.id === sub.agentId) + .find((item) => item.id === agentContext.agentId) : undefined const full = sub ? agent diff --git a/packages/extension-agent/src/service/sub_agent.ts b/packages/extension-agent/src/service/sub_agent.ts index c2d144d55..7add2ce0f 100644 --- a/packages/extension-agent/src/service/sub_agent.ts +++ b/packages/extension-agent/src/service/sub_agent.ts @@ -320,13 +320,16 @@ export class ChatLunaAgentSubAgentService { toolMask: await this.permission.createSubAgentToolMask( info, ctx.session, - ctx.source ?? 'chatluna' + ctx.agentContext?.source ?? 'chatluna' ) }) return createTaskTool({ - list: ({ session, source }) => - this.listRunnableAgents(session, source).map((item) => ({ + list: ({ session, agentContext }) => + this.listRunnableAgents( + session, + agentContext?.source ?? 'chatluna' + ).map((item) => ({ id: item.id, name: item.name, description: item.description @@ -335,7 +338,7 @@ export class ChatLunaAgentSubAgentService { const info = this.findRunnableAgent( name, ctx.session, - ctx.source + ctx.agentContext?.source ) if (!info) { @@ -358,7 +361,7 @@ export class ChatLunaAgentSubAgentService { !this.permission.canUseSubAgent( info, ctx.session, - ctx.source + ctx.agentContext?.source ) ) { return undefined @@ -418,16 +421,13 @@ export class ChatLunaAgentSubAgentService { this._promptDispose = this.ctx.chatluna.contextManager.pipeline( 'after_system_prompts', async (runtime: PromptContextRuntime, next) => { - if (!runtime.configurable?.conversationId) return next() - if (runtime.configurable?.subagentContext) return next() + const agentContext = runtime.configurable?.agentContext + if (!agentContext?.conversationId) return next() + if (agentContext.kind === 'subagent') return next() const session = runtime.configurable?.session - const source = (runtime.configurable?.source ?? - 'chatluna') as Parameters< - ChatLunaAgentPermissionService['canUseSubAgent'] - >[2] - - const mask = runtime.configurable?.toolMask + const source = agentContext.source + const mask = agentContext.toolMask if ( mask != null && diff --git a/packages/extension-agent/src/service/trigger.ts b/packages/extension-agent/src/service/trigger.ts index 758b8dba7..9f02541d1 100644 --- a/packages/extension-agent/src/service/trigger.ts +++ b/packages/extension-agent/src/service/trigger.ts @@ -1,6 +1,5 @@ import { SystemMessage } from '@langchain/core/messages' import { type Context } from 'koishi' -import type { ToolMask } from 'koishi-plugin-chatluna/llm-core/agent' import { countMessageTokens, PromptContextRuntime @@ -176,12 +175,11 @@ export class ChatLunaAgentTriggerService { this._promptDispose = this.ctx.chatluna.contextManager.pipeline( 'after_system_prompts', async (runtime: PromptContextRuntime, next) => { - if (!runtime.configurable?.conversationId) return next() - if (runtime.configurable?.subagentContext) return next() + const agentContext = runtime.configurable?.agentContext + if (!agentContext?.conversationId) return next() + if (agentContext.kind === 'subagent') return next() - const mask = ( - runtime.configurable as { toolMask?: ToolMask } - ).toolMask + const mask = agentContext.toolMask if ( mask != null && !this.ctx.chatluna.platform diff --git a/packages/extension-agent/src/sub-agent/render.ts b/packages/extension-agent/src/sub-agent/render.ts index 1a27ee1f6..004d2c712 100644 --- a/packages/extension-agent/src/sub-agent/render.ts +++ b/packages/extension-agent/src/sub-agent/render.ts @@ -1,7 +1,5 @@ -import { - renderAvailableAgents, - SubagentContext -} from 'koishi-plugin-chatluna/llm-core/agent' +import { renderAvailableAgents } from 'koishi-plugin-chatluna/llm-core/agent' +import type { SubagentContext } from 'koishi-plugin-chatluna/llm-core/agent' import { SubAgentInfo } from '../types' export function renderAvailableSubAgents( @@ -14,7 +12,7 @@ export function renderAvailableSubAgents( export function renderSubAgentSystemPrompt( info: SubAgentInfo, - context: SubagentContext, + subagentContext: SubagentContext, skills?: string, computer?: { enabled: boolean; backends: string[]; capabilities: string[] } ) { @@ -23,7 +21,8 @@ export function renderSubAgentSystemPrompt( ...(skills ? ['', skills] : []), '', '', - `Sub-agent "${info.name}" | depth: ${context.depth} | parent: ${context.traceInfo.parentAgent} | run: ${context.traceInfo.runId}`, + `Sub-agent "${info.name}" | depth: ${subagentContext.depth} | ` + + `parent: ${subagentContext.traceInfo.parentAgent} | run: ${subagentContext.traceInfo.runId}`, 'You are executing a delegated task. Do NOT delegate to other sub-agents.', 'If the task exceeds your scope, summarize findings and return to the parent.', 'If shell or computer work may take a while, use managed background execution and inspect it later instead of waiting for the default timeout.', diff --git a/packages/extension-agent/src/sub-agent/run.ts b/packages/extension-agent/src/sub-agent/run.ts index f079b027a..d3afe7db1 100644 --- a/packages/extension-agent/src/sub-agent/run.ts +++ b/packages/extension-agent/src/sub-agent/run.ts @@ -49,9 +49,7 @@ export async function createSubAgent( return await base.agent.generate({ ...input, - prompt, - toolMask: base.toolMask, - subagentContext: base.subCtx + prompt }) }, async stream(input) { @@ -66,9 +64,7 @@ export async function createSubAgent( return await base.agent.stream({ ...input, - prompt, - toolMask: base.toolMask, - subagentContext: base.subCtx + prompt }) }, asTool(toolOptions?: AgentToolOptions) { @@ -152,27 +148,9 @@ async function createInnerAgent( ) : [] - const subCtx = - input.subagentContext != null - ? { ...input.subagentContext, toolMask } - : { - agentId: options.info.id, - agentName: options.info.name, - parentConversationId: input.conversationId ?? '', - depth: 1, - maxDepth: 1, - toolMask, - disableHandoff: true, - traceInfo: { - runId: options.info.id, - parentAgent: 'main', - startedAt: Date.now() - } - } - const system = renderSubAgentSystemPrompt( options.info, - subCtx, + input.subagentContext, skills, backends.length > 0 && computer?.getStatus().enabled ? { @@ -194,7 +172,6 @@ async function createInnerAgent( return { llm, toolMask, - subCtx, agent: await options.ctx.chatluna.createAgent({ id: options.info.id, name: options.info.name, diff --git a/packages/extension-agent/src/sub-agent/runtime.ts b/packages/extension-agent/src/sub-agent/runtime.ts index 87bceae83..999a64253 100644 --- a/packages/extension-agent/src/sub-agent/runtime.ts +++ b/packages/extension-agent/src/sub-agent/runtime.ts @@ -1,9 +1,8 @@ /** @module sub-agent/runtime */ import { - MessageQueue, - SubagentContext, - ToolMask + AgentRunContext, + MessageQueue } from 'koishi-plugin-chatluna/llm-core/agent' import { ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model' import { Session } from 'koishi' @@ -15,8 +14,7 @@ export interface RunSubAgentOptions { prompt: string session: Session parentConversationId: string - parentSubagentContext?: SubagentContext - parentToolMask?: ToolMask + parentContext?: AgentRunContext signal?: AbortSignal model?: ChatLunaChatModel task?: SubAgentTaskSession diff --git a/packages/extension-agent/src/sub-agent/task_attach.ts b/packages/extension-agent/src/sub-agent/task_attach.ts index 7fce50cba..ecf2a7541 100644 --- a/packages/extension-agent/src/sub-agent/task_attach.ts +++ b/packages/extension-agent/src/sub-agent/task_attach.ts @@ -1,6 +1,7 @@ /** @module sub-agent/task_attach */ import { Context, Session } from 'koishi' +import { randomUUID } from 'crypto' import type { ConversationRecord } from 'koishi-plugin-chatluna/services/chat' import type { ChatLunaToolRunnable } from 'koishi-plugin-chatluna/llm-core/platform/types' import { logger } from '..' @@ -54,8 +55,6 @@ export class ChatLunaAgentTaskAttachService { session.content, { session, - conversationId: item.parentConversationId, - source: 'chatluna', runConfig: await this.runConfig(session, conversation) } ) @@ -141,10 +140,18 @@ export class ChatLunaAgentTaskAttachService { configurable: { model: model.value, session, - conversationId: conversation.id, preset: conversation.preset, - userId: session.userId, - source: 'chatluna' + agentContext: { + kind: 'main', + agentId: conversation.id, + agentName: conversation.preset, + conversationId: conversation.id, + requestId: randomUUID(), + source: 'chatluna', + userId: session.userId, + guildId: session.guildId, + channelId: session.channelId + } } } as ChatLunaToolRunnable } diff --git a/packages/extension-agent/src/trigger/runner.ts b/packages/extension-agent/src/trigger/runner.ts index 36ad3ddbb..bf03c8cc9 100644 --- a/packages/extension-agent/src/trigger/runner.ts +++ b/packages/extension-agent/src/trigger/runner.ts @@ -542,9 +542,10 @@ export class TriggerRunner { stats: candidate?.stats, excerpts: candidate?.excerpts }), + conversationId: `trigger:${task.id}`, + requestId: randomUUID(), session, - signal: controller.signal, - toolMask + signal: controller.signal }) used = result.message?.usage_metadata?.total_tokens ?? 0 const text = getMessageContent( diff --git a/packages/extension-agent/src/trigger/tool.ts b/packages/extension-agent/src/trigger/tool.ts index 87e5ae016..c58f63448 100644 --- a/packages/extension-agent/src/trigger/tool.ts +++ b/packages/extension-agent/src/trigger/tool.ts @@ -288,10 +288,7 @@ export class FinishTriggerRunTool extends StructuredTool { _: unknown, config?: ChatLunaToolRunnable ) { - const requestId = - config?.configurable.agentContext?.requestId ?? - (config?.configurable as { requestId?: string } | undefined) - ?.requestId + const requestId = config?.configurable.agentContext?.requestId if (!requestId) { return JSON.stringify({ ok: false, diff --git a/packages/extension-agent/src/utils/runtime_sync.ts b/packages/extension-agent/src/utils/runtime_sync.ts index 6c004f359..39d04681f 100644 --- a/packages/extension-agent/src/utils/runtime_sync.ts +++ b/packages/extension-agent/src/utils/runtime_sync.ts @@ -332,12 +332,5 @@ async function listRemoteFiles(session: ComputerSessionApi, root: string) { } function getRuntimeKey(context: AgentRunContext) { - return [ - context.requestId ?? - context.conversationId ?? - context.parentConversationId ?? - 'runtime', - context.kind, - context.agentId ?? 'main' - ].join(':') + return [context.requestId, context.kind, context.agentId].join(':') } diff --git a/packages/extension-long-memory/src/plugins/tool.ts b/packages/extension-long-memory/src/plugins/tool.ts index 090e374bf..949b79565 100644 --- a/packages/extension-long-memory/src/plugins/tool.ts +++ b/packages/extension-long-memory/src/plugins/tool.ts @@ -21,7 +21,7 @@ function buildMemoryInfo(config: ChatLunaToolRunnable) { guildId: config.configurable.session.guildId || config.configurable.session.channelId, - userId: config.configurable.userId + userId: config.configurable.agentContext.userId } } @@ -164,7 +164,7 @@ export class MemorySearchTool extends StructuredTool { await this.ctx.chatluna_long_memory.initMemoryLayers( info, - config.configurable.conversationId, + config.configurable.agentContext.conversationId, parsedLayerType ) @@ -265,7 +265,7 @@ export class MemoryAddTool extends StructuredTool { await this.ctx.chatluna_long_memory.initMemoryLayers( info, - config.configurable.conversationId, + config.configurable.agentContext.conversationId, parsedLayerType ) @@ -349,7 +349,7 @@ export class MemoryDeleteTool extends StructuredTool { await this.ctx.chatluna_long_memory.initMemoryLayers( info, - config.configurable.conversationId, + config.configurable.agentContext.conversationId, parsedLayerType ) @@ -445,7 +445,7 @@ export class MemoryUpdateTool extends StructuredTool { await this.ctx.chatluna_long_memory.initMemoryLayers( info, - config.configurable.conversationId, + config.configurable.agentContext.conversationId, layers ) diff --git a/packages/extension-tools/src/plugins/todos.ts b/packages/extension-tools/src/plugins/todos.ts index e2a19bc45..bafa25570 100644 --- a/packages/extension-tools/src/plugins/todos.ts +++ b/packages/extension-tools/src/plugins/todos.ts @@ -100,7 +100,8 @@ export async function apply( toolConfig: ChatLunaToolRunnable ) => { const todos = input.todos as Todo[] - const conversationId = toolConfig.configurable.conversationId + const conversationId = + toolConfig.configurable.agentContext.conversationId const session = toolConfig.configurable.session const prev = todosStore.get(conversationId) const unchanged = @@ -209,7 +210,8 @@ export async function apply( contextManager.pipeline( 'after_scratchpad', async (runtime: PromptContextRuntime, next) => { - const conversationId = runtime.configurable?.conversationId + const conversationId = + runtime.configurable?.agentContext?.conversationId if (!conversationId) return next() const todos = todosStore.get(conversationId) diff --git a/packages/service-multimodal/src/plugins/read_files.ts b/packages/service-multimodal/src/plugins/read_files.ts index 7948f41f9..457a3cffa 100644 --- a/packages/service-multimodal/src/plugins/read_files.ts +++ b/packages/service-multimodal/src/plugins/read_files.ts @@ -90,7 +90,8 @@ export class ReadFilesTool extends StructuredTool { ) { const files = input.files const model = runConfig?.configurable?.model - const conversationId = runConfig?.configurable?.conversationId + const conversationId = + runConfig?.configurable?.agentContext?.conversationId const fileConfig = model?.fileHandlingConfig const maxTotal = fileConfig?.maxTotalSizeBytes ?? DEFAULT_MAX_TOTAL_SIZE_BYTES diff --git a/packages/service-search/src/chain/browsing_chain.ts b/packages/service-search/src/chain/browsing_chain.ts index 78ec4887b..bd134f988 100644 --- a/packages/service-search/src/chain/browsing_chain.ts +++ b/packages/service-search/src/chain/browsing_chain.ts @@ -1,4 +1,5 @@ /* eslint-disable max-len */ +import { randomUUID } from 'crypto' import { Embeddings } from '@langchain/core/embeddings' import { AIMessage, @@ -27,7 +28,11 @@ import { ChatLunaTool, ChatLunaToolRunnable } from 'koishi-plugin-chatluna/llm-core/platform/types' -import { applyToolMask, ToolMask } from 'koishi-plugin-chatluna/llm-core/agent' +import { + AgentRunContext, + applyToolMask, + ToolMask +} from 'koishi-plugin-chatluna/llm-core/agent' import { Session } from 'koishi' import { SearchAction, SummaryType } from '../types' import { parseSearchAction } from '../utils/parse' @@ -399,13 +404,10 @@ export class ChatLunaBrowsingChain Promise.allSettled( questions.map(async (question) => { const raw = await tool - .invoke(question, { - configurable: { - model: this.model, - session, - conversationId - } - }) + .invoke( + question, + this.buildRunConfig(session, conversationId, signal) + ) .then((text) => text as string) const parsed = JSON.parse(raw) as SearchResultLike[] @@ -426,28 +428,46 @@ export class ChatLunaBrowsingChain ) } - private async _browseUrls( - urls: string[], + private buildRunConfig( session: Session, conversationId: string, signal: AbortSignal - ) { - if (!this.browserManager) return [] - - const runConfig = { + ): ChatLunaToolRunnable { + const agentContext = { + kind: 'main', + agentId: 'browsing-chain', + agentName: this.botName, + conversationId, + requestId: randomUUID(), + source: 'chatluna', + userId: session.userId, + guildId: session.guildId, + channelId: session.channelId + } satisfies AgentRunContext + return { + signal, configurable: { model: this.model, session, - conversationId + agentContext } - } as ChatLunaToolRunnable + } + } + + private async _browseUrls( + urls: string[], + session: Session, + conversationId: string, + signal: AbortSignal + ) { + if (!this.browserManager) return [] const results = await raceAbort( Promise.allSettled( urls.map(async (url) => { const text = await this.browserManager.readText( { url }, - runConfig + this.buildRunConfig(session, conversationId, signal) ) if (this.thoughtMessage) { diff --git a/packages/service-search/src/tools/browser/manager.ts b/packages/service-search/src/tools/browser/manager.ts index 8d88a10bd..e37fab1e5 100644 --- a/packages/service-search/src/tools/browser/manager.ts +++ b/packages/service-search/src/tools/browser/manager.ts @@ -388,7 +388,12 @@ export class BrowserManager { } function getSessionKey(runConfig?: ChatLunaToolRunnable) { - const cfg = runConfig?.configurable + const cfg = runConfig?.configurable as + | (ChatLunaToolRunnable['configurable'] & { + conversationId?: string + }) + | undefined + if (cfg?.agentContext) return cfg.agentContext.conversationId return String( cfg?.conversationId ?? cfg?.session?.channelId ?? diff --git a/packages/shared-adapter/src/requester.ts b/packages/shared-adapter/src/requester.ts index c3edfed0e..d3a4b1b1f 100644 --- a/packages/shared-adapter/src/requester.ts +++ b/packages/shared-adapter/src/requester.ts @@ -31,6 +31,7 @@ import { import { convertDeltaToMessageChunk, convertMessageToMessageChunk, + createRequestSignal, formatToolsToOpenAITools, formatToolsToResponseTools, langchainMessageToOpenAIMessage, @@ -89,10 +90,7 @@ export interface ResponseToolOptions { builtinTools?: ResponseBuiltinTool[] } -function throwIfUnsafeCode( - code: string | undefined | null, - detail: string -): void { +function throwIfBadCode(code: string | undefined | null, detail: string): void { if (code != null && UNSAFE_OPENAI_ERROR_CODES.includes(code)) { throw new ChatLunaError( ChatLunaErrorCode.API_UNSAFE_CONTENT, @@ -104,7 +102,7 @@ function throwIfUnsafeCode( function throwIfUnsafeBody(body: string): void { try { const parsed = JSON.parse(body) as { error?: OpenAIError } | null - if (parsed) throwIfUnsafeCode(parsed.error?.code, body) + if (parsed) throwIfBadCode(parsed.error?.code, body) } catch (e) { if (e instanceof ChatLunaError) throw e } @@ -248,7 +246,7 @@ export async function* processStreamResponse< const data = JSON.parse(chunk) as ChatCompletionResponse if (data.error) { - throwIfUnsafeCode(data.error.code, chunk) + throwIfBadCode(data.error.code, chunk) throw new ChatLunaError( ChatLunaErrorCode.API_REQUEST_FAILED, new Error('Error when calling completion, Result: ' + chunk) @@ -257,7 +255,7 @@ export async function* processStreamResponse< const choice = data.choices?.[0] - throwIfUnsafeCode(choice?.finish_reason, chunk) + throwIfBadCode(choice?.finish_reason, chunk) if (data.usage) { const usageMetadata = openAIUsageToUsageMetadata(data.usage) @@ -423,7 +421,7 @@ export async function processResponse< const data = JSON.parse(responseText) as ChatCompletionResponse if (data.error) { - throwIfUnsafeCode(data.error.code, responseText) + throwIfBadCode(data.error.code, responseText) throw new ChatLunaError( ChatLunaErrorCode.API_REQUEST_FAILED, new Error( @@ -434,7 +432,7 @@ export async function processResponse< const choice = data.choices?.[0] - throwIfUnsafeCode(choice?.finish_reason, responseText) + throwIfBadCode(choice?.finish_reason, responseText) if (!choice) { throw new ChatLunaError( @@ -480,14 +478,14 @@ export async function responseToChatGeneration( imageProvider?: ResponseImageProvider ) { if (response.error) { - throwIfUnsafeCode(response.error.code, response.error.message ?? '') + throwIfBadCode(response.error.code, response.error.message ?? '') throw new ChatLunaError( ChatLunaErrorCode.API_REQUEST_FAILED, new Error(response.error.message ?? JSON.stringify(response.error)) ) } - throwIfUnsafeCode(response.incomplete_details?.reason, '') + throwIfBadCode(response.incomplete_details?.reason, '') const text = responseOutputText(response) const toolCalls = responseOutputToolCalls(response) @@ -600,7 +598,7 @@ export async function* processResponseApiStream< const data = JSON.parse(chunk) as ResponseStreamEvent if (data.type === 'error') { - throwIfUnsafeCode(data.code, data.message ?? chunk) + throwIfBadCode(data.code, data.message ?? chunk) throw new ChatLunaError( ChatLunaErrorCode.API_REQUEST_FAILED, new Error(chunk) @@ -711,8 +709,8 @@ export async function* processResponseApiStream< data.type === 'response.incomplete' || data.type === 'response.error' ) { - throwIfUnsafeCode(data.response?.incomplete_details?.reason, '') - throwIfUnsafeCode(data.response?.error?.code, '') + throwIfBadCode(data.response?.incomplete_details?.reason, '') + throwIfBadCode(data.response?.error?.code, '') throw new ChatLunaError( ChatLunaErrorCode.API_REQUEST_FAILED, @@ -744,6 +742,13 @@ export async function* completionStream< enableGoogleSearch?: boolean, supportImageInput?: boolean ): AsyncGenerator { + if (params.signal?.aborted) { + throw ( + params.signal.reason ?? + new ChatLunaError(ChatLunaErrorCode.ABORTED, undefined, true) + ) + } + const { modelRequester } = requestContext const chatCompletionParams = await buildChatCompletionParams( @@ -752,17 +757,29 @@ export async function* completionStream< enableGoogleSearch ?? false, supportImageInput ?? true ) + const requestSignal = createRequestSignal(params) try { + if (requestSignal.signal?.aborted) { + throw ( + requestSignal.signal.reason ?? + new ChatLunaError(ChatLunaErrorCode.ABORTED, undefined, true) + ) + } + const response = await modelRequester.post( completionUrl, chatCompletionParams, { - signal: params.signal + signal: requestSignal.signal } ) + requestSignal.clearTimeout() - const iterator = sseIterable(response) + const iterator = sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }) yield* processStreamResponse(requestContext, iterator) } catch (e) { if (requestContext.ctx.chatluna.currentConfig.isLog) { @@ -773,11 +790,16 @@ export async function* completionStream< 'warn' ) } + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } if (e instanceof ChatLunaError) { throw e } else { throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) } + } finally { + requestSignal.dispose() } } @@ -803,12 +825,13 @@ export async function completion< delete chatCompletionParams.stream + const requestSignal = createRequestSignal(params) try { const response = await modelRequester.post( completionUrl, chatCompletionParams, { - signal: params.signal + signal: requestSignal.signal } ) @@ -830,6 +853,8 @@ export async function completion< } else { throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) } + } finally { + requestSignal.dispose() } } @@ -844,6 +869,13 @@ export async function* responseApiCompletionStream< supportImageInput?: boolean, imageProvider?: ResponseImageProvider ): AsyncGenerator { + if (params.signal?.aborted) { + throw ( + params.signal.reason ?? + new ChatLunaError(ChatLunaErrorCode.ABORTED, undefined, true) + ) + } + const { modelRequester } = requestContext const request = await buildResponseParams( params, @@ -851,15 +883,27 @@ export async function* responseApiCompletionStream< opts, supportImageInput ?? true ) + const requestSignal = createRequestSignal(params) try { + if (requestSignal.signal?.aborted) { + throw ( + requestSignal.signal.reason ?? + new ChatLunaError(ChatLunaErrorCode.ABORTED, undefined, true) + ) + } + const response = await modelRequester.post('responses', request, { - signal: params.signal + signal: requestSignal.signal }) + requestSignal.clearTimeout() yield* processResponseApiStream( requestContext, - sseIterable(response), + sseIterable(response, { + timeout: params.timeout, + signal: requestSignal.signal + }), imageProvider ) } catch (e) { @@ -871,8 +915,13 @@ export async function* responseApiCompletionStream< 'warn' ) } + if (requestSignal.signal?.aborted) { + throw requestSignal.signal.reason ?? e + } if (e instanceof ChatLunaError) throw e throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } @@ -898,9 +947,10 @@ export async function responseApiCompletion< delete request.stream delete request.stream_options + const requestSignal = createRequestSignal(params) try { const response = await modelRequester.post('responses', request, { - signal: params.signal + signal: requestSignal.signal }) return attachUsage( @@ -918,6 +968,8 @@ export async function responseApiCompletion< } if (e instanceof ChatLunaError) throw e throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } @@ -930,12 +982,17 @@ export async function createEmbeddings< embeddingUrl: string = 'embeddings' ): Promise { const { modelRequester } = requestContext + const requestSignal = createRequestSignal(params) try { - const response = await modelRequester.post(embeddingUrl, { - input: params.input, - model: params.model - }) + const response = await modelRequester.post( + embeddingUrl, + { + input: params.input, + model: params.model + }, + { signal: requestSignal.signal } + ) const data = (await response.json()) as CreateEmbeddingResponse @@ -959,6 +1016,8 @@ export async function createEmbeddings< } throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } @@ -971,6 +1030,7 @@ export async function createRerank< rerankUrl: string = 'rerank' ): Promise { const { modelRequester } = requestContext + const requestSignal = createRequestSignal(params) try { const response = await modelRequester.post( @@ -984,7 +1044,7 @@ export async function createRerank< return_documents: false }, { - signal: params.signal + signal: requestSignal.signal } ) @@ -1024,6 +1084,8 @@ export async function createRerank< } throw new ChatLunaError(ChatLunaErrorCode.API_REQUEST_FAILED, e) + } finally { + requestSignal.dispose() } } diff --git a/packages/shared-adapter/src/utils.ts b/packages/shared-adapter/src/utils.ts index eaf24a46d..99739905c 100644 --- a/packages/shared-adapter/src/utils.ts +++ b/packages/shared-adapter/src/utils.ts @@ -47,6 +47,50 @@ import { supportAudioInput, supportImageInput } from './client' +import { + EmbeddingsRequestParams, + ModelRequestParams, + RerankerRequestParams +} from 'koishi-plugin-chatluna/llm-core/platform/api' +import { createTimeoutError } from 'koishi-plugin-chatluna/utils/error' + +export function createRequestSignal( + params: + EmbeddingsRequestParams | ModelRequestParams | RerankerRequestParams, + timeout = params.timeout +) { + if (timeout == null) { + return { + signal: params.signal, + clearTimeout() {}, + dispose() {} + } + } + + const controller = new AbortController() + const onAbort = () => controller.abort(params.signal?.reason) + if (params.signal?.aborted) { + onAbort() + } else { + params.signal?.addEventListener('abort', onAbort, { once: true }) + } + + const timer = setTimeout( + () => controller.abort(createTimeoutError()), + timeout + ) + + return { + signal: controller.signal, + clearTimeout() { + clearTimeout(timer) + }, + dispose() { + clearTimeout(timer) + params.signal?.removeEventListener('abort', onAbort) + } + } +} export class ReasoningState { content = ''