@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
59 lines • 2.22 kB
JavaScript
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. =========
import { offsetPages } from "./paginate.js";
const PAGE_SIZE = 25;
function flattenContexts(contexts) {
const out = [];
for (const ctx of contexts) {
if (ctx.length > 0 && ctx[0] !== undefined)
out.push(ctx[0]);
}
return out;
}
async function* searchGuildStream(accessor, guildId, query, channelId, maxPages) {
const baseParams = { content: query };
if (channelId !== undefined && channelId !== '')
baseParams.channel_id = channelId;
for await (const raw of offsetPages(accessor, {
endpoint: `/guilds/${guildId}/messages/search`,
baseParams,
itemsPath: ['messages'],
totalKey: 'total_results',
pageSize: PAGE_SIZE,
...(maxPages !== undefined ? { maxPages } : {}),
})) {
const flat = flattenContexts(raw);
if (flat.length > 0)
yield flat;
}
}
export async function searchGuild(accessor, guildId, query, channelId, limit = 100) {
const messages = [];
for await (const page of searchGuildStream(accessor, guildId, query, channelId)) {
for (const msg of page) {
messages.push(msg);
if (messages.length >= limit)
break;
}
if (messages.length >= limit)
break;
}
messages.sort((a, b) => {
const ai = BigInt(a.id ?? '0');
const bi = BigInt(b.id ?? '0');
return ai < bi ? -1 : ai > bi ? 1 : 0;
});
return messages.slice(0, limit);
}
//# sourceMappingURL=search.js.map