MCPcopy Create free account
hub / github.com/OpenAnonymity/oa-chat / OpenRouterAPI

Class OpenRouterAPI

chat/api.js:46–1197  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44// Example:
45// const getSystemPrompt = () => `You are a helpful AI assistant.
46
47function hasPdfContent(messages = []) {
48 return messages.some((message) => {
49 if (!Array.isArray(message?.content)) return false;
50 return message.content.some((part) => {
51 const filename = part?.file?.filename || '';
52 const fileData = part?.file?.file_data || '';
53 return part?.type === 'file' && (
54 /\.pdf$/i.test(filename) ||
55 (typeof fileData === 'string' && fileData.startsWith('data:application/pdf'))
56 );
57 });
58 });
59}
60
61function applyPdfFileParserPlugin(body, messages = []) {
62 if (!hasPdfContent(messages)) return;
63 body.plugins = [
64 {
65 id: 'file-parser',
66 pdf: {
67 engine: 'mistral-ocr'
68 }
69 }
70 ];
71}
72
73class OpenRouterAPI {
74 constructor() {
75 this.baseUrl = 'https://openrouter.ai/api/v1';
76
77 // Load display name overrides from centralized config
78 const defaults = getDefaultModelConfig();
79 this.displayNameOverrides = { ...defaults.displayNameOverrides };
80 }
81
82 // Get API key - only use ticket-based key
83 getApiKey() {
84 const key = apiKeyStore.getApiKey();
85 if (!key) return null;
86 if (apiKeyStore.isExpired()) return null;
87 return key;
88 }
89
90 getCachedModels() {
91 const cachedModels = loadModelCatalog(OPENROUTER_BACKEND_ID);
92 if (!Array.isArray(cachedModels)) {
93 return [];
94 }
95 return normalizeOpenRouterModelProviders(cachedModels).map(model => ({
96 ...model,
97 name: this.getDisplayName(model.id, model.name, model.provider)
98 }));
99 }
100
101 // Fetch available models from OpenRouter
102 async fetchModels() {
103 const url = `${this.baseUrl}/models`;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected