feat: add workflow action runtime policies

This commit is contained in:
Xin Wang
2026-07-31 23:30:04 +08:00
parent c2f0f5eb04
commit f155f98e6e
10 changed files with 445 additions and 71 deletions

View File

@@ -26,7 +26,10 @@ import {
TabsList,
TabsTrigger,
} from "@/components/ui/tabs";
import type { WorkflowGraph } from "@/components/workflow/specs";
import {
actionResultAssignmentMode,
type WorkflowGraph,
} from "@/components/workflow/specs";
import type { DynamicVariableDefinition } from "@/lib/api";
const DYNAMIC_VARIABLE_PATTERN = /{{\s*([A-Za-z][A-Za-z0-9_]{0,63})\s*}}/g;
@@ -93,6 +96,12 @@ export function activeWorkflowDynamicVariableDefinitions(
}
}
for (const node of graph.nodes) {
if (
node.type === "action" &&
actionResultAssignmentMode(node.data) !== "override"
) {
continue;
}
for (const name of Object.keys(node.data.resultAssignments ?? {})) {
if (isPublicDynamicVariableName(name)) names.add(name);
}

View File

@@ -955,7 +955,8 @@ function ClientToolFields({
</div>
<div className="mt-0.5 text-xs text-muted-foreground">
status=ok
status=ok Action
</div>
</div>
<Switch
@@ -1065,7 +1066,8 @@ function ToolRuntimeFields({
<div>
<div className="text-sm font-medium text-foreground"></div>
<div className="mt-1 text-xs leading-5 text-muted-foreground">
Agent
Agent Workflow Action
使
</div>
</div>
<Field label="执行模式">
@@ -1088,7 +1090,7 @@ function ToolRuntimeFields({
<div>
<div className="text-sm font-medium text-foreground"></div>
<div className="mt-0.5 text-xs text-muted-foreground">
Agent
Agent
</div>
</div>
<Switch

View File

@@ -70,6 +70,12 @@ function defaultNodeData(spec: RuntimeNodeSpec): WorkflowNodeData {
entryMode: "wait_user",
entrySpeech: "",
}
: spec.type === "action"
? {
arguments: {},
resultAssignmentMode: "inherit",
userInputPolicy: "queue",
}
: {}),
};
for (const field of spec.fields) {

View File

@@ -6,7 +6,11 @@ import { SectionCard } from "@/components/editor/section-card";
import { Textarea } from "@/components/ui/textarea";
import { NodeSelect } from "./controls";
import type { WorkflowNodeData } from "../specs";
import {
actionResultAssignmentMode,
actionUserInputPolicy,
type WorkflowNodeData,
} from "../specs";
import type { ModelOption } from "../types";
type ActionNodePanelProps = {
@@ -32,11 +36,14 @@ export function ActionNodePanel({
setAssignmentsJson,
commitActionJson,
}: ActionNodePanelProps) {
const resultAssignmentMode = actionResultAssignmentMode(draft);
const userInputPolicy = actionUserInputPolicy(draft);
return (
<SectionCard
icon={<Zap size={15} />}
title="工具执行"
description="确定性调用工具,并将响应字段写入会话动态变量"
description="确定性调用工具,并控制结果写入及执行期间的用户输入"
>
<NodeSelect
label="执行工具"
@@ -63,24 +70,60 @@ export function ActionNodePanel({
使 {"{{variable}}"}
</span>
</label>
<label className="block">
<div className="mb-1.5 text-sm font-medium text-foreground">
JSON
</div>
<Textarea
rows={4}
value={assignmentsJson}
onChange={(event) => {
const value = event.target.value;
setAssignmentsJson(value);
commitActionJson(argumentsJson, value);
}}
className="field-sizing-fixed min-h-28 resize-y border-hairline-strong bg-background font-mono text-xs text-foreground placeholder:text-muted-soft"
/>
<span className="mt-1.5 block text-xs text-muted-foreground">
JSON Path
</span>
</label>
<NodeSelect
label="结果变量更新"
value={resultAssignmentMode}
options={[
{ value: "inherit", label: "继承工具高级设置" },
{ value: "override", label: "使用节点自定义映射" },
{ value: "none", label: "不更新动态变量" },
]}
onChange={(value) =>
set("resultAssignmentMode", value || "inherit")
}
allowNone={false}
/>
{resultAssignmentMode === "override" ? (
<label className="block">
<div className="mb-1.5 text-sm font-medium text-foreground">
JSON
</div>
<Textarea
rows={4}
value={assignmentsJson}
onChange={(event) => {
const value = event.target.value;
setAssignmentsJson(value);
commitActionJson(argumentsJson, value);
}}
className="field-sizing-fixed min-h-28 resize-y border-hairline-strong bg-background font-mono text-xs text-foreground placeholder:text-muted-soft"
/>
<span className="mt-1.5 block text-xs text-muted-foreground">
JSON Path Action
</span>
</label>
) : (
<p className="-mt-1 text-xs leading-5 text-muted-foreground">
{resultAssignmentMode === "inherit"
? "使用所选工具高级设置中的动态变量赋值配置。"
: "工具执行结果不会写入会话动态变量。"}
</p>
)}
<NodeSelect
label="执行期间用户输入"
value={userInputPolicy}
options={[
{ value: "queue", label: "允许输入并排队(默认)" },
{ value: "block", label: "暂时禁止输入" },
]}
onChange={(value) => set("userInputPolicy", value || "queue")}
allowNone={false}
/>
<p className="-mt-1 text-xs leading-5 text-muted-foreground">
{userInputPolicy === "queue"
? "用户输入会保留Action 完成并进入后续节点后再处理。"
: "Action 执行期间忽略新的语音、文本和图片输入。"}
</p>
{jsonError && (
<p role="alert" className="text-xs text-destructive">
{jsonError}

View File

@@ -52,8 +52,12 @@ export function NodeSettingsPanel({
setDraft(next);
onChange(next);
};
const set = (key: string, val: unknown) =>
const set = (key: string, val: unknown) => {
if (key === "resultAssignmentMode" && val !== "override") {
setJsonError("");
}
commit({ ...draft, [key]: val });
};
const setPatch = (patch: Partial<WorkflowNodeData>) =>
commit({ ...draft, ...patch });
const commitActionJson = (
@@ -230,44 +234,17 @@ export function NodeSettingsPanel({
)}
{spec.type === "action" && (
<>
<NodeSelect
label="确定性执行工具"
value={(draft.toolId as string) || ""}
options={toolOptions}
onChange={(value) => set("toolId", value || "")}
noneLabel="请选择工具"
/>
<div className="flex flex-col gap-2">
<label className="text-sm font-medium text-foreground"> JSON</label>
<Textarea
rows={5}
value={argumentsJson}
onChange={(event) => {
const value = event.target.value;
setArgumentsJson(value);
commitActionJson(value, assignmentsJson);
}}
className="field-sizing-fixed min-h-32 resize-y border-hairline-strong bg-background text-foreground placeholder:text-muted-soft"
/>
<span className="text-xs text-muted-soft">使 {"{{variable}}"} </span>
</div>
<div className="flex flex-col gap-2">
<label className="text-sm font-medium text-foreground"> JSON</label>
<Textarea
rows={4}
value={assignmentsJson}
onChange={(event) => {
const value = event.target.value;
setAssignmentsJson(value);
commitActionJson(argumentsJson, value);
}}
className="field-sizing-fixed min-h-28 resize-y border-hairline-strong bg-background text-foreground placeholder:text-muted-soft"
/>
<span className="text-xs text-muted-soft"> JSON Path</span>
</div>
{jsonError && <span className="text-xs text-destructive">{jsonError}</span>}
</>
<ActionNodePanel
draft={draft}
set={set}
toolOptions={toolOptions}
argumentsJson={argumentsJson}
assignmentsJson={assignmentsJson}
jsonError={jsonError}
setArgumentsJson={setArgumentsJson}
setAssignmentsJson={setAssignmentsJson}
commitActionJson={commitActionJson}
/>
)}
{spec.type === "handoff" && (

View File

@@ -10,6 +10,8 @@ export type WorkflowNodeType = "start" | "agent" | "action" | "handoff" | "end";
export type ContextPolicy = "inherit" | "fresh";
export type KnowledgeMode = "automatic" | "on_demand" | "disabled";
export type AgentEntryMode = "wait_user" | "generate" | "fixed_speech";
export type ActionResultAssignmentMode = "inherit" | "override" | "none";
export type ActionUserInputPolicy = "queue" | "block";
export type EdgeMode = "llm" | "expression" | "always";
export type ExpressionOperator =
| "eq"
@@ -44,7 +46,9 @@ export type WorkflowNodeData = {
turnConfig?: TurnConfig;
toolId?: string;
arguments?: Record<string, unknown>;
resultAssignmentMode?: ActionResultAssignmentMode;
resultAssignments?: Record<string, string>;
userInputPolicy?: ActionUserInputPolicy;
targetType?: "ai" | "human" | "queue" | "phone";
target?: string;
message?: string;
@@ -52,6 +56,33 @@ export type WorkflowNodeData = {
[key: string]: unknown;
};
/**
* Resolve how an Action writes tool results while preserving workflows saved
* before resultAssignmentMode existed. Those nodes explicitly passed an empty
* mapping at runtime, so an absent/empty mapping means "none", not "inherit".
*/
export function actionResultAssignmentMode(
data: WorkflowNodeData,
): ActionResultAssignmentMode {
if (
data.resultAssignmentMode === "inherit" ||
data.resultAssignmentMode === "override" ||
data.resultAssignmentMode === "none"
) {
return data.resultAssignmentMode;
}
return Object.keys(data.resultAssignments ?? {}).length > 0
? "override"
: "none";
}
/** Older Action nodes allowed input to continue, which matches queue. */
export function actionUserInputPolicy(
data: WorkflowNodeData,
): ActionUserInputPolicy {
return data.userInputPolicy === "block" ? "block" : "queue";
}
export type ExpressionRule = {
variable: string;
operator: ExpressionOperator;