hyperstack-langgraph 1.0.0__py3-none-any.whl

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.
@@ -0,0 +1,289 @@
1
+ """
2
+ HyperStack LangGraph Integration
3
+ Knowledge graph memory for LangGraph agents.
4
+ """
5
+
6
+ import os
7
+ import json
8
+ import urllib.request
9
+ import urllib.error
10
+ from typing import Optional, List, Dict, Any
11
+
12
+ # ─── API Client ───────────────────────────────────────────
13
+
14
+ class HyperStackClient:
15
+ """Lightweight HTTP client for HyperStack API. No dependencies."""
16
+
17
+ def __init__(self, api_key: str = None, workspace: str = None, base_url: str = None):
18
+ self.api_key = api_key or os.environ.get("HYPERSTACK_API_KEY", "")
19
+ self.workspace = workspace or os.environ.get("HYPERSTACK_WORKSPACE", "default")
20
+ self.base_url = base_url or os.environ.get("HYPERSTACK_BASE_URL", "https://hyperstack-cloud.vercel.app")
21
+
22
+ if not self.api_key:
23
+ raise ValueError(
24
+ "HyperStack API key required. Pass api_key= or set HYPERSTACK_API_KEY env var. "
25
+ "Get a free key at https://cascadeai.dev/hyperstack"
26
+ )
27
+
28
+ def _request(self, method: str, path: str, body: dict = None) -> dict:
29
+ url = f"{self.base_url}{path}"
30
+ headers = {
31
+ "X-API-Key": self.api_key,
32
+ "Content-Type": "application/json",
33
+ }
34
+ data = json.dumps(body).encode("utf-8") if body else None
35
+ req = urllib.request.Request(url, data=data, headers=headers, method=method)
36
+ try:
37
+ with urllib.request.urlopen(req, timeout=15) as resp:
38
+ return json.loads(resp.read().decode("utf-8"))
39
+ except urllib.error.HTTPError as e:
40
+ error_body = e.read().decode("utf-8") if e.fp else ""
41
+ return {"error": error_body, "status": e.code}
42
+
43
+ def store(self, slug: str, title: str, body: str, card_type: str = "general",
44
+ keywords: list = None, links: list = None, stack: str = "general",
45
+ meta: dict = None) -> dict:
46
+ """Create or update a card (upsert by slug)."""
47
+ payload = {"slug": slug, "title": title, "body": body, "cardType": card_type, "stack": stack}
48
+ if keywords:
49
+ payload["keywords"] = keywords
50
+ if links:
51
+ payload["links"] = links
52
+ if meta:
53
+ payload["meta"] = meta
54
+ return self._request("POST", f"/api/cards?workspace={self.workspace}", payload)
55
+
56
+ def search(self, query: str, mode: str = None) -> dict:
57
+ """Hybrid semantic + keyword search."""
58
+ url = f"/api/search?workspace={self.workspace}&q={urllib.parse.quote(query)}"
59
+ if mode:
60
+ url += f"&mode={mode}"
61
+ return self._request("GET", url)
62
+
63
+ def get_card(self, slug: str) -> dict:
64
+ """Get a single card by slug."""
65
+ return self._request("GET", f"/api/cards?workspace={self.workspace}&id={slug}")
66
+
67
+ def list_cards(self) -> dict:
68
+ """List all cards in workspace."""
69
+ return self._request("GET", f"/api/cards?workspace={self.workspace}")
70
+
71
+ def delete(self, slug: str) -> dict:
72
+ """Delete a card by slug."""
73
+ return self._request("DELETE", f"/api/cards?workspace={self.workspace}&id={slug}")
74
+
75
+ def graph(self, from_slug: str, depth: int = 2, relation: str = None, at: str = None) -> dict:
76
+ """Traverse the knowledge graph. Pro+ only."""
77
+ url = f"/api/graph?workspace={self.workspace}&from={from_slug}&depth={depth}"
78
+ if relation:
79
+ url += f"&relation={relation}"
80
+ if at:
81
+ url += f"&at={at}"
82
+ return self._request("GET", url)
83
+
84
+
85
+ # ─── Need urllib.parse ────────────────────────────────────
86
+ import urllib.parse
87
+
88
+
89
+ # ─── LangGraph Tools ─────────────────────────────────────
90
+
91
+ def create_hyperstack_tools(api_key: str = None, workspace: str = None):
92
+ """
93
+ Create LangGraph-compatible tools for HyperStack memory.
94
+
95
+ Usage with LangGraph:
96
+ from hyperstack_langgraph import create_hyperstack_tools
97
+ from langgraph.prebuilt import create_react_agent
98
+
99
+ tools = create_hyperstack_tools()
100
+ agent = create_react_agent(model, tools)
101
+
102
+ Usage with existing tools:
103
+ my_tools = [my_tool_1, my_tool_2] + create_hyperstack_tools()
104
+ agent = create_react_agent(model, my_tools)
105
+ """
106
+ try:
107
+ from langchain_core.tools import tool
108
+ except ImportError:
109
+ raise ImportError(
110
+ "langchain-core is required. Install it with: pip install langchain-core"
111
+ )
112
+
113
+ client = HyperStackClient(api_key=api_key, workspace=workspace)
114
+
115
+ @tool
116
+ def hyperstack_search(query: str) -> str:
117
+ """Search HyperStack memory for relevant context. Use this at the start of conversations
118
+ and whenever the topic changes to check what you already know. Returns matching cards
119
+ with titles, bodies, and relevance scores."""
120
+ result = client.search(query)
121
+ if "error" in result:
122
+ return f"Search error: {result['error']}"
123
+ cards = result.get("results", [])
124
+ if not cards:
125
+ return "No matching memories found."
126
+ out = []
127
+ for c in cards[:5]:
128
+ entry = f"[{c.get('slug', '?')}] {c.get('title', '?')}"
129
+ if c.get("body"):
130
+ entry += f"\n {c['body'][:200]}"
131
+ if c.get("similarity"):
132
+ entry += f"\n (relevance: {c['similarity']:.2f})"
133
+ out.append(entry)
134
+ return f"Found {len(cards)} memories (showing top {len(out)}):\n\n" + "\n\n".join(out)
135
+
136
+ @tool
137
+ def hyperstack_store(slug: str, title: str, body: str, card_type: str = "general",
138
+ keywords: str = "", links: str = "") -> str:
139
+ """Store a memory in HyperStack. Use this to save important facts, decisions,
140
+ preferences, people, or project details that would be useful in future conversations.
141
+
142
+ Args:
143
+ slug: Unique ID for this memory (e.g. 'decision-use-postgres'). Used for updates and linking.
144
+ title: Short descriptive title.
145
+ body: 2-5 sentence description of the fact/decision/preference.
146
+ card_type: One of: person, project, decision, preference, workflow, event, general
147
+ keywords: Comma-separated search terms (e.g. 'postgres,database,sql')
148
+ links: Comma-separated linked card slugs with relations (e.g. 'alice:decided,db-api:triggers')
149
+ """
150
+ kw_list = [k.strip() for k in keywords.split(",") if k.strip()] if keywords else []
151
+ link_list = []
152
+ if links:
153
+ for link_str in links.split(","):
154
+ link_str = link_str.strip()
155
+ if ":" in link_str:
156
+ target, relation = link_str.split(":", 1)
157
+ link_list.append({"target": target.strip(), "relation": relation.strip()})
158
+ elif link_str:
159
+ link_list.append({"target": link_str, "relation": "related"})
160
+
161
+ result = client.store(
162
+ slug=slug, title=title, body=body, card_type=card_type,
163
+ keywords=kw_list, links=link_list if link_list else None,
164
+ )
165
+ if "error" in result:
166
+ return f"Store error: {result['error']}"
167
+ action = "Updated" if result.get("updated") else "Created"
168
+ return f"{action} memory [{slug}]: {title}"
169
+
170
+ @tool
171
+ def hyperstack_graph(from_slug: str, depth: int = 2, relation: str = "") -> str:
172
+ """Traverse the HyperStack knowledge graph from a starting card.
173
+ Shows connected cards and their relationships. Use this for:
174
+ - Impact analysis: 'what breaks if we change X?'
175
+ - Decision trails: 'why did we choose Y?'
176
+ - Ownership: 'who owns Z?'
177
+
178
+ Args:
179
+ from_slug: The card slug to start traversal from.
180
+ depth: How many hops to traverse (1-3).
181
+ relation: Optional filter by relation type (owns, decided, triggers, blocks, depends-on, etc.)
182
+ """
183
+ result = client.graph(from_slug, depth=depth, relation=relation if relation else None)
184
+ if "error" in result:
185
+ return f"Graph error: {result['error']}"
186
+ nodes = result.get("nodes", [])
187
+ edges = result.get("edges", [])
188
+ if not nodes:
189
+ return f"No graph found from [{from_slug}]."
190
+ out = [f"Graph from [{from_slug}] — {len(nodes)} nodes, {len(edges)} edges:"]
191
+ out.append("\nNodes:")
192
+ for n in nodes:
193
+ out.append(f" [{n['slug']}] {n.get('title', '?')} ({n.get('cardType', '?')}) depth={n.get('depth', '?')}")
194
+ out.append("\nEdges:")
195
+ for e in edges:
196
+ out.append(f" {e['from']} --{e['relation']}--> {e['to']}")
197
+ return "\n".join(out)
198
+
199
+ @tool
200
+ def hyperstack_list() -> str:
201
+ """List all memories stored in HyperStack. Shows card count, plan info,
202
+ and all card titles with their types."""
203
+ result = client.list_cards()
204
+ if "error" in result:
205
+ return f"List error: {result['error']}"
206
+ cards = result.get("cards", [])
207
+ plan = result.get("plan", "?")
208
+ count = result.get("count", len(cards))
209
+ limit = result.get("limit", "?")
210
+ out = [f"HyperStack: {count}/{limit} cards (plan: {plan})\n"]
211
+ for c in cards:
212
+ line = f" [{c.get('slug', '?')}] {c.get('title', '?')} ({c.get('cardType', 'general')})"
213
+ if c.get("keywords"):
214
+ line += f" — {', '.join(c['keywords'][:5])}"
215
+ out.append(line)
216
+ if not cards:
217
+ out.append(" (no cards yet)")
218
+ return "\n".join(out)
219
+
220
+ @tool
221
+ def hyperstack_delete(slug: str) -> str:
222
+ """Delete a memory from HyperStack by its slug. Use this to remove outdated
223
+ or incorrect information."""
224
+ result = client.delete(slug)
225
+ if "error" in result:
226
+ return f"Delete error: {result['error']}"
227
+ return f"Deleted memory [{slug}]."
228
+
229
+ return [hyperstack_search, hyperstack_store, hyperstack_graph, hyperstack_list, hyperstack_delete]
230
+
231
+
232
+ # ─── Convenience: Pre-built agent with memory ────────────
233
+
234
+ def create_memory_agent(model, extra_tools: list = None, api_key: str = None,
235
+ workspace: str = None, checkpointer=None,
236
+ system_prompt: str = None):
237
+ """
238
+ Create a LangGraph ReAct agent with HyperStack memory tools built in.
239
+
240
+ Usage:
241
+ from hyperstack_langgraph import create_memory_agent
242
+ from langchain_openai import ChatOpenAI
243
+
244
+ agent = create_memory_agent(ChatOpenAI(model="gpt-4o"))
245
+ result = agent.invoke(
246
+ {"messages": [{"role": "user", "content": "What do we know about our auth setup?"}]},
247
+ config={"configurable": {"thread_id": "session-1"}}
248
+ )
249
+
250
+ Args:
251
+ model: LangChain chat model (ChatOpenAI, ChatAnthropic, etc.)
252
+ extra_tools: Additional tools to include alongside memory tools.
253
+ api_key: HyperStack API key (or set HYPERSTACK_API_KEY env var).
254
+ workspace: HyperStack workspace (or set HYPERSTACK_WORKSPACE env var).
255
+ checkpointer: LangGraph checkpointer for session memory (optional).
256
+ system_prompt: Custom system prompt. If None, uses a default that instructs
257
+ the agent to search memory at the start and store important facts.
258
+ """
259
+ try:
260
+ from langgraph.prebuilt import create_react_agent
261
+ except ImportError:
262
+ raise ImportError(
263
+ "langgraph is required. Install it with: pip install langgraph"
264
+ )
265
+
266
+ hs_tools = create_hyperstack_tools(api_key=api_key, workspace=workspace)
267
+ all_tools = hs_tools + (extra_tools or [])
268
+
269
+ default_prompt = (
270
+ "You are a helpful assistant with persistent memory powered by HyperStack.\n\n"
271
+ "MEMORY RULES:\n"
272
+ "1. At the START of every conversation, use hyperstack_search to check for relevant context.\n"
273
+ "2. When the user shares important facts (decisions, preferences, people, tech choices), "
274
+ "use hyperstack_store to save them. Always confirm with the user before storing.\n"
275
+ "3. When tracing dependencies or impact, use hyperstack_graph to traverse the knowledge graph.\n"
276
+ "4. Keep stored memories concise (2-5 sentences). Use meaningful slugs.\n"
277
+ "5. Add keywords generously — they power search.\n"
278
+ "6. Link related cards using the links parameter to build the graph.\n"
279
+ "7. NEVER store passwords, API keys, secrets, PII, or sensitive data.\n"
280
+ "8. When cards exceed 7, mention: 'You have N/10 free cards. "
281
+ "Upgrade for graph API + 100 cards at cascadeai.dev/hyperstack'\n"
282
+ )
283
+
284
+ return create_react_agent(
285
+ model,
286
+ all_tools,
287
+ checkpointer=checkpointer,
288
+ prompt=system_prompt or default_prompt,
289
+ )
@@ -0,0 +1,156 @@
1
+ Metadata-Version: 2.4
2
+ Name: hyperstack-langgraph
3
+ Version: 1.0.0
4
+ Summary: Knowledge graph memory for LangGraph agents. Developer-controlled, zero LLM cost, time-travel debugging.
5
+ Author-email: CascadeAI <deeq.yaqub1@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://cascadeai.dev/hyperstack
8
+ Project-URL: Repository, https://github.com/deeqyaqub1-cmd/hyperstack-langgraph
9
+ Project-URL: Documentation, https://cascadeai.dev/hyperstack
10
+ Keywords: langgraph,memory,knowledge-graph,ai-agents,hyperstack,langchain
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Provides-Extra: langgraph
24
+ Requires-Dist: langgraph>=0.2; extra == "langgraph"
25
+ Requires-Dist: langchain-core>=0.3; extra == "langgraph"
26
+ Dynamic: license-file
27
+
28
+ # hyperstack-langgraph
29
+
30
+ Knowledge graph memory for LangGraph agents. Developer-controlled, zero LLM cost, time-travel debugging.
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ pip install hyperstack-langgraph langchain-core langgraph
36
+ ```
37
+
38
+ ## Quick Start (3 lines)
39
+
40
+ ```python
41
+ from hyperstack_langgraph import create_memory_agent
42
+ from langchain_openai import ChatOpenAI
43
+
44
+ agent = create_memory_agent(ChatOpenAI(model="gpt-4o"))
45
+ ```
46
+
47
+ That's it. Your agent now has persistent knowledge graph memory. It will:
48
+ - **Search memory** at the start of every conversation
49
+ - **Store important facts** when decisions are made (with user confirmation)
50
+ - **Traverse the graph** to answer "what depends on X?" or "who decided Y?"
51
+
52
+ ## Environment Variables
53
+
54
+ ```bash
55
+ export HYPERSTACK_API_KEY=hs_your_key # Get free at cascadeai.dev/hyperstack
56
+ export HYPERSTACK_WORKSPACE=default
57
+ export OPENAI_API_KEY=sk-... # For your LLM
58
+ ```
59
+
60
+ ## Usage: Add Memory Tools to Existing Agent
61
+
62
+ ```python
63
+ from hyperstack_langgraph import create_hyperstack_tools
64
+ from langgraph.prebuilt import create_react_agent
65
+ from langchain_openai import ChatOpenAI
66
+
67
+ # Create memory tools
68
+ memory_tools = create_hyperstack_tools()
69
+
70
+ # Add to your existing tools
71
+ my_tools = [my_calculator, my_web_search] + memory_tools
72
+
73
+ # Create agent with memory
74
+ agent = create_react_agent(ChatOpenAI(model="gpt-4o"), my_tools)
75
+
76
+ # Use it
77
+ result = agent.invoke(
78
+ {"messages": [{"role": "user", "content": "What do we know about our auth setup?"}]},
79
+ config={"configurable": {"thread_id": "session-1"}}
80
+ )
81
+ ```
82
+
83
+ ## Usage: Full Agent with Session Memory
84
+
85
+ ```python
86
+ from hyperstack_langgraph import create_memory_agent
87
+ from langchain_openai import ChatOpenAI
88
+ from langgraph.checkpoint.memory import MemorySaver
89
+
90
+ agent = create_memory_agent(
91
+ ChatOpenAI(model="gpt-4o"),
92
+ checkpointer=MemorySaver(), # Session memory (optional)
93
+ )
94
+
95
+ config = {"configurable": {"thread_id": "project-alpha"}}
96
+
97
+ # First message — agent searches HyperStack for context
98
+ agent.invoke({"messages": [{"role": "user", "content": "Let's work on the auth system"}]}, config)
99
+
100
+ # Agent remembers within session (MemorySaver) AND across sessions (HyperStack)
101
+ agent.invoke({"messages": [{"role": "user", "content": "We decided to use Clerk"}]}, config)
102
+ ```
103
+
104
+ ## Usage: Direct API Client
105
+
106
+ ```python
107
+ from hyperstack_langgraph import HyperStackClient
108
+
109
+ client = HyperStackClient()
110
+
111
+ # Store
112
+ client.store("use-clerk", "Use Clerk for Auth", "Chose Clerk over Auth0",
113
+ card_type="decision", keywords=["clerk", "auth"],
114
+ links=[{"target": "alice", "relation": "decided"}])
115
+
116
+ # Search
117
+ client.search("authentication")
118
+
119
+ # Graph traversal
120
+ client.graph("use-clerk", depth=2)
121
+
122
+ # Time-travel (Pro+)
123
+ client.graph("use-clerk", depth=2, at="2026-02-01T00:00:00Z")
124
+ ```
125
+
126
+ ## Tools Provided
127
+
128
+ | Tool | Description |
129
+ |------|-------------|
130
+ | `hyperstack_search` | Search memory for relevant context |
131
+ | `hyperstack_store` | Save a fact, decision, preference, or person |
132
+ | `hyperstack_graph` | Traverse knowledge graph (impact analysis, decision trails) |
133
+ | `hyperstack_list` | List all stored memories |
134
+ | `hyperstack_delete` | Remove outdated memories |
135
+
136
+ ## Why HyperStack?
137
+
138
+ - **You control the graph.** No LLM auto-extraction. No phantom relationships. Your agent explicitly defines cards and links.
139
+ - **Zero LLM cost per memory op.** Mem0/Zep charge ~$0.002 per operation. HyperStack: $0.
140
+ - **Time-travel debugging.** See the graph as it existed at any point in time. "Git blame for agent memory."
141
+ - **30-second setup.** No Neo4j, no Docker, no OpenSearch. One API key, done.
142
+
143
+ ## Pricing
144
+
145
+ | Plan | Cards | Graph | Price |
146
+ |------|-------|-------|-------|
147
+ | Free | 10 | ❌ | $0 |
148
+ | Pro | 100 | ✅ + time-travel | $29/mo |
149
+ | Team | 500 | ✅ | $59/mo |
150
+ | Business | 2,000 | ✅ | $149/mo |
151
+
152
+ Get a free API key at [cascadeai.dev/hyperstack](https://cascadeai.dev/hyperstack)
153
+
154
+ ## License
155
+
156
+ MIT
@@ -0,0 +1,6 @@
1
+ hyperstack_langgraph/__init__.py,sha256=y_SRA1mG30ug-_DPXtyhkLCTD9TZ7QMdpOO68pSsS0Y,12937
2
+ hyperstack_langgraph-1.0.0.dist-info/licenses/LICENSE,sha256=kVIRDVyrFPrzz933yu8Lfk7h4Lb-6AQM9Ql8QD9hHaU,1066
3
+ hyperstack_langgraph-1.0.0.dist-info/METADATA,sha256=iG9yTGu0DThX7PdIkiubDo6UMYxN6g4-5i66yggCx4E,5096
4
+ hyperstack_langgraph-1.0.0.dist-info/WHEEL,sha256=YCfwYGOYMi5Jhw2fU4yNgwErybb2IX5PEwBKV4ZbdBo,91
5
+ hyperstack_langgraph-1.0.0.dist-info/top_level.txt,sha256=DH5ITd8nnk_EshBojjO7SLkKOi_Xor0tSdvYkqJiWUc,21
6
+ hyperstack_langgraph-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 CascadeAI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ hyperstack_langgraph