@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
59 lines • 2.76 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 { jsonlBytes } from "../render/json.js";
import { cursorPages } from "./paginate.js";
export async function* streamMessagesForDay(accessor, channelId, dateStr, options = {}) {
const limit = options.limit ?? 200;
const after = options.after;
const [oldest] = accessor.timeRange.dayBounds(dateStr);
const [, latest] = accessor.timeRange.dayBounds(options.lastDay ?? dateStr);
if (oldest >= latest)
return;
for await (const page of cursorPages(accessor.transport, 'conversations.history', {
channel: channelId,
oldest: after ?? oldest.toFixed(6),
latest: latest.toFixed(6),
limit: String(limit),
inclusive: 'true',
}, 'messages')) {
yield page.filter((message) => Number(message.ts) >= oldest &&
Number(message.ts) < latest &&
(after === undefined || Number(message.ts) > Number(after)));
}
}
/** Every message of a channel-day, or only those after `after`, oldest first. */
export async function fetchMessagesForDay(accessor, channelId, dateStr, after) {
const messages = [];
for await (const page of streamMessagesForDay(accessor, channelId, dateStr, after === undefined ? {} : { after })) {
for (const item of page)
messages.push(item);
}
messages.sort((a, b) => Number.parseFloat(a.ts) - Number.parseFloat(b.ts));
return messages;
}
export async function getHistoryJsonl(accessor, channelId, dateStr) {
const messages = await fetchMessagesForDay(accessor, channelId, dateStr);
return jsonlBytes(messages);
}
export async function fetchRecentMessages(accessor, channelId, limit = 20) {
const data = await accessor.transport.call('conversations.history', {
channel: channelId,
limit: String(limit),
});
const messages = Array.isArray(data.messages) ? data.messages : [];
messages.sort((a, b) => parseFloat(typeof a.ts === 'string' ? a.ts : '0') -
parseFloat(typeof b.ts === 'string' ? b.ts : '0'));
return messages;
}
//# sourceMappingURL=history.js.map