Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

150 lines
4.0 KiB
TypeScript

/**
* Streaming-specific tokenization helpers
*/
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { calculateStreamingCost } from '@/lib/tokenization/calculators'
import { TOKENIZATION_CONFIG } from '@/lib/tokenization/constants'
import {
extractTextContent,
hasRealCostData,
hasRealTokenData,
isTokenizableBlockType,
logTokenizationDetails,
} from '@/lib/tokenization/utils'
import type { BlockLog } from '@/executor/types'
const logger = createLogger('StreamingTokenization')
/**
* Processes a block log and adds tokenization data if needed
*/
export function processStreamingBlockLog(log: BlockLog, streamedContent: string): boolean {
// Check if this block should be tokenized
if (!isTokenizableBlockType(log.blockType)) {
return false
}
// Check if we already have meaningful token/cost data
if (hasRealTokenData(log.output?.tokens) && hasRealCostData(log.output?.cost)) {
return false
}
// Skip recalculation if cost was explicitly set by the billing layer (e.g. BYOK zero cost)
if (log.output?.cost?.pricing) {
return false
}
// Check if we have content to tokenize
if (!streamedContent?.trim()) {
return false
}
try {
// Determine model to use
const model = getModelForBlock(log)
// Prepare input text from log
const inputText = extractTextContent(log.input)
// Calculate streaming cost
const systemPrompt =
typeof log.input?.systemPrompt === 'string' ? log.input.systemPrompt : undefined
const context = typeof log.input?.context === 'string' ? log.input.context : undefined
const messages = Array.isArray(log.input?.messages)
? (log.input.messages as Array<{ role: string; content: string }>)
: undefined
const result = calculateStreamingCost(
model,
inputText,
streamedContent,
systemPrompt,
context,
messages
)
// Update the log output with tokenization data
if (!log.output) {
log.output = {}
}
log.output.tokens = result.tokens
log.output.cost = result.cost
log.output.model = result.model
logTokenizationDetails(`Streaming tokenization completed for ${log.blockType}`, {
blockId: log.blockId,
blockType: log.blockType,
model: result.model,
provider: result.provider,
inputLength: inputText.length,
outputLength: streamedContent.length,
tokens: result.tokens,
cost: result.cost,
method: result.method,
})
return true
} catch (error) {
logger.error(`Streaming tokenization failed for block ${log.blockId}`, {
blockType: log.blockType,
error: toError(error).message,
contentLength: streamedContent?.length || 0,
})
// Don't throw - graceful degradation
return false
}
}
/**
* Determines the appropriate model for a block
*/
function getModelForBlock(log: BlockLog): string {
// Try to get model from output first
if (log.output?.model?.trim()) {
return log.output.model
}
// Try to get model from input
const inputModel = log.input?.model
if (typeof inputModel === 'string' && inputModel.trim()) {
return inputModel
}
// Use block type specific defaults
const blockType = log.blockType
if (blockType === 'agent' || blockType === 'router' || blockType === 'evaluator') {
return TOKENIZATION_CONFIG.defaults.model
}
// Final fallback
return TOKENIZATION_CONFIG.defaults.model
}
/**
* Processes multiple block logs for streaming tokenization
*/
export function processStreamingBlockLogs(
logs: BlockLog[],
streamedContentMap: Map<string, string>
): number {
let processedCount = 0
for (const log of logs) {
const content = streamedContentMap.get(log.blockId)
if (content && processStreamingBlockLog(log, content)) {
processedCount++
}
}
logger.info(`Streaming tokenization summary`, {
totalLogs: logs.length,
processedBlocks: processedCount,
streamedBlocks: streamedContentMap.size,
})
return processedCount
}