Без опису

DashboardView.swift 99KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. //
  2. // DashboardView.swift
  3. // App for Indeed
  4. //
  5. import Cocoa
  6. import QuartzCore
  7. import Security
  8. private enum JobListingCardContext {
  9. case homeSearchResults
  10. case savedJobsPage
  11. }
  12. final class DashboardView: NSView, NSTextFieldDelegate {
  13. /// Indeed.com-inspired neutrals and brand blue (white surfaces, `#2557a7` accent, `#2d2d2d` / `#767676` text, `#d4d2d0` borders).
  14. private enum Theme {
  15. static let brandBlue = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  16. static let pageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  17. static let chromeBackground = NSColor(srgbRed: 247 / 255, green: 247 / 255, blue: 247 / 255, alpha: 1)
  18. static let sidebarBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  19. static let mainHostBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  20. /// Subtitle on the welcome hero: readable blue-gray aligned with brand.
  21. static let welcomeSubtitleText = NSColor(srgbRed: 52 / 255, green: 92 / 255, blue: 142 / 255, alpha: 1)
  22. static let selectionFill = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.12)
  23. static let cardBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  24. static let toggleBackground = NSColor(srgbRed: 232 / 255, green: 232 / 255, blue: 232 / 255, alpha: 1)
  25. static let primaryText = NSColor(srgbRed: 45 / 255, green: 45 / 255, blue: 45 / 255, alpha: 1)
  26. static let secondaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  27. static let tertiaryText = NSColor(srgbRed: 118 / 255, green: 118 / 255, blue: 118 / 255, alpha: 1)
  28. static let border = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  29. /// Job search bar outer stroke (charcoal).
  30. static let searchBarBorder = NSColor(srgbRed: 58 / 255, green: 58 / 255, blue: 58 / 255, alpha: 1)
  31. /// Search bar border on hover (brand-tinted, matches focus affordance elsewhere).
  32. static let searchBarBorderHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.45)
  33. static let proCardFill = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  34. static let proCardBorder = NSColor(srgbRed: 212 / 255, green: 210 / 255, blue: 208 / 255, alpha: 1)
  35. static let proAccent = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  36. static let proCTABackground = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 1)
  37. static let proCTAText = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  38. /// Slightly lighter blue for the top of the search-bar “Find jobs” pill (reads less flat than a solid fill).
  39. static let findJobsCTAHighlight = NSColor(srgbRed: 54 / 255, green: 110 / 255, blue: 198 / 255, alpha: 1)
  40. /// Hover states: darker brand blue, deeper gradient top, stronger tints, and subtle neutral fills used across CTAs, toggles, and the sidebar.
  41. static let brandBlueHover = NSColor(srgbRed: 28 / 255, green: 70 / 255, blue: 140 / 255, alpha: 1)
  42. static let findJobsCTAHighlightHover = NSColor(srgbRed: 44 / 255, green: 94 / 255, blue: 178 / 255, alpha: 1)
  43. static let selectionFillHover = NSColor(srgbRed: 37 / 255, green: 87 / 255, blue: 167 / 255, alpha: 0.2)
  44. static let neutralHoverFill = NSColor(srgbRed: 240 / 255, green: 240 / 255, blue: 240 / 255, alpha: 1)
  45. static let sidebarRowHoverFill = NSColor(srgbRed: 0, green: 0, blue: 0, alpha: 0.04)
  46. static let settingsPageBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  47. static let settingsGroupBackground = NSColor(srgbRed: 1, green: 1, blue: 1, alpha: 1)
  48. static let settingsIconBackground = NSColor(srgbRed: 239 / 255, green: 244 / 255, blue: 252 / 255, alpha: 1)
  49. static let settingsDivider = NSColor(srgbRed: 228 / 255, green: 228 / 255, blue: 228 / 255, alpha: 1)
  50. }
  51. /// Multiline `NSTextField` often ignores `alignment` for wrapped runs; explicit paragraph alignment matches the title.
  52. private static func jobListingDescriptionAttributedString(_ plain: String) -> NSAttributedString {
  53. let paragraph = NSMutableParagraphStyle()
  54. paragraph.alignment = .left
  55. paragraph.lineBreakMode = .byWordWrapping
  56. paragraph.baseWritingDirection = .leftToRight
  57. let font = NSFont.systemFont(ofSize: 13, weight: .regular)
  58. return NSAttributedString(string: plain, attributes: [
  59. .font: font,
  60. .foregroundColor: Theme.secondaryText,
  61. .paragraphStyle: paragraph
  62. ])
  63. }
  64. private let contentStack = NSStackView()
  65. private let chromeContainer = NSView()
  66. private let sidebar = NSStackView()
  67. private let mainHost = NSView()
  68. private let mainOverlay = NSStackView()
  69. private let greetingLabel = NSTextField(labelWithString: "")
  70. private let subtitleLabel = NSTextField(labelWithString: "")
  71. private let searchBarShadowHost = NSView()
  72. private let searchCard = HoverableView()
  73. private let jobSearchIcon = NSImageView()
  74. private let jobKeywordsField = NSTextField()
  75. private let findJobsButton = HoverableButton()
  76. private let findJobsCTAHost = NSView()
  77. private let findJobsCTAChrome = HoverableView()
  78. private var findJobsCTAGradientLayer: CAGradientLayer?
  79. private let chatStatusStack = NSStackView()
  80. private let chatStatusIcon = NSImageView()
  81. private let chatStatusLabel = NSTextField(labelWithString: "Opening the vault...")
  82. private let chatScrollView = NSScrollView()
  83. private let chatDocumentView = JobListingsDocumentView()
  84. private let chatStack = NSStackView()
  85. /// Shown when a sidebar item other than Home is selected.
  86. private let nonHomeHost = NSView()
  87. private let nonHomeGenericContainer = NSView()
  88. private let nonHomeTitleLabel = NSTextField(labelWithString: "")
  89. private let nonHomeSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  90. private let savedJobsPageContainer = NSView()
  91. private let savedJobsPageTitleLabel = NSTextField(labelWithString: "Saved Jobs")
  92. private let savedJobsPageSubtitleLabel = NSTextField(wrappingLabelWithString: "")
  93. private let savedJobsScrollView = NSScrollView()
  94. private let savedJobsDocumentView = JobListingsDocumentView()
  95. private let savedJobsStack = NSStackView()
  96. private let settingsPageContainer = NSView()
  97. private let themeControl = NSSegmentedControl(labels: ["System", "Light", "Dark"], trackingMode: .selectOne, target: nil, action: nil)
  98. private var currentSidebarItems: [SidebarItem] = []
  99. private var selectedSidebarIndex: Int = 0
  100. /// All jobs that have been shown in the current chat session, oldest first. Used to deduplicate continuation searches so "show more" doesn't re-display the same listings.
  101. private var lastSearchResults: [JobListing] = []
  102. /// Most recently saved jobs appear first; persisted across launches.
  103. private var savedJobOrder: [JobListing] = []
  104. private var chatMessages: [ChatMessage] = []
  105. private var isAwaitingResponse = false
  106. private let jobSearchService = OpenAIJobSearchService()
  107. override init(frame frameRect: NSRect) {
  108. super.init(frame: frameRect)
  109. setupLayout()
  110. }
  111. required init?(coder: NSCoder) {
  112. super.init(coder: coder)
  113. setupLayout()
  114. }
  115. override func layout() {
  116. super.layout()
  117. updateSearchBarShadowPath()
  118. findJobsCTAGradientLayer?.frame = findJobsCTAChrome.bounds
  119. updateFindJobsCTAShadowPath()
  120. updateJobListingDescriptionWidths()
  121. updateChatBubbleWidths()
  122. }
  123. func render(_ data: DashboardData) {
  124. greetingLabel.stringValue = "Welcome"
  125. subtitleLabel.stringValue = data.subtitle
  126. currentSidebarItems = data.sidebarItems
  127. if selectedSidebarIndex >= currentSidebarItems.count {
  128. selectedSidebarIndex = max(0, currentSidebarItems.count - 1)
  129. }
  130. configureSidebar()
  131. savedJobOrder = Self.normalizedSavedJobs(SavedJobsStore.load())
  132. resetChatState()
  133. updateMainContentVisibility()
  134. }
  135. private func setupLayout() {
  136. wantsLayer = true
  137. layer?.backgroundColor = Theme.pageBackground.cgColor
  138. contentStack.orientation = .horizontal
  139. contentStack.spacing = 10
  140. contentStack.distribution = .fill
  141. contentStack.translatesAutoresizingMaskIntoConstraints = false
  142. contentStack.alignment = .height
  143. // Tighter chrome insets so panels sit closer to the window edges (especially leading / top under the title bar).
  144. contentStack.edgeInsets = NSEdgeInsets(top: 10, left: 12, bottom: 20, right: 20)
  145. chromeContainer.translatesAutoresizingMaskIntoConstraints = false
  146. chromeContainer.wantsLayer = true
  147. chromeContainer.layer?.backgroundColor = Theme.chromeBackground.cgColor
  148. chromeContainer.layer?.cornerRadius = 0
  149. addSubview(chromeContainer)
  150. chromeContainer.addSubview(contentStack)
  151. sidebar.orientation = .vertical
  152. sidebar.spacing = 10
  153. sidebar.distribution = .fill
  154. sidebar.alignment = .leading
  155. sidebar.wantsLayer = true
  156. sidebar.layer?.backgroundColor = Theme.sidebarBackground.cgColor
  157. sidebar.layer?.cornerRadius = 16
  158. sidebar.edgeInsets = NSEdgeInsets(top: 16, left: 12, bottom: 16, right: 12)
  159. sidebar.translatesAutoresizingMaskIntoConstraints = false
  160. mainHost.translatesAutoresizingMaskIntoConstraints = false
  161. mainHost.wantsLayer = true
  162. mainHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  163. mainHost.layer?.cornerRadius = 16
  164. mainHost.layer?.masksToBounds = true
  165. sidebar.setContentHuggingPriority(.required, for: .horizontal)
  166. mainHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  167. mainHost.addSubview(mainOverlay)
  168. configureNonHomePlaceholder()
  169. mainHost.addSubview(nonHomeHost)
  170. mainOverlay.orientation = .vertical
  171. mainOverlay.spacing = 0
  172. mainOverlay.alignment = .centerX
  173. mainOverlay.distribution = .fill
  174. mainOverlay.translatesAutoresizingMaskIntoConstraints = false
  175. mainOverlay.setContentHuggingPriority(.defaultLow, for: .vertical)
  176. greetingLabel.font = .systemFont(ofSize: 32, weight: .bold)
  177. greetingLabel.textColor = Theme.brandBlue
  178. greetingLabel.alignment = .center
  179. greetingLabel.maximumNumberOfLines = 1
  180. subtitleLabel.font = .systemFont(ofSize: 15, weight: .regular)
  181. subtitleLabel.textColor = Theme.welcomeSubtitleText
  182. subtitleLabel.alignment = .center
  183. subtitleLabel.maximumNumberOfLines = 2
  184. let topInset = NSView()
  185. topInset.translatesAutoresizingMaskIntoConstraints = false
  186. topInset.heightAnchor.constraint(equalToConstant: 18).isActive = true
  187. configureSearchBar()
  188. configureChatViews()
  189. let titleBlock = NSStackView(views: [greetingLabel, subtitleLabel])
  190. titleBlock.orientation = .vertical
  191. titleBlock.spacing = 10
  192. titleBlock.alignment = .centerX
  193. let midSpacer = NSView()
  194. midSpacer.translatesAutoresizingMaskIntoConstraints = false
  195. midSpacer.heightAnchor.constraint(equalToConstant: 18).isActive = true
  196. let chatTopSpacer = NSView()
  197. chatTopSpacer.translatesAutoresizingMaskIntoConstraints = false
  198. chatTopSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  199. let chatBottomSpacer = NSView()
  200. chatBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  201. chatBottomSpacer.heightAnchor.constraint(equalToConstant: 14).isActive = true
  202. mainOverlay.addArrangedSubview(topInset)
  203. mainOverlay.addArrangedSubview(titleBlock)
  204. mainOverlay.addArrangedSubview(midSpacer)
  205. mainOverlay.addArrangedSubview(chatStatusStack)
  206. mainOverlay.addArrangedSubview(chatTopSpacer)
  207. mainOverlay.addArrangedSubview(chatScrollView)
  208. mainOverlay.addArrangedSubview(chatBottomSpacer)
  209. mainOverlay.addArrangedSubview(searchBarShadowHost)
  210. contentStack.addArrangedSubview(sidebar)
  211. contentStack.addArrangedSubview(mainHost)
  212. NSLayoutConstraint.activate([
  213. chromeContainer.leadingAnchor.constraint(equalTo: leadingAnchor),
  214. chromeContainer.trailingAnchor.constraint(equalTo: trailingAnchor),
  215. chromeContainer.topAnchor.constraint(equalTo: topAnchor),
  216. chromeContainer.bottomAnchor.constraint(equalTo: bottomAnchor),
  217. contentStack.leadingAnchor.constraint(equalTo: chromeContainer.leadingAnchor),
  218. contentStack.trailingAnchor.constraint(equalTo: chromeContainer.trailingAnchor),
  219. contentStack.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor),
  220. contentStack.bottomAnchor.constraint(equalTo: chromeContainer.bottomAnchor),
  221. sidebar.widthAnchor.constraint(equalToConstant: 218),
  222. mainHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 720),
  223. mainOverlay.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  224. mainOverlay.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  225. mainOverlay.topAnchor.constraint(equalTo: mainHost.topAnchor),
  226. mainOverlay.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  227. nonHomeHost.leadingAnchor.constraint(equalTo: mainHost.leadingAnchor),
  228. nonHomeHost.trailingAnchor.constraint(equalTo: mainHost.trailingAnchor),
  229. nonHomeHost.topAnchor.constraint(equalTo: mainHost.topAnchor),
  230. nonHomeHost.bottomAnchor.constraint(equalTo: mainHost.bottomAnchor, constant: -24),
  231. searchBarShadowHost.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  232. chatStatusStack.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  233. chatScrollView.widthAnchor.constraint(equalTo: mainOverlay.widthAnchor, multiplier: 0.92),
  234. greetingLabel.leadingAnchor.constraint(equalTo: mainOverlay.leadingAnchor, constant: 16),
  235. greetingLabel.trailingAnchor.constraint(equalTo: mainOverlay.trailingAnchor, constant: -16),
  236. subtitleLabel.leadingAnchor.constraint(equalTo: greetingLabel.leadingAnchor),
  237. subtitleLabel.trailingAnchor.constraint(equalTo: greetingLabel.trailingAnchor)
  238. ])
  239. }
  240. private func configureChatViews() {
  241. chatStatusStack.orientation = .vertical
  242. chatStatusStack.spacing = 6
  243. chatStatusStack.alignment = .centerX
  244. chatStatusStack.translatesAutoresizingMaskIntoConstraints = false
  245. chatStatusIcon.translatesAutoresizingMaskIntoConstraints = false
  246. chatStatusIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 36, weight: .regular)
  247. chatStatusIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "Assistant status")
  248. chatStatusIcon.contentTintColor = Theme.brandBlue
  249. chatStatusLabel.font = .systemFont(ofSize: 20, weight: .semibold)
  250. chatStatusLabel.textColor = Theme.primaryText
  251. chatStatusLabel.alignment = .center
  252. chatStatusLabel.maximumNumberOfLines = 1
  253. chatStatusStack.addArrangedSubview(chatStatusIcon)
  254. chatStatusStack.addArrangedSubview(chatStatusLabel)
  255. chatDocumentView.translatesAutoresizingMaskIntoConstraints = false
  256. chatStack.orientation = .vertical
  257. chatStack.spacing = 18
  258. chatStack.alignment = .width
  259. chatStack.distribution = .fill
  260. chatStack.translatesAutoresizingMaskIntoConstraints = false
  261. chatDocumentView.addSubview(chatStack)
  262. NSLayoutConstraint.activate([
  263. chatStack.leadingAnchor.constraint(equalTo: chatDocumentView.leadingAnchor, constant: 4),
  264. chatStack.trailingAnchor.constraint(equalTo: chatDocumentView.trailingAnchor, constant: -4),
  265. chatStack.topAnchor.constraint(equalTo: chatDocumentView.topAnchor, constant: 8),
  266. chatStack.bottomAnchor.constraint(equalTo: chatDocumentView.bottomAnchor, constant: -8)
  267. ])
  268. chatScrollView.translatesAutoresizingMaskIntoConstraints = false
  269. chatScrollView.hasVerticalScroller = true
  270. chatScrollView.hasHorizontalScroller = false
  271. chatScrollView.autohidesScrollers = true
  272. chatScrollView.drawsBackground = false
  273. chatScrollView.borderType = .noBorder
  274. chatScrollView.documentView = chatDocumentView
  275. chatScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  276. chatScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  277. chatScrollView.heightAnchor.constraint(greaterThanOrEqualToConstant: 320).isActive = true
  278. }
  279. private func updateJobListingDescriptionWidths() {
  280. updateDescriptionColumnWidths(in: savedJobsStack, containerWidth: savedJobsDocumentView.bounds.width)
  281. walkChatJobStacks { stack in
  282. updateDescriptionColumnWidths(in: stack, containerWidth: stack.bounds.width)
  283. }
  284. }
  285. private func walkChatJobStacks(_ visitor: (ChatJobsStackView) -> Void) {
  286. func walk(_ view: NSView) {
  287. if let stack = view as? ChatJobsStackView {
  288. visitor(stack)
  289. }
  290. for sub in view.subviews { walk(sub) }
  291. }
  292. walk(chatStack)
  293. }
  294. /// Chat bubble text fields are wrapping labels whose `preferredMaxLayoutWidth` needs to track the available row width so long strings reflow correctly when the window resizes.
  295. private func updateChatBubbleWidths() {
  296. func walk(_ view: NSView) {
  297. if let label = view as? ChatBubbleLabel,
  298. let bubble = label.superview, bubble.bounds.width > 1 {
  299. let target = max(40, bubble.bounds.width - 28)
  300. if abs(label.preferredMaxLayoutWidth - target) > 0.5 {
  301. label.preferredMaxLayoutWidth = target
  302. label.invalidateIntrinsicContentSize()
  303. }
  304. }
  305. for sub in view.subviews { walk(sub) }
  306. }
  307. walk(chatStack)
  308. }
  309. private func updateDescriptionColumnWidths(in stack: NSStackView, containerWidth: CGFloat) {
  310. guard containerWidth > 1 else { return }
  311. // Matches `contentColumn` insets on the card (16 leading + 16 trailing). The description spans the full row below the buttons, so never subtract a “button strip” here — a too-narrow `preferredMaxLayoutWidth` inside a wider field makes wrapped `NSTextField` text lay out like a trailing-aligned band after resizes.
  312. let contentHorizontalInset: CGFloat = 32
  313. var didChange = false
  314. for card in stack.arrangedSubviews {
  315. guard let desc = card.viewWithTag(502) as? NSTextField else { continue }
  316. let cardWidth = card.bounds.width > 1 ? card.bounds.width : containerWidth
  317. let fallbackColumn = max(1, cardWidth - contentHorizontalInset)
  318. let columnWidth: CGFloat
  319. if desc.bounds.width > 1 {
  320. columnWidth = desc.bounds.width
  321. } else if let column = desc.superview, column.bounds.width > 1 {
  322. columnWidth = column.bounds.width
  323. } else {
  324. columnWidth = fallbackColumn
  325. }
  326. if abs(desc.preferredMaxLayoutWidth - columnWidth) > 0.5 {
  327. desc.preferredMaxLayoutWidth = columnWidth
  328. desc.invalidateIntrinsicContentSize()
  329. didChange = true
  330. }
  331. }
  332. if didChange {
  333. stack.needsLayout = true
  334. }
  335. }
  336. private static func normalizedSavedJobs(_ jobs: [JobListing]) -> [JobListing] {
  337. var seen = Set<JobListing>()
  338. var out: [JobListing] = []
  339. for job in jobs where seen.insert(job).inserted {
  340. out.append(job)
  341. }
  342. return out
  343. }
  344. private func isJobSaved(_ job: JobListing) -> Bool {
  345. savedJobOrder.contains(job)
  346. }
  347. private func persistSavedJobs() {
  348. SavedJobsStore.save(savedJobOrder)
  349. }
  350. private func applySavedState(_ saved: Bool, for job: JobListing) {
  351. if saved {
  352. savedJobOrder.removeAll { $0 == job }
  353. savedJobOrder.insert(job, at: 0)
  354. } else {
  355. savedJobOrder.removeAll { $0 == job }
  356. }
  357. persistSavedJobs()
  358. }
  359. private func makeJobListingCard(_ job: JobListing, context: JobListingCardContext) -> NSView {
  360. let card = NSView()
  361. card.translatesAutoresizingMaskIntoConstraints = false
  362. card.wantsLayer = true
  363. card.layer?.backgroundColor = Theme.cardBackground.cgColor
  364. card.layer?.cornerRadius = 12
  365. card.layer?.borderWidth = 1
  366. card.layer?.borderColor = Theme.border.cgColor
  367. card.layer?.masksToBounds = true
  368. let titleField = NSTextField(labelWithString: job.title)
  369. titleField.font = .systemFont(ofSize: 16, weight: .semibold)
  370. titleField.textColor = Theme.primaryText
  371. titleField.maximumNumberOfLines = 2
  372. titleField.lineBreakMode = .byWordWrapping
  373. titleField.alignment = .left
  374. titleField.translatesAutoresizingMaskIntoConstraints = false
  375. let descriptionField = NSTextField(wrappingLabelWithString: job.description)
  376. descriptionField.font = .systemFont(ofSize: 13, weight: .regular)
  377. descriptionField.textColor = Theme.secondaryText
  378. descriptionField.maximumNumberOfLines = 0
  379. descriptionField.alignment = .left
  380. descriptionField.lineBreakMode = .byWordWrapping
  381. descriptionField.baseWritingDirection = .leftToRight
  382. descriptionField.attributedStringValue = Self.jobListingDescriptionAttributedString(job.description)
  383. if let cell = descriptionField.cell as? NSTextFieldCell {
  384. cell.alignment = .left
  385. cell.wraps = true
  386. }
  387. descriptionField.setContentHuggingPriority(.defaultLow, for: .horizontal)
  388. descriptionField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  389. descriptionField.tag = 502
  390. descriptionField.translatesAutoresizingMaskIntoConstraints = false
  391. let applyButton = JobPayloadButton(title: "Apply", target: self, action: #selector(didTapJobApply(_:)))
  392. applyButton.jobPayload = job
  393. applyButton.cardContext = context
  394. applyButton.isBordered = false
  395. applyButton.bezelStyle = .rounded
  396. applyButton.font = .systemFont(ofSize: 13, weight: .semibold)
  397. applyButton.wantsLayer = true
  398. applyButton.layer?.cornerRadius = 6
  399. applyButton.layer?.backgroundColor = Theme.brandBlue.cgColor
  400. applyButton.contentTintColor = Theme.proCTAText
  401. applyButton.focusRingType = .none
  402. applyButton.pointerCursor = true
  403. applyButton.hoverHandler = { [weak applyButton] hovering in
  404. applyButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.brandBlue).cgColor
  405. }
  406. applyButton.setContentHuggingPriority(.required, for: .horizontal)
  407. applyButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  408. let savedOn = isJobSaved(job)
  409. let savedButton = JobPayloadButton(title: savedOn ? "Saved" : "Save", target: self, action: #selector(didTapJobSaved(_:)))
  410. savedButton.jobPayload = job
  411. savedButton.cardContext = context
  412. savedButton.setButtonType(.toggle)
  413. savedButton.isBordered = false
  414. savedButton.bezelStyle = .rounded
  415. savedButton.font = .systemFont(ofSize: 13, weight: .semibold)
  416. savedButton.focusRingType = .none
  417. savedButton.state = savedOn ? .on : .off
  418. savedButton.pointerCursor = true
  419. savedButton.hoverHandler = { [weak self, weak savedButton] _ in
  420. guard let savedButton = savedButton else { return }
  421. self?.styleJobSavedButton(savedButton)
  422. }
  423. styleJobSavedButton(savedButton)
  424. savedButton.setContentHuggingPriority(.required, for: .horizontal)
  425. savedButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  426. let dismissButton = JobPayloadButton()
  427. dismissButton.jobPayload = job
  428. dismissButton.cardContext = context
  429. dismissButton.image = NSImage(systemSymbolName: "xmark", accessibilityDescription: "Dismiss")
  430. dismissButton.imagePosition = .imageOnly
  431. dismissButton.imageScaling = .scaleProportionallyDown
  432. dismissButton.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 11, weight: .semibold)
  433. dismissButton.isBordered = false
  434. dismissButton.bezelStyle = .rounded
  435. dismissButton.contentTintColor = Theme.secondaryText
  436. dismissButton.target = self
  437. dismissButton.action = #selector(didTapJobDismiss(_:))
  438. dismissButton.toolTip = context == .savedJobsPage ? "Remove from saved" : "Dismiss"
  439. dismissButton.focusRingType = .none
  440. dismissButton.wantsLayer = true
  441. dismissButton.layer?.cornerRadius = 14
  442. dismissButton.layer?.backgroundColor = NSColor.clear.cgColor
  443. dismissButton.pointerCursor = true
  444. dismissButton.hoverHandler = { [weak dismissButton] hovering in
  445. dismissButton?.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : NSColor.clear).cgColor
  446. dismissButton?.contentTintColor = hovering ? Theme.primaryText : Theme.secondaryText
  447. }
  448. dismissButton.setContentHuggingPriority(.required, for: .horizontal)
  449. let buttonRow = NSStackView(views: [applyButton, savedButton, dismissButton])
  450. buttonRow.orientation = .horizontal
  451. buttonRow.spacing = 8
  452. buttonRow.alignment = .centerY
  453. buttonRow.translatesAutoresizingMaskIntoConstraints = false
  454. buttonRow.setContentHuggingPriority(.required, for: .horizontal)
  455. buttonRow.setContentCompressionResistancePriority(.required, for: .horizontal)
  456. // Title hugs the leading edge; a low–hugging-priority spacer absorbs remaining width so buttons stay trailing.
  457. titleField.setContentHuggingPriority(.defaultHigh, for: .horizontal)
  458. titleField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  459. let titleRowSpacer = NSView()
  460. titleRowSpacer.translatesAutoresizingMaskIntoConstraints = false
  461. titleRowSpacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  462. titleRowSpacer.setContentCompressionResistancePriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  463. let titleAndActionsRow = NSStackView(views: [titleField, titleRowSpacer, buttonRow])
  464. titleAndActionsRow.orientation = .horizontal
  465. titleAndActionsRow.spacing = 0
  466. titleAndActionsRow.setCustomSpacing(14, after: titleRowSpacer)
  467. titleAndActionsRow.alignment = .centerY
  468. titleAndActionsRow.distribution = .fill
  469. titleAndActionsRow.userInterfaceLayoutDirection = .leftToRight
  470. titleAndActionsRow.translatesAutoresizingMaskIntoConstraints = false
  471. let contentColumn = NSStackView(views: [titleAndActionsRow, descriptionField])
  472. contentColumn.orientation = .vertical
  473. contentColumn.spacing = 6
  474. contentColumn.alignment = .width
  475. contentColumn.translatesAutoresizingMaskIntoConstraints = false
  476. card.addSubview(contentColumn)
  477. NSLayoutConstraint.activate([
  478. contentColumn.leadingAnchor.constraint(equalTo: card.leadingAnchor, constant: 16),
  479. contentColumn.trailingAnchor.constraint(equalTo: card.trailingAnchor, constant: -16),
  480. contentColumn.topAnchor.constraint(equalTo: card.topAnchor, constant: 14),
  481. contentColumn.bottomAnchor.constraint(equalTo: card.bottomAnchor, constant: -14),
  482. applyButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  483. applyButton.heightAnchor.constraint(equalToConstant: 28),
  484. savedButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 72),
  485. savedButton.heightAnchor.constraint(equalToConstant: 28),
  486. dismissButton.widthAnchor.constraint(equalToConstant: 28),
  487. dismissButton.heightAnchor.constraint(equalToConstant: 28),
  488. descriptionField.widthAnchor.constraint(equalTo: contentColumn.widthAnchor)
  489. ])
  490. return card
  491. }
  492. private func styleJobSavedButton(_ button: NSButton) {
  493. button.wantsLayer = true
  494. button.layer?.cornerRadius = 6
  495. let on = button.state == .on
  496. let hovering = (button as? HoverableButton)?.isHovering ?? false
  497. if on {
  498. button.layer?.backgroundColor = (hovering ? Theme.selectionFillHover : Theme.selectionFill).cgColor
  499. button.layer?.borderWidth = 1
  500. button.layer?.borderColor = Theme.brandBlue.cgColor
  501. button.contentTintColor = Theme.brandBlue
  502. } else {
  503. button.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  504. button.layer?.borderWidth = 1
  505. button.layer?.borderColor = Theme.border.cgColor
  506. button.contentTintColor = Theme.primaryText
  507. }
  508. }
  509. @objc private func didTapJobApply(_ sender: NSButton) {
  510. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  511. if let rawURL = job.url, let url = URL(string: rawURL), !rawURL.isEmpty {
  512. NSWorkspace.shared.open(url)
  513. return
  514. }
  515. let allowed = CharacterSet.urlQueryAllowed
  516. let q = job.title.addingPercentEncoding(withAllowedCharacters: allowed) ?? ""
  517. guard let url = URL(string: "https://www.indeed.com/jobs?q=\(q)") else { return }
  518. NSWorkspace.shared.open(url)
  519. }
  520. @objc private func didTapJobSaved(_ sender: NSButton) {
  521. guard let job = (sender as? JobPayloadButton)?.jobPayload else { return }
  522. let willSave = !isJobSaved(job)
  523. applySavedState(willSave, for: job)
  524. sender.state = willSave ? .on : .off
  525. sender.title = willSave ? "Saved" : "Save"
  526. styleJobSavedButton(sender)
  527. if isSavedJobsSidebarIndex(selectedSidebarIndex) {
  528. reloadSavedJobsListings()
  529. }
  530. }
  531. @objc private func didTapJobDismiss(_ sender: NSButton) {
  532. guard let button = sender as? JobPayloadButton, let job = button.jobPayload else { return }
  533. switch button.cardContext {
  534. case .homeSearchResults:
  535. removeJobCardFromChat(originating: button, job: job)
  536. case .savedJobsPage:
  537. applySavedState(false, for: job)
  538. reloadSavedJobsListings()
  539. }
  540. }
  541. /// Walks up from a dismiss button until it finds the enclosing chat job stack, then removes only the card that owns the button. Other chat history (older searches, the assistant summary text) is untouched.
  542. private func removeJobCardFromChat(originating button: NSView, job: JobListing) {
  543. var node: NSView? = button
  544. var card: NSView?
  545. var stack: ChatJobsStackView?
  546. while let v = node {
  547. if let parent = v.superview as? ChatJobsStackView {
  548. card = v
  549. stack = parent
  550. break
  551. }
  552. node = v.superview
  553. }
  554. guard let card, let stack else { return }
  555. stack.removeArrangedSubview(card)
  556. card.removeFromSuperview()
  557. lastSearchResults.removeAll { $0 == job }
  558. }
  559. private func configureSearchBar() {
  560. let pillCorner: CGFloat = 27
  561. let barHeight: CGFloat = 54
  562. searchBarShadowHost.translatesAutoresizingMaskIntoConstraints = false
  563. searchBarShadowHost.wantsLayer = true
  564. searchBarShadowHost.layer?.masksToBounds = false
  565. searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(0.18).cgColor
  566. searchBarShadowHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  567. searchBarShadowHost.layer?.shadowRadius = 10
  568. searchBarShadowHost.layer?.shadowOpacity = 1
  569. searchBarShadowHost.setContentHuggingPriority(.defaultHigh, for: .vertical)
  570. searchCard.translatesAutoresizingMaskIntoConstraints = false
  571. searchCard.wantsLayer = true
  572. searchCard.layer?.backgroundColor = Theme.cardBackground.cgColor
  573. searchCard.layer?.cornerRadius = pillCorner
  574. searchCard.layer?.borderWidth = 1
  575. searchCard.layer?.borderColor = Theme.searchBarBorder.cgColor
  576. searchCard.layer?.masksToBounds = true
  577. searchCard.hoverHandler = { [weak self] hovering in
  578. guard let self else { return }
  579. CATransaction.begin()
  580. CATransaction.setAnimationDuration(0.15)
  581. self.searchCard.layer?.backgroundColor = (hovering ? Theme.neutralHoverFill : Theme.cardBackground).cgColor
  582. self.searchCard.layer?.borderColor = (hovering ? Theme.searchBarBorderHover : Theme.searchBarBorder).cgColor
  583. self.searchBarShadowHost.layer?.shadowColor = NSColor.black.withAlphaComponent(hovering ? 0.24 : 0.18).cgColor
  584. self.searchBarShadowHost.layer?.shadowRadius = hovering ? 12 : 10
  585. CATransaction.commit()
  586. }
  587. searchBarShadowHost.addSubview(searchCard)
  588. func configureField(_ field: NSTextField, placeholder: String) {
  589. field.translatesAutoresizingMaskIntoConstraints = false
  590. field.isBordered = false
  591. field.drawsBackground = false
  592. field.focusRingType = .none
  593. field.font = .systemFont(ofSize: 14, weight: .regular)
  594. field.textColor = Theme.primaryText
  595. field.delegate = self
  596. field.placeholderAttributedString = NSAttributedString(
  597. string: placeholder,
  598. attributes: [
  599. .foregroundColor: Theme.secondaryText,
  600. .font: NSFont.systemFont(ofSize: 14, weight: .regular)
  601. ]
  602. )
  603. field.cell?.usesSingleLineMode = true
  604. field.cell?.wraps = false
  605. field.cell?.isScrollable = true
  606. field.target = self
  607. field.action = #selector(didSubmitSearch)
  608. }
  609. jobSearchIcon.translatesAutoresizingMaskIntoConstraints = false
  610. jobSearchIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .medium)
  611. jobSearchIcon.image = NSImage(systemSymbolName: "magnifyingglass", accessibilityDescription: "Job search")
  612. jobSearchIcon.contentTintColor = Theme.primaryText
  613. configureField(jobKeywordsField, placeholder: "Ask for roles, skills, salary, or job descriptions...")
  614. let ctaHeight: CGFloat = 42
  615. let ctaCorner = ctaHeight / 2
  616. findJobsCTAHost.translatesAutoresizingMaskIntoConstraints = false
  617. findJobsCTAHost.wantsLayer = true
  618. findJobsCTAHost.layer?.masksToBounds = false
  619. findJobsCTAHost.layer?.shadowColor = NSColor.black.cgColor
  620. findJobsCTAHost.layer?.shadowOpacity = 0.16
  621. findJobsCTAHost.layer?.shadowOffset = CGSize(width: 0, height: 2)
  622. findJobsCTAHost.layer?.shadowRadius = 6
  623. findJobsCTAChrome.translatesAutoresizingMaskIntoConstraints = false
  624. findJobsCTAChrome.wantsLayer = true
  625. findJobsCTAChrome.layer?.masksToBounds = true
  626. findJobsCTAChrome.layer?.cornerRadius = ctaCorner
  627. if #available(macOS 11.0, *) {
  628. findJobsCTAChrome.layer?.cornerCurve = .continuous
  629. }
  630. let gradient = CAGradientLayer()
  631. gradient.colors = [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  632. gradient.startPoint = CGPoint(x: 0.5, y: 1)
  633. gradient.endPoint = CGPoint(x: 0.5, y: 0)
  634. findJobsCTAChrome.layer?.addSublayer(gradient)
  635. findJobsCTAGradientLayer = gradient
  636. // Tracks hover over the full pill (the button only covers an inset area), so the gradient darkens whenever the mouse is anywhere over the CTA.
  637. findJobsCTAChrome.pointerCursor = true
  638. findJobsCTAChrome.hoverHandler = { [weak self] hovering in
  639. guard let layer = self?.findJobsCTAGradientLayer else { return }
  640. CATransaction.begin()
  641. CATransaction.setAnimationDuration(0.15)
  642. layer.colors = hovering
  643. ? [Theme.findJobsCTAHighlightHover.cgColor, Theme.brandBlueHover.cgColor]
  644. : [Theme.findJobsCTAHighlight.cgColor, Theme.brandBlue.cgColor]
  645. CATransaction.commit()
  646. }
  647. findJobsButton.translatesAutoresizingMaskIntoConstraints = false
  648. findJobsButton.title = ""
  649. findJobsButton.attributedTitle = NSAttributedString(
  650. string: "Send",
  651. attributes: [
  652. .font: NSFont.systemFont(ofSize: 14, weight: .semibold),
  653. .foregroundColor: Theme.proCTAText,
  654. .kern: 0.35
  655. ]
  656. )
  657. findJobsButton.isBordered = false
  658. findJobsButton.bezelStyle = .rounded
  659. findJobsButton.wantsLayer = true
  660. findJobsButton.layer?.backgroundColor = NSColor.clear.cgColor
  661. findJobsButton.focusRingType = .none
  662. findJobsButton.target = self
  663. findJobsButton.action = #selector(didSubmitSearch)
  664. findJobsButton.setContentHuggingPriority(.required, for: .horizontal)
  665. findJobsButton.setContentCompressionResistancePriority(.required, for: .horizontal)
  666. findJobsCTAHost.addSubview(findJobsCTAChrome)
  667. findJobsCTAHost.addSubview(findJobsButton)
  668. NSLayoutConstraint.activate([
  669. findJobsCTAChrome.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor),
  670. findJobsCTAChrome.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor),
  671. findJobsCTAChrome.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  672. findJobsCTAChrome.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor),
  673. findJobsButton.leadingAnchor.constraint(equalTo: findJobsCTAHost.leadingAnchor, constant: 14),
  674. findJobsButton.trailingAnchor.constraint(equalTo: findJobsCTAHost.trailingAnchor, constant: -14),
  675. findJobsButton.topAnchor.constraint(equalTo: findJobsCTAHost.topAnchor),
  676. findJobsButton.bottomAnchor.constraint(equalTo: findJobsCTAHost.bottomAnchor)
  677. ])
  678. let keywordsStack = NSStackView(views: [jobSearchIcon, jobKeywordsField])
  679. keywordsStack.orientation = .horizontal
  680. keywordsStack.spacing = 10
  681. keywordsStack.alignment = .centerY
  682. keywordsStack.translatesAutoresizingMaskIntoConstraints = false
  683. keywordsStack.edgeInsets = NSEdgeInsets(top: 0, left: 18, bottom: 0, right: 10)
  684. keywordsStack.setContentHuggingPriority(.defaultLow, for: .horizontal)
  685. let row = NSStackView(views: [keywordsStack, findJobsCTAHost])
  686. row.orientation = .horizontal
  687. row.spacing = 0
  688. row.alignment = .centerY
  689. row.distribution = .fill
  690. row.translatesAutoresizingMaskIntoConstraints = false
  691. row.edgeInsets = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 7)
  692. searchCard.addSubview(row)
  693. NSLayoutConstraint.activate([
  694. searchCard.leadingAnchor.constraint(equalTo: searchBarShadowHost.leadingAnchor),
  695. searchCard.trailingAnchor.constraint(equalTo: searchBarShadowHost.trailingAnchor),
  696. searchCard.topAnchor.constraint(equalTo: searchBarShadowHost.topAnchor),
  697. searchCard.bottomAnchor.constraint(equalTo: searchBarShadowHost.bottomAnchor),
  698. searchBarShadowHost.heightAnchor.constraint(equalToConstant: barHeight),
  699. row.leadingAnchor.constraint(equalTo: searchCard.leadingAnchor),
  700. row.trailingAnchor.constraint(equalTo: searchCard.trailingAnchor),
  701. row.topAnchor.constraint(equalTo: searchCard.topAnchor),
  702. row.bottomAnchor.constraint(equalTo: searchCard.bottomAnchor),
  703. jobSearchIcon.widthAnchor.constraint(equalToConstant: 18),
  704. jobSearchIcon.heightAnchor.constraint(equalToConstant: 18),
  705. findJobsCTAHost.heightAnchor.constraint(equalToConstant: ctaHeight),
  706. findJobsCTAHost.widthAnchor.constraint(greaterThanOrEqualToConstant: 112)
  707. ])
  708. searchCard.hoverHandler = nil
  709. }
  710. private func updateFindJobsCTAShadowPath() {
  711. guard findJobsCTAHost.bounds.width > 0, findJobsCTAHost.bounds.height > 0 else { return }
  712. let r = findJobsCTAHost.bounds
  713. let radius = min(r.height / 2, r.width / 2)
  714. findJobsCTAHost.layer?.shadowPath = CGPath(
  715. roundedRect: r,
  716. cornerWidth: radius,
  717. cornerHeight: radius,
  718. transform: nil
  719. )
  720. }
  721. private func configureNonHomePlaceholder() {
  722. nonHomeHost.translatesAutoresizingMaskIntoConstraints = false
  723. nonHomeHost.wantsLayer = true
  724. nonHomeHost.layer?.backgroundColor = Theme.mainHostBackground.cgColor
  725. nonHomeHost.isHidden = true
  726. nonHomeGenericContainer.translatesAutoresizingMaskIntoConstraints = false
  727. savedJobsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  728. settingsPageContainer.translatesAutoresizingMaskIntoConstraints = false
  729. nonHomeHost.addSubview(nonHomeGenericContainer)
  730. nonHomeHost.addSubview(savedJobsPageContainer)
  731. nonHomeHost.addSubview(settingsPageContainer)
  732. NSLayoutConstraint.activate([
  733. nonHomeGenericContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  734. nonHomeGenericContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  735. nonHomeGenericContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  736. nonHomeGenericContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  737. savedJobsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  738. savedJobsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  739. savedJobsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  740. savedJobsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor),
  741. settingsPageContainer.leadingAnchor.constraint(equalTo: nonHomeHost.leadingAnchor),
  742. settingsPageContainer.trailingAnchor.constraint(equalTo: nonHomeHost.trailingAnchor),
  743. settingsPageContainer.topAnchor.constraint(equalTo: nonHomeHost.topAnchor),
  744. settingsPageContainer.bottomAnchor.constraint(equalTo: nonHomeHost.bottomAnchor)
  745. ])
  746. nonHomeTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  747. nonHomeTitleLabel.textColor = Theme.primaryText
  748. nonHomeTitleLabel.alignment = .center
  749. nonHomeTitleLabel.maximumNumberOfLines = 1
  750. nonHomeSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  751. nonHomeSubtitleLabel.textColor = Theme.secondaryText
  752. nonHomeSubtitleLabel.alignment = .center
  753. nonHomeSubtitleLabel.maximumNumberOfLines = 0
  754. nonHomeSubtitleLabel.stringValue = "This area is not available in the preview build. Use Home to search jobs."
  755. let genericStack = NSStackView(views: [nonHomeTitleLabel, nonHomeSubtitleLabel])
  756. genericStack.orientation = .vertical
  757. genericStack.spacing = 10
  758. genericStack.alignment = .centerX
  759. genericStack.translatesAutoresizingMaskIntoConstraints = false
  760. nonHomeGenericContainer.addSubview(genericStack)
  761. NSLayoutConstraint.activate([
  762. genericStack.centerXAnchor.constraint(equalTo: nonHomeGenericContainer.centerXAnchor),
  763. genericStack.centerYAnchor.constraint(equalTo: nonHomeGenericContainer.centerYAnchor),
  764. genericStack.leadingAnchor.constraint(greaterThanOrEqualTo: nonHomeGenericContainer.leadingAnchor, constant: 32),
  765. genericStack.trailingAnchor.constraint(lessThanOrEqualTo: nonHomeGenericContainer.trailingAnchor, constant: -32),
  766. nonHomeSubtitleLabel.widthAnchor.constraint(lessThanOrEqualToConstant: 420)
  767. ])
  768. savedJobsPageTitleLabel.font = .systemFont(ofSize: 22, weight: .bold)
  769. savedJobsPageTitleLabel.textColor = Theme.primaryText
  770. savedJobsPageTitleLabel.alignment = .left
  771. savedJobsPageTitleLabel.maximumNumberOfLines = 1
  772. savedJobsPageSubtitleLabel.font = .systemFont(ofSize: 14, weight: .regular)
  773. savedJobsPageSubtitleLabel.textColor = Theme.secondaryText
  774. savedJobsPageSubtitleLabel.alignment = .left
  775. savedJobsPageSubtitleLabel.maximumNumberOfLines = 0
  776. savedJobsDocumentView.translatesAutoresizingMaskIntoConstraints = false
  777. savedJobsStack.orientation = .vertical
  778. savedJobsStack.spacing = 14
  779. savedJobsStack.alignment = .leading
  780. savedJobsStack.distribution = .fill
  781. savedJobsStack.translatesAutoresizingMaskIntoConstraints = false
  782. savedJobsStack.setContentHuggingPriority(.defaultHigh, for: .vertical)
  783. savedJobsStack.setHuggingPriority(.defaultLow, for: .horizontal)
  784. savedJobsDocumentView.addSubview(savedJobsStack)
  785. NSLayoutConstraint.activate([
  786. savedJobsStack.leadingAnchor.constraint(equalTo: savedJobsDocumentView.leadingAnchor),
  787. savedJobsStack.trailingAnchor.constraint(equalTo: savedJobsDocumentView.trailingAnchor),
  788. savedJobsStack.topAnchor.constraint(equalTo: savedJobsDocumentView.topAnchor),
  789. savedJobsStack.bottomAnchor.constraint(equalTo: savedJobsDocumentView.bottomAnchor)
  790. ])
  791. savedJobsScrollView.translatesAutoresizingMaskIntoConstraints = false
  792. savedJobsScrollView.hasVerticalScroller = true
  793. savedJobsScrollView.hasHorizontalScroller = false
  794. savedJobsScrollView.autohidesScrollers = true
  795. savedJobsScrollView.drawsBackground = false
  796. savedJobsScrollView.borderType = .noBorder
  797. savedJobsScrollView.documentView = savedJobsDocumentView
  798. savedJobsScrollView.setContentHuggingPriority(.defaultLow, for: .vertical)
  799. savedJobsScrollView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  800. let savedHeaderStack = NSStackView(views: [savedJobsPageTitleLabel, savedJobsPageSubtitleLabel])
  801. savedHeaderStack.orientation = .vertical
  802. savedHeaderStack.spacing = 6
  803. savedHeaderStack.alignment = .leading
  804. savedHeaderStack.translatesAutoresizingMaskIntoConstraints = false
  805. let savedOuterStack = NSStackView(views: [savedHeaderStack, savedJobsScrollView])
  806. savedOuterStack.orientation = .vertical
  807. savedOuterStack.spacing = 16
  808. // Leading alignment plus explicit column width keeps the title and subtitle on the same edge as the cards.
  809. savedOuterStack.alignment = .leading
  810. savedOuterStack.translatesAutoresizingMaskIntoConstraints = false
  811. savedJobsPageContainer.userInterfaceLayoutDirection = .leftToRight
  812. savedJobsPageContainer.addSubview(savedOuterStack)
  813. NSLayoutConstraint.activate([
  814. savedOuterStack.leadingAnchor.constraint(equalTo: savedJobsPageContainer.leadingAnchor, constant: 32),
  815. savedOuterStack.trailingAnchor.constraint(equalTo: savedJobsPageContainer.trailingAnchor, constant: -32),
  816. savedOuterStack.topAnchor.constraint(equalTo: savedJobsPageContainer.topAnchor, constant: 8),
  817. savedOuterStack.bottomAnchor.constraint(equalTo: savedJobsPageContainer.bottomAnchor),
  818. savedHeaderStack.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  819. savedJobsScrollView.widthAnchor.constraint(equalTo: savedOuterStack.widthAnchor),
  820. savedJobsDocumentView.topAnchor.constraint(equalTo: savedJobsScrollView.contentView.topAnchor),
  821. savedJobsDocumentView.leadingAnchor.constraint(equalTo: savedJobsScrollView.contentView.leadingAnchor),
  822. savedJobsDocumentView.widthAnchor.constraint(equalTo: savedJobsScrollView.contentView.widthAnchor)
  823. ])
  824. configureSettingsPage()
  825. }
  826. private func configureSettingsPage() {
  827. settingsPageContainer.wantsLayer = true
  828. settingsPageContainer.layer?.backgroundColor = Theme.settingsPageBackground.cgColor
  829. settingsPageContainer.isHidden = true
  830. let contentStack = NSStackView()
  831. contentStack.orientation = .vertical
  832. contentStack.spacing = 26
  833. contentStack.alignment = .leading
  834. contentStack.translatesAutoresizingMaskIntoConstraints = false
  835. let settingsSection = makeSettingsSection(rows: [
  836. makeSettingsRow(title: "Share App", systemImage: "square.and.arrow.up", accessory: nil),
  837. makeSettingsRow(title: "Theme", systemImage: "circle.lefthalf.filled", accessory: makeThemeControl()),
  838. makeSettingsRow(title: "More Apps", systemImage: "square.grid.2x2", accessory: nil)
  839. ])
  840. let aboutTitle = NSTextField(labelWithString: "About")
  841. aboutTitle.font = .systemFont(ofSize: 12, weight: .semibold)
  842. aboutTitle.textColor = Theme.secondaryText
  843. aboutTitle.alignment = .left
  844. let aboutSection = makeSettingsSection(rows: [
  845. makeSettingsRow(title: "Support", systemImage: "questionmark.circle", accessory: nil),
  846. makeSettingsRow(title: "Terms of Use", systemImage: "doc.text", accessory: nil),
  847. makeSettingsRow(title: "Privacy Policy", systemImage: "shield", accessory: nil)
  848. ])
  849. let aboutStack = NSStackView(views: [aboutTitle, aboutSection])
  850. aboutStack.orientation = .vertical
  851. aboutStack.spacing = 14
  852. aboutStack.alignment = .leading
  853. aboutStack.translatesAutoresizingMaskIntoConstraints = false
  854. contentStack.addArrangedSubview(settingsSection)
  855. contentStack.addArrangedSubview(aboutStack)
  856. settingsPageContainer.addSubview(contentStack)
  857. NSLayoutConstraint.activate([
  858. contentStack.leadingAnchor.constraint(equalTo: settingsPageContainer.leadingAnchor, constant: 42),
  859. contentStack.trailingAnchor.constraint(lessThanOrEqualTo: settingsPageContainer.trailingAnchor, constant: -42),
  860. contentStack.topAnchor.constraint(equalTo: settingsPageContainer.topAnchor, constant: 48),
  861. settingsSection.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  862. aboutStack.widthAnchor.constraint(equalTo: contentStack.widthAnchor),
  863. aboutSection.widthAnchor.constraint(equalTo: aboutStack.widthAnchor),
  864. contentStack.widthAnchor.constraint(equalTo: settingsPageContainer.widthAnchor, constant: -84)
  865. ])
  866. }
  867. private func makeThemeControl() -> NSSegmentedControl {
  868. themeControl.target = self
  869. themeControl.action = #selector(didChangeThemeSelection(_:))
  870. themeControl.selectedSegment = 0
  871. themeControl.segmentStyle = .rounded
  872. themeControl.controlSize = .large
  873. themeControl.font = .systemFont(ofSize: 13, weight: .semibold)
  874. themeControl.translatesAutoresizingMaskIntoConstraints = false
  875. themeControl.widthAnchor.constraint(equalToConstant: 204).isActive = true
  876. themeControl.heightAnchor.constraint(equalToConstant: 30).isActive = true
  877. return themeControl
  878. }
  879. private func makeSettingsSection(rows: [NSView]) -> NSView {
  880. let section = NSStackView()
  881. section.orientation = .vertical
  882. section.spacing = 0
  883. section.alignment = .leading
  884. section.translatesAutoresizingMaskIntoConstraints = false
  885. section.wantsLayer = true
  886. section.layer?.backgroundColor = Theme.settingsGroupBackground.cgColor
  887. section.layer?.cornerRadius = 14
  888. section.layer?.borderWidth = 1
  889. section.layer?.borderColor = Theme.border.cgColor
  890. section.layer?.masksToBounds = true
  891. for (index, row) in rows.enumerated() {
  892. section.addArrangedSubview(row)
  893. row.widthAnchor.constraint(equalTo: section.widthAnchor).isActive = true
  894. if index < rows.count - 1 {
  895. let divider = NSView()
  896. divider.translatesAutoresizingMaskIntoConstraints = false
  897. divider.wantsLayer = true
  898. divider.layer?.backgroundColor = Theme.settingsDivider.cgColor
  899. section.addArrangedSubview(divider)
  900. NSLayoutConstraint.activate([
  901. divider.heightAnchor.constraint(equalToConstant: 1),
  902. divider.leadingAnchor.constraint(equalTo: section.leadingAnchor),
  903. divider.trailingAnchor.constraint(equalTo: section.trailingAnchor)
  904. ])
  905. }
  906. }
  907. return section
  908. }
  909. private func makeSettingsRow(title: String, systemImage: String, accessory: NSView?) -> NSView {
  910. let row = NSView()
  911. row.translatesAutoresizingMaskIntoConstraints = false
  912. row.wantsLayer = true
  913. let iconTile = NSView()
  914. iconTile.translatesAutoresizingMaskIntoConstraints = false
  915. iconTile.wantsLayer = true
  916. iconTile.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  917. iconTile.layer?.cornerRadius = 9
  918. let icon = NSImageView()
  919. icon.translatesAutoresizingMaskIntoConstraints = false
  920. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  921. icon.image = NSImage(systemSymbolName: systemImage, accessibilityDescription: title)
  922. icon.contentTintColor = Theme.brandBlue
  923. let titleLabel = NSTextField(labelWithString: title)
  924. titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
  925. titleLabel.textColor = Theme.secondaryText
  926. titleLabel.alignment = .left
  927. let rowStack = NSStackView()
  928. rowStack.orientation = .horizontal
  929. rowStack.spacing = 16
  930. rowStack.alignment = .centerY
  931. rowStack.translatesAutoresizingMaskIntoConstraints = false
  932. let spacer = NSView()
  933. spacer.translatesAutoresizingMaskIntoConstraints = false
  934. spacer.setContentHuggingPriority(NSLayoutConstraint.Priority(1), for: .horizontal)
  935. iconTile.addSubview(icon)
  936. rowStack.addArrangedSubview(iconTile)
  937. rowStack.addArrangedSubview(titleLabel)
  938. rowStack.addArrangedSubview(spacer)
  939. if let accessory {
  940. rowStack.addArrangedSubview(accessory)
  941. }
  942. row.addSubview(rowStack)
  943. NSLayoutConstraint.activate([
  944. row.heightAnchor.constraint(equalToConstant: 68),
  945. iconTile.widthAnchor.constraint(equalToConstant: 38),
  946. iconTile.heightAnchor.constraint(equalToConstant: 38),
  947. icon.centerXAnchor.constraint(equalTo: iconTile.centerXAnchor),
  948. icon.centerYAnchor.constraint(equalTo: iconTile.centerYAnchor),
  949. icon.widthAnchor.constraint(equalToConstant: 20),
  950. icon.heightAnchor.constraint(equalToConstant: 20),
  951. rowStack.leadingAnchor.constraint(equalTo: row.leadingAnchor, constant: 16),
  952. rowStack.trailingAnchor.constraint(equalTo: row.trailingAnchor, constant: -16),
  953. rowStack.topAnchor.constraint(equalTo: row.topAnchor),
  954. rowStack.bottomAnchor.constraint(equalTo: row.bottomAnchor)
  955. ])
  956. return row
  957. }
  958. private func reloadSavedJobsListings() {
  959. savedJobsStack.arrangedSubviews.forEach {
  960. savedJobsStack.removeArrangedSubview($0)
  961. $0.removeFromSuperview()
  962. }
  963. if savedJobOrder.isEmpty {
  964. savedJobsPageSubtitleLabel.stringValue = "Save jobs from Home to see them here."
  965. let empty = NSTextField(wrappingLabelWithString: "No saved jobs yet. Search on Home, then tap Save on a listing.")
  966. empty.font = .systemFont(ofSize: 14, weight: .regular)
  967. empty.textColor = Theme.secondaryText
  968. empty.alignment = .left
  969. empty.maximumNumberOfLines = 0
  970. empty.translatesAutoresizingMaskIntoConstraints = false
  971. savedJobsStack.addArrangedSubview(empty)
  972. empty.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  973. return
  974. }
  975. savedJobsPageSubtitleLabel.stringValue = "\(savedJobOrder.count) saved \(savedJobOrder.count == 1 ? "position" : "positions")"
  976. for job in savedJobOrder {
  977. let card = makeJobListingCard(job, context: .savedJobsPage)
  978. savedJobsStack.addArrangedSubview(card)
  979. card.widthAnchor.constraint(equalTo: savedJobsStack.widthAnchor).isActive = true
  980. }
  981. }
  982. private func isSavedJobsSidebarIndex(_ index: Int) -> Bool {
  983. guard index >= 0, index < currentSidebarItems.count else { return false }
  984. return currentSidebarItems[index].title == "Saved Jobs"
  985. }
  986. private func isHomeSidebarIndex(_ index: Int) -> Bool {
  987. guard index >= 0, index < currentSidebarItems.count else { return false }
  988. return currentSidebarItems[index].title == "Home"
  989. }
  990. private func isSettingsSidebarIndex(_ index: Int) -> Bool {
  991. guard index >= 0, index < currentSidebarItems.count else { return false }
  992. return currentSidebarItems[index].title == "Settings"
  993. }
  994. private func updateMainContentVisibility() {
  995. let home = isHomeSidebarIndex(selectedSidebarIndex)
  996. let savedJobs = isSavedJobsSidebarIndex(selectedSidebarIndex)
  997. let settings = isSettingsSidebarIndex(selectedSidebarIndex)
  998. mainOverlay.isHidden = !home
  999. nonHomeHost.isHidden = home
  1000. nonHomeGenericContainer.isHidden = savedJobs || settings
  1001. savedJobsPageContainer.isHidden = !savedJobs
  1002. settingsPageContainer.isHidden = !settings
  1003. if !home, selectedSidebarIndex < currentSidebarItems.count {
  1004. if savedJobs {
  1005. reloadSavedJobsListings()
  1006. } else if settings {
  1007. window?.makeFirstResponder(nil)
  1008. } else {
  1009. nonHomeTitleLabel.stringValue = currentSidebarItems[selectedSidebarIndex].title
  1010. }
  1011. }
  1012. }
  1013. /// Restores the main job-search experience: cleared query and a fresh chat history.
  1014. private func applyHomeState() {
  1015. jobKeywordsField.stringValue = ""
  1016. resetChatState()
  1017. window?.makeFirstResponder(nil)
  1018. }
  1019. private func updateSearchBarShadowPath() {
  1020. guard searchBarShadowHost.bounds.width > 0, searchBarShadowHost.bounds.height > 0 else { return }
  1021. let r = searchBarShadowHost.bounds
  1022. let radius = min(r.height / 2, 27)
  1023. searchBarShadowHost.layer?.shadowPath = CGPath(
  1024. roundedRect: r,
  1025. cornerWidth: radius,
  1026. cornerHeight: radius,
  1027. transform: nil
  1028. )
  1029. }
  1030. @objc private func didSubmitSearch() {
  1031. let prompt = jobKeywordsField.stringValue.trimmingCharacters(in: .whitespacesAndNewlines)
  1032. guard !prompt.isEmpty, !isAwaitingResponse else { return }
  1033. let isContinuation = isContinuationPrompt(prompt)
  1034. let effectiveQuery = resolvedSearchQuery(for: prompt)
  1035. appendChatBubble(text: prompt, isUser: true)
  1036. chatMessages.append(ChatMessage(role: "user", content: prompt))
  1037. jobKeywordsField.stringValue = ""
  1038. isAwaitingResponse = true
  1039. chatStatusLabel.stringValue = "Thinking..."
  1040. setInputEnabled(false)
  1041. let contextMessages = chatMessages
  1042. jobSearchService.searchJobs(query: effectiveQuery, conversation: contextMessages) { [weak self] result in
  1043. DispatchQueue.main.async {
  1044. guard let self else { return }
  1045. self.isAwaitingResponse = false
  1046. self.setInputEnabled(true)
  1047. switch result {
  1048. case .success(let output):
  1049. let normalizedJobs = self.normalizedJobs(output.jobs)
  1050. let freshJobs: [JobListing]
  1051. if isContinuation {
  1052. // Continuations append only the *new* matches; previous cards already live in their own assistant message above.
  1053. let alreadySeen = Set(self.lastSearchResults)
  1054. freshJobs = normalizedJobs.filter { !alreadySeen.contains($0) }
  1055. } else {
  1056. freshJobs = normalizedJobs
  1057. }
  1058. self.lastSearchResults.append(contentsOf: freshJobs)
  1059. let reply = self.makeAssistantSearchReply(
  1060. query: effectiveQuery,
  1061. newJobsCount: freshJobs.count,
  1062. isContinuation: isContinuation
  1063. )
  1064. self.chatMessages.append(ChatMessage(role: "assistant", content: reply))
  1065. self.appendChatBubble(text: reply, isUser: false, jobs: freshJobs)
  1066. self.chatStatusLabel.stringValue = "Ask for another role, company, or skill match"
  1067. case .failure(let error):
  1068. self.appendChatBubble(text: error.localizedDescription, isUser: false)
  1069. if error is URLError {
  1070. self.chatStatusLabel.stringValue = "Could not reach API. Try again."
  1071. } else {
  1072. self.chatStatusLabel.stringValue = "Search did not finish. Try again."
  1073. }
  1074. }
  1075. }
  1076. }
  1077. window?.makeFirstResponder(nil)
  1078. }
  1079. private func normalizedJobs(_ jobs: [JobListing]) -> [JobListing] {
  1080. let trimmed = jobs.map {
  1081. JobListing(
  1082. title: $0.title.trimmingCharacters(in: .whitespacesAndNewlines),
  1083. description: $0.description.trimmingCharacters(in: .whitespacesAndNewlines),
  1084. url: $0.url?.trimmingCharacters(in: .whitespacesAndNewlines)
  1085. )
  1086. }
  1087. return trimmed.filter { !$0.title.isEmpty && !$0.description.isEmpty }
  1088. }
  1089. private func makeAssistantSearchReply(query: String, newJobsCount: Int, isContinuation: Bool) -> String {
  1090. if newJobsCount == 0 {
  1091. if isContinuation {
  1092. return "I couldn't find new matches for \u{201C}\(query)\u{201D}. Try a different angle or a more specific keyword."
  1093. }
  1094. return "No jobs found for \u{201C}\(query)\u{201D}. Try another title, skill, company, or location."
  1095. }
  1096. let plural = newJobsCount == 1 ? "match" : "matches"
  1097. if isContinuation {
  1098. return "Here are \(newJobsCount) more \(plural) for \u{201C}\(query)\u{201D}."
  1099. }
  1100. return "Found \(newJobsCount) \(plural) for \u{201C}\(query)\u{201D}. Tap Apply to open the listing or Save to revisit later."
  1101. }
  1102. private func resolvedSearchQuery(for prompt: String) -> String {
  1103. guard isContinuationPrompt(prompt) else { return prompt }
  1104. for message in chatMessages.reversed() where message.role == "user" {
  1105. let candidate = message.content.trimmingCharacters(in: .whitespacesAndNewlines)
  1106. if !candidate.isEmpty, !isContinuationPrompt(candidate) {
  1107. return candidate
  1108. }
  1109. }
  1110. return prompt
  1111. }
  1112. private func isContinuationPrompt(_ prompt: String) -> Bool {
  1113. let normalized = prompt.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
  1114. let continuationPhrases: Set<String> = [
  1115. "more",
  1116. "show more",
  1117. "more jobs",
  1118. "more results",
  1119. "do more searches",
  1120. "more searches",
  1121. "search more",
  1122. "continue",
  1123. "next"
  1124. ]
  1125. if continuationPhrases.contains(normalized) {
  1126. return true
  1127. }
  1128. return normalized.contains("more search") || normalized.contains("more job")
  1129. }
  1130. func controlTextDidBeginEditing(_ obj: Notification) {
  1131. applySearchFieldInsertionPoint(obj.object)
  1132. if (obj.object as? NSTextField) === jobKeywordsField {
  1133. chatStatusLabel.stringValue = "Opening the vault..."
  1134. }
  1135. }
  1136. func controlTextDidChange(_ obj: Notification) {
  1137. applySearchFieldInsertionPoint(obj.object)
  1138. }
  1139. func control(_ control: NSControl, textView: NSTextView, doCommandBy commandSelector: Selector) -> Bool {
  1140. guard control === jobKeywordsField, commandSelector == #selector(NSResponder.insertNewline(_:)) else {
  1141. return false
  1142. }
  1143. didSubmitSearch()
  1144. return true
  1145. }
  1146. private func applySearchFieldInsertionPoint(_ object: Any?) {
  1147. guard let field = object as? NSTextField,
  1148. field === jobKeywordsField,
  1149. let textView = field.window?.fieldEditor(true, for: field) as? NSTextView else { return }
  1150. textView.insertionPointColor = Theme.primaryText
  1151. }
  1152. private func resetChatState() {
  1153. chatMessages.removeAll()
  1154. lastSearchResults.removeAll()
  1155. chatStack.arrangedSubviews.forEach {
  1156. chatStack.removeArrangedSubview($0)
  1157. $0.removeFromSuperview()
  1158. }
  1159. let welcome = "Tell me what role you want and I will return job descriptions, key skills, and a quick fit summary."
  1160. chatMessages.append(ChatMessage(role: "assistant", content: welcome))
  1161. appendChatBubble(text: welcome, isUser: false)
  1162. chatStatusLabel.stringValue = "Ask me to find jobs"
  1163. }
  1164. private func appendChatBubble(text: String, isUser: Bool, jobs: [JobListing]? = nil) {
  1165. let host = NSView()
  1166. host.translatesAutoresizingMaskIntoConstraints = false
  1167. if isUser {
  1168. installUserBubble(text: text, into: host)
  1169. } else {
  1170. installAssistantBubble(text: text, jobs: jobs, into: host)
  1171. }
  1172. chatStack.addArrangedSubview(host)
  1173. host.widthAnchor.constraint(equalTo: chatStack.widthAnchor).isActive = true
  1174. DispatchQueue.main.async { [weak self] in
  1175. self?.updateChatBubbleWidths()
  1176. self?.scrollChatToBottom()
  1177. }
  1178. }
  1179. private func installUserBubble(text: String, into host: NSView) {
  1180. let bubble = makeChatBubbleContainer(text: text, isUser: true)
  1181. host.addSubview(bubble)
  1182. NSLayoutConstraint.activate([
  1183. bubble.topAnchor.constraint(equalTo: host.topAnchor),
  1184. bubble.bottomAnchor.constraint(equalTo: host.bottomAnchor),
  1185. bubble.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1186. bubble.leadingAnchor.constraint(greaterThanOrEqualTo: host.leadingAnchor, constant: 64),
  1187. bubble.widthAnchor.constraint(lessThanOrEqualTo: host.widthAnchor, multiplier: 0.78)
  1188. ])
  1189. }
  1190. private func installAssistantBubble(text: String, jobs: [JobListing]?, into host: NSView) {
  1191. let avatar = makeAssistantAvatarView()
  1192. let nameLabel = NSTextField(labelWithString: "AI Job Finder")
  1193. nameLabel.font = .systemFont(ofSize: 11, weight: .semibold)
  1194. nameLabel.textColor = Theme.secondaryText
  1195. nameLabel.translatesAutoresizingMaskIntoConstraints = false
  1196. let bubble = makeChatBubbleContainer(text: text, isUser: false)
  1197. let column = NSStackView(views: [nameLabel, bubble])
  1198. column.orientation = .vertical
  1199. column.spacing = 6
  1200. column.alignment = .leading
  1201. column.translatesAutoresizingMaskIntoConstraints = false
  1202. if let jobs, !jobs.isEmpty {
  1203. let jobsStack = makeChatJobsStackView(jobs: jobs)
  1204. column.addArrangedSubview(jobsStack)
  1205. jobsStack.widthAnchor.constraint(equalTo: column.widthAnchor).isActive = true
  1206. }
  1207. host.addSubview(avatar)
  1208. host.addSubview(column)
  1209. NSLayoutConstraint.activate([
  1210. avatar.leadingAnchor.constraint(equalTo: host.leadingAnchor),
  1211. avatar.topAnchor.constraint(equalTo: host.topAnchor),
  1212. avatar.widthAnchor.constraint(equalToConstant: 36),
  1213. avatar.heightAnchor.constraint(equalToConstant: 36),
  1214. column.leadingAnchor.constraint(equalTo: avatar.trailingAnchor, constant: 12),
  1215. column.trailingAnchor.constraint(equalTo: host.trailingAnchor),
  1216. column.topAnchor.constraint(equalTo: host.topAnchor),
  1217. column.bottomAnchor.constraint(equalTo: host.bottomAnchor)
  1218. ])
  1219. }
  1220. private func makeChatBubbleContainer(text: String, isUser: Bool) -> NSView {
  1221. let container = NSView()
  1222. container.translatesAutoresizingMaskIntoConstraints = false
  1223. container.wantsLayer = true
  1224. container.layer?.cornerRadius = 14
  1225. if #available(macOS 11.0, *) {
  1226. container.layer?.cornerCurve = .continuous
  1227. }
  1228. container.layer?.masksToBounds = true
  1229. if isUser {
  1230. container.layer?.backgroundColor = Theme.brandBlue.cgColor
  1231. } else {
  1232. container.layer?.backgroundColor = Theme.chromeBackground.cgColor
  1233. container.layer?.borderWidth = 1
  1234. container.layer?.borderColor = Theme.border.cgColor
  1235. }
  1236. let label = ChatBubbleLabel(wrappingLabelWithString: text)
  1237. label.font = .systemFont(ofSize: 13.5, weight: .regular)
  1238. label.textColor = isUser ? .white : Theme.primaryText
  1239. label.maximumNumberOfLines = 0
  1240. label.lineBreakMode = .byWordWrapping
  1241. label.alignment = .left
  1242. label.translatesAutoresizingMaskIntoConstraints = false
  1243. label.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1244. label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
  1245. container.addSubview(label)
  1246. NSLayoutConstraint.activate([
  1247. label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 14),
  1248. label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -14),
  1249. label.topAnchor.constraint(equalTo: container.topAnchor, constant: 10),
  1250. label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -10)
  1251. ])
  1252. return container
  1253. }
  1254. private func makeAssistantAvatarView() -> NSView {
  1255. let view = NSView()
  1256. view.translatesAutoresizingMaskIntoConstraints = false
  1257. view.wantsLayer = true
  1258. view.layer?.cornerRadius = 18
  1259. if #available(macOS 11.0, *) {
  1260. view.layer?.cornerCurve = .continuous
  1261. }
  1262. view.layer?.backgroundColor = Theme.settingsIconBackground.cgColor
  1263. view.layer?.borderWidth = 1
  1264. view.layer?.borderColor = Theme.proCardBorder.cgColor
  1265. let icon = NSImageView()
  1266. icon.translatesAutoresizingMaskIntoConstraints = false
  1267. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold)
  1268. icon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: "AI Job Finder")
  1269. icon.contentTintColor = Theme.brandBlue
  1270. view.addSubview(icon)
  1271. NSLayoutConstraint.activate([
  1272. icon.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  1273. icon.centerYAnchor.constraint(equalTo: view.centerYAnchor)
  1274. ])
  1275. return view
  1276. }
  1277. private func makeChatJobsStackView(jobs: [JobListing]) -> ChatJobsStackView {
  1278. let stack = ChatJobsStackView()
  1279. stack.orientation = .vertical
  1280. stack.spacing = 10
  1281. stack.alignment = .leading
  1282. stack.distribution = .fill
  1283. stack.translatesAutoresizingMaskIntoConstraints = false
  1284. for job in jobs {
  1285. let card = makeJobListingCard(job, context: .homeSearchResults)
  1286. stack.addArrangedSubview(card)
  1287. card.widthAnchor.constraint(equalTo: stack.widthAnchor).isActive = true
  1288. }
  1289. return stack
  1290. }
  1291. private func scrollChatToBottom() {
  1292. let maxY = max(0, chatDocumentView.bounds.height - chatScrollView.contentView.bounds.height)
  1293. chatScrollView.contentView.scroll(to: NSPoint(x: 0, y: maxY))
  1294. chatScrollView.reflectScrolledClipView(chatScrollView.contentView)
  1295. }
  1296. private func setInputEnabled(_ enabled: Bool) {
  1297. jobKeywordsField.isEnabled = enabled
  1298. findJobsButton.isEnabled = enabled
  1299. findJobsButton.alphaValue = enabled ? 1 : 0.65
  1300. }
  1301. private func configureSidebar() {
  1302. let items = currentSidebarItems
  1303. sidebar.arrangedSubviews.forEach {
  1304. sidebar.removeArrangedSubview($0)
  1305. $0.removeFromSuperview()
  1306. }
  1307. let brand = NSTextField(labelWithString: "Indeed AI\nJob Finder")
  1308. brand.font = .systemFont(ofSize: 18, weight: .bold)
  1309. brand.textColor = Theme.brandBlue
  1310. brand.alignment = .left
  1311. brand.maximumNumberOfLines = 2
  1312. // Tight multiline height in the sidebar stack (zero width makes intrinsic height unreliable).
  1313. brand.preferredMaxLayoutWidth = 194
  1314. sidebar.addArrangedSubview(brand)
  1315. sidebar.setCustomSpacing(10, after: brand)
  1316. items.enumerated().forEach { index, item in
  1317. let isSelected = index == selectedSidebarIndex
  1318. let rowHost = SidebarNavRowView { [weak self] in
  1319. self?.selectSidebarItem(at: index)
  1320. }
  1321. rowHost.translatesAutoresizingMaskIntoConstraints = false
  1322. rowHost.wantsLayer = true
  1323. rowHost.layer?.cornerRadius = 8
  1324. rowHost.restingBackgroundColor = isSelected ? Theme.selectionFill : nil
  1325. rowHost.hoverBackgroundColor = isSelected ? Theme.selectionFillHover : Theme.sidebarRowHoverFill
  1326. rowHost.setAccessibilityLabel(item.title)
  1327. rowHost.setAccessibilityRole(.button)
  1328. rowHost.setAccessibilitySelected(isSelected)
  1329. let row = NSStackView()
  1330. row.orientation = .horizontal
  1331. row.spacing = 8
  1332. row.alignment = .centerY
  1333. row.translatesAutoresizingMaskIntoConstraints = false
  1334. let icon = NSImageView()
  1335. icon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .medium)
  1336. icon.image = NSImage(systemSymbolName: item.systemImage, accessibilityDescription: item.title)
  1337. icon.contentTintColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1338. let text = NSTextField(labelWithString: item.title)
  1339. text.font = .systemFont(ofSize: 14, weight: .medium)
  1340. text.textColor = isSelected ? Theme.brandBlue : Theme.secondaryText
  1341. text.refusesFirstResponder = true
  1342. row.addArrangedSubview(icon)
  1343. row.addArrangedSubview(text)
  1344. if let badge = item.badge {
  1345. let badgeField = NSTextField(labelWithString: badge)
  1346. badgeField.font = .systemFont(ofSize: 11, weight: .semibold)
  1347. badgeField.textColor = Theme.primaryText
  1348. badgeField.wantsLayer = true
  1349. badgeField.layer?.backgroundColor = Theme.toggleBackground.cgColor
  1350. badgeField.layer?.cornerRadius = 8
  1351. badgeField.alignment = .center
  1352. badgeField.maximumNumberOfLines = 1
  1353. badgeField.lineBreakMode = .byClipping
  1354. badgeField.refusesFirstResponder = true
  1355. badgeField.translatesAutoresizingMaskIntoConstraints = false
  1356. badgeField.widthAnchor.constraint(equalToConstant: 42).isActive = true
  1357. row.addArrangedSubview(NSView())
  1358. row.addArrangedSubview(badgeField)
  1359. }
  1360. rowHost.addSubview(row)
  1361. NSLayoutConstraint.activate([
  1362. row.leadingAnchor.constraint(equalTo: rowHost.leadingAnchor, constant: 10),
  1363. row.trailingAnchor.constraint(equalTo: rowHost.trailingAnchor, constant: -10),
  1364. row.topAnchor.constraint(equalTo: rowHost.topAnchor, constant: 8),
  1365. row.bottomAnchor.constraint(equalTo: rowHost.bottomAnchor, constant: -8)
  1366. ])
  1367. rowHost.setContentHuggingPriority(.defaultLow, for: .horizontal)
  1368. sidebar.addArrangedSubview(rowHost)
  1369. let sidebarHorizontalInset = sidebar.edgeInsets.left + sidebar.edgeInsets.right
  1370. rowHost.widthAnchor.constraint(equalTo: sidebar.widthAnchor, constant: -sidebarHorizontalInset).isActive = true
  1371. }
  1372. let sidebarBottomSpacer = NSView()
  1373. sidebarBottomSpacer.translatesAutoresizingMaskIntoConstraints = false
  1374. sidebarBottomSpacer.setContentHuggingPriority(.defaultLow, for: .vertical)
  1375. sidebarBottomSpacer.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
  1376. sidebar.addArrangedSubview(sidebarBottomSpacer)
  1377. let upgradeCard = NSView()
  1378. upgradeCard.translatesAutoresizingMaskIntoConstraints = false
  1379. upgradeCard.wantsLayer = true
  1380. upgradeCard.layer?.backgroundColor = Theme.proCardFill.cgColor
  1381. upgradeCard.layer?.cornerRadius = 14
  1382. upgradeCard.layer?.borderWidth = 1
  1383. upgradeCard.layer?.borderColor = Theme.proCardBorder.cgColor
  1384. upgradeCard.layer?.masksToBounds = true
  1385. let accentBar = NSView()
  1386. accentBar.translatesAutoresizingMaskIntoConstraints = false
  1387. accentBar.wantsLayer = true
  1388. accentBar.layer?.backgroundColor = Theme.proAccent.cgColor
  1389. let inner = NSStackView()
  1390. inner.translatesAutoresizingMaskIntoConstraints = false
  1391. inner.orientation = .vertical
  1392. inner.spacing = 10
  1393. inner.alignment = .centerX
  1394. let proIcon = NSImageView()
  1395. proIcon.translatesAutoresizingMaskIntoConstraints = false
  1396. proIcon.symbolConfiguration = NSImage.SymbolConfiguration(pointSize: 13, weight: .semibold)
  1397. proIcon.image = NSImage(systemSymbolName: "sparkles", accessibilityDescription: nil)
  1398. proIcon.contentTintColor = Theme.proAccent
  1399. let proEyebrow = NSTextField(labelWithString: "Premium")
  1400. proEyebrow.font = .systemFont(ofSize: 11, weight: .heavy)
  1401. proEyebrow.textColor = Theme.proAccent
  1402. proEyebrow.alignment = .center
  1403. let eyebrowRow = NSStackView(views: [proIcon, proEyebrow])
  1404. eyebrowRow.orientation = .horizontal
  1405. eyebrowRow.spacing = 6
  1406. eyebrowRow.alignment = .centerY
  1407. let headline = NSTextField(labelWithString: "Upgrade to Pro")
  1408. headline.font = .systemFont(ofSize: 16, weight: .bold)
  1409. headline.textColor = Theme.primaryText
  1410. headline.alignment = .center
  1411. let upgradeDescription = NSTextField(wrappingLabelWithString: "Unlimited AI matches, smart alerts, and interview prep—all in one place.")
  1412. upgradeDescription.font = .systemFont(ofSize: 12, weight: .regular)
  1413. upgradeDescription.textColor = Theme.secondaryText
  1414. upgradeDescription.alignment = .center
  1415. // Sidebar content width is the fixed sidebar width minus horizontal edge insets; card must stay within that band.
  1416. let cardWidth: CGFloat = 186
  1417. let innerContentWidth = cardWidth - 28
  1418. upgradeDescription.preferredMaxLayoutWidth = innerContentWidth
  1419. let upgradeButton = HoverableButton(title: "Upgrade to Pro", target: self, action: #selector(didTapUpgradeToPro))
  1420. upgradeButton.isBordered = false
  1421. upgradeButton.bezelStyle = .rounded
  1422. upgradeButton.font = .systemFont(ofSize: 13, weight: .bold)
  1423. upgradeButton.contentTintColor = Theme.proCTAText
  1424. upgradeButton.alignment = .center
  1425. upgradeButton.wantsLayer = true
  1426. upgradeButton.layer?.backgroundColor = Theme.proCTABackground.cgColor
  1427. upgradeButton.layer?.cornerRadius = 20
  1428. upgradeButton.translatesAutoresizingMaskIntoConstraints = false
  1429. upgradeButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
  1430. upgradeButton.pointerCursor = true
  1431. upgradeButton.hoverHandler = { [weak upgradeButton] hovering in
  1432. upgradeButton?.layer?.backgroundColor = (hovering ? Theme.brandBlueHover : Theme.proCTABackground).cgColor
  1433. }
  1434. inner.addArrangedSubview(eyebrowRow)
  1435. inner.addArrangedSubview(headline)
  1436. inner.addArrangedSubview(upgradeDescription)
  1437. inner.addArrangedSubview(upgradeButton)
  1438. upgradeCard.addSubview(accentBar)
  1439. upgradeCard.addSubview(inner)
  1440. NSLayoutConstraint.activate([
  1441. upgradeCard.widthAnchor.constraint(equalToConstant: cardWidth),
  1442. accentBar.topAnchor.constraint(equalTo: upgradeCard.topAnchor),
  1443. accentBar.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor),
  1444. accentBar.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor),
  1445. accentBar.heightAnchor.constraint(equalToConstant: 2),
  1446. inner.leadingAnchor.constraint(equalTo: upgradeCard.leadingAnchor, constant: 14),
  1447. inner.trailingAnchor.constraint(equalTo: upgradeCard.trailingAnchor, constant: -14),
  1448. inner.topAnchor.constraint(equalTo: accentBar.bottomAnchor, constant: 12),
  1449. inner.bottomAnchor.constraint(equalTo: upgradeCard.bottomAnchor, constant: -14),
  1450. upgradeButton.widthAnchor.constraint(equalTo: inner.widthAnchor)
  1451. ])
  1452. sidebar.addArrangedSubview(upgradeCard)
  1453. }
  1454. @objc private func didTapUpgradeToPro() {
  1455. guard let url = URL(string: "https://www.indeed.com") else { return }
  1456. NSWorkspace.shared.open(url)
  1457. }
  1458. @objc private func didChangeThemeSelection(_ sender: NSSegmentedControl) {
  1459. switch sender.selectedSegment {
  1460. case 1:
  1461. NSApp.appearance = NSAppearance(named: .aqua)
  1462. case 2:
  1463. NSApp.appearance = NSAppearance(named: .darkAqua)
  1464. default:
  1465. NSApp.appearance = nil
  1466. }
  1467. }
  1468. private func selectSidebarItem(at index: Int) {
  1469. guard index >= 0, index < currentSidebarItems.count else { return }
  1470. let selectingHome = isHomeSidebarIndex(index)
  1471. if index == selectedSidebarIndex {
  1472. if selectingHome {
  1473. applyHomeState()
  1474. }
  1475. return
  1476. }
  1477. selectedSidebarIndex = index
  1478. configureSidebar()
  1479. updateMainContentVisibility()
  1480. if selectingHome {
  1481. applyHomeState()
  1482. }
  1483. }
  1484. }
  1485. private struct ChatMessage: Codable {
  1486. let role: String
  1487. let content: String
  1488. }
  1489. private final class OpenAIJobSearchService {
  1490. private let endpoint = URL(string: "https://api.openai.com/v1/responses")!
  1491. private let session = URLSession(configuration: .ephemeral)
  1492. func searchJobs(query: String, conversation: [ChatMessage], completion: @escaping (Result<JobSearchOutput, Error>) -> Void) {
  1493. let apiKey = OpenAIConfiguration.apiKey
  1494. guard OpenAIConfiguration.hasAPIKey else {
  1495. completion(.failure(NSError(
  1496. domain: "OpenAIJobSearchService",
  1497. code: 1,
  1498. userInfo: [NSLocalizedDescriptionKey: "Missing API key. Set OPENAI_API_KEY in Xcode Build Settings."]
  1499. )))
  1500. return
  1501. }
  1502. var request = URLRequest(url: endpoint)
  1503. request.httpMethod = "POST"
  1504. request.setValue("application/json", forHTTPHeaderField: "Content-Type")
  1505. request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
  1506. request.timeoutInterval = 45
  1507. let recentContext = conversation.suffix(8)
  1508. .map { "\($0.role.uppercased()): \($0.content)" }
  1509. .joined(separator: "\n")
  1510. let contextBlock: String
  1511. if recentContext.isEmpty {
  1512. contextBlock = "No prior conversation context."
  1513. } else {
  1514. contextBlock = recentContext
  1515. }
  1516. let instructions = """
  1517. Continue this same job-search conversation. Use prior context when useful, and prioritize the latest user query.
  1518. Conversation context:
  1519. \(contextBlock)
  1520. Latest user query: "\(query)"
  1521. Search the web for currently available jobs related to the latest query (and relevant context above).
  1522. Return ONLY strict JSON that matches this schema:
  1523. {
  1524. "jobs": [
  1525. {
  1526. "title": "string",
  1527. "description": "string",
  1528. "url": "https://..."
  1529. }
  1530. ]
  1531. }
  1532. Keep descriptions concise (1 sentence), include real apply/listing URLs when available, and return up to 8 jobs.
  1533. """
  1534. let payload = OpenAIResponsesRequest(
  1535. model: "gpt-4o-mini",
  1536. input: instructions,
  1537. tools: [OpenAIResponsesTool(type: "web_search_preview")]
  1538. )
  1539. do {
  1540. request.httpBody = try JSONEncoder().encode(payload)
  1541. } catch {
  1542. completion(.failure(error))
  1543. return
  1544. }
  1545. session.dataTask(with: request) { data, response, error in
  1546. if let error {
  1547. completion(.failure(error))
  1548. return
  1549. }
  1550. guard let data else {
  1551. completion(.failure(NSError(
  1552. domain: "OpenAIJobSearchService",
  1553. code: 2,
  1554. userInfo: [NSLocalizedDescriptionKey: "API returned an empty response."]
  1555. )))
  1556. return
  1557. }
  1558. if let http = response as? HTTPURLResponse, !(200...299).contains(http.statusCode) {
  1559. if let apiError = try? JSONDecoder().decode(OpenAIAPIErrorResponse.self, from: data) {
  1560. completion(.failure(NSError(
  1561. domain: "OpenAIJobSearchService",
  1562. code: http.statusCode,
  1563. userInfo: [NSLocalizedDescriptionKey: apiError.error.message]
  1564. )))
  1565. } else {
  1566. completion(.failure(NSError(
  1567. domain: "OpenAIJobSearchService",
  1568. code: http.statusCode,
  1569. userInfo: [NSLocalizedDescriptionKey: "Job search request failed with status \(http.statusCode)."]
  1570. )))
  1571. }
  1572. return
  1573. }
  1574. do {
  1575. let modelText = try Self.extractModelTextFromResponsesBody(data)
  1576. let trimmed = modelText.trimmingCharacters(in: .whitespacesAndNewlines)
  1577. guard !trimmed.isEmpty else {
  1578. throw NSError(
  1579. domain: "OpenAIJobSearchService",
  1580. code: 4,
  1581. userInfo: [NSLocalizedDescriptionKey: "The API returned an empty text payload."]
  1582. )
  1583. }
  1584. let jobs = try Self.parseJobListings(fromModelText: trimmed)
  1585. completion(.success(JobSearchOutput(jobs: jobs)))
  1586. } catch {
  1587. completion(.failure(error))
  1588. }
  1589. }.resume()
  1590. }
  1591. /// Walks the `/v1/responses` JSON without strict Codable so tool calls, refusals, and future output item types do not break decoding. Collects only `output_text` segments from assistant `message` items (and any other output items that expose a `content` array).
  1592. private static func extractModelTextFromResponsesBody(_ data: Data) throws -> String {
  1593. let rootObject: Any
  1594. do {
  1595. rootObject = try JSONSerialization.jsonObject(with: data, options: [])
  1596. } catch {
  1597. throw NSError(
  1598. domain: "OpenAIJobSearchService",
  1599. code: 5,
  1600. userInfo: [NSLocalizedDescriptionKey: "The job search service returned data that was not valid JSON."]
  1601. )
  1602. }
  1603. guard let root = rootObject as? [String: Any] else {
  1604. throw NSError(
  1605. domain: "OpenAIJobSearchService",
  1606. code: 5,
  1607. userInfo: [NSLocalizedDescriptionKey: "Unexpected response shape from the job search service."]
  1608. )
  1609. }
  1610. if let status = root["status"] as? String {
  1611. if status == "failed" {
  1612. let message = (root["error"] as? [String: Any])?["message"] as? String ?? "The search request failed."
  1613. throw NSError(domain: "OpenAIJobSearchService", code: 7, userInfo: [NSLocalizedDescriptionKey: message])
  1614. }
  1615. if status == "incomplete",
  1616. let details = root["incomplete_details"] as? [String: Any],
  1617. let reason = details["reason"] as? String {
  1618. throw NSError(
  1619. domain: "OpenAIJobSearchService",
  1620. code: 8,
  1621. userInfo: [NSLocalizedDescriptionKey: "Search stopped early (\(reason)). Try a simpler query or try again."]
  1622. )
  1623. }
  1624. }
  1625. if let direct = root["output_text"] as? String {
  1626. let trimmed = direct.trimmingCharacters(in: .whitespacesAndNewlines)
  1627. if !trimmed.isEmpty { return trimmed }
  1628. }
  1629. guard let output = root["output"] as? [Any] else {
  1630. throw NSError(
  1631. domain: "OpenAIJobSearchService",
  1632. code: 9,
  1633. userInfo: [NSLocalizedDescriptionKey: "The search service returned no assistant text. Try again in a moment."]
  1634. )
  1635. }
  1636. var segments: [String] = []
  1637. for case let item as [String: Any] in output where (item["type"] as? String) == "message" {
  1638. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  1639. }
  1640. if segments.isEmpty {
  1641. for case let item as [String: Any] in output {
  1642. collectOutputTextSegments(fromOutputItem: item, into: &segments)
  1643. }
  1644. }
  1645. let combined = segments.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
  1646. if !combined.isEmpty {
  1647. return combined
  1648. }
  1649. throw NSError(
  1650. domain: "OpenAIJobSearchService",
  1651. code: 9,
  1652. userInfo: [NSLocalizedDescriptionKey: "The model did not return readable job-search text. Try again."]
  1653. )
  1654. }
  1655. private static func collectOutputTextSegments(fromOutputItem item: [String: Any], into segments: inout [String]) {
  1656. guard let content = item["content"] as? [Any] else { return }
  1657. for case let part as [String: Any] in content {
  1658. guard (part["type"] as? String) == "output_text" else { continue }
  1659. if let s = part["text"] as? String {
  1660. segments.append(s)
  1661. } else if let nested = part["text"] as? [String: Any], let value = nested["value"] as? String {
  1662. segments.append(value)
  1663. }
  1664. }
  1665. }
  1666. private static func parseJobListings(fromModelText text: String) throws -> [JobListing] {
  1667. let jsonString = extractJobJSONObjectString(from: text) ?? extractJSONObject(from: text)
  1668. let jsonData = Data(jsonString.utf8)
  1669. if let payload = try? JSONDecoder().decode(JobSearchResultsPayload.self, from: jsonData) {
  1670. return payload.jobs
  1671. }
  1672. if let listings = try? JSONDecoder().decode([JobListing].self, from: jsonData) {
  1673. return listings
  1674. }
  1675. throw NSError(
  1676. domain: "OpenAIJobSearchService",
  1677. code: 10,
  1678. userInfo: [NSLocalizedDescriptionKey: "The assistant reply did not include job listings in the expected JSON format. Try your search again."]
  1679. )
  1680. }
  1681. private static func stripMarkdownCodeFence(_ text: String) -> String {
  1682. var s = text.trimmingCharacters(in: .whitespacesAndNewlines)
  1683. guard s.hasPrefix("```") else { return s }
  1684. s.removeFirst(3)
  1685. if s.lowercased().hasPrefix("json") {
  1686. s.removeFirst(4)
  1687. }
  1688. s = s.trimmingCharacters(in: .whitespacesAndNewlines)
  1689. if let fence = s.range(of: "```", options: .backwards) {
  1690. s = String(s[..<fence.lowerBound])
  1691. }
  1692. return s.trimmingCharacters(in: .whitespacesAndNewlines)
  1693. }
  1694. private static func balancedJSONObject(from openBrace: String.Index, in s: String) -> String? {
  1695. var depth = 0
  1696. var inString = false
  1697. var escaped = false
  1698. var i = openBrace
  1699. while i < s.endIndex {
  1700. let ch = s[i]
  1701. if inString {
  1702. if escaped {
  1703. escaped = false
  1704. } else if ch == "\\" {
  1705. escaped = true
  1706. } else if ch == "\"" {
  1707. inString = false
  1708. }
  1709. } else {
  1710. switch ch {
  1711. case "\"":
  1712. inString = true
  1713. case "{":
  1714. depth += 1
  1715. case "}":
  1716. depth -= 1
  1717. if depth == 0 {
  1718. return String(s[openBrace...i])
  1719. }
  1720. default:
  1721. break
  1722. }
  1723. }
  1724. i = s.index(after: i)
  1725. }
  1726. return nil
  1727. }
  1728. /// Prefers the JSON object that contains a `"jobs"` key so prose before/after the payload does not confuse the decoder.
  1729. private static func extractJobJSONObjectString(from text: String) -> String? {
  1730. let s = stripMarkdownCodeFence(text)
  1731. guard let jobsRange = s.range(of: "\"jobs\"") else { return nil }
  1732. let head = s[..<jobsRange.lowerBound]
  1733. guard let open = head.lastIndex(of: "{") else { return nil }
  1734. return balancedJSONObject(from: open, in: s)
  1735. }
  1736. private static func extractJSONObject(from text: String) -> String {
  1737. if let extracted = extractJobJSONObjectString(from: text) {
  1738. return extracted
  1739. }
  1740. let stripped = stripMarkdownCodeFence(text)
  1741. if let first = stripped.firstIndex(of: "{"), let balanced = balancedJSONObject(from: first, in: stripped) {
  1742. return balanced
  1743. }
  1744. if let range = text.range(of: "\\{[\\s\\S]*\\}", options: .regularExpression) {
  1745. return String(text[range])
  1746. }
  1747. return text
  1748. }
  1749. }
  1750. private struct OpenAIResponsesRequest: Codable {
  1751. let model: String
  1752. let input: String
  1753. let tools: [OpenAIResponsesTool]
  1754. }
  1755. private struct OpenAIResponsesTool: Codable {
  1756. let type: String
  1757. }
  1758. private struct JobSearchResultsPayload: Codable {
  1759. let jobs: [JobListing]
  1760. }
  1761. private struct JobSearchOutput {
  1762. let jobs: [JobListing]
  1763. }
  1764. private struct OpenAIAPIErrorResponse: Codable {
  1765. let error: APIError
  1766. struct APIError: Codable {
  1767. let message: String
  1768. }
  1769. }
  1770. /// `NSButton` that carries a `JobListing` for card actions (`representedObject` is unavailable on `NSButton` in this target).
  1771. private final class JobPayloadButton: HoverableButton {
  1772. var jobPayload: JobListing?
  1773. var cardContext: JobListingCardContext = .homeSearchResults
  1774. }
  1775. /// `NSButton` with a tracking area that reports hover transitions and (optionally) swaps in a pointing-hand cursor while hovered.
  1776. private class HoverableButton: NSButton {
  1777. var hoverHandler: ((Bool) -> Void)?
  1778. var pointerCursor: Bool = false
  1779. private(set) var isHovering: Bool = false
  1780. private var trackingArea: NSTrackingArea?
  1781. private var didPushCursor: Bool = false
  1782. override func updateTrackingAreas() {
  1783. super.updateTrackingAreas()
  1784. if let area = trackingArea { removeTrackingArea(area) }
  1785. let area = NSTrackingArea(
  1786. rect: bounds,
  1787. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1788. owner: self,
  1789. userInfo: nil
  1790. )
  1791. addTrackingArea(area)
  1792. trackingArea = area
  1793. }
  1794. override func mouseEntered(with event: NSEvent) {
  1795. super.mouseEntered(with: event)
  1796. isHovering = true
  1797. hoverHandler?(true)
  1798. if pointerCursor, !didPushCursor {
  1799. NSCursor.pointingHand.push()
  1800. didPushCursor = true
  1801. }
  1802. }
  1803. override func mouseExited(with event: NSEvent) {
  1804. super.mouseExited(with: event)
  1805. isHovering = false
  1806. hoverHandler?(false)
  1807. if didPushCursor {
  1808. NSCursor.pop()
  1809. didPushCursor = false
  1810. }
  1811. }
  1812. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1813. super.viewWillMove(toWindow: newWindow)
  1814. // Guard against an unbalanced cursor stack if the button is removed mid-hover (e.g. job card replaced after a search).
  1815. if newWindow == nil, didPushCursor {
  1816. NSCursor.pop()
  1817. didPushCursor = false
  1818. isHovering = false
  1819. }
  1820. }
  1821. }
  1822. /// `NSView` companion to `HoverableButton`: emits hover transitions and can manage a pointing-hand cursor. Used to track hover over composite controls like the gradient "Find jobs" pill.
  1823. private class HoverableView: NSView {
  1824. var hoverHandler: ((Bool) -> Void)?
  1825. var pointerCursor: Bool = false
  1826. private(set) var isHovering: Bool = false
  1827. private var trackingArea: NSTrackingArea?
  1828. private var didPushCursor: Bool = false
  1829. override func updateTrackingAreas() {
  1830. super.updateTrackingAreas()
  1831. if let area = trackingArea { removeTrackingArea(area) }
  1832. let area = NSTrackingArea(
  1833. rect: bounds,
  1834. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1835. owner: self,
  1836. userInfo: nil
  1837. )
  1838. addTrackingArea(area)
  1839. trackingArea = area
  1840. }
  1841. override func mouseEntered(with event: NSEvent) {
  1842. super.mouseEntered(with: event)
  1843. isHovering = true
  1844. hoverHandler?(true)
  1845. if pointerCursor, !didPushCursor {
  1846. NSCursor.pointingHand.push()
  1847. didPushCursor = true
  1848. }
  1849. }
  1850. override func mouseExited(with event: NSEvent) {
  1851. super.mouseExited(with: event)
  1852. isHovering = false
  1853. hoverHandler?(false)
  1854. if didPushCursor {
  1855. NSCursor.pop()
  1856. didPushCursor = false
  1857. }
  1858. }
  1859. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1860. super.viewWillMove(toWindow: newWindow)
  1861. if newWindow == nil, didPushCursor {
  1862. NSCursor.pop()
  1863. didPushCursor = false
  1864. isHovering = false
  1865. }
  1866. }
  1867. }
  1868. /// Document view for the job list `NSScrollView`; flipped coordinates keep short result sets aligned to the top of the clip (avoids a large empty band above the cards on macOS).
  1869. private final class JobListingsDocumentView: NSView {
  1870. override var isFlipped: Bool { true }
  1871. }
  1872. /// Marker subclass for the per-assistant-message job stack embedded inside a chat bubble. `NSView.tag` is read-only on macOS, so a typed subclass is the cleanest way to identify these stacks during dismiss and layout passes.
  1873. private final class ChatJobsStackView: NSStackView {}
  1874. /// Marker subclass for the wrapping label inside a chat bubble. Lets the layout pass find each bubble label to update its `preferredMaxLayoutWidth` when the chat width changes.
  1875. private final class ChatBubbleLabel: NSTextField {}
  1876. /// Captures clicks for the full sidebar pill so icon, label, and padding behave as one tab. Manages its own hover background so non-selected rows highlight subtly on hover without disturbing the selected-row fill.
  1877. private final class SidebarNavRowView: NSView {
  1878. private let onSelect: () -> Void
  1879. var restingBackgroundColor: NSColor? {
  1880. didSet { applyBackground() }
  1881. }
  1882. var hoverBackgroundColor: NSColor?
  1883. private var isHovering: Bool = false
  1884. private var didPushCursor: Bool = false
  1885. init(onSelect: @escaping () -> Void) {
  1886. self.onSelect = onSelect
  1887. super.init(frame: .zero)
  1888. }
  1889. @available(*, unavailable)
  1890. required init?(coder: NSCoder) {
  1891. fatalError("init(coder:) has not been implemented")
  1892. }
  1893. override func hitTest(_ point: NSPoint) -> NSView? {
  1894. guard let superview else { return super.hitTest(point) }
  1895. let local = convert(point, from: superview)
  1896. return bounds.contains(local) ? self : nil
  1897. }
  1898. override func mouseDown(with event: NSEvent) {
  1899. onSelect()
  1900. }
  1901. override func updateTrackingAreas() {
  1902. super.updateTrackingAreas()
  1903. trackingAreas.forEach { removeTrackingArea($0) }
  1904. addTrackingArea(NSTrackingArea(
  1905. rect: bounds,
  1906. options: [.activeInKeyWindow, .mouseEnteredAndExited, .inVisibleRect],
  1907. owner: self,
  1908. userInfo: nil
  1909. ))
  1910. }
  1911. override func mouseEntered(with event: NSEvent) {
  1912. super.mouseEntered(with: event)
  1913. isHovering = true
  1914. applyBackground()
  1915. if !didPushCursor {
  1916. NSCursor.pointingHand.push()
  1917. didPushCursor = true
  1918. }
  1919. }
  1920. override func mouseExited(with event: NSEvent) {
  1921. super.mouseExited(with: event)
  1922. isHovering = false
  1923. applyBackground()
  1924. if didPushCursor {
  1925. NSCursor.pop()
  1926. didPushCursor = false
  1927. }
  1928. }
  1929. override func viewWillMove(toWindow newWindow: NSWindow?) {
  1930. super.viewWillMove(toWindow: newWindow)
  1931. if newWindow == nil, didPushCursor {
  1932. NSCursor.pop()
  1933. didPushCursor = false
  1934. isHovering = false
  1935. }
  1936. }
  1937. private func applyBackground() {
  1938. let color = isHovering ? (hoverBackgroundColor ?? restingBackgroundColor) : restingBackgroundColor
  1939. layer?.backgroundColor = color?.cgColor
  1940. }
  1941. }