MCPcopy Index your code
hub / github.com/simstudioai/sim / buildChunks

Function buildChunks

apps/sim/lib/chunkers/utils.ts:97–129  ·  view source on GitHub ↗
(texts: string[], overlapTokens: number)

Source from the content-addressed store, hash-verified

95}
96
97export function buildChunks(texts: string[], overlapTokens: number): Chunk[] {
98 let previousEndIndex = 0
99 const overlapChars = tokensToChars(overlapTokens)
100
101 return texts.map((text, index) => {
102 let startIndex: number
103 let actualContentLength: number
104
105 if (index === 0 || overlapTokens <= 0) {
106 startIndex = previousEndIndex
107 actualContentLength = text.length
108 } else {
109 const prevChunk = texts[index - 1]
110 const overlapLength = Math.min(overlapChars, prevChunk.length, text.length)
111 startIndex = previousEndIndex - overlapLength
112 actualContentLength = text.length - overlapLength
113 }
114
115 const safeStart = Math.max(0, startIndex)
116 const endIndex = safeStart + Math.max(0, actualContentLength)
117
118 previousEndIndex = endIndex
119
120 return {
121 text,
122 tokenCount: estimateTokens(text),
123 metadata: {
124 startIndex: safeStart,
125 endIndex,
126 },
127 }
128 })
129}
130
131export function resolveChunkerOptions(options: {
132 chunkSize?: number

Callers 6

chunkMethod · 0.90
chunkMethod · 0.90
chunkMethod · 0.90
utils.test.tsFile · 0.90
chunkMethod · 0.90
chunkMethod · 0.90

Calls 2

tokensToCharsFunction · 0.85
estimateTokensFunction · 0.85

Tested by

no test coverage detected