@xynehq/jaf
Version:
Juspay Agent Framework - A purely functional agent framework with immutable state and composable tools
296 lines • 9.96 kB
TypeScript
import { z } from 'zod';
import { Message, TraceId } from '../core/types';
export type Result<T, E = MemoryErrorUnion> = {
readonly success: true;
readonly data: T;
} | {
readonly success: false;
readonly error: E;
};
/**
* Memory management types for the JAF framework
*/
export interface ConversationMemory {
readonly conversationId: string;
readonly userId?: string;
readonly messages: readonly Message[];
readonly metadata?: {
readonly createdAt: Date;
readonly updatedAt: Date;
readonly totalMessages: number;
readonly lastActivity: Date;
readonly [key: string]: any;
};
}
export interface MemoryQuery {
readonly conversationId?: string;
readonly userId?: string;
readonly traceId?: TraceId;
readonly limit?: number;
readonly offset?: number;
readonly since?: Date;
readonly until?: Date;
}
export type MemoryProvider = {
/**
* Store messages for a conversation
*/
readonly storeMessages: (conversationId: string, messages: readonly Message[], metadata?: {
userId?: string;
traceId?: TraceId;
[key: string]: any;
}) => Promise<Result<void>>;
/**
* Retrieve conversation history
*/
readonly getConversation: (conversationId: string) => Promise<Result<ConversationMemory | null>>;
/**
* Append new messages to existing conversation
*/
readonly appendMessages: (conversationId: string, messages: readonly Message[], metadata?: {
traceId?: TraceId;
[key: string]: any;
}) => Promise<Result<void>>;
/**
* Search conversations by query
*/
readonly findConversations: (query: MemoryQuery) => Promise<Result<ConversationMemory[]>>;
/**
* Get recent messages from a conversation
*/
readonly getRecentMessages: (conversationId: string, limit?: number) => Promise<Result<readonly Message[]>>;
/**
* Delete conversation
*/
readonly deleteConversation: (conversationId: string) => Promise<Result<boolean>>;
/**
* Clear all conversations for a user
*/
readonly clearUserConversations: (userId: string) => Promise<Result<number>>;
/**
* Get conversation statistics
*/
readonly getStats: (userId?: string) => Promise<Result<{
totalConversations: number;
totalMessages: number;
oldestConversation?: Date;
newestConversation?: Date;
}>>;
/**
* Health check for the memory provider
*/
readonly healthCheck: () => Promise<Result<{
healthy: boolean;
latencyMs?: number;
error?: string;
}>>;
/**
* Close/cleanup the provider
*/
readonly close: () => Promise<Result<void>>;
};
export interface MemoryConfig {
readonly provider: MemoryProvider;
readonly autoStore?: boolean;
readonly maxMessages?: number;
readonly ttl?: number;
readonly compressionThreshold?: number;
}
export declare const InMemoryConfigSchema: z.ZodObject<{
type: z.ZodLiteral<"memory">;
maxConversations: z.ZodDefault<z.ZodNumber>;
maxMessagesPerConversation: z.ZodDefault<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "memory";
maxConversations: number;
maxMessagesPerConversation: number;
}, {
type: "memory";
maxConversations?: number | undefined;
maxMessagesPerConversation?: number | undefined;
}>;
export declare const RedisConfigSchema: z.ZodObject<{
type: z.ZodLiteral<"redis">;
url: z.ZodOptional<z.ZodString>;
host: z.ZodDefault<z.ZodString>;
port: z.ZodDefault<z.ZodNumber>;
password: z.ZodOptional<z.ZodString>;
db: z.ZodDefault<z.ZodNumber>;
keyPrefix: z.ZodDefault<z.ZodString>;
ttl: z.ZodOptional<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "redis";
host: string;
port: number;
db: number;
keyPrefix: string;
url?: string | undefined;
password?: string | undefined;
ttl?: number | undefined;
}, {
type: "redis";
url?: string | undefined;
host?: string | undefined;
port?: number | undefined;
password?: string | undefined;
db?: number | undefined;
keyPrefix?: string | undefined;
ttl?: number | undefined;
}>;
export declare const PostgresConfigSchema: z.ZodObject<{
type: z.ZodLiteral<"postgres">;
connectionString: z.ZodOptional<z.ZodString>;
host: z.ZodDefault<z.ZodString>;
port: z.ZodDefault<z.ZodNumber>;
database: z.ZodDefault<z.ZodString>;
username: z.ZodDefault<z.ZodString>;
password: z.ZodOptional<z.ZodString>;
ssl: z.ZodDefault<z.ZodBoolean>;
tableName: z.ZodDefault<z.ZodString>;
maxConnections: z.ZodDefault<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "postgres";
host: string;
port: number;
database: string;
username: string;
ssl: boolean;
tableName: string;
maxConnections: number;
password?: string | undefined;
connectionString?: string | undefined;
}, {
type: "postgres";
host?: string | undefined;
port?: number | undefined;
password?: string | undefined;
connectionString?: string | undefined;
database?: string | undefined;
username?: string | undefined;
ssl?: boolean | undefined;
tableName?: string | undefined;
maxConnections?: number | undefined;
}>;
export declare const MemoryProviderConfigSchema: z.ZodUnion<[z.ZodObject<{
type: z.ZodLiteral<"memory">;
maxConversations: z.ZodDefault<z.ZodNumber>;
maxMessagesPerConversation: z.ZodDefault<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "memory";
maxConversations: number;
maxMessagesPerConversation: number;
}, {
type: "memory";
maxConversations?: number | undefined;
maxMessagesPerConversation?: number | undefined;
}>, z.ZodObject<{
type: z.ZodLiteral<"redis">;
url: z.ZodOptional<z.ZodString>;
host: z.ZodDefault<z.ZodString>;
port: z.ZodDefault<z.ZodNumber>;
password: z.ZodOptional<z.ZodString>;
db: z.ZodDefault<z.ZodNumber>;
keyPrefix: z.ZodDefault<z.ZodString>;
ttl: z.ZodOptional<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "redis";
host: string;
port: number;
db: number;
keyPrefix: string;
url?: string | undefined;
password?: string | undefined;
ttl?: number | undefined;
}, {
type: "redis";
url?: string | undefined;
host?: string | undefined;
port?: number | undefined;
password?: string | undefined;
db?: number | undefined;
keyPrefix?: string | undefined;
ttl?: number | undefined;
}>, z.ZodObject<{
type: z.ZodLiteral<"postgres">;
connectionString: z.ZodOptional<z.ZodString>;
host: z.ZodDefault<z.ZodString>;
port: z.ZodDefault<z.ZodNumber>;
database: z.ZodDefault<z.ZodString>;
username: z.ZodDefault<z.ZodString>;
password: z.ZodOptional<z.ZodString>;
ssl: z.ZodDefault<z.ZodBoolean>;
tableName: z.ZodDefault<z.ZodString>;
maxConnections: z.ZodDefault<z.ZodNumber>;
}, "strip", z.ZodTypeAny, {
type: "postgres";
host: string;
port: number;
database: string;
username: string;
ssl: boolean;
tableName: string;
maxConnections: number;
password?: string | undefined;
connectionString?: string | undefined;
}, {
type: "postgres";
host?: string | undefined;
port?: number | undefined;
password?: string | undefined;
connectionString?: string | undefined;
database?: string | undefined;
username?: string | undefined;
ssl?: boolean | undefined;
tableName?: string | undefined;
maxConnections?: number | undefined;
}>]>;
export type InMemoryConfig = z.infer<typeof InMemoryConfigSchema>;
export type RedisConfig = z.infer<typeof RedisConfigSchema>;
export type PostgresConfig = z.infer<typeof PostgresConfigSchema>;
export type MemoryProviderConfig = z.infer<typeof MemoryProviderConfigSchema>;
export type MemoryError = {
readonly _tag: 'MemoryError';
readonly message: string;
readonly code: string;
readonly provider: string;
readonly cause?: Error;
};
export type MemoryConnectionError = {
readonly _tag: 'MemoryConnectionError';
readonly message: string;
readonly provider: string;
readonly cause?: Error;
};
export type MemoryNotFoundError = {
readonly _tag: 'MemoryNotFoundError';
readonly message: string;
readonly conversationId: string;
readonly provider: string;
};
export type MemoryStorageError = {
readonly _tag: 'MemoryStorageError';
readonly message: string;
readonly operation: string;
readonly provider: string;
readonly cause?: Error;
};
export type MemoryErrorUnion = MemoryConnectionError | MemoryNotFoundError | MemoryStorageError;
export declare const createMemoryError: (message: string, code: string, provider: string, cause?: Error) => MemoryError;
export declare const createMemoryConnectionError: (provider: string, cause?: Error) => MemoryConnectionError;
export declare const createMemoryNotFoundError: (conversationId: string, provider: string) => MemoryNotFoundError;
export declare const createMemoryStorageError: (operation: string, provider: string, cause?: Error) => MemoryStorageError;
export declare const isMemoryError: (error: any) => error is MemoryErrorUnion;
export declare const isMemoryConnectionError: (error: any) => error is MemoryConnectionError;
export declare const isMemoryNotFoundError: (error: any) => error is MemoryNotFoundError;
export declare const isMemoryStorageError: (error: any) => error is MemoryStorageError;
export declare const createSuccess: <T>(data: T) => Result<T>;
export declare const createFailure: <E extends MemoryErrorUnion>(error: E) => Result<never, E>;
export declare const isSuccess: <T, E>(result: Result<T, E>) => result is {
success: true;
data: T;
};
export declare const isFailure: <T, E>(result: Result<T, E>) => result is {
success: false;
error: E;
};
//# sourceMappingURL=types.d.ts.map