Skip to main content

HarborFlow: Declarative graph orchestration atop LangGraph

Project description

HarborFlow

基于 LangGraph v1.0 的轻量语法糖层,让“写图编排”退化为“写普通 Python 类与函数”。

为什么选择 HarborFlow

  • 代码即图:使用 @graph 声明类、@node 声明方法,即可自动编译成可运行的状态图。
  • 返回即路由:节点统一返回 Route(goto=..., update=...),直接决定下一步与状态更新。
  • 异步支持:完整支持 async/await,同步和异步节点可混合使用。
  • 高级功能:条件路由、状态验证、错误处理、并行执行等现代工作流特性。
  • 与 LangGraph 100% 兼容:编译后返回原生 CompiledStateGraph(或轻量包装),可直接 invoke/stream/checkpoint
  • 最小侵入与清晰心智模型:不重造底层运行时,只在 API 表面做减法,减少样板与心智负担。

安装

pip install harborflow

快速开始

以下示例展示如何用 HarborFlow 声明一张图并运行:

from typing_extensions import TypedDict
from harborflow import graph, node, Route, END

class State(TypedDict):
    messages: list[tuple[str, str]]

@graph(state=State, start="agent", finish=END, name="SimpleChat")
class SimpleChat:
    @node
    def agent(self, state: State) -> Route:
        last = state["messages"][-1][1] if state["messages"] else "none"
        reply = f"echo: {last}"
        return {"messages": [("assistant", reply)]}

    @node
    def done(self, state: State) -> Route:
        return Route(goto=END, update={})

if __name__ == "__main__":
    app = SimpleChat().compile()
    result = app.invoke({"messages": [("user", "hello")]} )
    print(result)

高级功能示例

异步节点支持

import asyncio
from harborflow import graph, node, Route, END, compile_graph_async

@graph(state=dict, start="async_processor", finish=END)
class AsyncWorkflow:
    @node
    async def async_processor(self, state):
        await asyncio.sleep(0.1)  # 异步操作
        return Route(goto=END, update={"result": "completed"})

# 异步编译和调用
async def main():
    flow = AsyncWorkflow()
    app = await compile_graph_async(flow)
    result = await app.ainvoke({"data": "test"})
    print(result)

asyncio.run(main())

条件路由

from harborflow import ConditionalRoute

def smart_router(state: dict) -> ConditionalRoute:
    if state.get("urgent", False):
        return ConditionalRoute.when(
            lambda s: s.get("urgent", False),
            "fast_processor",
            priority=10
        )
    return Route(goto="normal_processor", update={})

错误处理和状态验证

from harborflow import NodeConfig, validate_state_transition

# 配置节点超时和重试
node_config = NodeConfig(
    timeout=5.0,
    max_retries=3,
    retry_delay=1.0
)

# 验证状态转换
validate_state_transition(
    old_state, new_state,
    required_fields=["results"],
    immutable_fields=["id", "timestamp"]
)

动态路由示例(返回即路由)

from harborflow import Route, END
from typing import Literal

def agent(state: dict) -> Route[Literal["tool", END]]:
    need_tool = state.get("need_tool", False)
    if need_tool:
        return Route(goto="tool", update={"messages": [("assistant", "using tool...")]})
    return Route(goto=END, update={"messages": [("assistant", "bye")]})

核心抽象

  • graph:类级图声明装饰器,提供 statestartfinishname 等配置,编译后返回 LangGraph CompiledStateGraph
  • node:方法级节点声明装饰器,节点函数应接收 state 并返回 Routedict(顺序更新)。支持同步和异步函数。
  • parallel_node:并行节点装饰器,标记可并行执行的节点。
  • Route:动态路由+状态更新的统一返回对象,语义等价于 LangGraph Command(goto, update)
  • ConditionalRoute:条件路由,支持基于状态的动态分支和优先级控制。
  • NodeConfig:节点配置,支持超时、重试和错误处理策略。
  • END:LangGraph 的结束哨兵常量,表示流程结束。
  • compile_graph_async:异步编译函数,支持包含异步节点的图编译。

设计原则(摘自架构与蓝图)

  • 统一“返回即路由”心智模型:读节点函数的 return 即可读懂流程如何前进。
  • 编译期尽可能多做事:自动推断节点类型(顺序/动态),为顺序节点生成静态边;动态节点不生成静态边,避免与 goto 混用。
  • 保持 LangGraph 能力透明:invoke/stream/ainvoke/checkpoint 与 Studio/观测生态无缝对接。
  • v1.0 边界:不内建 Guard/错误处理 DSL/复杂调度,只做极薄封装,保持可演进空间。

API 速览

  • @graph(state, start, finish=END, name=...)
  • @node(name=None)
  • Route(goto: str|END, update: Mapping|Any)
  • compile() -> CompiledStateGraph
  • 运行:app.invoke(initial_state) / app.stream(...) / app.ainvoke(...)

版本与发布

  • 目标对齐 LangGraph >= 1.0.0
  • 使用 GitHub Actions + PyPI Trusted Publishing 自动发布。
  • 版本语义:在功能稳定前以 0.x 迭代,小版本发布修复与小增强。

贡献指南

  • 欢迎通过 Issue / PR 参与,建议基于 Docstring(L3)与测试协作,保持实现与契约一致。
  • 重要行为变更请记录到 specs/DEVELOPMENT_DIARY.md(若启用)。

许可证

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

harborflow-0.1.4.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

harborflow-0.1.4-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file harborflow-0.1.4.tar.gz.

File metadata

  • Download URL: harborflow-0.1.4.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for harborflow-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1e444f2fc558ab3fdeaa8f81ace896ca81c4c255fbcfd3d1c44a349a3730d367
MD5 30cd11ee1fd825960b3d443010cc95c8
BLAKE2b-256 daf518eb2aed8144ce905945638692f62f8b5eec485d1e2a601ed809d0ed4f97

See more details on using hashes here.

Provenance

The following attestation bundles were made for harborflow-0.1.4.tar.gz:

Publisher: release.yml on ailijian/harbor-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file harborflow-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: harborflow-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for harborflow-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0a3ad8809942f9e11a43ae2e0e264a6c375dfe5948a53b7cfae5427b634ef4c5
MD5 defcfd9d0f101c618417f8657150ce19
BLAKE2b-256 96b62611885ab30b67786128cb100536d86cae2f0f80b2c554ee8df28f77ebb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for harborflow-0.1.4-py3-none-any.whl:

Publisher: release.yml on ailijian/harbor-flow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page