Merge branch 'Gitlawb:main' into development

This commit is contained in:
James Shawn Carnley
2026-04-02 12:43:24 -04:00
committed by GitHub
6 changed files with 422 additions and 217 deletions

107
.env.example Normal file
View File

@@ -0,0 +1,107 @@
# =============================================================================
# OpenClaude Environment Configuration
# =============================================================================
# Copy this file to .env and fill in your values:
# cp .env.example .env
#
# Only set the variables for the provider you want to use.
# All other sections can be left commented out.
# =============================================================================
# =============================================================================
# PROVIDER SELECTION — uncomment ONE block below
# =============================================================================
# -----------------------------------------------------------------------------
# Option 1: Anthropic (default — no provider flag needed)
# -----------------------------------------------------------------------------
ANTHROPIC_API_KEY=sk-ant-your-key-here
# Override the default model (optional)
# ANTHROPIC_MODEL=claude-sonnet-4-5
# Use a custom Anthropic-compatible endpoint (optional)
# ANTHROPIC_BASE_URL=https://api.anthropic.com
# -----------------------------------------------------------------------------
# Option 2: OpenAI
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_OPENAI=1
# OPENAI_API_KEY=sk-your-key-here
# OPENAI_MODEL=gpt-4o
# Use a custom OpenAI-compatible endpoint (optional — defaults to api.openai.com)
# OPENAI_BASE_URL=https://api.openai.com/v1
# -----------------------------------------------------------------------------
# Option 3: Google Gemini
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_GEMINI=1
# GEMINI_API_KEY=your-gemini-key-here
# GEMINI_MODEL=gemini-2.0-flash
# Use a custom Gemini endpoint (optional)
# GEMINI_BASE_URL=https://generativelanguage.googleapis.com
# -----------------------------------------------------------------------------
# Option 4: GitHub Models
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_GITHUB=1
# GITHUB_TOKEN=ghp_your-token-here
# -----------------------------------------------------------------------------
# Option 5: Ollama (local models)
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_OPENAI=1
# OPENAI_BASE_URL=http://localhost:11434/v1
# OPENAI_API_KEY=ollama
# OPENAI_MODEL=llama3.2
# -----------------------------------------------------------------------------
# Option 6: AWS Bedrock
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_BEDROCK=1
# AWS_REGION=us-east-1
# AWS_DEFAULT_REGION=us-east-1
# AWS_BEARER_TOKEN_BEDROCK=your-bearer-token-here
# ANTHROPIC_BEDROCK_BASE_URL=https://bedrock-runtime.us-east-1.amazonaws.com
# -----------------------------------------------------------------------------
# Option 7: Google Vertex AI
# -----------------------------------------------------------------------------
# CLAUDE_CODE_USE_VERTEX=1
# ANTHROPIC_VERTEX_PROJECT_ID=your-gcp-project-id
# CLOUD_ML_REGION=us-east5
# GOOGLE_CLOUD_PROJECT=your-gcp-project-id
# =============================================================================
# OPTIONAL TUNING
# =============================================================================
# Max number of API retries on failure (default: 10)
# CLAUDE_CODE_MAX_RETRIES=10
# Enable persistent retry mode for unattended/CI sessions
# Retries 429/529 indefinitely with smart backoff
# CLAUDE_CODE_UNATTENDED_RETRY=1
# Enable extended key reporting (Kitty keyboard protocol)
# Useful for iTerm2, WezTerm, Ghostty if modifier keys feel off
# OPENCLAUDE_ENABLE_EXTENDED_KEYS=1
# Disable "Co-authored-by" line in git commits made by OpenClaude
# OPENCLAUDE_DISABLE_CO_AUTHORED_BY=1
# Custom timeout for API requests in milliseconds (default: varies)
# API_TIMEOUT_MS=60000
# Enable debug logging
# CLAUDE_DEBUG=1

1
.gitignore vendored
View File

@@ -3,5 +3,6 @@ dist/
*.tsbuildinfo *.tsbuildinfo
.env .env
.env.* .env.*
!.env.example
.openclaude-profile.json .openclaude-profile.json
reports/ reports/

View File

@@ -1,216 +1 @@
function isSchemaRecord(value: unknown): value is Record<string, unknown> { export { sanitizeSchemaForOpenAICompat } from '../../utils/schemaSanitizer.js'
return value !== null && typeof value === 'object' && !Array.isArray(value)
}
function deepEqualJsonValue(a: unknown, b: unknown): boolean {
if (Object.is(a, b)) return true
if (typeof a !== typeof b) return false
if (Array.isArray(a) && Array.isArray(b)) {
return (
a.length === b.length &&
a.every((value, index) => deepEqualJsonValue(value, b[index]))
)
}
if (isSchemaRecord(a) && isSchemaRecord(b)) {
const aKeys = Object.keys(a)
const bKeys = Object.keys(b)
return (
aKeys.length === bKeys.length &&
aKeys.every(key => key in b && deepEqualJsonValue(a[key], b[key]))
)
}
return false
}
function matchesJsonSchemaType(type: string, value: unknown): boolean {
switch (type) {
case 'string':
return typeof value === 'string'
case 'number':
return typeof value === 'number' && Number.isFinite(value)
case 'integer':
return typeof value === 'number' && Number.isInteger(value)
case 'boolean':
return typeof value === 'boolean'
case 'object':
return value !== null && typeof value === 'object' && !Array.isArray(value)
case 'array':
return Array.isArray(value)
case 'null':
return value === null
default:
return true
}
}
function getJsonSchemaTypes(record: Record<string, unknown>): string[] {
const raw = record.type
if (typeof raw === 'string') {
return [raw]
}
if (Array.isArray(raw)) {
return raw.filter((value): value is string => typeof value === 'string')
}
return []
}
function schemaAllowsValue(schema: Record<string, unknown>, value: unknown): boolean {
if (Array.isArray(schema.anyOf)) {
return schema.anyOf.some(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
)
}
if (Array.isArray(schema.oneOf)) {
return (
schema.oneOf.filter(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
).length === 1
)
}
if (Array.isArray(schema.allOf)) {
return schema.allOf.every(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
)
}
if ('const' in schema && !deepEqualJsonValue(schema.const, value)) {
return false
}
if (Array.isArray(schema.enum)) {
if (!schema.enum.some(item => deepEqualJsonValue(item, value))) {
return false
}
}
const types = getJsonSchemaTypes(schema)
if (types.length > 0 && !types.some(type => matchesJsonSchemaType(type, value))) {
return false
}
return true
}
function sanitizeTypeField(record: Record<string, unknown>): void {
const allowed = new Set([
'string',
'number',
'integer',
'boolean',
'object',
'array',
'null',
])
const raw = record.type
if (typeof raw === 'string') {
if (!allowed.has(raw)) delete record.type
return
}
if (!Array.isArray(raw)) return
const filtered = raw.filter(
(value, index): value is string =>
typeof value === 'string' &&
allowed.has(value) &&
raw.indexOf(value) === index,
)
if (filtered.length === 0) {
delete record.type
} else if (filtered.length === 1) {
record.type = filtered[0]
} else {
record.type = filtered
}
}
/**
* Sanitize loose/invalid JSON Schema into a form OpenAI-compatible providers
* are more likely to accept. This is intentionally defensive for external MCP
* servers that may advertise imperfect schemas.
*/
export function sanitizeSchemaForOpenAICompat(
schema: unknown,
): Record<string, unknown> {
if (!isSchemaRecord(schema)) {
return {}
}
const record = { ...schema }
delete record.$schema
delete record.propertyNames
sanitizeTypeField(record)
if (isSchemaRecord(record.properties)) {
const sanitizedProps: Record<string, unknown> = {}
for (const [key, value] of Object.entries(record.properties)) {
sanitizedProps[key] = sanitizeSchemaForOpenAICompat(value)
}
record.properties = sanitizedProps
}
if ('items' in record) {
if (Array.isArray(record.items)) {
record.items = record.items.map(item =>
sanitizeSchemaForOpenAICompat(item),
)
} else {
record.items = sanitizeSchemaForOpenAICompat(record.items)
}
}
for (const key of ['anyOf', 'oneOf', 'allOf'] as const) {
if (Array.isArray(record[key])) {
record[key] = record[key].map(item =>
sanitizeSchemaForOpenAICompat(item),
)
}
}
if (Array.isArray(record.required) && isSchemaRecord(record.properties)) {
record.required = record.required.filter(
(value): value is string =>
typeof value === 'string' && value in record.properties,
)
}
const schemaWithoutEnum = { ...record }
delete schemaWithoutEnum.enum
if (Array.isArray(record.enum)) {
const filteredEnum = record.enum.filter(value =>
schemaAllowsValue(schemaWithoutEnum, value),
)
if (filteredEnum.length > 0) {
record.enum = filteredEnum
} else {
delete record.enum
}
}
const schemaWithoutConst = { ...record }
delete schemaWithoutConst.const
if ('const' in record && !schemaAllowsValue(schemaWithoutConst, record.const)) {
delete record.const
}
const schemaWithoutDefault = { ...record }
delete schemaWithoutDefault.default
if (
'default' in record &&
!schemaAllowsValue(schemaWithoutDefault, record.default)
) {
delete record.default
}
return record
}

View File

@@ -38,8 +38,8 @@ import {
resolveCodexApiCredentials, resolveCodexApiCredentials,
resolveProviderRequest, resolveProviderRequest,
} from './providerConfig.js' } from './providerConfig.js'
import { sanitizeSchemaForOpenAICompat } from '../../utils/schemaSanitizer.js'
import { redactSecretValueForDisplay } from '../../utils/providerProfile.js' import { redactSecretValueForDisplay } from '../../utils/providerProfile.js'
import { sanitizeSchemaForOpenAICompat } from './openaiSchemaSanitizer.js'
const GITHUB_MODELS_DEFAULT_BASE = 'https://models.github.ai/inference' const GITHUB_MODELS_DEFAULT_BASE = 'https://models.github.ai/inference'
const GITHUB_API_VERSION = '2022-11-28' const GITHUB_API_VERSION = '2022-11-28'

66
src/utils/api.test.ts Normal file
View File

@@ -0,0 +1,66 @@
import { expect, test } from 'bun:test'
import { z } from 'zod/v4'
import { getEmptyToolPermissionContext, type Tool, type Tools } from '../Tool.js'
import { toolToAPISchema } from './api.js'
test('toolToAPISchema preserves provider-specific schema keywords in input_schema', async () => {
const schema = await toolToAPISchema(
{
name: 'WebFetch',
inputSchema: z.strictObject({}),
inputJSONSchema: {
type: 'object',
properties: {
url: {
type: 'string',
format: 'uri',
description: 'Public HTTP or HTTPS URL',
},
metadata: {
type: 'object',
propertyNames: {
pattern: '^[a-z]+$',
},
properties: {
callback: {
type: 'string',
format: 'uri-reference',
},
},
},
},
},
prompt: async () => 'Fetch a URL',
} as unknown as Tool,
{
getToolPermissionContext: async () => getEmptyToolPermissionContext(),
tools: [] as unknown as Tools,
agents: [],
},
)
expect(schema).toMatchObject({
input_schema: {
type: 'object',
properties: {
url: {
type: 'string',
format: 'uri',
description: 'Public HTTP or HTTPS URL',
},
metadata: {
type: 'object',
propertyNames: {
pattern: '^[a-z]+$',
},
properties: {
callback: {
type: 'string',
format: 'uri-reference',
},
},
},
},
},
})
})

View File

@@ -0,0 +1,246 @@
const OPENAI_INCOMPATIBLE_SCHEMA_KEYWORDS = new Set([
'$comment',
'$schema',
'default',
'else',
'examples',
'format',
'if',
'maxLength',
'maximum',
'minLength',
'minimum',
'multipleOf',
'pattern',
'patternProperties',
'propertyNames',
'then',
'unevaluatedProperties',
])
function isSchemaRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' && !Array.isArray(value)
}
function stripSchemaKeywords(schema: unknown, keywords: Set<string>): unknown {
if (Array.isArray(schema)) {
return schema.map(item => stripSchemaKeywords(item, keywords))
}
if (!isSchemaRecord(schema)) {
return schema
}
const result: Record<string, unknown> = {}
for (const [key, value] of Object.entries(schema)) {
if (keywords.has(key)) {
continue
}
result[key] = stripSchemaKeywords(value, keywords)
}
return result
}
function deepEqualJsonValue(a: unknown, b: unknown): boolean {
if (Object.is(a, b)) return true
if (typeof a !== typeof b) return false
if (Array.isArray(a) && Array.isArray(b)) {
return (
a.length === b.length &&
a.every((value, index) => deepEqualJsonValue(value, b[index]))
)
}
if (isSchemaRecord(a) && isSchemaRecord(b)) {
const aKeys = Object.keys(a)
const bKeys = Object.keys(b)
return (
aKeys.length === bKeys.length &&
aKeys.every(key => key in b && deepEqualJsonValue(a[key], b[key]))
)
}
return false
}
function matchesJsonSchemaType(type: string, value: unknown): boolean {
switch (type) {
case 'string':
return typeof value === 'string'
case 'number':
return typeof value === 'number' && Number.isFinite(value)
case 'integer':
return typeof value === 'number' && Number.isInteger(value)
case 'boolean':
return typeof value === 'boolean'
case 'object':
return value !== null && typeof value === 'object' && !Array.isArray(value)
case 'array':
return Array.isArray(value)
case 'null':
return value === null
default:
return true
}
}
function getJsonSchemaTypes(record: Record<string, unknown>): string[] {
const raw = record.type
if (typeof raw === 'string') {
return [raw]
}
if (Array.isArray(raw)) {
return raw.filter((value): value is string => typeof value === 'string')
}
return []
}
function schemaAllowsValue(schema: Record<string, unknown>, value: unknown): boolean {
if (Array.isArray(schema.anyOf)) {
return schema.anyOf.some(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
)
}
if (Array.isArray(schema.oneOf)) {
return (
schema.oneOf.filter(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
).length === 1
)
}
if (Array.isArray(schema.allOf)) {
return schema.allOf.every(item =>
schemaAllowsValue(sanitizeSchemaForOpenAICompat(item), value),
)
}
if ('const' in schema && !deepEqualJsonValue(schema.const, value)) {
return false
}
if (Array.isArray(schema.enum)) {
if (!schema.enum.some(item => deepEqualJsonValue(item, value))) {
return false
}
}
const types = getJsonSchemaTypes(schema)
if (types.length > 0 && !types.some(type => matchesJsonSchemaType(type, value))) {
return false
}
return true
}
function sanitizeTypeField(record: Record<string, unknown>): void {
const allowed = new Set([
'string',
'number',
'integer',
'boolean',
'object',
'array',
'null',
])
const raw = record.type
if (typeof raw === 'string') {
if (!allowed.has(raw)) delete record.type
return
}
if (!Array.isArray(raw)) return
const filtered = raw.filter(
(value, index): value is string =>
typeof value === 'string' &&
allowed.has(value) &&
raw.indexOf(value) === index,
)
if (filtered.length === 0) {
delete record.type
} else if (filtered.length === 1) {
record.type = filtered[0]
} else {
record.type = filtered
}
}
/**
* Sanitize JSON Schema into a shape OpenAI-compatible providers and Codex
* strict-mode tooling are more likely to accept. This strips provider-rejected
* keywords while keeping enum/const cleanup defensive for imperfect MCP schemas.
*/
export function sanitizeSchemaForOpenAICompat(
schema: unknown,
): Record<string, unknown> {
const stripped = stripSchemaKeywords(schema, OPENAI_INCOMPATIBLE_SCHEMA_KEYWORDS)
if (!isSchemaRecord(stripped)) {
return {}
}
const record = { ...stripped }
sanitizeTypeField(record)
if (isSchemaRecord(record.properties)) {
const sanitizedProps: Record<string, unknown> = {}
for (const [key, value] of Object.entries(record.properties)) {
sanitizedProps[key] = sanitizeSchemaForOpenAICompat(value)
}
record.properties = sanitizedProps
}
if ('items' in record) {
if (Array.isArray(record.items)) {
record.items = record.items.map(item =>
sanitizeSchemaForOpenAICompat(item),
)
} else {
record.items = sanitizeSchemaForOpenAICompat(record.items)
}
}
for (const key of ['anyOf', 'oneOf', 'allOf'] as const) {
if (Array.isArray(record[key])) {
record[key] = record[key].map(item =>
sanitizeSchemaForOpenAICompat(item),
)
}
}
if (Array.isArray(record.required) && isSchemaRecord(record.properties)) {
record.required = record.required.filter(
(value): value is string =>
typeof value === 'string' && value in record.properties,
)
}
const schemaWithoutEnum = { ...record }
delete schemaWithoutEnum.enum
if (Array.isArray(record.enum)) {
const filteredEnum = record.enum.filter(value =>
schemaAllowsValue(schemaWithoutEnum, value),
)
if (filteredEnum.length > 0) {
record.enum = filteredEnum
} else {
delete record.enum
}
}
const schemaWithoutConst = { ...record }
delete schemaWithoutConst.const
if ('const' in record && !schemaAllowsValue(schemaWithoutConst, record.const)) {
delete record.const
}
return record
}