generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 1.6.1 * Update PDFExtractorAction.kt * wip * wip
- Loading branch information
1 parent
831a440
commit d7ee5ea
Showing
8 changed files
with
167 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ain/kotlin/com/github/simiacryptus/aicoder/actions/generic/DocumentDataExtractorAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.github.simiacryptus.aicoder.actions.generic | ||
|
||
import com.github.simiacryptus.aicoder.AppServer | ||
import com.github.simiacryptus.aicoder.actions.BaseAction | ||
import com.github.simiacryptus.aicoder.util.UITools | ||
import com.intellij.openapi.actionSystem.ActionUpdateThread | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.simiacryptus.skyenet.apps.general.DocumentParserApp | ||
import com.simiacryptus.skyenet.core.platform.StorageInterface | ||
import com.simiacryptus.skyenet.core.platform.file.DataStorage | ||
import org.slf4j.LoggerFactory | ||
import java.awt.Desktop | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.simiacryptus.skyenet.core.platform.Session | ||
import java.io.File | ||
|
||
class DocumentDataExtractorAction : BaseAction() { | ||
val path = "/pdfExtractor" | ||
private var settings = DocumentParserApp.Settings() | ||
|
||
override fun getActionUpdateThread() = ActionUpdateThread.BGT | ||
|
||
override fun isEnabled(event: AnActionEvent): Boolean { | ||
if(!super.isEnabled(event)) return false | ||
val selectedFile = UITools.getSelectedFile(event) | ||
return selectedFile != null && ( | ||
selectedFile.name.endsWith(".pdf", ignoreCase = true) || | ||
selectedFile.name.endsWith(".txt", ignoreCase = true) || | ||
selectedFile.name.endsWith(".md", ignoreCase = true) || | ||
selectedFile.name.endsWith(".html", ignoreCase = true) | ||
) | ||
} | ||
|
||
override fun handle(e: AnActionEvent) { | ||
val selectedFile = UITools.getSelectedFile(e) | ||
if (selectedFile == null || (!selectedFile.name.endsWith( | ||
".pdf", | ||
ignoreCase = true | ||
) && !selectedFile.name.endsWith(".txt", ignoreCase = true)) | ||
) { | ||
UITools.showErrorDialog(e.project, "Please select a PDF or text file.", "Invalid Selection") | ||
return | ||
} | ||
val configDialog = DocumentDataExtractorConfigDialog(e.project, settings) | ||
if (!configDialog.showAndGet()) return | ||
settings = configDialog.settings | ||
|
||
|
||
val session = StorageInterface.newGlobalID() | ||
val pdfFile = selectedFile.toFile | ||
DataStorage.sessionPaths[session] = pdfFile.parentFile | ||
|
||
val documentParserApp = object : DocumentParserApp( | ||
applicationName = "Document Extractor", | ||
path = path, | ||
api = api, | ||
fileInput = pdfFile.toPath(), | ||
) { | ||
override fun <T : Any> initSettings(session: Session): T = settings as T | ||
override val root: File get() = selectedFile.parent.toFile | ||
} | ||
|
||
SessionProxyServer.chats[session] = documentParserApp | ||
val server = AppServer.getServer(e.project) | ||
Thread { | ||
Thread.sleep(500) | ||
try { | ||
val uri = server.server.uri.resolve("/#$session") | ||
BaseAction.log.info("Opening browser to $uri") | ||
Desktop.getDesktop().browse(uri) | ||
} catch (e: Throwable) { | ||
BaseAction.log.warn("Error opening browser", e) | ||
} | ||
}.start() | ||
} | ||
|
||
companion object { | ||
private val log = LoggerFactory.getLogger(DocumentDataExtractorAction::class.java) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...tlin/com/github/simiacryptus/aicoder/actions/generic/DocumentDataExtractorConfigDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.simiacryptus.aicoder.actions.generic | ||
|
||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.ui.DialogWrapper | ||
import com.intellij.ui.components.JBCheckBox | ||
import com.intellij.ui.components.JBTextField | ||
import com.simiacryptus.skyenet.apps.general.DocumentParserApp | ||
import javax.swing.* | ||
|
||
class DocumentDataExtractorConfigDialog( | ||
project: Project?, | ||
var settings: DocumentParserApp.Settings | ||
) : DialogWrapper(project) { | ||
|
||
private val dpiField = JBTextField(settings.dpi.toString()) | ||
private val maxPagesField = JBTextField(settings.maxPages.toString()) | ||
private val outputFormatField = JBTextField(settings.outputFormat) | ||
private val pagesPerBatchField = JBTextField(settings.pagesPerBatch.toString()) | ||
private val showImagesCheckbox = JBCheckBox("Show Images", settings.showImages) | ||
private val saveImageFilesCheckbox = JBCheckBox("Save Image Files", settings.saveImageFiles) | ||
private val saveTextFilesCheckbox = JBCheckBox("Save Text Files", settings.saveTextFiles) | ||
private val saveFinalJsonCheckbox = JBCheckBox("Save Final JSON", settings.saveFinalJson) | ||
|
||
init { | ||
init() | ||
title = "Configure Document Data Extractor" | ||
} | ||
|
||
override fun createCenterPanel(): JComponent { | ||
val panel = JPanel() | ||
panel.layout = BoxLayout(panel, BoxLayout.Y_AXIS) | ||
|
||
panel.add(createLabeledField("DPI:", dpiField)) | ||
panel.add(createLabeledField("Max Pages:", maxPagesField)) | ||
panel.add(createLabeledField("Output Format:", outputFormatField)) | ||
panel.add(createLabeledField("Pages Per Batch:", pagesPerBatchField)) | ||
panel.add(showImagesCheckbox) | ||
panel.add(saveImageFilesCheckbox) | ||
panel.add(saveTextFilesCheckbox) | ||
panel.add(saveFinalJsonCheckbox) | ||
|
||
return panel | ||
} | ||
|
||
private fun createLabeledField(label: String, field: JComponent): JPanel { | ||
val panel = JPanel() | ||
panel.layout = BoxLayout(panel, BoxLayout.X_AXIS) | ||
panel.add(JLabel(label)) | ||
panel.add(Box.createHorizontalStrut(10)) | ||
panel.add(field) | ||
return panel | ||
} | ||
|
||
override fun doOKAction() { | ||
settings = DocumentParserApp.Settings( | ||
dpi = dpiField.text.toFloatOrNull() ?: settings.dpi, | ||
maxPages = maxPagesField.text.toIntOrNull() ?: settings.maxPages, | ||
outputFormat = outputFormatField.text, | ||
pagesPerBatch = pagesPerBatchField.text.toIntOrNull() ?: settings.pagesPerBatch, | ||
showImages = showImagesCheckbox.isSelected, | ||
saveImageFiles = saveImageFilesCheckbox.isSelected, | ||
saveTextFiles = saveTextFilesCheckbox.isSelected, | ||
saveFinalJson = saveFinalJsonCheckbox.isSelected | ||
) | ||
super.doOKAction() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
src/main/kotlin/com/simiacryptus/jopenai/exceptions/CustomException.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters