-
Notifications
You must be signed in to change notification settings - Fork 15.6k
feat: 添加 Ollama 原生供应商适配与审批模式选择 #409
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
Open
pei-lin-001
wants to merge
2
commits into
claude-code-best:main
Choose a base branch
from
pei-lin-001:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Ollama Native Provider | ||
|
|
||
| Claude Code Best supports Ollama through the native Ollama API, not the | ||
| OpenAI-compatible endpoint. This lets Cloud and local Ollama use the same | ||
| request shape for chat, tool calling, thinking, model discovery, and web | ||
| utilities. | ||
|
|
||
| ## Configure | ||
|
|
||
| Use `/login` and choose `Ollama`, or configure these environment variables: | ||
|
|
||
| ```bash | ||
| CLAUDE_CODE_USE_OLLAMA=1 | ||
| OLLAMA_API_KEY=ollama_api_key | ||
| OLLAMA_BASE_URL=https://ollama.com/api | ||
| OLLAMA_DEFAULT_HAIKU_MODEL=qwen3:cloud | ||
| OLLAMA_DEFAULT_SONNET_MODEL=qwen3-coder | ||
| OLLAMA_DEFAULT_OPUS_MODEL=glm-4.7:cloud | ||
| ``` | ||
|
|
||
| `OLLAMA_API_KEY` is required for direct Ollama Cloud API access. It is not | ||
| required for local Ollama. For local Ollama, set: | ||
|
|
||
| ```bash | ||
| OLLAMA_BASE_URL=http://localhost:11434/api | ||
| ``` | ||
|
|
||
| If `OLLAMA_BASE_URL` is omitted, Claude Code Best uses | ||
| `https://ollama.com/api`. | ||
|
|
||
| ## Model Mapping | ||
|
|
||
| Ollama model routing uses the same three Claude model families shown by | ||
| `/model`: | ||
|
|
||
| - `OLLAMA_DEFAULT_HAIKU_MODEL` | ||
| - `OLLAMA_DEFAULT_SONNET_MODEL` | ||
| - `OLLAMA_DEFAULT_OPUS_MODEL` | ||
|
|
||
| There is no global `OLLAMA_MODEL` override. This keeps Ollama behavior aligned | ||
| with other third-party providers, where Haiku/Sonnet/Opus can map to different | ||
| backend models. | ||
|
|
||
| When a direct Ollama model name is selected from `/model` or `--model`, it is | ||
| sent to Ollama unchanged. When a Claude family model is selected, Claude Code | ||
| Best maps it through the matching `OLLAMA_DEFAULT_*_MODEL` variable. If no | ||
| family mapping is configured, the fallback is `qwen3-coder`. | ||
|
|
||
| ## Supported Features | ||
|
|
||
| - Native `POST /api/chat` streaming | ||
| - Ollama tool calling through `tools` | ||
| - Ollama thinking through `think` | ||
| - Native `POST /api/web_search` | ||
| - Native `POST /api/web_fetch` | ||
| - Dynamic context length discovery through `POST /api/show` | ||
| - Local Ollama and Ollama Cloud through the same provider | ||
|
|
||
| The provider reads model context length from `model_info.*.context_length` or | ||
| the `num_ctx` parameter returned by `/api/show`, then uses that value to choose | ||
| the request output limit. | ||
|
|
||
| ## Known Limits | ||
|
|
||
| Ollama does not expose an official Cloud quota or remaining-balance API in the | ||
| documented native API. Claude Code Best therefore does not show Ollama Cloud | ||
| remaining quota. | ||
|
|
||
| Anthropic-only server tools are not sent directly to Ollama. Web search and web | ||
| fetch are handled client-side through Ollama's native web APIs when the Ollama | ||
| provider is active. |
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
67 changes: 67 additions & 0 deletions
67
packages/@ant/model-provider/src/providers/ollama/__tests__/convertMessages.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,67 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { anthropicMessagesToOllama } from '../convertMessages.js' | ||
| import type { SystemPrompt } from '../../../types/systemPrompt.js' | ||
|
|
||
| describe('anthropicMessagesToOllama', () => { | ||
| test('converts system, text, tool use, and tool result messages', () => { | ||
| const result = anthropicMessagesToOllama( | ||
| [ | ||
| { | ||
| type: 'user', | ||
| message: { | ||
| role: 'user', | ||
| content: [{ type: 'text', text: 'weather?' }], | ||
| }, | ||
| } as any, | ||
| { | ||
| type: 'assistant', | ||
| message: { | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_1', | ||
| name: 'get_weather', | ||
| input: { city: 'Paris' }, | ||
| }, | ||
| ], | ||
| }, | ||
| } as any, | ||
| { | ||
| type: 'user', | ||
| message: { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'tool_result', | ||
| tool_use_id: 'toolu_1', | ||
| content: 'sunny', | ||
| }, | ||
| ], | ||
| }, | ||
| } as any, | ||
| ], | ||
| ['You are concise.'] as unknown as SystemPrompt, | ||
| ) | ||
|
|
||
| expect(result).toEqual([ | ||
| { role: 'system', content: 'You are concise.' }, | ||
| { role: 'user', content: 'weather?' }, | ||
| { | ||
| role: 'assistant', | ||
| content: '', | ||
| tool_calls: [ | ||
| { | ||
| type: 'function', | ||
| function: { | ||
| index: 0, | ||
| name: 'get_weather', | ||
| arguments: { city: 'Paris' }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { role: 'tool', tool_name: 'get_weather', content: 'sunny' }, | ||
| ]) | ||
| }) | ||
| }) |
119 changes: 119 additions & 0 deletions
119
packages/@ant/model-provider/src/providers/ollama/__tests__/convertTools.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,119 @@ | ||
| import { describe, expect, test } from 'bun:test' | ||
| import { anthropicToolsToOllama } from '../convertTools.js' | ||
|
|
||
| describe('anthropicToolsToOllama', () => { | ||
| test('converts basic tools to Ollama function tools', () => { | ||
| const tools = [ | ||
| { | ||
| type: 'custom', | ||
| name: 'bash', | ||
| description: 'Run a bash command', | ||
| input_schema: { | ||
| type: 'object', | ||
| properties: { command: { type: 'string' } }, | ||
| required: ['command'], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| expect(anthropicToolsToOllama(tools as any)).toEqual([ | ||
| { | ||
| type: 'function', | ||
| function: { | ||
| name: 'bash', | ||
| description: 'Run a bash command', | ||
| parameters: { | ||
| type: 'object', | ||
| properties: { command: { type: 'string' } }, | ||
| required: ['command'], | ||
| }, | ||
| }, | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| test('keeps WebFetch parameters in Ollama-compatible schema subset', () => { | ||
| const tools = [ | ||
| { | ||
| type: 'custom', | ||
| name: 'WebFetch', | ||
| description: 'Fetch a URL', | ||
| input_schema: { | ||
| $schema: 'https://json-schema.org/draft/2020-12/schema', | ||
| type: 'object', | ||
| properties: { | ||
| url: { | ||
| type: 'string', | ||
| format: 'uri', | ||
| description: 'The URL to fetch content from', | ||
| }, | ||
| prompt: { | ||
| type: 'string', | ||
| description: 'The prompt to run on the fetched content', | ||
| }, | ||
| }, | ||
| required: ['url', 'prompt'], | ||
| additionalProperties: false, | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| expect( | ||
| anthropicToolsToOllama(tools as any)[0]?.function.parameters, | ||
| ).toEqual({ | ||
| type: 'object', | ||
| properties: { | ||
| url: { | ||
| type: 'string', | ||
| description: 'The URL to fetch content from', | ||
| }, | ||
| prompt: { | ||
| type: 'string', | ||
| description: 'The prompt to run on the fetched content', | ||
| }, | ||
| }, | ||
| required: ['url', 'prompt'], | ||
| }) | ||
| }) | ||
|
|
||
| test('converts const and strips unsupported schema keywords recursively', () => { | ||
| const tools = [ | ||
| { | ||
| type: 'custom', | ||
| name: 'complex', | ||
| description: 'Complex schema', | ||
| input_schema: { | ||
| type: 'object', | ||
| patternProperties: { | ||
| '^x-': { type: 'string' }, | ||
| }, | ||
| properties: { | ||
| mode: { const: 'strict' }, | ||
| metadata: { | ||
| type: 'object', | ||
| additionalProperties: { type: 'string' }, | ||
| propertyNames: { pattern: '^[a-z]+$' }, | ||
| }, | ||
| }, | ||
| required: ['mode'], | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| expect( | ||
| anthropicToolsToOllama(tools as any)[0]?.function.parameters, | ||
| ).toEqual({ | ||
| type: 'object', | ||
| properties: { | ||
| mode: { | ||
| type: 'string', | ||
| enum: ['strict'], | ||
| }, | ||
| metadata: { | ||
| type: 'object', | ||
| }, | ||
| }, | ||
| required: ['mode'], | ||
| }) | ||
| }) | ||
| }) |
57 changes: 57 additions & 0 deletions
57
packages/@ant/model-provider/src/providers/ollama/__tests__/modelMapping.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,57 @@ | ||
| import { afterAll, beforeEach, describe, expect, test } from 'bun:test' | ||
| import { resolveOllamaModel } from '../modelMapping.js' | ||
|
|
||
| const envKeys = [ | ||
| 'OLLAMA_MODEL', | ||
| 'OLLAMA_DEFAULT_HAIKU_MODEL', | ||
| 'OLLAMA_DEFAULT_SONNET_MODEL', | ||
| 'OLLAMA_DEFAULT_OPUS_MODEL', | ||
| 'ANTHROPIC_DEFAULT_SONNET_MODEL', | ||
| ] as const | ||
|
|
||
| const savedEnv: Record<string, string | undefined> = {} | ||
|
|
||
| for (const key of envKeys) { | ||
| savedEnv[key] = process.env[key] | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| for (const key of envKeys) { | ||
| delete process.env[key] | ||
| } | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| for (const key of envKeys) { | ||
| if (savedEnv[key] === undefined) { | ||
| delete process.env[key] | ||
| } else { | ||
| process.env[key] = savedEnv[key] | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| describe('resolveOllamaModel', () => { | ||
| test('keeps direct Ollama model names selected from /model', () => { | ||
| expect(resolveOllamaModel('qwen3-coder')).toBe('qwen3-coder') | ||
| expect(resolveOllamaModel('glm-4.7:cloud')).toBe('glm-4.7:cloud') | ||
| }) | ||
|
|
||
| test('maps Claude family model ids to Ollama defaults', () => { | ||
| process.env.OLLAMA_DEFAULT_SONNET_MODEL = 'qwen3-coder' | ||
|
|
||
| expect(resolveOllamaModel('claude-sonnet-4-6')).toBe('qwen3-coder') | ||
| }) | ||
|
|
||
| test('does not fall back to Anthropic model env vars for Ollama', () => { | ||
| process.env.ANTHROPIC_DEFAULT_SONNET_MODEL = 'claude-sonnet-custom' | ||
|
|
||
| expect(resolveOllamaModel('claude-sonnet-4-6')).toBe('qwen3-coder') | ||
| }) | ||
|
|
||
| test('ignores legacy OLLAMA_MODEL global override', () => { | ||
| process.env.OLLAMA_MODEL = 'legacy-global-model' | ||
|
|
||
| expect(resolveOllamaModel('claude-sonnet-4-6')).toBe('qwen3-coder') | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.