Compare commits

...
Author SHA1 Message Date
Aiden Cline 0597d225a3 ignore: add truncation namespace 2026-01-07 01:24:50 -06:00
3 changed files with 140 additions and 0 deletions

No files matched your search

@@ -0,0 +1,60 @@
export namespace Truncate {
export const MAX_LINES = 2000
export const MAX_BYTES = 50 * 1024
export interface Result {
content: string
truncated: boolean
}
export interface Options {
maxLines?: number
maxBytes?: number
direction?: "head" | "tail"
}
export function output(text: string, options: Options = {}): Result {
const maxLines = options.maxLines ?? MAX_LINES
const maxBytes = options.maxBytes ?? MAX_BYTES
const direction = options.direction ?? "head"
const lines = text.split("\n")
const totalBytes = Buffer.byteLength(text, "utf-8")
if (lines.length <= maxLines && totalBytes <= maxBytes) {
return { content: text, truncated: false }
}
const out: string[] = []
var i = 0
var bytes = 0
var hitBytes = false
if (direction === "head") {
for (i = 0; i < lines.length && i < maxLines; i++) {
const size = Buffer.byteLength(lines[i], "utf-8") + (i > 0 ? 1 : 0)
if (bytes + size > maxBytes) {
hitBytes = true
break
}
out.push(lines[i])
bytes += size
}
const removed = hitBytes ? totalBytes - bytes : lines.length - out.length
const unit = hitBytes ? "chars" : "lines"
return { content: `${out.join("\n")}\n\n...${removed} ${unit} truncated...`, truncated: true }
}
for (i = lines.length - 1; i >= 0 && out.length < maxLines; i--) {
const size = Buffer.byteLength(lines[i], "utf-8") + (out.length > 0 ? 1 : 0)
if (bytes + size > maxBytes) {
hitBytes = true
break
}
out.unshift(lines[i])
bytes += size
}
const removed = hitBytes ? totalBytes - bytes : lines.length - out.length
const unit = hitBytes ? "chars" : "lines"
return { content: `...${removed} ${unit} truncated...\n\n${out.join("\n")}`, truncated: true }
}
}
@@ -0,0 +1 @@
{"moonshotai-cn":{"id":"moonshotai-cn","env":["MOONSHOT_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://api.moonshot.cn/v1","name":"Moonshot AI (China)","doc":"https://platform.moonshot.cn/docs/api/chat","models":{"kimi-k2-thinking-turbo":{"id":"kimi-k2-thinking-turbo","name":"Kimi K2 Thinking Turbo","family":"kimi-k2","attachment":false,"reasoning":true,"tool_call":true,"temperature":true,"knowledge":"2024-08","release_date":"2025-11-06","last_updated":"2025-11-06","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":1.15,"output":8,"cache_read":0.15},"limit":{"context":262144,"output":262144}},"kimi-k2-thinking":{"id":"kimi-k2-thinking","name":"Kimi K2 Thinking","family":"kimi-k2","attachment":false,"reasoning":true,"tool_call":true,"temperature":true,"knowledge":"2024-08","release_date":"2025-11-06","last_updated":"2025-11-06","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":0.6,"output":2.5,"cache_read":0.15},"limit":{"context":262144,"output":262144}},"kimi-k2-0905-preview":{"id":"kimi-k2-0905-preview","name":"Kimi K2 0905","family":"kimi-k2","attachment":false,"reasoning":false,"tool_call":true,"temperature":true,"knowledge":"2024-10","release_date":"2025-09-05","last_updated":"2025-09-05","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":0.6,"output":2.5,"cache_read":0.15},"limit":{"context":262144,"output":262144}},"kimi-k2-0711-preview":{"id":"kimi-k2-0711-preview","name":"Kimi K2 0711","family":"kimi-k2","attachment":false,"reasoning":false,"tool_call":true,"temperature":true,"knowledge":"2024-10","release_date":"2025-07-14","last_updated":"2025-07-14","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":0.6,"output":2.5,"cache_read":0.15},"limit":{"context":131072,"output":16384}},"kimi-k2-turbo-preview":{"id":"kimi-k2-turbo-preview","name":"Kimi K2 Turbo","family":"kimi-k2","attachment":false,"reasoning":false,"tool_call":true,"temperature":true,"knowledge":"2024-10","release_date":"2025-09-05","last_updated":"2025-09-05","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":2.4,"output":10,"cache_read":0.6},"limit":{"context":262144,"output":262144}}}},"lucidquery":{"id":"lucidquery","env":["LUCIDQUERY_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://lucidquery.com/api/v1","name":"LucidQuery AI","doc":"https://lucidquery.com/api/docs","models":{"lucidquery-nexus-coder":{"id":"lucidquery-nexus-coder","name":"LucidQuery Nexus Coder","family":"lucidquery-nexus-coder","attachment":true,"reasoning":true,"tool_call":true,"temperature":false,"knowledge":"2025-08-01","release_date":"2025-09-01","last_updated":"2025-09-01","modalities":{"input":["text"],"output":["text"]},"open_weights":false,"cost":{"input":2,"output":5},"limit":{"context":250000,"output":60000}},"lucidnova-rf1-100b":{"id":"lucidnova-rf1-100b","name":"LucidNova RF1 100B","family":"nova","attachment":true,"reasoning":true,"tool_call":true,"temperature":false,"knowledge":"2025-09-16","release_date":"2024-12-28","last_updated":"2025-09-10","modalities":{"input":["text"],"output":["text"]},"open_weights":false,"cost":{"input":2,"output":5},"limit":{"context":120000,"output":8000}}}},"moonshotai":{"id":"moonshotai","env":["MOONSHOT_API_KEY"],"npm":"@ai-sdk/openai-compatible","api":"https://api.moonshot.ai/v1","name":"Moonshot AI","doc":"https://platform.moonshot.ai/docs/api/chat","models":{"kimi-k2-thinking-turbo":{"id":"kimi-k2-thinking-turbo","name":"Kimi K2 Thinking Turbo","family":"kimi-k2","attachment":false,"reasoning":true,"tool_call":true,"interleaved":{"field":"reasoning_content"},"temperature":true,"knowledge":"2024-08","release_date":"2025-11-06","last_updated":"2025-11-06","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":1.15,"output":8,"cache_read":0.15},"limit":{"context":262144,"output":262144}},"kimi-k2-turbo-preview":{"id":"kimi-k2-turbo-preview","name":"Kimi K2 Turbo","family":"kimi-k2","attachment":false,"reasoning":false,"tool_call":true,"temperature":true,"knowledge":"2024-10","release_date":"2025-09-05","last_updated":"2025-09-05","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":2.4,"output":10,"cache_read":0.6},"limit":{"context":262144,"output":262144}},"kimi-k2-0711-preview":{"id":"kimi-k2-0711-preview","name":"Kimi K2 0711","family":"kimi-k2","attachment":false,"reasoning":false,"tool_call":true,"temperature":true,"knowledge":"2024-10","release_date":"2025-07-14","last_updated":"2025-07-14","modalities":{"input":["text"],"output":["text"]},"open_weights":true,"cost":{"input":0.6,"output":2.5,"cache_read":0.15},"limit":{"context":131072,"output":16384}},"kimi-k2-thinking":{"id":"kimi-k2-thinking","name":"Kimi K2 Thinking","family":"kimi-k2","attachment":false,"reasoning":true,"tool_call":true,"interleaved":{"field":"reasoning_content"},"temperature":trueLine truncated
@@ -0,0 +1,79 @@
import { describe, test, expect } from "bun:test"
import { Truncate } from "../../src/session/truncation"
import path from "path"
const FIXTURES_DIR = path.join(import.meta.dir, "fixtures")
describe("Truncate", () => {
describe("output", () => {
test("truncates large json file by bytes", async () => {
const content = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
const result = Truncate.output(content)
expect(result.truncated).toBe(true)
expect(Buffer.byteLength(result.content, "utf-8")).toBeLessThanOrEqual(Truncate.MAX_BYTES + 100)
expect(result.content).toContain("truncated...")
})
test("returns content unchanged when under limits", () => {
const content = "line1\nline2\nline3"
const result = Truncate.output(content)
expect(result.truncated).toBe(false)
expect(result.content).toBe(content)
})
test("truncates by line count", () => {
const lines = Array.from({ length: 100 }, (_, i) => `line${i}`).join("\n")
const result = Truncate.output(lines, { maxLines: 10 })
expect(result.truncated).toBe(true)
expect(result.content.split("\n").length).toBeLessThanOrEqual(12)
expect(result.content).toContain("...90 lines truncated...")
})
test("truncates by byte count", () => {
const content = "a".repeat(1000)
const result = Truncate.output(content, { maxBytes: 100 })
expect(result.truncated).toBe(true)
expect(result.content).toContain("truncated...")
})
test("truncates from head by default", () => {
const lines = Array.from({ length: 10 }, (_, i) => `line${i}`).join("\n")
const result = Truncate.output(lines, { maxLines: 3 })
expect(result.truncated).toBe(true)
expect(result.content).toContain("line0")
expect(result.content).toContain("line1")
expect(result.content).toContain("line2")
expect(result.content).not.toContain("line9")
})
test("truncates from tail when direction is tail", () => {
const lines = Array.from({ length: 10 }, (_, i) => `line${i}`).join("\n")
const result = Truncate.output(lines, { maxLines: 3, direction: "tail" })
expect(result.truncated).toBe(true)
expect(result.content).toContain("line7")
expect(result.content).toContain("line8")
expect(result.content).toContain("line9")
expect(result.content).not.toContain("line0")
})
test("uses default MAX_LINES and MAX_BYTES", () => {
expect(Truncate.MAX_LINES).toBe(2000)
expect(Truncate.MAX_BYTES).toBe(50 * 1024)
})
test("large single-line file truncates with byte message", async () => {
const content = await Bun.file(path.join(FIXTURES_DIR, "models-api.json")).text()
const result = Truncate.output(content)
expect(result.truncated).toBe(true)
expect(result.content).toContain("chars truncated...")
expect(Buffer.byteLength(content, "utf-8")).toBeGreaterThan(Truncate.MAX_BYTES)
})
})
})