AI translate refactor
This commit is contained in:
@@ -44,29 +44,72 @@ type StoredDocument struct {
|
||||
// TranslateRequest represents the request payload sent to the AI service
|
||||
// POST /ai/translate endpoint.
|
||||
type TranslateRequest struct {
|
||||
NoticeID string `json:"notice_id"` // required (TED-style notice key)
|
||||
ContractFolderID string `json:"contract_folder_id"` // required by AI service (procedure / MinIO prefix)
|
||||
NoticePublicationID string `json:"notice_publication_id"` // required by AI service
|
||||
Title string `json:"title"` // required
|
||||
Description string `json:"description"` // required
|
||||
Language string `json:"language,omitempty"` // optional target language override
|
||||
ContractFolderID string `json:"contract_folder_id"`
|
||||
NoticePublicationID string `json:"notice_publication_id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
// TranslateResponse represents the response from the AI translation API.
|
||||
// TranslateResponse represents the translation payload from POST /ai/translate.
|
||||
type TranslateResponse struct {
|
||||
NoticeID string `json:"notice_id"`
|
||||
ContractFolderID string `json:"contract_folder_id"`
|
||||
NoticePublicationID string `json:"notice_publication_id"`
|
||||
Language string `json:"language"`
|
||||
TranslatedTitle string `json:"translated_title"`
|
||||
TranslatedDescription string `json:"translated_description"`
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
// PipelineTranslateRequest represents the request payload for POST /pipeline/translate.
|
||||
type PipelineTranslateRequest struct {
|
||||
Languages []string `json:"languages"`
|
||||
}
|
||||
|
||||
// PipelineTranslateResponse represents the response from POST /pipeline/translate.
|
||||
type PipelineTranslateResponse struct {
|
||||
Status string `json:"status"`
|
||||
Languages []string `json:"languages"`
|
||||
}
|
||||
|
||||
// StoredTranslation is one language entry inside tender.json translations map.
|
||||
type StoredTranslation struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// TranslationStatus tracks which languages the async pipeline has finished.
|
||||
type TranslationStatus struct {
|
||||
Done []string `json:"done"`
|
||||
DoneUpper []string `json:"Done"`
|
||||
}
|
||||
|
||||
func (s TranslationStatus) languagesDone() []string {
|
||||
if len(s.Done) > 0 {
|
||||
return s.Done
|
||||
}
|
||||
return s.DoneUpper
|
||||
}
|
||||
|
||||
func languageInDone(done []string, language string) bool {
|
||||
lang := strings.ToLower(strings.TrimSpace(language))
|
||||
for _, item := range done {
|
||||
if strings.ToLower(strings.TrimSpace(item)) == lang {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TenderJSON represents the structure of the <notice_id>/tender.json file
|
||||
// stored in the AI service's MinIO bucket.
|
||||
type TenderJSON struct {
|
||||
OverallSummary *string `json:"overall_summary"`
|
||||
OverallSummaryCamel *string `json:"overallSummary"`
|
||||
SummarizeStatus SummarizeStatus `json:"summarize_status"`
|
||||
SummarizeStatusCamel SummarizeStatus `json:"summarizeStatus"`
|
||||
OverallSummary *string `json:"overall_summary"`
|
||||
OverallSummaryCamel *string `json:"overallSummary"`
|
||||
SummarizeStatus SummarizeStatus `json:"summarize_status"`
|
||||
SummarizeStatusCamel SummarizeStatus `json:"summarizeStatus"`
|
||||
Translations map[string]StoredTranslation `json:"translations"`
|
||||
TranslationStatus TranslationStatus `json:"translation_status"`
|
||||
TranslationStatusCamel TranslationStatus `json:"translationStatus"`
|
||||
}
|
||||
|
||||
// resolved returns whether summarization is marked done and the best-effort
|
||||
@@ -90,3 +133,33 @@ type SummarizeStatus struct {
|
||||
func (s SummarizeStatus) isDone() bool {
|
||||
return s.Done || s.DoneUpper
|
||||
}
|
||||
|
||||
// translationsDone returns languages marked complete by the translation pipeline.
|
||||
func (t *TenderJSON) translationsDone() []string {
|
||||
done := t.TranslationStatus.languagesDone()
|
||||
if len(done) > 0 {
|
||||
return done
|
||||
}
|
||||
return t.TranslationStatusCamel.languagesDone()
|
||||
}
|
||||
|
||||
// translationForLanguage returns stored translation text when the pipeline marked
|
||||
// the language done and non-empty title exists.
|
||||
func (t *TenderJSON) translationForLanguage(language string) (StoredTranslation, bool) {
|
||||
if t == nil || !languageInDone(t.translationsDone(), language) {
|
||||
return StoredTranslation{}, false
|
||||
}
|
||||
lang := strings.ToLower(strings.TrimSpace(language))
|
||||
if t.Translations != nil {
|
||||
for key, entry := range t.Translations {
|
||||
if strings.ToLower(strings.TrimSpace(key)) != lang {
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(entry.Title) == "" {
|
||||
continue
|
||||
}
|
||||
return entry, true
|
||||
}
|
||||
}
|
||||
return StoredTranslation{}, false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user