Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.flink.agents.api;

import org.apache.flink.agents.api.agents.Agent;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.table.api.Schema;
import org.apache.flink.table.api.Table;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* limitations under the License.
*/

package org.apache.flink.agents.api;
package org.apache.flink.agents.api.agents;

import org.apache.flink.agents.api.Event;
import org.apache.flink.agents.api.resource.ResourceDescriptor;
import org.apache.flink.agents.api.resource.ResourceType;
import org.apache.flink.agents.api.resource.SerializableResource;
Expand Down Expand Up @@ -108,4 +109,22 @@ public Agent addResource(String name, ResourceType type, Object instance) {
}
return this;
}

public enum ErrorHandlingStrategy {
FAIL("fail"),
RETRY("retry"),
IGNORE("ignore");

private final String value;

ErrorHandlingStrategy(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}

public static String STRUCTURED_OUTPUT = "structured_output";
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

import org.apache.flink.agents.api.configuration.ConfigOption;

/** Config Options for {@link ReActAgent}. */
public class ReActAgentConfigOptions {
/** The option specifies the error handling strategy for react agent. */
public static final ConfigOption<ReActAgent.ErrorHandlingStrategy> ERROR_HANDLING_STRATEGY =
public class AgentExecutionOptions {
public static final ConfigOption<Agent.ErrorHandlingStrategy> ERROR_HANDLING_STRATEGY =
new ConfigOption<>(
"error-handling-strategy",
ReActAgent.ErrorHandlingStrategy.class,
ReActAgent.ErrorHandlingStrategy.FAIL);
Agent.ErrorHandlingStrategy.class,
Agent.ErrorHandlingStrategy.FAIL);

public static final ConfigOption<Integer> MAX_RETRIES =
new ConfigOption<>("max-retries", Integer.class, 0);
}
134 changes: 134 additions & 0 deletions api/src/main/java/org/apache/flink/agents/api/agents/OutputSchema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.flink.agents.api.agents;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JacksonException;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.DeserializationContext;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonNode;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Helper class for {@link RowTypeInfo} serialization.
*
* <p>Currently, only support row contains basic type.
*/
@VisibleForTesting
@JsonSerialize(using = OutputSchema.OutputSchemaJsonSerializer.class)
@JsonDeserialize(using = OutputSchema.OutputSchemaJsonDeserializer.class)
public class OutputSchema {
private final RowTypeInfo schema;

public OutputSchema(RowTypeInfo schema) {
this.schema = schema;
for (TypeInformation<?> info : schema.getFieldTypes()) {
if (!info.isBasicType()) {
throw new IllegalArgumentException(
"Currently, output schema only support row contains basic type.");
}
}
}

public RowTypeInfo getSchema() {
return schema;
}

public static class OutputSchemaJsonSerializer extends StdSerializer<OutputSchema> {

protected OutputSchemaJsonSerializer() {
super(OutputSchema.class);
}

@Override
public void serialize(
OutputSchema schema,
JsonGenerator jsonGenerator,
SerializerProvider serializerProvider)
throws IOException {
RowTypeInfo typeInfo = schema.getSchema();
jsonGenerator.writeStartObject();

jsonGenerator.writeFieldName("fieldNames");
jsonGenerator.writeStartArray();
for (String name : typeInfo.getFieldNames()) {
jsonGenerator.writeString(name);
}
jsonGenerator.writeEndArray();

// TODO: support type information which is not basic.
jsonGenerator.writeFieldName("types");
jsonGenerator.writeStartArray();
for (TypeInformation<?> info : typeInfo.getFieldTypes()) {
jsonGenerator.writeObject(info.getTypeClass());
}
jsonGenerator.writeEndArray();

jsonGenerator.writeEndObject();
}
}

public static class OutputSchemaJsonDeserializer extends StdDeserializer<OutputSchema> {
private static final ObjectMapper mapper = new ObjectMapper();

protected OutputSchemaJsonDeserializer() {
super(OutputSchema.class);
}

@Override
public OutputSchema deserialize(
JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException, JacksonException {
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
List<String> fieldNames = new ArrayList<>();
node.get("fieldNames").forEach(fieldNameNode -> fieldNames.add(fieldNameNode.asText()));
List<TypeInformation<?>> types = new ArrayList<>();
node.get("types")
.forEach(
typeNode -> {
try {
types.add(
BasicTypeInfo.getInfoFor(
mapper.treeToValue(typeNode, Class.class)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
});

return new OutputSchema(
new RowTypeInfo(
types.toArray(new TypeInformation[0]),
fieldNames.toArray(new String[0])));
}
}
}
Loading
Loading