#ifndef DBMS_VECTOR_REST_INTERNAL_HPP
|
#define DBMS_VECTOR_REST_INTERNAL_HPP
|
|
/*
|
* DBMS_VECTOR / DBMS_VECTOR_CHAIN shared REST/credential core.
|
*
|
* One implementation of the provider transport used by every REST-backed
|
* API of both packages (UTL_TO_EMBEDDING/UTL_TO_EMBEDDINGS today,
|
* UTL_TO_GENERATE_TEXT later): params parsing (provider/url/model/
|
* credential_name/transfer_timeout), the CHECK_REST_PARAMS pre-flight,
|
* credential resolution to a bearer key, the libcurl JSON POST, passthrough
|
* of non-reserved params into request bodies (REMOVE_ORACLE_KEYS parity)
|
* and the GET_PROVIDER_ERROR response parser. Business logic must not be
|
* duplicated into dbms_vector.cpp or dbms_vector_chain.cpp.
|
*/
|
|
#include <string>
|
|
extern "C"
|
{
|
|
#include "postgres.h"
|
#include "utils/jsonb.h"
|
|
} // extern "C"
|
|
/*
|
* Oracle DEFAULT_TRANSFER_TIMEOUT := 60 (seconds); applies to the whole
|
* REST request (connect + transfer), 0 disables the timeout.
|
*/
|
#define DBMS_VECTOR_DEFAULT_TRANSFER_TIMEOUT 60
|
|
/// Recognized REST params of the provider dispatch layer.
|
struct DbmsVectorRestParams
|
{
|
std::string provider = "database";
|
std::string url;
|
std::string model;
|
std::string credential_name;
|
int32_t transfer_timeout = DBMS_VECTOR_DEFAULT_TRANSFER_TIMEOUT;
|
};
|
|
/// One completed HTTP exchange (status 0 means curl reported no status).
|
struct DbmsVectorRestResponse
|
{
|
long status_code = 0;
|
std::string body;
|
};
|
|
/*
|
* Parse the jsonb params of a REST call: provider (default 'database',
|
* lowercased), url, model, credential_name and transfer_timeout
|
* (default 60 s). A nullptr params keeps every default.
|
*/
|
void dbms_vector_rest_parse_params(Jsonb *params,
|
DbmsVectorRestParams &out);
|
|
/*
|
* CHECK_REST_PARAMS parity (pls:3639): url, model and credential_name are
|
* required. A missing/unusable credential is reported by the lookup helper
|
* with the ORA-20001 parity error.
|
*/
|
void dbms_vector_rest_check_params(const DbmsVectorRestParams ¶ms);
|
|
/*
|
* Keys consumed by the dispatch layer and never forwarded to the provider
|
* (Oracle REMOVE_ORACLE_KEYS, pls:3664).
|
*/
|
bool dbms_vector_rest_is_reserved_key(const char *key, int len);
|
|
/*
|
* Append every non-reserved key/value pair of params to an object under
|
* construction (WJB_KEY/WJB_VALUE pairs; the caller owns the WJB_BEGIN/
|
* END_OBJECT brackets). This is the passthrough half of request-body
|
* builders: reserved keys are stripped exactly once, here.
|
*/
|
void dbms_vector_rest_push_passthrough(JsonbInState *state, Jsonb *params);
|
|
/*
|
* POST body as JSON with a bearer key (Oracle HTTP_SEND_REQUEST +
|
* Authorization header parity). Any transport failure raises; HTTP error
|
* statuses do not - the body is left to the response parser so the caller
|
* reports "provider returned an error" like Oracle does.
|
*/
|
DbmsVectorRestResponse dbms_vector_rest_call(
|
const DbmsVectorRestParams ¶ms, const std::string &api_key,
|
const char *body);
|
|
/*
|
* GET_PROVIDER_ERROR analog (pls:3731): extract a human-readable message
|
* from $.message / $.error.message / $.error[0] / $.error when the output is
|
* a short (<512 bytes) JSON document; otherwise return the raw output.
|
*/
|
std::string dbms_vector_rest_provider_error(const char *output);
|
|
/*
|
* Raw stored params of a credential (secrets included) for internal REST
|
* consumers. The SQL boundary (dbms_vector_get_credential,
|
* SYS.all_vector_credentials) redacts the secret values; this accessor is
|
* deliberately not SQL-exposed. Honors OWNER.NAME qualification and the
|
* owner-or-superuser access rule, returns a copy allocated in the caller's
|
* memory context, or raises the ORA-20001 parity error.
|
*/
|
Jsonb *dbms_vector_credential_lookup(const std::string &credential_name);
|
|
/*
|
* Map the stored credential params onto the REST bearer key. Oracle
|
* providers always send 'Authorization: Bearer <KEY>' (pls:2253, 2335), so
|
* only the access_token form carries a usable key; the username/password
|
* and OCI forms raise a clear local error instead of a doomed HTTP call.
|
*/
|
std::string dbms_vector_rest_credential_key(Jsonb *credential,
|
const std::string
|
&credential_name);
|
|
#endif // DBMS_VECTOR_REST_INTERNAL_HPP
|