Skip to content

♻️ refactor(bot): migrate Bot service to Agent Runtime Hooks framework#13546

Merged
arvinxx merged 7 commits intocanaryfrom
feat/bot-hooks-migration
Apr 5, 2026
Merged

♻️ refactor(bot): migrate Bot service to Agent Runtime Hooks framework#13546
arvinxx merged 7 commits intocanaryfrom
feat/bot-hooks-migration

Conversation

@arvinxx
Copy link
Copy Markdown
Member

@arvinxx arvinxx commented Apr 3, 2026

Summary

  • Migrate Bot service (AgentBridgeService) from legacy completionWebhook/stepWebhook/stepCallbacks dual-track pattern to the unified hooks API
  • Enrich AgentHookEvent with step presentation data (content, toolsCalling, executionTimeMs, etc.) and cross-step tracking data (lastLLMContent, totalToolCalls, etc.)
  • Remove legacy webhook/callback infrastructure from AgentRuntimeService (~260 lines deleted)
  • This completes LOBE-6208 Step 4 — Bot was the last consumer still on the old pattern

Changes

File Change
hooks/types.ts Add step presentation fields to AgentHookEvent
AgentRuntimeService.ts Enrich afterStep dispatch, extend _stepTracking for hooks, remove triggerCompletionWebhook/triggerStepWebhook/stepCallbacks/deliverWebhook
types.ts Remove completionWebhook/stepWebhook/stepCallbacks/webhookDelivery from OperationCreationParams
aiAgent/index.ts Remove legacy params from InternalExecAgentParams and execAgent
AgentBridgeService.ts Merge executeWithWebhooks + executeWithInMemoryCallbacks into unified hooks pattern
BotCallbackService.ts Add hookId/hookType fields to BotCallbackBody

Test plan

  • bun run type-check — no new type errors
  • HookDispatcher.test.ts — 19/19 tests passing
  • Verify bot step progress updates work in local mode
  • Verify bot completion messages work in queue mode via QStash webhook delivery

Closes LOBE-6675

🤖 Generated with Claude Code

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lobehub Ready Ready Preview, Comment Apr 3, 2026 4:17pm

Request Review

@lobehubbot
Copy link
Copy Markdown
Member

@rdmclin2 @nekomeowww - This refactors the Bot service to use the unified Agent Runtime Hooks framework, with database and backend API changes. Please coordinate review.

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've reviewed this pull request using the Sourcery rules engine

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 3, 2026

Codecov Report

❌ Patch coverage is 35.47170% with 171 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.71%. Comparing base (dbdbe16) to head (243f24b).
⚠️ Report is 10 commits behind head on canary.

❗ There is a different number of reports uploaded between BASE (dbdbe16) and HEAD (243f24b). Click for more details.

HEAD has 13 uploads less than BASE
Flag BASE (dbdbe16) HEAD (243f24b)
packages/file-loaders 2 1
packages/prompts 2 1
packages/model-runtime 2 1
packages/web-crawler 2 1
packages/python-interpreter 2 1
packages/context-engine 2 1
packages/utils 2 1
packages/agent-runtime 2 1
packages/conversation-flow 2 1
packages/ssrf-safe-fetch 2 1
packages/model-bank 2 1
database 2 1
packages/memory-user-memory 2 1
Additional details and impacted files
@@             Coverage Diff             @@
##           canary   #13546       +/-   ##
===========================================
- Coverage   86.00%   66.71%   -19.30%     
===========================================
  Files         609     1996     +1387     
  Lines       49045   165500   +116455     
  Branches     7596    16832     +9236     
===========================================
+ Hits        42179   110406    +68227     
- Misses       6742    54970    +48228     
  Partials      124      124               
Flag Coverage Δ
app 58.59% <35.47%> (?)
database 92.57% <ø> (ø)
packages/agent-runtime 88.98% <ø> (ø)
packages/context-engine 85.39% <ø> (ø)
packages/conversation-flow 92.36% <ø> (ø)
packages/file-loaders 87.02% <ø> (ø)
packages/memory-user-memory 66.68% <ø> (ø)
packages/model-bank 99.85% <ø> (ø)
packages/model-runtime 84.66% <ø> (ø)
packages/prompts 65.80% <ø> (ø)
packages/python-interpreter 92.90% <ø> (ø)
packages/ssrf-safe-fetch 0.00% <ø> (ø)
packages/utils 90.02% <ø> (ø)
packages/web-crawler 88.82% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
Store 67.09% <ø> (∅)
Services 51.53% <ø> (∅)
Server 66.29% <35.47%> (∅)
Libs 51.03% <ø> (∅)
Utils 91.01% <ø> (-2.46%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@arvinxx arvinxx force-pushed the feat/bot-hooks-migration branch from 955d90d to 6d9968e Compare April 3, 2026 12:53
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 955d90d744

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 475 to 477
const reason = this.determineCompletionReason(agentState);

// Trigger completion callback so eval run can finalize properly
if (callbacks?.onComplete) {
try {
await callbacks.onComplete({
finalState: agentState,
operationId,
reason,
});
this.unregisterStepCallbacks(operationId);
} catch (callbackError) {
log('[%s] onComplete callback error: %O', operationId, callbackError);
}
}

return {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Dispatch completion hooks before early terminal return

In executeStep, the early-terminal branch (interrupted/done/error) returns immediately after computing reason and never calls dispatchCompletionHooks. This path is reachable when an operation is interrupted between queued steps (for example via /stop), so onComplete hooks/webhooks are skipped entirely. That breaks consumers that finalize on onComplete (e.g., bot local-mode promise resolution in executeWithHooksLocalMode) and can leave executions hanging until timeout or miss completion-side cleanup.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

@rdmclin2 rdmclin2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, looks good, check Codex's P1 Review


This review was translated by Claude.

Original Content

+1,看上去没啥问题,Codex 的 P1 Review 看下

arvinxx and others added 7 commits April 4, 2026 00:11
Migrate the last consumer (Bot/AgentBridgeService) from legacy
completionWebhook/stepWebhook/stepCallbacks dual-track pattern
to the unified hooks API. This completes LOBE-6208 Step 4.

- Enrich AgentHookEvent with step presentation + tracking data
- Enrich afterStep hook dispatch with full step context
- Merge executeWithWebhooks + executeWithInMemoryCallbacks into unified hooks
- Remove legacy triggerCompletionWebhook, triggerStepWebhook, stepCallbacks
- Remove completionWebhook/stepWebhook/webhookDelivery from params

LOBE-6675

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…fix totalToolCalls lag

- Add dispatchCompletionHooks in early-terminal branch of executeStep
  so onComplete hooks fire when operation is already interrupted/done/error
  between queued steps (e.g., via /stop)
- Include current step's toolsCalling in afterStep totalToolCalls so
  consumers get an accurate cumulative count instead of lagging by one step

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Rewrite executeStep tests to use hookDispatcher spies instead of
  removed registerStepCallbacks/getStepCallbacks API
- Rewrite completionWebhook tests to use hooks param and _hooks metadata
  instead of removed completionWebhook param
- Delete stepLifecycleCallbacks.test.ts (tests removed API, coverage
  now provided by HookDispatcher.test.ts + executeStep.test.ts)
- Update AgentRuntimeService.test.ts abort test to remove stepCallbacks

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Fix TS18048 errors: guard metadata access with null check in
  _stepTracking block
- Migrate remaining registerStepCallbacks usage in
  AgentRuntimeService.test.ts to hookDispatcher.dispatch spies:
  onComplete error tests and onAfterStep tool result extraction tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Old tests expected execAgent to NOT be called (because APP_URL check
would throw in queue mode). With hooks migration, the APP_URL check
is gone (hooks use relative URLs resolved by HookDispatcher), so
execAgent is now called. Update tests to verify hooks are passed
correctly instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add tests verifying that webhook payloads from HookDispatcher (containing
hookId/hookType fields) are correctly handled by BotCallbackService.
This validates the critical contract between the hooks framework and
the bot callback endpoint for step progress, completion, and error paths.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Add integration tests that verify the full executeStep → hookDispatcher
chain produces events with all fields bot consumers depend on:

- afterStep event includes content, stepType, totalTokens, executionTimeMs
- afterStep event includes cross-step tracking (lastLLMContent, totalToolCalls)
- afterStep event includes toolsResult for tool_result phases
- onComplete fires on early-terminal states (interrupted) with lastAssistantContent
- All RenderStepParams-required fields are present and correctly typed

These tests catch payload format regressions without needing production
infrastructure (Redis, QStash, real bot platforms).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@arvinxx arvinxx force-pushed the feat/bot-hooks-migration branch from f0a7a14 to 243f24b Compare April 3, 2026 16:11
@arvinxx arvinxx merged commit e9d43cb into canary Apr 5, 2026
35 checks passed
@arvinxx arvinxx deleted the feat/bot-hooks-migration branch April 5, 2026 14:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants