langgraph-api 0.19.0.dev7__py3-none-any.whl → 0.19.0.dev9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langgraph_api/__init__.py +1 -1
- langgraph_api/_checkpointer/_adapter.py +3 -1
- langgraph_api/api/__init__.py +1 -1
- langgraph_api/api/threads.py +6 -1
- langgraph_api/cli.py +20 -1
- langgraph_api/config/__init__.py +6 -0
- langgraph_api/embedded_core.py +114 -0
- langgraph_api/event_streaming/service.py +19 -16
- langgraph_api/feature_flags.py +8 -2
- langgraph_api/graph.py +4 -0
- langgraph_api/grpc/client.py +1 -1
- langgraph_api/grpc/ops/threads.py +8 -0
- langgraph_api/js/client.mts +41 -8
- langgraph_api/js/remote.py +25 -1
- langgraph_api/js/src/graph.mts +20 -3
- langgraph_api/runtime/checkpoint.py +6 -0
- langgraph_api/runtime/database.py +38 -0
- langgraph_api/runtime/lifespan.py +50 -0
- langgraph_api/runtime/routes.py +56 -0
- langgraph_api/runtime/store.py +44 -0
- langgraph_api/store.py +3 -3
- langgraph_api/stream.py +2 -0
- {langgraph_api-0.19.0.dev7.dist-info → langgraph_api-0.19.0.dev9.dist-info}/METADATA +5 -1
- {langgraph_api-0.19.0.dev7.dist-info → langgraph_api-0.19.0.dev9.dist-info}/RECORD +31 -26
- langgraph_grpc_common/proto/core_api_pb2.py +160 -160
- langgraph_grpc_common/proto/core_api_pb2.pyi +36 -4
- langgraph_runtime/__init__.py +16 -1
- openapi.json +25 -1
- {langgraph_api-0.19.0.dev7.dist-info → langgraph_api-0.19.0.dev9.dist-info}/WHEEL +0 -0
- {langgraph_api-0.19.0.dev7.dist-info → langgraph_api-0.19.0.dev9.dist-info}/entry_points.txt +0 -0
- {langgraph_api-0.19.0.dev7.dist-info → langgraph_api-0.19.0.dev9.dist-info}/licenses/LICENSE +0 -0
langgraph_api/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "0.19.0.
|
|
1
|
+
__version__ = "0.19.0.dev9"
|
|
@@ -347,11 +347,13 @@ async def get_checkpointer(
|
|
|
347
347
|
)
|
|
348
348
|
|
|
349
349
|
from langgraph_api.feature_flags import ( # noqa: PLC0415
|
|
350
|
+
INMEM_USES_CORE,
|
|
350
351
|
IS_POSTGRES_BACKEND,
|
|
351
352
|
PREFER_GRPC_CHECKPOINTER,
|
|
352
353
|
)
|
|
353
354
|
|
|
354
|
-
|
|
355
|
+
# Inmem on Go core has no Python checkpointer.
|
|
356
|
+
if INMEM_USES_CORE or (IS_POSTGRES_BACKEND and PREFER_GRPC_CHECKPOINTER):
|
|
355
357
|
return cast(
|
|
356
358
|
"FullCheckpointerProtocol",
|
|
357
359
|
GrpcCheckpointer(
|
langgraph_api/api/__init__.py
CHANGED
|
@@ -287,7 +287,7 @@ custom_app_lifespan = (
|
|
|
287
287
|
|
|
288
288
|
|
|
289
289
|
if "__inmem" in MIGRATIONS_PATH:
|
|
290
|
-
from
|
|
290
|
+
from langgraph_runtime.routes import get_internal_routes
|
|
291
291
|
|
|
292
292
|
if get_internal_routes is not None:
|
|
293
293
|
for route in get_internal_routes():
|
langgraph_api/api/threads.py
CHANGED
|
@@ -122,7 +122,12 @@ def _exclude_metadata_kwargs(payload: dict) -> dict:
|
|
|
122
122
|
return {"exclude_metadata": exclude_metadata}
|
|
123
123
|
|
|
124
124
|
|
|
125
|
-
_THREAD_TIME_RANGE_FIELDS = (
|
|
125
|
+
_THREAD_TIME_RANGE_FIELDS = (
|
|
126
|
+
"created_after",
|
|
127
|
+
"created_before",
|
|
128
|
+
"state_updated_after",
|
|
129
|
+
"state_updated_before",
|
|
130
|
+
)
|
|
126
131
|
|
|
127
132
|
|
|
128
133
|
def _time_range_kwargs(payload: dict) -> dict:
|
langgraph_api/cli.py
CHANGED
|
@@ -16,6 +16,8 @@ from concurrent.futures import ThreadPoolExecutor
|
|
|
16
16
|
from concurrent.futures import TimeoutError as FutureTimeoutError
|
|
17
17
|
from typing import Literal
|
|
18
18
|
|
|
19
|
+
from langgraph_api import embedded_core
|
|
20
|
+
|
|
19
21
|
if typing.TYPE_CHECKING:
|
|
20
22
|
from packaging.version import Version
|
|
21
23
|
|
|
@@ -299,7 +301,24 @@ def run_server(
|
|
|
299
301
|
continue
|
|
300
302
|
to_patch[k] = v
|
|
301
303
|
|
|
302
|
-
|
|
304
|
+
def _env(name: str, default: str = "") -> str:
|
|
305
|
+
if env_vars and name in env_vars:
|
|
306
|
+
return env_vars[name]
|
|
307
|
+
return os.getenv(name, default)
|
|
308
|
+
|
|
309
|
+
use_core = runtime_edition == "inmem" and _env(
|
|
310
|
+
"FF_INMEM_CORE", "false"
|
|
311
|
+
).lower() in ("true", "1", "yes")
|
|
312
|
+
if use_core:
|
|
313
|
+
to_patch[embedded_core.ADDRESS_ENV] = ""
|
|
314
|
+
core = (
|
|
315
|
+
embedded_core.start(None if disable_persistence else embedded_core.SQLITE_PATH)
|
|
316
|
+
if use_core
|
|
317
|
+
else contextlib.nullcontext(None)
|
|
318
|
+
)
|
|
319
|
+
with patch_environment(**to_patch), core as core_address:
|
|
320
|
+
if core_address is not None:
|
|
321
|
+
os.environ[embedded_core.ADDRESS_ENV] = core_address
|
|
303
322
|
is_ssl = ssl_certfile is not None and ssl_keyfile is not None
|
|
304
323
|
local_url = _resolve_server_url(
|
|
305
324
|
host,
|
langgraph_api/config/__init__.py
CHANGED
|
@@ -207,6 +207,12 @@ LANGGRAPH_CLOUD_LICENSE_KEY = env("LANGGRAPH_CLOUD_LICENSE_KEY", cast=str, defau
|
|
|
207
207
|
# When true, the Go core-server owns Postgres schema setup (DB migrations +
|
|
208
208
|
# vector index)
|
|
209
209
|
DB_MIGRATION_BY_CORE_API = env("DB_MIGRATION_BY_CORE_API", cast=bool, default=True)
|
|
210
|
+
# Bounds core-server startup, including migrations, and how long Python waits
|
|
211
|
+
# for it. Core-server reads the same env var. Keep under the startup probe.
|
|
212
|
+
# Not in public docs: infrastructure, set by platform
|
|
213
|
+
STARTUP_TIMEOUT_SECONDS = env("STARTUP_TIMEOUT_SECONDS", cast=float, default=270.0)
|
|
214
|
+
if STARTUP_TIMEOUT_SECONDS <= 0:
|
|
215
|
+
STARTUP_TIMEOUT_SECONDS = 270.0
|
|
210
216
|
|
|
211
217
|
# redis
|
|
212
218
|
# Not in public docs: infrastructure, set by platform
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
import tempfile
|
|
7
|
+
import time
|
|
8
|
+
from contextlib import contextmanager
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
try:
|
|
13
|
+
from langgraph_api_core_bin import binary_path
|
|
14
|
+
except ImportError:
|
|
15
|
+
binary_path = None
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from collections.abc import Iterator
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
# Hands the address of a core started by ``langgraph dev`` to its server process.
|
|
23
|
+
ADDRESS_ENV = "LANGGRAPH_EMBEDDED_CORE_ADDRESS"
|
|
24
|
+
SQLITE_PATH = os.path.join(".langgraph_api", "core.sqlite")
|
|
25
|
+
# Local data formats this version no longer reads. Add a pattern whenever a
|
|
26
|
+
# change drops local dev data instead of migrating it.
|
|
27
|
+
DROPPED_LOCAL_DATA = (
|
|
28
|
+
".*.pckl", # Python in-memory runtime
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@contextmanager
|
|
33
|
+
def start(sqlite_path: str | None = None) -> Iterator[str]:
|
|
34
|
+
"""Run Go core on SQLite; in-memory unless ``sqlite_path`` is given."""
|
|
35
|
+
if binary_path is None:
|
|
36
|
+
raise RuntimeError(
|
|
37
|
+
"Local development requires the inmem extra: "
|
|
38
|
+
"pip install 'langgraph-api[inmem]'"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
with tempfile.TemporaryDirectory(prefix="langgraph-core-") as temp_dir:
|
|
42
|
+
address_file = Path(temp_dir, "address")
|
|
43
|
+
env = os.environ.copy()
|
|
44
|
+
env.update(
|
|
45
|
+
DATABASE_URI="",
|
|
46
|
+
DISPATCH_MODE="single-process",
|
|
47
|
+
LANGGRAPH_STORE_BACKEND="grpc",
|
|
48
|
+
LSD_GRPC_SERVER_ADDRESS="127.0.0.1:0",
|
|
49
|
+
PPROF_ADDR="127.0.0.1:0",
|
|
50
|
+
)
|
|
51
|
+
# An empty DATABASE_URI falls back to POSTGRES_URI, which sqlite_only cores refuse.
|
|
52
|
+
env.pop("POSTGRES_URI", None)
|
|
53
|
+
env.pop("REDIS_URI", None)
|
|
54
|
+
env.pop("REDIS_URI_CUSTOM", None)
|
|
55
|
+
# Core exits when this pipe closes, so it never outlives this process.
|
|
56
|
+
args = [
|
|
57
|
+
binary_path(),
|
|
58
|
+
"-apply-db-schema",
|
|
59
|
+
"-exit-on-stdin-close",
|
|
60
|
+
"-address-file",
|
|
61
|
+
address_file,
|
|
62
|
+
]
|
|
63
|
+
if sqlite_path:
|
|
64
|
+
Path(sqlite_path).parent.mkdir(parents=True, exist_ok=True)
|
|
65
|
+
_warn_dropped_local_data(Path(sqlite_path).parent)
|
|
66
|
+
args += ["-sqlite-path", sqlite_path]
|
|
67
|
+
process = subprocess.Popen(
|
|
68
|
+
args,
|
|
69
|
+
env=env,
|
|
70
|
+
stdin=subprocess.PIPE,
|
|
71
|
+
)
|
|
72
|
+
try:
|
|
73
|
+
yield _wait_for_address(process, address_file)
|
|
74
|
+
finally:
|
|
75
|
+
if process.poll() is None:
|
|
76
|
+
process.terminate()
|
|
77
|
+
try:
|
|
78
|
+
process.wait(timeout=5)
|
|
79
|
+
except subprocess.TimeoutExpired:
|
|
80
|
+
process.kill()
|
|
81
|
+
process.wait()
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _warn_dropped_local_data(data_dir: Path) -> None:
|
|
85
|
+
dropped = sorted(
|
|
86
|
+
str(f) for pattern in DROPPED_LOCAL_DATA for f in data_dir.glob(pattern)
|
|
87
|
+
)
|
|
88
|
+
if dropped:
|
|
89
|
+
logger.warning(
|
|
90
|
+
"Ignoring local data from an older format that this version no longer "
|
|
91
|
+
"reads; threads and runs stored there won't appear: %s",
|
|
92
|
+
", ".join(dropped),
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _wait_for_address(
|
|
97
|
+
process: subprocess.Popen[bytes], address_file: Path, timeout: float = 30
|
|
98
|
+
) -> str:
|
|
99
|
+
deadline = time.monotonic() + timeout
|
|
100
|
+
while time.monotonic() < deadline:
|
|
101
|
+
if process.poll() is not None:
|
|
102
|
+
raise RuntimeError(
|
|
103
|
+
f"LangGraph API core exited with code {process.returncode}"
|
|
104
|
+
)
|
|
105
|
+
try:
|
|
106
|
+
if address := address_file.read_text().strip():
|
|
107
|
+
return address
|
|
108
|
+
except FileNotFoundError:
|
|
109
|
+
pass
|
|
110
|
+
time.sleep(0.05)
|
|
111
|
+
raise RuntimeError("Timed out waiting for LangGraph API core to start")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
__all__ = ["ADDRESS_ENV", "SQLITE_PATH", "start"]
|
|
@@ -788,10 +788,25 @@ class ThreadRunManager:
|
|
|
788
788
|
sanitize_reserved_keys(params)
|
|
789
789
|
config = params.get("config") if _is_record(params.get("config")) else {}
|
|
790
790
|
|
|
791
|
-
#
|
|
792
|
-
# the
|
|
791
|
+
# The fork target arrives only via ``config.configurable.checkpoint_id``
|
|
792
|
+
# (the SDK folds its ergonomic ``forkFrom`` option into this field
|
|
793
|
+
# client-side, so there is a single way to provide it). Extract it
|
|
794
|
+
# here and map it onto the top-level ``checkpoint_id`` / ``checkpoint``
|
|
795
|
+
# fields of ``run_payload`` below: ``create_valid_run``, the shared
|
|
796
|
+
# run-creation path also used by the legacy REST run-create endpoints,
|
|
797
|
+
# reads the fork target from those fields (UUID-validating it and
|
|
798
|
+
# injecting it back into ``config.configurable``), so populating them is
|
|
799
|
+
# what makes the run replay from the requested checkpoint rather than
|
|
800
|
+
# the thread's latest state.
|
|
801
|
+
checkpoint_id = _checkpoint_id_from_run_start_config(params)
|
|
802
|
+
|
|
803
|
+
# Only a plain run.start needs the lookups below, which can wait seconds
|
|
804
|
+
# on a busy thread: ``input.respond`` checked its interrupts already and
|
|
805
|
+
# always resumes, and a fork starts a new run from its checkpoint
|
|
806
|
+
# whatever the thread's latest one waits on.
|
|
793
807
|
force_resume = params.get("force_resume")
|
|
794
|
-
|
|
808
|
+
infer_resume = not force_resume and checkpoint_id is None
|
|
809
|
+
current_run = await self._fetch_current_run() if infer_resume else None
|
|
795
810
|
|
|
796
811
|
current_status = None
|
|
797
812
|
if current_run is not None:
|
|
@@ -802,7 +817,7 @@ class ThreadRunManager:
|
|
|
802
817
|
)
|
|
803
818
|
|
|
804
819
|
has_interrupts = False
|
|
805
|
-
if params.get("input") is not None and
|
|
820
|
+
if params.get("input") is not None and infer_resume:
|
|
806
821
|
has_interrupts = await self._has_pending_interrupts()
|
|
807
822
|
if has_interrupts and not keyed_by_interrupt_id(params["input"]):
|
|
808
823
|
# One answer can't say which of several pending interrupts it is
|
|
@@ -831,18 +846,6 @@ class ThreadRunManager:
|
|
|
831
846
|
},
|
|
832
847
|
}
|
|
833
848
|
|
|
834
|
-
# The fork target arrives only via ``config.configurable.checkpoint_id``
|
|
835
|
-
# (the SDK folds its ergonomic ``forkFrom`` option into this field
|
|
836
|
-
# client-side, so there is a single way to provide it). Extract it
|
|
837
|
-
# here and map it onto the top-level ``checkpoint_id`` / ``checkpoint``
|
|
838
|
-
# fields of ``run_payload`` below: ``create_valid_run`` — the shared
|
|
839
|
-
# run-creation path also used by the legacy REST run-create endpoints —
|
|
840
|
-
# reads the fork target from those fields (UUID-validating it and
|
|
841
|
-
# injecting it back into ``config.configurable``), so populating them is
|
|
842
|
-
# what makes the run replay from the requested checkpoint rather than
|
|
843
|
-
# the thread's latest state.
|
|
844
|
-
checkpoint_id = _checkpoint_id_from_run_start_config(params)
|
|
845
|
-
|
|
846
849
|
run_payload: dict[str, Any] = {
|
|
847
850
|
"assistant_id": assistant_id,
|
|
848
851
|
"input": None if is_resume else params.get("input"),
|
langgraph_api/feature_flags.py
CHANGED
|
@@ -20,7 +20,13 @@ USE_DURABILITY = LANGGRAPH_PY_MINOR >= (0, 6)
|
|
|
20
20
|
_RUNTIME_EDITION = os.getenv("LANGGRAPH_RUNTIME_EDITION", "inmem")
|
|
21
21
|
IS_POSTGRES_BACKEND = _RUNTIME_EDITION == "postgres"
|
|
22
22
|
IS_INMEM_BACKEND = _RUNTIME_EDITION == "inmem"
|
|
23
|
-
|
|
23
|
+
# Opt-in: run the in-memory runtime on Go core with SQLite.
|
|
24
|
+
INMEM_USES_CORE = IS_INMEM_BACKEND and os.getenv("FF_INMEM_CORE", "false").lower() in (
|
|
25
|
+
"true",
|
|
26
|
+
"1",
|
|
27
|
+
"yes",
|
|
28
|
+
)
|
|
29
|
+
IS_POSTGRES_OR_GRPC_BACKEND = IS_POSTGRES_BACKEND or INMEM_USES_CORE
|
|
24
30
|
# Server-side kill switch for the Protocol v2 event-streaming surface
|
|
25
31
|
# (#3296). When false, the v2 routes are not registered (clients hit a
|
|
26
32
|
# 404), and ``_create_or_resume_run`` rejects any direct call with a
|
|
@@ -58,7 +64,7 @@ def _resolve_prefer_grpc_checkpointer(
|
|
|
58
64
|
|
|
59
65
|
|
|
60
66
|
# When true on the postgres runtime, use ``GrpcCheckpointer``.
|
|
61
|
-
# Inmem
|
|
67
|
+
# Inmem on Go core always uses it; Python inmem never does.
|
|
62
68
|
# Custom checkpointer (``backend=custom`` in ``LANGGRAPH_CHECKPOINTER``) and
|
|
63
69
|
# Mongo (``backend=mongo``) is also unaffected.
|
|
64
70
|
# langgraph < 0.5 keeps Send() packets in checkpoint["pending_sends"], which only
|
langgraph_api/graph.py
CHANGED
|
@@ -434,6 +434,10 @@ async def get_graph(
|
|
|
434
434
|
"__is_for_execution__": is_for_execution(access_context),
|
|
435
435
|
},
|
|
436
436
|
}
|
|
437
|
+
update["access_context"] = access_context
|
|
438
|
+
# The sidecar builds the factory after the run context is filtered
|
|
439
|
+
# by its schema, so it also gets the unfiltered context here.
|
|
440
|
+
update["factory_context"] = context
|
|
437
441
|
yield graph_obj.copy(update=update)
|
|
438
442
|
finally:
|
|
439
443
|
var_child_runnable_config.set(None)
|
langgraph_api/grpc/client.py
CHANGED
|
@@ -380,6 +380,8 @@ class Threads(Authenticated):
|
|
|
380
380
|
exclude_metadata: list[dict[str, Any]] | None = None,
|
|
381
381
|
created_after: datetime | None = None,
|
|
382
382
|
created_before: datetime | None = None,
|
|
383
|
+
state_updated_after: datetime | None = None,
|
|
384
|
+
state_updated_before: datetime | None = None,
|
|
383
385
|
raw_values: bool = False,
|
|
384
386
|
ctx: Any = None,
|
|
385
387
|
) -> tuple[AsyncIterator[Thread], int | None]:
|
|
@@ -405,6 +407,8 @@ class Threads(Authenticated):
|
|
|
405
407
|
"values_json": json_dumpb_optional(values),
|
|
406
408
|
"created_after": created_after,
|
|
407
409
|
"created_before": created_before,
|
|
410
|
+
"state_updated_after": state_updated_after,
|
|
411
|
+
"state_updated_before": state_updated_before,
|
|
408
412
|
"limit": limit,
|
|
409
413
|
"offset": offset,
|
|
410
414
|
"sort_by": _map_threads_sort_by(sort_by),
|
|
@@ -462,6 +466,8 @@ class Threads(Authenticated):
|
|
|
462
466
|
exclude_metadata: list[dict[str, Any]] | None = None,
|
|
463
467
|
created_after: datetime | None = None,
|
|
464
468
|
created_before: datetime | None = None,
|
|
469
|
+
state_updated_after: datetime | None = None,
|
|
470
|
+
state_updated_before: datetime | None = None,
|
|
465
471
|
ctx: Any = None,
|
|
466
472
|
) -> int:
|
|
467
473
|
metadata = metadata or {}
|
|
@@ -483,6 +489,8 @@ class Threads(Authenticated):
|
|
|
483
489
|
"values_json": json_dumpb_optional(values),
|
|
484
490
|
"created_after": created_after,
|
|
485
491
|
"created_before": created_before,
|
|
492
|
+
"state_updated_after": state_updated_after,
|
|
493
|
+
"state_updated_before": state_updated_before,
|
|
486
494
|
}
|
|
487
495
|
if exclude_metadata:
|
|
488
496
|
request_kwargs["exclude_metadata_json"] = json_dumpb(exclude_metadata)
|
langgraph_api/js/client.mts
CHANGED
|
@@ -69,7 +69,7 @@ import { collectMutations } from "./src/auth-mutations.mts";
|
|
|
69
69
|
import { patchFetch } from "./traceblock.mts";
|
|
70
70
|
import { writeHeapSnapshot } from "node:v8";
|
|
71
71
|
|
|
72
|
-
import { DurabilitySchema } from "./src/graph.mts";
|
|
72
|
+
import { AccessContextSchema, DurabilitySchema } from "./src/graph.mts";
|
|
73
73
|
|
|
74
74
|
const injectConfigFormatter = format((info) => {
|
|
75
75
|
const config = getConfig();
|
|
@@ -171,6 +171,10 @@ async function getGraph(
|
|
|
171
171
|
graphId: string,
|
|
172
172
|
config: Parameters<CompiledGraphFactory<string>>[0],
|
|
173
173
|
name: string | null | undefined,
|
|
174
|
+
options?: {
|
|
175
|
+
accessContext?: z.infer<typeof AccessContextSchema> | null;
|
|
176
|
+
context?: unknown;
|
|
177
|
+
},
|
|
174
178
|
) {
|
|
175
179
|
if (!GRAPH_RESOLVED[graphId])
|
|
176
180
|
throw new HTTPException(404, { message: `Graph "${graphId}" not found` });
|
|
@@ -181,9 +185,16 @@ async function getGraph(
|
|
|
181
185
|
store: GRAPH_OPTIONS.store,
|
|
182
186
|
};
|
|
183
187
|
|
|
188
|
+
const accessContext = options?.accessContext ?? "assistants.read";
|
|
184
189
|
const resolved =
|
|
185
190
|
typeof GRAPH_RESOLVED[graphId] === "function"
|
|
186
|
-
? await GRAPH_RESOLVED[graphId](factoryConfig
|
|
191
|
+
? await GRAPH_RESOLVED[graphId](factoryConfig, {
|
|
192
|
+
accessContext,
|
|
193
|
+
executionRuntime:
|
|
194
|
+
accessContext === "threads.create_run"
|
|
195
|
+
? { context: options?.context }
|
|
196
|
+
: null,
|
|
197
|
+
})
|
|
187
198
|
: GRAPH_RESOLVED[graphId];
|
|
188
199
|
|
|
189
200
|
if (GRAPH_OPTIONS.checkpointer) {
|
|
@@ -699,6 +710,7 @@ const StreamEventsPayload = z.object({
|
|
|
699
710
|
graph_id: z.string(),
|
|
700
711
|
graph_name: z.string().nullish(),
|
|
701
712
|
graph_config: RunnableConfigSchema.nullish(),
|
|
713
|
+
access_context: AccessContextSchema.nullish(),
|
|
702
714
|
input: z.unknown(),
|
|
703
715
|
command: z
|
|
704
716
|
.object({
|
|
@@ -716,6 +728,7 @@ const StreamEventsPayload = z.object({
|
|
|
716
728
|
stream_mode: z.union([z.string(), z.array(z.string())]).optional(),
|
|
717
729
|
config: RunnableConfigSchema.nullish(),
|
|
718
730
|
context: z.record(z.unknown()).nullish(),
|
|
731
|
+
factory_context: z.record(z.unknown()).nullish(),
|
|
719
732
|
interrupt_before: z.union([z.array(z.string()), z.literal("*")]).nullish(),
|
|
720
733
|
interrupt_after: z.union([z.array(z.string()), z.literal("*")]).nullish(),
|
|
721
734
|
subgraphs: z.boolean().optional(),
|
|
@@ -761,7 +774,10 @@ async function* streamEventsRequest(
|
|
|
761
774
|
...getRunnableConfig(payload.config),
|
|
762
775
|
context: payload.context ?? undefined,
|
|
763
776
|
};
|
|
764
|
-
const graph = await getGraph(graphId, config, payload.graph_name
|
|
777
|
+
const graph = await getGraph(graphId, config, payload.graph_name, {
|
|
778
|
+
accessContext: payload.access_context,
|
|
779
|
+
context: payload.factory_context ?? undefined,
|
|
780
|
+
});
|
|
765
781
|
const input = reviveCommand(payload.command) ?? payload.input;
|
|
766
782
|
// TODO Check if it's a remote graph and don't set in that case
|
|
767
783
|
config.configurable = {
|
|
@@ -984,6 +1000,7 @@ const GetGraphPayload = z.object({
|
|
|
984
1000
|
graph_id: z.string(),
|
|
985
1001
|
graph_name: z.string().nullish(),
|
|
986
1002
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1003
|
+
access_context: AccessContextSchema.nullish(),
|
|
987
1004
|
config: RunnableConfigSchema.nullish(),
|
|
988
1005
|
xray: z.union([z.number(), z.boolean()]).nullish(),
|
|
989
1006
|
});
|
|
@@ -995,6 +1012,7 @@ async function getGraphRequest(rawPayload: z.infer<typeof GetGraphPayload>) {
|
|
|
995
1012
|
graphId,
|
|
996
1013
|
getRunnableConfig(payload.graph_config),
|
|
997
1014
|
payload.graph_name,
|
|
1015
|
+
{ accessContext: payload.access_context },
|
|
998
1016
|
);
|
|
999
1017
|
|
|
1000
1018
|
const drawable = await graph.getGraphAsync({
|
|
@@ -1007,6 +1025,7 @@ async function getGraphRequest(rawPayload: z.infer<typeof GetGraphPayload>) {
|
|
|
1007
1025
|
const GetSubgraphsPayload = z.object({
|
|
1008
1026
|
graph_id: z.string(),
|
|
1009
1027
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1028
|
+
access_context: AccessContextSchema.nullish(),
|
|
1010
1029
|
graph_name: z.string().nullish(),
|
|
1011
1030
|
namespace: z.string().nullish(),
|
|
1012
1031
|
recurse: z.boolean().nullish(),
|
|
@@ -1018,7 +1037,9 @@ async function getSubgraphsRequest(
|
|
|
1018
1037
|
const { graph_id: graphId, ...payload } = rawPayload;
|
|
1019
1038
|
|
|
1020
1039
|
const config = getRunnableConfig(payload.graph_config);
|
|
1021
|
-
const graph = await getGraph(graphId, config, payload.graph_name
|
|
1040
|
+
const graph = await getGraph(graphId, config, payload.graph_name, {
|
|
1041
|
+
accessContext: payload.access_context,
|
|
1042
|
+
});
|
|
1022
1043
|
|
|
1023
1044
|
const result: Array<[name: string, schema: Record<string, any>]> = [];
|
|
1024
1045
|
let graphSchemaPromise: ReturnType<typeof getOrExtractSchema> | undefined;
|
|
@@ -1049,6 +1070,7 @@ async function getSubgraphsRequest(
|
|
|
1049
1070
|
const GetStatePayload = z.object({
|
|
1050
1071
|
graph_id: z.string(),
|
|
1051
1072
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1073
|
+
access_context: AccessContextSchema.nullish(),
|
|
1052
1074
|
graph_name: z.string().nullish(),
|
|
1053
1075
|
config: RunnableConfigSchema,
|
|
1054
1076
|
subgraphs: z.boolean().nullish(),
|
|
@@ -1057,7 +1079,9 @@ const GetStatePayload = z.object({
|
|
|
1057
1079
|
async function getStateRequest(rawPayload: z.infer<typeof GetStatePayload>) {
|
|
1058
1080
|
const { graph_id: graphId, ...payload } = rawPayload;
|
|
1059
1081
|
const graphConfig = getRunnableConfig(payload.graph_config);
|
|
1060
|
-
const graph = await getGraph(graphId, graphConfig, payload.graph_name
|
|
1082
|
+
const graph = await getGraph(graphId, graphConfig, payload.graph_name, {
|
|
1083
|
+
accessContext: payload.access_context,
|
|
1084
|
+
});
|
|
1061
1085
|
|
|
1062
1086
|
const state = await graph.getState(getRunnableConfig(payload.config), {
|
|
1063
1087
|
subgraphs: payload.subgraphs ?? undefined,
|
|
@@ -1070,6 +1094,7 @@ async function getStateRequest(rawPayload: z.infer<typeof GetStatePayload>) {
|
|
|
1070
1094
|
const UpdateStatePayload = z.object({
|
|
1071
1095
|
graph_id: z.string(),
|
|
1072
1096
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1097
|
+
access_context: AccessContextSchema.nullish(),
|
|
1073
1098
|
graph_name: z.string().nullish(),
|
|
1074
1099
|
config: RunnableConfigSchema,
|
|
1075
1100
|
values: z.unknown(),
|
|
@@ -1081,7 +1106,9 @@ async function updateStateRequest(
|
|
|
1081
1106
|
) {
|
|
1082
1107
|
const { graph_id: graphId, ...payload } = rawPayload;
|
|
1083
1108
|
const graphConfig = getRunnableConfig(payload.graph_config);
|
|
1084
|
-
const graph = await getGraph(graphId, graphConfig, payload.graph_name
|
|
1109
|
+
const graph = await getGraph(graphId, graphConfig, payload.graph_name, {
|
|
1110
|
+
accessContext: payload.access_context,
|
|
1111
|
+
});
|
|
1085
1112
|
|
|
1086
1113
|
const config = await graph.updateState(
|
|
1087
1114
|
getRunnableConfig(payload.config),
|
|
@@ -1095,6 +1122,7 @@ async function updateStateRequest(
|
|
|
1095
1122
|
const GetSchemaPayload = z.object({
|
|
1096
1123
|
graph_id: z.string(),
|
|
1097
1124
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1125
|
+
access_context: AccessContextSchema.nullish(),
|
|
1098
1126
|
graph_name: z.string().nullish(),
|
|
1099
1127
|
});
|
|
1100
1128
|
|
|
@@ -1102,7 +1130,9 @@ async function getSchemaRequest(payload: z.infer<typeof GetSchemaPayload>) {
|
|
|
1102
1130
|
const { graph_id: graphId } = payload;
|
|
1103
1131
|
|
|
1104
1132
|
const config = getRunnableConfig(payload.graph_config);
|
|
1105
|
-
const graph = await getGraph(graphId, config, payload.graph_name
|
|
1133
|
+
const graph = await getGraph(graphId, config, payload.graph_name, {
|
|
1134
|
+
accessContext: payload.access_context,
|
|
1135
|
+
});
|
|
1106
1136
|
|
|
1107
1137
|
const schema = await (async () => {
|
|
1108
1138
|
const runtimeSchema = await getRuntimeGraphSchema(graph);
|
|
@@ -1120,6 +1150,7 @@ async function getSchemaRequest(payload: z.infer<typeof GetSchemaPayload>) {
|
|
|
1120
1150
|
const GetStateHistoryPayload = z.object({
|
|
1121
1151
|
graph_id: z.string(),
|
|
1122
1152
|
graph_config: RunnableConfigSchema.nullish(),
|
|
1153
|
+
access_context: AccessContextSchema.nullish(),
|
|
1123
1154
|
graph_name: z.string().nullish(),
|
|
1124
1155
|
config: RunnableConfigSchema,
|
|
1125
1156
|
limit: z.number().nullish(),
|
|
@@ -1132,7 +1163,9 @@ async function* getStateHistoryRequest(
|
|
|
1132
1163
|
) {
|
|
1133
1164
|
const { graph_id: graphId, ...payload } = rawPayload;
|
|
1134
1165
|
const config = getRunnableConfig(payload.graph_config);
|
|
1135
|
-
const graph = await getGraph(graphId, config, payload.graph_name
|
|
1166
|
+
const graph = await getGraph(graphId, config, payload.graph_name, {
|
|
1167
|
+
accessContext: payload.access_context,
|
|
1168
|
+
});
|
|
1136
1169
|
|
|
1137
1170
|
for await (const item of graph.getStateHistory(
|
|
1138
1171
|
getRunnableConfig(payload.config),
|
langgraph_api/js/remote.py
CHANGED
|
@@ -49,6 +49,7 @@ from starlette.routing import Route
|
|
|
49
49
|
|
|
50
50
|
from langgraph_api import config as lg_config
|
|
51
51
|
from langgraph_api import store as api_store
|
|
52
|
+
from langgraph_api._factory_utils import AccessContext
|
|
52
53
|
from langgraph_api.asyncio import NoSignalServer
|
|
53
54
|
from langgraph_api.auth.custom import DotDict, ProxyUser, connection_method
|
|
54
55
|
from langgraph_api.config import LANGGRAPH_AUTH, LANGGRAPH_AUTH_TYPE
|
|
@@ -290,11 +291,15 @@ class RemotePregel(BaseRemotePregel):
|
|
|
290
291
|
graph_id: str,
|
|
291
292
|
*,
|
|
292
293
|
config: Config | None = None,
|
|
294
|
+
access_context: AccessContext | None = None,
|
|
295
|
+
factory_context: Any | None = None,
|
|
293
296
|
**kwargs: Any,
|
|
294
297
|
):
|
|
295
298
|
super().__init__()
|
|
296
299
|
self.graph_id = graph_id
|
|
297
300
|
self.config = config
|
|
301
|
+
self.access_context = access_context
|
|
302
|
+
self.factory_context = factory_context
|
|
298
303
|
|
|
299
304
|
async def astream_events(
|
|
300
305
|
self,
|
|
@@ -310,6 +315,8 @@ class RemotePregel(BaseRemotePregel):
|
|
|
310
315
|
data = {
|
|
311
316
|
"graph_id": self.graph_id,
|
|
312
317
|
"graph_config": self.config,
|
|
318
|
+
"access_context": self.access_context,
|
|
319
|
+
"factory_context": self.factory_context,
|
|
313
320
|
"graph_name": self.name,
|
|
314
321
|
"command" if isinstance(input, Command) else "input": input,
|
|
315
322
|
"config": self._with_execution_signal(config),
|
|
@@ -345,6 +352,8 @@ class RemotePregel(BaseRemotePregel):
|
|
|
345
352
|
data = {
|
|
346
353
|
"graph_id": self.graph_id,
|
|
347
354
|
"graph_config": self.config,
|
|
355
|
+
"access_context": self.access_context,
|
|
356
|
+
"factory_context": self.factory_context,
|
|
348
357
|
"graph_name": self.name,
|
|
349
358
|
"command" if isinstance(input, Command) else "input": input,
|
|
350
359
|
"config": self._with_execution_signal(config),
|
|
@@ -359,11 +368,21 @@ class RemotePregel(BaseRemotePregel):
|
|
|
359
368
|
yield event
|
|
360
369
|
|
|
361
370
|
async def fetch_state_schema(self):
|
|
371
|
+
# A run can fetch its schema through its own graph, so mark this as a read.
|
|
372
|
+
config = self.config or {}
|
|
373
|
+
graph_config = {
|
|
374
|
+
**config,
|
|
375
|
+
"configurable": {
|
|
376
|
+
**(config.get("configurable") or {}),
|
|
377
|
+
"__is_for_execution__": False,
|
|
378
|
+
},
|
|
379
|
+
}
|
|
362
380
|
return await _client_invoke(
|
|
363
381
|
"getSchema",
|
|
364
382
|
{
|
|
365
383
|
"graph_id": self.graph_id,
|
|
366
|
-
"graph_config": self._inject_auth_to_config(
|
|
384
|
+
"graph_config": self._inject_auth_to_config(graph_config),
|
|
385
|
+
"access_context": "assistants.read",
|
|
367
386
|
},
|
|
368
387
|
)
|
|
369
388
|
|
|
@@ -399,6 +418,7 @@ class RemotePregel(BaseRemotePregel):
|
|
|
399
418
|
{
|
|
400
419
|
"graph_id": self.graph_id,
|
|
401
420
|
"graph_config": self._inject_auth_to_config(self.config),
|
|
421
|
+
"access_context": self.access_context,
|
|
402
422
|
"graph_name": self.name,
|
|
403
423
|
"config": self._inject_auth_to_config(config),
|
|
404
424
|
"xray": xray,
|
|
@@ -440,6 +460,7 @@ class RemotePregel(BaseRemotePregel):
|
|
|
440
460
|
{
|
|
441
461
|
"graph_id": self.graph_id,
|
|
442
462
|
"graph_config": self._inject_auth_to_config(self.config),
|
|
463
|
+
"access_context": self.access_context,
|
|
443
464
|
"graph_name": self.name,
|
|
444
465
|
"namespace": namespace,
|
|
445
466
|
"recurse": recurse,
|
|
@@ -495,6 +516,7 @@ class RemotePregel(BaseRemotePregel):
|
|
|
495
516
|
{
|
|
496
517
|
"graph_id": self.graph_id,
|
|
497
518
|
"graph_config": self._inject_auth_to_config(self.config),
|
|
519
|
+
"access_context": self.access_context,
|
|
498
520
|
"graph_name": self.name,
|
|
499
521
|
"config": self._inject_auth_to_config(config),
|
|
500
522
|
"subgraphs": subgraphs,
|
|
@@ -513,6 +535,7 @@ class RemotePregel(BaseRemotePregel):
|
|
|
513
535
|
{
|
|
514
536
|
"graph_id": self.graph_id,
|
|
515
537
|
"graph_config": self._inject_auth_to_config(self.config),
|
|
538
|
+
"access_context": self.access_context,
|
|
516
539
|
"graph_name": self.name,
|
|
517
540
|
"config": self._inject_auth_to_config(config),
|
|
518
541
|
"values": values,
|
|
@@ -534,6 +557,7 @@ class RemotePregel(BaseRemotePregel):
|
|
|
534
557
|
{
|
|
535
558
|
"graph_id": self.graph_id,
|
|
536
559
|
"graph_config": self._inject_auth_to_config(self.config),
|
|
560
|
+
"access_context": self.access_context,
|
|
537
561
|
"graph_name": self.name,
|
|
538
562
|
"config": self._inject_auth_to_config(config),
|
|
539
563
|
"limit": limit,
|
langgraph_api/js/src/graph.mts
CHANGED
|
@@ -28,8 +28,22 @@ export type FactoryConfig = {
|
|
|
28
28
|
checkpointer?: BaseCheckpointSaver<string | number>;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
+
export const AccessContextSchema = z.enum([
|
|
32
|
+
"threads.create_run",
|
|
33
|
+
"threads.update",
|
|
34
|
+
"threads.read",
|
|
35
|
+
"assistants.read",
|
|
36
|
+
]);
|
|
37
|
+
export type AccessContext = z.infer<typeof AccessContextSchema>;
|
|
38
|
+
|
|
39
|
+
export type FactoryRuntime = {
|
|
40
|
+
accessContext: AccessContext;
|
|
41
|
+
executionRuntime: { context: unknown } | null;
|
|
42
|
+
};
|
|
43
|
+
|
|
31
44
|
export type CompiledGraphFactory<T extends string> = (
|
|
32
45
|
config: FactoryConfig,
|
|
46
|
+
runtime: FactoryRuntime,
|
|
33
47
|
) => Promise<CompiledGraph<T>>;
|
|
34
48
|
|
|
35
49
|
export async function resolveGraph(
|
|
@@ -64,7 +78,10 @@ export async function resolveGraph(
|
|
|
64
78
|
type GraphUnknown =
|
|
65
79
|
| GraphLike
|
|
66
80
|
| Promise<GraphLike>
|
|
67
|
-
| ((
|
|
81
|
+
| ((
|
|
82
|
+
config: FactoryConfig,
|
|
83
|
+
runtime: FactoryRuntime,
|
|
84
|
+
) => GraphLike | Promise<GraphLike>)
|
|
68
85
|
| undefined;
|
|
69
86
|
|
|
70
87
|
const isGraph = (graph: GraphLike): graph is Graph<string> => {
|
|
@@ -107,8 +124,8 @@ export async function resolveGraph(
|
|
|
107
124
|
};
|
|
108
125
|
|
|
109
126
|
if (typeof graph === "function") {
|
|
110
|
-
return async (config: FactoryConfig) => {
|
|
111
|
-
const graphLike = await graph(config);
|
|
127
|
+
return async (config: FactoryConfig, runtime: FactoryRuntime) => {
|
|
128
|
+
const graphLike = await graph(config, runtime);
|
|
112
129
|
return afterResolve(graphLike);
|
|
113
130
|
};
|
|
114
131
|
}
|