Skip to content

Commit d9742af

Browse files
authored
Handle colliding short names (microsoft#26080)
## Description This PR handles short name collisions for the tree-agent. example: `scope1.foo` and `scope2.foo` becomes `foo_1` and `foo_2`
1 parent e83a0ed commit d9742af

File tree

7 files changed

+401
-139
lines changed

7 files changed

+401
-139
lines changed

packages/framework/tree-agent/src/prompt.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import { normalizeFieldSchema, ValueSchema } from "@fluidframework/tree/internal
1212

1313
import type { Subtree } from "./subtree.js";
1414
import { generateEditTypesForPrompt } from "./typeGeneration.js";
15-
import { getFriendlyName, communize, findSchemas } from "./utils.js";
15+
import {
16+
IdentifierCollisionResolver,
17+
getFriendlyName,
18+
communize,
19+
findSchemas,
20+
} from "./utils.js";
1621

1722
/**
1823
* The type name used for handles in generated TypeScript.
@@ -33,18 +38,23 @@ export function getPrompt(args: {
3338
const mapInterfaceName = "TreeMap";
3439
// Inspect the schema to determine what kinds of nodes are possible - this will affect how much information we need to include in the prompt.
3540
const rootTypes = [...normalizeFieldSchema(schema).allowedTypeSet];
36-
const rootTypeUnion = `${rootTypes.map((t) => getFriendlyName(t)).join(" | ")}`;
41+
const allSchemas = findSchemas(schema);
42+
const resolver = new IdentifierCollisionResolver();
43+
for (const schemaNode of allSchemas) {
44+
resolver.resolve(schemaNode);
45+
}
46+
47+
const rootTypeUnion = `${rootTypes.map((t) => resolver.resolve(t) ?? getFriendlyName(t)).join(" | ")}`;
3748
let nodeTypeUnion: string | undefined;
3849
let hasArrays = false;
3950
let hasMaps = false;
4051
let hasFluidHandles = false;
4152
let exampleObjectName: string | undefined;
42-
for (const s of findSchemas(schema)) {
53+
for (const s of allSchemas) {
4354
if (s.kind !== NodeKind.Leaf) {
55+
const friendlyName = resolver.resolve(s);
4456
nodeTypeUnion =
45-
nodeTypeUnion === undefined
46-
? getFriendlyName(s)
47-
: `${nodeTypeUnion} | ${getFriendlyName(s)}`;
57+
nodeTypeUnion === undefined ? friendlyName : `${nodeTypeUnion} | ${friendlyName}`;
4858
}
4959

5060
switch (s.kind) {
@@ -57,7 +67,7 @@ export function getPrompt(args: {
5767
break;
5868
}
5969
case NodeKind.Object: {
60-
exampleObjectName ??= getFriendlyName(s);
70+
exampleObjectName ??= resolver.resolve(s);
6171
break;
6272
}
6373
case NodeKind.Leaf: {
@@ -68,7 +78,6 @@ export function getPrompt(args: {
6878
}
6979
}
7080

71-
const stringified = stringifyTree(field);
7281
const { schemaText: typescriptSchemaTypes, hasHelperMethods } = generateEditTypesForPrompt(
7382
schema,
7483
getSimpleSchema(schema),
@@ -306,10 +315,10 @@ ${
306315
? ""
307316
: `\nThe application supplied the following additional instructions: ${domainHints}`
308317
}
309-
The current state of \`context.root\` (a \`${field === undefined ? "undefined" : getFriendlyName(Tree.schema(field))}\`) is:
318+
The current state of \`context.root\` (a \`${field === undefined ? "undefined" : resolver.resolve(Tree.schema(field))}\`) is:
310319
311320
\`\`\`JSON
312-
${stringified}
321+
${stringifyTree(field, resolver)}
313322
\`\`\``;
314323
return prompt;
315324
}
@@ -318,7 +327,11 @@ ${stringified}
318327
* Serializes tree data e.g. to include in a prompt or message.
319328
* @remarks This includes some extra metadata to make it easier to understand the structure of the tree.
320329
*/
321-
export function stringifyTree(tree: ReadableField<ImplicitFieldSchema>): string {
330+
export function stringifyTree(
331+
tree: ReadableField<ImplicitFieldSchema>,
332+
collisionResolver?: IdentifierCollisionResolver,
333+
): string {
334+
const resolver = collisionResolver ?? new IdentifierCollisionResolver();
322335
const typeReplacementKey = "_e944da5a5fd04ea2b8b2eb6109e089ed";
323336
const indexReplacementKey = "_27bb216b474d45e6aaee14d1ec267b96";
324337
const mapReplacementKey = "_a0d98d22a1c644539f07828d3f064d71";
@@ -331,8 +344,9 @@ export function stringifyTree(tree: ReadableField<ImplicitFieldSchema>): string
331344
const schema = Tree.schema(node);
332345
switch (schema.kind) {
333346
case NodeKind.Object: {
347+
const friendlyName = resolver.resolve(schema);
334348
return {
335-
[typeReplacementKey]: getFriendlyName(schema),
349+
[typeReplacementKey]: friendlyName,
336350
[indexReplacementKey]: index,
337351
...node,
338352
};

0 commit comments

Comments
 (0)