@iqai/adk-cli
Version:
CLI tool for creating, running, and testing ADK-TS agents
195 lines • 8.12 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArtifactsService = void 0;
const node_util_1 = require("node:util");
const adk_1 = require("@iqai/adk");
const common_1 = require("@nestjs/common");
const constants_1 = require("../../common/constants");
const sessions_service_1 = require("../sessions/sessions.service");
let ArtifactsService = class ArtifactsService {
sessionsService;
artifactService;
logger;
constructor(sessionsService, artifactService) {
this.sessionsService = sessionsService;
this.artifactService = artifactService;
this.logger = new common_1.Logger("artifacts-service");
}
/**
* List all artifacts for a session
*/
async listArtifacts(agentPath, sessionId) {
try {
const loaded = await this.sessionsService.ensureAgentLoaded(agentPath);
if (!loaded) {
return { error: "Failed to load agent" };
}
this.logger.log((0, node_util_1.format)("Listing artifacts for session: %s", sessionId));
const artifactKeys = await this.artifactService.listArtifactKeys({
appName: loaded.appName,
userId: this.getUserIdFromAgentPath(agentPath),
sessionId,
});
return {
artifacts: artifactKeys.map((filename) => ({
filename,
})),
};
}
catch (error) {
this.logger.error("Error listing artifacts: %o", error);
return { error: error.message };
}
}
/**
* Get an artifact (latest version)
*/
async getArtifact(agentPath, sessionId, artifactName) {
try {
const loaded = await this.sessionsService.ensureAgentLoaded(agentPath);
if (!loaded) {
throw new common_1.NotFoundException("Failed to load agent");
}
this.logger.log((0, node_util_1.format)("Getting artifact: %s for session: %s", artifactName, sessionId));
const artifact = await this.artifactService.loadArtifact({
appName: loaded.appName,
userId: this.getUserIdFromAgentPath(agentPath),
sessionId,
filename: artifactName,
});
if (!artifact) {
return { error: `Artifact not found: ${artifactName}` };
}
// Extract content from Part object
const contents = artifact.text || artifact.inlineData?.data || "";
const mimeType = artifact.inlineData?.mimeType || "text/plain";
return {
filename: artifactName,
mimeType,
contents,
version: 0, // Version will be determined from listVersions if needed
timestamp: Date.now(),
};
}
catch (error) {
this.logger.error("Error getting artifact: %o", error);
return { error: error.message };
}
}
/**
* Get a specific version of an artifact
*/
async getArtifactVersion(agentPath, sessionId, artifactName, version) {
try {
const loaded = await this.sessionsService.ensureAgentLoaded(agentPath);
if (!loaded) {
return { error: "Failed to load agent" };
}
this.logger.log((0, node_util_1.format)("Getting artifact version: %s v%d for session: %s", artifactName, version, sessionId));
const artifact = await this.artifactService.loadArtifact({
appName: loaded.appName,
userId: this.getUserIdFromAgentPath(agentPath),
sessionId,
filename: artifactName,
version,
});
if (!artifact) {
return {
error: `Artifact version not found: ${artifactName} v${version}`,
};
}
// Extract content from Part object
const contents = artifact.text || artifact.inlineData?.data || "";
const mimeType = artifact.inlineData?.mimeType || "text/plain";
return {
filename: artifactName,
mimeType,
contents,
version,
timestamp: Date.now(),
};
}
catch (error) {
this.logger.error("Error getting artifact version: %o", error);
return { error: error.message };
}
}
/**
* List all versions of an artifact
*/
async listArtifactVersions(agentPath, sessionId, artifactName) {
try {
const loaded = await this.sessionsService.ensureAgentLoaded(agentPath);
if (!loaded) {
return { error: "Failed to load agent" };
}
this.logger.log((0, node_util_1.format)("Listing artifact versions: %s for session: %s", artifactName, sessionId));
const versions = await this.artifactService.listVersions({
appName: loaded.appName,
userId: this.getUserIdFromAgentPath(agentPath),
sessionId,
filename: artifactName,
});
return {
versions: versions.map((version) => ({
version,
timestamp: Date.now(), // In-memory service doesn't track timestamps per version
})),
};
}
catch (error) {
this.logger.error("Error listing artifact versions: %o", error);
return { error: error.message };
}
}
/**
* Delete an artifact and all its versions
*/
async deleteArtifact(agentPath, sessionId, artifactName) {
try {
const loaded = await this.sessionsService.ensureAgentLoaded(agentPath);
if (!loaded) {
return { error: "Failed to load agent" };
}
this.logger.log((0, node_util_1.format)("Deleting artifact: %s for session: %s", artifactName, sessionId));
await this.artifactService.deleteArtifact({
appName: loaded.appName,
userId: this.getUserIdFromAgentPath(agentPath),
sessionId,
filename: artifactName,
});
return { success: true };
}
catch (error) {
this.logger.error("Error deleting artifact: %o", error);
return { error: error.message };
}
}
/**
* Helper method to construct userId from agent path
*/
getUserIdFromAgentPath(agentPath) {
return `${constants_1.USER_ID_PREFIX}${agentPath}`;
}
};
exports.ArtifactsService = ArtifactsService;
exports.ArtifactsService = ArtifactsService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(sessions_service_1.SessionsService)),
__param(1, (0, common_1.Inject)(adk_1.InMemoryArtifactService)),
__metadata("design:paramtypes", [sessions_service_1.SessionsService,
adk_1.InMemoryArtifactService])
], ArtifactsService);
//# sourceMappingURL=artifacts.service.js.map