import { Activity } from '../types.js'; import { ActivityStorage } from '../storage/types.js'; import { ActivityClient, ListOptions, SelectOptions } from './types.js'; /** * Interface for the network layer used by the activity client. * Abstracts away the details of polling and fetching from the API. * @internal */ export interface NetworkClient { rawStream(): AsyncIterable; listActivities(options?: ListOptions): Promise<{ activities: Activity[]; nextPageToken?: string; }>; fetchActivity(activityId: string): Promise; } /** * The default implementation of the ActivityClient. * Implements a "local-first" architecture where activities are fetched from * the network, cached locally, and then served from the cache. */ export declare class DefaultActivityClient implements ActivityClient { private storage; private network; constructor(storage: ActivityStorage, network: NetworkClient); /** * Re-hydrates plain artifact objects from storage into rich class instances. * JSON serialization loses class information (methods), so we need to restore it. * * **Behavior:** * - Iterates through artifacts in an activity. * - If an artifact is a plain object (not a class instance), it's re-instantiated. * - Handles backward compatibility: if an artifact is already a class instance, it's skipped. * * @param activity The activity from storage, potentially with plain artifacts. * @returns The same activity with its artifacts guaranteed to be class instances. */ private _hydrateActivityArtifacts; /** * Returns an async iterable of all activities. * * **Behavior:** * - Always syncs new activities from the network first (via hydrate). * - Then yields all activities from local storage. * * This ensures callers always get the complete, up-to-date history * rather than potentially stale cached data. */ history(): AsyncIterable; /** * Fetches all activities from the network and caches them. * Used to populate an empty cache. * @internal */ private fetchAndCacheAll; /** * Syncs new activities from the network to local cache. * * **Optimization Strategy:** * Activities are immutable - once downloaded, they never change. * We use the Jules API's pageToken (nanosecond timestamp) to fetch * only activities newer than our latest cached one. * * **Behavior:** * - Empty cache: Fetches all activities (no pageToken) * - Has cached activities: Constructs pageToken from latest createTime, * fetches only newer activities * - Frozen session (> 30 days): Skips API call entirely * * @returns The number of new activities synced. */ hydrate(): Promise; /** * Returns an async iterable of new activities from the network. * This method polls the network and updates the local storage. * * **Side Effects:** * - Polls the network continuously. * - Appends new activities to local storage (write-through caching). * * **Logic:** * - Reads the latest activity from storage to determine the "high-water mark". * - Ignores incoming activities older than or equal to the high-water mark. */ updates(): AsyncIterable; /** * Returns a combined stream of history and updates. * This is the primary method for consuming the activity stream. * * **Behavior:** * 1. Yields all historical activities from local storage (offline capable). * 2. Switches to `updates()` to yield new activities from the network (real-time). */ stream(): AsyncIterable; /** * Queries local storage for activities matching the given options. */ select(options?: SelectOptions): Promise; /** * Lists activities from the network directly. * @param options Pagination options. */ list(options?: ListOptions): Promise<{ activities: Activity[]; nextPageToken?: string; }>; /** * Gets a single activity by ID. * Implements a "read-through" caching strategy. * * **Logic:** * 1. Checks local storage. If found, returns it immediately (fast). * 2. If missing, fetches from the network. * 3. Persists the fetched activity to storage (future reads will hit cache). * 4. Returns the activity. * * **Side Effects:** * - May perform a network request. * - May write to local storage. */ get(activityId: string): Promise; }