import { Injectable } from '@nestjs/common'; import { AttendanceService } from '../../attendance/attendance.service'; import { AgentBusinessScopeFactory } from '../agent-business-scope.factory'; import type { AgentToolContext, ToolDef, ToolInputResult } from '../agent-tool.types'; import { optionalDate, optionalPositiveInt, rejectUnknownKeys } from './tool-input'; interface Input { classId?: number; dateFrom?: string; dateTo?: string; limit?: number } @Injectable() export class GetAttendanceSummaryTool implements ToolDef { readonly name = 'get_attendance_summary'; readonly description = '按日期和班级汇总当前用户有权查看的考勤数据。'; readonly requiredPermission = 'attendance:view'; readonly inputSchema = { type: 'object', properties: { classId: { type: 'integer', minimum: 1 }, dateFrom: { type: 'string', format: 'date' }, dateTo: { type: 'string', format: 'date' }, limit: { type: 'integer', minimum: 1, maximum: 50 }, }, additionalProperties: false }; constructor(private readonly service: AttendanceService, private readonly scopes: AgentBusinessScopeFactory) {} validate(raw: Record): ToolInputResult { const invalid = rejectUnknownKeys(raw, ['classId', 'dateFrom', 'dateTo', 'limit']); if (invalid) return invalid; const classId = optionalPositiveInt(raw.classId, 'classId'); if (!classId.ok) return classId; const dateFrom = optionalDate(raw.dateFrom, 'dateFrom'); if (!dateFrom.ok) return dateFrom; const dateTo = optionalDate(raw.dateTo, 'dateTo'); if (!dateTo.ok) return dateTo; if (dateFrom.value && dateTo.value && dateFrom.value > dateTo.value) return { ok: false, error: 'dateTo 不能早于 dateFrom' }; const limit = optionalPositiveInt(raw.limit, 'limit', 50); if (!limit.ok) return limit; return { ok: true, value: { classId: classId.value, dateFrom: dateFrom.value, dateTo: dateTo.value, limit: limit.value } }; } execute(input: Input, context: AgentToolContext) { return this.service.agentGetAttendanceSummary(context.userId, this.scopes.canManageAllAttendance(context), input); } }