Skip to content

Latest commit

ย 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

OpenCode Mobile Server

่ฟ™ๆ˜ฏไธ€ไธช้ขๅ‘ๆ‰‹ๆœบๆต่งˆๅ™จใ€ๅฏ่‡ชๆ‰˜็ฎกๅˆฐๆœๅŠกๅ™จๆˆ–ๅฑ€ๅŸŸ็ฝ‘็š„ OpenCode Web ๅฎขๆˆท็ซฏไธŽ้ƒจ็ฝฒ็ฝ‘ๅ…ณ๏ผŒๆไพ›ไผš่ฏๅˆ—่กจใ€ๆตๅผๅ›žๅคใ€Markdownใ€ๅทฅๅ…ท่ฐƒ็”จๅฑ•็คบใ€ๆ–‡ไปถไธŠไผ ๅ’Œ็งปๅŠจ็ซฏๆŠฝๅฑ‰ๅผๅฏผ่ˆชใ€‚ๅฎƒๅฑžไบŽ OpenCode ็”Ÿๆ€๏ผŒๆ—ขๅฏไปฅๅœจ Windows ็”ต่„‘ไธŠๅฏๅŠจไพ›ๆ‰‹ๆœบ้€š่ฟ‡ๅฑ€ๅŸŸ็ฝ‘่ฎฟ้—ฎ๏ผŒไนŸๅฏไปฅ้ƒจ็ฝฒๅˆฐ Linux ๆœๅŠกๅ™จๅนถ้€š่ฟ‡ๅŸŸๅๅฏนๅค–ๆไพ› HTTPS ๆœๅŠกใ€‚

ๆœฌๆ–‡ๆกฃๆไพ›ๅฎŒๆ•ด็š„ไธญๆ–‡่ฏดๆ˜Žๅ’ŒๅฎŒๆ•ด็š„ English documentation๏ผŒ่‹ฑๆ–‡ๆ–‡ๆกฃไฝไบŽไธ‹ๆ–นใ€‚

็•Œ้ข้ข„่งˆ / Screenshots

ไธป็•Œ้ข ไผš่ฏๅˆ—่กจ๏ผˆ็‚นๅผ€ โ˜ฐ ่œๅ•๏ผ‰
ไธป็•Œ้ข ไผš่ฏๅˆ—่กจ

ไธญๆ–‡ๆ–‡ๆกฃ

ๅŠŸ่ƒฝ

  • ็งปๅŠจ็ซฏไผ˜ๅ…ˆ็š„ๆทฑ่‰ฒ็•Œ้ข๏ผŒๆ”ฏๆŒๆ‰‹ๆœบๅฎ‰ๅ…จๅŒบๅŸŸใ€‚
  • ไผš่ฏๅˆ‡ๆขใ€ๅކๅฒๆถˆๆฏ็ผ“ๅญ˜ๅ’Œๆธธๆ ‡ๅˆ†้กตๅŠ ่ฝฝใ€‚
  • ้€š่ฟ‡ OpenCode ไบ‹ไปถๆŽฅๅฃๅฎžๆ—ถๆ˜พ็คบ็”Ÿๆˆๅ†…ๅฎน๏ผ›SSE ไธๅฏ็”จๆ—ถไฝฟ็”จ่ฝฎ่ฏขๅŒๆญฅ็Šถๆ€ๅ’Œๆถˆๆฏใ€‚
  • SSE ๅ…ผๅฎน LF/CRLF ๅˆ†้š”็ฌฆ๏ผŒ็”Ÿๆˆไธญ็š„ๆ–‡ๆœฌๅ’Œๆ€่€ƒๅ†…ๅฎนไผš้€ๅ—ๆ˜พ็คบใ€‚
  • ๆ”ฏๆŒ้€‰ๆ‹ฉๆจกๅž‹ใ€Agent ๅ’Œๆ€่€ƒๅผบๅบฆ๏ผˆvariant๏ผ‰๏ผŒๅนถๆŒ‰ๆจกๅž‹ไฟๅญ˜้€‰ๆ‹ฉ๏ผ›ๅˆ‡ๆขไผš่ฏๆ—ถๆจกๅž‹้€‰ๆ‹ฉๅ™จไผš่‡ชๅŠจๅŒๆญฅๅˆฐ่ฏฅไผš่ฏๅฝ“ๅ‰ๅฎž้™…ไฝฟ็”จ็š„ๆจกๅž‹ใ€‚ๆจกๅž‹ๅˆ—่กจไผšๆ ‡ๆณจๅ„ๆจกๅž‹ๆ”ฏๆŒ็š„ๆจกๆ€๏ผˆ๐Ÿ–ผ๏ธ ๅ›พๅƒใ€๐ŸŽ™๏ธ ้Ÿณ้ข‘ใ€๐ŸŽฌ ่ง†้ข‘ใ€๐Ÿ“„ ๆ–‡ๆกฃ๏ผ‰๏ผŒ็บฏๆ–‡ๆœฌๆจกๅž‹ๆ˜พ็คบโ€œ็บฏๆ–‡ๆœฌโ€ใ€‚
  • ๆ”ฏๆŒๆ˜พ็คบๅฝ“ๅ‰ไผš่ฏ็š„ไธŠไธ‹ๆ–‡็”จ้‡ๅ’Œๆจกๅž‹ไธŠไธ‹ๆ–‡ไธŠ้™ใ€‚
  • ๆ”ฏๆŒๅค„็† OpenCode ็š„ AI ่ฟฝ้—ฎ๏ผŒๅฏๅ•้€‰ใ€ๅคš้€‰ใ€่‡ชๅฎšไน‰ๅ›ž็ญ”ๆˆ–่ทณ่ฟ‡ใ€‚
  • ๆ”ฏๆŒๅ•ๆฌก่ฏทๆฑ‚ๆœ€ๅคš 20 MB ็š„ๆ–‡ไปถ้™„ไปถ๏ผŒๅฏๆŠŠๆ–‡ไปถๆˆ–ๅ›พ็‰‡็›ดๆŽฅๆ‹–่ฟ›่พ“ๅ…ฅๆก†ใ€‚
  • ไพง่พนๆ ็›ดๆŽฅ็ฎก็†ๅ„ provider ็š„ API ๅฏ†้’ฅ๏ผŒๅˆ‡ๆขๅŽ่‡ชๅŠจ้‡ๅฏ OpenCode ๆœๅŠก็”Ÿๆ•ˆใ€‚
  • ๆ”ฏๆŒๅœๆญข็”Ÿๆˆใ€้‡่ฏ•็Šถๆ€ๆ็คบใ€ไปฃ็ ๅ—ๅคๅˆถๅ’Œ็งปๅŠจ็ซฏ PWA ๅฎ‰่ฃ…ๅ…ฅๅฃใ€‚
  • ๅ‰็ซฏๆ— ้œ€ๆž„ๅปบ๏ผŒ่ฟ่กŒๆ—ถๆ— ้œ€ๅฎ‰่ฃ… npm ไพ่ต–ใ€‚
  • Node ็ฝ‘ๅ…ณๅŒๆ—ถๆไพ›้™ๆ€้กต้ข๏ผŒๅนถไปฃ็† /opencode/api/* ๅˆฐ OpenCode ๆœๅŠกใ€‚

ๅทฅไฝœๅŽŸ็†

ๆ‰‹ๆœบๆˆ–็”ต่„‘ๆต่งˆๅ™จ
  -> /opencode/                 ๆ‰‹ๆœบ Web ้กต้ข
  -> /opencode/api/*            Node ็ฝ‘ๅ…ณ
                                  -> 127.0.0.1:4096 OpenCode ๆœๅŠก

ๆœฌ้กน็›ฎๆ˜ฏ OpenCode ็š„ๅฎขๆˆท็ซฏๅ’Œ็ฝ‘ๅ…ณ๏ผŒไธๅŒ…ๅซ OpenCode CLI ๆœฌ่บซใ€‚ไฝฟ็”จๅ‰้œ€่ฆๅ•็‹ฌๅฎ‰่ฃ… OpenCode๏ผŒๅนถ็กฎไฟ opencode serve ๅฏไปฅ่ฟ่กŒใ€‚

ๆ–ฐ็‰ˆๅฎขๆˆท็ซฏไพ่ต– OpenCode ็š„ providerใ€Agentใ€ไบ‹ไปถใ€้—ฎ้ข˜ๅ’Œๆธธๆ ‡ๅˆ†้กตๆŽฅๅฃใ€‚่‹ฅ OpenCode ็‰ˆๆœฌ่ฟ‡ๆ—ง๏ผŒๅฏ่ƒฝๅ‡บ็Žฐๆจกๅž‹ๅˆ—่กจไธบ็ฉบใ€AI ่ฟฝ้—ฎๆ— ๆณ•ๆ˜พ็คบๆˆ–โ€œๅŠ ่ฝฝๆ›ดๆ—ฉๆถˆๆฏโ€ๅคฑ่ดฅ๏ผ›่ฏทไฝฟ็”จไธŽๅฝ“ๅ‰ OpenCode API ๅŒน้…็š„็‰ˆๆœฌใ€‚

Linux ็›ฎๅฝ•็บฆๅฎš

Linux ๆœๅŠกๅ™จๅชไฟ็•™ไธ€ไปฝ Web ้กน็›ฎ๏ผš

/opt/opencode-mobile-server/                 ๆœฌ้กน็›ฎๆบ็ ใ€้กต้ขๅ’Œ Node ็ฝ‘ๅ…ณ
/etc/opencode-mobile-server/opencode.env    ๅฏ†็ ็ญ‰่ฟ่กŒ้…็ฝฎ
/root/.opencode/                              OpenCode CLI ๆœฌ่บซ

Nginx ๅช่ดŸ่ดฃๅŸŸๅใ€HTTPS ๅ’Œ /opencode/ ๅๅ‘ไปฃ็†๏ผŒไธๅ†ไปŽ /www/wwwroot ๅคๅˆถๆˆ–่ฏปๅ–ๅ‰็ซฏๆ–‡ไปถใ€‚่ฟ™ๆ ท Windows ๅ’Œ Linux ไฝฟ็”จๅŒไธ€ไธช้กน็›ฎ็›ฎๅฝ•็ป“ๆž„๏ผŒๅ‡็บงๆ—ถๅช้œ€่ฆๆ›ดๆ–ฐ /opt/opencode-mobile-serverใ€‚้กน็›ฎๆไพ›็š„ Nginx ๆจกๆฟ้ป˜่ฎคๅช็›‘ๅฌ HTTP 80 ็ซฏๅฃ๏ผŒ่ฏไนฆๅ’Œ 443 ้…็ฝฎไป้œ€ๅ•็‹ฌๅฎŒๆˆใ€‚

้ป˜่ฎคๅฏ†็ ๅ’Œไฟฎๆ”นๆ–นๅผ

ๆ–ฐ้ƒจ็ฝฒ็š„้ป˜่ฎคๅฏ†็ ๆ˜ฏ๏ผš

opencode

ๅฏนๅค–็ฝ‘ๅผ€ๆ”พๅ‰ๅฟ…้กปไฟฎๆ”นๅฏ†็ ใ€‚ๅคๅˆถ .env.example ไธบ .env๏ผŒไฟฎๆ”นไธ‹้ข็š„้…็ฝฎ๏ผš

OPENCODE_SERVER_PASSWORD=่ฏทๆ›ฟๆขไธบ้šๆœบ้•ฟๅฏ†็ 

็ฝ‘ๅ…ณๅ’Œ OpenCode ๆœๅŠกๅฟ…้กปไฝฟ็”จๅŒไธ€ไธชๅฏ†็ ๏ผš

  • Windows๏ผšไฟฎๆ”น้กน็›ฎๆ น็›ฎๅฝ•็š„ .envใ€‚
  • Linux ๆœๅŠกๅ™จ๏ผšไฟฎๆ”น /etc/opencode-mobile-server/opencode.envใ€‚

ๆ‰‹ๆœบ้กต้ขไผšๅฐ†่พ“ๅ…ฅ็š„ๅฏ†็ ไฟๅญ˜ๅœจๆต่งˆๅ™จๆœฌๅœฐๅญ˜ๅ‚จไธญใ€‚ๆ”นๅฏ†็ ๅŽ๏ผŒ่ฏท็‚นๅ‡ป้กต้ข้‡Œ็š„โ€œ้€€ๅ‡บ็™ปๅฝ•โ€้‡ๆ–ฐ่พ“ๅ…ฅ๏ผ›ๅฆ‚ๆžœไป็„ถๅคฑ่ดฅ๏ผŒ่ฏทๆธ…้™ค่ฏฅ็ฝ‘็ซ™็š„ๆœฌๅœฐๅญ˜ๅ‚จไธญ็š„ oc_pwdใ€‚

็ฝ‘ๅ…ณไฝฟ็”จ Basic Auth ็š„ๅฏ†็ ้ƒจๅˆ†ๆ ก้ชŒ่ฎฟ้—ฎๅฏ†็ ๏ผŒ็”จๆˆทๅไธไผš็”จไบŽๆŽˆๆƒใ€‚ๆ–ฐ็‰ˆ็ฝ‘ๅ…ณไผšๅ…ˆๆ ก้ชŒ /opencode/api/*๏ผŒๅ†่ฝฌๅ‘็ป™ OpenCode๏ผ›OpenCode ๆœๅŠกไปๅฟ…้กปไฝฟ็”จ็›ธๅŒๅฏ†็ ใ€‚

Windows ๅฑ€ๅŸŸ็ฝ‘้ƒจ็ฝฒ

็Žฏๅขƒ่ฆๆฑ‚

  • Windows 10 ๆˆ–ๆ›ด้ซ˜็‰ˆๆœฌใ€‚
  • Node.js 18 ๆˆ–ๆ›ด้ซ˜็‰ˆๆœฌใ€‚
  • ๅทฒๅฎ‰่ฃ… OpenCode๏ผŒไธ”ๅ‘ฝไปค่กŒๅฏไปฅ็›ดๆŽฅๆ‰ง่กŒ opencodeใ€‚

ๅฏๅŠจๆญฅ้ชค

ๅฐ็™ฝ็”จๆˆท่ฏทๅ…ˆ้˜…่ฏปๅฎŒๆ•ด็š„ Windows ๅฑ€ๅŸŸ็ฝ‘่ฎฟ้—ฎๆŒ‡ๅ—๏ผŒๅ…ถไธญ่งฃ้‡Šไบ† 127.0.0.1ใ€0.0.0.0 ๅ’Œ 192.168.x.x ็š„ๅŒบๅˆซใ€‚

ๆŽจ่็›ดๆŽฅๅŒๅ‡ปไธ‹้ข็š„ๆ–‡ไปถ่ฟ›่กŒไธ€้”ฎ้ƒจ็ฝฒ๏ผš

scripts\deploy-windows.bat

ๅฎƒไผšๆฃ€ๆŸฅ Node.js ๅ’Œ OpenCode๏ผŒ่‡ชๅŠจๅˆ›ๅปบ .env๏ผŒๅฏๅŠจ OpenCode ๅ’Œ Web ็ฝ‘ๅ…ณ๏ผŒๅนถๆ‰“ๅผ€ๆœฌๆœบ้กต้ขใ€‚้ฆ–ๆฌกๅฏๅŠจๅŽ่ฏท็ผ–่พ‘้กน็›ฎๆ น็›ฎๅฝ•็š„ .env ไฟฎๆ”นๅฏ†็ ๏ผŒๅ†้‡ๆ–ฐ่ฟ่กŒ่„šๆœฌใ€‚

ๅฆ‚ๆžœ 4096 ็ซฏๅฃๅทฒๆœ‰ OpenCode ๆœๅŠก๏ผŒ่„šๆœฌไผšๅค็”จๅฎƒ๏ผŒ้€šๅธธๅช้œ€่ฆๅฏๅŠจ็ช—ๅฃๅ’Œ Web ็ฝ‘ๅ…ณ็ช—ๅฃ๏ผ›ๅฆ‚ๆžœ่„šๆœฌ้œ€่ฆๆ–ฐๅฏๅŠจ OpenCode๏ผŒๅฏ่ƒฝไผš้ขๅค–ๅ‡บ็Žฐไธ€ไธชๅŽ็ซฏ็ช—ๅฃใ€‚ๅฆ‚ๆžœๅŽ็ซฏๅฏๅŠจๅคฑ่ดฅ๏ผŒWeb ็ฝ‘ๅ…ณ PowerShell ็ช—ๅฃไผšไฟ็•™้”™่ฏฏไฟกๆฏ๏ผŒไธ่ฆๅช็œ‹่‡ชๅŠจๅ…ณ้—ญ็š„ๅŽ็ซฏ็ช—ๅฃใ€‚

ๅœจ PowerShell ไธญ่ฟ›ๅ…ฅ้กน็›ฎ็›ฎๅฝ•๏ผš

Copy-Item .env.example .env
notepad .env
powershell -ExecutionPolicy Bypass -File .\scripts\start-windows.ps1

่„šๆœฌไผšๅฏๅŠจ๏ผš

  • OpenCode API๏ผš127.0.0.1:4096๏ผŒๅชๅ…่ฎธๆœฌๆœบ่ฎฟ้—ฎใ€‚
  • Web ็ฝ‘ๅ…ณ๏ผš0.0.0.0:8787๏ผŒๅ…่ฎธๅฑ€ๅŸŸ็ฝ‘่ฎฟ้—ฎใ€‚

ไฝฟ็”จ ipconfig ๆŸฅ็œ‹ Windows ็š„ๅฑ€ๅŸŸ็ฝ‘ IP๏ผŒไพ‹ๅฆ‚ 192.168.1.100๏ผŒ็„ถๅŽๅœจๆ‰‹ๆœบๆต่งˆๅ™จๆ‰“ๅผ€๏ผš

http://192.168.1.100:8787/opencode/

ๅฆ‚ๆžœ Windows ้˜ฒ็ซๅข™ๆ‹ฆๆˆช 8787 ็ซฏๅฃ๏ผŒ่ฏทไฝฟ็”จ็ฎก็†ๅ‘˜ PowerShell ๆ‰ง่กŒ๏ผš

New-NetFirewallRule -DisplayName "OpenCode Mobile Server" -Direction Inbound -Protocol TCP -LocalPort 8787 -Action Allow

ๅฆ‚ๆžœ OpenCode ๅทฒ็ปไฝœไธบๅ…ถไป–ๆœๅŠก่ฟ่กŒ๏ผŒๅชๅฏๅŠจ Web ็ฝ‘ๅ…ณ๏ผš

powershell -ExecutionPolicy Bypass -File .\scripts\start-windows.ps1 -SkipBackend

Linux ๆœๅŠกๅ™จๅ’ŒๅŸŸๅ้ƒจ็ฝฒ

็Žฏๅขƒ่ฆๆฑ‚

  • Linuxใ€systemd ๅ’Œ Nginxใ€‚
  • Node.js 18 ๆˆ–ๆ›ด้ซ˜็‰ˆๆœฌใ€‚
  • OpenCode ๅทฒๅฎ‰่ฃ…ใ€‚็คบไพ‹ๆœๅŠกๆ–‡ไปถ้ป˜่ฎคไฝฟ็”จ /usr/local/bin/opencode๏ผŒ่ฏทๆŒ‰ๅฎž้™…ๅฎ‰่ฃ…่ทฏๅพ„ไฟฎๆ”นใ€‚
  • ๅŸŸๅ็š„ DNS A ๆˆ– AAAA ่ฎฐๅฝ•ๅทฒ็ปๆŒ‡ๅ‘ๆœๅŠกๅ™จใ€‚

ๅฎ‰่ฃ… Web ็ฝ‘ๅ…ณ

ๅฐ†้กน็›ฎๆ”พๅˆฐ /opt/opencode-mobile-server๏ผŒๅˆ›ๅปบๆœๅŠกๅ™จ้…็ฝฎๆ–‡ไปถ๏ผš

sudo mkdir -p /opt/opencode-mobile-server
sudo cp -a . /opt/opencode-mobile-server/
sudo mkdir -p /etc/opencode-mobile-server
sudo cp /opt/opencode-mobile-server/.env.example /etc/opencode-mobile-server/opencode.env
sudo chmod 600 /etc/opencode-mobile-server/opencode.env
sudo editor /etc/opencode-mobile-server/opencode.env
NODE_BIN="$(command -v node)"
sudo sed -e "s|__INSTALL_DIR__|/opt/opencode-mobile-server|g" -e "s|__NODE_BIN__|${NODE_BIN}|g" /opt/opencode-mobile-server/scripts/opencode-mobile-server.service | sudo tee /etc/systemd/system/opencode-mobile-server.service >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable --now opencode-mobile-server.service

ๆŽจ่ไฝฟ็”จไธ‹้ข็š„ Linux ไธ€้”ฎ้ƒจ็ฝฒ่„šๆœฌใ€‚ๅฎƒไผšๅฎ‰่ฃ… Web ็ฝ‘ๅ…ณๅ’Œ OpenCode systemd ๆœๅŠก๏ผ›ไผ ๅ…ฅๅŸŸๅๅŽ่ฟ˜ไผš็”Ÿๆˆๅนถๆฃ€ๆŸฅ Nginx ้…็ฝฎ๏ผš

sudo bash scripts/deploy-linux.sh example.com

ๅฆ‚ๆžœๅช้œ€่ฆๅฎ‰่ฃ…ๆœๅŠกใ€ไธ่‡ชๅŠจๅ†™ๅ…ฅ Nginx๏ผš

sudo bash scripts/deploy-linux.sh --skip-nginx

ๅฆ‚ๆžœ OpenCode ไธๅœจ PATH ไธญ๏ผŒๅฏไปฅๆŒ‡ๅฎšไบŒ่ฟ›ๅˆถ่ทฏๅพ„๏ผš

sudo OPENCODE_BIN=/path/to/opencode bash scripts/deploy-linux.sh example.com

่„šๆœฌ่ฟ˜ๆ”ฏๆŒไปฅไธ‹้€‰้กน๏ผš

  • --domain=example.com๏ผš็”จ้€‰้กนๅฝขๅผๆŒ‡ๅฎšๅŸŸๅใ€‚
  • --skip-nginx๏ผšๅชๅฎ‰่ฃ…ไธคไธช systemd ๆœๅŠก๏ผŒไธๅ†™ๅ…ฅ Nginx ้…็ฝฎใ€‚
  • INSTALL_DIR=/srv/opencode-mobile-server๏ผšไฟฎๆ”น้กน็›ฎๅฎ‰่ฃ…็›ฎๅฝ•ใ€‚

Linux ็ฝ‘ๅ…ณๅบ”ๅช็›‘ๅฌๅ›ž็Žฏๅœฐๅ€ใ€‚่„šๆœฌ็”Ÿๆˆ็š„ systemd ๅ•ๅ…ƒไผš่ฎพ็ฝฎ OPENCODE_WEB_HOST=127.0.0.1๏ผ›้ƒจ็ฝฒๅŽๅฏ็”จไธ‹้ข็š„ๅ‘ฝไปค็กฎ่ฎค 8787 ๆฒกๆœ‰็›ดๆŽฅๆšด้œฒๅˆฐๅ…ฌ็ฝ‘๏ผš

sudo ss -ltnp | grep ':8787'

ๅฆ‚ๆžœๆ‰‹ๅŠจไฟฎๆ”นไบ† systemd ๅ•ๅ…ƒๆˆ–็Žฏๅขƒๆ–‡ไปถ๏ผŒ่ฏทไฟ็•™ OPENCODE_WEB_HOST=127.0.0.1๏ผŒๅ†ๆ‰ง่กŒ sudo systemctl daemon-reload && sudo systemctl restart opencode-mobile-server.serviceใ€‚

ๅฎ‰่ฃ… OpenCode API ๆœๅŠก

OpenCode ๆœๅŠกๅฟ…้กปไฝฟ็”จๅ’Œ /etc/opencode-mobile-server/opencode.env ็›ธๅŒ็š„ๅฏ†็ ใ€‚้กน็›ฎๆไพ›ไบ†ๅ‚่€ƒๆœๅŠกๆ–‡ไปถ๏ผš

sudo cp /opt/opencode-mobile-server/scripts/opencode-server.service /etc/systemd/system/opencode-server.service
sudo editor /etc/systemd/system/opencode-server.service
sudo systemctl daemon-reload
sudo systemctl enable --now opencode-server.service

ๅฎ‰่ฃ…ๅ‰่ฏทๆฃ€ๆŸฅ ExecStart ไธญ็š„ OpenCode ่ทฏๅพ„ใ€‚ๆœๅŠกๅ™จไธŠ็š„ OpenCode API ้ป˜่ฎคๅช็›‘ๅฌ 127.0.0.1:4096๏ผŒไธ่ฆ็›ดๆŽฅๆšด้œฒ 4096 ็ซฏๅฃใ€‚

้…็ฝฎๅŸŸๅ่ฎฟ้—ฎ

็”Ÿๆˆๆˆ–ๅคๅˆถ scripts/nginx-opencode-mobile-server.conf.example๏ผŒๆŠŠ __DOMAIN__ ๆ›ฟๆขไธบๅฎž้™…ๅŸŸๅใ€‚ๆจกๆฟๅฎŒๆˆ Nginx ๅˆฐๆœฌๆœบ็ฝ‘ๅ…ณ็š„ HTTP ๅๅ‘ไปฃ็†๏ผŒๅˆๅง‹่ฎฟ้—ฎๅœฐๅ€ๆ˜ฏ๏ผš

http://example.com/opencode/

ๆฃ€ๆŸฅๅนถ้‡ๆ–ฐๅŠ ่ฝฝ Nginx๏ผš

sudo nginx -t
sudo systemctl reload nginx

ๅฆ‚ๆžœๅŸŸๅๅทฒ็ปๆœ‰ Nginx server ๅ—๏ผŒไธ่ฆๅˆ›ๅปบ้‡ๅค็š„ server๏ผŒๅชๆŠŠๆจกๆฟไธญ็š„ /opencode ไธคไธช location ๅคๅˆถๅˆฐ็Žฐๆœ‰้…็ฝฎไธญใ€‚ๅ…ฌ็ฝ‘ไฝฟ็”จๆ—ถๅฟ…้กปๅฆๅค–้…็ฝฎ HTTPSใ€443 ็›‘ๅฌๅ’Œ่ฏไนฆ๏ผ›ๅฎŒๆˆๅŽๅ†ไฝฟ็”จ๏ผš

https://example.com/opencode/

ๅฏไปฅไฝฟ็”จ Certbot ๆˆ–ๆœๅŠกๅ™จๅทฒๆœ‰็š„่ฏไนฆ็ฎก็†ๅทฅๅ…ท้…็ฝฎ HTTPSใ€‚ไธ่ฆๆŠŠ 4096 ็ซฏๅฃๆˆ–ๆœช็ป HTTPS ไฟๆŠค็š„ 8787 ็ซฏๅฃ็›ดๆŽฅๆšด้œฒๅˆฐๅ…ฌ็ฝ‘ใ€‚

ๆ‰‹ๅŠจ่ฟ่กŒ

ๅคๅˆถๅนถ็ผ–่พ‘ .env ๅŽๆ‰ง่กŒ๏ผš

npm start

้ป˜่ฎค่ฎฟ้—ฎๅœฐๅ€๏ผš

http://127.0.0.1:8787/opencode/

ๆœฌ้กน็›ฎๆฒกๆœ‰่ฟ่กŒๆ—ถ npm ไพ่ต–๏ผŒไธ้œ€่ฆๆ‰ง่กŒ npm installใ€‚

npm run start:lan ๅชๆ˜ฏ npm start ็š„ๅˆซๅ๏ผŒไธไผš่‡ชๅŠจไฟฎๆ”น็›‘ๅฌๅœฐๅ€๏ผ›ๆ˜ฏๅฆๅ…่ฎธๅฑ€ๅŸŸ็ฝ‘่ฎฟ้—ฎ็”ฑ OPENCODE_WEB_HOST ๅ†ณๅฎšใ€‚

้…็ฝฎ้กน

้…็ฝฎ้กน ้ป˜่ฎคๅ€ผ ่ฏดๆ˜Ž
OPENCODE_SERVER_PASSWORD opencode OpenCode Basic Auth ๅฏ†็ ๏ผŒๅฟ…้กปไฟฎๆ”น
OPENCODE_API_URL http://127.0.0.1:4096 OpenCode ๆœๅŠกๅœฐๅ€
OPENCODE_WEB_HOST 127.0.0.1 Web ็ฝ‘ๅ…ณ็›‘ๅฌๅœฐๅ€๏ผ›Windows ่„šๆœฌไผšไฝฟ็”จ 0.0.0.0
OPENCODE_WEB_PORT 8787 Web ็ฝ‘ๅ…ณ็ซฏๅฃ
OPENCODE_SERVICE_NAME opencode-server.service ๅฏ†้’ฅๅˆ‡ๆขๅŽ่‡ชๅŠจ้‡ๅฏ็š„ OpenCode systemd ๆœๅŠก๏ผ›่ฎพไธบ none ๅ…ณ้—ญ่‡ชๅŠจ้‡ๅฏ
OPENCODE_AUTH_FILE ่‡ชๅŠจๆฃ€ๆต‹ OpenCode ็š„ auth.json ่ทฏๅพ„๏ผˆLinux ไธบ ~/.local/share/opencode/auth.json๏ผŒmacOS ไธบ ~/Library/Application Support/opencode/auth.json๏ผŒWindows ไธบ %LOCALAPPDATA%\opencode\auth.json๏ผ‰
OPENCODE_AUTH_KEYS_FILE ้กน็›ฎ็›ฎๅฝ• .auth-keys.json ็ฝ‘ๅ…ณไฟๅญ˜ๅ…จ้ƒจๅฏ†้’ฅ็š„ๆœฌๅœฐๆ–‡ไปถ๏ผˆๅปบ่ฎฎ Linux ๆœๅŠกๅ™จๆ”พๅˆฐ /etc/opencode-mobile-server/๏ผ‰

ๆต่งˆๅ™จ API ่ทฏๅพ„ๅ›บๅฎšไธบ /opencode/api๏ผŒๅ› ๆญคๅŒไธ€ไปฝๅ‰็ซฏไปฃ็ ๆ—ขๅฏไปฅ็”จไบŽ Windows ๅฑ€ๅŸŸ็ฝ‘๏ผŒไนŸๅฏไปฅ็”จไบŽๆœๅŠกๅ™จๅŸŸๅ่ทฏๅพ„๏ผŒไธ้œ€่ฆ้‡ๆ–ฐๆž„ๅปบใ€‚

OPENCODE_AUTH_FILEใ€OPENCODE_AUTH_KEYS_FILE ๅปบ่ฎฎไฝฟ็”จ็ปๅฏน่ทฏๅพ„ใ€‚Linux ไฝฟ็”จ XDG_DATA_HOME ๆ—ถ๏ผŒ่‡ชๅŠจๆฃ€ๆต‹็š„ OpenCode ๅฏ†้’ฅๆ–‡ไปถไธบ $XDG_DATA_HOME/opencode/auth.jsonใ€‚

ๆ–ฐ็‰ˆๅฎขๆˆท็ซฏๅŠŸ่ƒฝ

้กถ้ƒจๅฏไปฅ้€‰ๆ‹ฉ OpenCode ่ฟ”ๅ›ž็š„ๆจกๅž‹ใ€Agent ๅ’Œๆ€่€ƒๅผบๅบฆใ€‚ๆฏไธชๆจกๅž‹ๅŽ้ขไผšๆ˜พ็คบๅฎƒๆ”ฏๆŒ็š„่พ“ๅ…ฅๆจกๆ€๏ผˆๅ›พๅƒใ€้Ÿณ้ข‘ใ€่ง†้ข‘ใ€ๆ–‡ๆกฃ๏ผ‰ไปฅๅŠ่พ“ๅ‡บๆจกๆ€๏ผˆๅฆ‚โ€œ็”Ÿๆˆ๐Ÿ–ผ๏ธโ€่กจ็คบๅฏไปฅ็”Ÿๆˆๅ›พๅƒ๏ผ‰๏ผ›ๆฒกๆœ‰้ขๅค–ๆจกๆ€็š„ๆจกๅž‹ๆ˜พ็คบโ€œ็บฏๆ–‡ๆœฌโ€ใ€‚ๆจกๆ€ไฟกๆฏๆฅ่‡ช OpenCode ็š„ provider ้…็ฝฎ๏ผŒOpenCode ็‰ˆๆœฌ่ฟ‡ๆ—งๆ—ถๅฏ่ƒฝไธๆ˜พ็คบใ€‚ๆ€่€ƒๅผบๅบฆๅชๅœจๅฝ“ๅ‰ๆจกๅž‹ๆ”ฏๆŒๆŽจ็†ๆ—ถๆ˜พ็คบ๏ผŒๅฏ็”จๅ€ผ็”ฑๆจกๅž‹้…็ฝฎๅ†ณๅฎš๏ผŒๅธธ่งๅ€ผๅŒ…ๆ‹ฌ noneใ€minimalใ€lowใ€mediumใ€highใ€max ๅ’Œ xhighใ€‚

ๅฝ“ๅ‰ๆจกๅž‹ๆไพ›ไธŠไธ‹ๆ–‡ไธŠ้™ๆ—ถ๏ผŒๆถˆๆฏๅŒบไธŠๆ–นไผšๆ˜พ็คบๅทฒ็”จ tokensใ€ไธŠ้™ๅ’Œ็™พๅˆ†ๆฏ”๏ผ›่พพๅˆฐ 70% ๆˆ– 90% ๆ—ถไผšๆ”นๅ˜ๆ็คบ้ขœ่‰ฒใ€‚ๅฎƒๆ˜ฏไผฐ็ฎ—ๆ็คบ๏ผŒไธไผšๆ›ฟไปฃ OpenCode ็š„ๅฎž้™…้™ๅˆถใ€‚

ๅฝ“ Agent ้€š่ฟ‡ OpenCode question.asked ไบ‹ไปถๆ้—ฎๆ—ถ๏ผŒ้กต้ขไผšๆ˜พ็คบ้—ฎ้ข˜้ขๆฟ๏ผŒๆ”ฏๆŒๅ•้€‰ใ€ๅคš้€‰ใ€่‡ชๅฎšไน‰ๆ–‡ๆœฌๅ’Œ่ทณ่ฟ‡ใ€‚ๅ›ž็ญ”ๆˆ–่ทณ่ฟ‡ๅŽ๏ผŒไปปๅŠกไผš็ปง็ปญๆ‰ง่กŒ๏ผ›ๅˆ‡ๆขไผš่ฏๆ—ถไธไผšๆ˜พ็คบๅ…ถไป–ไผš่ฏ็š„้—ฎ้ข˜ใ€‚ๅฎขๆˆท็ซฏๅŒๆ—ถๅ…ผๅฎนๆ–ฐ็‰ˆ OpenCode ไธ‹ๅ‘็š„ question.v2.* ๅ’Œ permission.v2.* ไบ‹ไปถๅใ€‚

็”Ÿๆˆไธญ็š„ๆถˆๆฏ้€š่ฟ‡ SSE ๅขž้‡ๆ˜พ็คบใ€‚่ฟžๆŽฅไธญๆ–ญๆ—ถ้กต้ขไผš่‡ชๅŠจ้‡่ฟž๏ผŒๅนถๅœจ้กต้ขๅฏ่งๆ—ถ่ฝฎ่ฏขไผš่ฏ็Šถๆ€ๅ’Œๆœ€่ฟ‘ๆถˆๆฏไฝœไธบๅ…œๅบ•ใ€‚้กถ้ƒจ็š„ๅœๆญขๆŒ‰้’ฎไผš่ฐƒ็”จ OpenCode ็š„ abort ๆŽฅๅฃใ€‚

ๅ›žๅคไฝฟ็”จ่ฝป้‡ Markdown๏ผŒๆ”ฏๆŒๆ ‡้ข˜ใ€็ฒ—ไฝ“ใ€ๆ–œไฝ“ใ€ๅˆ ้™ค็บฟใ€่กŒๅ†…ไปฃ็ ใ€ไปฃ็ ๅ—ใ€ๅผ•็”จใ€ๆ— ๅบๅˆ—่กจใ€้“พๆŽฅใ€็ฎ€ๅ•่กจๆ ผๅ’Œๅˆ†้š”็บฟ๏ผ›ไธๆไพ›ๅฎŒๆ•ด Markdown ่งฃๆžๆˆ–ไปฃ็ ้ซ˜ไบฎใ€‚้™„ไปถไผš่ฝฌๆˆ Data URL ๅŽๅ‘้€๏ผŒๅ•ๆฌกๆถˆๆฏ้€‰ๆ‹ฉ็š„ๆ–‡ไปถๅŽŸๅง‹ๅคงๅฐๅˆ่ฎกๆœ€ๅคš 20 MB๏ผŒๆ”ฏๆŒ็›ดๆŽฅๆŠŠๆ–‡ไปถๆˆ–ๅ›พ็‰‡ๆ‹–่ฟ›่พ“ๅ…ฅๆก†๏ผ›ไป…ๅธฆ้™„ไปถไนŸๅฏไปฅ็›ดๆŽฅๅ‘้€ใ€‚ๅๅ‘ไปฃ็†็š„่ฏทๆฑ‚ไฝ“้™ๅˆถๅบ”ๅคงไบŽ 20 MB๏ผŒ้กน็›ฎ Nginx ๆจกๆฟไฝฟ็”จ 50 MBใ€‚

้กต้ขๆไพ› PWA ๅฎ‰่ฃ…ๆธ…ๅ•๏ผŒไฝ†ไธๅŒ…ๅซ็ฆป็บฟ็ผ“ๅญ˜ใ€‚ๅฏ†็ ใ€ๅฝ“ๅ‰ไผš่ฏใ€ๆจกๅž‹ใ€Agent ๅ’Œๆ€่€ƒๅผบๅบฆไฟๅญ˜ๅœจๅฝ“ๅ‰ๆต่งˆๅ™จ็š„ๆœฌๅœฐๅญ˜ๅ‚จไธญ๏ผ›ๆถˆๆฏๅ†…ๅฎนไธป่ฆๅœจ้กต้ขๅ†…ๅญ˜ไธญ็ผ“ๅญ˜๏ผŒๅˆทๆ–ฐๅŽไผš้‡ๆ–ฐไปŽ OpenCode ๅŠ ่ฝฝใ€‚

็ฎก็† API ๅฏ†้’ฅ

ไพง่พนๆ ็š„โ€œ๐Ÿ”‘ ๅฏ†้’ฅโ€้ขๆฟๅฏไปฅไฟๅญ˜ใ€ๅˆ‡ๆขๅ’Œๅˆ ้™คๅ„ provider๏ผˆๅฆ‚ opencode-go๏ผ‰็š„ API ๅฏ†้’ฅ๏ผŒไธๅ†้œ€่ฆ็™ปๅฝ•ๆœๅŠกๅ™จๆ‰ง่กŒ opencode auth loginใ€‚

  • ไฟๅญ˜ๅนถๅˆ‡ๆข๏ผšๆ–ฐๅฏ†้’ฅไผšๅ†™ๅ…ฅ OpenCode ็š„ auth.json ๅนถๆˆไธบๅฝ“ๅ‰็”Ÿๆ•ˆๅฏ†้’ฅใ€‚Linux ไธŠ็ฝ‘ๅ…ณไผš่‡ชๅŠจ้‡ๅฏ OPENCODE_SERVICE_NAME ๆŒ‡ๅฎš็š„ๆœๅŠก่ฎฉๆ–ฐๅฏ†้’ฅ็ซ‹ๅณ็”Ÿๆ•ˆ๏ผˆ็บฆๅ‡ ็ง’้’Ÿ๏ผ‰๏ผŒ้กต้ขไผš่‡ชๅŠจ้‡่ฟž๏ผ›Windows ๆˆ–่‡ชๅŠจ้‡ๅฏไธๅฏ็”จๆ—ถ๏ผŒ่ฏทๆ‰‹ๅŠจ้‡ๅฏ OpenCode ๅŽๅˆทๆ–ฐ้กต้ขใ€‚่‹ฅๅœจๆœๅŠกๅ™จ็ปˆ็ซฏ็›ดๆŽฅ่ฟ่กŒ opencode๏ผŒไนŸ้œ€่ฆ้‡ๅฏ่ฏฅ็ปˆ็ซฏไผš่ฏใ€‚
  • ๅˆ‡ๆข๏ผšๅฏไปฅๅœจๅทฒไฟๅญ˜็š„ๅคšไธชๅฏ†้’ฅไน‹้—ดๆฅๅ›žๅˆ‡ๆขใ€‚
  • ๅˆ ้™ค๏ผšๅชไปŽไฟๅญ˜ๅˆ—่กจไธญ็งป้™ค๏ผŒไธๅฝฑๅ“ๅฝ“ๅ‰็”Ÿๆ•ˆๅฏ†้’ฅใ€‚
  • ๅฏ†้’ฅไฟๅญ˜ๅœจ OPENCODE_AUTH_KEYS_FILE ๆŒ‡ๅฎš็š„ๆ–‡ไปถไธญ๏ผˆ้ป˜่ฎค้กน็›ฎ็›ฎๅฝ• .auth-keys.json๏ผŒๅทฒๅŠ ๅ…ฅ .gitignore๏ผ‰๏ผŒไธไผšๅ†™ๅ…ฅไป“ๅบ“๏ผ›ๅ†™ๅ…ฅ auth.json ๅ‰ไผšไฟ็•™ไธ€ไปฝ auth.json.bak ๅค‡ไปฝใ€‚ๆŽฅๅฃๅช่ฟ”ๅ›žๆŽฉ็ ๅ’Œๅฏ†้’ฅๅ…ƒๆ•ฐๆฎ๏ผŒไธไผšๆŠŠๅŽŸๅง‹ๅฏ†้’ฅ่ฟ”ๅ›ž็ป™้กต้ขใ€‚
  • Linux ๅปบ่ฎฎๆŠŠ OPENCODE_AUTH_KEYS_FILE ๆ”พๅœจ /etc/opencode-mobile-server/๏ผŒๅนถ้™ๅˆถไธบไป…ๆœๅŠก็”จๆˆทๅฏ่ฏป๏ผ›ไธ่ฆๆŠŠ .auth-keys.jsonใ€auth.json ๆˆ– auth.json.bak ๆ”พๅ…ฅ Git ไป“ๅบ“ๆˆ–ๅค‡ไปฝๅˆฐๅ…ฌๅผ€ไฝ็ฝฎใ€‚

ๆณจๆ„๏ผšOpenCode ๆœๅŠกๅœจๅฏๅŠจๆ—ถ่ฏปๅ–ๅฏ†้’ฅๅนถ็ผ“ๅญ˜๏ผŒๆ‰€ไปฅไฟฎๆ”นๅฏ†้’ฅๅŽๅฟ…้กป้‡ๅฏๆœๅŠกๆ‰ไผš็”Ÿๆ•ˆ๏ผŒ่ฟ™ๅฐฑๆ˜ฏไฟๅญ˜/ๅˆ‡ๆขๅŽ็ฝ‘ๅ…ณ่‡ชๅŠจๆ‰ง่กŒ้‡ๅฏ็š„ๅŽŸๅ› ใ€‚

ๅ‡็บงๆ–ฐ็‰ˆ

ๅ‡็บงๆ—ถ่ฏทๅŒๆ—ถๆ›ฟๆข index.htmlใ€server.mjsใ€package.json ๅ’Œ scripts/ ไธ‹็š„้ƒจ็ฝฒๆ–‡ไปถ๏ผŒไธ่ฆๅชๆ›ฟๆข้กต้ขใ€‚Windows ไฟ็•™้กน็›ฎๆ น็›ฎๅฝ•็š„ .env๏ผ›Linux ไฟ็•™ /etc/opencode-mobile-server/opencode.env ๅ’Œๅฏ†้’ฅๆ–‡ไปถใ€‚ๆ›ฟๆขๅฎŒๆˆๅŽ้‡ๅฏ Web ็ฝ‘ๅ…ณๅ’Œ OpenCode ๆœๅŠก๏ผš

sudo systemctl restart opencode-server.service opencode-mobile-server.service

ๅฆ‚ๆžœไฝฟ็”จ Windows๏ผŒๅ…ณ้—ญๆ—ง็š„ PowerShell ็ฝ‘ๅ…ณ็ช—ๅฃๅŽ้‡ๆ–ฐ่ฟ่กŒ scripts\deploy-windows.bat๏ผŒๆˆ–่ฟ่กŒ scripts\start-windows.ps1ใ€‚ๆต่งˆๅ™จไปๆ˜พ็คบๆ—ง้กต้ขๆ—ถ๏ผŒๅ…ˆๅผบๅˆถๅˆทๆ–ฐ๏ผ›ๆจกๅž‹ใ€Agent ๆˆ–ๆ€่€ƒๅผบๅบฆๅผ‚ๅธธๆ—ถ๏ผŒๅฏๆธ…็†ๆต่งˆๅ™จๆœฌๅœฐๅญ˜ๅ‚จไธญ็š„ oc_modelใ€oc_agent ๅ’Œ oc_variants ๅŽ้‡ๆ–ฐๆ‰“ๅผ€้กต้ขใ€‚

ๆ•…้šœๆŽ’ๆŸฅ

ไธ€็›ดๆ˜พ็คบ 401 ๆˆ–ๅๅคๅ‡บ็Žฐ็™ปๅฝ•้กต้ข

ๆฃ€ๆŸฅ .envใ€/etc/opencode-mobile-server/opencode.env ๅ’Œ opencode serve ไฝฟ็”จ็š„ๅฏ†็ ๆ˜ฏๅฆไธ€่‡ดใ€‚็„ถๅŽ้€€ๅ‡บ็™ปๅฝ•๏ผŒๆˆ–ๆธ…้™ค็ฝ‘็ซ™ๆœฌๅœฐๅญ˜ๅ‚จไธญ็š„ oc_pwd ๅŽ้‡ๆ–ฐ็™ปๅฝ•ใ€‚

ๅฆ‚ๆžœๆต่งˆๅ™จๅผนๅ‡บๆ ‡้ข˜ไธบ Secure Area ็š„็”จๆˆทๅๅ’Œๅฏ†็ ๅฏน่ฏๆก†๏ผŒ้‚ฃๆ˜ฏๆ—ง็ฝ‘ๅ…ณ้€ไผ ไบ† OpenCode ็š„ WWW-Authenticate ๅ“ๅบ”ๅคด๏ผŒ่งฆๅ‘ไบ†ๆต่งˆๅ™จๅŽŸ็”Ÿ Basic Authใ€‚ๅฝ“ๅ‰็ฝ‘ๅ…ณไผš้š่—่ฟ™ไธชๅ“ๅบ”ๅคด๏ผŒๅชๆ˜พ็คบ้กต้ข่‡ชๅทฑ็š„็™ปๅฝ•ๆก†๏ผ›ๆ›ดๆ–ฐ้กต้ขๅŽ่ฏทๅˆทๆ–ฐๆต่งˆๅ™จ็ผ“ๅญ˜ใ€‚

/opencode/api ่ฟ”ๅ›ž 502

Node ็ฝ‘ๅ…ณๆ— ๆณ•่ฟžๆŽฅ OPENCODE_API_URLใ€‚ๆฃ€ๆŸฅ OpenCode ๆ˜ฏๅฆ็›‘ๅฌ 4096 ็ซฏๅฃ๏ผŒไปฅๅŠ้…็ฝฎๆ–‡ไปถไธญ็š„ๅœฐๅ€ๆ˜ฏๅฆๆญฃ็กฎใ€‚

ๅ›žๅค่ฆ็ญ‰็”ŸๆˆๅฎŒๆˆๅŽๆ‰ๅ‡บ็Žฐ

่ฏทๅˆทๆ–ฐ้กต้ข่Žทๅ–ๆœ€ๆ–ฐ็š„ index.htmlใ€‚ๅฎขๆˆท็ซฏไผšๆŒ‰ SSE ๆ ‡ๅ‡†ๅŒๆ—ถ่งฃๆž LF ๅ’Œ CRLF ไบ‹ไปถๅˆ†้š”็ฌฆ๏ผ›็ฝ‘ๅ…ณๅ’Œ Nginx ไนŸไผšๅฏนไบ‹ไปถๆตๅ…ณ้—ญ็ผ“ๅญ˜ๅ’Œๅ†…ๅฎน่ฝฌๆขใ€‚ๅฆ‚ๆžœไป็„ถ่ขซ็ผ“ๅญ˜๏ผŒ่ฏท็กฎ่ฎค่‡ชๅฎšไน‰ Nginx ้…็ฝฎๅŒ…ๅซ proxy_buffering off ๅ’Œ proxy_cache offใ€‚

ๆ€่€ƒๅผบๅบฆ

้กถ้ƒจโ€œๆ€่€ƒโ€้€‰ๆ‹ฉๅ™จไผšๅœจๅฝ“ๅ‰ๆจกๅž‹ๆ”ฏๆŒๆŽจ็†ๆ—ถๆ˜พ็คบใ€‚ๅฎƒไผ ้€’ OpenCode ็š„ variant ๅ‚ๆ•ฐ๏ผŒๅธธ่ง้€‰้กนๅŒ…ๆ‹ฌ noneใ€minimalใ€lowใ€mediumใ€highใ€max ๅ’Œ xhigh๏ผ›ๅ…ทไฝ“ๅฏ็”จ้€‰้กนไปฅๆจกๅž‹ๅ’Œ OpenCode ็‰ˆๆœฌไธบๅ‡†ใ€‚้€‰ๆ‹ฉไผšๆŒ‰ๆจกๅž‹ไฟๅญ˜ๅœจๆต่งˆๅ™จๆœฌๅœฐใ€‚

้กต้ข่ƒฝๆ‰“ๅผ€๏ผŒไฝ†ไธๆ˜ฏ /opencode/

่ฏทไฝฟ็”จๅธฆ็ป“ๅฐพๆ–œๆ ็š„ๅฎŒๆ•ด่ทฏๅพ„ใ€‚็ฝ‘ๅ…ณไผš่‡ชๅŠจๅฐ† / ๅ’Œ /opencode ้‡ๅฎšๅ‘ๅˆฐ /opencode/ใ€‚

็‚นๅ‡ปโ€œๅŠ ่ฝฝๆ›ดๆ—ฉๆถˆๆฏโ€่ฟ”ๅ›ž 400

ๆ–ฐ็‰ˆๅฎขๆˆท็ซฏไฝฟ็”จ OpenCode ๅˆ†้กตๆŽฅๅฃ่ฟ”ๅ›ž็š„ X-Next-Cursor ๆธธๆ ‡ใ€‚ๅฆ‚ๆžœๅˆšๅ‡็บง่ฟ‡๏ผŒ่ฏทๅˆทๆ–ฐ้กต้ข๏ผŒ้ฟๅ…ๆต่งˆๅ™จ็ปง็ปญไฝฟ็”จๆ—ง็š„ index.html ็ผ“ๅญ˜ใ€‚

ๆจกๅž‹ๅˆ—่กจไธบ็ฉบๆˆ– AI ่ฟฝ้—ฎไธๆ˜พ็คบ

็กฎ่ฎค OpenCode ็‰ˆๆœฌๆไพ› /config/providersใ€/agentใ€/questionใ€/session/status ๅ’Œไบ‹ไปถๆŽฅๅฃ๏ผŒๅนถๆฃ€ๆŸฅ็ฝ‘ๅ…ณๆ—ฅๅฟ—ใ€‚ๅ‡็บง้กต้ขๅŽๅผบๅˆถๅˆทๆ–ฐ๏ผ›ๅฆ‚ๆžœๅชๆœ‰ๆŸไธชๆจกๅž‹ๆฒกๆœ‰ๆ€่€ƒ้€‰ๆ‹ฉๅ™จ๏ผŒ้€šๅธธๆ˜ฏ่ฏฅๆจกๅž‹ๆฒกๆœ‰้…็ฝฎๆŽจ็†่ƒฝๅŠ›ๆˆ– variantsใ€‚

็›ดๆŽฅ่ฎฟ้—ฎ API ๆฒกๆœ‰็™ปๅฝ•้กต้ข

/opencode/api/* ๆ˜ฏ API ๅœฐๅ€๏ผŒไธๆ˜ฏ็ฝ‘้กต็™ปๅฝ•้กตใ€‚่ฏทๆฑ‚ๅฟ…้กปๆบๅธฆ็ฝ‘ๅ…ณๅฏ†็ ๅฏนๅบ”็š„ Basic Auth๏ผ›ๆต่งˆๅ™จๅบ”่ฎฟ้—ฎ /opencode/๏ผŒ็”ฑ้กต้ขๆ˜พ็คบ็™ปๅฝ•ๆก†ใ€‚ไธ่ฆๆŠŠ OPENCODE_API_URL ๆŒ‡ๅ‘ไธ€ไธชๆฒกๆœ‰่ฎค่ฏใ€ๅดๅˆๅฏ่ขซๅ…ฌ็ฝ‘่ฎฟ้—ฎ็š„ๅŽ็ซฏใ€‚

ๅฎ‰ๅ…จๅปบ่ฎฎ

  • ้ป˜่ฎคๅฏ†็ ๅช็”จไบŽ้ฆ–ๆฌกๅฏๅŠจ๏ผŒๅค–็ฝ‘้ƒจ็ฝฒๅ‰ๅฟ…้กปไฟฎๆ”นใ€‚
  • ๆœๅŠกๅ™จไธŠไฟๆŒ 4096 ๅช็›‘ๅฌๅ›ž็Žฏๅœฐๅ€ใ€‚
  • Linux ็ฝ‘ๅ…ณๅช็›‘ๅฌ 127.0.0.1๏ผŒ็”ฑ Nginx ่ดŸ่ดฃๅ…ฌ็ฝ‘ๅ…ฅๅฃ๏ผ›Windows ๅฑ€ๅŸŸ็ฝ‘ไฝฟ็”จ 0.0.0.0 ๆ—ถๅชๅผ€ๆ”พๅฏไฟก็ฝ‘็ปœใ€‚
  • ๅ…ฌ็ฝ‘่ฎฟ้—ฎๅฟ…้กปๅฏ็”จ HTTPSใ€‚
  • ไธ่ฆๆŠŠ .envใ€.auth-keys.jsonใ€OpenCode auth.jsonใ€API ๅฏ†้’ฅๆˆ–ๅ…ถไป–ๅ‡ญๆฎๆไบคๅˆฐ GitHub๏ผŒๅชๆไบค .env.exampleใ€‚
  • ๅ…ฌ็ฝ‘้ƒจ็ฝฒไธ่ฆไฝฟ็”จ --skip-nginx ๅŽ็›ดๆŽฅๆšด้œฒ 8787๏ผ›ๅ…ˆ้…็ฝฎ HTTPS ๅ’Œๅๅ‘ไปฃ็†ใ€‚

English Documentation

OpenCode Mobile Server

This is a self-hosted mobile web client and deployment gateway for the OpenCode ecosystem. It provides a session list, streaming replies, Markdown rendering, tool-call display, file uploads, and a mobile drawer navigation. You can run it on a Windows computer for phone access over your LAN, or deploy it to a Linux server and expose it with a domain over HTTPS.

Features

  • Mobile-first dark interface with safe-area support.
  • Session switching with cached history and cursor-based pagination.
  • Live generation updates through the OpenCode event API, with polling fallback when SSE is unavailable.
  • Model, Agent, and per-model reasoning-variant selection; when you switch sessions the model selector follows the model actually in use by that session. The model list shows each model's supported modalities (๐Ÿ–ผ๏ธ image, ๐ŸŽ™๏ธ audio, ๐ŸŽฌ video, ๐Ÿ“„ document); text-only models are labeled "็บฏๆ–‡ๆœฌ".
  • Context usage display when the current model reports a context limit.
  • OpenCode AI question prompts with single-choice, multiple-choice, custom answers, and skip.
  • File attachments up to 20 MB per request; files or images can be dropped straight onto the input box.
  • Manage provider API keys from the drawer; the gateway restarts OpenCode so the new key takes effect.
  • Stop-generation control, retry notifications, code-block copy, and a mobile PWA install entry point.
  • No frontend build step and no runtime npm dependencies.
  • A Node gateway serves the static page and proxies /opencode/api/* to OpenCode.

How It Works

Phone or desktop browser
  -> /opencode/                 mobile web page
  -> /opencode/api/*            Node gateway
                                  -> 127.0.0.1:4096 OpenCode server

This project is an OpenCode client and gateway. It does not include or replace the OpenCode CLI. Install OpenCode separately and make sure opencode serve is available.

The current client expects OpenCode provider, Agent, event, question, and cursor-pagination endpoints. With an older OpenCode API, the model list may be empty, AI questions may not appear, or loading older messages may fail. Use a version compatible with the current OpenCode API.

Linux Directory Layout

Linux keeps one Web project directory:

/opt/opencode-mobile-server/                 project files and Node gateway
/etc/opencode-mobile-server/opencode.env    password and runtime settings
/root/.opencode/                              OpenCode CLI itself

Nginx is responsible for the domain, HTTPS, and reverse-proxying /opencode/. It no longer serves a second frontend copy from /www/wwwroot. Windows and Linux therefore use the same project layout. The included Nginx template listens on HTTP port 80 only; certificates and port 443 still need to be configured separately.

Default Password and Password Changes

The default password for a new installation is:

opencode

Change it before exposing the service to the Internet. Copy .env.example to .env and edit:

OPENCODE_SERVER_PASSWORD=replace-with-a-random-long-password

The gateway and the OpenCode server must use the same password:

  • Windows: edit .env in the project root.
  • Linux server: edit /etc/opencode-mobile-server/opencode.env.

The mobile page stores the entered password in browser local storage. After changing the password, use the logout button to sign in again. If that does not work, clear the site's oc_pwd local-storage value.

The gateway validates the password portion of Basic Auth for every /opencode/api/* request before proxying it to OpenCode. The OpenCode service must still use the same password; the Basic Auth username is not used for authorization.

Windows LAN Deployment

Requirements

  • Windows 10 or newer.
  • Node.js 18 or newer.
  • OpenCode installed and available as the opencode command.

Start

For a beginner-friendly explanation of 127.0.0.1, 0.0.0.0, and 192.168.x.x, read the full Windows LAN Access Guide.

For a one-click Windows deployment, double-click:

scripts\deploy-windows.bat

It checks Node.js and OpenCode, creates .env when needed, starts OpenCode and the web gateway, and opens the local page. Edit the project-root .env after the first run to change the password, then run the script again.

If OpenCode is already listening on port 4096, the script reuses it and normally only the launcher and web-gateway windows are needed. If the script starts OpenCode itself, an additional backend window may appear. If the backend fails, the web-gateway PowerShell window keeps the error message instead of hiding it in a closed window.

Open PowerShell in the project directory:

Copy-Item .env.example .env
notepad .env
powershell -ExecutionPolicy Bypass -File .\scripts\start-windows.ps1

The script starts:

  • OpenCode API: 127.0.0.1:4096, accessible only from the Windows machine.
  • Web gateway: 0.0.0.0:8787, accessible from the LAN.

Run ipconfig to find the Windows LAN address. For example, if it is 192.168.1.100, open this URL on your phone:

http://192.168.1.100:8787/opencode/

If Windows Firewall blocks port 8787, run PowerShell as Administrator:

New-NetFirewallRule -DisplayName "OpenCode Mobile Server" -Direction Inbound -Protocol TCP -LocalPort 8787 -Action Allow

If OpenCode is already running as a separate service, start only the web gateway:

powershell -ExecutionPolicy Bypass -File .\scripts\start-windows.ps1 -SkipBackend

Linux Server and Domain Deployment

Requirements

  • Linux with systemd and Nginx.
  • Node.js 18 or newer.
  • OpenCode installed. The example service uses /usr/local/bin/opencode; adjust it to the actual path.
  • An A or AAAA DNS record pointing your domain to the server.

Install the Web Gateway

Copy the project to /opt/opencode-mobile-server and create the server environment file:

sudo mkdir -p /opt/opencode-mobile-server
sudo cp -a . /opt/opencode-mobile-server/
sudo mkdir -p /etc/opencode-mobile-server
sudo cp /opt/opencode-mobile-server/.env.example /etc/opencode-mobile-server/opencode.env
sudo chmod 600 /etc/opencode-mobile-server/opencode.env
sudo editor /etc/opencode-mobile-server/opencode.env
NODE_BIN="$(command -v node)"
sudo sed -e "s|__INSTALL_DIR__|/opt/opencode-mobile-server|g" -e "s|__NODE_BIN__|${NODE_BIN}|g" /opt/opencode-mobile-server/scripts/opencode-mobile-server.service | sudo tee /etc/systemd/system/opencode-mobile-server.service >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable --now opencode-mobile-server.service

For a one-click Linux deployment, run the following script. It installs the web gateway and OpenCode systemd services. When you pass a domain, it also generates and checks the Nginx configuration:

sudo bash scripts/deploy-linux.sh example.com

To install the services without writing an Nginx configuration:

sudo bash scripts/deploy-linux.sh --skip-nginx

If OpenCode is not in PATH, provide its binary path:

sudo OPENCODE_BIN=/path/to/opencode bash scripts/deploy-linux.sh example.com

The script also supports:

  • --domain=example.com to provide the domain as an option.
  • --skip-nginx to install only the two systemd services.
  • INSTALL_DIR=/srv/opencode-mobile-server to change the installation directory.

The Linux gateway should bind only to the loopback address. The generated systemd unit sets OPENCODE_WEB_HOST=127.0.0.1. After deployment, verify that port 8787 is not exposed directly:

sudo ss -ltnp | grep ':8787'

If you edit the systemd unit or environment file, keep OPENCODE_WEB_HOST=127.0.0.1, then run sudo systemctl daemon-reload && sudo systemctl restart opencode-mobile-server.service.

Install the OpenCode API Service

The OpenCode service must use the same password as /etc/opencode-mobile-server/opencode.env. A reference service file is included:

sudo cp /opt/opencode-mobile-server/scripts/opencode-server.service /etc/systemd/system/opencode-server.service
sudo editor /etc/systemd/system/opencode-server.service
sudo systemctl daemon-reload
sudo systemctl enable --now opencode-server.service

Review the OpenCode path in ExecStart before installing the service. The OpenCode API should listen on 127.0.0.1:4096; do not expose port 4096 directly to the Internet.

Configure the Domain

Use scripts/nginx-opencode-mobile-server.conf.example and replace __DOMAIN__ with your actual domain. The template creates an HTTP reverse proxy to the local gateway. The initial URL is:

http://example.com/opencode/

Test and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

If the domain already has an Nginx server block, do not create a duplicate one. Copy only the two /opencode locations from the template into the existing block. For public access, configure HTTPS, port 443, and a certificate separately with Certbot or your existing certificate manager. Then use:

https://example.com/opencode/

Do not expose port 4096 or an unprotected port 8787 directly to the Internet.

Manual Run

Copy and edit .env, then run:

npm start

The default local URL is:

http://127.0.0.1:8787/opencode/

There are no runtime npm dependencies, so npm install is not required.

npm run start:lan is only an alias for npm start; it does not change the bind address. Set OPENCODE_WEB_HOST explicitly when LAN access is needed.

Configuration

Variable Default Description
OPENCODE_SERVER_PASSWORD opencode OpenCode Basic Auth password; change it
OPENCODE_API_URL http://127.0.0.1:4096 OpenCode server address
OPENCODE_WEB_HOST 127.0.0.1 Web gateway bind address; Windows uses 0.0.0.0
OPENCODE_WEB_PORT 8787 Web gateway port
OPENCODE_SERVICE_NAME opencode-server.service systemd service restarted after a key switch; set to none to disable
OPENCODE_AUTH_FILE auto-detected Path to OpenCode's auth.json (Linux: ~/.local/share/opencode/auth.json, macOS: ~/Library/Application Support/opencode/auth.json, Windows: %LOCALAPPDATA%\opencode\auth.json)
OPENCODE_AUTH_KEYS_FILE <project>/.auth-keys.json Local file where the gateway keeps all saved keys (put it under /etc/opencode-mobile-server/ on Linux servers)

The browser API path is intentionally fixed to /opencode/api, so the same frontend works on Windows LAN and behind a domain path without rebuilding.

Use absolute paths for OPENCODE_AUTH_FILE and OPENCODE_AUTH_KEYS_FILE when possible. On Linux, automatic auth-file detection honors XDG_DATA_HOME.

Current Client Features

The top bar can select the models, Agents, and reasoning variants returned by OpenCode. Each model name is followed by its input modalities (image, audio, video, document) and output modalities (e.g. "็”Ÿๆˆ๐Ÿ–ผ๏ธ" means it can generate images); models without extra modalities show "็บฏๆ–‡ๆœฌ". Modality info comes from the OpenCode provider configuration and may be absent with older OpenCode versions. The available variants depend on the model configuration and commonly include none, minimal, low, medium, high, max, and xhigh.

When the current model reports a context limit, the message area shows used tokens, the limit, and a percentage. The colors change at 70% and 90%. This is an estimate and does not replace OpenCode's own limit enforcement.

When an Agent sends an OpenCode question.asked event, the page shows a question panel with single-choice, multiple-choice, custom text, and skip actions. The task continues after an answer or skip. Questions from another session are not shown in the current session. The client also normalizes the question.v2.* and permission.v2.* event names used by newer OpenCode servers.

Messages stream through SSE. The page reconnects after a disconnect and polls session status and recent messages while visible as a fallback. The stop button calls OpenCode's abort endpoint.

Replies use lightweight Markdown: headings, bold, italics, strikethrough, inline code, code blocks, blockquotes, unordered lists, links, simple tables, and horizontal rules. It is not a full Markdown parser and does not provide syntax highlighting. Attachments are sent as Data URLs; the original files selected for one message may total at most 20 MB, and files or images can be dropped straight onto the input box; a message with only attachments can be sent directly. A reverse proxy should allow more than 20 MB for the encoded request; the included Nginx template allows 50 MB.

The page includes a PWA manifest but has no offline cache. The current browser stores the password, current session, model, Agent, and reasoning variant in local storage. Message history is primarily an in-memory cache and is loaded again from OpenCode after a refresh.

Managing API Keys

The "๐Ÿ”‘ Keys" panel in the drawer lets you save, switch, and delete provider (e.g. opencode-go) API keys from the browser โ€” no need to log into the server and run opencode auth login.

  • Save & switch: the new key is written to OpenCode's auth.json and becomes active. On Linux the gateway automatically restarts the service named by OPENCODE_SERVICE_NAME so the key takes effect within a few seconds, and the page reconnects. On Windows, or when auto-restart is unavailable, restart OpenCode manually and refresh the page. If you also run opencode in a server terminal, restart that session too.
  • Switch: toggle between any saved keys at any time.
  • Delete: only removes the key from the saved list; the active key is untouched.
  • Keys live in the file pointed to by OPENCODE_AUTH_KEYS_FILE (default .auth-keys.json in the project root, gitignored), never in the repository. Before overwriting auth.json a auth.json.bak backup is kept. The API returns only masked values and metadata, not raw keys.
  • On Linux, put OPENCODE_AUTH_KEYS_FILE under /etc/opencode-mobile-server/ and restrict it to the service user. Do not commit or publicly back up .auth-keys.json, auth.json, or auth.json.bak.

Note: the OpenCode server reads keys at startup and caches them, so a restart is required after a key change โ€” that is why the gateway restarts the service after save/switch.

Upgrading

Replace index.html, server.mjs, package.json, and the deployment files under scripts/ together. Keep the Windows project .env, and keep /etc/opencode-mobile-server/opencode.env plus the key files on Linux. Restart both services after replacing the files:

sudo systemctl restart opencode-server.service opencode-mobile-server.service

On Windows, close the old PowerShell gateway window and run scripts\deploy-windows.bat again, or run scripts\start-windows.ps1. If the browser still shows the old page, hard-refresh it. If model, Agent, or variant selection is inconsistent, clear oc_model, oc_agent, and oc_variants from browser local storage and reopen the page.

Troubleshooting

Repeated 401 responses or login screen

Check that the password in .env, /etc/opencode-mobile-server/opencode.env, and opencode serve is identical. Log out, or clear the site's oc_pwd local-storage value, and sign in again.

If the browser shows a Secure Area username/password dialog, an older gateway exposed OpenCode's WWW-Authenticate header and triggered the browser's native Basic Auth prompt. The current gateway hides that header and shows only the web page login form. Refresh the page after upgrading.

502 from /opencode/api

The Node gateway cannot reach OPENCODE_API_URL. Check that OpenCode is listening on port 4096 and that the address in .env is correct.

Page is not available at /opencode/

Use the complete path with the trailing slash. The gateway redirects / and /opencode to /opencode/.

Loading older messages returns 400

The current client uses the X-Next-Cursor cursor returned by OpenCode's paginated message API. After an upgrade, refresh the page so the browser does not keep using an old index.html.

Empty model list or missing AI questions

Confirm that the OpenCode version provides /config/providers, /agent, /question, /session/status, and the event API. Check the gateway log and hard-refresh after upgrading. If only one model lacks the reasoning selector, that model likely has no reasoning capability or variants configured.

Direct API access does not show a login page

/opencode/api/* is an API path, not the web login page. Requests must include Basic Auth with the gateway password. Open /opencode/ in a browser to use the page login form. Do not point OPENCODE_API_URL at an unauthenticated backend that is reachable from the Internet.

Security Recommendations

  • The default password is for first-run convenience only. Change it before Internet access.
  • Keep port 4096 bound to the loopback address on servers.
  • Bind the Linux gateway to 127.0.0.1 and use Nginx as the public entry point; when Windows uses 0.0.0.0, allow only trusted LAN access.
  • Enable HTTPS for any public deployment.
  • Do not commit .env, .auth-keys.json, OpenCode auth.json, API keys, or other credentials to GitHub. Commit only .env.example.
  • Do not expose port 8787 directly after using --skip-nginx; configure HTTPS and a reverse proxy first.

License

MIT License. See LICENSE.

About

OpenCode mobile web client and deployment gateway

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages