chore: import upstream snapshot with attribution
CI / changes (push) Has been cancelled
CI / cd libs/checkpoint (push) Has been cancelled
CI / cd libs/checkpoint-conformance (push) Has been cancelled
CI / cd libs/checkpoint-postgres (push) Has been cancelled
CI / cd libs/checkpoint-sqlite (push) Has been cancelled
CI / cd libs/cli (push) Has been cancelled
CI / cd libs/prebuilt (push) Has been cancelled
CI / cd libs/sdk-py (push) Has been cancelled
CI / cd libs/langgraph (push) Has been cancelled
CI / Check SDK methods matching (push) Has been cancelled
CI / Check CLI schema hasn't changed #3.13 (push) Has been cancelled
CI / CLI integration test (push) Has been cancelled
CI / sdk-py integration test (push) Has been cancelled
CI / CI Success (push) Has been cancelled
baseline / benchmark (push) Has been cancelled
Deploy Redirects to GitHub Pages / deploy (push) Has been cancelled
CI / changes (push) Has been cancelled
CI / cd libs/checkpoint (push) Has been cancelled
CI / cd libs/checkpoint-conformance (push) Has been cancelled
CI / cd libs/checkpoint-postgres (push) Has been cancelled
CI / cd libs/checkpoint-sqlite (push) Has been cancelled
CI / cd libs/cli (push) Has been cancelled
CI / cd libs/prebuilt (push) Has been cancelled
CI / cd libs/sdk-py (push) Has been cancelled
CI / cd libs/langgraph (push) Has been cancelled
CI / Check SDK methods matching (push) Has been cancelled
CI / Check CLI schema hasn't changed #3.13 (push) Has been cancelled
CI / CLI integration test (push) Has been cancelled
CI / sdk-py integration test (push) Has been cancelled
CI / CI Success (push) Has been cancelled
baseline / benchmark (push) Has been cancelled
Deploy Redirects to GitHub Pages / deploy (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1 @@
|
||||
.langgraph_api/
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 LangChain, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,41 @@
|
||||
.PHONY: test lint type format test-integration update-schema bump-version
|
||||
|
||||
######################
|
||||
# TESTING AND COVERAGE
|
||||
######################
|
||||
|
||||
TEST?= "tests/unit_tests"
|
||||
test:
|
||||
uv run pytest $(TEST)
|
||||
test-integration:
|
||||
uv run pytest tests/integration_tests
|
||||
|
||||
######################
|
||||
# LINTING AND FORMATTING
|
||||
######################
|
||||
|
||||
# Define a variable for Python and notebook files.
|
||||
PYTHON_FILES=.
|
||||
lint format: PYTHON_FILES=.
|
||||
lint_diff format_diff: PYTHON_FILES=$(shell git diff --name-only --relative --diff-filter=d main . | grep -E '\.py$$|\.ipynb$$')
|
||||
lint_package: PYTHON_FILES=langgraph_cli
|
||||
lint_tests: PYTHON_FILES=tests
|
||||
|
||||
lint lint_diff lint_package lint_tests:
|
||||
uv run ruff check .
|
||||
[ "$(PYTHON_FILES)" = "" ] || uv run ruff format $(PYTHON_FILES) --diff
|
||||
[ "$(PYTHON_FILES)" = "" ] || uv run ruff check --select I $(PYTHON_FILES)
|
||||
[ "$(PYTHON_FILES)" = "" ] || uv run ty check $(PYTHON_FILES)
|
||||
|
||||
type:
|
||||
uv run ty check $(PYTHON_FILES)
|
||||
|
||||
format format_diff:
|
||||
uv run ruff format $(PYTHON_FILES)
|
||||
uv run ruff check --select I --fix $(PYTHON_FILES)
|
||||
|
||||
update-schema:
|
||||
uv run python generate_schema.py
|
||||
|
||||
bump-version:
|
||||
uv run hatch version patch
|
||||
@@ -0,0 +1,136 @@
|
||||
# LangGraph CLI
|
||||
|
||||
[](https://pypi.org/project/langgraph-cli/#history)
|
||||
[](https://opensource.org/licenses/MIT)
|
||||
[](https://pypistats.org/packages/langgraph-cli)
|
||||
[](https://x.com/langchain_oss)
|
||||
|
||||
To help you ship LangGraph apps to production faster, check out [LangSmith](https://www.langchain.com/langsmith).
|
||||
[LangSmith](https://www.langchain.com/langsmith) is a unified developer platform for building, testing, and monitoring LLM applications.
|
||||
|
||||
## Quick Install
|
||||
|
||||
```bash
|
||||
uv add langgraph-cli
|
||||
```
|
||||
|
||||
## 🤔 What is this?
|
||||
|
||||
The LangGraph CLI is the official command-line interface for LangGraph. It provides tools to create, develop, build, and run LangGraph applications locally or in Docker.
|
||||
|
||||
## 📖 Documentation
|
||||
|
||||
For full documentation, see the [LangGraph CLI reference](https://reference.langchain.com/python/langgraph-cli). For conceptual guides and tutorials, see the [LangGraph Docs](https://docs.langchain.com/oss/python/langgraph/overview).
|
||||
|
||||
For development mode with hot reloading:
|
||||
|
||||
```bash
|
||||
uv add "langgraph-cli[inmem]"
|
||||
```
|
||||
|
||||
## Commands
|
||||
|
||||
### `langgraph new` 🌱
|
||||
|
||||
Create a new LangGraph project from a template.
|
||||
|
||||
```bash
|
||||
langgraph new [PATH] --template TEMPLATE_NAME
|
||||
```
|
||||
|
||||
### `langgraph dev` 🏃♀️
|
||||
|
||||
Run LangGraph API server in development mode with hot reloading.
|
||||
|
||||
```bash
|
||||
langgraph dev [OPTIONS]
|
||||
--host TEXT Host to bind to (default: 127.0.0.1)
|
||||
--port INTEGER Port to bind to (default: 2024)
|
||||
--no-reload Disable auto-reload
|
||||
--debug-port INTEGER Enable remote debugging
|
||||
--no-browser Skip opening browser window
|
||||
-c, --config FILE Config file path (default: langgraph.json)
|
||||
```
|
||||
|
||||
### `langgraph up` 🚀
|
||||
|
||||
Launch LangGraph API server in Docker.
|
||||
|
||||
```bash
|
||||
langgraph up [OPTIONS]
|
||||
-p, --port INTEGER Port to expose (default: 8123)
|
||||
--wait Wait for services to start
|
||||
--watch Restart on file changes
|
||||
--verbose Show detailed logs
|
||||
-c, --config FILE Config file path
|
||||
-d, --docker-compose Additional services file
|
||||
```
|
||||
|
||||
### `langgraph build`
|
||||
|
||||
Build a Docker image for your LangGraph application.
|
||||
|
||||
```bash
|
||||
langgraph build -t IMAGE_TAG [OPTIONS]
|
||||
--platform TEXT Target platforms (e.g., linux/amd64,linux/arm64)
|
||||
--pull / --no-pull Use latest/local base image
|
||||
-c, --config FILE Config file path
|
||||
```
|
||||
|
||||
### `langgraph dockerfile`
|
||||
|
||||
Generate a Dockerfile for custom deployments.
|
||||
|
||||
```bash
|
||||
langgraph dockerfile SAVE_PATH [OPTIONS]
|
||||
-c, --config FILE Config file path
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
The CLI uses a `langgraph.json` configuration file with these key settings:
|
||||
|
||||
```json
|
||||
{
|
||||
"dependencies": ["langchain_openai", "./your_package"],
|
||||
"graphs": {
|
||||
"my_graph": "./your_package/file.py:graph"
|
||||
},
|
||||
"env": "./.env",
|
||||
"python_version": "3.11",
|
||||
"pip_config_file": "./pip.conf",
|
||||
"dockerfile_lines": []
|
||||
}
|
||||
```
|
||||
|
||||
See the [full documentation](https://reference.langchain.com/python/langgraph-cli) for detailed configuration options.
|
||||
|
||||
## Development
|
||||
|
||||
To develop the CLI itself:
|
||||
|
||||
1. Clone the repository
|
||||
2. Navigate to the CLI directory: `cd libs/cli`
|
||||
3. Install development dependencies: `uv sync`
|
||||
4. Make your changes to the CLI code
|
||||
5. Test your changes:
|
||||
|
||||
```bash
|
||||
# Run CLI commands directly
|
||||
uv run langgraph --help
|
||||
|
||||
# Or use the examples
|
||||
cd examples
|
||||
uv sync
|
||||
uv run langgraph dev # or other commands
|
||||
```
|
||||
|
||||
## 📕 Releases & Versioning
|
||||
|
||||
See our [Releases](https://docs.langchain.com/oss/python/release-policy) and [Versioning](https://docs.langchain.com/oss/python/versioning) policies.
|
||||
|
||||
## 💁 Contributing
|
||||
|
||||
As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.
|
||||
|
||||
For detailed information on how to contribute, see the [Contributing Guide](https://docs.langchain.com/oss/python/contributing/overview).
|
||||
@@ -0,0 +1,3 @@
|
||||
OPENAI_API_KEY=placeholder
|
||||
ANTHROPIC_API_KEY=placeholder
|
||||
TAVILY_API_KEY=placeholder
|
||||
@@ -0,0 +1 @@
|
||||
.langgraph-data
|
||||
@@ -0,0 +1,13 @@
|
||||
.PHONY: run_w_override
|
||||
|
||||
run:
|
||||
uv run langgraph up --watch --no-pull
|
||||
|
||||
run_faux:
|
||||
cd graphs && uv run langgraph up --no-pull
|
||||
|
||||
run_graphs_reqs_a:
|
||||
cd graphs_reqs_a && uv run langgraph up --no-pull
|
||||
|
||||
run_graphs_reqs_b:
|
||||
cd graphs_reqs_b && uv run langgraph up --no-pull
|
||||
@@ -0,0 +1,88 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.prebuilt import ToolNode
|
||||
|
||||
tools = []
|
||||
|
||||
model_oai = ChatOpenAI(temperature=0)
|
||||
|
||||
model_oai = model_oai.bind_tools(tools)
|
||||
|
||||
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
|
||||
|
||||
# Define the function that determines whether to continue or not
|
||||
def should_continue(state):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If there are no tool calls, then we finish
|
||||
if not last_message.tool_calls:
|
||||
return "end"
|
||||
# Otherwise if there is, we continue
|
||||
else:
|
||||
return "continue"
|
||||
|
||||
|
||||
# Define the function that calls the model
|
||||
def call_model(state, config):
|
||||
model = model_oai
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
|
||||
|
||||
# Define the function to execute tools
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
|
||||
class ContextSchema(TypedDict):
|
||||
model: Literal["anthropic", "openai"]
|
||||
|
||||
|
||||
# Define a new graph
|
||||
workflow = StateGraph(AgentState, context_schema=ContextSchema)
|
||||
|
||||
# Define the two nodes we will cycle between
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("action", tool_node)
|
||||
|
||||
# Set the entrypoint as `agent`
|
||||
# This means that this node is the first one called
|
||||
workflow.set_entry_point("agent")
|
||||
|
||||
# We now add a conditional edge
|
||||
workflow.add_conditional_edges(
|
||||
# First, we define the start node. We use `agent`.
|
||||
# This means these are the edges taken after the `agent` node is called.
|
||||
"agent",
|
||||
# Next, we pass in the function that will determine which node is called next.
|
||||
should_continue,
|
||||
# Finally we pass in a mapping.
|
||||
# The keys are strings, and the values are other nodes.
|
||||
# END is a special node marking that the graph should finish.
|
||||
# What will happen is we will call `should_continue`, and then the output of that
|
||||
# will be matched against the keys in this mapping.
|
||||
# Based on which one it matches, that node will then be called.
|
||||
{
|
||||
# If `tools`, then we call the tool node.
|
||||
"continue": "action",
|
||||
# Otherwise we finish.
|
||||
"end": END,
|
||||
},
|
||||
)
|
||||
|
||||
# We now add a normal edge from `tools` to `agent`.
|
||||
# This means that after `tools` is called, `agent` node is called next.
|
||||
workflow.add_edge("action", "agent")
|
||||
|
||||
# Finally, we compile it!
|
||||
# This compiles it into a LangChain Runnable,
|
||||
# meaning you can use it as you would any other runnable
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1,9 @@
|
||||
[project]
|
||||
name = "graph-prerelease-reqs-additional-deps"
|
||||
version = "0.1.0"
|
||||
description = "Test for prerelease stuff"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"langgraph==1.1.5"
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
[project]
|
||||
name = "graph-prerelease-reqs-zuper-deps"
|
||||
version = "0.1.0"
|
||||
description = "Test for prerelease stuff"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"langchain-openai==1.1.14"
|
||||
]
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"python_version": "3.12",
|
||||
"dependencies": [
|
||||
".",
|
||||
"./deps/additional_deps",
|
||||
"./deps/zuper_deps"
|
||||
],
|
||||
"graphs": {
|
||||
"agent": "./agent.py:graph"
|
||||
},
|
||||
"env": "../.env"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
[project]
|
||||
name = "graph-prerelease-reqs"
|
||||
version = "0.1.0"
|
||||
description = "Test for prerelease stuff"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"langchain-openai==1.1.14",
|
||||
"langchain-anthropic==1.4.6",
|
||||
"langgraph==1.1.5"
|
||||
]
|
||||
|
||||
[tool.uv]
|
||||
prerelease = "allow"
|
||||
@@ -0,0 +1,89 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.prebuilt import ToolNode
|
||||
|
||||
tools = [TavilySearchResults(max_results=1)]
|
||||
|
||||
model_oai = ChatOpenAI(temperature=0)
|
||||
|
||||
model_oai = model_oai.bind_tools(tools)
|
||||
|
||||
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
|
||||
|
||||
# Define the function that determines whether to continue or not
|
||||
def should_continue(state):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If there are no tool calls, then we finish
|
||||
if not last_message.tool_calls:
|
||||
return "end"
|
||||
# Otherwise if there is, we continue
|
||||
else:
|
||||
return "continue"
|
||||
|
||||
|
||||
# Define the function that calls the model
|
||||
def call_model(state, config):
|
||||
model = model_oai
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
|
||||
|
||||
# Define the function to execute tools
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
|
||||
class ContextSchema(TypedDict):
|
||||
model: Literal["anthropic", "openai"]
|
||||
|
||||
|
||||
# Define a new graph
|
||||
workflow = StateGraph(AgentState, context_schema=ContextSchema)
|
||||
|
||||
# Define the two nodes we will cycle between
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("action", tool_node)
|
||||
|
||||
# Set the entrypoint as `agent`
|
||||
# This means that this node is the first one called
|
||||
workflow.set_entry_point("agent")
|
||||
|
||||
# We now add a conditional edge
|
||||
workflow.add_conditional_edges(
|
||||
# First, we define the start node. We use `agent`.
|
||||
# This means these are the edges taken after the `agent` node is called.
|
||||
"agent",
|
||||
# Next, we pass in the function that will determine which node is called next.
|
||||
should_continue,
|
||||
# Finally we pass in a mapping.
|
||||
# The keys are strings, and the values are other nodes.
|
||||
# END is a special node marking that the graph should finish.
|
||||
# What will happen is we will call `should_continue`, and then the output of that
|
||||
# will be matched against the keys in this mapping.
|
||||
# Based on which one it matches, that node will then be called.
|
||||
{
|
||||
# If `tools`, then we call the tool node.
|
||||
"continue": "action",
|
||||
# Otherwise we finish.
|
||||
"end": END,
|
||||
},
|
||||
)
|
||||
|
||||
# We now add a normal edge from `tools` to `agent`.
|
||||
# This means that after `tools` is called, `agent` node is called next.
|
||||
workflow.add_edge("action", "agent")
|
||||
|
||||
# Finally, we compile it!
|
||||
# This compiles it into a LangChain Runnable,
|
||||
# meaning you can use it as you would any other runnable
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"python_version": "3.12",
|
||||
"dependencies": [
|
||||
"."
|
||||
],
|
||||
"graphs": {
|
||||
"agent": "./agent.py:graph"
|
||||
},
|
||||
"env": "../.env"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
[project]
|
||||
name = "graph-prerelease-reqs"
|
||||
version = "0.1.0"
|
||||
description = "Test for prerelease stuff"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"langchain-openai==1.1.14",
|
||||
"langgraph==1.1.2",
|
||||
"langchain_community>=0.3.0",
|
||||
]
|
||||
@@ -0,0 +1,96 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
|
||||
from langchain_anthropic import ChatAnthropic
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.prebuilt import ToolNode
|
||||
from langgraph.runtime import Runtime
|
||||
|
||||
tools = [TavilySearchResults(max_results=1)]
|
||||
|
||||
model_anth = ChatAnthropic(temperature=0, model_name="claude-3-sonnet-20240229")
|
||||
model_oai = ChatOpenAI(temperature=0)
|
||||
|
||||
model_anth = model_anth.bind_tools(tools)
|
||||
model_oai = model_oai.bind_tools(tools)
|
||||
|
||||
|
||||
class AgentContext(TypedDict):
|
||||
model: Literal["anthropic", "openai"]
|
||||
|
||||
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
|
||||
|
||||
# Define the function that determines whether to continue or not
|
||||
def should_continue(state):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If there are no tool calls, then we finish
|
||||
if not last_message.tool_calls:
|
||||
return "end"
|
||||
# Otherwise if there is, we continue
|
||||
else:
|
||||
return "continue"
|
||||
|
||||
|
||||
# Define the function that calls the model
|
||||
def call_model(state, runtime: Runtime[AgentContext]):
|
||||
if runtime.context.get("model", "anthropic") == "anthropic":
|
||||
model = model_anth
|
||||
else:
|
||||
model = model_oai
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
|
||||
|
||||
# Define the function to execute tools
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
|
||||
# Define a new graph
|
||||
workflow = StateGraph(AgentState, context_schema=AgentContext)
|
||||
|
||||
# Define the two nodes we will cycle between
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("action", tool_node)
|
||||
|
||||
# Set the entrypoint as `agent`
|
||||
# This means that this node is the first one called
|
||||
workflow.set_entry_point("agent")
|
||||
|
||||
# We now add a conditional edge
|
||||
workflow.add_conditional_edges(
|
||||
# First, we define the start node. We use `agent`.
|
||||
# This means these are the edges taken after the `agent` node is called.
|
||||
"agent",
|
||||
# Next, we pass in the function that will determine which node is called next.
|
||||
should_continue,
|
||||
# Finally we pass in a mapping.
|
||||
# The keys are strings, and the values are other nodes.
|
||||
# END is a special node marking that the graph should finish.
|
||||
# What will happen is we will call `should_continue`, and then the output of that
|
||||
# will be matched against the keys in this mapping.
|
||||
# Based on which one it matches, that node will then be called.
|
||||
{
|
||||
# If `tools`, then we call the tool node.
|
||||
"continue": "action",
|
||||
# Otherwise we finish.
|
||||
"end": END,
|
||||
},
|
||||
)
|
||||
|
||||
# We now add a normal edge from `tools` to `agent`.
|
||||
# This means that after `tools` is called, `agent` node is called next.
|
||||
workflow.add_edge("action", "agent")
|
||||
|
||||
# Finally, we compile it!
|
||||
# This compiles it into a LangChain Runnable,
|
||||
# meaning you can use it as you would any other runnable
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"$schema": "https://langgra.ph/schema.json",
|
||||
"python_version": "3.12",
|
||||
"dependencies": [
|
||||
"langchain_community",
|
||||
"langchain_anthropic",
|
||||
"langchain_openai",
|
||||
"wikipedia",
|
||||
"scikit-learn",
|
||||
"."
|
||||
],
|
||||
"graphs": {
|
||||
"agent": "./agent.py:graph",
|
||||
"storm": "./storm.py:graph"
|
||||
},
|
||||
"env": "../.env"
|
||||
}
|
||||
@@ -0,0 +1,636 @@
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Annotated
|
||||
|
||||
from langchain_community.retrievers import WikipediaRetriever
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_community.vectorstores import SKLearnVectorStore
|
||||
from langchain_core.documents import Document
|
||||
from langchain_core.messages import (
|
||||
AIMessage,
|
||||
AnyMessage,
|
||||
HumanMessage,
|
||||
ToolMessage,
|
||||
)
|
||||
from langchain_core.output_parsers import StrOutputParser
|
||||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
||||
from langchain_core.runnables import RunnableConfig, RunnableLambda
|
||||
from langchain_core.runnables import chain as as_runnable
|
||||
from langchain_core.tools import tool
|
||||
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
||||
from langgraph.graph import END, StateGraph
|
||||
from pydantic import BaseModel, Field
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
fast_llm = ChatOpenAI(model="gpt-4o-mini")
|
||||
# Uncomment for a Fireworks model
|
||||
# fast_llm = ChatFireworks(model="accounts/fireworks/models/firefunction-v1", max_tokens=32_000)
|
||||
long_context_llm = ChatOpenAI(model="gpt-4o")
|
||||
|
||||
|
||||
direct_gen_outline_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"You are a Wikipedia writer. Write an outline for a Wikipedia page about a user-provided topic. Be comprehensive and specific.",
|
||||
),
|
||||
("user", "{topic}"),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class Subsection(BaseModel):
|
||||
subsection_title: str = Field(..., title="Title of the subsection")
|
||||
description: str = Field(..., title="Content of the subsection")
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
return f"### {self.subsection_title}\n\n{self.description}".strip()
|
||||
|
||||
|
||||
class Section(BaseModel):
|
||||
section_title: str = Field(..., title="Title of the section")
|
||||
description: str = Field(..., title="Content of the section")
|
||||
subsections: list[Subsection] | None = Field(
|
||||
default=None,
|
||||
title="Titles and descriptions for each subsection of the Wikipedia page.",
|
||||
)
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
subsections = "\n\n".join(
|
||||
f"### {subsection.subsection_title}\n\n{subsection.description}"
|
||||
for subsection in self.subsections or []
|
||||
)
|
||||
return f"## {self.section_title}\n\n{self.description}\n\n{subsections}".strip()
|
||||
|
||||
|
||||
class Outline(BaseModel):
|
||||
page_title: str = Field(..., title="Title of the Wikipedia page")
|
||||
sections: list[Section] = Field(
|
||||
default_factory=list,
|
||||
title="Titles and descriptions for each section of the Wikipedia page.",
|
||||
)
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
sections = "\n\n".join(section.as_str for section in self.sections)
|
||||
return f"# {self.page_title}\n\n{sections}".strip()
|
||||
|
||||
|
||||
generate_outline_direct = direct_gen_outline_prompt | fast_llm.with_structured_output(
|
||||
Outline
|
||||
)
|
||||
|
||||
gen_related_topics_prompt = ChatPromptTemplate.from_template(
|
||||
"""I'm writing a Wikipedia page for a topic mentioned below. Please identify and recommend some Wikipedia pages on closely related subjects. I'm looking for examples that provide insights into interesting aspects commonly associated with this topic, or examples that help me understand the typical content and structure included in Wikipedia pages for similar topics.
|
||||
|
||||
Please list the as many subjects and urls as you can.
|
||||
|
||||
Topic of interest: {topic}
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
class RelatedSubjects(BaseModel):
|
||||
topics: list[str] = Field(
|
||||
description="Comprehensive list of related subjects as background research.",
|
||||
)
|
||||
|
||||
|
||||
expand_chain = gen_related_topics_prompt | fast_llm.with_structured_output(
|
||||
RelatedSubjects
|
||||
)
|
||||
|
||||
|
||||
class Editor(BaseModel):
|
||||
affiliation: str = Field(
|
||||
description="Primary affiliation of the editor.",
|
||||
)
|
||||
name: str = Field(
|
||||
description="Name of the editor.",
|
||||
)
|
||||
role: str = Field(
|
||||
description="Role of the editor in the context of the topic.",
|
||||
)
|
||||
description: str = Field(
|
||||
description="Description of the editor's focus, concerns, and motives.",
|
||||
)
|
||||
|
||||
@property
|
||||
def persona(self) -> str:
|
||||
return f"Name: {self.name}\nRole: {self.role}\nAffiliation: {self.affiliation}\nDescription: {self.description}\n"
|
||||
|
||||
|
||||
class Perspectives(BaseModel):
|
||||
editors: list[Editor] = Field(
|
||||
description="Comprehensive list of editors with their roles and affiliations.",
|
||||
# Add a pydantic validation/restriction to be at most M editors
|
||||
)
|
||||
|
||||
|
||||
gen_perspectives_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"""You need to select a diverse (and distinct) group of Wikipedia editors who will work together to create a comprehensive article on the topic. Each of them represents a different perspective, role, or affiliation related to this topic.\
|
||||
You can use other Wikipedia pages of related topics for inspiration. For each editor, add a description of what they will focus on.
|
||||
|
||||
Wiki page outlines of related topics for inspiration:
|
||||
{examples}""",
|
||||
),
|
||||
("user", "Topic of interest: {topic}"),
|
||||
]
|
||||
)
|
||||
|
||||
gen_perspectives_chain = gen_perspectives_prompt | ChatOpenAI(
|
||||
model="gpt-4o-mini"
|
||||
).with_structured_output(Perspectives)
|
||||
|
||||
|
||||
wikipedia_retriever = WikipediaRetriever(load_all_available_meta=True, top_k_results=1)
|
||||
|
||||
|
||||
def format_doc(doc, max_length=1000):
|
||||
related = "- ".join(doc.metadata["categories"])
|
||||
return f"### {doc.metadata['title']}\n\nSummary: {doc.page_content}\n\nRelated\n{related}"[
|
||||
:max_length
|
||||
]
|
||||
|
||||
|
||||
def format_docs(docs):
|
||||
return "\n\n".join(format_doc(doc) for doc in docs)
|
||||
|
||||
|
||||
@as_runnable
|
||||
async def survey_subjects(topic: str):
|
||||
related_subjects = await expand_chain.ainvoke({"topic": topic})
|
||||
retrieved_docs = await wikipedia_retriever.abatch(
|
||||
related_subjects.topics, return_exceptions=True
|
||||
)
|
||||
all_docs = []
|
||||
for docs in retrieved_docs:
|
||||
if isinstance(docs, BaseException):
|
||||
continue
|
||||
all_docs.extend(docs)
|
||||
formatted = format_docs(all_docs)
|
||||
return await gen_perspectives_chain.ainvoke({"examples": formatted, "topic": topic})
|
||||
|
||||
|
||||
def add_messages(left, right):
|
||||
if not isinstance(left, list):
|
||||
left = [left]
|
||||
if not isinstance(right, list):
|
||||
right = [right]
|
||||
return left + right
|
||||
|
||||
|
||||
def update_references(references, new_references):
|
||||
if not references:
|
||||
references = {}
|
||||
references.update(new_references)
|
||||
return references
|
||||
|
||||
|
||||
def update_editor(editor, new_editor):
|
||||
# Can only set at the outset
|
||||
if not editor:
|
||||
return new_editor
|
||||
return editor
|
||||
|
||||
|
||||
class InterviewState(TypedDict):
|
||||
messages: Annotated[list[AnyMessage], add_messages]
|
||||
references: Annotated[dict | None, update_references]
|
||||
editor: Annotated[Editor | None, update_editor]
|
||||
|
||||
|
||||
gen_qn_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"""You are an experienced Wikipedia writer and want to edit a specific page. \
|
||||
Besides your identity as a Wikipedia writer, you have a specific focus when researching the topic. \
|
||||
Now, you are chatting with an expert to get information. Ask good questions to get more useful information.
|
||||
|
||||
When you have no more questions to ask, say "Thank you so much for your help!" to end the conversation.\
|
||||
Please only ask one question at a time and don't ask what you have asked before.\
|
||||
Your questions should be related to the topic you want to write.
|
||||
Be comprehensive and curious, gaining as much unique insight from the expert as possible.\
|
||||
|
||||
Stay true to your specific perspective:
|
||||
|
||||
{persona}""",
|
||||
),
|
||||
MessagesPlaceholder(variable_name="messages", optional=True),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def tag_with_name(ai_message: AIMessage, name: str):
|
||||
ai_message.name = name.replace(" ", "_").replace(".", "_")
|
||||
return ai_message
|
||||
|
||||
|
||||
def swap_roles(state: InterviewState, name: str):
|
||||
converted = []
|
||||
for message in state["messages"]:
|
||||
if isinstance(message, AIMessage) and message.name != name:
|
||||
message = HumanMessage(**message.model_dump(exclude={"type"}))
|
||||
converted.append(message)
|
||||
return {"messages": converted}
|
||||
|
||||
|
||||
@as_runnable
|
||||
async def generate_question(state: InterviewState):
|
||||
editor = state["editor"]
|
||||
gn_chain = (
|
||||
RunnableLambda(swap_roles).bind(name=editor.name)
|
||||
| gen_qn_prompt.partial(persona=editor.persona)
|
||||
| fast_llm
|
||||
| RunnableLambda(tag_with_name).bind(name=editor.name)
|
||||
)
|
||||
result = await gn_chain.ainvoke(state)
|
||||
return {"messages": [result]}
|
||||
|
||||
|
||||
class Queries(BaseModel):
|
||||
queries: list[str] = Field(
|
||||
description="Comprehensive list of search engine queries to answer the user's questions.",
|
||||
)
|
||||
|
||||
|
||||
gen_queries_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"You are a helpful research assistant. Query the search engine to answer the user's questions.",
|
||||
),
|
||||
MessagesPlaceholder(variable_name="messages", optional=True),
|
||||
]
|
||||
)
|
||||
gen_queries_chain = gen_queries_prompt | ChatOpenAI(
|
||||
model="gpt-4o-mini"
|
||||
).with_structured_output(Queries, include_raw=True)
|
||||
|
||||
|
||||
class AnswerWithCitations(BaseModel):
|
||||
answer: str = Field(
|
||||
description="Comprehensive answer to the user's question with citations.",
|
||||
)
|
||||
cited_urls: list[str] = Field(
|
||||
description="List of urls cited in the answer.",
|
||||
)
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
return f"{self.answer}\n\nCitations:\n\n" + "\n".join(
|
||||
f"[{i + 1}]: {url}" for i, url in enumerate(self.cited_urls)
|
||||
)
|
||||
|
||||
|
||||
gen_answer_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"""You are an expert who can use information effectively. You are chatting with a Wikipedia writer who wants\
|
||||
to write a Wikipedia page on the topic you know. You have gathered the related information and will now use the information to form a response.
|
||||
|
||||
Make your response as informative as possible and make sure every sentence is supported by the gathered information.
|
||||
Each response must be backed up by a citation from a reliable source, formatted as a footnote, reproducing the URLS after your response.""",
|
||||
),
|
||||
MessagesPlaceholder(variable_name="messages", optional=True),
|
||||
]
|
||||
)
|
||||
|
||||
gen_answer_chain = gen_answer_prompt | fast_llm.with_structured_output(
|
||||
AnswerWithCitations, include_raw=True
|
||||
).with_config(run_name="GenerateAnswer")
|
||||
|
||||
|
||||
# Tavily is typically a better search engine, but your free queries are limited
|
||||
tavily_search = TavilySearchResults(max_results=4)
|
||||
|
||||
|
||||
@tool
|
||||
async def search_engine(query: str):
|
||||
"""Search engine to the internet."""
|
||||
results = tavily_search.invoke(query)
|
||||
return [{"content": r["content"], "url": r["url"]} for r in results]
|
||||
|
||||
|
||||
async def gen_answer(
|
||||
state: InterviewState,
|
||||
config: RunnableConfig | None = None,
|
||||
name: str = "Subject_Matter_Expert",
|
||||
max_str_len: int = 15000,
|
||||
):
|
||||
swapped_state = swap_roles(state, name) # Convert all other AI messages
|
||||
queries = await gen_queries_chain.ainvoke(swapped_state)
|
||||
query_results = await search_engine.abatch(
|
||||
queries["parsed"].queries, config, return_exceptions=True
|
||||
)
|
||||
successful_results = [
|
||||
res for res in query_results if not isinstance(res, Exception)
|
||||
]
|
||||
all_query_results = {
|
||||
res["url"]: res["content"] for results in successful_results for res in results
|
||||
}
|
||||
# We could be more precise about handling max token length if we wanted to here
|
||||
dumped = json.dumps(all_query_results)[:max_str_len]
|
||||
ai_message: AIMessage = queries["raw"]
|
||||
tool_call = queries["raw"].tool_calls[0]
|
||||
tool_id = tool_call["id"]
|
||||
tool_message = ToolMessage(tool_call_id=tool_id, content=dumped)
|
||||
swapped_state["messages"].extend([ai_message, tool_message])
|
||||
# Only update the shared state with the final answer to avoid
|
||||
# polluting the dialogue history with intermediate messages
|
||||
generated = await gen_answer_chain.ainvoke(swapped_state)
|
||||
cited_urls = set(generated["parsed"].cited_urls)
|
||||
# Save the retrieved information to a the shared state for future reference
|
||||
cited_references = {k: v for k, v in all_query_results.items() if k in cited_urls}
|
||||
formatted_message = AIMessage(name=name, content=generated["parsed"].as_str)
|
||||
return {"messages": [formatted_message], "references": cited_references}
|
||||
|
||||
|
||||
max_num_turns = 5
|
||||
|
||||
|
||||
def route_messages(state: InterviewState, name: str = "Subject_Matter_Expert"):
|
||||
messages = state["messages"]
|
||||
num_responses = len(
|
||||
[m for m in messages if isinstance(m, AIMessage) and m.name == name]
|
||||
)
|
||||
if num_responses >= max_num_turns:
|
||||
return END
|
||||
last_question = messages[-2]
|
||||
if last_question.content.endswith("Thank you so much for your help!"):
|
||||
return END
|
||||
return "ask_question"
|
||||
|
||||
|
||||
builder = StateGraph(InterviewState)
|
||||
|
||||
builder.add_node("ask_question", generate_question)
|
||||
builder.add_node("answer_question", gen_answer)
|
||||
builder.add_conditional_edges("answer_question", route_messages)
|
||||
builder.add_edge("ask_question", "answer_question")
|
||||
|
||||
builder.set_entry_point("ask_question")
|
||||
interview_graph = builder.compile().with_config(run_name="Conduct Interviews")
|
||||
|
||||
refine_outline_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"""You are a Wikipedia writer. You have gathered information from experts and search engines. Now, you are refining the outline of the Wikipedia page. \
|
||||
You need to make sure that the outline is comprehensive and specific. \
|
||||
Topic you are writing about: {topic}
|
||||
|
||||
Old outline:
|
||||
|
||||
{old_outline}""",
|
||||
),
|
||||
(
|
||||
"user",
|
||||
"Refine the outline based on your conversations with subject-matter experts:\n\nConversations:\n\n{conversations}\n\nWrite the refined Wikipedia outline:",
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
# Using turbo preview since the context can get quite long
|
||||
refine_outline_chain = refine_outline_prompt | long_context_llm.with_structured_output(
|
||||
Outline
|
||||
)
|
||||
|
||||
|
||||
embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
|
||||
# reference_docs = [
|
||||
# Document(page_content=v, metadata={"source": k})
|
||||
# for k, v in final_state["references"].items()
|
||||
# ]
|
||||
# # This really doesn't need to be a vectorstore for this size of data.
|
||||
# # It could just be a numpy matrix. Or you could store documents
|
||||
# # across requests if you want.
|
||||
# vectorstore = SKLearnVectorStore.from_documents(
|
||||
# reference_docs,
|
||||
# embedding=embeddings,
|
||||
# )
|
||||
# retriever = vectorstore.as_retriever(k=10)
|
||||
|
||||
vectorstore = SKLearnVectorStore(embedding=embeddings)
|
||||
retriever = vectorstore.as_retriever(k=10)
|
||||
|
||||
|
||||
class SubSection(BaseModel):
|
||||
subsection_title: str = Field(..., title="Title of the subsection")
|
||||
content: str = Field(
|
||||
...,
|
||||
title="Full content of the subsection. Include [#] citations to the cited sources where relevant.",
|
||||
)
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
return f"### {self.subsection_title}\n\n{self.content}".strip()
|
||||
|
||||
|
||||
class WikiSection(BaseModel):
|
||||
section_title: str = Field(..., title="Title of the section")
|
||||
content: str = Field(..., title="Full content of the section")
|
||||
subsections: list[Subsection] | None = Field(
|
||||
default=None,
|
||||
title="Titles and descriptions for each subsection of the Wikipedia page.",
|
||||
)
|
||||
citations: list[str] = Field(default_factory=list)
|
||||
|
||||
@property
|
||||
def as_str(self) -> str:
|
||||
subsections = "\n\n".join(
|
||||
subsection.as_str for subsection in self.subsections or []
|
||||
)
|
||||
citations = "\n".join([f" [{i}] {cit}" for i, cit in enumerate(self.citations)])
|
||||
return (
|
||||
f"## {self.section_title}\n\n{self.content}\n\n{subsections}".strip()
|
||||
+ f"\n\n{citations}".strip()
|
||||
)
|
||||
|
||||
|
||||
section_writer_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"You are an expert Wikipedia writer. Complete your assigned WikiSection from the following outline:\n\n"
|
||||
"{outline}\n\nCite your sources, using the following references:\n\n<Documents>\n{docs}\n<Documents>",
|
||||
),
|
||||
("user", "Write the full WikiSection for the {section} section."),
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def retrieve(inputs: dict):
|
||||
docs = await retriever.ainvoke(inputs["topic"] + ": " + inputs["section"])
|
||||
formatted = "\n".join(
|
||||
[
|
||||
f'<Document href="{doc.metadata["source"]}"/>\n{doc.page_content}\n</Document>'
|
||||
for doc in docs
|
||||
]
|
||||
)
|
||||
return {"docs": formatted, **inputs}
|
||||
|
||||
|
||||
section_writer = (
|
||||
retrieve
|
||||
| section_writer_prompt
|
||||
| long_context_llm.with_structured_output(WikiSection)
|
||||
)
|
||||
|
||||
|
||||
writer_prompt = ChatPromptTemplate.from_messages(
|
||||
[
|
||||
(
|
||||
"system",
|
||||
"You are an expert Wikipedia author. Write the complete wiki article on {topic} using the following section drafts:\n\n"
|
||||
"{draft}\n\nStrictly follow Wikipedia format guidelines.",
|
||||
),
|
||||
(
|
||||
"user",
|
||||
'Write the complete Wiki article using markdown format. Organize citations using footnotes like "[1]",'
|
||||
" avoiding duplicates in the footer. Include URLs in the footer.",
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
writer = writer_prompt | long_context_llm | StrOutputParser()
|
||||
|
||||
|
||||
class ResearchState(TypedDict):
|
||||
topic: str
|
||||
outline: Outline
|
||||
editors: list[Editor]
|
||||
interview_results: list[InterviewState]
|
||||
# The final sections output
|
||||
sections: list[WikiSection]
|
||||
article: str
|
||||
|
||||
|
||||
async def initialize_research(state: ResearchState):
|
||||
topic = state["topic"]
|
||||
coros = (
|
||||
generate_outline_direct.ainvoke({"topic": topic}),
|
||||
survey_subjects.ainvoke(topic),
|
||||
)
|
||||
results = await asyncio.gather(*coros)
|
||||
return {
|
||||
**state,
|
||||
"outline": results[0],
|
||||
"editors": results[1].editors,
|
||||
}
|
||||
|
||||
|
||||
async def conduct_interviews(state: ResearchState):
|
||||
topic = state["topic"]
|
||||
initial_states = [
|
||||
{
|
||||
"editor": editor,
|
||||
"messages": [
|
||||
AIMessage(
|
||||
content=f"So you said you were writing an article on {topic}?",
|
||||
name="Subject_Matter_Expert",
|
||||
)
|
||||
],
|
||||
}
|
||||
for editor in state["editors"]
|
||||
]
|
||||
# We call in to the sub-graph here to parallelize the interviews
|
||||
interview_results = await interview_graph.abatch(initial_states)
|
||||
|
||||
return {
|
||||
**state,
|
||||
"interview_results": interview_results,
|
||||
}
|
||||
|
||||
|
||||
def format_conversation(interview_state):
|
||||
messages = interview_state["messages"]
|
||||
convo = "\n".join(f"{m.name}: {m.content}" for m in messages)
|
||||
return f"Conversation with {interview_state['editor'].name}\n\n" + convo
|
||||
|
||||
|
||||
async def refine_outline(state: ResearchState):
|
||||
convos = "\n\n".join(
|
||||
[
|
||||
format_conversation(interview_state)
|
||||
for interview_state in state["interview_results"]
|
||||
]
|
||||
)
|
||||
|
||||
updated_outline = await refine_outline_chain.ainvoke(
|
||||
{
|
||||
"topic": state["topic"],
|
||||
"old_outline": state["outline"].as_str,
|
||||
"conversations": convos,
|
||||
}
|
||||
)
|
||||
return {**state, "outline": updated_outline}
|
||||
|
||||
|
||||
async def index_references(state: ResearchState):
|
||||
all_docs = []
|
||||
for interview_state in state["interview_results"]:
|
||||
reference_docs = [
|
||||
Document(page_content=v, metadata={"source": k})
|
||||
for k, v in interview_state["references"].items()
|
||||
]
|
||||
all_docs.extend(reference_docs)
|
||||
await vectorstore.aadd_documents(all_docs)
|
||||
return state
|
||||
|
||||
|
||||
async def write_sections(state: ResearchState):
|
||||
outline = state["outline"]
|
||||
sections = await section_writer.abatch(
|
||||
[
|
||||
{
|
||||
"outline": outline.as_str,
|
||||
"section": section.section_title,
|
||||
"topic": state["topic"],
|
||||
}
|
||||
for section in outline.sections
|
||||
]
|
||||
)
|
||||
return {
|
||||
**state,
|
||||
"sections": sections,
|
||||
}
|
||||
|
||||
|
||||
async def write_article(state: ResearchState):
|
||||
topic = state["topic"]
|
||||
sections = state["sections"]
|
||||
draft = "\n\n".join([section.as_str for section in sections])
|
||||
article = await writer.ainvoke({"topic": topic, "draft": draft})
|
||||
return {
|
||||
**state,
|
||||
"article": article,
|
||||
}
|
||||
|
||||
|
||||
builder_of_storm = StateGraph(ResearchState)
|
||||
|
||||
nodes = [
|
||||
("init_research", initialize_research),
|
||||
("conduct_interviews", conduct_interviews),
|
||||
("refine_outline", refine_outline),
|
||||
("index_references", index_references),
|
||||
("write_sections", write_sections),
|
||||
("write_article", write_article),
|
||||
]
|
||||
for i in range(len(nodes)):
|
||||
name, node = nodes[i]
|
||||
builder_of_storm.add_node(name, node)
|
||||
if i > 0:
|
||||
builder_of_storm.add_edge(nodes[i - 1][0], name)
|
||||
|
||||
builder_of_storm.set_entry_point(nodes[0][0])
|
||||
builder_of_storm.set_finish_point(nodes[-1][0])
|
||||
graph = builder_of_storm.compile()
|
||||
@@ -0,0 +1,99 @@
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
|
||||
from langchain_anthropic import ChatAnthropic
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.prebuilt import ToolNode
|
||||
from langgraph.runtime import Runtime
|
||||
|
||||
tools = [TavilySearchResults(max_results=1)]
|
||||
|
||||
model_anth = ChatAnthropic(temperature=0, model_name="claude-3-sonnet-20240229")
|
||||
model_oai = ChatOpenAI(temperature=0)
|
||||
|
||||
model_anth = model_anth.bind_tools(tools)
|
||||
model_oai = model_oai.bind_tools(tools)
|
||||
|
||||
prompt = open(Path(__file__).parent.parent / "prompt.txt").read()
|
||||
subprompt = open(Path(__file__).parent / "subprompt.txt").read()
|
||||
|
||||
|
||||
class AgentContext(TypedDict):
|
||||
model: Literal["anthropic", "openai"]
|
||||
|
||||
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
|
||||
|
||||
# Define the function that determines whether to continue or not
|
||||
def should_continue(state):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If there are no tool calls, then we finish
|
||||
if not last_message.tool_calls:
|
||||
return "end"
|
||||
# Otherwise if there is, we continue
|
||||
else:
|
||||
return "continue"
|
||||
|
||||
|
||||
# Define the function that calls the model
|
||||
def call_model(state, runtime: Runtime[AgentContext]):
|
||||
if runtime.context.get("model", "anthropic") == "anthropic":
|
||||
model = model_anth
|
||||
else:
|
||||
model = model_oai
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
|
||||
|
||||
# Define the function to execute tools
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
# Define a new graph
|
||||
workflow = StateGraph(AgentState, context_schema=AgentContext)
|
||||
|
||||
# Define the two nodes we will cycle between
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("action", tool_node)
|
||||
|
||||
# Set the entrypoint as `agent`
|
||||
# This means that this node is the first one called
|
||||
workflow.set_entry_point("agent")
|
||||
|
||||
# We now add a conditional edge
|
||||
workflow.add_conditional_edges(
|
||||
# First, we define the start node. We use `agent`.
|
||||
# This means these are the edges taken after the `agent` node is called.
|
||||
"agent",
|
||||
# Next, we pass in the function that will determine which node is called next.
|
||||
should_continue,
|
||||
# Finally we pass in a mapping.
|
||||
# The keys are strings, and the values are other nodes.
|
||||
# END is a special node marking that the graph should finish.
|
||||
# What will happen is we will call `should_continue`, and then the output of that
|
||||
# will be matched against the keys in this mapping.
|
||||
# Based on which one it matches, that node will then be called.
|
||||
{
|
||||
# If `tools`, then we call the tool node.
|
||||
"continue": "action",
|
||||
# Otherwise we finish.
|
||||
"end": END,
|
||||
},
|
||||
)
|
||||
|
||||
# We now add a normal edge from `tools` to `agent`.
|
||||
# This means that after `tools` is called, `agent` node is called next.
|
||||
workflow.add_edge("action", "agent")
|
||||
|
||||
# Finally, we compile it!
|
||||
# This compiles it into a LangChain Runnable,
|
||||
# meaning you can use it as you would any other runnable
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1 @@
|
||||
from graphs_reqs_a.graphs_submod.agent import graph # noqa
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "https://langgra.ph/schema.json",
|
||||
"dependencies": [
|
||||
"."
|
||||
],
|
||||
"env": "../.env",
|
||||
"graphs": {
|
||||
"graph": "./hello.py:graph"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
requests
|
||||
langchain_anthropic
|
||||
langchain_openai
|
||||
langchain_community
|
||||
@@ -0,0 +1,100 @@
|
||||
from collections.abc import Sequence
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Literal, TypedDict
|
||||
|
||||
from langchain_anthropic import ChatAnthropic
|
||||
from langchain_community.tools.tavily_search import TavilySearchResults
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langchain_openai import ChatOpenAI
|
||||
from langgraph.graph import END, StateGraph, add_messages
|
||||
from langgraph.prebuilt import ToolNode
|
||||
from langgraph.runtime import Runtime
|
||||
|
||||
tools = [TavilySearchResults(max_results=1)]
|
||||
|
||||
model_anth = ChatAnthropic(temperature=0, model_name="claude-3-sonnet-20240229")
|
||||
model_oai = ChatOpenAI(temperature=0)
|
||||
|
||||
model_anth = model_anth.bind_tools(tools)
|
||||
model_oai = model_oai.bind_tools(tools)
|
||||
|
||||
prompt = open(Path(__file__).parent.parent / "prompt.txt").read()
|
||||
subprompt = open(Path(__file__).parent / "subprompt.txt").read()
|
||||
|
||||
|
||||
class AgentContext(TypedDict):
|
||||
model: Literal["anthropic", "openai"]
|
||||
|
||||
|
||||
class AgentState(TypedDict):
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
|
||||
|
||||
# Define the function that determines whether to continue or not
|
||||
def should_continue(state):
|
||||
messages = state["messages"]
|
||||
last_message = messages[-1]
|
||||
# If there are no tool calls, then we finish
|
||||
if not last_message.tool_calls:
|
||||
return "end"
|
||||
# Otherwise if there is, we continue
|
||||
else:
|
||||
return "continue"
|
||||
|
||||
|
||||
# Define the function that calls the model
|
||||
def call_model(state, runtime: Runtime[AgentContext]):
|
||||
if runtime.context.get("model", "anthropic") == "anthropic":
|
||||
model = model_anth
|
||||
else:
|
||||
model = model_oai
|
||||
messages = state["messages"]
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
|
||||
|
||||
# Define the function to execute tools
|
||||
tool_node = ToolNode(tools)
|
||||
|
||||
|
||||
# Define a new graph
|
||||
workflow = StateGraph(AgentState, context_schema=AgentContext)
|
||||
|
||||
# Define the two nodes we will cycle between
|
||||
workflow.add_node("agent", call_model)
|
||||
workflow.add_node("action", tool_node)
|
||||
|
||||
# Set the entrypoint as `agent`
|
||||
# This means that this node is the first one called
|
||||
workflow.set_entry_point("agent")
|
||||
|
||||
# We now add a conditional edge
|
||||
workflow.add_conditional_edges(
|
||||
# First, we define the start node. We use `agent`.
|
||||
# This means these are the edges taken after the `agent` node is called.
|
||||
"agent",
|
||||
# Next, we pass in the function that will determine which node is called next.
|
||||
should_continue,
|
||||
# Finally we pass in a mapping.
|
||||
# The keys are strings, and the values are other nodes.
|
||||
# END is a special node marking that the graph should finish.
|
||||
# What will happen is we will call `should_continue`, and then the output of that
|
||||
# will be matched against the keys in this mapping.
|
||||
# Based on which one it matches, that node will then be called.
|
||||
{
|
||||
# If `tools`, then we call the tool node.
|
||||
"continue": "action",
|
||||
# Otherwise we finish.
|
||||
"end": END,
|
||||
},
|
||||
)
|
||||
|
||||
# We now add a normal edge from `tools` to `agent`.
|
||||
# This means that after `tools` is called, `agent` node is called next.
|
||||
workflow.add_edge("action", "agent")
|
||||
|
||||
# Finally, we compile it!
|
||||
# This compiles it into a LangChain Runnable,
|
||||
# meaning you can use it as you would any other runnable
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1,4 @@
|
||||
from graphs_submod.agent import graph # noqa
|
||||
from utils.greeter import greet
|
||||
|
||||
greet()
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "https://langgra.ph/schema.json",
|
||||
"dependencies": [
|
||||
"."
|
||||
],
|
||||
"env": "../.env",
|
||||
"graphs": {
|
||||
"graph": "./hello.py:graph"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
requests
|
||||
langchain_anthropic
|
||||
langchain_openai
|
||||
langchain_community
|
||||
@@ -0,0 +1,2 @@
|
||||
def greet():
|
||||
print("Hello, world!")
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"pip_config_file": "./pipconf.txt",
|
||||
"dependencies": [
|
||||
"langchain_community",
|
||||
"langchain_anthropic",
|
||||
"langchain_openai",
|
||||
"wikipedia",
|
||||
"scikit-learn",
|
||||
"./graphs"
|
||||
],
|
||||
"keep_pkg_tools": false,
|
||||
"graphs": {
|
||||
"agent": "./graphs/agent.py:graph",
|
||||
"storm": "./graphs/storm.py:graph"
|
||||
},
|
||||
"env": ".env"
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from contextvars import ContextVar
|
||||
from typing import Any
|
||||
|
||||
from starlette.applications import Starlette
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.responses import JSONResponse
|
||||
from starlette.routing import Route
|
||||
|
||||
my_context_var: ContextVar[str] = ContextVar("my_context_var", default="")
|
||||
LIFESPAN_VAL = ""
|
||||
other_context_var = ContextVar("other_context_var", default="")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def my_lifespan(app):
|
||||
global LIFESPAN_VAL
|
||||
LIFESPAN_VAL = "foobar-lifespan"
|
||||
yield
|
||||
assert LIFESPAN_VAL == "foobar-lifespan"
|
||||
LIFESPAN_VAL = ""
|
||||
|
||||
|
||||
class MyContextMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Any, call_next: Any) -> Any:
|
||||
token = my_context_var.set("Foobar")
|
||||
try:
|
||||
response = await call_next(request)
|
||||
return response
|
||||
finally:
|
||||
my_context_var.reset(token)
|
||||
|
||||
|
||||
async def custom_my_route(request):
|
||||
"""A great route."""
|
||||
assert my_context_var.get() == "Foobar"
|
||||
assert LIFESPAN_VAL == "foobar-lifespan"
|
||||
return JSONResponse({"foo": "bar"})
|
||||
|
||||
|
||||
async def runs_afakeroute(request):
|
||||
"""Another great route."""
|
||||
assert my_context_var.get() == "Foobar"
|
||||
assert LIFESPAN_VAL == "foobar-lifespan"
|
||||
return JSONResponse({"foo": "afakeroute"})
|
||||
|
||||
|
||||
async def other_middleware(request: Any, call_next: Any) -> Any:
|
||||
other_context_var.set("foobar")
|
||||
response = await call_next(request)
|
||||
other_context_var.reset()
|
||||
return response
|
||||
|
||||
|
||||
app = Starlette(
|
||||
middleware=[(MyContextMiddleware, {}, {})],
|
||||
routes=[
|
||||
Route("/custom/my-route", custom_my_route),
|
||||
Route("/runs/afakeroute", runs_afakeroute),
|
||||
],
|
||||
lifespan=my_lifespan,
|
||||
)
|
||||
@@ -0,0 +1,2 @@
|
||||
[global]
|
||||
timeout = 60
|
||||
Generated
+285
@@ -0,0 +1,285 @@
|
||||
# This file is automatically @generated by Poetry 2.0.0 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "anyio"
|
||||
version = "4.4.0"
|
||||
description = "High level compatibility layer for multiple asynchronous event loop implementations"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "anyio-4.4.0-py3-none-any.whl", hash = "sha256:c1b2d8f46a8a812513012e1107cb0e68c17159a7a594208005a57dc776e1bdc7"},
|
||||
{file = "anyio-4.4.0.tar.gz", hash = "sha256:5aadc6a1bbb7cdb0bede386cac5e2940f5e2ff3aa20277e991cf028e0585ce94"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
exceptiongroup = {version = ">=1.0.2", markers = "python_version < \"3.11\""}
|
||||
idna = ">=2.8"
|
||||
sniffio = ">=1.1"
|
||||
typing-extensions = {version = ">=4.1", markers = "python_version < \"3.11\""}
|
||||
|
||||
[package.extras]
|
||||
doc = ["Sphinx (>=7)", "packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
|
||||
test = ["anyio[trio]", "coverage[toml] (>=7)", "exceptiongroup (>=1.2.0)", "hypothesis (>=4.0)", "psutil (>=5.9)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (>=0.17)"]
|
||||
trio = ["trio (>=0.23)"]
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
version = "2024.7.4"
|
||||
description = "Python package for providing Mozilla's CA Bundle."
|
||||
optional = false
|
||||
python-versions = ">=3.6"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "certifi-2024.7.4-py3-none-any.whl", hash = "sha256:c198e21b1289c2ab85ee4e67bb4b4ef3ead0892059901a8d5b622f24a1101e90"},
|
||||
{file = "certifi-2024.7.4.tar.gz", hash = "sha256:5a1e7645bc0ec61a09e26c36f6106dd4cf40c6db3a1fb6352b0244e7fb057c7b"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.1.7"
|
||||
description = "Composable command line interface toolkit"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
|
||||
{file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
description = "Cross-platform colored terminal text."
|
||||
optional = false
|
||||
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
|
||||
groups = ["main"]
|
||||
markers = "platform_system == \"Windows\""
|
||||
files = [
|
||||
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
|
||||
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "exceptiongroup"
|
||||
version = "1.2.1"
|
||||
description = "Backport of PEP 654 (exception groups)"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
groups = ["main"]
|
||||
markers = "python_version < \"3.11\""
|
||||
files = [
|
||||
{file = "exceptiongroup-1.2.1-py3-none-any.whl", hash = "sha256:5258b9ed329c5bbdd31a309f53cbfb0b155341807f6ff7606a1e801a891b29ad"},
|
||||
{file = "exceptiongroup-1.2.1.tar.gz", hash = "sha256:a4785e48b045528f5bfe627b6ad554ff32def154f42372786903b7abcfe1aa16"},
|
||||
]
|
||||
|
||||
[package.extras]
|
||||
test = ["pytest (>=6)"]
|
||||
|
||||
[[package]]
|
||||
name = "h11"
|
||||
version = "0.16.0"
|
||||
description = "A pure-Python, bring-your-own-I/O implementation of HTTP/1.1"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86"},
|
||||
{file = "h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httpcore"
|
||||
version = "1.0.9"
|
||||
description = "A minimal low-level HTTP client."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55"},
|
||||
{file = "httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
certifi = "*"
|
||||
h11 = ">=0.16"
|
||||
|
||||
[package.extras]
|
||||
asyncio = ["anyio (>=4.0,<5.0)"]
|
||||
http2 = ["h2 (>=3,<5)"]
|
||||
socks = ["socksio (==1.*)"]
|
||||
trio = ["trio (>=0.22.0,<1.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "httpx"
|
||||
version = "0.28.1"
|
||||
description = "The next generation HTTP client."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad"},
|
||||
{file = "httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
anyio = "*"
|
||||
certifi = "*"
|
||||
httpcore = "==1.*"
|
||||
idna = "*"
|
||||
|
||||
[package.extras]
|
||||
brotli = ["brotli", "brotlicffi"]
|
||||
cli = ["click (==8.*)", "pygments (==2.*)", "rich (>=10,<14)"]
|
||||
http2 = ["h2 (>=3,<5)"]
|
||||
socks = ["socksio (==1.*)"]
|
||||
zstd = ["zstandard (>=0.18.0)"]
|
||||
|
||||
[[package]]
|
||||
name = "httpx-sse"
|
||||
version = "0.4.0"
|
||||
description = "Consume Server-Sent Event (SSE) messages with HTTPX."
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "httpx-sse-0.4.0.tar.gz", hash = "sha256:1e81a3a3070ce322add1d3529ed42eb5f70817f45ed6ec915ab753f961139721"},
|
||||
{file = "httpx_sse-0.4.0-py3-none-any.whl", hash = "sha256:f329af6eae57eaa2bdfd962b42524764af68075ea87370a2de920af5341e318f"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.7"
|
||||
description = "Internationalized Domain Names in Applications (IDNA)"
|
||||
optional = false
|
||||
python-versions = ">=3.5"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "idna-3.7-py3-none-any.whl", hash = "sha256:82fee1fc78add43492d3a1898bfa6d8a904cc97d8427f683ed8e798d07761aa0"},
|
||||
{file = "idna-3.7.tar.gz", hash = "sha256:028ff3aadf0609c1fd278d8ea3089299412a7a8b9bd005dd08b9f8285bcb5cfc"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-cli"
|
||||
version = "0.1.52"
|
||||
description = "CLI for interacting with LangGraph API"
|
||||
optional = false
|
||||
python-versions = "^3.9.0,<4.0"
|
||||
groups = ["main"]
|
||||
files = []
|
||||
develop = true
|
||||
|
||||
[package.dependencies]
|
||||
click = "^8.1.7"
|
||||
|
||||
[package.source]
|
||||
type = "directory"
|
||||
url = ".."
|
||||
|
||||
[[package]]
|
||||
name = "langgraph-sdk"
|
||||
version = "0.1.29"
|
||||
description = "SDK for interacting with LangGraph API"
|
||||
optional = false
|
||||
python-versions = "^3.9.0,<4.0"
|
||||
groups = ["main"]
|
||||
files = []
|
||||
develop = true
|
||||
|
||||
[package.dependencies]
|
||||
httpx = ">=0.25.2"
|
||||
httpx-sse = ">=0.4.0"
|
||||
orjson = ">=3.10.1"
|
||||
|
||||
[package.source]
|
||||
type = "directory"
|
||||
url = "../../sdk-py"
|
||||
|
||||
[[package]]
|
||||
name = "orjson"
|
||||
version = "3.10.5"
|
||||
description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "orjson-3.10.5-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:545d493c1f560d5ccfc134803ceb8955a14c3fcb47bbb4b2fee0232646d0b932"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f4324929c2dd917598212bfd554757feca3e5e0fa60da08be11b4aa8b90013c1"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8c13ca5e2ddded0ce6a927ea5a9f27cae77eee4c75547b4297252cb20c4d30e6"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b6c8e30adfa52c025f042a87f450a6b9ea29649d828e0fec4858ed5e6caecf63"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:338fd4f071b242f26e9ca802f443edc588fa4ab60bfa81f38beaedf42eda226c"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:6970ed7a3126cfed873c5d21ece1cd5d6f83ca6c9afb71bbae21a0b034588d96"},
|
||||
{file = "orjson-3.10.5-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:235dadefb793ad12f7fa11e98a480db1f7c6469ff9e3da5e73c7809c700d746b"},
|
||||
{file = "orjson-3.10.5-cp310-none-win32.whl", hash = "sha256:be79e2393679eda6a590638abda16d167754393f5d0850dcbca2d0c3735cebe2"},
|
||||
{file = "orjson-3.10.5-cp310-none-win_amd64.whl", hash = "sha256:c4a65310ccb5c9910c47b078ba78e2787cb3878cdded1702ac3d0da71ddc5228"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:cdf7365063e80899ae3a697def1277c17a7df7ccfc979990a403dfe77bb54d40"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6b68742c469745d0e6ca5724506858f75e2f1e5b59a4315861f9e2b1df77775a"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:7d10cc1b594951522e35a3463da19e899abe6ca95f3c84c69e9e901e0bd93d38"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:dcbe82b35d1ac43b0d84072408330fd3295c2896973112d495e7234f7e3da2e1"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:10c0eb7e0c75e1e486c7563fe231b40fdd658a035ae125c6ba651ca3b07936f5"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:53ed1c879b10de56f35daf06dbc4a0d9a5db98f6ee853c2dbd3ee9d13e6f302f"},
|
||||
{file = "orjson-3.10.5-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:099e81a5975237fda3100f918839af95f42f981447ba8f47adb7b6a3cdb078fa"},
|
||||
{file = "orjson-3.10.5-cp311-none-win32.whl", hash = "sha256:1146bf85ea37ac421594107195db8bc77104f74bc83e8ee21a2e58596bfb2f04"},
|
||||
{file = "orjson-3.10.5-cp311-none-win_amd64.whl", hash = "sha256:36a10f43c5f3a55c2f680efe07aa93ef4a342d2960dd2b1b7ea2dd764fe4a37c"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:68f85ecae7af14a585a563ac741b0547a3f291de81cd1e20903e79f25170458f"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:28afa96f496474ce60d3340fe8d9a263aa93ea01201cd2bad844c45cd21f5268"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9cd684927af3e11b6e754df80b9ffafd9fb6adcaa9d3e8fdd5891be5a5cad51e"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:3d21b9983da032505f7050795e98b5d9eee0df903258951566ecc358f6696969"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1ad1de7fef79736dde8c3554e75361ec351158a906d747bd901a52a5c9c8d24b"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:2d97531cdfe9bdd76d492e69800afd97e5930cb0da6a825646667b2c6c6c0211"},
|
||||
{file = "orjson-3.10.5-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:d69858c32f09c3e1ce44b617b3ebba1aba030e777000ebdf72b0d8e365d0b2b3"},
|
||||
{file = "orjson-3.10.5-cp312-none-win32.whl", hash = "sha256:64c9cc089f127e5875901ac05e5c25aa13cfa5dbbbd9602bda51e5c611d6e3e2"},
|
||||
{file = "orjson-3.10.5-cp312-none-win_amd64.whl", hash = "sha256:b2efbd67feff8c1f7728937c0d7f6ca8c25ec81373dc8db4ef394c1d93d13dc5"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:03b565c3b93f5d6e001db48b747d31ea3819b89abf041ee10ac6988886d18e01"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:584c902ec19ab7928fd5add1783c909094cc53f31ac7acfada817b0847975f26"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5a35455cc0b0b3a1eaf67224035f5388591ec72b9b6136d66b49a553ce9eb1e6"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:1670fe88b116c2745a3a30b0f099b699a02bb3482c2591514baf5433819e4f4d"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:185c394ef45b18b9a7d8e8f333606e2e8194a50c6e3c664215aae8cf42c5385e"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:ca0b3a94ac8d3886c9581b9f9de3ce858263865fdaa383fbc31c310b9eac07c9"},
|
||||
{file = "orjson-3.10.5-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:dfc91d4720d48e2a709e9c368d5125b4b5899dced34b5400c3837dadc7d6271b"},
|
||||
{file = "orjson-3.10.5-cp38-none-win32.whl", hash = "sha256:c05f16701ab2a4ca146d0bca950af254cb7c02f3c01fca8efbbad82d23b3d9d4"},
|
||||
{file = "orjson-3.10.5-cp38-none-win_amd64.whl", hash = "sha256:8a11d459338f96a9aa7f232ba95679fc0c7cedbd1b990d736467894210205c09"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:85c89131d7b3218db1b24c4abecea92fd6c7f9fab87441cfc342d3acc725d807"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:fb66215277a230c456f9038d5e2d84778141643207f85336ef8d2a9da26bd7ca"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:51bbcdea96cdefa4a9b4461e690c75ad4e33796530d182bdd5c38980202c134a"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:dbead71dbe65f959b7bd8cf91e0e11d5338033eba34c114f69078d59827ee139"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5df58d206e78c40da118a8c14fc189207fffdcb1f21b3b4c9c0c18e839b5a214"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:c4057c3b511bb8aef605616bd3f1f002a697c7e4da6adf095ca5b84c0fd43595"},
|
||||
{file = "orjson-3.10.5-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:b39e006b00c57125ab974362e740c14a0c6a66ff695bff44615dcf4a70ce2b86"},
|
||||
{file = "orjson-3.10.5-cp39-none-win32.whl", hash = "sha256:eded5138cc565a9d618e111c6d5c2547bbdd951114eb822f7f6309e04db0fb47"},
|
||||
{file = "orjson-3.10.5-cp39-none-win_amd64.whl", hash = "sha256:cc28e90a7cae7fcba2493953cff61da5a52950e78dc2dacfe931a317ee3d8de7"},
|
||||
{file = "orjson-3.10.5.tar.gz", hash = "sha256:7a5baef8a4284405d96c90c7c62b755e9ef1ada84c2406c24a9ebec86b89f46d"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sniffio"
|
||||
version = "1.3.1"
|
||||
description = "Sniff out which async library your code is running under"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2"},
|
||||
{file = "sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.12.2"
|
||||
description = "Backported and Experimental Type Hints for Python 3.8+"
|
||||
optional = false
|
||||
python-versions = ">=3.8"
|
||||
groups = ["main"]
|
||||
markers = "python_version < \"3.11\""
|
||||
files = [
|
||||
{file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"},
|
||||
{file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"},
|
||||
]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.1"
|
||||
python-versions = "^3.9.0,<4.0"
|
||||
content-hash = "ec5109729f30d2033a10a10e8f8d3ed94c7d96d5d31025b4815b0123664bb063"
|
||||
@@ -0,0 +1,21 @@
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-examples"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"langgraph-cli",
|
||||
"langgraph-sdk",
|
||||
]
|
||||
|
||||
[tool.uv.sources]
|
||||
langgraph-cli = { path = "../cli", editable = true }
|
||||
langgraph-sdk = { path = "../sdk_py", editable = true }
|
||||
|
||||
[tool.hatch.build]
|
||||
packages = []
|
||||
@@ -0,0 +1,296 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Script to generate a JSON schema for the langgraph-cli Config class.
|
||||
|
||||
This script creates a schema.json file that can be referenced in langgraph.json files
|
||||
to provide IDE autocompletion and validation.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
import textwrap
|
||||
from pathlib import Path
|
||||
|
||||
import msgspec
|
||||
|
||||
from langgraph_cli.schemas import (
|
||||
AuthConfig,
|
||||
CacheConfig,
|
||||
CheckpointerConfig,
|
||||
Config,
|
||||
ConfigurableHeaderConfig,
|
||||
CorsConfig,
|
||||
GraphDef,
|
||||
HttpConfig,
|
||||
IndexConfig,
|
||||
SecurityConfig,
|
||||
SerdeConfig,
|
||||
StoreConfig,
|
||||
ThreadTTLConfig,
|
||||
TTLConfig,
|
||||
WebhooksConfig,
|
||||
WebhookUrlPolicy,
|
||||
)
|
||||
|
||||
|
||||
def add_descriptions_to_schema(schema, cls):
|
||||
"""Add docstring descriptions to the schema properties."""
|
||||
if schema.get("description"):
|
||||
schema["description"] = inspect.cleandoc(schema["description"])
|
||||
elif class_doc := inspect.getdoc(cls):
|
||||
schema["description"] = inspect.cleandoc(class_doc)
|
||||
# Get attribute docstrings from the class
|
||||
attr_docs = {}
|
||||
|
||||
# Also check class annotations for docstrings
|
||||
source_lines = inspect.getsourcelines(cls)[0]
|
||||
current_attr = None
|
||||
docstring_lines = []
|
||||
|
||||
for line in source_lines:
|
||||
line = line.strip()
|
||||
|
||||
# Check for attribute definition (TypedDict style)
|
||||
if ":" in line and not line.startswith("#") and not line.startswith('"""'):
|
||||
parts = line.split(":", 1)
|
||||
if len(parts) == 2 and parts[0].strip().isidentifier():
|
||||
# If we were collecting a docstring, save it for the previous attribute
|
||||
if current_attr and docstring_lines:
|
||||
attr_docs[current_attr] = "\n".join(docstring_lines).strip('"')
|
||||
docstring_lines = []
|
||||
|
||||
current_attr = parts[0].strip()
|
||||
|
||||
# Check for docstring after attribute
|
||||
elif line.startswith('"""') and current_attr:
|
||||
# Start or end of a docstring
|
||||
if len(line) > 3 and line.endswith('"""'):
|
||||
# Single line docstring
|
||||
attr_docs[current_attr] = line.strip('"')
|
||||
current_attr = None
|
||||
elif docstring_lines:
|
||||
# End of multi-line docstring
|
||||
docstring_lines.append(line.rstrip('"'))
|
||||
attr_docs[current_attr] = "\n".join(docstring_lines).strip('"')
|
||||
docstring_lines = []
|
||||
current_attr = None
|
||||
else:
|
||||
# Start of multi-line docstring
|
||||
docstring_lines.append(line.lstrip('"'))
|
||||
|
||||
# Continue multi-line docstring
|
||||
elif docstring_lines and current_attr:
|
||||
docstring_lines.append(line.strip('"'))
|
||||
|
||||
# Add the last docstring if there is one
|
||||
if current_attr and docstring_lines:
|
||||
attr_docs[current_attr] = "\n".join(docstring_lines).strip('"')
|
||||
|
||||
# Add descriptions to properties
|
||||
if "properties" in schema:
|
||||
for prop_name, prop_schema in schema["properties"].items():
|
||||
# First try to get from attribute docstrings
|
||||
if prop_name in attr_docs and "description" not in prop_schema:
|
||||
prop_schema["description"] = textwrap.dedent(attr_docs[prop_name])
|
||||
# Fall back to class docstring parsing
|
||||
elif class_doc:
|
||||
for line in class_doc.split("\n"):
|
||||
if line.strip().startswith(
|
||||
f"{prop_name}:"
|
||||
) or line.strip().startswith(f'"{prop_name}"'):
|
||||
description = line.split(":", 1)[1].strip()
|
||||
if description and "description" not in prop_schema:
|
||||
prop_schema["description"] = description
|
||||
break
|
||||
|
||||
# Recursively process nested definitions
|
||||
if "$defs" in schema:
|
||||
for def_name, def_schema in schema["$defs"].items():
|
||||
# Find the class that corresponds to this definition
|
||||
for potential_cls in [
|
||||
Config,
|
||||
GraphDef,
|
||||
StoreConfig,
|
||||
IndexConfig,
|
||||
AuthConfig,
|
||||
SecurityConfig,
|
||||
HttpConfig,
|
||||
CorsConfig,
|
||||
CacheConfig,
|
||||
ThreadTTLConfig,
|
||||
CheckpointerConfig,
|
||||
SerdeConfig,
|
||||
TTLConfig,
|
||||
ConfigurableHeaderConfig,
|
||||
WebhooksConfig,
|
||||
WebhookUrlPolicy,
|
||||
]:
|
||||
if potential_cls.__name__ == def_name:
|
||||
add_descriptions_to_schema(def_schema, potential_cls)
|
||||
break
|
||||
|
||||
return schema
|
||||
|
||||
|
||||
def generate_schema():
|
||||
"""Generate a JSON schema for the Config class using msgspec."""
|
||||
# Generate the basic schema
|
||||
schema = msgspec.json.schema(Config)
|
||||
|
||||
# Add title and description
|
||||
schema["title"] = "LangGraph CLI Configuration"
|
||||
schema["description"] = "Configuration schema for langgraph-cli"
|
||||
|
||||
# Add docstring descriptions
|
||||
schema = add_descriptions_to_schema(schema, Config)
|
||||
|
||||
# Add constraint that only one of python_version or node_version should be specified
|
||||
config_schema = schema["$defs"]["Config"]
|
||||
|
||||
# Create two subschemas: one with python_version and one with node_version
|
||||
# Define properties specific to Python projects
|
||||
python_specific_props = ["python_version", "pip_config_file"]
|
||||
# Define properties specific to Node.js projects
|
||||
node_specific_props = ["node_version"]
|
||||
# Define properties common to both project types
|
||||
common_props = [
|
||||
k
|
||||
for k in config_schema["properties"]
|
||||
if k not in python_specific_props and k not in node_specific_props
|
||||
]
|
||||
|
||||
# Create legacy Python schema with python_version and pip_config_file
|
||||
legacy_python_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
# Include Python-specific properties
|
||||
**{k: config_schema["properties"][k].copy() for k in python_specific_props},
|
||||
# Include common properties
|
||||
**{k: config_schema["properties"][k].copy() for k in common_props},
|
||||
},
|
||||
"required": ["dependencies", "graphs"],
|
||||
}
|
||||
legacy_python_schema["properties"]["pip_installer"] = {
|
||||
"anyOf": [
|
||||
{"type": "string", "enum": ["auto", "pip", "uv"]},
|
||||
{"type": "null"},
|
||||
]
|
||||
}
|
||||
|
||||
uv_source_python_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
**{
|
||||
k: config_schema["properties"][k].copy()
|
||||
for k in python_specific_props + common_props
|
||||
},
|
||||
},
|
||||
"required": ["graphs", "source"],
|
||||
}
|
||||
# source must be a UvSource object (not null)
|
||||
uv_source_python_schema["properties"]["source"] = {"$ref": "#/$defs/UvSource"}
|
||||
uv_source_python_schema["properties"]["pip_installer"] = {
|
||||
"anyOf": [
|
||||
{"type": "string", "enum": ["auto", "pip", "uv"]},
|
||||
{"type": "null"},
|
||||
]
|
||||
}
|
||||
|
||||
# Add enum constraint for python_version
|
||||
if "python_version" in legacy_python_schema["properties"]:
|
||||
legacy_python_schema["properties"]["python_version"]["enum"] = [
|
||||
"3.11",
|
||||
"3.12",
|
||||
"3.13",
|
||||
]
|
||||
if "python_version" in uv_source_python_schema["properties"]:
|
||||
uv_source_python_schema["properties"]["python_version"]["enum"] = [
|
||||
"3.11",
|
||||
"3.12",
|
||||
"3.13",
|
||||
]
|
||||
|
||||
# Create Node.js schema with node_version
|
||||
node_schema = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
# Include Node-specific properties
|
||||
**{k: config_schema["properties"][k].copy() for k in node_specific_props},
|
||||
# Include common properties
|
||||
**{k: config_schema["properties"][k].copy() for k in common_props},
|
||||
},
|
||||
"required": ["node_version", "graphs"],
|
||||
}
|
||||
node_schema["properties"]["pip_installer"] = {
|
||||
"anyOf": [
|
||||
{"type": "string", "enum": ["auto", "pip", "uv"]},
|
||||
{"type": "null"},
|
||||
]
|
||||
}
|
||||
|
||||
# Add enum constraint for node_version
|
||||
if "node_version" in node_schema["properties"]:
|
||||
node_schema["properties"]["node_version"]["anyOf"] = [
|
||||
{"type": "string", "enum": ["20"]},
|
||||
{"type": "null"},
|
||||
]
|
||||
|
||||
# Add enum constraint for image_distro
|
||||
if "image_distro" in node_schema["properties"]:
|
||||
node_schema["properties"]["image_distro"]["anyOf"] = [
|
||||
{"type": "string", "enum": ["debian", "wolfi"]},
|
||||
{"type": "null"},
|
||||
]
|
||||
|
||||
# Replace the Config schema with a oneOf constraint
|
||||
config_schema["oneOf"] = [
|
||||
legacy_python_schema,
|
||||
uv_source_python_schema,
|
||||
node_schema,
|
||||
]
|
||||
|
||||
# Remove the properties field as it's now defined in the oneOf subschemas
|
||||
if "properties" in config_schema:
|
||||
del config_schema["properties"]
|
||||
|
||||
return schema
|
||||
|
||||
|
||||
def main():
|
||||
"""Generate the schema and write it to a file."""
|
||||
schema = generate_schema()
|
||||
|
||||
# Add versioning to the schema
|
||||
import importlib.metadata
|
||||
|
||||
try:
|
||||
version = importlib.metadata.version("langgraph_cli").split(".")
|
||||
schema_version = f"v{version[0]}"
|
||||
except importlib.metadata.PackageNotFoundError:
|
||||
schema_version = "v1"
|
||||
|
||||
# Add version to schema
|
||||
schema["version"] = schema_version
|
||||
|
||||
config_dir = Path(__file__).parent / "schemas"
|
||||
|
||||
# Create versioned schema file
|
||||
versioned_path = config_dir / f"schema.{schema_version}.json"
|
||||
with open(versioned_path, "w") as f:
|
||||
json.dump(schema, f, indent=2)
|
||||
|
||||
# Also create a latest version
|
||||
latest_path = config_dir / "schema.json"
|
||||
with open(latest_path, "w") as f:
|
||||
json.dump(schema, f, indent=2)
|
||||
|
||||
print(f"Schema written to {versioned_path} and {latest_path}")
|
||||
print(
|
||||
f"You can now add '$schema: https://raw.githubusercontent.com/langchain-ai/langgraph/refs/heads/main/libs/cli/schemas/schema.json'"
|
||||
f" or '$schema: https://raw.githubusercontent.com/langchain-ai/langgraph/refs/heads/main/libs/cli/schemas/schema.{schema_version}.json'"
|
||||
" to your langgraph.json files"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
||||
@@ -0,0 +1,3 @@
|
||||
# Copy this over:
|
||||
# cp .env.example .env
|
||||
# Then modify to suit your needs
|
||||
@@ -0,0 +1,62 @@
|
||||
module.exports = {
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
"prettier",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 12,
|
||||
parser: "@typescript-eslint/parser",
|
||||
project: "./tsconfig.json",
|
||||
sourceType: "module",
|
||||
},
|
||||
plugins: ["import", "@typescript-eslint", "no-instanceof"],
|
||||
ignorePatterns: [
|
||||
".eslintrc.cjs",
|
||||
"scripts",
|
||||
"src/utils/lodash/*",
|
||||
"node_modules",
|
||||
"dist",
|
||||
"dist-cjs",
|
||||
"*.js",
|
||||
"*.cjs",
|
||||
"*.d.ts",
|
||||
],
|
||||
rules: {
|
||||
"no-process-env": 2,
|
||||
"no-instanceof/no-instanceof": 2,
|
||||
"@typescript-eslint/explicit-module-boundary-types": 0,
|
||||
"@typescript-eslint/no-empty-function": 0,
|
||||
"@typescript-eslint/no-shadow": 0,
|
||||
"@typescript-eslint/no-empty-interface": 0,
|
||||
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
|
||||
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-misused-promises": "error",
|
||||
camelcase: 0,
|
||||
"class-methods-use-this": 0,
|
||||
"import/extensions": [2, "ignorePackages"],
|
||||
"import/no-extraneous-dependencies": [
|
||||
"error",
|
||||
{ devDependencies: ["**/*.test.ts"] },
|
||||
],
|
||||
"import/no-unresolved": 0,
|
||||
"import/prefer-default-export": 0,
|
||||
"keyword-spacing": "error",
|
||||
"max-classes-per-file": 0,
|
||||
"max-len": 0,
|
||||
"no-await-in-loop": 0,
|
||||
"no-bitwise": 0,
|
||||
"no-console": 0,
|
||||
"no-restricted-syntax": 0,
|
||||
"no-shadow": 0,
|
||||
"no-continue": 0,
|
||||
"no-underscore-dangle": 0,
|
||||
"no-use-before-define": 0,
|
||||
"no-useless-constructor": 0,
|
||||
"no-return-await": 0,
|
||||
"consistent-return": 0,
|
||||
"no-else-return": 0,
|
||||
"new-cap": ["error", { properties: false, capIsNew: false }],
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
index.cjs
|
||||
index.js
|
||||
index.d.ts
|
||||
node_modules
|
||||
dist
|
||||
.yarn/*
|
||||
!.yarn/patches
|
||||
!.yarn/plugins
|
||||
!.yarn/releases
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
|
||||
.turbo
|
||||
**/.turbo
|
||||
**/.eslintcache
|
||||
|
||||
.env
|
||||
.ipynb_checkpoints
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 LangChain
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,83 @@
|
||||
# New LangGraph.js Project
|
||||
|
||||
[](https://github.com/langchain-ai/new-langgraphjs-project/actions/workflows/unit-tests.yml)
|
||||
[](https://github.com/langchain-ai/new-langgraphjs-project/actions/workflows/integration-tests.yml)
|
||||
[](https://langgraph-studio.vercel.app/templates/open?githubUrl=https://github.com/langchain-ai/new-langgraphjs-project)
|
||||
|
||||
This template demonstrates a simple chatbot implemented using [LangGraph.js](https://github.com/langchain-ai/langgraphjs), designed for [LangGraph Studio](https://github.com/langchain-ai/langgraph-studio). The chatbot maintains persistent chat memory, allowing for coherent conversations across multiple interactions.
|
||||
|
||||

|
||||
|
||||
The core logic, defined in `src/agent/graph.ts`, showcases a straightforward chatbot that responds to user queries while maintaining context from previous messages.
|
||||
|
||||
## 🤔 What is this?
|
||||
|
||||
The simple chatbot:
|
||||
|
||||
1. Takes a user **message** as input
|
||||
2. Maintains a history of the conversation
|
||||
3. Returns a placeholder response, updating the conversation history
|
||||
|
||||
This template provides a foundation that can be easily customized and extended to create more complex conversational agents.
|
||||
|
||||
## 📖 Documentation
|
||||
|
||||
For JavaScript and TypeScript documentation, see the [LangGraph.js docs](https://docs.langchain.com/oss/javascript/langgraph/overview). LangGraph Studio also integrates with [LangSmith](https://smith.langchain.com/) for tracing and collaboration with teammates.
|
||||
|
||||
## Getting Started
|
||||
|
||||
Assuming you have already [installed LangGraph Studio](https://github.com/langchain-ai/langgraph-studio?tab=readme-ov-file#download), to set up:
|
||||
|
||||
1. Create a `.env` file. This template does not require any environment variables by default, but you will likely want to add some when customizing.
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
<!--
|
||||
Setup instruction auto-generated by `langgraph template lock`. DO NOT EDIT MANUALLY.
|
||||
-->
|
||||
|
||||
<!--
|
||||
End setup instructions
|
||||
-->
|
||||
|
||||
2. Open the folder in LangGraph Studio!
|
||||
3. Customize the code as needed.
|
||||
|
||||
## How to customize
|
||||
|
||||
1. **Add an LLM call**: You can select and install a chat model wrapper from [the LangChain.js ecosystem](https://js.langchain.com/docs/integrations/chat/), or use LangGraph.js without LangChain.js.
|
||||
2. **Extend the graph**: The core logic of the chatbot is defined in [graph.ts](./src/agent/graph.ts). You can modify this file to add new nodes, edges, or change the flow of the conversation.
|
||||
|
||||
You can also extend this template by:
|
||||
|
||||
- Adding [custom tools or functions](https://js.langchain.com/docs/how_to/tool_calling) to enhance the chatbot's capabilities.
|
||||
- Implementing additional logic for handling specific types of user queries or tasks.
|
||||
- Add retrieval-augmented generation (RAG) capabilities by integrating [external APIs or databases](https://langchain-ai.github.io/langgraphjs/tutorials/rag/langgraph_agentic_rag/) to provide more customized responses.
|
||||
|
||||
## Development
|
||||
|
||||
While iterating on your graph, you can edit past state and rerun your app from previous states to debug specific nodes. Local changes will be automatically applied via hot reload. Try experimenting with:
|
||||
|
||||
- Modifying the system prompt to give your chatbot a unique personality.
|
||||
- Adding new nodes to the graph for more complex conversation flows.
|
||||
- Implementing conditional logic to handle different types of user inputs.
|
||||
|
||||
Follow-up requests will be appended to the same thread. You can create an entirely new thread, clearing previous history, using the `+` button in the top right.
|
||||
|
||||
For more advanced features and examples, refer to the [LangGraph.js documentation](https://github.com/langchain-ai/langgraphjs). These resources can help you adapt this template for your specific use case and build more sophisticated conversational agents.
|
||||
|
||||
LangGraph Studio also integrates with [LangSmith](https://smith.langchain.com/) for more in-depth tracing and collaboration with teammates, allowing you to analyze and optimize your chatbot's performance.
|
||||
|
||||
<!--
|
||||
Configuration auto-generated by `langgraph template lock`. DO NOT EDIT MANUALLY.
|
||||
{
|
||||
"config_schemas": {
|
||||
"agent": {
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}
|
||||
}
|
||||
}
|
||||
-->
|
||||
@@ -0,0 +1,18 @@
|
||||
export default {
|
||||
preset: "ts-jest/presets/default-esm",
|
||||
moduleNameMapper: {
|
||||
"^(\\.{1,2}/.*)\\.js$": "$1",
|
||||
},
|
||||
transform: {
|
||||
"^.+\\.tsx?$": [
|
||||
"ts-jest",
|
||||
{
|
||||
useESM: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
extensionsToTreatAsEsm: [".ts"],
|
||||
setupFiles: ["dotenv/config"],
|
||||
passWithNoTests: true,
|
||||
testTimeout: 20_000,
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"$schema": "https://langgra.ph/schema.json",
|
||||
"node_version": "20",
|
||||
"graphs": {
|
||||
"agent": "./src/agent/graph.ts:graph"
|
||||
},
|
||||
"env": ".env",
|
||||
"dependencies": ["."]
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "example-graph",
|
||||
"version": "0.0.1",
|
||||
"description": "A starter template for creating a LangGraph workflow.",
|
||||
"packageManager": "yarn@1.22.22",
|
||||
"main": "my_app/graph.ts",
|
||||
"author": "Your Name",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"clean": "rm -rf dist",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=\\.test\\.ts$ --testPathIgnorePatterns=\\.int\\.test\\.ts$",
|
||||
"test:int": "node --experimental-vm-modules node_modules/jest/bin/jest.js --testPathPattern=\\.int\\.test\\.ts$",
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint src",
|
||||
"format:check": "prettier --check .",
|
||||
"lint:langgraph-json": "node scripts/checkLanggraphPaths.js",
|
||||
"lint:all": "yarn lint & yarn lint:langgraph-json & yarn format:check",
|
||||
"test:all": "yarn test && yarn test:int && yarn lint:langgraph"
|
||||
},
|
||||
"dependencies": {
|
||||
"@langchain/core": "^1.2.1",
|
||||
"@langchain/langgraph": "^1.4.7"
|
||||
},
|
||||
"resolutions": {
|
||||
"@langchain/langgraph-checkpoint": "1.0.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.3.5",
|
||||
"@eslint/js": "^10.0.1",
|
||||
"@tsconfig/recommended": "^1.0.13",
|
||||
"@types/jest": "^30.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^8.62.1",
|
||||
"@typescript-eslint/parser": "^8.62.1",
|
||||
"dotenv": "^17.4.2",
|
||||
"eslint": "^10.6.0",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"eslint-plugin-import": "^2.32.0",
|
||||
"eslint-plugin-no-instanceof": "^1.0.1",
|
||||
"eslint-plugin-prettier": "^5.5.6",
|
||||
"jest": "^30.4.2",
|
||||
"prettier": "^3.9.4",
|
||||
"ts-jest": "^29.4.11",
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Starter LangGraph.js Template
|
||||
* Make this code your own!
|
||||
*/
|
||||
import { StateGraph } from "@langchain/langgraph";
|
||||
import { RunnableConfig } from "@langchain/core/runnables";
|
||||
import { StateAnnotation } from "./state.js";
|
||||
|
||||
/**
|
||||
* Define a node, these do the work of the graph and should have most of the logic.
|
||||
* Must return a subset of the properties set in StateAnnotation.
|
||||
* @param state The current state of the graph.
|
||||
* @param config Extra parameters passed into the state graph.
|
||||
* @returns Some subset of parameters of the graph state, used to update the state
|
||||
* for the edges and nodes executed next.
|
||||
*/
|
||||
const callModel = async (
|
||||
state: typeof StateAnnotation.State,
|
||||
_config: RunnableConfig,
|
||||
): Promise<typeof StateAnnotation.Update> => {
|
||||
/**
|
||||
* Do some work... (e.g. call an LLM)
|
||||
* For example, with LangChain you could do something like:
|
||||
*
|
||||
* ```bash
|
||||
* $ npm i @langchain/anthropic
|
||||
* ```
|
||||
*
|
||||
* ```ts
|
||||
* import { ChatAnthropic } from "@langchain/anthropic";
|
||||
* const model = new ChatAnthropic({
|
||||
* model: "claude-3-5-sonnet-20240620",
|
||||
* apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
* });
|
||||
* const res = await model.invoke(state.messages);
|
||||
* ```
|
||||
*
|
||||
* Or, with an SDK directly:
|
||||
*
|
||||
* ```bash
|
||||
* $ npm i openai
|
||||
* ```
|
||||
*
|
||||
* ```ts
|
||||
* import OpenAI from "openai";
|
||||
* const openai = new OpenAI({
|
||||
* apiKey: process.env.OPENAI_API_KEY,
|
||||
* });
|
||||
*
|
||||
* const chatCompletion = await openai.chat.completions.create({
|
||||
* messages: [{
|
||||
* role: state.messages[0]._getType(),
|
||||
* content: state.messages[0].content,
|
||||
* }],
|
||||
* model: "gpt-4o-mini",
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
console.log("Current state:", state);
|
||||
return {
|
||||
messages: [
|
||||
{
|
||||
role: "assistant",
|
||||
content: `Hi there! How are you?`,
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Routing function: Determines whether to continue research or end the builder.
|
||||
* This function decides if the gathered information is satisfactory or if more research is needed.
|
||||
*
|
||||
* @param state - The current state of the research builder
|
||||
* @returns Either "callModel" to continue research or END to finish the builder
|
||||
*/
|
||||
export const route = (
|
||||
state: typeof StateAnnotation.State,
|
||||
): "__end__" | "callModel" => {
|
||||
if (state.messages.length > 0) {
|
||||
return "__end__";
|
||||
}
|
||||
// Loop back
|
||||
return "callModel";
|
||||
};
|
||||
|
||||
// Finally, create the graph itself.
|
||||
const builder = new StateGraph(StateAnnotation)
|
||||
// Add the nodes to do the work.
|
||||
// Chaining the nodes together in this way
|
||||
// updates the types of the StateGraph instance
|
||||
// so you have static type checking when it comes time
|
||||
// to add the edges.
|
||||
.addNode("callModel", callModel)
|
||||
// Regular edges mean "always transition to node B after node A is done"
|
||||
// The "__start__" and "__end__" nodes are "virtual" nodes that are always present
|
||||
// and represent the beginning and end of the builder.
|
||||
.addEdge("__start__", "callModel")
|
||||
// Conditional edges optionally route to different nodes (or end)
|
||||
.addConditionalEdges("callModel", route);
|
||||
|
||||
export const graph = builder.compile();
|
||||
|
||||
graph.name = "New Agent";
|
||||
@@ -0,0 +1,59 @@
|
||||
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
||||
import { Annotation, messagesStateReducer } from "@langchain/langgraph";
|
||||
|
||||
/**
|
||||
* A graph's StateAnnotation defines three main things:
|
||||
* 1. The structure of the data to be passed between nodes (which "channels" to read from/write to and their types)
|
||||
* 2. Default values for each field
|
||||
* 3. Reducers for the state's. Reducers are functions that determine how to apply updates to the state.
|
||||
* See [Reducers](https://langchain-ai.github.io/langgraphjs/concepts/low_level/#reducers) for more information.
|
||||
*/
|
||||
|
||||
// This is the primary state of your agent, where you can store any information
|
||||
export const StateAnnotation = Annotation.Root({
|
||||
/**
|
||||
* Messages track the primary execution state of the agent.
|
||||
*
|
||||
* Typically accumulates a pattern of:
|
||||
*
|
||||
* 1. HumanMessage - user input
|
||||
* 2. AIMessage with .tool_calls - agent picking tool(s) to use to collect
|
||||
* information
|
||||
* 3. ToolMessage(s) - the responses (or errors) from the executed tools
|
||||
*
|
||||
* (... repeat steps 2 and 3 as needed ...)
|
||||
* 4. AIMessage without .tool_calls - agent responding in unstructured
|
||||
* format to the user.
|
||||
*
|
||||
* 5. HumanMessage - user responds with the next conversational turn.
|
||||
*
|
||||
* (... repeat steps 2-5 as needed ... )
|
||||
*
|
||||
* Merges two lists of messages or message-like objects with role and content,
|
||||
* updating existing messages by ID.
|
||||
*
|
||||
* Message-like objects are automatically coerced by `messagesStateReducer` into
|
||||
* LangChain message classes. If a message does not have a given id,
|
||||
* LangGraph will automatically assign one.
|
||||
*
|
||||
* By default, this ensures the state is "append-only", unless the
|
||||
* new message has the same ID as an existing message.
|
||||
*
|
||||
* Returns:
|
||||
* A new list of messages with the messages from \`right\` merged into \`left\`.
|
||||
* If a message in \`right\` has the same ID as a message in \`left\`, the
|
||||
* message from \`right\` will replace the message from \`left\`.`
|
||||
*/
|
||||
messages: Annotation<BaseMessage[], BaseMessageLike[]>({
|
||||
reducer: messagesStateReducer,
|
||||
default: () => [],
|
||||
}),
|
||||
/**
|
||||
* Feel free to add additional attributes to your state as needed.
|
||||
* Common examples include retrieved documents, extracted entities, API connections, etc.
|
||||
*
|
||||
* For simple fields whose value should be overwritten by the return value of a node,
|
||||
* you don't need to define a reducer or default.
|
||||
*/
|
||||
// additionalField: Annotation<string>,
|
||||
});
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 583 KiB |
@@ -0,0 +1,8 @@
|
||||
import { describe, it, expect } from "@jest/globals";
|
||||
import { route } from "../src/agent/graph.js";
|
||||
describe("Routers", () => {
|
||||
it("Test route", async () => {
|
||||
const res = route({ messages: [] });
|
||||
expect(res).toEqual("callModel");
|
||||
}, 100_000);
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { describe, it, expect } from "@jest/globals";
|
||||
import { graph } from "../src/agent/graph.js";
|
||||
|
||||
describe("Graph", () => {
|
||||
it("should process input through the graph", async () => {
|
||||
const input = "What is the capital of France?";
|
||||
const result = await graph.invoke({ input });
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(typeof result).toBe("object");
|
||||
expect(result.messages).toBeDefined();
|
||||
expect(Array.isArray(result.messages)).toBe(true);
|
||||
expect(result.messages.length).toBeGreaterThan(0);
|
||||
|
||||
const lastMessage = result.messages[result.messages.length - 1];
|
||||
expect(lastMessage.content.toString().toLowerCase()).toContain("hi");
|
||||
}, 30000); // Increased timeout to 30 seconds
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"extends": "@tsconfig/recommended",
|
||||
"compilerOptions": {
|
||||
"target": "ES2021",
|
||||
"lib": ["ES2021", "ES2022.Object", "DOM"],
|
||||
"module": "NodeNext",
|
||||
"moduleResolution": "nodenext",
|
||||
"esModuleInterop": true,
|
||||
"noImplicitReturns": true,
|
||||
"declaration": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"useDefineForClassFields": true,
|
||||
"strictPropertyInitialization": false,
|
||||
"allowJs": true,
|
||||
"strict": true,
|
||||
"strictFunctionTypes": false,
|
||||
"outDir": "dist",
|
||||
"types": ["jest", "node"],
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["**/*.ts", "**/*.js"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,62 @@
|
||||
module.exports = {
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
"prettier",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 12,
|
||||
parser: "@typescript-eslint/parser",
|
||||
project: "./tsconfig.json",
|
||||
sourceType: "module",
|
||||
},
|
||||
plugins: ["import", "@typescript-eslint", "no-instanceof"],
|
||||
ignorePatterns: [
|
||||
".eslintrc.cjs",
|
||||
"scripts",
|
||||
"src/utils/lodash/*",
|
||||
"node_modules",
|
||||
"dist",
|
||||
"dist-cjs",
|
||||
"*.js",
|
||||
"*.cjs",
|
||||
"*.d.ts",
|
||||
],
|
||||
rules: {
|
||||
"no-process-env": 2,
|
||||
"no-instanceof/no-instanceof": 2,
|
||||
"@typescript-eslint/explicit-module-boundary-types": 0,
|
||||
"@typescript-eslint/no-empty-function": 0,
|
||||
"@typescript-eslint/no-shadow": 0,
|
||||
"@typescript-eslint/no-empty-interface": 0,
|
||||
"@typescript-eslint/no-use-before-define": ["error", "nofunc"],
|
||||
"@typescript-eslint/no-unused-vars": ["warn", { args: "none" }],
|
||||
"@typescript-eslint/no-floating-promises": "error",
|
||||
"@typescript-eslint/no-misused-promises": "error",
|
||||
camelcase: 0,
|
||||
"class-methods-use-this": 0,
|
||||
"import/extensions": [2, "ignorePackages"],
|
||||
"import/no-extraneous-dependencies": [
|
||||
"error",
|
||||
{ devDependencies: ["**/*.test.ts"] },
|
||||
],
|
||||
"import/no-unresolved": 0,
|
||||
"import/prefer-default-export": 0,
|
||||
"keyword-spacing": "error",
|
||||
"max-classes-per-file": 0,
|
||||
"max-len": 0,
|
||||
"no-await-in-loop": 0,
|
||||
"no-bitwise": 0,
|
||||
"no-console": 0,
|
||||
"no-restricted-syntax": 0,
|
||||
"no-shadow": 0,
|
||||
"no-continue": 0,
|
||||
"no-underscore-dangle": 0,
|
||||
"no-use-before-define": 0,
|
||||
"no-useless-constructor": 0,
|
||||
"no-return-await": 0,
|
||||
"consistent-return": 0,
|
||||
"no-else-return": 0,
|
||||
"new-cap": ["error", { properties: false, capIsNew: false }],
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"node_version": "20",
|
||||
"graphs": {
|
||||
"agent": "./src/graph.ts:graph"
|
||||
},
|
||||
"env": "../../.env"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "@js-monorepo-example/agent",
|
||||
"version": "0.0.1",
|
||||
"type": "module",
|
||||
"main": "src/graph.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@js-monorepo-example/shared": "*",
|
||||
"@langchain/core": "^1.2.1",
|
||||
"@langchain/langgraph": "^1.4.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Simple LangGraph.js example for monorepo testing
|
||||
*/
|
||||
import { StateGraph } from "@langchain/langgraph";
|
||||
import { RunnableConfig } from "@langchain/core/runnables";
|
||||
import { StateAnnotation } from "./state.js";
|
||||
import { getGreeting } from "@js-monorepo-example/shared";
|
||||
|
||||
/**
|
||||
* Simple node that uses the shared library
|
||||
*/
|
||||
const callModel = async (
|
||||
state: typeof StateAnnotation.State,
|
||||
_config: RunnableConfig,
|
||||
): Promise<typeof StateAnnotation.Update> => {
|
||||
// Use functions from the shared library
|
||||
const greeting = getGreeting();
|
||||
|
||||
return {
|
||||
messages: [
|
||||
{
|
||||
role: "assistant",
|
||||
content: `${greeting}`,
|
||||
},
|
||||
],
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Simple routing function
|
||||
*/
|
||||
export const route = (
|
||||
state: typeof StateAnnotation.State,
|
||||
): "__end__" | "callModel" => {
|
||||
if (state.messages.length > 0) {
|
||||
return "__end__";
|
||||
}
|
||||
return "callModel";
|
||||
};
|
||||
|
||||
// Create the graph
|
||||
const builder = new StateGraph(StateAnnotation)
|
||||
.addNode("callModel", callModel)
|
||||
.addEdge("__start__", "callModel")
|
||||
.addConditionalEdges("callModel", route);
|
||||
|
||||
export const graph = builder.compile();
|
||||
@@ -0,0 +1,15 @@
|
||||
import { BaseMessage, BaseMessageLike } from "@langchain/core/messages";
|
||||
import { Annotation, messagesStateReducer } from "@langchain/langgraph";
|
||||
|
||||
/**
|
||||
* Simple state annotation for the agent
|
||||
*/
|
||||
export const StateAnnotation = Annotation.Root({
|
||||
/**
|
||||
* Messages track the primary execution state of the agent.
|
||||
*/
|
||||
messages: Annotation<BaseMessage[], BaseMessageLike[]>({
|
||||
reducer: messagesStateReducer,
|
||||
default: () => [],
|
||||
}),
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "@js-monorepo-example/shared",
|
||||
"version": "0.0.1",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Simple utility functions for monorepo testing
|
||||
*/
|
||||
export function getGreeting(): string {
|
||||
return "Hello from shared library!";
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "js-monorepo-example",
|
||||
"version": "0.0.1",
|
||||
"packageManager": "yarn@1.22.22",
|
||||
"description": "A simple monorepo example for LangGraph integration testing.",
|
||||
"private": true,
|
||||
"workspaces": [
|
||||
"libs/*",
|
||||
"apps/*"
|
||||
],
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "turbo build",
|
||||
"clean": "turbo clean",
|
||||
"test": "turbo test",
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint 'apps/**/*.ts' 'libs/**/*.ts'"
|
||||
},
|
||||
"devDependencies": {
|
||||
"turbo": "^2.10.2",
|
||||
"typescript": "^6.0.3",
|
||||
"@tsconfig/recommended": "^1.0.13",
|
||||
"@eslint/eslintrc": "^3.3.5",
|
||||
"@eslint/js": "^10.0.1",
|
||||
"eslint": "^10.6.0",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"eslint-plugin-import": "^2.27.5",
|
||||
"eslint-plugin-no-instanceof": "^1.0.1",
|
||||
"eslint-plugin-prettier": "^5.5.6",
|
||||
"@typescript-eslint/eslint-plugin": "^8.62.1",
|
||||
"@typescript-eslint/parser": "^8.62.1",
|
||||
"prettier": "^3.9.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "@tsconfig/recommended",
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"declaration": true,
|
||||
"outDir": "./dist"
|
||||
},
|
||||
"include": ["apps/**/*", "libs/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"$schema": "https://turbo.build/schema.json",
|
||||
"tasks": {
|
||||
"build": {
|
||||
"dependsOn": ["^build"],
|
||||
"outputs": ["dist/**"]
|
||||
},
|
||||
"clean": {
|
||||
"dependsOn": ["^clean"]
|
||||
},
|
||||
"test": {
|
||||
"dependsOn": ["^test"]
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1 @@
|
||||
__version__ = "0.4.31"
|
||||
@@ -0,0 +1,4 @@
|
||||
from .cli import cli
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
@@ -0,0 +1,124 @@
|
||||
"""Shared ignore-file handling for local source filtering."""
|
||||
|
||||
import pathlib
|
||||
from dataclasses import dataclass
|
||||
|
||||
import pathspec
|
||||
|
||||
_ALWAYS_EXCLUDE = [
|
||||
"__pycache__/",
|
||||
".git/",
|
||||
".venv/",
|
||||
"venv/",
|
||||
"node_modules/",
|
||||
".tox/",
|
||||
".mypy_cache/",
|
||||
]
|
||||
_ALWAYS_EXCLUDE_NAMES = frozenset(
|
||||
pattern.rstrip("/").split("/")[-1] for pattern in _ALWAYS_EXCLUDE
|
||||
)
|
||||
_GLOB_CHARS = frozenset("*?[")
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class _NegatedDockerignoreHints:
|
||||
exact_dirs: frozenset[pathlib.PurePosixPath] = frozenset()
|
||||
wildcard_prefixes: frozenset[pathlib.PurePosixPath] = frozenset()
|
||||
recurse_all: bool = False
|
||||
|
||||
def requires_dir_walk(self, path: pathlib.PurePosixPath) -> bool:
|
||||
if self.recurse_all or path in self.exact_dirs:
|
||||
return True
|
||||
return any(
|
||||
path == prefix or path in prefix.parents or prefix in path.parents
|
||||
for prefix in self.wildcard_prefixes
|
||||
)
|
||||
|
||||
|
||||
def _build_ignore_spec(
|
||||
directory: pathlib.Path, *, include_gitignore: bool = True
|
||||
) -> pathspec.PathSpec:
|
||||
"""Build a PathSpec combining built-in exclusions with ignore files.
|
||||
|
||||
Always excludes common non-source directories (`_ALWAYS_EXCLUDE`). On top
|
||||
of that, patterns from `.dockerignore` are merged in. `.gitignore` patterns
|
||||
are optional because some callers need Docker build-context semantics,
|
||||
while archive creation wants both files.
|
||||
"""
|
||||
lines: list[str] = list(_ALWAYS_EXCLUDE)
|
||||
ignore_files = [".dockerignore"]
|
||||
if include_gitignore:
|
||||
ignore_files.append(".gitignore")
|
||||
for name in ignore_files:
|
||||
ignore_file = directory / name
|
||||
if ignore_file.is_file():
|
||||
lines.extend(ignore_file.read_text(encoding="utf-8").splitlines())
|
||||
return pathspec.PathSpec.from_lines("gitwildmatch", lines)
|
||||
|
||||
|
||||
def _is_always_excluded(path: pathlib.PurePosixPath, *, is_dir: bool) -> bool:
|
||||
"""Whether `path` lives inside a built-in excluded directory."""
|
||||
parent_parts = path.parts if is_dir else path.parts[:-1]
|
||||
return any(part in _ALWAYS_EXCLUDE_NAMES for part in parent_parts)
|
||||
|
||||
|
||||
def _build_dockerignore_negation_hints(
|
||||
directory: pathlib.Path,
|
||||
) -> _NegatedDockerignoreHints:
|
||||
"""Summarize which ignored directories must still be traversed.
|
||||
|
||||
Most negations only require walking a small, concrete chain of parent
|
||||
directories (for example `!assets/keep.txt` requires entering `assets/`).
|
||||
Broader glob negations may force a wider walk.
|
||||
"""
|
||||
ignore_file = directory / ".dockerignore"
|
||||
if not ignore_file.is_file():
|
||||
return _NegatedDockerignoreHints()
|
||||
|
||||
exact_dirs: set[pathlib.PurePosixPath] = set()
|
||||
wildcard_prefixes: set[pathlib.PurePosixPath] = set()
|
||||
recurse_all = False
|
||||
|
||||
for raw_line in ignore_file.read_text(encoding="utf-8").splitlines():
|
||||
line = raw_line.strip()
|
||||
if not line or line.startswith("#") or line.startswith("\\!"):
|
||||
continue
|
||||
if line.startswith("\\#"):
|
||||
line = line[1:]
|
||||
if not line.startswith("!"):
|
||||
continue
|
||||
|
||||
pattern = line[1:].lstrip("/")
|
||||
while pattern.startswith("./"):
|
||||
pattern = pattern[2:]
|
||||
pattern = pattern.rstrip("/")
|
||||
parts = [part for part in pattern.split("/") if part and part != "."]
|
||||
if not parts:
|
||||
recurse_all = True
|
||||
continue
|
||||
|
||||
wildcard_index = next(
|
||||
(
|
||||
idx
|
||||
for idx, part in enumerate(parts)
|
||||
if any(char in part for char in _GLOB_CHARS)
|
||||
),
|
||||
None,
|
||||
)
|
||||
if wildcard_index is not None:
|
||||
literal_parts = parts[:wildcard_index]
|
||||
if not literal_parts:
|
||||
recurse_all = True
|
||||
continue
|
||||
wildcard_prefixes.add(pathlib.PurePosixPath(*literal_parts))
|
||||
continue
|
||||
|
||||
parent_parts = parts[:-1]
|
||||
for idx in range(1, len(parent_parts) + 1):
|
||||
exact_dirs.add(pathlib.PurePosixPath(*parent_parts[:idx]))
|
||||
|
||||
return _NegatedDockerignoreHints(
|
||||
exact_dirs=frozenset(exact_dirs),
|
||||
wildcard_prefixes=frozenset(wildcard_prefixes),
|
||||
recurse_all=recurse_all,
|
||||
)
|
||||
@@ -0,0 +1,105 @@
|
||||
import functools
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import platform
|
||||
import threading
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from typing import Any, TypedDict
|
||||
|
||||
from langgraph_cli.constants import (
|
||||
DEFAULT_CONFIG,
|
||||
DEFAULT_PORT,
|
||||
SUPABASE_PUBLIC_API_KEY,
|
||||
SUPABASE_URL,
|
||||
)
|
||||
from langgraph_cli.version import __version__
|
||||
|
||||
|
||||
class LogData(TypedDict):
|
||||
os: str
|
||||
os_version: str
|
||||
python_version: str
|
||||
cli_version: str
|
||||
cli_command: str
|
||||
params: dict[str, Any]
|
||||
|
||||
|
||||
def get_anonymized_params(
|
||||
kwargs: dict[str, Any], *, cli_command: str
|
||||
) -> dict[str, bool | str]:
|
||||
params: dict[str, bool | str] = {}
|
||||
|
||||
if cli_command == "deploy" and (
|
||||
analytics_source := os.getenv("LANGGRAPH_CLI_ANALYTICS_SOURCE")
|
||||
):
|
||||
params["source"] = analytics_source
|
||||
|
||||
# anonymize params with values
|
||||
if config := kwargs.get("config"):
|
||||
if config != pathlib.Path(DEFAULT_CONFIG).resolve():
|
||||
params["config"] = True
|
||||
|
||||
if port := kwargs.get("port"):
|
||||
if port != DEFAULT_PORT:
|
||||
params["port"] = True
|
||||
|
||||
if kwargs.get("docker_compose"):
|
||||
params["docker_compose"] = True
|
||||
|
||||
if kwargs.get("debugger_port"):
|
||||
params["debugger_port"] = True
|
||||
|
||||
if kwargs.get("postgres_uri"):
|
||||
params["postgres_uri"] = True
|
||||
|
||||
# pick up exact values for boolean flags
|
||||
for boolean_param in ["recreate", "pull", "watch", "wait", "verbose"]:
|
||||
if kwargs.get(boolean_param):
|
||||
params[boolean_param] = kwargs[boolean_param]
|
||||
|
||||
return params
|
||||
|
||||
|
||||
def log_data(data: LogData) -> None:
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"apikey": SUPABASE_PUBLIC_API_KEY,
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
}
|
||||
supabase_url = SUPABASE_URL
|
||||
|
||||
req = urllib.request.Request(
|
||||
f"{supabase_url}/rest/v1/logs",
|
||||
data=json.dumps(data).encode("utf-8"),
|
||||
headers=headers,
|
||||
method="POST",
|
||||
)
|
||||
|
||||
try:
|
||||
urllib.request.urlopen(req)
|
||||
except urllib.error.URLError:
|
||||
pass
|
||||
|
||||
|
||||
def log_command(func):
|
||||
@functools.wraps(func)
|
||||
def decorator(*args, **kwargs):
|
||||
if os.getenv("LANGGRAPH_CLI_NO_ANALYTICS") == "1":
|
||||
return func(*args, **kwargs)
|
||||
|
||||
data = {
|
||||
"os": platform.system(),
|
||||
"os_version": platform.version(),
|
||||
"python_version": platform.python_version(),
|
||||
"cli_version": __version__,
|
||||
"cli_command": func.__name__,
|
||||
"params": get_anonymized_params(kwargs, cli_command=func.__name__),
|
||||
}
|
||||
|
||||
background_thread = threading.Thread(target=log_data, args=(data,))
|
||||
background_thread.start()
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return decorator
|
||||
@@ -0,0 +1,138 @@
|
||||
"""Create a tarball of project source for remote builds."""
|
||||
|
||||
import os
|
||||
import pathlib
|
||||
import tarfile
|
||||
import tempfile
|
||||
from contextlib import contextmanager
|
||||
|
||||
import click
|
||||
import pathspec
|
||||
|
||||
from langgraph_cli._ignore import _build_ignore_spec
|
||||
from langgraph_cli.config import Config, _assemble_local_deps
|
||||
|
||||
_WARN_SIZE = 50 * 1024 * 1024 # 50 MB
|
||||
_MAX_SIZE = 200 * 1024 * 1024 # 200 MB
|
||||
|
||||
|
||||
def _tar_filter(tarinfo: tarfile.TarInfo) -> tarfile.TarInfo | None:
|
||||
"""Strip symlinks, hardlinks, and traversal paths from archive."""
|
||||
if tarinfo.issym() or tarinfo.islnk():
|
||||
return None
|
||||
if ".." in tarinfo.name.split("/"):
|
||||
return None
|
||||
return tarinfo
|
||||
|
||||
|
||||
def _add_directory(
|
||||
tar: tarfile.TarFile,
|
||||
source_dir: pathlib.Path,
|
||||
arcname_prefix: str | None,
|
||||
ignore_spec: pathspec.PathSpec,
|
||||
) -> None:
|
||||
"""Recursively add a directory to the tarball under the given prefix.
|
||||
|
||||
If arcname_prefix is None, files are added at the archive root.
|
||||
Paths matching ignore_spec are excluded.
|
||||
"""
|
||||
for root, dirs, files in os.walk(source_dir):
|
||||
rel_root = os.path.relpath(root, source_dir).replace(os.sep, "/")
|
||||
dirs[:] = [
|
||||
d
|
||||
for d in dirs
|
||||
if not ignore_spec.match_file(
|
||||
f"{rel_root}/{d}/" if rel_root != "." else f"{d}/"
|
||||
)
|
||||
]
|
||||
for f in files:
|
||||
full_path = os.path.join(root, f)
|
||||
rel = os.path.relpath(full_path, source_dir).replace(os.sep, "/")
|
||||
if ignore_spec.match_file(rel):
|
||||
continue
|
||||
arcname = f"{arcname_prefix}/{rel}" if arcname_prefix else rel
|
||||
info = tar.gettarinfo(full_path, arcname=arcname)
|
||||
filtered = _tar_filter(info)
|
||||
if filtered is None:
|
||||
continue
|
||||
with open(full_path, "rb") as fobj:
|
||||
tar.addfile(filtered, fobj)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def create_archive(
|
||||
config_path: pathlib.Path,
|
||||
config: Config,
|
||||
):
|
||||
"""Context manager that creates a .tar.gz archive of the project source.
|
||||
|
||||
Uses _assemble_local_deps to discover local dependencies referenced in
|
||||
langgraph.json, including those outside config.parent (monorepo case).
|
||||
|
||||
The archive preserves the real filesystem layout relative to the common
|
||||
ancestor of config.parent and all external dependency directories, so that
|
||||
relative references (e.g. `../shared-lib`) resolve correctly after
|
||||
extraction.
|
||||
|
||||
Yields (archive_path, file_size, config_relative_path). The temporary
|
||||
directory holding the archive is cleaned up automatically on exit.
|
||||
"""
|
||||
config_path = config_path.resolve()
|
||||
context_dir = config_path.parent
|
||||
|
||||
local_deps = _assemble_local_deps(config_path, config)
|
||||
extra_contexts = local_deps.additional_contexts or []
|
||||
|
||||
dirs_to_include = [context_dir] + list(extra_contexts)
|
||||
|
||||
common = context_dir
|
||||
for d in extra_contexts:
|
||||
common = pathlib.Path(os.path.commonpath([common, d]))
|
||||
|
||||
tmp_dir = tempfile.mkdtemp(prefix="langgraph-deploy-")
|
||||
try:
|
||||
archive_path = os.path.join(tmp_dir, "source.tar.gz")
|
||||
|
||||
added_dirs: set[str] = set()
|
||||
with tarfile.open(archive_path, "w:gz") as tar:
|
||||
for dir_path in dirs_to_include:
|
||||
rel = dir_path.relative_to(common)
|
||||
prefix = str(rel).replace(os.sep, "/") if str(rel) != "." else None
|
||||
key = prefix or ""
|
||||
if key in added_dirs:
|
||||
continue
|
||||
added_dirs.add(key)
|
||||
ignore_spec = _build_ignore_spec(dir_path)
|
||||
_add_directory(
|
||||
tar, dir_path, arcname_prefix=prefix, ignore_spec=ignore_spec
|
||||
)
|
||||
|
||||
file_size = os.path.getsize(archive_path)
|
||||
|
||||
config_rel = str(config_path.relative_to(common)).replace(os.sep, "/")
|
||||
|
||||
with tarfile.open(archive_path, "r:gz") as tar:
|
||||
names = tar.getnames()
|
||||
if config_rel not in names:
|
||||
raise click.ClickException(
|
||||
f"Archive validation failed: {config_rel} not found in archive"
|
||||
)
|
||||
|
||||
if file_size > _MAX_SIZE:
|
||||
raise click.ClickException(
|
||||
f"Source archive is {file_size / 1_048_576:.1f} MB, which exceeds the 200 MB limit. "
|
||||
"Add large files to .dockerignore or .gitignore (model weights, data sets, etc.)."
|
||||
)
|
||||
|
||||
if file_size > _WARN_SIZE:
|
||||
click.secho(
|
||||
f" Warning: source archive is {file_size / 1_048_576:.1f} MB. "
|
||||
"Consider adding large files to .dockerignore or .gitignore.",
|
||||
fg="yellow",
|
||||
)
|
||||
|
||||
yield archive_path, file_size, config_rel
|
||||
finally:
|
||||
import shutil
|
||||
|
||||
shutil.rmtree(tmp_dir, ignore_errors=True)
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,6 @@
|
||||
DEFAULT_CONFIG = "langgraph.json"
|
||||
DEFAULT_PORT = 8123
|
||||
|
||||
# analytics
|
||||
SUPABASE_PUBLIC_API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Imt6cmxwcG9qaW5wY3l5YWlweG5iIiwicm9sZSI6ImFub24iLCJpYXQiOjE3MTkyNTc1NzksImV4cCI6MjAzNDgzMzU3OX0.kkVOlLz3BxemA5nP-vat3K4qRtrDuO4SwZSR_htcX9c"
|
||||
SUPABASE_URL = "https://kzrlppojinpcyyaipxnb.supabase.co"
|
||||
@@ -0,0 +1,141 @@
|
||||
"""Detection of tracked Python packages in a local LangGraph project.
|
||||
|
||||
Mirrors host-backend's `host.models.dependency_tracking` so that CLI-based
|
||||
deploys report the same `tracked_packages` revision metadata that
|
||||
GitHub-based deploys do. The host backend strictly validates each entry
|
||||
against `<package-name>:<version>` with package-name in `TRACKED_PACKAGES`,
|
||||
so the detection rules here must match exactly.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pathlib
|
||||
import re
|
||||
|
||||
# Single source of truth for which packages the host backend cares about.
|
||||
# Keep in sync with host-backend/host/models/tracked_packages.py.
|
||||
TRACKED_PACKAGES: tuple[str, ...] = ("google-adk",)
|
||||
|
||||
_MAX_READ_BYTES = 5 * 1024 * 1024
|
||||
|
||||
_PACKAGES_ALT = "|".join(re.escape(p) for p in TRACKED_PACKAGES)
|
||||
|
||||
_DEPS_RE = re.compile(
|
||||
rf"(?<![a-zA-Z0-9_-])({_PACKAGES_ALT})"
|
||||
r"(?:\[[^\]]*\])?"
|
||||
r"\s*((?:(?:==|>=|<=|~=|!=|>|<)\s*[\w.*]+\s*,?\s*)+)"
|
||||
)
|
||||
|
||||
_UV_LOCK_RE = re.compile(
|
||||
rf'name\s*=\s*"({_PACKAGES_ALT})"\s*\n\s*version\s*=\s*"([^"]+)"'
|
||||
)
|
||||
|
||||
_BARE_RE = re.compile(rf'(?<![a-zA-Z0-9_-])({_PACKAGES_ALT})(?:\[[^\]]*\])?\s*[,"\'\n]')
|
||||
|
||||
_EXTRAS_BRACKET_RE = re.compile(r"\[([a-zA-Z0-9_.\- ,\t]+)\]")
|
||||
|
||||
|
||||
def _appears_in_extras(content: str, pkg: str) -> bool:
|
||||
for m in _EXTRAS_BRACKET_RE.finditer(content):
|
||||
for token in m.group(1).split(","):
|
||||
if token.strip() == pkg:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _read_text(path: pathlib.Path) -> str | None:
|
||||
try:
|
||||
if not path.is_file():
|
||||
return None
|
||||
with open(path, "rb") as f:
|
||||
data = f.read(_MAX_READ_BYTES + 1)
|
||||
except OSError:
|
||||
return None
|
||||
if len(data) > _MAX_READ_BYTES:
|
||||
data = data[:_MAX_READ_BYTES]
|
||||
return data.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _find_version_for(
|
||||
pkg: str,
|
||||
lock_content: str | None,
|
||||
pyproject_content: str | None,
|
||||
requirements_content: str | None,
|
||||
) -> str | None:
|
||||
if lock_content is not None:
|
||||
for m in _UV_LOCK_RE.finditer(lock_content):
|
||||
if m.group(1) == pkg:
|
||||
return m.group(2)
|
||||
for content in (pyproject_content, requirements_content):
|
||||
if content is None:
|
||||
continue
|
||||
for m in _DEPS_RE.finditer(content):
|
||||
if m.group(1) == pkg:
|
||||
return m.group(2).strip().rstrip(",")
|
||||
for m in _BARE_RE.finditer(content):
|
||||
if m.group(1) == pkg:
|
||||
return "unknown"
|
||||
if _appears_in_extras(content, pkg):
|
||||
return "unknown"
|
||||
return None
|
||||
|
||||
|
||||
def _resolved_dep_base(
|
||||
project_root: pathlib.Path, dep_path: str
|
||||
) -> pathlib.Path | None:
|
||||
"""Return the resolved dep directory if it stays inside the project root."""
|
||||
try:
|
||||
candidate = (project_root / dep_path).resolve()
|
||||
except (OSError, RuntimeError):
|
||||
return None
|
||||
try:
|
||||
candidate.relative_to(project_root)
|
||||
except ValueError:
|
||||
return None
|
||||
return candidate
|
||||
|
||||
|
||||
def find_tracked_packages(
|
||||
config: pathlib.Path,
|
||||
config_json: dict,
|
||||
) -> list[str]:
|
||||
"""Return every tracked package found in deps as `<name>:<version>` entries.
|
||||
|
||||
`config` is the absolute path to `langgraph.json`; dep paths in
|
||||
`config_json["dependencies"]` are resolved relative to its parent.
|
||||
Detection precedence per package: uv.lock resolved > pyproject.toml /
|
||||
requirements.txt specifier > bare reference > extras bracket (last
|
||||
two recorded as "unknown"). Output is ordered by `TRACKED_PACKAGES`.
|
||||
"""
|
||||
try:
|
||||
project_root = config.parent.resolve()
|
||||
except (OSError, RuntimeError):
|
||||
return []
|
||||
|
||||
dep_paths = config_json.get("dependencies") or ["."]
|
||||
|
||||
found: dict[str, str] = {}
|
||||
|
||||
for dep_path in dep_paths:
|
||||
if all(pkg in found for pkg in TRACKED_PACKAGES):
|
||||
break
|
||||
if not isinstance(dep_path, str):
|
||||
continue
|
||||
base = _resolved_dep_base(project_root, dep_path)
|
||||
if base is None or not base.is_dir():
|
||||
continue
|
||||
|
||||
lock_content = _read_text(base / "uv.lock")
|
||||
pyproject_content = _read_text(base / "pyproject.toml")
|
||||
requirements_content = _read_text(base / "requirements.txt")
|
||||
|
||||
for pkg in TRACKED_PACKAGES:
|
||||
if pkg in found:
|
||||
continue
|
||||
version = _find_version_for(
|
||||
pkg, lock_content, pyproject_content, requirements_content
|
||||
)
|
||||
if version is not None:
|
||||
found[pkg] = version
|
||||
|
||||
return [f"{pkg}:{found[pkg]}" for pkg in TRACKED_PACKAGES if pkg in found]
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,406 @@
|
||||
import copy
|
||||
import json
|
||||
import pathlib
|
||||
import platform
|
||||
import shutil
|
||||
from collections.abc import Callable, Sequence
|
||||
from typing import Literal, NamedTuple
|
||||
|
||||
import click.exceptions
|
||||
|
||||
import langgraph_cli.config
|
||||
from langgraph_cli.exec import subp_exec
|
||||
|
||||
ROOT = pathlib.Path(__file__).parent.resolve()
|
||||
DEFAULT_POSTGRES_URI = (
|
||||
"postgres://postgres:postgres@langgraph-postgres:5432/postgres?sslmode=disable"
|
||||
)
|
||||
|
||||
|
||||
class Version(NamedTuple):
|
||||
major: int
|
||||
minor: int
|
||||
patch: int
|
||||
|
||||
|
||||
DockerComposeType = Literal["plugin", "standalone"]
|
||||
|
||||
|
||||
class DockerCapabilities(NamedTuple):
|
||||
version_docker: Version
|
||||
version_compose: Version
|
||||
healthcheck_start_interval: bool
|
||||
compose_type: DockerComposeType = "plugin"
|
||||
|
||||
|
||||
def _parse_version(version: str) -> Version:
|
||||
parts = version.split(".", 2)
|
||||
if len(parts) == 1:
|
||||
major = parts[0]
|
||||
minor = "0"
|
||||
patch = "0"
|
||||
elif len(parts) == 2:
|
||||
major, minor = parts
|
||||
patch = "0"
|
||||
else:
|
||||
major, minor, patch = parts
|
||||
return Version(
|
||||
int(major.lstrip("v")), int(minor), int(patch.split("-")[0].split("+")[0])
|
||||
)
|
||||
|
||||
|
||||
def can_build_locally() -> tuple[bool, str | None]:
|
||||
"""Return whether local deployment builds can run on this machine.
|
||||
|
||||
Checks:
|
||||
- Docker binary is installed
|
||||
- Docker daemon is running
|
||||
- Buildx is available when cross-compilation is required (non-x86_64)
|
||||
"""
|
||||
if shutil.which("docker") is None:
|
||||
return (
|
||||
False,
|
||||
"Docker is required but not installed.\n"
|
||||
"Install Docker Desktop: https://docs.docker.com/get-docker/",
|
||||
)
|
||||
try:
|
||||
import subprocess
|
||||
|
||||
docker_info = subprocess.run(
|
||||
["docker", "info"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
if docker_info.returncode != 0:
|
||||
return (
|
||||
False,
|
||||
"Docker is installed but not running.\nStart Docker and try again.",
|
||||
)
|
||||
|
||||
if platform.machine() != "x86_64":
|
||||
buildx = subprocess.run(
|
||||
["docker", "buildx", "version"],
|
||||
capture_output=True,
|
||||
timeout=10,
|
||||
)
|
||||
if buildx.returncode != 0:
|
||||
return (
|
||||
False,
|
||||
"Docker Buildx is required but not installed.\n"
|
||||
"Your machine architecture ("
|
||||
+ platform.machine()
|
||||
+ ") requires Buildx to cross-compile images for linux/amd64.\n"
|
||||
"Install Buildx: https://docs.docker.com/build/install-buildx/",
|
||||
)
|
||||
return True, None
|
||||
except Exception:
|
||||
return False, "Unable to verify local Docker build support."
|
||||
|
||||
|
||||
def check_capabilities(runner) -> DockerCapabilities:
|
||||
# check docker available
|
||||
if shutil.which("docker") is None:
|
||||
raise click.UsageError("Docker not installed") from None
|
||||
|
||||
try:
|
||||
stdout, _ = runner.run(
|
||||
subp_exec("docker", "info", "-f", "{{json .}}", collect=True)
|
||||
)
|
||||
info = json.loads(stdout)
|
||||
except (click.exceptions.Exit, json.JSONDecodeError):
|
||||
raise click.UsageError("Docker not installed or not running") from None
|
||||
|
||||
if not info["ServerVersion"]:
|
||||
raise click.UsageError("Docker not running") from None
|
||||
|
||||
compose_type: DockerComposeType
|
||||
try:
|
||||
compose = next(
|
||||
p for p in info["ClientInfo"]["Plugins"] if p["Name"] == "compose"
|
||||
)
|
||||
compose_version_str = compose["Version"]
|
||||
compose_type = "plugin"
|
||||
except (KeyError, StopIteration):
|
||||
if shutil.which("docker-compose") is None:
|
||||
raise click.UsageError("Docker Compose not installed") from None
|
||||
|
||||
compose_version_str, _ = runner.run(
|
||||
subp_exec("docker-compose", "--version", "--short", collect=True)
|
||||
)
|
||||
compose_type = "standalone"
|
||||
|
||||
# parse versions
|
||||
docker_version = _parse_version(info["ServerVersion"])
|
||||
compose_version = _parse_version(compose_version_str)
|
||||
|
||||
# check capabilities
|
||||
return DockerCapabilities(
|
||||
version_docker=docker_version,
|
||||
version_compose=compose_version,
|
||||
healthcheck_start_interval=docker_version >= Version(25, 0, 0),
|
||||
compose_type=compose_type,
|
||||
)
|
||||
|
||||
|
||||
def debugger_compose(*, port: int | None = None, base_url: str | None = None) -> dict:
|
||||
if port is None:
|
||||
return ""
|
||||
|
||||
config = {
|
||||
"langgraph-debugger": {
|
||||
"image": "langchain/langgraph-debugger",
|
||||
"restart": "on-failure",
|
||||
"depends_on": {
|
||||
"langgraph-postgres": {"condition": "service_healthy"},
|
||||
},
|
||||
"ports": [f'"{port}:3968"'],
|
||||
}
|
||||
}
|
||||
|
||||
if base_url:
|
||||
config["langgraph-debugger"]["environment"] = {
|
||||
"VITE_STUDIO_LOCAL_GRAPH_URL": base_url
|
||||
}
|
||||
|
||||
return config
|
||||
|
||||
|
||||
# Function to convert dictionary to YAML
|
||||
def dict_to_yaml(d: dict, *, indent: int = 0) -> str:
|
||||
"""Convert a dictionary to a YAML string."""
|
||||
yaml_str = ""
|
||||
|
||||
for idx, (key, value) in enumerate(d.items()):
|
||||
# Format things in a visually appealing way
|
||||
# Use an extra newline for top-level keys only
|
||||
if idx >= 1 and indent < 2:
|
||||
yaml_str += "\n"
|
||||
space = " " * indent
|
||||
if isinstance(value, dict):
|
||||
yaml_str += f"{space}{key}:\n" + dict_to_yaml(value, indent=indent + 1)
|
||||
elif isinstance(value, list):
|
||||
yaml_str += f"{space}{key}:\n"
|
||||
for item in value:
|
||||
yaml_str += f"{space} - {item}\n"
|
||||
else:
|
||||
yaml_str += f"{space}{key}: {value}\n"
|
||||
return yaml_str
|
||||
|
||||
|
||||
def compose_as_dict(
|
||||
capabilities: DockerCapabilities,
|
||||
*,
|
||||
port: int,
|
||||
debugger_port: int | None = None,
|
||||
debugger_base_url: str | None = None,
|
||||
# postgres://user:password@host:port/database?option=value
|
||||
postgres_uri: str | None = None,
|
||||
# If you are running against an already-built image, you can pass it here
|
||||
image: str | None = None,
|
||||
# Base image to use for the LangGraph API server
|
||||
base_image: str | None = None,
|
||||
# API version of the base image
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> dict:
|
||||
"""Create a docker compose file as a dictionary in YML style."""
|
||||
if postgres_uri is None:
|
||||
include_db = True
|
||||
postgres_uri = DEFAULT_POSTGRES_URI
|
||||
else:
|
||||
include_db = False
|
||||
|
||||
# The services below are defined in a non-intuitive order to match
|
||||
# the existing unit tests for this function.
|
||||
# It's fine to re-order just requires updating the unit tests, so it should
|
||||
# be done with caution.
|
||||
|
||||
# Define the Redis service first as per the test order
|
||||
services = {
|
||||
"langgraph-redis": {
|
||||
"image": "redis:6",
|
||||
"healthcheck": {
|
||||
"test": "redis-cli ping",
|
||||
"interval": "5s",
|
||||
"timeout": "1s",
|
||||
"retries": 5,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
# Add Postgres service before langgraph-api if it is needed
|
||||
if include_db:
|
||||
services["langgraph-postgres"] = {
|
||||
"image": "pgvector/pgvector:pg16",
|
||||
"ports": ['"5433:5432"'],
|
||||
"environment": {
|
||||
"POSTGRES_DB": "postgres",
|
||||
"POSTGRES_USER": "postgres",
|
||||
"POSTGRES_PASSWORD": "postgres",
|
||||
},
|
||||
"command": ["postgres", "-c", "shared_preload_libraries=vector"],
|
||||
"volumes": ["langgraph-data:/var/lib/postgresql/data"],
|
||||
"healthcheck": {
|
||||
"test": "pg_isready -U postgres",
|
||||
"start_period": "10s",
|
||||
"timeout": "1s",
|
||||
"retries": 5,
|
||||
},
|
||||
}
|
||||
if capabilities.healthcheck_start_interval:
|
||||
services["langgraph-postgres"]["healthcheck"]["interval"] = "60s"
|
||||
services["langgraph-postgres"]["healthcheck"]["start_interval"] = "1s"
|
||||
else:
|
||||
services["langgraph-postgres"]["healthcheck"]["interval"] = "5s"
|
||||
|
||||
# Add optional debugger service if debugger_port is specified
|
||||
if debugger_port:
|
||||
services["langgraph-debugger"] = debugger_compose(
|
||||
port=debugger_port, base_url=debugger_base_url
|
||||
)["langgraph-debugger"]
|
||||
|
||||
# Add langgraph-api service
|
||||
api_environment = {
|
||||
"REDIS_URI": "redis://langgraph-redis:6379",
|
||||
"POSTGRES_URI": postgres_uri,
|
||||
}
|
||||
if engine_runtime_mode == "distributed":
|
||||
api_environment["N_JOBS_PER_WORKER"] = '"0"'
|
||||
|
||||
services["langgraph-api"] = {
|
||||
"ports": [f'"{port}:8000"'],
|
||||
"depends_on": {
|
||||
"langgraph-redis": {"condition": "service_healthy"},
|
||||
},
|
||||
"environment": api_environment,
|
||||
}
|
||||
if image:
|
||||
services["langgraph-api"]["image"] = image
|
||||
|
||||
# If Postgres is included, add it to the dependencies of langgraph-api
|
||||
if include_db:
|
||||
services["langgraph-api"]["depends_on"]["langgraph-postgres"] = {
|
||||
"condition": "service_healthy"
|
||||
}
|
||||
|
||||
# Additional healthcheck for langgraph-api if required
|
||||
if capabilities.healthcheck_start_interval:
|
||||
services["langgraph-api"]["healthcheck"] = {
|
||||
"test": "python /api/healthcheck.py",
|
||||
"interval": "60s",
|
||||
"start_interval": "1s",
|
||||
"start_period": "10s",
|
||||
}
|
||||
|
||||
# Final compose dictionary with volumes included if needed
|
||||
compose_dict = {}
|
||||
if include_db:
|
||||
compose_dict["volumes"] = {"langgraph-data": {"driver": "local"}}
|
||||
compose_dict["services"] = services
|
||||
|
||||
return compose_dict
|
||||
|
||||
|
||||
def compose(
|
||||
capabilities: DockerCapabilities,
|
||||
*,
|
||||
port: int,
|
||||
debugger_port: int | None = None,
|
||||
debugger_base_url: str | None = None,
|
||||
# postgres://user:password@host:port/database?option=value
|
||||
postgres_uri: str | None = None,
|
||||
image: str | None = None,
|
||||
base_image: str | None = None,
|
||||
api_version: str | None = None,
|
||||
engine_runtime_mode: str = "combined_queue_worker",
|
||||
) -> str:
|
||||
"""Create a docker compose file as a string."""
|
||||
compose_content = compose_as_dict(
|
||||
capabilities,
|
||||
port=port,
|
||||
debugger_port=debugger_port,
|
||||
debugger_base_url=debugger_base_url,
|
||||
postgres_uri=postgres_uri,
|
||||
image=image,
|
||||
base_image=base_image,
|
||||
api_version=api_version,
|
||||
engine_runtime_mode=engine_runtime_mode,
|
||||
)
|
||||
compose_str = dict_to_yaml(compose_content)
|
||||
return compose_str
|
||||
|
||||
|
||||
def build_docker_image(
|
||||
runner,
|
||||
set: Callable[[str], None],
|
||||
config: pathlib.Path,
|
||||
config_json: dict,
|
||||
base_image: str | None,
|
||||
api_version: str | None,
|
||||
pull: bool,
|
||||
tag: str,
|
||||
passthrough: Sequence[str] = (),
|
||||
install_command: str | None = None,
|
||||
build_command: str | None = None,
|
||||
docker_command: Sequence[str] | None = None,
|
||||
extra_flags: Sequence[str] = (),
|
||||
verbose: bool = True,
|
||||
):
|
||||
"""Build a Docker image from a LangGraph config."""
|
||||
# pull latest images
|
||||
if pull:
|
||||
runner.run(
|
||||
subp_exec(
|
||||
"docker",
|
||||
"pull",
|
||||
langgraph_cli.config.docker_tag(config_json, base_image, api_version),
|
||||
verbose=verbose,
|
||||
)
|
||||
)
|
||||
set("Building...")
|
||||
# apply options
|
||||
args = [
|
||||
"-f",
|
||||
"-", # stdin
|
||||
"-t",
|
||||
tag,
|
||||
]
|
||||
# determine build context: use current directory for JS projects, config parent for Python
|
||||
is_js_project = config_json.get("node_version") and not config_json.get(
|
||||
"python_version"
|
||||
)
|
||||
# build/install commands only apply to JS projects for now
|
||||
# without install/build command, JS projects will follow the old behavior
|
||||
if is_js_project and (build_command or install_command):
|
||||
build_context = str(pathlib.Path.cwd())
|
||||
else:
|
||||
build_context = str(config.parent)
|
||||
|
||||
# Deep copy to avoid mutating the caller's config (config_to_docker
|
||||
# rewrites graph paths to container-internal paths in place).
|
||||
config_json = copy.deepcopy(config_json)
|
||||
stdin, additional_contexts = langgraph_cli.config.config_to_docker(
|
||||
config_path=config,
|
||||
config=config_json,
|
||||
base_image=base_image,
|
||||
api_version=api_version,
|
||||
install_command=install_command,
|
||||
build_command=build_command,
|
||||
build_context=build_context,
|
||||
)
|
||||
# add additional_contexts
|
||||
if additional_contexts:
|
||||
for k, v in additional_contexts.items():
|
||||
args.extend(["--build-context", f"{k}={v}"])
|
||||
cmd = tuple(docker_command) if docker_command else ("docker", "build")
|
||||
runner.run(
|
||||
subp_exec(
|
||||
*cmd,
|
||||
*args,
|
||||
*extra_flags,
|
||||
*passthrough,
|
||||
build_context,
|
||||
input=stdin,
|
||||
verbose=verbose,
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,174 @@
|
||||
import asyncio
|
||||
import signal
|
||||
import sys
|
||||
from collections.abc import Callable
|
||||
from contextlib import contextmanager
|
||||
from typing import cast
|
||||
|
||||
import click.exceptions
|
||||
|
||||
|
||||
@contextmanager
|
||||
def Runner():
|
||||
if hasattr(asyncio, "Runner"):
|
||||
with asyncio.Runner() as runner:
|
||||
yield runner
|
||||
else:
|
||||
|
||||
class _Runner:
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args):
|
||||
pass
|
||||
|
||||
def run(self, coro):
|
||||
return asyncio.run(coro)
|
||||
|
||||
yield _Runner()
|
||||
|
||||
|
||||
async def subp_exec(
|
||||
cmd: str,
|
||||
*args: str,
|
||||
input: str | None = None,
|
||||
wait: float | None = None,
|
||||
verbose: bool = False,
|
||||
collect: bool = False,
|
||||
on_stdout: Callable[[str], bool | None] | None = None,
|
||||
) -> tuple[str | None, str | None]:
|
||||
if verbose:
|
||||
cmd_str = f"+ {cmd} {' '.join(map(str, args))}"
|
||||
if input:
|
||||
print(cmd_str, " <\n", "\n".join(filter(None, input.splitlines())), sep="")
|
||||
else:
|
||||
print(cmd_str)
|
||||
if wait:
|
||||
await asyncio.sleep(wait)
|
||||
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
cmd,
|
||||
*args,
|
||||
stdin=asyncio.subprocess.PIPE if input else None,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
|
||||
def signal_handler():
|
||||
# make sure process exists, then terminate it
|
||||
if proc.returncode is None:
|
||||
proc.terminate()
|
||||
|
||||
original_sigint_handler = signal.getsignal(signal.SIGINT)
|
||||
if sys.platform == "win32":
|
||||
|
||||
def handle_windows_signal(signum, frame):
|
||||
signal_handler()
|
||||
original_sigint_handler(signum, frame)
|
||||
|
||||
signal.signal(signal.SIGINT, handle_windows_signal)
|
||||
# NOTE: we're not adding a handler for SIGTERM since it's ignored on Windows
|
||||
else:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.add_signal_handler(signal.SIGINT, signal_handler)
|
||||
loop.add_signal_handler(signal.SIGTERM, signal_handler)
|
||||
|
||||
empty_fut: asyncio.Future = asyncio.Future()
|
||||
empty_fut.set_result(None)
|
||||
stdout, stderr, _ = await asyncio.gather(
|
||||
monitor_stream(
|
||||
cast(asyncio.StreamReader, proc.stdout),
|
||||
collect=True,
|
||||
display=verbose,
|
||||
on_line=on_stdout,
|
||||
),
|
||||
monitor_stream(
|
||||
cast(asyncio.StreamReader, proc.stderr),
|
||||
collect=True,
|
||||
display=verbose,
|
||||
),
|
||||
proc._feed_stdin(input.encode()) if input else empty_fut, # type: ignore[attr-defined]
|
||||
)
|
||||
returncode = await proc.wait()
|
||||
if (
|
||||
returncode is not None
|
||||
and returncode != 0 # success
|
||||
and returncode != 130 # user interrupt
|
||||
):
|
||||
sys.stdout.write(stdout.decode() if stdout else "")
|
||||
sys.stderr.write(stderr.decode() if stderr else "")
|
||||
raise click.exceptions.Exit(returncode)
|
||||
if collect:
|
||||
return (
|
||||
stdout.decode() if stdout else None,
|
||||
stderr.decode() if stderr else None,
|
||||
)
|
||||
else:
|
||||
return None, None
|
||||
finally:
|
||||
try:
|
||||
if proc.returncode is None:
|
||||
try:
|
||||
proc.terminate()
|
||||
except (ProcessLookupError, KeyboardInterrupt):
|
||||
pass
|
||||
|
||||
if sys.platform == "win32":
|
||||
signal.signal(signal.SIGINT, original_sigint_handler)
|
||||
else:
|
||||
loop.remove_signal_handler(signal.SIGINT)
|
||||
loop.remove_signal_handler(signal.SIGTERM)
|
||||
except UnboundLocalError:
|
||||
pass
|
||||
|
||||
|
||||
async def monitor_stream(
|
||||
stream: asyncio.StreamReader,
|
||||
collect: bool = False,
|
||||
display: bool = False,
|
||||
on_line: Callable[[str], bool | None] | None = None,
|
||||
) -> bytearray | None:
|
||||
if collect:
|
||||
ba = bytearray()
|
||||
|
||||
def handle(line: bytes, overrun: bool):
|
||||
nonlocal on_line
|
||||
nonlocal display
|
||||
|
||||
if display:
|
||||
sys.stdout.buffer.write(line)
|
||||
if overrun:
|
||||
return
|
||||
if collect:
|
||||
ba.extend(line)
|
||||
if on_line:
|
||||
if on_line(line.decode()):
|
||||
on_line = None
|
||||
display = True
|
||||
|
||||
"""Adapted from asyncio.StreamReader.readline() to handle LimitOverrunError."""
|
||||
sep = b"\n"
|
||||
seplen = len(sep)
|
||||
while True:
|
||||
try:
|
||||
line = await stream.readuntil(sep)
|
||||
overrun = False
|
||||
except asyncio.IncompleteReadError as e:
|
||||
line = e.partial
|
||||
overrun = False
|
||||
except asyncio.LimitOverrunError as e:
|
||||
if stream._buffer.startswith(sep, e.consumed):
|
||||
line = stream._buffer[: e.consumed + seplen]
|
||||
else:
|
||||
line = stream._buffer.clear()
|
||||
overrun = True
|
||||
stream._maybe_resume_transport()
|
||||
await asyncio.to_thread(handle, line, overrun)
|
||||
if line == b"":
|
||||
break
|
||||
|
||||
if collect:
|
||||
return ba
|
||||
else:
|
||||
return None
|
||||
@@ -0,0 +1,205 @@
|
||||
"""HTTP client for LangGraph host backend deployments."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
import httpx
|
||||
|
||||
|
||||
class HostBackendError(click.ClickException):
|
||||
"""Raised when the host backend returns an error response."""
|
||||
|
||||
def __init__(self, message: str, status_code: int | None = None):
|
||||
super().__init__(message)
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
class HostBackendClient:
|
||||
"""Minimal JSON HTTP client for the host backend deployment service."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_url: str,
|
||||
api_key: str,
|
||||
tenant_id: str | None = None,
|
||||
):
|
||||
if not base_url:
|
||||
raise click.UsageError("Host backend URL is required")
|
||||
transport = httpx.HTTPTransport(retries=3)
|
||||
headers: dict[str, str] = {
|
||||
"X-Api-Key": api_key,
|
||||
"Accept": "application/json",
|
||||
}
|
||||
if tenant_id:
|
||||
headers["X-Tenant-ID"] = tenant_id
|
||||
self._base_url = base_url.rstrip("/")
|
||||
self._client = httpx.Client(
|
||||
base_url=self._base_url,
|
||||
headers=headers,
|
||||
transport=transport,
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
def _request(
|
||||
self,
|
||||
method: str,
|
||||
path: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
params: dict[str, Any] | None = None,
|
||||
) -> Any:
|
||||
try:
|
||||
resp = self._client.request(method, path, json=payload, params=params)
|
||||
resp.raise_for_status()
|
||||
except httpx.HTTPStatusError as err:
|
||||
detail = err.response.text or str(err.response.status_code)
|
||||
raise HostBackendError(
|
||||
f"{method} {path} failed with status {err.response.status_code}: {detail}",
|
||||
status_code=err.response.status_code,
|
||||
) from None
|
||||
except httpx.TransportError as err:
|
||||
raise HostBackendError(str(err)) from None
|
||||
|
||||
if not resp.content:
|
||||
return None
|
||||
try:
|
||||
return resp.json()
|
||||
except ValueError as err:
|
||||
raise HostBackendError(
|
||||
f"Failed to decode response from {path}: {err}"
|
||||
) from None
|
||||
|
||||
def create_deployment(
|
||||
self,
|
||||
name: str,
|
||||
deployment_type: str,
|
||||
source: str,
|
||||
config_path: str | None = None,
|
||||
secrets: list[dict[str, str]] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Create a deployment."""
|
||||
payload: dict[str, Any] = {
|
||||
"name": name,
|
||||
"source": source,
|
||||
"source_config": {"deployment_type": deployment_type},
|
||||
"source_revision_config": {},
|
||||
}
|
||||
if source == "internal_source" and config_path:
|
||||
payload["source_revision_config"]["langgraph_config_path"] = config_path
|
||||
if secrets is not None:
|
||||
payload["secrets"] = secrets
|
||||
return self._request("POST", "/v2/deployments", payload)
|
||||
|
||||
def list_deployments(self, name_contains: str = "") -> dict[str, Any]:
|
||||
return self._request(
|
||||
"GET",
|
||||
"/v2/deployments",
|
||||
params={"name_contains": name_contains},
|
||||
)
|
||||
|
||||
def get_deployment(self, deployment_id: str) -> dict[str, Any]:
|
||||
return self._request("GET", f"/v2/deployments/{deployment_id}")
|
||||
|
||||
def delete_deployment(self, deployment_id: str) -> None:
|
||||
return self._request("DELETE", f"/v2/deployments/{deployment_id}")
|
||||
|
||||
def request_push_token(self, deployment_id: str) -> dict[str, Any]:
|
||||
return self._request(
|
||||
"POST",
|
||||
f"/v2/deployments/{deployment_id}/push-token",
|
||||
)
|
||||
|
||||
def request_upload_url(self, deployment_id: str) -> dict[str, Any]:
|
||||
"""Get a signed GCS URL for uploading the source tarball."""
|
||||
return self._request(
|
||||
"POST",
|
||||
f"/v2/deployments/{deployment_id}/upload-url",
|
||||
)
|
||||
|
||||
def update_deployment(
|
||||
self,
|
||||
deployment_id: str,
|
||||
image_uri: str,
|
||||
secrets: list[dict[str, str]] | None = None,
|
||||
tracked_packages: list[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {
|
||||
"revision_source": "internal_docker",
|
||||
"source_revision_config": {"image_uri": image_uri},
|
||||
}
|
||||
if tracked_packages:
|
||||
payload["tracked_packages"] = tracked_packages
|
||||
if secrets is not None:
|
||||
payload["secrets"] = secrets
|
||||
return self._request(
|
||||
"PATCH",
|
||||
f"/v2/deployments/{deployment_id}",
|
||||
payload,
|
||||
)
|
||||
|
||||
def update_deployment_internal_source(
|
||||
self,
|
||||
deployment_id: str,
|
||||
source_tarball_path: str,
|
||||
config_path: str,
|
||||
secrets: list[dict[str, str]] | None = None,
|
||||
install_command: str | None = None,
|
||||
build_command: str | None = None,
|
||||
tracked_packages: list[str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Trigger a remote build revision with the uploaded tarball."""
|
||||
payload: dict[str, Any] = {
|
||||
"revision_source": "internal_source",
|
||||
"source_revision_config": {
|
||||
"source_tarball_path": source_tarball_path,
|
||||
"langgraph_config_path": config_path,
|
||||
},
|
||||
}
|
||||
if tracked_packages:
|
||||
payload["tracked_packages"] = tracked_packages
|
||||
|
||||
source_config: dict[str, Any] = {}
|
||||
if install_command is not None:
|
||||
source_config["install_command"] = install_command
|
||||
if build_command is not None:
|
||||
source_config["build_command"] = build_command
|
||||
if source_config:
|
||||
payload["source_config"] = source_config
|
||||
|
||||
if secrets is not None:
|
||||
payload["secrets"] = secrets
|
||||
return self._request("PATCH", f"/v2/deployments/{deployment_id}", payload)
|
||||
|
||||
def list_revisions(self, deployment_id: str, limit: int = 1) -> dict[str, Any]:
|
||||
return self._request(
|
||||
"GET",
|
||||
f"/v2/deployments/{deployment_id}/revisions?limit={limit}",
|
||||
)
|
||||
|
||||
def get_revision(self, deployment_id: str, revision_id: str) -> dict[str, Any]:
|
||||
return self._request(
|
||||
"GET",
|
||||
f"/v2/deployments/{deployment_id}/revisions/{revision_id}",
|
||||
)
|
||||
|
||||
def get_build_logs(
|
||||
self, project_id: str, revision_id: str, payload: dict[str, Any]
|
||||
) -> Any:
|
||||
return self._request(
|
||||
"POST",
|
||||
f"/v1/projects/{project_id}/revisions/{revision_id}/build_logs",
|
||||
payload,
|
||||
)
|
||||
|
||||
def get_deploy_logs(
|
||||
self,
|
||||
project_id: str,
|
||||
payload: dict[str, Any],
|
||||
revision_id: str | None = None,
|
||||
) -> Any:
|
||||
if revision_id:
|
||||
path = f"/v1/projects/{project_id}/revisions/{revision_id}/deploy_logs"
|
||||
else:
|
||||
path = f"/v1/projects/{project_id}/deploy_logs"
|
||||
return self._request("POST", path, payload)
|
||||
@@ -0,0 +1,107 @@
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from collections.abc import Callable
|
||||
|
||||
|
||||
class Progress:
|
||||
delay: float = 0.1
|
||||
|
||||
@staticmethod
|
||||
def spinning_cursor():
|
||||
while True:
|
||||
yield from "|/-\\"
|
||||
|
||||
def __init__(self, *, message="", elapsed: bool = False, json_mode: bool = False):
|
||||
self.message = message
|
||||
self._base_message = message
|
||||
self._show_elapsed = elapsed
|
||||
self._json_mode = json_mode
|
||||
# use this to make sure we don't kill thread when we set msg to ""
|
||||
self._stop = threading.Event()
|
||||
# signalled when the spinner has no text on screen
|
||||
self._line_clear = threading.Event()
|
||||
self._line_clear.set()
|
||||
self.spinner_generator = self.spinning_cursor()
|
||||
|
||||
def spinner_iteration(self):
|
||||
message = self.message
|
||||
sys.stdout.write(next(self.spinner_generator) + " " + message)
|
||||
sys.stdout.flush()
|
||||
time.sleep(self.delay)
|
||||
# clear the spinner and message
|
||||
sys.stdout.write(
|
||||
"\b" * (len(message) + 2)
|
||||
+ " " * (len(message) + 2)
|
||||
+ "\b" * (len(message) + 2)
|
||||
)
|
||||
sys.stdout.flush()
|
||||
|
||||
def _format_elapsed(self, seconds: float) -> str:
|
||||
mins, secs = divmod(int(seconds), 60)
|
||||
if mins:
|
||||
return f"{self._base_message} ({mins}m {secs:02d}s)"
|
||||
return f"{self._base_message} ({secs}s)"
|
||||
|
||||
def spinner_task(self):
|
||||
start = time.monotonic()
|
||||
while not self._stop.is_set():
|
||||
if not self.message:
|
||||
self._line_clear.set()
|
||||
time.sleep(self.delay)
|
||||
continue
|
||||
if self._show_elapsed:
|
||||
self.message = self._format_elapsed(time.monotonic() - start)
|
||||
message = self.message
|
||||
if not message:
|
||||
self._line_clear.set()
|
||||
continue
|
||||
self._line_clear.clear()
|
||||
sys.stdout.write(next(self.spinner_generator) + " " + message)
|
||||
sys.stdout.flush()
|
||||
time.sleep(self.delay)
|
||||
# clear the spinner and message
|
||||
sys.stdout.write(
|
||||
"\b" * (len(message) + 2)
|
||||
+ " " * (len(message) + 2)
|
||||
+ "\b" * (len(message) + 2)
|
||||
)
|
||||
sys.stdout.flush()
|
||||
self._line_clear.set()
|
||||
|
||||
def __enter__(self) -> Callable[[str], None]:
|
||||
if self._json_mode:
|
||||
return lambda message: None
|
||||
|
||||
if sys.stdout.isatty():
|
||||
self.thread = threading.Thread(target=self.spinner_task)
|
||||
self.thread.start()
|
||||
|
||||
def set_message(message):
|
||||
self.message = message
|
||||
self._base_message = message or self._base_message
|
||||
if not message:
|
||||
self._line_clear.wait(timeout=0.5)
|
||||
|
||||
return set_message
|
||||
else:
|
||||
|
||||
def set_message(message):
|
||||
if message:
|
||||
sys.stderr.write(message + "\n")
|
||||
sys.stderr.flush()
|
||||
|
||||
return set_message
|
||||
|
||||
def __exit__(self, exception, value, tb):
|
||||
if self._json_mode:
|
||||
return
|
||||
if sys.stdout.isatty():
|
||||
self.message = ""
|
||||
self._stop.set()
|
||||
try:
|
||||
self.thread.join()
|
||||
finally:
|
||||
del self.thread
|
||||
if exception is not None:
|
||||
return False
|
||||
@@ -0,0 +1,788 @@
|
||||
from typing import Any, Literal, TypedDict
|
||||
|
||||
from typing_extensions import Required
|
||||
|
||||
Distros = Literal["debian", "wolfi", "bookworm"]
|
||||
MiddlewareOrders = Literal["auth_first", "middleware_first"]
|
||||
|
||||
|
||||
class TTLConfig(TypedDict, total=False):
|
||||
"""Configuration for TTL (time-to-live) behavior in the store."""
|
||||
|
||||
refresh_on_read: bool
|
||||
"""Default behavior for refreshing TTLs on read operations (`GET` and `SEARCH`).
|
||||
|
||||
If `True`, TTLs will be refreshed on read operations (get/search) by default.
|
||||
This can be overridden per-operation by explicitly setting `refresh_ttl`.
|
||||
Defaults to `True` if not configured.
|
||||
"""
|
||||
default_ttl: float | None
|
||||
"""Optional. Default TTL (time-to-live) in minutes for new items.
|
||||
|
||||
If provided, all new items will have this TTL unless explicitly overridden.
|
||||
If omitted, items will have no TTL by default.
|
||||
"""
|
||||
sweep_interval_minutes: int | None
|
||||
"""Optional. Interval in minutes between TTL sweep iterations.
|
||||
|
||||
If provided, the store will periodically delete expired items based on the TTL.
|
||||
If omitted, no automatic sweeping will occur.
|
||||
"""
|
||||
|
||||
|
||||
class IndexConfig(TypedDict, total=False):
|
||||
"""Configuration for indexing documents for semantic search in the store.
|
||||
|
||||
This governs how text is converted into embeddings and stored for vector-based lookups.
|
||||
"""
|
||||
|
||||
dims: int
|
||||
"""Required. Dimensionality of the embedding vectors you will store.
|
||||
|
||||
Must match the output dimension of your selected embedding model or custom embed function.
|
||||
If mismatched, you will likely encounter shape/size errors when inserting or querying vectors.
|
||||
|
||||
Common embedding model output dimensions:
|
||||
- openai:text-embedding-3-large: 3072
|
||||
- openai:text-embedding-3-small: 1536
|
||||
- openai:text-embedding-ada-002: 1536
|
||||
- cohere:embed-english-v3.0: 1024
|
||||
- cohere:embed-english-light-v3.0: 384
|
||||
- cohere:embed-multilingual-v3.0: 1024
|
||||
- cohere:embed-multilingual-light-v3.0: 384
|
||||
"""
|
||||
|
||||
embed: str
|
||||
"""Required. Identifier or reference to the embedding model or a custom embedding function.
|
||||
|
||||
The format can vary:
|
||||
- "<provider>:<model_name>" for recognized providers (e.g., "openai:text-embedding-3-large")
|
||||
- "path/to/module.py:function_name" for your own local embedding function
|
||||
- "my_custom_embed" if it's a known alias in your system
|
||||
|
||||
Examples:
|
||||
- "openai:text-embedding-3-large"
|
||||
- "cohere:embed-multilingual-v3.0"
|
||||
- "src/app.py:embeddings"
|
||||
|
||||
Note: Must return embeddings of dimension `dims`.
|
||||
"""
|
||||
|
||||
fields: list[str] | None
|
||||
"""Optional. List of JSON fields to extract before generating embeddings.
|
||||
|
||||
Defaults to ["$"], which means the entire JSON object is embedded as one piece of text.
|
||||
If you provide multiple fields (e.g. ["title", "content"]), each is extracted and embedded separately,
|
||||
often saving token usage if you only care about certain parts of the data.
|
||||
|
||||
Example:
|
||||
fields=["title", "abstract", "author.biography"]
|
||||
"""
|
||||
|
||||
|
||||
class StoreConfig(TypedDict, total=False):
|
||||
"""Configuration for the built-in long-term memory store.
|
||||
|
||||
This store can optionally perform semantic search. If you omit `index`,
|
||||
the store will just handle traditional (non-embedded) data without vector lookups.
|
||||
"""
|
||||
|
||||
index: IndexConfig | None
|
||||
"""Optional. Defines the vector-based semantic search configuration.
|
||||
|
||||
If provided, the store will:
|
||||
- Generate embeddings according to `index.embed`
|
||||
- Enforce the embedding dimension given by `index.dims`
|
||||
- Embed only specified JSON fields (if any) from `index.fields`
|
||||
|
||||
If omitted, no vector index is initialized.
|
||||
"""
|
||||
|
||||
ttl: TTLConfig | None
|
||||
"""Optional. Defines the TTL (time-to-live) behavior configuration.
|
||||
|
||||
If provided, the store will apply TTL settings according to the configuration.
|
||||
If omitted, no TTL behavior is configured.
|
||||
"""
|
||||
|
||||
|
||||
class ThreadTTLConfig(TypedDict, total=False):
|
||||
"""Configure a default TTL for checkpointed data within threads."""
|
||||
|
||||
strategy: Literal["delete", "keep_latest"]
|
||||
"""Action taken when a thread exceeds its TTL.
|
||||
|
||||
- "delete": Remove the thread and all its data entirely.
|
||||
- "keep_latest": Prune old checkpoints but keep the thread and its latest state.
|
||||
"""
|
||||
default_ttl: float | None
|
||||
"""Default TTL (time-to-live) in minutes for checkpointed data."""
|
||||
sweep_interval_minutes: int | None
|
||||
"""Interval in minutes between sweep iterations.
|
||||
If omitted, a default interval will be used (typically ~ 5 minutes)."""
|
||||
sweep_limit: int | None
|
||||
"""Maximum number of threads to process per sweep iteration. Defaults to 1000."""
|
||||
|
||||
|
||||
class SerdeConfig(TypedDict, total=False):
|
||||
"""Configuration for the built-in serde, which handles checkpointing of state.
|
||||
|
||||
If omitted, no serde is set up (the object store will still be present, however)."""
|
||||
|
||||
allowed_json_modules: list[list[str]] | bool | None
|
||||
"""Optional. List of allowed python modules to de-serialize custom objects from JSON.
|
||||
|
||||
If provided, only the specified modules will be allowed to be deserialized.
|
||||
If omitted, no modules are allowed, and the object returned will simply be a json object OR
|
||||
a deserialized langchain object.
|
||||
|
||||
Example:
|
||||
{...
|
||||
"serde": {
|
||||
"allowed_json_modules": [
|
||||
["my_agent", "my_file", "SomeType"],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
If you set this to True, any module will be allowed to be deserialized.
|
||||
|
||||
Example:
|
||||
{...
|
||||
"serde": {
|
||||
"allowed_json_modules": True
|
||||
}
|
||||
}
|
||||
|
||||
"""
|
||||
allowed_msgpack_modules: list[list[str]] | bool | None
|
||||
"""Optional. List of allowed python modules to de-serialize custom objects from msgpack.
|
||||
|
||||
Known safe types (langgraph.checkpoint.serde.jsonplus.SAFE_MSGPACK_TYPES) are always
|
||||
allowed regardless of this setting. Use this to allowlist your custom Pydantic models,
|
||||
dataclasses, and other user-defined types.
|
||||
|
||||
If True (default), unregistered types will log a warning but still be deserialized.
|
||||
If None, only known safe types will be deserialized; unregistered types will be blocked.
|
||||
|
||||
Example - allowlist specific types (no warnings for these):
|
||||
{...
|
||||
"serde": {
|
||||
"allowed_msgpack_modules": [
|
||||
["my_agent.models", "MyState"],
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
Example - strict mode (only safe types allowed):
|
||||
{...
|
||||
"serde": {
|
||||
"allowed_msgpack_modules": null
|
||||
}
|
||||
}
|
||||
|
||||
"""
|
||||
pickle_fallback: bool
|
||||
"""Optional. Whether to allow pickling as a fallback for deserialization.
|
||||
|
||||
If True, pickling will be allowed as a fallback for deserialization.
|
||||
If False, pickling will not be allowed as a fallback for deserialization.
|
||||
Defaults to True if not configured."""
|
||||
|
||||
|
||||
class CheckpointerConfig(TypedDict, total=False):
|
||||
"""Configuration for the built-in checkpointer, which handles checkpointing of state.
|
||||
|
||||
If omitted, no checkpointer is set up (the object store will still be present, however).
|
||||
"""
|
||||
|
||||
path: str
|
||||
"""Import path to an async context manager that yields a `BaseCheckpointSaver`
|
||||
instance.
|
||||
|
||||
The referenced object should be an `@asynccontextmanager`-decorated function
|
||||
so that the server can properly manage the checkpointer's lifecycle (e.g.
|
||||
opening and closing connections).
|
||||
|
||||
Examples:
|
||||
- "./my_checkpointer.py:create_checkpointer"
|
||||
- "my_package.checkpointer:create_checkpointer"
|
||||
|
||||
When provided, this replaces the default checkpointer.
|
||||
|
||||
You can use the `langgraph-checkpoint-conformance` package
|
||||
(https://pypi.org/project/langgraph-checkpoint-conformance/) to run simple
|
||||
conformance tests against your custom checkpointer and catch
|
||||
incompatibilities early.
|
||||
"""
|
||||
|
||||
ttl: ThreadTTLConfig | None
|
||||
"""Optional. Defines the TTL (time-to-live) behavior configuration.
|
||||
|
||||
If provided, the checkpointer will apply TTL settings according to the configuration.
|
||||
If omitted, no TTL behavior is configured.
|
||||
"""
|
||||
serde: SerdeConfig | None
|
||||
"""Optional. Defines the serde configuration.
|
||||
|
||||
If provided, the checkpointer will apply serde settings according to the configuration.
|
||||
If omitted, no serde behavior is configured.
|
||||
|
||||
This configuration requires server version 0.5 or later to take effect.
|
||||
"""
|
||||
|
||||
|
||||
class SecurityConfig(TypedDict, total=False):
|
||||
"""Configuration for OpenAPI security definitions and requirements.
|
||||
|
||||
Useful for specifying global or path-level authentication and authorization flows
|
||||
(e.g., OAuth2, API key headers, etc.).
|
||||
"""
|
||||
|
||||
securitySchemes: dict[str, dict[str, Any]]
|
||||
"""Describe each security scheme recognized by your OpenAPI spec.
|
||||
|
||||
Keys are scheme names (e.g. "OAuth2", "ApiKeyAuth") and values are their definitions.
|
||||
Example:
|
||||
{
|
||||
"OAuth2": {
|
||||
"type": "oauth2",
|
||||
"flows": {
|
||||
"password": {
|
||||
"tokenUrl": "/token",
|
||||
"scopes": {"read": "Read data", "write": "Write data"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
security: list[dict[str, list[str]]]
|
||||
"""Global security requirements across all endpoints.
|
||||
|
||||
Each element in the list maps a security scheme (e.g. "OAuth2") to a list of scopes (e.g. ["read", "write"]).
|
||||
Example:
|
||||
[
|
||||
{"OAuth2": ["read", "write"]},
|
||||
{"ApiKeyAuth": []}
|
||||
]
|
||||
"""
|
||||
# path => {method => security}
|
||||
paths: dict[str, dict[str, list[dict[str, list[str]]]]]
|
||||
"""Path-specific security overrides.
|
||||
|
||||
Keys are path templates (e.g., "/items/{item_id}"), mapping to:
|
||||
- Keys that are HTTP methods (e.g., "GET", "POST"),
|
||||
- Values are lists of security definitions (just like `security`) for that method.
|
||||
|
||||
Example:
|
||||
{
|
||||
"/private_data": {
|
||||
"GET": [{"OAuth2": ["read"]}],
|
||||
"POST": [{"OAuth2": ["write"]}]
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class CacheConfig(TypedDict, total=False):
|
||||
cache_keys: list[str]
|
||||
"""Optional. List of header keys to use for caching.
|
||||
|
||||
Example:
|
||||
["user_id", "workspace_id"]
|
||||
"""
|
||||
ttl_seconds: int
|
||||
"""Optional. Time-to-live in seconds for cached items.
|
||||
|
||||
Example:
|
||||
3600
|
||||
"""
|
||||
max_size: int
|
||||
"""Optional. Maximum size of the cache.
|
||||
|
||||
Example:
|
||||
100
|
||||
"""
|
||||
|
||||
|
||||
class AuthConfig(TypedDict, total=False):
|
||||
"""Configuration for custom authentication logic and how it integrates into the OpenAPI spec."""
|
||||
|
||||
path: str
|
||||
"""Required. Path to an instance of the Auth() class that implements custom authentication.
|
||||
|
||||
Format: "path/to/file.py:my_auth"
|
||||
"""
|
||||
disable_studio_auth: bool
|
||||
"""Optional. Whether to disable LangSmith API-key authentication for requests originating the Studio.
|
||||
|
||||
Defaults to False, meaning that if a particular header is set, the server will verify the `x-api-key` header
|
||||
value is a valid API key for the deployment's workspace. If `True`, all requests will go through your custom
|
||||
authentication logic, regardless of origin of the request.
|
||||
"""
|
||||
openapi: SecurityConfig
|
||||
"""The security configuration to include in your server's OpenAPI spec.
|
||||
|
||||
Example (OAuth2):
|
||||
{
|
||||
"securitySchemes": {
|
||||
"OAuth2": {
|
||||
"type": "oauth2",
|
||||
"flows": {
|
||||
"password": {
|
||||
"tokenUrl": "/token",
|
||||
"scopes": {"me": "Read user info", "items": "Manage items"}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"security": [
|
||||
{"OAuth2": ["me"]}
|
||||
]
|
||||
}
|
||||
"""
|
||||
cache: CacheConfig
|
||||
"""Optional. Cache configuration for the server.
|
||||
|
||||
Example:
|
||||
{
|
||||
"cache_keys": ["user_id", "workspace_id"],
|
||||
"ttl_seconds": 3600,
|
||||
"max_size": 100
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class EncryptionConfig(TypedDict, total=False):
|
||||
"""Configuration for custom at-rest encryption logic.
|
||||
|
||||
Allows you to implement custom encryption for sensitive data stored in the database,
|
||||
including metadata fields and checkpoint blobs."""
|
||||
|
||||
path: str
|
||||
"""Required. Path to an instance of the Encryption() class that implements custom encryption handlers.
|
||||
|
||||
Format: "path/to/file.py:my_encryption"
|
||||
|
||||
Example:
|
||||
{
|
||||
"encryption": {
|
||||
"path": "./encryption.py:my_encryption"
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
class CorsConfig(TypedDict, total=False):
|
||||
"""Specifies Cross-Origin Resource Sharing (CORS) rules for your server.
|
||||
|
||||
If omitted, defaults are typically very restrictive (often no cross-origin requests).
|
||||
Configure carefully if you want to allow usage from browsers hosted on other domains.
|
||||
"""
|
||||
|
||||
allow_origins: list[str]
|
||||
"""Optional. List of allowed origins (e.g., "https://example.com").
|
||||
|
||||
Default is often an empty list (no external origins).
|
||||
Use "*" only if you trust all origins, as that bypasses most restrictions.
|
||||
"""
|
||||
allow_methods: list[str]
|
||||
"""Optional. HTTP methods permitted for cross-origin requests (e.g. ["GET", "POST"]).
|
||||
|
||||
Default might be ["GET", "POST", "OPTIONS"] depending on your server framework.
|
||||
"""
|
||||
allow_headers: list[str]
|
||||
"""Optional. HTTP headers that can be used in cross-origin requests (e.g. ["Content-Type", "Authorization"])."""
|
||||
allow_credentials: bool
|
||||
"""Optional. If `True`, cross-origin requests can include credentials (cookies, auth headers).
|
||||
|
||||
Default False to avoid accidentally exposing secured endpoints to untrusted sites.
|
||||
"""
|
||||
allow_origin_regex: str
|
||||
"""Optional. A regex pattern for matching allowed origins, used if you have dynamic subdomains.
|
||||
|
||||
Example: "^https://.*\\.mycompany\\.com$"
|
||||
"""
|
||||
expose_headers: list[str]
|
||||
"""Optional. List of headers that browsers are allowed to read from the response in cross-origin contexts."""
|
||||
max_age: int
|
||||
"""Optional. How many seconds the browser may cache preflight responses.
|
||||
|
||||
Default might be 600 (10 minutes). Larger values reduce preflight requests but can cause stale configurations.
|
||||
"""
|
||||
|
||||
|
||||
class ConfigurableHeaderConfig(TypedDict, total=False):
|
||||
"""Customize which headers to include as configurable values in your runs.
|
||||
|
||||
By default, omits x-api-key, x-tenant-id, and x-service-key.
|
||||
|
||||
Exclusions (if provided) take precedence.
|
||||
|
||||
Each value can be a raw string with an optional wildcard.
|
||||
"""
|
||||
|
||||
includes: list[str] | None
|
||||
"""Headers to include (if not also matched against an 'excludes' pattern).
|
||||
|
||||
Examples:
|
||||
- 'user-agent'
|
||||
- 'x-configurable-*'
|
||||
"""
|
||||
excludes: list[str] | None
|
||||
"""Headers to exclude. Applied before the 'includes' checks.
|
||||
|
||||
Examples:
|
||||
- 'x-api-key'
|
||||
- '*key*'
|
||||
- '*token*'
|
||||
"""
|
||||
|
||||
|
||||
class HttpConfig(TypedDict, total=False):
|
||||
"""Configuration for the built-in HTTP server that powers your deployment's routes and endpoints."""
|
||||
|
||||
app: str
|
||||
"""Optional. Import path to a custom Starlette/FastAPI application to mount.
|
||||
|
||||
Format: "path/to/module.py:app_var"
|
||||
If provided, it can override or extend the default routes.
|
||||
"""
|
||||
disable_assistants: bool
|
||||
"""Optional. If `True`, /assistants routes are removed from the server.
|
||||
|
||||
Default is False (meaning /assistants is enabled).
|
||||
"""
|
||||
disable_threads: bool
|
||||
"""Optional. If `True`, /threads routes are removed.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_runs: bool
|
||||
"""Optional. If `True`, /runs routes are removed.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_store: bool
|
||||
"""Optional. If `True`, /store routes are removed, disabling direct store interactions via HTTP.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_mcp: bool
|
||||
"""Optional. If `True`, /mcp routes are removed, disabling default support to expose the deployment as an MCP server.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_a2a: bool
|
||||
"""Optional. If `True`, /a2a routes are removed, disabling default support to expose the deployment as an agent-to-agent (A2A) server.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_meta: bool
|
||||
"""Optional. Remove meta endpoints.
|
||||
|
||||
Set to True to disable the following endpoints: /openapi.json, /info, /metrics, /docs.
|
||||
This will also make the /ok endpoint skip any DB or other checks, always returning {"ok": True}.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_ui: bool
|
||||
"""Optional. If `True`, /ui routes are removed, disabling the UI server.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
disable_webhooks: bool
|
||||
"""Optional. If `True`, webhooks are disabled. Runs created with an associated webhook will
|
||||
still be executed, but the webhook event will not be sent.
|
||||
|
||||
Default is False.
|
||||
"""
|
||||
cors: CorsConfig | None
|
||||
"""Optional. Defines CORS restrictions. If omitted, no special rules are set and
|
||||
cross-origin behavior depends on default server settings.
|
||||
"""
|
||||
configurable_headers: ConfigurableHeaderConfig | None
|
||||
"""Optional. Defines how headers are treated for a run's configuration.
|
||||
|
||||
You can include or exclude headers as configurable values to condition your
|
||||
agent's behavior or permissions on a request's headers."""
|
||||
logging_headers: ConfigurableHeaderConfig | None
|
||||
"""Optional. Defines which headers are excluded from logging."""
|
||||
middleware_order: MiddlewareOrders | None
|
||||
"""Optional. Defines the order in which to apply server customizations.
|
||||
|
||||
Choices:
|
||||
- "auth_first": Authentication hooks (custom or default) are evaluated
|
||||
before custom middleware.
|
||||
- "middleware_first": Custom middleware is evaluated
|
||||
before authentication hooks (custom or default).
|
||||
|
||||
Default is `middleware_first`.
|
||||
"""
|
||||
enable_custom_route_auth: bool
|
||||
"""Optional. If `True`, authentication is enabled for custom routes,
|
||||
not just the routes that are protected by default.
|
||||
(Routes protected by default include /assistants, /threads, and /runs).
|
||||
|
||||
Default is False. This flag only affects authentication behavior
|
||||
if `app` is provided and contains custom routes.
|
||||
"""
|
||||
mount_prefix: str
|
||||
"""Optional. URL prefix to prepend to all the routes.
|
||||
|
||||
Example:
|
||||
"/api"
|
||||
"""
|
||||
|
||||
|
||||
class WebhookUrlPolicy(TypedDict, total=False):
|
||||
require_https: bool
|
||||
"""Enforce HTTPS scheme for absolute URLs; reject `http://` when true."""
|
||||
allowed_domains: list[str]
|
||||
"""Hostname allowlist. Supports exact hosts and wildcard subdomains.
|
||||
|
||||
Use entries like "hooks.example.com" or "*.mycorp.com". The wildcard only
|
||||
matches subdomains ("foo.mycorp.com"), not the apex ("mycorp.com"). When
|
||||
empty or omitted, any public host is allowed (subject to SSRF IP checks).
|
||||
"""
|
||||
allowed_ports: list[int]
|
||||
"""Explicit port allowlist for absolute URLs.
|
||||
|
||||
If set, requests must use one of these ports. Defaults are respected when
|
||||
a port is not present in the URL (443 for https, 80 for http).
|
||||
"""
|
||||
max_url_length: int
|
||||
"""Maximum permitted URL length in characters; longer inputs are rejected early."""
|
||||
disable_loopback: bool
|
||||
"""Disallow relative URLs (internal loopback calls) when true."""
|
||||
|
||||
|
||||
class GraphDef(TypedDict, total=False):
|
||||
"""Definition of a graph with additional metadata."""
|
||||
|
||||
path: str
|
||||
"""Required. Import path to the graph object.
|
||||
|
||||
Format: "path/to/file.py:object_name"
|
||||
"""
|
||||
description: str | None
|
||||
"""Optional. A description of the graph's purpose and functionality.
|
||||
|
||||
This description is surfaced in the API and can help users understand what the graph does.
|
||||
"""
|
||||
|
||||
|
||||
class WebhooksConfig(TypedDict, total=False):
|
||||
env_prefix: str
|
||||
"""Required prefix for environment variables referenced in header templates.
|
||||
|
||||
Acts as an allowlist boundary to prevent leaking arbitrary environment
|
||||
variables. Defaults to "LG_WEBHOOK_" when omitted.
|
||||
"""
|
||||
url: WebhookUrlPolicy
|
||||
"""URL validation policy for user-supplied webhook endpoints."""
|
||||
headers: dict[str, str]
|
||||
"""Static headers to include with webhook requests.
|
||||
|
||||
Values may contain templates of the form "${{ env.VAR }}". On startup, these
|
||||
are resolved via the process environment after verifying `VAR` starts with
|
||||
`env_prefix`. Mixed literals and multiple templates are allowed.
|
||||
"""
|
||||
|
||||
|
||||
class UvSource(TypedDict, total=False):
|
||||
"""Deployment source rooted at a uv project or workspace."""
|
||||
|
||||
kind: Required[Literal["uv"]]
|
||||
"""Discriminator for uv-backed deployment mode."""
|
||||
|
||||
root: str
|
||||
"""Relative path from langgraph.json to the authoritative uv project root.
|
||||
|
||||
The resolved directory must contain `pyproject.toml` and `uv.lock`. If the
|
||||
root is a workspace, package discovery happens within this root.
|
||||
"""
|
||||
|
||||
package: str
|
||||
"""Optional. Workspace package name to deploy when the target is ambiguous.
|
||||
|
||||
If omitted, the CLI tries to infer the target package from the location of
|
||||
`langgraph.json`, or falls back to the only package if the root contains
|
||||
exactly one candidate.
|
||||
"""
|
||||
|
||||
|
||||
class Config(TypedDict, total=False):
|
||||
"""Top-level config for langgraph-cli or similar deployment tooling."""
|
||||
|
||||
python_version: str
|
||||
"""Optional. Python version in 'major.minor' format (e.g. '3.11').
|
||||
Must be at least 3.11 or greater for this deployment to function properly.
|
||||
"""
|
||||
|
||||
node_version: str | None
|
||||
"""Optional. Node.js version as a major version (e.g. '20'), if your deployment needs Node.
|
||||
Must be >= 20 if provided.
|
||||
"""
|
||||
|
||||
api_version: str | None
|
||||
"""Optional. Which semantic version of the LangGraph API server to use.
|
||||
|
||||
Defaults to latest. Check the
|
||||
[changelog](https://docs.langchain.com/langgraph-platform/langgraph-server-changelog)
|
||||
for more information."""
|
||||
|
||||
_INTERNAL_docker_tag: str | None
|
||||
"""Optional. Internal use only.
|
||||
"""
|
||||
|
||||
base_image: str | None
|
||||
"""Optional. Base image to use for the LangGraph API server.
|
||||
|
||||
Defaults to langchain/langgraph-api or langchain/langgraphjs-api."""
|
||||
|
||||
image_distro: Distros | None
|
||||
"""Optional. Linux distribution for the base image.
|
||||
|
||||
Must be one of 'wolfi', 'debian', or 'bookworm'.
|
||||
If omitted, defaults to 'debian' ('latest').
|
||||
"""
|
||||
|
||||
pip_config_file: str | None
|
||||
"""Optional. Path to a pip config file (e.g., "/etc/pip.conf" or "pip.ini") for controlling
|
||||
package installation (custom indices, credentials, etc.).
|
||||
|
||||
Only relevant if Python dependencies are installed via pip. If omitted, default pip settings are used.
|
||||
"""
|
||||
|
||||
pip_installer: str | None
|
||||
"""Optional. Python package installer to use ('auto', 'pip', or 'uv').
|
||||
|
||||
- 'auto' (default): Use uv for supported base images, otherwise pip
|
||||
- 'pip': Force use of pip regardless of base image support
|
||||
- 'uv': Force use of uv (will fail if base image doesn't support it)
|
||||
"""
|
||||
|
||||
source: UvSource | None
|
||||
"""Optional. Explicit deployment source configuration.
|
||||
|
||||
Use `{ "kind": "uv", "root": "." }` to deploy from a uv project rooted at
|
||||
`root/pyproject.toml` and `root/uv.lock`. If `root` is a workspace and the
|
||||
target is ambiguous, set `package` to the desired workspace member.
|
||||
"""
|
||||
|
||||
dockerfile_lines: list[str]
|
||||
"""Optional. Additional Docker instructions that will be appended to your base Dockerfile.
|
||||
|
||||
Useful for installing OS packages, setting environment variables, etc.
|
||||
Example:
|
||||
dockerfile_lines=[
|
||||
"RUN apt-get update && apt-get install -y libmagic-dev",
|
||||
"ENV MY_CUSTOM_VAR=hello_world"
|
||||
]
|
||||
"""
|
||||
|
||||
dependencies: list[str]
|
||||
"""List of Python dependencies to install, either from PyPI or local paths.
|
||||
|
||||
Examples:
|
||||
- "." or "./src" if you have a local Python package
|
||||
- str (aka "anthropic") for a PyPI package
|
||||
- "git+https://github.com/org/repo.git@main" for a Git-based package
|
||||
Defaults to an empty list, meaning no additional packages installed beyond your base environment.
|
||||
|
||||
This field is not supported when `source.kind` is `uv`.
|
||||
"""
|
||||
|
||||
graphs: dict[str, str | GraphDef]
|
||||
"""Optional. Named definitions of graphs, each pointing to a Python object.
|
||||
|
||||
|
||||
Graphs can be StateGraph, @entrypoint, or any other Pregel object OR they can point to (async) context
|
||||
managers that accept a single configuration argument (of type RunnableConfig) and return a pregel object
|
||||
(instance of Stategraph, etc.).
|
||||
|
||||
Keys are graph names, values are either "path/to/file.py:object_name" strings
|
||||
or objects with a "path" key and optional "description" key.
|
||||
Example:
|
||||
{
|
||||
"mygraph": "graphs/my_graph.py:graph_definition",
|
||||
"anothergraph": {
|
||||
"path": "graphs/another.py:get_graph",
|
||||
"description": "A graph that does X"
|
||||
}
|
||||
}
|
||||
"""
|
||||
|
||||
env: dict[str, str] | str
|
||||
"""Optional. Environment variables to set for your deployment.
|
||||
|
||||
- If given as a dict, keys are variable names and values are their values.
|
||||
- If given as a string, it must be a path to a file containing lines in KEY=VALUE format.
|
||||
|
||||
Example as a dict:
|
||||
env={"API_TOKEN": "abc123", "DEBUG": "true"}
|
||||
Example as a file path:
|
||||
env=".env"
|
||||
"""
|
||||
|
||||
store: StoreConfig | None
|
||||
"""Optional. Configuration for the built-in long-term memory store, including semantic search indexing.
|
||||
|
||||
If omitted, no vector index is set up (the object store will still be present, however).
|
||||
"""
|
||||
|
||||
checkpointer: CheckpointerConfig | None
|
||||
"""Optional. Configuration for the built-in checkpointer, which handles checkpointing of state.
|
||||
|
||||
If omitted, no checkpointer is set up (the object store will still be present, however).
|
||||
"""
|
||||
|
||||
auth: AuthConfig | None
|
||||
"""Optional. Custom authentication config, including the path to your Python auth logic and
|
||||
the OpenAPI security definitions it uses.
|
||||
"""
|
||||
|
||||
encryption: EncryptionConfig | None
|
||||
"""Optional. Custom at-rest encryption config, including the path to your Python encryption logic.
|
||||
|
||||
Allows you to implement custom encryption for sensitive data stored in the database.
|
||||
"""
|
||||
|
||||
http: HttpConfig | None
|
||||
"""Optional. Configuration for the built-in HTTP server, controlling which custom routes are exposed
|
||||
and how cross-origin requests are handled.
|
||||
"""
|
||||
|
||||
webhooks: WebhooksConfig | None
|
||||
"""Optional. Webhooks configuration for outbound event delivery.
|
||||
|
||||
Forwarded into the container as `LANGGRAPH_WEBHOOKS`. See `WebhooksConfig`
|
||||
for URL policy and header templating details.
|
||||
"""
|
||||
|
||||
ui: dict[str, str] | None
|
||||
"""Optional. Named definitions of UI components emitted by the agent, each pointing to a JS/TS file.
|
||||
"""
|
||||
|
||||
keep_pkg_tools: bool | list[str] | None
|
||||
"""Optional. Control whether to retain Python packaging tools in the final image.
|
||||
|
||||
Allowed tools are: "pip", "setuptools", "wheel".
|
||||
You can also set to true to include all packaging tools.
|
||||
"""
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Config",
|
||||
"GraphDef",
|
||||
"StoreConfig",
|
||||
"CheckpointerConfig",
|
||||
"AuthConfig",
|
||||
"EncryptionConfig",
|
||||
"HttpConfig",
|
||||
"MiddlewareOrders",
|
||||
"Distros",
|
||||
"TTLConfig",
|
||||
"IndexConfig",
|
||||
]
|
||||
@@ -0,0 +1,186 @@
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from io import BytesIO
|
||||
from urllib import error, request
|
||||
from zipfile import ZipFile
|
||||
|
||||
import click
|
||||
|
||||
TEMPLATES: dict[str, dict[str, str]] = {
|
||||
"Deep Agent": {
|
||||
"description": "An opinionated deployment template for a Deep Agent.",
|
||||
"python": "https://github.com/langchain-ai/deep-agent-template/archive/refs/heads/main.zip",
|
||||
"js": "https://github.com/langchain-ai/deep-agent-template-js/archive/refs/heads/main.zip",
|
||||
},
|
||||
"Agent": {
|
||||
"description": "A simple agent that can be flexibly extended to many tools.",
|
||||
"python": "https://github.com/langchain-ai/simple-agent-template/archive/refs/heads/main.zip",
|
||||
},
|
||||
"New LangGraph Project": {
|
||||
"description": "A simple, minimal chatbot with memory.",
|
||||
"python": "https://github.com/langchain-ai/new-langgraph-project/archive/refs/heads/main.zip",
|
||||
"js": "https://github.com/langchain-ai/new-langgraphjs-project/archive/refs/heads/main.zip",
|
||||
},
|
||||
}
|
||||
|
||||
# Generate TEMPLATE_IDS programmatically
|
||||
TEMPLATE_ID_TO_CONFIG = {
|
||||
f"{name.lower().replace(' ', '-')}-{lang}": (name, lang, url)
|
||||
for name, versions in TEMPLATES.items()
|
||||
for lang, url in versions.items()
|
||||
if lang in {"python", "js"}
|
||||
}
|
||||
|
||||
TEMPLATE_IDS = list(TEMPLATE_ID_TO_CONFIG.keys())
|
||||
|
||||
TEMPLATE_HELP_STRING = (
|
||||
"The name of the template to use. Available options:\n"
|
||||
+ "\n".join(f"{id_}" for id_ in TEMPLATE_ID_TO_CONFIG)
|
||||
)
|
||||
|
||||
|
||||
def _choose_template() -> str:
|
||||
"""Presents a list of templates to the user and prompts them to select one.
|
||||
|
||||
Returns:
|
||||
str: The URL of the selected template.
|
||||
"""
|
||||
click.secho("🌟 Please select a template:", bold=True, fg="yellow")
|
||||
for idx, (template_name, template_info) in enumerate(TEMPLATES.items(), 1):
|
||||
click.secho(f"{idx}. ", nl=False, fg="cyan")
|
||||
click.secho(template_name, fg="cyan", nl=False)
|
||||
click.secho(f" - {template_info['description']}", fg="white")
|
||||
|
||||
# Get the template choice from the user, defaulting to the first template if blank
|
||||
template_choice: int | None = click.prompt(
|
||||
"Enter the number of your template choice (default is 1)",
|
||||
type=int,
|
||||
default=1,
|
||||
show_default=False,
|
||||
)
|
||||
|
||||
template_keys = list(TEMPLATES.keys())
|
||||
if 1 <= template_choice <= len(template_keys):
|
||||
selected_template: str = template_keys[template_choice - 1]
|
||||
else:
|
||||
click.secho("❌ Invalid choice. Please try again.", fg="red")
|
||||
return _choose_template()
|
||||
|
||||
template_info = TEMPLATES[selected_template]
|
||||
available_langs = [lang for lang in ("python", "js") if lang in template_info]
|
||||
|
||||
click.secho(
|
||||
f"\nYou selected: {selected_template} - {template_info['description']}",
|
||||
fg="green",
|
||||
)
|
||||
|
||||
if len(available_langs) == 1:
|
||||
return template_info[available_langs[0]]
|
||||
|
||||
version_choice: int = click.prompt(
|
||||
"Choose language (1 for Python 🐍, 2 for JS/TS 🌐)", type=int
|
||||
)
|
||||
|
||||
if version_choice == 1:
|
||||
return template_info["python"]
|
||||
elif version_choice == 2:
|
||||
return template_info["js"]
|
||||
else:
|
||||
click.secho("❌ Invalid choice. Please try again.", fg="red")
|
||||
return _choose_template()
|
||||
|
||||
|
||||
def _download_repo_with_requests(repo_url: str, path: str) -> None:
|
||||
"""Download a ZIP archive from the given URL and extracts it to the specified path.
|
||||
|
||||
Args:
|
||||
repo_url: The URL of the repository to download.
|
||||
path: The path where the repository should be extracted.
|
||||
"""
|
||||
click.secho("📥 Attempting to download repository as a ZIP archive...", fg="yellow")
|
||||
click.secho(f"URL: {repo_url}", fg="yellow")
|
||||
try:
|
||||
with request.urlopen(repo_url) as response:
|
||||
if response.status == 200:
|
||||
with ZipFile(BytesIO(response.read())) as zip_file:
|
||||
zip_file.extractall(path)
|
||||
# Move extracted contents to path
|
||||
for item in os.listdir(path):
|
||||
if item.endswith("-main"):
|
||||
extracted_dir = os.path.join(path, item)
|
||||
for filename in os.listdir(extracted_dir):
|
||||
shutil.move(os.path.join(extracted_dir, filename), path)
|
||||
shutil.rmtree(extracted_dir)
|
||||
click.secho(
|
||||
f"✅ Downloaded and extracted repository to {path}", fg="green"
|
||||
)
|
||||
except error.HTTPError as e:
|
||||
click.secho(
|
||||
f"❌ Error: Failed to download repository.\nDetails: {e}\n",
|
||||
fg="red",
|
||||
bold=True,
|
||||
err=True,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def create_new(path: str | None, template: str | None) -> None:
|
||||
"""Create a new LangGraph project at the specified PATH using the chosen TEMPLATE.
|
||||
|
||||
Args:
|
||||
path: The path where the new project will be created.
|
||||
template: The name of the template to use.
|
||||
"""
|
||||
# Prompt for path if not provided
|
||||
if not path:
|
||||
path = click.prompt(
|
||||
"📂 Please specify the path to create the application", default="."
|
||||
)
|
||||
|
||||
path = os.path.abspath(path) # Ensure path is absolute
|
||||
|
||||
# Check if path exists and is not empty
|
||||
if os.path.exists(path) and os.listdir(path):
|
||||
click.secho(
|
||||
"❌ The specified directory already exists and is not empty. "
|
||||
"Aborting to prevent overwriting files.",
|
||||
fg="red",
|
||||
bold=True,
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
# Get template URL either from command-line argument or
|
||||
# through interactive selection
|
||||
if template:
|
||||
if template not in TEMPLATE_ID_TO_CONFIG:
|
||||
# Format available options in a readable way with descriptions
|
||||
template_options = ""
|
||||
for id_ in TEMPLATE_IDS:
|
||||
name, lang, _ = TEMPLATE_ID_TO_CONFIG[id_]
|
||||
description = TEMPLATES[name]["description"]
|
||||
|
||||
# Add each template option with color formatting
|
||||
template_options += (
|
||||
click.style("- ", fg="yellow", bold=True)
|
||||
+ click.style(f"{id_}", fg="cyan")
|
||||
+ click.style(f": {description}", fg="white")
|
||||
+ "\n"
|
||||
)
|
||||
|
||||
# Display error message with colors and formatting
|
||||
click.secho("❌ Error:", fg="red", bold=True, nl=False)
|
||||
click.secho(f" Template '{template}' not found.", fg="red")
|
||||
click.secho(
|
||||
"Please select from the available options:\n", fg="yellow", bold=True
|
||||
)
|
||||
click.secho(template_options, fg="cyan")
|
||||
sys.exit(1)
|
||||
_, _, template_url = TEMPLATE_ID_TO_CONFIG[template]
|
||||
else:
|
||||
template_url = _choose_template()
|
||||
|
||||
# Download and extract the template
|
||||
_download_repo_with_requests(template_url, path)
|
||||
|
||||
click.secho(f"🎉 New project created at {path}", fg="green", bold=True)
|
||||
@@ -0,0 +1,50 @@
|
||||
"""General-purpose utilities shared across the LangGraph CLI."""
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
import click
|
||||
|
||||
|
||||
def clean_empty_lines(input_str: str):
|
||||
return "\n".join(filter(None, input_str.splitlines()))
|
||||
|
||||
|
||||
def warn_non_wolfi_distro(
|
||||
config_json: dict,
|
||||
*,
|
||||
emit: Callable[[str], None] | None = None,
|
||||
) -> None:
|
||||
"""Show warning if image_distro is not set to 'wolfi'.
|
||||
|
||||
When ``emit`` is provided, each warning line is sent through it (used by
|
||||
callers that need JSON-aware output). Otherwise falls back to colored
|
||||
``click.secho`` output.
|
||||
"""
|
||||
image_distro = config_json.get("image_distro", "debian") # Default is debian
|
||||
if image_distro == "wolfi":
|
||||
return
|
||||
if emit is not None:
|
||||
emit(
|
||||
"⚠️ Security Recommendation: Consider switching to Wolfi Linux for enhanced security."
|
||||
)
|
||||
emit(
|
||||
" Wolfi is a security-oriented, minimal Linux distribution designed for containers."
|
||||
)
|
||||
emit(
|
||||
' To switch, add \'"image_distro": "wolfi"\' to your langgraph.json config file.'
|
||||
)
|
||||
return
|
||||
click.secho(
|
||||
"⚠️ Security Recommendation: Consider switching to Wolfi Linux for enhanced security.",
|
||||
fg="yellow",
|
||||
bold=True,
|
||||
)
|
||||
click.secho(
|
||||
" Wolfi is a security-oriented, minimal Linux distribution designed for containers.",
|
||||
fg="yellow",
|
||||
)
|
||||
click.secho(
|
||||
' To switch, add \'"image_distro": "wolfi"\' to your langgraph.json config file.',
|
||||
fg="yellow",
|
||||
)
|
||||
click.secho("") # Empty line for better readability
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,10 @@
|
||||
"""Main entrypoint into package."""
|
||||
|
||||
from importlib import metadata
|
||||
|
||||
try:
|
||||
__version__ = metadata.version(__package__)
|
||||
except metadata.PackageNotFoundError:
|
||||
# Case where package metadata is not available.
|
||||
__version__ = ""
|
||||
del metadata # optional, avoids polluting the results of dir(__package__)
|
||||
@@ -0,0 +1,92 @@
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "langgraph-cli"
|
||||
dynamic = ["version"]
|
||||
description = "CLI for interacting with LangGraph API"
|
||||
authors = []
|
||||
requires-python = ">=3.10"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
license-files = ['LICENSE']
|
||||
dependencies = [
|
||||
"click>=8.1.7",
|
||||
"httpx>=0.24.0",
|
||||
"langgraph-sdk>=0.1.0 ; python_version >= '3.11'",
|
||||
"pathspec>=0.11.0",
|
||||
"python-dotenv>=0.8.0",
|
||||
"tomli>=2.0.1 ; python_version < '3.11'",
|
||||
]
|
||||
[tool.hatch.version]
|
||||
path = "langgraph_cli/__init__.py"
|
||||
[project.optional-dependencies]
|
||||
inmem = [
|
||||
"langgraph-api>=0.5.35,<1.0.0 ; python_version >= '3.11'",
|
||||
"langgraph-runtime-inmem>=0.7 ; python_version >= '3.11'",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Source = "https://github.com/langchain-ai/langgraph/tree/main/libs/cli"
|
||||
Twitter = "https://x.com/langchain_oss"
|
||||
Slack = "https://www.langchain.com/join-community"
|
||||
Reddit = "https://www.reddit.com/r/LangChain/"
|
||||
|
||||
[project.scripts]
|
||||
langgraph = "langgraph_cli.cli:cli"
|
||||
|
||||
[dependency-groups]
|
||||
test = [
|
||||
"pytest",
|
||||
"pytest-asyncio",
|
||||
"pytest-mock",
|
||||
"pytest-watch",
|
||||
"msgspec",
|
||||
]
|
||||
lint = [
|
||||
"ruff",
|
||||
"codespell",
|
||||
"ty",
|
||||
]
|
||||
dev = [
|
||||
{include-group = "test"},
|
||||
{include-group = "lint"},
|
||||
"hatch>=1.16.2",
|
||||
]
|
||||
|
||||
[tool.uv]
|
||||
default-groups = ['dev']
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
include = ["langgraph_cli"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
addopts = "--strict-markers --strict-config --durations=5 -vv"
|
||||
asyncio_mode = "auto"
|
||||
|
||||
[tool.ruff]
|
||||
lint.select = [
|
||||
"E", # pycodestyle
|
||||
"F", # Pyflakes
|
||||
"UP", # pyupgrade
|
||||
"B", # flake8-bugbear
|
||||
"I", # isort
|
||||
"UP", # pyupgrade
|
||||
]
|
||||
lint.ignore = ["E501", "B008"]
|
||||
target-version = "py310"
|
||||
|
||||
[tool.ty.rules]
|
||||
invalid-argument-type = "ignore"
|
||||
invalid-assignment = "ignore"
|
||||
invalid-key = "ignore"
|
||||
invalid-parameter-default = "ignore"
|
||||
invalid-return-type = "ignore"
|
||||
missing-argument = "ignore"
|
||||
no-matching-overload = "ignore"
|
||||
not-subscriptable = "ignore"
|
||||
unused-type-ignore-comment = "ignore"
|
||||
unresolved-attribute = "ignore"
|
||||
unresolved-import = "ignore"
|
||||
unsupported-operator = "ignore"
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://langgra.ph/schema.json",
|
||||
"dependencies": [".", "../../libs/shared", "../../libs/common"],
|
||||
"graphs": {
|
||||
"agent": "./src/agent/graph.py:graph"
|
||||
},
|
||||
"env": ".env"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
[project]
|
||||
name = "agent"
|
||||
version = "0.0.1"
|
||||
description = "Agent for the Python monorepo"
|
||||
authors = [
|
||||
{ name = "Developer", email = "dev@example.com" },
|
||||
]
|
||||
license = { text = "MIT" }
|
||||
requires-python = ">=3.11,<4.0"
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=73.0.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.setuptools]
|
||||
packages = ["agent"]
|
||||
|
||||
[tool.setuptools.package-dir]
|
||||
"agent" = "src/agent"
|
||||
@@ -0,0 +1 @@
|
||||
"""Agent package."""
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Simple LangGraph agent for monorepo testing."""
|
||||
|
||||
from common import get_common_prefix
|
||||
from langchain_core.messages import AIMessage
|
||||
from langgraph.graph import END, START, StateGraph
|
||||
from shared import get_dummy_message
|
||||
|
||||
from agent.state import State
|
||||
|
||||
|
||||
def call_model(state: State) -> dict:
|
||||
"""Simple node that uses the shared libraries."""
|
||||
# Use functions from both shared packages
|
||||
dummy_message = get_dummy_message()
|
||||
prefix = get_common_prefix()
|
||||
|
||||
message = AIMessage(content=f"{prefix} Agent says: {dummy_message}")
|
||||
|
||||
return {"messages": [message]}
|
||||
|
||||
|
||||
def should_continue(state: State):
|
||||
"""Conditional edge - end after first message."""
|
||||
messages = state["messages"]
|
||||
if len(messages) > 0:
|
||||
return END
|
||||
return "call_model"
|
||||
|
||||
|
||||
# Build the graph
|
||||
workflow = StateGraph(State)
|
||||
|
||||
# Add the node
|
||||
workflow.add_node("call_model", call_model)
|
||||
|
||||
# Add edges
|
||||
workflow.add_edge(START, "call_model")
|
||||
workflow.add_conditional_edges("call_model", should_continue)
|
||||
|
||||
graph = workflow.compile()
|
||||
@@ -0,0 +1,13 @@
|
||||
"""State definition for the agent."""
|
||||
|
||||
from collections.abc import Sequence
|
||||
from typing import Annotated, TypedDict
|
||||
|
||||
from langchain_core.messages import BaseMessage
|
||||
from langgraph.graph.message import add_messages
|
||||
|
||||
|
||||
class State(TypedDict):
|
||||
"""The state of the agent."""
|
||||
|
||||
messages: Annotated[Sequence[BaseMessage], add_messages]
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Common helper functions package."""
|
||||
|
||||
from .helpers import get_common_prefix
|
||||
|
||||
__all__ = ["get_common_prefix"]
|
||||
@@ -0,0 +1,6 @@
|
||||
"""Common helper functions."""
|
||||
|
||||
|
||||
def get_common_prefix() -> str:
|
||||
"""Get a common prefix for messages."""
|
||||
return "[COMMON]"
|
||||
@@ -0,0 +1,20 @@
|
||||
[project]
|
||||
name = "shared"
|
||||
version = "0.0.1"
|
||||
description = "Shared utilities for the Python monorepo"
|
||||
authors = [
|
||||
{ name = "Developer", email = "dev@example.com" },
|
||||
]
|
||||
license = { text = "MIT" }
|
||||
requires-python = ">=3.11,<4.0"
|
||||
dependencies = []
|
||||
|
||||
[build-system]
|
||||
requires = ["setuptools>=73.0.0", "wheel"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[tool.setuptools]
|
||||
packages = ["shared"]
|
||||
|
||||
[tool.setuptools.package-dir]
|
||||
"shared" = "src/shared"
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Shared utilities package."""
|
||||
|
||||
from .utils import get_dummy_message
|
||||
|
||||
__all__ = ["get_dummy_message"]
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user