xonecas.io
62159
Sy

symb / commit e4a5f21

feat: use @ to reference a file in your messages

xonecas2026-02-19 e4a5f212e6f308b6c4b4a8a00d89157ed671281f parent 05d013d

9 files changed

TODO.md+7−0
@@ -0,0 +1,7 @@
1+- [ ]: Investigate:
2+
3+Timeout shouldn't interrupt subagents.
4+
5+```
6+← sub-agent failed: LLM stream failed: context deadline exceeded (Client.Timeout or context cancellation while reading body)
7+```
docs/DESIGN.md+2−0
@@ -37,9 +37,11 @@ delete, paste, tab indent)
3737
3838 Agent input, simple multiline textarea with markdown highlighting.
3939
40+// TODO:
4041 Use a special key `@` to spawn an autocomplete/filesearch modal. This can match files, skills, commands or subagents.
4142 When the user selects a match with `Enter` it replaces the @ with the selected item once the message is sent to the, includes hashes when injected into the user message.
4243 Start with file search only for the initial implementation.
44+Remove the ctrl+f keybind
4345
4446 ### Search modal
4547
internal/tui/atexpand.go+32−0
@@ -0,0 +1,32 @@
1+package tui
2+
3+import (
4+ "fmt"
5+ "os"
6+ "regexp"
7+ "strings"
8+
9+ "github.com/xonecas/symb/internal/hashline"
10+)
11+
12+// atMentionRe matches @path tokens, where path is any non-whitespace sequence.
13+var atMentionRe = regexp.MustCompile(`@(\S+)`)
14+
15+// expandAtMentions replaces @path tokens in the input string with the
16+// hashline-tagged contents of the referenced file. If the file cannot be
17+// read, the token is left as-is.
18+func expandAtMentions(input string) string {
19+ return atMentionRe.ReplaceAllStringFunc(input, func(match string) string {
20+ path := match[1:] // strip leading @
21+ data, err := os.ReadFile(path)
22+ if err != nil {
23+ return match // leave token intact
24+ }
25+ content := strings.TrimRight(string(data), "\n")
26+ tagged := hashline.TagLines(content, 1)
27+ var sb strings.Builder
28+ fmt.Fprintf(&sb, "@%s\n", path)
29+ sb.WriteString(hashline.FormatTagged(tagged))
30+ return sb.String()
31+ })
32+}
internal/tui/editor/editor.go+10−0
@@ -219,6 +219,16 @@ func (m *Model) textInRange(start, end pos) string {
219219 return sb.String()
220220 }
221221
222+// CursorOffset returns the rune offset of the cursor from the beginning of the document.
223+func (m Model) CursorOffset() int {
224+ offset := 0
225+ for i := 0; i < m.row && i < len(m.lines); i++ {
226+ offset += len(m.lines[i]) + 1 // +1 for newline
227+ }
228+ offset += m.col
229+ return offset
230+}
231+
222232 // DeleteSelection removes the selected text and positions the cursor at the
223233 // start of the deleted range. Returns true if a selection was deleted.
224234 func (m *Model) DeleteSelection() bool {
internal/tui/messages.go+12−3
@@ -22,7 +22,10 @@ import (
2222 // ELM messages
2323 // ---------------------------------------------------------------------------
2424
25-type llmUserMsg struct{ content string }
25+type llmUserMsg struct {
26+ display string // raw text shown in conversation (with @tokens)
27+ content string // expanded text sent to LLM (@ tokens replaced with file content)
28+}
2629
2730 type llmAssistantMsg struct {
2831 reasoning string
@@ -155,8 +158,8 @@ func gitBranchTick() tea.Cmd {
155158 })
156159 }
157160
158-func (m Model) sendToLLM(userInput string) tea.Cmd {
159- return func() tea.Msg { return llmUserMsg{content: userInput} }
161+func (m Model) sendToLLM(display, content string) tea.Cmd {
162+ return func() tea.Msg { return llmUserMsg{display: display, content: content} }
160163 }
161164
162165 func (m Model) waitForLLMUpdate() tea.Cmd {
@@ -229,6 +232,12 @@ func (m Model) runLLMTurn(deps llmTurnDeps, extra []provider.Message) {
229232 }
230233 history = ensureSystemMessage(history, deps.systemMsg)
231234 if len(extra) > 0 {
235+ // extra carries the current user message in its expanded form. The DB
236+ // already saved the display form as the last entry, so trim it to avoid
237+ // sending a duplicate to the LLM.
238+ if n := len(history); n > 0 && history[n-1].Role == "user" {
239+ history = history[:n-1]
240+ }
232241 history = append(history, extra...)
233242 }
234243
internal/tui/tui.go+1−0
@@ -203,6 +203,7 @@ type Model struct {
203203
204204 // File finder modal
205205 fileModal *modal.Model
206+ atOffset int // rune offset where @ was typed (for file modal replacement)
206207 // Keybinds modal
207208 keybindsModal *modal.Model
208209 // Models modal
internal/tui/update_keypress.go+11−8
@@ -21,7 +21,7 @@ func (m *Model) keyPressHandlers() map[string]func(*Model) (Model, tea.Cmd, bool
2121 "ctrl+shift+v": (*Model).handleCtrlShiftV,
2222 "esc": (*Model).handleEsc,
2323 "enter": (*Model).handleEnter,
24- "ctrl+f": (*Model).handleCtrlF,
24+ "@": (*Model).handleAtSign,
2525 "ctrl+h": (*Model).handleCtrlH,
2626 "ctrl+m": (*Model).handleCtrlM,
2727 }
@@ -65,19 +65,22 @@ func (m *Model) cancelProgramCmd() tea.Cmd {
6565
6666 func (m *Model) handleEnter() (Model, tea.Cmd, bool) {
6767 if m.agentInput.Value() != "" && m.turnCancel == nil && !m.turnPending && !m.undoInFlight {
68- userMsg := m.agentInput.Value()
68+ display := m.agentInput.Value()
6969 m.agentInput.Reset()
70- return *m, m.sendToLLM(userMsg), true
70+ return *m, m.sendToLLM(display, expandAtMentions(display)), true
7171 }
7272 return Model{}, nil, false
7373 }
7474
75-func (m *Model) handleCtrlF() (Model, tea.Cmd, bool) {
76- if m.searcher != nil {
77- m.openFileModal()
78- return *m, nil, true
75+func (m *Model) handleAtSign() (Model, tea.Cmd, bool) {
76+ if m.searcher == nil || !m.agentInput.Focused() {
77+ return Model{}, nil, false
7978 }
80- return *m, nil, false
79+ m.agentInput.DeleteSelection()
80+ m.agentInput.InsertText("@")
81+ m.atOffset = m.agentInput.CursorOffset() // offset is now just after the @
82+ m.openFileModal()
83+ return *m, nil, true
8184 }
8285
8386 func (m *Model) handleCtrlH() (Model, tea.Cmd, bool) {
internal/tui/update_llm.go+14−12
@@ -23,7 +23,8 @@ func (m Model) handleLLMUser(msg llmUserMsg) (tea.Model, tea.Cmd) {
2323 // handleUserMsg records a user message in the conversation display.
2424 func (m *Model) handleUserMsg(msg llmUserMsg) (Model, tea.Cmd) {
2525 now := time.Now()
26- userMsg := provider.Message{Role: "user", Content: msg.content, CreatedAt: now}
26+ llmMsg := provider.Message{Role: "user", Content: msg.content, CreatedAt: now}
27+ storeMsg := provider.Message{Role: "user", Content: msg.display, CreatedAt: now}
2728
2829 convIdx := len(m.convEntries)
2930
@@ -39,7 +40,7 @@ func (m *Model) handleUserMsg(msg llmUserMsg) (Model, tea.Cmd) {
3940 })
4041
4142 m.appendText("")
42- m.appendText(highlightMarkdown(msg.content, m.styles.Text)...)
43+ m.appendText(highlightMarkdown(msg.display, m.styles.Text)...)
4344 wasBottom := m.appendText("")
4445 m.turnInputTokens = 0
4546 m.turnOutputTokens = 0
@@ -47,24 +48,24 @@ func (m *Model) handleUserMsg(msg llmUserMsg) (Model, tea.Cmd) {
4748 if wasBottom {
4849 m.scrollOffset = 0
4950 }
50- cmd := m.saveUserMessageCmd(userMsg, convIdx)
51+ cmd := m.saveUserMessageCmd(llmMsg, storeMsg, convIdx)
5152 return *m, cmd
5253 }
5354
54-func (m *Model) saveUserMessageCmd(userMsg provider.Message, convIdx int) tea.Cmd {
55+func (m *Model) saveUserMessageCmd(llmMsg, storeMsg provider.Message, convIdx int) tea.Cmd {
5556 store := m.store
5657 sessionID := m.sessionID
5758 if store == nil {
5859 return func() tea.Msg {
59- return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: userMsg}
60+ return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: llmMsg}
6061 }
6162 }
6263 return func() tea.Msg {
63- id, err := store.SaveMessageSync(sessionID, messageToStore(userMsg))
64+ id, err := store.SaveMessageSync(sessionID, messageToStore(storeMsg))
6465 if err != nil {
65- return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: userMsg, err: err}
66+ return userMsgSavedMsg{convIdx: convIdx, dbMsgID: 0, userMsg: llmMsg, err: err}
6667 }
67- return userMsgSavedMsg{convIdx: convIdx, dbMsgID: id, userMsg: userMsg}
68+ return userMsgSavedMsg{convIdx: convIdx, dbMsgID: id, userMsg: llmMsg}
6869 }
6970 }
7071
@@ -84,10 +85,11 @@ func (m Model) handleUserMsgSaved(msg userMsgSavedMsg) (tea.Model, tea.Cmd) {
8485 }
8586 m.llmInFlight = true
8687 m.turnCtx, m.turnCancel = context.WithCancel(context.Background())
87- var extra []provider.Message
88- if msg.dbMsgID == 0 {
89- extra = append(extra, msg.userMsg)
90- }
88+ // Always supply the current user message via extra so the LLM receives the
89+ // expanded form (@ mentions replaced with file content). When the store is
90+ // present the display form was saved to DB; we need to exclude it from the
91+ // loaded history to avoid sending a duplicate.
92+ extra := []provider.Message{msg.userMsg}
9193 return m, tea.Batch(m.processLLMWithExtra(extra), m.waitForLLMUpdate())
9294 }
9395
internal/tui/update_modals.go+11−4
@@ -30,7 +30,7 @@ func (m *Model) openFileModal() {
3030 }
3131 return items
3232 }
33- md := modal.New(searchFn, "Open: ", modal.Colors{
33+ md := modal.New(searchFn, "File: ", modal.Colors{
3434 Fg: palette.Fg,
3535 Bg: palette.Bg,
3636 Dim: palette.Dim,
@@ -46,7 +46,7 @@ func (m *Model) openFileModal() {
4646 func (m *Model) openKeybindsModal() {
4747 items := []modal.Item{
4848 {Name: "ctrl+h", Desc: "keybinds"},
49- {Name: "ctrl+f", Desc: "file search"},
49+ {Name: "@", Desc: "file search"},
5050 {Name: "ctrl+m", Desc: "switch model"},
5151 {Name: "ctrl+shift+c", Desc: "copy selection"},
5252 {Name: "ctrl+shift+v", Desc: "paste"},
@@ -118,9 +118,16 @@ func (m *Model) updateFileModal(msg tea.Msg) (Model, tea.Cmd, bool) {
118118 return *m, nil, false
119119 }
120120 action, cmd := m.fileModal.HandleMsg(msg)
121- switch action.(type) {
122- case modal.ActionClose, modal.ActionSelect:
121+ switch a := action.(type) {
122+ case modal.ActionClose:
123+ m.fileModal = nil
124+ m.atOffset = 0
125+ return *m, nil, true
126+ case modal.ActionSelect:
123127 m.fileModal = nil
128+ m.atOffset = 0
129+ m.agentInput.InsertText(a.Item.Name)
130+ m.agentInput.Focus()
124131 return *m, nil, true
125132 }
126133 if cmd != nil {