feat: add generation source handling for task creation and updates
All checks were successful
Build And Publish Production Image / Build And Publish Production Image (push) Successful in 50s
All checks were successful
Build And Publish Production Image / Build And Publish Production Image (push) Successful in 50s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.condado.newsletter.dto
|
||||
|
||||
import com.condado.newsletter.model.EntityTask
|
||||
import com.condado.newsletter.model.TaskGenerationSource
|
||||
import jakarta.validation.constraints.NotBlank
|
||||
import jakarta.validation.constraints.NotNull
|
||||
import java.time.LocalDateTime
|
||||
@@ -11,7 +12,8 @@ data class EntityTaskCreateDto(
|
||||
@field:NotBlank val name: String,
|
||||
val prompt: String,
|
||||
@field:NotBlank val scheduleCron: String,
|
||||
@field:NotBlank val emailLookback: String
|
||||
@field:NotBlank val emailLookback: String,
|
||||
val generationSource: TaskGenerationSource = TaskGenerationSource.LLAMA
|
||||
)
|
||||
|
||||
data class EntityTaskUpdateDto(
|
||||
@@ -19,7 +21,8 @@ data class EntityTaskUpdateDto(
|
||||
@field:NotBlank val name: String,
|
||||
@field:NotBlank val prompt: String,
|
||||
@field:NotBlank val scheduleCron: String,
|
||||
@field:NotBlank val emailLookback: String
|
||||
@field:NotBlank val emailLookback: String,
|
||||
val generationSource: TaskGenerationSource? = null
|
||||
)
|
||||
|
||||
data class EntityTaskResponseDto(
|
||||
@@ -29,6 +32,7 @@ data class EntityTaskResponseDto(
|
||||
val prompt: String,
|
||||
val scheduleCron: String,
|
||||
val emailLookback: String,
|
||||
val generationSource: TaskGenerationSource,
|
||||
val active: Boolean,
|
||||
val createdAt: LocalDateTime?
|
||||
) {
|
||||
@@ -41,6 +45,7 @@ data class EntityTaskResponseDto(
|
||||
prompt = task.prompt,
|
||||
scheduleCron = task.scheduleCron,
|
||||
emailLookback = task.emailLookback,
|
||||
generationSource = task.generationSource,
|
||||
active = task.active,
|
||||
createdAt = task.createdAt
|
||||
)
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.condado.newsletter.model
|
||||
import jakarta.persistence.CascadeType
|
||||
import jakarta.persistence.Column
|
||||
import jakarta.persistence.Entity
|
||||
import jakarta.persistence.EnumType
|
||||
import jakarta.persistence.Enumerated
|
||||
import jakarta.persistence.FetchType
|
||||
import jakarta.persistence.GeneratedValue
|
||||
import jakarta.persistence.GenerationType
|
||||
@@ -37,6 +39,10 @@ class EntityTask(
|
||||
@Column(name = "email_lookback", nullable = false)
|
||||
val emailLookback: String,
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "generation_source", nullable = false)
|
||||
val generationSource: TaskGenerationSource = TaskGenerationSource.LLAMA,
|
||||
|
||||
@Column(nullable = false)
|
||||
val active: Boolean = true,
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.condado.newsletter.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator
|
||||
import com.fasterxml.jackson.annotation.JsonValue
|
||||
|
||||
enum class TaskGenerationSource(
|
||||
@get:JsonValue val value: String
|
||||
) {
|
||||
OPENAI("openai"),
|
||||
LLAMA("llama");
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@JsonCreator
|
||||
fun from(value: String): TaskGenerationSource =
|
||||
entries.firstOrNull { it.value.equals(value, ignoreCase = true) }
|
||||
?: throw IllegalArgumentException("Invalid generationSource: $value")
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ class EntityTaskService(
|
||||
prompt = dto.prompt,
|
||||
scheduleCron = dto.scheduleCron,
|
||||
emailLookback = dto.emailLookback,
|
||||
generationSource = dto.generationSource,
|
||||
active = true
|
||||
)
|
||||
|
||||
@@ -66,6 +67,7 @@ class EntityTaskService(
|
||||
prompt = dto.prompt,
|
||||
scheduleCron = dto.scheduleCron,
|
||||
emailLookback = dto.emailLookback,
|
||||
generationSource = dto.generationSource ?: existing.generationSource,
|
||||
active = existing.active,
|
||||
createdAt = existing.createdAt
|
||||
).apply { id = existing.id }
|
||||
@@ -83,6 +85,7 @@ class EntityTaskService(
|
||||
prompt = existing.prompt,
|
||||
scheduleCron = existing.scheduleCron,
|
||||
emailLookback = existing.emailLookback,
|
||||
generationSource = existing.generationSource,
|
||||
active = false,
|
||||
createdAt = existing.createdAt
|
||||
).apply { id = existing.id }
|
||||
@@ -100,6 +103,7 @@ class EntityTaskService(
|
||||
prompt = existing.prompt,
|
||||
scheduleCron = existing.scheduleCron,
|
||||
emailLookback = existing.emailLookback,
|
||||
generationSource = existing.generationSource,
|
||||
active = true,
|
||||
createdAt = existing.createdAt
|
||||
).apply { id = existing.id }
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.condado.newsletter.service
|
||||
import com.condado.newsletter.dto.GeneratedMessageHistoryResponseDto
|
||||
import com.condado.newsletter.dto.TaskPreviewGenerateRequestDto
|
||||
import com.condado.newsletter.model.GeneratedMessageHistory
|
||||
import com.condado.newsletter.model.TaskGenerationSource
|
||||
import com.condado.newsletter.repository.EntityTaskRepository
|
||||
import com.condado.newsletter.repository.GeneratedMessageHistoryRepository
|
||||
import org.springframework.stereotype.Service
|
||||
@@ -16,7 +17,8 @@ import java.util.UUID
|
||||
class TaskGeneratedMessageService(
|
||||
private val generatedMessageHistoryRepository: GeneratedMessageHistoryRepository,
|
||||
private val entityTaskRepository: EntityTaskRepository,
|
||||
private val llamaPreviewService: LlamaPreviewService
|
||||
private val llamaPreviewService: LlamaPreviewService,
|
||||
private val aiService: AiService
|
||||
) {
|
||||
|
||||
/** Lists persisted generated messages for a task. */
|
||||
@@ -25,15 +27,19 @@ class TaskGeneratedMessageService(
|
||||
.findAllByTask_IdOrderByCreatedAtDesc(taskId)
|
||||
.map { GeneratedMessageHistoryResponseDto.from(it) }
|
||||
|
||||
/**
|
||||
* Generates a new message using local Llama, persists it, and returns it.
|
||||
*/
|
||||
/** Generates a new message with the task-selected provider, persists it, and returns it. */
|
||||
@Transactional
|
||||
fun generateAndSave(taskId: UUID, request: TaskPreviewGenerateRequestDto): GeneratedMessageHistoryResponseDto {
|
||||
val task = entityTaskRepository.findById(taskId)
|
||||
.orElseThrow { IllegalArgumentException("Task not found: $taskId") }
|
||||
val prompt = buildPrompt(request)
|
||||
val generatedContent = llamaPreviewService.generate(prompt)
|
||||
val generatedContent = when (task.generationSource) {
|
||||
TaskGenerationSource.LLAMA -> llamaPreviewService.generate(prompt)
|
||||
TaskGenerationSource.OPENAI -> {
|
||||
val parsed = aiService.generate(prompt)
|
||||
"SUBJECT: ${parsed.subject}\nBODY:\n${parsed.body}"
|
||||
}
|
||||
}
|
||||
val nextLabel = "Message #${generatedMessageHistoryRepository.countByTask_Id(taskId) + 1}"
|
||||
|
||||
val saved = generatedMessageHistoryRepository.save(
|
||||
|
||||
Reference in New Issue
Block a user