@fluidframework/tree-agent 2.74.0 → 2.81.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/api-report/tree-agent.alpha.api.md +195 -20
  3. package/dist/alpha.d.ts +31 -1
  4. package/dist/index.d.ts +3 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +5 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/methodBinding.d.ts +54 -10
  9. package/dist/methodBinding.d.ts.map +1 -1
  10. package/dist/methodBinding.js.map +1 -1
  11. package/dist/propertyBinding.d.ts +52 -2
  12. package/dist/propertyBinding.d.ts.map +1 -1
  13. package/dist/propertyBinding.js +28 -3
  14. package/dist/propertyBinding.js.map +1 -1
  15. package/dist/renderSchemaTypeScript.d.ts.map +1 -1
  16. package/dist/renderSchemaTypeScript.js +24 -9
  17. package/dist/renderSchemaTypeScript.js.map +1 -1
  18. package/dist/renderTypeFactoryTypeScript.d.ts +13 -0
  19. package/dist/renderTypeFactoryTypeScript.d.ts.map +1 -0
  20. package/dist/renderTypeFactoryTypeScript.js +290 -0
  21. package/dist/renderTypeFactoryTypeScript.js.map +1 -0
  22. package/dist/subtree.d.ts.map +1 -1
  23. package/dist/subtree.js +4 -4
  24. package/dist/subtree.js.map +1 -1
  25. package/dist/treeAgentTypes.d.ts +430 -0
  26. package/dist/treeAgentTypes.d.ts.map +1 -0
  27. package/dist/treeAgentTypes.js +227 -0
  28. package/dist/treeAgentTypes.js.map +1 -0
  29. package/dist/utils.d.ts +0 -4
  30. package/dist/utils.d.ts.map +1 -1
  31. package/dist/utils.js +2 -9
  32. package/dist/utils.js.map +1 -1
  33. package/eslint.config.mts +4 -4
  34. package/lib/alpha.d.ts +31 -1
  35. package/lib/index.d.ts +3 -1
  36. package/lib/index.d.ts.map +1 -1
  37. package/lib/index.js +1 -0
  38. package/lib/index.js.map +1 -1
  39. package/lib/methodBinding.d.ts +54 -10
  40. package/lib/methodBinding.d.ts.map +1 -1
  41. package/lib/methodBinding.js.map +1 -1
  42. package/lib/propertyBinding.d.ts +52 -2
  43. package/lib/propertyBinding.d.ts.map +1 -1
  44. package/lib/propertyBinding.js +28 -3
  45. package/lib/propertyBinding.js.map +1 -1
  46. package/lib/renderSchemaTypeScript.d.ts.map +1 -1
  47. package/lib/renderSchemaTypeScript.js +24 -9
  48. package/lib/renderSchemaTypeScript.js.map +1 -1
  49. package/lib/renderTypeFactoryTypeScript.d.ts +13 -0
  50. package/lib/renderTypeFactoryTypeScript.d.ts.map +1 -0
  51. package/lib/renderTypeFactoryTypeScript.js +285 -0
  52. package/lib/renderTypeFactoryTypeScript.js.map +1 -0
  53. package/lib/subtree.d.ts.map +1 -1
  54. package/lib/subtree.js +4 -4
  55. package/lib/subtree.js.map +1 -1
  56. package/lib/treeAgentTypes.d.ts +430 -0
  57. package/lib/treeAgentTypes.d.ts.map +1 -0
  58. package/lib/treeAgentTypes.js +223 -0
  59. package/lib/treeAgentTypes.js.map +1 -0
  60. package/lib/utils.d.ts +0 -4
  61. package/lib/utils.d.ts.map +1 -1
  62. package/lib/utils.js +2 -8
  63. package/lib/utils.js.map +1 -1
  64. package/package.json +17 -17
  65. package/src/index.ts +36 -0
  66. package/src/methodBinding.ts +94 -15
  67. package/src/propertyBinding.ts +66 -9
  68. package/src/renderSchemaTypeScript.ts +32 -10
  69. package/src/renderTypeFactoryTypeScript.ts +339 -0
  70. package/src/subtree.ts +6 -5
  71. package/src/treeAgentTypes.ts +611 -0
  72. package/src/utils.ts +2 -9
  73. package/.eslintrc.cjs +0 -48
@@ -7,7 +7,9 @@
7
7
  import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
8
8
  import { NodeKind } from "@fluidframework/tree";
9
9
  import type { z } from "zod";
10
+
10
11
  import { instanceOf } from "./renderZodTypeScript.js";
12
+ import type { TypeFactoryType } from "./treeAgentTypes.js";
11
13
 
12
14
  /**
13
15
  * A utility type that extracts the method keys from a given type.
@@ -61,7 +63,8 @@ export function getExposedMethods(schemaClass: BindableSchema): {
61
63
  * A type that represents a function argument.
62
64
  * @alpha
63
65
  */
64
- export type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string, type: T];
66
+ export type Arg<T extends z.ZodTypeAny | TypeFactoryType = z.ZodTypeAny | TypeFactoryType> =
67
+ readonly [name: string, type: T];
65
68
 
66
69
  /**
67
70
  * A function definition interface that describes the structure of a function.
@@ -69,12 +72,24 @@ export type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string,
69
72
  */
70
73
  export interface FunctionDef<
71
74
  Args extends readonly Arg[],
72
- Return extends z.ZodTypeAny,
73
- Rest extends z.ZodTypeAny | null = null,
75
+ Return extends z.ZodTypeAny | TypeFactoryType,
76
+ Rest extends z.ZodTypeAny | TypeFactoryType | null = null,
74
77
  > {
78
+ /**
79
+ * Optional description of the function.
80
+ */
75
81
  description?: string;
82
+ /**
83
+ * The function's parameters.
84
+ */
76
85
  args: Args;
86
+ /**
87
+ * Optional rest parameter type.
88
+ */
77
89
  rest?: Rest;
90
+ /**
91
+ * The function's return type.
92
+ */
78
93
  returns: Return;
79
94
  }
80
95
 
@@ -82,15 +97,20 @@ export interface FunctionDef<
82
97
  * A class that implements the FunctionDef interface.
83
98
  */
84
99
  export class FunctionWrapper
85
- implements FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>
100
+ implements
101
+ FunctionDef<
102
+ readonly Arg[],
103
+ z.ZodTypeAny | TypeFactoryType,
104
+ z.ZodTypeAny | TypeFactoryType | null
105
+ >
86
106
  {
87
107
  public constructor(
88
108
  public readonly name: string,
89
109
  public readonly description: string | undefined,
90
110
  public readonly args: readonly Arg[],
91
111
  // eslint-disable-next-line @rushstack/no-new-null
92
- public readonly rest: z.ZodTypeAny | null,
93
- public readonly returns: z.ZodTypeAny,
112
+ public readonly rest: z.ZodTypeAny | TypeFactoryType | null,
113
+ public readonly returns: z.ZodTypeAny | TypeFactoryType,
94
114
  ) {}
95
115
  }
96
116
 
@@ -109,9 +129,9 @@ export type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Sing
109
129
  * @alpha
110
130
  */
111
131
  export function buildFunc<
112
- const Return extends z.ZodTypeAny,
132
+ const Return extends z.ZodTypeAny | TypeFactoryType,
113
133
  const Args extends readonly Arg[],
114
- const Rest extends z.ZodTypeAny | null = null,
134
+ const Rest extends z.ZodTypeAny | TypeFactoryType | null = null,
115
135
  >(
116
136
  def: { description?: string; returns: Return; rest?: Rest },
117
137
  ...args: Args
@@ -124,12 +144,48 @@ export function buildFunc<
124
144
  };
125
145
  }
126
146
 
147
+ /**
148
+ * A utility type that extracts inferred parameter types from Zod args.
149
+ * @alpha
150
+ */
151
+ export type InferArgsZod<Args extends readonly Arg<z.ZodTypeAny>[]> = Args extends readonly [
152
+ infer Head extends Arg<z.ZodTypeAny>,
153
+ ...infer Tail extends readonly Arg<z.ZodTypeAny>[],
154
+ ]
155
+ ? [z.infer<Head[1]>, ...InferArgsZod<Tail>]
156
+ : [];
157
+
158
+ /**
159
+ * A utility type that infers the function signature from a Zod function definition with strict type checking.
160
+ * @alpha
161
+ */
162
+ export type InferZod<T> = T extends FunctionDef<
163
+ infer Args extends readonly Arg<z.ZodTypeAny>[],
164
+ infer Return extends z.ZodTypeAny,
165
+ any
166
+ >
167
+ ? (...args: InferArgsZod<Args>) => z.infer<Return>
168
+ : never;
169
+
170
+ /**
171
+ * A utility type that infers the function signature from a type factory function definition with relaxed type checking.
172
+ * @alpha
173
+ */
174
+ export type InferTypeFactory<T> = T extends FunctionDef<readonly Arg[], infer Return, any>
175
+ ? (...args: any[]) => any
176
+ : never;
177
+
127
178
  /**
128
179
  * A utility type that infers the return type of a function definition.
129
180
  * @alpha
181
+ * @remarks
182
+ * For Zod types, provides strict compile-time type checking. For type factory types, returns `any`.
183
+ * @deprecated Use InferZod or InferTypeFactory directly for better type safety.
130
184
  */
131
- export type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Rest>
132
- ? z.infer<z.ZodFunction<z.ZodTuple<ArgsTuple<Args>, Rest>, Return>>
185
+ export type Infer<T> = T extends FunctionDef<readonly Arg[], infer Return, any>
186
+ ? Return extends z.ZodTypeAny
187
+ ? InferZod<T>
188
+ : InferTypeFactory<T>
133
189
  : never;
134
190
 
135
191
  /**
@@ -137,16 +193,32 @@ export type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Res
137
193
  * @alpha
138
194
  */
139
195
  export interface ExposedMethods {
196
+ /**
197
+ * Expose a method with Zod types (strict compile-time type checking).
198
+ */
140
199
  expose<
141
200
  const K extends string & keyof MethodKeys<InstanceType<S>>,
142
- S extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,
143
- Z extends FunctionDef<any, any, any>,
201
+ S extends BindableSchema & Ctor<Record<K, InferZod<Z>>> & IExposedMethods,
202
+ Z extends FunctionDef<readonly Arg<z.ZodTypeAny>[], z.ZodTypeAny, z.ZodTypeAny | null>,
144
203
  >(schema: S, methodName: K, zodFunction: Z): void;
145
204
 
205
+ /**
206
+ * Expose a method with type factory types (relaxed compile-time type checking).
207
+ */
208
+ expose<
209
+ const K extends string & keyof MethodKeys<InstanceType<S>>,
210
+ S extends BindableSchema & Ctor & IExposedMethods,
211
+ Z extends FunctionDef<
212
+ readonly Arg<TypeFactoryType>[],
213
+ TypeFactoryType,
214
+ TypeFactoryType | null
215
+ >,
216
+ >(schema: S, methodName: K, tfFunction: Z): void;
217
+
146
218
  /**
147
219
  * Create a Zod schema for a SharedTree schema class.
148
220
  * @remarks
149
- * Use it to "wrap" schema types that are referenced as arguments or return types when exposing methods (with {@link ExposedMethods.expose}).
221
+ * Use it to "wrap" schema types that are referenced as arguments or return types when exposing methods with {@link ExposedMethods}.
150
222
  */
151
223
  instanceOf<T extends TreeNodeSchemaClass>(
152
224
  schema: T,
@@ -174,6 +246,9 @@ export const exposeMethodsSymbol: unique symbol = Symbol("run");
174
246
  * @alpha
175
247
  */
176
248
  export interface IExposedMethods {
249
+ /**
250
+ * Static method that exposes methods of this schema class to an agent.
251
+ */
177
252
  [exposeMethodsSymbol](methods: ExposedMethods): void;
178
253
  }
179
254
 
@@ -185,8 +260,12 @@ class ExposedMethodsI implements ExposedMethods {
185
260
 
186
261
  public expose<
187
262
  const K extends string & keyof MethodKeys<InstanceType<S>>,
188
- S extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,
189
- Z extends FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>,
263
+ S extends BindableSchema & Ctor & IExposedMethods,
264
+ Z extends FunctionDef<
265
+ readonly Arg[],
266
+ z.ZodTypeAny | TypeFactoryType,
267
+ z.ZodTypeAny | TypeFactoryType | null
268
+ >,
190
269
  >(schema: S, methodName: K, functionDef: Z): void {
191
270
  if (schema !== this.schemaClass) {
192
271
  throw new Error('Must expose methods on the "this" object');
@@ -9,6 +9,8 @@ import type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from "zod";
9
9
 
10
10
  import type { BindableSchema, Ctor } from "./methodBinding.js";
11
11
  import { instanceOf } from "./renderZodTypeScript.js";
12
+ import type { TypeFactoryType } from "./treeAgentTypes.js";
13
+ import { isTypeFactoryType } from "./treeAgentTypes.js";
12
14
 
13
15
  /**
14
16
  * A symbol used to expose properties to the LLM.
@@ -73,9 +75,21 @@ export type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]
73
75
  */
74
76
  export class PropertyDef {
75
77
  public constructor(
78
+ /**
79
+ * The name of the property.
80
+ */
76
81
  public readonly name: string,
82
+ /**
83
+ * Optional description of the property.
84
+ */
77
85
  public readonly description: string | undefined,
78
- public readonly schema: ZodTypeAny,
86
+ /**
87
+ * The schema defining the property's type (either Zod or TypeFactory).
88
+ */
89
+ public readonly schema: ZodTypeAny | TypeFactoryType,
90
+ /**
91
+ * Whether the property is readonly.
92
+ */
79
93
  public readonly readOnly: boolean,
80
94
  ) {}
81
95
  }
@@ -85,6 +99,9 @@ export class PropertyDef {
85
99
  * @alpha
86
100
  */
87
101
  export interface ExposedProperties {
102
+ /**
103
+ * Expose a property with Zod type checking.
104
+ */
88
105
  exposeProperty<
89
106
  S extends BindableSchema & Ctor,
90
107
  K extends string & ExposableKeys<InstanceType<S>>,
@@ -96,6 +113,29 @@ export interface ExposedProperties {
96
113
  TypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,
97
114
  ): void;
98
115
 
116
+ /**
117
+ * Expose a property with type factory type and metadata.
118
+ */
119
+ exposeProperty<
120
+ S extends BindableSchema & Ctor,
121
+ K extends string & ExposableKeys<InstanceType<S>>,
122
+ >(
123
+ schema: S,
124
+ name: K,
125
+ def: { schema: TypeFactoryType; description?: string; readOnly?: boolean },
126
+ ): void;
127
+
128
+ /**
129
+ * Expose a property with type factory type (simple form).
130
+ */
131
+ exposeProperty<
132
+ S extends BindableSchema & Ctor,
133
+ K extends string & ExposableKeys<InstanceType<S>>,
134
+ >(schema: S, name: K, tfType: TypeFactoryType): void;
135
+
136
+ /**
137
+ * Create a Zod type that references a SharedTree schema class.
138
+ */
99
139
  instanceOf<T extends TreeNodeSchemaClass>(
100
140
  schema: T,
101
141
  ): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;
@@ -116,6 +156,9 @@ export interface ExposedProperties {
116
156
  * @alpha
117
157
  */
118
158
  export interface IExposedProperties {
159
+ /**
160
+ * Static method that exposes properties of this schema class to an agent.
161
+ */
119
162
  [exposePropertiesSymbol]?(properties: ExposedProperties): void;
120
163
  }
121
164
 
@@ -132,18 +175,32 @@ class ExposedPropertiesI implements ExposedProperties {
132
175
  >(
133
176
  schema: S,
134
177
  name: K,
135
- def: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &
136
- TypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,
178
+ defOrType:
179
+ | ({ schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &
180
+ TypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>)
181
+ | TypeFactoryType,
137
182
  ): void {
138
183
  if (schema !== this.schemaClass) {
139
184
  throw new Error('Must expose properties on the "this" schema class');
140
185
  }
141
- this.properties[name] = new PropertyDef(
142
- name,
143
- def.description,
144
- def.schema,
145
- def.readOnly === true,
146
- );
186
+
187
+ // Handle TypeFactoryType (simple case - type passed directly)
188
+ if (isTypeFactoryType(defOrType)) {
189
+ this.properties[name] = new PropertyDef(name, undefined, defOrType, false);
190
+ } else {
191
+ // Handle object with schema property (works for both Zod and TypeFactory)
192
+ const def = defOrType as {
193
+ schema: TZ | TypeFactoryType;
194
+ description?: string;
195
+ readOnly?: boolean;
196
+ };
197
+ this.properties[name] = new PropertyDef(
198
+ name,
199
+ def.description,
200
+ def.schema,
201
+ def.readOnly === true,
202
+ );
203
+ }
147
204
  }
148
205
 
149
206
  public instanceOf<T extends TreeNodeSchemaClass>(
@@ -18,8 +18,14 @@ import { z } from "zod";
18
18
  import type { BindableSchema, FunctionWrapper } from "./methodBinding.js";
19
19
  import { getExposedMethods } from "./methodBinding.js";
20
20
  import { getExposedProperties, type PropertyDef } from "./propertyBinding.js";
21
- import { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from "./utils.js";
21
+ import {
22
+ instanceOfsTypeFactory,
23
+ renderTypeFactoryTypeScript,
24
+ } from "./renderTypeFactoryTypeScript.js";
22
25
  import { instanceOfs, renderZodTypeScript } from "./renderZodTypeScript.js";
26
+ import type { TypeFactoryOptional, TypeFactoryType } from "./treeAgentTypes.js";
27
+ import { isTypeFactoryType } from "./treeAgentTypes.js";
28
+ import { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from "./utils.js";
23
29
 
24
30
  interface BoundMembers {
25
31
  methods: Record<string, FunctionWrapper>;
@@ -284,7 +290,9 @@ export function renderSchemaTypeScript(
284
290
  lines.push(`// ${note}`);
285
291
  }
286
292
  }
287
- lines.push(formatMethod(name, method));
293
+ const methodString = formatMethod(name, method);
294
+ const methodLines = methodString.split("\n");
295
+ lines.push(...methodLines);
288
296
  }
289
297
  if (lines.length > 0) {
290
298
  hasHelperMethods = true;
@@ -434,7 +442,11 @@ function renderPropertyLines(properties: Record<string, PropertyDef>): string[]
434
442
  }
435
443
  }
436
444
  const modifier = property.readOnly ? "readonly " : "";
437
- lines.push(`${modifier}${name}: ${renderZodType(property.schema)};`);
445
+ const typeString = renderType(property.schema, 0);
446
+ const propertyLine = `${modifier}${name}: ${typeString};`;
447
+ // Split multi-line type strings and add to lines array
448
+ const propertyLines = propertyLine.split("\n");
449
+ lines.push(...propertyLines);
438
450
  }
439
451
  return lines;
440
452
  }
@@ -443,13 +455,13 @@ function formatMethod(name: string, method: FunctionWrapper): string {
443
455
  const args: string[] = [];
444
456
  for (const [argName, argType] of method.args) {
445
457
  const { innerType, optional } = unwrapOptional(argType);
446
- const renderedType = renderZodType(innerType);
458
+ const renderedType = renderType(innerType, 0);
447
459
  args.push(`${argName}${optional ? "?" : ""}: ${renderedType}`);
448
460
  }
449
461
  if (method.rest !== null) {
450
- args.push(`...rest: ${renderZodType(method.rest)}[]`);
462
+ args.push(`...rest: ${renderType(method.rest, 0)}[]`);
451
463
  }
452
- return `${name}(${args.join(", ")}): ${renderZodType(method.returns)};`;
464
+ return `${name}(${args.join(", ")}): ${renderType(method.returns, 0)};`;
453
465
  }
454
466
 
455
467
  function renderLeaf(leafKind: ValueSchema): string {
@@ -482,7 +494,15 @@ function formatExpression(
482
494
  /**
483
495
  * Detects optional zod wrappers so argument lists can keep TypeScript optional markers in sync.
484
496
  */
485
- function unwrapOptional(type: z.ZodTypeAny): { innerType: z.ZodTypeAny; optional: boolean } {
497
+ function unwrapOptional(type: z.ZodTypeAny | TypeFactoryType): {
498
+ innerType: z.ZodTypeAny | TypeFactoryType;
499
+ optional: boolean;
500
+ } {
501
+ // Handle type factory optional type
502
+ if (isTypeFactoryType(type) && type._kind === "optional") {
503
+ return { innerType: (type as TypeFactoryOptional).innerType, optional: true };
504
+ }
505
+ // Handle Zod optional type
486
506
  if (type instanceof z.ZodOptional) {
487
507
  const inner = type.unwrap() as z.ZodTypeAny;
488
508
  return { innerType: inner, optional: true };
@@ -516,8 +536,10 @@ function ensureNoMemberConflicts(
516
536
  }
517
537
 
518
538
  /**
519
- * Converts schema metadata into TypeScript declarations suitable for prompt inclusion.
539
+ * Dispatches to the correct renderer based on whether the type is Zod or type factory.
520
540
  */
521
- function renderZodType(type: z.ZodTypeAny): string {
522
- return renderZodTypeScript(type, getFriendlyName, instanceOfs);
541
+ function renderType(type: z.ZodTypeAny | TypeFactoryType, indentLevel: number = 0): string {
542
+ return isTypeFactoryType(type)
543
+ ? renderTypeFactoryTypeScript(type, getFriendlyName, instanceOfsTypeFactory, indentLevel)
544
+ : renderZodTypeScript(type, getFriendlyName, instanceOfs);
523
545
  }