Repository navigation
feat(deepseek): use native tool calling in agent mode #2741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zhouyu123666
merged 12 commits into
chaterm:main
from
lijinxiao1982:feat/deepseek-native-tools
Dec 4, 2026
+423
−10
Merged
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3799bb6
feat(deepseek): use native tool calling in agent mode
lijinxiao1982 cb8e3cc
chore(deepseek): log native tool mode and enforce one tool per turn
lijinxiao1982 3a55773
fix(deepseek): address native tool calling review feedback
lijinxiao1982 98844d6
fix(deepseek): force native tool calls in agent mode
lijinxiao1982 d8d2d21
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 ecd9aac
fix(deepseek): carry native tool provenance out of band
zhouyu123666 439012b
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 b69f055
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 87bb730
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 b7948fe
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 a01fc8a
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 87edd88
Merge branch 'main' into feat/deepseek-native-tools
zhouyu123666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/main/agent/api/providers/__tests__/deepseek.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { createMock } = vi.hoisted(() => ({ createMock: vi.fn() })) | ||
|
|
||
| vi.mock('openai', () => ({ | ||
| default: class { | ||
| chat = { completions: { create: createMock } } | ||
| } | ||
| })) | ||
|
|
||
| import { parseAssistantMessageV2 } from '../../../core/assistant-message' | ||
| import { DeepSeekHandler, buildNativeToolXml, buildNativeTools } from '../deepseek' | ||
|
|
||
| function streamEvents() { | ||
| return (async function* () { | ||
| yield { choices: [{ delta: { content: 'Checking.' } }] } | ||
| yield { | ||
| choices: [ | ||
| { | ||
| delta: { | ||
| tool_calls: [ | ||
| { | ||
| index: 0, | ||
| id: 'call_1', | ||
| function: { name: 'execute_command', arguments: '{"command":"ls' } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| yield { | ||
| choices: [ | ||
| { | ||
| delta: { | ||
| tool_calls: [{ index: 0, function: { arguments: ' -la","ip":"10.0.0.1"}' } }] | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| yield { choices: [{ delta: {}, finish_reason: 'tool_calls' }] } | ||
| })() | ||
| } | ||
|
|
||
| async function collect(handler: DeepSeekHandler) { | ||
| const chunks: any[] = [] | ||
| for await (const chunk of handler.createMessage('<execute_command>legacy docs</execute_command>', [ | ||
| { role: 'user', content: [{ type: 'text', text: 'run it' }] } | ||
| ])) { | ||
| chunks.push(chunk) | ||
| } | ||
| return chunks | ||
| } | ||
|
|
||
| describe('DeepSeek native tool calls', () => { | ||
| it('passes native tools and converts streamed tool calls into XML', async () => { | ||
| createMock.mockReset() | ||
| createMock.mockResolvedValue(streamEvents()) | ||
|
|
||
| const handler = new DeepSeekHandler({ | ||
| apiModelId: 'deepseek-flash', | ||
| deepSeekApiKey: 'test-key', | ||
| useNativeTools: true, | ||
| workspace: 'server' | ||
| }) | ||
| const chunks = await collect(handler) | ||
|
|
||
| const request = createMock.mock.calls[0][0] | ||
| expect(request.tools?.length).toBeGreaterThan(0) | ||
| expect(request.tool_choice).toBe('required') | ||
| expect(request.thinking).toEqual({ type: 'disabled' }) | ||
| expect(request.messages[0].content).toContain('Native tool calling') | ||
|
|
||
| const textChunks = chunks.filter((chunk) => chunk.type === 'text') | ||
| expect(textChunks).toHaveLength(1) | ||
| expect(textChunks[0].nativeToolCall).toBe(true) | ||
| const text = textChunks[0].text | ||
| expect(text).not.toContain('Checking.') | ||
|
|
||
| const blocks = parseAssistantMessageV2(text, { nativeToolCall: true }) | ||
| expect(blocks).toContainEqual({ | ||
| type: 'tool_use', | ||
| name: 'execute_command', | ||
| params: { command: 'ls -la', ip: '10.0.0.1' }, | ||
| partial: false | ||
| }) | ||
| }) | ||
|
|
||
| it('keeps the XML path when native tools are disabled', async () => { | ||
| createMock.mockReset() | ||
| createMock.mockResolvedValue( | ||
| (async function* () { | ||
| yield { choices: [{ delta: { content: 'plain answer' } }] } | ||
| })() | ||
| ) | ||
|
|
||
| const handler = new DeepSeekHandler({ | ||
| apiModelId: 'deepseek-flash', | ||
| deepSeekApiKey: 'test-key', | ||
| useNativeTools: false | ||
| }) | ||
| await collect(handler) | ||
|
|
||
| const request = createMock.mock.calls[0][0] | ||
| expect(request.tools).toBeUndefined() | ||
| expect(request.messages[0].content).not.toContain('Native tool calling') | ||
| }) | ||
|
|
||
| it('filters native tool schemas by workspace', () => { | ||
| const serverTools = buildNativeTools('server').flatMap((tool) => ('function' in tool ? [tool.function.name] : [])) | ||
| expect(serverTools).toContain('execute_command') | ||
| expect(serverTools).not.toContain('list_databases') | ||
| }) | ||
|
|
||
| it('drops arguments that are not registered for the tool', () => { | ||
| const xml = buildNativeToolXml({ | ||
| id: 'call_2', | ||
| name: 'execute_command', | ||
| arguments: '{"command":"echo <x>","not_a_param":"ignored"}' | ||
| }) | ||
|
|
||
| expect(xml).toContain('<command>') | ||
| expect(xml).not.toContain('not_a_param') | ||
| expect(parseAssistantMessageV2(xml, { nativeToolCall: true })).toEqual([ | ||
| { | ||
| type: 'tool_use', | ||
| name: 'execute_command', | ||
| params: { command: 'echo <x>' }, | ||
| partial: false | ||
| } | ||
| ]) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Native tool mode breaks the auxiliary LLM requests of agent-mode DeepSeek tasks.
When
useNativeToolsis set, DeepSeek requests force a tool call, disable thinking, and suppress text content. Interaction detection and experience extraction use the samethis.apifor their requests and have no per-request override. Interaction detection requires a JSON object in the response, so it fails withNo JSON found in responsewhen the reply is only the synthetic XML or empty. The experience completion can receive the same XML or empty reply instead of the response its prompt requests.Auxiliary requests must receive their own requested response format. Keep native tool calling limited to the agent turn loop so that auxiliary requests do not get
tools,tool_choice, or suppressed content.