-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathcopilot-cli-char-budget.ts
More file actions
84 lines (72 loc) · 2.65 KB
/
Copy pathcopilot-cli-char-budget.ts
File metadata and controls
84 lines (72 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Check if the formatted skill description of all the skills fit within Copilot CLI's default char budget.
* See https://github.com/github/copilot-agent-runtime/blob/0e43d66f7570421ba4b27a36a86ea908de188e59/src/skills/skillToolDescription.ts#L30
*/
import { listSkills, loadSkill } from "./shared/skill-helper.js";
import { escapeXml } from "./shared/string-helpers.js";
const SKILL_CHAR_BUDGET_ENV = "SKILL_CHAR_BUDGET";
// Note: Copilot CLI's default skill char budget is 15000,
// we set our budget to 20000 as a soft cap to not block skill contributions
// and remind us when the consumption grows too fast.
const DEFAULT_SKILL_CHAR_BUDGET = 20000;
function getSkillCharBudget() {
const envBudget = process.env[SKILL_CHAR_BUDGET_ENV];
if (envBudget) {
const parsed = parseInt(envBudget, 10);
if (!isNaN(parsed) && parsed > 0) {
return parsed;
}
}
return DEFAULT_SKILL_CHAR_BUDGET;
}
function formatSkillForToolDescription(skillName: string): string {
const skill = loadSkill(skillName);
// azure plugin skills are loaded from "Custom" locations when they are installed via marketplace.
// The formatted text may be different but the char count would be similar.
return `<skill>
<name>${escapeXml(skill.metadata.name)}</name>
<description>${escapeXml(skill.metadata.description)}</description>
<location>Custom</location>
</skill>`;
}
type CheckSkillCharBudgetResult = {
canFitInBudget: boolean;
budget: number;
actualCharCount: number;
};
/**
* Checks if the formatted skill description of azure plugin skills can fit in Copilot CLI's skill char budget.
*/
function checkCopilotCliSkillsCharBudget(): CheckSkillCharBudgetResult {
const skills = listSkills();
const budget = getSkillCharBudget();
if (skills.length === 0) {
return {
canFitInBudget: true,
budget: budget,
actualCharCount: 0
}
}
let charCount = 0;
for (const skill of skills) {
const skillXml = formatSkillForToolDescription(skill);
// +1 for newline between skills
charCount += skillXml.length + 1;
}
return {
canFitInBudget: charCount <= budget,
budget: budget,
actualCharCount: charCount
}
}
function main() {
const result = checkCopilotCliSkillsCharBudget();
if (!result.canFitInBudget) {
console.error(`Formatted skill description char count exceeds the Copilot CLI skill char budget. budget: ${result.budget}, actualCharCount: ${result.actualCharCount}`);
process.exitCode = 1;
return;
} else {
console.log(`Formatted skill description char count fits within the Copilot CLI skill char budget. budget: ${result.budget}, actualCharCount: ${result.actualCharCount}`);
}
}
main();