MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / fetchMessageContext

Function fetchMessageContext

packages/core/src/query/message-query-functions.ts:154–188  ·  view source on GitHub ↗
(
  executor: AsyncSqlExecutor,
  messageIds: number | number[],
  contextSize: number = 20
)

Source from the content-addressed store, hash-verified

152 * Uses simple id-based ordering (not session-aware).
153 */
154export async function fetchMessageContext(
155 executor: AsyncSqlExecutor,
156 messageIds: number | number[],
157 contextSize: number = 20
158): Promise<MappedMessage[]> {
159 const ids = Array.isArray(messageIds) ? messageIds : [messageIds]
160 if (ids.length === 0) return []
161
162 const allIds = new Set<number>()
163
164 for (const id of ids) {
165 allIds.add(id)
166 if (contextSize > 0) {
167 const before = await executor.all<{ id: number }>(
168 'SELECT id FROM message WHERE id < ? ORDER BY id DESC LIMIT ?',
169 [id, contextSize]
170 )
171 before.forEach((r) => allIds.add(r.id))
172
173 const after = await executor.all<{ id: number }>('SELECT id FROM message WHERE id > ? ORDER BY id ASC LIMIT ?', [
174 id,
175 contextSize,
176 ])
177 after.forEach((r) => allIds.add(r.id))
178 }
179 }
180
181 const idList = Array.from(allIds).sort((a, b) => a - b)
182 if (idList.length === 0) return []
183
184 const placeholders = idList.map(() => '?').join(', ')
185 const sql = `${FULL_MSG_SELECT} WHERE msg.id IN (${placeholders}) ORDER BY msg.id ASC`
186 const rows = await executor.all<FullMessageRow>(sql, idList)
187 return rows.map(mapMessageRow)
188}
189
190/**
191 * Get context messages around search results.

Callers 3

getMessageContextFunction · 0.90
getMessageContextMethod · 0.90

Calls 1

addMethod · 0.45

Tested by

no test coverage detected