UNPKG

@kontext.dev/kontext-sdk

Version:

Unified SDK for AI-powered context and personalization

2 lines 2.97 kB
var o=class extends Error{constructor(t,r,i){super(t);this.code=r;this.statusCode=i;this.name="KontextError";}};var c=class{constructor(e,t){this.apiUrl=e;this.apiKey=t;}async getProfile(e){if(!e)throw this.createError("GetProfileOptions is required. Please provide an object with at least a userId field.","INVALID_OPTIONS",400);let{userId:t,task:r,maxTokens:i,privacyLevel:n,userQuery:d}=e;if(!t||typeof t!="string")throw this.createError("Valid userId is required. Please provide a non-empty string userId.","INVALID_USER_ID",400);if(i!==void 0&&(typeof i!="number"||i<=0))throw this.createError("maxTokens must be a positive number if provided.","INVALID_MAX_TOKENS",400);if(n&&!["strict","moderate","none"].includes(n))throw this.createError("privacyLevel must be one of: strict, moderate, none","INVALID_PRIVACY_LEVEL",400);try{let a=await fetch(`${this.apiUrl}/profile`,{method:"POST",headers:{"x-api-key":this.apiKey,"content-type":"application/json"},body:JSON.stringify({userId:t,task:r||"general",maxFacts:i?Math.min(i/10,100):void 0,cachePolicy:"fresh",includeRecentData:!0,privacyLevel:n||"none",userQuery:d})});if(!a.ok){let u=await a.text().catch(()=>"");throw new Error(u||`HTTP ${a.status}`)}let s=await a.json();return {systemPrompt:s.systemPrompt,metadata:{userId:t,timestamp:s.metadata?.generatedAt?new Date(s.metadata.generatedAt):new Date,providers:s.metadata?.providers||["gmail"]},tokenCount:typeof s.systemPrompt=="string"?s.systemPrompt.length/4:void 0}}catch(a){let s=a instanceof Error?a.message:"";throw s.includes("OAuth access")||s.includes("No OAuth")?this.createError("User has not authorized access. Please ensure the user has connected their account.","UNAUTHORIZED_USER",401):this.createError("Failed to get profile","PROFILE_FAILED",500)}}async listFacts(e){if(!e||!e.userId)throw this.createError("userId is required to list facts.","INVALID_USER_ID",400);let t=new URLSearchParams({userId:e.userId});typeof e.limit=="number"&&t.set("limit",String(e.limit)),typeof e.minConfidence=="number"&&t.set("minConfidence",String(e.minConfidence)),e.category&&t.set("category",e.category);let r=await fetch(`${this.apiUrl}/profile/facts?${t.toString()}`,{headers:{"x-api-key":this.apiKey}});if(!r.ok){let n=await r.text().catch(()=>"");throw this.createError(n||`HTTP ${r.status}`,"FACTS_LIST_FAILED",r.status)}return await r.json()}async deleteFacts(e){if(!e||!e.userId)throw this.createError("userId is required to delete facts.","INVALID_USER_ID",400);let t=await fetch(`${this.apiUrl}/profile/facts`,{method:"DELETE",headers:{"x-api-key":this.apiKey,"content-type":"application/json"},body:JSON.stringify({userId:e.userId,factIds:e.factIds})});if(!t.ok){let r=await t.text().catch(()=>"");throw this.createError(r||`HTTP ${t.status}`,"FACTS_DELETE_FAILED",t.status)}return await t.json()}createError(e,t,r){return new o(e,t,r)}};export{o as a,c as b};//# sourceMappingURL=chunk-FRDJ4LIP.js.map //# sourceMappingURL=chunk-FRDJ4LIP.js.map