copilot-custom-endpoint 3.7.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ import { hasActiveToolCall, summarizePayload } from './rewrite-summary.mjs'
2
+
3
+ export const QWEN_REWRITE_DEFAULTS = {
4
+ disableThinkingWithTools: true
5
+ }
6
+
7
+ /**
8
+ * Rewrite a DashScope/Qwen chat-completions payload in place.
9
+ *
10
+ * Thinking stays on for plain chat (the field is removed so DashScope uses its
11
+ * default) and is suppressed only for detected tool activity.
12
+ *
13
+ * @returns {{ summary: object, consoleMsg: string }}
14
+ */
15
+ export function rewriteQwen(payload, options = {}) {
16
+ const { disableThinkingWithTools } = {
17
+ ...QWEN_REWRITE_DEFAULTS,
18
+ ...options
19
+ }
20
+
21
+ const hasTools = hasActiveToolCall(payload)
22
+ const incomingEnableThinking = payload.enable_thinking
23
+ const suppressThinking = disableThinkingWithTools && hasTools
24
+
25
+ if (suppressThinking) {
26
+ payload.enable_thinking = false
27
+ } else {
28
+ delete payload.enable_thinking
29
+ }
30
+
31
+ const rewriteInfo = {
32
+ incomingEnableThinking,
33
+ rewrittenEnableThinking: suppressThinking ? false : undefined
34
+ }
35
+
36
+ const modeTag = hasTools ? '[tools]' : '[chat]'
37
+ const consoleMsg = `${modeTag} enable_thinking=${String(incomingEnableThinking)} -> ${
38
+ suppressThinking ? 'false' : '<deleted>'
39
+ }, model=${payload.model ?? '?'}`
40
+
41
+ return {
42
+ summary: summarizePayload(payload, hasTools, rewriteInfo),
43
+ consoleMsg
44
+ }
45
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Build the NDJSON log summary shared by the provider rewrites.
3
+ * Keep this shape stable — it is the documented debug_log contract.
4
+ */
5
+ export function summarizePayload(payload, hasTools, rewriteInfo) {
6
+ const messages = Array.isArray(payload.messages) ? payload.messages : []
7
+ const tools = Array.isArray(payload.tools) ? payload.tools : []
8
+
9
+ return {
10
+ model: payload.model,
11
+ stream: payload.stream,
12
+ ...rewriteInfo,
13
+ maxTokens:
14
+ payload.max_tokens ??
15
+ payload.max_completion_tokens ??
16
+ payload.max_output_tokens,
17
+ toolChoice: payload.tool_choice,
18
+ toolCount: tools.length,
19
+ hasTools,
20
+ messageCount: messages.length,
21
+ messageRoles: messages.map((message) => message?.role).slice(0, 16),
22
+ topLevelKeys: Object.keys(payload).sort()
23
+ }
24
+ }
25
+
26
+ /**
27
+ * A request counts as tool activity when a "tool" role message is present or
28
+ * `tool_choice` is set to anything other than "none"/null. A nonempty `tools`
29
+ * array alone is not enough — see docs/models/qwen.md.
30
+ */
31
+ export function hasActiveToolCall(payload) {
32
+ const messages = Array.isArray(payload.messages) ? payload.messages : []
33
+ const hasToolRole = messages.some((message) => message?.role === 'tool')
34
+ const toolChoice = payload.tool_choice
35
+
36
+ return (
37
+ hasToolRole ||
38
+ (toolChoice !== undefined && toolChoice !== 'none' && toolChoice !== null)
39
+ )
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copilot-custom-endpoint",
3
- "version": "3.7.1",
3
+ "version": "4.0.0",
4
4
  "description": "Local proxies for VS Code Copilot custom endpoints — Kimi K2, Qwen 3.x & MiMo V2.6",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -57,4 +57,4 @@
57
57
  "dependencies": {
58
58
  "dotenv": "^17.4.2"
59
59
  }
60
- }
60
+ }
@@ -2,6 +2,7 @@
2
2
  import 'dotenv/config'
3
3
  import { fileURLToPath } from 'node:url'
4
4
  import { createProxy } from '../lib/create-proxy.mjs'
5
+ import { rewriteKimi } from '../lib/kimi-rewrite.mjs'
5
6
 
6
7
  /**
7
8
  * Supported model scope for this proxy:
@@ -86,107 +87,6 @@ if (!Number.isFinite(forcedTopP)) {
86
87
  )
87
88
  }
88
89
 
89
- // ---- Provider-specific rewrite logic ----
90
-
91
- function summarizePayload(payload, hasTools, rewriteInfo) {
92
- const messages = Array.isArray(payload.messages) ? payload.messages : []
93
- const tools = Array.isArray(payload.tools) ? payload.tools : []
94
-
95
- return {
96
- model: payload.model,
97
- stream: payload.stream,
98
- ...rewriteInfo,
99
- maxTokens:
100
- payload.max_tokens ??
101
- payload.max_completion_tokens ??
102
- payload.max_output_tokens,
103
- toolChoice: payload.tool_choice,
104
- toolCount: tools.length,
105
- hasTools,
106
- messageCount: messages.length,
107
- messageRoles: messages.map((message) => message?.role).slice(0, 16),
108
- topLevelKeys: Object.keys(payload).sort()
109
- }
110
- }
111
-
112
- function rewriteKimi(payload) {
113
- const incomingTemperature = payload.temperature
114
- const incomingTopP = payload.top_p
115
- const incomingThinkingType = payload?.thinking?.type
116
- const incomingReasoningEffort = payload?.reasoning_effort
117
- const model = payload.model ?? ''
118
-
119
- // K2.7 and K3 are always-thinking and reject thinking: disabled.
120
- // K2.7 uses `thinking` parameter; K3 uses `reasoning_effort` instead.
121
- // Detect K2.7 variants (e.g. kimi-k2.7-code) and K3-family variants
122
- // (e.g. kimi-k3, kimi-k3.1-pro, kimi-k3.1-pro-highspeed) and skip the
123
- // thinking-disable rewrite while keeping temperature/top_p enforcement.
124
- const isK27 = model.startsWith('kimi-k2.7')
125
- const isK3 = model.startsWith('kimi-k3')
126
- const isAlwaysThinking = isK27 || isK3
127
-
128
- // Determine if a tool is actually being invoked:
129
- // - tool_choice is set and not "none"
130
- // - OR there is a "tool" role message in the conversation
131
- const messages = Array.isArray(payload.messages) ? payload.messages : []
132
- const hasToolRole = messages.some((message) => message?.role === 'tool')
133
- const toolChoice = payload.tool_choice
134
- const hasActiveToolCall =
135
- hasToolRole ||
136
- (toolChoice !== undefined && toolChoice !== 'none' && toolChoice !== null)
137
- const hasTools = hasActiveToolCall
138
-
139
- const useNonThinkingMode =
140
- !isAlwaysThinking && disableThinkingWithTools && hasTools
141
- const rewrittenTemperature = useNonThinkingMode
142
- ? forcedNonThinkingTemperature
143
- : forcedTemperature
144
-
145
- // Capture incoming state before mutation
146
- payload.__incomingThinkingType = incomingThinkingType
147
- payload.__incomingReasoningEffort = incomingReasoningEffort
148
-
149
- // Apply rewrites
150
- payload.temperature = rewrittenTemperature
151
- payload.top_p = forcedTopP
152
-
153
- if (useNonThinkingMode) {
154
- payload.thinking = { type: 'disabled' }
155
- }
156
-
157
- // K3 does not accept `thinking` — delete it if present so it doesn't cause a 400
158
- if (isK3 && payload.thinking) {
159
- delete payload.thinking
160
- }
161
-
162
- const rewrittenThinkingType = payload.thinking?.type
163
- const rewriteInfo = {
164
- model,
165
- isK27,
166
- isK3,
167
- incomingTemperature,
168
- rewrittenTemperature,
169
- incomingTopP,
170
- rewrittenTopP: forcedTopP,
171
- incomingThinkingType,
172
- rewrittenThinkingType,
173
- incomingReasoningEffort
174
- }
175
-
176
- const summary = summarizePayload(payload, hasTools, rewriteInfo)
177
-
178
- const modeTag = hasTools ? '[tools]' : '[chat]'
179
- const k27Tag = isK27 ? '[k2.7]' : ''
180
- const k3Tag = isK3 ? '[k3]' : ''
181
- const consoleMsg = `${k27Tag}${k3Tag}${modeTag} temperature ${String(incomingTemperature)} -> ${String(rewrittenTemperature)}, top_p ${String(incomingTopP)} -> ${String(forcedTopP)}, thinking ${String(incomingThinkingType)} -> ${String(rewrittenThinkingType)}, reasoning_effort ${String(incomingReasoningEffort)}`
182
-
183
- // Clean up internal keys before forwarding
184
- delete payload.__incomingThinkingType
185
- delete payload.__incomingReasoningEffort
186
-
187
- return { summary, consoleMsg }
188
- }
189
-
190
90
  // ---- Create and start ----
191
91
 
192
92
  const { start } = createProxy({
@@ -195,7 +95,13 @@ const { start } = createProxy({
195
95
  logPath,
196
96
  label: 'kimi-proxy',
197
97
  healthCheckExtras: { forcedTemperature, forcedTopP },
198
- rewriteRequest: rewriteKimi,
98
+ rewriteRequest: (payload) =>
99
+ rewriteKimi(payload, {
100
+ forcedTemperature,
101
+ forcedNonThinkingTemperature,
102
+ forcedTopP,
103
+ disableThinkingWithTools
104
+ }),
199
105
  startupMessages: (_port, _upstreamUrl) => [
200
106
  `[kimi-proxy] listening on http://127.0.0.1:${_port}/v1/chat/completions`,
201
107
  `[kimi-proxy] forwarding to ${_upstreamUrl}`,
@@ -2,6 +2,7 @@
2
2
  import 'dotenv/config'
3
3
  import { fileURLToPath } from 'node:url'
4
4
  import { createProxy } from '../lib/create-proxy.mjs'
5
+ import { rewriteQwen } from '../lib/qwen-rewrite.mjs'
5
6
 
6
7
  /**
7
8
  * Supported model scope for this proxy:
@@ -33,8 +34,9 @@ if (process.argv.includes('--help')) {
33
34
  console.log(`Qwen proxy
34
35
 
35
36
  Starts a local HTTP proxy that conditionally injects enable_thinking: false
36
- when the request includes a tools array, letting Qwen hybrid-thinking models
37
- show reasoning in plain chat while keeping tool loops stable.
37
+ when the request shows tool activity (a tool-role message or a non-default
38
+ tool_choice), letting Qwen hybrid-thinking models show reasoning in plain
39
+ chat while keeping tool loops stable.
38
40
 
39
41
  The proxy forwards the requested model ID unchanged, including the DashScope
40
42
  ID qwen3.8-max and OpenRouter ID qwen/qwen3.8-max-0902.
@@ -57,66 +59,6 @@ Suggested VS Code model URL:
57
59
  process.exit(0)
58
60
  }
59
61
 
60
- // ---- Provider-specific rewrite logic ----
61
-
62
- function summarizePayload(payload, hasTools, rewriteInfo) {
63
- const messages = Array.isArray(payload.messages) ? payload.messages : []
64
- const tools = Array.isArray(payload.tools) ? payload.tools : []
65
-
66
- return {
67
- model: payload.model,
68
- stream: payload.stream,
69
- hasTools,
70
- toolCount: tools.length,
71
- toolChoice: payload.tool_choice,
72
- ...rewriteInfo,
73
- maxTokens:
74
- payload.max_tokens ??
75
- payload.max_completion_tokens ??
76
- payload.max_output_tokens,
77
- messageCount: messages.length,
78
- messageRoles: messages.map((message) => message?.role).slice(0, 16),
79
- topLevelKeys: Object.keys(payload).sort()
80
- }
81
- }
82
-
83
- function rewriteQwen(payload) {
84
- // Determine if a tool is actually being invoked:
85
- // - tool_choice is set and not "none"
86
- // - OR there is a "tool" role message in the conversation
87
- const messages = Array.isArray(payload.messages) ? payload.messages : []
88
- const hasToolRole = messages.some((message) => message?.role === 'tool')
89
- const toolChoice = payload.tool_choice
90
- const hasActiveToolCall =
91
- hasToolRole ||
92
- (toolChoice !== undefined && toolChoice !== 'none' && toolChoice !== null)
93
- const hasTools = hasActiveToolCall
94
- const incomingEnableThinking = payload.enable_thinking
95
-
96
- if (disableThinkingWithTools && hasTools) {
97
- // Tool-enabled request: suppress thinking to avoid reasoning_content issues
98
- payload.enable_thinking = false
99
- } else {
100
- // Plain chat: remove enable_thinking so the model uses its default (true)
101
- delete payload.enable_thinking
102
- }
103
-
104
- const rewrittenEnableThinking =
105
- disableThinkingWithTools && hasTools ? false : undefined
106
-
107
- const summary = summarizePayload(payload, hasTools, {
108
- incomingEnableThinking,
109
- rewrittenEnableThinking
110
- })
111
-
112
- const modeTag = hasTools ? '[tools]' : '[chat]'
113
- const consoleMsg = `${modeTag} enable_thinking=${String(incomingEnableThinking)} -> ${
114
- hasTools && disableThinkingWithTools ? 'false' : '<deleted>'
115
- }, model=${payload.model ?? '?'}`
116
-
117
- return { summary, consoleMsg }
118
- }
119
-
120
62
  // ---- Create and start ----
121
63
 
122
64
  const { start } = createProxy({
@@ -125,7 +67,8 @@ const { start } = createProxy({
125
67
  logPath,
126
68
  label: 'qwen-proxy',
127
69
  healthCheckExtras: { disableThinkingWithTools },
128
- rewriteRequest: rewriteQwen,
70
+ rewriteRequest: (payload) =>
71
+ rewriteQwen(payload, { disableThinkingWithTools }),
129
72
  startupMessages: (_port, _upstreamUrl) => [
130
73
  `[qwen-proxy] listening on http://127.0.0.1:${_port}/v1/chat/completions`,
131
74
  `[qwen-proxy] forwarding to ${_upstreamUrl}`,