初始化web服务功能

This commit is contained in:
xintaofei
2026-03-25 14:26:26 +08:00
parent ae70f17d2e
commit ac09d3db9e
99 changed files with 3253 additions and 304 deletions

View File

@@ -28,7 +28,7 @@ import {
Trash2,
Wrench,
} from "lucide-react"
import { openUrl } from "@tauri-apps/plugin-opener"
import { openUrl } from "@/lib/platform"
import { toast } from "sonner"
import { AgentIcon } from "@/components/agent-icon"
import {
@@ -64,7 +64,7 @@ import {
acpReorderAgents,
acpUninstallAgent,
acpUpdateAgentPreferences,
} from "@/lib/tauri"
} from "@/lib/api"
import type {
AcpAgentInfo,
AgentType,

View File

@@ -13,7 +13,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { saveAccountToken } from "@/lib/tauri"
import { saveAccountToken } from "@/lib/api"
import type { GitHubAccount } from "@/lib/types"
interface AddGitAccountDialogProps {

View File

@@ -2,7 +2,7 @@
import { useCallback, useState } from "react"
import { ExternalLink, Eye, EyeOff, Loader2 } from "lucide-react"
import { openUrl } from "@tauri-apps/plugin-opener"
import { openUrl } from "@/lib/platform"
import { useTranslations } from "next-intl"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
@@ -14,7 +14,7 @@ import {
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { validateGitHubToken, saveAccountToken } from "@/lib/tauri"
import { validateGitHubToken, saveAccountToken } from "@/lib/api"
import type { GitHubAccount } from "@/lib/types"
interface AddGitHubAccountDialogProps {

View File

@@ -45,7 +45,7 @@ import {
mcpScanLocal,
mcpSearchMarketplace,
mcpUpsertLocalServer,
} from "@/lib/tauri"
} from "@/lib/api"
import { cn } from "@/lib/utils"
import type {
LocalMcpServer,

View File

@@ -5,6 +5,7 @@ import {
Bot,
BookOpenText,
GitBranch,
Globe,
Keyboard,
Palette,
PlugZap,
@@ -28,6 +29,7 @@ interface SettingsNavItem {
| "shortcuts"
| "version_control"
| "system"
| "web_service"
icon: ComponentType<{ className?: string }>
}
@@ -67,6 +69,11 @@ const SETTINGS_NAV_ITEMS: SettingsNavItem[] = [
labelKey: "system",
icon: Settings,
},
{
href: "/settings/web-service",
labelKey: "web_service",
icon: Globe,
},
]
interface SettingsShellProps {

View File

@@ -55,7 +55,7 @@ import {
openFolderWindow,
acpReadAgentSkill,
acpSaveAgentSkill,
} from "@/lib/tauri"
} from "@/lib/api"
import type {
AcpAgentInfo,
AgentSkillItem,

View File

@@ -9,7 +9,8 @@ import {
RefreshCw,
Wifi,
} from "lucide-react"
import type { Update } from "@tauri-apps/plugin-updater"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Update = any
import { useLocale, useTranslations } from "next-intl"
import { toast } from "sonner"
import { useAppI18n } from "@/components/i18n-provider"
@@ -26,7 +27,7 @@ import {
getSystemProxySettings,
updateSystemLanguageSettings,
updateSystemProxySettings,
} from "@/lib/tauri"
} from "@/lib/api"
import type { AppLocale } from "@/lib/types"
import {
checkAppUpdate,

View File

@@ -35,7 +35,7 @@ import {
validateGitHubToken,
getAccountToken,
deleteAccountToken,
} from "@/lib/tauri"
} from "@/lib/api"
import type {
GitDetectResult,
GitHubAccount,

View File

@@ -0,0 +1,179 @@
"use client"
import { useCallback, useEffect, useState } from "react"
import {
startWebServer,
stopWebServer,
getWebServerStatus,
type WebServerInfo,
} from "@/lib/api"
export function WebServiceSettings() {
const [status, setStatus] = useState<WebServerInfo | null>(null)
const [port, setPort] = useState("3080")
const [loading, setLoading] = useState(false)
const [error, setError] = useState("")
const fetchStatus = useCallback(async () => {
try {
const info = await getWebServerStatus()
setStatus(info)
if (info) {
setPort(String(info.port))
}
} catch {
// Server status unavailable
}
}, [])
useEffect(() => {
fetchStatus()
}, [fetchStatus])
async function handleStart() {
setError("")
setLoading(true)
try {
const info = await startWebServer({
port: parseInt(port, 10) || 3080,
})
setStatus(info)
} catch (e: unknown) {
const msg =
e && typeof e === "object" && "message" in e
? (e as { message: string }).message
: "启动失败"
setError(msg)
} finally {
setLoading(false)
}
}
async function handleStop() {
setLoading(true)
try {
await stopWebServer()
setStatus(null)
} catch {
setError("停止失败")
} finally {
setLoading(false)
}
}
function copyToken() {
if (status?.token) {
navigator.clipboard.writeText(status.token)
}
}
function copyUrl() {
if (status?.addresses?.[1]) {
navigator.clipboard.writeText(status.addresses[1])
} else if (status?.addresses?.[0]) {
navigator.clipboard.writeText(status.addresses[0])
}
}
const isRunning = status !== null
return (
<div className="space-y-6">
<div>
<h3 className="text-lg font-medium">Web </h3>
<p className="text-sm text-muted-foreground">
访 Codeg
</p>
</div>
<div className="space-y-4">
{/* Port config */}
<div className="flex items-center gap-4">
<label className="w-20 text-sm font-medium"></label>
<input
type="number"
value={port}
onChange={(e) => setPort(e.target.value)}
disabled={isRunning}
min={1024}
max={65535}
className="flex h-9 w-32 rounded-md border border-input bg-background px-3 py-1 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50"
/>
</div>
{/* Start/Stop button */}
<div className="flex items-center gap-4">
<label className="w-20 text-sm font-medium"></label>
<div className="flex items-center gap-3">
<span
className={`inline-block h-2 w-2 rounded-full ${
isRunning ? "bg-green-500" : "bg-muted-foreground/30"
}`}
/>
<span className="text-sm">
{isRunning ? "运行中" : "已停止"}
</span>
<button
onClick={isRunning ? handleStop : handleStart}
disabled={loading}
className="inline-flex h-8 items-center rounded-md border border-input bg-background px-3 text-xs font-medium ring-offset-background transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50"
>
{loading
? "处理中..."
: isRunning
? "停止"
: "启动"}
</button>
</div>
</div>
{error && (
<p className="text-sm text-destructive">{error}</p>
)}
{/* Connection info */}
{isRunning && (
<div className="rounded-md border p-4 space-y-3">
<div className="space-y-1">
<div className="text-xs font-medium text-muted-foreground">
访
</div>
{status.addresses.map((addr) => (
<div key={addr} className="flex items-center gap-2">
<code className="text-sm">{addr}</code>
</div>
))}
<button
onClick={copyUrl}
className="text-xs text-primary hover:underline"
>
</button>
</div>
<div className="space-y-1">
<div className="text-xs font-medium text-muted-foreground">
访 Token
</div>
<div className="flex items-center gap-2">
<code className="rounded bg-muted px-2 py-0.5 text-xs">
{status.token}
</code>
<button
onClick={copyToken}
className="text-xs text-primary hover:underline"
>
</button>
</div>
</div>
<p className="text-xs text-muted-foreground">
Web 访 Token
</p>
</div>
)}
</div>
</div>
)
}