@fluidframework/tree-agent 2.74.0-365691 → 2.74.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/methodBinding.d.ts +6 -1
  3. package/dist/methodBinding.d.ts.map +1 -1
  4. package/dist/methodBinding.js +14 -3
  5. package/dist/methodBinding.js.map +1 -1
  6. package/dist/prompt.d.ts.map +1 -1
  7. package/dist/prompt.js +2 -12
  8. package/dist/prompt.js.map +1 -1
  9. package/dist/propertyBinding.js +2 -2
  10. package/dist/propertyBinding.js.map +1 -1
  11. package/dist/renderSchemaTypeScript.d.ts +18 -0
  12. package/dist/renderSchemaTypeScript.d.ts.map +1 -0
  13. package/dist/renderSchemaTypeScript.js +399 -0
  14. package/dist/renderSchemaTypeScript.js.map +1 -0
  15. package/dist/renderZodTypeScript.d.ts +21 -0
  16. package/dist/renderZodTypeScript.d.ts.map +1 -0
  17. package/dist/renderZodTypeScript.js +272 -0
  18. package/dist/renderZodTypeScript.js.map +1 -0
  19. package/dist/typeGeneration.d.ts +3 -5
  20. package/dist/typeGeneration.d.ts.map +1 -1
  21. package/dist/typeGeneration.js +21 -254
  22. package/dist/typeGeneration.js.map +1 -1
  23. package/dist/utils.d.ts +10 -27
  24. package/dist/utils.d.ts.map +1 -1
  25. package/dist/utils.js +18 -362
  26. package/dist/utils.js.map +1 -1
  27. package/eslint.config.mts +51 -0
  28. package/lib/methodBinding.d.ts +6 -1
  29. package/lib/methodBinding.d.ts.map +1 -1
  30. package/lib/methodBinding.js +11 -1
  31. package/lib/methodBinding.js.map +1 -1
  32. package/lib/prompt.d.ts.map +1 -1
  33. package/lib/prompt.js +3 -13
  34. package/lib/prompt.js.map +1 -1
  35. package/lib/propertyBinding.js +1 -1
  36. package/lib/propertyBinding.js.map +1 -1
  37. package/lib/renderSchemaTypeScript.d.ts +18 -0
  38. package/lib/renderSchemaTypeScript.d.ts.map +1 -0
  39. package/lib/renderSchemaTypeScript.js +395 -0
  40. package/lib/renderSchemaTypeScript.js.map +1 -0
  41. package/lib/renderZodTypeScript.d.ts +21 -0
  42. package/lib/renderZodTypeScript.d.ts.map +1 -0
  43. package/lib/renderZodTypeScript.js +267 -0
  44. package/lib/renderZodTypeScript.js.map +1 -0
  45. package/lib/typeGeneration.d.ts +3 -5
  46. package/lib/typeGeneration.d.ts.map +1 -1
  47. package/lib/typeGeneration.js +24 -257
  48. package/lib/typeGeneration.js.map +1 -1
  49. package/lib/utils.d.ts +10 -27
  50. package/lib/utils.d.ts.map +1 -1
  51. package/lib/utils.js +18 -360
  52. package/lib/utils.js.map +1 -1
  53. package/package.json +13 -12
  54. package/src/methodBinding.ts +15 -3
  55. package/src/prompt.ts +6 -22
  56. package/src/propertyBinding.ts +1 -1
  57. package/src/renderSchemaTypeScript.ts +523 -0
  58. package/src/renderZodTypeScript.ts +314 -0
  59. package/src/typeGeneration.ts +32 -414
  60. package/src/utils.ts +18 -412
@@ -1 +1 @@
1
- {"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA8E;AAC9E,uEAAsE;AACtE,4DAUuC;AAQvC,6BAAyC;AAGzC,yDAAwE;AACxE,6DAA4D;AAC5D,yCAQoB;AAEpB;;;;;;;;;GASG;AAEH;;GAEG;AACH,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAGlC,CAAC;AAEJ;;GAEG;AACH,SAAgB,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAIxB,OAAO,IAAA,sBAAW,EAAC,iBAAiB,EAAE,MAAM,EAAE,GAAG,EAAE;QAClD,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;QAC7C,MAAM,iBAAiB,GAAG,IAAI,GAAG,EAAkB,CAAC;QACpD,IAAA,0BAAe,EAAC,UAAU,EAAE;YAC3B,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE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\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert, unreachableCase } from \"@fluidframework/core-utils/internal\";\nimport { UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tArrayNodeSchema,\n\tFieldKind,\n\tgetSimpleSchema,\n\tMapNodeSchema,\n\tNodeKind,\n\tObjectNodeSchema,\n\tRecordNodeSchema,\n\tValueSchema,\n\twalkFieldSchema,\n} from \"@fluidframework/tree/internal\";\nimport type {\n\tImplicitFieldSchema,\n\tSimpleFieldSchema,\n\tSimpleNodeSchema,\n\tSimpleTreeSchema,\n\tTreeNodeSchema,\n} from \"@fluidframework/tree/internal\";\nimport { z, type ZodTypeAny } from \"zod\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { FunctionWrapper, getExposedMethods } from \"./methodBinding.js\";\nimport { getExposedProperties } from \"./propertyBinding.js\";\nimport {\n\tfail,\n\tgetOrCreate,\n\thasAtLeastTwo,\n\tllmDefault,\n\tmapIterable,\n\ttryGetSingleton,\n\ttype MapGetSet,\n} from \"./utils.js\";\n\n/**\n *\n * TODO: Add a prompt suggestion API!\n *\n * TODO: Handle rate limit errors.\n *\n * TODO: Pass descriptions from schema metadata to the generated TS types that we put in the prompt\n *\n * TODO make the Ids be \"Vector-2\" instead of \"Vector2\" (or else it gets weird when you have a type called \"Vector2\")\n */\n\n/**\n * Cache used to prevent repeatedly generating the same Zod validation objects for the same {@link SimpleTreeSchema} as generate propts for repeated calls to an LLM\n */\nconst promptSchemaCache = new WeakMap<\n\tSimpleTreeSchema,\n\tReturnType<typeof generateEditTypes>\n>();\n\n/**\n * TODO\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\treturn getOrCreate(promptSchemaCache, schema, () => {\n\t\tconst allSchemas = new Set<TreeNodeSchema>();\n\t\tconst objectTypeSchemas = new Set<TreeNodeSchema>();\n\t\twalkFieldSchema(rootSchema, {\n\t\t\tnode: (n) => {\n\t\t\t\tallSchemas.add(n);\n\t\t\t\tif (\n\t\t\t\t\tn instanceof ObjectNodeSchema ||\n\t\t\t\t\tn instanceof MapNodeSchema ||\n\t\t\t\t\tn instanceof ArrayNodeSchema ||\n\t\t\t\t\tn instanceof RecordNodeSchema\n\t\t\t\t) {\n\t\t\t\t\tobjectTypeSchemas.add(n);\n\t\t\t\t\tconst exposedMethods = getExposedMethods(n);\n\t\t\t\t\tfor (const t of exposedMethods.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t\tconst exposedProperties = getExposedProperties(n);\n\t\t\t\t\tfor (const t of exposedProperties.referencedTypes) {\n\t\t\t\t\t\tallSchemas.add(t);\n\t\t\t\t\t\tobjectTypeSchemas.add(t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t},\n\t\t});\n\t\tconst nodeSchemas = [...objectTypeSchemas.values()] as BindableSchema[];\n\t\tconst bindableSchemas = new Map<string, BindableSchema>(\n\t\t\tnodeSchemas.map((nodeSchema) => [nodeSchema.identifier, nodeSchema]),\n\t\t);\n\t\treturn generateEditTypes(\n\t\t\t[...allSchemas.values()].map((s) => getSimpleSchema(s)),\n\t\t\tnew Map(),\n\t\t\tbindableSchemas,\n\t\t);\n\t});\n}\n\n/**\n * Generates a set of ZOD validation objects for the various types of data that can be put into the provided {@link SimpleTreeSchema}\n * and then uses those sets to generate an all-encompassing ZOD object for each type of {@link TreeEdit} that can validate any of the types of data that can be put into the tree.\n *\n * @returns a Record of schema names to Zod validation objects, and the name of the root schema used to encompass all of the other schemas.\n *\n * @remarks The return type of this function is designed to work with Typechat's createZodJsonValidator as well as be used as the JSON schema for OpenAi's structured output response format.\n */\nfunction generateEditTypes(\n\tschemas: Iterable<SimpleTreeSchema>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): {\n\tdomainTypes: Record<string, ZodTypeAny>;\n} {\n\tconst domainTypes: Record<string, ZodTypeAny> = {};\n\tfor (const schema of schemas) {\n\t\tfor (const name of schema.definitions.keys()) {\n\t\t\t// If this does overwrite anything in domainTypes, it is guaranteed to be overwritten with an identical value due to the getOrCreate\n\t\t\tdomainTypes[name] = getOrCreateType(\n\t\t\t\tschema.definitions,\n\t\t\t\tname,\n\t\t\t\tobjectCache,\n\t\t\t\tbindableSchemas,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn {\n\t\tdomainTypes,\n\t};\n}\n\nfunction getBoundMembersForBindable(bindableSchema: BindableSchema): {\n\treferencedTypes: Set<TreeNodeSchema>;\n\tmembers: [string, ZodTypeAny][];\n} {\n\tconst memberTypes: [string, ZodTypeAny][] = [];\n\n\tconst methods = getExposedMethods(bindableSchema);\n\tfor (const [name, method] of Object.entries(methods.methods)) {\n\t\tconst zodFunction = z.instanceof(FunctionWrapper);\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(zodFunction as any).method = method;\n\t\tmemberTypes.push([name, zodFunction]);\n\t}\n\tconst props = getExposedProperties(bindableSchema);\n\tfor (const [name, prop] of Object.entries(props.properties)) {\n\t\tconst schema = prop.schema;\n\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any\n\t\t(schema as any).property = prop;\n\t\tmemberTypes.push([name, prop.schema]);\n\t}\n\n\treturn {\n\t\tmembers: memberTypes,\n\t\treferencedTypes: new Set([...props.referencedTypes, ...methods.referencedTypes]),\n\t};\n}\n\nfunction getBoundMembers(\n\tdefinition: string,\n\tbindableSchemas: Map<string, BindableSchema>,\n): [string, ZodTypeAny][] {\n\tconst bindableSchema = bindableSchemas.get(definition) ?? fail(\"unknown definition\");\n\treturn getBoundMembersForBindable(bindableSchema).members;\n}\n\nfunction addBindingIntersectionIfNeeded(\n\ttypeString: \"array\" | \"map\" | \"record\",\n\tzodTypeBound: ZodTypeAny,\n\tdefinition: string,\n\tsimpleNodeSchema: SimpleNodeSchema,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tlet zodType = zodTypeBound;\n\tlet description = simpleNodeSchema.metadata?.description ?? \"\";\n\n\tconst boundMembers = getBoundMembers(definition, bindableSchemas);\n\tconst methods: Record<string, z.ZodTypeAny> = {};\n\tconst properties: Record<string, z.ZodTypeAny> = {};\n\n\tlet hasProperties: boolean = false;\n\tlet hasMethods: boolean = false;\n\n\tif (boundMembers.length > 0) {\n\t\tfor (const [name, zodMember] of boundMembers) {\n\t\t\tconst kind =\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(zodMember as any).method === undefined\n\t\t\t\t\t? // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(zodMember as any).property === undefined\n\t\t\t\t\t\t? \"Unknown\"\n\t\t\t\t\t\t: \"Property\"\n\t\t\t\t\t: \"Method\";\n\n\t\t\tif (kind === \"Unknown\") {\n\t\t\t\tthrow new UsageError(`Unrecognized bound member ${name} in schema ${definition}`);\n\t\t\t}\n\t\t\tif (kind === \"Method\") {\n\t\t\t\tif (methods[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tmethods[name] = zodMember;\n\t\t\t}\n\t\t\tif (kind === \"Property\") {\n\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tproperties[name] = zodMember;\n\t\t\t}\n\t\t}\n\t\thasMethods = Object.keys(methods).length > 0 ? true : false;\n\t\thasProperties = Object.keys(properties).length > 0 ? true : false;\n\n\t\tif (hasMethods) {\n\t\t\tzodType = z.intersection(zodType, z.object(methods));\n\t\t}\n\t\tif (hasProperties) {\n\t\t\tzodType = z.intersection(zodType, z.object(properties));\n\t\t}\n\t}\n\n\tlet note = \"\";\n\tif (hasMethods && hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods and properties directly on it.`;\n\t} else if (hasMethods) {\n\t\tnote = `Note: this ${typeString} has custom user-defined methods directly on it.`;\n\t} else if (hasProperties) {\n\t\tnote = `Note: this ${typeString} has custom user-defined properties directly on it.`;\n\t}\n\tif (note !== \"\") {\n\t\tdescription = description === \"\" ? note : `${description} - ${note}`;\n\t}\n\n\treturn zodType.describe(description);\n}\n\nfunction getOrCreateType(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tdefinition: string,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst simpleNodeSchema = definitionMap.get(definition) ?? fail(\"Unexpected definition\");\n\treturn getOrCreate(objectCache, simpleNodeSchema, () => {\n\t\t// Handle recursive types: temporarily create a zod \"lazy\" type that can be referenced by a recursive call to getOrCreateType.\n\t\tlet type: ZodTypeAny | undefined;\n\t\tobjectCache.set(\n\t\t\tsimpleNodeSchema,\n\t\t\tz.lazy(() => type ?? fail(\"Recursive type used before creation\")),\n\t\t);\n\t\tswitch (simpleNodeSchema.kind) {\n\t\t\tcase NodeKind.Object: {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\t\t\t\tconst properties: Record<string, z.ZodTypeAny> = Object.fromEntries(\n\t\t\t\t\t[...simpleNodeSchema.fields]\n\t\t\t\t\t\t.map(([key, field]) => {\n\t\t\t\t\t\t\treturn [\n\t\t\t\t\t\t\t\tkey,\n\t\t\t\t\t\t\t\tgetOrCreateTypeForField(definitionMap, field, objectCache, bindableSchemas),\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.filter(([, value]) => value !== undefined),\n\t\t\t\t);\n\n\t\t\t\tfor (const [name, zodMember] of getBoundMembers(definition, bindableSchemas)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst kind = (zodMember as any).method === undefined ? \"Property\" : \"Method\";\n\t\t\t\t\tif (properties[name] !== undefined) {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${kind} ${name} conflicts with field of the same name in schema ${definition}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tproperties[name] = zodMember;\n\t\t\t\t}\n\n\t\t\t\treturn (type = z\n\t\t\t\t\t.object(properties)\n\t\t\t\t\t.describe(simpleNodeSchema.metadata?.description ?? \"\"));\n\t\t\t}\n\t\t\tcase NodeKind.Map: {\n\t\t\t\tconst zodType = z.map(\n\t\t\t\t\tz.string(),\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"map\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Record: {\n\t\t\t\tconst zodType = z.record(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"record\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Array: {\n\t\t\t\tconst zodType = z.array(\n\t\t\t\t\tgetTypeForAllowedTypes(\n\t\t\t\t\t\tdefinitionMap,\n\t\t\t\t\t\tnew Set(simpleNodeSchema.simpleAllowedTypes.keys()),\n\t\t\t\t\t\tobjectCache,\n\t\t\t\t\t\tbindableSchemas,\n\t\t\t\t\t),\n\t\t\t\t);\n\t\t\t\treturn (type = addBindingIntersectionIfNeeded(\n\t\t\t\t\t\"array\",\n\t\t\t\t\tzodType,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tsimpleNodeSchema,\n\t\t\t\t\tbindableSchemas,\n\t\t\t\t));\n\t\t\t}\n\t\t\tcase NodeKind.Leaf: {\n\t\t\t\tswitch (simpleNodeSchema.leafKind) {\n\t\t\t\t\tcase ValueSchema.Boolean: {\n\t\t\t\t\t\treturn (type = z.boolean());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Number: {\n\t\t\t\t\t\treturn (type = z.number());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.String: {\n\t\t\t\t\t\treturn (type = z.string());\n\t\t\t\t\t}\n\t\t\t\t\tcase ValueSchema.Null: {\n\t\t\t\t\t\treturn (type = z.null());\n\t\t\t\t\t}\n\t\t\t\t\tdefault: {\n\t\t\t\t\t\tthrow new Error(`Unsupported leaf kind ${NodeKind[simpleNodeSchema.leafKind]}.`);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\tdefault: {\n\t\t\t\treturn unreachableCase(simpleNodeSchema, \"Unknown node kind\");\n\t\t\t}\n\t\t}\n\t});\n}\n\nfunction getOrCreateTypeForField(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tfieldSchema: SimpleFieldSchema,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst customMetadata = fieldSchema.metadata.custom as\n\t\t| Record<string | symbol, unknown>\n\t\t| undefined;\n\tconst getDefault = customMetadata?.[llmDefault];\n\tif (getDefault !== undefined) {\n\t\tif (typeof getDefault !== \"function\") {\n\t\t\tthrow new UsageError(\n\t\t\t\t`Expected value of ${llmDefault.description} property to be a function, but got ${typeof getDefault}`,\n\t\t\t);\n\t\t}\n\n\t\tif (fieldSchema.kind !== FieldKind.Optional) {\n\t\t\tthrow new UsageError(\n\t\t\t\t`The ${llmDefault.description} property is only permitted on optional fields.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tconst field = getTypeForAllowedTypes(\n\t\tdefinitionMap,\n\t\tnew Set(fieldSchema.simpleAllowedTypes.keys()),\n\t\tobjectCache,\n\t\tbindableSchemas,\n\t).describe(\n\t\tgetDefault === undefined\n\t\t\t? (fieldSchema.metadata?.description ?? \"\")\n\t\t\t: \"Do not populate this field. It will be automatically supplied by the system after insertion.\",\n\t);\n\n\tswitch (fieldSchema.kind) {\n\t\tcase FieldKind.Required: {\n\t\t\treturn field;\n\t\t}\n\t\tcase FieldKind.Optional: {\n\t\t\treturn field.optional();\n\t\t}\n\t\tcase FieldKind.Identifier: {\n\t\t\treturn field\n\t\t\t\t.optional()\n\t\t\t\t.describe(\n\t\t\t\t\t\"This is an ID automatically generated by the system. Do not supply it when constructing a new object.\",\n\t\t\t\t);\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new Error(`Unsupported field kind ${NodeKind[fieldSchema.kind]}.`);\n\t\t}\n\t}\n}\n\nfunction getTypeForAllowedTypes(\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tallowedTypes: ReadonlySet<string>,\n\tobjectCache: MapGetSet<SimpleNodeSchema, ZodTypeAny>,\n\tbindableSchemas: Map<string, BindableSchema>,\n): ZodTypeAny {\n\tconst single = tryGetSingleton(allowedTypes);\n\tif (single === undefined) {\n\t\tconst types = [\n\t\t\t...mapIterable(allowedTypes, (name) => {\n\t\t\t\treturn getOrCreateType(definitionMap, name, objectCache, bindableSchemas);\n\t\t\t}),\n\t\t];\n\t\tassert(hasAtLeastTwo(types), 0xa7e /* Expected at least two types */);\n\t\treturn z.union(types);\n\t} else {\n\t\treturn getOrCreateType(definitionMap, single, objectCache, bindableSchemas);\n\t}\n}\n"]}
1
+ {"version":3,"file":"typeGeneration.js","sourceRoot":"","sources":["../src/typeGeneration.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,4DAAiF;AAIjF,yDAAyE;AACzE,6DAA4D;AAC5D,2EAGqC;AACrC,yCAAyC;AAEzC,MAAM,iBAAiB,GAAG,IAAI,OAAO,EAAkD,CAAC;AAExF;;GAEG;AACH,SAAgB,0BAA0B,CACzC,UAA+B,EAC/B,MAAwB;IAExB,OAAO,IAAA,sBAAW,EAAC,iBAAiB,EAAE,MAAM,EAAE,GAAG,EAAE,CAClD,4BAA4B,CAAC,UAAU,CAAC,CACxC,CAAC;AACH,CAAC;AAPD,gEAOC;AAED,SAAS,4BAA4B,CACpC,UAA+B;IAE/B,MAAM,eAAe,GAAG,IAAI,GAAG,EAA0B,CAAC;IAE1D,IAAA,0BAAe,EAAC,UAAU,EAAE;QAC3B,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE;YACd,IAAI,IAAA,mCAAgB,EAAC,IAAI,CAAC,EAAE,CAAC;gBAC5B,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;gBAE3C,MAAM,cAAc,GAAG,IAAA,oCAAiB,EAAC,IAAI,CAAC,CAAC;gBAC/C,KAAK,MAAM,UAAU,IAAI,cAAc,CAAC,eAAe,EAAE,CAAC;oBACzD,IAAI,IAAA,mCAAgB,EAAC,UAAU,CAAC,EAAE,CAAC;wBAClC,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;oBACxD,CAAC;gBACF,CAAC;gBAED,MAAM,iBAAiB,GAAG,IAAA,yCAAoB,EAAC,IAAI,CAAC,CAAC;gBACrD,KAAK,MAAM,UAAU,IAAI,iBAAiB,CAAC,eAAe,EAAE,CAAC;oBAC5D,IAAI,IAAA,mCAAgB,EAAC,UAAU,CAAC,EAAE,CAAC;wBAClC,eAAe,CAAC,GAAG,CAAC,UAAU,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;oBACxD,CAAC;gBACF,CAAC;YACF,CAAC;QACF,CAAC;KACD,CAAC,CAAC;IAEH,MAAM,gBAAgB,GAAG,IAAA,0BAAe,EAAC,UAAU,CAAC,CAAC;IACrD,OAAO,IAAA,kDAAsB,EAAC,gBAAgB,CAAC,WAAW,EAAE,eAAe,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { getSimpleSchema, walkFieldSchema } from \"@fluidframework/tree/internal\";\nimport type { ImplicitFieldSchema, SimpleTreeSchema } from \"@fluidframework/tree/internal\";\n\nimport type { BindableSchema } from \"./methodBinding.js\";\nimport { getExposedMethods, isBindableSchema } from \"./methodBinding.js\";\nimport { getExposedProperties } from \"./propertyBinding.js\";\nimport {\n\trenderSchemaTypeScript,\n\ttype SchemaTypeScriptRenderResult,\n} from \"./renderSchemaTypeScript.js\";\nimport { getOrCreate } from \"./utils.js\";\n\nconst promptSchemaCache = new WeakMap<SimpleTreeSchema, SchemaTypeScriptRenderResult>();\n\n/**\n * Generates TypeScript declarations for the schemas reachable from the provided root field schema.\n */\nexport function generateEditTypesForPrompt(\n\trootSchema: ImplicitFieldSchema,\n\tschema: SimpleTreeSchema,\n): SchemaTypeScriptRenderResult {\n\treturn getOrCreate(promptSchemaCache, schema, () =>\n\t\tbuildPromptSchemaDescription(rootSchema),\n\t);\n}\n\nfunction buildPromptSchemaDescription(\n\trootSchema: ImplicitFieldSchema,\n): SchemaTypeScriptRenderResult {\n\tconst bindableSchemas = new Map<string, BindableSchema>();\n\n\twalkFieldSchema(rootSchema, {\n\t\tnode: (node) => {\n\t\t\tif (isBindableSchema(node)) {\n\t\t\t\tbindableSchemas.set(node.identifier, node);\n\n\t\t\t\tconst exposedMethods = getExposedMethods(node);\n\t\t\t\tfor (const referenced of exposedMethods.referencedTypes) {\n\t\t\t\t\tif (isBindableSchema(referenced)) {\n\t\t\t\t\t\tbindableSchemas.set(referenced.identifier, referenced);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst exposedProperties = getExposedProperties(node);\n\t\t\t\tfor (const referenced of exposedProperties.referencedTypes) {\n\t\t\t\t\tif (isBindableSchema(referenced)) {\n\t\t\t\t\t\tbindableSchemas.set(referenced.identifier, referenced);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t},\n\t});\n\n\tconst simpleTreeSchema = getSimpleSchema(rootSchema);\n\treturn renderSchemaTypeScript(simpleTreeSchema.definitions, bindableSchemas);\n}\n"]}
package/dist/utils.d.ts CHANGED
@@ -2,9 +2,8 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
- import type { ImplicitFieldSchema, TreeNodeSchemaClass } from "@fluidframework/tree";
5
+ import type { ImplicitFieldSchema } from "@fluidframework/tree";
6
6
  import type { InsertableContent, TreeNode, TreeNodeSchema } from "@fluidframework/tree/alpha";
7
- import { z } from "zod";
8
7
  /**
9
8
  * Subset of Map interface.
10
9
  *
@@ -80,33 +79,17 @@ export declare function getFriendlyName(schema: TreeNodeSchema): string;
80
79
  */
81
80
  export declare function isNamedSchema(schemaIdentifier: string): boolean;
82
81
  /**
83
- * Returns the unqualified name of a schema (e.g. `"my.scope.MyNode"` returns `"MyNode"`).
84
- * @remarks This works by removing all characters before the last dot in the schema name.
85
- * If there is a dot in a user's schema name, this might produce unexpected results.
82
+ * Returns the unqualified, sanitized Typescript-safe name of a schema
83
+ * Examples:
84
+ * - `"my.scope.MyNode"` returns `"MyNode"`
85
+ * - `"my.scope.MyNode-2"` returns `"MyNode_2"`
86
+ * - `"my.scope.MyNode!"` returns `"MyNode_"`
87
+ * @remarks
88
+ * - Removes all characters before the last dot in the schema name.
89
+ * - Sanitizes the remainder into a valid Typescript identifier
90
+ * - If there is a dot in a user's schema name, this might produce unexpected results.
86
91
  */
87
92
  export declare function unqualifySchema(schemaIdentifier: string): string;
88
- /**
89
- * Details about the properties of a TypeScript schema represented as Zod.
90
- */
91
- export interface SchemaDetails {
92
- hasHelperMethods: boolean;
93
- }
94
- /**
95
- * Returns the TypeScript source code corresponding to a Zod schema. The schema is supplied as an object where each
96
- * property provides a name for an associated Zod type. The return value is a string containing the TypeScript source
97
- * code corresponding to the schema. Each property of the schema object is emitted as a named `interface` or `type`
98
- * declaration for the associated type and is referenced by that name in the emitted type declarations. Other types
99
- * referenced in the schema are emitted in their structural form.
100
- * @param schema - A schema object where each property provides a name for an associated Zod type.
101
- * @param details - Optional details about the schema. The fields will be set according to the details in the given schema.
102
- * @returns The TypeScript source code corresponding to the schema.
103
- */
104
- export declare function getZodSchemaAsTypeScript(schema: Record<string, z.ZodType>, details?: SchemaDetails): string;
105
- /**
106
- * Create a Zod schema for a SharedTree schema class.
107
- * @alpha
108
- */
109
- export declare function instanceOf<T extends TreeNodeSchemaClass>(schema: T): z.ZodType<InstanceType<T>, z.ZodTypeDef, InstanceType<T>>;
110
93
  /**
111
94
  * Adds all (optionally filtered) schemas reachable from the given schema to the given set.
112
95
  * @returns The set of schemas added (same as the `schemas` parameter, if supplied).
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO,KAAK,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACrF,OAAO,KAAK,EACX,i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,EAAE,cAAc,EAAE,KAAK,EAAE,iBAAiB,GAAG,QAAQ,CAOxF;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,cAAc,GAAG,MAAM,CAoB9D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAU/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,gBAAgB,EAAE,MAAM,GAAG,MAAM,CAehE;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAC1B,MAAM,EAAE,mBAAmB,EAC3B,MAAM,GAAE,CAAC,MAAM,EAAE,cAAc,KAAK,OAAoB,EACxD,OAAO,sBAA4B,GACjC,GAAG,CAAC,cAAc,CAAC,CAUrB;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CASpD"}
package/dist/utils.js CHANGED
@@ -4,17 +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.instanceOf = exports.getZodSchemaAsTypeScript = exports.unqualifySchema = exports.isNamedSchema = exports.getFriendlyName = exports.constructNode = exports.failUsage = exports.llmDefault = exports.hasAtLeastTwo = exports.tryGetSingleton = exports.getOrCreate = exports.mapIterable = exports.fail = void 0;
8
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
9
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
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;
10
8
  const internal_1 = require("@fluidframework/core-utils/internal");
11
9
  const runtime_utils_1 = require("@fluidframework/runtime-utils");
12
10
  const internal_2 = require("@fluidframework/telemetry-utils/internal");
13
11
  const alpha_1 = require("@fluidframework/tree/alpha");
14
12
  const internal_3 = require("@fluidframework/tree/internal");
15
- const zod_1 = require("zod");
16
- const methodBinding_js_1 = require("./methodBinding.js");
17
- const propertyBinding_js_1 = require("./propertyBinding.js");
18
13
  /**
19
14
  * TBD
20
15
  */
@@ -143,369 +138,30 @@ function isNamedSchema(schemaIdentifier) {
143
138
  }
144
139
  exports.isNamedSchema = isNamedSchema;
145
140
  /**
146
- * Returns the unqualified name of a schema (e.g. `"my.scope.MyNode"` returns `"MyNode"`).
147
- * @remarks This works by removing all characters before the last dot in the schema name.
148
- * If there is a dot in a user's schema name, this might produce unexpected results.
141
+ * Returns the unqualified, sanitized Typescript-safe name of a schema
142
+ * Examples:
143
+ * - `"my.scope.MyNode"` returns `"MyNode"`
144
+ * - `"my.scope.MyNode-2"` returns `"MyNode_2"`
145
+ * - `"my.scope.MyNode!"` returns `"MyNode_"`
146
+ * @remarks
147
+ * - Removes all characters before the last dot in the schema name.
148
+ * - Sanitizes the remainder into a valid Typescript identifier
149
+ * - If there is a dot in a user's schema name, this might produce unexpected results.
149
150
  */
150
151
  function unqualifySchema(schemaIdentifier) {
151
152
  // Get the unqualified name by removing the scope (everything before the last dot).
152
153
  const matches = /[^.]+$/.exec(schemaIdentifier);
153
- if (matches === null) {
154
- return schemaIdentifier; // Return the original name if it is unscoped.
154
+ const unqualifiedName = matches === null ? schemaIdentifier : matches[0];
155
+ let sanitizedName = unqualifiedName;
156
+ // Replace invalid characters with "_".
157
+ sanitizedName = sanitizedName.replace(/[^\w$]/g, "_");
158
+ // If the first character is a number, prefix it with "_".
159
+ if (!/^[$A-Z_a-z]/.test(sanitizedName)) {
160
+ sanitizedName = `_${sanitizedName}`;
155
161
  }
156
- return matches[0];
162
+ return sanitizedName;
157
163
  }
158
164
  exports.unqualifySchema = unqualifySchema;
159
- // TODO: yuck, this entire file has too many statics. we should rewrite it as a generic zod schema walk.
160
- let detailsI = {
161
- hasHelperMethods: false,
162
- };
163
- /**
164
- * Returns the TypeScript source code corresponding to a Zod schema. The schema is supplied as an object where each
165
- * property provides a name for an associated Zod type. The return value is a string containing the TypeScript source
166
- * code corresponding to the schema. Each property of the schema object is emitted as a named `interface` or `type`
167
- * declaration for the associated type and is referenced by that name in the emitted type declarations. Other types
168
- * referenced in the schema are emitted in their structural form.
169
- * @param schema - A schema object where each property provides a name for an associated Zod type.
170
- * @param details - Optional details about the schema. The fields will be set according to the details in the given schema.
171
- * @returns The TypeScript source code corresponding to the schema.
172
- */
173
- function getZodSchemaAsTypeScript(schema, details) {
174
- detailsI = details ?? { hasHelperMethods: false };
175
- let result = "";
176
- let startOfLine = true;
177
- let indent = 0;
178
- const entries = [...Object.entries(schema)];
179
- const namedTypes = new Map(entries.map(([name, type]) => [getTypeIdentity(type), name]));
180
- for (const [name, type] of entries) {
181
- if (result) {
182
- appendNewLine();
183
- }
184
- const description = type._def.description;
185
- if (description !== undefined && description !== "") {
186
- for (const comment of description.split("\n")) {
187
- append(`// ${comment}`);
188
- appendNewLine();
189
- }
190
- }
191
- if (getTypeKind(type) === zod_1.z.ZodFirstPartyTypeKind.ZodObject) {
192
- append(`interface ${name} `);
193
- appendObjectType(type);
194
- }
195
- else {
196
- append(`type ${name} = `);
197
- appendTypeDefinition(type);
198
- append(";");
199
- }
200
- appendNewLine();
201
- }
202
- return result;
203
- function append(s) {
204
- if (startOfLine) {
205
- result += " ".repeat(indent);
206
- startOfLine = false;
207
- }
208
- result += s;
209
- }
210
- function appendNewLine() {
211
- append("\n");
212
- startOfLine = true;
213
- }
214
- function appendType(type, minPrecedence = 2 /* TypePrecedence.Object */) {
215
- const name = namedTypes.get(getTypeIdentity(type));
216
- if (name === undefined) {
217
- const parenthesize = getTypePrecendece(type) < minPrecedence;
218
- if (parenthesize)
219
- append("(");
220
- appendTypeDefinition(type);
221
- if (parenthesize)
222
- append(")");
223
- }
224
- else {
225
- append(name);
226
- }
227
- }
228
- function appendTypeDefinition(type) {
229
- switch (getTypeKind(type)) {
230
- case zod_1.z.ZodFirstPartyTypeKind.ZodString: {
231
- return append("string");
232
- }
233
- case zod_1.z.ZodFirstPartyTypeKind.ZodNumber: {
234
- return append("number");
235
- }
236
- case zod_1.z.ZodFirstPartyTypeKind.ZodBoolean: {
237
- return append("boolean");
238
- }
239
- case zod_1.z.ZodFirstPartyTypeKind.ZodDate: {
240
- return append("Date");
241
- }
242
- case zod_1.z.ZodFirstPartyTypeKind.ZodUndefined: {
243
- return append("undefined");
244
- }
245
- case zod_1.z.ZodFirstPartyTypeKind.ZodNull: {
246
- return append("null");
247
- }
248
- case zod_1.z.ZodFirstPartyTypeKind.ZodUnknown: {
249
- return append("unknown");
250
- }
251
- case zod_1.z.ZodFirstPartyTypeKind.ZodArray: {
252
- return appendArrayType(type);
253
- }
254
- case zod_1.z.ZodFirstPartyTypeKind.ZodObject: {
255
- return appendObjectType(type);
256
- }
257
- case zod_1.z.ZodFirstPartyTypeKind.ZodUnion: {
258
- return appendUnionOrIntersectionTypes(type._def.options, 0 /* TypePrecedence.Union */);
259
- }
260
- case zod_1.z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
261
- return appendUnionOrIntersectionTypes([...type._def.options.values()], 0 /* TypePrecedence.Union */);
262
- }
263
- case zod_1.z.ZodFirstPartyTypeKind.ZodIntersection: {
264
- return appendUnionOrIntersectionTypes([
265
- type._def.left,
266
- type._def.right,
267
- ], 1 /* TypePrecedence.Intersection */);
268
- }
269
- case zod_1.z.ZodFirstPartyTypeKind.ZodTuple: {
270
- return appendTupleType(type);
271
- }
272
- case zod_1.z.ZodFirstPartyTypeKind.ZodRecord: {
273
- return appendRecordType(type);
274
- }
275
- case zod_1.z.ZodFirstPartyTypeKind.ZodMap: {
276
- return appendMapType(type);
277
- }
278
- case zod_1.z.ZodFirstPartyTypeKind.ZodLiteral: {
279
- return appendLiteral(type._def.value);
280
- }
281
- case zod_1.z.ZodFirstPartyTypeKind.ZodEnum: {
282
- return append(type._def.values.map((value) => JSON.stringify(value)).join(" | "));
283
- }
284
- case zod_1.z.ZodFirstPartyTypeKind.ZodOptional: {
285
- return appendUnionOrIntersectionTypes([type._def.innerType, zod_1.z.undefined()], 0 /* TypePrecedence.Union */);
286
- }
287
- case zod_1.z.ZodFirstPartyTypeKind.ZodReadonly: {
288
- return appendReadonlyType(type);
289
- }
290
- case zod_1.z.ZodFirstPartyTypeKind.ZodEffects: {
291
- // Currently, this only handles schema class instances, but there are other cases in which a ZodEffects could theoretically be used.
292
- if (instanceOfs.has(type)) {
293
- const objectNodeSchema = instanceOfs.get(type);
294
- if (objectNodeSchema === undefined) {
295
- throw new internal_2.UsageError(`Unsupported zod effects type when transforming class method: ${getTypeKind(type)}`);
296
- }
297
- return append(getFriendlyName(objectNodeSchema));
298
- }
299
- throw new Error("Unsupported zod effects type. Did you use z.instanceOf? Use ExposedMethods.instanceOf function to reference schema classes in methods.");
300
- }
301
- case zod_1.z.ZodFirstPartyTypeKind.ZodVoid: {
302
- return append("void");
303
- }
304
- case zod_1.z.ZodFirstPartyTypeKind.ZodLazy: {
305
- return appendType(type._def.getter());
306
- }
307
- default: {
308
- throw new internal_2.UsageError(`Unsupported type when transforming class method: ${getTypeKind(type)}`);
309
- }
310
- }
311
- }
312
- function appendBoundMethods(boundType) {
313
- // eslint-disable-next-line prefer-const
314
- for (let [name, type] of Object.entries(boundType._def.shape())) {
315
- // Special handling of methods on objects
316
- const method = type.method;
317
- if (method !== undefined && method instanceof methodBinding_js_1.FunctionWrapper) {
318
- detailsI.hasHelperMethods = true;
319
- append(name);
320
- append("(");
321
- let first = true;
322
- for (const [argName, argType] of method.args) {
323
- if (!first)
324
- append(", ");
325
- if (getTypeKind(argType) === zod_1.z.ZodFirstPartyTypeKind.ZodOptional) {
326
- append(`${argName}?: `);
327
- appendType(argType._def.innerType, 2 /* TypePrecedence.Object */);
328
- }
329
- else {
330
- append(`${argName}: `);
331
- appendType(argType);
332
- }
333
- first = false;
334
- }
335
- if (method.rest !== null) {
336
- if (!first)
337
- append(", ");
338
- append("...rest: ");
339
- appendType(method.rest, 2 /* TypePrecedence.Object */);
340
- append("[]");
341
- }
342
- append(`): `);
343
- appendType(method.returns, 2 /* TypePrecedence.Object */);
344
- append(";");
345
- if (method.description !== undefined) {
346
- append(` // ${method.description}`);
347
- }
348
- appendNewLine();
349
- }
350
- }
351
- }
352
- function appendBoundProperties(type) {
353
- const property = type.property;
354
- if (!(property instanceof propertyBinding_js_1.PropertyDef)) {
355
- if (type.description !== undefined && type.description !== "") {
356
- append(` // ${type.description}`);
357
- }
358
- return;
359
- }
360
- if (property.readOnly === true) {
361
- append(" // readonly");
362
- }
363
- if (property.description !== undefined && property.description !== "") {
364
- append(` - ${property.description}`);
365
- }
366
- }
367
- function appendArrayType(arrayType) {
368
- appendType(arrayType._def.type, 2 /* TypePrecedence.Object */);
369
- append("[]");
370
- }
371
- function appendObjectType(objectType) {
372
- append("{");
373
- appendNewLine();
374
- indent++;
375
- // eslint-disable-next-line prefer-const
376
- for (let [name, type] of Object.entries(objectType._def.shape())) {
377
- const method = type.method;
378
- if (method === undefined || !(method instanceof methodBinding_js_1.FunctionWrapper)) {
379
- append(name);
380
- if (getTypeKind(type) === zod_1.z.ZodFirstPartyTypeKind.ZodOptional) {
381
- append("?");
382
- type = type._def.innerType;
383
- }
384
- append(": ");
385
- appendType(type);
386
- append(";");
387
- appendBoundProperties(type);
388
- appendNewLine();
389
- }
390
- }
391
- appendBoundMethods(objectType);
392
- indent--;
393
- append("}");
394
- }
395
- function appendUnionOrIntersectionTypes(types, minPrecedence) {
396
- let first = true;
397
- for (const type of types) {
398
- if (!first)
399
- append(minPrecedence === 1 /* TypePrecedence.Intersection */ ? " & " : " | ");
400
- appendType(type, minPrecedence);
401
- first = false;
402
- }
403
- }
404
- function appendTupleType(tupleType) {
405
- append("[");
406
- let first = true;
407
- for (const type of tupleType._def.items) {
408
- if (!first)
409
- append(", ");
410
- if (getTypeKind(type) === zod_1.z.ZodFirstPartyTypeKind.ZodOptional) {
411
- appendType(type._def.innerType, 2 /* TypePrecedence.Object */);
412
- append("?");
413
- }
414
- else {
415
- appendType(type);
416
- }
417
- first = false;
418
- }
419
- const rest = tupleType._def.rest;
420
- if (rest !== null) {
421
- if (!first)
422
- append(", ");
423
- append("...");
424
- appendType(rest, 2 /* TypePrecedence.Object */);
425
- append("[]");
426
- }
427
- append("]");
428
- }
429
- function appendRecordType(recordType) {
430
- append("Record<");
431
- appendType(recordType._def.keyType);
432
- append(", ");
433
- appendType(recordType._def.valueType);
434
- append(">");
435
- }
436
- function appendMapType(mapType) {
437
- append("Map<");
438
- appendType(mapType._def.keyType);
439
- append(", ");
440
- appendType(mapType._def.valueType);
441
- append(">");
442
- }
443
- function appendLiteral(value) {
444
- append(typeof value === "string" || typeof value === "number" || typeof value === "boolean"
445
- ? JSON.stringify(value)
446
- : "any");
447
- }
448
- function appendReadonlyType(readonlyType) {
449
- append("Readonly<");
450
- appendType(readonlyType._def.innerType);
451
- append(">");
452
- }
453
- }
454
- exports.getZodSchemaAsTypeScript = getZodSchemaAsTypeScript;
455
- function getTypeKind(type) {
456
- return type._def.typeName;
457
- }
458
- function getTypeIdentity(type) {
459
- switch (getTypeKind(type)) {
460
- case zod_1.z.ZodFirstPartyTypeKind.ZodObject: {
461
- return type._def.shape();
462
- }
463
- case zod_1.z.ZodFirstPartyTypeKind.ZodEnum: {
464
- return type._def.values;
465
- }
466
- case zod_1.z.ZodFirstPartyTypeKind.ZodUnion: {
467
- return type._def.options;
468
- }
469
- default: {
470
- return type;
471
- }
472
- }
473
- }
474
- var TypePrecedence;
475
- (function (TypePrecedence) {
476
- TypePrecedence[TypePrecedence["Union"] = 0] = "Union";
477
- TypePrecedence[TypePrecedence["Intersection"] = 1] = "Intersection";
478
- TypePrecedence[TypePrecedence["Object"] = 2] = "Object";
479
- })(TypePrecedence || (TypePrecedence = {}));
480
- function getTypePrecendece(type) {
481
- switch (getTypeKind(type)) {
482
- case zod_1.z.ZodFirstPartyTypeKind.ZodEnum:
483
- case zod_1.z.ZodFirstPartyTypeKind.ZodUnion:
484
- case zod_1.z.ZodFirstPartyTypeKind.ZodDiscriminatedUnion: {
485
- return 0 /* TypePrecedence.Union */;
486
- }
487
- case zod_1.z.ZodFirstPartyTypeKind.ZodIntersection: {
488
- return 1 /* TypePrecedence.Intersection */;
489
- }
490
- default: {
491
- return 2 /* TypePrecedence.Object */;
492
- }
493
- }
494
- }
495
- /**
496
- * Create a Zod schema for a SharedTree schema class.
497
- * @alpha
498
- */
499
- function instanceOf(schema) {
500
- if (!(schema instanceof alpha_1.ObjectNodeSchema)) {
501
- throw new internal_2.UsageError(`${schema.identifier} must be an instance of ObjectNodeSchema.`);
502
- }
503
- const effect = zod_1.z.instanceof(schema);
504
- instanceOfs.set(effect, schema);
505
- return effect;
506
- }
507
- exports.instanceOf = instanceOf;
508
- const instanceOfs = new WeakMap();
509
165
  /**
510
166
  * Adds all (optionally filtered) schemas reachable from the given schema to the given set.
511
167
  * @returns The set of schemas added (same as the `schemas` parameter, if supplied).