mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 11:20:07 +08:00
* Refactor agent streaming from EventEmitter to ReadableStream Replaced EventEmitter-based agent streaming with ReadableStream for better compatibility with AI SDK patterns. Modified SessionMessageService to return stream/completion pair instead of event emitter, updated HTTP handlers to use stream pumping, and added IPC contract for renderer-side message persistence. * Add accessible paths management to agent configuration Move accessible paths functionality from session modal to agent modal, add validation requiring at least one path, and update form handling to inherit agent paths in sessions. * Add provider_name field to model objects and improve display - Add provider_name field to ApiModel schema and transformation logic - Update model options to include providerName for better display - Improve provider label fallback chain in model transformation - Fix agent hook to use proper SWR key and conditional fetching - Enhance option rendering with better truncation and provider display * fix(i18n): Auto update translations for PR #10276 * Optimize chat components with memoization and shared layout - Wrap `SessionMessages` and `SessionInputBar` in `useMemo` to prevent unnecessary re-renders - Refactor `AgentSessionMessages` to use shared layout components and message grouping - Extract common styled components to `shared.tsx` for reuse across message components * Add smooth animations to SessionsTab and Sessions components - Replace static conditional rendering with Framer Motion animations for no-agent and session states - Animate session list items with staggered entrance and exit transitions - Add loading spinner animation with fade effect - Apply motion to session creation button with delayed entrance * Add loading state with spinner and i18n support to SessionsTab - Replace static "No active agent" message with a spinner and loading text - Integrate react-i18next for translation of loading message - Adjust animation timing and styling for smoother loading state transition * Support API models with provider_name field in getModelName - Add ApiModel type import and update function signature to accept ApiModel - Return formatted name using provider_name field for API models - Maintain backward compatibility for legacy models by looking up provider in store * Simplify provider display name logic and add debug logging - Replace complex fallback chain for provider display name with direct provider name access - Add console.log for model debugging in getModelName function * Extract model name from session model string - Use split and pop to isolate the model name after the colon - Fall back to the full model string if no colon is present - Maintain provider and group identifiers for model object consistency * Improve model name resolution for agent sessions - Extract actual model ID from session model string and resolve model details - Use resolved model name, provider, and group when available instead of defaults - Remove redundant API model handling in getModelName function * Set default active agent and session on load - Automatically select first agent if none active after loading - Automatically select first session per agent if none active after loading - Prevent empty selection states in UI components --------- Co-authored-by: GitHub Action <action@github.com>
238 lines
7.5 KiB
TypeScript
238 lines
7.5 KiB
TypeScript
import { loggerService } from '@logger'
|
|
import { formatAgentServerError } from '@renderer/utils/error'
|
|
import {
|
|
AddAgentForm,
|
|
AgentServerErrorSchema,
|
|
ApiModelsFilter,
|
|
ApiModelsResponse,
|
|
ApiModelsResponseSchema,
|
|
CreateAgentRequest,
|
|
CreateAgentResponse,
|
|
CreateAgentResponseSchema,
|
|
CreateSessionForm,
|
|
CreateSessionRequest,
|
|
CreateSessionResponse,
|
|
CreateSessionResponseSchema,
|
|
GetAgentResponse,
|
|
GetAgentResponseSchema,
|
|
GetAgentSessionResponse,
|
|
ListAgentSessionsResponse,
|
|
ListAgentSessionsResponseSchema,
|
|
type ListAgentsResponse,
|
|
ListAgentsResponseSchema,
|
|
objectEntries,
|
|
objectKeys,
|
|
UpdateAgentForm,
|
|
UpdateAgentRequest,
|
|
UpdateAgentResponse,
|
|
UpdateAgentResponseSchema,
|
|
UpdateSessionForm,
|
|
UpdateSessionRequest,
|
|
UpdateSessionResponse,
|
|
UpdateSessionResponseSchema
|
|
} from '@types'
|
|
import axios, { Axios, AxiosRequestConfig, isAxiosError } from 'axios'
|
|
import { ZodError } from 'zod'
|
|
|
|
type ApiVersion = 'v1'
|
|
|
|
const logger = loggerService.withContext('AgentApiClient')
|
|
|
|
// const logger = loggerService.withContext('AgentClient')
|
|
const processError = (error: unknown, fallbackMessage: string) => {
|
|
logger.error(fallbackMessage, error as Error)
|
|
if (isAxiosError(error)) {
|
|
const result = AgentServerErrorSchema.safeParse(error.response?.data)
|
|
if (result.success) {
|
|
return new Error(formatAgentServerError(result.data))
|
|
}
|
|
} else if (error instanceof ZodError) {
|
|
return error
|
|
}
|
|
return new Error(fallbackMessage, { cause: error })
|
|
}
|
|
|
|
export class AgentApiClient {
|
|
private axios: Axios
|
|
private apiVersion: ApiVersion = 'v1'
|
|
constructor(config: AxiosRequestConfig, apiVersion?: ApiVersion) {
|
|
if (!config.baseURL || !config.headers?.Authorization) {
|
|
throw new Error('Please pass in baseUrl and Authroization header.')
|
|
}
|
|
if (config.baseURL.endsWith('/')) {
|
|
throw new Error('baseURL should not end with /')
|
|
}
|
|
this.axios = axios.create(config)
|
|
if (apiVersion) {
|
|
this.apiVersion = apiVersion
|
|
}
|
|
}
|
|
|
|
public agentPaths = {
|
|
base: `/${this.apiVersion}/agents`,
|
|
withId: (id: string) => `/${this.apiVersion}/agents/${id}`
|
|
}
|
|
|
|
public getSessionPaths = (agentId: string) => ({
|
|
base: `/${this.apiVersion}/agents/${agentId}/sessions`,
|
|
withId: (id: string) => `/${this.apiVersion}/agents/${agentId}/sessions/${id}`
|
|
})
|
|
|
|
public getSessionMessagesPath = (agentId: string, sessionId: string) =>
|
|
`/${this.apiVersion}/agents/${agentId}/sessions/${sessionId}/messages`
|
|
|
|
public getModelsPath = (props?: ApiModelsFilter) => {
|
|
const base = `/${this.apiVersion}/models`
|
|
if (!props) return base
|
|
if (objectKeys(props).length > 0) {
|
|
const params = objectEntries(props)
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join('&')
|
|
return `${base}?${params}`
|
|
} else {
|
|
return base
|
|
}
|
|
}
|
|
|
|
public async listAgents(): Promise<ListAgentsResponse> {
|
|
const url = this.agentPaths.base
|
|
try {
|
|
const response = await this.axios.get(url)
|
|
const result = ListAgentsResponseSchema.safeParse(response.data)
|
|
if (!result.success) {
|
|
throw new Error('Not a valid Agents array.')
|
|
}
|
|
return result.data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to list agents.')
|
|
}
|
|
}
|
|
|
|
public async createAgent(form: AddAgentForm): Promise<CreateAgentResponse> {
|
|
const url = this.agentPaths.base
|
|
try {
|
|
const payload = form satisfies CreateAgentRequest
|
|
const response = await this.axios.post(url, payload)
|
|
const data = CreateAgentResponseSchema.parse(response.data)
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to create agent.')
|
|
}
|
|
}
|
|
|
|
public async getAgent(id: string): Promise<GetAgentResponse> {
|
|
const url = this.agentPaths.withId(id)
|
|
try {
|
|
const response = await this.axios.get(url)
|
|
const data = GetAgentResponseSchema.parse(response.data)
|
|
if (data.id !== id) {
|
|
throw new Error('Agent ID mismatch in response')
|
|
}
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to get agent.')
|
|
}
|
|
}
|
|
|
|
public async deleteAgent(id: string): Promise<void> {
|
|
const url = this.agentPaths.withId(id)
|
|
try {
|
|
await this.axios.delete(url)
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to delete agent.')
|
|
}
|
|
}
|
|
|
|
public async updateAgent(form: UpdateAgentForm): Promise<UpdateAgentResponse> {
|
|
const url = this.agentPaths.withId(form.id)
|
|
try {
|
|
const payload = form satisfies UpdateAgentRequest
|
|
const response = await this.axios.patch(url, payload)
|
|
const data = UpdateAgentResponseSchema.parse(response.data)
|
|
if (data.id !== form.id) {
|
|
throw new Error('Agent ID mismatch in response')
|
|
}
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to updateAgent.')
|
|
}
|
|
}
|
|
|
|
public async listSessions(agentId: string): Promise<ListAgentSessionsResponse> {
|
|
const url = this.getSessionPaths(agentId).base
|
|
try {
|
|
const response = await this.axios.get(url)
|
|
const result = ListAgentSessionsResponseSchema.safeParse(response.data)
|
|
if (!result.success) {
|
|
throw new Error('Not a valid Sessions array.')
|
|
}
|
|
return result.data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to list sessions.')
|
|
}
|
|
}
|
|
|
|
public async createSession(agentId: string, session: CreateSessionForm): Promise<CreateSessionResponse> {
|
|
const url = this.getSessionPaths(agentId).base
|
|
try {
|
|
const payload = session satisfies CreateSessionRequest
|
|
const response = await this.axios.post(url, payload)
|
|
const data = CreateSessionResponseSchema.parse(response.data)
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to add session.')
|
|
}
|
|
}
|
|
|
|
public async getSession(agentId: string, sessionId: string): Promise<GetAgentSessionResponse> {
|
|
const url = this.getSessionPaths(agentId).withId(sessionId)
|
|
try {
|
|
const response = await this.axios.get(url)
|
|
// const data = GetAgentSessionResponseSchema.parse(response.data)
|
|
// TODO: enable validation
|
|
const data = response.data
|
|
if (sessionId !== data.id) {
|
|
throw new Error('Session ID mismatch in response')
|
|
}
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to get session.')
|
|
}
|
|
}
|
|
|
|
public async deleteSession(agentId: string, sessionId: string): Promise<void> {
|
|
const url = this.getSessionPaths(agentId).withId(sessionId)
|
|
try {
|
|
await this.axios.delete(url)
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to delete session.')
|
|
}
|
|
}
|
|
|
|
public async updateSession(agentId: string, session: UpdateSessionForm): Promise<UpdateSessionResponse> {
|
|
const url = this.getSessionPaths(agentId).withId(session.id)
|
|
try {
|
|
const payload = session satisfies UpdateSessionRequest
|
|
const response = await this.axios.patch(url, payload)
|
|
const data = UpdateSessionResponseSchema.parse(response.data)
|
|
if (session.id !== data.id) {
|
|
throw new Error('Session ID mismatch in response')
|
|
}
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to update session.')
|
|
}
|
|
}
|
|
|
|
public async getModels(props?: ApiModelsFilter): Promise<ApiModelsResponse> {
|
|
const url = this.getModelsPath(props)
|
|
try {
|
|
const response = await this.axios.get(url)
|
|
const data = ApiModelsResponseSchema.parse(response.data)
|
|
return data
|
|
} catch (error) {
|
|
throw processError(error, 'Failed to get models.')
|
|
}
|
|
}
|
|
}
|