yams/plugins/zyp/zyp_plugin.cpp
Trevon 0dd3985db4
refactor: simplify runtime paths and harden retrieval (#61)
* refactor(tuning): prune dormant runtime knobs

Remove unused tuning controls and adaptive search budget state. Extract testable ONNX slot reconciliation and search telemetry hydration seams, and correct the Apple Silicon sysctl value width.

* refactor(vector): retire dormant backends

Remove unused FAISS, model-infrastructure, VectorIndexManager, and TurboQuant paths while keeping Simeon PQ and float-vector storage.

Migrate unused V2.2 quantized schemas transactionally to V2.3. Databases containing non-default quantized formats or packed-code blobs fail closed with recovery guidance and no mutation.

* refactor(plugins): remove legacy SDK tooling

Remove the Ghidra and SDK submodules, their bespoke tests, and stale scan exclusions. Retain generic PluginProcess coverage against the repository mock fixture.

* build(vector): remove stale FAISS overrides

* refactor(ipc): remove dormant FSM snapshots

* refactor(ingest): unify bounded KG enqueue

* fix(plugins): harden allocation contracts

Keep ABI outputs null or fully initialized when allocations fail, and represent empty object payloads without relying on malloc(0).

* fix(storage): define empty object reads

Return a null buffer with zero length for successful empty-object reads instead of treating malloc(0) as an allocation contract.

* refactor(storage): trim through string views

* test: prune dormant harness helpers

Remove unused readiness and fixture helpers, and keep benchmark client-count doubling in a wide integer domain.

* chore(cpp): initialize analyzer-facing locals

* build(simeon): advance analyzer cleanup

* refactor(config): drop dead daemon client keys

* refactor(ipc): inject daemon client transport

Socket and in-process execution now share DaemonClient request dispatch, so CLI fallbacks do not need alternate service paths.

* refactor(retrieval): route CLI through daemon

Search and grep now use the same daemon handlers over socket or in-process transport. Live grep remains an explicit freshness inspection.

* refactor(graph): move CLI scope into daemon

Graph CLI requests now share daemon handlers across socket and in-process transports. CWD filtering occurs before pagination so scoped totals and pages stay correct.

* build: isolate test-only runtime controls

* refactor(metadata): decouple lock telemetry

* fix(services): return on request timeout

* fix(retrieval): enforce workspace scope

* fix(graph): scope nodes before pagination

* fix(grep): drop binary result lines

* fix(daemon): bound checkpoint wait

* fix(repair): use KG health checks

* fix(storage): always verify compressed CRC

* perf(vector): skip unchanged PQ snapshots

* build(zyp): exclude bench from default build

* build: protect Meson files from formatter

* refactor: remove unreachable production APIs

* refactor(daemon): remove dormant runtime state

* fix(vector): harden daemon provider calls

Timed-out coroutines must own their client and requests. Provider discovery must also fail instead of returning synthetic embeddings in production.

* refactor(storage): remove unusable adapters

* fix(cli): unify in-process add requests

* fix(tsan): preserve async worker progress

* test(cli): isolate daemon-dependent suites

* fix(repair): expose queue backoff progress
2026-07-31 14:16:45 -04:00

225 lines
6.1 KiB
C++

/**
* @file zyp_plugin.cpp
* @brief YAMS plugin ABI implementation for zyp (Zig YAMS PDF)
*
* This implements the content_extractor_v1 interface using zpdf for
* high-performance PDF text extraction.
*/
#include <yams/plugins/abi.h>
#include <yams/plugins/content_extractor_v1.h>
#include "pdf_metadata.h"
#include "zpdf_wrapper.h"
#include <cstdlib>
#include <cstring>
namespace {
void free_result(yams_extraction_result_t* result);
/**
* Duplicate a C++ string to a malloc'd C string.
*/
char* dupString(const std::string& s) {
if (s.empty()) {
return nullptr;
}
char* result = static_cast<char*>(std::malloc(s.size() + 1));
if (result) {
std::memcpy(result, s.c_str(), s.size() + 1);
}
return result;
}
/**
* Check if this extractor supports the given MIME type or extension.
*/
bool supports(const char* mime_type, const char* extension) {
if (mime_type && std::strcmp(mime_type, "application/pdf") == 0) {
return true;
}
if (extension) {
if (std::strcmp(extension, ".pdf") == 0 || std::strcmp(extension, "pdf") == 0) {
return true;
}
}
return false;
}
/**
* Extract text and metadata from a PDF buffer.
*/
int extract(const uint8_t* content, size_t content_len, yams_extraction_result_t** result) {
if (!content || content_len == 0 || !result) {
return YAMS_PLUGIN_ERR_INVALID;
}
*result = nullptr;
std::span<const uint8_t> buffer(content, content_len);
// Get page count and validate PDF (probe open/close)
int pageCount = 0;
{
auto probeDoc = yams::zyp::Document::openMemory(buffer);
if (!probeDoc) {
return YAMS_PLUGIN_ERR_INVALID;
}
pageCount = probeDoc->pageCount();
}
// Extract text using true parallel extraction across pages
auto text = yams::zyp::Document::extractAllParallelized(buffer);
// Extract metadata
auto metadata = yams::zyp::extractMetadata(buffer);
auto metaMap = metadata ? metadata->toMap() : std::unordered_map<std::string, std::string>{};
// Add page count
metaMap["page_count"] = std::to_string(pageCount);
// Allocate result structure
*result = static_cast<yams_extraction_result_t*>(std::malloc(sizeof(yams_extraction_result_t)));
if (!*result) {
return YAMS_PLUGIN_ERR_INVALID;
}
std::memset(*result, 0, sizeof(yams_extraction_result_t));
// Copy text
if (text && !text.empty()) {
auto textView = text.text();
(*result)->text = static_cast<char*>(std::malloc(textView.size() + 1));
if (!(*result)->text) {
free_result(*result);
*result = nullptr;
return YAMS_PLUGIN_ERR_INVALID;
}
std::memcpy((*result)->text, textView.data(), textView.size());
(*result)->text[textView.size()] = '\0';
} else {
// Empty text
(*result)->text = static_cast<char*>(std::malloc(1));
if (!(*result)->text) {
free_result(*result);
*result = nullptr;
return YAMS_PLUGIN_ERR_INVALID;
}
(*result)->text[0] = '\0';
}
// Populate metadata
if (!metaMap.empty()) {
(*result)->metadata.pairs = static_cast<yams_key_value_pair_t*>(
std::calloc(metaMap.size(), sizeof(yams_key_value_pair_t)));
if (!(*result)->metadata.pairs) {
free_result(*result);
*result = nullptr;
return YAMS_PLUGIN_ERR_INVALID;
}
(*result)->metadata.count = metaMap.size();
size_t i = 0;
for (const auto& [key, value] : metaMap) {
(*result)->metadata.pairs[i].key = dupString(key);
(*result)->metadata.pairs[i].value = dupString(value);
if ((!key.empty() && !(*result)->metadata.pairs[i].key) ||
(!value.empty() && !(*result)->metadata.pairs[i].value)) {
free_result(*result);
*result = nullptr;
return YAMS_PLUGIN_ERR_INVALID;
}
++i;
}
}
return YAMS_PLUGIN_OK;
}
/**
* Free an extraction result.
*/
void free_result(yams_extraction_result_t* result) {
if (!result) {
return;
}
std::free(result->text);
std::free(result->error);
if (result->metadata.pairs) {
for (size_t i = 0; i < result->metadata.count; ++i) {
std::free(result->metadata.pairs[i].key);
std::free(result->metadata.pairs[i].value);
}
std::free(result->metadata.pairs);
}
std::free(result);
}
} // anonymous namespace
extern "C" {
int yams_plugin_get_abi_version(void) {
return YAMS_PLUGIN_ABI_VERSION;
}
const char* yams_plugin_get_name(void) {
return "zyp";
}
const char* yams_plugin_get_version(void) {
return "1.0.0";
}
const char* yams_plugin_get_manifest_json(void) {
return "{"
"\"name\": \"zyp\","
"\"version\": \"1.0.0\","
"\"abi\": 1,"
"\"interfaces\": [{\"id\": \"content_extractor_v1\", \"version\": 1}],"
"\"description\": \"High-performance PDF text extraction using zpdf\","
"\"health\": {\"has_health\": false}"
"}";
}
int yams_plugin_init(const char* config_json, const void* host_context) {
(void)config_json;
(void)host_context;
return YAMS_PLUGIN_OK;
}
void yams_plugin_shutdown(void) {
// No cleanup needed
}
int yams_plugin_get_interface(const char* iface_id, uint32_t version, void** out_iface) {
if (!iface_id || !out_iface) {
return YAMS_PLUGIN_ERR_INVALID;
}
*out_iface = nullptr;
if (std::strcmp(iface_id, YAMS_IFACE_CONTENT_EXTRACTOR_V1_ID) == 0 &&
version == YAMS_IFACE_CONTENT_EXTRACTOR_V1_VERSION) {
static yams_content_extractor_v1 iface = {
.abi_version = YAMS_IFACE_CONTENT_EXTRACTOR_V1_VERSION,
.supports = supports,
.extract = extract,
.free_result = free_result,
};
*out_iface = &iface;
return YAMS_PLUGIN_OK;
}
return YAMS_PLUGIN_ERR_NOT_FOUND;
}
int yams_plugin_get_health_json(char** out_json) {
(void)out_json;
return YAMS_PLUGIN_ERR_NOT_FOUND;
}
} // extern "C"