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
82 changes: 82 additions & 0 deletions core/src/main/java/com/google/adk/artifacts/ArtifactService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2026 Google LLC
*
* Licensed 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 com.google.adk.artifacts;

import com.google.adk.sessions.SessionKey;
import com.google.common.collect.ImmutableList;
import com.google.genai.types.Part;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Single;

/** Standard interface for artifact services. */
public interface ArtifactService {

/**
* Saves an artifact and returns it with fileData if available.
*
* @param sessionKey the session key
* @param filename the filename
* @param artifact the artifact to save
* @return the saved artifact with fileData if available.
*/
Single<Part> saveAndReloadArtifact(SessionKey sessionKey, String filename, Part artifact);

/**
* Loads the latest version of an artifact from the service.
*
* @param sessionKey the session key
* @param filename the filename
* @return the loaded artifact with fileData if available.
*/
Maybe<Part> loadArtifact(SessionKey sessionKey, String filename);

/**
* Loads a specific version of an artifact from the service.
*
* @param sessionKey the session key
* @param filename the filename
* @param version the version
* @return the loaded artifact with fileData if available.
*/
Maybe<Part> loadArtifact(SessionKey sessionKey, String filename, int version);

/**
* Lists all the artifact filenames within a session.
*
* @param sessionKey the session key
* @return the list artifact response containing filenames
*/
Single<ListArtifactsResponse> listArtifactKeys(SessionKey sessionKey);

/**
* Deletes an artifact.
*
* @param sessionKey the session key
* @param filename the filename
*/
Completable deleteArtifact(SessionKey sessionKey, String filename);

/**
* Lists all the versions (as revision IDs) of an artifact.
*
* @param sessionKey the session key
* @param filename the filename
* @return A list of integer version numbers.
*/
Single<ImmutableList<Integer>> listVersions(SessionKey sessionKey, String filename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
import io.reactivex.rxjava3.core.Single;
import org.jspecify.annotations.Nullable;

/** Base interface for artifact services. */
public interface BaseArtifactService {
/**
* Base interface for artifact services.
*
* @deprecated Use {@link ArtifactService} instead.
*/
@Deprecated(forRemoval = true)
public interface BaseArtifactService extends ArtifactService {

/**
* Saves an artifact.
Expand All @@ -40,55 +45,40 @@ public interface BaseArtifactService {
Single<Integer> saveArtifact(
String appName, String userId, String sessionId, String filename, Part artifact);

/** Saves an artifact. */
default Single<Integer> saveArtifact(SessionKey sessionKey, String filename, Part artifact) {
return saveArtifact(
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, artifact);
}

/**
* Saves an artifact and returns it with fileData if available.
*
* <p>Implementations should override this default method for efficiency, as the default performs
* two I/O operations (save then load).
*
* @param appName the app name
* @param userId the user ID
* @param sessionId the session ID
* @param filename the filename
* @param artifact the artifact to save
* @return the saved artifact with fileData if available.
*/
default Single<Part> saveAndReloadArtifact(
String appName, String userId, String sessionId, String filename, Part artifact) {
return saveArtifact(appName, userId, sessionId, filename, artifact)
.flatMap(version -> loadArtifact(appName, userId, sessionId, filename, version).toSingle());
}

/** Saves an artifact and returns it with fileData if available. */
@Override
default Single<Part> saveAndReloadArtifact(
SessionKey sessionKey, String filename, Part artifact) {
return saveAndReloadArtifact(
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, artifact);
}

/** Loads the latest version of an artifact from the service. */
default Maybe<Part> loadArtifact(
String appName, String userId, String sessionId, String filename) {
return loadArtifact(appName, userId, sessionId, filename, /* version= */ (Integer) null);
}

/** Loads the latest version of an artifact from the service. */
@Override
default Maybe<Part> loadArtifact(SessionKey sessionKey, String filename) {
return loadArtifact(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
}

/** Loads a specific version of an artifact from the service. */
default Maybe<Part> loadArtifact(
String appName, String userId, String sessionId, String filename, int version) {
return loadArtifact(appName, userId, sessionId, filename, Integer.valueOf(version));
}

@Override
default Maybe<Part> loadArtifact(SessionKey sessionKey, String filename, int version) {
return loadArtifact(
sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename, version);
Expand All @@ -97,46 +87,24 @@ default Maybe<Part> loadArtifact(SessionKey sessionKey, String filename, int ver
Maybe<Part> loadArtifact(
String appName, String userId, String sessionId, String filename, @Nullable Integer version);

/**
* Lists all the artifact filenames within a session.
*
* @param appName the app name
* @param userId the user ID
* @param sessionId the session ID
* @return the list artifact response containing filenames
*/
Single<ListArtifactsResponse> listArtifactKeys(String appName, String userId, String sessionId);

@Override
default Single<ListArtifactsResponse> listArtifactKeys(SessionKey sessionKey) {
return listArtifactKeys(sessionKey.appName(), sessionKey.userId(), sessionKey.id());
}

/**
* Deletes an artifact.
*
* @param appName the app name
* @param userId the user ID
* @param sessionId the session ID
* @param filename the filename
*/
Completable deleteArtifact(String appName, String userId, String sessionId, String filename);

@Override
default Completable deleteArtifact(SessionKey sessionKey, String filename) {
return deleteArtifact(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
}

/**
* Lists all the versions (as revision IDs) of an artifact.
*
* @param appName the app name
* @param userId the user ID
* @param sessionId the session ID
* @param filename the artifact filename
* @return A list of integer version numbers.
*/
Single<ImmutableList<Integer>> listVersions(
String appName, String userId, String sessionId, String filename);

@Override
default Single<ImmutableList<Integer>> listVersions(SessionKey sessionKey, String filename) {
return listVersions(sessionKey.appName(), sessionKey.userId(), sessionKey.id(), filename);
}
Expand Down