mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
fix(core): convert each tool schema to JSON Schema once
This commit is contained in:
parent
8ec08f4913
commit
7ee9a75870
2 changed files with 17 additions and 5 deletions
|
|
@ -166,16 +166,28 @@ const validateStandard = (
|
|||
}),
|
||||
)
|
||||
|
||||
// Tool lists are rebuilt on every config change in every Location, so convert each schema once.
|
||||
const inputJsonSchemas = new WeakMap<object, JsonSchema.JsonSchema>()
|
||||
const outputJsonSchemas = new WeakMap<object, JsonSchema.JsonSchema>()
|
||||
|
||||
const cached = (cache: WeakMap<object, JsonSchema.JsonSchema>, schema: object, make: () => JsonSchema.JsonSchema) => {
|
||||
const hit = cache.get(schema)
|
||||
if (hit) return hit
|
||||
const value = make()
|
||||
cache.set(schema, value)
|
||||
return value
|
||||
}
|
||||
|
||||
const inputJsonSchema = (schema: Tool.ValueSchema<any>): JsonSchema.JsonSchema => {
|
||||
if (schema === undefined || schema === null) return {}
|
||||
if (isStandardSchema(schema)) return standardJsonSchema(schema, "input")
|
||||
if (isStandardSchema(schema)) return cached(inputJsonSchemas, schema, () => standardJsonSchema(schema, "input"))
|
||||
if (!Schema.isSchema(schema)) return schema
|
||||
return emptyInputJsonSchema(schema) ?? toJsonSchema(schema)
|
||||
return cached(inputJsonSchemas, schema, () => emptyInputJsonSchema(schema) ?? toJsonSchema(schema))
|
||||
}
|
||||
|
||||
const outputJsonSchema = (schema: Tool.ValueSchema<any>): JsonSchema.JsonSchema => {
|
||||
if (isStandardSchema(schema)) return standardJsonSchema(schema, "output")
|
||||
return Schema.isSchema(schema) ? toJsonSchema(schema) : schema
|
||||
if (isStandardSchema(schema)) return cached(outputJsonSchemas, schema, () => standardJsonSchema(schema, "output"))
|
||||
return Schema.isSchema(schema) ? cached(outputJsonSchemas, schema, () => toJsonSchema(schema)) : schema
|
||||
}
|
||||
|
||||
const standardJsonSchema = (schema: StandardSchemaV1<any, any>, io: "input" | "output"): JsonSchema.JsonSchema => {
|
||||
|
|
|
|||
|
|
@ -995,7 +995,7 @@ test("executions share the prepared runtime of an inventory while keeping per-ca
|
|||
await Effect.runPromise(
|
||||
createCodeMode(new Map(inventory.tools)).execute({ code: 'return await tools.echo({ value: "next" })' }, context),
|
||||
)
|
||||
expect(conversions).toBe(2)
|
||||
expect(conversions).toBe(1)
|
||||
})
|
||||
|
||||
const progressCodeMode = (started: Deferred.Deferred<void>, wait: Effect.Effect<void>) =>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue