pre-process docs on upload

This commit is contained in:
DarioGii
2025-11-15 20:07:31 +00:00
committed by Dario Ghunney Ware
parent f4b39101ac
commit 8139987919
5 changed files with 435 additions and 205 deletions

View File

@@ -45,21 +45,7 @@ public class ChatbotController {
@RequestBody ChatbotSessionCreateRequest request) {
ChatbotSession session = chatbotService.createSession(request);
ChatbotSettings settings = featureProperties.current();
ChatbotSessionResponse response =
ChatbotSessionResponse.builder()
.sessionId(session.getSessionId())
.documentId(session.getDocumentId())
.alphaWarning(settings.alphaWarning())
.ocrRequested(session.isOcrRequested())
.imageContentDetected(session.isImageContentDetected())
.textCharacters(session.getTextCharacters())
.estimatedTokens(session.getEstimatedTokens())
.maxCachedCharacters(cacheService.getMaxDocumentCharacters())
.createdAt(session.getCreatedAt())
.warnings(sessionWarnings(settings, session))
.metadata(session.getMetadata())
.usageSummary(session.getUsageSummary())
.build();
ChatbotSessionResponse response = toResponse(session, settings);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
@@ -76,24 +62,21 @@ public class ChatbotController {
sessionRegistry
.findById(sessionId)
.orElseThrow(() -> new ChatbotException("Session not found"));
ChatbotSessionResponse response =
ChatbotSessionResponse.builder()
.sessionId(session.getSessionId())
.documentId(session.getDocumentId())
.alphaWarning(settings.alphaWarning())
.ocrRequested(session.isOcrRequested())
.imageContentDetected(session.isImageContentDetected())
.textCharacters(session.getTextCharacters())
.estimatedTokens(session.getEstimatedTokens())
.maxCachedCharacters(cacheService.getMaxDocumentCharacters())
.createdAt(session.getCreatedAt())
.warnings(sessionWarnings(settings, session))
.metadata(session.getMetadata())
.usageSummary(session.getUsageSummary())
.build();
ChatbotSessionResponse response = toResponse(session, settings);
return ResponseEntity.ok(response);
}
@GetMapping("/document/{documentId}")
public ResponseEntity<ChatbotSessionResponse> getSessionByDocument(
@PathVariable String documentId) {
ChatbotSettings settings = featureProperties.current();
ChatbotSession session =
sessionRegistry
.findByDocumentId(documentId)
.orElseThrow(() -> new ChatbotException("Session not found"));
return ResponseEntity.ok(toResponse(session, settings));
}
@DeleteMapping("/session/{sessionId}")
public ResponseEntity<Void> closeSession(@PathVariable String sessionId) {
chatbotService.close(sessionId);
@@ -123,4 +106,21 @@ public class ChatbotController {
return warnings;
}
private ChatbotSessionResponse toResponse(ChatbotSession session, ChatbotSettings settings) {
return ChatbotSessionResponse.builder()
.sessionId(session.getSessionId())
.documentId(session.getDocumentId())
.alphaWarning(settings.alphaWarning())
.ocrRequested(session.isOcrRequested())
.imageContentDetected(session.isImageContentDetected())
.textCharacters(session.getTextCharacters())
.estimatedTokens(session.getEstimatedTokens())
.maxCachedCharacters(cacheService.getMaxDocumentCharacters())
.createdAt(session.getCreatedAt())
.warnings(sessionWarnings(settings, session))
.metadata(session.getMetadata())
.usageSummary(session.getUsageSummary())
.build();
}
}

View File

@@ -12,9 +12,13 @@ import stirling.software.proprietary.model.chatbot.ChatbotSession;
public class ChatbotSessionRegistry {
private final Map<String, ChatbotSession> sessionStore = new ConcurrentHashMap<>();
private final Map<String, String> documentToSession = new ConcurrentHashMap<>();
public void register(ChatbotSession session) {
sessionStore.put(session.getSessionId(), session);
if (session.getDocumentId() != null) {
documentToSession.put(session.getDocumentId(), session.getSessionId());
}
}
public Optional<ChatbotSession> findById(String sessionId) {
@@ -22,6 +26,16 @@ public class ChatbotSessionRegistry {
}
public void remove(String sessionId) {
sessionStore.remove(sessionId);
Optional.ofNullable(sessionStore.remove(sessionId))
.map(ChatbotSession::getDocumentId)
.ifPresent(documentToSession::remove);
}
public Optional<ChatbotSession> findByDocumentId(String documentId) {
return Optional.ofNullable(documentToSession.get(documentId)).flatMap(this::findById);
}
public void removeByDocumentId(String documentId) {
Optional.ofNullable(documentToSession.remove(documentId)).ifPresent(sessionStore::remove);
}
}