Files
linkding-ios/Marks/Intents/MarksAppIntents.swift
Krishna Kumar 96ea5fe6f6
All checks were successful
CI / build-and-deploy (push) Successful in 22s
feat(ai): on-device RAG over bookmarks + Spotlight indexing + unified logging
- SpotlightIndexer: actually push BookmarkEntity into the Spotlight index via
  indexAppEntities (IndexedEntity conformance alone indexes nothing). Wired into
  every sync/mutation point. This is what lets Apple Intelligence answer
  free-form Siri questions grounded in the user's bookmarks.
- On-device RAG: BookmarkAssistant (LanguageModelSession) + BookmarkSearchTool
  + SpotlightBookmarkSearch (CSSearchQuery retrieval), surfaced via AskView with
  lightweight Markdown rendering of answers.
- Log: os.Logger facility (com.magicive.marks) replacing ad-hoc print(); shared
  with ShareExtension. ClaudeService now logs server error bodies on non-2xx.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-24 12:55:32 -05:00

185 lines
6.5 KiB
Swift

import Foundation
import AppIntents
// MARK: - Open
/// Opens a bookmark in the in-app browser. Conforms to `OpenIntent` the
/// system-blessed "open" type Siri understands. (iOS 26 has no separate
/// `system.open` App Schema; `OpenIntent` with an `AppEntity` target *is* the
/// supported mechanism, so no schema macro is needed here.)
struct OpenBookmarkIntent: OpenIntent {
static let title: LocalizedStringResource = "Open Bookmark"
static let openAppWhenRun = true
@Parameter(title: "Bookmark")
var target: BookmarkEntity
@MainActor
func perform() async throws -> some IntentResult {
IntentRouter.shared.openBookmark(url: target.url.absoluteString)
return .result()
}
}
// MARK: - Search
/// Searches Marks and shows the results in the app's search tab.
///
/// Conforms to the formal `system.search` App Schema (`ShowInAppSearchResultsIntent`),
/// which is what makes it executable by conversational Siri / Apple Intelligence:
/// Siri hands over the spoken term as `criteria` and the app re-runs it in its
/// own search UI. `searchScopes` defaults to `[.general]` for string criteria.
@AppIntent(schema: .system.search)
struct SearchMarksIntent {
static let title: LocalizedStringResource = "Search Marks"
static let description = IntentDescription("Search your Marks bookmarks.")
@Parameter(title: "Search", requestValueDialog: "What do you want to search for?")
var criteria: StringSearchCriteria
@MainActor
func perform() async throws -> some IntentResult {
IntentRouter.shared.search(criteria.term)
return .result()
}
}
// MARK: - Add
/// Saves a link to Marks without opening the app. Mirrors the Share Extension's
/// headless save, but goes through the typed `LinkdingAPI`.
struct AddBookmarkIntent: AppIntent {
static let title: LocalizedStringResource = "Add Bookmark to Marks"
static let description = IntentDescription("Save a link to your Marks bookmarks.")
static let openAppWhenRun = false
@Parameter(title: "URL")
var url: URL
@Parameter(title: "Title")
var name: String?
@Parameter(title: "Tags")
var tags: [String]?
static var parameterSummary: some ParameterSummary {
Summary("Add \(\.$url) to Marks") {
\.$name
\.$tags
}
}
func perform() async throws -> some IntentResult & ReturnsValue<BookmarkEntity> & ProvidesDialog & ShowsSnippetView {
let api = try MarksIntent.api()
let create = BookmarkCreate(
url: url.absoluteString,
title: name ?? "",
tagNames: tags ?? [],
isArchived: false,
unread: true,
shared: false
)
let bookmark = try await api.createBookmark(create)
let entity = BookmarkEntity(from: bookmark)
MarksIntent.refreshWidgets(adding: bookmark)
return .result(
value: entity,
dialog: IntentDialog("Saved \(entity.title) to Marks."),
view: BookmarkSnippetView(entity: entity)
)
}
}
// MARK: - Show unread
/// Returns the user's unread bookmarks "what's unread in Marks?"
struct ShowUnreadIntent: AppIntent {
static let title: LocalizedStringResource = "Show Unread Bookmarks"
static let description = IntentDescription("List the bookmarks you haven't read yet.")
func perform() async throws -> some IntentResult & ReturnsValue<[BookmarkEntity]> & ProvidesDialog {
let api = try MarksIntent.api()
let response = try await api.fetchBookmarks(limit: 10, unread: true)
let entities = response.results.map(BookmarkEntity.init(from:))
let dialog: IntentDialog = entities.isEmpty
? "You have no unread bookmarks."
: "You have \(entities.count) unread bookmark\(entities.count == 1 ? "" : "s")."
return .result(value: entities, dialog: dialog)
}
}
// MARK: - Summarize (AI)
/// Produces an AI summary of a bookmark via the bundled ClaudeService.
struct SummarizeBookmarkIntent: AppIntent {
static let title: LocalizedStringResource = "Summarize Bookmark"
static let description = IntentDescription("Get an AI summary of a saved bookmark.")
@Parameter(title: "Bookmark")
var target: BookmarkEntity
static var parameterSummary: some ParameterSummary {
Summary("Summarize \(\.$target)")
}
func perform() async throws -> some IntentResult & ReturnsValue<String> & ProvidesDialog & ShowsSnippetView {
let claude = ClaudeService()
let (summary, _) = try await claude.enrich(bookmark: target.makeBookmark())
return .result(
value: summary,
dialog: IntentDialog("\(summary)"),
view: SummarySnippetView(title: target.title, host: target.host, summary: summary)
)
}
}
// Note: there is intentionally no "Ask" AppIntent. Apple Intelligence already
// answers free-form questions over the user's bookmarks by querying the
// Spotlight index of `BookmarkEntity` (an `IndexedEntity`) directly verified
// on device (Campo CoreSpotlight on-device model). A named intent would be
// redundant, and "Ask <app>" collides with Siri's built-in "Ask". The in-app
// `AskView` remains as a manual on-device-RAG surface.
// MARK: - App Shortcuts
struct MarksShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: AddBookmarkIntent(),
phrases: [
"Add a bookmark to \(.applicationName)",
"Save this link to \(.applicationName)"
],
shortTitle: "Add Bookmark",
systemImageName: "bookmark"
)
AppShortcut(
intent: SearchMarksIntent(),
phrases: [
"Search \(.applicationName)",
"Search my bookmarks in \(.applicationName)"
],
shortTitle: "Search",
systemImageName: "magnifyingglass"
)
AppShortcut(
intent: ShowUnreadIntent(),
phrases: [
"Show unread in \(.applicationName)",
"What's unread in \(.applicationName)"
],
shortTitle: "Unread",
systemImageName: "envelope.badge"
)
AppShortcut(
intent: SummarizeBookmarkIntent(),
phrases: [
"Summarize a bookmark in \(.applicationName)",
"Summarize this with \(.applicationName)"
],
shortTitle: "Summarize",
systemImageName: "sparkles"
)
}
}