feat: add workflow action runtime policies
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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" && (
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user