|
| 1 | +// SPDX-FileCopyrightText: 2026 Deutsche Telekom AG and others |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package org.eclipse.lmos.adl.server.repositories.impl |
| 6 | + |
| 7 | +import org.eclipse.lmos.adl.server.DEFAULT_OWNER |
| 8 | +import org.eclipse.lmos.adl.server.currentOwner |
| 9 | +import org.eclipse.lmos.adl.server.models.Agent |
| 10 | +import org.eclipse.lmos.adl.server.repositories.AgentRepository |
| 11 | +import org.slf4j.LoggerFactory |
| 12 | +import java.io.File |
| 13 | +import java.nio.file.AtomicMoveNotSupportedException |
| 14 | +import java.nio.file.Files |
| 15 | +import java.nio.file.StandardCopyOption.ATOMIC_MOVE |
| 16 | +import java.nio.file.StandardCopyOption.REPLACE_EXISTING |
| 17 | +import java.util.Base64 |
| 18 | + |
| 19 | +/** |
| 20 | + * Stores and loads ADL agent definitions from Markdown files with YAML front matter. |
| 21 | + */ |
| 22 | +class FileSystemAgentRepository(private val folder: File) : AgentRepository { |
| 23 | + private val log = LoggerFactory.getLogger(FileSystemAgentRepository::class.java) |
| 24 | + private val processor = YamlFrontMatterProcessor() |
| 25 | + private val parser = ADLAgentParser(processor) |
| 26 | + |
| 27 | + init { |
| 28 | + if (!folder.exists()) { |
| 29 | + folder.mkdirs() |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + override fun save(agent: Agent): Agent { |
| 34 | + val scopedAgent = agent.copy(owner = currentOwner()) |
| 35 | + val targetFile = fileForId(scopedAgent.id, scopedAgent.owner) |
| 36 | + targetFile.parentFile?.mkdirs() |
| 37 | + |
| 38 | + val metadata = mutableMapOf<String, Any>("id" to scopedAgent.id) |
| 39 | + metadata["owner"] = scopedAgent.owner |
| 40 | + if (!scopedAgent.active) metadata["active"] = scopedAgent.active |
| 41 | + if (!scopedAgent.tags.isNullOrEmpty()) metadata["tags"] = scopedAgent.tags |
| 42 | + if (!scopedAgent.mcpServers.isNullOrEmpty()) metadata["mcpServers"] = scopedAgent.mcpServers |
| 43 | + if (!scopedAgent.models.isNullOrEmpty()) metadata["models"] = scopedAgent.models |
| 44 | + if (!scopedAgent.role.isNullOrBlank()) metadata["role"] = scopedAgent.role |
| 45 | + |
| 46 | + val tempFile = File.createTempFile(targetFile.nameWithoutExtension, ".tmp", targetFile.parentFile) |
| 47 | + return try { |
| 48 | + tempFile.writeText(processor.write(YamlFrontMatter(metadata, renderContent(scopedAgent)))) |
| 49 | + try { |
| 50 | + Files.move(tempFile.toPath(), targetFile.toPath(), REPLACE_EXISTING, ATOMIC_MOVE) |
| 51 | + } catch (_: AtomicMoveNotSupportedException) { |
| 52 | + Files.move(tempFile.toPath(), targetFile.toPath(), REPLACE_EXISTING) |
| 53 | + } |
| 54 | + scopedAgent |
| 55 | + } catch (exception: Exception) { |
| 56 | + tempFile.delete() |
| 57 | + throw exception |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + override fun findById(id: String): Agent? { |
| 62 | + val file = fileForId(id, currentOwner()) |
| 63 | + if (file.exists()) { |
| 64 | + return readAgent(file) |
| 65 | + } |
| 66 | + |
| 67 | + return findAll().firstOrNull { it.id == id } |
| 68 | + } |
| 69 | + |
| 70 | + override fun findAll(): List<Agent> { |
| 71 | + val ownerFolder = folderForOwner(currentOwner()) |
| 72 | + return ownerFolder.listFiles { _, fileName -> fileName.endsWith(FILE_EXTENSION) } |
| 73 | + ?.sortedBy { it.name } |
| 74 | + ?.mapNotNull(::readAgent) |
| 75 | + ?: emptyList() |
| 76 | + } |
| 77 | + |
| 78 | + override fun delete(id: String): Boolean { |
| 79 | + val file = fileForId(id, currentOwner()) |
| 80 | + if (file.exists()) { |
| 81 | + return file.delete() |
| 82 | + } |
| 83 | + |
| 84 | + val matchingFile = folderForOwner(currentOwner()) |
| 85 | + .listFiles { _, fileName -> fileName.endsWith(FILE_EXTENSION) } |
| 86 | + ?.firstOrNull { readAgent(it)?.id == id } |
| 87 | + |
| 88 | + return matchingFile?.delete() ?: false |
| 89 | + } |
| 90 | + |
| 91 | + private fun readAgent(file: File): Agent? { |
| 92 | + return try { |
| 93 | + parser.parse(file.readText(), fallbackId = file.name.removeSuffix(FILE_EXTENSION), fallbackOwner = currentOwner()) |
| 94 | + } catch (exception: Exception) { |
| 95 | + log.error("Error reading ADL agent file {}", file.name, exception) |
| 96 | + null |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private fun renderContent(agent: Agent): String = buildString { |
| 101 | + appendLine("## Agent: ${agent.name}") |
| 102 | + appendLine() |
| 103 | + agent.description?.let { description -> |
| 104 | + appendLine("### Description") |
| 105 | + appendLine(description.trimEnd()) |
| 106 | + appendLine() |
| 107 | + } |
| 108 | + agent.corePrompt?.let { prompt -> |
| 109 | + appendLine("### Prompt") |
| 110 | + append(prompt.trimEnd()) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private fun fileForId(id: String, owner: String): File { |
| 115 | + return File(folderForOwner(owner), "${encode(id)}$FILE_EXTENSION") |
| 116 | + } |
| 117 | + |
| 118 | + private fun folderForOwner(owner: String): File { |
| 119 | + return if (owner == DEFAULT_OWNER) folder else File(folder, encode(owner)).apply { mkdirs() } |
| 120 | + } |
| 121 | + |
| 122 | + private fun encode(value: String): String = |
| 123 | + Base64.getUrlEncoder().withoutPadding().encodeToString(value.toByteArray(Charsets.UTF_8)) |
| 124 | + |
| 125 | + private companion object { |
| 126 | + const val FILE_EXTENSION = ".agent.md" |
| 127 | + } |
| 128 | +} |
| 129 | + |
0 commit comments