Add isArray and isMap to tree-agent context#25725
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds isArray and isMap helper functions to the tree-agent context, providing LLM-friendly alternatives to Array.isArray and instanceof Map that work correctly with Fluid Framework's custom node types. These helpers also support native JavaScript arrays and Maps, allowing consistent usage patterns in LLM-generated code.
Key changes:
- Added
isArrayandisMaphelper functions to theContextinterface and implementation - Updated prompt generation to include documentation for these new helpers
- Added comprehensive test coverage for both helpers
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
packages/framework/tree-agent/src/api.ts |
Adds isArray and isMap method signatures to the Context interface |
packages/framework/tree-agent/src/agent.ts |
Implements isArray and isMap helpers using NodeKind checks for TreeNode instances |
packages/framework/tree-agent/src/prompt.ts |
Updates prompt generation to document the new helpers and improves example type name extraction |
packages/framework/tree-agent/src/test/agent.spec.ts |
Adds test coverage for isArray and isMap helpers, removes obsolete test and eslint directives |
packages/framework/tree-agent/src/test/__snapshots__/prompt.md |
Updates snapshot to reflect new helper documentation and improved examples |
packages/dds/tree/src/test/simple-tree/node-kinds/arrayNode.spec.ts |
Documents that custom array nodes don't pass Array.isArray |
| editToolName, | ||
| async query({ edit }) { | ||
| const result = await edit( | ||
| `if (!context.isArray([]) ||!context.isArray(context.root)) { throw new Error('Expected array root'); } context.root.insertAt(0, 99);`, |
There was a problem hiding this comment.
Missing space after || operator. Should be || !context.isArray for consistency with coding standards.
|
🔗 Found some broken links! 💔 Run a link check locally to find them. See linkcheck output |
This gives the LLM an alternative to `Array.isArray` and `instanceof Map` that will work on our node types, since `Array.isArray` does not work with our non-pojo array classes. The provided functions also work for JS arrays and Maps, so the LLM doesn't have to distinguish between when to use the built-in Array.isArray vs. when to use our provided `isArray` helper - it can always use the latter. This also gives us the option in the future to force it to use ours by making `Array.isArray` error within the boundaries of the eval. This also adds some explicit tests in SharedTree for `Array.isArray` support.
This gives the LLM an alternative to
Array.isArrayandinstanceof Mapthat will work on our node types, sinceArray.isArraydoes not work with our non-pojo array classes.The provided functions also work for JS arrays and Maps, so the LLM doesn't have to distinguish between when to use the built-in Array.isArray vs. when to use our provided
isArrayhelper - it can always use the latter. This also gives us the option in the future to force it to use ours by makingArray.isArrayerror within the boundaries of the eval.This also adds some explicit tests in SharedTree for
Array.isArraysupport.