Skip to main content

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

The chat function is wrapped with LangSmith’s traceable decorator for observability:

Tool Call Loop

The agent implements an agentic loop that:
  1. Sends messages to the LLM with available tools
  2. Executes any tool calls requested by the model
  3. Sends tool results back to the LLM
  4. 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

Tool Definitions

The agent uses OpenAI function calling schemas to define available tools:

QUERY_DATABASE_TOOL

Tool schema for database queries:

SEARCH_KNOWLEDGE_BASE_TOOL

Tool schema for knowledge base search:

Complete Example