Skip to content

Commit 1ae38e5

Browse files
committed
fix: first message
1 parent b365a61 commit 1ae38e5

5 files changed

Lines changed: 36 additions & 8 deletions

File tree

internal/agent/session_runtime.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (s *Session) Prompt(text string) error {
2020

2121
var msgs []agentcore.AgentMessage
2222
if !s.preambleInjected && s.deferredToolsPreamble != "" {
23-
msgs = append(msgs, agentcore.UserMsg(s.deferredToolsPreamble))
23+
msgs = append(msgs, injectedUserMsg(s.deferredToolsPreamble))
2424
s.preambleInjected = true
2525
}
2626
msgs = append(msgs, s.buildUserMessage(agentcore.TextBlock(text)))
@@ -35,7 +35,7 @@ func (s *Session) PromptWithBlocks(blocks []agentcore.ContentBlock) error {
3535

3636
var msgs []agentcore.AgentMessage
3737
if !s.preambleInjected && s.deferredToolsPreamble != "" {
38-
msgs = append(msgs, agentcore.UserMsg(s.deferredToolsPreamble))
38+
msgs = append(msgs, injectedUserMsg(s.deferredToolsPreamble))
3939
s.preambleInjected = true
4040
}
4141
msgs = append(msgs, s.buildUserMessage(blocks...))
@@ -426,6 +426,7 @@ func (s *Session) SwitchSession(id string) error {
426426
if err := s.agent.SetMessages(snapshot.Messages); err != nil {
427427
return fmt.Errorf("restore messages: %w", err)
428428
}
429+
agentcore.ReactivateDeferred(s.allTools, snapshot.Messages)
429430
}
430431
if snapshot.Thinking != "" {
431432
thinkingLevel := agentcore.ThinkingLevel(snapshot.Thinking)
@@ -565,3 +566,11 @@ func (s *Session) Close() {
565566
store.Close()
566567
}
567568
}
569+
570+
// injectedUserMsg creates a user message marked as system-injected via metadata.
571+
// Auto-naming and session listing skip messages with this marker.
572+
func injectedUserMsg(text string) agentcore.Message {
573+
msg := agentcore.UserMsg(text)
574+
msg.Metadata = map[string]any{"injected": true}
575+
return msg
576+
}

internal/agent/session_state.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ func (p *sessionPersistence) tryAutoName() {
174174
if !ok || msg.Role != agentcore.RoleUser {
175175
continue
176176
}
177-
text := msg.TextContent()
177+
if msg.Metadata["injected"] == true {
178+
continue
179+
}
180+
text := lastTextBlock(msg)
178181
if text == "" {
179182
continue
180183
}
@@ -573,6 +576,19 @@ func truncateMessageText(msg agentcore.Message) agentcore.Message {
573576
}
574577
}
575578

579+
// lastTextBlock returns the text of the last ContentText block in msg.
580+
// In buildUserMessage, reminder blocks are prepended and the user's actual
581+
// input is always the final text block, so this extracts the real user text.
582+
func lastTextBlock(msg agentcore.Message) string {
583+
var last string
584+
for _, b := range msg.Content {
585+
if b.Type == agentcore.ContentText && b.Text != "" {
586+
last = b.Text
587+
}
588+
}
589+
return last
590+
}
591+
576592
func truncateBytes(b []byte, n int) string {
577593
if len(b) <= n {
578594
return string(b)

internal/bootstrap/boot_assembly.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ func buildRuntime(input *bootInput, spec *bootSpec) (*Runtime, error) {
480480
if err := ag.SetMessages(input.snapshot.Messages); err != nil {
481481
return nil, fmt.Errorf("restore agent messages: %w", err)
482482
}
483+
agentcore.ReactivateDeferred(spec.tools, input.snapshot.Messages)
483484
}
484485
if input.snapshot.Thinking != "" {
485486
ag.SetThinkingLevel(agentcore.ThinkingLevel(input.snapshot.Thinking))

internal/storage/session_manager.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,18 @@ func readSessionInfo(path string) (SessionInfo, error) {
148148
messageCount++
149149
if firstMessage == "" {
150150
var msg struct {
151-
Role string `json:"role"`
152-
Content []struct {
151+
Role string `json:"role"`
152+
Content []struct {
153153
Text string `json:"text"`
154154
} `json:"content"`
155+
Metadata map[string]any `json:"metadata,omitempty"`
155156
}
156-
if json.Unmarshal(e.Data, &msg) == nil && msg.Role == "user" {
157+
if json.Unmarshal(e.Data, &msg) == nil && msg.Role == "user" && msg.Metadata["injected"] != true {
158+
// Take the last text block: reminders are prepended,
159+
// the user's actual input is always the final text block.
157160
for _, c := range msg.Content {
158161
if c.Text != "" {
159162
firstMessage = c.Text
160-
break
161163
}
162164
}
163165
if len(firstMessage) > 80 {

internal/ui/tui/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
475475
return m, nil
476476
case "enter":
477477
m.acceptCompletion()
478-
return m, nil
478+
// Fall through to normal enter handling to execute the command directly.
479479
case "up":
480480
if m.compIdx > 0 {
481481
m.compIdx--

0 commit comments

Comments
 (0)