feat(sessions): add CreateSessionResponse type for better type safety

Introduce CreateSessionResponse type and schema to clearly define the return type of session creation operations. This improves type safety and consistency across the codebase when handling session responses.
This commit is contained in:
icarus 2025-09-18 21:43:06 +08:00
parent 3b5b1986e6
commit f5f542911f
3 changed files with 8 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import { loggerService } from '@logger' import { loggerService } from '@logger'
import { sessionMessageService, sessionService } from '@main/services/agents' import { sessionMessageService, sessionService } from '@main/services/agents'
import { CreateSessionResponse } from '@types'
import { Request, Response } from 'express' import { Request, Response } from 'express'
const logger = loggerService.withContext('ApiServerSessionsHandlers') const logger = loggerService.withContext('ApiServerSessionsHandlers')
@ -12,7 +13,7 @@ export const createSession = async (req: Request, res: Response): Promise<Respon
logger.info(`Creating new session for agent: ${agentId}`) logger.info(`Creating new session for agent: ${agentId}`)
logger.debug('Session data:', sessionData) logger.debug('Session data:', sessionData)
const session = await sessionService.createSession(agentId, sessionData) const session = (await sessionService.createSession(agentId, sessionData)) satisfies CreateSessionResponse
logger.info(`Session created successfully: ${session.id}`) logger.info(`Session created successfully: ${session.id}`)
return res.status(201).json(session) return res.status(201).json(session)

View File

@ -2,6 +2,7 @@ import type {
AgentEntity, AgentEntity,
AgentSessionEntity, AgentSessionEntity,
CreateSessionRequest, CreateSessionRequest,
CreateSessionResponse,
GetAgentSessionResponse, GetAgentSessionResponse,
ListOptions, ListOptions,
UpdateSessionRequest UpdateSessionRequest
@ -25,7 +26,7 @@ export class SessionService extends BaseService {
await BaseService.initialize() await BaseService.initialize()
} }
async createSession(agentId: string, req: CreateSessionRequest): Promise<AgentSessionEntity> { async createSession(agentId: string, req: CreateSessionRequest): Promise<CreateSessionResponse> {
this.ensureInitialized() this.ensureInitialized()
// Validate agent exists - we'll need to import AgentService for this check // Validate agent exists - we'll need to import AgentService for this check

View File

@ -186,6 +186,10 @@ export interface CreateSessionMessageRequest {
content: string content: string
} }
export const CreateSessionResponseSchema = AgentSessionEntitySchema
export type CreateSessionResponse = AgentSessionEntity
export const AgentServerErrorSchema = z.object({ export const AgentServerErrorSchema = z.object({
message: z.string(), message: z.string(),
type: z.string(), type: z.string(),