mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-21 10:34:35 -07:00
Support context injection via SessionStart hook. (#15746)
This commit is contained in:
committed by
GitHub
parent
2da911e4a0
commit
6d1e27633a
@@ -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.`,
|
||||
|
||||
@@ -300,7 +300,31 @@ export const AppContainer = (props: AppContainerProps) => {
|
||||
const sessionStartSource = resumedSessionData
|
||||
? SessionStartSource.Resume
|
||||
: SessionStartSource.Startup;
|
||||
await fireSessionStartHook(hookMessageBus, sessionStartSource);
|
||||
const result = await fireSessionStartHook(
|
||||
hookMessageBus,
|
||||
sessionStartSource,
|
||||
);
|
||||
|
||||
if (result) {
|
||||
if (result.systemMessage) {
|
||||
historyManager.addItem(
|
||||
{
|
||||
type: MessageType.INFO,
|
||||
text: result.systemMessage,
|
||||
},
|
||||
Date.now(),
|
||||
);
|
||||
}
|
||||
|
||||
const additionalContext = result.getAdditionalContext();
|
||||
const geminiClient = config.getGeminiClient();
|
||||
if (additionalContext && geminiClient) {
|
||||
await geminiClient.addHistory({
|
||||
role: 'user',
|
||||
parts: [{ text: additionalContext }],
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fire-and-forget: generate summary for previous session in background
|
||||
@@ -321,6 +345,12 @@ export const AppContainer = (props: AppContainerProps) => {
|
||||
await fireSessionEndHook(hookMessageBus, SessionEndReason.Exit);
|
||||
}
|
||||
});
|
||||
// Disable the dependencies check here. historyManager gets flagged
|
||||
// but we don't want to react to changes to it because each new history
|
||||
// item, including the ones from the start session hook will cause a
|
||||
// re-render and an error when we try to reload config.
|
||||
//
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [config, resumedSessionData]);
|
||||
|
||||
useEffect(
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type { DefaultHookOutput } from '@google/gemini-cli-core';
|
||||
import {
|
||||
uiTelemetryService,
|
||||
fireSessionEndHook,
|
||||
@@ -14,6 +15,7 @@ import {
|
||||
} from '@google/gemini-cli-core';
|
||||
import type { SlashCommand } from './types.js';
|
||||
import { CommandKind } from './types.js';
|
||||
import { MessageType } from '../types.js';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
export const clearCommand: SlashCommand = {
|
||||
@@ -52,8 +54,9 @@ export const clearCommand: SlashCommand = {
|
||||
}
|
||||
|
||||
// Fire SessionStart hook after clearing
|
||||
let result: DefaultHookOutput | undefined;
|
||||
if (config?.getEnableHooks() && messageBus) {
|
||||
await fireSessionStartHook(messageBus, SessionStartSource.Clear);
|
||||
result = await fireSessionStartHook(messageBus, SessionStartSource.Clear);
|
||||
}
|
||||
|
||||
// Give the event loop a chance to process any pending telemetry operations
|
||||
@@ -68,5 +71,15 @@ export const clearCommand: SlashCommand = {
|
||||
|
||||
uiTelemetryService.setLastPromptTokenCount(0);
|
||||
context.ui.clear();
|
||||
|
||||
if (result?.systemMessage) {
|
||||
context.ui.addItem(
|
||||
{
|
||||
type: MessageType.INFO,
|
||||
text: result.systemMessage,
|
||||
},
|
||||
Date.now(),
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user