Compare commits
8 Commits
fix/update
...
fix/383-ba
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a25d71004 | ||
|
|
50efbe5614 | ||
|
|
b20d878b76 | ||
|
|
f2fc454baf | ||
|
|
10f17d38ea | ||
|
|
889c472ddb | ||
|
|
0ad7746b7a | ||
|
|
91df124064 |
@@ -27,6 +27,21 @@ async function flushClipboardCopy(): Promise<void> {
|
|||||||
await new Promise(resolve => setTimeout(resolve, 0))
|
await new Promise(resolve => setTimeout(resolve, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function waitForExecCall(
|
||||||
|
command: string,
|
||||||
|
attempts = 20,
|
||||||
|
): Promise<(typeof execFileNoThrowMock.mock.calls)[number] | undefined> {
|
||||||
|
for (let attempt = 0; attempt < attempts; attempt++) {
|
||||||
|
const call = execFileNoThrowMock.mock.calls.find(([cmd]) => cmd === command)
|
||||||
|
if (call) {
|
||||||
|
return call
|
||||||
|
}
|
||||||
|
await flushClipboardCopy()
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
describe('Windows clipboard fallback', () => {
|
describe('Windows clipboard fallback', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
execFileNoThrowMock.mockClear()
|
execFileNoThrowMock.mockClear()
|
||||||
@@ -62,9 +77,7 @@ describe('Windows clipboard fallback', () => {
|
|||||||
await setClipboard('Привет мир')
|
await setClipboard('Привет мир')
|
||||||
await flushClipboardCopy()
|
await flushClipboardCopy()
|
||||||
|
|
||||||
const windowsCall = execFileNoThrowMock.mock.calls.find(
|
const windowsCall = await waitForExecCall('powershell')
|
||||||
([cmd]) => cmd === 'powershell',
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(windowsCall?.[2]).toMatchObject({
|
expect(windowsCall?.[2]).toMatchObject({
|
||||||
stdin: 'ignore',
|
stdin: 'ignore',
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -42,6 +42,10 @@ import {
|
|||||||
} from './providerConfig.js'
|
} from './providerConfig.js'
|
||||||
import { sanitizeSchemaForOpenAICompat } from '../../utils/schemaSanitizer.js'
|
import { sanitizeSchemaForOpenAICompat } from '../../utils/schemaSanitizer.js'
|
||||||
import { redactSecretValueForDisplay } from '../../utils/providerProfile.js'
|
import { redactSecretValueForDisplay } from '../../utils/providerProfile.js'
|
||||||
|
import {
|
||||||
|
normalizeToolArguments,
|
||||||
|
hasToolFieldMapping,
|
||||||
|
} from './toolArgumentNormalization.js'
|
||||||
|
|
||||||
type SecretValueSource = Partial<{
|
type SecretValueSource = Partial<{
|
||||||
OPENAI_API_KEY: string
|
OPENAI_API_KEY: string
|
||||||
@@ -476,6 +480,30 @@ function convertChunkUsage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const JSON_REPAIR_SUFFIXES = [
|
||||||
|
'}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}'
|
||||||
|
]
|
||||||
|
|
||||||
|
function repairPossiblyTruncatedObjectJson(raw: string): string | null {
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(raw)
|
||||||
|
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
|
||||||
|
? raw
|
||||||
|
: null
|
||||||
|
} catch {
|
||||||
|
for (const combo of JSON_REPAIR_SUFFIXES) {
|
||||||
|
try {
|
||||||
|
const repaired = raw + combo
|
||||||
|
const parsed = JSON.parse(repaired)
|
||||||
|
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
||||||
|
return repaired
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Async generator that transforms an OpenAI SSE stream into
|
* Async generator that transforms an OpenAI SSE stream into
|
||||||
* Anthropic-format BetaRawMessageStreamEvent objects.
|
* Anthropic-format BetaRawMessageStreamEvent objects.
|
||||||
@@ -486,7 +514,16 @@ async function* openaiStreamToAnthropic(
|
|||||||
): AsyncGenerator<AnthropicStreamEvent> {
|
): AsyncGenerator<AnthropicStreamEvent> {
|
||||||
const messageId = makeMessageId()
|
const messageId = makeMessageId()
|
||||||
let contentBlockIndex = 0
|
let contentBlockIndex = 0
|
||||||
const activeToolCalls = new Map<number, { id: string; name: string; index: number; jsonBuffer: string }>()
|
const activeToolCalls = new Map<
|
||||||
|
number,
|
||||||
|
{
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
index: number
|
||||||
|
jsonBuffer: string
|
||||||
|
normalizeAtStop: boolean
|
||||||
|
}
|
||||||
|
>()
|
||||||
let hasEmittedContentStart = false
|
let hasEmittedContentStart = false
|
||||||
let lastStopReason: 'tool_use' | 'max_tokens' | 'end_turn' | null = null
|
let lastStopReason: 'tool_use' | 'max_tokens' | 'end_turn' | null = null
|
||||||
let hasEmittedFinalUsage = false
|
let hasEmittedFinalUsage = false
|
||||||
@@ -577,11 +614,14 @@ async function* openaiStreamToAnthropic(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const toolBlockIndex = contentBlockIndex
|
const toolBlockIndex = contentBlockIndex
|
||||||
|
const initialArguments = tc.function.arguments ?? ''
|
||||||
|
const normalizeAtStop = hasToolFieldMapping(tc.function.name)
|
||||||
activeToolCalls.set(tc.index, {
|
activeToolCalls.set(tc.index, {
|
||||||
id: tc.id,
|
id: tc.id,
|
||||||
name: tc.function.name,
|
name: tc.function.name,
|
||||||
index: toolBlockIndex,
|
index: toolBlockIndex,
|
||||||
jsonBuffer: tc.function.arguments ?? '',
|
jsonBuffer: initialArguments,
|
||||||
|
normalizeAtStop,
|
||||||
})
|
})
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
@@ -598,7 +638,7 @@ async function* openaiStreamToAnthropic(
|
|||||||
contentBlockIndex++
|
contentBlockIndex++
|
||||||
|
|
||||||
// Emit any initial arguments
|
// Emit any initial arguments
|
||||||
if (tc.function.arguments) {
|
if (tc.function.arguments && !normalizeAtStop) {
|
||||||
yield {
|
yield {
|
||||||
type: 'content_block_delta',
|
type: 'content_block_delta',
|
||||||
index: toolBlockIndex,
|
index: toolBlockIndex,
|
||||||
@@ -615,6 +655,11 @@ async function* openaiStreamToAnthropic(
|
|||||||
if (tc.function.arguments) {
|
if (tc.function.arguments) {
|
||||||
active.jsonBuffer += tc.function.arguments
|
active.jsonBuffer += tc.function.arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (active.normalizeAtStop) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
yield {
|
yield {
|
||||||
type: 'content_block_delta',
|
type: 'content_block_delta',
|
||||||
index: active.index,
|
index: active.index,
|
||||||
@@ -642,16 +687,44 @@ async function* openaiStreamToAnthropic(
|
|||||||
}
|
}
|
||||||
// Close active tool calls
|
// Close active tool calls
|
||||||
for (const [, tc] of activeToolCalls) {
|
for (const [, tc] of activeToolCalls) {
|
||||||
|
if (tc.normalizeAtStop) {
|
||||||
|
let partialJson: string
|
||||||
|
if (choice.finish_reason === 'length') {
|
||||||
|
// Truncated by max tokens — preserve raw buffer to avoid
|
||||||
|
// turning an incomplete tool call into an executable command
|
||||||
|
partialJson = tc.jsonBuffer
|
||||||
|
} else {
|
||||||
|
const repairedStructuredJson = repairPossiblyTruncatedObjectJson(
|
||||||
|
tc.jsonBuffer,
|
||||||
|
)
|
||||||
|
if (repairedStructuredJson) {
|
||||||
|
partialJson = repairedStructuredJson
|
||||||
|
} else {
|
||||||
|
partialJson = JSON.stringify(
|
||||||
|
normalizeToolArguments(tc.name, tc.jsonBuffer),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yield {
|
||||||
|
type: 'content_block_delta',
|
||||||
|
index: tc.index,
|
||||||
|
delta: {
|
||||||
|
type: 'input_json_delta',
|
||||||
|
partial_json: partialJson,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
yield { type: 'content_block_stop', index: tc.index }
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
let suffixToAdd = ''
|
let suffixToAdd = ''
|
||||||
if (tc.jsonBuffer) {
|
if (tc.jsonBuffer) {
|
||||||
try {
|
try {
|
||||||
JSON.parse(tc.jsonBuffer)
|
JSON.parse(tc.jsonBuffer)
|
||||||
} catch {
|
} catch {
|
||||||
const str = tc.jsonBuffer.trimEnd()
|
const str = tc.jsonBuffer.trimEnd()
|
||||||
const combinations = [
|
for (const combo of JSON_REPAIR_SUFFIXES) {
|
||||||
'}', '"}', ']}', '"]}', '}}', '"}}', ']}}', '"]}}', '"]}]}', '}]}'
|
|
||||||
]
|
|
||||||
for (const combo of combinations) {
|
|
||||||
try {
|
try {
|
||||||
JSON.parse(str + combo)
|
JSON.parse(str + combo)
|
||||||
suffixToAdd = combo
|
suffixToAdd = combo
|
||||||
@@ -1087,12 +1160,10 @@ class OpenAIShimMessages {
|
|||||||
|
|
||||||
if (choice?.message?.tool_calls) {
|
if (choice?.message?.tool_calls) {
|
||||||
for (const tc of choice.message.tool_calls) {
|
for (const tc of choice.message.tool_calls) {
|
||||||
let input: unknown
|
const input = normalizeToolArguments(
|
||||||
try {
|
tc.function.name,
|
||||||
input = JSON.parse(tc.function.arguments)
|
tc.function.arguments,
|
||||||
} catch {
|
)
|
||||||
input = { raw: tc.function.arguments }
|
|
||||||
}
|
|
||||||
content.push({
|
content.push({
|
||||||
type: 'tool_use',
|
type: 'tool_use',
|
||||||
id: tc.id,
|
id: tc.id,
|
||||||
|
|||||||
180
src/services/api/toolArgumentNormalization.test.ts
Normal file
180
src/services/api/toolArgumentNormalization.test.ts
Normal file
@@ -0,0 +1,180 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
import { normalizeToolArguments } from './toolArgumentNormalization'
|
||||||
|
|
||||||
|
describe('normalizeToolArguments', () => {
|
||||||
|
describe('Bash tool', () => {
|
||||||
|
test('wraps plain string into { command }', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', 'pwd')).toEqual({ command: 'pwd' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('wraps multi-word command', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', 'ls -la /tmp')).toEqual({
|
||||||
|
command: 'ls -la /tmp',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Bash', '{"command":"echo hi"}'),
|
||||||
|
).toEqual({ command: 'echo hi' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns empty object for blank string', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '')).toEqual({})
|
||||||
|
expect(normalizeToolArguments('Bash', ' ')).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns parsed blank for JSON-encoded blank string', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '""')).toEqual('')
|
||||||
|
expect(normalizeToolArguments('Bash', '" "')).toEqual(' ')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns empty object for malformed structured object literal', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '{ "command": "pwd"')).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['{command:"pwd"}'],
|
||||||
|
["{'command':'pwd'}"],
|
||||||
|
['{command: pwd}'],
|
||||||
|
])(
|
||||||
|
'returns empty object for malformed object-shaped string %s (does not wrap into command)',
|
||||||
|
(input) => {
|
||||||
|
expect(normalizeToolArguments('Bash', input)).toEqual({})
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['false', false],
|
||||||
|
['null', null],
|
||||||
|
['[]', [] as unknown[]],
|
||||||
|
['0', 0],
|
||||||
|
['true', true],
|
||||||
|
['123', 123],
|
||||||
|
])(
|
||||||
|
'preserves JSON literal %s as-is (does not wrap into command)',
|
||||||
|
(input, expected) => {
|
||||||
|
expect(normalizeToolArguments('Bash', input)).toEqual(expected)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
test('wraps JSON-encoded string into { command }', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', '"pwd"')).toEqual({
|
||||||
|
command: 'pwd',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('undefined arguments', () => {
|
||||||
|
test('returns empty object for undefined', () => {
|
||||||
|
expect(normalizeToolArguments('Bash', undefined)).toEqual({})
|
||||||
|
expect(normalizeToolArguments('UnknownTool', undefined)).toEqual({})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Read tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Read', '/home/user/file.txt')).toEqual({
|
||||||
|
file_path: '/home/user/file.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('wraps JSON-encoded string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Read', '"/home/user/file.txt"')).toEqual({
|
||||||
|
file_path: '/home/user/file.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Read', '{"file_path":"/tmp/f.txt","limit":10}'),
|
||||||
|
).toEqual({ file_path: '/tmp/f.txt', limit: 10 })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Write tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Write', '/tmp/out.txt')).toEqual({
|
||||||
|
file_path: '/tmp/out.txt',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments(
|
||||||
|
'Write',
|
||||||
|
'{"file_path":"/tmp/out.txt","content":"hello"}',
|
||||||
|
),
|
||||||
|
).toEqual({ file_path: '/tmp/out.txt', content: 'hello' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Edit tool', () => {
|
||||||
|
test('wraps plain string into { file_path }', () => {
|
||||||
|
expect(normalizeToolArguments('Edit', '/tmp/edit.ts')).toEqual({
|
||||||
|
file_path: '/tmp/edit.ts',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments(
|
||||||
|
'Edit',
|
||||||
|
'{"file_path":"/tmp/f.ts","old_string":"a","new_string":"b"}',
|
||||||
|
),
|
||||||
|
).toEqual({ file_path: '/tmp/f.ts', old_string: 'a', new_string: 'b' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Glob tool', () => {
|
||||||
|
test('wraps plain string into { pattern }', () => {
|
||||||
|
expect(normalizeToolArguments('Glob', '**/*.ts')).toEqual({
|
||||||
|
pattern: '**/*.ts',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Glob', '{"pattern":"*.js","path":"/src"}'),
|
||||||
|
).toEqual({ pattern: '*.js', path: '/src' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Grep tool', () => {
|
||||||
|
test('wraps plain string into { pattern }', () => {
|
||||||
|
expect(normalizeToolArguments('Grep', 'TODO')).toEqual({
|
||||||
|
pattern: 'TODO',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('Grep', '{"pattern":"fixme","path":"/src"}'),
|
||||||
|
).toEqual({ pattern: 'fixme', path: '/src' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('unknown tools', () => {
|
||||||
|
test('returns empty object for plain string (no known field mapping)', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'some value')).toEqual({})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('passes through structured JSON object', () => {
|
||||||
|
expect(
|
||||||
|
normalizeToolArguments('UnknownTool', '{"key":"val"}'),
|
||||||
|
).toEqual({ key: 'val' })
|
||||||
|
})
|
||||||
|
|
||||||
|
test('preserves JSON literals as-is', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'false')).toEqual(false)
|
||||||
|
expect(normalizeToolArguments('UnknownTool', 'null')).toEqual(null)
|
||||||
|
expect(normalizeToolArguments('UnknownTool', '[]')).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('returns parsed string for JSON-encoded string on unknown tools', () => {
|
||||||
|
expect(normalizeToolArguments('UnknownTool', '"hello"')).toEqual(
|
||||||
|
'hello',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
69
src/services/api/toolArgumentNormalization.ts
Normal file
69
src/services/api/toolArgumentNormalization.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
const STRING_ARGUMENT_TOOL_FIELDS: Record<string, string> = {
|
||||||
|
Bash: 'command',
|
||||||
|
Read: 'file_path',
|
||||||
|
Write: 'file_path',
|
||||||
|
Edit: 'file_path',
|
||||||
|
Glob: 'pattern',
|
||||||
|
Grep: 'pattern',
|
||||||
|
}
|
||||||
|
|
||||||
|
function isBlankString(value: string): boolean {
|
||||||
|
return value.trim().length === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function isLikelyStructuredObjectLiteral(value: string): boolean {
|
||||||
|
// Match object-like patterns with key-value syntax:
|
||||||
|
// {"key":, {key:, {'key':, { "key" :, etc.
|
||||||
|
// But NOT bash compound commands like { pwd; } or { echo hi; }
|
||||||
|
return /^\s*\{\s*['"]?\w+['"]?\s*:/.test(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||||
|
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPlainStringToolArgumentField(toolName: string): string | null {
|
||||||
|
return STRING_ARGUMENT_TOOL_FIELDS[toolName] ?? null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function hasToolFieldMapping(toolName: string): boolean {
|
||||||
|
return toolName in STRING_ARGUMENT_TOOL_FIELDS
|
||||||
|
}
|
||||||
|
|
||||||
|
function wrapPlainStringToolArguments(
|
||||||
|
toolName: string,
|
||||||
|
value: string,
|
||||||
|
): Record<string, string> | null {
|
||||||
|
const field = getPlainStringToolArgumentField(toolName)
|
||||||
|
if (!field) return null
|
||||||
|
return { [field]: value }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function normalizeToolArguments(
|
||||||
|
toolName: string,
|
||||||
|
rawArguments: string | undefined,
|
||||||
|
): unknown {
|
||||||
|
if (rawArguments === undefined) return {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(rawArguments)
|
||||||
|
if (isRecord(parsed)) {
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
// Parsed as a non-object JSON value (string, number, boolean, null, array)
|
||||||
|
if (typeof parsed === 'string' && !isBlankString(parsed)) {
|
||||||
|
return wrapPlainStringToolArguments(toolName, parsed) ?? parsed
|
||||||
|
}
|
||||||
|
// For blank strings, booleans, null, arrays — pass through as-is
|
||||||
|
// and let Zod schema validation produce a meaningful error
|
||||||
|
return parsed
|
||||||
|
} catch {
|
||||||
|
// rawArguments is not valid JSON — treat as a plain string
|
||||||
|
if (isBlankString(rawArguments) || isLikelyStructuredObjectLiteral(rawArguments)) {
|
||||||
|
// Blank or looks like a malformed object literal — don't wrap into
|
||||||
|
// a tool field to avoid turning garbage into executable input
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
return wrapPlainStringToolArguments(toolName, rawArguments) ?? {}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,17 @@
|
|||||||
import { afterEach, expect, mock, test } from 'bun:test'
|
import { afterEach, beforeEach, expect, mock, test } from 'bun:test'
|
||||||
|
|
||||||
|
import { resetModelStringsForTestingOnly } from '../../bootstrap/state.js'
|
||||||
import { saveGlobalConfig } from '../config.js'
|
import { saveGlobalConfig } from '../config.js'
|
||||||
|
|
||||||
|
async function importFreshModelOptionsModule() {
|
||||||
|
mock.restore()
|
||||||
|
mock.module('./providers.js', () => ({
|
||||||
|
getAPIProvider: () => 'github',
|
||||||
|
}))
|
||||||
|
const nonce = `${Date.now()}-${Math.random()}`
|
||||||
|
return import(`./modelOptions.js?ts=${nonce}`)
|
||||||
|
}
|
||||||
|
|
||||||
const originalEnv = {
|
const originalEnv = {
|
||||||
CLAUDE_CODE_USE_GITHUB: process.env.CLAUDE_CODE_USE_GITHUB,
|
CLAUDE_CODE_USE_GITHUB: process.env.CLAUDE_CODE_USE_GITHUB,
|
||||||
CLAUDE_CODE_USE_OPENAI: process.env.CLAUDE_CODE_USE_OPENAI,
|
CLAUDE_CODE_USE_OPENAI: process.env.CLAUDE_CODE_USE_OPENAI,
|
||||||
@@ -14,6 +24,20 @@ const originalEnv = {
|
|||||||
ANTHROPIC_CUSTOM_MODEL_OPTION: process.env.ANTHROPIC_CUSTOM_MODEL_OPTION,
|
ANTHROPIC_CUSTOM_MODEL_OPTION: process.env.ANTHROPIC_CUSTOM_MODEL_OPTION,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mock.restore()
|
||||||
|
delete process.env.CLAUDE_CODE_USE_GITHUB
|
||||||
|
delete process.env.CLAUDE_CODE_USE_OPENAI
|
||||||
|
delete process.env.CLAUDE_CODE_USE_GEMINI
|
||||||
|
delete process.env.CLAUDE_CODE_USE_BEDROCK
|
||||||
|
delete process.env.CLAUDE_CODE_USE_VERTEX
|
||||||
|
delete process.env.CLAUDE_CODE_USE_FOUNDRY
|
||||||
|
delete process.env.OPENAI_MODEL
|
||||||
|
delete process.env.OPENAI_BASE_URL
|
||||||
|
delete process.env.ANTHROPIC_CUSTOM_MODEL_OPTION
|
||||||
|
resetModelStringsForTestingOnly()
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = originalEnv.CLAUDE_CODE_USE_GITHUB
|
process.env.CLAUDE_CODE_USE_GITHUB = originalEnv.CLAUDE_CODE_USE_GITHUB
|
||||||
process.env.CLAUDE_CODE_USE_OPENAI = originalEnv.CLAUDE_CODE_USE_OPENAI
|
process.env.CLAUDE_CODE_USE_OPENAI = originalEnv.CLAUDE_CODE_USE_OPENAI
|
||||||
@@ -34,17 +58,9 @@ afterEach(() => {
|
|||||||
providerProfiles: [],
|
providerProfiles: [],
|
||||||
activeProviderProfileId: undefined,
|
activeProviderProfileId: undefined,
|
||||||
}))
|
}))
|
||||||
|
resetModelStringsForTestingOnly()
|
||||||
})
|
})
|
||||||
|
|
||||||
async function importFreshModelOptionsModule() {
|
|
||||||
mock.restore()
|
|
||||||
mock.module('./providers.js', () => ({
|
|
||||||
getAPIProvider: () => 'github',
|
|
||||||
}))
|
|
||||||
const nonce = `${Date.now()}-${Math.random()}`
|
|
||||||
return import(`./modelOptions.js?ts=${nonce}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
test('GitHub provider exposes only default + GitHub model in /model options', async () => {
|
test('GitHub provider exposes only default + GitHub model in /model options', async () => {
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
delete process.env.CLAUDE_CODE_USE_OPENAI
|
delete process.env.CLAUDE_CODE_USE_OPENAI
|
||||||
@@ -58,7 +74,9 @@ test('GitHub provider exposes only default + GitHub model in /model options', as
|
|||||||
|
|
||||||
const { getModelOptions } = await importFreshModelOptionsModule()
|
const { getModelOptions } = await importFreshModelOptionsModule()
|
||||||
const options = getModelOptions(false)
|
const options = getModelOptions(false)
|
||||||
const nonDefault = options.filter(option => option.value !== null)
|
const nonDefault = options.filter(
|
||||||
|
(option: { value: unknown }) => option.value !== null,
|
||||||
|
)
|
||||||
|
|
||||||
expect(nonDefault.length).toBe(1)
|
expect(nonDefault.length).toBe(1)
|
||||||
expect(nonDefault[0]?.value).toBe('github:copilot')
|
expect(nonDefault[0]?.value).toBe('github:copilot')
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { describe, expect, test, afterEach } from 'bun:test'
|
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
||||||
import {
|
import {
|
||||||
parseProviderFlag,
|
parseProviderFlag,
|
||||||
applyProviderFlag,
|
applyProviderFlag,
|
||||||
@@ -8,18 +8,26 @@ import {
|
|||||||
|
|
||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
|
const RESET_KEYS = [
|
||||||
|
'CLAUDE_CODE_USE_OPENAI',
|
||||||
|
'CLAUDE_CODE_USE_GEMINI',
|
||||||
|
'CLAUDE_CODE_USE_GITHUB',
|
||||||
|
'CLAUDE_CODE_USE_BEDROCK',
|
||||||
|
'CLAUDE_CODE_USE_VERTEX',
|
||||||
|
'OPENAI_BASE_URL',
|
||||||
|
'OPENAI_API_KEY',
|
||||||
|
'OPENAI_MODEL',
|
||||||
|
'GEMINI_MODEL',
|
||||||
|
] as const
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
for (const key of RESET_KEYS) {
|
||||||
|
delete process.env[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
for (const key of [
|
for (const key of RESET_KEYS) {
|
||||||
'CLAUDE_CODE_USE_OPENAI',
|
|
||||||
'CLAUDE_CODE_USE_GEMINI',
|
|
||||||
'CLAUDE_CODE_USE_GITHUB',
|
|
||||||
'CLAUDE_CODE_USE_BEDROCK',
|
|
||||||
'CLAUDE_CODE_USE_VERTEX',
|
|
||||||
'OPENAI_BASE_URL',
|
|
||||||
'OPENAI_API_KEY',
|
|
||||||
'OPENAI_MODEL',
|
|
||||||
'GEMINI_MODEL',
|
|
||||||
]) {
|
|
||||||
if (originalEnv[key] === undefined) delete process.env[key]
|
if (originalEnv[key] === undefined) delete process.env[key]
|
||||||
else process.env[key] = originalEnv[key]
|
else process.env[key] = originalEnv[key]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,10 @@ import { afterEach, describe, expect, mock, test } from 'bun:test'
|
|||||||
|
|
||||||
import type { ProviderProfile } from './config.js'
|
import type { ProviderProfile } from './config.js'
|
||||||
|
|
||||||
|
async function importFreshProvidersModule() {
|
||||||
|
return import(`./model/providers.ts?ts=${Date.now()}-${Math.random()}`)
|
||||||
|
}
|
||||||
|
|
||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
|
|
||||||
const RESTORED_KEYS = [
|
const RESTORED_KEYS = [
|
||||||
@@ -96,24 +100,26 @@ function buildProfile(overrides: Partial<ProviderProfile> = {}): ProviderProfile
|
|||||||
|
|
||||||
describe('applyProviderProfileToProcessEnv', () => {
|
describe('applyProviderProfileToProcessEnv', () => {
|
||||||
test('openai profile clears competing gemini/github flags', async () => {
|
test('openai profile clears competing gemini/github flags', async () => {
|
||||||
const { applyProviderProfileToProcessEnv, getAPIProvider } =
|
const { applyProviderProfileToProcessEnv } =
|
||||||
await importFreshProviderProfileModules()
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
|
|
||||||
applyProviderProfileToProcessEnv(buildProfile())
|
applyProviderProfileToProcessEnv(buildProfile())
|
||||||
|
const { getAPIProvider: getFreshAPIProvider } =
|
||||||
|
await importFreshProvidersModule()
|
||||||
|
|
||||||
expect(process.env.CLAUDE_CODE_USE_GEMINI).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_USE_GEMINI).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
|
expect(String(process.env.CLAUDE_CODE_USE_OPENAI)).toBe('1')
|
||||||
expect(process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID).toBe(
|
expect(process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED_ID).toBe(
|
||||||
'provider_test',
|
'provider_test',
|
||||||
)
|
)
|
||||||
expect(getAPIProvider()).toBe('openai')
|
expect(getFreshAPIProvider()).toBe('openai')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('anthropic profile clears competing gemini/github flags', async () => {
|
test('anthropic profile clears competing gemini/github flags', async () => {
|
||||||
const { applyProviderProfileToProcessEnv, getAPIProvider } =
|
const { applyProviderProfileToProcessEnv } =
|
||||||
await importFreshProviderProfileModules()
|
await importFreshProviderProfileModules()
|
||||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||||
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
process.env.CLAUDE_CODE_USE_GITHUB = '1'
|
||||||
@@ -125,11 +131,13 @@ describe('applyProviderProfileToProcessEnv', () => {
|
|||||||
model: 'claude-sonnet-4-6',
|
model: 'claude-sonnet-4-6',
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
const { getAPIProvider: getFreshAPIProvider } =
|
||||||
|
await importFreshProvidersModule()
|
||||||
|
|
||||||
expect(process.env.CLAUDE_CODE_USE_GEMINI).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_USE_GEMINI).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_USE_GITHUB).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBeUndefined()
|
||||||
expect(getAPIProvider()).toBe('firstParty')
|
expect(getFreshAPIProvider()).toBe('firstParty')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -177,7 +185,7 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
} as any)
|
} as any)
|
||||||
|
|
||||||
expect(applied).toBeUndefined()
|
expect(applied).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
|
expect(String(process.env.CLAUDE_CODE_USE_OPENAI)).toBe('1')
|
||||||
expect(process.env.OPENAI_BASE_URL).toBe('http://localhost:11434/v1')
|
expect(process.env.OPENAI_BASE_URL).toBe('http://localhost:11434/v1')
|
||||||
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
||||||
})
|
})
|
||||||
@@ -267,7 +275,7 @@ describe('applyActiveProviderProfileFromConfig', () => {
|
|||||||
} as any)
|
} as any)
|
||||||
|
|
||||||
expect(applied?.id).toBe('saved_openai')
|
expect(applied?.id).toBe('saved_openai')
|
||||||
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
|
expect(String(process.env.CLAUDE_CODE_USE_OPENAI)).toBe('1')
|
||||||
expect(process.env.OPENAI_BASE_URL).toBe('https://api.openai.com/v1')
|
expect(process.env.OPENAI_BASE_URL).toBe('https://api.openai.com/v1')
|
||||||
expect(process.env.OPENAI_MODEL).toBe('gpt-4o')
|
expect(process.env.OPENAI_MODEL).toBe('gpt-4o')
|
||||||
})
|
})
|
||||||
@@ -286,10 +294,10 @@ describe('persistActiveProviderProfileModel', () => {
|
|||||||
model: 'kimi-k2.5:cloud',
|
model: 'kimi-k2.5:cloud',
|
||||||
})
|
})
|
||||||
|
|
||||||
saveMockGlobalConfig(current => ({
|
saveMockGlobalConfig(current => ({
|
||||||
...current,
|
...current,
|
||||||
providerProfiles: [activeProfile],
|
providerProfiles: [activeProfile],
|
||||||
activeProviderProfileId: activeProfile.id,
|
activeProviderProfileId: activeProfile.id,
|
||||||
}))
|
}))
|
||||||
applyProviderProfileToProcessEnv(activeProfile)
|
applyProviderProfileToProcessEnv(activeProfile)
|
||||||
|
|
||||||
@@ -303,7 +311,7 @@ describe('persistActiveProviderProfileModel', () => {
|
|||||||
)
|
)
|
||||||
|
|
||||||
const saved = getProviderProfiles().find(
|
const saved = getProviderProfiles().find(
|
||||||
profile => profile.id === activeProfile.id,
|
(profile: ProviderProfile) => profile.id === activeProfile.id,
|
||||||
)
|
)
|
||||||
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
||||||
})
|
})
|
||||||
@@ -333,7 +341,7 @@ describe('persistActiveProviderProfileModel', () => {
|
|||||||
|
|
||||||
expect(process.env.OPENAI_MODEL).toBe('cli-model')
|
expect(process.env.OPENAI_MODEL).toBe('cli-model')
|
||||||
const saved = getProviderProfiles().find(
|
const saved = getProviderProfiles().find(
|
||||||
profile => profile.id === activeProfile.id,
|
(profile: ProviderProfile) => profile.id === activeProfile.id,
|
||||||
)
|
)
|
||||||
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
expect(saved?.model).toBe('minimax-m2.5:cloud')
|
||||||
})
|
})
|
||||||
@@ -414,7 +422,7 @@ describe('deleteProviderProfile', () => {
|
|||||||
expect(result.activeProfileId).toBeUndefined()
|
expect(result.activeProfileId).toBeUndefined()
|
||||||
|
|
||||||
expect(process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED).toBeUndefined()
|
expect(process.env.CLAUDE_CODE_PROVIDER_PROFILE_ENV_APPLIED).toBeUndefined()
|
||||||
expect(process.env.CLAUDE_CODE_USE_OPENAI).toBe('1')
|
expect(String(process.env.CLAUDE_CODE_USE_OPENAI)).toBe('1')
|
||||||
expect(process.env.OPENAI_BASE_URL).toBe('http://localhost:11434/v1')
|
expect(process.env.OPENAI_BASE_URL).toBe('http://localhost:11434/v1')
|
||||||
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
expect(process.env.OPENAI_MODEL).toBe('qwen2.5:3b')
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user