Skip to content

Commit 16fb27d

Browse files
committed
fix: address greptile review — trim persistence output, restore tests, move JSDoc
- stripPersistedAssistantText: add .trim() to prevent whitespace-only assistant messages after malformed-prefix cleanup (Greptile P2) - Restore unit tests for byteSize and isThinkingPart removed during rewrite (Greptile P2) - Move file-level JSDoc after import in text-complete.ts (Greptile P2) Related to #97
1 parent bafa59a commit 16fb27d

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

packages/plugin/src/hooks/magic-context/tag-content-primitives.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, expect, it } from "bun:test";
22

33
import {
4+
byteSize,
5+
isThinkingPart,
46
peelLeadingMcTagNotation,
57
prependTag,
68
stripPersistedAssistantText,
@@ -140,3 +142,53 @@ describe("peelLeadingMcTagNotation", () => {
140142
});
141143
});
142144
});
145+
146+
describe("stripPersistedAssistantText edge cases", () => {
147+
it("#given malformed prefix with trailing space #when strip runs #then trims result", () => {
148+
expect(stripPersistedAssistantText(`${SECTION}15298">§15298§ `)).toBe("");
149+
});
150+
151+
it("#given tag-only text with trailing space #when strip runs #then returns empty", () => {
152+
expect(stripPersistedAssistantText(`${SECTION}42${SECTION} `)).toBe("");
153+
});
154+
155+
it("#given whitespace-only after strip #when strip runs #then trims to empty", () => {
156+
expect(stripPersistedAssistantText(` `)).toBe("");
157+
});
158+
});
159+
160+
describe("byteSize", () => {
161+
it("#given ascii string #when byteSize runs #then returns byte length", () => {
162+
expect(byteSize("hello")).toBe(5);
163+
});
164+
165+
it("#given empty string #when byteSize runs #then returns 0", () => {
166+
expect(byteSize("")).toBe(0);
167+
});
168+
169+
it("#given multibyte string #when byteSize runs #then returns encoded byte length", () => {
170+
expect(byteSize("§42§")).toBe(6);
171+
});
172+
});
173+
174+
describe("isThinkingPart", () => {
175+
it("#given thinking part #when isThinkingPart runs #then returns true", () => {
176+
expect(isThinkingPart({ type: "thinking", thinking: "..." })).toBe(true);
177+
});
178+
179+
it("#given reasoning part #when isThinkingPart runs #then returns true", () => {
180+
expect(isThinkingPart({ type: "reasoning", reasoning: "..." })).toBe(true);
181+
});
182+
183+
it("#given text part #when isThinkingPart runs #then returns false", () => {
184+
expect(isThinkingPart({ type: "text", text: "hello" })).toBe(false);
185+
});
186+
187+
it("#given null #when isThinkingPart runs #then returns false", () => {
188+
expect(isThinkingPart(null)).toBe(false);
189+
});
190+
191+
it("#given primitive #when isThinkingPart runs #then returns false", () => {
192+
expect(isThinkingPart("string")).toBe(false);
193+
});
194+
});

packages/plugin/src/hooks/magic-context/tag-content-primitives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function stripPersistedAssistantText(value: string): string {
6565
text = stripCompleteTagPairsGlobally(text);
6666
text = stripMalformedTagNotationGlobally(text);
6767
text = stripTagSectionCharacters(text);
68-
return text;
68+
return text.trim();
6969
}
7070

7171
export function byteSize(value: string): number {

packages/plugin/src/hooks/magic-context/text-complete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stripPersistedAssistantText } from "./tag-content-primitives";
2+
13
/**
24
* Persistence-boundary strip for assistant completions (`experimental.text.complete`).
35
*
@@ -22,8 +24,6 @@
2224
* sentinels like `[dropped §N§]`.
2325
*/
2426

25-
import { stripPersistedAssistantText } from "./tag-content-primitives";
26-
2727
export function createTextCompleteHandler() {
2828
return async (
2929
_input: { sessionID: string; messageID: string; partID: string },

0 commit comments

Comments
 (0)