@altamsh04/openref
v1.2.0
Published
Agentic web sources search SDK — turns natural language queries into ranked sources
Downloads
326
Readme
OpenRef
OpenRef is a production-oriented TypeScript SDK for web-grounded answers with optional inline citations.
It keeps the runtime model simple:
Query -> Web Search -> Content Extraction/Chunking -> Streaming Chat ResponseFeatures
- Web search with provider fallback (
Brave->DuckDuckGo) - Source deduplication and domain diversity filtering
- Optional LLM reranking of search candidates
- Optional LLM query expansion for broader retrieval
- Query-aware page extraction and chunk scoring
- Streaming chat response
- Configurable citation behavior (
citationStrictness) - Typed SDK surface (
search,chat, event streams)
Install
npm install @altamsh04/openrefQuick Start
import { OpenRef } from "@altamsh04/openref";
const agent = new OpenRef({
llm: {
apiKey: "sk-or-v1......",
chatModel: "stepfun/step-3.5-flash:free",
fallbackChatModels: [
"nvidia/nemotron-3-nano-30b-a3b:free",
"mistralai/mistral-small-3.1-24b-instruct:free"
],
systemPrompt: "Answer in short bullet points.",
citationStrictness: true,
maxRetries: 2,
retryDelayMs: 1200,
maxOutputTokens: 2048,
maxContinuationRequests: 2
},
search: {
engineProvider: { provider: "brave" }, // fallback order: duckduckgo -> bing
preferLatest: true,
timeZone: "America/New_York",
maxSources: 5,
queryExpansion: true,
queryExpansionValue: 3,
queryExpansionTimeout: 1200,
searchTimeout: 5000,
enableReranking: true,
rerankTimeout: 4000
},
retrieval: {
contentTimeout: 6000,
maxContextTokens: 6000,
chunkTargetTokens: 400
},
response: {
stream: true
}
});
const query = "Today's top news in AI";
async function run() {
// Per-request overrides
const response = await agent.chat(query, {
stream: false,
systemPrompt: "Keep it under 120 words and mention uncertainty clearly.",
citationStrictness: false
});
console.log(JSON.stringify(response, null, 2));
}
run();Local Example
Run the local SDK example:
OPENROUTER_API_KEY=sk-or-v1-xxxx npm run example:run -- "Today's top news in AI"Run dedicated non-stream and stream examples:
OPENROUTER_API_KEY=sk-or-v1-xxxx npm run example:non-stream -- "What is OpenRouter?"
OPENROUTER_API_KEY=sk-or-v1-xxxx npm run example:stream -- "What is OpenRouter?"Files:
example/index.tsexample app using local source (../src)example/non-stream.tsnon-stream response exampleexample/stream.tsstream response exampletsconfig.example.jsonexample build config
API
new OpenRef(config)
config.llm
apiKey?: stringOpenRouter API key. Preferred key location.chatModel?: stringPrimary chat model.fallbackChatModels?: string[]Models used if primary model fails.systemPrompt?: stringBase system instruction for response style/behavior.citationStrictness?: booleanCitation policy in response text.maxRetries?: numberRetry attempts per model request.retryDelayMs?: numberBackoff delay between retries.maxOutputTokens?: numberMax tokens per generation request.maxContinuationRequests?: numberExtra continuation requests when output is truncated.
citationStrictness behavior:
true(default): model is instructed to include inline[N]citations for factual claims.false: model is instructed to avoid[N]citations unless user explicitly asks.
config.search
preferLatest?: booleanAdds recency bias to search and prompting.timeZone?: stringUsed for date context formatting.maxSources?: numberFinal number of sources to keep.queryExpansion?: booleanExpand user query into subqueries using LLM before retrieval.queryExpansionValue?: numberNumber of expanded subqueries (0-5).queryExpansionTimeout?: numberMax expansion wait time in ms.engineProvider?: { provider?: "brave" | "duckduckgo" | "bing" | "searxng" | "searxncg" | Array<...>, queryUrl?: string }Choose preferred engine order. If one provider is given (e.g."brave"), OpenRef auto-falls back to the other mainstream engines. Forprovider: "searxng", you can pass a customqueryUrl(for examplehttp://localhost:8080/search?q={query}).searchTimeout?: numberSearch request timeout in ms.enableReranking?: booleanEnable LLM reranking for candidates.rerankTimeout?: numberReranking timeout in ms.
config.retrieval
contentTimeout?: numberPage fetch/extraction timeout in ms.maxContextTokens?: numberToken budget for assembled context.chunkTargetTokens?: numberApproximate target size of chunks.
config.response
stream?: booleanDefault chat mode (truefor event stream,falsefor aggregated response).
search(query: string): Promise<SearchResult>
Runs retrieval and ranking only.
chat(query: string, options?)
Per-request options:
stream?: booleansystemPrompt?: stringOverrides constructorllm.systemPrompt.citationStrictness?: booleanOverrides constructorllm.citationStrictness.
When stream: true, returns AsyncGenerator<ChatEvent> with:
expanded_queries(optional, early event when enabled)sourcestext(multiple chunks)citationsdone
When stream: false, returns Promise<ChatResponse> with:
textsourcescitationMapchatTokenUsagemetadata
search() and chat(..., { stream: false }) include metadata.expandedQueries when query expansion is enabled.
Legacy Config Support
Top-level fields like openRouterApiKey, chatModel, maxSources, etc. are still accepted for backward compatibility.
Preferred format is grouped config (llm, search, retrieval, response).
Test Before Publish
Run full pre-publish checks:
npm run checkThis runs:
npm run typecheck(tsc --noEmit)npm run test:smoke(build + SDK smoke checks)npm run test:pack(build +npm packinstall/import verification in a temp project)
test:pack tries a real temp-project install first. If npm registry access is unavailable, it automatically runs an offline tarball import fallback.
Smoke Test Modes
Without
OPENROUTER_API_KEY:- validates constructor/config compatibility (grouped + legacy)
- validates API surface (
search,chat) - skips live network requests
With
OPENROUTER_API_KEY:- runs live
search - runs
chatnon-stream - runs
chatstream and confirmsdoneevent
- runs live
Example:
OPENROUTER_API_KEY=sk-or-v1-xxxx npm run test:smokeNotes
querymust be a non-empty string.- If no sources are found,
chatreturns a graceful text response with empty citation map. - OpenRef uses OpenRouter-compatible chat models for reranking and response generation.
