auto-coder 0.1.302__py3-none-any.whl → 0.1.304__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 auto-coder might be problematic. Click here for more details.

@@ -0,0 +1,423 @@
1
+ """
2
+ Predefined event content models using Pydantic.
3
+ These models provide structured data definitions for different event types.
4
+ """
5
+
6
+ from typing import Dict, List, Optional, Any, Union
7
+ from enum import Enum
8
+ from datetime import datetime
9
+ from pydantic import BaseModel, Field, field_validator, ConfigDict
10
+ import json
11
+
12
+
13
+ class ContentType(str, Enum):
14
+ """内容类型枚举"""
15
+ TEXT = "text"
16
+ CODE = "code"
17
+ IMAGE = "image"
18
+ JSON = "json"
19
+ HTML = "html"
20
+ MARKDOWN = "markdown"
21
+
22
+
23
+ class StreamState(str, Enum):
24
+ """流式内容的状态"""
25
+ THINKING = "thinking" # 思考中的内容
26
+ CONTENT = "content" # 正式的内容
27
+ COMPLETE = "complete" # 完成的标记
28
+
29
+
30
+ class BaseEventContent(BaseModel):
31
+ """所有事件内容的基础模型"""
32
+ timestamp: float = Field(default_factory=lambda: datetime.now().timestamp())
33
+ metadata: Dict[str, Any] = Field(default_factory=dict)
34
+
35
+ model_config = ConfigDict(
36
+ extra="allow", # 允许额外的字段
37
+ json_encoders={
38
+ datetime: lambda v: v.isoformat()
39
+ }
40
+ )
41
+
42
+ def to_dict(self) -> Dict[str, Any]:
43
+ """转换为字典"""
44
+ return self.model_dump()
45
+
46
+ def to_json(self) -> str:
47
+ """转换为JSON字符串"""
48
+ return self.model_dump_json()
49
+
50
+
51
+ class StreamContent(BaseEventContent):
52
+ """
53
+ 流式内容模型
54
+ 用于表示流式传输的内容,如思考过程和正式输出
55
+ """
56
+ state: StreamState = StreamState.CONTENT
57
+ content: str = ""
58
+ content_type: ContentType = ContentType.TEXT
59
+ sequence: int = 0 # 序列号,用于排序
60
+ is_thinking: bool = Field(default=False, description="是否是思考过程")
61
+
62
+ @field_validator('is_thinking')
63
+ @classmethod
64
+ def set_is_thinking(cls, v, info):
65
+ """根据state自动设置is_thinking字段"""
66
+ values = info.data
67
+ if 'state' in values:
68
+ return values['state'] == StreamState.THINKING
69
+ return v
70
+
71
+ model_config = ConfigDict(
72
+ json_schema_extra={
73
+ "example": {
74
+ "state": "content",
75
+ "content": "正在处理请求...",
76
+ "content_type": "text",
77
+ "sequence": 1,
78
+ "timestamp": 1626888000.0
79
+ }
80
+ }
81
+ )
82
+
83
+
84
+ class ResultContent(BaseEventContent):
85
+ """
86
+ 结果内容模型
87
+ 用于表示处理完成的结果
88
+ """
89
+ content: Any
90
+ content_type: ContentType = ContentType.TEXT
91
+
92
+ model_config = ConfigDict(
93
+ json_schema_extra={
94
+ "example": {
95
+ "content": "处理已完成",
96
+ "content_type": "text",
97
+ "metadata": {"processing_time": 1.23, "status": "success"},
98
+ "timestamp": 1626888000.0
99
+ }
100
+ }
101
+ )
102
+ ## ResultContent.content 字段的类型
103
+ ## begin===============================
104
+ class ResultTokenStatContent(BaseModel):
105
+ model_name:str = ""
106
+ elapsed_time:float = 0.0
107
+ first_token_time:float = 0.0
108
+ input_tokens:int = 0
109
+ output_tokens:int = 0
110
+ input_cost:float = 0.0
111
+ output_cost:float = 0.0
112
+ speed:float = 0.0
113
+
114
+ def to_dict(self) -> Dict[str, Any]:
115
+ """转换为字典"""
116
+ return self.model_dump()
117
+
118
+ def to_json(self) -> str:
119
+ """转换为JSON字符串"""
120
+ return self.model_dump_json()
121
+
122
+
123
+ class ResultCommandPrepareStatContent(BaseModel):
124
+ command:str = ""
125
+ parameters:Dict[str,Any] = {}
126
+
127
+ def to_dict(self) -> Dict[str, Any]:
128
+ """转换为字典"""
129
+ return self.model_dump()
130
+
131
+ def to_json(self) -> str:
132
+ """转换为JSON字符串"""
133
+ return self.model_dump_json()
134
+
135
+ class ResultCommandExecuteStatContent(BaseModel):
136
+ command:str = ""
137
+ content:str = ""
138
+
139
+ def to_dict(self) -> Dict[str, Any]:
140
+ """转换为字典"""
141
+ return self.model_dump()
142
+
143
+ def to_json(self) -> str:
144
+ """转换为JSON字符串"""
145
+ return self.model_dump_json()
146
+
147
+ class ResultContextUsedContent(BaseModel):
148
+ files:List[str] = []
149
+ title:str = ""
150
+ description:str = ""
151
+
152
+ def to_dict(self) -> Dict[str, Any]:
153
+ """转换为字典"""
154
+ return self.model_dump()
155
+
156
+ def to_json(self) -> str:
157
+ """转换为JSON字符串"""
158
+ return self.model_dump_json()
159
+
160
+
161
+ ## 最后总结性消息内容
162
+ class ResultSummaryContent(BaseModel):
163
+ summary:str = ""
164
+
165
+ def to_dict(self) -> Dict[str, Any]:
166
+ """转换为字典"""
167
+ return self.model_dump()
168
+
169
+ def to_json(self) -> str:
170
+ """转换为JSON字符串"""
171
+ return self.model_dump_json()
172
+
173
+ ## ResultContent.content 字段的类型
174
+ ## end===============================
175
+
176
+ class MarkDownResultContent(ResultContent):
177
+ """
178
+ Markdown结果内容模型
179
+ 用于表示Markdown格式的处理结果
180
+ """
181
+ content_type: ContentType = ContentType.MARKDOWN
182
+
183
+ model_config = ConfigDict(
184
+ json_schema_extra={
185
+ "example": {
186
+ "content": "# 处理结果\n处理已完成,详情如下...",
187
+ "content_type": "markdown",
188
+ "metadata": {"processing_time": 1.23, "status": "success"},
189
+ "timestamp": 1626888000.0
190
+ }
191
+ }
192
+ )
193
+
194
+
195
+ class AskUserContent(BaseEventContent):
196
+ """
197
+ 询问用户的内容模型
198
+ 用于请求用户提供输入
199
+ """
200
+ prompt: str
201
+ options: Optional[List[str]] = None
202
+ default_option: Optional[str] = None
203
+ required: bool = True
204
+ timeout: Optional[float] = None # 超时时间(秒)
205
+
206
+ model_config = ConfigDict(
207
+ json_schema_extra={
208
+ "example": {
209
+ "prompt": "您想继续吗?",
210
+ "options": ["是", "否"],
211
+ "default_option": "是",
212
+ "required": True,
213
+ "timeout": 60.0,
214
+ "timestamp": 1626888000.0
215
+ }
216
+ }
217
+ )
218
+
219
+
220
+ class UserResponseContent(BaseEventContent):
221
+ """
222
+ 用户响应的内容模型
223
+ 用于表示用户对询问的回应
224
+ """
225
+ response: str
226
+ response_time: float = Field(default_factory=lambda: datetime.now().timestamp())
227
+ original_prompt: Optional[str] = None
228
+
229
+ model_config = ConfigDict(
230
+ json_schema_extra={
231
+ "example": {
232
+ "response": "是",
233
+ "response_time": 1626888030.0,
234
+ "original_prompt": "您想继续吗?",
235
+ "timestamp": 1626888030.0
236
+ }
237
+ }
238
+ )
239
+
240
+
241
+ # 扩展的内容类型
242
+
243
+ class CodeContent(StreamContent):
244
+ """代码内容模型"""
245
+ content_type: ContentType = ContentType.CODE
246
+ language: str = "python" # 代码语言
247
+
248
+ model_config = ConfigDict(
249
+ json_schema_extra={
250
+ "example": {
251
+ "state": "content",
252
+ "content": "def hello():\n print('Hello, world!')",
253
+ "content_type": "code",
254
+ "language": "python",
255
+ "sequence": 1,
256
+ "timestamp": 1626888000.0
257
+ }
258
+ }
259
+ )
260
+
261
+
262
+ class MarkdownContent(StreamContent):
263
+ """Markdown内容模型"""
264
+ content_type: ContentType = ContentType.MARKDOWN
265
+
266
+ model_config = ConfigDict(
267
+ json_schema_extra={
268
+ "example": {
269
+ "state": "content",
270
+ "content": "# 标题\n这是一段Markdown内容",
271
+ "content_type": "markdown",
272
+ "sequence": 1,
273
+ "timestamp": 1626888000.0
274
+ }
275
+ }
276
+ )
277
+
278
+
279
+ class ErrorContent(BaseEventContent):
280
+ """错误内容模型"""
281
+ error_code: str
282
+ error_message: str
283
+ details: Optional[Dict[str, Any]] = None
284
+
285
+ model_config = ConfigDict(
286
+ json_schema_extra={
287
+ "example": {
288
+ "error_code": "E1001",
289
+ "error_message": "处理失败",
290
+ "details": {"location": "process_data", "reason": "invalid input"},
291
+ "timestamp": 1626888000.0
292
+ }
293
+ }
294
+ )
295
+
296
+
297
+ class CompletionContent(BaseEventContent):
298
+ """
299
+ 完成内容模型
300
+ 用于表示事件或操作正常完成的情况
301
+ """
302
+ success_code: str
303
+ success_message: str
304
+ result: Optional[Any] = None
305
+ details: Optional[Dict[str, Any]] = None
306
+ completion_time: float = Field(default_factory=lambda: datetime.now().timestamp())
307
+
308
+ model_config = ConfigDict(
309
+ json_schema_extra={
310
+ "example": {
311
+ "success_code": "S1001",
312
+ "success_message": "操作成功完成",
313
+ "result": {"items_processed": 50, "warnings": 0},
314
+ "details": {"operation": "data_sync", "duration": 120.5},
315
+ "timestamp": 1626888000.0,
316
+ "completion_time": 1626888000.0
317
+ }
318
+ }
319
+ )
320
+
321
+
322
+ # 工厂函数,便于创建各种内容
323
+ def create_stream_thinking(content: str, sequence: int = 0, metadata: Dict[str, Any] = {}) -> StreamContent:
324
+ """创建思考中的流式内容"""
325
+ return StreamContent(
326
+ state=StreamState.THINKING,
327
+ content=content,
328
+ sequence=sequence,
329
+ is_thinking=True,
330
+ metadata=metadata
331
+ )
332
+
333
+
334
+ def create_stream_content(content: str, sequence: int = 0, metadata: Dict[str, Any] = {}) -> StreamContent:
335
+ """创建正式的流式内容"""
336
+ return StreamContent(
337
+ state=StreamState.CONTENT,
338
+ content=content,
339
+ sequence=sequence,
340
+ is_thinking=False,
341
+ metadata=metadata
342
+ )
343
+
344
+
345
+ def create_result(content: Any, metadata: Dict[str, Any] = None) -> ResultContent:
346
+ """创建结果内容"""
347
+ return ResultContent(
348
+ content=content,
349
+ metadata=metadata or {}
350
+ )
351
+
352
+
353
+ def create_markdown_result(content: str, metadata: Dict[str, Any] = None) -> MarkDownResultContent:
354
+ """
355
+ 创建Markdown结果内容
356
+
357
+ Args:
358
+ content: Markdown格式的内容
359
+ metadata: 元数据信息
360
+
361
+ Returns:
362
+ MarkDownResultContent 实例
363
+ """
364
+ return MarkDownResultContent(
365
+ content=content,
366
+ metadata=metadata or {}
367
+ )
368
+
369
+
370
+ def create_ask_user(prompt: str, options: List[str] = None) -> AskUserContent:
371
+ """创建询问用户的内容"""
372
+ return AskUserContent(
373
+ prompt=prompt,
374
+ options=options
375
+ )
376
+
377
+
378
+ def create_user_response(response: str, original_prompt: str = None) -> UserResponseContent:
379
+ """创建用户响应的内容"""
380
+ return UserResponseContent(
381
+ response=response,
382
+ original_prompt=original_prompt
383
+ )
384
+
385
+
386
+ def create_completion(success_code: str, success_message: str, result: Any = None, details: Dict[str, Any] = None) -> CompletionContent:
387
+ """
388
+ 创建完成内容
389
+
390
+ Args:
391
+ success_code: 成功代码
392
+ success_message: 成功信息
393
+ result: 操作结果
394
+ details: 详细信息
395
+
396
+ Returns:
397
+ CompletionContent 实例
398
+ """
399
+ return CompletionContent(
400
+ success_code=success_code,
401
+ success_message=success_message,
402
+ result=result,
403
+ details=details or {}
404
+ )
405
+
406
+
407
+ def create_error(error_code: str, error_message: str, details: Dict[str, Any] = None) -> ErrorContent:
408
+ """
409
+ 创建错误内容
410
+
411
+ Args:
412
+ error_code: 错误代码
413
+ error_message: 错误信息
414
+ details: 详细错误信息
415
+
416
+ Returns:
417
+ ErrorContent 实例
418
+ """
419
+ return ErrorContent(
420
+ error_code=error_code,
421
+ error_message=error_message,
422
+ details=details or {}
423
+ )