Add resource upload APIs
This commit is contained in:
@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
@ -136,6 +137,59 @@ class OpenVikingMemorySystemClient:
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
async def upload_temp_file(self, credential: OpenVikingCredential | str, path: str | Path) -> dict[str, Any]:
|
||||
file_path = Path(path)
|
||||
async with self._credential_client(credential, json_content_type=False) as client:
|
||||
with file_path.open("rb") as file_obj:
|
||||
response = await client.post(
|
||||
"/api/v1/resources/temp_upload",
|
||||
files={"file": (file_path.name, file_obj)},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
async def add_resource(
|
||||
self,
|
||||
credential: OpenVikingCredential | str,
|
||||
*,
|
||||
to: str,
|
||||
reason: str | None = None,
|
||||
wait: bool = True,
|
||||
directly_upload_media: bool = True,
|
||||
path: str | None = None,
|
||||
temp_file_id: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {
|
||||
"to": to,
|
||||
"wait": wait,
|
||||
"directly_upload_media": directly_upload_media,
|
||||
}
|
||||
if reason is not None:
|
||||
payload["reason"] = reason
|
||||
if temp_file_id is not None:
|
||||
payload["temp_file_id"] = temp_file_id
|
||||
else:
|
||||
payload["path"] = path
|
||||
|
||||
async with self._credential_client(credential) as client:
|
||||
response = await client.post("/api/v1/resources", json=payload)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
async def delete_resource(
|
||||
self,
|
||||
credential: OpenVikingCredential | str,
|
||||
uri: str,
|
||||
recursive: bool = True,
|
||||
) -> dict[str, Any]:
|
||||
async with self._credential_client(credential) as client:
|
||||
response = await client.delete(
|
||||
"/api/v1/fs",
|
||||
params={"uri": uri, "recursive": str(recursive).lower()},
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
async def find(self, credential: OpenVikingCredential | str, query: str, limit: int) -> dict[str, Any]:
|
||||
user_id = credential.user_id if isinstance(credential, OpenVikingCredential) else None
|
||||
target_uri = f"viking://user/{user_id}/memories/" if user_id else "viking://user/memories/"
|
||||
@ -198,11 +252,15 @@ class OpenVikingMemorySystemClient:
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
|
||||
def _credential_client(self, credential: OpenVikingCredential | str) -> httpx.AsyncClient:
|
||||
def _credential_client(
|
||||
self,
|
||||
credential: OpenVikingCredential | str,
|
||||
json_content_type: bool = True,
|
||||
) -> httpx.AsyncClient:
|
||||
if isinstance(credential, str):
|
||||
return self._client(credential)
|
||||
return self._client(credential, json_content_type=json_content_type)
|
||||
if credential.user_key_auth:
|
||||
return self._client(credential.api_key)
|
||||
return self._client(credential.api_key, json_content_type=json_content_type)
|
||||
headers = {}
|
||||
if credential.account_id:
|
||||
headers["X-OpenViking-Account"] = credential.account_id
|
||||
@ -210,10 +268,17 @@ class OpenVikingMemorySystemClient:
|
||||
headers["X-OpenViking-User"] = credential.user_id
|
||||
if credential.agent_id:
|
||||
headers["X-OpenViking-Agent"] = credential.agent_id
|
||||
return self._client(credential.api_key, headers)
|
||||
return self._client(credential.api_key, headers, json_content_type=json_content_type)
|
||||
|
||||
def _client(self, api_key: str, extra_headers: dict[str, str] | None = None) -> httpx.AsyncClient:
|
||||
headers = {"Content-Type": "application/json", "X-API-Key": api_key}
|
||||
def _client(
|
||||
self,
|
||||
api_key: str,
|
||||
extra_headers: dict[str, str] | None = None,
|
||||
json_content_type: bool = True,
|
||||
) -> httpx.AsyncClient:
|
||||
headers = {"X-API-Key": api_key}
|
||||
if json_content_type:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
return httpx.AsyncClient(
|
||||
|
||||
Reference in New Issue
Block a user