2026-10-13 00:03:21 +08:00
|
|
|
import { degreeToSecond } from '@shumai/core/src/utils/model3d'
|
2026-11-11 16:05:18 +08:00
|
|
|
import { prisma } from '@shumai/db'
|
|
|
|
|
import { getShumaiMessage } from '@shumai/dtos'
|
|
|
|
|
import { HTTPException } from 'hono/http-exception'
|
|
|
|
|
import { agentSessionService } from '../agent/session-service'
|
2026-09-02 15:17:56 +08:00
|
|
|
|
2026-10-27 08:09:10 +08:00
|
|
|
/**
|
|
|
|
|
* Resolve the visual markup referenced by an annotation ID: a comment (comments mode) or an
|
|
|
|
|
* agent session entry (1-on-1 chat mode). The annotation must belong to `assetId`.
|
|
|
|
|
*/
|
2026-09-02 15:17:56 +08:00
|
|
|
export async function resolveAnnotationsById(
|
|
|
|
|
assetId: string,
|
2026-09-02 21:51:07 +08:00
|
|
|
annotationId?: string | null,
|
2026-09-02 15:17:56 +08:00
|
|
|
): Promise<{ annotations: PrismaJson.AnnotationList | null; timestamp: number | null }> {
|
|
|
|
|
if (!annotationId) {
|
|
|
|
|
return { annotations: null, timestamp: null }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Check AssetComment table (Comments Mode)
|
|
|
|
|
const comment = await prisma.assetComment.findUnique({
|
|
|
|
|
where: { id: annotationId },
|
|
|
|
|
select: { assetId: true, annotation: true, second: true },
|
|
|
|
|
})
|
|
|
|
|
if (comment) {
|
|
|
|
|
if (comment.assetId !== assetId) {
|
2026-10-27 08:09:10 +08:00
|
|
|
throw new HTTPException(400, {
|
|
|
|
|
message: `Annotation "${annotationId}" belongs to asset "${comment.assetId}", not target asset "${assetId}".`,
|
|
|
|
|
})
|
2026-09-02 15:17:56 +08:00
|
|
|
}
|
|
|
|
|
const list = comment.annotation
|
|
|
|
|
const annotations =
|
|
|
|
|
Array.isArray(list) && list.length > 0 ? (list as PrismaJson.AnnotationList) : null
|
|
|
|
|
return { annotations, timestamp: comment.second }
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-11 16:05:18 +08:00
|
|
|
const reference = /^chat:([^:]+):(.+)$/.exec(annotationId)
|
|
|
|
|
const entry = reference
|
|
|
|
|
? await agentSessionService.getEntryForRequest(reference[1], reference[2])
|
|
|
|
|
: undefined
|
2026-09-02 15:17:56 +08:00
|
|
|
if (entry) {
|
2026-11-11 16:05:18 +08:00
|
|
|
const details = getShumaiMessage(entry)?.context
|
|
|
|
|
const entryAssetId = details?.currentAsset?.id
|
2026-09-02 15:17:56 +08:00
|
|
|
if (entryAssetId && entryAssetId !== assetId) {
|
2026-10-27 08:09:10 +08:00
|
|
|
throw new HTTPException(400, {
|
|
|
|
|
message: `Annotation "${annotationId}" belongs to asset "${entryAssetId}", not target asset "${assetId}".`,
|
|
|
|
|
})
|
2026-09-02 15:17:56 +08:00
|
|
|
}
|
|
|
|
|
const list = details?.annotations
|
|
|
|
|
const annotations =
|
|
|
|
|
Array.isArray(list) && list.length > 0 ? (list as unknown as PrismaJson.AnnotationList) : null
|
2026-10-13 00:03:21 +08:00
|
|
|
let timestamp: number | null = null
|
|
|
|
|
if (details?.position?.type === 'time') {
|
|
|
|
|
timestamp = details.position.seconds
|
|
|
|
|
} else if (details?.position?.type === 'degree') {
|
|
|
|
|
timestamp = degreeToSecond(details.position.degree)
|
|
|
|
|
} else if (details?.position?.type === 'page') {
|
|
|
|
|
timestamp = details.position.page
|
|
|
|
|
}
|
2026-09-02 15:17:56 +08:00
|
|
|
return { annotations, timestamp }
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-27 08:09:10 +08:00
|
|
|
throw new HTTPException(404, { message: `Annotation with ID "${annotationId}" not found.` })
|
2026-09-02 15:17:56 +08:00
|
|
|
}
|