Overview
The TypeScript implementation provides a complete customer support agent with conversation history, tool integration, and knowledge base search capabilities.Core Agent Function
chat()
The main agent function that processes user questions and manages tool calls.string
required
The user’s question or message to process
any[]
Complete conversation history including system prompt, user messages, assistant responses, and tool calls
string
The agent’s final response to the user
Implementation Details
Thechat function is wrapped with LangSmith’s traceable decorator for observability:
Tool Call Loop
The agent implements an agentic loop that:- Sends messages to the LLM with available tools
- Executes any tool calls requested by the model
- Sends tool results back to the LLM
- Repeats until the model provides a final response
Thread Management
getThreadHistory()
Retrieves conversation history for a specific thread.string
required
Unique identifier for the conversation thread
any[]
Array of conversation messages for the thread
saveThreadHistory()
Persists conversation history for a thread.string
required
Unique identifier for the conversation thread
any[]
required
Array of messages to save
setThreadId()
Sets the active thread ID for the agent session.string
required
Thread identifier to set as active
Knowledge Base Management
loadKnowledgeBase()
Loads and indexes knowledge base documents with embeddings caching.string
default:"./knowledge_base"
Path to the knowledge base directory
Cache Strategy
The function implements intelligent caching:- Checks if embeddings are stale by comparing file modification times
- Loads from cache if available and up-to-date
- Regenerates embeddings only when documents change
Utility Functions
cosineSimilarity()
Calculates cosine similarity between two embedding vectors.number[]
required
First embedding vector
number[]
required
Second embedding vector
number
Cosine similarity score between 0 and 1