The best way to control Claude Code, Codex and more from any computer or phone

main
ts 111 lines 4.89 KB
Raw
1 import * as Predicate from "effect/Predicate";
2 import { Tool } from "effect/ai";
3
4 type Json = unknown;
5 type JsonObject = { [key: string]: Json };
6
7 const isNullSchema = (value: Json) =>
8 Predicate.isObject(value) && value.type === "null" && Object.keys(value).length === 1;
9
10 const isBareAnyOf = (value: Json): value is { anyOf: Array<Json> } =>
11 Predicate.isObject(value) && Array.isArray(value.anyOf) && Object.keys(value).length === 1;
12
13 // A `Schema.fromJsonString` fallback. Advertised beside object shapes, it invites stringified input.
14 const isJsonStringSchema = (value: Json) =>
15 Predicate.isObject(value) &&
16 value.type === "string" &&
17 value.contentMediaType === "application/json";
18
19 const SAFE_INTEGER_BOUNDS: Record<string, number> = {
20 maximum: Number.MAX_SAFE_INTEGER,
21 minimum: Number.MIN_SAFE_INTEGER,
22 };
23
24 // Gemini rejects function declarations whose enums hold anything but strings.
25 const isNonStringEnum = (key: string, value: Json) =>
26 key === "enum" && Array.isArray(value) && value.some((item) => typeof item !== "string");
27
28 const tagOf = (branch: JsonObject, key: string): Json => {
29 const property = Predicate.isObject(branch.properties) ? branch.properties[key] : undefined;
30 return Predicate.isObject(property) && Array.isArray(property.enum) && property.enum.length === 1
31 ? property.enum[0]
32 : undefined;
33 };
34
35 /** One object for a tagged union of objects: each property once, the tag listing every value. */
36 const mergeTaggedObjects = (branches: ReadonlyArray<Json>): JsonObject | undefined => {
37 if (branches.length < 2) return undefined;
38 const objects = branches.filter(
39 (branch): branch is JsonObject & { properties: JsonObject } =>
40 Predicate.isObject(branch) &&
41 branch.type === "object" &&
42 Predicate.isObject(branch.properties),
43 );
44 if (objects.length !== branches.length) return undefined;
45 const tag = Object.keys(objects[0]!.properties).find((key) => {
46 const values = objects.map((object) => tagOf(object, key));
47 return values.every((value) => value !== undefined) && new Set(values).size === values.length;
48 });
49 if (tag === undefined) return undefined;
50 const properties: JsonObject = {
51 [tag]: { type: "string", enum: objects.map((object) => tagOf(object, tag)) },
52 };
53 for (const object of objects) {
54 for (const [name, property] of Object.entries(object.properties)) {
55 if (name === tag) continue;
56 const seen = properties[name];
57 if (seen === undefined) properties[name] = property;
58 else if (JSON.stringify(seen) !== JSON.stringify(property)) {
59 const options = Predicate.isObject(seen) && isBareAnyOf(seen) ? seen.anyOf : [seen];
60 if (!options.some((option) => JSON.stringify(option) === JSON.stringify(property)))
61 properties[name] = { anyOf: [...options, property] };
62 }
63 }
64 }
65 const [first = [], ...others] = objects.map((object) =>
66 Array.isArray(object.required) ? object.required : [],
67 );
68 const requiredByAll = first.filter(
69 (name) => name !== tag && others.every((required) => required.includes(name)),
70 );
71 return { type: "object", properties, required: [tag, ...requiredByAll] };
72 };
73
74 const compact = (schema: Json, optional: boolean): Json => {
75 if (Array.isArray(schema)) return schema.map((item) => compact(item, false));
76 if (!Predicate.isObject(schema)) return schema;
77 const required = new Set(Array.isArray(schema.required) ? schema.required : []);
78 let result: JsonObject = {};
79 for (const [key, value] of Object.entries(schema)) {
80 if (key === "additionalProperties" && value === true) continue;
81 if (SAFE_INTEGER_BOUNDS[key] === value) continue;
82 if (isNonStringEnum(key, value)) continue;
83 result[key] =
84 key === "properties" && Predicate.isObject(value)
85 ? Object.fromEntries(
86 Object.entries(value).map(([name, property]) => [
87 name,
88 compact(property, !required.has(name)),
89 ]),
90 )
91 : compact(value, false);
92 }
93 if (Array.isArray(result.anyOf)) {
94 const branches = result.anyOf
95 .flatMap((branch) => (isBareAnyOf(branch) ? branch.anyOf : [branch]))
96 .filter((branch) => !(optional && isNullSchema(branch)) && !isJsonStringSchema(branch));
97 const { anyOf: _anyOf, ...rest } = result;
98 const only = branches.length === 1 ? branches[0] : mergeTaggedObjects(branches);
99 result = Predicate.isObject(only) ? { ...only, ...rest } : { ...rest, anyOf: branches };
100 }
101 return result;
102 };
103
104 /**
105 * The input schema T3 advertises for a tool. Drops JSON noise that every thread pays for:
106 * `additionalProperties: true`, safe-integer bounds, nested `anyOf`, JSON-string fallbacks, and
107 * the `null` branch of optional properties. A tagged union of objects becomes one object.
108 * Decoding still uses the full schema, so each tag's own required fields still apply.
109 */
110 export const advertisedJsonSchema = (tool: Tool.Any) =>
111 compact(Tool.getJsonSchema(tool), false) as ReturnType<typeof Tool.getJsonSchema>;