@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/dist/utils.js CHANGED
@@ -4,19 +4,12 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.toErrorString = exports.communize = exports.findSchemas = exports.unqualifySchema = exports.isNamedSchema = exports.getFriendlyName = exports.constructNode = exports.failUsage = exports.llmDefault = exports.hasAtLeastTwo = exports.tryGetSingleton = exports.getOrCreate = exports.mapIterable = exports.fail = void 0;
7
+ exports.toErrorString = exports.communize = exports.findSchemas = exports.unqualifySchema = exports.isNamedSchema = exports.getFriendlyName = exports.constructNode = exports.failUsage = exports.llmDefault = exports.hasAtLeastTwo = exports.tryGetSingleton = exports.getOrCreate = exports.mapIterable = void 0;
8
8
  const internal_1 = require("@fluidframework/core-utils/internal");
9
9
  const runtime_utils_1 = require("@fluidframework/runtime-utils");
10
10
  const internal_2 = require("@fluidframework/telemetry-utils/internal");
11
11
  const alpha_1 = require("@fluidframework/tree/alpha");
12
12
  const internal_3 = require("@fluidframework/tree/internal");
13
- /**
14
- * TBD
15
- */
16
- function fail(message) {
17
- throw new Error(message);
18
- }
19
- exports.fail = fail;
20
13
  /**
21
14
  * Map one iterable to another by transforming each element one at a time
22
15
  * @param iterable - the iterable to transform
@@ -123,7 +116,7 @@ function getFriendlyName(schema) {
123
116
  ? `Record<string, (${childNames.join(" | ")})>`
124
117
  : `Record<string, ${childNames[0]}>`;
125
118
  }
126
- fail("Unexpected node schema");
119
+ (0, internal_1.fail)(0xcb7 /* Unexpected node schema */);
127
120
  }
128
121
  exports.getFriendlyName = getFriendlyName;
129
122
  /**
package/dist/utils.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,iEAA8D;AAC9D,uEAAsE;AAQtE,sDAKoC;AACpC,4DAA+E;AAY/E;;GAEG;AACH,SAAgB,IAAI,CAAC,OAAe;IACnC,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAFD,oBAEC;AAED;;;;;;;GAOG;AACH,QAAe,CAAC,CAAC,WAAW,CAC3B,QAAqB,EACrB,GAAgB;IAEhB,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;QAC1B,MAAM,GAAG,CAAC,CAAC,CA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C;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;AAdD,kCAcC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AAFD,8BAEC;AAED;;GAEG;AACH,SAAgB,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;AATD,sCASC","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,kEAAmE;AACnE,iEAA8D;AAC9D,uEAAsE;AAQtE,sDAKoC;AACpC,4DAA+E;AAY/E;;;;;;;GAOG;AACH,QAAe,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;AAPD,kCAOC;AAED;;;;;;;;GAQG;AACH,SAAgB,WAAW,CAC1B,GAAoB,EACpB,GAAM,EACN,YAA2B;IAE3B,IAAI,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACzB,KAAK,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC;AAXD,kCAWC;AAED;;GAEG;AACH,SAAgB,eAAe,CAAI,GAAmB;IACrD,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QACpB,KAAK,MAAM,IAAI,IAAI,GAAG,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC;QACb,CAAC;IACF,CAAC;AACF,CAAC;AAND,0CAMC;AAED;;GAEG;AACH,SAAgB,aAAa,CAAI,KAAU;IAC1C,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;AAC1B,CAAC;AAFD,sCAEC;AAED;;;;;;;;;;;;;;G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,CAAC,EAAE,CAAC;QACxC,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC;IACrC,CAAC;IACD,OAAO,aAAa,CAAC;AACtB,CAAC;AAfD,0CAeC;AAED;;;GAGG;AACH,SAAgB,WAAW,CAC1B,MAA2B,EAC3B,SAA8C,GAAG,EAAE,CAAC,IAAI,EACxD,UAAU,IAAI,GAAG,EAAkB;IAEnC,KAAK,MAAM,UAAU,IAAI,IAAA,+BAAoB,EAAC,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;AAdD,kCAcC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACnD,CAAC;AAFD,8BAEC;AAED;;GAEG;AACH,SAAgB,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;AATD,sCASC","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/eslint.config.mts CHANGED
@@ -1,8 +1,8 @@
1
- /* eslint-disable */
2
- /**
3
- * GENERATED FILE - DO NOT EDIT DIRECTLY.
4
- * To regenerate: pnpm tsx scripts/generate-flat-eslint-configs.ts --typescript
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
5
4
  */
5
+
6
6
  import type { Linter } from "eslint";
7
7
  import { strict } from "../../../common/build/eslint-config-fluid/flat.mts";
8
8
 
package/lib/alpha.d.ts CHANGED
@@ -31,6 +31,9 @@ export {
31
31
  IExposedProperties,
32
32
  IfEquals,
33
33
  Infer,
34
+ InferArgsZod,
35
+ InferTypeFactory,
36
+ InferZod,
34
37
  Logger,
35
38
  MethodKeys,
36
39
  PropertyDef,
@@ -42,12 +45,34 @@ export {
42
45
  SharedTreeSemanticAgent,
43
46
  SynchronousEditor,
44
47
  TreeView,
48
+ TypeFactoryArray,
49
+ TypeFactoryBoolean,
50
+ TypeFactoryInstanceOf,
51
+ TypeFactoryLiteral,
52
+ TypeFactoryMap,
53
+ TypeFactoryNull,
54
+ TypeFactoryNumber,
55
+ TypeFactoryObject,
56
+ TypeFactoryOptional,
57
+ TypeFactoryReadonly,
58
+ TypeFactoryRecord,
59
+ TypeFactoryString,
60
+ TypeFactoryTuple,
61
+ TypeFactoryType,
62
+ TypeFactoryTypeKind,
63
+ TypeFactoryUndefined,
64
+ TypeFactoryUnion,
65
+ TypeFactoryUnknown,
66
+ TypeFactoryVoid,
45
67
  TypeMatchOrError,
46
68
  ViewOrTree,
47
69
  buildFunc,
48
70
  createContext,
49
71
  exposeMethodsSymbol,
50
72
  exposePropertiesSymbol,
51
- llmDefault
73
+ instanceOfsTypeFactory,
74
+ isTypeFactoryType,
75
+ llmDefault,
76
+ typeFactory
52
77
  // #endregion
53
78
  } from "./index.js";
package/lib/index.d.ts CHANGED
@@ -10,6 +10,8 @@
10
10
  export { SharedTreeSemanticAgent, createContext, } from "./agent.js";
11
11
  export type { EditResult, SharedTreeChatModel, SharedTreeChatQuery, Logger, SemanticAgentOptions, SynchronousEditor, AsynchronousEditor, TreeView, ViewOrTree, Context, } from "./api.js";
12
12
  export { llmDefault } from "./utils.js";
13
- export { buildFunc, exposeMethodsSymbol, type ArgsTuple, type ExposedMethods, type Arg, type FunctionDef, type MethodKeys, type BindableSchema, type Ctor, type Infer, type IExposedMethods, } from "./methodBinding.js";
13
+ export { buildFunc, exposeMethodsSymbol, type ArgsTuple, type ExposedMethods, type Arg, type FunctionDef, type MethodKeys, type BindableSchema, type Ctor, type Infer, type InferZod, type InferArgsZod, type InferTypeFactory, type IExposedMethods, } from "./methodBinding.js";
14
14
  export type { exposePropertiesSymbol, PropertyDef, ExposedProperties, IExposedProperties, ExposableKeys, ReadOnlyRequirement, ReadonlyKeys, TypeMatchOrError, IfEquals, } from "./propertyBinding.js";
15
+ export { typeFactory, isTypeFactoryType, instanceOfsTypeFactory, } from "./treeAgentTypes.js";
16
+ export type { TypeFactoryType, TypeFactoryTypeKind, TypeFactoryString, TypeFactoryNumber, TypeFactoryBoolean, TypeFactoryVoid, TypeFactoryUndefined, TypeFactoryNull, TypeFactoryUnknown, TypeFactoryArray, TypeFactoryObject, TypeFactoryRecord, TypeFactoryMap, TypeFactoryTuple, TypeFactoryUnion, TypeFactoryLiteral, TypeFactoryOptional, TypeFactoryReadonly, TypeFactoryInstanceOf, } from "./treeAgentTypes.js";
15
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EACN,uBAAuB,EACvB,aAAa,GACb,MAAM,YAAY,CAAC;AACpB,YAAY,EACX,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,EACN,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,QAAQ,EACR,UAAU,EACV,OAAO,GACP,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACN,SAAS,EACT,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,GAAG,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,IAAI,EACT,KAAK,KAAK,EACV,KAAK,eAAe,GACpB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACX,sBAAsB,EACtB,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,mBAAmB,EACnB,YAAY,EACZ,gBAAgB,EAChB,QAAQ,GACR,MAAM,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EACN,uBAAuB,EACvB,aAAa,GACb,MAAM,YAAY,CAAC;AACpB,YAAY,EACX,UAAU,EACV,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,EACN,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,QAAQ,EACR,UAAU,EACV,OAAO,GACP,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACN,SAAS,EACT,mBAAmB,EACnB,KAAK,SAAS,EACd,KAAK,cAAc,EACnB,KAAK,GAAG,EACR,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,IAAI,EACT,KAAK,KAAK,EACV,KAAK,QAAQ,EACb,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,eAAe,GACpB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACX,sBAAsB,EACtB,WAAW,EACX,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,mBAAmB,EACnB,YAAY,EACZ,gBAAgB,EAChB,QAAQ,GACR,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,sBAAsB,GACtB,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EACX,eAAe,EACf,mBAAmB,EACnB,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,oBAAoB,EACpB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,GACrB,MAAM,qBAAqB,CAAC"}
package/lib/index.js CHANGED
@@ -10,4 +10,5 @@
10
10
  export { SharedTreeSemanticAgent, createContext, } from "./agent.js";
11
11
  export { llmDefault } from "./utils.js";
12
12
  export { buildFunc, exposeMethodsSymbol, } from "./methodBinding.js";
13
+ export { typeFactory, isTypeFactoryType, instanceOfsTypeFactory, } from "./treeAgentTypes.js";
13
14
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EACN,uBAAuB,EACvB,aAAa,GACb,MAAM,YAAY,CAAC;AAapB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACN,SAAS,EACT,mBAAmB,GAUnB,MAAM,oBAAoB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A library for creating AI agents to interact with a {@link SharedTree | https://fluidframework.com/docs/data-structures/tree/}.\n *\n * @packageDocumentation\n */\n\nexport {\n\tSharedTreeSemanticAgent,\n\tcreateContext,\n} from \"./agent.js\";\nexport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n\tLogger,\n\tSemanticAgentOptions,\n\tSynchronousEditor,\n\tAsynchronousEditor,\n\tTreeView,\n\tViewOrTree,\n\tContext,\n} from \"./api.js\";\nexport { llmDefault } from \"./utils.js\";\nexport {\n\tbuildFunc,\n\texposeMethodsSymbol,\n\ttype ArgsTuple,\n\ttype ExposedMethods,\n\ttype Arg,\n\ttype FunctionDef,\n\ttype MethodKeys,\n\ttype BindableSchema,\n\ttype Ctor,\n\ttype Infer,\n\ttype IExposedMethods,\n} from \"./methodBinding.js\";\nexport type {\n\texposePropertiesSymbol,\n\tPropertyDef,\n\tExposedProperties,\n\tIExposedProperties,\n\tExposableKeys,\n\tReadOnlyRequirement,\n\tReadonlyKeys,\n\tTypeMatchOrError,\n\tIfEquals,\n} from \"./propertyBinding.js\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AAEH,OAAO,EACN,uBAAuB,EACvB,aAAa,GACb,MAAM,YAAY,CAAC;AAapB,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EACN,SAAS,EACT,mBAAmB,GAanB,MAAM,oBAAoB,CAAC;AAa5B,OAAO,EACN,WAAW,EACX,iBAAiB,EACjB,sBAAsB,GACtB,MAAM,qBAAqB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * A library for creating AI agents to interact with a {@link SharedTree | https://fluidframework.com/docs/data-structures/tree/}.\n *\n * @packageDocumentation\n */\n\nexport {\n\tSharedTreeSemanticAgent,\n\tcreateContext,\n} from \"./agent.js\";\nexport type {\n\tEditResult,\n\tSharedTreeChatModel,\n\tSharedTreeChatQuery,\n\tLogger,\n\tSemanticAgentOptions,\n\tSynchronousEditor,\n\tAsynchronousEditor,\n\tTreeView,\n\tViewOrTree,\n\tContext,\n} from \"./api.js\";\nexport { llmDefault } from \"./utils.js\";\nexport {\n\tbuildFunc,\n\texposeMethodsSymbol,\n\ttype ArgsTuple,\n\ttype ExposedMethods,\n\ttype Arg,\n\ttype FunctionDef,\n\ttype MethodKeys,\n\ttype BindableSchema,\n\ttype Ctor,\n\ttype Infer,\n\ttype InferZod,\n\ttype InferArgsZod,\n\ttype InferTypeFactory,\n\ttype IExposedMethods,\n} from \"./methodBinding.js\";\nexport type {\n\texposePropertiesSymbol,\n\tPropertyDef,\n\tExposedProperties,\n\tIExposedProperties,\n\tExposableKeys,\n\tReadOnlyRequirement,\n\tReadonlyKeys,\n\tTypeMatchOrError,\n\tIfEquals,\n} from \"./propertyBinding.js\";\n\nexport {\n\ttypeFactory,\n\tisTypeFactoryType,\n\tinstanceOfsTypeFactory,\n} from \"./treeAgentTypes.js\";\n\nexport type {\n\tTypeFactoryType,\n\tTypeFactoryTypeKind,\n\tTypeFactoryString,\n\tTypeFactoryNumber,\n\tTypeFactoryBoolean,\n\tTypeFactoryVoid,\n\tTypeFactoryUndefined,\n\tTypeFactoryNull,\n\tTypeFactoryUnknown,\n\tTypeFactoryArray,\n\tTypeFactoryObject,\n\tTypeFactoryRecord,\n\tTypeFactoryMap,\n\tTypeFactoryTuple,\n\tTypeFactoryUnion,\n\tTypeFactoryLiteral,\n\tTypeFactoryOptional,\n\tTypeFactoryReadonly,\n\tTypeFactoryInstanceOf,\n} from \"./treeAgentTypes.js\";\n"]}
@@ -5,6 +5,7 @@
5
5
  import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
6
6
  import { NodeKind } from "@fluidframework/tree";
7
7
  import type { z } from "zod";
8
+ import type { TypeFactoryType } from "./treeAgentTypes.js";
8
9
  /**
9
10
  * A utility type that extracts the method keys from a given type.
10
11
  * @alpha
@@ -39,27 +40,39 @@ export declare function getExposedMethods(schemaClass: BindableSchema): {
39
40
  * A type that represents a function argument.
40
41
  * @alpha
41
42
  */
42
- export type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string, type: T];
43
+ export type Arg<T extends z.ZodTypeAny | TypeFactoryType = z.ZodTypeAny | TypeFactoryType> = readonly [name: string, type: T];
43
44
  /**
44
45
  * A function definition interface that describes the structure of a function.
45
46
  * @alpha
46
47
  */
47
- export interface FunctionDef<Args extends readonly Arg[], Return extends z.ZodTypeAny, Rest extends z.ZodTypeAny | null = null> {
48
+ export interface FunctionDef<Args extends readonly Arg[], Return extends z.ZodTypeAny | TypeFactoryType, Rest extends z.ZodTypeAny | TypeFactoryType | null = null> {
49
+ /**
50
+ * Optional description of the function.
51
+ */
48
52
  description?: string;
53
+ /**
54
+ * The function's parameters.
55
+ */
49
56
  args: Args;
57
+ /**
58
+ * Optional rest parameter type.
59
+ */
50
60
  rest?: Rest;
61
+ /**
62
+ * The function's return type.
63
+ */
51
64
  returns: Return;
52
65
  }
53
66
  /**
54
67
  * A class that implements the FunctionDef interface.
55
68
  */
56
- export declare class FunctionWrapper implements FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null> {
69
+ export declare class FunctionWrapper implements FunctionDef<readonly Arg[], z.ZodTypeAny | TypeFactoryType, z.ZodTypeAny | TypeFactoryType | null> {
57
70
  readonly name: string;
58
71
  readonly description: string | undefined;
59
72
  readonly args: readonly Arg[];
60
- readonly rest: z.ZodTypeAny | null;
61
- readonly returns: z.ZodTypeAny;
62
- constructor(name: string, description: string | undefined, args: readonly Arg[], rest: z.ZodTypeAny | null, returns: z.ZodTypeAny);
73
+ readonly rest: z.ZodTypeAny | TypeFactoryType | null;
74
+ readonly returns: z.ZodTypeAny | TypeFactoryType;
75
+ constructor(name: string, description: string | undefined, args: readonly Arg[], rest: z.ZodTypeAny | TypeFactoryType | null, returns: z.ZodTypeAny | TypeFactoryType);
63
76
  }
64
77
  /**
65
78
  * A utility type that extracts the argument types from a function definition.
@@ -70,26 +83,54 @@ export type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Sing
70
83
  * A utility function to build a function definition.
71
84
  * @alpha
72
85
  */
73
- export declare function buildFunc<const Return extends z.ZodTypeAny, const Args extends readonly Arg[], const Rest extends z.ZodTypeAny | null = null>(def: {
86
+ export declare function buildFunc<const Return extends z.ZodTypeAny | TypeFactoryType, const Args extends readonly Arg[], const Rest extends z.ZodTypeAny | TypeFactoryType | null = null>(def: {
74
87
  description?: string;
75
88
  returns: Return;
76
89
  rest?: Rest;
77
90
  }, ...args: Args): FunctionDef<Args, Return, Rest>;
91
+ /**
92
+ * A utility type that extracts inferred parameter types from Zod args.
93
+ * @alpha
94
+ */
95
+ export type InferArgsZod<Args extends readonly Arg<z.ZodTypeAny>[]> = Args extends readonly [
96
+ infer Head extends Arg<z.ZodTypeAny>,
97
+ ...infer Tail extends readonly Arg<z.ZodTypeAny>[]
98
+ ] ? [z.infer<Head[1]>, ...InferArgsZod<Tail>] : [];
99
+ /**
100
+ * A utility type that infers the function signature from a Zod function definition with strict type checking.
101
+ * @alpha
102
+ */
103
+ export type InferZod<T> = T extends FunctionDef<infer Args extends readonly Arg<z.ZodTypeAny>[], infer Return extends z.ZodTypeAny, any> ? (...args: InferArgsZod<Args>) => z.infer<Return> : never;
104
+ /**
105
+ * A utility type that infers the function signature from a type factory function definition with relaxed type checking.
106
+ * @alpha
107
+ */
108
+ export type InferTypeFactory<T> = T extends FunctionDef<readonly Arg[], infer Return, any> ? (...args: any[]) => any : never;
78
109
  /**
79
110
  * A utility type that infers the return type of a function definition.
80
111
  * @alpha
112
+ * @remarks
113
+ * For Zod types, provides strict compile-time type checking. For type factory types, returns `any`.
114
+ * @deprecated Use InferZod or InferTypeFactory directly for better type safety.
81
115
  */
82
- export type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Rest> ? z.infer<z.ZodFunction<z.ZodTuple<ArgsTuple<Args>, Rest>, Return>> : never;
116
+ export type Infer<T> = T extends FunctionDef<readonly Arg[], infer Return, any> ? Return extends z.ZodTypeAny ? InferZod<T> : InferTypeFactory<T> : never;
83
117
  /**
84
118
  * An interface for exposing methods of schema classes to an agent.
85
119
  * @alpha
86
120
  */
87
121
  export interface ExposedMethods {
88
- expose<const K extends string & keyof MethodKeys<InstanceType<S>>, S extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods, Z extends FunctionDef<any, any, any>>(schema: S, methodName: K, zodFunction: Z): void;
122
+ /**
123
+ * Expose a method with Zod types (strict compile-time type checking).
124
+ */
125
+ expose<const K extends string & keyof MethodKeys<InstanceType<S>>, S extends BindableSchema & Ctor<Record<K, InferZod<Z>>> & IExposedMethods, Z extends FunctionDef<readonly Arg<z.ZodTypeAny>[], z.ZodTypeAny, z.ZodTypeAny | null>>(schema: S, methodName: K, zodFunction: Z): void;
126
+ /**
127
+ * Expose a method with type factory types (relaxed compile-time type checking).
128
+ */
129
+ expose<const K extends string & keyof MethodKeys<InstanceType<S>>, S extends BindableSchema & Ctor & IExposedMethods, Z extends FunctionDef<readonly Arg<TypeFactoryType>[], TypeFactoryType, TypeFactoryType | null>>(schema: S, methodName: K, tfFunction: Z): void;
89
130
  /**
90
131
  * Create a Zod schema for a SharedTree schema class.
91
132
  * @remarks
92
- * Use it to "wrap" schema types that are referenced as arguments or return types when exposing methods (with {@link ExposedMethods.expose}).
133
+ * Use it to "wrap" schema types that are referenced as arguments or return types when exposing methods with {@link ExposedMethods}.
93
134
  */
94
135
  instanceOf<T extends TreeNodeSchemaClass>(schema: T): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;
95
136
  }
@@ -113,6 +154,9 @@ export declare const exposeMethodsSymbol: unique symbol;
113
154
  * @alpha
114
155
  */
115
156
  export interface IExposedMethods {
157
+ /**
158
+ * Static method that exposes methods of this schema class to an agent.
159
+ */
116
160
  [exposeMethodsSymbol](methods: ExposedMethods): void;
117
161
  }
118
162
  //# sourceMappingURL=methodBinding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"methodBinding.d.ts","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAG7B;;;GAGG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;KAC1B,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GAAG,CAAC,GAAG,KAAK;CAChE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,IAAI,CAAC,CAAC,GAAG,GAAG,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;AAEtD;;;GAGG;AACH,MAAM,MAAM,cAAc,GACvB,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GACvC,cAAc,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,GACvC,cAAc,CAAC,MAAM,EAA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YACN,OAAO,EAAE,cAAc,CAAC,OAAO;YAC/B,eAAe,EAAE,cAAc,CAAC,eAAe;SAC/C,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport { NodeKind } from \"@fluidframework/tree\";\nimport type { z } from \"zod\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\n\n/**\n * A utility type that extracts the method keys from a given type.\n * @alpha\n */\nexport type MethodKeys<T> = {\n\t[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;\n};\n\n/**\n * A type that represents a constructor function.\n * @alpha\n */\nexport type Ctor<T = any> = new (...args: any[]) => T;\n\n/**\n * A type that represents an object schema class.\n * @alpha\n */\nexport type BindableSchema =\n\t| TreeNodeSchema<string, NodeKind.Object>\n\t| TreeNodeSchema<string, NodeKind.Record>\n\t| TreeNodeSchema<string, NodeKind.Array>\n\t| TreeNodeSchema<string, NodeKind.Map>;\n\n/**\n * A type guard to check if a schema is {@link BindableSchema | bindable}.\n */\nexport function isBindableSchema(schema: TreeNodeSchema): schema is BindableSchema {\n\treturn (\n\t\tschema.kind === NodeKind.Object ||\n\t\tschema.kind === NodeKind.Record ||\n\t\tschema.kind === NodeKind.Array ||\n\t\tschema.kind === NodeKind.Map\n\t);\n}\n\n/**\n * Get the exposed methods of a schema class.\n * @param schemaClass - The schema class to extract methods from.\n * @returns A record of method names and their corresponding Zod types.\n */\nexport function getExposedMethods(schemaClass: BindableSchema): {\n\tmethods: Record<string, FunctionWrapper>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedMethodsI.getExposedMethods(schemaClass);\n}\n\n/**\n * A type that represents a function argument.\n * @alpha\n */\nexport type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string, type: T];\n\n/**\n * A function definition interface that describes the structure of a function.\n * @alpha\n */\nexport interface FunctionDef<\n\tArgs extends readonly Arg[],\n\tReturn extends z.ZodTypeAny,\n\tRest extends z.ZodTypeAny | null = null,\n> {\n\tdescription?: string;\n\targs: Args;\n\trest?: Rest;\n\treturns: Return;\n}\n\n/**\n * A class that implements the FunctionDef interface.\n */\nexport class FunctionWrapper\n\timplements FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>\n{\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly description: string | undefined,\n\t\tpublic readonly args: readonly Arg[],\n\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\tpublic readonly rest: z.ZodTypeAny | null,\n\t\tpublic readonly returns: z.ZodTypeAny,\n\t) {}\n}\n\n/**\n * A utility type that extracts the argument types from a function definition.\n * @alpha\n */\nexport type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Single extends Arg]\n\t? [Single[1]]\n\t: T extends readonly [infer Head extends Arg, ...infer Tail extends readonly Arg[]]\n\t\t? [Head[1], ...ArgsTuple<Tail>]\n\t\t: never;\n\n/**\n * A utility function to build a function definition.\n * @alpha\n */\nexport function buildFunc<\n\tconst Return extends z.ZodTypeAny,\n\tconst Args extends readonly Arg[],\n\tconst Rest extends z.ZodTypeAny | null = null,\n>(\n\tdef: { description?: string; returns: Return; rest?: Rest },\n\t...args: Args\n): FunctionDef<Args, Return, Rest> {\n\treturn {\n\t\tdescription: def.description,\n\t\treturns: def.returns,\n\t\targs,\n\t\trest: def.rest,\n\t};\n}\n\n/**\n * A utility type that infers the return type of a function definition.\n * @alpha\n */\nexport type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Rest>\n\t? z.infer<z.ZodFunction<z.ZodTuple<ArgsTuple<Args>, Rest>, Return>>\n\t: never;\n\n/**\n * An interface for exposing methods of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedMethods {\n\texpose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,\n\t\tZ extends FunctionDef<any, any, any>,\n\t>(schema: S, methodName: K, zodFunction: Z): void;\n\n\t/**\n\t * Create a Zod schema for a SharedTree schema class.\n\t * @remarks\n\t * Use it to \"wrap\" schema types that are referenced as arguments or return types when exposing methods (with {@link ExposedMethods.expose}).\n\t */\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * A symbol used to expose methods to the LLM.\n * @alpha\n */\nexport const exposeMethodsSymbol: unique symbol = Symbol(\"run\");\n\n/**\n * An interface that SharedTree schema classes should implement to expose their methods to the LLM.\n *\n * @remarks\n * The `getExposedMethods` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedMethods {\n\t[exposeMethodsSymbol](methods: ExposedMethods): void;\n}\n\nclass ExposedMethodsI implements ExposedMethods {\n\tprivate readonly methods: Record<string, FunctionWrapper> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic expose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,\n\t\tZ extends FunctionDef<readonly Arg[], z.ZodTypeAny, z.ZodTypeAny | null>,\n\t>(schema: S, methodName: K, functionDef: Z): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose methods on the \"this\" object');\n\t\t}\n\t\tthis.methods[methodName] = new FunctionWrapper(\n\t\t\tmethodName,\n\t\t\tfunctionDef.description,\n\t\t\tfunctionDef.args,\n\t\t\t// eslint-disable-next-line unicorn/no-null\n\t\t\tfunctionDef.rest ?? null,\n\t\t\tfunctionDef.returns,\n\t\t);\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedMethods(schemaClass: BindableSchema): {\n\t\tmethods: Record<string, FunctionWrapper>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposedMethods = new ExposedMethodsI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedMethods;\n\t\tif (extractable[exposeMethodsSymbol] !== undefined) {\n\t\t\textractable[exposeMethodsSymbol](exposedMethods);\n\t\t}\n\t\treturn {\n\t\t\tmethods: exposedMethods.methods,\n\t\t\treferencedTypes: exposedMethods.referencedTypes,\n\t\t};\n\t}\n}\n"]}
1
+ {"version":3,"file":"methodBinding.js","sourceRoot":"","sources":["../src/methodBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAGhD,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AA2BtD;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAsB;IACtD,OAAO,CACN,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM;QAC/B,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM;QAC/B,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,KAAK;QAC9B,MAAM,CAAC,IAAI,KAAK,QAAQ,CAAC,GAAG,CAC5B,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAA2B;IAI5D,OAAO,eAAe,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;AACvD,CAAC;AAoCD;;GAEG;AACH,MAAM,OAAO,eAAe;IAQ3B,YACiB,IAAY,EACZ,WA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C3B,CAAC;IAEM,MAAM,CAAC,iBAAiB,CAAC,WAA2B;QAI1D,MAAM,cAAc,GAAG,IAAI,eAAe,CAAC,WAAW,CAAC,CAAC;QACxD,MAAM,WAAW,GAAG,WAAyC,CAAC;QAC9D,IAAI,WAAW,CAAC,mBAAmB,CAAC,KAAK,SAAS,EAAE,CAAC;YACpD,WAAW,CAAC,mBAAmB,CAAC,CAAC,cAAc,CAAC,CAAC;QAClD,CAAC;QACD,OAAO;YACN,OAAO,EAAE,cAAc,CAAC,OAAO;YAC/B,eAAe,EAAE,cAAc,CAAC,eAAe;SAC/C,CAAC;IACH,CAAC;CACD","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport { NodeKind } from \"@fluidframework/tree\";\nimport type { z } from \"zod\";\n\nimport { instanceOf } from \"./renderZodTypeScript.js\";\nimport type { TypeFactoryType } from \"./treeAgentTypes.js\";\n\n/**\n * A utility type that extracts the method keys from a given type.\n * @alpha\n */\nexport type MethodKeys<T> = {\n\t[K in keyof T]: T[K] extends (...args: any[]) => any ? K : never;\n};\n\n/**\n * A type that represents a constructor function.\n * @alpha\n */\nexport type Ctor<T = any> = new (...args: any[]) => T;\n\n/**\n * A type that represents an object schema class.\n * @alpha\n */\nexport type BindableSchema =\n\t| TreeNodeSchema<string, NodeKind.Object>\n\t| TreeNodeSchema<string, NodeKind.Record>\n\t| TreeNodeSchema<string, NodeKind.Array>\n\t| TreeNodeSchema<string, NodeKind.Map>;\n\n/**\n * A type guard to check if a schema is {@link BindableSchema | bindable}.\n */\nexport function isBindableSchema(schema: TreeNodeSchema): schema is BindableSchema {\n\treturn (\n\t\tschema.kind === NodeKind.Object ||\n\t\tschema.kind === NodeKind.Record ||\n\t\tschema.kind === NodeKind.Array ||\n\t\tschema.kind === NodeKind.Map\n\t);\n}\n\n/**\n * Get the exposed methods of a schema class.\n * @param schemaClass - The schema class to extract methods from.\n * @returns A record of method names and their corresponding Zod types.\n */\nexport function getExposedMethods(schemaClass: BindableSchema): {\n\tmethods: Record<string, FunctionWrapper>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedMethodsI.getExposedMethods(schemaClass);\n}\n\n/**\n * A type that represents a function argument.\n * @alpha\n */\nexport type Arg<T extends z.ZodTypeAny | TypeFactoryType = z.ZodTypeAny | TypeFactoryType> =\n\treadonly [name: string, type: T];\n\n/**\n * A function definition interface that describes the structure of a function.\n * @alpha\n */\nexport interface FunctionDef<\n\tArgs extends readonly Arg[],\n\tReturn extends z.ZodTypeAny | TypeFactoryType,\n\tRest extends z.ZodTypeAny | TypeFactoryType | null = null,\n> {\n\t/**\n\t * Optional description of the function.\n\t */\n\tdescription?: string;\n\t/**\n\t * The function's parameters.\n\t */\n\targs: Args;\n\t/**\n\t * Optional rest parameter type.\n\t */\n\trest?: Rest;\n\t/**\n\t * The function's return type.\n\t */\n\treturns: Return;\n}\n\n/**\n * A class that implements the FunctionDef interface.\n */\nexport class FunctionWrapper\n\timplements\n\t\tFunctionDef<\n\t\t\treadonly Arg[],\n\t\t\tz.ZodTypeAny | TypeFactoryType,\n\t\t\tz.ZodTypeAny | TypeFactoryType | null\n\t\t>\n{\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly description: string | undefined,\n\t\tpublic readonly args: readonly Arg[],\n\t\t// eslint-disable-next-line @rushstack/no-new-null\n\t\tpublic readonly rest: z.ZodTypeAny | TypeFactoryType | null,\n\t\tpublic readonly returns: z.ZodTypeAny | TypeFactoryType,\n\t) {}\n}\n\n/**\n * A utility type that extracts the argument types from a function definition.\n * @alpha\n */\nexport type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Single extends Arg]\n\t? [Single[1]]\n\t: T extends readonly [infer Head extends Arg, ...infer Tail extends readonly Arg[]]\n\t\t? [Head[1], ...ArgsTuple<Tail>]\n\t\t: never;\n\n/**\n * A utility function to build a function definition.\n * @alpha\n */\nexport function buildFunc<\n\tconst Return extends z.ZodTypeAny | TypeFactoryType,\n\tconst Args extends readonly Arg[],\n\tconst Rest extends z.ZodTypeAny | TypeFactoryType | null = null,\n>(\n\tdef: { description?: string; returns: Return; rest?: Rest },\n\t...args: Args\n): FunctionDef<Args, Return, Rest> {\n\treturn {\n\t\tdescription: def.description,\n\t\treturns: def.returns,\n\t\targs,\n\t\trest: def.rest,\n\t};\n}\n\n/**\n * A utility type that extracts inferred parameter types from Zod args.\n * @alpha\n */\nexport type InferArgsZod<Args extends readonly Arg<z.ZodTypeAny>[]> = Args extends readonly [\n\tinfer Head extends Arg<z.ZodTypeAny>,\n\t...infer Tail extends readonly Arg<z.ZodTypeAny>[],\n]\n\t? [z.infer<Head[1]>, ...InferArgsZod<Tail>]\n\t: [];\n\n/**\n * A utility type that infers the function signature from a Zod function definition with strict type checking.\n * @alpha\n */\nexport type InferZod<T> = T extends FunctionDef<\n\tinfer Args extends readonly Arg<z.ZodTypeAny>[],\n\tinfer Return extends z.ZodTypeAny,\n\tany\n>\n\t? (...args: InferArgsZod<Args>) => z.infer<Return>\n\t: never;\n\n/**\n * A utility type that infers the function signature from a type factory function definition with relaxed type checking.\n * @alpha\n */\nexport type InferTypeFactory<T> = T extends FunctionDef<readonly Arg[], infer Return, any>\n\t? (...args: any[]) => any\n\t: never;\n\n/**\n * A utility type that infers the return type of a function definition.\n * @alpha\n * @remarks\n * For Zod types, provides strict compile-time type checking. For type factory types, returns `any`.\n * @deprecated Use InferZod or InferTypeFactory directly for better type safety.\n */\nexport type Infer<T> = T extends FunctionDef<readonly Arg[], infer Return, any>\n\t? Return extends z.ZodTypeAny\n\t\t? InferZod<T>\n\t\t: InferTypeFactory<T>\n\t: never;\n\n/**\n * An interface for exposing methods of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedMethods {\n\t/**\n\t * Expose a method with Zod types (strict compile-time type checking).\n\t */\n\texpose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor<Record<K, InferZod<Z>>> & IExposedMethods,\n\t\tZ extends FunctionDef<readonly Arg<z.ZodTypeAny>[], z.ZodTypeAny, z.ZodTypeAny | null>,\n\t>(schema: S, methodName: K, zodFunction: Z): void;\n\n\t/**\n\t * Expose a method with type factory types (relaxed compile-time type checking).\n\t */\n\texpose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor & IExposedMethods,\n\t\tZ extends FunctionDef<\n\t\t\treadonly Arg<TypeFactoryType>[],\n\t\t\tTypeFactoryType,\n\t\t\tTypeFactoryType | null\n\t\t>,\n\t>(schema: S, methodName: K, tfFunction: Z): void;\n\n\t/**\n\t * Create a Zod schema for a SharedTree schema class.\n\t * @remarks\n\t * Use it to \"wrap\" schema types that are referenced as arguments or return types when exposing methods with {@link ExposedMethods}.\n\t */\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * A symbol used to expose methods to the LLM.\n * @alpha\n */\nexport const exposeMethodsSymbol: unique symbol = Symbol(\"run\");\n\n/**\n * An interface that SharedTree schema classes should implement to expose their methods to the LLM.\n *\n * @remarks\n * The `getExposedMethods` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedMethods {\n\t/**\n\t * Static method that exposes methods of this schema class to an agent.\n\t */\n\t[exposeMethodsSymbol](methods: ExposedMethods): void;\n}\n\nclass ExposedMethodsI implements ExposedMethods {\n\tprivate readonly methods: Record<string, FunctionWrapper> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic expose<\n\t\tconst K extends string & keyof MethodKeys<InstanceType<S>>,\n\t\tS extends BindableSchema & Ctor & IExposedMethods,\n\t\tZ extends FunctionDef<\n\t\t\treadonly Arg[],\n\t\t\tz.ZodTypeAny | TypeFactoryType,\n\t\t\tz.ZodTypeAny | TypeFactoryType | null\n\t\t>,\n\t>(schema: S, methodName: K, functionDef: Z): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose methods on the \"this\" object');\n\t\t}\n\t\tthis.methods[methodName] = new FunctionWrapper(\n\t\t\tmethodName,\n\t\t\tfunctionDef.description,\n\t\t\tfunctionDef.args,\n\t\t\t// eslint-disable-next-line unicorn/no-null\n\t\t\tfunctionDef.rest ?? null,\n\t\t\tfunctionDef.returns,\n\t\t);\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedMethods(schemaClass: BindableSchema): {\n\t\tmethods: Record<string, FunctionWrapper>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposedMethods = new ExposedMethodsI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedMethods;\n\t\tif (extractable[exposeMethodsSymbol] !== undefined) {\n\t\t\textractable[exposeMethodsSymbol](exposedMethods);\n\t\t}\n\t\treturn {\n\t\t\tmethods: exposedMethods.methods,\n\t\t\treferencedTypes: exposedMethods.referencedTypes,\n\t\t};\n\t}\n}\n"]}
@@ -5,6 +5,7 @@
5
5
  import type { TreeNodeSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
6
6
  import type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from "zod";
7
7
  import type { BindableSchema, Ctor } from "./methodBinding.js";
8
+ import type { TypeFactoryType } from "./treeAgentTypes.js";
8
9
  /**
9
10
  * A symbol used to expose properties to the LLM.
10
11
  * @alpha
@@ -60,21 +61,67 @@ export type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]
60
61
  * @alpha
61
62
  */
62
63
  export declare class PropertyDef {
64
+ /**
65
+ * The name of the property.
66
+ */
63
67
  readonly name: string;
68
+ /**
69
+ * Optional description of the property.
70
+ */
64
71
  readonly description: string | undefined;
65
- readonly schema: ZodTypeAny;
72
+ /**
73
+ * The schema defining the property's type (either Zod or TypeFactory).
74
+ */
75
+ readonly schema: ZodTypeAny | TypeFactoryType;
76
+ /**
77
+ * Whether the property is readonly.
78
+ */
66
79
  readonly readOnly: boolean;
67
- constructor(name: string, description: string | undefined, schema: ZodTypeAny, readOnly: boolean);
80
+ constructor(
81
+ /**
82
+ * The name of the property.
83
+ */
84
+ name: string,
85
+ /**
86
+ * Optional description of the property.
87
+ */
88
+ description: string | undefined,
89
+ /**
90
+ * The schema defining the property's type (either Zod or TypeFactory).
91
+ */
92
+ schema: ZodTypeAny | TypeFactoryType,
93
+ /**
94
+ * Whether the property is readonly.
95
+ */
96
+ readOnly: boolean);
68
97
  }
69
98
  /**
70
99
  * An interface for exposing properties of schema classes to an agent.
71
100
  * @alpha
72
101
  */
73
102
  export interface ExposedProperties {
103
+ /**
104
+ * Expose a property with Zod type checking.
105
+ */
74
106
  exposeProperty<S extends BindableSchema & Ctor, K extends string & ExposableKeys<InstanceType<S>>, TZ extends ZodTypeAny>(schema: S, name: K, def: {
75
107
  schema: TZ;
76
108
  description?: string;
77
109
  } & ReadOnlyRequirement<InstanceType<S>, K> & TypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>): void;
110
+ /**
111
+ * Expose a property with type factory type and metadata.
112
+ */
113
+ exposeProperty<S extends BindableSchema & Ctor, K extends string & ExposableKeys<InstanceType<S>>>(schema: S, name: K, def: {
114
+ schema: TypeFactoryType;
115
+ description?: string;
116
+ readOnly?: boolean;
117
+ }): void;
118
+ /**
119
+ * Expose a property with type factory type (simple form).
120
+ */
121
+ exposeProperty<S extends BindableSchema & Ctor, K extends string & ExposableKeys<InstanceType<S>>>(schema: S, name: K, tfType: TypeFactoryType): void;
122
+ /**
123
+ * Create a Zod type that references a SharedTree schema class.
124
+ */
78
125
  instanceOf<T extends TreeNodeSchemaClass>(schema: T): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;
79
126
  }
80
127
  /**
@@ -92,6 +139,9 @@ export interface ExposedProperties {
92
139
  * @alpha
93
140
  */
94
141
  export interface IExposedProperties {
142
+ /**
143
+ * Static method that exposes properties of this schema class to an agent.
144
+ */
95
145
  [exposePropertiesSymbol]?(properties: ExposedProperties): void;
96
146
  }
97
147
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"propertyBinding.d.ts","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAChF,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,KAAK,IAAI,QAAQ,EAAE,MAAM,KAAK,CAAC;AAE9E,OAAO,KAAK,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAG/D;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,OAAO,MAE3C,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI;KAC7B,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,GAAG,KAAK,GAAG,CAAC;CACjE,CAAC,MAAM,CAAC,CAAC,CAAC;AAEX;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,S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C,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,GACjD,IAAI,CAAC;IAER;;OAEG;IACH,cAAc,CACb,CAAC,SAAS,cAAc,GAAG,IAAI,EAC/B,CAAC,SAAS,MAAM,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAEjD,MAAM,EAAE,CAAC,EACT,IAAI,EAAE,CAAC,EACP,GAAG,EAAE;QAAE,MAAM,EAAE,eAAe,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GACxE,IAAI,CAAC;IAER;;OAEG;IACH,cAAc,CACb,CAAC,SAAS,cAAc,GAAG,IAAI,EAC/B,CAAC,SAAS,MAAM,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,EAChD,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI,CAAC;IAErD;;OAEG;IACH,UAAU,CAAC,CAAC,SAAS,mBAAmB,EACvC,MAAM,EAAE,CAAC,GACP,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;CACzD;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,WAAW,kBAAkB;IAClC;;OAEG;IACH,CAAC,sBAAsB,CAAC,CAAC,CAAC,UAAU,EAAE,iBAAiB,GAAG,IAAI,CAAC;CAC/D;AAkED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,cAAc,GAAG;IAClE,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACxC,eAAe,EAAE,GAAG,CAAC,cAAc,CAAC,CAAC;CACrC,CAEA"}
@@ -3,6 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
  import { instanceOf } from "./renderZodTypeScript.js";
6
+ import { isTypeFactoryType } from "./treeAgentTypes.js";
6
7
  /**
7
8
  * A symbol used to expose properties to the LLM.
8
9
  * @alpha
@@ -13,7 +14,23 @@ export const exposePropertiesSymbol = Symbol.for("@fluidframework/tree-agent/exp
13
14
  * @alpha
14
15
  */
15
16
  export class PropertyDef {
16
- constructor(name, description, schema, readOnly) {
17
+ constructor(
18
+ /**
19
+ * The name of the property.
20
+ */
21
+ name,
22
+ /**
23
+ * Optional description of the property.
24
+ */
25
+ description,
26
+ /**
27
+ * The schema defining the property's type (either Zod or TypeFactory).
28
+ */
29
+ schema,
30
+ /**
31
+ * Whether the property is readonly.
32
+ */
33
+ readOnly) {
17
34
  this.name = name;
18
35
  this.description = description;
19
36
  this.schema = schema;
@@ -26,11 +43,19 @@ class ExposedPropertiesI {
26
43
  this.properties = {};
27
44
  this.referencedTypes = new Set();
28
45
  }
29
- exposeProperty(schema, name, def) {
46
+ exposeProperty(schema, name, defOrType) {
30
47
  if (schema !== this.schemaClass) {
31
48
  throw new Error('Must expose properties on the "this" schema class');
32
49
  }
33
- this.properties[name] = new PropertyDef(name, def.description, def.schema, def.readOnly === true);
50
+ // Handle TypeFactoryType (simple case - type passed directly)
51
+ if (isTypeFactoryType(defOrType)) {
52
+ this.properties[name] = new PropertyDef(name, undefined, defOrType, false);
53
+ }
54
+ else {
55
+ // Handle object with schema property (works for both Zod and TypeFactory)
56
+ const def = defOrType;
57
+ this.properties[name] = new PropertyDef(name, def.description, def.schema, def.readOnly === true);
58
+ }
34
59
  }
35
60
  instanceOf(schema) {
36
61
  this.referencedTypes.add(schema);
@@ -1 +1 @@
1
- {"version":3,"file":"propertyBinding.js","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAkB,MAAM,CAAC,GAAG,CAC9D,6CAA6C,CAC7C,CAAC;AAmDF;;;GAGG;AACH,MAAM,O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AC;IACH,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,WAA2B;IAI/D,OAAO,kBAAkB,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from \"zod\";\n\nimport type { BindableSchema, Ctor } from \"./methodBinding.js\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\n\n/**\n * A symbol used to expose properties to the LLM.\n * @alpha\n */\nexport const exposePropertiesSymbol: unique symbol = Symbol.for(\n\t\"@fluidframework/tree-agent/exposeProperties\",\n);\n\n/**\n * Set of property keys from `T` that are not methods.\n * @alpha\n */\nexport type ExposableKeys<T> = {\n\t[K in keyof T]?: T[K] extends (...args: any[]) => any ? never : K;\n}[keyof T];\n\n/**\n * Type-level equality test used as a helper to evaluate readonly keys.\n * - If X and Y are the same type, it evaluates to A.\n * - If X and Y are different, it evaluates to B.\n * @alpha\n */\nexport type IfEquals<X, Y, A = true, B = false> = (<T>() => T extends X ? 1 : 2) extends <\n\tT,\n>() => T extends Y ? 1 : 2\n\t? A\n\t: B;\n\n/**\n * Produces a union of keys of `T` which are readonly.\n * @alpha\n */\nexport type ReadonlyKeys<T> = {\n\t// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\n\t[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>;\n}[keyof T];\n\n/**\n * Type to enforce `readOnly: true` for readonly properties.\n * @alpha\n */\nexport type ReadOnlyRequirement<TObj, K extends keyof TObj> = {\n\t[P in K]-?: P extends ReadonlyKeys<TObj> ? { readOnly: true } : { readOnly?: false };\n}[K];\n\n/**\n * Emits compile-time error when there is a type mismatch.\n * @alpha\n */\nexport type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]\n\t? unknown\n\t: {\n\t\t\t__error__: \"Zod schema value type does not match the property's declared type\";\n\t\t\texpected: Expected;\n\t\t\treceived: Received;\n\t\t};\n\n/**\n * A property definition class that describes the structure of the property\n * @alpha\n */\nexport class PropertyDef {\n\tpublic constructor(\n\t\tpublic readonly name: string,\n\t\tpublic readonly description: string | undefined,\n\t\tpublic readonly schema: ZodTypeAny,\n\t\tpublic readonly readOnly: boolean,\n\t) {}\n}\n\n/**\n * An interface for exposing properties of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedProperties {\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void;\n\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * An interface that SharedTree schema classes should implement to expose their properties to the LLM.\n *\n * @remarks\n * The `getExposedProperties` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedProperties {\n\t[exposePropertiesSymbol]?(properties: ExposedProperties): void;\n}\n\nclass ExposedPropertiesI implements ExposedProperties {\n\tprivate readonly properties: Record<string, PropertyDef> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic exposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose properties on the \"this\" schema class');\n\t\t}\n\t\tthis.properties[name] = new PropertyDef(\n\t\t\tname,\n\t\t\tdef.description,\n\t\t\tdef.schema,\n\t\t\tdef.readOnly === true,\n\t\t);\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedProperties(schemaClass: BindableSchema): {\n\t\tproperties: Record<string, PropertyDef>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposed = new ExposedPropertiesI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedProperties;\n\t\tif (extractable[exposePropertiesSymbol] !== undefined) {\n\t\t\textractable[exposePropertiesSymbol](exposed);\n\t\t}\n\t\treturn {\n\t\t\tproperties: exposed.properties,\n\t\t\treferencedTypes: exposed.referencedTypes,\n\t\t};\n\t}\n}\n\n/**\n * Get the exposed properties of a schema class.\n * @param schemaClass - The schema class to extract properties from.\n * @returns A record of property names and their corresponding Zod types.\n */\nexport function getExposedProperties(schemaClass: BindableSchema): {\n\tproperties: Record<string, PropertyDef>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedPropertiesI.getExposedProperties(schemaClass);\n}\n"]}
1
+ {"version":3,"file":"propertyBinding.js","sourceRoot":"","sources":["../src/propertyBinding.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAOH,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAEtD,OAAO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,CAChB,MAAS;QAET,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjC,OAAO,UAAU,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,oBAAoB,CAAC,WAA2B;QAI7D,MAAM,OAAO,GAAG,IAAI,kBAAkB,CAAC,WAAW,CAAC,CAAC;QACpD,MAAM,WAAW,GAAG,WAA4C,CAAC;QACjE,IAAI,WAAW,CAAC,sBAAsB,CAAC,KAAK,SAAS,EAAE,CAAC;YACvD,WAAW,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAAC,CAAC;QAC9C,CAAC;QACD,OAAO;YACN,UAAU,EAAE,OAAO,CAAC,UAAU;YAC9B,eAAe,EAAE,OAAO,CAAC,eAAe;SACxC,CAAC;IACH,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,WAA2B;IAI/D,OAAO,kBAAkB,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { TreeNodeSchema, TreeNodeSchemaClass } from \"@fluidframework/tree\";\nimport type { ZodType, ZodTypeAny, ZodTypeDef, infer as ZodInfer } from \"zod\";\n\nimport type { BindableSchema, Ctor } from \"./methodBinding.js\";\nimport { instanceOf } from \"./renderZodTypeScript.js\";\nimport type { TypeFactoryType } from \"./treeAgentTypes.js\";\nimport { isTypeFactoryType } from \"./treeAgentTypes.js\";\n\n/**\n * A symbol used to expose properties to the LLM.\n * @alpha\n */\nexport const exposePropertiesSymbol: unique symbol = Symbol.for(\n\t\"@fluidframework/tree-agent/exposeProperties\",\n);\n\n/**\n * Set of property keys from `T` that are not methods.\n * @alpha\n */\nexport type ExposableKeys<T> = {\n\t[K in keyof T]?: T[K] extends (...args: any[]) => any ? never : K;\n}[keyof T];\n\n/**\n * Type-level equality test used as a helper to evaluate readonly keys.\n * - If X and Y are the same type, it evaluates to A.\n * - If X and Y are different, it evaluates to B.\n * @alpha\n */\nexport type IfEquals<X, Y, A = true, B = false> = (<T>() => T extends X ? 1 : 2) extends <\n\tT,\n>() => T extends Y ? 1 : 2\n\t? A\n\t: B;\n\n/**\n * Produces a union of keys of `T` which are readonly.\n * @alpha\n */\nexport type ReadonlyKeys<T> = {\n\t// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style\n\t[P in keyof T]-?: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, never, P>;\n}[keyof T];\n\n/**\n * Type to enforce `readOnly: true` for readonly properties.\n * @alpha\n */\nexport type ReadOnlyRequirement<TObj, K extends keyof TObj> = {\n\t[P in K]-?: P extends ReadonlyKeys<TObj> ? { readOnly: true } : { readOnly?: false };\n}[K];\n\n/**\n * Emits compile-time error when there is a type mismatch.\n * @alpha\n */\nexport type TypeMatchOrError<Expected, Received> = [Received] extends [Expected]\n\t? unknown\n\t: {\n\t\t\t__error__: \"Zod schema value type does not match the property's declared type\";\n\t\t\texpected: Expected;\n\t\t\treceived: Received;\n\t\t};\n\n/**\n * A property definition class that describes the structure of the property\n * @alpha\n */\nexport class PropertyDef {\n\tpublic constructor(\n\t\t/**\n\t\t * The name of the property.\n\t\t */\n\t\tpublic readonly name: string,\n\t\t/**\n\t\t * Optional description of the property.\n\t\t */\n\t\tpublic readonly description: string | undefined,\n\t\t/**\n\t\t * The schema defining the property's type (either Zod or TypeFactory).\n\t\t */\n\t\tpublic readonly schema: ZodTypeAny | TypeFactoryType,\n\t\t/**\n\t\t * Whether the property is readonly.\n\t\t */\n\t\tpublic readonly readOnly: boolean,\n\t) {}\n}\n\n/**\n * An interface for exposing properties of schema classes to an agent.\n * @alpha\n */\nexport interface ExposedProperties {\n\t/**\n\t * Expose a property with Zod type checking.\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>,\n\t): void;\n\n\t/**\n\t * Expose a property with type factory type and metadata.\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdef: { schema: TypeFactoryType; description?: string; readOnly?: boolean },\n\t): void;\n\n\t/**\n\t * Expose a property with type factory type (simple form).\n\t */\n\texposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t>(schema: S, name: K, tfType: TypeFactoryType): void;\n\n\t/**\n\t * Create a Zod type that references a SharedTree schema class.\n\t */\n\tinstanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>>;\n}\n\n/**\n * An interface that SharedTree schema classes should implement to expose their properties to the LLM.\n *\n * @remarks\n * The `getExposedProperties` free function will cause the method here to be called on the class passed to it.\n *\n * @privateremarks\n * Implementing this interface correctly seems tricky?\n * To actually implement it in a way that satisfies TypeScript,\n * classes need to declare both a static version and an instance version of the method\n * (the instance one can just delegate to the static one).\n *\n * @alpha\n */\nexport interface IExposedProperties {\n\t/**\n\t * Static method that exposes properties of this schema class to an agent.\n\t */\n\t[exposePropertiesSymbol]?(properties: ExposedProperties): void;\n}\n\nclass ExposedPropertiesI implements ExposedProperties {\n\tprivate readonly properties: Record<string, PropertyDef> = {};\n\tprivate readonly referencedTypes = new Set<TreeNodeSchema>();\n\n\tpublic constructor(private readonly schemaClass: BindableSchema) {}\n\n\tpublic exposeProperty<\n\t\tS extends BindableSchema & Ctor,\n\t\tK extends string & ExposableKeys<InstanceType<S>>,\n\t\tTZ extends ZodTypeAny,\n\t>(\n\t\tschema: S,\n\t\tname: K,\n\t\tdefOrType:\n\t\t\t| ({ schema: TZ; description?: string } & ReadOnlyRequirement<InstanceType<S>, K> &\n\t\t\t\t\tTypeMatchOrError<InstanceType<S>[K], ZodInfer<TZ>>)\n\t\t\t| TypeFactoryType,\n\t): void {\n\t\tif (schema !== this.schemaClass) {\n\t\t\tthrow new Error('Must expose properties on the \"this\" schema class');\n\t\t}\n\n\t\t// Handle TypeFactoryType (simple case - type passed directly)\n\t\tif (isTypeFactoryType(defOrType)) {\n\t\t\tthis.properties[name] = new PropertyDef(name, undefined, defOrType, false);\n\t\t} else {\n\t\t\t// Handle object with schema property (works for both Zod and TypeFactory)\n\t\t\tconst def = defOrType as {\n\t\t\t\tschema: TZ | TypeFactoryType;\n\t\t\t\tdescription?: string;\n\t\t\t\treadOnly?: boolean;\n\t\t\t};\n\t\t\tthis.properties[name] = new PropertyDef(\n\t\t\t\tname,\n\t\t\t\tdef.description,\n\t\t\t\tdef.schema,\n\t\t\t\tdef.readOnly === true,\n\t\t\t);\n\t\t}\n\t}\n\n\tpublic instanceOf<T extends TreeNodeSchemaClass>(\n\t\tschema: T,\n\t): ZodType<InstanceType<T>, ZodTypeDef, InstanceType<T>> {\n\t\tthis.referencedTypes.add(schema);\n\t\treturn instanceOf(schema);\n\t}\n\n\tpublic static getExposedProperties(schemaClass: BindableSchema): {\n\t\tproperties: Record<string, PropertyDef>;\n\t\treferencedTypes: Set<TreeNodeSchema>;\n\t} {\n\t\tconst exposed = new ExposedPropertiesI(schemaClass);\n\t\tconst extractable = schemaClass as unknown as IExposedProperties;\n\t\tif (extractable[exposePropertiesSymbol] !== undefined) {\n\t\t\textractable[exposePropertiesSymbol](exposed);\n\t\t}\n\t\treturn {\n\t\t\tproperties: exposed.properties,\n\t\t\treferencedTypes: exposed.referencedTypes,\n\t\t};\n\t}\n}\n\n/**\n * Get the exposed properties of a schema class.\n * @param schemaClass - The schema class to extract properties from.\n * @returns A record of property names and their corresponding Zod types.\n */\nexport function getExposedProperties(schemaClass: BindableSchema): {\n\tproperties: Record<string, PropertyDef>;\n\treferencedTypes: Set<TreeNodeSchema>;\n} {\n\treturn ExposedPropertiesI.getExposedProperties(schemaClass);\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"renderSchemaTypeScript.d.ts","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAIX,gBAAgB,EAGhB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,oBAAoB,CAAC;AAiC1E;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAClD,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GAC1C,4BAA4B,CAkV9B"}
1
+ {"version":3,"file":"renderSchemaTypeScript.d.ts","sourceRoot":"","sources":["../src/renderSchemaTypeScript.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAIH,OAAO,KAAK,EAIX,gBAAgB,EAGhB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EAAE,cAAc,EAAmB,MAAM,oBAAoB,CAAC;AAuC1E;;GAEG;AACH,MAAM,WAAW,4BAA4B;IAC5C,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACrC,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EAClD,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,GAC1C,4BAA4B,CAkV9B"}