File size: 5,251 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { firstNonEmptyString } from "../attachments/mime";
import { isRecord } from "../shared/types";
import type { UnknownRecord } from "../shared/types";
import { extractToolMeta, toolFunctionDeclarations } from "./tool-meta";
import { isToolBundle } from "./tool-bundle";

type ParsedToolCall = UnknownRecord & { name?: unknown; input?: unknown };
type ToolSchemaIndex = Record<string, UnknownRecord>;
type ArraySchemaRecord = UnknownRecord & { items?: unknown };

export function normalizeParsedToolCallsForSchemas(
	calls: ParsedToolCall[],
	toolsRaw: unknown,
): ParsedToolCall[];
export function normalizeParsedToolCallsForSchemas(
	calls: unknown,
	toolsRaw: unknown,
): unknown;
export function normalizeParsedToolCallsForSchemas(
	calls: unknown,
	toolsRaw: unknown,
): unknown {
	if (!Array.isArray(calls) || !calls.length) return calls;
	const schemas = buildToolSchemaIndex(toolsRaw);
	if (!schemas) return calls;
	let changedAny = false;
	const out = calls.map((call) => {
		if (!isRecord(call)) return call;
		const name = String(call.name || "")
			.trim()
			.toLowerCase();
		const schema = schemas[name];
		if (!schema || !isRecord(call.input)) return call;
		const [normalized, changed] = normalizeToolValueWithSchema(
			call.input,
			schema,
		);
		if (!changed || !isRecord(normalized)) return call;
		changedAny = true;
		return { ...call, input: normalized };
	});
	return changedAny ? out : calls;
}

export function buildToolSchemaIndex(
	toolsRaw: unknown,
): ToolSchemaIndex | null {
	if (isToolBundle(toolsRaw)) return toolsRaw.schemaIndex;
	if (!Array.isArray(toolsRaw) || !toolsRaw.length) return null;
	const out: ToolSchemaIndex = {};
	const addToolSchema = (item: unknown) => {
		const meta = extractToolMeta(item);
		const name = meta && firstNonEmptyString(meta.name);
		if (name && isRecord(meta.parameters))
			out[name.toLowerCase()] = meta.parameters;
	};
	for (const item of toolsRaw) {
		const declarations = toolFunctionDeclarations(item);
		if (declarations.length) {
			for (const fn of declarations) addToolSchema(fn);
		} else {
			addToolSchema(item);
		}
	}
	return Object.keys(out).length ? out : null;
}

export function normalizeToolValueWithSchema(
	value: unknown,
	schema: unknown,
): [unknown, boolean] {
	if (value == null || !isRecord(schema)) return [value, false];
	if (shouldCoerceSchemaToString(schema)) return stringifySchemaValue(value);
	if (looksLikeObjectSchema(schema)) {
		if (!isRecord(value)) return [value, false];
		const properties = isRecord(schema.properties) ? schema.properties : null;
		const additional = schema.additionalProperties;
		let changed = false;
		const out: UnknownRecord = {};
		for (const [key, current] of Object.entries(value)) {
			let next = current;
			let fieldChanged = false;
			if (properties && Object.hasOwn(properties, key))
				[next, fieldChanged] = normalizeToolValueWithSchema(
					current,
					properties[key],
				);
			else if (additional != null)
				[next, fieldChanged] = normalizeToolValueWithSchema(
					current,
					additional,
				);
			out[key] = next;
			changed = changed || fieldChanged;
		}
		return changed ? [out, true] : [value, false];
	}
	if (looksLikeArraySchema(schema)) {
		const itemsSchema = schema.items;
		if (!Array.isArray(value) || !value.length || itemsSchema == null)
			return [value, false];
		let changed = false;
		const out = value.map((item, idx) => {
			const itemSchema = Array.isArray(itemsSchema)
				? itemsSchema[idx]
				: itemsSchema;
			if (itemSchema == null) return item;
			const [next, itemChanged] = normalizeToolValueWithSchema(
				item,
				itemSchema,
			);
			changed = changed || itemChanged;
			return next;
		});
		return changed ? [out, true] : [value, false];
	}
	return [value, false];
}

export function shouldCoerceSchemaToString(schema: unknown): boolean {
	if (!isRecord(schema)) return false;
	if (typeof schema.const === "string") return true;
	if (
		Array.isArray(schema.enum) &&
		schema.enum.length &&
		schema.enum.every((item) => typeof item === "string")
	)
		return true;
	if (typeof schema.type === "string")
		return schema.type.trim().toLowerCase() === "string";
	if (Array.isArray(schema.type) && schema.type.length) {
		let hasString = false;
		for (const item of schema.type) {
			if (typeof item !== "string") return false;
			const typ = item.trim().toLowerCase();
			if (typ === "string") hasString = true;
			else if (typ !== "null") return false;
		}
		return hasString;
	}
	return false;
}

export function looksLikeObjectSchema(schema: unknown): boolean {
	return (
		isRecord(schema) &&
		((typeof schema.type === "string" &&
			schema.type.trim().toLowerCase() === "object") ||
			isRecord(schema.properties) ||
			schema.additionalProperties != null)
	);
}

export function looksLikeArraySchema(
	schema: unknown,
): schema is ArraySchemaRecord {
	return (
		isRecord(schema) &&
		((typeof schema.type === "string" &&
			schema.type.trim().toLowerCase() === "array") ||
			schema.items != null)
	);
}

export function stringifySchemaValue(value: unknown): [unknown, boolean] {
	if (value == null || typeof value === "string") return [value, false];
	try {
		return [JSON.stringify(value), true];
	} catch (_) {
		return [value, false];
	}
}