Skip to content
Open

hi #100

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/services/mcp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './ollama-proxy';
export * from './openrouter-adapter';
37 changes: 37 additions & 0 deletions api/services/mcp/ollama-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Ollama } from 'ollama-node';
import { MCPBaseService } from '../core/mcp-base';

export class OllamaProxyService extends MCPBaseService {
private ollama: Ollama;

constructor() {
super('ollama-proxy');
this.ollama = new Ollama({
host: 'http://localhost:11434',
timeout: 60_000
});
}

async execute(payload: any) {
const { model, prompt, stream = false } = payload;

try {
if (stream) {
const stream = await this.ollama.stream(model, prompt);
return { stream, type: 'application/x-ndjson' };
}

const response = await this.ollama.generate(model, prompt);
return {
response,
usage: {
input_tokens: prompt.length / 4,
output_tokens: response.length / 4
}
};
} catch (error) {
this.logger.error('OllamaProxy error:', error);
throw new Error('OllamaProxy service failed');
}
}
}
40 changes: 40 additions & 0 deletions api/services/mcp/openrouter-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import axios from 'axios';
import { MCPBaseService } from '../core/mcp-base';

interface OpenRouterRequest {
model: string;
messages: Array<{ role: string; content: string }>;
temperature?: number;
max_tokens?: number;
}

export class OpenRouterAdapter extends MCPBaseService {
private apiKey: string;
private readonly baseURL = 'https://openrouter.ai/api/v1';

constructor() {
super('openrouter-adapter');
this.apiKey = process.env.OPENROUTER_API_KEY || '';
}

async execute(payload: OpenRouterRequest) {
try {
const response = await axios.post(`${this.baseURL}/chat/completions`, payload, {
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'HTTP-Referer': 'https://cofounder.local',
'X-Title': 'Cofounder IDE'
},
timeout: 30000
});

return {
content: response.data.choices[0].message.content,
usage: response.data.usage
};
} catch (error) {
this.logger.error('OpenRouter API error:', error);
throw new Error('OpenRouter service request failed');
}
}
}
34 changes: 34 additions & 0 deletions api/services/websocketService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const http = require('http');
const socketIO = require('socket.io');

let io;

const initWebSocket = (app) => {
const server = http.createServer(app);
io = socketIO(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});

io.on('connection', (socket) => {
console.log('New client connected');

socket.on('chat message', (msg) => {
io.emit('chat message', msg);
});

socket.on('new video', (video) => {
io.emit('new video', video);
});

socket.on('disconnect', () => {
console.log('Client disconnected');
});
});

return server;
};

module.exports = { initWebSocket };
Loading