类 Command<Resume, Update, Nodes>

一个或多个用于更新图状态并向节点发送消息的命令。可用于将路由逻辑与状态更新相结合,以代替条件边。

示例

import { Annotation, Command } from "@langchain/langgraph";

// Define graph state
const StateAnnotation = Annotation.Root({
foo: Annotation<string>,
});

// Define the nodes
const nodeA = async (_state: typeof StateAnnotation.State) => {
console.log("Called A");
// this is a replacement for a real conditional edge function
const goto = Math.random() > .5 ? "nodeB" : "nodeC";
// note how Command allows you to BOTH update the graph state AND route to the next node
return new Command({
// this is the state update
update: {
foo: "a",
},
// this is a replacement for an edge
goto,
});
};

// Nodes B and C are unchanged
const nodeB = async (state: typeof StateAnnotation.State) => {
console.log("Called B");
return {
foo: state.foo + "|b",
};
}

const nodeC = async (state: typeof StateAnnotation.State) => {
console.log("Called C");
return {
foo: state.foo + "|c",
};
}

import { StateGraph } from "@langchain/langgraph";

// NOTE: there are no edges between nodes A, B and C!
const graph = new StateGraph(StateAnnotation)
.addNode("nodeA", nodeA, {
ends: ["nodeB", "nodeC"],
})
.addNode("nodeB", nodeB)
.addNode("nodeC", nodeC)
.addEdge("__start__", "nodeA")
.compile();

await graph.invoke({ foo: "" });

// Randomly oscillates between
// { foo: 'a|c' } and { foo: 'a|b' }

类型参数

  • Resume = unknown
  • Update extends Record<string, unknown> = Record<string, unknown>
  • Nodes extends string = string

继承关系

  • CommandInstance
    • 命令

构造函数

属性

[COMMAND_SYMBOL]: true
goto?: Nodes | Send<Nodes, any> | (Nodes | Send<Nodes, any>)[]

可以是以下之一

  • 要导航到的下一个节点的名称(属于指定 `graph` 的任何节点)
  • 要导航到的下一个节点名称序列
  • Send 对象(用于使用 Send 对象中提供的确切输入执行一个节点)
  • Send 对象的序列
graph?: string

要将命令发送到的图。支持的值为

  • None: 当前图(默认)
  • 要将命令发送到的图的具体名称
  • Command.PARENT:最近的父图(仅在从子图中的节点返回时支持)
lc_direct_tool_output: boolean
lg_name: "Command" = "Command"
resume?: Resume

用于恢复执行的值。与 interrupt 一起使用。

update?: [string, unknown][] | Update

因执行返回该命令的节点而应用于图状态的更新。写入状态的方式就好像该节点只是返回了这个值,而不是 Command 对象一样。

PARENT: string

方法

  • 返回 {
        goto: undefined | Nodes | {
            args: any;
            lg_name: string;
            node: Nodes;
        } | (Nodes | {
            args: any;
            lg_name: string;
            node: Nodes;
        })[];
        lg_name: string;
        resume: undefined | Resume;
        update: undefined | [string, unknown][] | Update;
    }

    • goto: undefined | Nodes | {
          args: any;
          lg_name: string;
          node: Nodes;
      } | (Nodes | {
          args: any;
          lg_name: string;
          node: Nodes;
      })[]
    • lg_name: string
    • resume: undefined | Resume
    • update: undefined | [string, unknown][] | Update