Skip to content

Commit 5ea7831

Browse files
cpcloudcursoragent
andcommitted
feat(mag): transform bare numbers in LLM prose, not just dollar amounts
magTransformText previously only matched prefixed amounts. Bare counts like "1" in "There is 1 flooring project" were left untouched. Now uses a combined regex that matches both dollar amounts and standalone numbers in a single pass (so replacement digits like the 4 in 🠡4 are never re-matched). refs #195 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent e75da94 commit 5ea7831

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

internal/app/mag.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,22 @@ func magOptionalCents(cents *int64) string {
8282
return magCents(*cents)
8383
}
8484

85-
// magMoneyRe matches dollar amounts like $1,234.56 or -$5.00 in prose.
86-
var magMoneyRe = regexp.MustCompile(`-?\$[\d,]+(?:\.\d+)?`)
87-
88-
// magTransformText replaces dollar amounts in free-form text with magnitude
89-
// notation. Used to post-process LLM responses when mag mode is on.
90-
// Does not pad (no width preservation needed in prose).
85+
// magTextRe matches dollar amounts ($1,234.56, -$5.00) and standalone bare
86+
// numbers (42, 1,000, 3.14) in prose. Dollar amounts are tried first via
87+
// alternation so their digits aren't consumed by the bare-number branch.
88+
// A single-pass replace ensures output digits (like the 4 in 🠡4) are never
89+
// re-matched.
90+
var magTextRe = regexp.MustCompile(`-?\$[\d,]+(?:\.\d+)?|\b\d[\d,]*(?:\.\d+)?\b`)
91+
92+
// magTransformText replaces dollar amounts and bare numbers in free-form
93+
// text with magnitude notation. Used to post-process LLM responses when
94+
// mag mode is on.
9195
func magTransformText(s string) string {
92-
return magMoneyRe.ReplaceAllStringFunc(s, func(match string) string {
93-
c := cell{Value: match, Kind: cellMoney}
94-
result := magFormat(c, true)
95-
// Strip leading padding -- prose doesn't need right-alignment.
96-
return strings.TrimLeft(result, " ")
96+
return magTextRe.ReplaceAllStringFunc(s, func(match string) string {
97+
if strings.ContainsRune(match, '$') {
98+
return magFormat(cell{Value: match, Kind: cellMoney}, true)
99+
}
100+
return magFormat(cell{Value: match, Kind: cellDrilldown}, false)
97101
})
98102
}
99103

internal/app/mag_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func TestMagTransformText(t *testing.T) {
173173
"Loss of -$ \U0001F8213 this month.",
174174
},
175175
{
176-
"no amounts",
176+
"no amounts or numbers",
177177
"The project is underway.",
178178
"The project is underway.",
179179
},
@@ -182,6 +182,26 @@ func TestMagTransformText(t *testing.T) {
182182
"Just $5.00.",
183183
"Just $ \U0001F8211.",
184184
},
185+
{
186+
"bare count",
187+
"There is 1 flooring project.",
188+
"There is \U0001F8210 flooring project.",
189+
},
190+
{
191+
"larger bare count",
192+
"You have 42 maintenance items.",
193+
"You have \U0001F8212 maintenance items.",
194+
},
195+
{
196+
"bare number with commas",
197+
"Total is 1,000 items.",
198+
"Total is \U0001F8213 items.",
199+
},
200+
{
201+
"mixed dollars and bare numbers",
202+
"Found 3 projects totaling $15,000.00.",
203+
"Found \U0001F8210 projects totaling $ \U0001F8214.",
204+
},
185205
}
186206
for _, tt := range tests {
187207
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)