-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodel.py
More file actions
248 lines (202 loc) · 7.62 KB
/
model.py
File metadata and controls
248 lines (202 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""Credential 模型定义 / Credential Model Definitions
定义凭证相关的数据模型和枚举。
Defines data models and enumerations related to credentials.
"""
from enum import Enum
from typing import Any, Dict, List, Optional
from agentrun.utils.config import Config
from agentrun.utils.model import BaseModel, PageableInput
class CredentialAuthType(str, Enum):
"""凭证认证类型 / Credential Authentication Type"""
JWT = "jwt"
API_KEY = "api_key"
BASIC = "basic"
AKSK = "ak_sk"
CUSTOM_HEADER = "custom_header"
class CredentialSourceType(str, Enum):
"""凭证来源类型 / Credential Source Type"""
LLM = "external_llm"
"""模型凭证"""
TOOL = "external_tool"
"""工具凭证"""
INTERNAL = "internal"
"""自定义凭证"""
class CredentialBasicAuth(BaseModel):
username: Optional[str]
password: Optional[str]
class RelatedResource(BaseModel):
resource_id: Optional[str] = None
"""资源 ID"""
resource_name: Optional[str] = None
"""资源名称"""
resource_type: Optional[str] = None
"""资源类型"""
class CredentialConfigInner(BaseModel):
credential_auth_type: Optional[CredentialAuthType] = None
"""凭证认证类型"""
credential_source_type: Optional[CredentialSourceType] = None
"""凭证来源类型"""
credential_public_config: Optional[Dict[str, Any]] = None
"""凭证公共配置"""
credential_secret: Optional[str] = None
"""凭证密钥"""
class CredentialConfig(CredentialConfigInner):
"""凭证配置"""
@classmethod
def inbound_api_key(cls, api_key: str, header_key: str = "Authorization"):
"""配置访问 AgentRun 的 api key 凭证"""
return cls(
credential_source_type=CredentialSourceType.INTERNAL,
credential_auth_type=CredentialAuthType.API_KEY,
credential_public_config={"headerKey": header_key},
credential_secret=api_key,
)
@classmethod
def inbound_static_jwt(cls, jwks: str):
"""配置访问 AgentRun 的静态 JWKS 凭证"""
return cls(
credential_source_type=CredentialSourceType.INTERNAL,
credential_auth_type=CredentialAuthType.JWT,
credential_public_config={"authType": "static_jwks", "jwks": jwks},
credential_secret="",
)
@classmethod
def inbound_remote_jwt(
cls, uri: str, timeout: int = 3000, ttl: int = 30000, **kwargs
):
"""配置访问 AgentRun 的远程 JWT 凭证"""
return cls(
credential_source_type=CredentialSourceType.INTERNAL,
credential_auth_type=CredentialAuthType.JWT,
credential_public_config={
"uri": uri,
"timeout": timeout,
"ttl": ttl,
**kwargs,
},
credential_secret="",
)
@classmethod
def inbound_basic(cls, users: List[CredentialBasicAuth]):
"""配置访问 AgentRun 的 Basic 凭证"""
return cls(
credential_source_type=CredentialSourceType.INTERNAL,
credential_auth_type=CredentialAuthType.BASIC,
credential_public_config={
"users": [user.model_dump() for user in users]
},
credential_secret="",
)
@classmethod
def outbound_llm_api_key(cls, api_key: str, provider: str):
"""配置访问第三方模型的 api key 凭证"""
return cls(
credential_source_type=CredentialSourceType.LLM,
credential_auth_type=CredentialAuthType.API_KEY,
credential_public_config={"provider": provider},
credential_secret=api_key,
)
@classmethod
def outbound_tool_api_key(cls, api_key: str):
"""配置访问第三方工具的 api key 凭证"""
return cls(
credential_source_type=CredentialSourceType.TOOL,
credential_auth_type=CredentialAuthType.API_KEY,
credential_public_config={},
credential_secret=api_key,
)
@classmethod
def outbound_tool_ak_sk(
cls,
provider: str,
access_key_id: str,
access_key_secret: str,
account_id: str,
):
"""配置访问第三方工具的 ak/sk 凭证"""
return cls(
credential_source_type=CredentialSourceType.TOOL,
credential_auth_type=CredentialAuthType.AKSK,
credential_public_config={
"provider": provider,
"authConfig": {
"accessKey": access_key_id,
"accountId": account_id,
},
},
credential_secret=access_key_secret,
)
@classmethod
def outbound_tool_ak_sk_custom(cls, auth_config: Dict[str, str]):
"""配置访问第三方工具的自定义凭证"""
return cls(
credential_source_type=CredentialSourceType.TOOL,
credential_auth_type=CredentialAuthType.AKSK,
credential_public_config={
"provider": "custom",
"authConfig": auth_config,
},
credential_secret="",
)
@classmethod
def outbound_tool_custom_header(cls, headers: Dict[str, str]):
"""配置访问第三方工具的自定义 Header 凭证"""
return cls(
credential_source_type=CredentialSourceType.TOOL,
credential_auth_type=CredentialAuthType.CUSTOM_HEADER,
credential_public_config={"authConfig": headers},
credential_secret="",
)
class CredentialMutableProps(BaseModel):
"""凭证公共配置"""
# credential_secret: Optional[str] = None
"""凭证密钥"""
description: Optional[str] = None
"""描述"""
enabled: Optional[bool] = None
"""是否启用"""
class CredentialImmutableProps(BaseModel):
credential_name: Optional[str] = None
"""凭证名称"""
class CredentialSystemProps(CredentialConfigInner):
credential_id: Optional[str] = None
"""凭证 ID"""
created_at: Optional[str] = None
"""创建时间"""
updated_at: Optional[str] = None
"""更新时间"""
related_resources: Optional[List[RelatedResource]] = None
"""关联资源"""
class CredentialCreateInput(CredentialImmutableProps, CredentialMutableProps):
"""凭证创建输入参数"""
credential_config: CredentialConfig
class CredentialUpdateInput(CredentialMutableProps):
credential_config: Optional[CredentialConfig] = None
class CredentialListInput(PageableInput):
credential_auth_type: Optional[CredentialAuthType] = None
"""凭证认证类型"""
credential_name: Optional[str] = None
"""凭证名称"""
credential_source_type: Optional[CredentialSourceType] = None
"""凭证来源类型(必填)"""
provider: Optional[str] = None
"""提供商"""
class CredentialListOutput(BaseModel):
created_at: Optional[str] = None
credential_auth_type: Optional[str] = None
credential_id: Optional[str] = None
credential_name: Optional[str] = None
credential_source_type: Optional[str] = None
enabled: Optional[bool] = None
related_resource_count: Optional[int] = None
updated_at: Optional[str] = None
async def to_credential_async(self, config: Optional[Config] = None):
from .client import CredentialClient
return await CredentialClient(config).get_async(
self.credential_name or "", config=config
)
def to_credential(self, config: Optional[Config] = None):
from .client import CredentialClient
return CredentialClient(config).get(
self.credential_name or "", config=config
)