fix: complete HoloLake Agent and Forgejo desktop runtime
This commit is contained in:
parent
28f68fa9ac
commit
46bdd0ca73
15 changed files with 1217 additions and 638 deletions
|
|
@ -9,6 +9,7 @@ import AgentChat from './components/AgentChat';
|
|||
type View = 'editor' | 'history';
|
||||
|
||||
export default function App() {
|
||||
const agentApiBase = window.location.protocol === 'file:' ? 'http://127.0.0.1:3890' : '';
|
||||
const [tree, setTree] = useState<DocTreeNode[]>([]);
|
||||
const [currentDoc, setCurrentDoc] = useState<DocContent | null>(null);
|
||||
const [currentPath, setCurrentPath] = useState<string>('');
|
||||
|
|
@ -181,7 +182,7 @@ export default function App() {
|
|||
{/* Agent 面板 */}
|
||||
{agentPanelOpen && (
|
||||
<aside className="kb-agent-panel">
|
||||
<AgentChat apiBase="" onDocSelect={openDoc} />
|
||||
<AgentChat apiBase={agentApiBase} onDocSelect={openDoc} />
|
||||
</aside>
|
||||
)}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
* Agent 和前端 UI 共用同一套接口。
|
||||
*/
|
||||
|
||||
const BASE = '/api';
|
||||
const BASE = typeof window !== 'undefined' && window.location.protocol === 'file:'
|
||||
? 'http://127.0.0.1:3890/api'
|
||||
: '/api';
|
||||
|
||||
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
||||
const res = await fetch(`${BASE}${path}`, {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,18 @@ interface AgentStatus {
|
|||
role: string;
|
||||
model: string;
|
||||
conversationLength: number;
|
||||
configured: boolean;
|
||||
operational: boolean;
|
||||
tools: string[];
|
||||
}
|
||||
|
||||
interface RepositoryStatus {
|
||||
branch: string;
|
||||
head: string;
|
||||
clean: boolean;
|
||||
ahead: number;
|
||||
behind: number;
|
||||
remote: { name: string; url: string } | null;
|
||||
}
|
||||
|
||||
interface Props {
|
||||
|
|
@ -33,11 +45,22 @@ export default function AgentChat({ apiBase, onDocSelect }: Props) {
|
|||
const [input, setInput] = useState('');
|
||||
const [sending, setSending] = useState(false);
|
||||
const [status, setStatus] = useState<AgentStatus | null>(null);
|
||||
const [repoStatus, setRepoStatus] = useState<RepositoryStatus | null>(null);
|
||||
const [remoteUrl, setRemoteUrl] = useState('');
|
||||
const [syncMessage, setSyncMessage] = useState('');
|
||||
const [syncing, setSyncing] = useState(false);
|
||||
const [configOpen, setConfigOpen] = useState(false);
|
||||
const [modelBaseUrl, setModelBaseUrl] = useState('https://api.openai.com/v1');
|
||||
const [modelName, setModelName] = useState('gpt-4o');
|
||||
const [modelKey, setModelKey] = useState('');
|
||||
const [configMessage, setConfigMessage] = useState('');
|
||||
const bottomRef = useRef<HTMLDivElement>(null);
|
||||
const inputRef = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
fetchStatus();
|
||||
fetchRepositoryStatus();
|
||||
loadModelConfig();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -54,11 +77,74 @@ export default function AgentChat({ apiBase, onDocSelect }: Props) {
|
|||
role: data.persona.role,
|
||||
model: data.persona.model,
|
||||
conversationLength: data.conversationLength,
|
||||
configured: data.configured,
|
||||
operational: data.operational,
|
||||
tools: data.tools || [],
|
||||
});
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
async function loadModelConfig() {
|
||||
const bridge = (window as any).hololake?.agent;
|
||||
if (!bridge) return;
|
||||
try {
|
||||
const config = await bridge.getConfig();
|
||||
setModelBaseUrl(config.baseUrl || 'https://api.openai.com/v1');
|
||||
setModelName(config.model || 'gpt-4o');
|
||||
} catch {}
|
||||
}
|
||||
|
||||
async function saveModelConfig() {
|
||||
const bridge = (window as any).hololake?.agent;
|
||||
if (!bridge) {
|
||||
setConfigMessage('模型安全配置只在桌面 App 中提供');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await bridge.saveConfig({ baseUrl: modelBaseUrl, model: modelName, apiKey: modelKey || undefined });
|
||||
setModelKey('');
|
||||
setConfigMessage('已保存到 macOS 加密存储');
|
||||
await fetchStatus();
|
||||
} catch (err: any) {
|
||||
setConfigMessage(err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchRepositoryStatus() {
|
||||
try {
|
||||
const res = await fetch(`${apiBase}/api/forgejo/status`);
|
||||
const data = await res.json();
|
||||
if (data.ok) {
|
||||
setRepoStatus(data.status);
|
||||
if (data.status.remote?.url) setRemoteUrl(data.status.remote.url);
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
async function runForgejoAction(action: 'configure' | 'fetch' | 'pull' | 'push') {
|
||||
if (syncing) return;
|
||||
if (action === 'push' && !confirm('确认把当前知识库提交推送到已配置的 Forgejo 仓库?')) return;
|
||||
setSyncing(true);
|
||||
setSyncMessage('');
|
||||
try {
|
||||
const endpoint = action === 'configure' ? 'remote' : action;
|
||||
const res = await fetch(`${apiBase}/api/forgejo/${endpoint}`, {
|
||||
method: action === 'configure' ? 'PUT' : 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(action === 'configure' ? { url: remoteUrl } : { confirm: action === 'push' }),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!data.ok) throw new Error(data.error || '操作失败');
|
||||
setRepoStatus(data.status);
|
||||
setSyncMessage(action === 'configure' ? 'Forgejo 已连接' : `${action} 已完成`);
|
||||
} catch (err: any) {
|
||||
setSyncMessage(err.message);
|
||||
} finally {
|
||||
setSyncing(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const text = input.trim();
|
||||
if (!text || sending) return;
|
||||
|
|
@ -135,19 +221,61 @@ export default function AgentChat({ apiBase, onDocSelect }: Props) {
|
|||
</div>
|
||||
<div className="agent-actions">
|
||||
<span className="agent-model">{status?.model || 'offline'}</span>
|
||||
<button className="btn-config" onClick={() => setConfigOpen(!configOpen)} title="配置模型">⚙</button>
|
||||
<button className="btn-clear" onClick={clearConversation} title="清空对话">
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="runtime-status">
|
||||
<div className="runtime-row">
|
||||
<span className={`runtime-dot ${status?.operational ? 'online' : 'waiting'}`} />
|
||||
<span>{status?.operational ? 'Agent 已接入模型' : 'Agent 等待模型配置'}</span>
|
||||
<span className="runtime-tools">{status?.tools?.length || 0} 个工具</span>
|
||||
</div>
|
||||
{configOpen && (
|
||||
<div className="model-config">
|
||||
<input value={modelBaseUrl} onChange={e => setModelBaseUrl(e.target.value)} placeholder="模型服务地址" />
|
||||
<input value={modelName} onChange={e => setModelName(e.target.value)} placeholder="模型名称" />
|
||||
<input type="password" value={modelKey} onChange={e => setModelKey(e.target.value)} placeholder={status?.configured ? '留空则保留现有密钥' : '模型密钥'} />
|
||||
<button onClick={saveModelConfig}>安全保存</button>
|
||||
{configMessage && <div className="forgejo-message">{configMessage}</div>}
|
||||
</div>
|
||||
)}
|
||||
<div className="forgejo-status">
|
||||
<div className="forgejo-title">
|
||||
<strong>Forgejo 代码引擎</strong>
|
||||
<span>{repoStatus?.remote ? `${repoStatus.branch} · ${repoStatus.head.slice(0, 7)}` : '尚未连接远端'}</span>
|
||||
</div>
|
||||
<input
|
||||
className="forgejo-url"
|
||||
value={remoteUrl}
|
||||
onChange={e => setRemoteUrl(e.target.value)}
|
||||
placeholder="HTTPS 或 SSH Forgejo 仓库地址"
|
||||
/>
|
||||
<div className="forgejo-actions">
|
||||
<button onClick={() => runForgejoAction('configure')} disabled={syncing || !remoteUrl.trim()}>连接</button>
|
||||
<button onClick={() => runForgejoAction('fetch')} disabled={syncing || !repoStatus?.remote}>检查</button>
|
||||
<button onClick={() => runForgejoAction('pull')} disabled={syncing || !repoStatus?.remote}>拉取</button>
|
||||
<button onClick={() => runForgejoAction('push')} disabled={syncing || !repoStatus?.remote}>确认推送</button>
|
||||
</div>
|
||||
{repoStatus?.remote && (
|
||||
<div className="forgejo-detail">
|
||||
{repoStatus.clean ? '本地已提交' : '本地有未提交内容'} · 领先 {repoStatus.ahead} / 落后 {repoStatus.behind}
|
||||
</div>
|
||||
)}
|
||||
{syncMessage && <div className="forgejo-message">{syncMessage}</div>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 对话区域 */}
|
||||
<div className="agent-messages">
|
||||
{messages.length === 0 && (
|
||||
<div className="agent-welcome">
|
||||
<div className="welcome-icon">🌊</div>
|
||||
<h2>光湖人格体</h2>
|
||||
<p>我是知识库的 AI Agent,可以直接操作 Git 引擎管理你的文档。</p>
|
||||
<p>{status?.operational ? '我是知识库的 AI Agent,可以调用工具管理文档。' : '知识库功能已运行;Agent 需要配置模型后才会回应,不会用模拟回复冒充。'}</p>
|
||||
<p className="welcome-hint">试试说:</p>
|
||||
<div className="welcome-suggestions">
|
||||
<button onClick={() => { setInput('帮我列出所有文档'); inputRef.current?.focus(); }}>
|
||||
|
|
@ -232,18 +360,18 @@ export default function AgentChat({ apiBase, onDocSelect }: Props) {
|
|||
onChange={e => setInput(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
rows={1}
|
||||
disabled={sending}
|
||||
disabled={sending || !status?.operational}
|
||||
/>
|
||||
<button
|
||||
className="btn-send"
|
||||
onClick={sendMessage}
|
||||
disabled={sending || !input.trim()}
|
||||
disabled={sending || !input.trim() || !status?.operational}
|
||||
>
|
||||
{sending ? '⏳' : '↑'}
|
||||
</button>
|
||||
</div>
|
||||
<div className="agent-hint">
|
||||
Enter 发送 · Shift+Enter 换行 · Agent 可直接操作知识库
|
||||
{status?.operational ? 'Enter 发送 · 写操作由本地 Git 留痕 · Forgejo 推送需确认' : 'Agent 尚未接入模型;知识库与 Forgejo 功能仍可独立使用'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ body {
|
|||
}
|
||||
|
||||
.kb-header {
|
||||
-webkit-app-region: drag;
|
||||
height: var(--kb-header-height);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
@ -55,6 +56,12 @@ body {
|
|||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.kb-header button,
|
||||
.kb-header input,
|
||||
.kb-header a {
|
||||
-webkit-app-region: no-drag;
|
||||
}
|
||||
|
||||
.kb-header-left {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
@ -838,7 +845,8 @@ body {
|
|||
font-family: var(--kb-font-mono);
|
||||
}
|
||||
|
||||
.btn-clear {
|
||||
.btn-clear,
|
||||
.btn-config {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--kb-text-muted);
|
||||
|
|
@ -849,11 +857,108 @@ body {
|
|||
transition: all 0.15s;
|
||||
}
|
||||
|
||||
.btn-clear:hover {
|
||||
.btn-clear:hover,
|
||||
.btn-config:hover {
|
||||
background: var(--kb-bg-tertiary);
|
||||
color: var(--kb-danger);
|
||||
}
|
||||
|
||||
.model-config {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 92px;
|
||||
gap: 6px;
|
||||
margin-top: 9px;
|
||||
padding: 9px;
|
||||
border: 1px solid var(--kb-border);
|
||||
border-radius: 8px;
|
||||
background: var(--kb-bg);
|
||||
}
|
||||
|
||||
.model-config input {
|
||||
min-width: 0;
|
||||
border: 1px solid var(--kb-border);
|
||||
border-radius: 5px;
|
||||
padding: 6px 7px;
|
||||
background: var(--kb-bg-secondary);
|
||||
color: var(--kb-text);
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.model-config input:first-child,
|
||||
.model-config input[type="password"],
|
||||
.model-config .forgejo-message { grid-column: 1 / -1; }
|
||||
|
||||
.model-config button {
|
||||
border: 1px solid var(--kb-accent);
|
||||
border-radius: 5px;
|
||||
background: rgba(88, 166, 255, .12);
|
||||
color: var(--kb-text);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.runtime-status {
|
||||
padding: 10px 14px;
|
||||
border-bottom: 1px solid var(--kb-border);
|
||||
background: var(--kb-bg-secondary);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.runtime-row,
|
||||
.forgejo-title,
|
||||
.forgejo-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
}
|
||||
|
||||
.runtime-dot {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.runtime-dot.online { background: #3fb950; box-shadow: 0 0 7px rgba(63, 185, 80, .55); }
|
||||
.runtime-dot.waiting { background: #d29922; }
|
||||
.runtime-tools { margin-left: auto; color: var(--kb-text-muted); }
|
||||
|
||||
.forgejo-status {
|
||||
margin-top: 9px;
|
||||
padding: 9px;
|
||||
border: 1px solid var(--kb-border);
|
||||
border-radius: 8px;
|
||||
background: var(--kb-bg);
|
||||
}
|
||||
|
||||
.forgejo-title { justify-content: space-between; margin-bottom: 7px; }
|
||||
.forgejo-title span,
|
||||
.forgejo-detail { color: var(--kb-text-muted); }
|
||||
|
||||
.forgejo-url {
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
border: 1px solid var(--kb-border);
|
||||
border-radius: 6px;
|
||||
padding: 7px 8px;
|
||||
background: var(--kb-bg-secondary);
|
||||
color: var(--kb-text);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.forgejo-actions { margin-top: 7px; }
|
||||
.forgejo-actions button {
|
||||
flex: 1;
|
||||
border: 1px solid var(--kb-border);
|
||||
border-radius: 5px;
|
||||
padding: 5px 3px;
|
||||
background: var(--kb-bg-tertiary);
|
||||
color: var(--kb-text);
|
||||
cursor: pointer;
|
||||
font-size: 10px;
|
||||
}
|
||||
.forgejo-actions button:disabled { opacity: .4; cursor: not-allowed; }
|
||||
.forgejo-detail { margin-top: 6px; }
|
||||
.forgejo-message { margin-top: 5px; color: var(--kb-accent); word-break: break-word; }
|
||||
|
||||
/* ─── 对话区域 ─── */
|
||||
|
||||
.agent-messages {
|
||||
|
|
|
|||
Loading…
Reference in a new issue