UNPKG

@kontext.dev/kontext-sdk

Version:

Unified SDK for AI-powered context and personalization

2 lines 2.76 kB
var c=class{constructor(t,s){this.apiUrl=t;this.apiKey=s;}async handleResponse(t,s){if(!t.ok){let r=await t.text().catch(()=>"");throw new Error(`${s} (${t.status}): ${r}`)}return await t.json()}async upload(t){let s=new FormData;s.append("file",t.file);let r=await fetch(`${this.apiUrl}/vault/files`,{method:"POST",headers:{"x-api-key":this.apiKey,"x-as-user":t.userId},body:s}),i=await this.handleResponse(r,"Upload failed");return {fileId:String(i?.fileId||""),status:i?.status??"pending"}}async getFileStatus(t){let s=await fetch(`${this.apiUrl}/vault/files/${t.fileId}/status`,{headers:{"x-api-key":this.apiKey}});return this.handleResponse(s,"getFileStatus failed")}async query(t){let s={userId:t.userId,query:t.query};typeof t.topK=="number"&&(s.topK=t.topK),t.fileId&&(s.fileId=t.fileId),t.includeAnswer&&(s.includeAnswer=true);let r=await fetch(`${this.apiUrl}/vault/query`,{method:"POST",headers:{"x-api-key":this.apiKey,"content-type":"application/json"},body:JSON.stringify(s)}),i=await this.handleResponse(r,"query failed"),u=e=>({id:String(e?.id??e?.row_id??e?.datasetItemId??e?.documentId??""),score:typeof e?.score=="number"?e.score:typeof e?.distance=="number"?e.distance:null,attributes:(e?.attributes&&typeof e.attributes=="object"?e.attributes:e)??{}}),y=e=>({id:String(e?.id??""),score:typeof e?.score=="number"?e.score:null,attributes:{doc_type:e?.doc_type,subject:e?.subject,timestamp:e?.timestamp,snippet:e?.snippet}}),o=Array.isArray(i?.hits)?i.hits:void 0,p=Array.isArray(i?.rows)?i.rows:void 0,n=Array.isArray(i?.sources)?i.sources:void 0,f=o?o.map(u):p?p.map(u):n?n.map(y):[],l;if(i?.answer){let e=i.answer,d=null;typeof e?.text=="string"&&(d=e.text),l={text:d,sources:Array.isArray(e?.sources)?e.sources.map(a=>({id:String(a?.id??""),score:typeof a?.score=="number"?a.score:0,doc_type:String(a?.doc_type??""),subject:a?.subject,timestamp:a?.timestamp,snippet:a?.snippet})):(n||[]).map(a=>({id:String(a?.id??""),score:typeof a?.score=="number"?a.score:0,doc_type:String(a?.doc_type??""),subject:a?.subject,timestamp:a?.timestamp,snippet:a?.snippet}))};}return {hits:f,...l?{answer:l}:{}}}async listFiles(t){let s=await fetch(`${this.apiUrl}/vault/users/${t.userId}/files`,{headers:{"x-api-key":this.apiKey}});return this.handleResponse(s,"listFiles failed")}async deleteFile(t){let s=await fetch(`${this.apiUrl}/vault/files/${t.fileId}`,{method:"DELETE",headers:{"x-api-key":this.apiKey}});return this.handleResponse(s,"deleteFile failed")}async clearVault(t){let s=await fetch(`${this.apiUrl}/vault/users/${t.userId}`,{method:"DELETE",headers:{"x-api-key":this.apiKey}});return this.handleResponse(s,"Vault namespace clear failed")}};export{c as a};//# sourceMappingURL=chunk-AXQVSE5D.js.map //# sourceMappingURL=chunk-AXQVSE5D.js.map