2026-07-31 04:41:02 +00:00
package app
import (
"context"
"fmt"
"gitea.maximumdirect.net/eric/weatherreporter/internal/briefing"
"gitea.maximumdirect.net/eric/weatherreporter/internal/collect"
"gitea.maximumdirect.net/eric/weatherreporter/internal/fileutil"
"gitea.maximumdirect.net/eric/weatherreporter/internal/generatedtext"
2026-07-31 17:08:33 +00:00
"gitea.maximumdirect.net/eric/weatherreporter/internal/module"
2026-08-01 19:35:12 +00:00
"gitea.maximumdirect.net/eric/weatherreporter/internal/promptdebug"
2026-07-31 04:41:02 +00:00
"gitea.maximumdirect.net/eric/weatherreporter/internal/promptexec"
"gitea.maximumdirect.net/eric/weatherreporter/internal/promptinput"
"gitea.maximumdirect.net/eric/weatherreporter/internal/report"
2026-08-01 19:52:22 +00:00
"gitea.maximumdirect.net/eric/weatherreporter/internal/weatherdata"
2026-07-31 04:41:02 +00:00
)
type promptReportRequest struct {
GenerateRequest
2026-07-31 04:48:16 +00:00
Resolved report . Resolved
Collection collect . Result
Inspection PromptInspectionResult
2026-08-01 19:35:12 +00:00
DebugWriter * promptdebug . PromptDebugWriter
2026-08-01 19:52:22 +00:00
Result * ReportResult
2026-07-31 04:56:48 +00:00
noNotify bool
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
type promptReportWorkflow struct {
ctx context . Context
req promptReportRequest
result * ReportResult
briefingMetadata briefing . Metadata
reportFacts ReportFacts
moduleSnapshot module . Snapshot
dataPackage [ ] byte
handler generatedtext . Handler
debugRef promptdebug . PromptDebugRef
callbackFailed bool
}
2026-07-31 04:41:02 +00:00
func generatePromptReport ( ctx context . Context , req promptReportRequest ) ( * ReportResult , error ) {
2026-07-31 17:08:33 +00:00
workflow , err := newPromptReportWorkflow ( ctx , req )
if err != nil {
return nil , err
}
if err := workflow . buildInputs ( ) ; err != nil {
return workflow . result , err
}
2026-08-01 19:52:22 +00:00
execution , err := workflow . executePrompt ( )
if err != nil {
if workflow . callbackFailed {
return workflow . result , err
}
return workflow . result , workflow . reportError ( "execute prompt" , classifiedPromptError ( "prompt execution failed" , err ) )
2026-07-31 17:08:33 +00:00
}
if execution == nil {
2026-08-01 19:52:22 +00:00
return workflow . result , workflow . reportError ( "execute prompt" , promptexec . NewError ( promptexec . Generation , "prompt executor returned no execution" , nil ) )
2026-07-31 17:08:33 +00:00
}
2026-08-01 19:52:22 +00:00
workflow . result . ValidationStatus = execution . Validation . Status
if err := workflow . writeExecutionDebug ( * execution ) ; err != nil {
2026-07-31 17:08:33 +00:00
return workflow . result , err
}
if execution . Validation . Status != promptexec . ValidationPassed && execution . Validation . Status != promptexec . ValidationFailed {
2026-08-01 19:52:22 +00:00
return workflow . result , workflow . reportError ( "validate prompt execution" , promptexec . NewError ( promptexec . OperationalValidation , "prompt execution did not complete validation" , nil ) )
2026-07-31 17:08:33 +00:00
}
if execution . Validation . Status == promptexec . ValidationFailed {
2026-08-01 19:52:22 +00:00
return workflow . result , workflow . reportError ( "validate prompt execution" , promptexec . NewError ( promptexec . ValidationRejected , "prompt output did not satisfy its schema" , nil ) )
2026-07-31 17:08:33 +00:00
}
2026-08-01 19:52:22 +00:00
return workflow . renderAndPublish ( execution . RawOutput )
2026-07-31 17:08:33 +00:00
}
func newPromptReportWorkflow ( ctx context . Context , req promptReportRequest ) ( * promptReportWorkflow , error ) {
if req . Collection . Bundle == nil {
2026-07-31 04:41:02 +00:00
return nil , fmt . Errorf ( "collected weather bundle is required" )
}
2026-08-01 19:52:22 +00:00
result := req . Result
if result == nil {
result = initialReportResult ( req . GenerateRequest , req . Resolved , req . Inspection )
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
return & promptReportWorkflow {
ctx : ctx , req : req ,
result : result ,
} , nil
2026-07-31 17:08:33 +00:00
}
2026-08-01 19:52:22 +00:00
func initialReportResult ( req GenerateRequest , resolved report . Resolved , inspection PromptInspectionResult ) * ReportResult {
metadata := resolved . Metadata ( )
return & ReportResult {
ReportID : resolved . Definition . ID , ReportName : resolved . Definition . Name ,
PromptID : resolved . Definition . PromptID , PromptVersion : resolved . Definition . PromptVersion ,
RunID : metadata . RunID , GeneratedAt : metadata . GeneratedAt , Timezone : req . Config . WeatherAPI . Timezone ,
ValidPeriod : metadata . ValidPeriod ,
ProfileID : inspection . ProfileID , BackendID : inspection . BackendID , ModelName : inspection . ModelName ,
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
}
func ( w * promptReportWorkflow ) buildInputs ( ) error {
var err error
2026-07-31 17:08:33 +00:00
w . reportFacts , err = BuildReportFacts ( ModuleSnapshotRequest { Config : w . req . Config , Resolved : w . req . Resolved } , w . req . Collection . Bundle )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . reportError ( "build report facts" , err )
2026-07-31 04:41:02 +00:00
}
2026-07-31 17:08:33 +00:00
w . moduleSnapshot , err = BuildModuleSnapshotFromFacts ( ModuleSnapshotRequest { Config : w . req . Config , Resolved : w . req . Resolved } , w . reportFacts )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . reportError ( "build module snapshot" , err )
2026-07-31 04:41:02 +00:00
}
2026-07-31 17:08:33 +00:00
w . briefingMetadata = briefing . BuildMetadata ( briefingBuildContext ( w . req . Config , w . req . Resolved , w . reportFacts . Collected ) )
2026-08-01 19:52:22 +00:00
w . result . SourceWarnings = append ( [ ] weatherdata . SourceWarning ( nil ) , w . briefingMetadata . SourceWarnings ... )
dataPackage , err := promptinput . Build ( promptinput . BuildRequest { Metadata : promptMetadata ( w . briefingMetadata ) , Modules : w . moduleSnapshot } )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-07-31 17:08:33 +00:00
return w . reportError ( "build data package" , err )
}
2026-08-01 19:52:22 +00:00
w . dataPackage , err = promptinput . MarshalYAML ( dataPackage )
2026-07-31 17:08:33 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . reportError ( "marshal data package" , err )
2026-07-31 04:41:02 +00:00
}
2026-07-31 17:08:33 +00:00
w . handler , err = generatedtext . LookupDefinition ( w . req . Resolved . Definition )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-07-31 17:08:33 +00:00
return w . reportError ( "lookup generated text catalog" , err )
}
2026-08-01 19:52:22 +00:00
w . debugRef = promptdebug . PromptDebugRef { ReportID : w . result . ReportID , ValidDate : w . req . Resolved . ValidPeriod . Start . Format ( "2006-01-02" ) , RunID : w . result . RunID }
2026-07-31 17:08:33 +00:00
return nil
}
func ( w * promptReportWorkflow ) executePrompt ( ) ( * promptexec . Execution , error ) {
2026-08-01 19:52:22 +00:00
captureDebug := w . req . DebugWriter != nil && w . req . DebugWriter . Enabled ( )
return w . req . Executor . Execute ( w . ctx , promptexec . ExecuteRequest { PromptID : w . req . Inspection . PromptID , PromptVersion : w . req . Inspection . PromptVersion , ProfileID : w . req . Inspection . ProfileID , DataPackage : w . dataPackage , CaptureDebug : captureDebug } , w . writePreparationDebug )
2026-07-31 17:08:33 +00:00
}
2026-07-31 04:41:02 +00:00
2026-08-01 19:52:22 +00:00
func ( w * promptReportWorkflow ) writePreparationDebug ( preparation promptexec . Preparation , debug * promptexec . PreparationDebug ) error {
w . result . ProfileID , w . result . BackendID , w . result . ModelName = preparation . ProfileID , preparation . BackendID , preparation . ModelName
if w . req . DebugWriter == nil {
return nil
2026-07-31 17:08:33 +00:00
}
2026-08-01 19:52:22 +00:00
path , err := w . req . DebugWriter . WritePreparation ( w . debugRef , preparation , debug )
2026-07-31 17:08:33 +00:00
if err != nil {
w . callbackFailed = true
return promptDebugWriteError ( err )
}
2026-08-01 19:52:22 +00:00
w . result . LLMDebugPath = path
2026-07-31 17:08:33 +00:00
return nil
}
2026-07-31 04:41:02 +00:00
2026-08-01 19:52:22 +00:00
func ( w * promptReportWorkflow ) writeExecutionDebug ( execution promptexec . Execution ) error {
if w . req . DebugWriter == nil {
return nil
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
path , err := w . req . DebugWriter . WriteExecution ( w . debugRef , execution )
2026-07-31 17:08:33 +00:00
if err != nil {
return w . reportError ( "write prompt debug" , promptDebugWriteError ( err ) )
2026-07-31 04:48:16 +00:00
}
2026-08-01 19:52:22 +00:00
if path != "" {
w . result . LLMDebugPath = path
2026-07-31 04:48:16 +00:00
}
2026-07-31 17:08:33 +00:00
return nil
}
2026-07-31 04:41:02 +00:00
2026-08-01 19:52:22 +00:00
func ( w * promptReportWorkflow ) renderAndPublish ( raw [ ] byte ) ( * ReportResult , error ) {
generatedText , _ , err := w . handler . Validate ( raw )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . result , w . reportError ( "validate generated text" , err )
2026-07-31 16:51:12 +00:00
}
2026-07-31 17:08:33 +00:00
renderContext , err := w . handler . BuildRenderContext ( w . briefingMetadata , w . moduleSnapshot , w . reportFacts . Collected , w . reportFacts . Derived , generatedText )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . result , w . reportError ( "build render context" , err )
2026-07-31 16:51:12 +00:00
}
2026-07-31 17:08:33 +00:00
rendered , err := w . handler . Render ( renderContext )
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . result , w . reportError ( "render template" , err )
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
if err := fileutil . WriteFileAtomic ( w . req . OutputPath , rendered ) ; err != nil {
2026-07-31 17:08:33 +00:00
return w . result , err
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
w . result . OutputPath = w . req . OutputPath
if w . req . noNotify {
return w . result , nil
2026-07-31 17:08:33 +00:00
}
2026-08-01 19:52:22 +00:00
notification , err := notifyReport ( w . ctx , w . req . Config , w . req . Resolved , w . result . OutputPath , w . result . RunID , w . result . GeneratedAt , w . req . Notifier )
w . result . Notification = notification
2026-07-31 04:41:02 +00:00
if err != nil {
2026-08-01 19:52:22 +00:00
return w . result , err
2026-07-31 04:41:02 +00:00
}
2026-08-01 19:52:22 +00:00
return w . result , nil
2026-07-31 17:08:33 +00:00
}
func ( w * promptReportWorkflow ) reportError ( operation string , err error ) error {
2026-08-01 19:52:22 +00:00
return generatedReportError ( w . req . Resolved , w . result . RunID , operation , err )
2026-07-31 04:41:02 +00:00
}
func classifiedPromptError ( operation string , err error ) error {
if promptexec . CategoryOf ( err ) != "" {
return err
}
return promptexec . NewError ( promptexec . Generation , operation , err )
}
2026-07-31 04:48:16 +00:00
func promptDebugWriteError ( err error ) error {
return promptexec . NewError ( promptexec . InvalidConfiguration , "write requested prompt debug artifact" , err )
}