Skip to content

Commit 51d6af6

Browse files
committed
Fix #732 - Add static imports and consumer recipes for Agentic DSL
Signed-off-by: Ricardo Zanini <ricardozanini@gmail.com>
1 parent c868d49 commit 51d6af6

File tree

10 files changed

+400
-58
lines changed

10 files changed

+400
-58
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.configurer;
17+
18+
import io.serverlessworkflow.fluent.agentic.AgentDoTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface AgentTaskConfigurer extends Consumer<AgentDoTaskBuilder> {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.configurer;
17+
18+
import io.serverlessworkflow.fluent.func.FuncPredicateEventPropertiesBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface FuncPredicateEventConfigurer
23+
extends Consumer<FuncPredicateEventPropertiesBuilder> {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.configurer;
17+
18+
import io.serverlessworkflow.fluent.agentic.AgentListenTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface ListenConfigurer extends Consumer<AgentListenTaskBuilder> {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.configurer;
17+
18+
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
19+
import java.util.function.Consumer;
20+
21+
@FunctionalInterface
22+
public interface SwitchCaseConfigurer
23+
extends Consumer<FuncSwitchTaskBuilder.SwitchCasePredicateBuilder> {}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.dsl;
17+
18+
import io.cloudevents.CloudEventData;
19+
import io.serverlessworkflow.api.types.FlowDirectiveEnum;
20+
import io.serverlessworkflow.fluent.agentic.AgentDoTaskBuilder;
21+
import io.serverlessworkflow.fluent.agentic.configurer.AgentTaskConfigurer;
22+
import io.serverlessworkflow.fluent.agentic.configurer.FuncPredicateEventConfigurer;
23+
import io.serverlessworkflow.fluent.agentic.configurer.SwitchCaseConfigurer;
24+
import io.serverlessworkflow.fluent.func.FuncCallTaskBuilder;
25+
import io.serverlessworkflow.fluent.func.FuncEmitTaskBuilder;
26+
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
27+
import java.util.List;
28+
import java.util.Objects;
29+
import java.util.function.Consumer;
30+
import java.util.function.Function;
31+
import java.util.function.Predicate;
32+
33+
public final class AgenticDSL {
34+
35+
private AgenticDSL() {}
36+
37+
public static <T, V> Consumer<FuncCallTaskBuilder> fn(
38+
Function<T, V> function, Class<T> argClass) {
39+
return f -> f.function(function, argClass);
40+
}
41+
42+
public static <T, V> Consumer<FuncCallTaskBuilder> fn(Function<T, V> function) {
43+
return f -> f.function(function);
44+
}
45+
46+
public static Consumer<FuncSwitchTaskBuilder> cases(SwitchCaseConfigurer... cases) {
47+
return s -> {
48+
for (SwitchCaseConfigurer c : cases) {
49+
s.onPredicate(c);
50+
}
51+
};
52+
}
53+
54+
public static <T> SwitchCaseSpec<T> on(Predicate<T> when, Class<T> whenClass) {
55+
return new SwitchCaseSpec<T>().when(when, whenClass);
56+
}
57+
58+
public static <T> SwitchCaseSpec<T> on(Predicate<T> when) {
59+
return new SwitchCaseSpec<T>().when(when);
60+
}
61+
62+
public static SwitchCaseConfigurer onDefault(String task) {
63+
return s -> s.then(task);
64+
}
65+
66+
public static SwitchCaseConfigurer onDefault(FlowDirectiveEnum directive) {
67+
return s -> s.then(directive);
68+
}
69+
70+
public static ListenSpec to() {
71+
return new ListenSpec();
72+
}
73+
74+
public static ListenSpec toOne(String type) {
75+
return new ListenSpec().one(e -> e.type(type));
76+
}
77+
78+
public static ListenSpec toAll(String... types) {
79+
FuncPredicateEventConfigurer[] events = new FuncPredicateEventConfigurer[types.length];
80+
for (int i = 0; i < types.length; i++) {
81+
events[i] = event(types[i]);
82+
}
83+
return new ListenSpec().all(events);
84+
}
85+
86+
public static ListenSpec toAny(String... types) {
87+
FuncPredicateEventConfigurer[] events = new FuncPredicateEventConfigurer[types.length];
88+
for (int i = 0; i < types.length; i++) {
89+
events[i] = event(types[i]);
90+
}
91+
return new ListenSpec().any(events);
92+
}
93+
94+
public static FuncPredicateEventConfigurer event(String type) {
95+
return e -> e.type(type);
96+
}
97+
98+
// TODO: expand the `event` static ref with more attributes based on community feedback
99+
100+
public static <T> Consumer<FuncEmitTaskBuilder> event(
101+
String type, Function<T, CloudEventData> function) {
102+
return event -> event.event(e -> e.type(type).data(function));
103+
}
104+
105+
public static <T> Consumer<FuncEmitTaskBuilder> event(
106+
String type, Function<T, CloudEventData> function, Class<T> clazz) {
107+
return event -> event.event(e -> e.type(type).data(function, clazz));
108+
}
109+
110+
// --------- Tasks ------ //
111+
public static Consumer<AgentDoTaskBuilder> doTasks(AgentTaskConfigurer... steps) {
112+
Objects.requireNonNull(steps, "Steps in a tasks are required");
113+
final List<AgentTaskConfigurer> snapshot = List.of(steps.clone());
114+
return list -> snapshot.forEach(s -> s.accept(list));
115+
}
116+
117+
public static <T, V> AgentTaskConfigurer function(Function<T, V> function, Class<T> argClass) {
118+
return list -> list.callFn(fn(function, argClass));
119+
}
120+
121+
public static <T, V> AgentTaskConfigurer function(Function<T, V> function) {
122+
return list -> list.callFn(fn(function));
123+
}
124+
125+
public static AgentTaskConfigurer agent(Object agent) {
126+
return list -> list.agent(agent);
127+
}
128+
129+
public static AgentTaskConfigurer emit(Consumer<FuncEmitTaskBuilder> event) {
130+
return list -> list.emit(event);
131+
}
132+
133+
public static AgentTaskConfigurer switchCase(Consumer<FuncSwitchTaskBuilder> switchCase) {
134+
return list -> list.switchCase(switchCase);
135+
}
136+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.dsl;
17+
18+
import io.serverlessworkflow.fluent.agentic.AgentListenTaskBuilder;
19+
import io.serverlessworkflow.fluent.agentic.configurer.FuncPredicateEventConfigurer;
20+
import io.serverlessworkflow.fluent.agentic.configurer.ListenConfigurer;
21+
import io.serverlessworkflow.fluent.func.FuncEventFilterBuilder;
22+
import io.serverlessworkflow.fluent.func.FuncListenToBuilder;
23+
import java.util.Objects;
24+
import java.util.function.Consumer;
25+
26+
public class ListenSpec implements ListenConfigurer {
27+
28+
private Consumer<FuncListenToBuilder> strategyStep;
29+
private Consumer<FuncListenToBuilder> untilStep;
30+
31+
@SuppressWarnings("unchecked")
32+
private static Consumer<FuncEventFilterBuilder>[] asFilters(
33+
FuncPredicateEventConfigurer[] events) {
34+
Consumer<FuncEventFilterBuilder>[] filters = new Consumer[events.length];
35+
for (int i = 0; i < events.length; i++) {
36+
FuncPredicateEventConfigurer ev = Objects.requireNonNull(events[i], "events[" + i + "]");
37+
filters[i] = f -> f.with(ev);
38+
}
39+
return filters;
40+
}
41+
42+
public final ListenSpec all(FuncPredicateEventConfigurer... events) {
43+
strategyStep = t -> t.all(asFilters(events));
44+
return this;
45+
}
46+
47+
public ListenSpec one(FuncPredicateEventConfigurer e) {
48+
strategyStep = t -> t.one(f -> f.with(e));
49+
return this;
50+
}
51+
52+
public final ListenSpec any(FuncPredicateEventConfigurer... events) {
53+
strategyStep = t -> t.any(asFilters(events));
54+
return this;
55+
}
56+
57+
public ListenSpec until(String expression) {
58+
untilStep = t -> t.until(expression);
59+
return this;
60+
}
61+
62+
@Override
63+
public void accept(AgentListenTaskBuilder agentListenTaskBuilder) {
64+
agentListenTaskBuilder.to(
65+
t -> {
66+
strategyStep.accept(t);
67+
if (untilStep != null) {
68+
untilStep.accept(t);
69+
}
70+
});
71+
}
72+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.agentic.dsl;
17+
18+
import io.serverlessworkflow.fluent.agentic.configurer.SwitchCaseConfigurer;
19+
import io.serverlessworkflow.fluent.func.FuncSwitchTaskBuilder;
20+
import java.util.function.Predicate;
21+
22+
public class SwitchCaseSpec<T> implements SwitchCaseConfigurer {
23+
24+
private String then = "";
25+
private Predicate<T> when;
26+
private Class<T> whenClass;
27+
28+
public SwitchCaseSpec<T> when(Predicate<T> when, Class<T> whenClass) {
29+
this.when = when;
30+
this.whenClass = whenClass;
31+
return this;
32+
}
33+
34+
public SwitchCaseSpec<T> when(Predicate<T> when) {
35+
this.when = when;
36+
return this;
37+
}
38+
39+
public SwitchCaseSpec<T> then(String directive) {
40+
this.then = directive;
41+
return this;
42+
}
43+
44+
@Override
45+
public void accept(FuncSwitchTaskBuilder.SwitchCasePredicateBuilder switchCasePredicateBuilder) {
46+
if (this.whenClass != null) {
47+
switchCasePredicateBuilder.then(this.then).when(this.when, this.whenClass);
48+
} else {
49+
switchCasePredicateBuilder.then(this.then).when(this.when);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)