Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/utils/__tests__/tiktoken.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ describe("tiktoken", () => {
expect(result).toBe(0)
})

it("should not throw on text content with special tokens", async () => {
const content: Anthropic.Messages.ContentBlockParam[] = [
{ type: "text", text: "something<|endoftext|>something" },
]

const result = await tiktoken(content)
expect(result).toBeGreaterThan(0)
})

it("should handle missing text content", async () => {
// Using 'as any' to bypass TypeScript's type checking for this test case
// since we're specifically testing how the function handles undefined text
Expand Down
2 changes: 1 addition & 1 deletion src/utils/tiktoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function tiktoken(content: Anthropic.Messages.ContentBlockParam[]):
const text = block.text || ""

if (text.length > 0) {
const tokens = encoder.encode(text)
const tokens = encoder.encode(text, undefined, [])
totalTokens += tokens.length
}
} else if (block.type === "image") {
Expand Down
Loading