Skip to content

♻️ refactor: implement SkillResolver, BaseSystemRoleProvider, and agent document injection pipeline#13315

Merged
arvinxx merged 19 commits into
canaryfrom
refactor/skill-resolver
Mar 27, 2026
Merged

♻️ refactor: implement SkillResolver, BaseSystemRoleProvider, and agent document injection pipeline#13315
arvinxx merged 19 commits into
canaryfrom
refactor/skill-resolver

Conversation

@arvinxx

@arvinxx arvinxx commented Mar 26, 2026

Copy link
Copy Markdown
Member

Summary

  • Implements two-layer skill resolution architecture mirroring ToolsEngine + ToolResolver
  • Creates BaseSystemRoleProvider base class to unify system message assembly
  • Splits AgentDocumentInjector into 5 position-based injectors for correct pipeline placement
  • Extracts @lobechat/agent-templates package for agent document templates
  • Adds server-side agent document injection in RuntimeExecutors (was missing)
  • Auto-enables lobe-agent-documents tool when agent has documents
  • Adds BOOTSTRAP.md template for first-run agent onboarding

Key Changes

SkillResolver

  • Add SkillResolver, OperationSkillSet, StepSkillDelta, ActivatedStepSkill types in @lobechat/context-engine
  • Enhance SkillEngine with enableChecker and generate() method
  • Wire SkillResolver into RuntimeExecutors call_llm
  • Replace manual skillMetas assembly in aiAgent with SkillEngine.generate()
  • Fixes agent-browser content (~1.5k tokens) being injected into non-desktop scenarios

BaseSystemRoleProvider

  • New base class for all providers that append to system message
  • Subclasses only implement buildSystemRoleContent() + optional onInjected() for metadata
  • Migrated: EvalContext, BotPlatformContext, SystemDate, ToolSystemRole, HistorySummary, SkillContext

Agent Document Injection Pipeline

  • Split monolithic AgentDocumentInjector into 5 injectors:
    • AgentDocumentBeforeSystemInjector (before-system) → Phase 2 start
    • AgentDocumentSystemAppendInjector (system-append) → Phase 2 end
    • AgentDocumentSystemReplaceInjector (system-replace) → Phase 2 end
    • AgentDocumentContextInjector (before-first-user) → Phase 3
    • AgentDocumentMessageInjector (after-first-user, context-end) → Phase 4
  • Server-side RuntimeExecutors now fetches and injects agent documents (was missing)
  • Result: single system message instead of multiple, correct injection ordering

MessagesEngine Pipeline Reorganization

  • 7 phases with clear responsibilities:
    1. History Truncation
    2. System Message Assembly (all BaseSystemRoleProvider)
    3. Context Injection (before first user message)
    4. User Message Augmentation
    5. Message Transformation
    6. Content Processing
    7. Cleanup
  • All providers use enabled field — no spread conditionals in pipeline

@lobechat/agent-templates Package

  • Extracted from @lobechat/database into standalone package
  • Contains types (DocumentLoadPosition, DocumentLoadFormat, etc.), templates, and registry
  • Claw templates use .md file imports for content
  • Added BOOTSTRAP.md onboarding template
  • Fixed template positions: AGENTS→before-system, IDENTITY/SOUL→system-append

Auto-enable Agent Documents Tool

  • Added AgentDocumentsManifest to defaultToolIds
  • Server createServerAgentToolsEngine enables tool via hasAgentDocuments rule
  • AiAgentService queries agent documents to determine flag

Fixes LOBE-6410

Test plan

  • context-engine: 717 tests passing (51 files)
  • database: 1833 tests passing
  • SkillResolver + SkillEngine unit tests
  • AgentDocumentInjector tests updated for split injectors
  • Verified single system message via agent tracing
  • Verified agent documents tool auto-enabled and functional
  • Verify Discord bot no longer receives agent-browser prompt injection
  • Verify desktop agent-browser skill still works when enabled

🤖 Generated with Claude Code

@vercel

vercel Bot commented Mar 26, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lobehub Ready Ready Preview, Comment Mar 27, 2026 2:00am

Request Review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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

@lobehubbot

Copy link
Copy Markdown
Member

@nekomeowww - This is a backend/agent-runtime refactor implementing SkillResolver architecture. Please take a look.

@codecov

codecov Bot commented Mar 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.12522% with 73 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.83%. Comparing base (926de07) to head (a0d2e2f).
⚠️ Report is 6 commits behind head on canary.

Additional details and impacted files
@@            Coverage Diff             @@
##           canary   #13315      +/-   ##
==========================================
+ Coverage   66.76%   66.83%   +0.07%     
==========================================
  Files        1889     1891       +2     
  Lines      151446   151326     -120     
  Branches    14506    14520      +14     
==========================================
+ Hits       101112   101142      +30     
+ Misses      50222    50065     -157     
- Partials      112      119       +7     
Flag Coverage Δ
app 58.22% <78.57%> (+0.01%) ⬆️
database 96.66% <100.00%> (+0.01%) ⬆️
packages/agent-runtime 89.61% <ø> (ø)
packages/context-engine 86.47% <88.88%> (+3.24%) ⬆️
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.48% <ø> (ø)
packages/prompts 67.76% <ø> (ø)
packages/python-interpreter 92.90% <ø> (ø)
packages/ssrf-safe-fetch 0.00% <ø> (ø)
packages/utils 90.41% <ø> (ø)
packages/web-crawler 88.82% <ø> (ø)

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

Components Coverage Δ
Store 66.07% <ø> (ø)
Services 49.58% <100.00%> (+0.01%) ⬆️
Server 67.38% <75.29%> (+0.02%) ⬆️
Libs 51.06% <ø> (ø)
Utils 91.01% <ø> (ø)
🚀 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.

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

Copy link
Copy Markdown

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: 17c299894f

ℹ️ 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 859 to 860
identifier: s.identifier,
name: s.name,

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 Include DB skill content in operation skill metadata

This refactor now auto-activates any skill whose identifier is in enabledPluginIds, but DB skills are still emitted without content. When a user/market skill is enabled in agentConfig.plugins, SkillResolver will mark it activated; then SkillContextProvider drops it from <available_skills> (because it is activated) and also cannot inject it (because content is missing), so the skill disappears from the prompt and becomes unusable. Add s.content here (or avoid activating skills that have no content).

Useful? React with 👍 / 👎.

arvinxx and others added 3 commits March 27, 2026 00:52
Introduces a two-layer skill resolution architecture mirroring ToolsEngine + ToolResolver:

- SkillEngine (assembly layer): accepts raw skills + enableChecker, outputs OperationSkillSet
- SkillResolver (resolution layer): merges operation + step delta + accumulated activations

Key changes:
- Add SkillResolver, OperationSkillSet, StepSkillDelta, ActivatedStepSkill types
- Enhance SkillEngine with enableChecker and generate() method
- Wire SkillResolver into RuntimeExecutors call_llm
- Replace manual skillMetas assembly in aiAgent with SkillEngine.generate()
- Update client-side skillEngineering to use SkillEngine + enableChecker
- Add activatedStepSkills to AgentState for step-level skill accumulation

Fixes: agent-browser content injected into non-desktop scenarios (Discord bot)
due to missing filterBuiltinSkills call in aiAgent

LOBE-6410

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… documents server-side

- Create @lobechat/agent-templates package with types, templates, and registry
- Move DocumentLoadPosition, DocumentLoadFormat, DocumentLoadRule, etc. to new package
- Move claw templates (AGENTS, BOOTSTRAP, IDENTITY, SOUL) with .md file imports
- Add BOOTSTRAP.md as new onboarding template (priority 1, system-append)
- Fix template positions: AGENTS→before-system, IDENTITY/SOUL→system-append
- Update database package to re-export from @lobechat/agent-templates
- Migrate all consumers to import directly from @lobechat/agent-templates
- Add agent documents injection in server-side RuntimeExecutors (was missing)
- Support -p CLI flag in devStartupSequence for port configuration

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

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@arvinxx arvinxx force-pushed the refactor/skill-resolver branch from 8572407 to eab8756 Compare March 26, 2026 16:52
arvinxx and others added 2 commits March 27, 2026 01:01
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Stop re-exporting template/templates from database — consumers import
directly from @lobechat/agent-templates. Keep types.ts re-exports for
internal database code only.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
arvinxx and others added 9 commits March 27, 2026 01:32
…pend pattern

All providers that append to the system message now inherit from
BaseSystemRoleProvider and only implement buildSystemRoleContent().
The base class handles find-or-create and join logic.

Migrated providers:
- EvalContextSystemInjector
- BotPlatformContextInjector
- SystemDateProvider
- ToolSystemRoleProvider
- HistorySummaryProvider
- SkillContextProvider

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

Add onInjected() callback to BaseSystemRoleProvider so subclasses can
update pipeline metadata after successful injection. Also add raw-md
plugin to context-engine vitest config for .md imports.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ionals in MessagesEngine

All providers now accept an `enabled` config field. MessagesEngine
pipeline is a flat array with no spread conditionals — each provider
is always instantiated and uses `enabled` to skip internally.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Remove numbered prefixes, keep descriptive comments for each provider.
Only phase headers use separator blocks.

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

Phase 1: History Truncation
Phase 2: System Message Assembly (all BaseSystemRoleProvider)
Phase 3: Context Injection (before first user message, BaseFirstUserContentProvider)
Phase 4: User Message Augmentation (last user message injections)
Phase 5: Message Transformation (flatten, template, variables)
Phase 6: Content Processing & Cleanup (multimodal, tool calls, cleanup)

Moved SkillContext, ToolSystemRole, HistorySummary from Phase 3 to
Phase 2 since they append to system message, not user context.

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

- AgentDocumentSystemInjector (Phase 2): before-system, system-append, system-replace
- AgentDocumentContextInjector (Phase 3): before-first-user
- AgentDocumentMessageInjector (Phase 4): after-first-user, context-end

Shared utilities (filterByRules, formatDocument, sortByPriority) extracted
to AgentDocumentInjector/shared.ts. Old monolithic injector removed.

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

- AgentDocumentBeforeSystemInjector: prepends as separate system message (before-system)
- AgentDocumentSystemAppendInjector: appends to system message (system-append)
- AgentDocumentSystemReplaceInjector: replaces entire system message (system-replace)

Each has distinct semantics and correct pipeline placement:
- BeforeSystem → before SystemRoleInjector
- SystemAppend → after HistorySummary (end of Phase 2)
- SystemReplace → last in Phase 2 (destructive)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
arvinxx and others added 3 commits March 27, 2026 02:37
- Add AgentDocumentsManifest to defaultToolIds
- Add hasAgentDocuments rule in server createServerAgentToolsEngine
- Query agent documents in AiAgentService.execAgent to determine flag
- Pattern matches KnowledgeBase auto-enable via enableChecker rules

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@arvinxx arvinxx changed the title ♻️ refactor: implement SkillResolver to replace ad-hoc skill assembly ♻️ refactor: implement SkillResolver, BaseSystemRoleProvider, and agent document injection pipeline Mar 27, 2026
@arvinxx arvinxx merged commit 4576059 into canary Mar 27, 2026
34 checks passed
@arvinxx arvinxx deleted the refactor/skill-resolver branch March 27, 2026 02:10
@lobehubbot

Copy link
Copy Markdown
Member

❤️ Great PR @arvinxx ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.

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.

2 participants