|
| 1 | +package cc.unitmesh.agent.subagent |
| 2 | + |
| 3 | +import cc.unitmesh.agent.artifact.ArtifactContext |
| 4 | +import cc.unitmesh.agent.artifact.ConversationMessage |
| 5 | +import cc.unitmesh.agent.artifact.ModelInfo |
| 6 | +import cc.unitmesh.agent.artifact.PEP723Parser |
| 7 | +import cc.unitmesh.agent.core.SubAgent |
| 8 | +import cc.unitmesh.agent.model.AgentDefinition |
| 9 | +import cc.unitmesh.agent.model.PromptConfig |
| 10 | +import cc.unitmesh.agent.model.RunConfig |
| 11 | +import cc.unitmesh.agent.tool.ToolResult |
| 12 | +import cc.unitmesh.llm.LLMService |
| 13 | +import cc.unitmesh.devins.llm.Message |
| 14 | +import cc.unitmesh.devins.llm.MessageRole |
| 15 | +import cc.unitmesh.llm.ModelConfig |
| 16 | +import kotlinx.serialization.Serializable |
| 17 | + |
| 18 | +/** |
| 19 | + * PythonArtifactAgent – Sub-agent responsible for generating |
| 20 | + * complete, self-contained Python scripts with PEP 723 inline metadata. |
| 21 | + * |
| 22 | + * The generated scripts follow the AutoDev Artifact convention and include |
| 23 | + * dependency declarations so that they can be executed with `uv run` or |
| 24 | + * after a simple `pip install`. |
| 25 | + * |
| 26 | + * @see <a href="https://github.com/phodal/auto-dev/issues/526">Issue #526</a> |
| 27 | + */ |
| 28 | +class PythonArtifactAgent( |
| 29 | + private val llmService: LLMService |
| 30 | +) : SubAgent<PythonArtifactInput, ToolResult.AgentResult>( |
| 31 | + AgentDefinition( |
| 32 | + name = "PythonArtifactAgent", |
| 33 | + displayName = "Python Artifact Agent", |
| 34 | + description = "Generates self-contained Python scripts with PEP 723 metadata for the AutoDev Unit system", |
| 35 | + promptConfig = PromptConfig( |
| 36 | + systemPrompt = SYSTEM_PROMPT, |
| 37 | + queryTemplate = null, |
| 38 | + initialMessages = emptyList() |
| 39 | + ), |
| 40 | + modelConfig = ModelConfig.default(), |
| 41 | + runConfig = RunConfig( |
| 42 | + maxTurns = 1, |
| 43 | + maxTimeMinutes = 5, |
| 44 | + terminateOnError = true |
| 45 | + ) |
| 46 | + ) |
| 47 | +) { |
| 48 | + |
| 49 | + override fun validateInput(input: Map<String, Any>): PythonArtifactInput { |
| 50 | + val prompt = input["prompt"] as? String |
| 51 | + ?: throw IllegalArgumentException("'prompt' is required") |
| 52 | + val dependencies = (input["dependencies"] as? List<*>) |
| 53 | + ?.filterIsInstance<String>() |
| 54 | + ?: emptyList() |
| 55 | + |
| 56 | + return PythonArtifactInput( |
| 57 | + prompt = prompt, |
| 58 | + dependencies = dependencies, |
| 59 | + requiresPython = input["requiresPython"] as? String ?: ">=3.11" |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + override suspend fun execute( |
| 64 | + input: PythonArtifactInput, |
| 65 | + onProgress: (String) -> Unit |
| 66 | + ): ToolResult.AgentResult { |
| 67 | + onProgress("🐍 Generating Python script...") |
| 68 | + |
| 69 | + val responseBuilder = StringBuilder() |
| 70 | + |
| 71 | + val historyMessages = listOf( |
| 72 | + Message(role = MessageRole.SYSTEM, content = SYSTEM_PROMPT) |
| 73 | + ) |
| 74 | + |
| 75 | + return try { |
| 76 | + llmService.streamPrompt( |
| 77 | + userPrompt = buildUserPrompt(input), |
| 78 | + historyMessages = historyMessages, |
| 79 | + compileDevIns = false |
| 80 | + ).collect { chunk -> |
| 81 | + responseBuilder.append(chunk) |
| 82 | + onProgress(chunk) |
| 83 | + } |
| 84 | + |
| 85 | + val rawResponse = responseBuilder.toString() |
| 86 | + val scriptContent = extractPythonCode(rawResponse) |
| 87 | + |
| 88 | + if (scriptContent.isNullOrBlank()) { |
| 89 | + return ToolResult.AgentResult( |
| 90 | + success = false, |
| 91 | + content = "Failed to extract Python code from LLM response." |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + // Validate PEP 723 metadata is present; inject if missing |
| 96 | + val meta = PEP723Parser.parse(scriptContent) |
| 97 | + val finalScript = if (meta.rawBlock == null && input.dependencies.isNotEmpty()) { |
| 98 | + PEP723Parser.injectMetadata( |
| 99 | + pythonContent = scriptContent, |
| 100 | + dependencies = input.dependencies, |
| 101 | + requiresPython = input.requiresPython |
| 102 | + ) |
| 103 | + } else { |
| 104 | + scriptContent |
| 105 | + } |
| 106 | + |
| 107 | + onProgress("\n✅ Python script generated successfully.") |
| 108 | + |
| 109 | + ToolResult.AgentResult( |
| 110 | + success = true, |
| 111 | + content = finalScript, |
| 112 | + metadata = mapOf( |
| 113 | + "type" to "python", |
| 114 | + "dependencies" to PEP723Parser.parseDependencies(finalScript).joinToString(","), |
| 115 | + "requiresPython" to (PEP723Parser.parse(finalScript).requiresPython ?: ">=3.11") |
| 116 | + ) |
| 117 | + ) |
| 118 | + } catch (e: Exception) { |
| 119 | + ToolResult.AgentResult( |
| 120 | + success = false, |
| 121 | + content = "Generation failed: ${e.message}" |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + override fun formatOutput(output: ToolResult.AgentResult): String = output.content |
| 127 | + |
| 128 | + // ---- helpers ---- |
| 129 | + |
| 130 | + private fun buildUserPrompt(input: PythonArtifactInput): String = buildString { |
| 131 | + appendLine(input.prompt) |
| 132 | + if (input.dependencies.isNotEmpty()) { |
| 133 | + appendLine() |
| 134 | + appendLine("Required dependencies: ${input.dependencies.joinToString(", ")}") |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Extract the Python code block from an LLM response. |
| 140 | + * Supports fenced code blocks (```python ... ```) and raw artifact XML. |
| 141 | + */ |
| 142 | + private fun extractPythonCode(response: String): String? { |
| 143 | + // Try autodev-artifact XML tag first |
| 144 | + val artifactPattern = Regex( |
| 145 | + """<autodev-artifact[^>]*type="application/autodev\.artifacts\.python"[^>]*>(.*?)</autodev-artifact>""", |
| 146 | + RegexOption.DOT_MATCHES_ALL |
| 147 | + ) |
| 148 | + artifactPattern.find(response)?.let { return it.groupValues[1].trim() } |
| 149 | + |
| 150 | + // Try fenced python code block |
| 151 | + val fencedPattern = Regex( |
| 152 | + """```python\s*\n(.*?)```""", |
| 153 | + RegexOption.DOT_MATCHES_ALL |
| 154 | + ) |
| 155 | + fencedPattern.find(response)?.let { return it.groupValues[1].trim() } |
| 156 | + |
| 157 | + // Fallback: if the whole response looks like Python code |
| 158 | + if (response.trimStart().startsWith("#") || response.trimStart().startsWith("import ") || response.trimStart().startsWith("from ")) { |
| 159 | + return response.trim() |
| 160 | + } |
| 161 | + |
| 162 | + return null |
| 163 | + } |
| 164 | + |
| 165 | + companion object { |
| 166 | + /** |
| 167 | + * System prompt guiding the LLM to generate PEP 723 compliant Python scripts. |
| 168 | + */ |
| 169 | + const val SYSTEM_PROMPT = """You are an expert Python developer specializing in creating self-contained, executable Python scripts. |
| 170 | +
|
| 171 | +## Rules |
| 172 | +
|
| 173 | +1. **PEP 723 Metadata** – Every script MUST begin with an inline metadata block: |
| 174 | +```python |
| 175 | +# /// script |
| 176 | +# requires-python = ">=3.11" |
| 177 | +# dependencies = [ |
| 178 | +# "some-package>=1.0", |
| 179 | +# ] |
| 180 | +# /// |
| 181 | +``` |
| 182 | +
|
| 183 | +2. **Self-Contained** – The script must run independently. All logic resides in a single file. |
| 184 | +
|
| 185 | +3. **Main Guard** – Always include: |
| 186 | +```python |
| 187 | +if __name__ == "__main__": |
| 188 | + main() |
| 189 | +``` |
| 190 | +
|
| 191 | +4. **Clear Output** – Use `print()` to provide meaningful output to stdout. |
| 192 | +
|
| 193 | +5. **Error Handling** – Include basic try/except blocks for I/O, network, or file operations. |
| 194 | +
|
| 195 | +6. **No External Config** – Avoid reading from external config files. Use environment variables via `os.environ.get()` when necessary. |
| 196 | +
|
| 197 | +7. **Output Format** – Wrap the script in `<autodev-artifact identifier="..." type="application/autodev.artifacts.python" title="...">` tags. |
| 198 | +""" |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * Input for PythonArtifactAgent |
| 204 | + */ |
| 205 | +@Serializable |
| 206 | +data class PythonArtifactInput( |
| 207 | + /** Natural-language description of what the script should do */ |
| 208 | + val prompt: String, |
| 209 | + /** Pre-declared dependencies (may be empty) */ |
| 210 | + val dependencies: List<String> = emptyList(), |
| 211 | + /** Python version constraint */ |
| 212 | + val requiresPython: String = ">=3.11" |
| 213 | +) |
0 commit comments