@fluidframework/tree-agent 2.74.0 → 2.81.0-374083

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 +4 -0
  2. package/api-report/tree-agent.alpha.api.md +163 -20
  3. package/dist/alpha.d.ts +26 -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 +17 -8
  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 +222 -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 +345 -0
  26. package/dist/treeAgentTypes.d.ts.map +1 -0
  27. package/dist/treeAgentTypes.js +190 -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 +26 -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 +17 -8
  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 +217 -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 +345 -0
  57. package/lib/treeAgentTypes.d.ts.map +1 -0
  58. package/lib/treeAgentTypes.js +186 -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 +31 -0
  66. package/src/methodBinding.ts +94 -15
  67. package/src/propertyBinding.ts +66 -9
  68. package/src/renderSchemaTypeScript.ts +25 -9
  69. package/src/renderTypeFactoryTypeScript.ts +259 -0
  70. package/src/subtree.ts +6 -5
  71. package/src/treeAgentTypes.ts +490 -0
  72. package/src/utils.ts +2 -9
  73. package/.eslintrc.cjs +0 -48
package/lib/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAA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C;AACtB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAC1B,MAA2B,EAC3B,SAA8C,GAAG,EAAE,CAAC,IAAI,EACxD,UAAU,IAAI,GAAG,EAAkB;IAEnC,KAAK,MAAM,UAAU,IAAI,oBAAoB,CAAC,MAAM,CAAC,CAAC,cAAc,EAAE,CAAC;QACtE,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;YAC9B,IAAI,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;gBACxB,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACzB,CAAC;YACD,WAAW,CAAC,CAAC,GAAG,UAAU,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAC1D,CAAC;IACF,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC3C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC,OAAO,CAAC;IACtB,CAAC;IACD,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;AACF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport type { ImplicitFieldSchema } from \"@fluidframework/tree\";\nimport type {\n\tInsertableContent,\n\tTreeNode,\n\tTreeNodeSchema,\n\tUnsafeUnknownSchema,\n} from \"@fluidframework/tree/alpha\";\nimport {\n\tArrayNodeSchema,\n\tMapNodeSchema,\n\tRecordNodeSchema,\n\tTreeAlpha,\n} from \"@fluidframework/tree/alpha\";\nimport { NodeKind, normalizeFieldSchema } from \"@fluidframework/tree/internal\";\n\n/**\n * Subset of Map interface.\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport interface MapGetSet<K, V> {\n\tget(key: K): V | undefined;\n\tset(key: K, value: V): void;\n}\n\n/**\n * TBD\n */\nexport function fail(message: string): never {\n\tthrow new Error(message);\n}\n\n/**\n * Map one iterable to another by transforming each element one at a time\n * @param iterable - the iterable to transform\n * @param map - the transformation function to run on each element of the iterable\n * @returns a new iterable of elements which have been transformed by the `map` function\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function* mapIterable<T, U>(\n\titerable: Iterable<T>,\n\tmap: (t: T) => U,\n): IterableIterator<U> {\n\tfor (const t of iterable) {\n\t\tyield map(t);\n\t}\n}\n\n/**\n * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.\n * @param map - The map to query/update\n * @param key - The key to lookup in the map\n * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists\n * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function getOrCreate<K, V>(\n\tmap: MapGetSet<K, V>,\n\tkey: K,\n\tdefaultValue: (key: K) => V,\n): V {\n\tlet value = map.get(key);\n\tif (value === undefined) {\n\t\tvalue = defaultValue(key);\n\t\tmap.set(key, value);\n\t}\n\treturn value;\n}\n\n/**\n * TODO\n */\nexport function tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\n/**\n * Does it have at least two elements?\n */\nexport function hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Include this property in a field's schema metadata to indicate that the field's value should be generated via a provided function rather than by the LLM.\n * @example\n * ```ts\n * class Object extends schemaFactory.object(\"Object\", {\n * created: sf.required(sf.number, {\n * custom: {\n * // The LLM will ignore this field, and instead it will be populated with the result of the function\n * [llmDefault]: () => Date.now(),\n * },\n * }),\n * }) {};\n * ```\n * @alpha\n */\nexport const llmDefault = Symbol(\"tree-agent/llmDefault\");\n// TODO: make this a wrapper function instead, and hide the symbol.\n// function llmDefault<T extends FieldSchemaMetadata>(metadata: T): T { ... }\n\n/**\n * Usage fail\n */\nexport function failUsage(message: string): never {\n\tthrow new UsageError(message);\n}\n\n/**\n * Construct an object node from a schema and value.\n */\nexport function constructNode(schema: TreeNodeSchema, value: InsertableContent): TreeNode {\n\tconst node = TreeAlpha.create<UnsafeUnknownSchema>(schema, value);\n\tassert(\n\t\tnode !== undefined && node !== null && typeof node === \"object\" && !isFluidHandle(node),\n\t\t0xc1e /* Expected a constructed node to be an object */,\n\t);\n\treturn node;\n}\n\n/**\n * Returns the unqualified name of a tree value's schema (e.g. a node with schema identifier `\"my.scope.MyNode\"` returns `\"MyNode\"`).\n * @remarks If the schema is an inlined array, map, or record type, then it has no name and this function will return a string representation of the type (e.g., `\"MyNode[]\"` or `\"Map<string, MyNode>\"`).\n */\nexport function getFriendlyName(schema: TreeNodeSchema): string {\n\tif (schema.kind === NodeKind.Leaf || isNamedSchema(schema.identifier)) {\n\t\treturn unqualifySchema(schema.identifier);\n\t}\n\n\tconst childNames = Array.from(schema.childTypes, (t) => getFriendlyName(t));\n\tif (schema instanceof ArrayNodeSchema) {\n\t\treturn childNames.length > 1 ? `(${childNames.join(\" | \")})[]` : `${childNames[0]}[]`;\n\t}\n\tif (schema instanceof MapNodeSchema) {\n\t\treturn childNames.length > 1\n\t\t\t? `Map<string, (${childNames.join(\" | \")})>`\n\t\t\t: `Map<string, ${childNames[0]}>`;\n\t}\n\tif (schema instanceof RecordNodeSchema) {\n\t\treturn childNames.length > 1\n\t\t\t? `Record<string, (${childNames.join(\" | \")})>`\n\t\t\t: `Record<string, ${childNames[0]}>`;\n\t}\n\tfail(\"Unexpected node schema\");\n}\n\n/**\n * Returns true if the schema identifier represents a named schema (object, named array, named map, or named record).\n * @remarks This does not include primitive schemas or inlined array/map/record schemas.\n */\nexport function isNamedSchema(schemaIdentifier: string): boolean {\n\tif (\n\t\t[\"string\", \"number\", \"boolean\", \"null\", \"handle\"].includes(\n\t\t\tunqualifySchema(schemaIdentifier),\n\t\t)\n\t) {\n\t\treturn false;\n\t}\n\n\treturn /(?:Array|Map|Record)<\\[\"(.*)\"]>/.exec(schemaIdentifier) === null;\n}\n\n/**\n * Returns the unqualified, sanitized Typescript-safe name of a schema\n * Examples:\n * - `\"my.scope.MyNode\"` returns `\"MyNode\"`\n * - `\"my.scope.MyNode-2\"` returns `\"MyNode_2\"`\n * - `\"my.scope.MyNode!\"` returns `\"MyNode_\"`\n * @remarks\n * - Removes all characters before the last dot in the schema name.\n * - Sanitizes the remainder into a valid Typescript identifier\n * - If there is a dot in a user's schema name, this might produce unexpected results.\n */\nexport function unqualifySchema(schemaIdentifier: string): string {\n\t// Get the unqualified name by removing the scope (everything before the last dot).\n\tconst matches = /[^.]+$/.exec(schemaIdentifier);\n\tconst unqualifiedName = matches === null ? schemaIdentifier : matches[0];\n\n\tlet sanitizedName = unqualifiedName;\n\n\t// Replace invalid characters with \"_\".\n\tsanitizedName = sanitizedName.replace(/[^\\w$]/g, \"_\");\n\n\t// If the first character is a number, prefix it with \"_\".\n\tif (!/^[$A-Z_a-z]/.test(sanitizedName)) {\n\t\tsanitizedName = `_${sanitizedName}`;\n\t}\n\treturn sanitizedName;\n}\n\n/**\n * Adds all (optionally filtered) schemas reachable from the given schema to the given set.\n * @returns The set of schemas added (same as the `schemas` parameter, if supplied).\n */\nexport function findSchemas(\n\tschema: ImplicitFieldSchema,\n\tfilter: (schema: TreeNodeSchema) => boolean = () => true,\n\tschemas = new Set<TreeNodeSchema>(),\n): Set<TreeNodeSchema> {\n\tfor (const nodeSchema of normalizeFieldSchema(schema).allowedTypeSet) {\n\t\tif (!schemas.has(nodeSchema)) {\n\t\t\tif (filter(nodeSchema)) {\n\t\t\t\tschemas.add(nodeSchema);\n\t\t\t}\n\t\t\tfindSchemas([...nodeSchema.childTypes], filter, schemas);\n\t\t}\n\t}\n\treturn schemas;\n}\n\n/**\n * De-capitalize (the first letter of) a string.\n */\nexport function communize(str: string): string {\n\treturn str.charAt(0).toLowerCase() + str.slice(1);\n}\n\n/**\n * Stringify an unknown error value\n */\nexport function toErrorString(error: unknown): string {\n\tif (error instanceof Error) {\n\t\treturn error.message;\n\t}\n\ttry {\n\t\treturn JSON.stringify(error);\n\t} catch {\n\t\treturn String(error);\n\t}\n}\n"]}
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,0CAA0C,CAAC;AAQtE,OAAO,EACN,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,SAAS,GACT,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AAY/E;;;;;;;GAOG;AACH,MAAM,SAAS,CAAC,CAAC,WAAW,CAC3B,QAAqB,EACrB,GAAgB;IAEhB,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;QAC1B,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;IACd,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAC1B,GAAoB,EACpB,GAAM,EACN,YAA2B;IAE3B,IAAI,KAAK,GAAG,GAAG,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,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc;IAC3C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC5B,OAAO,KAAK,CAAC,OAAO,CAAC;IACtB,CAAC;IACD,IAAI,CAAC;QACJ,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;AACF,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, fail } from \"@fluidframework/core-utils/internal\";\nimport { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport type { ImplicitFieldSchema } from \"@fluidframework/tree\";\nimport type {\n\tInsertableContent,\n\tTreeNode,\n\tTreeNodeSchema,\n\tUnsafeUnknownSchema,\n} from \"@fluidframework/tree/alpha\";\nimport {\n\tArrayNodeSchema,\n\tMapNodeSchema,\n\tRecordNodeSchema,\n\tTreeAlpha,\n} from \"@fluidframework/tree/alpha\";\nimport { NodeKind, normalizeFieldSchema } from \"@fluidframework/tree/internal\";\n\n/**\n * Subset of Map interface.\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport interface MapGetSet<K, V> {\n\tget(key: K): V | undefined;\n\tset(key: K, value: V): void;\n}\n\n/**\n * Map one iterable to another by transforming each element one at a time\n * @param iterable - the iterable to transform\n * @param map - the transformation function to run on each element of the iterable\n * @returns a new iterable of elements which have been transformed by the `map` function\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function* mapIterable<T, U>(\n\titerable: Iterable<T>,\n\tmap: (t: T) => U,\n): IterableIterator<U> {\n\tfor (const t of iterable) {\n\t\tyield map(t);\n\t}\n}\n\n/**\n * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.\n * @param map - The map to query/update\n * @param key - The key to lookup in the map\n * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists\n * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function getOrCreate<K, V>(\n\tmap: MapGetSet<K, V>,\n\tkey: K,\n\tdefaultValue: (key: K) => V,\n): V {\n\tlet value = map.get(key);\n\tif (value === undefined) {\n\t\tvalue = defaultValue(key);\n\t\tmap.set(key, value);\n\t}\n\treturn value;\n}\n\n/**\n * TODO\n */\nexport function tryGetSingleton<T>(set: ReadonlySet<T>): T | undefined {\n\tif (set.size === 1) {\n\t\tfor (const item of set) {\n\t\t\treturn item;\n\t\t}\n\t}\n}\n\n/**\n * Does it have at least two elements?\n */\nexport function hasAtLeastTwo<T>(array: T[]): array is [T, T, ...T[]] {\n\treturn array.length >= 2;\n}\n\n/**\n * Include this property in a field's schema metadata to indicate that the field's value should be generated via a provided function rather than by the LLM.\n * @example\n * ```ts\n * class Object extends schemaFactory.object(\"Object\", {\n * created: sf.required(sf.number, {\n * custom: {\n * // The LLM will ignore this field, and instead it will be populated with the result of the function\n * [llmDefault]: () => Date.now(),\n * },\n * }),\n * }) {};\n * ```\n * @alpha\n */\nexport const llmDefault = Symbol(\"tree-agent/llmDefault\");\n// TODO: make this a wrapper function instead, and hide the symbol.\n// function llmDefault<T extends FieldSchemaMetadata>(metadata: T): T { ... }\n\n/**\n * Usage fail\n */\nexport function failUsage(message: string): never {\n\tthrow new UsageError(message);\n}\n\n/**\n * Construct an object node from a schema and value.\n */\nexport function constructNode(schema: TreeNodeSchema, value: InsertableContent): TreeNode {\n\tconst node = TreeAlpha.create<UnsafeUnknownSchema>(schema, value);\n\tassert(\n\t\tnode !== undefined && node !== null && typeof node === \"object\" && !isFluidHandle(node),\n\t\t0xc1e /* Expected a constructed node to be an object */,\n\t);\n\treturn node;\n}\n\n/**\n * Returns the unqualified name of a tree value's schema (e.g. a node with schema identifier `\"my.scope.MyNode\"` returns `\"MyNode\"`).\n * @remarks If the schema is an inlined array, map, or record type, then it has no name and this function will return a string representation of the type (e.g., `\"MyNode[]\"` or `\"Map<string, MyNode>\"`).\n */\nexport function getFriendlyName(schema: TreeNodeSchema): string {\n\tif (schema.kind === NodeKind.Leaf || isNamedSchema(schema.identifier)) {\n\t\treturn unqualifySchema(schema.identifier);\n\t}\n\n\tconst childNames = Array.from(schema.childTypes, (t) => getFriendlyName(t));\n\tif (schema instanceof ArrayNodeSchema) {\n\t\treturn childNames.length > 1 ? `(${childNames.join(\" | \")})[]` : `${childNames[0]}[]`;\n\t}\n\tif (schema instanceof MapNodeSchema) {\n\t\treturn childNames.length > 1\n\t\t\t? `Map<string, (${childNames.join(\" | \")})>`\n\t\t\t: `Map<string, ${childNames[0]}>`;\n\t}\n\tif (schema instanceof RecordNodeSchema) {\n\t\treturn childNames.length > 1\n\t\t\t? `Record<string, (${childNames.join(\" | \")})>`\n\t\t\t: `Record<string, ${childNames[0]}>`;\n\t}\n\tfail(0xcb7 /* Unexpected node schema */);\n}\n\n/**\n * Returns true if the schema identifier represents a named schema (object, named array, named map, or named record).\n * @remarks This does not include primitive schemas or inlined array/map/record schemas.\n */\nexport function isNamedSchema(schemaIdentifier: string): boolean {\n\tif (\n\t\t[\"string\", \"number\", \"boolean\", \"null\", \"handle\"].includes(\n\t\t\tunqualifySchema(schemaIdentifier),\n\t\t)\n\t) {\n\t\treturn false;\n\t}\n\n\treturn /(?:Array|Map|Record)<\\[\"(.*)\"]>/.exec(schemaIdentifier) === null;\n}\n\n/**\n * Returns the unqualified, sanitized Typescript-safe name of a schema\n * Examples:\n * - `\"my.scope.MyNode\"` returns `\"MyNode\"`\n * - `\"my.scope.MyNode-2\"` returns `\"MyNode_2\"`\n * - `\"my.scope.MyNode!\"` returns `\"MyNode_\"`\n * @remarks\n * - Removes all characters before the last dot in the schema name.\n * - Sanitizes the remainder into a valid Typescript identifier\n * - If there is a dot in a user's schema name, this might produce unexpected results.\n */\nexport function unqualifySchema(schemaIdentifier: string): string {\n\t// Get the unqualified name by removing the scope (everything before the last dot).\n\tconst matches = /[^.]+$/.exec(schemaIdentifier);\n\tconst unqualifiedName = matches === null ? schemaIdentifier : matches[0];\n\n\tlet sanitizedName = unqualifiedName;\n\n\t// Replace invalid characters with \"_\".\n\tsanitizedName = sanitizedName.replace(/[^\\w$]/g, \"_\");\n\n\t// If the first character is a number, prefix it with \"_\".\n\tif (!/^[$A-Z_a-z]/.test(sanitizedName)) {\n\t\tsanitizedName = `_${sanitizedName}`;\n\t}\n\treturn sanitizedName;\n}\n\n/**\n * Adds all (optionally filtered) schemas reachable from the given schema to the given set.\n * @returns The set of schemas added (same as the `schemas` parameter, if supplied).\n */\nexport function findSchemas(\n\tschema: ImplicitFieldSchema,\n\tfilter: (schema: TreeNodeSchema) => boolean = () => true,\n\tschemas = new Set<TreeNodeSchema>(),\n): Set<TreeNodeSchema> {\n\tfor (const nodeSchema of normalizeFieldSchema(schema).allowedTypeSet) {\n\t\tif (!schemas.has(nodeSchema)) {\n\t\t\tif (filter(nodeSchema)) {\n\t\t\t\tschemas.add(nodeSchema);\n\t\t\t}\n\t\t\tfindSchemas([...nodeSchema.childTypes], filter, schemas);\n\t\t}\n\t}\n\treturn schemas;\n}\n\n/**\n * De-capitalize (the first letter of) a string.\n */\nexport function communize(str: string): string {\n\treturn str.charAt(0).toLowerCase() + str.slice(1);\n}\n\n/**\n * Stringify an unknown error value\n */\nexport function toErrorString(error: unknown): string {\n\tif (error instanceof Error) {\n\t\treturn error.message;\n\t}\n\ttry {\n\t\treturn JSON.stringify(error);\n\t} catch {\n\t\treturn String(error);\n\t}\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluidframework/tree-agent",
3
- "version": "2.74.0",
3
+ "version": "2.81.0-374083",
4
4
  "description": "Experimental package to simplify integrating AI into Fluid-based applications",
5
5
  "homepage": "https://fluidframework.com",
6
6
  "repository": {
@@ -70,24 +70,24 @@
70
70
  },
71
71
  "dependencies": {
72
72
  "@anthropic-ai/sdk": "^0.39.0",
73
- "@fluidframework/core-utils": "~2.74.0",
74
- "@fluidframework/runtime-utils": "~2.74.0",
75
- "@fluidframework/telemetry-utils": "~2.74.0",
76
- "@fluidframework/tree": "~2.74.0",
73
+ "@fluidframework/core-utils": "2.81.0-374083",
74
+ "@fluidframework/runtime-utils": "2.81.0-374083",
75
+ "@fluidframework/telemetry-utils": "2.81.0-374083",
76
+ "@fluidframework/tree": "2.81.0-374083",
77
77
  "uuid": "^11.1.0",
78
78
  "zod": "^3.25.32"
79
79
  },
80
80
  "devDependencies": {
81
- "@arethetypeswrong/cli": "^0.17.1",
81
+ "@arethetypeswrong/cli": "^0.18.2",
82
82
  "@biomejs/biome": "~1.9.3",
83
- "@fluid-internal/mocha-test-setup": "~2.74.0",
84
- "@fluid-tools/build-cli": "^0.61.0",
83
+ "@fluid-internal/mocha-test-setup": "2.81.0-374083",
84
+ "@fluid-tools/build-cli": "^0.63.0",
85
85
  "@fluidframework/build-common": "^2.0.3",
86
- "@fluidframework/build-tools": "^0.61.0",
87
- "@fluidframework/eslint-config-fluid": "~2.74.0",
88
- "@fluidframework/id-compressor": "~2.74.0",
89
- "@fluidframework/runtime-utils": "~2.74.0",
90
- "@fluidframework/test-runtime-utils": "~2.74.0",
86
+ "@fluidframework/build-tools": "^0.63.0",
87
+ "@fluidframework/eslint-config-fluid": "2.81.0-374083",
88
+ "@fluidframework/id-compressor": "2.81.0-374083",
89
+ "@fluidframework/runtime-utils": "2.81.0-374083",
90
+ "@fluidframework/test-runtime-utils": "2.81.0-374083",
91
91
  "@langchain/anthropic": "^0.3.24",
92
92
  "@langchain/core": "^0.3.78",
93
93
  "@langchain/google-genai": "^0.2.16",
@@ -97,16 +97,16 @@
97
97
  "@types/node": "^18.19.0",
98
98
  "@types/uuid": "^9.0.2",
99
99
  "c8": "^10.1.3",
100
- "concurrently": "^8.2.1",
100
+ "concurrently": "^9.2.1",
101
101
  "copyfiles": "^2.4.1",
102
- "cross-env": "^7.0.3",
103
- "eslint": "~8.57.1",
102
+ "cross-env": "^10.1.0",
103
+ "eslint": "~9.39.1",
104
104
  "eslint-config-prettier": "~10.1.8",
105
105
  "jiti": "^2.6.1",
106
106
  "mocha": "^10.8.2",
107
107
  "mocha-multi-reporters": "^1.5.1",
108
108
  "prettier": "~3.6.2",
109
- "rimraf": "^4.4.0",
109
+ "rimraf": "^6.1.2",
110
110
  "ses": "^1.14.0",
111
111
  "typescript": "~5.4.5"
112
112
  },
package/src/index.ts CHANGED
@@ -37,6 +37,9 @@ export {
37
37
  type BindableSchema,
38
38
  type Ctor,
39
39
  type Infer,
40
+ type InferZod,
41
+ type InferArgsZod,
42
+ type InferTypeFactory,
40
43
  type IExposedMethods,
41
44
  } from "./methodBinding.js";
42
45
  export type {
@@ -50,3 +53,31 @@ export type {
50
53
  TypeMatchOrError,
51
54
  IfEquals,
52
55
  } from "./propertyBinding.js";
56
+
57
+ export {
58
+ typeFactory,
59
+ isTypeFactoryType,
60
+ instanceOfsTypeFactory,
61
+ } from "./treeAgentTypes.js";
62
+
63
+ export type {
64
+ TypeFactoryType,
65
+ TypeFactoryTypeKind,
66
+ TypeFactoryString,
67
+ TypeFactoryNumber,
68
+ TypeFactoryBoolean,
69
+ TypeFactoryVoid,
70
+ TypeFactoryUndefined,
71
+ TypeFactoryNull,
72
+ TypeFactoryUnknown,
73
+ TypeFactoryArray,
74
+ TypeFactoryObject,
75
+ TypeFactoryRecord,
76
+ TypeFactoryMap,
77
+ TypeFactoryTuple,
78
+ TypeFactoryUnion,
79
+ TypeFactoryLiteral,
80
+ TypeFactoryOptional,
81
+ TypeFactoryReadonly,
82
+ TypeFactoryInstanceOf,
83
+ } from "./treeAgentTypes.js";
@@ -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>;
@@ -434,7 +440,7 @@ function renderPropertyLines(properties: Record<string, PropertyDef>): string[]
434
440
  }
435
441
  }
436
442
  const modifier = property.readOnly ? "readonly " : "";
437
- lines.push(`${modifier}${name}: ${renderZodType(property.schema)};`);
443
+ lines.push(`${modifier}${name}: ${renderType(property.schema)};`);
438
444
  }
439
445
  return lines;
440
446
  }
@@ -443,13 +449,13 @@ function formatMethod(name: string, method: FunctionWrapper): string {
443
449
  const args: string[] = [];
444
450
  for (const [argName, argType] of method.args) {
445
451
  const { innerType, optional } = unwrapOptional(argType);
446
- const renderedType = renderZodType(innerType);
452
+ const renderedType = renderType(innerType);
447
453
  args.push(`${argName}${optional ? "?" : ""}: ${renderedType}`);
448
454
  }
449
455
  if (method.rest !== null) {
450
- args.push(`...rest: ${renderZodType(method.rest)}[]`);
456
+ args.push(`...rest: ${renderType(method.rest)}[]`);
451
457
  }
452
- return `${name}(${args.join(", ")}): ${renderZodType(method.returns)};`;
458
+ return `${name}(${args.join(", ")}): ${renderType(method.returns)};`;
453
459
  }
454
460
 
455
461
  function renderLeaf(leafKind: ValueSchema): string {
@@ -482,7 +488,15 @@ function formatExpression(
482
488
  /**
483
489
  * Detects optional zod wrappers so argument lists can keep TypeScript optional markers in sync.
484
490
  */
485
- function unwrapOptional(type: z.ZodTypeAny): { innerType: z.ZodTypeAny; optional: boolean } {
491
+ function unwrapOptional(type: z.ZodTypeAny | TypeFactoryType): {
492
+ innerType: z.ZodTypeAny | TypeFactoryType;
493
+ optional: boolean;
494
+ } {
495
+ // Handle type factory optional type
496
+ if (isTypeFactoryType(type) && type._kind === "optional") {
497
+ return { innerType: (type as TypeFactoryOptional).innerType, optional: true };
498
+ }
499
+ // Handle Zod optional type
486
500
  if (type instanceof z.ZodOptional) {
487
501
  const inner = type.unwrap() as z.ZodTypeAny;
488
502
  return { innerType: inner, optional: true };
@@ -516,8 +530,10 @@ function ensureNoMemberConflicts(
516
530
  }
517
531
 
518
532
  /**
519
- * Converts schema metadata into TypeScript declarations suitable for prompt inclusion.
533
+ * Dispatches to the correct renderer based on whether the type is Zod or type factory.
520
534
  */
521
- function renderZodType(type: z.ZodTypeAny): string {
522
- return renderZodTypeScript(type, getFriendlyName, instanceOfs);
535
+ function renderType(type: z.ZodTypeAny | TypeFactoryType): string {
536
+ return isTypeFactoryType(type)
537
+ ? renderTypeFactoryTypeScript(type, getFriendlyName, instanceOfsTypeFactory)
538
+ : renderZodTypeScript(type, getFriendlyName, instanceOfs);
523
539
  }