Skip to content

Commit

Permalink
docs: minor comments added
Browse files Browse the repository at this point in the history
  • Loading branch information
sroussey committed Jan 18, 2025
1 parent 47710a9 commit ea5bd34
Show file tree
Hide file tree
Showing 20 changed files with 185 additions and 6 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/source/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export interface DocumentSectionMetadata {
title: string;
}

/**
* Represents a document with its content and metadata.
*/
export class Document {
public metadata: DocumentMetadata;

Expand Down Expand Up @@ -81,11 +84,7 @@ export class Document {
}

export class DocumentSection extends Document {
constructor(
public parent: Document,
content?: ContentType,
metadata?: DocumentSectionMetadata
) {
constructor(public parent: Document, content?: ContentType, metadata?: DocumentSectionMetadata) {
super(content, metadata);
this.parent = parent;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/source/DocumentConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import { Document, DocumentMetadata } from "./Document";

/**
* Abstract class for converting different types of content into a Document.
*/
export abstract class DocumentConverter {
public metadata: DocumentMetadata;
constructor(metadata: DocumentMetadata) {
Expand Down
5 changes: 4 additions & 1 deletion packages/storage/src/browser/indexeddb/IndexedDbJobQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { Job, JobQueue, ILimiter } from "ellmers-core";
import { makeFingerprint } from "../../util/Misc";
import { ensureIndexedDbTable } from "./base/IndexedDbTable";

// The IndexedDbQueue class
/**
* IndexedDB implementation of a job queue.
* Provides storage and retrieval for job execution states using IndexedDB.
*/
export class IndexedDbQueue<Input, Output> extends JobQueue<Input, Output> {
private dbPromise: Promise<IDBDatabase>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import { TaskGraphRepository } from "ellmers-core";
import { IndexedDbKVRepository } from "./base/IndexedDbKVRepository";

/**
* IndexedDB implementation of a task graph repository.
* Provides storage and retrieval for task graphs using IndexedDB.
*/
export class IndexedDbTaskGraphRepository extends TaskGraphRepository {
kvRepository: IndexedDbKVRepository;
public type = "IndexedDbTaskGraphRepository" as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
} from "ellmers-core";
import { IndexedDbKVRepository } from "./base/IndexedDbKVRepository";

/**
* IndexedDB implementation of a task output repository.
* Provides storage and retrieval for task outputs using IndexedDB.
*/
export class IndexedDbTaskOutputRepository extends TaskOutputRepository {
kvRepository: IndexedDbKVRepository<
TaskOutputPrimaryKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
} from "ellmers-ai";
import { DefaultValueType } from "ellmers-core";

/**
* In-memory implementation of a model repository.
* Provides storage and retrieval for models and task-to-model mappings.
*/
export class InMemoryModelRepository extends ModelRepository {
modelKvRepository: InMemoryKVRepository<
ModelPrimaryKey,
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/browser/inmemory/InMemoryRateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import { ILimiter } from "ellmers-core";

/**
* In-memory implementation of a rate limiter.
* Manages request counts and delays to control job execution.
*/
export class InMemoryRateLimiter implements ILimiter {
private requests: Date[] = [];
private nextAvailableTime: Date = new Date(); // New property to track externally set delay
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import { TaskGraphRepository } from "ellmers-core";
import { InMemoryKVRepository } from "./base/InMemoryKVRepository";

/**
* In-memory implementation of a task graph repository.
* Provides storage and retrieval for task graphs.
*/
export class InMemoryTaskGraphRepository extends TaskGraphRepository {
kvRepository: InMemoryKVRepository;
public type = "InMemoryTaskGraphRepository" as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
} from "ellmers-core";
import { InMemoryKVRepository } from "./base/InMemoryKVRepository";

/**
* In-memory implementation of a task output repository.
* Provides storage and retrieval for task outputs.
*/
export class InMemoryTaskOutputRepository extends TaskOutputRepository {
kvRepository: InMemoryKVRepository<
TaskOutputPrimaryKey,
Expand Down
49 changes: 49 additions & 0 deletions packages/storage/src/bun/sqlite/SqliteJobQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { type Database } from "bun:sqlite";

// TODO: reuse prepared statements

/**
* SQLite implementation of a job queue.
* Provides storage and retrieval for job execution states using SQLite.
*/
export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
constructor(
protected db: Database,
Expand Down Expand Up @@ -49,6 +53,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return this;
}

/**
* Creates a new job instance from the provided database results.
* @param results - The job data from the database
* @returns A new Job instance with populated properties
*/
public createNewJob(results: any): Job<Input, Output> {
return new this.jobClass({
...results,
Expand All @@ -61,6 +70,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
});
}

/**
* Adds a new job to the queue.
* @param job - The job to add
* @returns The ID of the added job
*/
public async add(job: Job<Input, Output>) {
job.queueName = this.queue;
const fingerprint = await makeFingerprint(job.input);
Expand Down Expand Up @@ -95,6 +109,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return result?.id;
}

/**
* Retrieves a job by its ID.
* @param id - The ID of the job to retrieve
* @returns The job if found, undefined otherwise
*/
public async get(id: string) {
const JobQuery = `
SELECT *
Expand All @@ -106,6 +125,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return result ? this.createNewJob(result) : undefined;
}

/**
* Retrieves a slice of jobs from the queue.
* @param num - Maximum number of jobs to return
* @returns An array of jobs
*/
public async peek(num: number = 100) {
num = Number(num) || 100; // TS does not validate, so ensure it is a number since we put directly in SQL string
const FutureJobQuery = `
Expand All @@ -123,6 +147,10 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return ret;
}

/**
* Retrieves all jobs currently being processed.
* @returns An array of jobs
*/
public async processing() {
const ProcessingQuery = `
SELECT *
Expand All @@ -136,6 +164,10 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return ret;
}

/**
* Retrieves the next available job that is ready to be processed.
* @returns The next job or undefined if no job is available
*/
public async next() {
let id: string | undefined;
{
Expand Down Expand Up @@ -164,6 +196,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
}
}

/**
* Retrieves the number of jobs in the queue with a specific status.
* @param status - The status of the jobs to count
* @returns The count of jobs with the specified status
*/
public async size(status = JobStatus.PENDING) {
const sizeQuery = `
SELECT COUNT(*) as count
Expand All @@ -175,6 +212,13 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
return result.count;
}

/**
* Marks a job as complete with its output or error.
* Handles retries for failed jobs and triggers completion callbacks.
* @param id - ID of the job to complete
* @param output - Result of the job execution
* @param error - Optional error message if job failed
*/
public async complete(id: string, output: Output | null = null, error: string | null = null) {
const job = await this.get(id);
if (!job) throw new Error(`Job ${id} not found`);
Expand Down Expand Up @@ -206,6 +250,11 @@ export class SqliteJobQueue<Input, Output> extends JobQueue<Input, Output> {
await this.limiter.clear();
}

/**
* Looks up cached output for a given task type and input
* Uses input fingerprinting for efficient matching
* @returns The cached output or null if not found
*/
public async outputForInput(taskType: string, input: Input) {
const fingerprint = await makeFingerprint(input);
const OutputQuery = `
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/bun/sqlite/SqliteModelRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
import { SqliteKVRepository } from "./base/SqliteKVRepository";
import { DefaultValueType } from "ellmers-core";

/**
* SQLite implementation of a model repository.
* Provides storage and retrieval for models and task-to-model mappings using SQLite.
*/
export class SqliteModelRepository extends ModelRepository {
public type = "SqliteModelRepository" as const;
modelKvRepository: SqliteKVRepository<
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/bun/sqlite/SqliteRateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { type Database } from "better-sqlite3";
import { ILimiter } from "ellmers-core";
import { toSQLiteTimestamp } from "../../util/Misc";

/**
* SQLite implementation of a rate limiter.
* Manages request counts and delays to control job execution.
*/
export class SqliteRateLimiter implements ILimiter {
private readonly db: Database;
private readonly queueName: string;
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/bun/sqlite/SqliteTaskGraphRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import { TaskGraphRepository } from "ellmers-core";
import { SqliteKVRepository } from "./base/SqliteKVRepository";

/**
* SQLite implementation of a task graph repository.
* Provides storage and retrieval for task graphs using SQLite.
*/
export class SqliteTaskGraphRepository extends TaskGraphRepository {
kvRepository: SqliteKVRepository;
public type = "SqliteTaskGraphRepository" as const;
Expand Down
4 changes: 4 additions & 0 deletions packages/storage/src/bun/sqlite/SqliteTaskOutputRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
} from "ellmers-core";
import { SqliteKVRepository } from "./base/SqliteKVRepository";

/**
* SQLite implementation of a task output repository.
* Provides storage and retrieval for task outputs using SQLite.
*/
export class SqliteTaskOutputRepository extends TaskOutputRepository {
kvRepository: SqliteKVRepository<
TaskOutputPrimaryKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import { TaskGraphRepository } from "ellmers-core";
import { FileKVRepository } from "./base/FileKVRepository";

/**
* File-based implementation of a task graph repository.
* Provides storage and retrieval for task graphs using a file system.
*/
export class FileTaskGraphRepository extends TaskGraphRepository {
kvRepository: FileKVRepository;
public type = "FileTaskGraphRepository" as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {
} from "ellmers-core";
import { FileKVRepository } from "./base/FileKVRepository";

/**
* File-based implementation of a task output repository.
* Provides storage and retrieval for task outputs using a file system.
*/
export class FileTaskOutputRepository extends TaskOutputRepository {
kvRepository: FileKVRepository<
TaskOutputPrimaryKey,
Expand Down
Loading

0 comments on commit ea5bd34

Please sign in to comment.