Add web search functionality and update dependencies in noThinkProxy

This commit is contained in:
2026-05-10 19:13:06 +02:00
parent af75920edc
commit 4fad0f22c7
4 changed files with 411 additions and 203 deletions

466
app.js
View File

@@ -1,10 +1,14 @@
const express = require('express'); const express = require('express');
const TurndownService = require('turndown');
const { parse: parseHtml } = require('node-html-parser');
const app = express(); const app = express();
const OLLAMA_URL = process.env.OLLAMA_URL || 'https://ollama.aquantico.de/api/chat'; const OLLAMA_URL = process.env.OLLAMA_URL || 'https://ollama.aquantico.de/api/chat';
const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'qwen3.6:35b-a3b-q4_K_M'; const OLLAMA_MODEL = process.env.OLLAMA_MODEL || 'qwen3.6:35b-a3b-q4_K_M';
const OLLAMA_AUTH = process.env.OLLAMA_AUTH || '324GF44-50AA-4B57-9386-K435DLJ764DFR'; const OLLAMA_AUTH = process.env.OLLAMA_AUTH || '324GF44-50AA-4B57-9386-K435DLJ764DFR';
const PORT = parseInt(process.env.PORT || '11435', 10); const GOOGLE_API_KEY = process.env.GOOGLE_API_KEY || 'AIzaSyChzsz8ZN8iHRqMUVFnxJXwyXWP_XwWy6g';
const GOOGLE_CX = process.env.GOOGLE_CX || '2331819c76d874bcc';
const PORT = parseInt(process.env.PORT || '11435', 10);
const colors = { const colors = {
reset: '\x1b[0m', reset: '\x1b[0m',
@@ -43,12 +47,11 @@ app.get('/', (req, res) => {
table{width:100%;border-collapse:collapse;margin:.5rem 0 1rem} table{width:100%;border-collapse:collapse;margin:.5rem 0 1rem}
td,th{border:1px solid #333;padding:.4rem .8rem;text-align:left} td,th{border:1px solid #333;padding:.4rem .8rem;text-align:left}
th{background:#1e1e2e;color:#7dd3fc} th{background:#1e1e2e;color:#7dd3fc}
.copy-btn{cursor:pointer;background:#2d2d4e;border:1px solid #555;color:#cba6f7;padding:.2rem .6rem;border-radius:.25rem;font-size:.75rem;margin-left:.5rem}
</style> </style>
</head> </head>
<body> <body>
<h1>noThinkProxy <span class="badge">v1.0</span></h1> <h1>noThinkProxy <span class="badge">v1.0</span></h1>
<p>Anthropic-API → Ollama-Proxy · Think-Modus deaktiviert · Modell-Substitution aktiv</p> <p>Anthropic-API → Ollama-Proxy · Think-Modus deaktiviert · Web-Suche aktiv</p>
<h2>Aktuelle Konfiguration</h2> <h2>Aktuelle Konfiguration</h2>
<table> <table>
@@ -57,6 +60,7 @@ app.get('/', (req, res) => {
<tr><td>Modell</td><td><code>${OLLAMA_MODEL}</code></td></tr> <tr><td>Modell</td><td><code>${OLLAMA_MODEL}</code></td></tr>
<tr><td>Kontext</td><td><code>262144 Token (256k)</code></td></tr> <tr><td>Kontext</td><td><code>262144 Token (256k)</code></td></tr>
<tr><td>Think</td><td><code>false</code></td></tr> <tr><td>Think</td><td><code>false</code></td></tr>
<tr><td>Web-Suche</td><td><code>Google Custom Search</code></td></tr>
<tr><td>Proxy-URL</td><td><code>${host}</code></td></tr> <tr><td>Proxy-URL</td><td><code>${host}</code></td></tr>
</table> </table>
@@ -70,7 +74,7 @@ app.get('/', (req, res) => {
<h2>API-Endpunkt</h2> <h2>API-Endpunkt</h2>
<pre><code>POST ${host}/v1/messages</code></pre> <pre><code>POST ${host}/v1/messages</code></pre>
<p>Kompatibel mit dem Anthropic SDK. Alle <code>claude-*</code> Modellnamen werden automatisch auf <code>${OLLAMA_MODEL}</code> umgeleitet.</p> <p>Alle <code>claude-*</code> Modellnamen werden auf <code>${OLLAMA_MODEL}</code> umgeleitet.</p>
</body> </body>
</html>`); </html>`);
}); });
@@ -131,26 +135,108 @@ echo ""
`); `);
}); });
// ── Web Search ────────────────────────────────────────────────────────────────
const WEB_SEARCH_TOOL = {
type: 'function',
function: {
name: 'web_search',
description: 'Searches the internet for current information. Use this when you need up-to-date facts, recent news, or information you are not certain about.',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'The search query' }
},
required: ['query']
}
}
};
const READ_URL_TOOL = {
type: 'function',
function: {
name: 'read_url',
description: 'Fetches a URL and returns the page content as Markdown. Use this to read articles, documentation, or any web page in detail.',
parameters: {
type: 'object',
properties: {
url: { type: 'string', description: 'The URL to fetch' }
},
required: ['url']
}
}
};
async function executeReadUrl(url) {
console.log(`${colors.cyan}[Read URL] ${url}${colors.reset}`);
try {
const resp = await fetch(url, {
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; noThinkProxy/1.0)' },
signal: AbortSignal.timeout(15000)
});
if (!resp.ok) return `Error fetching ${url}: HTTP ${resp.status}`;
const html = await resp.text();
const root = parseHtml(html, { blockTextElements: { script: false, style: false } });
// Rauschen entfernen
for (const sel of ['script', 'style', 'nav', 'header', 'footer', 'aside', 'iframe', 'noscript']) {
root.querySelectorAll(sel).forEach(el => el.remove());
}
const title = root.querySelector('title')?.text.trim()
|| root.querySelector('h1')?.text.trim()
|| '';
// Hauptinhalt extrahieren
const mainEl = root.querySelector('main, article, [role="main"], #content, #main, .content, .post');
const contentHtml = mainEl ? mainEl.innerHTML : (root.querySelector('body')?.innerHTML || html);
const td = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced' });
let markdown = `# ${title}\n\n${td.turndown(contentHtml)}`;
if (markdown.length > 12000) {
markdown = markdown.substring(0, 12000) + '\n\n[... content truncated ...]';
}
console.log(`${colors.cyan}[Read URL] ${markdown.length} Zeichen${colors.reset}`);
return markdown;
} catch (e) {
return `Error fetching ${url}: ${e.message}`;
}
}
async function executeWebSearch(query) {
const url = `https://www.googleapis.com/customsearch/v1?key=${GOOGLE_API_KEY}&cx=${GOOGLE_CX}&q=${encodeURIComponent(query)}&num=5`;
console.log(`${colors.cyan}[Web Search] "${query}"${colors.reset}`);
try {
const resp = await fetch(url);
const data = await resp.json();
if (data.error) return `Search error: ${data.error.message}`;
if (!data.items?.length) return `No results found for "${query}".`;
const results = data.items
.map((item, i) => `[${i + 1}] ${item.title}\n${item.link}\n${item.snippet}`)
.join('\n\n');
console.log(`${colors.cyan}[Web Search] ${data.items.length} Ergebnisse${colors.reset}`);
return results;
} catch (e) {
return `Search error: ${e.message}`;
}
}
// ── Hilfsfunktionen ─────────────────────────────────────────────────────────── // ── Hilfsfunktionen ───────────────────────────────────────────────────────────
function sanitizeToolSchema(schema) { function sanitizeToolSchema(schema) {
if (!schema || typeof schema !== 'object') { if (!schema || typeof schema !== 'object') return { type: 'object', properties: {} };
return { type: 'object', properties: {} };
}
const clean = JSON.parse(JSON.stringify(schema)); const clean = JSON.parse(JSON.stringify(schema));
if (!clean.type) clean.type = 'object'; if (!clean.type) clean.type = 'object';
if (!clean.properties) clean.properties = {}; if (!clean.properties) clean.properties = {};
return clean; return clean;
} }
function convertAnthropicTools(anthropicTools) { function convertAnthropicTools(anthropicTools) {
if (!anthropicTools || anthropicTools.length === 0) return []; if (!anthropicTools || anthropicTools.length === 0) return [];
const validTools = []; const validTools = [];
for (const tool of anthropicTools) { for (const tool of anthropicTools) {
try { try {
const ollamaTool = { const ollamaTool = {
@@ -161,30 +247,24 @@ function convertAnthropicTools(anthropicTools) {
parameters: sanitizeToolSchema(tool.input_schema) parameters: sanitizeToolSchema(tool.input_schema)
} }
}; };
JSON.stringify(ollamaTool); JSON.stringify(ollamaTool);
validTools.push(ollamaTool); validTools.push(ollamaTool);
} catch (e) { } catch (e) {
console.error(`${colors.red}[Tool Schema Error] ${e.message}${colors.reset}`); console.error(`${colors.red}[Tool Schema Error] ${e.message}${colors.reset}`);
} }
} }
return validTools; return validTools;
} }
function stringifyToolResultContent(content) { function stringifyToolResultContent(content) {
if (Array.isArray(content)) { if (Array.isArray(content)) {
return content return content.map(c => {
.map(c => { if (typeof c === 'string') return c;
if (typeof c === 'string') return c; if (c?.text) return c.text;
if (c?.text) return c.text; return JSON.stringify(c);
return JSON.stringify(c); }).join('\n');
})
.join('\n');
} }
if (typeof content === 'string') return content; if (typeof content === 'string') return content;
return JSON.stringify(content); return JSON.stringify(content);
} }
@@ -194,10 +274,9 @@ function convertAnthropicToOllama(anthropicBody) {
if (anthropicBody.system) { if (anthropicBody.system) {
ollamaMessages.push({ ollamaMessages.push({
role: 'system', role: 'system',
content: content: typeof anthropicBody.system === 'string'
typeof anthropicBody.system === 'string' ? anthropicBody.system
? anthropicBody.system : JSON.stringify(anthropicBody.system)
: JSON.stringify(anthropicBody.system)
}); });
} }
@@ -206,35 +285,23 @@ function convertAnthropicToOllama(anthropicBody) {
ollamaMessages.push({ role: msg.role, content: msg.content }); ollamaMessages.push({ role: msg.role, content: msg.content });
continue; continue;
} }
if (!Array.isArray(msg.content)) continue; if (!Array.isArray(msg.content)) continue;
if (msg.role === 'assistant') { if (msg.role === 'assistant') {
const textParts = []; const textParts = [];
const toolCalls = []; const toolCalls = [];
for (const item of msg.content) { for (const item of msg.content) {
if (item.type === 'text') { if (item.type === 'text') textParts.push(item.text || '');
textParts.push(item.text || ''); else if (item.type === 'tool_use') {
} else if (item.type === 'tool_use') { toolCalls.push({ function: { name: item.name, arguments: item.input || {} } });
toolCalls.push({
function: {
name: item.name,
arguments: item.input || {}
}
});
} }
} }
const assistantMsg = { role: 'assistant', content: textParts.join('\n\n') }; const assistantMsg = { role: 'assistant', content: textParts.join('\n\n') };
if (toolCalls.length > 0) { if (toolCalls.length > 0) assistantMsg.tool_calls = toolCalls;
assistantMsg.tool_calls = toolCalls;
}
ollamaMessages.push(assistantMsg); ollamaMessages.push(assistantMsg);
} else { } else {
const pendingText = []; const pendingText = [];
for (const item of msg.content) { for (const item of msg.content) {
if (item.type === 'text') { if (item.type === 'text') {
pendingText.push(item.text || ''); pendingText.push(item.text || '');
@@ -243,16 +310,12 @@ function convertAnthropicToOllama(anthropicBody) {
ollamaMessages.push({ role: 'user', content: pendingText.join('\n\n') }); ollamaMessages.push({ role: 'user', content: pendingText.join('\n\n') });
pendingText.length = 0; pendingText.length = 0;
} }
const resultText = stringifyToolResultContent(item.content); const resultText = stringifyToolResultContent(item.content);
console.log(`${colors.blue}📥 Tool Result ${item.tool_use_id}:${colors.reset}`); console.log(`${colors.blue}📥 Tool Result ${item.tool_use_id}:${colors.reset}`);
console.log(`${colors.blue}${resultText}${colors.reset}`); console.log(`${colors.blue}${resultText}${colors.reset}\n`);
console.log('');
ollamaMessages.push({ role: 'tool', content: resultText }); ollamaMessages.push({ role: 'tool', content: resultText });
} }
} }
if (pendingText.length > 0) { if (pendingText.length > 0) {
ollamaMessages.push({ role: 'user', content: pendingText.join('\n\n') }); ollamaMessages.push({ role: 'user', content: pendingText.join('\n\n') });
} }
@@ -271,55 +334,39 @@ function convertAnthropicToOllama(anthropicBody) {
} }
}; };
if (anthropicBody.tools && anthropicBody.tools.length > 0) { // Anthropic-Tools konvertieren + interne Tools voranstellen
const validTools = convertAnthropicTools(anthropicBody.tools); const convertedTools = convertAnthropicTools(anthropicBody.tools || []);
ollamaBody.tools = [WEB_SEARCH_TOOL, READ_URL_TOOL, ...convertedTools];
if (validTools.length > 0) {
ollamaBody.tools = validTools;
}
}
return ollamaBody; return ollamaBody;
} }
function parseToolArguments(args) { function parseToolArguments(args) {
if (!args) return {}; if (!args) return {};
if (typeof args === 'string') { if (typeof args === 'string') {
try { try { return JSON.parse(args); } catch (e) { return {}; }
return JSON.parse(args);
} catch (e) {
console.error(`${colors.red}[Tool Args Parse Error] ${e.message}${colors.reset}`);
return {};
}
} }
if (typeof args === 'object') return args; if (typeof args === 'object') return args;
return {}; return {};
} }
function makeToolDedupeKey(tc) { function makeToolDedupeKey(tc) {
const name = tc.function?.name || ''; const name = tc.function?.name || '';
const args = tc.function?.arguments || {}; const args = tc.function?.arguments || {};
const argsString = typeof args === 'string' ? args : JSON.stringify(args); return `${name}:${typeof args === 'string' ? args : JSON.stringify(args)}`;
return `${name}:${argsString}`;
} }
// ── Response-Handler ────────────────────────────────────────────────────────── // ── Response-Handler mit Web-Search-Loop ──────────────────────────────────────
async function handleResponse(response, anthropicBody, res, requestNum) { async function handleResponse(initialResponse, anthropicBody, res, requestNum, ollamaBody) {
res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive'); res.setHeader('Connection', 'keep-alive');
const messageId = 'msg_' + requestNum;
res.write(`event: message_start\ndata: ${JSON.stringify({ res.write(`event: message_start\ndata: ${JSON.stringify({
type: 'message_start', type: 'message_start',
message: { message: {
id: messageId, id: 'msg_' + requestNum,
type: 'message', type: 'message',
role: 'assistant', role: 'assistant',
content: [], content: [],
@@ -330,153 +377,171 @@ async function handleResponse(response, anthropicBody, res, requestNum) {
} }
})}\n\n`); })}\n\n`);
const reader = response.body.getReader();
const decoder = new TextDecoder();
let contentBlocks = [];
let currentBlockIndex = 0; let currentBlockIndex = 0;
const seenToolCalls = new Set();
let emittedToolUse = false; let emittedToolUse = false;
let messageFinished = false; const seenToolCalls = new Set();
let buffer = ''; let currentResponse = initialResponse;
function processChunk(data) { for (let searchIteration = 0; searchIteration < 5; searchIteration++) {
if (messageFinished) return; const reader = currentResponse.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let textBlockOpen = false;
let evalCount = 0;
if (data.message?.tool_calls && data.message.tool_calls.length > 0) { // Gesammelt pro Iteration für eventuelle Web-Search-Fortsetzung
for (const tc of data.message.tool_calls) { let iterContent = '';
const dedupeKey = makeToolDedupeKey(tc); let iterAllToolCalls = [];
let iterWebSearches = [];
if (seenToolCalls.has(dedupeKey)) { function processChunk(data) {
console.log(`${colors.yellow}[Duplicate Tool Call skipped] ${dedupeKey}${colors.reset}`); // Tool Calls
continue; if (data.message?.tool_calls?.length) {
for (const tc of data.message.tool_calls) {
iterAllToolCalls.push(tc);
// Proxy-eigene Tools + Claude Code Web-Tools intern abfangen
const INTERNAL = new Set(['web_search', 'read_url', 'WebSearch', 'web_fetch', 'WebFetch']);
if (INTERNAL.has(tc.function?.name)) {
iterWebSearches.push(tc);
continue; // intern verarbeiten, nicht an Client senden
}
const key = makeToolDedupeKey(tc);
if (seenToolCalls.has(key)) {
console.log(`${colors.yellow}[Duplicate Tool Call skipped] ${key}${colors.reset}`);
continue;
}
seenToolCalls.add(key);
emittedToolUse = true;
const toolName = tc.function?.name;
const toolInput = parseToolArguments(tc.function?.arguments);
const toolUseId = `toolu_${requestNum}_${currentBlockIndex}`;
console.log(`${colors.magenta}[Tool Use: ${toolName}] ${JSON.stringify(toolInput)}${colors.reset}`);
res.write(`event: content_block_start\ndata: ${JSON.stringify({
type: 'content_block_start', index: currentBlockIndex,
content_block: { type: 'tool_use', id: toolUseId, name: toolName, input: {} }
})}\n\n`);
res.write(`event: content_block_delta\ndata: ${JSON.stringify({
type: 'content_block_delta', index: currentBlockIndex,
delta: { type: 'input_json_delta', partial_json: JSON.stringify(toolInput) }
})}\n\n`);
res.write(`event: content_block_stop\ndata: ${JSON.stringify({
type: 'content_block_stop', index: currentBlockIndex
})}\n\n`);
currentBlockIndex++;
}
}
// Text
if (data.message?.content) {
const text = data.message.content;
iterContent += text;
if (!textBlockOpen) {
res.write(`event: content_block_start\ndata: ${JSON.stringify({
type: 'content_block_start', index: currentBlockIndex,
content_block: { type: 'text', text: '' }
})}\n\n`);
textBlockOpen = true;
} }
seenToolCalls.add(dedupeKey); process.stdout.write(`${colors.green}${text}${colors.reset}`);
emittedToolUse = true;
const toolName = tc.function?.name;
const toolInput = parseToolArguments(tc.function?.arguments);
const toolUseId = `toolu_${requestNum}_${currentBlockIndex}`;
console.log(`${colors.yellow}[Raw Tool Call] ${JSON.stringify(tc)}${colors.reset}`);
console.log(`${colors.magenta}[Sending Tool Use: ${toolName}]${colors.reset}`);
console.log(`${colors.magenta}Input: ${JSON.stringify(toolInput)}${colors.reset}`);
res.write(`event: content_block_start\ndata: ${JSON.stringify({
type: 'content_block_start',
index: currentBlockIndex,
content_block: { type: 'tool_use', id: toolUseId, name: toolName, input: {} }
})}\n\n`);
res.write(`event: content_block_delta\ndata: ${JSON.stringify({ res.write(`event: content_block_delta\ndata: ${JSON.stringify({
type: 'content_block_delta', type: 'content_block_delta', index: currentBlockIndex,
index: currentBlockIndex, delta: { type: 'text_delta', text }
delta: { type: 'input_json_delta', partial_json: JSON.stringify(toolInput) }
})}\n\n`); })}\n\n`);
}
res.write(`event: content_block_stop\ndata: ${JSON.stringify({ // Done
type: 'content_block_stop', if (data.done) {
index: currentBlockIndex evalCount = data.eval_count || 0;
})}\n\n`); if (textBlockOpen) {
res.write(`event: content_block_stop\ndata: ${JSON.stringify({
currentBlockIndex++; type: 'content_block_stop', index: currentBlockIndex
})}\n\n`);
currentBlockIndex++;
textBlockOpen = false;
}
} }
} }
if (data.message?.content) { // Stream lesen
const text = data.message.content; while (true) {
const { done, value } = await reader.read();
if (contentBlocks[currentBlockIndex] === undefined) { if (done) break;
res.write(`event: content_block_start\ndata: ${JSON.stringify({ buffer += decoder.decode(value, { stream: true });
type: 'content_block_start', const lines = buffer.split('\n');
index: currentBlockIndex, buffer = lines.pop() || '';
content_block: { type: 'text', text: '' } for (const line of lines) {
})}\n\n`); const trimmed = line.trim();
if (!trimmed) continue;
contentBlocks[currentBlockIndex] = ''; try { processChunk(JSON.parse(trimmed)); }
catch (e) { console.error(`${colors.red}[Parse Error] ${e.message}${colors.reset}`); }
} }
}
if (buffer.trim()) {
try { processChunk(JSON.parse(buffer.trim())); }
catch (e) { console.error(`${colors.red}[Final Buffer Error] ${e.message}${colors.reset}`); }
}
process.stdout.write(`${colors.green}${text}${colors.reset}`); // Offenen Text-Block schließen falls Ollama keinen done:true geschickt hat
if (textBlockOpen) {
res.write(`event: content_block_delta\ndata: ${JSON.stringify({ res.write(`event: content_block_stop\ndata: ${JSON.stringify({
type: 'content_block_delta', type: 'content_block_stop', index: currentBlockIndex
index: currentBlockIndex,
delta: { type: 'text_delta', text }
})}\n\n`); })}\n\n`);
currentBlockIndex++;
contentBlocks[currentBlockIndex] += text; textBlockOpen = false;
} }
if (data.done) { // Keine Web-Suchen → finale Events senden
messageFinished = true; if (iterWebSearches.length === 0) {
if (contentBlocks[currentBlockIndex] !== undefined) {
res.write(`event: content_block_stop\ndata: ${JSON.stringify({
type: 'content_block_stop',
index: currentBlockIndex
})}\n\n`);
}
res.write(`event: message_delta\ndata: ${JSON.stringify({ res.write(`event: message_delta\ndata: ${JSON.stringify({
type: 'message_delta', type: 'message_delta',
delta: { stop_reason: emittedToolUse ? 'tool_use' : 'end_turn' }, delta: { stop_reason: emittedToolUse ? 'tool_use' : 'end_turn' },
usage: { output_tokens: data.eval_count || 0 } usage: { output_tokens: evalCount }
})}\n\n`); })}\n\n`);
res.write(`event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop' })}\n\n`); res.write(`event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop' })}\n\n`);
console.log(`${colors.green}${colors.reset}\n`); console.log(`${colors.green}${colors.reset}\n`);
break;
} }
}
while (true) { // Web-Suchen ausführen und Ergebnisse in Messages einbauen
const { done, value } = await reader.read(); ollamaBody.messages.push({
if (done) break; role: 'assistant',
content: iterContent,
tool_calls: iterAllToolCalls
});
buffer += decoder.decode(value, { stream: true }); for (const tc of iterWebSearches) {
const args = parseToolArguments(tc.function?.arguments);
const lines = buffer.split('\n'); let result;
buffer = lines.pop() || ''; const name = tc.function?.name;
if (name === 'web_search' || name === 'WebSearch') {
for (const line of lines) { result = await executeWebSearch(args?.query || args?.q || '');
const trimmed = line.trim(); } else if (name === 'read_url' || name === 'web_fetch' || name === 'WebFetch') {
if (!trimmed) continue; result = await executeReadUrl(args?.url || '');
try {
processChunk(JSON.parse(trimmed));
} catch (e) {
console.error(`${colors.red}[Stream Parse Error] ${e.message}${colors.reset}`);
console.error(`${colors.red}${line}${colors.reset}`);
} }
} ollamaBody.messages.push({ role: 'tool', content: result ?? `Tool '${name}' returned no result.` });
}
if (buffer.trim()) {
try {
processChunk(JSON.parse(buffer.trim()));
} catch (e) {
console.error(`${colors.red}[Final Buffer Parse Error] ${e.message}${colors.reset}`);
console.error(buffer);
}
}
if (!messageFinished) {
if (contentBlocks[currentBlockIndex] !== undefined) {
res.write(`event: content_block_stop\ndata: ${JSON.stringify({
type: 'content_block_stop',
index: currentBlockIndex
})}\n\n`);
} }
res.write(`event: message_delta\ndata: ${JSON.stringify({ // Neuer Ollama-Request (weiter streamen)
type: 'message_delta', currentResponse = await fetch(OLLAMA_URL, {
delta: { stop_reason: emittedToolUse ? 'tool_use' : 'end_turn' }, method: 'POST',
usage: { output_tokens: 0 } headers: {
})}\n\n`); 'Content-Type': 'application/json',
'Authorization': `Bearer ${OLLAMA_AUTH}`
},
body: JSON.stringify(ollamaBody)
});
res.write(`event: message_stop\ndata: ${JSON.stringify({ type: 'message_stop' })}\n\n`); if (!currentResponse.ok) {
const errText = await currentResponse.text();
throw new Error(`Ollama: ${currentResponse.status} ${errText}`);
}
} }
res.end(); res.end();
@@ -486,7 +551,6 @@ async function handleResponse(response, anthropicBody, res, requestNum) {
app.post('/v1/messages', async (req, res) => { app.post('/v1/messages', async (req, res) => {
const requestNum = Date.now(); const requestNum = Date.now();
console.log(`${colors.magenta}━━━ #${requestNum} ━━━${colors.reset}`); console.log(`${colors.magenta}━━━ #${requestNum} ━━━${colors.reset}`);
try { try {
@@ -499,7 +563,7 @@ app.post('/v1/messages', async (req, res) => {
const ollamaBody = convertAnthropicToOllama(anthropicBody); const ollamaBody = convertAnthropicToOllama(anthropicBody);
console.log( console.log(
`${colors.magenta}[msgs=${ollamaBody.messages.length}, tools=${ollamaBody.tools?.length || 0}, ctx=256k, think=false, model=${OLLAMA_MODEL}]${colors.reset}` `${colors.magenta}[msgs=${ollamaBody.messages.length}, tools=${ollamaBody.tools.length}, ctx=256k, think=false]${colors.reset}`
); );
const response = await fetch(OLLAMA_URL, { const response = await fetch(OLLAMA_URL, {
@@ -513,14 +577,13 @@ app.post('/v1/messages', async (req, res) => {
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
console.error(`${colors.red}${errorText}${colors.reset}`); throw new Error(`Ollama: ${response.status} ${errorText}`);
throw new Error(`Ollama: ${response.status}`);
} }
return handleResponse(response, anthropicBody, res, requestNum); return handleResponse(response, anthropicBody, res, requestNum, ollamaBody);
} catch (error) { } catch (error) {
console.error(`${colors.red}${error.message}${colors.reset}`); console.error(`${colors.red}${error.message}${colors.reset}`);
if (!res.headersSent) { if (!res.headersSent) {
res.status(500).json({ res.status(500).json({
type: 'error', type: 'error',
@@ -534,7 +597,8 @@ app.post('/v1/messages', async (req, res) => {
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`${colors.magenta}noThinkProxy: localhost:${PORT}${colors.reset}`); console.log(`${colors.magenta}noThinkProxy: localhost:${PORT}${colors.reset}`);
console.log(`${colors.cyan} Ollama : ${OLLAMA_URL}${colors.reset}`); console.log(`${colors.cyan} Ollama : ${OLLAMA_URL}${colors.reset}`);
console.log(`${colors.cyan} Modell : ${OLLAMA_MODEL}${colors.reset}`); console.log(`${colors.cyan} Modell : ${OLLAMA_MODEL}${colors.reset}`);
console.log(`${colors.cyan} Ctx : 256k Think: false${colors.reset}\n`); console.log(`${colors.cyan} Ctx : 256k Think: false${colors.reset}`);
console.log(`${colors.cyan} Search : Google Custom Search${colors.reset}\n`);
}); });

View File

@@ -10,6 +10,8 @@ services:
- OLLAMA_URL=https://ollama.aquantico.de/api/chat - OLLAMA_URL=https://ollama.aquantico.de/api/chat
- OLLAMA_MODEL=qwen3.6:35b-a3b-q4_K_M - OLLAMA_MODEL=qwen3.6:35b-a3b-q4_K_M
- OLLAMA_AUTH=324GF44-50AA-4B57-9386-K435DLJ764DFR - OLLAMA_AUTH=324GF44-50AA-4B57-9386-K435DLJ764DFR
- GOOGLE_API_KEY=AIzaSyChzsz8ZN8iHRqMUVFnxJXwyXWP_XwWy6g
- GOOGLE_CX=2331819c76d874bcc
labels: labels:
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.nothinkproxy.rule=Host(`claude-proxy.aquantico.lan`)" - "traefik.http.routers.nothinkproxy.rule=Host(`claude-proxy.aquantico.lan`)"

142
package-lock.json generated
View File

@@ -6,9 +6,16 @@
"": { "": {
"dependencies": { "dependencies": {
"express": "^5.2.1", "express": "^5.2.1",
"node-fetch": "^3.3.2" "node-fetch": "^3.3.2",
"node-html-parser": "^7.1.0",
"turndown": "^7.2.4"
} }
}, },
"node_modules/@mixmark-io/domino": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@mixmark-io/domino/-/domino-2.2.0.tgz",
"integrity": "sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw=="
},
"node_modules/accepts": { "node_modules/accepts": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz", "resolved": "https://registry.npmjs.org/accepts/-/accepts-2.0.0.tgz",
@@ -44,6 +51,11 @@
"url": "https://opencollective.com/express" "url": "https://opencollective.com/express"
} }
}, },
"node_modules/boolbase": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
"integrity": "sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww=="
},
"node_modules/bytes": { "node_modules/bytes": {
"version": "3.1.2", "version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
@@ -115,6 +127,32 @@
"node": ">=6.6.0" "node": ">=6.6.0"
} }
}, },
"node_modules/css-select": {
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/css-select/-/css-select-5.2.2.tgz",
"integrity": "sha512-TizTzUddG/xYLA3NXodFM0fSbNizXjOKhqiQQwvhlspadZokn1KDy0NZFS0wuEubIYAV5/c1/lAr0TaaFXEXzw==",
"dependencies": {
"boolbase": "^1.0.0",
"css-what": "^6.1.0",
"domhandler": "^5.0.2",
"domutils": "^3.0.1",
"nth-check": "^2.0.1"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/css-what": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.2.2.tgz",
"integrity": "sha512-u/O3vwbptzhMs3L1fQE82ZSLHQQfto5gyZzwteVIEyeaY5Fc7R4dapF/BvRoSYFeqfBk4m0V1Vafq5Pjv25wvA==",
"engines": {
"node": ">= 6"
},
"funding": {
"url": "https://github.com/sponsors/fb55"
}
},
"node_modules/data-uri-to-buffer": { "node_modules/data-uri-to-buffer": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
@@ -147,6 +185,57 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/dom-serializer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
"integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==",
"dependencies": {
"domelementtype": "^2.3.0",
"domhandler": "^5.0.2",
"entities": "^4.2.0"
},
"funding": {
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
}
},
"node_modules/domelementtype": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.3.0.tgz",
"integrity": "sha512-OLETBj6w0OsagBwdXnPdN0cnMfF9opN69co+7ZrbfPGrdpPVNBUj02spi6B1N7wChLQiPn4CSH/zJvXw56gmHw==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fb55"
}
]
},
"node_modules/domhandler": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz",
"integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==",
"dependencies": {
"domelementtype": "^2.3.0"
},
"engines": {
"node": ">= 4"
},
"funding": {
"url": "https://github.com/fb55/domhandler?sponsor=1"
}
},
"node_modules/domutils": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-3.2.2.tgz",
"integrity": "sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==",
"dependencies": {
"dom-serializer": "^2.0.0",
"domelementtype": "^2.3.0",
"domhandler": "^5.0.3"
},
"funding": {
"url": "https://github.com/fb55/domutils?sponsor=1"
}
},
"node_modules/dunder-proto": { "node_modules/dunder-proto": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
@@ -173,6 +262,17 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/entities": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
"engines": {
"node": ">=0.12"
},
"funding": {
"url": "https://github.com/fb55/entities?sponsor=1"
}
},
"node_modules/es-define-property": { "node_modules/es-define-property": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
@@ -400,6 +500,14 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"bin": {
"he": "bin/he"
}
},
"node_modules/http-errors": { "node_modules/http-errors": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz", "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
@@ -551,6 +659,26 @@
"url": "https://opencollective.com/node-fetch" "url": "https://opencollective.com/node-fetch"
} }
}, },
"node_modules/node-html-parser": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/node-html-parser/-/node-html-parser-7.1.0.tgz",
"integrity": "sha512-iJo8b2uYGT40Y8BTyy5ufL6IVbN8rbm/1QK2xffXU/1a/v3AAa0d1YAoqBNYqaS4R/HajkWIpIfdE6KcyFh1AQ==",
"dependencies": {
"css-select": "^5.1.0",
"he": "1.2.0"
}
},
"node_modules/nth-check": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/nth-check/-/nth-check-2.1.1.tgz",
"integrity": "sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==",
"dependencies": {
"boolbase": "^1.0.0"
},
"funding": {
"url": "https://github.com/fb55/nth-check?sponsor=1"
}
},
"node_modules/object-inspect": { "node_modules/object-inspect": {
"version": "1.13.4", "version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
@@ -798,6 +926,18 @@
"node": ">=0.6" "node": ">=0.6"
} }
}, },
"node_modules/turndown": {
"version": "7.2.4",
"resolved": "https://registry.npmjs.org/turndown/-/turndown-7.2.4.tgz",
"integrity": "sha512-I8yFsfRzmzK0WV1pNNOA4A7y4RDfFxPRxb3t+e3ui14qSGOxGtiSP6GjeX+Y6CHb7HYaFj7ECUD7VE5kQMZWGQ==",
"dependencies": {
"@mixmark-io/domino": "^2.2.0"
},
"engines": {
"node": ">=18",
"npm": ">=9"
}
},
"node_modules/type-is": { "node_modules/type-is": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz", "resolved": "https://registry.npmjs.org/type-is/-/type-is-2.0.1.tgz",

View File

@@ -1,6 +1,8 @@
{ {
"dependencies": { "dependencies": {
"express": "^5.2.1", "express": "^5.2.1",
"node-fetch": "^3.3.2" "node-fetch": "^3.3.2",
"node-html-parser": "^7.1.0",
"turndown": "^7.2.4"
} }
} }