Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class OpenAiCompatibleProvider(
max_tokens = settings.maxTokens.takeIf { it > 0 }
)

val curlCmd = buildCurlCommand(endpoint, apiKey, req)
val curlCmd = buildCurlCommand(endpoint, req)
LlmRuntimeLogger.info("curl | $curlCmd")

val response = http.post(endpoint) {
Expand Down Expand Up @@ -98,10 +98,33 @@ class OpenAiCompatibleProvider(
companion object {
private val curlJson = Json { prettyPrint = false }

private fun buildCurlCommand(endpoint: String, apiKey: String, req: ChatCompletionsRequest): String {
val body = curlJson.encodeToString(ChatCompletionsRequest.serializer(), req)
.replace("'", "'\"'\"'")
return "curl -X POST '$endpoint' -H 'Authorization: Bearer ***' -H 'Content-Type: application/json' --data '$body'"
private fun buildCurlCommand(endpoint: String, req: ChatCompletionsRequest): String {
val rawBody = curlJson.encodeToString(ChatCompletionsRequest.serializer(), req)
val safeBody = redactSensitivePayload(rawBody)
val body = if (safeBody.length <= MAX_LOG_BODY_CHARS) safeBody
else safeBody.take(MAX_LOG_BODY_CHARS) + "...<truncated ${safeBody.length - MAX_LOG_BODY_CHARS} chars>"
return "curl -X POST ${shellQuote(redactSensitiveUrl(endpoint))} -H 'Authorization: Bearer ***' -H 'Content-Type: application/json' --data ${shellQuote(body)}"
}

private fun redactSensitivePayload(text: String): String {
var result = text
listOf("password", "token", "access_token", "id_token", "refresh_token", "apiKey", "api_key", "secret").forEach { key ->
val pattern = Regex("""("${Regex.escape(key)}"\s*:\s*")[^"]*(")""", RegexOption.IGNORE_CASE)
result = result.replace(pattern) { "${it.groupValues[1]}***${it.groupValues[2]}" }
}
return result
}

private fun redactSensitiveUrl(url: String): String {
var result = url
listOf("token", "access_token", "apiKey", "api_key", "key", "secret").forEach { key ->
result = result.replace(Regex("(?i)([?&]${Regex.escape(key)}=)[^&#]*")) { "${it.groupValues[1]}***" }
}
return result
}

private fun shellQuote(value: String): String = "'" + value.replace("'", "'\"'\"'") + "'"

private const val MAX_LOG_BODY_CHARS = 4_000
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class TemplateRequestExecutor(
}
val safeRequestHeaders = redactHeaders(effectiveRequestHeaders)
val safeRequestBody = redactSensitivePayload(renderedBody)
val safeRequestUrl = redactSensitiveUrl(renderedUrl)
val curlCommand = toCurlCommand(config.method.uppercase(), renderedUrl, effectiveRequestHeaders, renderedBody)
LlmRuntimeLogger.info(
"Template request start | method=${config.method.uppercase()} | url=$renderedUrl | headers=$safeRequestHeaders | body=$safeRequestBody | curl=$curlCommand"
"Template request start | method=${config.method.uppercase()} | url=$safeRequestUrl | headers=$safeRequestHeaders | body=$safeRequestBody | curl=$curlCommand"
)

val response = http.request {
Expand Down Expand Up @@ -144,11 +145,19 @@ class TemplateRequestExecutor(
}
val redactedBody = redactSensitivePayload(body)
val bodyArg = if (redactedBody.isNotBlank()) " --data " + shellQuote(redactedBody) else ""
return "curl -X $method " + shellQuote(url) +
return "curl -X $method " + shellQuote(redactSensitiveUrl(url)) +
(if (headerArgs.isNotBlank()) " $headerArgs" else "") +
bodyArg
}

private fun redactSensitiveUrl(url: String): String {
var result = url
listOf("token", "access_token", "apiKey", "api_key", "key", "secret", "password").forEach { key ->
result = result.replace(Regex("(?i)([?&]${Regex.escape(key)}=)[^&#]*")) { "${it.groupValues[1]}***" }
}
return result
}

private fun redactHeaderValue(name: String, value: String): String {
return if (name.contains("authorization", true) ||
name.contains("token", true) ||
Expand Down
Loading
Loading