Skip to content
Open
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
31 changes: 28 additions & 3 deletions packages/core/src/llm-core/agent/legacy-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ export async function* runAgent(

yield {
type: 'round-decision',
canContinue: false
willConsumePendingMessages: false
}

const pending = queue?.drain() ?? []
Expand Down Expand Up @@ -262,7 +262,7 @@ export async function* runAgent(

yield {
type: 'round-decision',
canContinue: !tool?.returnDirect
willConsumePendingMessages: !tool?.returnDirect
}

yield {
Expand Down Expand Up @@ -293,6 +293,31 @@ export async function* runAgent(
const last = newSteps[newSteps.length - 1]
const tool = last ? toolMap[last.action.tool?.toLowerCase()] : undefined

if (last?.observation === '__character_reply_final__') {
yield {
type: 'round-decision',
willConsumePendingMessages: false
}

const pending = queue?.drain() ?? []
if (pending.length > 0) {
yield {
type: 'human-update',
messages: pending
}
}

yield {
type: 'done',
output: '',
log: last.action.log,
steps,
replyEmitted: true
}

return
}

if (tool?.returnDirect && last != null) {
const pending = queue?.drain() ?? []
if (pending.length > 0) {
Expand All @@ -317,7 +342,7 @@ export async function* runAgent(

yield {
type: 'round-decision',
canContinue: false
willConsumePendingMessages: false
}

yield {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/llm-core/agent/sub-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@
'Delegate a focused task to a specialized agent when parallel work, deeper investigation, or a narrower prompt will help.',
'Use the exact agent name from the injected catalog.',
'If delegated work may take a while, set background=true so it can continue beyond the normal tool timeout.',
'Use action=list or action=status to inspect background tasks, action=message to send more guidance while they run, and action=run with the same id to continue the same session later.'

Check warning on line 412 in packages/core/src/llm-core/agent/sub-agent.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 192. Maximum allowed is 160
].join('\n')
}

Expand Down Expand Up @@ -464,7 +464,7 @@
.enum(['run', 'status', 'list', 'message'])
.optional()
.describe(
'run starts or resumes an agent task, status inspects one task, list shows recent tasks in this conversation, message sends live guidance to a running background task.'

Check warning on line 467 in packages/core/src/llm-core/agent/sub-agent.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 188. Maximum allowed is 160
),
agent: z
.string()
Expand Down Expand Up @@ -784,6 +784,7 @@
at: Date.now(),
title: '最终输出',
text:
(event.replyEmitted ? '最终回复已由工具发送。' : '') ||
getMessageContent(event.message?.content ?? '') ||
event.output ||
event.log
Expand Down Expand Up @@ -882,7 +883,7 @@
`agent: ${task.agentName}`,
`run_id: ${run.runId}`,
`state: ${run.state}`,
`resume_hint: use ${toolName} with {"action":"run","id":"${task.id}","prompt":"next instruction"} to continue this session. Add "background":true when the work may take a while.`,

Check warning on line 886 in packages/core/src/llm-core/agent/sub-agent.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 187. Maximum allowed is 160
'',
output.trim() || '(empty)'
].join('\n')
Expand Down Expand Up @@ -975,7 +976,7 @@

if (run?.state !== 'running') {
lines.push(
`resume_hint: use ${toolName} with {"action":"run","id":"${task.id}","prompt":"next instruction"} to continue this session. Add "background":true when the work may take a while.`

Check warning on line 979 in packages/core/src/llm-core/agent/sub-agent.ts

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 190. Maximum allowed is 160
)
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/llm-core/agent/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,15 @@ export type AgentEvent =
}
| {
type: 'round-decision'
canContinue?: boolean
willConsumePendingMessages?: boolean
}
| {
type: 'done'
output: string
log: string
steps: AgentStep[]
message?: AIMessage
replyEmitted?: boolean
}

export interface AgentRuntimeConfigurable {
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ type ActiveRequest = {
abortController: AbortController
chatMode: string
messageQueue: MessageQueue
roundDecisionResolvers: ((canContinue: boolean) => void)[]
roundDecisionResolvers: ((willConsume: boolean) => void)[]
lastDecision?: boolean
}

Expand Down Expand Up @@ -1096,13 +1096,14 @@ class ChatInterfaceWrapper {
toolMask: mask,
onAgentEvent: async (agentEvent) => {
if (agentEvent.type === 'round-decision') {
activeRequest.lastDecision = agentEvent.canContinue
if (agentEvent.canContinue == null) {
activeRequest.lastDecision =
agentEvent.willConsumePendingMessages
if (agentEvent.willConsumePendingMessages == null) {
return
}

for (const resolve of activeRequest.roundDecisionResolvers) {
resolve(agentEvent.canContinue)
resolve(agentEvent.willConsumePendingMessages)
}
activeRequest.roundDecisionResolvers = []
}
Expand Down Expand Up @@ -1202,11 +1203,11 @@ class ChatInterfaceWrapper {
}

return new Promise((resolve) => {
activeRequest.roundDecisionResolvers.push((canContinue) => {
if (canContinue) {
activeRequest.roundDecisionResolvers.push((willConsume) => {
if (willConsume) {
activeRequest.messageQueue.push(message)
}
resolve(canContinue)
resolve(willConsume)
})
})
}
Expand Down
Loading