mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-01 17:59:09 +08:00
refactor(types): rename Agent type to AssistantPreset for clarity
The type was renamed to better reflect its purpose as a preset configuration for assistants rather than representing an active agent. This change improves code readability and maintainability by using more accurate terminology throughout the codebase.
This commit is contained in:
parent
ee82b23886
commit
6b0a1a42ad
@ -5,7 +5,7 @@ import { useTimer } from '@renderer/hooks/useTimer'
|
||||
import { useSystemAgents } from '@renderer/pages/agents'
|
||||
import { createAssistantFromAgent } from '@renderer/services/AssistantService'
|
||||
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
|
||||
import { Agent, Assistant } from '@renderer/types'
|
||||
import { Assistant, AssistantPreset } from '@renderer/types'
|
||||
import { uuid } from '@renderer/utils'
|
||||
import { Divider, Input, InputRef, Modal, Tag } from 'antd'
|
||||
import { take } from 'lodash'
|
||||
@ -37,7 +37,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
const { setTimeoutTimer } = useTimer()
|
||||
|
||||
const agents = useMemo(() => {
|
||||
const allAgents = [...userAgents, ...systemAgents] as Agent[]
|
||||
const allAgents = [...userAgents, ...systemAgents] as AssistantPreset[]
|
||||
const list = [defaultAssistant, ...allAgents.filter((agent) => !assistants.map((a) => a.id).includes(agent.id))]
|
||||
const filtered = searchText
|
||||
? list.filter(
|
||||
@ -48,7 +48,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
: list
|
||||
|
||||
if (searchText.trim()) {
|
||||
const newAgent: Agent = {
|
||||
const newAgent: AssistantPreset = {
|
||||
id: 'new',
|
||||
name: searchText.trim(),
|
||||
prompt: '',
|
||||
@ -67,7 +67,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
}, [agents.length, searchText])
|
||||
|
||||
const onCreateAssistant = useCallback(
|
||||
async (agent: Agent) => {
|
||||
async (agent: AssistantPreset) => {
|
||||
if (loadingRef.current) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { useAppDispatch, useAppSelector } from '@renderer/store'
|
||||
import { addAgent, removeAgent, updateAgent, updateAgents, updateAgentSettings } from '@renderer/store/agents'
|
||||
import { Agent, AssistantSettings } from '@renderer/types'
|
||||
import { AssistantPreset, AssistantSettings } from '@renderer/types'
|
||||
|
||||
export function useAgents() {
|
||||
const agents = useAppSelector((state) => state.agents.agents)
|
||||
@ -8,19 +8,19 @@ export function useAgents() {
|
||||
|
||||
return {
|
||||
agents,
|
||||
updateAgents: (agents: Agent[]) => dispatch(updateAgents(agents)),
|
||||
addAgent: (agent: Agent) => dispatch(addAgent(agent)),
|
||||
updateAgents: (agents: AssistantPreset[]) => dispatch(updateAgents(agents)),
|
||||
addAgent: (agent: AssistantPreset) => dispatch(addAgent(agent)),
|
||||
removeAgent: (id: string) => dispatch(removeAgent({ id }))
|
||||
}
|
||||
}
|
||||
|
||||
export function useAgent(id: string) {
|
||||
const agent = useAppSelector((state) => state.agents.agents.find((a) => a.id === id) as Agent)
|
||||
const agent = useAppSelector((state) => state.agents.agents.find((a) => a.id === id) as AssistantPreset)
|
||||
const dispatch = useAppDispatch()
|
||||
|
||||
return {
|
||||
agent,
|
||||
updateAgent: (agent: Agent) => dispatch(updateAgent(agent)),
|
||||
updateAgent: (agent: AssistantPreset) => dispatch(updateAgent(agent)),
|
||||
updateAgentSettings: (settings: Partial<AssistantSettings>) => {
|
||||
dispatch(updateAgentSettings({ assistantId: agent.id, settings }))
|
||||
}
|
||||
|
||||
@ -7,7 +7,7 @@ import CustomTag from '@renderer/components/Tags/CustomTag'
|
||||
import { useAgents } from '@renderer/hooks/useAgents'
|
||||
import { useNavbarPosition } from '@renderer/hooks/useSettings'
|
||||
import { createAssistantFromAgent } from '@renderer/services/AssistantService'
|
||||
import { Agent } from '@renderer/types'
|
||||
import { AssistantPreset } from '@renderer/types'
|
||||
import { uuid } from '@renderer/utils'
|
||||
import { Button, Empty, Flex, Input } from 'antd'
|
||||
import { omit } from 'lodash'
|
||||
@ -28,7 +28,7 @@ const AgentsPage: FC = () => {
|
||||
const [search, setSearch] = useState('')
|
||||
const [searchInput, setSearchInput] = useState('')
|
||||
const [activeGroup, setActiveGroup] = useState('我的')
|
||||
const [agentGroups, setAgentGroups] = useState<Record<string, Agent[]>>({})
|
||||
const [agentGroups, setAgentGroups] = useState<Record<string, AssistantPreset[]>>({})
|
||||
const [isSearchExpanded, setIsSearchExpanded] = useState(false)
|
||||
const systemAgents = useSystemAgents()
|
||||
const { agents: userAgents } = useAgents()
|
||||
@ -40,7 +40,7 @@ const AgentsPage: FC = () => {
|
||||
我的: userAgents,
|
||||
精选: [],
|
||||
...systemAgentsGroupList
|
||||
} as Record<string, Agent[]>
|
||||
} as Record<string, AssistantPreset[]>
|
||||
setAgentGroups(agentsGroupList)
|
||||
}, [systemAgents, userAgents])
|
||||
|
||||
@ -49,7 +49,7 @@ const AgentsPage: FC = () => {
|
||||
if (!search.trim()) {
|
||||
return agentGroups[activeGroup] || []
|
||||
}
|
||||
const uniqueAgents = new Map<string, Agent>()
|
||||
const uniqueAgents = new Map<string, AssistantPreset>()
|
||||
Object.entries(agentGroups).forEach(([, agents]) => {
|
||||
agents.forEach((agent) => {
|
||||
if (
|
||||
@ -66,7 +66,7 @@ const AgentsPage: FC = () => {
|
||||
const { t, i18n } = useTranslation()
|
||||
|
||||
const onAddAgentConfirm = useCallback(
|
||||
(agent: Agent) => {
|
||||
(agent: AssistantPreset) => {
|
||||
window.modal.confirm({
|
||||
title: agent.name,
|
||||
content: (
|
||||
|
||||
@ -11,7 +11,7 @@ import { fetchGenerate } from '@renderer/services/ApiService'
|
||||
import { getDefaultModel } from '@renderer/services/AssistantService'
|
||||
import { estimateTextTokens } from '@renderer/services/TokenService'
|
||||
import { useAppSelector } from '@renderer/store'
|
||||
import { Agent, KnowledgeBase } from '@renderer/types'
|
||||
import { AssistantPreset, KnowledgeBase } from '@renderer/types'
|
||||
import { getLeadingEmoji, uuid } from '@renderer/utils'
|
||||
import { Button, Form, FormInstance, Input, Modal, Popover, Select, SelectProps } from 'antd'
|
||||
import TextArea from 'antd/es/input/TextArea'
|
||||
@ -21,7 +21,7 @@ import stringWidth from 'string-width'
|
||||
import styled from 'styled-components'
|
||||
|
||||
interface Props {
|
||||
resolve: (data: Agent | null) => void
|
||||
resolve: (data: AssistantPreset | null) => void
|
||||
}
|
||||
|
||||
type FieldType = {
|
||||
@ -77,7 +77,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
return
|
||||
}
|
||||
|
||||
const _agent: Agent = {
|
||||
const _agent: AssistantPreset = {
|
||||
id: uuid(),
|
||||
name: values.name,
|
||||
knowledge_bases: values.knowledge_base_ids
|
||||
@ -272,7 +272,7 @@ export default class AddAgentPopup {
|
||||
TopView.hide('AddAgentPopup')
|
||||
}
|
||||
static show() {
|
||||
return new Promise<Agent | null>((resolve) => {
|
||||
return new Promise<AssistantPreset | null>((resolve) => {
|
||||
TopView.show(
|
||||
<PopupContainer
|
||||
resolve={(v) => {
|
||||
|
||||
@ -3,7 +3,7 @@ import CustomTag from '@renderer/components/Tags/CustomTag'
|
||||
import { useAgents } from '@renderer/hooks/useAgents'
|
||||
import AssistantSettingsPopup from '@renderer/pages/settings/AssistantSettings'
|
||||
import { createAssistantFromAgent } from '@renderer/services/AssistantService'
|
||||
import type { Agent } from '@renderer/types'
|
||||
import type { AssistantPreset } from '@renderer/types'
|
||||
import { getLeadingEmoji } from '@renderer/utils'
|
||||
import { Button, Dropdown } from 'antd'
|
||||
import { t } from 'i18next'
|
||||
@ -14,7 +14,7 @@ import styled from 'styled-components'
|
||||
import ManageAgentsPopup from './ManageAgentsPopup'
|
||||
|
||||
interface Props {
|
||||
agent: Agent
|
||||
agent: AssistantPreset
|
||||
activegroup?: string
|
||||
onClick: () => void
|
||||
getLocalizedGroupName: (group: string) => string
|
||||
@ -26,7 +26,7 @@ const AgentCard: FC<Props> = ({ agent, onClick, activegroup, getLocalizedGroupNa
|
||||
const cardRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleDelete = useCallback(
|
||||
(agent: Agent) => {
|
||||
(agent: AssistantPreset) => {
|
||||
window.modal.confirm({
|
||||
centered: true,
|
||||
content: t('agents.delete.popup.content'),
|
||||
|
||||
@ -3,14 +3,14 @@ import { useAgents } from '@renderer/hooks/useAgents'
|
||||
import { useTimer } from '@renderer/hooks/useTimer'
|
||||
import { getDefaultModel } from '@renderer/services/AssistantService'
|
||||
import { EVENT_NAMES, EventEmitter } from '@renderer/services/EventService'
|
||||
import { Agent } from '@renderer/types'
|
||||
import { AssistantPreset } from '@renderer/types'
|
||||
import { uuid } from '@renderer/utils'
|
||||
import { Button, Flex, Form, Input, Modal, Radio } from 'antd'
|
||||
import { useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
interface Props {
|
||||
resolve: (value: Agent[] | null) => void
|
||||
resolve: (value: AssistantPreset[] | null) => void
|
||||
}
|
||||
|
||||
const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
@ -25,7 +25,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
const onFinish = async (values: { url?: string }) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
let agents: Agent[] = []
|
||||
let agents: AssistantPreset[] = []
|
||||
|
||||
if (importType === 'url') {
|
||||
if (!values.url) {
|
||||
@ -58,7 +58,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
throw new Error(t('agents.import.error.invalid_format'))
|
||||
}
|
||||
|
||||
const newAgent: Agent = {
|
||||
const newAgent: AssistantPreset = {
|
||||
id: uuid(),
|
||||
name: agent.name,
|
||||
emoji: agent.emoji || '🤖',
|
||||
@ -133,7 +133,7 @@ const PopupContainer: React.FC<Props> = ({ resolve }) => {
|
||||
|
||||
export default class ImportAgentPopup {
|
||||
static show() {
|
||||
return new Promise<Agent[] | null>((resolve) => {
|
||||
return new Promise<AssistantPreset[] | null>((resolve) => {
|
||||
TopView.show(
|
||||
<PopupContainer
|
||||
resolve={(v) => {
|
||||
|
||||
@ -2,19 +2,24 @@ import { loggerService } from '@logger'
|
||||
import { useRuntime } from '@renderer/hooks/useRuntime'
|
||||
import { useSettings } from '@renderer/hooks/useSettings'
|
||||
import store from '@renderer/store'
|
||||
import { Agent } from '@renderer/types'
|
||||
import { AssistantPreset } from '@renderer/types'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
const logger = loggerService.withContext('useSystemAgents')
|
||||
|
||||
let _agents: Agent[] = []
|
||||
let _agents: AssistantPreset[] = []
|
||||
|
||||
export const getAgentsFromSystemAgents = (systemAgents: any) => {
|
||||
const agents: Agent[] = []
|
||||
const agents: AssistantPreset[] = []
|
||||
for (let i = 0; i < systemAgents.length; i++) {
|
||||
for (let j = 0; j < systemAgents[i].group.length; j++) {
|
||||
const agent = { ...systemAgents[i], group: systemAgents[i].group[j], topics: [], type: 'agent' } as Agent
|
||||
const agent = {
|
||||
...systemAgents[i],
|
||||
group: systemAgents[i].group[j],
|
||||
topics: [],
|
||||
type: 'agent'
|
||||
} as AssistantPreset
|
||||
agents.push(agent)
|
||||
}
|
||||
}
|
||||
@ -23,7 +28,7 @@ export const getAgentsFromSystemAgents = (systemAgents: any) => {
|
||||
|
||||
export function useSystemAgents() {
|
||||
const { defaultAgent } = useSettings()
|
||||
const [agents, setAgents] = useState<Agent[]>([])
|
||||
const [agents, setAgents] = useState<AssistantPreset[]>([])
|
||||
const { resourcesPath } = useRuntime()
|
||||
const { agentssubscribeUrl } = store.getState().settings
|
||||
const { i18n } = useTranslation()
|
||||
@ -40,7 +45,7 @@ export function useSystemAgents() {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`)
|
||||
}
|
||||
const agentsData = (await response.json()) as Agent[]
|
||||
const agentsData = (await response.json()) as AssistantPreset[]
|
||||
setAgents(agentsData)
|
||||
return
|
||||
} catch (error) {
|
||||
@ -54,7 +59,7 @@ export function useSystemAgents() {
|
||||
try {
|
||||
const fileName = currentLanguage === 'zh-CN' ? 'agents-zh.json' : 'agents-en.json'
|
||||
const localAgentsData = await window.api.fs.read(`${resourcesPath}/data/${fileName}`, 'utf-8')
|
||||
_agents = JSON.parse(localAgentsData) as Agent[]
|
||||
_agents = JSON.parse(localAgentsData) as AssistantPreset[]
|
||||
} catch (error) {
|
||||
logger.error('Failed to load local agents:', error as Error)
|
||||
}
|
||||
@ -74,8 +79,8 @@ export function useSystemAgents() {
|
||||
return agents
|
||||
}
|
||||
|
||||
export function groupByCategories(data: Agent[]) {
|
||||
const groupedMap = new Map<string, Agent[]>()
|
||||
export function groupByCategories(data: AssistantPreset[]) {
|
||||
const groupedMap = new Map<string, AssistantPreset[]>()
|
||||
data.forEach((item) => {
|
||||
item.group?.forEach((category) => {
|
||||
if (!groupedMap.has(category)) {
|
||||
@ -84,7 +89,7 @@ export function groupByCategories(data: Agent[]) {
|
||||
groupedMap.get(category)?.push(item)
|
||||
})
|
||||
})
|
||||
const result: Record<string, Agent[]> = {}
|
||||
const result: Record<string, AssistantPreset[]> = {}
|
||||
Array.from(groupedMap.entries()).forEach(([category, items]) => {
|
||||
result[category] = items
|
||||
})
|
||||
|
||||
@ -12,7 +12,7 @@ import i18n from '@renderer/i18n'
|
||||
import store from '@renderer/store'
|
||||
import { addAssistant } from '@renderer/store/assistants'
|
||||
import type {
|
||||
Agent,
|
||||
AssistantPreset,
|
||||
Assistant,
|
||||
AssistantSettings,
|
||||
Model,
|
||||
@ -185,7 +185,7 @@ export function getAssistantById(id: string) {
|
||||
return assistants.find((a) => a.id === id)
|
||||
}
|
||||
|
||||
export async function createAssistantFromAgent(agent: Agent) {
|
||||
export async function createAssistantFromAgent(agent: AssistantPreset) {
|
||||
const assistantId = uuid()
|
||||
const topic = getDefaultTopic(assistantId)
|
||||
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { DEFAULT_CONTEXTCOUNT, DEFAULT_TEMPERATURE } from '@renderer/config/constant'
|
||||
import { Agent, AssistantSettings } from '@renderer/types'
|
||||
import { AssistantPreset, AssistantSettings } from '@renderer/types'
|
||||
|
||||
export interface AgentsState {
|
||||
agents: Agent[]
|
||||
/** @deprecated They are actually assistant presets. */
|
||||
agents: AssistantPreset[]
|
||||
}
|
||||
|
||||
const initialState: AgentsState = {
|
||||
@ -14,16 +15,16 @@ const assistantsSlice = createSlice({
|
||||
name: 'agents',
|
||||
initialState,
|
||||
reducers: {
|
||||
updateAgents: (state, action: PayloadAction<Agent[]>) => {
|
||||
updateAgents: (state, action: PayloadAction<AssistantPreset[]>) => {
|
||||
state.agents = action.payload
|
||||
},
|
||||
addAgent: (state, action: PayloadAction<Agent>) => {
|
||||
addAgent: (state, action: PayloadAction<AssistantPreset>) => {
|
||||
state.agents.push(action.payload)
|
||||
},
|
||||
removeAgent: (state, action: PayloadAction<{ id: string }>) => {
|
||||
state.agents = state.agents.filter((c) => c.id !== action.payload.id)
|
||||
},
|
||||
updateAgent: (state, action: PayloadAction<Agent>) => {
|
||||
updateAgent: (state, action: PayloadAction<AssistantPreset>) => {
|
||||
state.agents = state.agents.map((c) => (c.id === action.payload.id ? action.payload : c))
|
||||
},
|
||||
updateAgentSettings: (
|
||||
|
||||
@ -130,7 +130,7 @@ export type AssistantSettings = {
|
||||
toolUseMode: 'function' | 'prompt'
|
||||
}
|
||||
|
||||
export type Agent = Omit<Assistant, 'model'> & {
|
||||
export type AssistantPreset = Omit<Assistant, 'model'> & {
|
||||
group?: string[]
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user