@mencraft/lore-headless
Version:
A headless library for chat streaming functionality
331 lines (327 loc) • 9.24 kB
TypeScript
/**
* Event types for the chat stream
*/
declare enum EventType {
BatchDataIntermediate = "batch_data_intermediate",
TextBatchIntermediate = "text_batch_intermediate",
TextStreamIntermediate = "text_stream_intermediate",
ToolCallIntermediate = "tool_call_intermediate",
BatchDataOutput = "batch_data_output",
TextBatchOutput = "text_batch_output",
TextStreamOutput = "text_stream_output",
ToolCallOutput = "tool_call_output",
End = "end",
Error = "error"
}
/**
* Metadata for stream events
*/
interface EventMetadata {
timestamp: string;
}
/**
* Stream event interface
*/
interface StreamEvent<T = any> {
event_type: EventType;
event_data: T | null;
metadata: EventMetadata;
}
/**
* Chat request interface
*/
interface ChatRequest {
user: string;
session: string;
query: string;
testNodeId?: string | undefined;
}
interface GraphChatRequest {
user: string;
session: string;
query: string;
testNodeId: string | undefined;
flow: {
nodes: any[];
edges: any[];
viewport: any;
};
}
/**
* Callbacks for chat stream
*/
interface ChatStreamCallbacks {
graphId?: string;
onTextStream?: (text: string) => void;
onBatchData?: (data: NodeResult) => void;
onTextBatch?: (data: NodeResult) => void;
onToolCall?: (toolCall: any) => void;
onBatchDataIntermediate?: (data: NodeResult) => void;
onTextBatchIntermediate?: (data: NodeResult) => void;
onTextStreamIntermediate?: (text: string) => void;
onToolCallIntermediate?: (toolCall: any) => void;
onEnd?: () => void;
onError?: (error: Error) => void;
onSystemMessage?: (text: string) => void;
onEvent?: (event: StreamEvent) => void;
}
/**
* Graph structure error response
*/
interface GraphStructureErrorResponse {
validation_errors?: Array<{
node_id: string;
message: string;
error_type: string;
}>;
test_failure?: boolean;
too_many_graphs?: boolean;
tests?: Array<{
error?: boolean;
error_text?: string;
events?: StreamEvent[];
}>;
}
/**
* Feedback request interface
*/
interface FeedbackRequest {
user: string;
session: string;
query: string;
response: string;
feedback: string;
feedback_value: number;
}
/**
* API response interface
*/
interface ApiResponse<T> {
data?: T;
status: number;
statusText: string;
headers: Headers;
}
interface NodeResult {
identity?: Identity;
node_type: LoreNodeTypes;
node_id: string;
node_results?: string | ConditionalNodeResult | ModelResult | ModelResultBool | HydeResult | Record<string, unknown> | RouterResult;
}
declare enum LoreNodeTypes {
HYDE_PROMPT = "HydePromptNode",
PROMPT = "PromptNode",
BOOL_PROMPT = "BoolPromptNode",
DEFAULT_RESPONSE = "DefaultResponseNode",
QUERY = "QueryNode",
ROUTER = "RouterNode",
REACT = "ReactNode",
CONDITIONAL = "ConditionalNode",
SEARCH = "SearchNode"
}
type ConditionType = "equals" | "contains" | "greater_than" | "less_than" | "regex" | "llm" | "has_memory";
interface ConditionalNodeResult {
condition_result: string;
matched_condition: Condition;
next_node_id: string;
}
interface ModelResult {
input_prompt: Prompt;
input_token_count?: number;
llm_output?: string;
llm_token_count?: number;
model?: string;
memory_prompt?: Prompt;
system_prompt?: Prompt;
}
interface Condition {
type: ConditionType;
value?: string;
prompt?: Prompt;
target_node_id: string;
}
interface ModelResultBool extends ModelResult {
bool_returned: boolean;
}
interface RouterResult extends ModelResult {
next_node_id: string;
}
interface HydeResult {
model_output: ModelResult;
hyde_model_output: ModelResult;
}
interface Prompt {
messages: Message$1[];
}
declare enum LLMRole {
ASSISTANT = "assistant",
USER = "user",
SYSTEM = "system",
DEVELOPER = "developer"
}
interface Message$1 {
role: LLMRole;
content: string;
}
interface Identity {
user_id: string;
session_id: string;
experiment: string;
graph_id: string;
client_id: string;
}
declare enum ToolParameterType {
STRING = "string",
NUMBER = "number",
INTEGER = "integer",
BOOLEAN = "boolean",
OBJECT = "object",
ARRAY = "array"
}
interface ToolParameter {
name: string;
type: ToolParameterType;
description: string;
required?: boolean;
enum?: any[];
}
interface ToolConfig {
name: string;
description: string;
parameters: ToolParameter[];
}
interface ToolCall {
tool_name: string;
str_ai_parameters?: string;
error?: boolean;
tool_id?: string;
ai_parameters?: Record<string, any>;
}
interface ToolCallResult extends ToolCall {
result?: Record<string, any> | any[];
str_result?: string;
}
/**
* Authentication credentials
*/
interface AuthCredentials {
clientId: string;
token: string;
}
/**
* API client for making requests
*/
declare class LoreClient {
private chatUrl;
private validationUrl;
/**
* Creates an instance of the LoreClient
* @param mode The mode to use for the API client. One of 'production', 'staging', or 'development'.
* @param customChatUrl Optional custom chat URL to use instead of the default based on the mode.
* @param customValidationUrl Optional custom validation URL to use instead of the default based on the mode.
* @throws Error if customChatUrl or customValidationUrl is provided but the other is not.
*/
constructor(mode?: 'production' | 'staging' | 'development', customChatUrl?: string, customValidationUrl?: string);
/**
* Make a POST request to the API
* @param endpoint API endpoint
* @param auth Authentication credentials
* @param body Request body
* @param options Request options
* @returns API response
*/
post<T>(endpoint: string, auth: AuthCredentials, body?: any, options?: RequestInit & {
headers?: Record<string, string>;
useValidationUrl?: boolean;
}): Promise<ApiResponse<T>>;
/**
* Make a chat POST request to the API
* @param endpoint API endpoint
* @param auth Authentication credentials
* @param body Request body
* @param options Request options
* @returns API response
*/
chatPost<T>(endpoint: string, auth: AuthCredentials, body?: any, options?: RequestInit & {
headers?: Record<string, string>;
}): Promise<ApiResponse<T>>;
/**
* Make an admin POST request to the API
* @param endpoint API endpoint
* @param clientId Client ID
* @param firebaseIdToken Firebase ID token
* @param body Request body
* @param options Request options
* @returns API response
*/
adminPost<T>(endpoint: string, auth: AuthCredentials, body?: any, options?: RequestInit & {
headers?: Record<string, string>;
useValidationUrl?: boolean;
}): Promise<ApiResponse<T>>;
}
/**
* Message interface for chat messages
*/
interface ChatMessage {
id: string;
role: LLMRole | 'error';
content: string;
timestamp: Date;
isStreaming?: boolean;
}
/**
* Image message interface
*/
interface ImageMessage {
id: string;
imageId: string;
storageType: string;
role: LLMRole;
timestamp: Date;
url?: string;
}
/**
* File message interface
*/
interface FileMessage {
id: string;
filename: string;
mediaType: string;
role: LLMRole;
timestamp: Date;
url?: string;
}
/**
* Union type for all message types
*/
type Message = ChatMessage | ImageMessage | FileMessage;
/**
* Hook for using the chat stream in React
* @param apiClient API client
* @param auth Authentication credentials
* @param callbacks Optional callbacks for stream events
* @returns Chat stream functions and messages
*/
declare const useChatStream: (apiClient: LoreClient, auth: AuthCredentials, callbacks?: ChatStreamCallbacks) => {
messages: (ChatMessage | ImageMessage | FileMessage)[];
isStreaming: boolean;
startChatStream: (request: ChatRequest | GraphChatRequest, graphId: string, useAdminServer: boolean) => Promise<void>;
cancelStream: () => void;
startGraphChatStream: (request: ChatRequest, graphId: string, skipValidation?: boolean) => Promise<void>;
clearMessages: () => void;
startVerification: (request: ChatRequest, graphId: string) => Promise<boolean>;
};
/**
* Hook for using feedback in React
* @param loreClient API client
* @param auth Authentication credentials
* @param graphId Graph ID
* @returns Feedback function and state
*/
declare const useFeedback: (loreClient: LoreClient, auth: AuthCredentials, graphId?: string) => {
sendFeedback: (feedback: FeedbackRequest) => Promise<any>;
loading: boolean;
error: Error | null;
data: any;
};
export { AuthCredentials, ChatMessage, ChatRequest, ChatStreamCallbacks, EventType, FeedbackRequest, FileMessage, GraphChatRequest, GraphStructureErrorResponse, ImageMessage, LoreClient, Message, StreamEvent, ToolCall, ToolCallResult, ToolConfig, ToolParameter, ToolParameterType, useChatStream, useFeedback };