Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions TakeNote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
objects = {

/* Begin PBXBuildFile section */
6602CB572E683E54005A4D38 /* ObjectBox.xcframework in Frameworks */ = {isa = PBXBuildFile; productRef = 6602CB562E683E54005A4D38 /* ObjectBox.xcframework */; };
6602CB632E6AF792005A4D38 /* EntityInfo-TakeNote.generated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6602CB622E6AF792005A4D38 /* EntityInfo-TakeNote.generated.swift */; };
66AC246F2E4609FB009B0C3F /* CodeEditorView in Frameworks */ = {isa = PBXBuildFile; productRef = 66AC246E2E4609FB009B0C3F /* CodeEditorView */; };
66AC24712E4609FB009B0C3F /* LanguageSupport in Frameworks */ = {isa = PBXBuildFile; productRef = 66AC24702E4609FB009B0C3F /* LanguageSupport */; };
66B616ED2E4CC2B6009A1857 /* SQLite in Frameworks */ = {isa = PBXBuildFile; productRef = 66B616EC2E4CC2B6009A1857 /* SQLite */; };
66EEDE112E3FFBDD00A899AE /* MarkdownUI in Frameworks */ = {isa = PBXBuildFile; productRef = 66EEDE102E3FFBDD00A899AE /* MarkdownUI */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
6602CB622E6AF792005A4D38 /* EntityInfo-TakeNote.generated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = "EntityInfo-TakeNote.generated.swift"; path = "generated/EntityInfo-TakeNote.generated.swift"; sourceTree = "<group>"; };
66B6907A2E3FCDF20013519B /* TakeNote.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TakeNote.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -47,6 +50,7 @@
buildActionMask = 2147483647;
files = (
66B616ED2E4CC2B6009A1857 /* SQLite in Frameworks */,
6602CB572E683E54005A4D38 /* ObjectBox.xcframework in Frameworks */,
66EEDE112E3FFBDD00A899AE /* MarkdownUI in Frameworks */,
66AC24712E4609FB009B0C3F /* LanguageSupport in Frameworks */,
66AC246F2E4609FB009B0C3F /* CodeEditorView in Frameworks */,
Expand All @@ -59,6 +63,7 @@
66B690712E3FCDF20013519B = {
isa = PBXGroup;
children = (
6602CB622E6AF792005A4D38 /* EntityInfo-TakeNote.generated.swift */,
66B6907C2E3FCDF20013519B /* TakeNote */,
66B6907B2E3FCDF20013519B /* Products */,
);
Expand Down Expand Up @@ -96,6 +101,7 @@
66AC246E2E4609FB009B0C3F /* CodeEditorView */,
66AC24702E4609FB009B0C3F /* LanguageSupport */,
66B616EC2E4CC2B6009A1857 /* SQLite */,
6602CB562E683E54005A4D38 /* ObjectBox.xcframework */,
);
productName = TakeNote;
productReference = 66B6907A2E3FCDF20013519B /* TakeNote.app */;
Expand Down Expand Up @@ -129,6 +135,7 @@
66EEDE0F2E3FFBDD00A899AE /* XCRemoteSwiftPackageReference "swift-markdown-ui" */,
66AC246D2E4609FB009B0C3F /* XCRemoteSwiftPackageReference "CodeEditorView" */,
66B616EB2E4CC2B6009A1857 /* XCRemoteSwiftPackageReference "SQLite" */,
6602CB552E683E54005A4D38 /* XCRemoteSwiftPackageReference "objectbox-swift-spm" */,
);
preferredProjectObjectVersion = 77;
productRefGroup = 66B6907B2E3FCDF20013519B /* Products */;
Expand All @@ -155,6 +162,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
6602CB632E6AF792005A4D38 /* EntityInfo-TakeNote.generated.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -429,6 +437,14 @@
/* End XCConfigurationList section */

/* Begin XCRemoteSwiftPackageReference section */
6602CB552E683E54005A4D38 /* XCRemoteSwiftPackageReference "objectbox-swift-spm" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/objectbox/objectbox-swift-spm";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.4.1;
};
};
66AC246D2E4609FB009B0C3F /* XCRemoteSwiftPackageReference "CodeEditorView" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/mchakravarty/CodeEditorView";
Expand Down Expand Up @@ -456,6 +472,11 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
6602CB562E683E54005A4D38 /* ObjectBox.xcframework */ = {
isa = XCSwiftPackageProductDependency;
package = 6602CB552E683E54005A4D38 /* XCRemoteSwiftPackageReference "objectbox-swift-spm" */;
productName = ObjectBox.xcframework;
};
66AC246E2E4609FB009B0C3F /* CodeEditorView */ = {
isa = XCSwiftPackageProductDependency;
package = 66AC246D2E4609FB009B0C3F /* XCRemoteSwiftPackageReference "CodeEditorView" */;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 37 additions & 42 deletions TakeNote/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,41 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Removed CFBundleDocumentTypes entirely -->

<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.adamdrew.takenote</string>
<key>CFBundleURLSchemes</key>
<array>
<string>takenote</string>
</array>
</dict>
</array>

<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>

<!-- Use UILaunchStoryboardName for launch storyboard, not UILaunchScreen -->
<key>UILaunchStoryboardName</key>
<string>Launch Screen</string>

<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeIdentifier</key>
<string>com.adamdrew.takenote.noteid</string>
<key>UTTypeDescription</key>
<string>Note ID</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<!-- Optional but recommended: add tags if this is ever file-backed -->
<key>UTTypeTagSpecification</key>
<dict/>
</dict>
</array>
<key>MagicChatEnabled</key>
<false/>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>com.adamdrew.takenote</string>
<key>CFBundleURLSchemes</key>
<array>
<string>takenote</string>
</array>
</dict>
</array>
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
<string>Launch Screen</string>
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeIdentifier</key>
<string>com.adamdrew.takenote.noteid</string>
<key>UTTypeDescription</key>
<string>Note ID</string>
<key>UTTypeConformsTo</key>
<array>
<string>public.data</string>
</array>
<key>UTTypeTagSpecification</key>
<dict/>
</dict>
</array>
</dict>
</plist>
</plist>
35 changes: 35 additions & 0 deletions TakeNote/Library/EmbeddingProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// EmbeddingProvider.swift
// TakeNote
//
// Created by Adam Drew on 9/5/25.
//

import NaturalLanguage

class EmbeddingProvider {
private let model: NLEmbedding?
private let dim: Int?

init(language: NLLanguage = .english, revision: Int? = nil) {
if let rev = revision {
self.model = NLEmbedding.sentenceEmbedding(for: language, revision: rev)
} else {
self.model = NLEmbedding.sentenceEmbedding(for: language)
}
self.dim = model?.dimension
}

/// Returns a unit-length Float vector or nil if unavailable
func embed(_ text: String) -> [Float]? {
guard let model else { return nil }
guard let v = model.vector(for: text) else { return nil } // [Double]
var f = v.map { Float($0) }
// L2 normalize
let norm = sqrt(max(1e-12, f.reduce(0) { $0 + $1*$1 }))
for i in 0..<f.count { f[i] /= norm }
// Optional: ensure consistent dimension
if let dim, f.count != dim { return nil }
return f
}
}
155 changes: 155 additions & 0 deletions TakeNote/Library/OBSearchIndex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
//
// OBSearchIndex.swift
// TakeNote
//
// Created by Adam Drew on 9/5/25.
//
import Foundation
import NaturalLanguage
import os
import ObjectBox

func makeStore() -> Store? {
let logger = Logger(subsystem: "com.adamdrew.takenote", category: "OBStoreMaker")

do {
// 1) Build a sane on-disk path
let appSupport = try FileManager.default.url(for: .applicationSupportDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true)
let bundleID = Bundle.main.bundleIdentifier ?? "com.adamdrew.takenote"
let directory = appSupport
.appendingPathComponent(bundleID, isDirectory: true)
.appendingPathComponent("chunks", isDirectory: true)

try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)

logger.info("OB dir: \(directory.path, privacy: .public) exists: \(FileManager.default.fileExists(atPath: directory.path)) writable: \(FileManager.default.isWritableFile(atPath: directory.path))")

// 2) Try on-disk store
let store = try Store(directoryPath: directory.path)
logger.info("ObjectBox store opened")
return store
} catch {
logger.error("ObjectBox on-disk open failed: \(String(describing: error), privacy: .public)")
}

// 3) Safe dev fallback: in-memory (keeps app from crashing)
do {
let mem = try Store(directoryPath: "memory:chunks-dev")
Logger(subsystem: "com.adamdrew.takenote", category: "OBStoreMaker")
.info("Using in-memory ObjectBox store (dev fallback)")
return mem
} catch {
Logger(subsystem: "com.adamdrew.takenote", category: "OBStoreMaker")
.error("ObjectBox in-memory open failed too: \(String(describing: error), privacy: .public)")
return nil
}
}

class OBSearchIndex {
let logger = Logger(
subsystem: "com.adamdrew.takenote",
category: "OBSearchIndex"
)
private let chunker: WindowChunker
private let embedder: EmbeddingProvider
private let store : Store?
private let box : Box<OBChunk>


init(inMemory: Bool = true) {
self.chunker = WindowChunker()
self.embedder = EmbeddingProvider()
self.store = makeStore()
guard let s = store else {
logger.error("OBSearchIndex: store unavailable; running with no search backend")
fatalError("OB store failed")
}
self.box = s.box(for: OBChunk.self)

}

// MARK: Public API

/// Index 1 note (replace all its chunks)
func reindex(noteID: UUID, markdown: String) {
delete(noteID: noteID)
for chunk in chunker.chunks(for: markdown) {
let embedding = embedder.embed(chunk.text)
let obChunk = OBChunk(noteID: noteID, chunk: chunk.text, embedding: embedding)
do {
try self.box.put(obChunk)
} catch {
logger.error("Error persisting chunk: \(error)")
}
}
logger.debug("Indexed note \(noteID)")
}

/// Index many notes (replace each note's chunks)
func reindex(_ notes: [(UUID, String)]) {
for note in notes {
reindex(noteID: note.0, markdown: note.1)
}
}

/// Destroy all records
func dropAll() {
do {
try self.box.removeAll()
logger.info("Deleted all chunks from the objectbox store")
} catch {
self.logger.error("Error deleting everything from store box: \(error)")
}
}

/// Remove a single note’s chunks
func delete(noteID: UUID) {
do {
let query: Query<OBChunk> = try self.box.query {
OBChunk.noteID == noteID.uuidString
}.build()
let chunks: [OBChunk] = try query.find()
for chunk in chunks {
try self.box.remove(chunk.id)
}
self.logger.info("Removed chunk from store")
} catch {
self.logger.error("Error deleting chunks: \(error)")
}
}

/// Back-compat alias
func searchNatural(_ text: String, limit: Int = 5) -> [SearchHit] {
return search(text, limit: limit)
}

/// Dense (cosine) search over embedded chunks with light anchor scoping
func search(_ query: String, limit: Int = 5) -> [SearchHit] {
guard let embedding = embedder.embed(query) else {
logger.error("Error generating query embedding")
return []
}
do {
let query = try box
.query { OBChunk.embedding.nearestNeighbors(queryVector: embedding, maxCount: 4) }
.build()
let results = try query.findWithScores()
let searchHits: [SearchHit] = results.map { result in
let id = result.object.id
let uuid = UUID(uuidString: result.object.noteID)
let chunk = result.object.chunk
return SearchHit(id: Int64(id), noteID: uuid ?? UUID(), chunk: chunk)
}
logger.info("OBS: Found \(results.count) search results")
return searchHits
} catch {
logger.error("Error performing vector search: \(error)")
}
return []
}


}
14 changes: 14 additions & 0 deletions TakeNote/Library/SearchHit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SearchHit.swift
// TakeNote
//
// Created by Adam Drew on 9/5/25.
//

import Foundation

struct SearchHit: Identifiable {
public let id: Int64 // rowid inside FTS table
public let noteID: UUID
public let chunk: String // the stored chunk text
}
Loading