Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix(prompts): keep input_completion system prompt stable across invocations (#15230)

Arvin Xub3cbc9a71059
diff --git a/packages/prompts/src/chains/inputCompletion.test.ts b/packages/prompts/src/chains/inputCompletion.test.ts
index ddd7ce0bcc3..882c311a824 100644
--- a/packages/prompts/src/chains/inputCompletion.test.ts
+++ b/packages/prompts/src/chains/inputCompletion.test.ts
@@ -25,15 +25,33 @@ describe('chainInputCompletion', () => {
expect(schema.schema.properties.completion.type).toBe('string');
});
- it('appends conversation context to the system prompt when provided', () => {
+ it('adds a separate user message for conversation context when provided', () => {
const { messages } = chainInputCompletion('write ', '', [
{ content: 'previous response', role: 'assistant' },
{ content: 'previous question', role: 'user' },
]);
- const sys = messages[0].content as string;
- expect(sys).toContain('Current conversation context');
- expect(sys).toContain('assistant: previous response');
- expect(sys).toContain('user: previous question');
+ expect(messages).toHaveLength(3);
+ expect(messages[0].role).toBe('system');
+ expect(messages[1].role).toBe('user');
+ expect(messages[2].role).toBe('user');
+
+ const contextMsg = messages[1].content as string;
+ expect(contextMsg).toContain('Current conversation context');
+ expect(contextMsg).toContain('assistant: previous response');
+ expect(contextMsg).toContain('user: previous question');
+ expect(contextMsg).not.toContain('Before cursor');
+
+ const cursorMsg = messages[2].content as string;
+ expect(cursorMsg).toContain('Before cursor: "write "');
+ });
+
+ it('keeps the system prompt stable regardless of conversation context', () => {
+ const a = chainInputCompletion('hi', '');
+ const b = chainInputCompletion('hi', '', [
+ { content: 'previous response', role: 'assistant' },
+ { content: 'previous question', role: 'user' },
+ ]);
+ expect(a.messages[0].content).toBe(b.messages[0].content);
});
it('exports a version constant the call site can pin to metadata', () => {
diff --git a/packages/prompts/src/chains/inputCompletion.ts b/packages/prompts/src/chains/inputCompletion.ts
index c0c0b2f7394..a809c3d81f3 100644
--- a/packages/prompts/src/chains/inputCompletion.ts
+++ b/packages/prompts/src/chains/inputCompletion.ts
@@ -6,7 +6,7 @@ import type { OpenAIChatMessage } from '@lobechat/types';
* groups runs by prompt iteration. The 6-char prompt hash on the row catches
* forgotten bumps.
*/
-export const INPUT_COMPLETION_PROMPT_VERSION = 'v1.0';
+export const INPUT_COMPLETION_PROMPT_VERSION = 'v1.1';
/**
* Symbolic schema name β€” also recorded on the tracing row's `schemaName`
@@ -83,15 +83,25 @@ export const chainInputCompletion = (
afterCursor: string,
context?: OpenAIChatMessage[],
): InputCompletionChainResult => {
- let contextBlock = '';
- if (context?.length) {
- contextBlock = `\n\nCurrent conversation context:\n${context.map((m) => `${m.role}: ${m.content}`).join('\n')}`;
- }
+ // Context is dynamic per conversation β€” keep it OUT of the system message so
+ // the system prompt (and thus the tracing `promptHash`) stays stable across
+ // invocations. Otherwise every keystroke in a longer conversation produces a
+ // distinct hash, defeating the per-prompt grouping.
+ const contextMessage: OpenAIChatMessage | null = context?.length
+ ? {
+ content: `Current conversation context:\n${context.map((m) => `${m.role}: ${m.content}`).join('\n')}`,
+ role: 'user',
+ }
+ : null;
return {
messages: [
- { content: `${SYSTEM_PROMPT}${contextBlock}`, role: 'system' },
- { content: `Before cursor: "${beforeCursor}"\nAfter cursor: "${afterCursor}"`, role: 'user' },
+ { content: SYSTEM_PROMPT, role: 'system' },
+ ...(contextMessage ? [contextMessage] : []),
+ {
+ content: `Before cursor: "${beforeCursor}"\nAfter cursor: "${afterCursor}"`,
+ role: 'user',
+ },
],
schema: INPUT_COMPLETION_SCHEMA,
};