Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/ewanc26/wolfram. Pure C11 AT Protocol SDK: client-side, wire-level implementation (XRPC, OAuth/DPoP
ansi-c api-client at-protocol atproto bluesky c c-sdk decentralized decentralized-social embedded lexicon libwolfram memory-management oauth2 posix sdk shared-library static-library systems-programming xrpc
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949#include "wolfram/agent.h"
#include "wolfram/identity.h"#include "wolfram/repo.h"#include "wolfram/richtext.h"#include "wolfram/server.h"#include "wolfram/session.h"#include "wolfram/syntax.h"#include "wolfram/store.h"
#include <cJSON.h>#include "wolfram/atproto_lex.h"
#include <limits.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>
#define WF_AGENT_CREATE_RECORD_NSID "com.atproto.repo.createRecord"#define WF_AGENT_DELETE_RECORD_NSID "com.atproto.repo.deleteRecord"#define WF_AGENT_PUT_RECORD_NSID "com.atproto.repo.putRecord"#define WF_AGENT_POST_COLLECTION "app.bsky.feed.post"#define WF_AGENT_POST_RECORD_TYPE "app.bsky.feed.post"#define WF_AGENT_FOLLOW_COLLECTION "app.bsky.graph.follow"#define WF_AGENT_FOLLOW_RECORD_TYPE "app.bsky.graph.follow"#define WF_AGENT_LIKE_COLLECTION "app.bsky.feed.like"#define WF_AGENT_LIKE_RECORD_TYPE "app.bsky.feed.like"#define WF_AGENT_REPOST_COLLECTION "app.bsky.feed.repost"#define WF_AGENT_REPOST_RECORD_TYPE "app.bsky.feed.repost"#define WF_AGENT_PROFILE_COLLECTION "app.bsky.actor.profile"#define WF_AGENT_PROFILE_RECORD_TYPE "app.bsky.actor.profile"#define WF_AGENT_PROFILE_RKEY "self"#define WF_AGENT_UPLOAD_BLOB_NSID "com.atproto.repo.uploadBlob"#define WF_AGENT_RESOLVE_HANDLE_NSID "com.atproto.identity.resolveHandle"#define WF_AGENT_FACET_MENTION_TYPE "app.bsky.richtext.facet#mention"#define WF_AGENT_FACET_LINK_TYPE "app.bsky.richtext.facet#link"#define WF_AGENT_FACET_TAG_TYPE "app.bsky.richtext.facet#tag"
typedef struct wf_agent { wf_xrpc_client *client; /* Separate XRPC client for the Bluesky chat service (chat.bsky.convo.*). * Lazily created via wf_agent_chat_service_resolve. Kept in sync with the * private struct in _internal.h. */ wf_xrpc_client *chat_client; wf_session *session; char *service_url; /* Offline identity (for local repo mirror without network login). */ char *mirror_did; char *mirror_signing_key; /* Local repo mirror — a wf_car whose root is the latest verified commit. */ wf_car mirror; /* TLS settings, remembered rather than applied once: an agent owns three * clients (data plane, session, and the lazily-created chat client), and a * platform that needs a CA bundle or its own handshake RNG needs every one * of them configured, including ones that do not exist yet at the time the * application sets this. */ char *ca_bundle; wf_tls_rng_fn tls_rng; void *tls_rng_userdata;#ifdef WOLFRAM_BUILD_STORE /* Optional persistence target. Caller-owned; never freed by the agent. */ wf_store *store; /* Labels loaded from the attached store (or NULL). Aggregated across the * agent's own DID and any followed/known DIDs reached at load time. * Caller of wf_agent_load_labels_from_store owns the lifecycle intent, but * the agent owns the allocation and frees it on wf_agent_free. */ wf_mod_label *persisted_labels; size_t persisted_label_count;#endif} wf_agent;
static char *wf_agent_strdup(const char *s) { if (!s) { return NULL; }
size_t len = strlen(s) + 1; char *dup = malloc(len); if (dup) { memcpy(dup, s, len); } return dup;}
static char *wf_agent_strndup(const char *s, size_t len) { char *dup = malloc(len + 1); if (!dup) { return NULL; }
memcpy(dup, s, len); dup[len] = '\0'; return dup;}
static void wf_agent_post_result_reset(wf_agent_post_result *result) { if (!result) { return; }
free(result->uri); free(result->cid); memset(result, 0, sizeof(*result));}
static void wf_agent_profile_reset(wf_agent_profile *profile) { if (!profile) { return; }
free(profile->did); free(profile->handle); free(profile->display_name); free(profile->description); free(profile->avatar_cid); free(profile->following); free(profile->blocking); memset(profile, 0, sizeof(*profile));}
static voidwf_agent_server_description_reset(wf_agent_server_description *desc) { if (!desc) { return; }
free(desc->did); for (size_t i = 0; i < desc->available_user_domain_count; ++i) { free(desc->available_user_domains[i]); } free(desc->available_user_domains); free(desc->privacy_policy); free(desc->terms_of_service); free(desc->contact_email); memset(desc, 0, sizeof(*desc)); desc->invite_code_required = -1; desc->phone_verification_required = -1;}
static void wf_agent_app_password_reset(wf_agent_app_password *pwd) { if (!pwd) { return; }
free(pwd->name); free(pwd->created_at); memset(pwd, 0, sizeof(*pwd)); pwd->privileged = -1;}
static void wf_agent_app_password_list_reset(wf_agent_app_password_list *list) { if (!list) { return; }
for (size_t i = 0; i < list->password_count; ++i) { wf_agent_app_password_reset(&list->passwords[i]); } free(list->passwords); memset(list, 0, sizeof(*list));}
static void wf_agent_session_data_reset(wf_session_data *data) { if (!data) { return; }
free(data->access_jwt); free(data->refresh_jwt); free(data->handle); free(data->did); free(data->email); free(data->status); free(data->pds_url); memset(data, 0, sizeof(*data)); data->email_confirmed = -1; data->email_auth_factor = -1; data->active = -1;}
static wf_status wf_agent_set_string(char **dst, const char *src);static int wf_agent_is_logged_in(const wf_agent *agent);
static wf_status wf_agent_session_data_copy(wf_session_data *dst, const wf_session_data *src) { if (!dst || !src) { return WF_ERR_INVALID_ARG; }
wf_agent_session_data_reset(dst);
wf_status status = wf_agent_set_string(&dst->access_jwt, src->access_jwt); if (status == WF_OK) { status = wf_agent_set_string(&dst->refresh_jwt, src->refresh_jwt); } if (status == WF_OK) { status = wf_agent_set_string(&dst->handle, src->handle); } if (status == WF_OK) { status = wf_agent_set_string(&dst->did, src->did); } if (status == WF_OK) { status = wf_agent_set_string(&dst->email, src->email); } if (status == WF_OK) { status = wf_agent_set_string(&dst->status, src->status); } if (status == WF_OK) { status = wf_agent_set_string(&dst->pds_url, src->pds_url); }
if (status == WF_OK) { dst->email_confirmed = src->email_confirmed; dst->email_auth_factor = src->email_auth_factor; dst->active = src->active; } else { wf_agent_session_data_reset(dst); }
return status;}
static void wf_agent_sync_auth(wf_agent *agent) { if (!agent || !agent->client || !agent->session) { return; }
wf_xrpc_client_set_auth(agent->client, wf_agent_is_logged_in(agent) ? agent->session->data.access_jwt : NULL);}
/* Route the agent's PDS client at the session's discovered PDS endpoint (from * the DID document), so data-plane XRPC calls hit the account's real PDS rather * than the login/entryway host. No-op when no PDS was discovered. */static void wf_agent_apply_session_pds(wf_agent *agent) { if (!agent || !agent->client || !agent->session) { return; } const char *pds = agent->session->data.pds_url; if (pds && pds[0]) { wf_xrpc_client_set_base_url(agent->client, pds); }}
/* Transport refresh hook: on an expired/invalid access token the transport * calls this to refresh the session and re-install the new access JWT (and any * re-pointed PDS) before retrying the original request exactly once. */static wf_status wf_agent_refresh_cb(void *userdata) { wf_agent *agent = userdata; if (!agent || !agent->session || !wf_session_has_session(agent->session)) { return WF_ERR_STATE; }
wf_status status = wf_session_refresh(agent->session); if (status == WF_OK) { wf_xrpc_client_set_auth(agent->client, agent->session->data.access_jwt); wf_agent_apply_session_pds(agent); } return status;}
static int wf_agent_make_rfc3339_timestamp(char *buf, size_t buf_len) { time_t now = time(NULL); struct tm tm_utc; struct tm *tmp = gmtime(&now); if (!tmp) { return 0; }
tm_utc = *tmp; return strftime(buf, buf_len, "%Y-%m-%dT%H:%M:%SZ", &tm_utc) != 0;}
static int wf_agent_is_logged_in(const wf_agent *agent) { return agent && agent->session && wf_session_has_session(agent->session) && agent->session->data.did && agent->session->data.access_jwt;}
static int wf_agent_authority_matches_session(const wf_agent *agent, const char *authority) { if (!agent || !agent->session || !authority) { return 0; }
const char *did = agent->session->data.did; const char *handle = agent->session->data.handle;
if (did && strcmp(authority, did) == 0) { return 1; } if (handle && strcmp(authority, handle) == 0) { return 1; } return 0;}
static wf_status wf_agent_set_string(char **dst, const char *src) { char *copy = wf_agent_strdup(src); if (src && !copy) { return WF_ERR_ALLOC; }
free(*dst); *dst = copy; return WF_OK;}
static wf_status wf_agent_add_feature_json(cJSON *features, const wf_richtext_segment *segment, const wf_richtext_feature *feature, wf_xrpc_client *client) { if (!features || !segment || !feature || !client) { return WF_ERR_INVALID_ARG; }
cJSON *feature_json = cJSON_CreateObject(); if (!feature_json) { return WF_ERR_ALLOC; }
wf_status status = WF_OK;
switch (feature->type) { case WF_RICHTEXT_FEATURE_MENTION: { if (!segment->text || segment->text_len < 2 || segment->text[0] != '@') { status = WF_ERR_PARSE; break; }
char *handle = wf_agent_strndup(segment->text + 1, segment->text_len - 1); if (!handle) { status = WF_ERR_ALLOC; break; }
if (!wf_syntax_handle_is_valid(handle)) { free(handle); status = WF_ERR_PARSE; break; }
char *did = NULL; status = wf_handle_resolve(client, handle, &did); free(handle); if (status != WF_OK) { break; }
if (!wf_syntax_did_is_valid(did)) { free(did); status = WF_ERR_PARSE; break; }
if (!cJSON_AddStringToObject(feature_json, "$type", WF_AGENT_FACET_MENTION_TYPE) || !cJSON_AddStringToObject(feature_json, "did", did)) { free(did); status = WF_ERR_ALLOC; break; }
free(did); break; }
case WF_RICHTEXT_FEATURE_LINK: if (!cJSON_AddStringToObject(feature_json, "$type", WF_AGENT_FACET_LINK_TYPE) || !cJSON_AddStringToObject(feature_json, "uri", feature->uri)) { status = WF_ERR_ALLOC; } break;
case WF_RICHTEXT_FEATURE_TAG: if (!cJSON_AddStringToObject(feature_json, "$type", WF_AGENT_FACET_TAG_TYPE) || !cJSON_AddStringToObject(feature_json, "tag", feature->tag)) { status = WF_ERR_ALLOC; } break;
default: status = WF_ERR_INVALID_ARG; break; }
if (status != WF_OK) { cJSON_Delete(feature_json); return status; }
if (!cJSON_AddItemToArray(features, feature_json)) { cJSON_Delete(feature_json); return WF_ERR_ALLOC; }
return WF_OK;}
static wf_status wf_agent_add_segment_facet_json( cJSON *facets, const wf_richtext_segment *segment, wf_xrpc_client *client) { if (!facets || !segment || !segment->facet || !segment->facet->features || segment->facet->feature_count == 0) { return WF_ERR_INVALID_ARG; }
cJSON *facet_json = cJSON_CreateObject(); if (!facet_json) { return WF_ERR_ALLOC; }
cJSON *index_json = cJSON_CreateObject(); if (!index_json) { cJSON_Delete(facet_json); return WF_ERR_ALLOC; }
if (!cJSON_AddNumberToObject(index_json, "byteStart", (double)segment->facet->byte_start) || !cJSON_AddNumberToObject(index_json, "byteEnd", (double)segment->facet->byte_end) || !cJSON_AddItemToObject(facet_json, "index", index_json)) { cJSON_Delete(index_json); cJSON_Delete(facet_json); return WF_ERR_ALLOC; }
cJSON *features_json = cJSON_CreateArray(); if (!features_json) { cJSON_Delete(facet_json); return WF_ERR_ALLOC; }
if (!cJSON_AddItemToObject(facet_json, "features", features_json)) { cJSON_Delete(features_json); cJSON_Delete(facet_json); return WF_ERR_ALLOC; }
for (size_t i = 0; i < segment->facet->feature_count; ++i) { wf_status status = wf_agent_add_feature_json( features_json, segment, &segment->facet->features[i], client); if (status != WF_OK) { cJSON_Delete(facet_json); return status; } }
if (!cJSON_AddItemToArray(facets, facet_json)) { cJSON_Delete(facet_json); return WF_ERR_ALLOC; }
return WF_OK;}
static wf_status wf_agent_build_post_record(wf_agent *agent, const char *text, cJSON *facets, cJSON **out_record) { if (!agent || !text || !out_record || !agent->session || !agent->session->data.did) { cJSON_Delete(facets); return WF_ERR_INVALID_ARG; }
if (!wf_syntax_did_is_valid(agent->session->data.did)) { cJSON_Delete(facets); return WF_ERR_PARSE; }
char created_at[32]; if (!wf_agent_make_rfc3339_timestamp(created_at, sizeof(created_at)) || !wf_syntax_datetime_is_valid(created_at)) { cJSON_Delete(facets); return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) { cJSON_Delete(facets); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(record, "$type", WF_AGENT_POST_RECORD_TYPE) || !cJSON_AddStringToObject(record, "text", text) || !cJSON_AddStringToObject(record, "createdAt", created_at)) { cJSON_Delete(facets); cJSON_Delete(record); return WF_ERR_ALLOC; }
if (facets) { if (!cJSON_AddItemToObject(record, "facets", facets)) { cJSON_Delete(facets); cJSON_Delete(record); return WF_ERR_ALLOC; } }
*out_record = record; return WF_OK;}
static wf_status wf_agent_create_record_call(wf_agent *agent, const char *collection, cJSON *record, wf_agent_post_result *out) { if (!wf_agent_is_logged_in(agent) || !collection || !record || !out) { cJSON_Delete(record); return WF_ERR_INVALID_ARG; }
if (wf_syntax_nsid_validate(collection) != WF_OK) { cJSON_Delete(record); return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did) || !cJSON_AddStringToObject(root, "collection", collection)) { cJSON_Delete(record); cJSON_Delete(root); return WF_ERR_ALLOC; }
if (!cJSON_AddItemToObject(root, "record", record)) { cJSON_Delete(record); cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) { return WF_ERR_ALLOC; }
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_procedure( agent->client, WF_AGENT_CREATE_RECORD_NSID, json, &res); free(json); if (status != WF_OK) { wf_response_free(&res); return status; }
wf_agent_post_result_reset(out);
cJSON *resp_root = cJSON_ParseWithLength(res.body, res.body_len); if (!resp_root) { wf_response_free(&res); return WF_ERR_PARSE; }
cJSON *uri = cJSON_GetObjectItemCaseSensitive(resp_root, "uri"); cJSON *cid = cJSON_GetObjectItemCaseSensitive(resp_root, "cid"); if (!cJSON_IsString(uri) || !cJSON_IsString(cid) || !uri->valuestring || !cid->valuestring) { cJSON_Delete(resp_root); wf_response_free(&res); return WF_ERR_PARSE; }
status = wf_agent_set_string(&out->uri, uri->valuestring); if (status == WF_OK) { status = wf_agent_set_string(&out->cid, cid->valuestring); } if (status != WF_OK) { wf_agent_post_result_reset(out); }
cJSON_Delete(resp_root); wf_response_free(&res); return status;}
static wf_status wf_agent_delete_record_call(wf_agent *agent, const char *collection, const char *rkey) { if (!wf_agent_is_logged_in(agent) || !collection || !rkey) { return WF_ERR_INVALID_ARG; }
if (wf_syntax_nsid_validate(collection) != WF_OK || !wf_syntax_record_key_is_valid(rkey)) { return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) { return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did) || !cJSON_AddStringToObject(root, "collection", collection) || !cJSON_AddStringToObject(root, "rkey", rkey)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) { return WF_ERR_ALLOC; }
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_procedure( agent->client, WF_AGENT_DELETE_RECORD_NSID, json, &res); free(json); wf_response_free(&res); return status;}
static wf_status wf_agent_profile_from_response(const wf_response *res, wf_agent_profile *out) { if (!res || !out) { return WF_ERR_INVALID_ARG; }
wf_agent_profile_reset(out);
cJSON *root = cJSON_ParseWithLength(res->body, res->body_len); if (!root) { return WF_ERR_PARSE; }
cJSON *did = cJSON_GetObjectItemCaseSensitive(root, "did"); cJSON *handle = cJSON_GetObjectItemCaseSensitive(root, "handle"); if (!cJSON_IsString(did) || !cJSON_IsString(handle) || !did->valuestring || !handle->valuestring || !wf_syntax_did_is_valid(did->valuestring) || !wf_syntax_handle_is_valid(handle->valuestring)) { cJSON_Delete(root); return WF_ERR_PARSE; }
wf_status status = wf_agent_set_string(&out->did, did->valuestring); if (status == WF_OK) { status = wf_agent_set_string(&out->handle, handle->valuestring); }
cJSON *display_name = cJSON_GetObjectItemCaseSensitive(root, "displayName"); if (status == WF_OK && cJSON_IsString(display_name) && display_name->valuestring) { status = wf_agent_set_string(&out->display_name, display_name->valuestring); }
cJSON *description = cJSON_GetObjectItemCaseSensitive(root, "description"); if (status == WF_OK && cJSON_IsString(description) && description->valuestring) { status = wf_agent_set_string(&out->description, description->valuestring); }
cJSON *avatar = cJSON_GetObjectItemCaseSensitive(root, "avatar"); if (status == WF_OK && cJSON_IsString(avatar) && avatar->valuestring) { /* The profile view exposes an avatar URL, not a CID; keep the * field populated with the server value for convenience. */ status = wf_agent_set_string(&out->avatar_cid, avatar->valuestring); }
cJSON *viewer = cJSON_GetObjectItemCaseSensitive(root, "viewer"); if (status == WF_OK && cJSON_IsObject(viewer)) { cJSON *following = cJSON_GetObjectItemCaseSensitive(viewer, "following"); if (cJSON_IsString(following) && following->valuestring) { status = wf_agent_set_string(&out->following, following->valuestring); }
cJSON *blocking = cJSON_GetObjectItemCaseSensitive(viewer, "blocking"); if (status == WF_OK && cJSON_IsString(blocking) && blocking->valuestring) { status = wf_agent_set_string(&out->blocking, blocking->valuestring); }
cJSON *muted = cJSON_GetObjectItemCaseSensitive(viewer, "muted"); if (cJSON_IsBool(muted)) { out->muted = cJSON_IsTrue(muted); } }
cJSON *followers_count = cJSON_GetObjectItemCaseSensitive(root, "followersCount"); cJSON *follows_count = cJSON_GetObjectItemCaseSensitive(root, "followsCount"); cJSON *posts_count = cJSON_GetObjectItemCaseSensitive(root, "postsCount");
if (status == WF_OK && cJSON_IsNumber(followers_count)) { double value = followers_count->valuedouble; if (value > (double)INT_MAX) { out->followers_count = INT_MAX; } else if (value < (double)INT_MIN) { out->followers_count = INT_MIN; } else { out->followers_count = (int)value; } } if (status == WF_OK && cJSON_IsNumber(follows_count)) { double value = follows_count->valuedouble; if (value > (double)INT_MAX) { out->follows_count = INT_MAX; } else if (value < (double)INT_MIN) { out->follows_count = INT_MIN; } else { out->follows_count = (int)value; } } if (status == WF_OK && cJSON_IsNumber(posts_count)) { double value = posts_count->valuedouble; if (value > (double)INT_MAX) { out->posts_count = INT_MAX; } else if (value < (double)INT_MIN) { out->posts_count = INT_MIN; } else { out->posts_count = (int)value; } }
if (status != WF_OK) { wf_agent_profile_reset(out); }
cJSON_Delete(root); return status;}
wf_status wf_agent_parse_profile(const char *json, size_t json_len, wf_agent_profile *out) { wf_response response = {0}; if (!json || !json_len || !out) return WF_ERR_INVALID_ARG; response.body = (char *)json; response.body_len = json_len; return wf_agent_profile_from_response(&response, out);}
static wf_status wf_agent_build_follow_record(wf_agent *agent, const char *subject_did, cJSON **out_record) { if (!wf_agent_is_logged_in(agent) || !subject_did || !out_record) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_did_is_valid(subject_did)) { return WF_ERR_INVALID_ARG; }
char created_at[32]; if (!wf_agent_make_rfc3339_timestamp(created_at, sizeof(created_at)) || !wf_syntax_datetime_is_valid(created_at)) { return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) { return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(record, "$type", WF_AGENT_FOLLOW_RECORD_TYPE) || !cJSON_AddStringToObject(record, "subject", subject_did) || !cJSON_AddStringToObject(record, "createdAt", created_at)) { cJSON_Delete(record); return WF_ERR_ALLOC; }
*out_record = record; return WF_OK;}
static wf_status wf_agent_build_like_record(wf_agent *agent, const char *post_uri, const char *post_cid, cJSON **out_record) { if (!wf_agent_is_logged_in(agent) || !post_uri || !post_cid || !*post_cid || !out_record) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi wf_uri = {0}; if (!wf_syntax_aturi_parse(post_uri, &wf_uri)) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi_free(&wf_uri);
char created_at[32]; if (!wf_agent_make_rfc3339_timestamp(created_at, sizeof(created_at)) || !wf_syntax_datetime_is_valid(created_at)) { return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) { return WF_ERR_ALLOC; }
cJSON *subject = cJSON_CreateObject(); if (!subject) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(subject, "uri", post_uri) || !cJSON_AddStringToObject(subject, "cid", post_cid) || !cJSON_AddStringToObject(record, "$type", WF_AGENT_LIKE_RECORD_TYPE) || !cJSON_AddItemToObject(record, "subject", subject) || !cJSON_AddStringToObject(record, "createdAt", created_at)) { cJSON_Delete(subject); cJSON_Delete(record); return WF_ERR_ALLOC; }
*out_record = record; return WF_OK;}
static wf_status wf_agent_build_repost_record(wf_agent *agent, const char *post_uri, const char *post_cid, cJSON **out_record) { if (!wf_agent_is_logged_in(agent) || !post_uri || !post_cid || !*post_cid || !out_record) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi wf_uri = {0}; if (!wf_syntax_aturi_parse(post_uri, &wf_uri)) { return WF_ERR_INVALID_ARG; } wf_syntax_aturi_free(&wf_uri);
char created_at[32]; if (!wf_agent_make_rfc3339_timestamp(created_at, sizeof(created_at)) || !wf_syntax_datetime_is_valid(created_at)) { return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) { return WF_ERR_ALLOC; }
cJSON *subject = cJSON_CreateObject(); if (!subject) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(subject, "uri", post_uri) || !cJSON_AddStringToObject(subject, "cid", post_cid) || !cJSON_AddStringToObject(record, "$type", WF_AGENT_REPOST_RECORD_TYPE) || !cJSON_AddItemToObject(record, "subject", subject) || !cJSON_AddStringToObject(record, "createdAt", created_at)) { cJSON_Delete(subject); cJSON_Delete(record); return WF_ERR_ALLOC; }
*out_record = record; return WF_OK;}
static wf_status wf_agent_put_record_call(wf_agent *agent, const char *collection, const char *rkey, cJSON *record, wf_agent_post_result *out) { if (!wf_agent_is_logged_in(agent) || !collection || !rkey || !record || !out) { cJSON_Delete(record); return WF_ERR_INVALID_ARG; }
if (wf_syntax_nsid_validate(collection) != WF_OK || !wf_syntax_record_key_is_valid(rkey)) { cJSON_Delete(record); return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did) || !cJSON_AddStringToObject(root, "collection", collection) || !cJSON_AddStringToObject(root, "rkey", rkey) || !cJSON_AddItemToObject(root, "record", record)) { cJSON_Delete(record); cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) { return WF_ERR_ALLOC; }
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_procedure(agent->client, WF_AGENT_PUT_RECORD_NSID, json, &res); free(json); if (status != WF_OK) { wf_response_free(&res); return status; }
wf_agent_post_result_reset(out);
cJSON *resp_root = cJSON_ParseWithLength(res.body, res.body_len); if (!resp_root) { wf_response_free(&res); return WF_ERR_PARSE; }
cJSON *uri = cJSON_GetObjectItemCaseSensitive(resp_root, "uri"); cJSON *cid = cJSON_GetObjectItemCaseSensitive(resp_root, "cid"); if (!cJSON_IsString(uri) || !cJSON_IsString(cid) || !uri->valuestring || !cid->valuestring) { cJSON_Delete(resp_root); wf_response_free(&res); return WF_ERR_PARSE; }
status = wf_agent_set_string(&out->uri, uri->valuestring); if (status == WF_OK) { status = wf_agent_set_string(&out->cid, cid->valuestring); } if (status != WF_OK) { wf_agent_post_result_reset(out); }
cJSON_Delete(resp_root); wf_response_free(&res); return status;}
static int wf_agent_int_to_str(int value, char *buf, size_t buf_len) { return snprintf(buf, buf_len, "%d", value) > 0;}
wf_agent *wf_agent_new(const char *service_url) { if (!service_url) { return NULL; }
wf_agent *agent = calloc(1, sizeof(*agent)); if (!agent) { return NULL; }
agent->service_url = wf_agent_strdup(service_url); if (!agent->service_url) { free(agent); return NULL; }
agent->client = wf_xrpc_client_new(service_url); if (!agent->client) { free(agent->service_url); free(agent); return NULL; }
agent->chat_client = NULL;
agent->session = wf_session_new(service_url); if (!agent->session) { wf_xrpc_client_free(agent->client); free(agent->service_url); free(agent); return NULL; }
/* Enable one transparent refresh+retry on the data-plane client when an * access token expires mid-request. */ wf_xrpc_client_set_refresh_handler(agent->client, wf_agent_refresh_cb, agent);
return agent;}
void wf_agent_apply_tls(wf_agent *agent, wf_xrpc_client *client) { if (!agent || !client) { return; } if (agent->ca_bundle) { wf_xrpc_client_set_ca_bundle(client, agent->ca_bundle); } if (agent->tls_rng) { /* Deliberately unchecked: an application that asked for its own RNG * has already been told by wf_agent_set_tls_rng whether this build can * honour it, and failing a chat-service call here would be a confusing * place to report it a second time. */ (void)wf_xrpc_client_set_tls_rng(client, agent->tls_rng, agent->tls_rng_userdata); }}
wf_status wf_agent_set_ca_bundle(wf_agent *agent, const char *path) { if (!agent) { return WF_ERR_INVALID_ARG; }
char *copy = NULL; if (path) { copy = wf_agent_strdup(path); if (!copy) { return WF_ERR_ALLOC; } }
free(agent->ca_bundle); agent->ca_bundle = copy;
/* Every client that already exists. The chat client picks it up when it is * created, via wf_agent_apply_tls. */ wf_xrpc_client_set_ca_bundle(agent->client, path); if (agent->session) { wf_xrpc_client_set_ca_bundle(agent->session->client, path); } if (agent->chat_client) { wf_xrpc_client_set_ca_bundle(agent->chat_client, path); } return WF_OK;}
wf_status wf_agent_set_tls_rng(wf_agent *agent, wf_tls_rng_fn fn, void *userdata) { if (!agent) { return WF_ERR_INVALID_ARG; }
wf_status status = wf_xrpc_client_set_tls_rng(agent->client, fn, userdata); if (status != WF_OK) { /* Report the first refusal and install nothing, so the agent never * ends up with some clients using the application RNG and others not. */ return status; }
agent->tls_rng = fn; agent->tls_rng_userdata = userdata;
if (agent->session) { (void)wf_xrpc_client_set_tls_rng(agent->session->client, fn, userdata); } if (agent->chat_client) { (void)wf_xrpc_client_set_tls_rng(agent->chat_client, fn, userdata); } return WF_OK;}
void wf_agent_free(wf_agent *agent) { if (!agent) { return; }
wf_session_free(agent->session); wf_xrpc_client_free(agent->client); wf_xrpc_client_free(agent->chat_client); free(agent->service_url); free(agent->ca_bundle); free(agent->mirror_did); free(agent->mirror_signing_key); wf_car_free(&agent->mirror);#ifdef WOLFRAM_BUILD_STORE if (agent->persisted_labels) { wf_mod_labels_free(agent->persisted_labels, agent->persisted_label_count); }#endif free(agent);}
wf_status wf_agent_login(wf_agent *agent, const char *identifier, const char *password) { if (!agent || !agent->session || !agent->client || !identifier || !password) { return WF_ERR_INVALID_ARG; }
wf_xrpc_client_set_auth(agent->client, NULL);
wf_status status = wf_session_login(agent->session, identifier, password); if (status != WF_OK) { return status; }
wf_agent_sync_auth(agent); wf_agent_apply_session_pds(agent); return WF_OK;}
wf_status wf_agent_resume(wf_agent *agent, const wf_session_data *data) { if (!agent || !agent->session || !agent->client || !data) { return WF_ERR_INVALID_ARG; }
wf_status status = wf_session_resume(agent->session, data); wf_agent_sync_auth(agent); wf_agent_apply_session_pds(agent); return status;}
wf_status wf_agent_get_session(wf_agent *agent) { if (!agent || !agent->session || !agent->client) { return WF_ERR_INVALID_ARG; }
wf_status status = wf_session_get(agent->session); wf_agent_sync_auth(agent); return status;}
wf_status wf_agent_logout(wf_agent *agent) { if (!agent || !agent->session || !agent->client) { return WF_ERR_INVALID_ARG; }
wf_status status = wf_session_delete(agent->session); wf_agent_sync_auth(agent); return status;}
wf_status wf_agent_get_session_data(wf_agent *agent, wf_session_data *out) { if (!agent || !out || !wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
return wf_agent_session_data_copy(out, &agent->session->data);}
void wf_agent_session_data_free(wf_session_data *data) { wf_agent_session_data_reset(data);}
const char *wf_agent_get_did(wf_agent *agent) { if (!wf_agent_is_logged_in(agent)) { return NULL; }
return agent->session->data.did;}
const char *wf_agent_get_handle(wf_agent *agent) { if (!wf_agent_is_logged_in(agent)) { return NULL; }
return agent->session->data.handle;}
/* Return the XRPC error message from the agent's most recent request, or NULL * if the last request succeeded or carried no error envelope. */const char *wf_agent_last_error(const wf_agent *agent) { if (!agent || !agent->client) return NULL; return wf_xrpc_last_error(agent->client);}
void wf_agent_post_result_free(wf_agent_post_result *result) { wf_agent_post_result_reset(result);}
void wf_agent_profile_free(wf_agent_profile *profile) { wf_agent_profile_reset(profile);}
/* ── getRecord ──────────────────────────────────────────────────────── */
wf_status wf_agent_get_record(wf_agent *agent, const char *collection, const char *rkey, wf_response *out) { if (!agent || !collection || !rkey || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
if (wf_syntax_nsid_validate(collection) != WF_OK || !wf_syntax_record_key_is_valid(rkey)) { return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC;
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did) || !cJSON_AddStringToObject(root, "collection", collection) || !cJSON_AddStringToObject(root, "rkey", rkey)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.repo.getRecord", json, out); free(json); return status;}
/* ── putRecord ──────────────────────────────────────────────────────── */
wf_status wf_agent_put_record(wf_agent *agent, const char *collection, const char *rkey, const char *record_json, wf_agent_post_result *out) { if (!agent || !collection || !rkey || !record_json || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
if (wf_syntax_nsid_validate(collection) != WF_OK || !wf_syntax_record_key_is_valid(rkey)) { return WF_ERR_INVALID_ARG; }
cJSON *value = cJSON_Parse(record_json); if (!value) return WF_ERR_PARSE; if (!cJSON_IsObject(value)) { cJSON_Delete(value); return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) { cJSON_Delete(value); return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did) || !cJSON_AddStringToObject(root, "collection", collection) || !cJSON_AddStringToObject(root, "rkey", rkey) || !cJSON_AddItemToObject(root, "record", value)) { cJSON_Delete(value); cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.repo.putRecord", json, &res); free(json); if (status != WF_OK) { wf_response_free(&res); return status; }
wf_agent_post_result_reset(out);
cJSON *resp_root = cJSON_ParseWithLength(res.body, res.body_len); if (!resp_root) { wf_response_free(&res); return WF_ERR_PARSE; }
cJSON *uri = cJSON_GetObjectItemCaseSensitive(resp_root, "uri"); cJSON *cid = cJSON_GetObjectItemCaseSensitive(resp_root, "cid"); if (!cJSON_IsString(uri) || !cJSON_IsString(cid) || !uri->valuestring || !cid->valuestring) { cJSON_Delete(resp_root); wf_response_free(&res); return WF_ERR_PARSE; }
status = wf_agent_set_string(&out->uri, uri->valuestring); if (status == WF_OK) { status = wf_agent_set_string(&out->cid, cid->valuestring); } if (status != WF_OK) { wf_agent_post_result_reset(out); }
cJSON_Delete(resp_root); wf_response_free(&res); return status;}
/* ── listRecords ───────────────────────────────────────────────────── */
wf_status wf_agent_list_records(wf_agent *agent, const char *collection, int limit, const char *cursor, wf_response *out) { if (!agent || !collection || !collection[0] || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; } if (wf_syntax_nsid_validate(collection) != WF_OK) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[4]; size_t param_count = 0; char limit_buf[16];
if (!agent->session->data.did) return WF_ERR_INVALID_ARG; params[param_count].name = "repo"; params[param_count].value = agent->session->data.did; param_count++;
params[param_count].name = "collection"; params[param_count].value = collection; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "com.atproto.repo.listRecords", params, param_count, out);}
#if 0 // post functions moved to post.cwf_status wf_agent_post_with_facets(wf_agent *agent, const char *text, const char *facets_json, wf_agent_post_result *out) { if (!agent || !text || !out) { return WF_ERR_INVALID_ARG; }
cJSON *facets = NULL; if (facets_json && facets_json[0] != '\0') { facets = cJSON_Parse(facets_json); if (!facets) { return WF_ERR_PARSE; } if (!cJSON_IsArray(facets)) { cJSON_Delete(facets); return WF_ERR_PARSE; } }
cJSON *record = NULL; wf_status status = wf_agent_build_post_record(agent, text, facets, &record); if (status != WF_OK) { return status; }
return wf_agent_create_record_call(agent, WF_AGENT_POST_COLLECTION, record, out);}
wf_status wf_agent_post(wf_agent *agent, const char *text, wf_agent_post_result *out) { if (!agent || !text || !out) { return WF_ERR_INVALID_ARG; }
wf_richtext rt = {0}; wf_status status = wf_richtext_init(&rt, text); if (status != WF_OK) { return status; }
status = wf_richtext_detect_facets(&rt); if (status != WF_OK) { wf_richtext_free(&rt); return status; }
cJSON *facets = NULL; size_t segment_count = wf_richtext_segment_count(&rt); for (size_t i = 0; i < segment_count; ++i) { wf_richtext_segment segment = wf_richtext_get_segment(&rt, i); if (!segment.facet || !segment.facet->features || segment.facet->feature_count == 0) { continue; }
if (!facets) { facets = cJSON_CreateArray(); if (!facets) { wf_richtext_free(&rt); return WF_ERR_ALLOC; } }
status = wf_agent_add_segment_facet_json(facets, &segment, agent->client); if (status != WF_OK) { cJSON_Delete(facets); wf_richtext_free(&rt); return status; } }
wf_richtext_free(&rt);
cJSON *record = NULL; status = wf_agent_build_post_record(agent, text, facets, &record); if (status != WF_OK) { return status; }
return wf_agent_create_record_call(agent, WF_AGENT_POST_COLLECTION, record, out);}
wf_status wf_agent_delete_post(wf_agent *agent, const char *uri) { if (!agent || !uri) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) { return WF_ERR_PARSE; }
wf_status status = WF_OK; if (!parsed.authority || !wf_agent_authority_matches_session(agent, parsed.authority) || !parsed.collection || !parsed.record_key || strcmp(parsed.collection, WF_AGENT_POST_COLLECTION) != 0) { status = WF_ERR_INVALID_ARG; goto done; }
status = wf_agent_delete_record_call(agent, parsed.collection, parsed.record_key);
done: wf_syntax_aturi_free(&parsed); return status;}
#endif // post functions moved to post.c
wf_status wf_agent_get_profile(wf_agent *agent, const char *actor, wf_agent_profile *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[] = { {"actor", actor}, };
wf_response res = {0}; wf_status status = wf_xrpc_query_params( agent->client, "app.bsky.actor.getProfile", params, 1, &res); if (status != WF_OK) { wf_response_free(&res); return status; }
status = wf_agent_profile_from_response(&res, out); wf_response_free(&res); return status;}
wf_status wf_agent_get_profile_raw(wf_agent *agent, const char *actor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[] = { {"actor", actor}, };
return wf_xrpc_query_params(agent->client, "app.bsky.actor.getProfile", params, 1, out);}
wf_status wf_agent_get_preferences(wf_agent *agent, char **out_json) { if (!agent || !out_json) { return WF_ERR_INVALID_ARG; }
*out_json = NULL;
wf_response res = {0}; wf_status status = wf_xrpc_query_params( agent->client, "app.bsky.actor.getPreferences", NULL, 0, &res); if (status != WF_OK) { wf_response_free(&res); return status; }
wf_status ret = WF_ERR_PARSE; cJSON *root = cJSON_ParseWithLength(res.body, res.body_len); if (root) { cJSON *prefs = cJSON_GetObjectItemCaseSensitive(root, "preferences"); if (cJSON_IsArray(prefs)) { char *json = cJSON_PrintUnformatted(prefs); if (json) { *out_json = json; ret = WF_OK; } else { ret = WF_ERR_ALLOC; } } cJSON_Delete(root); }
wf_response_free(&res); return ret;}
wf_status wf_agent_get_preferences_typed( wf_agent *agent, wf_lex_app_bsky_actor_get_preferences_main_output **out) { if (!agent || !out) return WF_ERR_INVALID_ARG; *out = NULL;
wf_lex_app_bsky_actor_get_preferences_main_params params = {0}; wf_response res = {0}; wf_agent_sync_auth(agent); wf_status status = wf_lex_app_bsky_actor_get_preferences_main_call( agent->client, ¶ms, &res); if (status != WF_OK) { wf_response_free(&res); return status; }
wf_lex_app_bsky_actor_get_preferences_main_output *output = NULL; status = wf_lex_app_bsky_actor_get_preferences_main_output_decode_json( res.body, res.body_len, &output); wf_response_free(&res); if (status == WF_OK) { *out = output; } return status;}
wf_status wf_agent_put_preferences_json( wf_agent *agent, const wf_lex_app_bsky_actor_put_preferences_main_input *input, wf_response *out) { if (!agent || !input || !out) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_lex_app_bsky_actor_put_preferences_main_call(agent->client, input, out);}
#if 0 // social functions moved to post.c
wf_status wf_agent_follow(wf_agent *agent, const char *subject_did, wf_agent_post_result *out) { if (!agent || !subject_did || !out) { return WF_ERR_INVALID_ARG; }
cJSON *record = NULL; wf_status status = wf_agent_build_follow_record(agent, subject_did, &record); if (status != WF_OK) { return status; }
return wf_agent_create_record_call(agent, WF_AGENT_FOLLOW_COLLECTION, record, out);}
wf_status wf_agent_unfollow(wf_agent *agent, const char *follow_uri) { if (!agent || !follow_uri) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(follow_uri, &parsed)) { return WF_ERR_PARSE; }
wf_status status = WF_OK; if (!parsed.authority || !wf_agent_authority_matches_session(agent, parsed.authority) || !parsed.collection || !parsed.record_key || strcmp(parsed.collection, WF_AGENT_FOLLOW_COLLECTION) != 0) { status = WF_ERR_INVALID_ARG; goto done; }
status = wf_agent_delete_record_call(agent, parsed.collection, parsed.record_key);
done: wf_syntax_aturi_free(&parsed); return status;}
wf_status wf_agent_like(wf_agent *agent, const char *post_uri, const char *post_cid, wf_agent_post_result *out) { if (!agent || !post_uri || !post_cid || !out) { return WF_ERR_INVALID_ARG; }
cJSON *record = NULL; wf_status status = wf_agent_build_like_record(agent, post_uri, post_cid, &record); if (status != WF_OK) { return status; }
return wf_agent_create_record_call(agent, WF_AGENT_LIKE_COLLECTION, record, out);}
wf_status wf_agent_unlike(wf_agent *agent, const char *like_uri) { if (!agent || !like_uri) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(like_uri, &parsed)) { return WF_ERR_PARSE; }
wf_status status = WF_OK; if (!parsed.authority || !wf_agent_authority_matches_session(agent, parsed.authority) || !parsed.collection || !parsed.record_key || strcmp(parsed.collection, WF_AGENT_LIKE_COLLECTION) != 0) { status = WF_ERR_INVALID_ARG; goto done; }
status = wf_agent_delete_record_call(agent, parsed.collection, parsed.record_key);
done: wf_syntax_aturi_free(&parsed); return status;}
/* ── mute / unmute ─────────────────────────────────────────────────── */
wf_status wf_agent_mute(wf_agent *agent, const char *actor) { if (!agent || !actor || !actor[0]) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(root, "actor", actor)) { cJSON_Delete(root); return WF_ERR_ALLOC; } char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_response res = {0}; wf_status status = wf_xrpc_procedure(agent->client, "app.bsky.graph.muteActor", json, &res); free(json); wf_response_free(&res); return status;}
wf_status wf_agent_unmute(wf_agent *agent, const char *actor) { if (!agent || !actor || !actor[0]) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(root, "actor", actor)) { cJSON_Delete(root); return WF_ERR_ALLOC; } char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_response res = {0}; wf_status status = wf_xrpc_procedure(agent->client, "app.bsky.graph.unmuteActor", json, &res); free(json); wf_response_free(&res); return status;}
wf_status wf_agent_repost(wf_agent *agent, const char *post_uri, const char *post_cid, wf_agent_post_result *out) { if (!agent || !post_uri || !post_cid || !out) { return WF_ERR_INVALID_ARG; }
cJSON *record = NULL; wf_status status = wf_agent_build_repost_record(agent, post_uri, post_cid, &record); if (status != WF_OK) { return status; }
return wf_agent_create_record_call(agent, WF_AGENT_REPOST_COLLECTION, record, out);}
wf_status wf_agent_delete_repost(wf_agent *agent, const char *repost_uri) { if (!agent || !repost_uri) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(repost_uri, &parsed)) { return WF_ERR_PARSE; }
wf_status status = WF_OK; if (!parsed.authority || !wf_agent_authority_matches_session(agent, parsed.authority) || !parsed.collection || !parsed.record_key || strcmp(parsed.collection, WF_AGENT_REPOST_COLLECTION) != 0) { status = WF_ERR_INVALID_ARG; goto done; }
status = wf_agent_delete_record_call(agent, parsed.collection, parsed.record_key);
done: wf_syntax_aturi_free(&parsed); return status;}
#endif // social functions moved to post.c
#if 0 // feed functions moved to feed.c
wf_status wf_agent_get_timeline(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getTimeline", params, param_count, out);}
wf_status wf_agent_get_timeline_lex(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; wf_lex_app_bsky_feed_get_timeline_main_params params = {0}; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_timeline_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_author_feed(wf_agent *agent, const char *actor, int limit, const char *cursor, const char *filter, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[4]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (filter && filter[0]) { params[param_count].name = "filter"; params[param_count].value = filter; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getAuthorFeed", params, param_count, out);}
wf_status wf_agent_get_author_feed_lex(wf_agent *agent, const char *actor, int limit, const char *cursor, const char *filter, wf_response *out) { if (!agent || !actor || !out) return WF_ERR_INVALID_ARG; if (!wf_syntax_at_identifier_is_valid(actor)) return WF_ERR_INVALID_ARG; wf_lex_app_bsky_feed_get_author_feed_main_params params = {0}; params.actor = actor; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (filter && filter[0]) { params.has_filter = true; params.filter = filter; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_author_feed_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_post_thread(wf_agent *agent, const char *uri, int depth, wf_response *out) { if (!agent || !uri || !out) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) { return WF_ERR_PARSE; } wf_syntax_aturi_free(&parsed);
wf_xrpc_param params[2]; size_t param_count = 0; char depth_buf[16];
params[param_count].name = "uri"; params[param_count].value = uri; param_count++;
if (depth > 0) { if (!wf_agent_int_to_str(depth, depth_buf, sizeof(depth_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "depth"; params[param_count].value = depth_buf; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getPostThread", params, param_count, out);}
wf_status wf_agent_get_posts(wf_agent *agent, const char *const *uris, size_t uri_count, wf_response *out) { if (!agent || !uris || uri_count == 0 || !out) { return WF_ERR_INVALID_ARG; }
/* Validate all URIs first */ for (size_t i = 0; i < uri_count; ++i) { if (!uris[i]) { return WF_ERR_INVALID_ARG; } wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uris[i], &parsed)) { return WF_ERR_PARSE; } wf_syntax_aturi_free(&parsed); }
/* Build query string: uris=at://...&uris=at://... */ /* wf_xrpc_query_params handles repeated params by passing the same name */ wf_xrpc_param *params = calloc(uri_count, sizeof(*params)); if (!params) { return WF_ERR_ALLOC; }
for (size_t i = 0; i < uri_count; ++i) { params[i].name = "uris"; params[i].value = uris[i]; }
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_query_params(agent->client, "app.bsky.feed.getPosts", params, uri_count, out); free(params); return status;}
/* ── searchPosts ───────────────────────────────────────────────────── */#if 0 // searchPosts moved to feed.c
wf_status wf_agent_search_posts(wf_agent *agent, const char *query, int limit, const char *cursor, const char *sort, const char *since, const char *until, const char *author, const char *lang, wf_response *out) { if (!agent || !query || !query[0] || !out) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[9]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "q"; params[param_count].value = query; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; } if (sort && sort[0]) { params[param_count].name = "sort"; params[param_count].value = sort; param_count++; } if (since && since[0]) { params[param_count].name = "since"; params[param_count].value = since; param_count++; } if (until && until[0]) { params[param_count].name = "until"; params[param_count].value = until; param_count++; } if (author && author[0]) { if (!wf_syntax_at_identifier_is_valid(author)) { return WF_ERR_INVALID_ARG; } params[param_count].name = "author"; params[param_count].value = author; param_count++; } if (lang && lang[0]) { if (!wf_syntax_language_is_valid(lang)) { return WF_ERR_INVALID_ARG; } params[param_count].name = "lang"; params[param_count].value = lang; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.searchPosts", params, param_count, out);}
wf_status wf_agent_search_posts_lex(wf_agent *agent, const char *query, int limit, const char *cursor, const char *sort, const char *since, const char *until, const char *author, const char *lang, wf_response *out) { if (!agent || !query || !query[0] || !out) return WF_ERR_INVALID_ARG; wf_lex_app_bsky_feed_search_posts_main_params params = {0}; params.q = query; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } if (sort && sort[0]) { params.has_sort = true; params.sort = sort; } if (since && since[0]) { params.has_since = true; params.since = since; } if (until && until[0]) { params.has_until = true; params.until = until; } if (author && author[0]) { if (!wf_syntax_at_identifier_is_valid(author)) return WF_ERR_INVALID_ARG; params.has_author = true; params.author = author; } if (lang && lang[0]) { if (!wf_syntax_language_is_valid(lang)) return WF_ERR_INVALID_ARG; params.has_lang = true; params.lang = lang; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_search_posts_main_call(agent->client, ¶ms, out);}#endif // searchPosts moved to feed.c
/* ── getActorLikes ─────────────────────────────────────────────────── */
wf_status wf_agent_get_actor_likes(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getActorLikes", params, param_count, out);}
/* ── getLikes ──────────────────────────────────────────────────────── */
wf_status wf_agent_get_likes(wf_agent *agent, const char *uri, int limit, const char *cursor, wf_response *out) { if (!agent || !uri || !out) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) { return WF_ERR_PARSE; } wf_syntax_aturi_free(&parsed);
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "uri"; params[param_count].value = uri; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getLikes", params, param_count, out);}
wf_status wf_agent_get_likes_lex(wf_agent *agent, const char *uri, int limit, const char *cursor, wf_response *out) { if (!agent || !uri || !out) return WF_ERR_INVALID_ARG; // Validate AT-URI syntax wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) return WF_ERR_PARSE; wf_syntax_aturi_free(&parsed);
wf_lex_app_bsky_feed_get_likes_main_params params = {0}; params.uri = uri; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } // Ensure auth wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_likes_main_call(agent->client, ¶ms, out);}
/* ── lexicon wrappers for feed endpoints ─────────────────────────────────────── */
wf_status wf_agent_get_quotes_lex(wf_agent *agent, const char *uri, int limit, const char *cursor, wf_response *out) { if (!agent || !uri || !out) return WF_ERR_INVALID_ARG; wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) return WF_ERR_PARSE; wf_syntax_aturi_free(&parsed); wf_lex_app_bsky_feed_get_quotes_main_params params = {0}; params.uri = uri; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_quotes_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_list_feed_lex(wf_agent *agent, const char *list_uri, int limit, const char *cursor, wf_response *out) { if (!agent || !list_uri || !out) return WF_ERR_INVALID_ARG; wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(list_uri, &parsed)) return WF_ERR_PARSE; wf_syntax_aturi_free(&parsed); wf_lex_app_bsky_feed_get_list_feed_main_params params = {0}; params.list = list_uri; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_list_feed_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_feed_lex(wf_agent *agent, const char *feed_uri, int limit, const char *cursor, wf_response *out) { if (!agent || !feed_uri || !out) return WF_ERR_INVALID_ARG; wf_lex_app_bsky_feed_get_feed_main_params params = {0}; params.feed = feed_uri; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_feed_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_actor_feeds_lex(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) return WF_ERR_INVALID_ARG; if (!wf_syntax_at_identifier_is_valid(actor)) return WF_ERR_INVALID_ARG; wf_lex_app_bsky_feed_get_actor_feeds_main_params params = {0}; params.actor = actor; if (limit > 0) { params.has_limit = true; params.limit = limit; } if (cursor && cursor[0]) { params.has_cursor = true; params.cursor = cursor; } wf_agent_sync_auth(agent); return wf_lex_app_bsky_feed_get_actor_feeds_main_call(agent->client, ¶ms, out);}
/* ── getRepostedBy ─────────────────────────────────────────────────── */
wf_status wf_agent_get_reposted_by(wf_agent *agent, const char *uri, int limit, const char *cursor, wf_response *out) { if (!agent || !uri || !out) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) { return WF_ERR_PARSE; } wf_syntax_aturi_free(&parsed);
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "uri"; params[param_count].value = uri; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getRepostedBy", params, param_count, out);}
wf_status wf_agent_get_quotes(wf_agent *agent, const char *uri, int limit, const char *cursor, wf_response *out) { if (!agent || !uri || !out) return WF_ERR_INVALID_ARG; wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(uri, &parsed)) return WF_ERR_PARSE; wf_syntax_aturi_free(&parsed);
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16]; params[param_count].name = "uri"; params[param_count].value = uri; param_count++; if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) return WF_ERR_INVALID_ARG; params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; } wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getQuotes", params, param_count, out);}
wf_status wf_agent_get_list_feed(wf_agent *agent, const char *list_uri, int limit, const char *cursor, wf_response *out) { if (!agent || !list_uri || !out) return WF_ERR_INVALID_ARG; wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16]; params[param_count].name = "list"; params[param_count].value = list_uri; param_count++; if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) return WF_ERR_INVALID_ARG; params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; } wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getListFeed", params, param_count, out);}
wf_status wf_agent_get_feed(wf_agent *agent, const char *feed_uri, int limit, const char *cursor, wf_response *out) { if (!agent || !feed_uri || !out) return WF_ERR_INVALID_ARG; wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16]; params[param_count].name = "feed"; params[param_count].value = feed_uri; param_count++; if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) return WF_ERR_INVALID_ARG; params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; } wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getFeed", params, param_count, out);}
wf_status wf_agent_get_actor_feeds(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) return WF_ERR_INVALID_ARG; if (!wf_syntax_at_identifier_is_valid(actor)) return WF_ERR_INVALID_ARG;
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16]; params[param_count].name = "actor"; params[param_count].value = actor; param_count++; if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) return WF_ERR_INVALID_ARG; params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; } wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.feed.getActorFeeds", params, param_count, out);}
wf_status wf_agent_get_follows(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getFollows", params, param_count, out);}
wf_status wf_agent_get_followers(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getFollowers", params, param_count, out);}
/* ── getBlocks ─────────────────────────────────────────────────────── */
wf_status wf_agent_get_blocks(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[2]; size_t param_count = 0; char limit_buf[16];
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getBlocks", params, param_count, out);}
/* ── getMutes ──────────────────────────────────────────────────────── */
wf_status wf_agent_get_mutes(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[2]; size_t param_count = 0; char limit_buf[16];
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getMutes", params, param_count, out);}
/* ── getKnownFollowers ─────────────────────────────────────────────── */
wf_status wf_agent_get_known_followers(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getKnownFollowers", params, param_count, out);}
/* ── getRelationships ──────────────────────────────────────────────── */
wf_status wf_agent_get_relationships(wf_agent *agent, const char *actor, const char *const *others, size_t others_count, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_did_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param *params = NULL; size_t param_count = 0;
params = calloc(1 + others_count, sizeof(*params)); if (!params) return WF_ERR_ALLOC;
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
for (size_t i = 0; i < others_count; i++) { if (!others[i] || !others[i][0] || !wf_syntax_did_is_valid(others[i])) { free(params); return WF_ERR_INVALID_ARG; } params[param_count].name = "others"; params[param_count].value = others[i]; param_count++; }
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_query_params(agent->client, "app.bsky.graph.getRelationships", params, param_count, out); free(params); return status;}
/* ── getList ───────────────────────────────────────────────────────── */
wf_status wf_agent_get_list(wf_agent *agent, const char *list_uri, int limit, const char *cursor, wf_response *out) { if (!agent || !list_uri || !out) { return WF_ERR_INVALID_ARG; }
wf_syntax_aturi parsed = {0}; if (!wf_syntax_aturi_parse(list_uri, &parsed)) { return WF_ERR_PARSE; } wf_syntax_aturi_free(&parsed);
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "list"; params[param_count].value = list_uri; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getList", params, param_count, out);}
/* ── getLists ──────────────────────────────────────────────────────── */
wf_status wf_agent_get_lists(wf_agent *agent, const char *actor, int limit, const char *cursor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "actor"; params[param_count].value = actor; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getLists", params, param_count, out);}
#endif // feed functions moved to feed.c
#if 0 // graph functions moved to graph.c
wf_status wf_agent_get_suggested_follows_by_actor(wf_agent *agent, const char *actor, wf_response *out) { if (!agent || !actor || !out) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_at_identifier_is_valid(actor)) { return WF_ERR_INVALID_ARG; } wf_xrpc_param params[1]; size_t param_count = 0; params[param_count].name = "actor"; params[param_count].value = actor; param_count++; wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.graph.getSuggestedFollowsByActor", params, param_count, out);}
#endif // graph functions moved to graph.c
#if 0 // notification functions moved to notification.cwf_status wf_agent_list_notifications(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[2]; size_t param_count = 0; char limit_buf[16];
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.notification.listNotifications", params, param_count, out);}
wf_status wf_agent_update_seen_notifications(wf_agent *agent, const char *seen_at) { if (!agent || !seen_at) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_datetime_is_valid(seen_at)) { return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) { return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(root, "seenAt", seen_at)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) { return WF_ERR_ALLOC; }
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_procedure(agent->client, "app.bsky.notification.updateSeen", json, &res); free(json); wf_response_free(&res); return status;}
/* ── getUnreadCount ────────────────────────────────────────────────── */
wf_status wf_agent_get_unread_count(wf_agent *agent, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.notification.getUnreadCount", NULL, 0, out);}
#endif // notification functions moved to notification.c
wf_status wf_agent_search_actors(wf_agent *agent, const char *query, int limit, const char *cursor, wf_response *out) { if (!agent || !query || !query[0] || !out) { return WF_ERR_INVALID_ARG; } if (limit < 0 || limit > 100) { return WF_ERR_INVALID_ARG; }
wf_xrpc_param params[3]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "q"; params[param_count].value = query; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.actor.searchActors", params, param_count, out);}
wf_status wf_agent_search_actors_typeahead(wf_agent *agent, const char *query, int limit, wf_response *out) { if (!agent || !query || !query[0] || !out) { return WF_ERR_INVALID_ARG; } if (limit < 0 || limit > 100) { return WF_ERR_INVALID_ARG; } wf_xrpc_param params[2]; size_t param_count = 0; char limit_buf[16];
params[param_count].name = "q"; params[param_count].value = query; param_count++;
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) { return WF_ERR_INVALID_ARG; } params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "app.bsky.actor.searchActorsTypeahead", params, param_count, out);}
wf_status wf_agent_update_profile(wf_agent *agent, const wf_agent_profile_update *update) { if (!agent || !update) { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
char created_at[32]; if (!wf_agent_make_rfc3339_timestamp(created_at, sizeof(created_at)) || !wf_syntax_datetime_is_valid(created_at)) { return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) { return WF_ERR_ALLOC; }
if (!cJSON_AddStringToObject(record, "$type", WF_AGENT_PROFILE_RECORD_TYPE) || !cJSON_AddStringToObject(record, "createdAt", created_at)) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (update->display_name) { if (!cJSON_AddStringToObject(record, "displayName", update->display_name)) { cJSON_Delete(record); return WF_ERR_ALLOC; } }
if (update->description) { if (!cJSON_AddStringToObject(record, "description", update->description)) { cJSON_Delete(record); return WF_ERR_ALLOC; } }
if (update->avatar_cid) { cJSON *avatar = cJSON_CreateObject(); if (!avatar) { cJSON_Delete(record); return WF_ERR_ALLOC; } cJSON *ref = cJSON_CreateObject(); if (!ref) { cJSON_Delete(avatar); cJSON_Delete(record); return WF_ERR_ALLOC; } if (!cJSON_AddStringToObject(ref, "$link", update->avatar_cid) || !cJSON_AddItemToObject(avatar, "ref", ref) || !cJSON_AddStringToObject(avatar, "$type", "blob") || !cJSON_AddItemToObject(record, "avatar", avatar)) { cJSON_Delete(ref); cJSON_Delete(avatar); cJSON_Delete(record); return WF_ERR_ALLOC; } }
if (update->banner_cid) { cJSON *banner = cJSON_CreateObject(); if (!banner) { cJSON_Delete(record); return WF_ERR_ALLOC; } cJSON *ref = cJSON_CreateObject(); if (!ref) { cJSON_Delete(banner); cJSON_Delete(record); return WF_ERR_ALLOC; } if (!cJSON_AddStringToObject(ref, "$link", update->banner_cid) || !cJSON_AddItemToObject(banner, "ref", ref) || !cJSON_AddStringToObject(banner, "$type", "blob") || !cJSON_AddItemToObject(record, "banner", banner)) { cJSON_Delete(ref); cJSON_Delete(banner); cJSON_Delete(record); return WF_ERR_ALLOC; } }
wf_agent_post_result result = {0}; wf_status status = wf_agent_put_record_call(agent, WF_AGENT_PROFILE_COLLECTION, WF_AGENT_PROFILE_RKEY, record, &result); wf_agent_post_result_free(&result); return status;}
wf_status wf_agent_upload_blob(wf_agent *agent, const void *data, size_t data_len, const char *content_type, wf_response *out) { if (!agent || !data || data_len == 0 || !content_type || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_agent_sync_auth(agent); return wf_xrpc_upload_blob(agent->client, WF_AGENT_UPLOAD_BLOB_NSID, data, data_len, content_type, out);}
/* ── applyWrites ────────────────────────────────────────────────────── */
/* Unused: wf_agent_build_write_value removed */
static const char *wf_agent_write_type_str(wf_agent_write_type type) { switch (type) { case WF_AGENT_WRITE_CREATE: return "com.atproto.repo.applyWrites#create"; case WF_AGENT_WRITE_UPDATE: return "com.atproto.repo.applyWrites#update"; case WF_AGENT_WRITE_DELETE: return "com.atproto.repo.applyWrites#delete"; } return NULL;}
static wf_status wf_agent_add_write(cJSON *writes_array, const wf_agent_write *write) { cJSON *item = cJSON_CreateObject(); if (!item) return WF_ERR_ALLOC;
const char *type_str = wf_agent_write_type_str(write->type); if (!type_str || !cJSON_AddStringToObject(item, "$type", type_str) || !cJSON_AddStringToObject(item, "collection", write->collection)) { cJSON_Delete(item); return WF_ERR_ALLOC; }
if (write->rkey && write->rkey[0]) { if (!cJSON_AddStringToObject(item, "rkey", write->rkey)) { cJSON_Delete(item); return WF_ERR_ALLOC; } }
if (write->type != WF_AGENT_WRITE_DELETE) { if (!write->value_json) { cJSON_Delete(item); return WF_ERR_INVALID_ARG; } cJSON *value = cJSON_Parse(write->value_json); if (!value) { cJSON_Delete(item); return WF_ERR_PARSE; } if (!cJSON_AddItemToObject(item, "value", value)) { cJSON_Delete(value); cJSON_Delete(item); return WF_ERR_ALLOC; } }
if (!cJSON_AddItemToArray(writes_array, item)) { cJSON_Delete(item); return WF_ERR_ALLOC; }
return WF_OK;}
wf_status wf_agent_apply_writes(wf_agent *agent, const wf_agent_write *writes, size_t write_count, wf_response *out) { size_t i; wf_status status;
if (!agent || !writes || write_count == 0 || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
for (i = 0; i < write_count; i++) { if (!writes[i].collection) return WF_ERR_INVALID_ARG; }
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC;
if (!cJSON_AddStringToObject(root, "repo", agent->session->data.did)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
cJSON *writes_array = cJSON_AddArrayToObject(root, "writes"); if (!writes_array) { cJSON_Delete(root); return WF_ERR_ALLOC; }
for (i = 0; i < write_count; i++) { status = wf_agent_add_write(writes_array, &writes[i]); if (status != WF_OK) { cJSON_Delete(root); return status; } }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); status = wf_xrpc_procedure(agent->client, "com.atproto.repo.applyWrites", json, out); free(json); return status;}
wf_status wf_agent_resolve_handle(wf_agent *agent, const char *handle, char **out_did) { if (!agent || !handle || !handle[0] || !out_did) { return WF_ERR_INVALID_ARG; }
if (!wf_syntax_handle_is_valid(handle)) { return WF_ERR_INVALID_ARG; }
*out_did = NULL;
wf_xrpc_param params[] = { {"handle", handle}, };
wf_agent_sync_auth(agent);
wf_response res = {0}; wf_status status = wf_xrpc_query_params( agent->client, WF_AGENT_RESOLVE_HANDLE_NSID, params, 1, &res); if (status != WF_OK) { wf_response_free(&res); return status; }
cJSON *root = cJSON_ParseWithLength(res.body, res.body_len); if (!root) { wf_response_free(&res); return WF_ERR_PARSE; }
cJSON *did = cJSON_GetObjectItemCaseSensitive(root, "did"); if (!cJSON_IsString(did) || !did->valuestring || !wf_syntax_did_is_valid(did->valuestring)) { cJSON_Delete(root); wf_response_free(&res); return WF_ERR_PARSE; }
status = wf_agent_set_string(out_did, did->valuestring); if (status != WF_OK) { free(*out_did); *out_did = NULL; }
cJSON_Delete(root); wf_response_free(&res); return status;}
/* Update handle – com.atproto.identity.updateHandle */wf_status wf_agent_update_handle(wf_agent *agent, const char *new_handle) { if (!agent || !new_handle || !new_handle[0]) { return WF_ERR_INVALID_ARG; } if (!wf_syntax_handle_is_valid(new_handle)) { return WF_ERR_INVALID_ARG; } wf_lex_com_atproto_identity_update_handle_main_input input = { .handle = new_handle}; wf_response res = {0}; wf_agent_sync_auth(agent); wf_status status = wf_lex_com_atproto_identity_update_handle_main_call( agent->client, &input, &res); wf_response_free(&res); return status;}
void wf_agent_server_description_free(wf_agent_server_description *desc) { wf_agent_server_description_reset(desc);}
void wf_agent_app_password_free(wf_agent_app_password *pwd) { wf_agent_app_password_reset(pwd);}
void wf_agent_app_password_list_free(wf_agent_app_password_list *list) { wf_agent_app_password_list_reset(list);}
wf_status wf_agent_describe_server(wf_agent *agent, wf_agent_server_description *out) { if (!agent || !agent->client || !out) { return WF_ERR_INVALID_ARG; }
wf_agent_server_description_reset(out);
wf_server_description sdesc = {0}; wf_status status = wf_server_describe(agent->client, &sdesc); if (status != WF_OK) { return status; }
status = wf_agent_set_string(&out->did, sdesc.did); if (status == WF_OK) { out->invite_code_required = sdesc.invite_code_required; out->phone_verification_required = sdesc.phone_verification_required; } if (status == WF_OK && sdesc.available_user_domains_count > 0) { out->available_user_domains = calloc(sdesc.available_user_domains_count, sizeof(char *)); if (!out->available_user_domains) { status = WF_ERR_ALLOC; } } if (status == WF_OK) { for (size_t i = 0; i < sdesc.available_user_domains_count; ++i) { out->available_user_domains[i] = wf_agent_strdup(sdesc.available_user_domains[i]); if (!out->available_user_domains[i]) { status = WF_ERR_ALLOC; break; } out->available_user_domain_count++; } } if (status == WF_OK) { status = wf_agent_set_string(&out->privacy_policy, sdesc.links_privacy_policy); } if (status == WF_OK) { status = wf_agent_set_string(&out->terms_of_service, sdesc.links_terms_of_service); } if (status == WF_OK) { status = wf_agent_set_string(&out->contact_email, sdesc.contact_email); }
if (status != WF_OK) { wf_agent_server_description_reset(out); }
wf_server_describe_free(&sdesc); return status;}
wf_status wf_agent_create_app_password(wf_agent *agent, const char *name, int privileged, wf_agent_app_password *out) { if (!agent || !agent->client || !out || !name || name[0] == '\0') { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_agent_app_password_reset(out);
wf_server_create_app_password_input input = { .name = name, .privileged = privileged, };
wf_server_app_password spwd = {0}; wf_agent_sync_auth(agent); wf_status status = wf_server_create_app_password(agent->client, &input, &spwd); if (status != WF_OK) { return status; }
status = wf_agent_set_string(&out->name, spwd.name); if (status == WF_OK) { status = wf_agent_set_string(&out->created_at, spwd.created_at); } if (status == WF_OK) { out->privileged = spwd.privileged; }
if (status != WF_OK) { wf_agent_app_password_reset(out); }
wf_server_app_password_free(&spwd); return status;}
wf_status wf_agent_list_app_passwords(wf_agent *agent, wf_agent_app_password_list *out) { if (!agent || !agent->client || !out) { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_agent_app_password_list_reset(out);
wf_server_app_password_list slist = {0}; wf_agent_sync_auth(agent); wf_status status = wf_server_list_app_passwords(agent->client, &slist); if (status != WF_OK) { return status; }
if (slist.password_count > 0) { out->passwords = calloc(slist.password_count, sizeof(wf_agent_app_password)); if (!out->passwords) { status = WF_ERR_ALLOC; } } if (status == WF_OK) { for (size_t i = 0; i < slist.password_count; ++i) { wf_server_app_password *src = &slist.passwords[i]; wf_agent_app_password *dst = &out->passwords[i];
status = wf_agent_set_string(&dst->name, src->name); if (status == WF_OK) { status = wf_agent_set_string(&dst->created_at, src->created_at); } if (status == WF_OK) { dst->privileged = src->privileged; } if (status != WF_OK) { break; } out->password_count++; } }
if (status != WF_OK) { wf_agent_app_password_list_reset(out); }
wf_server_app_password_list_free(&slist); return status;}
wf_status wf_agent_revoke_app_password(wf_agent *agent, const char *name) { if (!agent || !agent->client || !name || name[0] == '\0') { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_server_revoke_app_password_input input = { .name = name, };
wf_agent_sync_auth(agent); return wf_server_revoke_app_password(agent->client, &input);}
wf_status wf_agent_delete_account(wf_agent *agent, const char *did, const char *password, const char *token) { if (!agent || !agent->client || !did || !password || !token) { return WF_ERR_INVALID_ARG; }
if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_server_delete_account_input input = { .did = did, .password = password, .token = token, };
wf_agent_sync_auth(agent); return wf_server_delete_account(agent->client, &input);}
/* ── actor status ───────────────────────────────────────────────────── */
wf_status wf_agent_put_actor_status(wf_agent *agent, const char *status, int duration_minutes, const char *embed_json, wf_agent_post_result *out) { if (!agent || !status || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
cJSON *record = cJSON_CreateObject(); if (!record) return WF_ERR_ALLOC;
if (!cJSON_AddStringToObject(record, "status", status) || !cJSON_AddStringToObject(record, "$type", "app.bsky.actor.status")) { cJSON_Delete(record); return WF_ERR_ALLOC; }
if (duration_minutes > 0) { if (!cJSON_AddNumberToObject(record, "durationMinutes", duration_minutes)) { cJSON_Delete(record); return WF_ERR_ALLOC; } }
if (embed_json && embed_json[0]) { cJSON *embed = cJSON_Parse(embed_json); if (!embed) { cJSON_Delete(record); return WF_ERR_PARSE; } if (!cJSON_AddItemToObject(record, "embed", embed)) { cJSON_Delete(embed); cJSON_Delete(record); return WF_ERR_ALLOC; } }
/* createdAt — use current time */ time_t now = time(NULL); struct tm tm_utc; gmtime_r(&now, &tm_utc); char ts_buf[32]; strftime(ts_buf, sizeof(ts_buf), "%Y-%m-%dT%H:%M:%SZ", &tm_utc); if (!cJSON_AddStringToObject(record, "createdAt", ts_buf)) { cJSON_Delete(record); return WF_ERR_ALLOC; }
char *json_str = cJSON_PrintUnformatted(record); cJSON_Delete(record); if (!json_str) return WF_ERR_ALLOC;
wf_status status_ret = wf_agent_put_record(agent, "app.bsky.actor.status", "self", json_str, out); free(json_str); return status_ret;}
wf_status wf_agent_get_video_job_status(wf_agent *agent, const char *job_id, wf_response *out) { if (!agent || !job_id || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
/* Use the generated marshalling rather than hand-building the query * param array: the NSID, param name ("jobId"), and encoding all come * from wf_lexgen, so this can't drift from the lexicon. */ wf_lex_app_bsky_video_get_job_status_main_params params = {0}; params.job_id = job_id;
wf_agent_sync_auth(agent); return wf_lex_app_bsky_video_get_job_status_main_call(agent->client, ¶ms, out);}
wf_status wf_agent_get_video_upload_limits(wf_agent *agent, wf_response *out) { if (!agent || !out) { return WF_ERR_INVALID_ARG; } if (!wf_agent_is_logged_in(agent)) { return WF_ERR_INVALID_ARG; }
wf_agent_sync_auth(agent); return wf_lex_app_bsky_video_get_upload_limits_main_call(agent->client, out);}
/* ── server wrappers ─────────────────────────────────────────────────── */
wf_status wf_agent_create_invite_code(wf_agent *agent, int use_count, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC; if (!cJSON_AddNumberToObject(root, "useCount", use_count)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.createInviteCode", json, out); free(json); return status;}
wf_status wf_agent_get_account_invite_codes(wf_agent *agent, int limit, const char *cursor, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
wf_xrpc_param params[2]; size_t param_count = 0; char limit_buf[16];
if (limit > 0) { if (!wf_agent_int_to_str(limit, limit_buf, sizeof(limit_buf))) return WF_ERR_INVALID_ARG; params[param_count].name = "limit"; params[param_count].value = limit_buf; param_count++; } if (cursor && cursor[0]) { params[param_count].name = "cursor"; params[param_count].value = cursor; param_count++; }
wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "com.atproto.server.getAccountInviteCodes", params, param_count, out);}
wf_status wf_agent_activate_account(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_procedure(agent->client, "com.atproto.server.activateAccount", "{}", out);}
wf_status wf_agent_deactivate_account(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_procedure(agent->client, "com.atproto.server.deactivateAccount", "{}", out);}
wf_status wf_agent_check_account_status(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_query(agent->client, "com.atproto.server.checkAccountStatus", NULL, out);}
wf_status wf_agent_confirm_email(wf_agent *agent, const char *email, const char *token, wf_response *out) { if (!agent || !email || !token || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(root, "email", email) || !cJSON_AddStringToObject(root, "token", token)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.confirmEmail", json, out); free(json); return status;}
wf_status wf_agent_update_email(wf_agent *agent, const char *email, wf_response *out) { if (!agent || !email || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
cJSON *root = cJSON_CreateObject(); if (!root) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(root, "email", email)) { cJSON_Delete(root); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(root); cJSON_Delete(root); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.updateEmail", json, out); free(json); return status;}
wf_status wf_agent_create_account(wf_agent *agent, const char *email, const char *handle, const char *did, const char *invite_code, const char *verification_phone, const char *password, wf_response *out) { if (!agent || !handle || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC;
if (email && email[0]) cJSON_AddStringToObject(body, "email", email); if (!cJSON_AddStringToObject(body, "handle", handle)) { cJSON_Delete(body); return WF_ERR_ALLOC; } if (did && did[0]) cJSON_AddStringToObject(body, "did", did); if (invite_code && invite_code[0]) cJSON_AddStringToObject(body, "inviteCode", invite_code); if (verification_phone && verification_phone[0]) cJSON_AddStringToObject(body, "verificationPhone", verification_phone); if (password && password[0]) cJSON_AddStringToObject(body, "password", password);
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.createAccount", json, out); free(json); return status;}
wf_status wf_agent_create_invite_codes(wf_agent *agent, int use_count, int code_count, const char *const *for_dids, size_t for_did_count, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC;
if (!cJSON_AddNumberToObject(body, "useCount", use_count) || !cJSON_AddNumberToObject(body, "codeCount", code_count)) { cJSON_Delete(body); return WF_ERR_ALLOC; }
if (for_dids && for_did_count > 0) { cJSON *arr = cJSON_AddArrayToObject(body, "forDids"); if (!arr) { cJSON_Delete(body); return WF_ERR_ALLOC; } for (size_t i = 0; i < for_did_count; i++) { cJSON *item = cJSON_CreateString(for_dids[i]); if (!item || !cJSON_AddItemToArray(arr, item)) { cJSON_Delete(body); return WF_ERR_ALLOC; } } }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.createInviteCodes", json, out); free(json); return status;}
wf_status wf_agent_request_account_delete(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_procedure( agent->client, "com.atproto.server.requestAccountDelete", "{}", out);}
wf_status wf_agent_request_email_confirmation(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_procedure(agent->client, "com.atproto.server.requestEmailConfirmation", "{}", out);}
wf_status wf_agent_request_email_update(wf_agent *agent, const char *email, wf_response *out) { if (!agent || !email || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(body, "email", email)) { cJSON_Delete(body); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.requestEmailUpdate", json, out); free(json); return status;}
wf_status wf_agent_request_password_reset(wf_agent *agent, const char *email, wf_response *out) { if (!agent || !email || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(body, "email", email)) { cJSON_Delete(body); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.requestPasswordReset", json, out); free(json); return status;}
wf_status wf_agent_reset_password(wf_agent *agent, const char *token, const char *password, wf_response *out) { if (!agent || !token || !password || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(body, "token", token) || !cJSON_AddStringToObject(body, "password", password)) { cJSON_Delete(body); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.resetPassword", json, out); free(json); return status;}
wf_status wf_agent_reserve_signing_key(wf_agent *agent, const char *did, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC; if (did && did[0]) cJSON_AddStringToObject(body, "did", did);
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.reserveSigningKey", json, out); free(json); return status;}
wf_status wf_agent_get_service_auth(wf_agent *agent, const char *aud, const char *lhf, wf_response *out) { if (!agent || !aud || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC; if (!cJSON_AddStringToObject(body, "aud", aud)) { cJSON_Delete(body); return WF_ERR_ALLOC; } if (lhf && lhf[0]) cJSON_AddStringToObject(body, "lhf", lhf);
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.server.getServiceAuth", json, out); free(json); return status;}
/* ── identity wrappers ───────────────────────────────────────────────── */
wf_status wf_agent_resolve_did(wf_agent *agent, const char *did, wf_response *out) { if (!agent || !did || !out) return WF_ERR_INVALID_ARG; if (!wf_syntax_did_is_valid(did)) return WF_ERR_INVALID_ARG;
wf_xrpc_param params[] = {{"did", did}}; wf_agent_sync_auth(agent); return wf_xrpc_query_params( agent->client, "com.atproto.identity.resolveDid", params, 1, out);}
wf_status wf_agent_get_recommended_did_credentials(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_query(agent->client, "com.atproto.identity.getRecommendedDidCredentials", NULL, out);}
wf_status wf_agent_sign_plc_operation(wf_agent *agent, const char *token, const char *rotation_keys_json, const char *also_known_as_json, const char *verification_methods_json, const char *services_json, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_CreateObject(); if (!body) return WF_ERR_ALLOC;
if (token && token[0]) cJSON_AddStringToObject(body, "token", token);
if (rotation_keys_json) { cJSON *arr = cJSON_Parse(rotation_keys_json); if (!arr) { cJSON_Delete(body); return WF_ERR_PARSE; } cJSON_AddItemToObject(body, "rotationKeys", arr); } if (also_known_as_json) { cJSON *arr = cJSON_Parse(also_known_as_json); if (!arr) { cJSON_Delete(body); return WF_ERR_PARSE; } cJSON_AddItemToObject(body, "alsoKnownAs", arr); } if (verification_methods_json) { cJSON *obj = cJSON_Parse(verification_methods_json); if (!obj) { cJSON_Delete(body); return WF_ERR_PARSE; } cJSON_AddItemToObject(body, "verificationMethods", obj); } if (services_json) { cJSON *obj = cJSON_Parse(services_json); if (!obj) { cJSON_Delete(body); return WF_ERR_PARSE; } cJSON_AddItemToObject(body, "services", obj); }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.identity.signPlcOperation", json, out); free(json); return status;}
wf_status wf_agent_submit_plc_operation(wf_agent *agent, const char *operation_json, wf_response *out) { if (!agent || !operation_json || !out) return WF_ERR_INVALID_ARG;
cJSON *body = cJSON_Parse(operation_json); if (!body) return WF_ERR_PARSE;
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "com.atproto.identity.submitPlcOperation", json, out); free(json); return status;}
wf_status wf_agent_request_plc_operation_signature(wf_agent *agent, wf_response *out) { if (!agent || !out) return WF_ERR_INVALID_ARG; wf_agent_sync_auth(agent); return wf_xrpc_procedure( agent->client, "com.atproto.identity.requestPlcOperationSignature", "{}", out);}
wf_status wf_agent_describe_repo(wf_agent *agent, const char *repo, wf_response *out) { if (!agent || !repo || !out) return WF_ERR_INVALID_ARG; if (!wf_syntax_at_identifier_is_valid(repo)) return WF_ERR_INVALID_ARG;
wf_xrpc_param params[] = {{"repo", repo}}; wf_agent_sync_auth(agent); return wf_xrpc_query_params(agent->client, "com.atproto.repo.describeRepo", params, 1, out);}wf_status wf_agent_send_interactions(wf_agent *agent, const char *feed_uri, const char *interactions_json, wf_response *out) { if (!agent || !interactions_json || !out) return WF_ERR_INVALID_ARG; if (!wf_agent_is_logged_in(agent)) return WF_ERR_INVALID_ARG;
cJSON *root = cJSON_Parse(interactions_json); if (!root) return WF_ERR_PARSE;
cJSON *body = cJSON_CreateObject(); if (!body) { cJSON_Delete(root); return WF_ERR_ALLOC; }
if (feed_uri && feed_uri[0]) { if (!cJSON_AddStringToObject(body, "feed", feed_uri)) { cJSON_Delete(root); cJSON_Delete(body); return WF_ERR_ALLOC; } } if (!cJSON_AddItemToObject(body, "interactions", root)) { cJSON_Delete(root); cJSON_Delete(body); return WF_ERR_ALLOC; }
char *json = cJSON_PrintUnformatted(body); cJSON_Delete(body); if (!json) return WF_ERR_ALLOC;
wf_agent_sync_auth(agent); wf_status status = wf_xrpc_procedure( agent->client, "app.bsky.feed.sendInteractions", json, out); free(json); return status;}