Skip to content

Commit cbbccc2

Browse files
committed
Enhance session file handling in CreateContextFromPrompt; improve error messaging for prompt changes and ensure context merging only occurs when existing context is available
1 parent 3aadaa3 commit cbbccc2

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

cmd/generate/context.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func (h *generateCommandHandler) CreateContextFromPrompt(promptFile string) (*Pr
3737
}
3838

3939
// Determine session file path
40-
if h.sessionFile == nil || *h.sessionFile == "" {
40+
defaultSessionFile := h.sessionFile == nil || *h.sessionFile == ""
41+
if defaultSessionFile {
4142
// Generate default session file name by replacing 'prompt.yml' with '.generate.json'
4243
h.sessionFile = util.Ptr(generateDefaultSessionFileName(promptFile))
4344
}
@@ -55,11 +56,20 @@ func (h *generateCommandHandler) CreateContextFromPrompt(promptFile string) (*Pr
5556
// Check if prompt hashes match
5657
if existingContext.PromptHash != nil && context.PromptHash != nil &&
5758
*existingContext.PromptHash != *context.PromptHash {
58-
return nil, fmt.Errorf("prompt hash mismatch: existing context has different prompt than current file")
59+
if !defaultSessionFile {
60+
return nil, fmt.Errorf("prompt changed unable to reuse session file")
61+
} else {
62+
// the prompt changed and the user is implicitely leverage the context file for caching,
63+
// silently clear out the context
64+
h.cfg.WriteToOut("Prompt changed, ignoring previous session...\n")
65+
existingContext = nil
66+
}
5967
}
6068

6169
// Merge existing context data
62-
context = mergeContexts(existingContext, context)
70+
if existingContext != nil {
71+
context = mergeContexts(existingContext, context)
72+
}
6373
}
6474

6575
return context, nil

examples/test_generate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ model: openai/gpt-4o-mini
44
messages:
55
- role: system
66
content: |
7-
You are an expert at telling jokes. Determine if the Joke below is funny or not.
7+
You are an expert at telling jokes. Determine if the Joke below is funny or not funny
88
- role: user
99
content: |
1010
{{text}}

0 commit comments

Comments
 (0)