Skip to content
Merged
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
12 changes: 10 additions & 2 deletions plugin/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from engram import EngramClient

from .client_origin import client_origin_header

DEFAULT_BASE = "https://api.engram.weaviate.io"

_PROFILES = (
Expand Down Expand Up @@ -91,7 +93,9 @@ def get_client():
api_key = engram_api_key()
if not api_key:
return None
return EngramClient(api_key=api_key, base_url=engram_base_url())
return EngramClient(
api_key=api_key, base_url=engram_base_url(), headers=client_origin_header()
)


def engram_warning():
Expand All @@ -110,7 +114,11 @@ def engram_get(path):
whole instead of continuing with a half-resolved scope."""
base = engram_base_url().rstrip("/")
req = urllib.request.Request(
base + path, headers={"Authorization": f"Bearer {engram_api_key()}"}
base + path,
headers={
"Authorization": f"Bearer {engram_api_key()}",
**client_origin_header(),
},
)
with urllib.request.urlopen(req, timeout=5) as resp:
return json.load(resp)
22 changes: 22 additions & 0 deletions plugin/core/client_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
import os

_MANIFEST = os.path.join(
os.path.dirname(__file__), "..", ".claude-plugin", "plugin.json"
)


def _platform():
return "claude"
Comment on lines +9 to +10

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this not just be a constant?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's just left for extending -> later function will resolve which plugin is the origin when there' gonna be multiple



def _plugin_version():
try:
with open(_MANIFEST) as f:
return json.load(f).get("version", "unknown")
except Exception:
return "unknown"


def client_origin_header():
return {"X-Engram-Client": f"{_platform()}-plugin/{_plugin_version()}"}
Comment on lines +1 to +22
44 changes: 44 additions & 0 deletions plugin/tests/test_client_origin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Loads the module directly by path — importing the `core` package would pull in the
Engram SDK, which only exists in the plugin venv."""

import importlib.util
import json
import os
import unittest
from unittest import mock

_HERE = os.path.dirname(__file__)
_MODULE = os.path.join(_HERE, "..", "core", "client_origin.py")

spec = importlib.util.spec_from_file_location("client_origin", _MODULE)
client_origin = importlib.util.module_from_spec(spec)
spec.loader.exec_module(client_origin)


class PlatformTest(unittest.TestCase):
def test_claude(self):
self.assertEqual(client_origin._platform(), "claude")


class PluginVersionTest(unittest.TestCase):
def test_reads_manifest(self):
with open(client_origin._MANIFEST) as f:
expected = json.load(f)["version"]
self.assertEqual(client_origin._plugin_version(), expected)

def test_missing_manifest(self):
with mock.patch.object(client_origin, "_MANIFEST", "/nonexistent/plugin.json"):
self.assertEqual(client_origin._plugin_version(), "unknown")


class HeaderTest(unittest.TestCase):
def test_format(self):
headers = client_origin.client_origin_header()
self.assertEqual(list(headers), ["X-Engram-Client"])
platform, _, version = headers["X-Engram-Client"].partition("/")
self.assertEqual(platform, "claude-plugin")
self.assertEqual(version, client_origin._plugin_version())


if __name__ == "__main__":
unittest.main()