htmlgen-mcp 0.2.5__py3-none-any.whl → 0.3.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of htmlgen-mcp might be problematic. Click here for more details.

htmlgen_mcp/__init__.py CHANGED
@@ -3,5 +3,5 @@ MCP web agent server module.
3
3
  """
4
4
  from .web_agent_server import main
5
5
 
6
- __version__ = "0.2.5"
6
+ __version__ = "0.3.1"
7
7
  __all__ = ["main"]
@@ -0,0 +1,328 @@
1
+ """AI 内容生成辅助模块 - 为页面生成个性化内容"""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ from typing import Dict, Any, Optional
6
+
7
+
8
+ class AIContentGenerator:
9
+ """AI 内容生成器 - 为各种页面生成个性化内容"""
10
+
11
+ def __init__(self, agent_instance):
12
+ """初始化生成器
13
+
14
+ Args:
15
+ agent_instance: SmartWebAgent 实例
16
+ """
17
+ self.agent = agent_instance
18
+
19
+ def generate_menu_content(
20
+ self,
21
+ project_name: str,
22
+ context: Dict[str, Any]
23
+ ) -> Dict[str, Any]:
24
+ """为菜单页面生成个性化内容"""
25
+
26
+ prompt = f"""你是一个专业的网页内容创作者。
27
+ 为"{project_name}"生成一个详细的菜单内容。
28
+
29
+ 项目信息:
30
+ - 名称:{project_name}
31
+ - 描述:{context.get('description', '')}
32
+ - 特色:{context.get('features', '')}
33
+ - 位置:{context.get('location', '')}
34
+ - 目标客户:{context.get('target_audience', '')}
35
+
36
+ 请生成一个完整的菜单,包含:
37
+ 1. 2-4个类别(如:热饮、冷饮、轻食、甜点)
38
+ 2. 每个类别3-5个项目
39
+ 3. 每个项目包含:
40
+ - name: 产品名称(有创意且符合品牌)
41
+ - price: 价格(符合定位,使用"¥"符号)
42
+ - description: 描述(简短吸引人,20字以内)
43
+ - image_topic: 图片主题描述(英文,用于生成图片)
44
+
45
+ 返回 JSON 格式:
46
+ {{
47
+ "categories": [
48
+ {{
49
+ "name": "类别名称",
50
+ "items": [
51
+ {{
52
+ "name": "产品名称",
53
+ "price": "¥ XX",
54
+ "description": "产品描述",
55
+ "image_topic": "image description"
56
+ }}
57
+ ]
58
+ }}
59
+ ],
60
+ "description": "菜单页面的副标题描述"
61
+ }}
62
+
63
+ 重要:
64
+ - 内容要符合项目的定位和风格
65
+ - 价格要合理且有梯度
66
+ - 描述要诱人且真实
67
+ - 直接返回 JSON,不要其他内容
68
+ """
69
+
70
+ try:
71
+ response = self.agent.client.chat.completions.create(
72
+ model=self.agent.model,
73
+ messages=[
74
+ {"role": "system", "content": "你是一个专业的内容创作者,擅长为各类商业项目创作个性化内容。"},
75
+ {"role": "user", "content": prompt}
76
+ ],
77
+ temperature=0.7,
78
+ max_tokens=2000
79
+ )
80
+
81
+ content = response.choices[0].message.content.strip()
82
+ # 清理可能的 markdown 代码块标记
83
+ if content.startswith("```"):
84
+ lines = content.split('\n')
85
+ content = '\n'.join(lines[1:-1] if lines[-1] == "```" else lines[1:])
86
+
87
+ return json.loads(content)
88
+
89
+ except Exception as e:
90
+ print(f"AI 生成菜单内容失败:{e}")
91
+ # 返回空内容,让页面使用默认值
92
+ return {}
93
+
94
+ def generate_about_content(
95
+ self,
96
+ project_name: str,
97
+ context: Dict[str, Any]
98
+ ) -> Dict[str, Any]:
99
+ """为关于页面生成个性化内容"""
100
+
101
+ prompt = f"""为"{project_name}"生成"关于我们"页面的内容。
102
+
103
+ 项目信息:
104
+ - 名称:{project_name}
105
+ - 描述:{context.get('description', '')}
106
+ - 使命:{context.get('mission', '')}
107
+ - 特色:{context.get('features', '')}
108
+ - 创始年份:{context.get('year_founded', '2020')}
109
+
110
+ 请生成以下内容(JSON格式):
111
+ {{
112
+ "headline": "页面主标题",
113
+ "subtitle": "副标题(一句话介绍)",
114
+ "story": {{
115
+ "title": "品牌故事标题",
116
+ "content": "品牌故事内容(100-150字)",
117
+ "image_topic": "story image description"
118
+ }},
119
+ "values": ["价值观1", "价值观2", "价值观3", "价值观4"],
120
+ "team": [
121
+ {{
122
+ "name": "团队成员姓名",
123
+ "role": "职位",
124
+ "description": "简介(20字以内)",
125
+ "image_topic": "team member photo description"
126
+ }}
127
+ ],
128
+ "achievements": [
129
+ {{"value": "数值", "label": "标签"}},
130
+ {{"value": "数值", "label": "标签"}}
131
+ ],
132
+ "cta_title": "行动号召标题",
133
+ "cta_text": "行动号召文字"
134
+ }}
135
+
136
+ 要求:
137
+ - 内容真实可信,避免夸大
138
+ - 团队成员生成2-3个核心岗位即可
139
+ - 成就数据要合理
140
+ - 符合品牌调性
141
+ """
142
+
143
+ try:
144
+ response = self.agent.client.chat.completions.create(
145
+ model=self.agent.model,
146
+ messages=[
147
+ {"role": "user", "content": prompt}
148
+ ],
149
+ temperature=0.7,
150
+ max_tokens=1500
151
+ )
152
+
153
+ content = response.choices[0].message.content.strip()
154
+ if content.startswith("```"):
155
+ lines = content.split('\n')
156
+ content = '\n'.join(lines[1:-1] if lines[-1] == "```" else lines[1:])
157
+
158
+ return json.loads(content)
159
+
160
+ except Exception as e:
161
+ print(f"AI 生成关于内容失败:{e}")
162
+ return {}
163
+
164
+ def generate_contact_content(
165
+ self,
166
+ project_name: str,
167
+ context: Dict[str, Any]
168
+ ) -> Dict[str, Any]:
169
+ """为联系页面生成个性化文案"""
170
+
171
+ prompt = f"""为"{project_name}"生成联系页面的文案。
172
+
173
+ 项目信息:
174
+ - 名称:{project_name}
175
+ - 类型:{context.get('type', '咖啡店')}
176
+ - 特色:{context.get('features', '')}
177
+
178
+ 生成以下文案(JSON格式):
179
+ {{
180
+ "headline": "联系页面标题",
181
+ "subtitle": "副标题(欢迎语)",
182
+ "form_title": "表单标题",
183
+ "form_text": "表单说明文字",
184
+ "response_time": "响应时间承诺"
185
+ }}
186
+
187
+ 要求文案:
188
+ - 友好亲切
189
+ - 专业可信
190
+ - 简洁明了
191
+ """
192
+
193
+ try:
194
+ response = self.agent.client.chat.completions.create(
195
+ model=self.agent.model,
196
+ messages=[
197
+ {"role": "user", "content": prompt}
198
+ ],
199
+ temperature=0.6,
200
+ max_tokens=500
201
+ )
202
+
203
+ content = response.choices[0].message.content.strip()
204
+ if content.startswith("```"):
205
+ lines = content.split('\n')
206
+ content = '\n'.join(lines[1:-1] if lines[-1] == "```" else lines[1:])
207
+
208
+ return json.loads(content)
209
+
210
+ except Exception as e:
211
+ print(f"AI 生成联系内容失败:{e}")
212
+ return {}
213
+
214
+ def enhance_project_context(
215
+ self,
216
+ project_name: str,
217
+ description: str
218
+ ) -> Dict[str, Any]:
219
+ """增强项目上下文信息,补充缺失的细节"""
220
+
221
+ prompt = f"""分析项目"{project_name}"并补充必要的商业信息。
222
+
223
+ 项目描述:{description}
224
+
225
+ 请推断并生成以下信息(JSON格式):
226
+ {{
227
+ "type": "项目类型(如:咖啡店、餐厅、科技公司等)",
228
+ "target_audience": "目标客户描述",
229
+ "features": "核心特色(3-5个关键词)",
230
+ "mission": "使命宣言(一句话)",
231
+ "address": "合理的示例地址",
232
+ "phone": "示例电话(使用 010-XXXX-XXXX 格式)",
233
+ "email": "示例邮箱",
234
+ "business_hours": "营业时间"
235
+ }}
236
+ """
237
+
238
+ try:
239
+ response = self.agent.client.chat.completions.create(
240
+ model=self.agent.model,
241
+ messages=[
242
+ {"role": "user", "content": prompt}
243
+ ],
244
+ temperature=0.5,
245
+ max_tokens=500
246
+ )
247
+
248
+ content = response.choices[0].message.content.strip()
249
+ if content.startswith("```"):
250
+ lines = content.split('\n')
251
+ content = '\n'.join(lines[1:-1] if lines[-1] == "```" else lines[1:])
252
+
253
+ return json.loads(content)
254
+
255
+ except Exception as e:
256
+ print(f"增强上下文失败:{e}")
257
+ return {
258
+ "type": "商业",
259
+ "target_audience": "大众客户",
260
+ "features": "品质、服务、创新",
261
+ "mission": f"为客户提供优质的产品与服务",
262
+ "address": "北京市朝阳区示例路123号",
263
+ "phone": "010-8888-8888",
264
+ "email": f"info@{project_name.lower().replace(' ', '')}.com",
265
+ "business_hours": "周一至周五 9:00-18:00"
266
+ }
267
+
268
+
269
+ def integrate_ai_generation(agent_instance, tool_name: str, params: Dict[str, Any]) -> Dict[str, Any]:
270
+ """集成 AI 生成到工具调用中
271
+
272
+ Args:
273
+ agent_instance: SmartWebAgent 实例
274
+ tool_name: 工具名称
275
+ params: 工具参数
276
+
277
+ Returns:
278
+ 增强后的参数,包含 AI 生成的内容
279
+ """
280
+
281
+ # 只处理特定的页面生成工具
282
+ if tool_name not in ["create_menu_page", "create_about_page", "create_contact_page"]:
283
+ return params
284
+
285
+ # 提取项目信息
286
+ project_name = params.get('project_name', '')
287
+ file_path = params.get('file_path', '')
288
+
289
+ # 获取或创建上下文
290
+ context = params.get('context', {})
291
+ if not context:
292
+ # 尝试从 agent 的执行历史中提取上下文
293
+ context = {
294
+ 'description': agent_instance.latest_user_request or '',
295
+ 'project_directory': agent_instance.project_directory
296
+ }
297
+
298
+ # 创建 AI 内容生成器
299
+ generator = AIContentGenerator(agent_instance)
300
+
301
+ # 根据工具类型生成相应内容
302
+ ai_content = {}
303
+ if tool_name == "create_menu_page":
304
+ ai_content = generator.generate_menu_content(project_name, context)
305
+ elif tool_name == "create_about_page":
306
+ ai_content = generator.generate_about_content(project_name, context)
307
+ elif tool_name == "create_contact_page":
308
+ ai_content = generator.generate_contact_content(project_name, context)
309
+
310
+ # 如果需要,增强上下文信息
311
+ if not context.get('address') or not context.get('phone'):
312
+ enhanced_context = generator.enhance_project_context(
313
+ project_name,
314
+ context.get('description', '')
315
+ )
316
+ context.update(enhanced_context)
317
+
318
+ # 更新参数
319
+ params['context'] = context
320
+ params['ai_content'] = ai_content
321
+
322
+ return params
323
+
324
+
325
+ __all__ = [
326
+ 'AIContentGenerator',
327
+ 'integrate_ai_generation'
328
+ ]