@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
52 lines • 2.35 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 { gmailBase, googleGet } from "../google/client.js";
const MAX_PAGES = 50;
/** The mailbox's history id now, where a delta starts from. Mirrors Python's `current_history_id`. */
export async function currentHistoryId(tokenManager) {
const url = `${gmailBase(tokenManager)}/users/me/profile`;
const data = (await googleGet(tokenManager, url));
if (data.historyId === undefined)
throw new Error('gmail: the profile names no historyId');
return String(data.historyId);
}
/**
* The mailbox's history records after `start`, and the history id they reach.
* Gmail answers 404 for a start older than the history it keeps, which
* rejects as any failed call does. Mirrors Python's `list_history`.
*/
export async function listHistory(tokenManager, start) {
const url = `${gmailBase(tokenManager)}/users/me/history`;
const records = [];
let latest = start;
let token = null;
for (let i = 0; i < MAX_PAGES; i += 1) {
const params = { startHistoryId: start, maxResults: '500' };
if (token !== null)
params.pageToken = token;
const data = (await googleGet(tokenManager, url, params));
for (const item of data.history ?? [])
records.push(item);
if (data.historyId !== undefined)
latest = String(data.historyId);
token =
typeof data.nextPageToken === 'string' && data.nextPageToken !== ''
? data.nextPageToken
: null;
if (token === null)
return [records, latest];
}
throw new Error('gmail: history exceeded 50 pages');
}
//# sourceMappingURL=history.js.map