6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
144 lines
3.7 KiB
TypeScript
144 lines
3.7 KiB
TypeScript
import type { Chunk } from '@/lib/chunkers/types'
|
|
|
|
/** 1 token ≈ 4 characters for English text */
|
|
export function estimateTokens(text: string): number {
|
|
if (!text?.trim()) return 0
|
|
return Math.ceil(text.length / 4)
|
|
}
|
|
|
|
export function tokensToChars(tokens: number): number {
|
|
return tokens * 4
|
|
}
|
|
|
|
export function cleanText(text: string): string {
|
|
return text
|
|
.replace(/\r\n/g, '\n')
|
|
.replace(/\r/g, '\n')
|
|
.replace(/\n{3,}/g, '\n\n')
|
|
.replace(/\t/g, ' ')
|
|
.replace(/ {2,}/g, ' ')
|
|
.trim()
|
|
}
|
|
|
|
export function addOverlap(chunks: string[], overlapChars: number): string[] {
|
|
if (overlapChars <= 0 || chunks.length <= 1) {
|
|
return chunks
|
|
}
|
|
|
|
const result: string[] = []
|
|
|
|
for (let i = 0; i < chunks.length; i++) {
|
|
let chunk = chunks[i]
|
|
|
|
if (i > 0) {
|
|
const prevChunk = chunks[i - 1]
|
|
const overlapLength = Math.min(overlapChars, prevChunk.length)
|
|
const overlapText = prevChunk.slice(-overlapLength)
|
|
|
|
const wordBoundaryMatch = overlapText.match(/^\s*\S/)
|
|
const cleanOverlap = wordBoundaryMatch
|
|
? overlapText.slice(overlapText.indexOf(wordBoundaryMatch[0].trim()))
|
|
: overlapText
|
|
|
|
if (cleanOverlap.trim()) {
|
|
chunk = `${cleanOverlap.trim()} ${chunk}`
|
|
}
|
|
}
|
|
|
|
result.push(chunk)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
/**
|
|
* When stepChars is provided (< chunkSizeChars), produces overlapping chunks
|
|
* using a sliding window where chunks stay within the size limit.
|
|
*/
|
|
export function splitAtWordBoundaries(
|
|
text: string,
|
|
chunkSizeChars: number,
|
|
stepChars?: number
|
|
): string[] {
|
|
const parts: string[] = []
|
|
let pos = 0
|
|
|
|
while (pos < text.length) {
|
|
let end = Math.min(pos + chunkSizeChars, text.length)
|
|
|
|
if (end < text.length) {
|
|
const lastSpace = text.lastIndexOf(' ', end)
|
|
if (lastSpace > pos) {
|
|
end = lastSpace
|
|
}
|
|
}
|
|
|
|
const part = text.slice(pos, end).trim()
|
|
if (part) {
|
|
parts.push(part)
|
|
}
|
|
|
|
if (stepChars !== undefined) {
|
|
// Sliding window: advance by step for predictable overlap
|
|
const nextPos = pos + Math.max(1, stepChars)
|
|
if (nextPos >= text.length) break
|
|
pos = nextPos
|
|
} else {
|
|
// Non-overlapping: advance from end of extracted content
|
|
if (end >= text.length) break
|
|
pos = end
|
|
}
|
|
while (pos < text.length && text[pos] === ' ') pos++
|
|
}
|
|
|
|
return parts
|
|
}
|
|
|
|
export function buildChunks(texts: string[], overlapTokens: number): Chunk[] {
|
|
let previousEndIndex = 0
|
|
const overlapChars = tokensToChars(overlapTokens)
|
|
|
|
return texts.map((text, index) => {
|
|
let startIndex: number
|
|
let actualContentLength: number
|
|
|
|
if (index === 0 || overlapTokens <= 0) {
|
|
startIndex = previousEndIndex
|
|
actualContentLength = text.length
|
|
} else {
|
|
const prevChunk = texts[index - 1]
|
|
const overlapLength = Math.min(overlapChars, prevChunk.length, text.length)
|
|
startIndex = previousEndIndex - overlapLength
|
|
actualContentLength = text.length - overlapLength
|
|
}
|
|
|
|
const safeStart = Math.max(0, startIndex)
|
|
const endIndex = safeStart + Math.max(0, actualContentLength)
|
|
|
|
previousEndIndex = endIndex
|
|
|
|
return {
|
|
text,
|
|
tokenCount: estimateTokens(text),
|
|
metadata: {
|
|
startIndex: safeStart,
|
|
endIndex,
|
|
},
|
|
}
|
|
})
|
|
}
|
|
|
|
export function resolveChunkerOptions(options: {
|
|
chunkSize?: number
|
|
chunkOverlap?: number
|
|
minCharactersPerChunk?: number
|
|
}): { chunkSize: number; chunkOverlap: number; minCharactersPerChunk: number } {
|
|
const chunkSize = options.chunkSize ?? 1024
|
|
const maxOverlap = Math.floor(chunkSize * 0.5)
|
|
return {
|
|
chunkSize,
|
|
chunkOverlap: Math.min(options.chunkOverlap ?? 0, maxOverlap),
|
|
minCharactersPerChunk: options.minCharactersPerChunk ?? 100,
|
|
}
|
|
}
|