Skip to content

Conversation

@Ricky-Hao
Copy link
Contributor

@Ricky-Hao Ricky-Hao commented Jan 7, 2026

Previously, server-side SubAgent task execution did not respect the user's 'Enable Responses API' setting from provider configuration. This caused issues when using providers like NewAPI with LiteLLM backend, where the Responses API format is not supported.

The fix reads the user's provider config from the database and sets apiMode to 'chatCompletion' by default, only using 'responses' when the user has explicitly enabled it. This ensures SubAgent tasks use the same API format as regular chat.

💻 Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • ✅ test
  • 📝 docs
  • 🔨 chore

🔗 Related Issue

🔀 Description of Change

🧪 How to Test

  • Tested locally
  • Added/updated tests
  • No tests needed

📸 Screenshots / Videos

Before After
... ...

📝 Additional Information

Summary by Sourcery

Bug Fixes:

  • Respect the user’s Enable Responses API setting when determining apiMode for server-side SubAgent task execution so non-responses-compatible backends default to chat completion.

@vercel
Copy link

vercel bot commented Jan 7, 2026

@Ricky-Hao is attempting to deploy a commit to the LobeHub OSS Team on Vercel.

A member of the Team first needs to authorize it.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jan 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures server-side SubAgent task execution respects the user's provider configuration for Responses API by deriving an explicit apiMode from the stored provider config and passing it through to the chat payload.

Sequence diagram for server-side SubAgent task execution respecting enableResponseApi

sequenceDiagram
  actor SubAgent
  participant RuntimeExecutors
  participant AiProviderModel
  participant ServerDB
  participant ModelRuntime
  participant ProviderAPI

  SubAgent->>RuntimeExecutors: executeSubAgentTask(provider, llmPayload)

  RuntimeExecutors->>ModelRuntime: initModelRuntimeFromDB(ServerDB, userId, provider)
  ModelRuntime-->>RuntimeExecutors: modelRuntime

  RuntimeExecutors->>AiProviderModel: constructor(ServerDB, userId)
  RuntimeExecutors->>AiProviderModel: findById(provider)
  AiProviderModel->>ServerDB: query provider config
  ServerDB-->>AiProviderModel: providerConfig(config.enableResponseApi)
  AiProviderModel-->>RuntimeExecutors: providerConfig

  RuntimeExecutors->>RuntimeExecutors: determine apiMode
  Note over RuntimeExecutors: apiMode = responses if enableResponseApi === true
  Note over RuntimeExecutors: apiMode = chatCompletion otherwise

  RuntimeExecutors->>RuntimeExecutors: build chatPayload(apiMode, messages, model, tools)

  RuntimeExecutors->>ProviderAPI: send chatPayload
  ProviderAPI-->>RuntimeExecutors: response
  RuntimeExecutors-->>SubAgent: task result
Loading

Flow diagram for apiMode selection based on enableResponseApi

flowchart TD
  A[Start SubAgent task execution] --> B[Load providerConfig from database]
  B --> C{enableResponseApi === true?}
  C -- Yes --> D[Set apiMode to responses]
  C -- No --> E[Set apiMode to chatCompletion]
  D --> F[Construct chatPayload with apiMode]
  E --> F[Construct chatPayload with apiMode]
  F --> G[Send chatPayload to provider]
  G --> H[End]
Loading

File-Level Changes

Change Details Files
Derive apiMode from the user’s provider configuration and include it in the SubAgent chat payload so Responses API is only used when explicitly enabled.
  • Initialize AiProviderModel with the current server DB and user ID to load provider configuration by provider ID.
  • Read enableResponseApi from the provider config and compute apiMode as 'responses' only when enableResponseApi is explicitly true, otherwise default to 'chatCompletion'.
  • Extend the constructed chat payload for SubAgent execution to include the computed apiMode field.
src/server/modules/AgentRuntime/RuntimeExecutors.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jan 7, 2026
@gru-agent
Copy link
Contributor

gru-agent bot commented Jan 7, 2026

TestGru Assignment

Summary

Link CommitId Status Reason
Detail ecee465 ✅ Finished

History Assignment

Files

File Pull Request
src/server/modules/AgentRuntime/RuntimeExecutors.ts ❌ Failed (I failed to setup the environment.)

Tip

You can @gru-agent and leave your feedback. TestGru will make adjustments based on your input

Copy link
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.

Hey - I've found 2 issues, and left some high level feedback:

  • The comment about defaulting to false for non-OpenAI providers no longer matches the implementation (which defaults to chatCompletion for all providers unless explicitly enabled), so consider updating or removing it to avoid confusion.
  • Since the provider config is now fetched here in addition to initModelRuntimeFromDB, consider centralizing the apiMode derivation in a shared helper (or exposing it from initModelRuntimeFromDB) to avoid duplicated provider-configuration logic.
  • It might be worth handling or logging the case where providerConfig is unexpectedly missing so that misconfigured providers are easier to diagnose rather than silently falling back to chatCompletion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The comment about defaulting to false for non-OpenAI providers no longer matches the implementation (which defaults to chatCompletion for all providers unless explicitly enabled), so consider updating or removing it to avoid confusion.
- Since the provider config is now fetched here in addition to initModelRuntimeFromDB, consider centralizing the apiMode derivation in a shared helper (or exposing it from initModelRuntimeFromDB) to avoid duplicated provider-configuration logic.
- It might be worth handling or logging the case where providerConfig is unexpectedly missing so that misconfigured providers are easier to diagnose rather than silently falling back to chatCompletion.

## Individual Comments

### Comment 1
<location> `src/server/modules/AgentRuntime/RuntimeExecutors.ts:132-134` </location>
<code_context>
+      // When user explicitly disables Responses API, set apiMode to 'chatCompletion'
+      const aiProviderModel = new AiProviderModel(ctx.serverDB, ctx.userId!);
+      const providerConfig = await aiProviderModel.findById(provider);
+      const enableResponseApi = providerConfig?.config?.enableResponseApi;
+      // Default to false for non-OpenAI providers, true only for OpenAI with explicit enable
+      const apiMode: 'responses' | 'chatCompletion' =
+        enableResponseApi === true ? 'responses' : 'chatCompletion';
+
</code_context>

<issue_to_address>
**issue:** Align the comment about non-OpenAI defaults with the actual apiMode computation.

The logic currently enables `responses` only when `enableResponseApi === true`, regardless of provider. There’s no OpenAI vs non‑OpenAI distinction in the code, so the comment describing different defaults per provider is misleading. Please either adjust the comment to match this behavior or add the missing provider check if that behavior is intended.
</issue_to_address>

### Comment 2
<location> `src/server/modules/AgentRuntime/RuntimeExecutors.ts:130-131` </location>
<code_context>

+      // Read user's provider config to determine apiMode
+      // When user explicitly disables Responses API, set apiMode to 'chatCompletion'
+      const aiProviderModel = new AiProviderModel(ctx.serverDB, ctx.userId!);
+      const providerConfig = await aiProviderModel.findById(provider);
+      const enableResponseApi = providerConfig?.config?.enableResponseApi;
+      // Default to false for non-OpenAI providers, true only for OpenAI with explicit enable
</code_context>

<issue_to_address>
**suggestion (performance):** Consider avoiding an extra DB round-trip for providerConfig on every execution.

This adds a second DB query via `AiProviderModel.findById` immediately after `initModelRuntimeFromDB`. If `initModelRuntimeFromDB` already loads (or can be extended to load) provider config, we could derive `apiMode` from that data instead and avoid the extra query on the hot path, reducing latency and DB load for frequent requests.

Suggested implementation:

```typescript
import { type MessageModel } from '@/database/models/message';
import { type LobeChatDatabase } from '@/database/type';
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
      // 初始化 ModelRuntime (从数据库读取用户的 keyVaults)
      const modelRuntime = await initModelRuntimeFromDB(ctx.serverDB, ctx.userId!, provider);

      // Read user's provider config (already loaded by initModelRuntimeFromDB) to determine apiMode
      // When user explicitly disables Responses API, set apiMode to 'chatCompletion'
      const enableResponseApi = modelRuntime.providerConfig?.config?.enableResponseApi;
      // Default to false for non-OpenAI providers, true only for OpenAI with explicit enable
      const apiMode: 'responses' | 'chatCompletion' =
        enableResponseApi === true ? 'responses' : 'chatCompletion';

      // 构造 ChatStreamPayload
      const chatPayload = {

```

To fully implement this optimization, you also need to:

1. Update `initModelRuntimeFromDB` (in `src/server/modules/ModelRuntime.ts` or its actual location) so that the returned `modelRuntime` instance includes the provider configuration:
   - Add a `providerConfig` (or similarly named) property to the `ModelRuntime` type/interface.
   - Inside `initModelRuntimeFromDB`, load the provider config (using `AiProviderModel` or equivalent) and assign it to `modelRuntime.providerConfig`.
2. Ensure all usages of `initModelRuntimeFromDB` are compatible with the enhanced return type (they can ignore `providerConfig` if not needed).
3. If `AiProviderModel` is still used elsewhere in `RuntimeExecutors.ts`, keep its import and only remove the now-unused instantiation and `findById` call; otherwise the import removal above is correct.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +130 to +131
const aiProviderModel = new AiProviderModel(ctx.serverDB, ctx.userId!);
const providerConfig = await aiProviderModel.findById(provider);
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (performance): Consider avoiding an extra DB round-trip for providerConfig on every execution.

This adds a second DB query via AiProviderModel.findById immediately after initModelRuntimeFromDB. If initModelRuntimeFromDB already loads (or can be extended to load) provider config, we could derive apiMode from that data instead and avoid the extra query on the hot path, reducing latency and DB load for frequent requests.

Suggested implementation:

import { type MessageModel } from '@/database/models/message';
import { type LobeChatDatabase } from '@/database/type';
import { initModelRuntimeFromDB } from '@/server/modules/ModelRuntime';
      // 初始化 ModelRuntime (从数据库读取用户的 keyVaults)
      const modelRuntime = await initModelRuntimeFromDB(ctx.serverDB, ctx.userId!, provider);

      // Read user's provider config (already loaded by initModelRuntimeFromDB) to determine apiMode
      // When user explicitly disables Responses API, set apiMode to 'chatCompletion'
      const enableResponseApi = modelRuntime.providerConfig?.config?.enableResponseApi;
      // Default to false for non-OpenAI providers, true only for OpenAI with explicit enable
      const apiMode: 'responses' | 'chatCompletion' =
        enableResponseApi === true ? 'responses' : 'chatCompletion';

      // 构造 ChatStreamPayload
      const chatPayload = {

To fully implement this optimization, you also need to:

  1. Update initModelRuntimeFromDB (in src/server/modules/ModelRuntime.ts or its actual location) so that the returned modelRuntime instance includes the provider configuration:
    • Add a providerConfig (or similarly named) property to the ModelRuntime type/interface.
    • Inside initModelRuntimeFromDB, load the provider config (using AiProviderModel or equivalent) and assign it to modelRuntime.providerConfig.
  2. Ensure all usages of initModelRuntimeFromDB are compatible with the enhanced return type (they can ignore providerConfig if not needed).
  3. If AiProviderModel is still used elsewhere in RuntimeExecutors.ts, keep its import and only remove the now-unused instantiation and findById call; otherwise the import removal above is correct.

@sxjeru
Copy link
Contributor

sxjeru commented Jan 7, 2026

Does this PR fix the issue: #10606


This comment was translated by Claude.

Original Content 这个 pr 是否修复了该问题:https://github.com//pull/10606

@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.40%. Comparing base (c0d165e) to head (cce7637).
⚠️ Report is 2 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff            @@
##             next   #11320     +/-   ##
=========================================
  Coverage   76.40%   76.40%             
=========================================
  Files        1134     1134             
  Lines       87549    87556      +7     
  Branches    11796     9854   -1942     
=========================================
+ Hits        66892    66899      +7     
  Misses      20581    20581             
  Partials       76       76             
Flag Coverage Δ
app 70.18% <100.00%> (+<0.01%) ⬆️
database 94.08% <ø> (ø)
packages/agent-runtime 89.08% <ø> (ø)
packages/context-engine 83.19% <ø> (ø)
packages/conversation-flow 92.41% <ø> (ø)
packages/file-loaders 88.66% <ø> (ø)
packages/memory-user-memory 70.29% <ø> (ø)
packages/model-bank 100.00% <ø> (ø)
packages/model-runtime 86.88% <ø> (ø)
packages/prompts 76.00% <ø> (ø)
packages/python-interpreter 92.90% <ø> (ø)
packages/ssrf-safe-fetch 0.00% <ø> (ø)
packages/utils 92.88% <ø> (ø)
packages/web-crawler 95.62% <ø> (ø)

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

Components Coverage Δ
Store 68.52% <ø> (ø)
Services 54.01% <ø> (ø)
Server 73.64% <100.00%> (+<0.01%) ⬆️
Libs 41.53% <ø> (ø)
Utils 94.40% <ø> (ø)
🚀 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.

…nt task execution

Previously, server-side SubAgent task execution did not respect the user's
'Enable Responses API' setting from provider configuration. This caused
issues when using providers like NewAPI with LiteLLM backend, where the
Responses API format is not supported.

The fix reads the user's provider config from the database and sets
apiMode to 'chatCompletion' by default, only using 'responses' when the
user has explicitly enabled it. This ensures SubAgent tasks use the same
API format as regular chat.
- Add vi.mock for AiProviderModel in RuntimeExecutors tests to fix test failures
- Fix misleading comment about OpenAI vs non-OpenAI provider distinction
@tjx666 tjx666 force-pushed the fix/subagent-response-api branch from 6aac83b to cce7637 Compare January 8, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants