TypeScript SDK
Add semantic search to your applications with the Lightfast TypeScript SDK for Node.js and browser environments.
TypeScript SDK
The lightfast package provides a type-safe client for the Lightfast API, letting you search your engineering org's memory from any Node.js application.
Installation
bashnpm install lightfastnpm install lightfast
Quick Start
typescriptimport { Lightfast } from "lightfast"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); // Search your organization's memory const results = await lightfast.search({ query: "how does authentication work", }); console.log(results.data[0].title); // → "Add JWT authentication to API endpoints"import { Lightfast } from "lightfast"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); // Search your organization's memory const results = await lightfast.search({ query: "how does authentication work", }); console.log(results.data[0].title); // → "Add JWT authentication to API endpoints"
Configuration
typescriptconst lightfast = new Lightfast({ // Required: Your API key (starts with sk-lf-) apiKey: "sk-lf-...", // Optional: Override base URL (default: https://lightfast.ai) baseUrl: "https://lightfast.ai", // Optional: Request timeout in ms (default: 30000) timeout: 30000, });const lightfast = new Lightfast({ // Required: Your API key (starts with sk-lf-) apiKey: "sk-lf-...", // Optional: Override base URL (default: https://lightfast.ai) baseUrl: "https://lightfast.ai", // Optional: Request timeout in ms (default: 30000) timeout: 30000, });
You can also set the LIGHTFAST_API_KEY environment variable and pass it directly:
typescriptconst lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, });const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, });
Methods
search()
Search through your organization's knowledge for relevant documents and observations.
typescriptconst results = await lightfast.search({ // Required: Natural language query query: "database migration patterns", // Optional: Number of results (default: 10) limit: 10, // Optional: Pagination offset (default: 0) offset: 0, // Optional: Search mode - "fast" | "balanced" | "thorough" (default: "balanced") mode: "balanced", // Optional: Filter results filters: { sourceTypes: ["github"], observationTypes: ["pull_request", "issue"], dateRange: { start: "2024-01-01T00:00:00Z", }, }, });const results = await lightfast.search({ // Required: Natural language query query: "database migration patterns", // Optional: Number of results (default: 10) limit: 10, // Optional: Pagination offset (default: 0) offset: 0, // Optional: Search mode - "fast" | "balanced" | "thorough" (default: "balanced") mode: "balanced", // Optional: Filter results filters: { sourceTypes: ["github"], observationTypes: ["pull_request", "issue"], dateRange: { start: "2024-01-01T00:00:00Z", }, }, });
Response:
typescript{ data: [ { id: "doc_abc123", type: "pull_request", title: "Add database migration framework", snippet: "Implemented Drizzle ORM migrations with...", score: 0.92, source: "github", url: "https://github.com/org/repo/pull/456" } ], meta: { total: 24, limit: 10, offset: 0, took: 145, mode: "balanced", paths: { vector: true, entity: true, cluster: true } }, requestId: "req_abc123" }{ data: [ { id: "doc_abc123", type: "pull_request", title: "Add database migration framework", snippet: "Implemented Drizzle ORM migrations with...", score: 0.92, source: "github", url: "https://github.com/org/repo/pull/456" } ], meta: { total: 24, limit: 10, offset: 0, took: 145, mode: "balanced", paths: { vector: true, entity: true, cluster: true } }, requestId: "req_abc123" }
Error Handling
The SDK provides typed errors for common scenarios:
typescriptimport { Lightfast, AuthenticationError, ValidationError, NotFoundError, RateLimitError, NetworkError, } from "lightfast"; try { const results = await lightfast.search({ query: "test" }); } catch (error) { if (error instanceof AuthenticationError) { // Invalid or expired API key } else if (error instanceof ValidationError) { // Invalid request parameters console.log(error.details); // Field-specific errors } else if (error instanceof RateLimitError) { // Too many requests console.log(error.retryAfter); // Seconds until retry } else if (error instanceof NotFoundError) { // Resource not found } else if (error instanceof NetworkError) { // Connection or timeout error } }import { Lightfast, AuthenticationError, ValidationError, NotFoundError, RateLimitError, NetworkError, } from "lightfast"; try { const results = await lightfast.search({ query: "test" }); } catch (error) { if (error instanceof AuthenticationError) { // Invalid or expired API key } else if (error instanceof ValidationError) { // Invalid request parameters console.log(error.details); // Field-specific errors } else if (error instanceof RateLimitError) { // Too many requests console.log(error.retryAfter); // Seconds until retry } else if (error instanceof NotFoundError) { // Resource not found } else if (error instanceof NetworkError) { // Connection or timeout error } }
TypeScript Types
All request and response types are exported:
typescriptimport type { LightfastConfig, SearchInput, SearchFilters, SearchRequest, SearchResponse, SearchResult, RerankMode, ProxySearchResponse, ProxyAction, ProxyResource, ProxyCall, ProxyCallResponse, } from "lightfast";import type { LightfastConfig, SearchInput, SearchFilters, SearchRequest, SearchResponse, SearchResult, RerankMode, ProxySearchResponse, ProxyAction, ProxyResource, ProxyCall, ProxyCallResponse, } from "lightfast";
Examples
Build a CLI Search Tool
typescriptimport { Lightfast } from "lightfast"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); async function search(query: string) { const { data } = await lightfast.search({ query, limit: 5 }); for (const result of data) { console.log(`[${result.score.toFixed(2)}] ${result.title}`); console.log(` ${result.url}\n`); } } search(process.argv[2]);import { Lightfast } from "lightfast"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); async function search(query: string) { const { data } = await lightfast.search({ query, limit: 5 }); for (const result of data) { console.log(`[${result.score.toFixed(2)}] ${result.title}`); console.log(` ${result.url}\n`); } } search(process.argv[2]);
Add Context to AI Prompts
typescriptimport { Lightfast } from "lightfast"; import { generateText } from "ai"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); async function answerWithContext(question: string) { // Get relevant context from your codebase const { data } = await lightfast.search({ query: question, limit: 5, }); const context = data .map((r) => `[${r.title}]\n${r.snippet}`) .join("\n\n"); // Use context in AI prompt const { text } = await generateText({ model: "gpt-4", prompt: `Based on this context from our codebase:\n\n${context}\n\nAnswer: ${question}`, }); return text; }import { Lightfast } from "lightfast"; import { generateText } from "ai"; const lightfast = new Lightfast({ apiKey: process.env.LIGHTFAST_API_KEY, }); async function answerWithContext(question: string) { // Get relevant context from your codebase const { data } = await lightfast.search({ query: question, limit: 5, }); const context = data .map((r) => `[${r.title}]\n${r.snippet}`) .join("\n\n"); // Use context in AI prompt const { text } = await generateText({ model: "gpt-4", prompt: `Based on this context from our codebase:\n\n${context}\n\nAnswer: ${question}`, }); return text; }