You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

40 lines
1.2 KiB

import { createDocument, deserializeTags, type AgentDocumentType } from "../../service/agent-document";
import { getAgentBySlug } from "../../service/agent/agent";
import { requireUser } from "#server/utils/context";
import { validate } from "#server/utils/validation";
import { z } from "zod";
const createSchema = z.object({
title: z.string().min(1).max(200),
content: z.string().min(1).max(50000),
type: z.enum(["memory", "document"]).optional(),
tags: z.array(z.string()).optional(),
sessionId: z.string().optional(),
agentSlug: z.string().optional(),
});
export default defineWrappedResponseHandler(async (event) => {
const user = await requireUser(event);
const body = await validate(createSchema, await readBody(event));
let agentId: number | null = null;
if (body.agentSlug) {
const agent = await getAgentBySlug(body.agentSlug);
agentId = agent?.id ?? null;
}
const created = await createDocument({
userId: user.id,
agentId,
sessionId: body.sessionId ?? null,
type: body.type as AgentDocumentType | undefined,
title: body.title,
content: body.content,
tags: body.tags ?? null,
});
return R.success({
...created,
tags: deserializeTags(created.tags),
});
});