-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathretool2api.py
More file actions
349 lines (313 loc) · 19.9 KB
/
retool2api.py
File metadata and controls
349 lines (313 loc) · 19.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from curl_cffi import requests
from chatbridge.chatbridge import *
from dotenv import load_dotenv
from fastapi import FastAPI
from bs4 import BeautifulSoup
import os
import uvicorn
import json
import time
load_dotenv()
app = FastAPI(title="retool2api")
chat_id = 0
access_token = os.getenv("accessToken")
x_xsrf_token = os.getenv("x_xsrf_token")
url_header = os.getenv("url_header")
if not access_token or not x_xsrf_token or not url_header:
raise ValueError(
"accessToken, x_xsrf_token, or url_header is not set in the environment variables."
)
def get_agent_id():
url = "https://googlexxxx.retool.com/api/agents"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.get(url, headers=headers, impersonate="chrome")
json_res = response.json()
agent_list = json_res.get("agents", [])
return agent_list[0].get("id", "") if agent_list else None
agent_id = get_agent_id()
@app.post("/v1/chat/completions")
@chatCompletions(1)
def retool(prompt: str, res: ChatResponse, new_session: bool):
global chat_id
print(new_session, chat_id)
set_model(model=res.model)
if new_session:
chat_id = 0
if chat_id != 0:
return retool2(chat_id, prompt)
url = f"{url_header}/api/agents/{agent_id}/threads"
print(url)
payload = {"name": f"tool use", "timezone": "Asia/Shanghai"}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
json_res = response.json()
chat_id = json_res.get("id")
print(f"Chat ID: {chat_id}")
if chat_id == None:
return {"error": f"{response.text}"}
print(prompt)
return retool2(chat_id, prompt)
def retool2(id, prompt: str):
url = f"{url_header}/api/agents/{agent_id}/threads/{id}/messages"
payload = {"type": "text", "text": prompt, "timezone": "Asia/Shanghai"}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
json_res = response.json()
run_id = json_res.get("content", {}).get("runId")
print(f"Run ID: {run_id}")
return retool3(run_id)
def should_continue(url, headers):
# 等待一段时间以确保请求完成
index = 0
while index < 10: # 尝试十次
index += 1
print(f"Checking status... Attempt {index}")
time.sleep(2)
res = requests.get(url, headers=headers)
json_res = res.json()
if json_res.get("status") == "COMPLETED":
print("Log completed.")
return json_res
def retool3(id):
url = f"{url_header}/api/agents/{agent_id}/logs/{id}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
json_res = should_continue(url, headers)
if json_res == None:
return {"error": "Log not completed or not found."}
trace = json_res.get("trace", [])
if not trace:
print("No trace found in the response.")
return
if (
trace[-1].get("reason", "") == "finished"
and trace[-1].get("spanType", "") == "AGENT_END"
):
response = trace[-1].get("data", {}).get("data", {}).get("content", "")
return response
@app.get("/v1/models")
@get_model_list
def get_models():
model_ids = [
("gpt-4o", "openAI"),
("o3", "anthropic"),
("o3-mini", "anthropic"),
("gpt-4.1", "openAI"),
("gpt-4.1-mini", "openAI"),
("gpt-4o-mini", "openAI"),
("claude-opus-4-20250514", "anthropic"),
("claude-sonnect-4-20250514", "anthropic"),
("claude-3-5-haiku-20241022", "anthropic"),
("deepseek-v3", "ossProvider"),
]
return model_ids
def set_model(model: str):
provider = ""
if model.startswith("gpt-") or model.startswith("o3"):
provider = "openAI"
elif model.startswith("claude-"):
provider = "anthropic"
elif model.startswith("deepseek-"):
provider = "ossProvider"
print(provider, model)
url = f"{url_header}/api/workflow/{agent_id}"
payload = {
"newWorkflowData": {
"id": "d2443c4c-02cd-4dfb-b090-0c89b4aab158",
"saveId": "d34d2da0-1de2-4acd-a641-34ccc7229a1d",
"name": "test",
"apiKey": "retool_wk_48442e2440fb4aa89fa2dc713ca733f5",
"description": "",
"organizationId": 1154899,
"isEnabled": False,
"crontab": None,
"timezone": "Asia/Shanghai",
"blockData": [
{
"top": 48,
"left": 48,
"uuid": "f5192d1b-4892-4248-82b7-dee4a75dac11",
"options": {},
"pluginId": "startTrigger",
"blockType": "webhook",
"editorType": "JavascriptQuery",
"environment": "production",
"isMinimized": False,
"resourceName": "webhook",
"incomingOnSuccessEdges": [],
},
{
"top": 48,
"left": 480,
"uuid": "39bd2c48-8c2e-4765-8f87-0c288599f906",
"pluginId": "aiAgentExecute1",
"blockType": "aiAgentExecute",
"editorType": "JavascriptQuery",
"environment": "production",
"isMinimized": False,
"resourceName": "RetoolAIAgentExecuteQuery",
"incomingPorts": [],
"incomingOnSuccessEdges": ["f5192d1b-4892-4248-82b7-dee4a75dac11"],
},
],
"templateData": '["~#iR",["^ ","n","appTemplate","v",["^ ","appMaxWidth","100%","appStyles","","appTesting",null,"appThemeId",null,"appThemeModeId",null,"appThemeName",null,"createdAt",null,"customComponentCollections",[],"customDocumentTitle","","customDocumentTitleEnabled",false,"customShortcuts",[],"experimentalDataTabEnabled",false,"experimentalFeatures",["^ ","disableMultiplayerEditing",false,"multiplayerEditingEnabled",false,"sourceControlTemplateDehydration",false],"folders",["~#iL",[]],"formAppSettings",["^ ","customRedirectUrl",""],"inAppRetoolPillAppearance","NO_OVERRIDE","instrumentationEnabled",false,"internationalizationSettings",["^ ","internationalizationEnabled",false,"internationalizationFiles",[]],"isFetching",false,"isFormApp",false,"isGlobalWidget",false,"isMobileApp",false,"loadingIndicatorsDisabled",false,"markdownLinkBehavior","auto","mobileAppSettings",["^ ","displaySetting",["^ ","landscapeMode",false,"tabletMode",false],"mobileOfflineModeBannerMode","default","mobileOfflineModeDelaySync",false,"mobileOfflineModeEnabled",false],"mobileOfflineAssets",[],"multiScreenMobileApp",false,"notificationsSettings",["^ ","globalQueryShowFailureToast",true,"globalQueryShowSuccessToast",false,"globalQueryToastDuration",4.5,"globalToastPosition","bottomRight"],"pageCodeFolders",["^ "],"pageLoadValueOverrides",["^B",[]],"persistUrlParams",false,"plugins",["~#iOM",["startTrigger",["^0",["^ ","n","pluginTemplate","v",["^ ","id","startTrigger","uuid",null,"comment",null,"type","datasource","subtype","JavascriptQuery","namespace",null,"resourceName","JavascriptQuery","resourceDisplayName",null,"template",["~#iM",["queryRefreshTime","","allowedGroupIds",["^B",[]],"streamResponse",false,"lastReceivedFromResourceAt",null,"isFunction",false,"functionParameters",null,"queryDisabledMessage","","servedFromCache",false,"offlineUserQueryInputs","","functionDescription",null,"successMessage","","queryDisabled","","playgroundQuerySaveId","latest","workflowParams",null,"resourceNameOverride","","runWhenModelUpdates",false,"workflowRunExecutionType","sync","showFailureToaster",true,"query","return null","playgroundQueryUuid","","playgroundQueryId",null,"error",null,"workflowRunBodyType","raw","privateParams",["^B",[]],"queryRunOnSelectorUpdate",false,"runWhenPageLoadsDelay","","data",null,"importedQueryInputs",["^1?",[]],"_additionalScope",["^B",[]],"isImported",false,"showSuccessToaster",true,"cacheKeyTtl","","requestSentTimestamp",null,"metadata",null,"queryRunTime",null,"changesetObject","","offlineOptimisticResponse",null,"errorTransformer","return data.error","finished",null,"confirmationMessage",null,"isFetching",false,"changeset","","rawData",null,"queryTriggerDelay","0","resourceTypeOverride",null,"watchedParams",["^B",[]],"enableErrorTransformer",false,"showLatestVersionUpdatedWarning",false,"timestamp",0,"evalType","script","importedQueryDefaults",["^1?",[]],"enableTransformer",false,"showUpdateSetValueDynamicallyToggle",true,"overrideOrgCacheForUserCache",false,"runWhenPageLoads",false,"transformer","return data","events",["^B",[]],"queryTimeout","100000","workflowId",null,"requireConfirmation",false,"queryFailureConditions","","changesetIsObject",false,"enableCaching",false,"allowedGroups",["^B",[]],"offlineQueryType","None","queryThrottleTime","750","updateSetValueDynamically",false,"notificationDuration",""]],"style",null,"position2",null,"mobilePosition2",null,"mobileAppPosition",null,"tabIndex",null,"container","","^7","~m1752662537424","updatedAt","~m1752662537424","folder","","presetName",null,"screen",null,"boxId",null,"subBoxIds",null]]],"aiAgentExecute1",["^0",["^ ","n","pluginTemplate","v",["^ ","id","aiAgentExecute1","^17",null,"^18",null,"^19","datasource","^1:","JavascriptQuery","^1;",null,"^1<","RetoolAIAgentExecuteQuery","^1=",null,"^1>",["^1?",["queryRefreshTime","","allowedGroupIds",["^B",[]],"streamResponse",false,"lastReceivedFromResourceAt",null,"isFunction",false,"functionParameters",null,"queryDisabledMessage","","servedFromCache",false,"offlineUserQueryInputs","","functionDescription",null,"successMessage","","queryDisabled","","instructions","You are a helpful assistant chatting with { current_user.firstName } { current_user.lastName }. Their email is { current_user.email }, and the date is { new Date() }.","playgroundQuerySaveId","latest","workflowParams",null,"resourceNameOverride","","runWhenModelUpdates",false,"workflowRunExecutionType","sync","showFailureToaster",true,"query","","playgroundQueryUuid","","playgroundQueryId",null,"error",null,"workflowRunBodyType","raw","privateParams",["^B",[]],"model","{model}","queryRunOnSelectorUpdate",false,"runWhenPageLoadsDelay","","data",null,"providerId","retoolAIBuiltIn::{provider}","importedQueryInputs",["^1?",[]],"_additionalScope",["^B",[]],"isImported",false,"showSuccessToaster",true,"cacheKeyTtl","","requestSentTimestamp",null,"metadata",null,"queryRunTime",null,"changesetObject","","offlineOptimisticResponse",null,"errorTransformer","return data.error","finished",null,"confirmationMessage",null,"isFetching",false,"changeset","","drafts",[],"rawData",null,"queryTriggerDelay","0","resourceTypeOverride",null,"watchedParams",["^B",[]],"temperature",0.00,"enableErrorTransformer",false,"showLatestVersionUpdatedWarning",false,"timestamp",0,"mcpServers",[],"importedQueryDefaults",["^1?",[]],"enableTransformer",false,"showUpdateSetValueDynamicallyToggle",true,"overrideOrgCacheForUserCache",false,"runWhenPageLoads",false,"transformer","return data","events",["^B",[]],"queryTimeout","100000","workflowId",null,"maxIterations",11,"requireConfirmation",false,"queryFailureConditions","","tools",[],"changesetIsObject",false,"providerName","{provider}","enableCaching",false,"allowedGroups",["^B",[]],"offlineQueryType","None","queryThrottleTime","750","updateSetValueDynamically",false,"notificationDuration",""]],"^1@",null,"^1A",null,"^1B",null,"^1C",null,"^1D",null,"^1E","","^7","~m1752662537424","^1F","~m1752678921915","^1G","","^1H",null,"^1I",null,"^1J",null,"^1K",null]]]]],"preloadedAppJavaScript",null,"preloadedAppJSLinks",[],"queryStatusVisibility",false,"responsiveLayoutDisabled",false,"rootScreen",null,"savePlatform","web","shortlink",null,"testEntities",[],"tests",[],"urlFragmentDefinitions",["^B",[]],"version","3.237.0","serializedLayout",null,"agentEvals",["^ "]]]]',
"triggerWebhooks": [
{
"uuid": "startTrigger",
"name": "startTrigger",
"inputSchema": {"properties": []},
"useHeaderApiKey": False,
"exampleInputJSON": "",
"headers": "{}",
"examplePathParams": "",
}
],
"releaseId": "2f5321f2-6043-443c-a58c-33e79c2147d2",
"customLibraries": [
{
"version": "4.17.21",
"language": "javascript",
"variable": "_",
"codeString": "/* Edit library variable below */\n\nconst _ = require('lodash')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n",
"libraryName": "lodash",
},
{
"version": "2.1.0",
"language": "javascript",
"variable": "numbro",
"codeString": "/* Edit library variable below */\n\nconst numbro = require('numbro')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n",
"libraryName": "numbro",
},
{
"version": "5.3.2",
"language": "javascript",
"variable": "Papa",
"codeString": "/* Edit library variable below */\n\nconst Papa = require('papaparse')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n",
"libraryName": "papaparse",
},
{
"version": "0.5.23",
"language": "javascript",
"variable": "moment",
"codeString": "/* Edit library variable below */\n\nconst moment = require('moment-timezone')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n",
"libraryName": "moment-timezone",
},
{
"version": "3.4.0",
"language": "javascript",
"variable": "uuid",
"codeString": "/* Edit library variable below */\n\nconst uuid = require('uuid')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n",
"libraryName": "uuid",
},
],
"createdAt": "2025-07-16T15:14:45.849Z",
"updatedAt": "2025-07-16T15:14:45.849Z",
"createdBy": 1793219,
"folderId": 7690296,
"protected": False,
"javascriptLanguageConfigurationSaveId": None,
"pythonLanguageConfigurationSaveId": None,
"setupScripts": {
"python": {"codeString": ""},
"javascript": {
"codeString": "// lodash\n/* Edit library variable below */\n\nconst _ = require('lodash')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n\n// numbro\n/* Edit library variable below */\n\nconst numbro = require('numbro')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n\n// papaparse\n/* Edit library variable below */\n\nconst Papa = require('papaparse')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n\n// moment-timezone\n/* Edit library variable below */\n\nconst moment = require('moment-timezone')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n\n// uuid\n/* Edit library variable below */\n\nconst uuid = require('uuid')\n\n/* Add destructured imports from library below\neg. const { pow, log } = require(\"mathjs\") */\n"
},
},
"subflows": [],
"type": "agent",
"iconName": "CircleDashedDuotone",
"iconColor": "gray",
"hasProtectedTriggers": False,
"protectedTriggers": [],
"accessLevel": "own",
}
}
payload_str = json.dumps(payload)
payload_str = payload_str.replace("{model}", model).replace("{provider}", provider)
payload = json.loads(payload_str)
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.post(
url, data=json.dumps(payload), headers=headers, impersonate="chrome"
)
def get_thread_id():
thread_id_list = []
url = f"{url_header}/api/agents/{agent_id}/threads"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.get(url, headers=headers)
threads = response.json().get("threads", [])
if not threads:
print("No threads found.")
return []
for thread in threads:
print(
f"Thread ID: {thread.get('id')}, Name: {thread.get('name')}, Created At: {thread.get('createdAt')}"
)
thread_id_list.append(thread.get("id"))
return thread_id_list
def del_thread():
thread_id_list = get_thread_id()
for thread_id in thread_id_list:
url = f"{url_header}/api/agents/{agent_id}/threads/{thread_id}"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"Accept": "application/json",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
"x-xsrf-token": f"{x_xsrf_token}",
"accept-language": "zh-CN,zh;q=0.9",
"Cookie": f"accessToken={access_token}",
}
response = requests.delete(url, headers=headers)
print(response.text)
def main():
uvicorn.run("retool2api:app", host="0.0.0.0", port=10001, reload=True)
if __name__ == "__main__":
print(f"Agent ID: {agent_id}")
del_thread()
main()