joeyutong commented on code in PR #1382: URL: https://github.com/apache/flink-agents/pull/1382#discussion_r4677733081
########## api/src/main/java/org/apache/flink/agents/api/media/BaseMediaModelSetup.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.media; + +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; + +import javax.annotation.Nullable; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Abstract base class for {@link ResourceType#MEDIA_MODEL} capability setups. + * + * <p>Concrete task setups extend one of the four task-specific subclasses ({@link + * BaseSpeechSynthesisModelSetup}, {@link BaseTranscriptionModelSetup}, {@link + * BaseImageGenerationModelSetup}, or {@link BaseVideoGenerationModelSetup}) and optionally + * implement {@link AsyncJobModel} when backed by resumable provider jobs. A setup can either be + * declared via {@link ResourceDescriptor} referencing a {@link ResourceType#MEDIA_MODEL_PROVIDER}, + * returned directly by {@link BaseMediaModelProvider} factory methods, or manage its own standalone + * client. + */ +public abstract class BaseMediaModelSetup extends Resource { + @Nullable protected final ResourceDescriptor descriptor; + @Nullable private final String providerName; + private final String model; + private final Map<String, Object> modelParameters; + @Nullable private transient BaseMediaModelProvider boundProvider; + private transient boolean ownsProviderBinding; + + /** + * Construct a media model setup from a resource descriptor. + * + * <p>Requires a non-blank {@code model} argument; {@code provider} and {@code model_parameters} + * are optional (validated when set). + */ + protected BaseMediaModelSetup( + ResourceDescriptor descriptor, @Nullable ResourceContext resourceContext) { + super(Objects.requireNonNull(descriptor, "descriptor"), resourceContext); + this.descriptor = descriptor; + Map<String, Object> rawArguments = descriptor.getInitialArguments(); + Map<String, Object> arguments = rawArguments == null ? Map.of() : rawArguments; + this.providerName = optionalNonBlankString(arguments, "provider"); + this.model = requireNonBlankString(arguments, "model"); + this.modelParameters = extractModelParameters(arguments); + } + + /** + * Construct a directly configured or provider-bound media model setup without a named provider + * reference. + */ + protected BaseMediaModelSetup( + String model, + @Nullable Map<String, Object> modelParameters, + @Nullable ResourceDescriptor descriptor, + @Nullable ResourceContext resourceContext) { + this(null, model, modelParameters, descriptor, resourceContext); + } + + /** + * Construct a media model setup with an explicit model, optional provider name, and model + * parameters. + */ + protected BaseMediaModelSetup( + @Nullable String providerName, + String model, + @Nullable Map<String, Object> modelParameters, + @Nullable ResourceDescriptor descriptor, + @Nullable ResourceContext resourceContext) { + super(descriptor, resourceContext); + this.descriptor = descriptor; + if (providerName != null && providerName.isBlank()) { + throw new IllegalArgumentException( + "Media model setup 'provider' must be a non-blank string when set."); + } + this.providerName = providerName; + this.model = + validateNonBlank(model, "Media model setup 'model' must be a non-blank string."); + this.modelParameters = + modelParameters == null ? Map.of() : MediaParameters.copy(modelParameters); + } + + @Nullable + public String getProviderName() { + return providerName; + } + + public String getModel() { + return model; + } + + public Map<String, Object> getModelParameters() { + return modelParameters; + } + + @Override + public ResourceType getResourceType() { + return ResourceType.MEDIA_MODEL; + } + + /** Expected provider base class enforced when resolving or binding a provider. */ + protected Class<? extends BaseMediaModelProvider> getProviderType() { + return BaseMediaModelProvider.class; + } + + /** Attach a provider instance directly when constructed from a provider factory method. */ + protected void bindProvider(BaseMediaModelProvider provider) { + Objects.requireNonNull(provider, "provider"); + Class<? extends BaseMediaModelProvider> expectedType = getProviderType(); + if (!expectedType.isInstance(provider)) { + throw new IllegalStateException( + getClass().getSimpleName() + + " requires a " + + expectedType.getSimpleName() + + ", got " + + provider.getClass().getName()); + } + this.boundProvider = provider; + this.ownsProviderBinding = false; + } + + @Override + public void open() throws Exception { + super.open(); + if (boundProvider == null && providerName != null) { + boundProvider = resolveProvider(); + ownsProviderBinding = true; + } + } + + @Override + public void close() throws Exception { + if (ownsProviderBinding) { + boundProvider = null; + ownsProviderBinding = false; + } + super.close(); + } + + /** Return the attached or opened {@link BaseMediaModelProvider} for this setup. */ + protected BaseMediaModelProvider getProvider() { + if (boundProvider == null) { + throw new IllegalStateException( + "Media model setup is not open. Call open() before using it."); + } + return boundProvider; + } + + /** Resolve the configured {@link ResourceType#MEDIA_MODEL_PROVIDER} from context. */ + protected BaseMediaModelProvider resolveProvider() throws Exception { + if (boundProvider != null) { + return boundProvider; + } + if (providerName == null) { + throw new IllegalStateException("Media model setup 'provider' is not configured."); + } + if (resourceContext == null) { + throw new IllegalStateException( + "ResourceContext is required to resolve media model provider '" + + providerName + + "'."); + } + Resource resource = + resourceContext.getResource(providerName, ResourceType.MEDIA_MODEL_PROVIDER); + Class<? extends BaseMediaModelProvider> expectedType = getProviderType(); + if (!expectedType.isInstance(resource)) { + throw new IllegalStateException( + "Resource '" + + providerName + + "' of type MEDIA_MODEL_PROVIDER is not a " + + expectedType.getSimpleName() + + ": " + + (resource == null ? "null" : resource.getClass().getName())); + } + return expectedType.cast(resource); + } + + @Nullable + protected static String optionalNonBlankString( Review Comment: Done. Added `StringValidationUtils` (`requireNonBlank` / `optionalNonBlank`) in Java and consolidated Python validation onto Pydantic field validators. ########## api/src/main/java/org/apache/flink/agents/api/media/BaseMediaModelSetup.java: ########## @@ -0,0 +1,251 @@ +/* + * 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.media; + +import org.apache.flink.agents.api.resource.Resource; +import org.apache.flink.agents.api.resource.ResourceContext; +import org.apache.flink.agents.api.resource.ResourceDescriptor; +import org.apache.flink.agents.api.resource.ResourceType; + +import javax.annotation.Nullable; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Objects; + +/** + * Abstract base class for {@link ResourceType#MEDIA_MODEL} capability setups. + * + * <p>Concrete task setups extend one of the four task-specific subclasses ({@link + * BaseSpeechSynthesisModelSetup}, {@link BaseTranscriptionModelSetup}, {@link + * BaseImageGenerationModelSetup}, or {@link BaseVideoGenerationModelSetup}) and optionally + * implement {@link AsyncJobModel} when backed by resumable provider jobs. A setup can either be + * declared via {@link ResourceDescriptor} referencing a {@link ResourceType#MEDIA_MODEL_PROVIDER}, + * returned directly by {@link BaseMediaModelProvider} factory methods, or manage its own standalone + * client. + */ +public abstract class BaseMediaModelSetup extends Resource { + @Nullable protected final ResourceDescriptor descriptor; + @Nullable private final String providerName; + private final String model; + private final Map<String, Object> modelParameters; + @Nullable private transient BaseMediaModelProvider boundProvider; + private transient boolean ownsProviderBinding; + + /** + * Construct a media model setup from a resource descriptor. + * + * <p>Requires a non-blank {@code model} argument; {@code provider} and {@code model_parameters} + * are optional (validated when set). + */ + protected BaseMediaModelSetup( + ResourceDescriptor descriptor, @Nullable ResourceContext resourceContext) { + super(Objects.requireNonNull(descriptor, "descriptor"), resourceContext); + this.descriptor = descriptor; + Map<String, Object> rawArguments = descriptor.getInitialArguments(); + Map<String, Object> arguments = rawArguments == null ? Map.of() : rawArguments; + this.providerName = optionalNonBlankString(arguments, "provider"); + this.model = requireNonBlankString(arguments, "model"); + this.modelParameters = extractModelParameters(arguments); + } + + /** + * Construct a directly configured or provider-bound media model setup without a named provider + * reference. + */ + protected BaseMediaModelSetup( Review Comment: Done. Simplified `BaseMediaModelSetup` and all four task setup base classes to a single 2-argument `(ResourceDescriptor, ResourceContext)` constructor matching `BaseChatModelSetup`. ########## api/src/main/java/org/apache/flink/agents/api/media/MediaParameters.java: ########## @@ -0,0 +1,84 @@ +/* + * 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.media; + +import javax.annotation.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** Recursively snapshots nested {@link Map} and {@link List} model parameters for immutability. */ +final class MediaParameters { Review Comment: Done. Removed `MediaParameters` and deep immutable snapshots in both Java and Python; requests retain caller-supplied maps directly (copying only when merging/mutating per call), with docs and tests updated to match. ########## api/src/main/java/org/apache/flink/agents/api/media/MediaJob.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.media; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import javax.annotation.Nullable; + +import java.util.Objects; + +/** Handle of a submitted asynchronous media task. */ +public final class MediaJob { + private final String provider; Review Comment: Done. Simplified `MediaJob` to `(job_id, submitted_at_ms, expires_at_ms)`, removed `MediaTask`, `_check_job()`, and bridge job-matching checks, and documented caller ownership for matching handles to the right setup. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
