Skip to content

Personal AI, On Personal Devices

Python

feat: add ConnectorRegistry and base connector types (Document, SyncStatus, BaseConnector)

krypticmouse29d275fe4eb5
diff --git a/src/openjarvis/connectors/__init__.py b/src/openjarvis/connectors/__init__.py
new file mode 100644
index 00000000..1642e5bd
--- /dev/null
+++ b/src/openjarvis/connectors/__init__.py
@@ -0,0 +1,10 @@
+"""Data source connectors for Deep Research."""
+
+from openjarvis.connectors._stubs import (
+ Attachment,
+ BaseConnector,
+ Document,
+ SyncStatus,
+)
+
+__all__ = ["Attachment", "BaseConnector", "Document", "SyncStatus"]
diff --git a/src/openjarvis/connectors/_stubs.py b/src/openjarvis/connectors/_stubs.py
new file mode 100644
index 00000000..6ea4e95a
--- /dev/null
+++ b/src/openjarvis/connectors/_stubs.py
@@ -0,0 +1,101 @@
+"""Base types for data source connectors."""
+
+from __future__ import annotations
+
+from abc import ABC, abstractmethod
+from dataclasses import dataclass, field
+from datetime import datetime
+from typing import Any, Dict, Iterator, List, Optional
+
+from openjarvis.tools._stubs import ToolSpec
+
+
+@dataclass(slots=True)
+class Attachment:
+ """A file attached to a document (email attachment, shared file, etc.)."""
+
+ filename: str
+ mime_type: str
+ size_bytes: int
+ sha256: str = ""
+ content: bytes = field(default=b"", repr=False)
+
+
+@dataclass(slots=True)
+class Document:
+ """Universal schema for data from any connector.
+
+ All connectors normalize their output to this format before ingestion.
+ """
+
+ doc_id: str
+ source: str
+ doc_type: str
+ content: str
+ title: str = ""
+ author: str = ""
+ participants: List[str] = field(default_factory=list)
+ timestamp: datetime = field(default_factory=datetime.now)
+ thread_id: Optional[str] = None
+ url: Optional[str] = None
+ attachments: List[Attachment] = field(default_factory=list)
+ metadata: Dict[str, Any] = field(default_factory=dict)
+
+
+@dataclass(slots=True)
+class SyncStatus:
+ """Progress of a connector's sync operation."""
+
+ state: str = "idle"
+ items_synced: int = 0
+ items_total: int = 0
+ last_sync: Optional[datetime] = None
+ cursor: Optional[str] = None
+ error: Optional[str] = None
+
+
+class BaseConnector(ABC):
+ """Abstract base for data source connectors.
+
+ Each connector knows how to authenticate with a service, bulk-sync
+ its data as ``Document`` objects, and optionally expose MCP tools
+ for real-time agent queries.
+ """
+
+ connector_id: str
+ display_name: str
+ auth_type: str # "oauth" | "local" | "bridge" | "filesystem"
+
+ @abstractmethod
+ def is_connected(self) -> bool:
+ """Return True if the connector has valid credentials."""
+
+ @abstractmethod
+ def disconnect(self) -> None:
+ """Revoke credentials and clean up."""
+
+ @abstractmethod
+ def sync(
+ self, *, since: Optional[datetime] = None, cursor: Optional[str] = None
+ ) -> Iterator[Document]:
+ """Yield documents from the data source.
+
+ If *since* is given, only return items created/modified after that time.
+ If *cursor* is given, resume from a previous checkpoint.
+ """
+
+ @abstractmethod
+ def sync_status(self) -> SyncStatus:
+ """Return current sync progress."""
+
+ def auth_url(self) -> str:
+ """Generate an OAuth consent URL. Only relevant for auth_type='oauth'."""
+ raise NotImplementedError(f"{self.connector_id} does not use OAuth")
+
+ def handle_callback(self, code: str) -> None:
+ """Handle the OAuth callback. Only relevant for auth_type='oauth'."""
+ raise NotImplementedError(f"{self.connector_id} does not use OAuth")
+
+ def mcp_tools(self) -> List[ToolSpec]:
+ """Return MCP tool specs for real-time agent queries. Optional."""
+ return []
diff --git a/src/openjarvis/core/registry.py b/src/openjarvis/core/registry.py
index f0fd1735..ec055efa 100644
--- a/src/openjarvis/core/registry.py
+++ b/src/openjarvis/core/registry.py
@@ -145,11 +145,16 @@ class CompressionRegistry(RegistryBase[Any]):
"""Registry for context compression strategies."""
+class ConnectorRegistry(RegistryBase[Any]):
+ """Registry for data source connectors (Gmail, Slack, etc.)."""
+
+
__all__ = [
"AgentRegistry",
"BenchmarkRegistry",
"ChannelRegistry",
"CompressionRegistry",
+ "ConnectorRegistry",
"EngineRegistry",
"LearningRegistry",
"MemoryRegistry",
diff --git a/tests/conftest.py b/tests/conftest.py
index cd709c1e..8c2a9295 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -14,6 +14,7 @@ from openjarvis.core.registry import (
BenchmarkRegistry,
ChannelRegistry,
CompressionRegistry,
+ ConnectorRegistry,
EngineRegistry,
MemoryRegistry,
ModelRegistry,
@@ -36,6 +37,7 @@ def _clean_registries() -> None:
ChannelRegistry.clear()
SpeechRegistry.clear()
CompressionRegistry.clear()
+ ConnectorRegistry.clear()
reset_event_bus()
diff --git a/tests/connectors/__init__.py b/tests/connectors/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/connectors/test_stubs.py b/tests/connectors/test_stubs.py
new file mode 100644
index 00000000..23c99ef4
--- /dev/null
+++ b/tests/connectors/test_stubs.py
@@ -0,0 +1,103 @@
+"""Tests for connector base types and registry."""
+
+from __future__ import annotations
+
+from datetime import datetime
+from typing import Iterator, List, Optional
+
+from openjarvis.connectors._stubs import (
+ Attachment,
+ BaseConnector,
+ Document,
+ SyncStatus,
+)
+from openjarvis.core.registry import ConnectorRegistry
+from openjarvis.tools._stubs import ToolSpec
+
+
+class FakeConnector(BaseConnector):
+ connector_id = "fake"
+ display_name = "Fake"
+ auth_type = "filesystem"
+
+ def __init__(self) -> None:
+ self._connected = True
+
+ def is_connected(self) -> bool:
+ return self._connected
+
+ def disconnect(self) -> None:
+ self._connected = False
+
+ def sync(
+ self, *, since: Optional[datetime] = None, cursor: Optional[str] = None
+ ) -> Iterator[Document]:
+ yield Document(
+ doc_id="fake:1",
+ source="fake",
+ doc_type="note",
+ content="Hello world",
+ title="Test",
+ )
+
+ def sync_status(self) -> SyncStatus:
+ return SyncStatus(state="idle", items_synced=1, items_total=1)
+
+
+def test_document_creation() -> None:
+ doc = Document(
+ doc_id="gmail:abc123",
+ source="gmail",
+ doc_type="email",
+ content="Meeting tomorrow at 3pm",
+ title="Re: Project sync",
+ author="[email protected]",
+ participants=["[email protected]", "[email protected]"],
+ )
+ assert doc.source == "gmail"
+ assert doc.doc_type == "email"
+ assert doc.thread_id is None
+ assert doc.attachments == []
+
+
+def test_attachment_creation() -> None:
+ att = Attachment(
+ filename="report.pdf",
+ mime_type="application/pdf",
+ size_bytes=1024,
+ sha256="abcdef1234567890",
+ )
+ assert att.filename == "report.pdf"
+ assert att.content == b""
+
+
+def test_sync_status_defaults() -> None:
+ status = SyncStatus()
+ assert status.state == "idle"
+ assert status.items_synced == 0
+ assert status.cursor is None
+ assert status.error is None
+
+
+def test_base_connector_lifecycle() -> None:
+ conn = FakeConnector()
+ assert conn.is_connected()
+ docs = list(conn.sync())
+ assert len(docs) == 1
+ assert docs[0].doc_id == "fake:1"
+ assert conn.sync_status().state == "idle"
+ conn.disconnect()
+ assert not conn.is_connected()
+
+
+def test_connector_registry() -> None:
+ ConnectorRegistry.register_value("fake", FakeConnector)
+ assert ConnectorRegistry.contains("fake")
+ cls = ConnectorRegistry.get("fake")
+ instance = cls()
+ assert instance.connector_id == "fake"
+
+
+def test_mcp_tools_default_empty() -> None:
+ conn = FakeConnector()
+ assert conn.mcp_tools() == []