diff --git a/README.md b/README.md index b41a54e..6d61bf8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,158 @@ -Local-LLM-as-coding-agent -=============== +# Open WebUI を使用したコーディングエージェントへの接続 + +Cline は、Ollama でバックアップされたセルフホスト型 Open WebUI インスタンスを使用し、商用 API ではなくラボでホストされたモデルに対してアジャナイティックなコーディングを実行します。 + +**設定手順は以下の通り:** Open WebUI は `avogadro.nakaguchi.org:3000` で動作し、Ollama を介して `qwen3-coder:30b` を提供しています。 + +--- + +## 前提条件 + +- VS Code +- Open WebUI インスタンスのアカウント +- サーバーへのネットワークアクセス(プライベートアドレス `10.44.104.23` に解決されるため、**キャンパスネットワークまたは VPN が必要です** — VPN なしでは家から動作しません) +- WSL の推奨([既知の問題](#known-issues)を参照) + +--- + +## ステップ 1 — 認証トークンを取得 + +1. Chrome または Firefox で Open WebUI にログイン +2. `F12` → **Application** タブを押す +3. **Cookies** を展開 → Open WebUI のオリジンをクリック +4. `token` という名前のクッキーの値をコピー + +これは `eyJ` で始まる長い文字列で、2つのドットを含みます。 + +> **注意:** このトークンは有効期限があります — デフォルトでは4週間で、SSO の場合より早く失効します。エージェントが401エラーを返すようになったら、再びコピーしてください。管理者に実際の API キーを要求することでこの問題を回避できます。 + +--- + +## ステップ 2 — トークンを確認 + +CMD で実行し、**1行で**: + +``` +curl -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models +``` + +JSON のモデル一覧が表示されれば成功です。 + +### 一般的な間違い + +| 間違い | 結果 | +|---|---| +| `Bearer` の単語を省略 | `{"detail":"Not authenticated"}` | +| PowerShell で `curl` を実行 | `Invoke-WebRequest` にエイリアス化されているため、`curl.exe` を使用する | +| トークンの周囲に引用符を含める | 401 | + +curl では `Bearer` が必要です(生のヘッダーを書いているため)。Cline の API キー欄では **`Bearer` 接頭辞は不要** です。Cline が自動的に追加します。 + +--- + +## ステップ 3 — モデル ID を確認 + +上記の JSON から `"id"` フィールドを見つけます。フレンドリーな表示名ではなく、**正確な文字列**を使用してください。 + +``` +curl -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models | findstr "\"id\"" +``` + +モデルが `"capabilities":["completion","tools"]` を報告していることを確認してください。`tools` がないとアジャナイティックな使用は動作しません — モデルはチャットできますがファイルや端末操作を呼び出しません。 + +--- + +## ステップ 4 — Cline を設定 + +VS Code の拡張機能パネルから **Cline** をインストールし、設定の歯車アイコンを開きます: + +| フィールド | 値 | +|---|---| +| API プロバイダー | `OpenAI Compatible` | +| ベース URL | `http://avogadro.nakaguchi.org:3000/api` | +| API キー | トークン — **`Bearer` 接頭辞は不要** | +| モデル ID | `qwen3-coder:30b` またはワークスペースのバリアント | + +URL は `/api` で終わるのに注意してください。**`/v1` ではありません**。 + +--- + +## ステップ 5 — 確認 + +Cline チャットで `hi` を送信します。 + +**成功:** 入力/出力数を示すトークン使用量の行付きで返答が表示されます。 + +**最初のリクエストは遅いです。** モデルが `"loaded":false` と表示される場合、Ollama はディスクから 18.5 GB をロードする必要があります。失敗と誤認しないように2分間待ってください。事前に curl コマンドで温めることで、設定の問題と誤認することを避けられます。 + +### エージェントループのテスト + +接続とアジャナイティックな動作は別のものです。実際のプロジェクトフォルダを開いて次のように尋ねてください: + +> このフォルダ内のファイルを読み取り、プロジェクトが何をするか要約してください + +ツール呼び出しボックス — *"Cline は src/main.py を読みたい"* — が表示され、承認/拒否ボタンがあるはずです。これでツール呼び出しが動作することを確認できます。もし実行する予定のことをしか述べない場合は、ステップ4を見直してください。 + +--- + +## 既知の問題 + +### WSL では動作しますが、Windows の VS Code では動作しません + +この設定中に発見された問題: 同じ設定で WSL Remote ウィンドウでは正常に返答するが、ネイティブ Windows VS Code では無限に待機します。 + +**除外済み:** プロキシ (`ProxyEnable = 0x0`, `AutoConfigURL` なし) や `Test-NetConnection avogadro.nakaguchi.org -Port 3000` が `TcpTestSucceeded : True` を返すことを確認しました。 + +**最も可能性が高い原因:** WSL Remote ウィンドウの拡張機能は、ローカル Windows インストールとは**別のストレージ**を使用します。設定は引き継がれません。Windows ウィンドウでは4つのフィールドが入力されていることを確認してください。 + +**診断:** `ヘルプ → 開発者ツールの切り替え → ネットワーク` で、クリアしてからメッセージを送信します。ホストへのリクエストがない場合、リクエストが構築されていないため、設定または拡張機能状態の問題です。ネットワークの問題ではありません。 + +**推奨:** WSL で作業してください。Cline は端末コマンドを実行するため、実際の bash シェルは PowerShell の引用符の癖やパス変換を回避します。`WSL: Open Folder in WSL` を使用してプロジェクトを開き、ファイルを `/mnt/c/` ではなく `~/` に保管してください。後者はファイルを多数読むと非常に遅くなるため、エージェントが動作するのに苦労します。 + +### リクエストのキャンセル + +**キャンセル**ボタン、`Escape`、または `開発者: ウィンドウの再読み込み` を使用してください。クライアント側でのキャンセルは Ollama の生成を停止しません — 共有マシンでリトライを積み重ねるのは避けてください。 + +--- + +## 追加項目 + +以下のセクションを **既知の問題** の後ろに追加してください: + +--- + +### PowerShell バージョン — `curl` の動作が異なる + +**Windows PowerShell 5.1**(青いアイコン、事前にインストール)は `curl` を `Invoke-WebRequest` にエイリアス化しており、実際の curl とは `-H` を受け付けません。ヘッダーは無視され、`{"detail":"Not authenticated"}` やパラメータバインディングエラーが発生します(トークンが正しい場合でも)。 + +**PowerShell 7+** はそのエイリアスを削除しました。`curl` は本物の `curl.exe` を呼び出すため、この README のコマンドはそのまま動作します。 + +どのバージョンを使用しているか確認: + +```powershell +$PSVersionTable.PSVersion +``` + +`5.1.x` が古いバージョンです。`7.x` が最新版です。 + +**更新方法:** + +```powershell +winget install --id Microsoft.PowerShell --source winget +``` + +これは Windows PowerShell を置き換えるのではなく、別途インストールされます — **PowerShell 7**(黒いアイコン)がスタートメニューに表示されます。`winget` が利用できない場合は、[github.com/PowerShell/PowerShell/releases](https://github.com/PowerShell/PowerShell/releases) から MSI をダウンロードしてください。 + +**更新しない場合:** 明示的に `curl.exe` を呼び出す: + +```powershell +curl.exe -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models +``` + +または CMD でコマンドを実行します。Windows 10 と 11 ではデフォルトで実際の curl が含まれています。 + +> **設定以外での重要性:** Cline はエージェントループの一部として端末コマンドを実行し、VS Code のデフォルトシェルを使用します。Windows の場合通常は PowerShell 5.1 であり、そのエイリアスと引用符はモデルが学習した bash 構文とは異なります。エージェントが作成するコマンドはコードとは無関係な理由で失敗することがあります。PowerShell 7 への更新が役立ちます。WSL での作業は問題を完全に回避します。 + +--- + +5.1 を使用している場合に知っておくと良い2つの違い: `Test-NetConnection` は両方のバージョンで動作するため、診断は影響を受けません。また、5.1 は `wget` と `ls` も PowerShell コマンドレットにエイリアス化しており、過去に貼り付けたシェルスニペットが同じように問題を引き起こします。 \ No newline at end of file diff --git a/README_EN.md b/README_EN.md new file mode 100644 index 0000000..3b1d930 --- /dev/null +++ b/README_EN.md @@ -0,0 +1,157 @@ +# Connecting VS Code to Open WebUI as a Coding Agent + +Uses [Cline](https://cline.bot) in VS Code with a self-hosted Open WebUI instance backed by Ollama, so agentic coding runs against a lab-hosted model instead of a commercial API. + +**Setup documented here:** Open WebUI at `avogadro.nakaguchi.org:3000`, serving `qwen3-coder:30b` via Ollama. + +--- + +## Prerequisites + +- VS Code +- An account on the Open WebUI instance +- Network access to the server (it resolves to a private address, `10.44.104.23`, so **campus network or VPN is required** — this will not work from home without VPN) +- WSL recommended (see [Known Issues](#known-issues)) + +--- + +## Step 1 — Get an authentication token + +1. Log into Open WebUI in Chrome or Firefox +2. Press `F12` → **Application** tab +3. Expand **Cookies** → click your Open WebUI origin +4. Copy the value of the cookie named `token` + +It's a long string starting with `eyJ` containing two dots. + +> **Note:** this token expires — four weeks by default, sooner with SSO. When the agent starts returning 401s, re-copy it. Requesting a real API key from the admin avoids this. + +--- + +## Step 2 — Verify the token + +Run in CMD, **all on one line**: + +``` +curl -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models +``` + +A JSON list of models means success. + +### Common mistakes + +| Mistake | Result | +|---|---| +| Omitting the word `Bearer` | `{"detail":"Not authenticated"}` | +| Running `curl` in PowerShell | Aliased to `Invoke-WebRequest`; use `curl.exe` | +| Including quotes copied around the token | 401 | + +`Bearer` is required in curl because you're writing the raw header. It must be **omitted** in Cline's API Key field, because Cline adds it for you. + +--- + +## Step 3 — Find your model ID + +From the JSON above, locate the `"id"` field. Use that exact string, not the friendly display name. + +``` +curl -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models | findstr "\"id\"" +``` + +Confirm the model reports `"capabilities":["completion","tools"]`. Without `tools`, agentic use will not work — the model will chat but never invoke file or terminal operations. + +--- + + +## Step 4 — Configure Cline + +Install **Cline** from the VS Code Extensions panel, then open its settings gear: + +| Field | Value | +|---|---| +| API Provider | `OpenAI Compatible` | +| Base URL | `http://avogadro.nakaguchi.org:3000/api` | +| API Key | your token — **no `Bearer` prefix** | +| Model ID | `qwen3-coder:30b` or your workspace variant | + +Note the URL ends in `/api`, with **no** `/v1`. + +--- + +## Step 5 — Verify + +Send `hi` in the Cline chat. + +**Success:** a reply appears with a token-usage line showing input/output counts. + +**First request is slow.** If the model shows `"loaded":false`, Ollama must load 18.5 GB from disk. Allow two full minutes before assuming failure. Warming it up with a plain curl request first avoids mistaking this for a broken config. + +### Test the agent loop + +Connecting and agentic behavior are different things. Open a real project folder and ask: + +> read the files in this folder and summarize what the project does + +You should see a tool-call box — *"Cline wants to read src/main.py"* — with approve/reject buttons. That confirms tool calling works. If it only describes what it *would* do, revisit Step 4. + +--- + +## Known Issues + +### Works in WSL, not in Windows VS Code + +Encountered during this setup: identical config replies in a WSL Remote window but spins indefinitely in native Windows VS Code. + +**Ruled out:** no proxy (`ProxyEnable = 0x0`, no `AutoConfigURL`), and `Test-NetConnection avogadro.nakaguchi.org -Port 3000` returns `TcpTestSucceeded : True`. + +**Most likely cause:** extensions in a WSL Remote window use **separate storage** from the local Windows install. Configuration does not carry over. Verify all four fields are populated in the Windows window specifically. + +**Diagnostic:** `Help → Toggle Developer Tools → Network`, clear, then send a message. If no request to the host appears, the request is never being constructed — a config or extension-state problem, not a network one. + +**Recommendation:** work in WSL regardless. Cline runs terminal commands, and a real bash shell avoids PowerShell quoting quirks and path translation. Open projects via `WSL: Open Folder in WSL` and keep files under `~/`, not `/mnt/c/` — the latter is slow enough that an agent reading many files will crawl. + +### Cancelling a request + +Use the **Cancel** button, `Escape`, or `Developer: Reload Window`. Cancelling client-side doesn't stop Ollama generating — avoid stacking retries on a shared machine. + +--- + +Here's the section to add under **Known Issues**, after the WSL entry: + +--- + +### PowerShell version — `curl` behaves differently + +**Windows PowerShell 5.1** (the blue icon, preinstalled) aliases `curl` to `Invoke-WebRequest`, which does not accept `-H` the way real curl does. The header is silently mangled, producing `{"detail":"Not authenticated"}` or a parameter-binding error, even when the token is correct. + +**PowerShell 7+** removed that alias. `curl` invokes the genuine `curl.exe`, so the commands in this README work as written. + +Check which you're running: + +```powershell +$PSVersionTable.PSVersion +``` + +`5.1.x` is the old one. `7.x` is current. + +**To update:** + +```powershell +winget install --id Microsoft.PowerShell --source winget +``` + +This installs alongside Windows PowerShell rather than replacing it — you'll have a separate **PowerShell 7** entry (black icon) in the Start menu. If `winget` isn't available, download the MSI from [github.com/PowerShell/PowerShell/releases](https://github.com/PowerShell/PowerShell/releases). + +**Without updating**, either call `curl.exe` explicitly: + +```powershell +curl.exe -H "Authorization: Bearer YOUR_TOKEN" http://avogadro.nakaguchi.org:3000/api/models +``` + +or run the commands in CMD, which ships real curl on Windows 10 and 11. + +> **Why this matters beyond setup:** Cline runs terminal commands as part of its agent loop, using VS Code's default shell. On Windows that's usually PowerShell 5.1, whose aliasing and quoting differ from the bash syntax most models are trained to emit. Commands the agent writes may fail for reasons unrelated to the code. Updating to PowerShell 7 helps; working in WSL avoids the problem entirely. + +--- + +Two other differences worth knowing while you're on 5.1: `Test-NetConnection` works in both versions, so that diagnostic was unaffected. And 5.1 also aliases `wget` and `ls` to PowerShell cmdlets, which trips up pasted shell snippets in the same way. \ No newline at end of file