mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-19 14:41:24 +08:00
feat(api): add models endpoint to agent client and type response
Add new models endpoint to AgentApiClient with query parameter support Ensure type safety by adding ApiModelsResponse type and schema validation
This commit is contained in:
parent
c426876d0d
commit
c77d7dff78
@ -1,4 +1,4 @@
|
||||
import { ApiModelsFilterSchema } from '@types'
|
||||
import { ApiModelsFilterSchema, ApiModelsResponse } from '@types'
|
||||
import express, { Request, Response } from 'express'
|
||||
|
||||
import { loggerService } from '../../services/LoggerService'
|
||||
@ -114,7 +114,7 @@ const router = express
|
||||
response.data.map((m) => m.id)
|
||||
)
|
||||
|
||||
return res.json(response)
|
||||
return res.json(response satisfies ApiModelsResponse)
|
||||
} catch (error: any) {
|
||||
logger.error('Error fetching models:', error)
|
||||
return res.status(503).json({
|
||||
|
||||
@ -3,6 +3,8 @@ import { formatAgentServerError } from '@renderer/utils'
|
||||
import {
|
||||
AddAgentForm,
|
||||
AgentServerErrorSchema,
|
||||
ApiModelsResponse,
|
||||
ApiModelsResponseSchema,
|
||||
CreateAgentRequest,
|
||||
CreateAgentResponse,
|
||||
CreateAgentResponseSchema,
|
||||
@ -18,6 +20,8 @@ import {
|
||||
ListAgentSessionsResponseSchema,
|
||||
type ListAgentsResponse,
|
||||
ListAgentsResponseSchema,
|
||||
objectEntries,
|
||||
objectKeys,
|
||||
UpdateAgentForm,
|
||||
UpdateAgentRequest,
|
||||
UpdateAgentResponse,
|
||||
@ -77,6 +81,19 @@ export class AgentApiClient {
|
||||
public getSessionMessagesPath = (agentId: string, sessionId: string) =>
|
||||
`/${this.apiVersion}/agents/${agentId}/sessions/${sessionId}/messages`
|
||||
|
||||
public modelsPath = (props?: { providerType?: 'anthropic'; limit?: number }) => {
|
||||
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 {
|
||||
@ -216,4 +233,15 @@ export class AgentApiClient {
|
||||
throw processError(error, 'Failed to post message.')
|
||||
}
|
||||
}
|
||||
|
||||
public async getModels(props?: { providerType?: 'anthropic'; limit?: number }): Promise<ApiModelsResponse> {
|
||||
const url = this.modelsPath(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.')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user