Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
57 changes: 53 additions & 4 deletions Sources/SessionIndexRegisteredAgents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,18 @@ extension SessionIndexStore {
metadata.branch = firstString(in: object, keys: ["gitBranch", "branch"])
}
if metadata.title.isEmpty {
metadata.title = firstString(in: object, keys: ["title", "prompt", "text", "content"]) ?? ""
metadata.title = firstText(in: object, keys: ["title", "prompt", "text", "content"]) ?? ""
Comment thread
austinywang marked this conversation as resolved.
Resolved
}
if metadata.title.isEmpty, let message = object["message"] as? [String: Any] {
metadata.title = firstString(in: message, keys: ["content", "text"]) ?? ""
let role = firstString(in: message, keys: ["role"])
if role == nil || isUserRole(role) {
metadata.title = firstText(in: message, keys: ["content", "text"]) ?? ""
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
}
if metadata.title.isEmpty, let messages = object["messages"] as? [[String: Any]] {
metadata.title = messages.compactMap { message in
firstString(in: message, keys: ["role"]) == "user"
? firstString(in: message, keys: ["content", "text"])
isUserRole(firstString(in: message, keys: ["role"]))
? firstText(in: message, keys: ["content", "text"])
: nil
}.first ?? ""
}
Expand Down Expand Up @@ -208,6 +211,52 @@ extension SessionIndexStore {
return nil
}

nonisolated private static func firstText(in object: [String: Any], keys: [String]) -> String? {
for key in keys {
guard let text = firstTextValue(object[key]) else { continue }
return text
}
return nil
}

nonisolated private static func firstTextValue(_ value: Any?) -> String? {
if let string = value as? String {
return trimmedNonEmpty(string)
}
if let values = value as? [Any] {
for value in values {
if let text = firstTextBlock(value) {
return text
}
}
}
if let block = value as? [String: Any] {
return firstTextBlock(block)
}
return nil
}

nonisolated private static func firstTextBlock(_ value: Any) -> String? {
if let string = value as? String {
return trimmedNonEmpty(string)
}
guard let block = value as? [String: Any] else { return nil }
if let type = firstString(in: block, keys: ["type"]),
type.caseInsensitiveCompare("text") != .orderedSame {
return nil
}
return firstString(in: block, keys: ["text"])
}

nonisolated private static func trimmedNonEmpty(_ value: String) -> String? {
let trimmed = value.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? nil : trimmed
}

nonisolated private static func isUserRole(_ role: String?) -> Bool {
role?.caseInsensitiveCompare("user") == .orderedSame
}

nonisolated private static func piCWDInferred(from url: URL) -> String? {
let directoryName = url.deletingLastPathComponent().lastPathComponent
guard directoryName.hasPrefix("--"), directoryName.hasSuffix("--"), directoryName.count > 4 else {
Expand Down
31 changes: 31 additions & 0 deletions cmuxTests/PiVaultAgentPersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,37 @@ final class PiVaultAgentPersistenceTests: XCTestCase {
XCTAssertEqual(entry.gitBranch, "issue-3575-vault-pi-agent-support")
}

func testPiJSONLTypedContentBlocksUseFirstUserTextAsTitle() async throws {
let tempDir = FileManager.default.temporaryDirectory
.appendingPathComponent("cmux-pi-vault-title-blocks-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: tempDir) }

let cwd = "/tmp/pi typed blocks"
let projectDirectory = try XCTUnwrap(PiSessionLocator.projectDirectoryName(for: cwd))
let sessionDir = tempDir.appendingPathComponent(projectDirectory, isDirectory: true)
try FileManager.default.createDirectory(at: sessionDir, withIntermediateDirectories: true)
let sessionFile = sessionDir.appendingPathComponent("019e1c86-def0-72c9-90d4-8543db20f981.jsonl")
try """
{"type":"message","message":{"role":"assistant","content":[{"type":"text","text":"assistant preface"}]}}
{"type":"message","message":{"role":"user","content":[{"type":"text","text":"ping"}]}}
""".write(to: sessionFile, atomically: true, encoding: .utf8)

var registration = CmuxVaultAgentRegistration.builtInPi
registration.sessionDirectory = tempDir.path
let entries = await SessionIndexStore.loadRegisteredAgentEntries(
registration: registration,
needle: "",
cwdFilter: cwd,
offset: 0,
limit: 10
)

let entry = try XCTUnwrap(entries.first)
XCTAssertEqual(entry.title, "ping")
XCTAssertEqual(entry.cwd, cwd)
}

func testPiVaultAgentSnapshotRoundTripBuildsTargetedSessionCommand() throws {
let tempDir = FileManager.default.temporaryDirectory
.appendingPathComponent("cmux-pi-vault-agent-\(UUID().uuidString)", isDirectory: true)
Expand Down
Loading