Skip to content
Merged
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
75 changes: 75 additions & 0 deletions mpp-core/src/commonMain/kotlin/cc/unitmesh/agent/ArtifactAgent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ArtifactAgent(
enum class ArtifactType(val mimeType: String) {
HTML("application/autodev.artifacts.html"),
REACT("application/autodev.artifacts.react"),
NODEJS("application/autodev.artifacts.nodejs"),
PYTHON("application/autodev.artifacts.python"),
SVG("application/autodev.artifacts.svg"),
MERMAID("application/autodev.artifacts.mermaid");
Expand All @@ -57,6 +58,80 @@ class ArtifactAgent(
}
}

/**
* Fix a failed artifact based on execution error
*
* @param originalArtifact The artifact that failed to execute
* @param errorMessage The error message from execution
* @param onProgress Callback for streaming progress
* @return Fixed artifact result
*/
suspend fun fix(
originalArtifact: Artifact,
errorMessage: String,
onProgress: (String) -> Unit = {}
): ArtifactResult {
val fixPrompt = buildFixPrompt(originalArtifact, errorMessage)

logger.info { "🔧 Attempting to fix artifact: ${originalArtifact.title}" }
logger.info { "📝 Error: ${errorMessage.take(200)}..." }

return generate(fixPrompt, onProgress)
}

/**
* Build a prompt to fix a failed artifact
*/
private fun buildFixPrompt(artifact: Artifact, errorMessage: String): String {
val language = if (this.language == "ZH") "中文" else "English"

return if (this.language == "ZH") {
"""
我之前生成的代码执行失败了,请帮我修复。

## 原始代码
```${artifact.type.name.lowercase()}
${artifact.content}
```

## 执行错误
```
$errorMessage
```

## 要求
1. 分析错误原因
2. 修复代码使其能够正确执行
3. 保持原有功能不变
4. 使用相同的 artifact 格式输出修复后的代码

请生成修复后的 artifact。
""".trimIndent()
} else {
"""
The code I generated earlier failed to execute. Please help me fix it.

## Original Code
```${artifact.type.name.lowercase()}
${artifact.content}
```

## Execution Error
```
$errorMessage
```

## Requirements
1. Analyze the error cause
2. Fix the code so it executes correctly
3. Keep the original functionality unchanged
4. Output the fixed code using the same artifact format

Please generate the fixed artifact.
""".trimIndent()
}
}

/**
* Generate artifact from user prompt
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ object ArtifactAgentTemplate {
- Can use Tailwind CSS for styling
- Exports default component

3. **application/autodev.artifacts.python** - Python scripts
3. **application/autodev.artifacts.nodejs** - Node.js applications
- Complete Node.js application code (Express.js, etc.)
- **IMPORTANT**: Only include the JavaScript code (index.js), NOT package.json
- The system will auto-generate package.json based on require/import statements
- Use require() or import to declare dependencies (they will be auto-detected)
- Must be executable standalone with `node index.js`

4. **application/autodev.artifacts.python** - Python scripts
- Complete Python scripts with PEP 723 inline metadata
- Dependencies declared in script header
- Must be executable standalone

4. **application/autodev.artifacts.svg** - SVG images
5. **application/autodev.artifacts.svg** - SVG images
- Complete SVG markup
- Can include inline styles and animations

5. **application/autodev.artifacts.mermaid** - Diagrams
6. **application/autodev.artifacts.mermaid** - Diagrams
- Mermaid diagram syntax
- Flowcharts, sequence diagrams, etc.
"""
Expand Down Expand Up @@ -156,6 +163,38 @@ if __name__ == "__main__":
main()
```

## Node.js Application Guidelines

When creating Node.js artifacts:

1. **Code Only**: Include ONLY the JavaScript code, NOT package.json
2. **Single Artifact**: Generate exactly ONE artifact containing the main application code
3. **Dependencies via require/import**: Use require() or import statements to declare dependencies
4. **Self-Contained**: The script should be the complete application

### Node.js Template Structure:

```javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

// Middleware
app.use(express.json());

// Routes
app.get('/', (req, res) => {
res.json({ message: 'Hello World!' });
});

// Start server
app.listen(PORT, () => {
console.log('Server running on http://localhost:' + PORT);
});
```

**CRITICAL**: Do NOT create a separate artifact for package.json. The system automatically generates package.json by detecting require() and import statements in your code.

## React Component Guidelines

When creating React artifacts:
Expand Down Expand Up @@ -298,6 +337,38 @@ $ARTIFACT_TYPES
2. **自包含**:脚本应无需外部设置即可运行
3. **清晰输出**:打印有意义的输出到 stdout

## Node.js 应用指南

创建 Node.js Artifact 时:

1. **仅包含代码**:只包含 JavaScript 代码,不要包含 package.json
2. **单个 Artifact**:只生成一个包含主应用代码的 Artifact
3. **通过 require/import 声明依赖**:使用 require() 或 import 语句声明依赖
4. **自包含**:脚本应该是完整的应用程序

### Node.js 模板结构:

```javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

// 中间件
app.use(express.json());

// 路由
app.get('/', (req, res) => {
res.json({ message: 'Hello World!' });
});

// 启动服务器
app.listen(PORT, () => {
console.log('服务器运行在 http://localhost:' + PORT);
});
```

**重要**:不要为 package.json 创建单独的 Artifact。系统会通过检测代码中的 require() 和 import 语句自动生成 package.json。

## React 组件指南

创建 React Artifact 时:
Expand Down
Loading
Loading