Skip to content

Commit 0e2f238

Browse files
committed
improvement(knowledge): use envNumber helper for KB_CONFIG_* env reads
1 parent f3ddb35 commit 0e2f238

4 files changed

Lines changed: 19 additions & 18 deletions

File tree

apps/sim/background/knowledge-processing.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLogger } from '@sim/logger'
22
import { task } from '@trigger.dev/sdk'
3-
import { env } from '@/lib/core/config/env'
3+
import { env, envNumber } from '@/lib/core/config/env'
44
import { processDocumentAsync } from '@/lib/knowledge/documents/service'
55

66
const logger = createLogger('TriggerKnowledgeProcessing')
@@ -23,16 +23,16 @@ export type DocumentProcessingPayload = {
2323

2424
export const processDocument = task({
2525
id: 'knowledge-process-document',
26-
maxDuration: env.KB_CONFIG_MAX_DURATION || 600,
26+
maxDuration: envNumber(env.KB_CONFIG_MAX_DURATION, 600),
2727
machine: 'large-1x', // 2 vCPU, 2GB RAM - needed for large PDF processing
2828
retry: {
29-
maxAttempts: env.KB_CONFIG_MAX_ATTEMPTS || 3,
30-
factor: env.KB_CONFIG_RETRY_FACTOR || 2,
31-
minTimeoutInMs: env.KB_CONFIG_MIN_TIMEOUT || 1000,
32-
maxTimeoutInMs: env.KB_CONFIG_MAX_TIMEOUT || 10000,
29+
maxAttempts: envNumber(env.KB_CONFIG_MAX_ATTEMPTS, 3),
30+
factor: envNumber(env.KB_CONFIG_RETRY_FACTOR, 2),
31+
minTimeoutInMs: envNumber(env.KB_CONFIG_MIN_TIMEOUT, 1000),
32+
maxTimeoutInMs: envNumber(env.KB_CONFIG_MAX_TIMEOUT, 10000),
3333
},
3434
queue: {
35-
concurrencyLimit: env.KB_CONFIG_CONCURRENCY_LIMIT || 20,
35+
concurrencyLimit: envNumber(env.KB_CONFIG_CONCURRENCY_LIMIT, 20),
3636
name: 'document-processing-queue',
3737
},
3838
run: async (payload: DocumentProcessingPayload) => {

apps/sim/lib/knowledge/documents/document-processor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
TokenChunker,
1414
} from '@/lib/chunkers'
1515
import type { ChunkingStrategy, StrategyOptions } from '@/lib/chunkers/types'
16-
import { env } from '@/lib/core/config/env'
16+
import { env, envNumber } from '@/lib/core/config/env'
1717
import { parseBuffer, parseFile } from '@/lib/file-parsers'
1818
import type { FileParseMetadata } from '@/lib/file-parsers/types'
1919
import { resolveParserExtension } from '@/lib/knowledge/documents/parser-extension'
@@ -30,7 +30,7 @@ const TIMEOUTS = {
3030
MISTRAL_OCR_API: 120000,
3131
} as const
3232

33-
const MAX_CONCURRENT_CHUNKS = env.KB_CONFIG_CHUNK_CONCURRENCY
33+
const MAX_CONCURRENT_CHUNKS = envNumber(env.KB_CONFIG_CHUNK_CONCURRENCY, 10)
3434

3535
type OCRResult = {
3636
success: boolean

apps/sim/lib/knowledge/documents/service.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import { recordUsage } from '@/lib/billing/core/usage-log'
3131
import { checkAndBillOverageThreshold } from '@/lib/billing/threshold-billing'
3232
import type { ChunkingStrategy, StrategyOptions } from '@/lib/chunkers/types'
33-
import { env } from '@/lib/core/config/env'
33+
import { env, envNumber } from '@/lib/core/config/env'
3434
import { getCostMultiplier, isTriggerDevEnabled } from '@/lib/core/config/feature-flags'
3535
import { processDocument } from '@/lib/knowledge/documents/document-processor'
3636
import type { DocumentSortField, SortOrder } from '@/lib/knowledge/documents/types'
@@ -54,12 +54,12 @@ import { calculateCost } from '@/providers/utils'
5454
const logger = createLogger('DocumentService')
5555

5656
const TIMEOUTS = {
57-
OVERALL_PROCESSING: (env.KB_CONFIG_MAX_DURATION || 600) * 1000,
57+
OVERALL_PROCESSING: envNumber(env.KB_CONFIG_MAX_DURATION, 600) * 1000,
5858
} as const
5959

6060
const LARGE_DOC_CONFIG = {
6161
MAX_CHUNKS_PER_BATCH: 500,
62-
MAX_EMBEDDING_BATCH: env.KB_CONFIG_BATCH_SIZE || 2000,
62+
MAX_EMBEDDING_BATCH: envNumber(env.KB_CONFIG_BATCH_SIZE, 2000),
6363
MAX_FILE_SIZE: 100 * 1024 * 1024,
6464
MAX_CHUNKS_PER_DOCUMENT: 100000,
6565
}
@@ -78,10 +78,11 @@ function withTimeout<T>(
7878
}
7979

8080
const PROCESSING_CONFIG = {
81-
maxConcurrentDocuments: Math.max(1, Math.floor((env.KB_CONFIG_CONCURRENCY_LIMIT || 20) / 5)) || 4,
82-
batchSize: Math.max(1, Math.floor((env.KB_CONFIG_BATCH_SIZE || 20) / 2)) || 10,
83-
delayBetweenBatches: (env.KB_CONFIG_DELAY_BETWEEN_BATCHES || 100) * 2,
84-
delayBetweenDocuments: (env.KB_CONFIG_DELAY_BETWEEN_DOCUMENTS || 50) * 2,
81+
maxConcurrentDocuments:
82+
Math.max(1, Math.floor(envNumber(env.KB_CONFIG_CONCURRENCY_LIMIT, 20) / 5)) || 4,
83+
batchSize: Math.max(1, Math.floor(envNumber(env.KB_CONFIG_BATCH_SIZE, 20) / 2)) || 10,
84+
delayBetweenBatches: envNumber(env.KB_CONFIG_DELAY_BETWEEN_BATCHES, 100) * 2,
85+
delayBetweenDocuments: envNumber(env.KB_CONFIG_DELAY_BETWEEN_DOCUMENTS, 50) * 2,
8586
}
8687

8788
export function getProcessingConfig() {

apps/sim/lib/knowledge/embeddings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLogger } from '@sim/logger'
22
import { getBYOKKey } from '@/lib/api-key/byok'
33
import { getRotatingApiKey } from '@/lib/core/config/api-keys'
4-
import { env } from '@/lib/core/config/env'
4+
import { env, envNumber } from '@/lib/core/config/env'
55
import { isRetryableError, retryWithExponentialBackoff } from '@/lib/knowledge/documents/utils'
66
import {
77
DEFAULT_EMBEDDING_MODEL,
@@ -15,7 +15,7 @@ import { batchByTokenLimit, estimateTokenCount } from '@/lib/tokenization'
1515
const logger = createLogger('EmbeddingUtils')
1616

1717
const MAX_TOKENS_PER_REQUEST = 8000
18-
const MAX_CONCURRENT_BATCHES = env.KB_CONFIG_CONCURRENCY_LIMIT || 50
18+
const MAX_CONCURRENT_BATCHES = envNumber(env.KB_CONFIG_CONCURRENCY_LIMIT, 50)
1919
const EMBEDDING_REQUEST_TIMEOUT_MS = 60_000
2020

2121
export type { EmbeddingModelInfo } from '@/lib/knowledge/embedding-models'

0 commit comments

Comments
 (0)