Files
fil/docs/snippets/swift/plugins/stateful_plugin.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.4 KiB

import Kreuzberg
import os.lock

final class StatefulPlugin: PostProcessor {
    private var lock = NSLock()
    private var callCount: Int = 0
    private var cache: [String: String] = [:]

    func name() -> String {
        "stateful-plugin"
    }

    func version() -> String {
        "1.0.0"
    }

    func processingStage() -> String {
        "middle"
    }

    func priority() -> Int32 {
        50
    }

    func process(result: ExtractionResult, config: ExtractionConfig) -> String {
        lock.lock()
        defer { lock.unlock() }

        callCount += 1
        cache["last_mime"] = result.mimeType()
        cache["call_count"] = String(callCount)

        return "{\"ok\": null}"
    }

    func shouldProcess(result: ExtractionResult, config: ExtractionConfig) -> Bool {
        true
    }

    func estimatedDurationMs(result: ExtractionResult) -> UInt64 {
        1  // Minimal overhead
    }

    func initialize() -> String {
        lock.lock()
        defer { lock.unlock() }
        callCount = 0
        cache.removeAll()
        return "{\"ok\": null}"
    }

    func shutdown() -> String {
        lock.lock()
        defer { lock.unlock() }
        let finalCount = callCount
        cache.removeAll()
        let message = "Processed \(finalCount) extractions"
        print(message)
        return "{\"ok\": null}"
    }
}

let plugin = StatefulPlugin()
try Kreuzberg.registerPostProcessor(plugin)