assistant-stream 0.2.47 → 0.3.0

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.
@@ -0,0 +1,112 @@
1
+ import type { JSONSchema7 } from "json-schema";
2
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
3
+ import type { Tool } from "./tool-types";
4
+
5
+ /**
6
+ * Type for a tool definition with JSON Schema parameters.
7
+ */
8
+ export type ToolJSONSchema = {
9
+ description?: string;
10
+ parameters: JSONSchema7;
11
+ };
12
+
13
+ export type ToToolsJSONSchemaOptions = {
14
+ /**
15
+ * Filter to determine which tools to include.
16
+ * Defaults to excluding disabled tools and backend tools.
17
+ */
18
+ filter?: (name: string, tool: Tool) => boolean;
19
+ };
20
+
21
+ function isStandardSchema(schema: unknown): schema is StandardSchemaV1 & {
22
+ "~standard": StandardSchemaV1["~standard"] & { toJSONSchema?: () => unknown };
23
+ } {
24
+ return (
25
+ typeof schema === "object" &&
26
+ schema !== null &&
27
+ "~standard" in schema &&
28
+ typeof (schema as StandardSchemaV1)["~standard"] === "object"
29
+ );
30
+ }
31
+
32
+ function hasToJSONSchemaMethod(
33
+ schema: unknown,
34
+ ): schema is { toJSONSchema: () => unknown } {
35
+ return (
36
+ typeof schema === "object" &&
37
+ schema !== null &&
38
+ "toJSONSchema" in schema &&
39
+ typeof (schema as { toJSONSchema: unknown }).toJSONSchema === "function"
40
+ );
41
+ }
42
+
43
+ function hasToJSONMethod(schema: unknown): schema is { toJSON: () => unknown } {
44
+ return (
45
+ typeof schema === "object" &&
46
+ schema !== null &&
47
+ "toJSON" in schema &&
48
+ typeof (schema as { toJSON: unknown }).toJSON === "function"
49
+ );
50
+ }
51
+
52
+ /**
53
+ * Converts a schema to JSONSchema7.
54
+ * Supports:
55
+ * - StandardSchemaV1 with ~standard.toJSONSchema (e.g., Zod v4)
56
+ * - Objects with toJSONSchema() method
57
+ * - Objects with toJSON() method
58
+ * - Plain JSONSchema7 objects
59
+ */
60
+ export function toJSONSchema(
61
+ schema: StandardSchemaV1 | JSONSchema7,
62
+ ): JSONSchema7 {
63
+ // StandardSchemaV1 with ~standard.toJSONSchema (e.g., Zod v4)
64
+ if (isStandardSchema(schema)) {
65
+ const toJSONSchemaMethod = schema["~standard"].toJSONSchema;
66
+ if (typeof toJSONSchemaMethod === "function") {
67
+ return toJSONSchemaMethod() as JSONSchema7;
68
+ }
69
+ }
70
+
71
+ // toJSONSchema method on the schema itself
72
+ if (hasToJSONSchemaMethod(schema)) {
73
+ return schema.toJSONSchema() as JSONSchema7;
74
+ }
75
+
76
+ // toJSON method on the schema
77
+ if (hasToJSONMethod(schema)) {
78
+ return schema.toJSON() as JSONSchema7;
79
+ }
80
+
81
+ // Already a plain JSONSchema7
82
+ return schema as JSONSchema7;
83
+ }
84
+
85
+ function defaultToolFilter(_name: string, tool: Tool): boolean {
86
+ return !tool.disabled && tool.type !== "backend";
87
+ }
88
+
89
+ /**
90
+ * Converts a record of tools to a record of tool definitions with JSON Schema parameters.
91
+ * By default, filters out disabled tools and backend tools.
92
+ */
93
+ export function toToolsJSONSchema(
94
+ tools: Record<string, Tool> | undefined,
95
+ options: ToToolsJSONSchemaOptions = {},
96
+ ): Record<string, ToolJSONSchema> {
97
+ if (!tools) return {};
98
+
99
+ const filter = options.filter ?? defaultToolFilter;
100
+
101
+ return Object.fromEntries(
102
+ Object.entries(tools)
103
+ .filter(([name, tool]) => filter(name, tool) && tool.parameters)
104
+ .map(([name, tool]) => [
105
+ name,
106
+ {
107
+ ...(tool.description && { description: tool.description }),
108
+ parameters: toJSONSchema(tool.parameters!),
109
+ },
110
+ ]),
111
+ );
112
+ }
@@ -71,7 +71,7 @@ describe("unstable_runPendingTools", () => {
71
71
 
72
72
  const executionTime = endTime - startTime;
73
73
 
74
- expect(executionTime).toBeGreaterThanOrEqual(100);
74
+ expect(executionTime).toBeGreaterThanOrEqual(90); // Allow for timer imprecision
75
75
  // The execution time should be less than the sum of the delays of both tools.
76
76
  expect(executionTime).toBeLessThan(300);
77
77