microsoft / vscode-prompt-tsx

MIT License
34 stars 1 forks source link

refactor: make priority values local #94

Closed connor4312 closed 4 weeks ago

connor4312 commented 1 month ago

Previously, priority was global within the entire prompt. This made composition difficult since a child component might not know the range of 'good' priorities in which is should act. Mistakes are also easy to make because the default priority is MAX_SAFE_INTEGER.

This PR refactors the world so that priority is local in each tree node. For example the tree of nodes...

<UserMessage priority={1}>
  <TextChunk priority={100}>A</TextChunk>
  <TextChunk priority={0}>B</TextChunk>
</UserMesssage>
<SystemMessage priority={2}>
  <TextChunk priority={200}>C</TextChunk>
  <TextChunk priority={20}>D</TextChunk>
</SystemMessage>

...would be pruned in the order B->A->D->C. If two sibling elements share the same priority, the renderer looks ahead at their direct children and picks whichever one has a child with the lowest priority: if the SystemMessage and UserMessage in the above example did not declare priorities, the pruning order would be B->D->A->C.

Internally this involved some refactoring so that we preserve the tree of elements during the render and incrementally remove elements from it until we reach our budget. I was also able to remove much of the special treatment of implicit line breaks with this new representation.

We may have a need for a priorityPassThru attribute in the future to allow for logical wrapper elements.

In reworking this I also added a local meta property to represent metadata that is local to an element and should be removed when the element is pruned, which resolves #51. References are now just local metadata that retain some special treatment in how they're returned from the render method.

connor4312 commented 1 month ago

fyi @joyceerhl @jrieken @alexdima