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
22 changes: 2 additions & 20 deletions Sources/CodexReview/Model/CodexReviewJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -631,16 +631,7 @@ public final class CodexReviewJob: Identifiable, Hashable {
if truncatedText.isEmpty {
entries.remove(at: index)
} else {
entries[index] = .init(
id: entry.id,
kind: entry.kind,
groupID: entry.groupID,
replacesGroup: entry.replacesGroup,
text: truncatedText,
metadata: entry.metadata,
audience: entry.audience,
timestamp: entry.timestamp
)
entries[index] = entry.retainingText(truncatedText, keeping: direction)
}
replaceLogEntries(entries)
}
Expand Down Expand Up @@ -768,16 +759,7 @@ public final class CodexReviewJob: Identifiable, Hashable {
return trimmedEntries
}

trimmedEntries[index] = .init(
id: entry.id,
kind: entry.kind,
groupID: entry.groupID,
replacesGroup: entry.replacesGroup,
text: truncatedText,
metadata: entry.metadata,
audience: entry.audience,
timestamp: entry.timestamp
)
trimmedEntries[index] = entry.retainingText(truncatedText, keeping: direction)
return trimmedEntries
}

Expand Down
45 changes: 43 additions & 2 deletions Sources/CodexReview/Model/ReviewLogEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public struct ReviewLogEntry: Codable, Identifiable, Sendable, Hashable {
public let tool: String?
public let query: String?
public let path: String?
public let resultText: String?
public let errorText: String?
public fileprivate(set) var resultText: String?
public fileprivate(set) var errorText: String?

public init(
sourceType: String,
Expand Down Expand Up @@ -194,6 +194,15 @@ package enum ReviewAgentMessagePhase: String, Codable, Sendable, Hashable {
}

package extension ReviewLogEntry.Metadata {
var toolOutput: String? {
switch sourceType {
case "mcpToolCall", "dynamicToolCall", "functionCallOutput":
errorText ?? resultText
default:
nil
}
}

var agentMessagePhase: ReviewAgentMessagePhase? {
guard sourceType == "agentMessage" || sourceType == "canonicalReviewResult" else {
return nil
Expand All @@ -202,6 +211,38 @@ package extension ReviewLogEntry.Metadata {
}
}

extension ReviewLogEntry {
func retainingText(_ retained: String, keeping direction: CodexReviewJob.TruncationDirection) -> Self {
var metadata = metadata
if kind == .toolCall, metadata?.toolOutput != nil {
let original = text as NSString
let length = retained.utf16.count
let start = direction == .prefix ? 0 : original.length - length
let retainedRange = NSRange(location: start, length: length)
func retainedOutput(_ value: String?) -> String? {
guard let value else { return nil }
let range = original.range(of: value, options: [.anchored, .backwards, .literal])
guard range.location != NSNotFound else { return nil }
let overlap = NSIntersectionRange(range, retainedRange)
if overlap == retainedRange { return retained }
return overlap.length > 0 ? original.substring(with: overlap) : nil
}
metadata?.resultText = retainedOutput(self.metadata?.resultText)
metadata?.errorText = retainedOutput(self.metadata?.errorText)
}
return .init(
id: id,
kind: kind,
groupID: groupID,
replacesGroup: replacesGroup,
text: retained,
metadata: metadata,
audience: audience,
timestamp: timestamp
)
}
}

package extension KeyedDecodingContainer {
func decode(
_ type: ReviewLogEntry.AudienceMetadata.Type,
Expand Down
63 changes: 49 additions & 14 deletions Sources/CodexReviewAppServer/AppServerCodexReviewBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4096,8 +4096,8 @@ private struct AppServerThreadItem: Decodable, Sendable {
var review: String?
var summary: [String]?
var content: [String]?
var result: AppServerNotificationValue?
var error: AppServerNotificationValue?
var resultText: String?
var errorText: String?
var success: Bool?
var prompt: String?
var activityKind: String?
Expand Down Expand Up @@ -4127,6 +4127,9 @@ private struct AppServerThreadItem: Decodable, Sendable {
case summary
case content
case result
case name
case output
case contentItems
case fileID = "fileId"
case error
case success
Expand All @@ -4152,7 +4155,7 @@ private struct AppServerThreadItem: Decodable, Sendable {
self.commandActions = try container.decodeIfPresent([AppServerCommandAction].self, forKey: .commandActions) ?? []
self.status = try container.decodeStringIfPresent(forKey: .status)
self.server = try container.decodeStringIfPresent(forKey: .server)
self.tool = try container.decodeStringIfPresent(forKey: .tool)
self.tool = try container.decodeStringIfPresent(forKey: type == "functionCallOutput" ? .name : .tool)
self.namespace = try container.decodeStringIfPresent(forKey: .namespace)
self.query = try container.decodeStringIfPresent(forKey: .query)
self.path = try container.decodeStringIfPresent(forKey: .path)
Expand All @@ -4164,12 +4167,17 @@ private struct AppServerThreadItem: Decodable, Sendable {
self.summary = nil
self.content = nil
}
if type == "imageGeneration", let fileID = try? container.decode(String.self, forKey: .fileID) {
self.result = .string(fileID)
if type == "functionCallOutput" {
resultText = try container.decode(AppServerNotificationValue.self, forKey: .output).toolOutputText
} else if type == "dynamicToolCall",
let items = try container.decodeIfPresent(AppServerNotificationValue.self, forKey: .contentItems) {
resultText = items.toolOutputText
} else if type == "imageGeneration", let fileID = try? container.decode(String.self, forKey: .fileID) {
resultText = fileID
} else {
self.result = try container.decodeIfPresent(AppServerNotificationValue.self, forKey: .result)
resultText = try container.decodeIfPresent(AppServerNotificationValue.self, forKey: .result)?.nonNullDebugText
}
self.error = try container.decodeIfPresent(AppServerNotificationValue.self, forKey: .error)
errorText = try container.decodeIfPresent(AppServerNotificationValue.self, forKey: .error)?.nonNullDebugText
self.success = try container.decodeIfPresent(Bool.self, forKey: .success)
self.prompt = try container.decodeStringIfPresent(forKey: .prompt)
self.activityKind = try container.decodeStringIfPresent(forKey: .activityKind)
Expand All @@ -4187,7 +4195,7 @@ private struct AppServerThreadItem: Decodable, Sendable {
agentMessagePhase: ReviewAgentMessagePhase? = nil
) throws -> [CodexReviewBackendModel.Review.Event] {
switch type {
case "userMessage":
case "userMessage", "functionCallOutput":
return []
case "enteredReviewMode":
return review.map { [.logEntry(kind: .progress, text: "Reviewing \($0)", groupID: id, replacesGroup: true)] } ?? []
Expand Down Expand Up @@ -4305,10 +4313,12 @@ private struct AppServerThreadItem: Decodable, Sendable {
return text.map { [.logEntry(kind: .plan, text: $0, groupID: id, replacesGroup: true)] } ?? []
case "reasoning":
return reasoningCompletionEvents(replacesGroup: true)
case "functionCallOutput":
return [logEntry(kind: .toolCall, text: "\(toolLabel) returned output.\(resultSuffix)", replacesGroup: true, title: toolLabel, status: "completed")]
case "mcpToolCall":
return [logEntry(kind: .toolCall, text: "\(toolLabel) \(status ?? "completed").\(resultSuffix)", replacesGroup: true, title: toolLabel, status: completedStatus)]
case "dynamicToolCall":
return [logEntry(kind: .toolCall, text: "Dynamic tool \(toolLabel) \(status ?? "completed").\(resultSuffix)", replacesGroup: true, title: toolLabel, status: completedStatus)]
return [logEntry(kind: .toolCall, text: "Dynamic tool \(toolLabel) \(completedStatus ?? "completed").\(resultSuffix)", replacesGroup: true, title: toolLabel, status: completedStatus)]
case "collabAgentToolCall":
return [logEntry(kind: .toolCall, text: "Collab tool \(toolLabel) \(status ?? "completed").\(promptSuffix)", replacesGroup: true, title: toolLabel, status: completedStatus, detail: prompt)]
case "subAgentActivity":
Expand Down Expand Up @@ -4499,8 +4509,8 @@ private struct AppServerThreadItem: Decodable, Sendable {
tool: tool,
query: query,
path: path,
resultText: result?.nonNullDebugText?.nilIfEmpty,
errorText: error?.nonNullDebugText?.nilIfEmpty
resultText: resultText?.isEmpty == false ? resultText : nil,
errorText: errorText?.nilIfEmpty
)
}

Expand Down Expand Up @@ -4536,13 +4546,16 @@ private struct AppServerThreadItem: Decodable, Sendable {
}

private var completedStatus: String? {
if type == "dynamicToolCall", success == false {
return "failed"
}
if let status = status?.nilIfEmpty {
return status
}
if let exitCode {
return exitCode == 0 ? "succeeded" : "failed"
}
if error?.nonNullDebugText?.nilIfEmpty != nil {
if errorText?.nilIfEmpty != nil {
return "failed"
}
if let success {
Expand Down Expand Up @@ -4574,10 +4587,10 @@ private struct AppServerThreadItem: Decodable, Sendable {
}

private var resultSuffix: String {
if let error = error?.nonNullDebugText?.nilIfEmpty {
if let error = errorText?.nilIfEmpty {
return " Error: \(error)"
}
if let result = result?.nonNullDebugText?.nilIfEmpty {
if let result = resultText, !result.isEmpty {
return " Result: \(result)"
}
return ""
Expand Down Expand Up @@ -4647,6 +4660,28 @@ private enum AppServerNotificationValue: Decodable, Sendable {
}
}

var toolOutputText: String {
guard case .array(let items) = self else { return debugText }
return items.map { item -> String in
guard case .object(let block) = item, case .string(let type) = block["type"] else {
return item.debugText
}
switch type {
case "inputText", "input_text":
if case .string(let text) = block["text"] { return text }
return item.debugText
case "inputImage", "input_image":
return "<image content>"
case "inputAudio", "input_audio":
return "<audio content>"
case "encrypted_content":
return "<encrypted content>"
default:
return item.debugText
}
}.joined(separator: "\n")
}

var nonNullDebugText: String? {
if case .null = self {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ enum CurrentV2ReviewNotificationDecoder {
_ = try requiredString("text", in: item)
try validateOptionalNullableEnum("delivery", in: item, allowed: agentMessageDeliveries)
try validateOptionalNullableEnum("phase", in: item, allowed: agentMessagePhases)
case "functionCallOutput":
_ = try requiredString("name", in: item)
if item["output"] is String == false {
_ = try requiredObjectArray("output", in: item)
}
case "plan":
_ = try requiredString("text", in: item)
case "reasoning":
Expand Down Expand Up @@ -787,6 +792,7 @@ enum CurrentV2ReviewNotificationDecoder {
"userMessage",
"hookPrompt",
"agentMessage",
"functionCallOutput",
"plan",
"reasoning",
"commandExecution",
Expand Down
Loading