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
|
|
@ -141,6 +141,14 @@ export class PersonaAgent {
|
|||
// ─── 内置工具注册 ───
|
||||
|
||||
private registerBuiltinTools(): void {
|
||||
// 读取 Git/Forgejo 状态;同步写操作必须由用户在界面中明确确认。
|
||||
this.tools.register({
|
||||
name: 'inspect_repository',
|
||||
description: '查看知识库本地 Git 与 Forgejo 远端的连接和同步状态(只读)',
|
||||
parameters: {},
|
||||
execute: async () => JSON.stringify(await this.git.getRepositoryStatus(), null, 2),
|
||||
});
|
||||
|
||||
// 读取知识库文档
|
||||
this.tools.register({
|
||||
name: 'read_document',
|
||||
|
|
@ -366,13 +374,10 @@ ${toolsList}
|
|||
private async callLLM(messages: Message[]): Promise<{ content: string; toolCalls?: ToolCall[] }> {
|
||||
const apiKey = process.env.OPENAI_API_KEY || process.env.HOLOLAKE_LLM_KEY || '';
|
||||
const baseUrl = process.env.HOLOLAKE_LLM_BASE || 'https://api.openai.com/v1';
|
||||
const model = this.definition.model || 'gpt-4o';
|
||||
const model = process.env.HOLOLAKE_LLM_MODEL || this.definition.model || 'gpt-4o';
|
||||
|
||||
if (!apiKey) {
|
||||
// 没有 API Key 时返回模拟回复(开发模式)
|
||||
return {
|
||||
content: `[${this.definition.name}] 收到你的消息。当前为离线模式,请配置 HOLOLAKE_LLM_KEY 环境变量启用 AI 能力。\n\n你说的: "${messages[messages.length - 1]?.content}"`,
|
||||
};
|
||||
throw new Error('Agent 尚未配置模型。请先在运行环境中设置 HOLOLAKE_LLM_KEY;当前不会伪装成已运行。');
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -384,11 +389,28 @@ ${toolsList}
|
|||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: messages.map(m => ({
|
||||
role: m.role,
|
||||
content: m.content,
|
||||
...(m.toolCalls ? { tool_calls: m.toolCalls } : {}),
|
||||
})),
|
||||
messages: messages.map(m => {
|
||||
if (m.role === 'assistant' && m.toolCalls?.length) {
|
||||
return {
|
||||
role: 'assistant',
|
||||
content: m.content || null,
|
||||
tool_calls: m.toolCalls.map(call => ({
|
||||
id: call.id,
|
||||
type: 'function',
|
||||
function: { name: call.name, arguments: JSON.stringify(call.arguments) },
|
||||
})),
|
||||
};
|
||||
}
|
||||
if (m.role === 'tool' && m.toolResults?.[0]) {
|
||||
return {
|
||||
role: 'tool',
|
||||
content: m.content,
|
||||
tool_call_id: m.toolResults[0].id,
|
||||
name: m.toolResults[0].name,
|
||||
};
|
||||
}
|
||||
return { role: m.role, content: m.content };
|
||||
}),
|
||||
tools: this.tools.toSchema(),
|
||||
temperature: this.definition.temperature,
|
||||
max_tokens: this.definition.maxTokens,
|
||||
|
|
@ -396,6 +418,9 @@ ${toolsList}
|
|||
});
|
||||
|
||||
const data = await res.json() as any;
|
||||
if (!res.ok) {
|
||||
throw new Error(data?.error?.message || `模型服务返回 HTTP ${res.status}`);
|
||||
}
|
||||
const choice = data.choices?.[0]?.message;
|
||||
|
||||
if (!choice) {
|
||||
|
|
@ -413,7 +438,7 @@ ${toolsList}
|
|||
toolCalls,
|
||||
};
|
||||
} catch (err: any) {
|
||||
return { content: `AI 调用失败: ${err.message}` };
|
||||
throw new Error(`AI 调用失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -428,7 +453,19 @@ ${toolsList}
|
|||
}
|
||||
|
||||
getDefinition(): PersonaDefinition {
|
||||
return { ...this.definition };
|
||||
return {
|
||||
...this.definition,
|
||||
model: process.env.HOLOLAKE_LLM_MODEL || this.definition.model,
|
||||
};
|
||||
}
|
||||
|
||||
getRuntimeStatus(): { configured: boolean; operational: boolean; toolNames: string[] } {
|
||||
const configured = Boolean(process.env.OPENAI_API_KEY || process.env.HOLOLAKE_LLM_KEY);
|
||||
return {
|
||||
configured,
|
||||
operational: configured,
|
||||
toolNames: this.tools.list().map(tool => tool.name),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -442,6 +479,7 @@ export function createDefaultPersona(git: GitEngine): PersonaAgent {
|
|||
role: '知识库管理者 — 负责文档的创建、整理、搜索和版本管理',
|
||||
systemPromptBase: '你是光湖知识库的人格体,一个活的知识管理 AI Agent。',
|
||||
tools: [
|
||||
'inspect_repository',
|
||||
'read_document',
|
||||
'create_document',
|
||||
'update_document',
|
||||
|
|
@ -458,4 +496,3 @@ export function createDefaultPersona(git: GitEngine): PersonaAgent {
|
|||
git
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue