Browse Source
- GET /api/agent-documents: list documents with type/keyword/pagination filters - GET /api/agent-documents/🆔 get document detail with content - PATCH /api/agent-documents/🆔 update document title/content/tags - DELETE /api/agent-documents/🆔 delete document - All endpoints require authentication and enforce user-level isolation Co-authored-by: CodeFree <codefree@chinatelcom.cn>feat/agent-independence
4 changed files with 88 additions and 0 deletions
@ -0,0 +1,16 @@ |
|||||
|
import { deleteDocument } from "../../service/agent-document"; |
||||
|
import { requireUser } from "#server/utils/context"; |
||||
|
|
||||
|
export default defineWrappedResponseHandler(async (event) => { |
||||
|
const user = await requireUser(event); |
||||
|
|
||||
|
const idParam = getRouterParam(event, "id"); |
||||
|
if (!idParam) return R.throwError(400, "缺少文档 ID", null); |
||||
|
const id = parseInt(idParam); |
||||
|
if (isNaN(id)) return R.throwError(400, "文档 ID 格式不正确", null); |
||||
|
|
||||
|
const deleted = await deleteDocument(id, user.id); |
||||
|
if (!deleted) return R.throwError(404, "文档不存在或无权访问", null); |
||||
|
|
||||
|
return R.success(null); |
||||
|
}); |
||||
@ -0,0 +1,19 @@ |
|||||
|
import { getDocumentById, deserializeTags } from "../../service/agent-document"; |
||||
|
import { requireUser } from "#server/utils/context"; |
||||
|
|
||||
|
export default defineWrappedResponseHandler(async (event) => { |
||||
|
const user = await requireUser(event); |
||||
|
|
||||
|
const idParam = getRouterParam(event, "id"); |
||||
|
if (!idParam) return R.throwError(400, "缺少文档 ID", null); |
||||
|
const id = parseInt(idParam); |
||||
|
if (isNaN(id)) return R.throwError(400, "文档 ID 格式不正确", null); |
||||
|
|
||||
|
const doc = await getDocumentById(id, user.id); |
||||
|
if (!doc) return R.throwError(404, "文档不存在或无权访问", null); |
||||
|
|
||||
|
return R.success({ |
||||
|
...doc, |
||||
|
tags: deserializeTags(doc.tags), |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,33 @@ |
|||||
|
import { updateDocument, deserializeTags } from "../../service/agent-document"; |
||||
|
import { requireUser } from "#server/utils/context"; |
||||
|
import { validate } from "#server/utils/validation"; |
||||
|
import { z } from "zod"; |
||||
|
|
||||
|
const updateSchema = z.object({ |
||||
|
title: z.string().min(1).max(200).optional(), |
||||
|
content: z.string().max(50000).optional(), |
||||
|
tags: z.array(z.string()).optional(), |
||||
|
}); |
||||
|
|
||||
|
export default defineWrappedResponseHandler(async (event) => { |
||||
|
const user = await requireUser(event); |
||||
|
|
||||
|
const idParam = getRouterParam(event, "id"); |
||||
|
if (!idParam) return R.throwError(400, "缺少文档 ID", null); |
||||
|
const id = parseInt(idParam); |
||||
|
if (isNaN(id)) return R.throwError(400, "文档 ID 格式不正确", null); |
||||
|
|
||||
|
const body = await validate(updateSchema, await readBody(event)); |
||||
|
|
||||
|
const updated = await updateDocument(id, user.id, { |
||||
|
title: body.title, |
||||
|
content: body.content, |
||||
|
tags: body.tags, |
||||
|
}); |
||||
|
if (!updated) return R.throwError(404, "文档不存在或无权访问", null); |
||||
|
|
||||
|
return R.success({ |
||||
|
...updated, |
||||
|
tags: deserializeTags(updated.tags), |
||||
|
}); |
||||
|
}); |
||||
@ -0,0 +1,20 @@ |
|||||
|
import { listDocuments, type AgentDocumentType } from "../../service/agent-document"; |
||||
|
import { requireUser } from "#server/utils/context"; |
||||
|
|
||||
|
export default defineWrappedResponseHandler(async (event) => { |
||||
|
const user = await requireUser(event); |
||||
|
const query = getQuery(event); |
||||
|
const page = Math.max(1, parseInt(String(query.page || "1"))); |
||||
|
const pageSize = Math.min(100, Math.max(1, parseInt(String(query.pageSize || "20")))); |
||||
|
const type = query.type as AgentDocumentType | undefined; |
||||
|
const keyword = query.keyword as string | undefined; |
||||
|
|
||||
|
const result = await listDocuments({ |
||||
|
userId: user.id, |
||||
|
type, |
||||
|
keyword, |
||||
|
page, |
||||
|
pageSize, |
||||
|
}); |
||||
|
return R.success(result); |
||||
|
}); |
||||
Loading…
Reference in new issue