feat: type

This commit is contained in:
Yeuoly
2024-08-30 21:10:19 +08:00
parent db8bf2a85e
commit 279dee485d
7 changed files with 121 additions and 147 deletions

View File

@ -1,4 +1,5 @@
import json
from datetime import datetime
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
@ -13,7 +14,7 @@ from .model import Account, App, Tenant
from .types import StringUUID
class BuiltinToolProvider(db.Model):
class BuiltinToolProvider(Base):
"""
This table stores the tool provider information for built-in tools for each tenant.
"""
@ -25,61 +26,22 @@ class BuiltinToolProvider(db.Model):
)
# id of the tool provider
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
# id of the tenant
tenant_id = db.Column(StringUUID, nullable=True)
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=True)
# who created this tool provider
user_id = db.Column(StringUUID, nullable=False)
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
# name of the tool provider
provider = db.Column(db.String(40), nullable=False)
provider: Mapped[str] = mapped_column(db.String(40), nullable=False)
# credential of the tool provider
encrypted_credentials = db.Column(db.Text, nullable=True)
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
encrypted_credentials: Mapped[str] = mapped_column(db.Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
updated_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
@property
def credentials(self) -> dict:
return json.loads(self.encrypted_credentials)
class PublishedAppTool(db.Model):
"""
The table stores the apps published as a tool for each person.
"""
__tablename__ = 'tool_published_apps'
__table_args__ = (
db.PrimaryKeyConstraint('id', name='published_app_tool_pkey'),
db.UniqueConstraint('app_id', 'user_id', name='unique_published_app_tool')
)
# id of the tool provider
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
# id of the app
app_id = db.Column(StringUUID, ForeignKey('apps.id'), nullable=False)
# who published this tool
user_id = db.Column(StringUUID, nullable=False)
# description of the tool, stored in i18n format, for human
description = db.Column(db.Text, nullable=False)
# llm_description of the tool, for LLM
llm_description = db.Column(db.Text, nullable=False)
# query description, query will be seem as a parameter of the tool, to describe this parameter to llm, we need this field
query_description = db.Column(db.Text, nullable=False)
# query name, the name of the query parameter
query_name = db.Column(db.String(40), nullable=False)
# name of the tool provider
tool_name = db.Column(db.String(40), nullable=False)
# author
author = db.Column(db.String(40), nullable=False)
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
@property
def description_i18n(self) -> I18nObject:
return I18nObject(**json.loads(self.description))
@property
def app(self) -> App | None:
return db.session.query(App).filter(App.id == self.app_id).first()
class ApiToolProvider(Base):
"""
The table stores the api providers.
@ -129,14 +91,14 @@ class ApiToolProvider(Base):
return json.loads(self.credentials_str)
@property
def user(self) -> Account:
def user(self) -> Account | None:
return db.session.query(Account).filter(Account.id == self.user_id).first()
@property
def tenant(self) -> Tenant:
def tenant(self) -> Tenant | None:
return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
class ToolLabelBinding(db.Model):
class ToolLabelBinding(Base):
"""
The table stores the labels for tools.
"""
@ -146,15 +108,15 @@ class ToolLabelBinding(db.Model):
db.UniqueConstraint('tool_id', 'label_name', name='unique_tool_label_bind'),
)
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
# tool id
tool_id = db.Column(db.String(64), nullable=False)
tool_id: Mapped[str] = mapped_column(db.String(64), nullable=False)
# tool type
tool_type = db.Column(db.String(40), nullable=False)
tool_type: Mapped[str] = mapped_column(db.String(40), nullable=False)
# label name
label_name = db.Column(db.String(40), nullable=False)
label_name: Mapped[str] = mapped_column(db.String(40), nullable=False)
class WorkflowToolProvider(db.Model):
class WorkflowToolProvider(Base):
"""
The table stores the workflow providers.
"""
@ -165,41 +127,37 @@ class WorkflowToolProvider(db.Model):
db.UniqueConstraint('tenant_id', 'app_id', name='unique_workflow_tool_provider_app_id'),
)
id = db.Column(StringUUID, server_default=db.text('uuid_generate_v4()'))
id: Mapped[str] = mapped_column(StringUUID, server_default=db.text('uuid_generate_v4()'))
# name of the workflow provider
name = db.Column(db.String(40), nullable=False)
name: Mapped[str] = mapped_column(db.String(40), nullable=False)
# label of the workflow provider
label = db.Column(db.String(255), nullable=False, server_default='')
label: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default='')
# icon
icon = db.Column(db.String(255), nullable=False)
icon: Mapped[str] = mapped_column(db.String(255), nullable=False)
# app id of the workflow provider
app_id = db.Column(StringUUID, nullable=False)
app_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
# version of the workflow provider
version = db.Column(db.String(255), nullable=False, server_default='')
version: Mapped[str] = mapped_column(db.String(255), nullable=False, server_default='')
# who created this tool
user_id = db.Column(StringUUID, nullable=False)
user_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
# tenant id
tenant_id = db.Column(StringUUID, nullable=False)
tenant_id: Mapped[str] = mapped_column(StringUUID, nullable=False)
# description of the provider
description = db.Column(db.Text, nullable=False)
description: Mapped[str] = mapped_column(db.Text, nullable=False)
# parameter configuration
parameter_configuration = db.Column(db.Text, nullable=False, server_default='[]')
parameter_configuration: Mapped[str] = mapped_column(db.Text, nullable=False, server_default='[]')
# privacy policy
privacy_policy = db.Column(db.String(255), nullable=True, server_default='')
privacy_policy: Mapped[str] = mapped_column(db.String(255), nullable=True, server_default='')
created_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
created_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
updated_at: Mapped[datetime] = mapped_column(db.DateTime, nullable=False, server_default=db.text('CURRENT_TIMESTAMP(0)'))
@property
def schema_type(self) -> ApiProviderSchemaType:
return ApiProviderSchemaType.value_of(self.schema_type_str)
@property
def user(self) -> Account:
def user(self) -> Account | None:
return db.session.query(Account).filter(Account.id == self.user_id).first()
@property
def tenant(self) -> Tenant:
def tenant(self) -> Tenant | None:
return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
@property
@ -210,7 +168,7 @@ class WorkflowToolProvider(db.Model):
]
@property
def app(self) -> App:
def app(self) -> App | None:
return db.session.query(App).filter(App.id == self.app_id).first()
class ToolModelInvoke(db.Model):