@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
112 lines • 5.63 kB
JavaScript
import { formatSkillsForPrompt } from "../skills.js";
import { buildHandoffSection } from "./handoff.js";
import { buildIdentitySection } from "./identity.js";
import { buildIntentGate } from "./intent-gate.js";
import { buildPoliciesSection } from "./policies.js";
import { buildStyleSection } from "./style.js";
import { categorizeTools } from "./tool-categorization.js";
import { buildToolSection } from "./tool-section.js";
import { buildVerificationSection } from "./verification.js";
import { buildWorkingTaskSection } from "./working-task.js";
import { buildWorkstationSection } from "./workstation.js";
export { terminalOrApp } from "./types.js";
export const PROMPT_SURFACE_ENV_VAR = "SENPI_PROMPT_SURFACE";
/** `SENPI_PROMPT_SURFACE=app` or `=chat` selects that surface; unset or any other value is the terminal. */
export function resolvePromptSurface(env) {
const value = env[PROMPT_SURFACE_ENV_VAR];
return value === "app" || value === "chat" ? value : "terminal";
}
const USER_INSTRUCTIONS_HEADER = "\n\n## User Instructions\n\nThe user's own instruction files (below) are standing instructions from the user. They override the default working style above, including whether, when, and how to ask the user questions; only an explicit instruction in the current conversation outranks them.\n\n";
const PROJECT_CONTEXT_HEADER = "\n\n## Project Context\n\nProject instruction files (below, and in [Directory Context: ...] blocks injected during reads) apply to work under their directory and override the default working style above, including whether, when, and how to ask the user questions; deeper files win on conflict; explicit user instructions override.\n\n";
export function buildDynamicSystemPromptParts(options) {
if (options.customPrompt !== undefined) {
return [
{ name: "custom_prompt", source: "loader", text: options.customPrompt },
...(options.appendSystemPrompt === undefined
? []
: [{ name: "append_system_prompt", source: "loader", text: `\n\n${options.appendSystemPrompt}` }]),
];
}
const tools = categorizeTools(options.selectedTools);
const sharedSections = options.sharedSections ?? "diet";
const toolPrompt = sharedSections === "full" ? options.fullToolPrompt : undefined;
const toolSection = buildToolSection({
tools,
toolSnippets: toolPrompt?.toolSnippets ?? options.toolSnippets,
promptGuidelines: toolPrompt?.promptGuidelines ?? options.promptGuidelines,
sharedSections,
});
const surface = options.surface ?? "terminal";
const sections = options.corePrompt
? [
options.corePrompt({
tools,
toolSection,
surface,
appName: options.appName,
sharedSections,
nativeSearchAvailable: options.nativeSearchAvailable,
}),
]
: [
buildIdentitySection(options.appName, sharedSections),
"",
buildIntentGate({ tools, surface, sharedSections }),
"",
buildWorkingTaskSection({ sharedSections }),
"",
buildVerificationSection({ surface, sharedSections }),
"",
...(toolSection ? [toolSection, ""] : []),
buildPoliciesSection({ sharedSections }),
"",
buildHandoffSection({ surface, sharedSections }),
"",
buildStyleSection({ surface, sharedSections }),
];
const parts = [{ name: "core", source: "dynamic-prompt", text: sections.join("\n") }];
const tuning = options.tuningSection?.trim();
if (tuning) {
parts.push({ name: "tuning", source: "prompt-preset", text: `\n\n${tuning}` });
}
// Instruction files outrank the core's and presets' default working style, asking included.
const userFiles = options.contextFiles.filter((file) => file.scope === "user");
const projectFiles = options.contextFiles.filter((file) => file.scope !== "user");
const contextGroups = [
{ files: userFiles, header: USER_INSTRUCTIONS_HEADER },
{ files: projectFiles, header: PROJECT_CONTEXT_HEADER },
].filter((group) => group.files.length > 0);
for (const group of contextGroups) {
parts.push({ name: "project_context", source: "dynamic-prompt", text: group.header });
for (const [index, file] of group.files.entries()) {
const text = `### ${file.path}\n\n${file.content.trimEnd()}`;
parts.push({
name: "project_context",
source: file.path,
text: index === group.files.length - 1 ? text.trimEnd() : `${text}\n\n`,
});
}
}
const skillsSection = formatSkillsForPrompt(options.skills);
if (skillsSection) {
parts.push({ name: "skills", source: "skills", text: `\n${skillsSection}` });
}
parts.push({
name: "workstation",
source: "dynamic-prompt",
text: `\n\n${buildWorkstationSection({
selectedTools: options.selectedTools,
dialect: options.workstationDialect ?? "default",
})}`,
});
if (options.appendSystemPrompt !== undefined) {
parts.push({ name: "append_system_prompt", source: "loader", text: `\n\n${options.appendSystemPrompt}` });
}
return parts;
}
export function buildDynamicSystemPrompt(options) {
const parts = buildDynamicSystemPromptParts(options);
options.onParts?.(parts);
return parts.map((part) => part.text).join("");
}
//# sourceMappingURL=build.js.map