157 lines
3.7 KiB
Python
157 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
面向 reflex.state 的类
|
||
"""
|
||
from datetime import datetime
|
||
from enum import StrEnum
|
||
from typing import Any
|
||
|
||
from pydantic import BaseModel, Field, TypeAdapter
|
||
from pydantic_ai import RunUsage
|
||
from pydantic_ai._uuid import uuid7
|
||
|
||
|
||
|
||
class NoResult(BaseModel):
|
||
"""
|
||
无结果类
|
||
"""
|
||
|
||
...
|
||
|
||
class AgentRunEvent(BaseModel):
|
||
"""
|
||
智能体运行事件类
|
||
"""
|
||
|
||
content: str = Field(
|
||
default="",
|
||
description="智能体运行内容",
|
||
)
|
||
|
||
|
||
class MessageType(StrEnum):
|
||
"""
|
||
消息类型枚举
|
||
"""
|
||
|
||
USER_PROMPT = "user_prompt"
|
||
THINKING = "thinking"
|
||
TOOL_CALL = "tool_call"
|
||
TEXT = "text"
|
||
|
||
|
||
class Message(BaseModel):
|
||
"""
|
||
消息类
|
||
"""
|
||
|
||
id: str = Field(default_factory=lambda: str(uuid7()), description="消息唯一标识")
|
||
type: MessageType = Field(..., description="消息类型")
|
||
title: str = Field(default="", description="消息标题")
|
||
content: str = Field(default="", description="消息内容")
|
||
is_running: bool = Field(
|
||
default=False, description="正在运行,True 表示正在运行, False 表示运行结束"
|
||
)
|
||
is_shown: bool = Field(
|
||
default=False, description="展示组件,True 表示展示,False 表示隐藏"
|
||
)
|
||
|
||
|
||
class WorkflowType(StrEnum):
|
||
"""
|
||
工作类型枚举
|
||
"""
|
||
|
||
BOOK_FLIGHT = "预定航班"
|
||
|
||
|
||
class Workflow(BaseModel):
|
||
"""
|
||
工作流类
|
||
"""
|
||
|
||
type: WorkflowType = Field(..., description="工作类型")
|
||
deps: Any = Field(default=None, description="工作依赖项")
|
||
|
||
|
||
class Conversation(BaseModel):
|
||
"""
|
||
会话类
|
||
"""
|
||
|
||
id: str = Field(..., description="会话唯一标识")
|
||
description: str = Field(..., description="会话描述")
|
||
created_at: datetime = Field(..., description="会话创建日期时间")
|
||
usage: RunUsage = Field(..., description="会话使用量")
|
||
messages: dict[str, Message] = Field(default_factory=dict, description="消息字典")
|
||
workflow: Workflow | None = Field(default=None, description="工作流")
|
||
user_prompt: str = Field(default="", description="用户提示词")
|
||
is_running: bool = Field(
|
||
default=False, description="正在运行,True 表示正在运行, False 表示运行结束"
|
||
)
|
||
awaiting_stream: bool = Field(
|
||
default=False,
|
||
description="等待流式输出,True 表示等待流式输出,False 表示已开始流式输出或已完成",
|
||
)
|
||
|
||
|
||
class ConversationHistoryItem(BaseModel):
|
||
"""
|
||
会话历史项类
|
||
"""
|
||
|
||
id: str = Field(..., description="会话唯一标识")
|
||
description: str = Field(..., description="会话描述")
|
||
created_at: str = Field(..., description="会话创建日期时间")
|
||
|
||
|
||
class MessageHistoryItem(Message):
|
||
"""
|
||
消息历史项类
|
||
"""
|
||
|
||
|
||
# 工作流适配器
|
||
WorkflowAdapter = TypeAdapter(Any)
|
||
|
||
|
||
def workflow_validate_json(workflow: str) -> Any:
|
||
"""
|
||
将工作流反序列化
|
||
"""
|
||
if not workflow:
|
||
return None
|
||
return WorkflowAdapter.validate_json(workflow)
|
||
|
||
|
||
def workflow_dump_json(workflow: Any) -> str:
|
||
"""
|
||
将工作流序列化
|
||
:param workflow: 工作流
|
||
:return: 字符串
|
||
"""
|
||
return WorkflowAdapter.dump_json(workflow).decode("utf-8")
|
||
|
||
|
||
# 使用量(RunUsage)适配器
|
||
UsageAdapter = TypeAdapter(RunUsage)
|
||
|
||
|
||
def usage_validate_json(usage: str) -> RunUsage:
|
||
"""
|
||
将使用量反序列化
|
||
"""
|
||
if not usage:
|
||
return RunUsage()
|
||
return UsageAdapter.validate_json(usage)
|
||
|
||
|
||
def usage_dump_json(usage: RunUsage) -> str:
|
||
"""
|
||
将使用量序列化
|
||
:param usage: 使用量
|
||
:return: 字符串
|
||
"""
|
||
return UsageAdapter.dump_json(usage).decode("utf-8")
|