@fluidframework/tree-agent 2.80.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 (72) hide show
  1. package/api-report/tree-agent.alpha.api.md +163 -20
  2. package/dist/alpha.d.ts +26 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +5 -1
  6. package/dist/index.js.map +1 -1
  7. package/dist/methodBinding.d.ts +54 -10
  8. package/dist/methodBinding.d.ts.map +1 -1
  9. package/dist/methodBinding.js.map +1 -1
  10. package/dist/propertyBinding.d.ts +52 -2
  11. package/dist/propertyBinding.d.ts.map +1 -1
  12. package/dist/propertyBinding.js +28 -3
  13. package/dist/propertyBinding.js.map +1 -1
  14. package/dist/renderSchemaTypeScript.d.ts.map +1 -1
  15. package/dist/renderSchemaTypeScript.js +16 -7
  16. package/dist/renderSchemaTypeScript.js.map +1 -1
  17. package/dist/renderTypeFactoryTypeScript.d.ts +13 -0
  18. package/dist/renderTypeFactoryTypeScript.d.ts.map +1 -0
  19. package/dist/renderTypeFactoryTypeScript.js +222 -0
  20. package/dist/renderTypeFactoryTypeScript.js.map +1 -0
  21. package/dist/subtree.d.ts.map +1 -1
  22. package/dist/subtree.js +4 -4
  23. package/dist/subtree.js.map +1 -1
  24. package/dist/treeAgentTypes.d.ts +345 -0
  25. package/dist/treeAgentTypes.d.ts.map +1 -0
  26. package/dist/treeAgentTypes.js +190 -0
  27. package/dist/treeAgentTypes.js.map +1 -0
  28. package/dist/utils.d.ts +0 -4
  29. package/dist/utils.d.ts.map +1 -1
  30. package/dist/utils.js +2 -9
  31. package/dist/utils.js.map +1 -1
  32. package/eslint.config.mts +4 -4
  33. package/lib/alpha.d.ts +26 -1
  34. package/lib/index.d.ts +3 -1
  35. package/lib/index.d.ts.map +1 -1
  36. package/lib/index.js +1 -0
  37. package/lib/index.js.map +1 -1
  38. package/lib/methodBinding.d.ts +54 -10
  39. package/lib/methodBinding.d.ts.map +1 -1
  40. package/lib/methodBinding.js.map +1 -1
  41. package/lib/propertyBinding.d.ts +52 -2
  42. package/lib/propertyBinding.d.ts.map +1 -1
  43. package/lib/propertyBinding.js +28 -3
  44. package/lib/propertyBinding.js.map +1 -1
  45. package/lib/renderSchemaTypeScript.d.ts.map +1 -1
  46. package/lib/renderSchemaTypeScript.js +16 -7
  47. package/lib/renderSchemaTypeScript.js.map +1 -1
  48. package/lib/renderTypeFactoryTypeScript.d.ts +13 -0
  49. package/lib/renderTypeFactoryTypeScript.d.ts.map +1 -0
  50. package/lib/renderTypeFactoryTypeScript.js +217 -0
  51. package/lib/renderTypeFactoryTypeScript.js.map +1 -0
  52. package/lib/subtree.d.ts.map +1 -1
  53. package/lib/subtree.js +4 -4
  54. package/lib/subtree.js.map +1 -1
  55. package/lib/treeAgentTypes.d.ts +345 -0
  56. package/lib/treeAgentTypes.d.ts.map +1 -0
  57. package/lib/treeAgentTypes.js +186 -0
  58. package/lib/treeAgentTypes.js.map +1 -0
  59. package/lib/utils.d.ts +0 -4
  60. package/lib/utils.d.ts.map +1 -1
  61. package/lib/utils.js +2 -8
  62. package/lib/utils.js.map +1 -1
  63. package/package.json +12 -12
  64. package/src/index.ts +31 -0
  65. package/src/methodBinding.ts +93 -15
  66. package/src/propertyBinding.ts +66 -9
  67. package/src/renderSchemaTypeScript.ts +24 -8
  68. package/src/renderTypeFactoryTypeScript.ts +259 -0
  69. package/src/subtree.ts +5 -4
  70. package/src/treeAgentTypes.ts +490 -0
  71. package/src/utils.ts +2 -9
  72. 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,CAAC;AAC9D,OAAO,EAAE,UA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GG;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,CAAC,GAAG,CAAC,GAAG,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,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.80.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.80.0",
74
- "@fluidframework/runtime-utils": "~2.80.0",
75
- "@fluidframework/telemetry-utils": "~2.80.0",
76
- "@fluidframework/tree": "~2.80.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
81
  "@arethetypeswrong/cli": "^0.18.2",
82
82
  "@biomejs/biome": "~1.9.3",
83
- "@fluid-internal/mocha-test-setup": "~2.80.0",
84
- "@fluid-tools/build-cli": "^0.62.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.62.0",
87
- "@fluidframework/eslint-config-fluid": "~2.80.0",
88
- "@fluidframework/id-compressor": "~2.80.0",
89
- "@fluidframework/runtime-utils": "~2.80.0",
90
- "@fluidframework/test-runtime-utils": "~2.80.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",
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";
@@ -9,6 +9,7 @@ import { NodeKind } from "@fluidframework/tree";
9
9
  import type { z } from "zod";
10
10
 
11
11
  import { instanceOf } from "./renderZodTypeScript.js";
12
+ import type { TypeFactoryType } from "./treeAgentTypes.js";
12
13
 
13
14
  /**
14
15
  * A utility type that extracts the method keys from a given type.
@@ -62,7 +63,8 @@ export function getExposedMethods(schemaClass: BindableSchema): {
62
63
  * A type that represents a function argument.
63
64
  * @alpha
64
65
  */
65
- 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];
66
68
 
67
69
  /**
68
70
  * A function definition interface that describes the structure of a function.
@@ -70,12 +72,24 @@ export type Arg<T extends z.ZodTypeAny = z.ZodTypeAny> = readonly [name: string,
70
72
  */
71
73
  export interface FunctionDef<
72
74
  Args extends readonly Arg[],
73
- Return extends z.ZodTypeAny,
74
- Rest extends z.ZodTypeAny | null = null,
75
+ Return extends z.ZodTypeAny | TypeFactoryType,
76
+ Rest extends z.ZodTypeAny | TypeFactoryType | null = null,
75
77
  > {
78
+ /**
79
+ * Optional description of the function.
80
+ */
76
81
  description?: string;
82
+ /**
83
+ * The function's parameters.
84
+ */
77
85
  args: Args;
86
+ /**
87
+ * Optional rest parameter type.
88
+ */
78
89
  rest?: Rest;
90
+ /**
91
+ * The function's return type.
92
+ */
79
93
  returns: Return;
80
94
  }
81
95
 
@@ -83,15 +97,20 @@ export interface FunctionDef<
83
97
  * A class that implements the FunctionDef interface.
84
98
  */
85
99
  export class FunctionWrapper
86
- 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
+ >
87
106
  {
88
107
  public constructor(
89
108
  public readonly name: string,
90
109
  public readonly description: string | undefined,
91
110
  public readonly args: readonly Arg[],
92
111
  // eslint-disable-next-line @rushstack/no-new-null
93
- public readonly rest: z.ZodTypeAny | null,
94
- public readonly returns: z.ZodTypeAny,
112
+ public readonly rest: z.ZodTypeAny | TypeFactoryType | null,
113
+ public readonly returns: z.ZodTypeAny | TypeFactoryType,
95
114
  ) {}
96
115
  }
97
116
 
@@ -110,9 +129,9 @@ export type ArgsTuple<T extends readonly Arg[]> = T extends readonly [infer Sing
110
129
  * @alpha
111
130
  */
112
131
  export function buildFunc<
113
- const Return extends z.ZodTypeAny,
132
+ const Return extends z.ZodTypeAny | TypeFactoryType,
114
133
  const Args extends readonly Arg[],
115
- const Rest extends z.ZodTypeAny | null = null,
134
+ const Rest extends z.ZodTypeAny | TypeFactoryType | null = null,
116
135
  >(
117
136
  def: { description?: string; returns: Return; rest?: Rest },
118
137
  ...args: Args
@@ -125,12 +144,48 @@ export function buildFunc<
125
144
  };
126
145
  }
127
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
+
128
178
  /**
129
179
  * A utility type that infers the return type of a function definition.
130
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.
131
184
  */
132
- export type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Rest>
133
- ? 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>
134
189
  : never;
135
190
 
136
191
  /**
@@ -138,16 +193,32 @@ export type Infer<T> = T extends FunctionDef<infer Args, infer Return, infer Res
138
193
  * @alpha
139
194
  */
140
195
  export interface ExposedMethods {
196
+ /**
197
+ * Expose a method with Zod types (strict compile-time type checking).
198
+ */
141
199
  expose<
142
200
  const K extends string & keyof MethodKeys<InstanceType<S>>,
143
- S extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,
144
- 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>,
145
203
  >(schema: S, methodName: K, zodFunction: Z): void;
146
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
+
147
218
  /**
148
219
  * Create a Zod schema for a SharedTree schema class.
149
220
  * @remarks
150
- * 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}.
151
222
  */
152
223
  instanceOf<T extends TreeNodeSchemaClass>(
153
224
  schema: T,
@@ -175,6 +246,9 @@ export const exposeMethodsSymbol: unique symbol = Symbol("run");
175
246
  * @alpha
176
247
  */
177
248
  export interface IExposedMethods {
249
+ /**
250
+ * Static method that exposes methods of this schema class to an agent.
251
+ */
178
252
  [exposeMethodsSymbol](methods: ExposedMethods): void;
179
253
  }
180
254
 
@@ -186,8 +260,12 @@ class ExposedMethodsI implements ExposedMethods {
186
260
 
187
261
  public expose<
188
262
  const K extends string & keyof MethodKeys<InstanceType<S>>,
189
- S extends BindableSchema & Ctor<Record<K, Infer<Z>>> & IExposedMethods,
190
- 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
+ >,
191
269
  >(schema: S, methodName: K, functionDef: Z): void {
192
270
  if (schema !== this.schemaClass) {
193
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,7 +18,13 @@ 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 {
22
+ instanceOfsTypeFactory,
23
+ renderTypeFactoryTypeScript,
24
+ } from "./renderTypeFactoryTypeScript.js";
21
25
  import { instanceOfs, renderZodTypeScript } from "./renderZodTypeScript.js";
26
+ import type { TypeFactoryOptional, TypeFactoryType } from "./treeAgentTypes.js";
27
+ import { isTypeFactoryType } from "./treeAgentTypes.js";
22
28
  import { getFriendlyName, isNamedSchema, llmDefault, unqualifySchema } from "./utils.js";
23
29
 
24
30
  interface BoundMembers {
@@ -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
  }