maki-synapse 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- maki_synapse-0.0.1/.gitignore +207 -0
- maki_synapse-0.0.1/CHANGELOG.md +9 -0
- maki_synapse-0.0.1/PKG-INFO +5 -0
- maki_synapse-0.0.1/pyproject.toml +23 -0
- maki_synapse-0.0.1/src/maki_synapse/__init__.py +0 -0
- maki_synapse-0.0.1/src/maki_synapse/_version.py +1 -0
- maki_synapse-0.0.1/src/maki_synapse/main.py +223 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.0.1](https://github.com/adhityaravi/maki/compare/maki-synapse-v0.0.1...maki-synapse-v0.0.1) (2026-03-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* **ci:** adds relese please process ([9ffac85](https://github.com/adhityaravi/maki/commit/9ffac851b65f751ba2e4154547a8cea847b1eb54))
|
|
9
|
+
* v0 validating heartbeats 1-10 ([2f42a87](https://github.com/adhityaravi/maki/commit/2f42a87b843e36b8ccefc0ffe6a836306881d8d5))
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "maki-synapse"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
requires-python = ">=3.12"
|
|
5
|
+
dependencies = [
|
|
6
|
+
"maki-common[claude,fastapi]",
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
[project.scripts]
|
|
10
|
+
maki-synapse = "maki_synapse.main:cli"
|
|
11
|
+
|
|
12
|
+
[tool.uv.sources]
|
|
13
|
+
maki-common = { workspace = true }
|
|
14
|
+
|
|
15
|
+
[build-system]
|
|
16
|
+
requires = ["hatchling"]
|
|
17
|
+
build-backend = "hatchling.build"
|
|
18
|
+
|
|
19
|
+
[tool.hatch.version]
|
|
20
|
+
path = "src/maki_synapse/_version.py"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/maki_synapse"]
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.0.1" # x-release-please-version
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""maki-synapse: OpenAI-compatible LLM proxy backed by Claude Agent SDK.
|
|
2
|
+
|
|
3
|
+
Translates OpenAI chat completion requests (including tool calling) into
|
|
4
|
+
Claude SDK query() calls, using the host's Claude subscription via OAuth.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
import time
|
|
13
|
+
import uuid
|
|
14
|
+
|
|
15
|
+
from fastapi import FastAPI, HTTPException
|
|
16
|
+
from maki_common import configure_logging
|
|
17
|
+
from maki_common.claude import invoke_claude
|
|
18
|
+
from pydantic import BaseModel
|
|
19
|
+
|
|
20
|
+
configure_logging()
|
|
21
|
+
log = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
app = FastAPI(title="maki-synapse", version="0.0.1")
|
|
24
|
+
|
|
25
|
+
MAX_CONCURRENT = int(os.environ.get("MAX_CONCURRENT_QUERIES", "3"))
|
|
26
|
+
_semaphore = asyncio.Semaphore(MAX_CONCURRENT)
|
|
27
|
+
|
|
28
|
+
MODEL = os.environ.get("CLAUDE_MODEL", "claude-sonnet-4-20250514")
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# --- Request / Response models (OpenAI-compatible subset) ---
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class ChatMessage(BaseModel):
|
|
35
|
+
role: str
|
|
36
|
+
content: str | None = None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ToolFunction(BaseModel):
|
|
40
|
+
name: str
|
|
41
|
+
description: str = ""
|
|
42
|
+
parameters: dict = {}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ToolDefinition(BaseModel):
|
|
46
|
+
type: str = "function"
|
|
47
|
+
function: ToolFunction
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ChatCompletionRequest(BaseModel):
|
|
51
|
+
model: str = MODEL
|
|
52
|
+
messages: list[ChatMessage]
|
|
53
|
+
tools: list[ToolDefinition] | None = None
|
|
54
|
+
tool_choice: str | None = "auto"
|
|
55
|
+
temperature: float | None = 0
|
|
56
|
+
max_tokens: int | None = 2000
|
|
57
|
+
response_format: dict | None = None
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class ToolCallFunction(BaseModel):
|
|
61
|
+
name: str
|
|
62
|
+
arguments: str
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ToolCallItem(BaseModel):
|
|
66
|
+
id: str
|
|
67
|
+
type: str = "function"
|
|
68
|
+
function: ToolCallFunction
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ResponseMessage(BaseModel):
|
|
72
|
+
role: str = "assistant"
|
|
73
|
+
content: str | None = None
|
|
74
|
+
tool_calls: list[ToolCallItem] | None = None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class Choice(BaseModel):
|
|
78
|
+
index: int = 0
|
|
79
|
+
message: ResponseMessage
|
|
80
|
+
finish_reason: str = "stop"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class Usage(BaseModel):
|
|
84
|
+
prompt_tokens: int = 0
|
|
85
|
+
completion_tokens: int = 0
|
|
86
|
+
total_tokens: int = 0
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class ChatCompletionResponse(BaseModel):
|
|
90
|
+
id: str
|
|
91
|
+
object: str = "chat.completion"
|
|
92
|
+
created: int
|
|
93
|
+
model: str
|
|
94
|
+
choices: list[Choice]
|
|
95
|
+
usage: Usage = Usage()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# --- Tool prompt building ---
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def build_tool_prompt(tools: list[ToolDefinition]) -> str:
|
|
102
|
+
tool_descs = []
|
|
103
|
+
for t in tools:
|
|
104
|
+
tool_descs.append(
|
|
105
|
+
f"- {t.function.name}: {t.function.description}\n Parameters schema: {json.dumps(t.function.parameters)}"
|
|
106
|
+
)
|
|
107
|
+
return (
|
|
108
|
+
"\n\n---\n"
|
|
109
|
+
"You have access to the following tools. To call a tool, respond ONLY "
|
|
110
|
+
"with a JSON object in this exact format (no markdown, no explanation, no extra text):\n"
|
|
111
|
+
'{"tool_calls": [{"name": "<tool_name>", "arguments": {<arguments>}}]}\n\n'
|
|
112
|
+
"If you don't need to call any tools, respond with plain text.\n\n"
|
|
113
|
+
"Available tools:\n" + "\n".join(tool_descs)
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# --- JSON extraction ---
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def extract_json_str(text: str) -> str:
|
|
121
|
+
"""Extract JSON from text that may contain markdown code blocks."""
|
|
122
|
+
m = re.search(r"```(?:json)?\s*\n?(.*?)\n?```", text, re.DOTALL)
|
|
123
|
+
if m:
|
|
124
|
+
return m.group(1).strip()
|
|
125
|
+
start = text.find("{")
|
|
126
|
+
end = text.rfind("}")
|
|
127
|
+
if start != -1 and end != -1 and end > start:
|
|
128
|
+
return text[start : end + 1]
|
|
129
|
+
return text
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
# --- Endpoints ---
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@app.get("/health")
|
|
136
|
+
def health():
|
|
137
|
+
return {"status": "ok"}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@app.get("/v1/models")
|
|
141
|
+
def list_models():
|
|
142
|
+
return {"data": [{"id": MODEL, "object": "model"}]}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@app.post("/v1/chat/completions")
|
|
146
|
+
async def chat_completions(req: ChatCompletionRequest):
|
|
147
|
+
system_parts = []
|
|
148
|
+
user_parts = []
|
|
149
|
+
|
|
150
|
+
for msg in req.messages:
|
|
151
|
+
if msg.role == "system":
|
|
152
|
+
system_parts.append(msg.content or "")
|
|
153
|
+
elif msg.role == "user":
|
|
154
|
+
user_parts.append(msg.content or "")
|
|
155
|
+
elif msg.role == "assistant":
|
|
156
|
+
user_parts.append(f"Assistant: {msg.content or ''}")
|
|
157
|
+
|
|
158
|
+
system_prompt = "\n".join(system_parts)
|
|
159
|
+
|
|
160
|
+
if req.tools:
|
|
161
|
+
system_prompt += build_tool_prompt(req.tools)
|
|
162
|
+
|
|
163
|
+
if req.response_format and req.response_format.get("type") == "json_object":
|
|
164
|
+
system_prompt += "\n\nYou MUST respond with valid JSON only."
|
|
165
|
+
|
|
166
|
+
user_prompt = "\n".join(user_parts)
|
|
167
|
+
full_prompt = f"{system_prompt}\n\n---\n\n{user_prompt}" if system_prompt else user_prompt
|
|
168
|
+
|
|
169
|
+
try:
|
|
170
|
+
log.info("Invoking Claude", extra={"tools": bool(req.tools), "user_prompt_len": len(user_prompt)})
|
|
171
|
+
text = await invoke_claude(full_prompt, model=MODEL, semaphore=_semaphore)
|
|
172
|
+
log.info("Claude response", extra={"response_len": len(text)})
|
|
173
|
+
except Exception as e:
|
|
174
|
+
log.exception("Claude invocation failed")
|
|
175
|
+
raise HTTPException(status_code=502, detail=str(e))
|
|
176
|
+
|
|
177
|
+
# Parse response
|
|
178
|
+
message: ResponseMessage
|
|
179
|
+
finish_reason = "stop"
|
|
180
|
+
|
|
181
|
+
if req.tools and text.strip():
|
|
182
|
+
try:
|
|
183
|
+
raw = extract_json_str(text)
|
|
184
|
+
parsed = json.loads(raw)
|
|
185
|
+
if isinstance(parsed, dict) and "tool_calls" in parsed:
|
|
186
|
+
tool_calls = [
|
|
187
|
+
ToolCallItem(
|
|
188
|
+
id=f"call_{uuid.uuid4().hex[:8]}",
|
|
189
|
+
function=ToolCallFunction(
|
|
190
|
+
name=tc["name"],
|
|
191
|
+
arguments=(
|
|
192
|
+
json.dumps(tc["arguments"]) if isinstance(tc["arguments"], dict) else tc["arguments"]
|
|
193
|
+
),
|
|
194
|
+
),
|
|
195
|
+
)
|
|
196
|
+
for tc in parsed["tool_calls"]
|
|
197
|
+
]
|
|
198
|
+
message = ResponseMessage(content=None, tool_calls=tool_calls)
|
|
199
|
+
finish_reason = "tool_calls"
|
|
200
|
+
else:
|
|
201
|
+
message = ResponseMessage(content=text)
|
|
202
|
+
except (json.JSONDecodeError, KeyError, TypeError) as e:
|
|
203
|
+
log.warning("Failed to parse tool response, returning as text", extra={"error": str(e)})
|
|
204
|
+
message = ResponseMessage(content=text)
|
|
205
|
+
else:
|
|
206
|
+
message = ResponseMessage(content=text)
|
|
207
|
+
|
|
208
|
+
return ChatCompletionResponse(
|
|
209
|
+
id=f"chatcmpl-{uuid.uuid4().hex[:12]}",
|
|
210
|
+
created=int(time.time()),
|
|
211
|
+
model=req.model,
|
|
212
|
+
choices=[Choice(message=message, finish_reason=finish_reason)],
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def cli():
|
|
217
|
+
import uvicorn
|
|
218
|
+
|
|
219
|
+
uvicorn.run("maki_synapse.main:app", host="0.0.0.0", port=8080)
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
if __name__ == "__main__":
|
|
223
|
+
cli()
|