( agentId?: string, )
| 1567 | * without leaking skills from other agent contexts. |
| 1568 | */ |
| 1569 | export function createSkillAttachmentIfNeeded( |
| 1570 | agentId?: string, |
| 1571 | ): AttachmentMessage | null { |
| 1572 | const invokedSkills = getInvokedSkillsForAgent(agentId) |
| 1573 | |
| 1574 | if (invokedSkills.size === 0) { |
| 1575 | return null |
| 1576 | } |
| 1577 | |
| 1578 | // Sorted most-recent-first so budget pressure drops the least-relevant skills. |
| 1579 | // Per-skill truncation keeps the head of each file (where setup/usage |
| 1580 | // instructions typically live) rather than dropping whole skills. |
| 1581 | let usedTokens = 0 |
| 1582 | const skills = Array.from(invokedSkills.values()) |
| 1583 | .sort((a, b) => b.invokedAt - a.invokedAt) |
| 1584 | .map(skill => ({ |
| 1585 | name: skill.skillName, |
| 1586 | path: skill.skillPath, |
| 1587 | content: truncateToTokens( |
| 1588 | skill.content, |
| 1589 | POST_COMPACT_MAX_TOKENS_PER_SKILL, |
| 1590 | ), |
| 1591 | })) |
| 1592 | .filter(skill => { |
| 1593 | const tokens = roughTokenCountEstimation(skill.content) |
| 1594 | if (usedTokens + tokens > POST_COMPACT_SKILLS_TOKEN_BUDGET) { |
| 1595 | return false |
| 1596 | } |
| 1597 | usedTokens += tokens |
| 1598 | return true |
| 1599 | }) |
| 1600 | |
| 1601 | if (skills.length === 0) { |
| 1602 | return null |
| 1603 | } |
| 1604 | |
| 1605 | return createAttachmentMessage({ |
| 1606 | type: 'invoked_skills', |
| 1607 | skills, |
| 1608 | }) |
| 1609 | } |
| 1610 | |
| 1611 | /** |
| 1612 | * Creates a plan_mode attachment if the user is currently in plan mode. |
no test coverage detected