diff --git a/Sources/CodexReview/Model/CodexReviewJob.swift b/Sources/CodexReview/Model/CodexReviewJob.swift index a296d810..ad82af9e 100644 --- a/Sources/CodexReview/Model/CodexReviewJob.swift +++ b/Sources/CodexReview/Model/CodexReviewJob.swift @@ -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) } @@ -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 } diff --git a/Sources/CodexReview/Model/ReviewLogEntry.swift b/Sources/CodexReview/Model/ReviewLogEntry.swift index d5084ef7..aa64a62d 100644 --- a/Sources/CodexReview/Model/ReviewLogEntry.swift +++ b/Sources/CodexReview/Model/ReviewLogEntry.swift @@ -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, @@ -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 @@ -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, diff --git a/Sources/CodexReviewAppServer/AppServerCodexReviewBackend.swift b/Sources/CodexReviewAppServer/AppServerCodexReviewBackend.swift index 6721909a..3a0f7b98 100644 --- a/Sources/CodexReviewAppServer/AppServerCodexReviewBackend.swift +++ b/Sources/CodexReviewAppServer/AppServerCodexReviewBackend.swift @@ -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? @@ -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 @@ -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) @@ -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) @@ -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)] } ?? [] @@ -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": @@ -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 ) } @@ -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 { @@ -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 "" @@ -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 "" + case "inputAudio", "input_audio": + return "