-
-
Notifications
You must be signed in to change notification settings - Fork 164
feat: add Kimi BYOK provider and ChatGPT thinking mode #83
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
simplypeace1
wants to merge
1
commit into
Zen4-bit:main
Choose a base branch
from
simplypeace1:feat/kimi-provider-chatgpt-thinking
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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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 |
|---|---|---|
|
|
@@ -15,9 +15,11 @@ function createRouteHandler(deps) { | |
| byok, | ||
| } = deps; | ||
|
|
||
| return async function handleRoute(method, pathname, body, res) { | ||
| return async function handleRoute(method, pathname, body, res, headers, searchParams) { | ||
| const handleMCPRequest = getHandler(); | ||
| const conversationId = (body && (body.conversationId || body.conversation_id || body.sessionId || body.session_id)) || null; | ||
| const conversationId = (body && (body.conversationId || body.conversation_id || body.sessionId || body.session_id)) | ||
| || (headers && (headers['x-proxima-conversation'] || headers['X-Proxima-Conversation'])) | ||
| || null; | ||
|
|
||
| if (method === 'POST' && pathname === `${API_PREFIX}/chat/completions`) { | ||
| const fn = (body.function || '').toLowerCase().trim(); | ||
|
|
@@ -253,10 +255,17 @@ function createRouteHandler(deps) { | |
| else sendJSON(res, 200, formatChatResponse(result, provider)); | ||
| } | ||
| } else { | ||
| const toolPrompt = buildToolCallingPrompt(body); | ||
| if (!toolPrompt) return sendError(res, 400, 'No message provided'); | ||
| // Kimi's engine needs the raw messages array (it reads the | ||
| // system prompt to detect a NEW chat and only forwards the | ||
| // latest user turn on follow-ups). Flattening to a string | ||
| // via buildToolCallingPrompt would lose the system prompt | ||
| // and make the engine reuse a stale/too-long chat_id. | ||
| const inputForEngine = provider === 'kimi' | ||
| ? (Array.isArray(body.messages) ? body.messages : message) | ||
| : buildToolCallingPrompt(body); | ||
| if (!inputForEngine) return sendError(res, 400, 'No message provided'); | ||
| if (TOOL_DEBUG) console.log('[TOOL-CALL/Legacy] Provider:', provider, '| Tools:', body.tools.length); | ||
| const result = await queryProvider(provider, toolPrompt, filePath, gemini, null, conversationId, body._byokKey, null, resolved.byokModelId); | ||
| const result = await queryProvider(provider, inputForEngine, filePath, gemini, null, conversationId, body._byokKey, null, resolved.byokModelId); | ||
|
Comment on lines
+266
to
+268
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 4. Rest ignores think flag REST /v1/chat/completions session-provider calls never forward a request’s think flag to the MCP sendMessage action, so REST clients cannot disable ChatGPT thinking mode (it will default to enabled). This breaks the “think flag end-to-end” behavior for API consumers. Agent Prompt
|
||
| if (TOOL_DEBUG) console.log('[TOOL-CALL/Legacy] Response text (first 300):', JSON.stringify(result.text?.slice(0, 300))); | ||
| const parsed = parseToolCallResponse(result.text); | ||
| if (TOOL_DEBUG) console.log('[TOOL-CALL/Legacy] Parsed:', parsed.isToolCall, '| toolCalls:', parsed.toolCalls?.length || 0); | ||
|
|
@@ -283,7 +292,12 @@ function createRouteHandler(deps) { | |
| if (resolved.mode === 'single') { | ||
| const provider = resolved.providers[0]; | ||
| const isByokActive = !!(body._byokKey || (byok.keys.isEnabled() ? byok.keys.getKey(provider) : null)); | ||
| const inputMessage = isByokActive ? (body.messages || message) : message; | ||
| // Kimi's engine needs the full messages array so it can send the | ||
| // system prompt + context once at chat start and only the new | ||
| // user turn afterwards (parent_id chain carries the rest). | ||
| const inputMessage = isByokActive | ||
| ? (body.messages || message) | ||
| : (provider === 'kimi' && Array.isArray(body.messages) ? body.messages : message); | ||
|
|
||
| let onChunk = null; | ||
| let streamedAny = false; | ||
|
|
@@ -373,7 +387,7 @@ function createRouteHandler(deps) { | |
| aliases: Object.entries(MODEL_ALIASES).filter(([_, v]) => v === p).map(([k]) => k).filter(k => k !== p), | ||
| }); | ||
| } | ||
| ['chatgpt', 'claude', 'gemini', 'perplexity'].filter(p => !enabled.includes(p)).forEach(p => | ||
| ['chatgpt', 'claude', 'gemini', 'perplexity', 'kimi'].filter(p => !enabled.includes(p)).forEach(p => | ||
| models.push({ id: p, object: 'model', owned_by: 'proxima', status: 'disabled' }) | ||
| ); | ||
| } | ||
|
|
@@ -395,7 +409,7 @@ function createRouteHandler(deps) { | |
| function: null, | ||
| name: 'chat', | ||
| description: 'Default chat completion. Omit the "function" field to chat with a model.', | ||
| fields: { model: 'auto|claude|chatgpt|gemini|perplexity|all (or array of models)', message: 'string (or use messages[])', stream: 'boolean (optional)', filePath: 'local file path for multimodal/Gemini routing (optional)' } | ||
| fields: { model: 'auto|claude|chatgpt|gemini|perplexity|kimi|all (or array of models)', message: 'string (or use messages[])', stream: 'boolean (optional)', filePath: 'local file path for multimodal/Gemini routing (optional)' } | ||
| }, | ||
| { | ||
| function: 'search', | ||
|
|
@@ -472,7 +486,7 @@ function createRouteHandler(deps) { | |
| if (method === 'POST' && pathname === `${API_PREFIX}/conversations/new`) { | ||
| const requested = (body && (body.provider || body.model)) || null; | ||
| if (!requested) { | ||
| return sendError(res, 400, 'A "provider" (or "model") is required: chatgpt, claude, gemini, or perplexity.'); | ||
| return sendError(res, 400, 'A "provider" (or "model") is required: chatgpt, claude, gemini, perplexity, or kimi.'); | ||
| } | ||
| const target = pickBestProvider(requested); | ||
| if (!target) { | ||
|
|
@@ -482,6 +496,20 @@ function createRouteHandler(deps) { | |
| catch (e) { return sendError(res, 500, e.message); } | ||
| } | ||
|
|
||
| if (method === 'GET' && pathname === `${API_PREFIX}/kimi/session`) { | ||
| // Session-status endpoint for the opencode plugin. The plugin polls this | ||
| // before each request; when resetCount changes, it knows Kimi rolled to | ||
| // a NEW chat_id and must re-send the system prompt + handoff summary. | ||
| const cid = (searchParams && (searchParams.get('conversationId') || searchParams.get('conversation_id') || searchParams.get('sessionId'))) | ||
| || (headers && headers['x-proxima-conversation']) | ||
| || null; | ||
| try { | ||
| const r = await handleMCPRequest({ action: 'getKimiSessionStatus', provider: 'kimi', data: { conversationId: cid } }); | ||
| if (!r || r.success === false) return sendJSON(res, 200, { success: false, resetCount: 0, chatId: null, parentId: null, error: (r && r.error) || 'unavailable' }); | ||
| return sendJSON(res, 200, { success: true, resetCount: r.resetCount || 0, chatId: r.chatId || null, parentId: r.parentId || null }); | ||
| } catch (e) { return sendJSON(res, 200, { success: false, resetCount: 0, chatId: null, parentId: null, error: e.message }); } | ||
| } | ||
|
|
||
| if (method === 'GET' && (pathname === `${API_PREFIX}/openapi.json` || pathname === '/openapi.json')) { | ||
| try { return sendJSON(res, 200, JSON.parse(require('fs').readFileSync(require('path').join(__dirname, '..', '..', 'docs', 'openapi.json'), 'utf8'))); } | ||
| catch (e) { return sendError(res, 500, 'OpenAPI spec not found'); } | ||
|
|
||
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.
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.
5. Kimi session exposed unauthenticated
🐞 Bug⛨ SecurityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools