Spaces:
Sleeping
Sleeping
Download src/core/opencode.js from ExocoreAi/Exocore-gateway: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/ExocoreAi/Exocore-gateway/resolve/main/src/core/opencode.js
- Command line
-
hf download hf://spaces/ExocoreAi/Exocore-gateway/src/core/opencode.js
-
curl -L -o opencode.js https://huggingface.co/spaces/ExocoreAi/Exocore-gateway/resolve/main/src/core/opencode.js
2.1 kB
| import axios from 'axios'; | |
| export const OPENCODE_BASE_URL = 'https://opencode.ai/zen/v1'; | |
| export const OPENCODE_MODELS = [ | |
| 'deepseek-v4-flash-free', | |
| 'mimo-v2.5-free', | |
| 'north-mini-code-free', | |
| 'nemotron-3-ultra-free', | |
| 'big-pickle', | |
| 'hy3-free', | |
| ]; | |
| export const DEFAULT_OPENCODE_MODEL = OPENCODE_MODELS[0]; | |
| export class OpenCodeError extends Error { | |
| constructor(message, code = 'opencode_error', details = null) { | |
| super(message); | |
| this.name = 'OpenCodeError'; | |
| this.code = code; | |
| this.details = details; | |
| } | |
| } | |
| export function isOpenCodeModel(model) { | |
| return OPENCODE_MODELS.includes(model); | |
| } | |
| export function openCodeModelObjects() { | |
| const created = Math.floor(Date.now() / 1000); | |
| return OPENCODE_MODELS.map((id) => ({ | |
| id, | |
| object: 'model', | |
| created, | |
| owned_by: 'opencode', | |
| })); | |
| } | |
| export function openCodeAnthropicModelObjects() { | |
| return OPENCODE_MODELS.map((id) => ({ | |
| type: 'model', | |
| id, | |
| display_name: id, | |
| created_at: new Date().toISOString(), | |
| })); | |
| } | |
| export async function opencodeChat({ | |
| model = DEFAULT_OPENCODE_MODEL, | |
| messages, | |
| maxTokens = 200, | |
| timeout = 120000, | |
| }) { | |
| if (!isOpenCodeModel(model)) { | |
| throw new OpenCodeError(`Model ${model} is not supported`, 'model_not_found'); | |
| } | |
| const body = { | |
| model, | |
| messages, | |
| max_tokens: maxTokens, | |
| }; | |
| try { | |
| const res = await axios.post(`${OPENCODE_BASE_URL}/chat/completions`, body, { | |
| headers: { 'Content-Type': 'application/json' }, | |
| timeout, | |
| }); | |
| return res.data; | |
| } catch (err) { | |
| const status = err.response?.status; | |
| const data = err.response?.data; | |
| const message = data?.error?.message || err.message; | |
| throw new OpenCodeError(message, status === 401 ? 'auth_error' : 'api_error', { | |
| status, | |
| data, | |
| }); | |
| } | |
| } | |
| export function openCodeContent(response) { | |
| const choice = response?.choices?.[0]; | |
| const content = choice?.message?.content; | |
| if (typeof content === 'string') return content; | |
| if (Array.isArray(content)) { | |
| return content.map((item) => item.text || '').join(''); | |
| } | |
| return ''; | |
| } | |