Compare commits
3 Commits
fix/repl-a
...
fix/skillt
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1e72fc94cb | ||
|
|
be7ec1b5fa | ||
|
|
cdfaea5ced |
@@ -8,34 +8,6 @@ import {
|
|||||||
validateProviderEnvOrExit,
|
validateProviderEnvOrExit,
|
||||||
} from '../utils/providerValidation.js'
|
} from '../utils/providerValidation.js'
|
||||||
|
|
||||||
// OpenClaude: polyfill globalThis.File for Node < 20.
|
|
||||||
// undici v7 references `File` at module evaluation time (webidl type
|
|
||||||
// assertions). Node 18 lacks the global, causing a ReferenceError inside
|
|
||||||
// the bundled __commonJS require chain which deadlocks the process when a
|
|
||||||
// proxy is configured (configureGlobalAgents → require_undici).
|
|
||||||
// eslint-disable-next-line custom-rules/no-top-level-side-effects
|
|
||||||
if (typeof globalThis.File === 'undefined') {
|
|
||||||
try {
|
|
||||||
// Node 18.13+ exposes File in node:buffer but not as a global.
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
||||||
const { File: NodeFile } = require('node:buffer')
|
|
||||||
// @ts-expect-error -- polyfilling missing global
|
|
||||||
globalThis.File = NodeFile
|
|
||||||
} catch {
|
|
||||||
// Absolute fallback: stub so `MakeTypeAssertion(File)` doesn't throw.
|
|
||||||
// @ts-expect-error -- minimal polyfill
|
|
||||||
globalThis.File = class File extends Blob {
|
|
||||||
name: string
|
|
||||||
lastModified: number
|
|
||||||
constructor(parts: BlobPart[], name: string, opts?: FilePropertyBag) {
|
|
||||||
super(parts, opts)
|
|
||||||
this.name = name
|
|
||||||
this.lastModified = opts?.lastModified ?? Date.now()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OpenClaude: disable experimental API betas by default.
|
// OpenClaude: disable experimental API betas by default.
|
||||||
// Tool search (defer_loading), global cache scope, and context management
|
// Tool search (defer_loading), global cache scope, and context management
|
||||||
// require internal API support not available to external accounts → 500.
|
// require internal API support not available to external accounts → 500.
|
||||||
|
|||||||
@@ -201,117 +201,6 @@ describe('Codex request translation', () => {
|
|||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('preserves Grep tool pattern field in Codex strict schemas', () => {
|
|
||||||
const tools = convertToolsToResponsesTools([
|
|
||||||
{
|
|
||||||
name: 'Grep',
|
|
||||||
description: 'Search file contents',
|
|
||||||
input_schema: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: { type: 'string', description: 'Search pattern' },
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
expect(tools).toEqual([
|
|
||||||
{
|
|
||||||
type: 'function',
|
|
||||||
name: 'Grep',
|
|
||||||
description: 'Search file contents',
|
|
||||||
parameters: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: { type: 'string', description: 'Search pattern' },
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern', 'path'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
strict: true,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('preserves Glob tool pattern field in Codex strict schemas', () => {
|
|
||||||
const tools = convertToolsToResponsesTools([
|
|
||||||
{
|
|
||||||
name: 'Glob',
|
|
||||||
description: 'Find files by pattern',
|
|
||||||
input_schema: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: { type: 'string', description: 'Glob pattern' },
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
expect(tools).toEqual([
|
|
||||||
{
|
|
||||||
type: 'function',
|
|
||||||
name: 'Glob',
|
|
||||||
description: 'Find files by pattern',
|
|
||||||
parameters: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: { type: 'string', description: 'Glob pattern' },
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern', 'path'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
strict: true,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('strips validator pattern keyword but keeps string field named pattern in Codex schemas', () => {
|
|
||||||
const tools = convertToolsToResponsesTools([
|
|
||||||
{
|
|
||||||
name: 'RegexProbe',
|
|
||||||
description: 'Probe regex schema handling',
|
|
||||||
input_schema: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: {
|
|
||||||
type: 'string',
|
|
||||||
pattern: '^[a-z]+$',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
])
|
|
||||||
|
|
||||||
expect(tools).toEqual([
|
|
||||||
{
|
|
||||||
type: 'function',
|
|
||||||
name: 'RegexProbe',
|
|
||||||
description: 'Probe regex schema handling',
|
|
||||||
parameters: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: {
|
|
||||||
type: 'string',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
strict: true,
|
|
||||||
},
|
|
||||||
])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('removes unsupported uri format from strict Responses schemas', () => {
|
test('removes unsupported uri format from strict Responses schemas', () => {
|
||||||
const tools = convertToolsToResponsesTools([
|
const tools = convertToolsToResponsesTools([
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -261,73 +261,6 @@ test('preserves Gemini tool call extra_content in follow-up requests', async ()
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('preserves Grep tool pattern field in OpenAI-compatible schemas', async () => {
|
|
||||||
let requestBody: Record<string, unknown> | undefined
|
|
||||||
|
|
||||||
globalThis.fetch = (async (_input, init) => {
|
|
||||||
requestBody = JSON.parse(String(init?.body))
|
|
||||||
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
id: 'chatcmpl-grep-schema',
|
|
||||||
model: 'qwen/qwen3.6-plus',
|
|
||||||
choices: [
|
|
||||||
{
|
|
||||||
message: {
|
|
||||||
role: 'assistant',
|
|
||||||
content: 'done',
|
|
||||||
},
|
|
||||||
finish_reason: 'stop',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
usage: {
|
|
||||||
prompt_tokens: 12,
|
|
||||||
completion_tokens: 4,
|
|
||||||
total_tokens: 16,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}) as FetchType
|
|
||||||
|
|
||||||
const client = createOpenAIShimClient({}) as OpenAIShimClient
|
|
||||||
|
|
||||||
await client.beta.messages.create({
|
|
||||||
model: 'qwen/qwen3.6-plus',
|
|
||||||
system: 'test system',
|
|
||||||
messages: [{ role: 'user', content: 'Use Grep' }],
|
|
||||||
tools: [
|
|
||||||
{
|
|
||||||
name: 'Grep',
|
|
||||||
description: 'Search file contents',
|
|
||||||
input_schema: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: { type: 'string', description: 'Search pattern' },
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
max_tokens: 64,
|
|
||||||
stream: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
const tools = requestBody?.tools as Array<Record<string, unknown>> | undefined
|
|
||||||
const grepTool = tools?.find(tool => (tool.function as Record<string, unknown>)?.name === 'Grep') as
|
|
||||||
| { function?: { parameters?: { properties?: Record<string, unknown>; required?: string[] } } }
|
|
||||||
| undefined
|
|
||||||
|
|
||||||
expect(Object.keys(grepTool?.function?.parameters?.properties ?? {})).toContain('pattern')
|
|
||||||
expect(grepTool?.function?.parameters?.required).toContain('pattern')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('does not infer Gemini mode from OPENAI_BASE_URL path substrings', async () => {
|
test('does not infer Gemini mode from OPENAI_BASE_URL path substrings', async () => {
|
||||||
let capturedAuthorization: string | null = null
|
let capturedAuthorization: string | null = null
|
||||||
|
|
||||||
|
|||||||
@@ -195,12 +195,10 @@ function convertContentBlocks(
|
|||||||
// handled separately
|
// handled separately
|
||||||
break
|
break
|
||||||
case 'thinking':
|
case 'thinking':
|
||||||
case 'redacted_thinking':
|
// Append thinking as text with a marker for models that support reasoning
|
||||||
// Strip thinking blocks for OpenAI-compatible providers.
|
if (block.thinking) {
|
||||||
// These are Anthropic-specific content types that 3P providers
|
parts.push({ type: 'text', text: `<thinking>${block.thinking}</thinking>` })
|
||||||
// don't understand. Serializing them as <thinking> text corrupts
|
}
|
||||||
// multi-turn context: the model sees the tags as part of its
|
|
||||||
// previous reply and may mimic or misattribute them.
|
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
if (block.text) {
|
if (block.text) {
|
||||||
|
|||||||
33
src/services/tools/toolExecution.test.ts
Normal file
33
src/services/tools/toolExecution.test.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
|
import { SkillTool } from '../../tools/SkillTool/SkillTool.js'
|
||||||
|
import {
|
||||||
|
getSchemaValidationErrorOverride,
|
||||||
|
getSchemaValidationToolUseResult,
|
||||||
|
} from './toolExecution.js'
|
||||||
|
|
||||||
|
describe('getSchemaValidationErrorOverride', () => {
|
||||||
|
test('returns actionable missing-skill error for SkillTool', () => {
|
||||||
|
expect(getSchemaValidationErrorOverride(SkillTool, {})).toBe(
|
||||||
|
'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('does not override unrelated tool schema failures', () => {
|
||||||
|
expect(getSchemaValidationErrorOverride({ name: 'Read' } as never, {})).toBe(
|
||||||
|
null,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('does not override SkillTool when skill is present', () => {
|
||||||
|
expect(
|
||||||
|
getSchemaValidationErrorOverride(SkillTool, { skill: 'commit' }),
|
||||||
|
).toBe(null)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('uses the actionable override for structured toolUseResult too', () => {
|
||||||
|
expect(getSchemaValidationToolUseResult(SkillTool, {} as never)).toBe(
|
||||||
|
'InputValidationError: Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -43,6 +43,7 @@ import { FILE_READ_TOOL_NAME } from '../../tools/FileReadTool/prompt.js'
|
|||||||
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
import { FILE_WRITE_TOOL_NAME } from '../../tools/FileWriteTool/prompt.js'
|
||||||
import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js'
|
import { NOTEBOOK_EDIT_TOOL_NAME } from '../../tools/NotebookEditTool/constants.js'
|
||||||
import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js'
|
import { POWERSHELL_TOOL_NAME } from '../../tools/PowerShellTool/toolName.js'
|
||||||
|
import { SKILL_TOOL_NAME } from '../../tools/SkillTool/constants.js'
|
||||||
import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js'
|
import { parseGitCommitId } from '../../tools/shared/gitOperationTracking.js'
|
||||||
import {
|
import {
|
||||||
isDeferredTool,
|
isDeferredTool,
|
||||||
@@ -596,6 +597,31 @@ export function buildSchemaNotSentHint(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getSchemaValidationErrorOverride(
|
||||||
|
tool: Tool,
|
||||||
|
input: unknown,
|
||||||
|
): string | null {
|
||||||
|
if (tool.name !== SKILL_TOOL_NAME || !input || typeof input !== 'object') {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
const skill = (input as { skill?: unknown }).skill
|
||||||
|
if (skill === undefined || skill === null) {
|
||||||
|
return 'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).'
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getSchemaValidationToolUseResult(
|
||||||
|
tool: Tool,
|
||||||
|
input: unknown,
|
||||||
|
fallbackMessage?: string,
|
||||||
|
): string {
|
||||||
|
const override = getSchemaValidationErrorOverride(tool, input)
|
||||||
|
return `InputValidationError: ${override ?? fallbackMessage ?? ''}`
|
||||||
|
}
|
||||||
|
|
||||||
async function checkPermissionsAndCallTool(
|
async function checkPermissionsAndCallTool(
|
||||||
tool: Tool,
|
tool: Tool,
|
||||||
toolUseID: string,
|
toolUseID: string,
|
||||||
@@ -614,7 +640,9 @@ async function checkPermissionsAndCallTool(
|
|||||||
// Validate input types with zod (surprisingly, the model is not great at generating valid input)
|
// Validate input types with zod (surprisingly, the model is not great at generating valid input)
|
||||||
const parsedInput = tool.inputSchema.safeParse(input)
|
const parsedInput = tool.inputSchema.safeParse(input)
|
||||||
if (!parsedInput.success) {
|
if (!parsedInput.success) {
|
||||||
let errorContent = formatZodValidationError(tool.name, parsedInput.error)
|
const fallbackErrorContent = formatZodValidationError(tool.name, parsedInput.error)
|
||||||
|
let errorContent =
|
||||||
|
getSchemaValidationErrorOverride(tool, input) ?? fallbackErrorContent
|
||||||
|
|
||||||
const schemaHint = buildSchemaNotSentHint(
|
const schemaHint = buildSchemaNotSentHint(
|
||||||
tool,
|
tool,
|
||||||
@@ -672,7 +700,11 @@ async function checkPermissionsAndCallTool(
|
|||||||
tool_use_id: toolUseID,
|
tool_use_id: toolUseID,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
toolUseResult: `InputValidationError: ${parsedInput.error.message}`,
|
toolUseResult: getSchemaValidationToolUseResult(
|
||||||
|
tool,
|
||||||
|
input,
|
||||||
|
parsedInput.error.message,
|
||||||
|
),
|
||||||
sourceToolAssistantUUID: assistantMessage.uuid,
|
sourceToolAssistantUUID: assistantMessage.uuid,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
|||||||
31
src/tools/SkillTool/SkillTool.test.ts
Normal file
31
src/tools/SkillTool/SkillTool.test.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { describe, expect, test } from 'bun:test'
|
||||||
|
|
||||||
|
import { SkillTool } from './SkillTool.js'
|
||||||
|
|
||||||
|
describe('SkillTool missing parameter handling', () => {
|
||||||
|
test('missing skill stays required at the schema level', async () => {
|
||||||
|
const parsed = SkillTool.inputSchema.safeParse({})
|
||||||
|
|
||||||
|
expect(parsed.success).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
test('validateInput still returns an actionable error when called with missing skill', async () => {
|
||||||
|
const result = await SkillTool.validateInput?.({} as never, {
|
||||||
|
options: { tools: [] },
|
||||||
|
messages: [],
|
||||||
|
} as never)
|
||||||
|
|
||||||
|
expect(result).toEqual({
|
||||||
|
result: false,
|
||||||
|
message:
|
||||||
|
'Missing skill name. Pass the slash command name as the skill parameter (e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
||||||
|
errorCode: 1,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
test('valid skill input still parses and validates', async () => {
|
||||||
|
const parsed = SkillTool.inputSchema.safeParse({ skill: 'commit' })
|
||||||
|
|
||||||
|
expect(parsed.success).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -352,6 +352,16 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
toAutoClassifierInput: ({ skill }) => skill ?? '',
|
toAutoClassifierInput: ({ skill }) => skill ?? '',
|
||||||
|
|
||||||
async validateInput({ skill }, context): Promise<ValidationResult> {
|
async validateInput({ skill }, context): Promise<ValidationResult> {
|
||||||
|
if (!skill || typeof skill !== 'string') {
|
||||||
|
return {
|
||||||
|
result: false,
|
||||||
|
message:
|
||||||
|
'Missing skill name. Pass the slash command name as the skill parameter ' +
|
||||||
|
'(e.g., skill: "commit" for /commit, skill: "review-pr" for /review-pr).',
|
||||||
|
errorCode: 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Skills are just skill names, no arguments
|
// Skills are just skill names, no arguments
|
||||||
const trimmed = skill.trim()
|
const trimmed = skill.trim()
|
||||||
if (!trimmed) {
|
if (!trimmed) {
|
||||||
@@ -434,7 +444,7 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
context,
|
context,
|
||||||
): Promise<PermissionDecision> {
|
): Promise<PermissionDecision> {
|
||||||
// Skills are just skill names, no arguments
|
// Skills are just skill names, no arguments
|
||||||
const trimmed = skill.trim()
|
const trimmed = skill ?? ''
|
||||||
|
|
||||||
// Remove leading slash if present (for compatibility)
|
// Remove leading slash if present (for compatibility)
|
||||||
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
||||||
@@ -592,7 +602,7 @@ export const SkillTool: Tool<InputSchema, Output, Progress> = buildTool({
|
|||||||
// - Skill is a prompt-based skill
|
// - Skill is a prompt-based skill
|
||||||
|
|
||||||
// Skills are just names, with optional arguments
|
// Skills are just names, with optional arguments
|
||||||
const trimmed = skill.trim()
|
const trimmed = skill ?? ''
|
||||||
|
|
||||||
// Remove leading slash if present (for compatibility)
|
// Remove leading slash if present (for compatibility)
|
||||||
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
const commandName = trimmed.startsWith('/') ? trimmed.substring(1) : trimmed
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { expect, test } from 'bun:test'
|
import { expect, test } from 'bun:test'
|
||||||
import { z } from 'zod/v4'
|
import { z } from 'zod/v4'
|
||||||
import { getEmptyToolPermissionContext, type Tool, type Tools } from '../Tool.js'
|
import { getEmptyToolPermissionContext, type Tool, type Tools } from '../Tool.js'
|
||||||
|
import { SkillTool } from '../tools/SkillTool/SkillTool.js'
|
||||||
import { toolToAPISchema } from './api.js'
|
import { toolToAPISchema } from './api.js'
|
||||||
|
|
||||||
test('toolToAPISchema preserves provider-specific schema keywords in input_schema', async () => {
|
test('toolToAPISchema preserves provider-specific schema keywords in input_schema', async () => {
|
||||||
@@ -64,3 +65,16 @@ test('toolToAPISchema preserves provider-specific schema keywords in input_schem
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('toolToAPISchema keeps skill required for SkillTool', async () => {
|
||||||
|
const schema = await toolToAPISchema(SkillTool, {
|
||||||
|
getToolPermissionContext: async () => getEmptyToolPermissionContext(),
|
||||||
|
tools: [] as unknown as Tools,
|
||||||
|
agents: [],
|
||||||
|
})
|
||||||
|
|
||||||
|
expect((schema as { input_schema: unknown }).input_schema).toMatchObject({
|
||||||
|
type: 'object',
|
||||||
|
required: ['skill'],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -72,23 +72,16 @@ export function getContextWindowForModel(
|
|||||||
return 1_000_000
|
return 1_000_000
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenAI-compatible provider — use known context windows for the model.
|
// OpenAI-compatible provider — use known context windows for the model
|
||||||
// Unknown models get a conservative 8k default so auto-compact triggers
|
if (
|
||||||
// before hitting a hard context_window_exceeded error (issue #248 finding 3).
|
|
||||||
const isOpenAIProvider =
|
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_OPENAI) ||
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_OPENAI) ||
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_GEMINI) ||
|
||||||
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
isEnvTruthy(process.env.CLAUDE_CODE_USE_GITHUB)
|
||||||
if (isOpenAIProvider) {
|
) {
|
||||||
const openaiWindow = getOpenAIContextWindow(model)
|
const openaiWindow = getOpenAIContextWindow(model)
|
||||||
if (openaiWindow !== undefined) {
|
if (openaiWindow !== undefined) {
|
||||||
return openaiWindow
|
return openaiWindow
|
||||||
}
|
}
|
||||||
console.error(
|
|
||||||
`[context] Warning: model "${model}" not in context window table — using conservative 8k default. ` +
|
|
||||||
'Add it to src/utils/model/openaiContextWindows.ts for accurate compaction.',
|
|
||||||
)
|
|
||||||
return 8_000
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const cap = getModelCapability(model)
|
const cap = getModelCapability(model)
|
||||||
|
|||||||
@@ -69,93 +69,3 @@ test('loadConversationForResume rejects oversized transcripts before resume hook
|
|||||||
)
|
)
|
||||||
expect(hookSpy).not.toHaveBeenCalled()
|
expect(hookSpy).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('deserializeMessagesWithInterruptDetection strips thinking blocks only for OpenAI-compatible providers', async () => {
|
|
||||||
const serializedMessages = [
|
|
||||||
user(id(10), 'hello'),
|
|
||||||
{
|
|
||||||
type: 'assistant',
|
|
||||||
uuid: id(11),
|
|
||||||
parentUuid: id(10),
|
|
||||||
timestamp: ts,
|
|
||||||
cwd: '/tmp',
|
|
||||||
sessionId,
|
|
||||||
version: 'test',
|
|
||||||
message: {
|
|
||||||
role: 'assistant',
|
|
||||||
content: [
|
|
||||||
{ type: 'thinking', thinking: 'secret reasoning' },
|
|
||||||
{ type: 'text', text: 'visible reply' },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: 'assistant',
|
|
||||||
uuid: id(12),
|
|
||||||
parentUuid: id(11),
|
|
||||||
timestamp: ts,
|
|
||||||
cwd: '/tmp',
|
|
||||||
sessionId,
|
|
||||||
version: 'test',
|
|
||||||
message: {
|
|
||||||
role: 'assistant',
|
|
||||||
content: [{ type: 'thinking', thinking: 'only hidden reasoning' }],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
user(id(13), 'follow up'),
|
|
||||||
]
|
|
||||||
|
|
||||||
mock.module('./model/providers.js', () => ({
|
|
||||||
getAPIProvider: () => 'openai',
|
|
||||||
isOpenAICompatibleProvider: (provider: string) =>
|
|
||||||
provider === 'openai' ||
|
|
||||||
provider === 'gemini' ||
|
|
||||||
provider === 'github' ||
|
|
||||||
provider === 'codex',
|
|
||||||
}))
|
|
||||||
|
|
||||||
const openaiModule = await import(`./conversationRecovery.ts?provider=openai-${Date.now()}`)
|
|
||||||
const thirdParty = openaiModule.deserializeMessagesWithInterruptDetection(serializedMessages as never[])
|
|
||||||
const thirdPartyAssistantMessages = thirdParty.messages.filter(
|
|
||||||
message => message.type === 'assistant',
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(thirdPartyAssistantMessages).toHaveLength(2)
|
|
||||||
expect(thirdPartyAssistantMessages[0]?.message?.content).toEqual([
|
|
||||||
{ type: 'text', text: 'visible reply' },
|
|
||||||
])
|
|
||||||
expect(
|
|
||||||
JSON.stringify(thirdPartyAssistantMessages.map(message => message.message?.content)),
|
|
||||||
).not.toContain('secret reasoning')
|
|
||||||
expect(
|
|
||||||
JSON.stringify(thirdPartyAssistantMessages.map(message => message.message?.content)),
|
|
||||||
).not.toContain('only hidden reasoning')
|
|
||||||
|
|
||||||
mock.restore()
|
|
||||||
mock.module('./model/providers.js', () => ({
|
|
||||||
getAPIProvider: () => 'bedrock',
|
|
||||||
isOpenAICompatibleProvider: (provider: string) =>
|
|
||||||
provider === 'openai' ||
|
|
||||||
provider === 'gemini' ||
|
|
||||||
provider === 'github' ||
|
|
||||||
provider === 'codex',
|
|
||||||
}))
|
|
||||||
|
|
||||||
const bedrockModule = await import(`./conversationRecovery.ts?provider=bedrock-${Date.now()}`)
|
|
||||||
const anthropicCompatible = bedrockModule.deserializeMessagesWithInterruptDetection(serializedMessages as never[])
|
|
||||||
const anthropicAssistantMessages = anthropicCompatible.messages.filter(
|
|
||||||
message => message.type === 'assistant',
|
|
||||||
)
|
|
||||||
|
|
||||||
expect(anthropicAssistantMessages).toHaveLength(2)
|
|
||||||
expect(anthropicAssistantMessages[0]?.message?.content).toEqual([
|
|
||||||
{ type: 'thinking', thinking: 'secret reasoning' },
|
|
||||||
{ type: 'text', text: 'visible reply' },
|
|
||||||
])
|
|
||||||
expect(
|
|
||||||
JSON.stringify(anthropicAssistantMessages.map(message => message.message?.content)),
|
|
||||||
).toContain('secret reasoning')
|
|
||||||
expect(
|
|
||||||
JSON.stringify(anthropicAssistantMessages.map(message => message.message?.content)),
|
|
||||||
).not.toContain('only hidden reasoning')
|
|
||||||
})
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ const originalSimple = process.env.CLAUDE_CODE_SIMPLE
|
|||||||
const sessionId = '00000000-0000-4000-8000-000000001999'
|
const sessionId = '00000000-0000-4000-8000-000000001999'
|
||||||
const ts = '2026-04-02T00:00:00.000Z'
|
const ts = '2026-04-02T00:00:00.000Z'
|
||||||
|
|
||||||
|
|
||||||
function id(n: number): string {
|
function id(n: number): string {
|
||||||
return `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`
|
return `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`
|
||||||
}
|
}
|
||||||
@@ -77,3 +76,4 @@ test('loadConversationForResume rejects oversized reconstructed transcripts', as
|
|||||||
'Reconstructed transcript is too large to resume safely',
|
'Reconstructed transcript is too large to resume safely',
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import {
|
|||||||
type FileHistorySnapshot,
|
type FileHistorySnapshot,
|
||||||
} from './fileHistory.js'
|
} from './fileHistory.js'
|
||||||
import { logError } from './log.js'
|
import { logError } from './log.js'
|
||||||
import { getAPIProvider } from './model/providers.js'
|
|
||||||
import {
|
import {
|
||||||
createAssistantMessage,
|
createAssistantMessage,
|
||||||
createUserMessage,
|
createUserMessage,
|
||||||
@@ -178,25 +177,6 @@ export type DeserializeResult = {
|
|||||||
turnInterruptionState: TurnInterruptionState
|
turnInterruptionState: TurnInterruptionState
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove thinking/redacted_thinking content blocks from assistant messages.
|
|
||||||
* Messages that become empty after stripping are removed entirely.
|
|
||||||
*/
|
|
||||||
function stripThinkingBlocks(messages: NormalizedMessage[]): NormalizedMessage[] {
|
|
||||||
return messages.reduce<NormalizedMessage[]>((acc, msg) => {
|
|
||||||
if (msg.type !== 'assistant' || !Array.isArray(msg.message?.content)) {
|
|
||||||
acc.push(msg)
|
|
||||||
return acc
|
|
||||||
}
|
|
||||||
const filtered = msg.message.content.filter(
|
|
||||||
(block: { type?: string }) => block.type !== 'thinking' && block.type !== 'redacted_thinking',
|
|
||||||
)
|
|
||||||
if (filtered.length === 0) return acc
|
|
||||||
acc.push({ ...msg, message: { ...msg.message, content: filtered } })
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserializes messages from a log file into the format expected by the REPL.
|
* Deserializes messages from a log file into the format expected by the REPL.
|
||||||
* Filters unresolved tool uses, orphaned thinking messages, and appends a
|
* Filters unresolved tool uses, orphaned thinking messages, and appends a
|
||||||
@@ -247,19 +227,10 @@ export function deserializeMessagesWithInterruptDetection(
|
|||||||
filteredToolUses,
|
filteredToolUses,
|
||||||
) as NormalizedMessage[]
|
) as NormalizedMessage[]
|
||||||
|
|
||||||
// Strip thinking/redacted_thinking content blocks from assistant messages
|
|
||||||
// when resuming against a 3P provider. These Anthropic-specific blocks cause
|
|
||||||
// 400 errors or context corruption on OpenAI-compatible providers (issue #248 finding 5).
|
|
||||||
const provider = getAPIProvider()
|
|
||||||
const isThirdPartyProvider = provider !== 'firstParty' && provider !== 'bedrock' && provider !== 'vertex' && provider !== 'foundry'
|
|
||||||
const thinkingStripped = isThirdPartyProvider
|
|
||||||
? stripThinkingBlocks(filteredThinking)
|
|
||||||
: filteredThinking
|
|
||||||
|
|
||||||
// Filter out assistant messages with only whitespace text content.
|
// Filter out assistant messages with only whitespace text content.
|
||||||
// This can happen when model outputs "\n\n" before thinking, user cancels mid-stream.
|
// This can happen when model outputs "\n\n" before thinking, user cancels mid-stream.
|
||||||
const filteredMessages = filterWhitespaceOnlyAssistantMessages(
|
const filteredMessages = filterWhitespaceOnlyAssistantMessages(
|
||||||
thinkingStripped,
|
filteredThinking,
|
||||||
) as NormalizedMessage[]
|
) as NormalizedMessage[]
|
||||||
|
|
||||||
const internalState = detectTurnInterruption(filteredMessages)
|
const internalState = detectTurnInterruption(filteredMessages)
|
||||||
|
|||||||
@@ -1,52 +1,11 @@
|
|||||||
import { expect, test } from 'bun:test'
|
import { expect, test } from 'bun:test'
|
||||||
import path from 'path'
|
|
||||||
|
|
||||||
import { resolveRipgrepConfig, wrapRipgrepUnavailableError } from './ripgrep.js'
|
import { wrapRipgrepUnavailableError } from './ripgrep.ts'
|
||||||
|
|
||||||
const MOCK_BUILTIN_PATH = path.normalize(
|
|
||||||
process.platform === 'win32'
|
|
||||||
? `vendor/ripgrep/${process.arch}-win32/rg.exe`
|
|
||||||
: `vendor/ripgrep/${process.arch}-${process.platform}/rg`,
|
|
||||||
)
|
|
||||||
|
|
||||||
test('ripgrepCommand falls back to system rg when builtin binary is missing', () => {
|
|
||||||
const config = resolveRipgrepConfig({
|
|
||||||
userWantsSystemRipgrep: false,
|
|
||||||
bundledMode: false,
|
|
||||||
builtinCommand: MOCK_BUILTIN_PATH,
|
|
||||||
builtinExists: false,
|
|
||||||
systemExecutablePath: '/usr/bin/rg',
|
|
||||||
processExecPath: '/fake/bun',
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(config).toMatchObject({
|
|
||||||
mode: 'system',
|
|
||||||
command: 'rg',
|
|
||||||
args: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('ripgrepCommand keeps builtin mode when bundled binary exists', () => {
|
|
||||||
const config = resolveRipgrepConfig({
|
|
||||||
userWantsSystemRipgrep: false,
|
|
||||||
bundledMode: false,
|
|
||||||
builtinCommand: MOCK_BUILTIN_PATH,
|
|
||||||
builtinExists: true,
|
|
||||||
systemExecutablePath: '/usr/bin/rg',
|
|
||||||
processExecPath: '/fake/bun',
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(config).toMatchObject({
|
|
||||||
mode: 'builtin',
|
|
||||||
command: MOCK_BUILTIN_PATH,
|
|
||||||
args: [],
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
||||||
const error = wrapRipgrepUnavailableError(
|
const error = wrapRipgrepUnavailableError(
|
||||||
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
||||||
{ mode: 'builtin', command: 'C:\\fake\\vendor\\ripgrep\\rg.exe', args: [] },
|
{ mode: 'builtin', command: 'C:\\fake\\vendor\\ripgrep\\rg.exe' },
|
||||||
'win32',
|
'win32',
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +18,7 @@ test('wrapRipgrepUnavailableError explains missing packaged fallback', () => {
|
|||||||
test('wrapRipgrepUnavailableError explains missing system ripgrep', () => {
|
test('wrapRipgrepUnavailableError explains missing system ripgrep', () => {
|
||||||
const error = wrapRipgrepUnavailableError(
|
const error = wrapRipgrepUnavailableError(
|
||||||
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
{ code: 'ENOENT', message: 'spawn rg ENOENT' },
|
||||||
{ mode: 'system', command: 'rg', args: [] },
|
{ mode: 'system', command: 'rg' },
|
||||||
'linux',
|
'linux',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import type { ChildProcess, ExecFileException } from 'child_process'
|
import type { ChildProcess, ExecFileException } from 'child_process'
|
||||||
import { execFile, spawn } from 'child_process'
|
import { execFile, spawn } from 'child_process'
|
||||||
import { existsSync } from 'fs'
|
|
||||||
import memoize from 'lodash-es/memoize.js'
|
import memoize from 'lodash-es/memoize.js'
|
||||||
import { homedir } from 'os'
|
import { homedir } from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -31,72 +30,40 @@ type RipgrepConfig = {
|
|||||||
|
|
||||||
type RipgrepErrorLike = Pick<NodeJS.ErrnoException, 'code' | 'message'>
|
type RipgrepErrorLike = Pick<NodeJS.ErrnoException, 'code' | 'message'>
|
||||||
|
|
||||||
function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
|
const getRipgrepConfig = memoize((): RipgrepConfig => {
|
||||||
return error instanceof Error
|
const userWantsSystemRipgrep = isEnvDefinedFalsy(
|
||||||
}
|
process.env.USE_BUILTIN_RIPGREP,
|
||||||
|
)
|
||||||
|
|
||||||
type ResolveRipgrepConfigArgs = {
|
// Try system ripgrep if user wants it
|
||||||
userWantsSystemRipgrep: boolean
|
if (userWantsSystemRipgrep) {
|
||||||
bundledMode: boolean
|
const { cmd: systemPath } = findExecutable('rg', [])
|
||||||
builtinCommand: string
|
if (systemPath !== 'rg') {
|
||||||
builtinExists: boolean
|
// SECURITY: Use command name 'rg' instead of systemPath to prevent PATH hijacking
|
||||||
systemExecutablePath: string
|
// If we used systemPath, a malicious ./rg.exe in current directory could be executed
|
||||||
processExecPath?: string
|
// Using just 'rg' lets the OS resolve it safely with NoDefaultCurrentDirectoryInExePath protection
|
||||||
}
|
return { mode: 'system', command: 'rg', args: [] }
|
||||||
|
}
|
||||||
export function resolveRipgrepConfig({
|
|
||||||
userWantsSystemRipgrep,
|
|
||||||
bundledMode,
|
|
||||||
builtinCommand,
|
|
||||||
builtinExists,
|
|
||||||
systemExecutablePath,
|
|
||||||
processExecPath = process.execPath,
|
|
||||||
}: ResolveRipgrepConfigArgs): RipgrepConfig {
|
|
||||||
if (userWantsSystemRipgrep && systemExecutablePath !== 'rg') {
|
|
||||||
// SECURITY: Use command name 'rg' instead of systemExecutablePath to prevent PATH hijacking
|
|
||||||
return { mode: 'system', command: 'rg', args: [] }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bundledMode) {
|
// In bundled (native) mode, ripgrep is statically compiled into bun-internal
|
||||||
|
// and dispatches based on argv[0]. We spawn ourselves with argv0='rg'.
|
||||||
|
if (isInBundledMode()) {
|
||||||
return {
|
return {
|
||||||
mode: 'embedded',
|
mode: 'embedded',
|
||||||
command: processExecPath,
|
command: process.execPath,
|
||||||
args: ['--no-config'],
|
args: ['--no-config'],
|
||||||
argv0: 'rg',
|
argv0: 'rg',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (builtinExists) {
|
|
||||||
return { mode: 'builtin', command: builtinCommand, args: [] }
|
|
||||||
}
|
|
||||||
|
|
||||||
if (systemExecutablePath !== 'rg') {
|
|
||||||
return { mode: 'system', command: 'rg', args: [] }
|
|
||||||
}
|
|
||||||
|
|
||||||
return { mode: 'builtin', command: builtinCommand, args: [] }
|
|
||||||
}
|
|
||||||
|
|
||||||
const getRipgrepConfig = memoize((): RipgrepConfig => {
|
|
||||||
const userWantsSystemRipgrep = isEnvDefinedFalsy(
|
|
||||||
process.env.USE_BUILTIN_RIPGREP,
|
|
||||||
)
|
|
||||||
const bundledMode = isInBundledMode()
|
|
||||||
const rgRoot = path.resolve(__dirname, 'vendor', 'ripgrep')
|
const rgRoot = path.resolve(__dirname, 'vendor', 'ripgrep')
|
||||||
const builtinCommand =
|
const command =
|
||||||
process.platform === 'win32'
|
process.platform === 'win32'
|
||||||
? path.resolve(rgRoot, `${process.arch}-win32`, 'rg.exe')
|
? path.resolve(rgRoot, `${process.arch}-win32`, 'rg.exe')
|
||||||
: path.resolve(rgRoot, `${process.arch}-${process.platform}`, 'rg')
|
: path.resolve(rgRoot, `${process.arch}-${process.platform}`, 'rg')
|
||||||
const builtinExists = existsSync(builtinCommand)
|
|
||||||
const { cmd: systemExecutablePath } = findExecutable('rg', [])
|
|
||||||
|
|
||||||
return resolveRipgrepConfig({
|
return { mode: 'builtin', command, args: [] }
|
||||||
userWantsSystemRipgrep,
|
|
||||||
bundledMode,
|
|
||||||
builtinCommand,
|
|
||||||
builtinExists,
|
|
||||||
systemExecutablePath,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export function ripgrepCommand(): {
|
export function ripgrepCommand(): {
|
||||||
@@ -357,9 +324,7 @@ async function ripGrepFileCount(
|
|||||||
if (settled) return
|
if (settled) return
|
||||||
settled = true
|
settled = true
|
||||||
reject(
|
reject(
|
||||||
isErrnoException(err) && err.code === 'ENOENT'
|
err.code === 'ENOENT' ? wrapRipgrepUnavailableError(err) : err,
|
||||||
? wrapRipgrepUnavailableError(err)
|
|
||||||
: err,
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -423,9 +388,7 @@ export async function ripGrepStream(
|
|||||||
if (settled) return
|
if (settled) return
|
||||||
settled = true
|
settled = true
|
||||||
reject(
|
reject(
|
||||||
isErrnoException(err) && err.code === 'ENOENT'
|
err.code === 'ENOENT' ? wrapRipgrepUnavailableError(err) : err,
|
||||||
? wrapRipgrepUnavailableError(err)
|
|
||||||
: err,
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -473,9 +436,7 @@ export async function ripGrep(
|
|||||||
const CRITICAL_ERROR_CODES = ['ENOENT', 'EACCES', 'EPERM']
|
const CRITICAL_ERROR_CODES = ['ENOENT', 'EACCES', 'EPERM']
|
||||||
if (CRITICAL_ERROR_CODES.includes(error.code as string)) {
|
if (CRITICAL_ERROR_CODES.includes(error.code as string)) {
|
||||||
reject(
|
reject(
|
||||||
isErrnoException(error) && error.code === 'ENOENT'
|
error.code === 'ENOENT' ? wrapRipgrepUnavailableError(error) : error,
|
||||||
? wrapRipgrepUnavailableError(error)
|
|
||||||
: error,
|
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
import { describe, expect, test } from 'bun:test'
|
|
||||||
|
|
||||||
import { sanitizeSchemaForOpenAICompat } from './schemaSanitizer'
|
|
||||||
|
|
||||||
describe('sanitizeSchemaForOpenAICompat', () => {
|
|
||||||
test('preserves Grep-like properties.pattern while keeping it required', () => {
|
|
||||||
const schema = {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: {
|
|
||||||
type: 'string',
|
|
||||||
description: 'The regular expression pattern to search for in file contents',
|
|
||||||
},
|
|
||||||
path: { type: 'string' },
|
|
||||||
glob: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
}
|
|
||||||
|
|
||||||
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
|
||||||
const properties = sanitized.properties as Record<string, unknown> | undefined
|
|
||||||
|
|
||||||
expect(Object.keys(properties ?? {})).toEqual(['pattern', 'path', 'glob'])
|
|
||||||
expect(properties?.pattern).toEqual({
|
|
||||||
type: 'string',
|
|
||||||
description: 'The regular expression pattern to search for in file contents',
|
|
||||||
})
|
|
||||||
expect(sanitized.required).toEqual(['pattern'])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('preserves Glob-like properties.pattern while keeping it required', () => {
|
|
||||||
const schema = {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
pattern: {
|
|
||||||
type: 'string',
|
|
||||||
description: 'The glob pattern to match files against',
|
|
||||||
},
|
|
||||||
path: { type: 'string' },
|
|
||||||
},
|
|
||||||
required: ['pattern'],
|
|
||||||
}
|
|
||||||
|
|
||||||
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
|
||||||
const properties = sanitized.properties as Record<string, unknown> | undefined
|
|
||||||
|
|
||||||
expect(Object.keys(properties ?? {})).toEqual(['pattern', 'path'])
|
|
||||||
expect(properties?.pattern).toEqual({
|
|
||||||
type: 'string',
|
|
||||||
description: 'The glob pattern to match files against',
|
|
||||||
})
|
|
||||||
expect(sanitized.required).toEqual(['pattern'])
|
|
||||||
})
|
|
||||||
|
|
||||||
test('strips JSON Schema validator pattern from string schemas', () => {
|
|
||||||
const schema = {
|
|
||||||
type: 'string',
|
|
||||||
pattern: '^[a-z]+$',
|
|
||||||
minLength: 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
const sanitized = sanitizeSchemaForOpenAICompat(schema)
|
|
||||||
|
|
||||||
expect(sanitized).toEqual({
|
|
||||||
type: 'string',
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
@@ -33,15 +33,6 @@ function stripSchemaKeywords(schema: unknown, keywords: Set<string>): unknown {
|
|||||||
|
|
||||||
const result: Record<string, unknown> = {}
|
const result: Record<string, unknown> = {}
|
||||||
for (const [key, value] of Object.entries(schema)) {
|
for (const [key, value] of Object.entries(schema)) {
|
||||||
if (key === 'properties' && isSchemaRecord(value)) {
|
|
||||||
const sanitizedProps: Record<string, unknown> = {}
|
|
||||||
for (const [propName, propSchema] of Object.entries(value)) {
|
|
||||||
sanitizedProps[propName] = stripSchemaKeywords(propSchema, keywords)
|
|
||||||
}
|
|
||||||
result[key] = sanitizedProps
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (keywords.has(key)) {
|
if (keywords.has(key)) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -224,13 +215,10 @@ export function sanitizeSchemaForOpenAICompat(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const properties = isSchemaRecord(record.properties)
|
if (Array.isArray(record.required) && isSchemaRecord(record.properties)) {
|
||||||
? record.properties
|
|
||||||
: undefined
|
|
||||||
|
|
||||||
if (Array.isArray(record.required) && properties) {
|
|
||||||
record.required = record.required.filter(
|
record.required = record.required.filter(
|
||||||
(value): value is string => typeof value === 'string' && value in properties,
|
(value): value is string =>
|
||||||
|
typeof value === 'string' && value in record.properties,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user