Skip to content

Commit ac4329d

Browse files
Eistee82claudeGermanBluefox
authored
feat: support custom OpenAI-compatible API endpoints (#2135)
* feat: support custom OpenAI-compatible API endpoints (Ollama, LM Studio, etc.) Add configurable base URL and custom model name to allow using any OpenAI-compatible API provider. Models are now fetched dynamically from the configured API endpoint instead of a hardcoded list. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add error handling for unreachable API providers and model fetch failures Show user-friendly error messages when the API endpoint is unreachable, API key is invalid, or model is not found. Add retry button for failed model loading. Includes translations for all 11 languages. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add Test API connection button, changelog and documentation - Add sendTo handler 'testApiConnection' in main.ts for server-side API connectivity testing - Add test button in admin jsonConfig using sendTo - Add WORK IN PROGRESS changelog entry in README.md - Add documentation for custom API support in docs/en and docs/de Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: remove Custom model name field, models are fetched dynamically The custom model name config field is unnecessary since available models are now fetched dynamically from the API endpoint and shown in the dropdown. Removed from config, i18n, dialog logic and documentation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: remove invalid icon from test API connection button Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * build: recompile backend to include testApiConnection handler The sendTo handler was only in src/main.ts but the compiled build/main.js was missing it, causing the test button to spin indefinitely. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: move all API calls server-side via sendTo to avoid CORS issues - Add 'chatCompletion' sendTo handler for proxying chat requests - Model loading already uses 'testApiConnection' sendTo handler - Remove OpenAI SDK usage from browser (no more dangerouslyAllowBrowser) - All API communication now goes through the adapter backend, which avoids CORS issues with local providers like Ollama Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: build frontend, fix TypeScript error type and disabled prop - Fix error state type from boolean to string | false - Fix disabled prop to use !!error for boolean coercion - Rebuild frontend assets with server-side API changes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: restore admin/tab.html and admin/custom/ removed by build The frontend build process incorrectly deleted tab.html and the custom components directory, breaking the script editor tab. Restore original build artifacts - source changes remain in src-editor for future builds. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: rebuild frontend correctly with manual copy and tab.html generation The automated build task failed on Node 25 due to deprecated rmdirSync. Built editor with vite directly and manually copied assets + generated tab.html with socket.io and monaco script injection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: add Content-Length header and increase timeout to 600s for chat requests Local LLMs (Ollama) need Content-Length to process the request body correctly and more time to generate responses with large prompts. Also explicitly set stream: false for compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: strip LLM thinking artifacts from code generator responses Local models (Ollama) often output <think> blocks, <|endoftext|> tokens, and reasoning commentary alongside code. The response parser now: - Strips <think>...</think> blocks - Strips special tokens (<|endoftext|>, <|im_start|>, <|im_end|>) - Intelligently extracts code from unstructured responses - Removes trailing LLM commentary after code Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * docs: add recommended providers (Gemini free, DeepSeek) and note about ChatGPT - Add Google Gemini as recommended free provider with setup instructions - Add DeepSeek as affordable alternative - Add guidance for local models (14B+ recommended) - Note that free OpenAI/ChatGPT API no longer works for code generation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: strip 'models/' prefix from Google Gemini model IDs Google's API returns model IDs like 'models/gemini-2.0-flash' but the chat completions endpoint expects just 'gemini-2.0-flash'. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * chore: cleanup orphaned build assets and update changelog - Remove old OpenAiDialog/ScriptEditor assets no longer referenced - Update WORK IN PROGRESS changelog (remove 'custom model name', add server-side proxy and artifact stripping) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: allow empty API key for local providers like Ollama Local providers (Ollama, LM Studio) don't require authentication. The API key check now only rejects requests when no key AND no custom base URL is set. The Authorization header is omitted entirely when no key is configured. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * style: fix prettier formatting for sendTo calls Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Removed useless models * Removed useless models * Fixed some minor possible errors * Fix linter --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: GermanBluefox <dogafox@gmail.com>
1 parent 2dd6051 commit ac4329d

234 files changed

Lines changed: 6341 additions & 159075 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ iob_npm.dode
1717

1818
#ignore .commitinfo created by ioBroker release script
1919
.commitinfo
20+
/.claude/settings.local.json

CLAUDE.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
ioBroker.javascript is a JavaScript/TypeScript/Blockly script engine adapter for the ioBroker home automation platform. It runs as a daemon adapter that executes user-written scripts in a sandboxed environment, with a web-based editor UI (Monaco + Blockly) and an admin configuration panel.
8+
9+
## Build Commands
10+
11+
```bash
12+
# Full build (backend TypeScript + both frontends)
13+
npm run build
14+
15+
# Backend only (TypeScript → build/)
16+
npm run build-backend
17+
18+
# Editor UI only (src-editor → admin/)
19+
npm run build-editor
20+
21+
# Admin config UI only (src-admin → admin/custom/)
22+
npm run admin-build
23+
24+
# Install all dependencies (root + src-editor + src-admin)
25+
npm run npm
26+
```
27+
28+
## Testing
29+
30+
```bash
31+
# All tests (declaration checks + integration)
32+
npm test
33+
34+
# Individual test suites
35+
npm run test:declarations # TypeScript declaration compilation checks
36+
npm run test:integration # Mocha integration tests (test/testFunctions.js etc.)
37+
npm run test:scheduler # Scheduler/cron tests only
38+
npm run test:package # Package structure validation
39+
40+
# Run a single test file directly
41+
npx mocha test/testScheduler.js --exit
42+
```
43+
44+
Test framework is Mocha, configured via `.mocharc.json`. Tests are in `test/`.
45+
46+
## Linting
47+
48+
```bash
49+
npm run lint # Lint backend (root) only
50+
npm run lint-all # Lint root + src-editor + src-admin
51+
```
52+
53+
ESLint uses `@iobroker/eslint-config` (flat config in `eslint.config.mjs`). Prettier config extends `@iobroker/eslint-config/prettier.config.mjs`. The root ESLint config only covers `src/` — frontend directories have their own configs.
54+
55+
## Architecture
56+
57+
### Three codebases in one repo
58+
59+
1. **Backend adapter** (`src/` → compiled to `build/`): Node.js TypeScript, compiled to CommonJS. Entry point is `src/main.ts`. Runs as an ioBroker adapter daemon.
60+
61+
2. **Script editor UI** (`src-editor/`): React + TypeScript app built with Vite. Contains the Monaco code editor, Blockly visual editor, script management sidebar, log viewer, and OpenAI integration. Built output is copied to `admin/` by `tasks.js`.
62+
63+
3. **Admin config UI** (`src-admin/`): React + TypeScript app built with Vite. Provides adapter configuration via `@iobroker/json-config`. Built output goes to `admin/custom/`.
64+
65+
Each frontend has its own `package.json`, `node_modules`, and ESLint config. The build orchestration in `tasks.js` handles cleanup, npm installs, compilation, and copying built assets to `admin/`.
66+
67+
### Key backend modules (`src/lib/`)
68+
69+
- **`sandbox.ts`** (~244KB) — The script execution sandbox. Provides all global functions available to user scripts (`getState`, `setState`, `on`, `schedule`, `createState`, `httpGet`, etc.). This is the core of the adapter.
70+
- **`scheduler.ts`** — Cron and astro-based scheduling engine (sunrise/sunset events via suncalc2).
71+
- **`protectFs.ts`** — Filesystem access control for sandboxed scripts.
72+
- **`debugger.ts`** — Script debugging support.
73+
- **`mirror.ts`** — Mirrors scripts to/from the filesystem.
74+
- **`javascript.d.ts`** (~140KB) — Complete TypeScript type definitions for the script API, used for editor autocompletion.
75+
- **`typescriptTools.ts`** — TypeScript compilation for user scripts (via `virtual-tsc`).
76+
77+
### Build orchestration (`tasks.js`)
78+
79+
Custom Node.js build script using `@iobroker/build-tools`. Supports granular steps via CLI flags (`--0-clean`, `--1-npm`, `--2-build`, `--3-copy`, `--4-patch`). The `npm run build` command runs backend compilation first, then `node tasks` for the full frontend build pipeline.
80+
81+
### Adapter configuration
82+
83+
`io-package.json` defines adapter metadata, native configuration defaults, instance objects (astro variables, debug states), and ioBroker dependency requirements (js-controller >=5.0.19, admin >=7.6.1).
84+
85+
## TypeScript Configuration
86+
87+
- Backend targets ES2022, compiled to CommonJS (`tsconfig.build.json`)
88+
- `@types/node` major version should match the lowest supported Node.js version
89+
- Frontend apps target Chrome 89+ (Vite build config)
90+
91+
## CI/CD
92+
93+
GitHub Actions (`.github/workflows/test-and-release.yml`) runs lint, build, and adapter tests across Node 18/20/22/24 on Ubuntu, Windows, and macOS. Releases are automated via `@alcalzone/release-script` with NPM publish and GitHub release creation.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ Executes Javascript, Typescript Scripts.
2525
## Changelog
2626
<!--
2727
### **WORK IN PROGRESS**
28+
* Added support for custom OpenAI-compatible API endpoints (e.g. Ollama, LM Studio, Google Gemini, DeepSeek, OpenRouter)
29+
* Added configurable base URL in adapter settings
30+
* Models are now fetched dynamically from the configured API endpoint
31+
* Added "Test API connection" button in adapter settings
32+
* Added error handling with user-friendly messages for unreachable providers
33+
* Added retry functionality for failed model loading
34+
* All API calls (models + chat) are proxied server-side to avoid CORS issues with local providers
35+
* Strip LLM thinking artifacts from responses (for local models like Ollama)
2836
-->
2937
### 9.0.18 (2026-01-11)
3038
* (@GermanBluefox) Corrected an error message with `lastSync`
@@ -40,7 +48,7 @@ Executes Javascript, Typescript Scripts.
4048
### 9.0.10 (2025-07-27)
4149
* (@klein0r) Added Blockly block to format a numeric value
4250
* (@GermanBluefox) Fixing some blocks in blockly: cron, time
43-
* (@GermanBluefox) Added new block: "unconditional return"
51+
* (@GermanBluefox) Added a new block: "unconditional return"
4452
* (@GermanBluefox) Type definitions for TypeScript were updated
4553
* (@GermanBluefox) Corrected bug with deleting of sub-folders
4654

0 commit comments

Comments
 (0)