001/* 002 * Copyright 2025 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.workspaces.documents.observers; 017 018import java.util.Map; 019 020import org.apache.avalon.framework.service.ServiceException; 021import org.apache.avalon.framework.service.ServiceManager; 022import org.apache.avalon.framework.service.Serviceable; 023import org.apache.commons.lang3.StringUtils; 024 025import org.ametys.core.observation.AsyncObserver; 026import org.ametys.core.observation.Event; 027import org.ametys.plugins.explorer.ObservationConstants; 028import org.ametys.plugins.explorer.resources.Resource; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.plugins.workspaces.WorkspacesHelper; 031import org.ametys.plugins.workspaces.WorkspacesHelper.FileType; 032import org.ametys.plugins.workspaces.documents.WorkspaceFileAIHelper; 033import org.ametys.plugins.workspaces.documents.jcr.File; 034import org.ametys.runtime.plugin.component.AbstractLogEnabled; 035 036/** 037 * This observer listens to document creation or update events and automatically 038 * generates a summary using an AI provider, storing it in the resource's metadata. 039 */ 040public class FileAISummaryObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable 041{ 042 043 /** The Ametys object resolver */ 044 protected AmetysObjectResolver _resolver; 045 046 private WorkspacesHelper _workspaceHelper; 047 048 private WorkspaceFileAIHelper _workspaceFileAIHelper; 049 050 public void service(ServiceManager manager) throws ServiceException 051 { 052 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 053 _workspaceHelper = (WorkspacesHelper) manager.lookup(WorkspacesHelper.ROLE); 054 _workspaceFileAIHelper = (WorkspaceFileAIHelper) manager.lookup(WorkspaceFileAIHelper.ROLE); 055 } 056 057 public int getPriority() 058 { 059 return MAX_PRIORITY; 060 } 061 062 public boolean supports(Event event) 063 { 064 boolean isEnabled = _workspaceFileAIHelper.isAutomaticGenerationEnabled(); 065 return isEnabled 066 && ( 067 event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED) 068 || event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED) 069 ); 070 } 071 072 public void observe(Event event, Map<String, Object> transientVars) throws Exception 073 { 074 Map<String, Object> args = event.getArguments(); 075 076 if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_CREATED)) 077 { 078 @SuppressWarnings("unchecked") 079 Map<String, Resource> resources = (Map<String, Resource>) args.get(ObservationConstants.ARGS_RESOURCES); 080 for (Resource resource : resources.values()) 081 { 082 // Resolve again the resource because JCR session can be lost 083 Resource addedResource = (Resource) _resolver.resolveById(resource.getId()); 084 _generateSummaryForResource(addedResource); 085 } 086 } 087 else if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_UPDATED)) 088 { 089 String resourceId = (String) args.get(ObservationConstants.ARGS_ID); 090 Resource resource = (Resource) _resolver.resolveById(resourceId); 091 _generateSummaryForResource(resource); 092 } 093 } 094 095 private void _generateSummaryForResource(Resource resource) throws Exception 096 { 097 FileType fileType = _workspaceHelper.getFileType(resource); 098 099 if ((fileType == FileType.TEXT || fileType == FileType.PDF) 100 && resource instanceof File file 101 && (file.isAutomaticallyGeneratedDescription() || StringUtils.isBlank(file.getDescription()))) // Only update files with automatic description or empty ones 102 { 103 _workspaceFileAIHelper.generateFileDescription(file); 104 } 105 } 106}