Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 44496ef2
authored
May 27, 2026
by
孙奇
Committed by
胡悦
Jul 06, 2026
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat(agent_hub): 从数据库同步 agent.code 值并优化配置加载逻辑
1 parent
84f4a7ed
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
39 deletions
workspace/agent-admin/backend/app/config/loader.py
workspace/agent-admin/backend/app/config/settings.py
workspace/agent-admin/backend/app/main.py
workspace/agent-admin/backend/app/service/agent_hub.py
workspace/agent-admin/backend/app/config/loader.py
View file @
44496ef
...
@@ -6,7 +6,9 @@ from typing import Dict, Optional
...
@@ -6,7 +6,9 @@ from typing import Dict, Optional
from
pydantic
import
ValidationError
from
pydantic
import
ValidationError
from
app.config.settings
import
AgentConfig
,
ModelConfig
,
Settings
from
app.config.settings
import
AGENT_CODE_SYNC_MAP
,
AgentConfig
,
ModelConfig
,
Settings
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
...
@@ -67,6 +69,52 @@ class ConfigLoader:
...
@@ -67,6 +69,52 @@ class ConfigLoader:
return
result
return
result
def
sync_agent_codes_from_db
(
self
,
agents
:
Dict
[
str
,
AgentConfig
],
db_session
)
->
None
:
"""从 official_site_agent_display 表同步 agent.code,失败时保留 JSON 默认值。
根据 AGENT_CODE_SYNC_MAP 中的映射,查询 DB 中 agent_name 对应的 agent_id,
然后更新 AgentConfig.code = str(agent_id)。
"""
try
:
from
app.model.official_site_agent_display
import
OfficialSiteAgentDisplay
target_names
=
list
(
AGENT_CODE_SYNC_MAP
.
keys
())
rows
=
(
db_session
.
query
(
OfficialSiteAgentDisplay
.
agent_name
,
OfficialSiteAgentDisplay
.
agent_id
,
)
.
filter
(
OfficialSiteAgentDisplay
.
agent_name
.
in_
(
target_names
))
.
all
()
)
name_to_id
=
{
row
.
agent_name
:
row
.
agent_id
for
row
in
rows
}
for
db_name
,
internal_key
in
AGENT_CODE_SYNC_MAP
.
items
():
if
internal_key
not
in
agents
:
continue
agent_id
=
name_to_id
.
get
(
db_name
)
if
agent_id
is
not
None
:
old_code
=
agents
[
internal_key
]
.
code
agents
[
internal_key
]
.
code
=
str
(
agent_id
)
logger
.
info
(
"Agent code 已从 DB 同步:
%
s.code
%
s →
%
s"
,
internal_key
,
old_code
,
agent_id
,
)
else
:
logger
.
warning
(
"DB 中未找到 agent_name='
%
s',保留 JSON 默认值 code='
%
s'"
,
db_name
,
agents
[
internal_key
]
.
code
,
)
except
Exception
:
logger
.
warning
(
"从 DB 同步 agent code 失败,使用 JSON 默认值"
,
exc_info
=
True
,
)
def
load_skills_from_toolkit
(
self
,
toolkit
)
->
Dict
[
str
,
dict
]:
def
load_skills_from_toolkit
(
self
,
toolkit
)
->
Dict
[
str
,
dict
]:
skills
=
{}
skills
=
{}
for
name
,
skill
in
toolkit
.
skills
.
items
():
for
name
,
skill
in
toolkit
.
skills
.
items
():
...
...
workspace/agent-admin/backend/app/config/settings.py
View file @
44496ef
from
pydantic
import
BaseModel
from
pydantic
import
BaseModel
from
typing
import
List
from
typing
import
Dict
,
List
# official_site_agent_display.agent_name → 内部 agent dict key
# 用于从 DB 同步 agent.code 值
AGENT_CODE_SYNC_MAP
:
Dict
[
str
,
str
]
=
{
"管家 Agent"
:
"main"
,
"会议Agent"
:
"meeting"
,
}
class
ModelConfig
(
BaseModel
):
class
ModelConfig
(
BaseModel
):
...
...
workspace/agent-admin/backend/app/main.py
View file @
44496ef
...
@@ -6,8 +6,6 @@ from fastapi import FastAPI, HTTPException, Request
...
@@ -6,8 +6,6 @@ from fastapi import FastAPI, HTTPException, Request
from
fastapi.exceptions
import
RequestValidationError
from
fastapi.exceptions
import
RequestValidationError
from
fastapi.responses
import
JSONResponse
,
Response
from
fastapi.responses
import
JSONResponse
,
Response
logger
=
logging
.
getLogger
(
__name__
)
from
app.api.v1.router
import
api_router
from
app.api.v1.router
import
api_router
from
app.config.loader
import
ConfigLoader
from
app.config.loader
import
ConfigLoader
from
app.infrastructure.database
import
engine
,
SessionLocal
from
app.infrastructure.database
import
engine
,
SessionLocal
...
@@ -40,13 +38,12 @@ from app.model.wecom_sync import ( # noqa: F401
...
@@ -40,13 +38,12 @@ from app.model.wecom_sync import ( # noqa: F401
WeComSyncCheckpoint
,
WeComSyncCheckpoint
,
)
)
from
app.schema.common
import
error_response_payload
,
is_wrapped_payload
,
success_response_payload
from
app.schema.common
import
error_response_payload
,
is_wrapped_payload
,
success_response_payload
from
app.
agent_cor
e.agent_hub
import
AgentHub
from
app.
servic
e.agent_hub
import
AgentHub
from
app.
agent_cor
e.active_session_service
import
ActiveSessionService
from
app.
servic
e.active_session_service
import
ActiveSessionService
from
app.
agent_cor
e.console_im_service
import
ConsoleIMService
from
app.
servic
e.console_im_service
import
ConsoleIMService
from
app.
agent_cor
e.im_service
import
IMService
from
app.
servic
e.im_service
import
IMService
agent_hub_instance
=
None
agent_hub_instance
=
None
message_gate_instance
=
None
configure_logging
(
"backend"
,
logging
.
INFO
)
configure_logging
(
"backend"
,
logging
.
INFO
)
runtime_settings
=
ConfigLoader
()
.
load_settings
()
runtime_settings
=
ConfigLoader
()
.
load_settings
()
base_path
=
(
runtime_settings
.
server
.
base_path
or
""
)
.
rstrip
(
"/"
)
base_path
=
(
runtime_settings
.
server
.
base_path
or
""
)
.
rstrip
(
"/"
)
...
@@ -58,25 +55,20 @@ oauth2_redirect_url = f"{base_path}/docs/oauth2-redirect" if base_path else "/do
...
@@ -58,25 +55,20 @@ oauth2_redirect_url = f"{base_path}/docs/oauth2-redirect" if base_path else "/do
@asynccontextmanager
@asynccontextmanager
async
def
lifespan
(
app
:
FastAPI
):
async
def
lifespan
(
app
:
FastAPI
):
global
agent_hub_instance
,
message_gate_instance
global
agent_hub_instance
Base
.
metadata
.
create_all
(
bind
=
engine
)
Base
.
metadata
.
create_all
(
bind
=
engine
)
ensure_runtime_schema
(
engine
)
ensure_runtime_schema
(
engine
)
config_loader
=
ConfigLoader
()
config_loader
=
ConfigLoader
()
settings
=
config_loader
.
load_settings
()
settings
=
config_loader
.
load_settings
()
db_factory
=
SessionLocal
db
=
SessionLocal
()
# 从数据库加载 Agent 配置
with
db_factory
()
as
db
:
agents
=
config_loader
.
load_agents_from_db
(
db
)
logger
.
info
(
"Agent 配置已从 DB 加载:
%
d 个, steward=
%
s"
,
len
(
agents
),
next
((
k
for
k
,
v
in
agents
.
items
()
if
v
.
is_steward
),
None
),
)
session_service
=
ActiveSessionService
(
db_factory
=
db_factory
)
# 加载 Agent 配置并从 DB 同步 code 值
agents
=
config_loader
.
load_agents
()
config_loader
.
sync_agent_codes_from_db
(
agents
,
db
)
session_service
=
ActiveSessionService
(
db
=
db
)
use_console_im
=
os
.
getenv
(
"USE_CONSOLE_IM"
)
use_console_im
=
os
.
getenv
(
"USE_CONSOLE_IM"
)
if
use_console_im
is
None
:
if
use_console_im
is
None
:
...
@@ -97,30 +89,13 @@ async def lifespan(app: FastAPI):
...
@@ -97,30 +89,13 @@ async def lifespan(app: FastAPI):
session_service
=
session_service
,
session_service
=
session_service
,
im_service
=
im_service
,
im_service
=
im_service
,
preloaded_agents
=
agents
,
preloaded_agents
=
agents
,
db_factory
=
db_factory
,
)
# ── MessageGate 实例化 ──
from
app.agent_core.message_gate
import
MessageGate
from
app.hooks.post_reply
import
PostReplyHook
main_agent_key
=
next
((
k
for
k
,
v
in
agents
.
items
()
if
v
.
is_steward
),
"main"
)
post_reply_hook
=
PostReplyHook
(
im_service
=
im_service
)
message_gate_instance
=
MessageGate
(
config_loader
=
config_loader
,
session_service
=
session_service
,
post_reply_hook
=
post_reply_hook
,
agent_hub
=
agent_hub_instance
,
main_agent_key
=
main_agent_key
,
)
)
yield
yield
if
agent_hub_instance
:
if
agent_hub_instance
:
await
agent_hub_instance
.
close
()
await
agent_hub_instance
.
close
()
if
message_gate_instance
:
db
.
close
()
await
message_gate_instance
.
close
()
# db_factory 是 class,不需要 close;短 session 由 with 自动管理
app
=
FastAPI
(
app
=
FastAPI
(
...
...
workspace/agent-admin/backend/app/service/agent_hub.py
0 → 100644
View file @
44496ef
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment