MCPcopy Create free account
hub / github.com/google-gemini/gemini-cli / extractFirstUserMessage

Function extractFirstUserMessage

packages/cli/src/utils/sessionUtils.ts:161–186  ·  view source on GitHub ↗
(messages: MessageRecord[])

Source from the content-addressed store, hash-verified

159 * Extracts the first meaningful user message from conversation messages.
160 */
161export const extractFirstUserMessage = (messages: MessageRecord[]): string => {
162 const userMessage = messages
163 // First try filtering out slash commands.
164 .filter((msg) => {
165 const content = partListUnionToString(msg.content);
166 return (
167 !content.startsWith('/') &&
168 !content.startsWith('?') &&
169 content.trim().length > 0
170 );
171 })
172 .find((msg) => msg.type === 'user');
173
174 let content: string;
175
176 if (!userMessage) {
177 // Fallback to first user message even if it's a slash command
178 const firstMsg = messages.find((msg) => msg.type === 'user');
179 if (!firstMsg) return 'Empty conversation';
180 content = cleanMessage(partListUnionToString(firstMsg.content));
181 } else {
182 content = cleanMessage(partListUnionToString(userMessage.content));
183 }
184
185 return content;
186};
187
188/**
189 * Formats a timestamp as relative time.

Callers 2

getAllSessionFilesFunction · 0.85

Calls 2

partListUnionToStringFunction · 0.85
cleanMessageFunction · 0.85

Tested by

no test coverage detected