-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.ts
More file actions
114 lines (105 loc) · 3.11 KB
/
Copy pathutils.ts
File metadata and controls
114 lines (105 loc) · 3.11 KB
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
import type { Tool as AnthropicTool } from "@anthropic-ai/sdk/resources/messages/messages";
import z from "zod";
import type { AnyTool, Tool } from "../../tool.ts";
import {
createSchemaCompatibility,
type ObjectSchemaCompatibility,
type ObjectSchemaCompatibilityOptions,
type SchemaCompatibility,
type SchemaCompatibilityFeatures,
type SchemaCompatibilityOptions,
} from "../shared/schema_compatibility.ts";
export type AnthropicToolMap = {
original: Tool;
anthropic: AnthropicTool;
compatibility?: SchemaCompatibility;
isVoid: boolean;
};
export interface AnthropicCompatibleSchemaOptions extends Omit<SchemaCompatibilityOptions, "features"> {
features?: SchemaCompatibilityFeatures;
}
interface AnthropicCompatibleObjectSchemaOptions extends Omit<ObjectSchemaCompatibilityOptions, "features"> {
features?: SchemaCompatibilityFeatures;
}
export const anthropicSchemaCompatibilityFeatures: SchemaCompatibilityFeatures = {
tuples: {
strategy: "object-surrogate",
itemKeyPrefix: "item",
restKey: "rest",
},
records: {
finite: "explicit-object",
dynamic: "entries-array",
keyPropertyName: "key",
valuePropertyName: "value",
},
strings: {
length: "instructions",
format: "instructions",
},
numbers: {
integerType: "number",
bounds: "instructions",
multipleOf: "instructions",
},
arrays: {
length: "min1-only",
},
};
/**
* `eager_input_streaming` stays off.
* It drops the server-side buffering that validates a tool's arguments, and nothing here reads a
* fragment before the block closes.
*/
export function normalizeAnthropicTools(tools: AnyTool[]): AnthropicToolMap[] {
return tools.map((tool): AnthropicToolMap => {
const name = tool.normalizedName;
if (tool.parameters instanceof z.ZodVoid) {
return {
original: tool,
anthropic: {
name,
strict: false,
input_schema: { type: "object" },
description: tool.description,
},
isVoid: true,
};
}
const compatibleSchema = createAnthropicCompatibleSchema(tool.parameters, {
kind: "tool",
requireTopLevelObject: true,
rootPath: "input",
});
return {
original: tool,
anthropic: {
name,
strict: tool.anthropicStrict ?? true,
input_schema: compatibleSchema.jsonSchema,
description: compatibleSchema.instructions
? `${tool.description}\n\n${compatibleSchema.instructions}`
: tool.description,
},
compatibility: compatibleSchema,
isVoid: false,
};
});
}
export function createAnthropicCompatibleSchema(
schema: z.ZodType,
options: AnthropicCompatibleObjectSchemaOptions,
): ObjectSchemaCompatibility;
export function createAnthropicCompatibleSchema(
schema: z.ZodType,
options: AnthropicCompatibleSchemaOptions,
): SchemaCompatibility;
export function createAnthropicCompatibleSchema(
schema: z.ZodType,
options: AnthropicCompatibleSchemaOptions,
): SchemaCompatibility {
return createSchemaCompatibility(schema, {
...options,
features: options.features ?? anthropicSchemaCompatibilityFeatures,
});
}