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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/src/llm-core/agent/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { AgentExecutorInput, AgentExecutorOutput } from './legacy-executor'
export {
coerceToAgentObservation,
LegacyAgentExecutor,
observationToMessageContent,
runAgent,
toToolInputErrorObservation,
type AgentExecutorInput,
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/llm-core/agent/legacy-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ function isAgentObservation(value: unknown): value is AgentObservation {
return true
}

if (isDirectToolOutput(value)) {
return true
}

if (!Array.isArray(value)) {
return false
}
Expand All @@ -490,6 +494,10 @@ export function coerceToAgentObservation(
observation: unknown,
toolName?: string
): AgentObservation {
if (isDirectToolOutput(observation)) {
return observation
}

if (isAgentObservation(observation)) {
if (
Array.isArray(observation) &&
Expand Down Expand Up @@ -591,3 +599,7 @@ function toParsingErrorAction(
log: text
}
}

export function observationToMessageContent(observation: AgentObservation) {
return isDirectToolOutput(observation) ? '' : observation
}
17 changes: 12 additions & 5 deletions packages/core/src/llm-core/agent/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './output_parser'
import { BaseChatPromptTemplate } from '@langchain/core/prompts'
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
import { observationToMessageContent } from '../legacy-executor'

/**
* Checks if the given action is a FunctionsAgentAction.
Expand All @@ -55,16 +56,22 @@ function _convertAgentStepToMessages(
) {
if (isToolsAgentAction(action) && action.toolCallId !== undefined) {
const log = action.messageLog as BaseMessage[]
const content = observationToMessageContent(observation)
if (
observation.length < 1 ||
observation == null ||
observation === 'null'
content === observation &&
(content.length < 1 || content === 'null')
) {
observation = `The tool ${action.tool} returned no output. Try again or stop the tool call, tell the user failed to execute the tool.`
return log.concat(
new ToolMessage({
content: `The tool ${action.tool} returned no output. Try again or stop the tool call, tell the user failed to execute the tool.`,
name: action.tool,
tool_call_id: action.toolCallId
})
)
}
return log.concat(
new ToolMessage({
content: observation,
content,
name: action.tool,
tool_call_id: action.toolCallId
})
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/llm-core/agent/sub-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { z } from 'zod'
import type { ChatLunaToolRunnable } from '../platform/types'
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
import type { ChatLunaAgent } from './agent'
import { observationToMessageContent } from './legacy-executor'
import { MessageQueue } from './types'
import type { AgentEvent, AgentStep, SubagentContext, ToolMask } from './types'

Expand Down Expand Up @@ -868,7 +869,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
...steps.map(
(step) =>
new ToolMessage({
content: step.observation,
content: observationToMessageContent(step.observation),
tool_call_id: step.action.toolCallId,
name: step.action.tool
})
Expand Down
10 changes: 9 additions & 1 deletion packages/core/src/llm-core/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
MessageContentFileUrl,
MessageContentVideo
} from 'koishi-plugin-chatluna/utils/langchain'
import type { DirectToolOutput } from '@langchain/core/messages/tool'

export interface ChatCompletionMessageToolCall {
/**
Expand Down Expand Up @@ -193,7 +194,14 @@ export type AgentObservationComplexContent =
| MessageContentAudio
| MessageContentVideo

export type AgentObservation = AgentObservationComplexContent[] | string
export type AgentDirectToolObservation = DirectToolOutput & {
replyEmitted?: boolean
}

export type AgentObservation =
| AgentObservationComplexContent[]
| AgentDirectToolObservation
| string

export interface ToolMask {
mode: 'all' | 'allow' | 'deny'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
gzipEncode
} from 'koishi-plugin-chatluna/utils/string'
import { randomUUID } from 'crypto'
import { observationToMessageContent } from '../../agent/legacy-executor'
import type { AgentStep } from '../../agent/types'
import type { MessageRecord } from '../../../services/conversation_types'

Expand Down Expand Up @@ -71,7 +72,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
...steps.map(
(step) =>
new ToolMessage({
content: step.observation,
content: observationToMessageContent(step.observation),
tool_call_id: step.action.toolCallId,
name: step.action.tool
})
Expand Down
7 changes: 5 additions & 2 deletions packages/extension-agent/src/sub-agent/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
HumanMessage,
ToolMessage
} from '@langchain/core/messages'
import type { AgentStep } from 'koishi-plugin-chatluna/llm-core/agent'
import {
type AgentStep,
observationToMessageContent
} from 'koishi-plugin-chatluna/llm-core/agent'
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
import type { SubAgentInfo, SubAgentRunInfo } from '../types'

Expand Down Expand Up @@ -219,7 +222,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
...steps.map(
(step) =>
new ToolMessage({
content: step.observation,
content: observationToMessageContent(step.observation),
tool_call_id: step.action.toolCallId,
name: step.action.tool
})
Expand Down
Loading