Skip to content

Commit 5572a4d

Browse files
authored
feat:(chat-api): add ChatService APIs. (#23)
add ChatService APIs: 1.chat_again 2.get_conversation_list 3.get_conversation_inputs 4.update_conversation 5.delete_conversation 6.stop_message 7.clear_message 8.get_conversation_messages 9.get_message_info 10.delete_message 11.feedback 12.set_message_answer_used 13.get_suggested_questions 14.run_app_workflow 15.sync_run_app_workflow 16.query_run_app_process 17.list_oauth2_token 18.event_trigger_webhook 19.chat_continue 20.list_long_memory 21.update_long_memory 22.delete_long_memory 23.clear_long_memory 24.async_resume_app_workflow 25.set_conversation_top 26.cancel_conversation_top 27.query_skill_async_task 28.sync_resume_app_workflow_blocking 29.sync_resume_app_workflow_streaming 30.get_app_user_variables 31.set_app_user_variables 32.query_trigger_run_records
1 parent 677f7ba commit 5572a4d

File tree

3 files changed

+2070
-46
lines changed

3 files changed

+2070
-46
lines changed

libs/api/hiagent_api/base.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ def __call__(self, r):
6767

6868
class Service(object):
6969
def __init__(
70-
self,
71-
service_info: ServiceInfo,
72-
api_info: dict[str, ApiInfo],
73-
http_client: Optional[httpx.Client] = None,
74-
async_http_client: Optional[httpx.AsyncClient] = None,
70+
self,
71+
service_info: ServiceInfo,
72+
api_info: dict[str, ApiInfo],
73+
http_client: Optional[httpx.Client] = None,
74+
async_http_client: Optional[httpx.AsyncClient] = None,
7575
):
7676
self.service_info = service_info
7777
self.api_info = api_info
7878
self.init()
7979
self.init_http_client(http_client, async_http_client)
8080

8181
def init_http_client(
82-
self,
83-
http_client: Optional[httpx.Client] = None,
84-
async_http_client: Optional[httpx.AsyncClient] = None,
82+
self,
83+
http_client: Optional[httpx.Client] = None,
84+
async_http_client: Optional[httpx.AsyncClient] = None,
8585
):
8686
if http_client:
8787
self.http_client = http_client
@@ -361,9 +361,9 @@ async def _arequest(self, action, params):
361361
def prepare_request(self, api_info, params, doseq=0):
362362
for key in params:
363363
if (
364-
type(params[key]) == int
365-
or type(params[key]) == float
366-
or type(params[key]) == bool
364+
type(params[key]) == int
365+
or type(params[key]) == float
366+
or type(params[key]) == bool
367367
):
368368
params[key] = str(params[key])
369369
elif sys.version_info[0] != 3:
@@ -444,12 +444,12 @@ def sign_sts2(self, policy, expire):
444444
inner_token.signature = Util.to_hex(Util.hmac_sha256(key, sign_str))
445445

446446
sts.session_token = (
447-
"STS2"
448-
+ base64.b64encode(
449-
json.dumps(inner_token, cls=ComplexEncoder, sort_keys=True)
450-
.replace(" ", "")
451-
.encode("utf-8")
452-
).decode()
447+
"STS2"
448+
+ base64.b64encode(
449+
json.dumps(inner_token, cls=ComplexEncoder, sort_keys=True)
450+
.replace(" ", "")
451+
.encode("utf-8")
452+
).decode()
453453
)
454454
return sts
455455

@@ -459,14 +459,14 @@ def to_rfc3339(t):
459459
pos = format_time.find("+")
460460
if pos == -1:
461461
pos = format_time.find("-")
462-
return format_time[: pos + 3] + ":" + format_time[pos + 3 : pos + 5]
462+
return format_time[: pos + 3] + ":" + format_time[pos + 3: pos + 5]
463463

464464

465465
class AppAPIMixin:
466466
def __init__(
467-
self,
468-
http_client: httpx.Client,
469-
async_http_client: httpx.AsyncClient,
467+
self,
468+
http_client: httpx.Client,
469+
async_http_client: httpx.AsyncClient,
470470
) -> None:
471471
self.base_url = ""
472472
self.http_client = http_client
@@ -475,7 +475,7 @@ def __init__(
475475
def set_app_base_url(self, base_url: str):
476476
self.base_url = base_url
477477

478-
async def _apost(self, app_key: str, action: str, params: dict) -> str:
478+
async def _apost(self, app_key: str, action: str, params: dict, _headers: Optional[dict]) -> str:
479479
if self.base_url == "":
480480
raise Exception(
481481
"base_url not set, you should call set_app_base_url() first"
@@ -484,7 +484,8 @@ async def _apost(self, app_key: str, action: str, params: dict) -> str:
484484
app_url = f"{self.base_url}/{action}"
485485

486486
headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
487-
487+
if _headers is not None:
488+
headers.update(_headers)
488489
response = await self.async_http_client.post(
489490
app_url, json=params, headers=headers
490491
)
@@ -497,15 +498,17 @@ async def _apost(self, app_key: str, action: str, params: dict) -> str:
497498
raise Exception("empty response")
498499
return res_text
499500

500-
def _post(self, app_key: str, action: str, params: dict) -> str:
501+
def _post(self, app_key: str, action: str, params: dict, _headers: Optional[dict]) -> str:
501502
if self.base_url == "":
502503
raise Exception(
503504
"base_url not set, you should call set_app_base_url() first"
504505
)
505506

506507
app_url = f"{self.base_url}/{action}"
507-
508508
headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
509+
if _headers is not None:
510+
headers.update(_headers)
511+
509512
response = self.http_client.post(app_url, json=params, headers=headers)
510513
try:
511514
response.raise_for_status() # Raise an exception for bad status codes
@@ -517,7 +520,7 @@ def _post(self, app_key: str, action: str, params: dict) -> str:
517520
return res_text
518521

519522
def _sse_post(
520-
self, app_key: str, action: str, params: dict
523+
self, app_key: str, action: str, params: dict
521524
) -> Generator[ServerSentEvent, None, None]:
522525
if self.base_url == "":
523526
raise Exception(
@@ -529,17 +532,17 @@ def _sse_post(
529532
headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
530533

531534
with connect_sse(
532-
self.http_client,
533-
method="POST",
534-
url=app_url,
535-
json=params,
536-
headers=headers,
535+
self.http_client,
536+
method="POST",
537+
url=app_url,
538+
json=params,
539+
headers=headers,
537540
) as event_source:
538541
for sse in event_source.iter_sse():
539542
yield sse
540543

541544
async def _asse_post(
542-
self, app_key: str, action: str, params: dict
545+
self, app_key: str, action: str, params: dict
543546
) -> AsyncGenerator[ServerSentEvent, None]:
544547
if self.base_url == "":
545548
raise Exception(
@@ -551,11 +554,11 @@ async def _asse_post(
551554
headers = {"Apikey": f"{app_key}", "Content-Type": "application/json"}
552555

553556
async with aconnect_sse(
554-
self.async_http_client,
555-
method="POST",
556-
url=app_url,
557-
json=params,
558-
headers=headers,
557+
self.async_http_client,
558+
method="POST",
559+
url=app_url,
560+
json=params,
561+
headers=headers,
559562
) as event_source:
560563
async for sse in event_source.aiter_sse():
561564
yield sse

0 commit comments

Comments
 (0)