Skip to content

Commit 4e82aef

Browse files
author
linyuan.yang
committed
整理 admin 代码
1 parent b55ced8 commit 4e82aef

32 files changed

Lines changed: 413 additions & 268 deletions

‎packages/apps/admin/src/components/SessionSelect.vue‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script setup lang="ts">
2-
import { computed, onMounted, ref } from 'vue'
2+
import { computed, onMounted } from 'vue'
33
import { SSelect } from '@sbot/ui'
4-
import { store } from '@/shared/store'
5-
import { cachedChannelSessions, loadChannelSessions, type ChannelSession } from '@/composables/useChannelSessions'
4+
import { channelManager, type ChannelSession } from '@/managers/channelManager'
65
76
const props = withDefaults(defineProps<{
87
/** 选中的频道会话 db id;null = 未选 / 自动。 */
@@ -24,23 +23,14 @@ const props = withDefaults(defineProps<{
2423
2524
const emit = defineEmits<{ 'update:modelValue': [value: number | null] }>()
2625
27-
const sessions = ref<ChannelSession[]>(cachedChannelSessions())
26+
const sessions = channelManager.list
2827
29-
onMounted(async () => {
30-
try {
31-
sessions.value = await loadChannelSessions()
32-
} catch {
33-
sessions.value = []
34-
}
28+
onMounted(() => {
29+
channelManager.ensure().catch(() => {})
3530
})
3631
3732
interface Group { channelName: string; sessions: ChannelSession[] }
3833
39-
function channelName(channelId: string): string {
40-
const channels = store.settings.channels as Record<string, { name?: string }> | undefined
41-
return channels?.[channelId]?.name || channelId
42-
}
43-
4434
function sessionText(s: ChannelSession): string {
4535
return s.sessionName || s.autoSessionName || s.sessionId || `#${s.id}`
4636
}
@@ -58,7 +48,7 @@ const groups = computed<Group[]>(() => {
5848
arr.push(s)
5949
map.set(s.channelId, arr)
6050
}
61-
return [...map.entries()].map(([channelId, arr]) => ({ channelName: channelName(channelId), sessions: arr }))
51+
return [...map.entries()].map(([channelId, arr]) => ({ channelName: channelManager.channelName(channelId), sessions: arr }))
6252
})
6353
6454
// 选中值不在可见分组里(会话被删/未绑定该 agenda)时补一条占位,避免值显示丢失。

‎packages/apps/admin/src/composables/useChannelSessions.ts‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎packages/apps/admin/src/composables/useResourceRefs.ts‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// 计算配置资源被频道、会话档案、会话及其他配置资源引用的情况,
22
// 供各资源页复刻 AgentsView 的「被引用情况」展示(见 ResourceRefs.vue)。
3-
import { ref } from 'vue'
4-
import { apiFetch } from '@/shared/api'
3+
import { computed } from 'vue'
54
import { store } from '@/shared/store'
5+
import { channelManager } from '@/managers/channelManager'
6+
import { profileManager } from '@/managers/profileManager'
67

78
/** 会话档案列表行(GET /api/session-profiles,仅 visible profile),仅取引用计算需要的字段。 */
89
interface ProfileLite {
@@ -77,20 +78,15 @@ export function parseList(raw: string | null | undefined): string[] {
7778
try { const v = JSON.parse(raw); return Array.isArray(v) ? v : [] } catch { return [] }
7879
}
7980

80-
// 模块级共享:多个页面实例共用同一份会话档案 / 会话列表。
81-
const profilesList = ref<ProfileLite[]>([])
82-
const sessionsList = ref<SessionLite[]>([])
81+
// 模块级共享:多个页面实例共用同一份会话档案 / 会话列表(来自全局 Manager,此处只收窄类型)。
82+
const profilesList = computed(() => profileManager.list.value as unknown as ProfileLite[])
83+
const sessionsList = computed(() => channelManager.list.value as unknown as SessionLite[])
8384

8485
export function useResourceRefs() {
8586
async function loadProfiles() {
8687
// visible profile 与会话各取所需字段:会话用于补全 auto profile 上的私有覆盖引用。
87-
const [profilesRes, sessionsRes] = await Promise.allSettled([
88-
apiFetch('/api/session-profiles'),
89-
apiFetch('/api/channel-sessions'),
90-
])
91-
// 引用信息为辅助展示,任一接口失败时静默降级为空。
92-
profilesList.value = profilesRes.status === 'fulfilled' ? (profilesRes.value.data || []) as ProfileLite[] : []
93-
sessionsList.value = sessionsRes.status === 'fulfilled' ? (sessionsRes.value.data || []) as SessionLite[] : []
88+
// 引用信息为辅助展示,任一接口失败时静默降级为空(manager 数据保持旧值/空数组)。
89+
await Promise.allSettled([profileManager.ensure(true), channelManager.ensure(true)])
9490
}
9591

9692
/** 用匹配器生成本页的 refs(id) 计算函数。 */

‎packages/apps/admin/src/layouts/Layout.vue‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { computed, ref, onMounted, onUnmounted, watch } from 'vue'
33
import { useRouter, useRoute } from 'vue-router'
44
import { useI18n } from 'vue-i18n'
55
import { apiFetch } from '@/shared/api'
6-
import { store, applyMcpList } from '@/shared/store'
6+
import { store } from '@/shared/store'
7+
import { mcpManager } from '@/managers/mcpManager'
8+
import { skillsManager } from '@/managers/skillsManager'
9+
import { settingsManager } from '@/managers/settingsManager'
710
import { useToast } from '@sbot/ui'
811
import { fetchLatestRelease, compareSemver, GITHUB_REPO_URL, GITHUB_RELEASES_URL, DOCS_URL, DOCS_ZH_URL } from '@sbot/shared'
912
import { useResponsive } from '../composables/useResponsive'
@@ -186,10 +189,7 @@ watch(activeKey, key => {
186189
async function reloadConfig() {
187190
try {
188191
await apiFetch('/api/reload', 'POST')
189-
const res = await apiFetch('/api/settings')
190-
Object.assign(store.settings, res.data)
191-
const mcpRes = await apiFetch('/api/mcp')
192-
applyMcpList(mcpRes.data || [])
192+
await Promise.all([settingsManager.refresh(), mcpManager.ensure(true)])
193193
show(t('nav.reload_success'))
194194
} catch (e: any) {
195195
show(e.message, 'error')
@@ -247,14 +247,13 @@ async function checkUpdate(currentVersion: string) {
247247
// Initial load
248248
async function init() {
249249
try {
250-
const [settingsRes, mcpRes, skillRes, aboutRes, sessionsRes] = await Promise.all([
251-
apiFetch('/api/settings'),
252-
apiFetch('/api/mcp'),
253-
apiFetch('/api/skills'),
250+
const [, , , aboutRes, sessionsRes] = await Promise.all([
251+
settingsManager.refresh(),
252+
mcpManager.ensure(),
253+
skillsManager.ensure(),
254254
apiFetch('/api/about'),
255255
apiFetch('/api/profiles'),
256256
])
257-
Object.assign(store.settings, settingsRes.data)
258257
// Convert sessions array to Record<id, SessionConfig>
259258
const sessionsArr: any[] = sessionsRes.data || []
260259
const sessionsMap: Record<string, any> = {}
@@ -263,9 +262,6 @@ async function init() {
263262
sessionsMap[id] = rest
264263
}
265264
store.sessions = sessionsMap
266-
applyMcpList(mcpRes.data || [])
267-
const allSkillsData = skillRes.data || []
268-
store.allSkills = allSkillsData
269265
if (aboutRes.data?.version) checkUpdate(aboutRes.data.version)
270266
} catch (_) {}
271267
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { computed } from 'vue'
2+
import { apiFetch } from '@/shared/api'
3+
import { store } from '@/shared/store'
4+
import { ListResourceManager } from '@/shared/resourceManager'
5+
6+
/** /api/channel-sessions 返回的会话行(仅取常用字段,其余宽松保留)。 */
7+
export interface ChannelSession {
8+
id: number
9+
channelId: string
10+
sessionId: string
11+
sessionName?: string | null
12+
autoSessionName?: string | null
13+
agenda?: string | null
14+
profileId?: number | string | null
15+
[key: string]: unknown
16+
}
17+
18+
/** 频道数据管理:频道会话列表(懒加载缓存)+ 频道显示名等便捷方法。 */
19+
class ChannelManager extends ListResourceManager<ChannelSession> {
20+
constructor() {
21+
super(async () => (await apiFetch('/api/channel-sessions')).data || [])
22+
}
23+
24+
/** 频道显示名:取 settings.channels 里的 name,缺省回退频道 id。 */
25+
channelName(channelId: string): string {
26+
const channels = store.settings.channels as Record<string, { name?: string }> | undefined
27+
return channels?.[channelId]?.name || channelId
28+
}
29+
30+
/** 按数据库 id 查找频道会话。 */
31+
session(id: number): ChannelSession | undefined {
32+
return this.list.value.find(s => s.id === id)
33+
}
34+
35+
/** profileId → 会话 的映射(一个 thread 即一个 profileId,供 saver thread 等关联场景)。 */
36+
readonly sessionByProfileId = computed(() => {
37+
const map = new Map<string, ChannelSession>()
38+
for (const s of this.list.value) {
39+
if (s.profileId != null) {
40+
const key = String(s.profileId)
41+
if (!map.has(key)) map.set(key, s)
42+
}
43+
}
44+
return map
45+
})
46+
}
47+
48+
export const channelManager = new ChannelManager()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { apiFetch } from '@/shared/api'
2+
import { ListResourceManager } from '@/shared/resourceManager'
3+
import type { McpItem } from '@/shared/types'
4+
5+
/** 全局 MCP 数据管理(懒加载缓存)。 */
6+
class McpManager extends ListResourceManager<McpItem> {
7+
constructor() {
8+
super(async () => (await apiFetch('/api/mcp')).data || [])
9+
}
10+
11+
/** 按 id 查找 MCP 配置。 */
12+
get(id: string): McpItem | undefined {
13+
return this.list.value.find(m => m.id === id)
14+
}
15+
16+
/** MCP 显示名(找不到时回退 id)。 */
17+
nameOf(id: string): string {
18+
return this.get(id)?.name || id
19+
}
20+
}
21+
22+
export const mcpManager = new McpManager()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { apiFetch } from '@/shared/api'
2+
import { ListResourceManager } from '@/shared/resourceManager'
3+
4+
/** /api/session-profiles 返回的会话档案行(仅 visible profile,字段按需宽松保留)。 */
5+
export interface SessionProfileItem {
6+
id: number
7+
name: string
8+
agentId?: string | null
9+
saver?: string | null
10+
intentModel?: string | null
11+
notes?: string | null // JSON 字符串
12+
wikis?: string | null // JSON 字符串
13+
memory?: string | null
14+
agenda?: string | null
15+
sessionCount?: number
16+
[key: string]: unknown
17+
}
18+
19+
/** 会话档案数据管理:可见 profile 列表(懒加载缓存)。 */
20+
class ProfileManager extends ListResourceManager<SessionProfileItem> {
21+
constructor() {
22+
super(async () => (await apiFetch('/api/session-profiles')).data || [])
23+
}
24+
}
25+
26+
export const profileManager = new ProfileManager()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { computed, reactive } from 'vue'
2+
import type { ComputedRef } from 'vue'
3+
import { apiFetch } from '@/shared/api'
4+
5+
/** /api/prompts/files 返回的文件行。 */
6+
export interface PromptFileItem {
7+
path: string
8+
isUserOnly?: boolean
9+
[key: string]: unknown
10+
}
11+
12+
/** prompts 文件列表管理:按 prefix 懒加载缓存(heartbeat / memory / agenda/sync …)。 */
13+
class PromptFileManager {
14+
private readonly filesMap = reactive<Record<string, PromptFileItem[]>>({})
15+
private inflight = new Map<string, Promise<PromptFileItem[]>>()
16+
17+
/** prefix 下的文件列表(响应式视图,未加载时为空数组)。 */
18+
list(prefix: string): ComputedRef<PromptFileItem[]> {
19+
return computed(() => this.filesMap[prefix] || [])
20+
}
21+
22+
/** 加载某 prefix 的文件列表(带缓存)。force=true 时强制刷新。 */
23+
async ensure(prefix: string, force = false): Promise<PromptFileItem[]> {
24+
if (!force && prefix in this.filesMap) return this.filesMap[prefix]
25+
if (!this.inflight.has(prefix)) {
26+
const p = apiFetch(`/api/prompts/files?prefix=${encodeURIComponent(prefix)}`)
27+
.then(res => { this.filesMap[prefix] = res.data || []; return this.filesMap[prefix] })
28+
.finally(() => { this.inflight.delete(prefix) })
29+
this.inflight.set(prefix, p)
30+
}
31+
return this.inflight.get(prefix)!
32+
}
33+
}
34+
35+
export const promptFileManager = new PromptFileManager()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { reactive } from 'vue'
2+
import { apiFetch } from '@/shared/api'
3+
4+
/** Saver 数据管理:各 saver 的 thread 列表懒加载缓存 + 历史清理(SaversView / ChannelsView 共用)。 */
5+
class SaverManager {
6+
/** saverId → thread 列表(加载完成后填充;reset 后重新加载)。 */
7+
readonly threadsMap = reactive<Record<string, string[]>>({})
8+
readonly loadingMap = reactive<Record<string, boolean>>({})
9+
private inflight = new Map<string, Promise<string[]>>()
10+
11+
/** 加载某个 saver 的 thread 列表(带缓存)。force=true 时强制刷新。 */
12+
async loadThreads(saverId: string, force = false): Promise<string[]> {
13+
if (!force && saverId in this.threadsMap) return this.threadsMap[saverId]
14+
if (!this.inflight.has(saverId)) {
15+
this.loadingMap[saverId] = true
16+
const p = apiFetch(`/api/savers/${encodeURIComponent(saverId)}/threads`)
17+
.then(res => { this.threadsMap[saverId] = res.data || []; return this.threadsMap[saverId] })
18+
.finally(() => { this.inflight.delete(saverId); this.loadingMap[saverId] = false })
19+
this.inflight.set(saverId, p)
20+
}
21+
return this.inflight.get(saverId)!
22+
}
23+
24+
/** 清空某个 thread 的历史;成功后同步移除 thread 缓存。 */
25+
async clearHistory(saverId: string, thread: string): Promise<void> {
26+
await apiFetch(
27+
`/api/savers/${encodeURIComponent(saverId)}/threads/${encodeURIComponent(thread)}/history`,
28+
'DELETE',
29+
)
30+
const list = this.threadsMap[saverId]
31+
if (list) this.threadsMap[saverId] = list.filter(t => t !== thread)
32+
}
33+
34+
/** 丢弃某个 saver 的 thread 缓存(配置刷新后重新加载)。 */
35+
reset(saverId: string) {
36+
delete this.threadsMap[saverId]
37+
}
38+
}
39+
40+
export const saverManager = new SaverManager()
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { apiFetch } from '@/shared/api'
2+
import { store } from '@/shared/store'
3+
import type { Settings } from '@/shared/types'
4+
5+
/** 全局 settings 数据管理:统一「重新拉取」与「保存后合并」两个入口。 */
6+
class SettingsManager {
7+
private inflight: Promise<void> | null = null
8+
9+
/** 重新拉取全量 settings 并合并进全局 store(并发调用共享同一次请求)。 */
10+
async refresh(): Promise<void> {
11+
if (!this.inflight) {
12+
this.inflight = apiFetch('/api/settings')
13+
.then(res => { this.apply(res.data); this.inflight = null })
14+
.catch(e => { this.inflight = null; throw e })
15+
}
16+
return this.inflight
17+
}
18+
19+
/** 用保存接口返回的 settings 增量合并进全局 store(不发请求)。 */
20+
apply(data: Settings | undefined) {
21+
if (data) Object.assign(store.settings, data)
22+
}
23+
}
24+
25+
export const settingsManager = new SettingsManager()

0 commit comments

Comments
 (0)