@opencode/ai 0.0.0-dev-20091 → 0.0.0-dev-20092

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -897,7 +897,6 @@ const fromRequest = Effect.fn("AnthropicMessages.fromRequest")(function* (reques
897
897
  const format = outputConfig?.format ?? undefined;
898
898
  const updates = resolveEffortUpdates(request, options.effort ?? outputConfig?.effort ?? undefined);
899
899
  const generation = request.generation;
900
- const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
901
900
  // Allocate the 4-breakpoint budget in invalidation order: tools → system →
902
901
  // messages. Tools live highest in the cache hierarchy, so when callers
903
902
  // over-mark we keep their tool hints and shed the message-tail ones first.
@@ -905,7 +904,7 @@ const fromRequest = Effect.fn("AnthropicMessages.fromRequest")(function* (reques
905
904
  const flattened = ProviderShared.flattenToolRequest(updates.request);
906
905
  const tools = flattened.tools.length === 0
907
906
  ? undefined
908
- : flattened.tools.map((tool) => lowerTool(breakpoints, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility)));
907
+ : flattened.tools.map((tool) => lowerTool(breakpoints, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, request.model)));
909
908
  // Anthropic rejects tool_choice when tools are absent; "none" is only meaningful with tools present.
910
909
  const toolChoice = tools === undefined || !request.toolChoice ? undefined : yield* lowerToolChoice(request.toolChoice);
911
910
  const systemParts = request.system.filter((part) => part.text.length > 0);
@@ -163,10 +163,10 @@ const lowerToolSpec = (tool, inputSchema) => ({
163
163
  inputSchema: { json: inputSchema },
164
164
  },
165
165
  });
166
- const lowerTools = (compatibility, breakpoints, tools) => {
166
+ const lowerTools = (model, breakpoints, tools) => {
167
167
  const result = [];
168
168
  for (const tool of tools) {
169
- result.push(lowerToolSpec(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, compatibility)));
169
+ result.push(lowerToolSpec(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, model)));
170
170
  const cachePoint = BedrockCache.block(breakpoints, tool.cache);
171
171
  if (cachePoint)
172
172
  result.push(cachePoint);
@@ -349,7 +349,7 @@ const fromRequest = Effect.fn("BedrockConverse.fromRequest")(function* (request)
349
349
  if (flattened.tools.length === 0)
350
350
  return undefined;
351
351
  return {
352
- tools: lowerTools(request.model.compatibility?.toolSchema, breakpoints, flattened.tools),
352
+ tools: lowerTools(request.model, breakpoints, flattened.tools),
353
353
  // Converse has no native "none". Keep definitions stable for prompt
354
354
  // caching and omit only the unsupported choice.
355
355
  toolChoice,
@@ -10,7 +10,6 @@ import { classifyProviderFailure } from "../provider-error.js";
10
10
  import { Media } from "../media.js";
11
11
  import { JsonObject, knownString, lenient, optionalArray, optionalNull, ProviderShared } from "./shared.js";
12
12
  import { GeminiGenerateContent } from "./utils/gemini-generate-content.js";
13
- import { GeminiJsonSchema } from "./utils/gemini-json-schema.js";
14
13
  import { Lifecycle } from "./utils/lifecycle.js";
15
14
  import { ToolSchemaProjection } from "./utils/tool-schema.js";
16
15
  const ADAPTER = "gemini";
@@ -189,12 +188,12 @@ const GeminiEvent = Schema.Struct({
189
188
  // =============================================================================
190
189
  // Request Lowering
191
190
  // =============================================================================
192
- // Tool schemas go in `parametersJsonSchema`, which accepts standard JSON Schema.
193
- // `utils/gemini-json-schema` rewrites only the few shapes Gemini still rejects.
194
- const lowerTool = (tool, inputSchema) => ({
191
+ // Tool schemas go in `parametersJsonSchema`, which accepts standard JSON Schema. Gemini's schema
192
+ // rules are this API's default, including for tuned endpoints whose IDs do not name Gemini.
193
+ const lowerTool = (tool, model) => ({
195
194
  name: tool.name,
196
195
  description: tool.description,
197
- parametersJsonSchema: GeminiJsonSchema.normalize(inputSchema),
196
+ parametersJsonSchema: ToolSchemaProjection.modelCompatibility(tool.inputSchema, model, "gemini"),
198
197
  });
199
198
  const lowerToolConfig = (toolChoice) => ProviderShared.matchToolChoice("Gemini", toolChoice, {
200
199
  auto: () => ({ functionCallingConfig: { mode: "AUTO" } }),
@@ -358,7 +357,6 @@ const fromRequest = Effect.fn("Gemini.fromRequest")(function* (request) {
358
357
  const hasTools = flattened.tools.length > 0;
359
358
  const generation = request.generation;
360
359
  const options = yield* decodeOptions(request.providerOptions ?? {});
361
- const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
362
360
  const generationConfig = {
363
361
  maxOutputTokens: generation?.maxTokens,
364
362
  temperature: generation?.temperature,
@@ -381,7 +379,7 @@ const fromRequest = Effect.fn("Gemini.fromRequest")(function* (request) {
381
379
  tools: hasTools
382
380
  ? [
383
381
  {
384
- functionDeclarations: flattened.tools.map((tool) => lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility))),
382
+ functionDeclarations: flattened.tools.map((tool) => lowerTool(tool, request.model)),
385
383
  },
386
384
  ]
387
385
  : undefined,
@@ -82,7 +82,7 @@ const fromRequest = Effect.fn("MetaResponses.fromRequest")(function* (request) {
82
82
  ? undefined
83
83
  : yield* Effect.forEach(projected.tools, (tool) => Effect.gen(function* () {
84
84
  if (tool.native === undefined)
85
- return yield* OpenResponses.lowerTool(NAME, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, request.model.compatibility?.toolSchema));
85
+ return yield* OpenResponses.lowerTool(NAME, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, request.model));
86
86
  return yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(NativeTool))(tool.native.meta);
87
87
  })),
88
88
  tool_choice: OpenResponses.allowedToolChoice(request) ??
@@ -603,7 +603,6 @@ export const allowedToolChoice = (request) => {
603
603
  };
604
604
  export const fromRequestWithAdapter = Effect.fn("OpenResponses.fromRequestWithAdapter")(function* (request, adapter) {
605
605
  const projected = ProviderShared.flattenToolRequest(request);
606
- const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
607
606
  return {
608
607
  ...(yield* lowerConversation(projected.request, adapter)),
609
608
  ...lowerGeneration(request),
@@ -611,7 +610,7 @@ export const fromRequestWithAdapter = Effect.fn("OpenResponses.fromRequestWithAd
611
610
  ? undefined
612
611
  : yield* Effect.forEach(projected.tools, (tool) => tool.native !== undefined && adapter.nativeTool
613
612
  ? adapter.nativeTool(tool.native)
614
- : lowerTool(adapter.name, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility))),
613
+ : lowerTool(adapter.name, tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, request.model))),
615
614
  tool_choice: allowedToolChoice(request) ??
616
615
  (request.toolChoice ? yield* lowerToolChoice(adapter.name, request.toolChoice) : undefined),
617
616
  };
@@ -631,7 +631,6 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
631
631
  if (reasoningField && RESERVED_REASONING_FIELDS.has(reasoningField))
632
632
  return yield* ProviderShared.invalidRequest(`OpenAI Chat reasoning field conflicts with reserved field ${reasoningField}`);
633
633
  const generation = request.generation;
634
- const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
635
634
  const flattened = ProviderShared.flattenToolRequest(request);
636
635
  const provider = String(request.model.provider);
637
636
  const baseURL = request.model.route.endpoint.baseURL;
@@ -650,7 +649,7 @@ export const fromRequest = Effect.fn("OpenAIChat.fromRequest")(function* (reques
650
649
  ? hasHistory
651
650
  ? []
652
651
  : undefined
653
- : flattened.tools.map((tool) => lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, toolSchemaCompatibility), options, supportsStrictMode)),
652
+ : flattened.tools.map((tool) => lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, request.model), options, supportsStrictMode)),
654
653
  tool_choice: hasActiveTools && request.toolChoice ? yield* lowerToolChoice(request.toolChoice) : undefined,
655
654
  stream: true,
656
655
  ...(supportsUsageInStreaming ? { stream_options: { include_usage: true } } : {}),
@@ -5,7 +5,7 @@ import { Auth } from "../route/auth.js";
5
5
  import { Endpoint } from "../route/endpoint.js";
6
6
  import { Protocol } from "../route/protocol.js";
7
7
  import { HttpTransport } from "../route/transport/index.js";
8
- import { LLMRequest, mergeJsonRecords } from "../schema/index.js";
8
+ import { LLMRequest, mergeJsonRecords, } from "../schema/index.js";
9
9
  import { resolveEffortUpdates } from "../effort-updates.js";
10
10
  import { OpenResponses } from "./open-responses.js";
11
11
  import { OpenResponsesOptions } from "./utils/open-responses-options.js";
@@ -137,16 +137,16 @@ const lowerTool = Effect.fn("OpenAIResponses.lowerTool")(function* (tool, inputS
137
137
  });
138
138
  // Native namespaces hold only function tools, so deeper levels flatten into
139
139
  // the leaf names the same way non-native protocols flatten the whole tree.
140
- const lowerToolEntry = Effect.fn("OpenAIResponses.lowerToolEntry")(function* (tool, compatibility) {
140
+ const lowerToolEntry = Effect.fn("OpenAIResponses.lowerToolEntry")(function* (tool, model) {
141
141
  if (tool.type === "tool")
142
- return yield* lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, compatibility));
142
+ return yield* lowerTool(tool, ToolSchemaProjection.modelCompatibility(tool.inputSchema, model));
143
143
  // OpenAI requires a namespace description; fall back to a generic one so a
144
144
  // missing description never blocks the request.
145
145
  return {
146
146
  type: "namespace",
147
147
  name: tool.name,
148
148
  description: tool.description ?? `Tools in the ${tool.name} namespace.`,
149
- tools: yield* Effect.forEach(ProviderShared.flattenTools(tool.tools), (leaf) => OpenResponses.lowerTool(NAME, leaf, ToolSchemaProjection.modelCompatibility(leaf.inputSchema, compatibility))),
149
+ tools: yield* Effect.forEach(ProviderShared.flattenTools(tool.tools), (leaf) => OpenResponses.lowerTool(NAME, leaf, ToolSchemaProjection.modelCompatibility(leaf.inputSchema, model))),
150
150
  };
151
151
  });
152
152
  const lowerToolChoice = (toolChoice, tools) => ProviderShared.matchToolChoice(NAME, toolChoice, {
@@ -162,14 +162,13 @@ const fromRequest = Effect.fn("OpenAIResponses.fromRequest")(function* (request)
162
162
  const management = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(Schema.UndefinedOr(ContextManagement)))(request.providerOptions?.contextManagement);
163
163
  const options = OpenResponsesOptions.resolve(request);
164
164
  const updates = resolveEffortUpdates(request, options.reasoningEffort);
165
- const toolSchemaCompatibility = request.model.compatibility?.toolSchema;
166
165
  return yield* decodeBody({
167
166
  ...(yield* OpenResponses.lowerConversation(updates.request, adapter)),
168
167
  ...OpenResponses.lowerGeneration(request, { ...options, reasoningEffort: updates.effort }),
169
168
  context_management: management?.map((edit) => ({ type: edit.type, compact_threshold: edit.compactThreshold })),
170
169
  tools: request.tools.length === 0
171
170
  ? undefined
172
- : yield* Effect.forEach(request.tools, (tool) => lowerToolEntry(tool, toolSchemaCompatibility)),
171
+ : yield* Effect.forEach(request.tools, (tool) => lowerToolEntry(tool, request.model)),
173
172
  tool_choice: request.tools.length === 0
174
173
  ? undefined
175
174
  : (OpenResponses.allowedToolChoice(request) ??
@@ -1,7 +1,7 @@
1
- import type { JsonSchema, LanguageModelToolSchemaCompatibility } from "../../schema/index.js";
1
+ import type { JsonSchema, LanguageModel, LanguageModelSanitizerCompatibility } from "../../schema/index.js";
2
2
  export declare const ToolSchemaProjection: {
3
3
  readonly gemini: (schema: JsonSchema) => JsonSchema;
4
- readonly modelCompatibility: (schema: JsonSchema, compatibility: LanguageModelToolSchemaCompatibility | undefined) => JsonSchema;
4
+ readonly modelCompatibility: (schema: JsonSchema, model: LanguageModel, protocolDefault?: LanguageModelSanitizerCompatibility) => JsonSchema;
5
5
  readonly moonshot: (schema: JsonSchema) => JsonSchema;
6
6
  readonly openAI: (schema: JsonSchema) => JsonSchema;
7
7
  readonly responses: (schema: JsonSchema) => JsonSchema;
@@ -1,5 +1,5 @@
1
1
  import { isRecord } from "../../utils/record.js";
2
- import { GeminiToolSchema } from "./gemini-tool-schema.js";
2
+ import { GeminiJsonSchema } from "./gemini-json-schema.js";
3
3
  const tupleItemsSchema = (items) => {
4
4
  const projected = items.map(moonshotNode);
5
5
  if (projected.length === 0)
@@ -49,15 +49,23 @@ const moonshot = (schema) => {
49
49
  };
50
50
  const openAI = (schema) => schema;
51
51
  const responses = openAI;
52
- const gemini = (schema) => GeminiToolSchema.convert(schema) ?? {};
53
- const modelCompatibility = (schema, compatibility) => {
54
- if (compatibility === undefined)
55
- return schema;
56
- switch (compatibility) {
52
+ const gemini = GeminiJsonSchema.normalize;
53
+ const MODEL_NAMES = [
54
+ [/gemini/i, "gemini"],
55
+ [/kimi/i, "moonshot"],
56
+ ];
57
+ // An explicit `sanitizer` wins, and `none` opts out. Otherwise the protocol's own default
58
+ // applies (the Gemini API always uses Gemini's rules), then the model name selects the family's rules
59
+ // so models reached through gateways and OpenAI-compatible endpoints get the same handling.
60
+ const modelCompatibility = (schema, model, protocolDefault) => {
61
+ switch (model.compatibility?.sanitizer ?? protocolDefault ?? MODEL_NAMES.find(([name]) => name.test(model.id))?.[1]) {
57
62
  case "gemini":
58
63
  return gemini(schema);
59
64
  case "moonshot":
60
65
  return moonshot(schema);
66
+ case "none":
67
+ case undefined:
68
+ return schema;
61
69
  }
62
70
  };
63
71
  export const ToolSchemaProjection = {
@@ -64,17 +64,17 @@ export const configure = (input = {}) => {
64
64
  supportsStore: false,
65
65
  supportsStrictMode: false,
66
66
  supportsPromptCacheKey: true,
67
- toolSchema: "moonshot",
67
+ sanitizer: "moonshot",
68
68
  reasoningField: "reasoning_content",
69
69
  },
70
70
  });
71
71
  const messages = (modelID) => messagesRoute.with(defaults).model({
72
72
  id: modelID,
73
- compatibility: { requireSignature: false, toolSchema: "moonshot" },
73
+ compatibility: { requireSignature: false, sanitizer: "moonshot" },
74
74
  });
75
75
  const responses = (modelID) => responsesRoute
76
76
  .with(defaults)
77
- .model({ id: modelID, compatibility: { toolSchema: "moonshot" } });
77
+ .model({ id: modelID, compatibility: { sanitizer: "moonshot" } });
78
78
  return { id, model: chat, chat, messages, responses, configure };
79
79
  };
80
80
  export const provider = configure();
@@ -70,12 +70,13 @@ export declare namespace LanguageModelDefaults {
70
70
  export declare const ReasoningEfforts: readonly ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
71
71
  export type ReasoningEffort = (typeof ReasoningEfforts)[number] | (string & {});
72
72
  export declare const ReasoningEffort: Schema.declare<ReasoningEffort, ReasoningEffort>;
73
- export declare const LanguageModelToolSchemaCompatibility: Schema.Literals<readonly ["gemini", "moonshot"]>;
74
- export type LanguageModelToolSchemaCompatibility = Schema.Schema.Type<typeof LanguageModelToolSchemaCompatibility>;
73
+ /** Tool schema sanitizer for a model family. `none` opts out of the protocol and model-name defaults. */
74
+ export declare const LanguageModelSanitizerCompatibility: Schema.Literals<readonly ["gemini", "moonshot", "none"]>;
75
+ export type LanguageModelSanitizerCompatibility = Schema.Schema.Type<typeof LanguageModelSanitizerCompatibility>;
75
76
  export declare const LanguageModelMaxTokensFieldCompatibility: Schema.Literals<readonly ["max_completion_tokens", "max_tokens"]>;
76
77
  export type LanguageModelMaxTokensFieldCompatibility = Schema.Schema.Type<typeof LanguageModelMaxTokensFieldCompatibility>;
77
78
  declare const LanguageModelCompatibility_base: Schema.Class<LanguageModelCompatibility, Schema.Struct<{
78
- readonly toolSchema: Schema.optional<Schema.Literals<readonly ["gemini", "moonshot"]>>;
79
+ readonly sanitizer: Schema.optional<Schema.Literals<readonly ["gemini", "moonshot", "none"]>>;
79
80
  readonly reasoningField: Schema.optional<Schema.String>;
80
81
  /** Require every assistant message to include its reasoning field, even when empty. */
81
82
  readonly requireReasoning: Schema.optional<Schema.Boolean>;
@@ -99,10 +99,11 @@ export class LanguageModelDefaults extends Schema.Class("LLM.LanguageModelDefaul
99
99
  })(LanguageModelDefaults || (LanguageModelDefaults = {}));
100
100
  export const ReasoningEfforts = ["none", "minimal", "low", "medium", "high", "xhigh", "max"];
101
101
  export const ReasoningEffort = Schema.declare((value) => typeof value === "string", { title: "ReasoningEffort" });
102
- export const LanguageModelToolSchemaCompatibility = Schema.Literals(["gemini", "moonshot"]);
102
+ /** Tool schema sanitizer for a model family. `none` opts out of the protocol and model-name defaults. */
103
+ export const LanguageModelSanitizerCompatibility = Schema.Literals(["gemini", "moonshot", "none"]);
103
104
  export const LanguageModelMaxTokensFieldCompatibility = Schema.Literals(["max_completion_tokens", "max_tokens"]);
104
105
  export class LanguageModelCompatibility extends Schema.Class("LLM.LanguageModelCompatibility")({
105
- toolSchema: Schema.optional(LanguageModelToolSchemaCompatibility),
106
+ sanitizer: Schema.optional(LanguageModelSanitizerCompatibility),
106
107
  reasoningField: Schema.optional(Schema.String),
107
108
  /** Require every assistant message to include its reasoning field, even when empty. */
108
109
  requireReasoning: Schema.optional(Schema.Boolean),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
- "version": "0.0.0-dev-20091",
3
+ "version": "0.0.0-dev-20092",
4
4
  "name": "@opencode/ai",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "devDependencies": {
35
35
  "@clack/prompts": "1.0.0-alpha.1",
36
36
  "@effect/platform-node": "4.0.0-rc.112",
37
- "@opencode/http-recorder": "0.0.0-dev-20091",
37
+ "@opencode/http-recorder": "0.0.0-dev-20092",
38
38
  "@tsconfig/bun": "1.0.9",
39
39
  "@types/bun": "1.4.0",
40
40
  "@typescript/native-preview": "7.0.0-dev.20251207.1",
@@ -44,7 +44,7 @@
44
44
  "@aws-sdk/credential-providers": "3.1057.0",
45
45
  "@smithy/eventstream-codec": "4.2.14",
46
46
  "@smithy/util-utf8": "4.2.2",
47
- "@opencode/schema": "0.0.0-dev-20091",
47
+ "@opencode/schema": "0.0.0-dev-20092",
48
48
  "aws4fetch": "1.0.20",
49
49
  "effect": "4.0.0-rc.112",
50
50
  "google-auth-library": "10.5.0"
@@ -1,2 +0,0 @@
1
- export declare const convert: (schema: unknown) => Record<string, unknown> | undefined;
2
- export * as GeminiToolSchema from "./gemini-tool-schema.js";
@@ -1,103 +0,0 @@
1
- import { isRecord } from "../../utils/record.js";
2
- // Gemini accepts a JSON Schema-like dialect for tool parameters, but rejects a
3
- // handful of common JSON Schema shapes. Keep this projection isolated so the
4
- // Gemini protocol file still reads like the other protocol modules.
5
- const SCHEMA_INTENT_KEYS = [
6
- "type",
7
- "properties",
8
- "items",
9
- "prefixItems",
10
- "enum",
11
- "const",
12
- "$ref",
13
- "additionalProperties",
14
- "patternProperties",
15
- "required",
16
- "not",
17
- "if",
18
- "then",
19
- "else",
20
- ];
21
- const hasCombiner = (schema) => isRecord(schema) && (Array.isArray(schema.anyOf) || Array.isArray(schema.oneOf) || Array.isArray(schema.allOf));
22
- const hasSchemaIntent = (schema) => isRecord(schema) && (hasCombiner(schema) || SCHEMA_INTENT_KEYS.some((key) => key in schema));
23
- const sanitizeNode = (schema) => {
24
- if (!isRecord(schema))
25
- return Array.isArray(schema) ? schema.map(sanitizeNode) : schema;
26
- const result = Object.fromEntries(Object.entries(schema).map(([key, value]) => [
27
- key,
28
- key === "enum" && Array.isArray(value) ? value.map(String) : sanitizeNode(value),
29
- ]));
30
- if (Array.isArray(result.enum) && (result.type === "integer" || result.type === "number"))
31
- result.type = "string";
32
- const properties = result.properties;
33
- if (result.type === "object" && isRecord(properties) && Array.isArray(result.required)) {
34
- result.required = result.required.filter((field) => typeof field === "string" && field in properties);
35
- }
36
- if (result.type === "array" && !hasCombiner(result)) {
37
- result.items = result.items ?? {};
38
- if (isRecord(result.items) && !hasSchemaIntent(result.items))
39
- result.items = { ...result.items, type: "string" };
40
- }
41
- if (typeof result.type === "string" && result.type !== "object" && !hasCombiner(result)) {
42
- delete result.properties;
43
- delete result.required;
44
- }
45
- return result;
46
- };
47
- const emptyObjectSchema = (schema) => schema.type === "object" &&
48
- (!isRecord(schema.properties) || Object.keys(schema.properties).length === 0) &&
49
- !schema.additionalProperties;
50
- const projectNode = (schema, nested = false) => {
51
- if (!isRecord(schema))
52
- return undefined;
53
- if (!nested && emptyObjectSchema(schema))
54
- return undefined;
55
- const types = Array.isArray(schema.type) ? schema.type.filter((type) => type !== "null") : undefined;
56
- const anyOf = Array.isArray(schema.anyOf) ? schema.anyOf : undefined;
57
- const hasNullAnyOf = anyOf?.some((item) => isRecord(item) && item.type === "null") ?? false;
58
- const anyOfTypes = hasNullAnyOf ? anyOf?.filter((item) => !isRecord(item) || item.type !== "null") : anyOf;
59
- const flattenedAnyOf = hasNullAnyOf && anyOfTypes?.length === 1 ? projectNode(anyOfTypes[0], true) : undefined;
60
- const result = Object.fromEntries([
61
- ["description", schema.description],
62
- ["required", schema.required],
63
- ["format", schema.format],
64
- ["type", types ? (types.length === 0 ? "null" : undefined) : schema.type],
65
- [
66
- "nullable",
67
- (Array.isArray(schema.type) && schema.type.includes("null") && types && types.length > 0) || hasNullAnyOf
68
- ? true
69
- : undefined,
70
- ],
71
- ["enum", schema.const !== undefined ? [schema.const] : schema.enum],
72
- [
73
- "properties",
74
- isRecord(schema.properties)
75
- ? Object.fromEntries(Object.entries(schema.properties).map(([key, value]) => [key, projectNode(value, true)]))
76
- : undefined,
77
- ],
78
- [
79
- "items",
80
- Array.isArray(schema.items)
81
- ? schema.items.map((item) => projectNode(item, true))
82
- : schema.items === undefined
83
- ? undefined
84
- : projectNode(schema.items, true),
85
- ],
86
- ["allOf", Array.isArray(schema.allOf) ? schema.allOf.map((item) => projectNode(item, true)) : undefined],
87
- [
88
- "anyOf",
89
- anyOfTypes
90
- ? hasNullAnyOf && anyOfTypes.length === 1
91
- ? undefined
92
- : anyOfTypes.map((item) => projectNode(item, true))
93
- : types && types.length > 0
94
- ? types.map((type) => ({ type }))
95
- : undefined,
96
- ],
97
- ["oneOf", Array.isArray(schema.oneOf) ? schema.oneOf.map((item) => projectNode(item, true)) : undefined],
98
- ["minLength", schema.minLength],
99
- ].filter((entry) => entry[1] !== undefined));
100
- return flattenedAnyOf ? { ...result, ...flattenedAnyOf } : result;
101
- };
102
- export const convert = (schema) => projectNode(sanitizeNode(schema));
103
- export * as GeminiToolSchema from "./gemini-tool-schema.js";