Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion apps/api/src/modules/ai/drafts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type CreateDraftRequest = Request<Record<string, string>, unknown, CreateAiDraft
type UpdateDraftRequest = Request<AiDraftIdParamsDto, unknown, UpdateAiDraftDto>;
type ApproveDraftRequest = Request<AiDraftIdParamsDto, unknown, ApproveAiDraftDto>;
type DraftByIdRequest = Request<AiDraftIdParamsDto>;
type DraftListRequest = Request<Record<string, string>, unknown, unknown, { encounterId?: string }>;

const toDraftPayload = (doc: {
_id: unknown;
Expand Down Expand Up @@ -69,6 +70,34 @@ router.post(
},
);

router.get(
"/drafts",
authorize(ALL_ROLES),
async (req: DraftListRequest, res: Response) => {
const clinicId = req.user?.clinicId;
if (!clinicId) {
return res.status(401).json({
error: "Unauthorized",
message: "Authentication required",
});
}

const drafts = await AiDraftModel.find({
clinicId,
...(req.query.encounterId ? { encounterId: req.query.encounterId } : {}),
status: "DRAFT",
})
.sort({ updatedAt: -1 })
.limit(20)
.lean();

return res.json({
status: "success",
data: drafts.map((draft) => toDraftPayload(draft as Parameters<typeof toDraftPayload>[0])),
});
},
);

router.patch(
"/drafts/:id",
authorize(ALL_ROLES),
Expand Down Expand Up @@ -185,4 +214,3 @@ router.post(
);

export const aiDraftRoutes = router;

36 changes: 34 additions & 2 deletions apps/web/components/ai/Summarizer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { apiFetch } from "@/lib/api-client";

type Props = {
Expand Down Expand Up @@ -54,6 +54,33 @@ export const Summarizer = ({ encounterId }: Props) => {
[content, draftId, isGenerating, isSaving, mode],
);

useEffect(() => {
const loadDraft = async () => {
try {
const response = await apiFetch(`/ai/drafts?encounterId=${encodeURIComponent(encounterId)}`);
if (!response.ok) {
return;
}

const payload = (await response.json()) as {
data?: Array<{ id: string; content: string }>;
};
const existingDraft = payload.data?.[0];
if (!existingDraft) {
return;
}

setDraftId(existingDraft.id);
setContent(existingDraft.content);
setMode("draft");
} catch {
// Ignore draft bootstrap failures.
}
};

void loadDraft();
}, [encounterId]);

const startGeneration = async (useDummyStream = false) => {
if (isGenerating || isSaving) {
return;
Expand Down Expand Up @@ -173,6 +200,12 @@ export const Summarizer = ({ encounterId }: Props) => {
setIsSaving(true);

try {
await apiFetch(`/ai/drafts/${encodeURIComponent(draftId)}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ content: content.trim() }),
});

const response = await apiFetch(`/ai/drafts/${encodeURIComponent(draftId)}/approve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand Down Expand Up @@ -288,4 +321,3 @@ export const Summarizer = ({ encounterId }: Props) => {
</section>
);
};

Loading