بنقرة واحدة
hai-audit
使用 @h-ai/audit 进行审计日志的记录、查询、清理与统计。当需求涉及操作审计、安全事件追踪、用户活动日志时使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
使用 @h-ai/audit 进行审计日志的记录、查询、清理与统计。当需求涉及操作审计、安全事件追踪、用户活动日志时使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Use when: using @h-ai/ai, LLM calls, chat completion, tool calling, function calling, MCP server, streaming, memory management, context compression, summarization, token estimation, RAG, knowledge base, AI client, embeddings, reasoning, rerank, file parsing, speech recognition ASR, speech synthesis TTS, audio, A2A agent-to-agent. 使用 @h-ai/ai 进行 LLM 调用、工具定义、MCP 服务器、流式处理、记忆管理、上下文压缩、知识库、推理引擎、Rerank、文件解析、语音识别与合成、A2A 与会话持久化。
Use when: using @h-ai/ai for LLM calls, tools, MCP, streaming, memory/context, RAG, audio, A2A, or the AI client. 当需求涉及 AI 对话、工具、Audio、会话、知识库或 AI 客户端时使用。
Use when: creating or extending apps in hai-framework, adding routes, pages, API endpoints, service workspaces, mobile app, H5 app, admin console. 在 hai-framework 中创建或扩展应用或 API service workspace,包含路由、API 端点、typed contract、服务层与 UI 脚手架代码。
Use when: creating a new module, new package, scaffold, add sub-feature, add provider, create repository, module structure, tsup config, error codes, NotInitializedKit pattern. 在 hai-framework 中创建新模块(package)。
Use when: reviewing app code in hai-framework, auditing app quality, checking app conventions, reviewing routes, reviewing API service workspaces, app security, app i18n review. 对 hai-framework 应用层代码进行审查:路由安全 → 认证授权 → i18n → 组件使用 → API 端点 / service workspace → 服务层 → 性能。
Use when: reviewing code, code review, auditing module quality, checking hai-framework conventions, verifying HaiResult<T> usage, reviewing module structure, PR review, checking naming consistency, verifying NotInitializedKit pattern, auditing performance, security, distributed systems. 对 hai-framework 模块进行全维度代码审查:架构 → 命名 → 类型 → 注释 → 性能 → 分布式 → 安全 → 日志 → 测试 → 文档。
| name | hai-audit |
| description | 使用 @h-ai/audit 进行审计日志的记录、查询、清理与统计。当需求涉及操作审计、安全事件追踪、用户活动日志时使用。 |
| 项目 | 契约 |
|---|---|
| 能力 | 使用 @h-ai/audit 进行审计日志的记录、查询、清理与统计。当需求涉及操作审计、安全事件追踪、用户活动日志时使用。 |
| 适用场景 | 当任务与 hai-audit 的能力描述匹配,并且需要遵循本 Skill 的流程和边界时 |
| 输入 | 模块配置、类型化业务参数、依赖初始化状态和目标运行环境 |
| 输出 | 符合模块公共 API 的实现或示例;业务结果使用 HaiResult,并同步必要测试与文档 |
| 限制 | 遵守 init → use → close 生命周期与运行环境边界;不绕过类型、授权、输入校验或敏感信息保护 |
统一审计日志模块,基于 @h-ai/reldb 实现持久化,提供审计日志的记录、分页查询、清理与统计。
⚠️ 服务端模块(Node.js only)。 审计日志由服务端自动记录,浏览器端通过 API 端点查询审计数据。
| 模块 | 用途 | 是否必需 | 初始化要求 |
|---|---|---|---|
@h-ai/reldb | 数据库(审计日志持久化) | 必需 | 需在 audit.init() 前初始化 |
审计模块依赖已初始化的 @h-ai/reldb 单例,无需独立配置文件。审计日志固定存储在 hai_audit_logs 表中。
import { audit } from '@h-ai/audit'
import { reldb } from '@h-ai/reldb'
// 先初始化依赖
await reldb.init({ type: 'sqlite', database: './data.db' })
// 再初始化审计模块(自动使用已初始化的 reldb 单例)
const result = await audit.init()
if (!result.success) {
// 处理错误
}
自定义用户表映射:
await audit.init({
userTable: 'hai_iam_users',
userIdColumn: 'id',
userNameColumn: 'username',
})
const closeResult = await audit.close()
if (!closeResult.success) {
throw new Error(closeResult.error.message)
}
| 方法 | 签名 | 说明 |
|---|---|---|
audit.init | (config: AuditInitConfig) => Promise<HaiResult<void>> | 初始化模块 |
audit.close | () => Promise<HaiResult<void>> | 关闭模块 |
audit.isInitialized | boolean | 初始化状态 |
| 方法 | 签名 | 说明 |
|---|---|---|
audit.log | (input: CreateAuditLogInput) => Promise<HaiResult<AuditLog>> | 记录审计日志 |
audit.list | (options?: ListAuditLogsOptions) => Promise<HaiResult<{ items, total }>> | 分页查询(含用户名) |
audit.getUserRecent | (userId: string, limit?: number) => Promise<HaiResult<AuditLog[]>> | 用户最近活动 |
audit.cleanup | (olderThanDays?: number) => Promise<HaiResult<number>> | 清理旧日志 |
audit.getStats | (days?: number) => Promise<HaiResult<AuditStatItem[]>> | 操作统计 |
audit.helper)| 方法 | 说明 |
|---|---|
audit.helper.login(userId, ip?, ua?) | 登录 |
audit.helper.logout(userId, ip?, ua?) | 登出 |
audit.helper.register(userId, ip?, ua?) | 注册 |
audit.helper.passwordResetRequest(email, ip?, ua?) | 密码重置请求 |
audit.helper.passwordResetComplete(userId, ip?, ua?) | 密码重置完成 |
audit.helper.crud(input) | CRUD 操作 |
audit.log()/audit.helper.*()中的details仅会自动脱敏凭证类字段(如password/token/secret);邮箱、手机号默认保留原值。
| 错误码 | code | 说明 |
|---|---|---|
HaiAuditError.LOG_FAILED | hai:audit:001 | 记录失败 |
HaiAuditError.QUERY_FAILED | hai:audit:002 | 查询失败 |
HaiAuditError.CLEANUP_FAILED | hai:audit:003 | 清理失败 |
HaiAuditError.STATS_FAILED | hai:audit:004 | 统计失败 |
HaiAuditError.INIT_IN_PROGRESS | hai:audit:005 | 初始化进行中 |
HaiAuditError.NOT_INITIALIZED | hai:audit:010 | 模块未初始化 |
HaiAuditError.CONFIG_ERROR | hai:audit:012 | 配置错误 |
// 登录
await audit.helper.login(userId, request.ip, request.headers['user-agent'])
// 登出
await audit.helper.logout(userId, request.ip, request.headers['user-agent'])
await audit.helper.crud({ userId, action: 'create', resource: 'users', resourceId: newUser.id, details: { name: newUser.name } })
await audit.helper.crud({ userId, action: 'update', resource: 'roles', resourceId: roleId, details: { changes } })
await audit.helper.crud({ userId, action: 'delete', resource: 'permissions', resourceId: permId })
// 最近日志
const recentLogs = await audit.list({ pageSize: 10 })
// 7 天统计
const stats = await audit.getStats(7)
// 清理 90 天前的日志
const result = await audit.cleanup(90)
if (result.success) {
logger.info(`Cleaned up ${result.data} old audit logs`)
}
hai-reldb — 数据库模块(审计模块依赖)hai-iam — 身份认证与权限管理(审计的主要记录对象)hai-core — 核心工具(日志、ID 生成、i18n)