@prexo/ai-chat-sdk
Version:
AI Chat SDK for building AI chat applications with Context and History
189 lines (186 loc) • 4.71 kB
TypeScript
import * as _upstash_redis from '@upstash/redis';
import { Message } from 'ai';
import { QueryMode } from '@upstash/vector';
type TelementryOptions = {
enabled?: boolean;
endpoint?: string;
ingestionKey?: string;
sdkVersion?: string;
};
type GetContextClientParams = {
vector?: {
url: string;
token: string;
namespace?: string;
};
};
type ExtVectorConfig = {
url: string;
token: string;
};
type GetHistoryClientParams = {
redis?: {
url: string;
token: string;
};
};
type RedisHistoryConfig = {
config?: _upstash_redis.RedisConfigNodejs;
client?: _upstash_redis.Redis;
};
type SDKConfig = {
telemetry?: TelementryOptions;
context?: GetContextClientParams;
history?: GetHistoryClientParams;
};
type SDKStatus = {
telemetry: boolean;
context: boolean;
history: boolean;
};
type EventProperties = Record<string, any>;
type EventName = string | number | symbol;
type TelementryEvents = {
agent_onFinish: {
llmModel?: string;
latencyMs?: number;
RAGDisabled?: boolean;
sessionId?: string;
sessionTTL?: number;
usage?: {
promptTokens: number;
completionTokens: number;
totalTokens: number;
};
finishReason?: string;
};
agent_onError: {
code: string;
error: Error;
};
user_message_sent: {
content: string;
sessionId?: string;
sessionTTL?: number;
};
user_message_error: {
content: string;
sessionId?: string;
sessionTTL?: number;
code: string;
};
chat_widget_activated: {
sessionId?: string;
sessionTTL?: number;
widgetId?: string;
value: boolean;
};
error: {
code: string;
message?: string;
};
api_call: {
latencyMs: number;
endpoint: string;
};
custom_event: {
[key: string]: any;
};
};
type VectorPayload = {
question: string | number[];
similarityThreshold?: number;
topK?: number;
namespace?: string;
contextFilter?: string;
queryMode?: QueryMode;
};
type FilePath = string;
type URL = string;
type ResetOptions = {
namespace: string;
};
type AddContextOptions = {
/**
* Namespace of the index you wanted to insert. Default is empty string.
* @default ""
*/
metadata?: UpstashDict;
namespace?: string;
};
type UpstashDict = Record<string, unknown>;
type SaveOperationResult = {
success: true;
ids: string[];
} | {
success: false;
error: string;
};
type DatasWithFileSource = {
type?: "pdf" | "csv" | "text-file" | "html";
fileSource: FilePath;
options?: AddContextOptions;
} | {
type: "pdf";
fileSource: FilePath | Blob;
options?: AddContextOptions;
} | {
type: "csv";
fileSource: FilePath | Blob;
options?: AddContextOptions;
} | {
type: "text-file";
fileSource: FilePath | Blob;
options?: AddContextOptions;
} | ({
type: "html";
source: URL;
options?: AddContextOptions;
} | {
type: "html";
source: FilePath | Blob;
options?: AddContextOptions;
});
type AddContextPayload = {
type: "text";
data: string;
options?: AddContextOptions;
id?: string | number;
} | {
type: "embedding";
data: number[];
text?: string;
options?: AddContextOptions;
id?: string | number;
} | DatasWithFileSource;
interface BaseVectorContext {
addContext(input: AddContextPayload): Promise<SaveOperationResult>;
removeContext(ids: string[]): Promise<void>;
getContext<TMetadata = any>(payload: Omit<VectorPayload, "namespace">): Promise<{
data: string;
id: string;
metadata: TMetadata;
}[]>;
resetContext(): Promise<void>;
}
type VectorContextResult<TMetadata = any> = {
data: string;
id: string;
metadata: TMetadata;
};
interface BaseMessageHistory {
addMessage(params: {
message: Message;
sessionId: string;
sessionTTL?: number;
}): Promise<void>;
deleteMessages(params: {
sessionId: string;
}): Promise<void>;
getMessages(params: {
sessionId: string;
amount?: number;
startIndex?: number;
}): Promise<Message[]>;
}
export type { AddContextOptions, AddContextPayload, BaseMessageHistory, BaseVectorContext, DatasWithFileSource, EventName, EventProperties, ExtVectorConfig, FilePath, GetContextClientParams, GetHistoryClientParams, RedisHistoryConfig, ResetOptions, SDKConfig, SDKStatus, SaveOperationResult, TelementryEvents, TelementryOptions, URL, UpstashDict, VectorContextResult, VectorPayload };