Support context injection via SessionStart hook. (#15746)

This commit is contained in:
Christian Gunderman
2026-01-05 13:27:53 -08:00
committed by GitHub
parent 4f26766dd1
commit 4581b1869f
7 changed files with 261 additions and 19 deletions
+27 -9
View File
@@ -634,6 +634,16 @@ export async function main() {
await config.initialize();
startupProfiler.flush(config);
// If not a TTY, read from stdin
// This is for cases where the user pipes input directly into the command
let stdinData: string | undefined = undefined;
if (!process.stdin.isTTY) {
stdinData = await readStdin();
if (stdinData) {
input = input ? `${stdinData}\n\n${input}` : stdinData;
}
}
// Fire SessionStart hook through MessageBus (only if hooks are enabled)
// Must be called AFTER config.initialize() to ensure HookRegistry is loaded
const hooksEnabled = config.getEnableHooks();
@@ -642,7 +652,23 @@ export async function main() {
const sessionStartSource = resumedSessionData
? SessionStartSource.Resume
: SessionStartSource.Startup;
await fireSessionStartHook(hookMessageBus, sessionStartSource);
const result = await fireSessionStartHook(
hookMessageBus,
sessionStartSource,
);
if (result) {
if (result.systemMessage) {
writeToStderr(result.systemMessage + '\n');
}
const additionalContext = result.getAdditionalContext();
if (additionalContext) {
// Prepend context to input (System Context -> Stdin -> Question)
input = input
? `${additionalContext}\n\n${input}`
: additionalContext;
}
}
// Register SessionEnd hook for graceful exit
registerCleanup(async () => {
@@ -650,14 +676,6 @@ export async function main() {
});
}
// If not a TTY, read from stdin
// This is for cases where the user pipes input directly into the command
if (!process.stdin.isTTY) {
const stdinData = await readStdin();
if (stdinData) {
input = `${stdinData}\n\n${input}`;
}
}
if (!input) {
debugLogger.error(
`No input provided via stdin. Input can be provided by piping data into gemini or using the --prompt option.`,