This commit is contained in:
greeneclipse committed 2026-10-20 17:50:32 +02:00
1 parent fd83a7c523
commit edbbf127ba
12 files changed
+246 -207

No files matched your search

+52 -21
View File
@@ -135,26 +135,42 @@ type Job struct {
// of ≤maxJobProducts so ClaimNext SKIP LOCKED workers stay parallelizable.
// Absolute request cap is MaxStartProducts (100k–1M scale).
func (p *Pipeline) StartJob(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", nil)
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", nil, "")
}
// StartAPIJob uses the same processing pipeline and company-scoped catalog as the
// dashboard, but completed API results do not require a dashboard approval action.
func (p *Pipeline) StartAPIJob(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", nil)
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", nil, "")
}
// StartJobWithTypes preserves the exact outputs selected in the dashboard.
func (p *Pipeline) StartJobWithTypes(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", outputs)
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", outputs, "")
}
// StartAPIJobWithTypes preserves explicit output selections for public API jobs.
func (p *Pipeline) StartAPIJobWithTypes(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", outputs)
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", outputs, "")
}
func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType, origin string, outputs []string) ([]Job, error) {
// StartJobWithTypesInLanguage queues only the explicitly selected company language.
func (p *Pipeline) StartJobWithTypesInLanguage(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string, language string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "dashboard", outputs, language)
}
// StartAPIJobWithTypesInLanguage keeps public API jobs single-language as well.
func (p *Pipeline) StartAPIJobWithTypesInLanguage(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType string, outputs []string, language string) ([]Job, error) {
return p.startJobWithOrigin(ctx, companyID, userID, rawIDs, processingType, "api", outputs, language)
}
func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uuid.UUID, rawIDs []uuid.UUID, processingType, origin string, outputs []string, language string) ([]Job, error) {
if language == "" {
language = company.LoadLanguage(ctx, p.Pool, companyID)
}
if !company.IsAllowedLanguage(language) {
return nil, fmt.Errorf("unsupported processing language %q", language)
}
if processingType == "" {
processingType = "full"
}
@@ -214,9 +230,9 @@ func (p *Pipeline) startJobWithOrigin(ctx context.Context, companyID, userID uui
var jobID uuid.UUID
err = tx.QueryRow(ctx, `
INSERT INTO processing_jobs (
company_id, user_id, status, total_products, processing_type, current_step, step_progress, origin, processing_types
) VALUES ($1, $2, 'pending', $3, $4, $5, $6::jsonb, $7, $8) RETURNING id`,
companyID, userID, len(chunk), processingType, firstStep, progressJSON, origin, outputs).Scan(&jobID)
company_id, user_id, status, total_products, processing_type, current_step, step_progress, origin, processing_types, processing_language
) VALUES ($1, $2, 'pending', $3, $4, $5, $6::jsonb, $7, $8, $9) RETURNING id`,
companyID, userID, len(chunk), processingType, firstStep, progressJSON, origin, outputs, language).Scan(&jobID)
if err != nil {
return nil, err
}
@@ -253,11 +269,7 @@ func (p *Pipeline) assertProcessingGates(ctx context.Context, companyID uuid.UUI
RequiresAI: billing.ProcessingTypeRequiresAI(processingType) || billing.ProcessingTypeIsEmailCampaignAI(processingType),
RequiresEPREL: billing.ProcessingTypeRequiresEPREL(processingType),
}
if rates, err := p.Billing.ContentRates(ctx, companyID); err == nil {
if units, err := billing.ContentDebitUnits(outputs, processingType, rates); err == nil {
opts.CreditsPerProduct = float64(units) / 10000
}
}
opts.CreditsPerProduct = 1
return p.Billing.AssertCanStartProcessing(ctx, companyID, batchSize, opts)
}
@@ -626,13 +638,13 @@ func (p *Pipeline) RetryJob(ctx context.Context, companyID, id uuid.UUID) (Job,
func (p *Pipeline) ProcessJob(ctx context.Context, jobID uuid.UUID) error {
var companyID uuid.UUID
var jobUserID *uuid.UUID
var status, processingType, origin string
var status, processingType, origin, processingLanguage string
var selectedOutputs []string
var alreadyProcessed int
err := p.Pool.QueryRow(ctx, `
SELECT company_id, user_id, status, processing_type, processed_products, origin, processing_types
SELECT company_id, user_id, status, processing_type, processed_products, origin, processing_types, COALESCE(processing_language, '')
FROM processing_jobs WHERE id = $1`, jobID).
Scan(&companyID, &jobUserID, &status, &processingType, &alreadyProcessed, &origin, &selectedOutputs)
Scan(&companyID, &jobUserID, &status, &processingType, &alreadyProcessed, &origin, &selectedOutputs, &processingLanguage)
if err != nil {
return err
}
@@ -700,6 +712,15 @@ func (p *Pipeline) ProcessJob(ctx context.Context, jobID uuid.UUID) error {
batch := resolveBatchSize(p.BatchSize)
progressEvery := resolveProgressEvery(p.ProgressEvery)
jobCache := p.loadJobScopedCache(ctx, companyID, jobID)
processingLanguage = company.NormalizeLanguage(processingLanguage)
if processingLanguage == "" {
processingLanguage = jobCache.language
}
// One target language, even if the company has many enabled languages.
// Keep cache.language as the primary so only primary jobs update row-level
// title/description/meta fields.
jobCache.contentLanguages = []string{processingLanguage}
jobCache.processingLanguage = processingLanguage
if !jobCache.stepPolicy().AllowAI {
log.Printf("processing: ai_skip job=%s reason=entitlement_can_use_ai", jobID)
} else if !jobEngine.CompleterEnabled() {
@@ -905,6 +926,7 @@ type jobScopedCache struct {
stdDefs []StandardFieldDef
brandPrompt string
language string
processingLanguage string
enhanceSystemTemplate string
enhanceUserTemplate string
enhanceByLang map[string]PromptTemplates
@@ -1675,6 +1697,9 @@ func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, i
result.MetaDescription = ""
if result.LocalizedContent != nil {
for lang, lf := range result.LocalizedContent {
if cache.processingLanguage != "" && lang != cache.processingLanguage {
continue
}
lf.MetaTitle = ""
lf.MetaDescription = ""
result.LocalizedContent[lang] = lf
@@ -1690,7 +1715,7 @@ func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, i
}
}
// Keep primary localized meta in sync with row-level fields used by upsert.
if primary := company.NormalizeLanguage(language); primary != "" && result.LocalizedContent != nil {
if primary := company.NormalizeLanguage(language); primary != "" && result.LocalizedContent != nil && (cache == nil || cache.processingLanguage == "" || cache.processingLanguage == primary) {
lf := company.FieldsForLanguage(result.LocalizedContent, primary)
changed := false
if lf.MetaTitle == "" && result.MetaTitle != "" {
@@ -1753,7 +1778,7 @@ func (p *Pipeline) processOne(ctx context.Context, companyID, jobID uuid.UUID, i
}
}
processedID, err := p.upsertProcessedProduct(ctx, tx, companyID, it.RawID, gtin, result, attrsJSON, procAttrsJSON, gptJSON, sourcesJSON, providerMode, language, origin)
processedID, err := p.upsertProcessedProduct(ctx, tx, companyID, it.RawID, gtin, result, attrsJSON, procAttrsJSON, gptJSON, sourcesJSON, providerMode, language, origin, cache != nil && cache.processingLanguage != "" && cache.processingLanguage != language)
if err != nil {
return false, 0, result, err
}
@@ -1834,10 +1859,12 @@ const upsertProcessedProductSQL = `
ELSE EXCLUDED.description
END,
processed_name = CASE
WHEN $19::boolean THEN processed_products.processed_name
WHEN EXCLUDED.processing_origin = 'api' THEN COALESCE(NULLIF(BTRIM(EXCLUDED.processed_name), ''), processed_products.processed_name)
ELSE EXCLUDED.processed_name
END,
processed_description = CASE
WHEN $19::boolean THEN processed_products.processed_description
WHEN EXCLUDED.processing_origin = 'api' THEN COALESCE(NULLIF(BTRIM(EXCLUDED.processed_description), ''), processed_products.processed_description)
ELSE EXCLUDED.processed_description
END,
@@ -1851,13 +1878,14 @@ const upsertProcessedProductSQL = `
THEN processed_products.processed_attributes ELSE EXCLUDED.processed_attributes END,
gpt_response = EXCLUDED.gpt_response,
total_tokens = COALESCE(processed_products.total_tokens, 0) + EXCLUDED.total_tokens,
field_sources = EXCLUDED.field_sources,
field_sources = CASE WHEN $19::boolean THEN processed_products.field_sources ELSE EXCLUDED.field_sources END,
ai_provider_mode = EXCLUDED.ai_provider_mode,
localized_content = CASE
WHEN EXCLUDED.processing_origin = 'api'
WHEN EXCLUDED.processing_origin = 'api' OR $19::boolean
THEN COALESCE(processed_products.localized_content, '{}'::jsonb) || EXCLUDED.localized_content
ELSE EXCLUDED.localized_content END,
meta_title = CASE
WHEN $19::boolean THEN processed_products.meta_title
WHEN EXCLUDED.processing_origin = 'api' AND NULLIF(BTRIM(EXCLUDED.meta_title), '') IS NOT NULL
THEN NULLIF(BTRIM(EXCLUDED.meta_title), '')
WHEN NULLIF(BTRIM(processed_products.meta_title), '') IS NULL
@@ -1875,6 +1903,7 @@ const upsertProcessedProductSQL = `
ELSE processed_products.meta_title
END,
meta_description = CASE
WHEN $19::boolean THEN processed_products.meta_description
WHEN EXCLUDED.processing_origin = 'api' AND NULLIF(BTRIM(EXCLUDED.meta_description), '') IS NOT NULL
THEN NULLIF(BTRIM(EXCLUDED.meta_description), '')
WHEN NULLIF(BTRIM(processed_products.meta_description), '') IS NULL
@@ -1925,7 +1954,9 @@ func (p *Pipeline) upsertProcessedProduct(
providerMode string,
primaryLang string,
origin string,
secondaryLanguage ...bool,
) (uuid.UUID, error) {
isSecondary := len(secondaryLanguage) > 0 && secondaryLanguage[0]
localized := result.LocalizedContent
if localized == nil {
localized = company.LocalizedContent{}
@@ -1952,7 +1983,7 @@ func (p *Pipeline) upsertProcessedProduct(
err = queryRow(ctx, upsertProcessedProductSQL,
companyID, rawID, gtin, result.Name, result.Category, result.Description,
result.ProcessedName, result.ProcessedDescription, attrsJSON, procAttrsJSON, gptJSON, result.TotalTokens, sourcesJSON, providerMode, string(locJSON),
result.MetaTitle, result.MetaDescription, origin,
result.MetaTitle, result.MetaDescription, origin, isSecondary,
).Scan(&processedID)
return processedID, err
}