Skip to content

Commit b6082d4

Browse files
CookSleepdingyi222666coderabbitai[bot]
authored
fix(core): 正式支持 direct tool observation (#815)
* fix(core): 正式支持 direct tool observation * fix(core,extension-agent): unify observation message content handling Reuse a shared observationToMessageContent helper so direct tool observations are converted consistently and ToolMessage content stays type-safe across agent flows. * Update packages/extension-agent/src/sub-agent/session.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: dingyi <dingyi222666@foxmail.com> Co-authored-by: dingyi <2187778735@qq.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 3692155 commit b6082d4

7 files changed

Lines changed: 43 additions & 10 deletions

File tree

packages/core/src/llm-core/agent/executor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { AgentExecutorInput, AgentExecutorOutput } from './legacy-executor'
1111
export {
1212
coerceToAgentObservation,
1313
LegacyAgentExecutor,
14+
observationToMessageContent,
1415
runAgent,
1516
toToolInputErrorObservation,
1617
type AgentExecutorInput,

packages/core/src/llm-core/agent/legacy-executor.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ function isAgentObservation(value: unknown): value is AgentObservation {
479479
return true
480480
}
481481

482+
if (isDirectToolOutput(value)) {
483+
return true
484+
}
485+
482486
if (!Array.isArray(value)) {
483487
return false
484488
}
@@ -490,6 +494,10 @@ export function coerceToAgentObservation(
490494
observation: unknown,
491495
toolName?: string
492496
): AgentObservation {
497+
if (isDirectToolOutput(observation)) {
498+
return observation
499+
}
500+
493501
if (isAgentObservation(observation)) {
494502
if (
495503
Array.isArray(observation) &&
@@ -591,3 +599,7 @@ function toParsingErrorAction(
591599
log: text
592600
}
593601
}
602+
603+
export function observationToMessageContent(observation: AgentObservation) {
604+
return isDirectToolOutput(observation) ? '' : observation
605+
}

packages/core/src/llm-core/agent/openai/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from './output_parser'
3131
import { BaseChatPromptTemplate } from '@langchain/core/prompts'
3232
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
33+
import { observationToMessageContent } from '../legacy-executor'
3334

3435
/**
3536
* Checks if the given action is a FunctionsAgentAction.
@@ -55,16 +56,22 @@ function _convertAgentStepToMessages(
5556
) {
5657
if (isToolsAgentAction(action) && action.toolCallId !== undefined) {
5758
const log = action.messageLog as BaseMessage[]
59+
const content = observationToMessageContent(observation)
5860
if (
59-
observation.length < 1 ||
60-
observation == null ||
61-
observation === 'null'
61+
content === observation &&
62+
(content.length < 1 || content === 'null')
6263
) {
63-
observation = `The tool ${action.tool} returned no output. Try again or stop the tool call, tell the user failed to execute the tool.`
64+
return log.concat(
65+
new ToolMessage({
66+
content: `The tool ${action.tool} returned no output. Try again or stop the tool call, tell the user failed to execute the tool.`,
67+
name: action.tool,
68+
tool_call_id: action.toolCallId
69+
})
70+
)
6471
}
6572
return log.concat(
6673
new ToolMessage({
67-
content: observation,
74+
content,
6875
name: action.tool,
6976
tool_call_id: action.toolCallId
7077
})

packages/core/src/llm-core/agent/sub-agent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { z } from 'zod'
1212
import type { ChatLunaToolRunnable } from '../platform/types'
1313
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
1414
import type { ChatLunaAgent } from './agent'
15+
import { observationToMessageContent } from './legacy-executor'
1516
import { MessageQueue } from './types'
1617
import type { AgentEvent, AgentStep, SubagentContext, ToolMask } from './types'
1718

@@ -868,7 +869,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
868869
...steps.map(
869870
(step) =>
870871
new ToolMessage({
871-
content: step.observation,
872+
content: observationToMessageContent(step.observation),
872873
tool_call_id: step.action.toolCallId,
873874
name: step.action.tool
874875
})

packages/core/src/llm-core/agent/types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
MessageContentFileUrl,
1111
MessageContentVideo
1212
} from 'koishi-plugin-chatluna/utils/langchain'
13+
import type { DirectToolOutput } from '@langchain/core/messages/tool'
1314

1415
export interface ChatCompletionMessageToolCall {
1516
/**
@@ -193,7 +194,14 @@ export type AgentObservationComplexContent =
193194
| MessageContentAudio
194195
| MessageContentVideo
195196

196-
export type AgentObservation = AgentObservationComplexContent[] | string
197+
export type AgentDirectToolObservation = DirectToolOutput & {
198+
replyEmitted?: boolean
199+
}
200+
201+
export type AgentObservation =
202+
| AgentObservationComplexContent[]
203+
| AgentDirectToolObservation
204+
| string
197205

198206
export interface ToolMask {
199207
mode: 'all' | 'allow' | 'deny'

packages/core/src/llm-core/memory/message/database_history.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
gzipEncode
1616
} from 'koishi-plugin-chatluna/utils/string'
1717
import { randomUUID } from 'crypto'
18+
import { observationToMessageContent } from '../../agent/legacy-executor'
1819
import type { AgentStep } from '../../agent/types'
1920
import type { MessageRecord } from '../../../services/conversation_types'
2021

@@ -71,7 +72,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
7172
...steps.map(
7273
(step) =>
7374
new ToolMessage({
74-
content: step.observation,
75+
content: observationToMessageContent(step.observation),
7576
tool_call_id: step.action.toolCallId,
7677
name: step.action.tool
7778
})

packages/extension-agent/src/sub-agent/session.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import {
66
HumanMessage,
77
ToolMessage
88
} from '@langchain/core/messages'
9-
import type { AgentStep } from 'koishi-plugin-chatluna/llm-core/agent'
9+
import {
10+
type AgentStep,
11+
observationToMessageContent
12+
} from 'koishi-plugin-chatluna/llm-core/agent'
1013
import { getMessageContent } from 'koishi-plugin-chatluna/utils/string'
1114
import type { SubAgentInfo, SubAgentRunInfo } from '../types'
1215

@@ -219,7 +222,7 @@ function createAgentToolMessages(steps: AgentStep[]): BaseMessage[] {
219222
...steps.map(
220223
(step) =>
221224
new ToolMessage({
222-
content: step.observation,
225+
content: observationToMessageContent(step.observation),
223226
tool_call_id: step.action.toolCallId,
224227
name: step.action.tool
225228
})

0 commit comments

Comments
 (0)