Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test } from 'bun:test'

import { prepareTools } from './openai-compatible-prepare-tools'

test('keeps keyword-named tool properties while inlining their schemas', () => {
const { tools } = prepareTools({
tools: [
{
type: 'function',
name: 'create_definitions',
inputSchema: {
type: 'object',
$defs: { text: { type: 'string' } },
properties: {
definitions: { $ref: '#/$defs/text' },
$defs: { $ref: '#/$defs/text' },
name: { $ref: '#/$defs/text' },
},
required: ['definitions', '$defs', 'name'],
additionalProperties: false,
},
},
],
})

expect(tools?.[0].function.parameters).toEqual({
type: 'object',
properties: {
definitions: { type: 'string' },
$defs: { type: 'string' },
name: { type: 'string' },
},
required: ['definitions', '$defs', 'name'],
additionalProperties: false,
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ function lookupJsonPointer(root: unknown, pointer: string) {
function inlineLocalSchemaRefs(schema: unknown): unknown {
const root = isRecord(schema) && 'jsonSchema' in schema ? schema.jsonSchema : schema

const visit = (value: unknown, refStack: Set<string>): unknown => {
const visit = (
value: unknown,
refStack: Set<string>,
isPropertyMap = false,
): unknown => {
if (Array.isArray(value)) {
return value.map((item) => visit(item, refStack))
}

if (!isRecord(value)) return value

// Keys inside `properties` are field names, not schema keywords.
if (isPropertyMap) {
return Object.fromEntries(
Object.entries(value).map(([name, child]) => [
name,
visit(child, refStack),
]),
)
}

const ref = typeof value.$ref === 'string' ? value.$ref : undefined
if (ref?.startsWith('#/')) {
if (refStack.has(ref)) return {}
Expand All @@ -59,7 +73,7 @@ function inlineLocalSchemaRefs(schema: unknown): unknown {
const result: Record<string, unknown> = {}
for (const [key, child] of Object.entries(value)) {
if (key === '$defs' || key === 'definitions') continue
result[key] = visit(child, refStack)
result[key] = visit(child, refStack, key === 'properties')
}
return result
}
Expand Down
Loading