@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
40 lines • 1.65 kB
JavaScript
const MAX_INSTRUCTIONS_CHARS = 4000;
export function refreshMcpInstructionsForSession(service) {
service.setMcpInstructions(buildMcpInstructionsBlock(service));
}
export function injectMcpInstructions(service, systemPrompt) {
const instructions = service.getMcpInstructions();
if (instructions.length === 0)
return undefined;
if (systemPrompt.includes(instructions))
return undefined;
return `${systemPrompt}\n\n${instructions}`;
}
function buildMcpInstructionsBlock(service) {
const blocks = [];
for (const snapshot of service.getServerSnapshots()) {
const connection = service.getConnection(snapshot.name);
const instructions = connection?.state === "connected"
? connection.client.getInstructions()
: service.getCachedInstructions(snapshot.name);
if (instructions === undefined || instructions.length === 0)
continue;
blocks.push(formatInstructionsBlock(snapshot.name, instructions));
}
return blocks.join("\n\n");
}
function formatInstructionsBlock(serverName, instructions) {
const escapedServerName = escapeXml(serverName);
const cappedInstructions = instructions.slice(0, MAX_INSTRUCTIONS_CHARS);
const escapedInstructions = escapeXml(cappedInstructions);
return `<mcp_instructions server="${escapedServerName}">\n${escapedInstructions}\n</mcp_instructions>`;
}
function escapeXml(value) {
return value
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
//# sourceMappingURL=instructions.js.map