@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
79 lines • 4.15 kB
TypeScript
import type { NormalizeConstructor } from '@adonisjs/core/types/helpers';
import type { BaseModel } from '@adonisjs/lucid/orm';
import { type EmbeddingProvider, type EmbeddingUsage } from '../spi/embedding-provider.js';
import type { Passage } from '../spi/retriever.js';
import { type ChunkOptions } from './chunk.js';
import type { VectorStore } from './vector-store.js';
type MaybePromise<T> = T | Promise<T>;
/** Metadata key separating models that share a store. Reserved: it always wins over yours. */
export declare const RAG_ENTITY_KEY = "ragEntity";
/** A required scope key was missing, from the search or from the row. Nothing ran. */
export declare class RagScopeRequiredError extends Error {
constructor(model: string, key: string, operation?: 'ragSearch' | 'ragIndex');
}
export interface WithRagOptions<Row> {
/** Lazy, so the model file can be imported before the app has booted. */
store: () => MaybePromise<VectorStore>;
embedder: () => MaybePromise<EmbeddingProvider>;
/** The text to index. `null`/empty indexes nothing. Optional if every call passes it to `ragIndex`. */
text?: (row: Row) => string | null | undefined;
/** Citation-facing origin of each chunk. */
source?: (row: Row) => string | null | undefined;
/** Stored with every chunk, and filterable in `ragSearch`. */
metadata?: (row: Row) => Record<string, unknown>;
/**
* Attributes that isolate this model's chunks, e.g. `['projectId']`. Each is written to the chunk
* metadata, and `ragSearch` throws {@link RagScopeRequiredError} without it. A caller's `filter` can
* never override it. This is what stops one tenant's search from reaching another tenant's text.
*/
scope?: readonly (keyof Row & string)[];
/** Metadata of one chunk, from its text and the whole document (a page number, say). */
chunkMetadata?: (chunk: string, fullText: string, row: Row) => Record<string, unknown>;
chunk?: ChunkOptions;
/** Keep only the first N chunks: an upper bound on what one huge document can cost. */
maxChunks?: number;
/** Embedding spend, indexing and searching alike. The package cannot know who pays for it. */
onUsage?: (usage: EmbeddingUsage) => void | Promise<void>;
}
export interface RagSearchOptions {
topK?: number;
/** Required for every key the model declares in `scope`. An array matches any of its values. */
scope?: Record<string, unknown>;
filter?: Record<string, unknown>;
minScore?: number;
}
/** What the mixin adds to a row. */
export interface RagRow {
/** Chunk ids are `<ragDocumentId>#<n>`, so two models can share one store. */
readonly ragDocumentId: string;
ragIndex(text?: string | null): Promise<number>;
ragRemove(): Promise<void>;
}
/** What the mixin adds to the model class. */
export interface RagModelStatics {
ragSearch(query: string, search?: RagSearchOptions): Promise<Passage[]>;
}
/**
* Declares RAG on a Lucid model, the way `withAuthFinder` declares auth:
*
* ```ts
* class ProjectDocument extends compose(BaseModel, withRag({
* store: () => store,
* embedder: () => embedder,
* text: (doc) => doc.textContent,
* scope: ['projectId'],
* })) {}
*
* await doc.ragIndex()
* await ProjectDocument.ragSearch('question', { scope: { projectId } })
* ```
*
* `row.delete()` removes its chunks, after the commit when inside a transaction. A savepoint's commit
* counts as one, so a later rollback of the outer transaction leaves the row without chunks until the
* next `ragIndex`. Nothing else removes them: a database `ON DELETE CASCADE`, a bulk
* `Model.query().delete()` and `deleteQuietly()` fire no hook, so call `ragRemove` or clean up yourself. Saving does NOT index: embedding is a slow, paid call that has no place in a
* `save()`, so call `ragIndex` from a job or wherever the text is known to have changed.
*/
export declare function withRag<Row = any>(options: WithRagOptions<Row>): <T extends NormalizeConstructor<typeof BaseModel>>(superclass: T) => T & RagModelStatics & (new (...args: any[]) => RagRow);
export {};
//# sourceMappingURL=with-rag.d.ts.map