@hasura/promptql 0.2.0-alpha.2 → 0.3.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.iife.js +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.iife.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var promptql=function(_){"use strict";var qe=Object.defineProperty;var ke=(_,v,I)=>v in _?qe(_,v,{enumerable:!0,configurable:!0,writable:!0,value:I}):_[v]=I;var bt=(_,v,I)=>ke(_,typeof v!="symbol"?v+"":v,I);class v extends Error{constructor(r,n){super(n||r.map(o=>o.msg).join(". "));bt(this,"detail",[]);this.detail=r}}var I=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},C="1.9.0",ut=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ut);if(!n)return function(){return!1};var o={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(o.prerelease!=null)return function(p){return p===t};function a(c){return r.add(c),!1}function i(c){return e.add(c),!0}return function(p){if(e.has(p))return!0;if(r.has(p))return!1;var g=p.match(ut);if(!g)return a(p);var m={major:+g[1],minor:+g[2],patch:+g[3],prerelease:g[4]};return m.prerelease!=null||o.major!==m.major?a(p):o.major===0?o.minor===m.minor&&o.patch<=m.patch?i(p):a(p):o.minor<=m.minor?i(p):a(p)}}var Et=wt(C),At=C.split(".")[0],S=Symbol.for("opentelemetry.js.api."+At),x=I;function j(t,e,r,n){var o;n===void 0&&(n=!1);var a=x[S]=(o=x[S])!==null&&o!==void 0?o:{version:C};if(!n&&a[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(a.version!==C){var i=new Error("@opentelemetry/api: Registration of version v"+a.version+" for "+t+" does not match previously registered API v"+C);return r.error(i.stack||i.message),!1}return a[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+C+"."),!0}function T(t){var e,r,n=(e=x[S])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=x[S])===null||r===void 0?void 0:r[t]}function B(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+C+".");var r=x[S];r&&delete r[t]}var Nt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},Ct=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("verbose",this._namespace,e)},t}();function G(t,e,r){var n=T("diag");if(n)return r.unshift(e),n[t].apply(n,Pt([],Nt(r),!1))}var h;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(h||(h={}));function Tt(t,e){t<h.NONE?t=h.NONE:t>h.ALL&&(t=h.ALL),e=e||{};function r(n,o){var a=e[n];return typeof a=="function"&&t>=o?a.bind(e):function(){}}return{error:r("error",h.ERROR),warn:r("warn",h.WARN),info:r("info",h.INFO),debug:r("debug",h.DEBUG),verbose:r("verbose",h.VERBOSE)}}var Ot=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Rt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},It="diag",w=function(){function t(){function e(o){return function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];var c=T("diag");if(c)return c[o].apply(c,Rt([],Ot(a),!1))}}var r=this,n=function(o,a){var i,c,p;if(a===void 0&&(a={logLevel:h.INFO}),o===r){var g=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=g.stack)!==null&&i!==void 0?i:g.message),!1}typeof a=="number"&&(a={logLevel:a});var m=T("diag"),f=Tt((c=a.logLevel)!==null&&c!==void 0?c:h.INFO,o);if(m&&!a.suppressOverrideMessage){var u=(p=new Error().stack)!==null&&p!==void 0?p:"<failed to generate stacktrace>";m.warn("Current logger will be overwritten from "+u),f.warn("Current logger will overwrite one already registered from "+u)}return j("diag",f,r,!0)};r.setLogger=n,r.disable=function(){B(It,r)},r.createComponentLogger=function(o){return new Ct(o)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}(),Mt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},St=function(t){var e=typeof Symbol=="function"&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")},xt=function(){function t(e){this._entries=e?new Map(e):new Map}return t.prototype.getEntry=function(e){var r=this._entries.get(e);if(r)return Object.assign({},r)},t.prototype.getAllEntries=function(){return Array.from(this._entries.entries()).map(function(e){var r=Mt(e,2),n=r[0],o=r[1];return[n,o]})},t.prototype.setEntry=function(e,r){var n=new t(this._entries);return n._entries.set(e,r),n},t.prototype.removeEntry=function(e){var r=new t(this._entries);return r._entries.delete(e),r},t.prototype.removeEntries=function(){for(var e,r,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var a=new t(this._entries);try{for(var i=St(n),c=i.next();!c.done;c=i.next()){var p=c.value;a._entries.delete(p)}}catch(g){e={error:g}}finally{try{c&&!c.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return a},t.prototype.clear=function(){return new t},t}();w.instance();function jt(t){return t===void 0&&(t={}),new xt(new Map(Object.entries(t)))}function ct(t){return Symbol.for(t)}var Bt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,o){var a=new t(r._currentContext);return a._currentContext.set(n,o),a},r.deleteValue=function(n){var o=new t(r._currentContext);return o._currentContext.delete(n),o}}return t}(),Gt=new Bt,O=function(){var t=function(e,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,o){n.__proto__=o}||function(n,o){for(var a in o)Object.prototype.hasOwnProperty.call(o,a)&&(n[a]=o[a])},t(e,r)};return function(e,r){if(typeof r!="function"&&r!==null)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");t(e,r);function n(){this.constructor=e}e.prototype=r===null?Object.create(r):(n.prototype=r.prototype,new n)}}(),Dt=function(){function t(){}return t.prototype.createGauge=function(e,r){return Wt},t.prototype.createHistogram=function(e,r){return Ht},t.prototype.createCounter=function(e,r){return zt},t.prototype.createUpDownCounter=function(e,r){return Xt},t.prototype.createObservableGauge=function(e,r){return Kt},t.prototype.createObservableCounter=function(e,r){return Jt},t.prototype.createObservableUpDownCounter=function(e,r){return Yt},t.prototype.addBatchObservableCallback=function(e,r){},t.prototype.removeBatchObservableCallback=function(e){},t}(),$=function(){function t(){}return t}(),Ut=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),F=function(){function t(){}return t.prototype.addCallback=function(e){},t.prototype.removeCallback=function(e){},t}(),Lt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Ft=new Dt,zt=new Ut,Wt=new kt,Ht=new $t,Xt=new qt,Jt=new Lt,Kt=new Vt,Yt=new Qt,Zt={get:function(t,e){if(t!=null)return t[e]},keys:function(t){return t==null?[]:Object.keys(t)}},te={set:function(t,e,r){t!=null&&(t[e]=r)}},ee=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},re=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},ne=function(){function t(){}return t.prototype.active=function(){return Gt},t.prototype.with=function(e,r,n){for(var o=[],a=3;a<arguments.length;a++)o[a-3]=arguments[a];return r.call.apply(r,re([n],ee(o),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),oe=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},ae=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},z="context",ie=new ne,L=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return j(z,e,w.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var o,a=[],i=3;i<arguments.length;i++)a[i-3]=arguments[i];return(o=this._getContextManager()).with.apply(o,ae([e,r,n],oe(a),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return T(z)||ie},t.prototype.disable=function(){this._getContextManager().disable(),B(z,w.instance())},t}(),W;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(W||(W={}));var st="0000000000000000",ft="00000000000000000000000000000000",ue={traceId:ft,spanId:st,traceFlags:W.NONE},D=function(){function t(e){e===void 0&&(e=ue),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),H=ct("OpenTelemetry Context Key SPAN");function X(t){return t.getValue(H)||void 0}function ce(){return X(L.getInstance().active())}function J(t,e){return t.setValue(H,e)}function se(t){return t.deleteValue(H)}function fe(t,e){return J(t,new D(e))}function lt(t){var e;return(e=X(t))===null||e===void 0?void 0:e.spanContext()}var le=/^([0-9a-f]{32})$/i,pe=/^[0-9a-f]{16}$/i;function de(t){return le.test(t)&&t!==ft}function ge(t){return pe.test(t)&&t!==st}function pt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new D(t)}var K=L.getInstance(),dt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=K.active());var o=!!(r!=null&&r.root);if(o)return new D;var a=n&<(n);return he(a)&&pt(a)?new D(a):new D},t.prototype.startActiveSpan=function(e,r,n,o){var a,i,c;if(!(arguments.length<2)){arguments.length===2?c=r:arguments.length===3?(a=r,c=n):(a=r,i=n,c=o);var p=i??K.active(),g=this.startSpan(e,a,p),m=J(p,g);return K.with(m,c,void 0,g)}},t}();function he(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var ye=new dt,_e=function(){function t(e,r,n,o){this._provider=e,this.name=r,this.version=n,this.options=o}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,o){var a=this._getTracer();return Reflect.apply(a.startActiveSpan,a,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):ye},t}(),me=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new dt},t}(),be=new me,gt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var o;return(o=this.getDelegateTracer(e,r,n))!==null&&o!==void 0?o:new _e(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:be},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var o;return(o=this._delegate)===null||o===void 0?void 0:o.getTracer(e,r,n)},t}(),U;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(U||(U={}));var Y;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Y||(Y={})),L.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,Z="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return j(Z,e,w.instance())},t.prototype.getMeterProvider=function(){return T(Z)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){B(Z,w.instance())},t}();Ae.getInstance();var Ne=function(){function t(){}return t.prototype.inject=function(e,r){},t.prototype.extract=function(e,r){return e},t.prototype.fields=function(){return[]},t}(),tt=ct("OpenTelemetry Baggage Key");function vt(t){return t.getValue(tt)||void 0}function Pe(){return vt(L.getInstance().active())}function Ce(t,e){return t.setValue(tt,e)}function Te(t){return t.deleteValue(tt)}var et="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=vt,this.getActiveBaggage=Pe,this.setBaggage=Ce,this.deleteBaggage=Te}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalPropagator=function(e){return j(et,e,w.instance())},t.prototype.inject=function(e,r,n){return n===void 0&&(n=te),this._getGlobalPropagator().inject(e,r,n)},t.prototype.extract=function(e,r,n){return n===void 0&&(n=Zt),this._getGlobalPropagator().extract(e,r,n)},t.prototype.fields=function(){return this._getGlobalPropagator().fields()},t.prototype.disable=function(){B(et,w.instance())},t.prototype._getGlobalPropagator=function(){return T(et)||Oe},t}();Re.getInstance();var rt="trace",Ie=function(){function t(){this._proxyTracerProvider=new gt,this.wrapSpanContext=ve,this.isSpanContextValid=pt,this.deleteSpan=se,this.getSpan=X,this.getActiveSpan=ce,this.getSpanContext=lt,this.setSpan=J,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=j(rt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return T(rt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){B(rt,w.instance()),this._proxyTracerProvider=new gt},t}(),Me=Ie.getInstance();const nt="data: ",ht=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new v([],e);try{const n=JSON.parse(e),o=(r=n.detail)!=null&&r.length?n.detail.map(a=>a.msg).join(". "):e;throw new v(n.detail??[],o)}catch{throw new v([],e)}},Se=t=>ht(t).then(e=>e.json());function V(t,e,r,n={}){return t.startActiveSpan(e,n,o=>{const a=i=>{(i instanceof Error||typeof i=="string")&&o.recordException(i),o.setStatus({code:Y.ERROR}),o.end()};try{const i=r(o);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(c=>(o.end(),c),c=>{throw a(c),c}):(o.end(),i)}catch(i){throw a(i),i}})}const yt=t=>t==="http:"||t==="https:",ot=(...t)=>`/${t.map(r=>xe(r,"/")).filter(r=>r).join("/")}`,xe=(t,e)=>{const r=je(t,e);return Be(r,e)},je=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Be=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Ge=t=>{const e=Me.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!yt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const o={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},a=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},c=async u=>{const s=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(u!=null&&u.url)&&!s.url)throw new v([],"`ddn.url` is required");const l=new URL((u==null?void 0:u.url)||s.url);if(!yt(l.protocol))throw new Error(`invalid ddn.url protocol: ${l.protocol}`);return l.pathname.endsWith("/sql")||(l.pathname=ot(l.pathname,"v1/sql")),{url:l.toString(),headers:{...(s==null?void 0:s.headers)??{},...(u==null?void 0:u.headers)??{}}}},p=async(u,{llm:s,ai_primitives_llm:l,ddn:b,...d},N,E)=>{var P;if(!d.interactions.length||d.interactions.every(R=>!R.user_message.text))throw new v([],"require at least 1 interaction with non-empty user message content");s=s??t.llm,l=l??t.aiPrimitivesLlm;const y="v1",A=d.timezone||a;u.setAttributes({"promptql.request.interactions_length":d.interactions.length,"promptql.request.artifacts_length":((P=d.artifacts)==null?void 0:P.length)??0,"promptql.request.timezone":A,"promptql.request.version":y}),s!=null&&s.provider&&u.setAttribute("promptql.request.llm_provider",s.provider),l!=null&&l.provider&&u.setAttribute("promptql.request.ai_primitives_llm_provider",l.provider);const q=await c(b);u.setAttribute("promptql.request.ddn_url",q.url);const M=new URL(n);return M.pathname=ot(M.pathname,"query"),r(M,{...E,method:"POST",headers:{...o,...E==null?void 0:E.headers,"Content-Type":"application/json"},body:JSON.stringify({...d,llm:s,ai_primitives_llm:l,ddn:q,timezone:A,version:y,stream:N})}).then(ht)};return{query:(u,s)=>V(e,"PromptQL Query",l=>p(l,u,!1,s).then(b=>b.json()).then(b=>{const d=b;return l.setAttributes({"promptql.response.assistant_actions_length":d.assistant_actions.length,"promptql.response.modified_artifacts_length":d.modified_artifacts.length}),d}),{kind:U.CLIENT}),queryStream:async(u,s,l)=>V(e,"PromptQL Query Stream",async b=>{var M;const d=await p(b,u,!0,l);if(typeof s!="function"||!d.body)return d;const N=d.body.getReader(),E=new TextDecoder("utf-8");let y="",A=0;const q=async P=>{if(!P&&!y)return;let R=y+P;y="",R.startsWith(nt)&&(R=P.substring(nt.length)),R.split(nt).forEach(async(at,mt)=>{const k=at.trim();if(k[k.length-1]!=="}"){y+=mt>0?`data: ${at}`:at;return}let it;try{it=JSON.parse(k)}catch{y+=mt>0?`data: ${k}`:k}it&&await s(it)})};for(;!((M=l==null?void 0:l.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const Q=E.decode(P);Q&&(A+=Q.length,await q(Q))}return b.setAttribute("http.response.body.size",A),y&&await q(""),d},{kind:U.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:l,...b},d)=>V(e,"PromptQL Execute Program",async N=>{if(!b.code)throw new v([],"`code` is required");N.setAttribute("promptql.request.artifacts_length",(s==null?void 0:s.length)??0),u=u??i,u!=null&&u.provider&&N.setAttribute("promptql.request.ai_primitives_llm_provider",u.provider);const E=await c(l);N.setAttribute("promptql.request.ddn_url",E.url);const y=new URL(n);return y.pathname=ot(y.pathname,"execute_program"),r(y,{...d,headers:{...o,...d==null?void 0:d.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...b,ai_primitives_llm:u,ddn:E,artifacts:s??[]})}).then(Se).then(A=>(N.setAttributes({"promptql.response.accessed_artifact_ids":A.accessed_artifact_ids,"promptql.response.modified_artifacts_length":A.modified_artifacts.length}),A))},{kind:U.CLIENT})}},_t=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],o=(t==null?void 0:t.modified_artifacts.reduce((f,u)=>{const s=e(u);return Object.assign(f,{[s]:u})},{}))??{},a=Object.keys(o).sort(),i=()=>_t({assistant_actions:n.map(f=>({...f})),modified_artifacts:a.map(f=>{const u=o[f];switch(u==null?void 0:u.artifact_type){case"table":{const{data:s,...l}=o[f];return Object.assign(l,{data:[...s]})}case"visualization":{const{data:s,...l}=o[f];return Object.assign(l,{data:{...s}})}default:return{...u}}})}),c=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const u=e(f.artifact),s=o[u];if(!s){o[u]=f.artifact,a.push(u),a.sort();return}switch(s.artifact_type){case"text":s.data+=f.artifact.data;break;case"table":s.data.push(...f.artifact.data);break;case"visualization":s.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let s=n.length;s<=f.index;s++)n.push({});const u=n[f.index];for(const s of Ue)u[s]=De(u[s],f[s]);break}}},p=(...f)=>{const u=i();for(const s of f)u.push(s);return u},g=()=>a.map(f=>o[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:g,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:g()}),push:c,append:p}},De=(t,e)=>t?e?t+e:t:e,Ue=["code","code_error","code_output","message","plan"];return _.PromptQLError=v,_.createPromptQLClient=Ge,_.createQueryChunks=_t,_.withActiveSpan=V,Object.defineProperty(_,Symbol.toStringTag,{value:"Module"}),_}({});
|
|
1
|
+
var promptql=function(_){"use strict";var qe=Object.defineProperty;var ke=(_,v,I)=>v in _?qe(_,v,{enumerable:!0,configurable:!0,writable:!0,value:I}):_[v]=I;var bt=(_,v,I)=>ke(_,typeof v!="symbol"?v+"":v,I);class v extends Error{constructor(r,n){super(n||r.map(o=>o.msg).join(". "));bt(this,"detail",[]);this.detail=r}}var I=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},C="1.9.0",ut=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ut);if(!n)return function(){return!1};var o={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(o.prerelease!=null)return function(p){return p===t};function a(c){return r.add(c),!1}function i(c){return e.add(c),!0}return function(p){if(e.has(p))return!0;if(r.has(p))return!1;var g=p.match(ut);if(!g)return a(p);var m={major:+g[1],minor:+g[2],patch:+g[3],prerelease:g[4]};return m.prerelease!=null||o.major!==m.major?a(p):o.major===0?o.minor===m.minor&&o.patch<=m.patch?i(p):a(p):o.minor<=m.minor?i(p):a(p)}}var Et=wt(C),At=C.split(".")[0],S=Symbol.for("opentelemetry.js.api."+At),x=I;function j(t,e,r,n){var o;n===void 0&&(n=!1);var a=x[S]=(o=x[S])!==null&&o!==void 0?o:{version:C};if(!n&&a[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(a.version!==C){var i=new Error("@opentelemetry/api: Registration of version v"+a.version+" for "+t+" does not match previously registered API v"+C);return r.error(i.stack||i.message),!1}return a[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+C+"."),!0}function T(t){var e,r,n=(e=x[S])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=x[S])===null||r===void 0?void 0:r[t]}function B(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+C+".");var r=x[S];r&&delete r[t]}var Nt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},Ct=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("verbose",this._namespace,e)},t}();function G(t,e,r){var n=T("diag");if(n)return r.unshift(e),n[t].apply(n,Pt([],Nt(r),!1))}var h;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(h||(h={}));function Tt(t,e){t<h.NONE?t=h.NONE:t>h.ALL&&(t=h.ALL),e=e||{};function r(n,o){var a=e[n];return typeof a=="function"&&t>=o?a.bind(e):function(){}}return{error:r("error",h.ERROR),warn:r("warn",h.WARN),info:r("info",h.INFO),debug:r("debug",h.DEBUG),verbose:r("verbose",h.VERBOSE)}}var Ot=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Rt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},It="diag",w=function(){function t(){function e(o){return function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];var c=T("diag");if(c)return c[o].apply(c,Rt([],Ot(a),!1))}}var r=this,n=function(o,a){var i,c,p;if(a===void 0&&(a={logLevel:h.INFO}),o===r){var g=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=g.stack)!==null&&i!==void 0?i:g.message),!1}typeof a=="number"&&(a={logLevel:a});var m=T("diag"),f=Tt((c=a.logLevel)!==null&&c!==void 0?c:h.INFO,o);if(m&&!a.suppressOverrideMessage){var u=(p=new Error().stack)!==null&&p!==void 0?p:"<failed to generate stacktrace>";m.warn("Current logger will be overwritten from "+u),f.warn("Current logger will overwrite one already registered from "+u)}return j("diag",f,r,!0)};r.setLogger=n,r.disable=function(){B(It,r)},r.createComponentLogger=function(o){return new Ct(o)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}(),Mt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},St=function(t){var e=typeof Symbol=="function"&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")},xt=function(){function t(e){this._entries=e?new Map(e):new Map}return t.prototype.getEntry=function(e){var r=this._entries.get(e);if(r)return Object.assign({},r)},t.prototype.getAllEntries=function(){return Array.from(this._entries.entries()).map(function(e){var r=Mt(e,2),n=r[0],o=r[1];return[n,o]})},t.prototype.setEntry=function(e,r){var n=new t(this._entries);return n._entries.set(e,r),n},t.prototype.removeEntry=function(e){var r=new t(this._entries);return r._entries.delete(e),r},t.prototype.removeEntries=function(){for(var e,r,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var a=new t(this._entries);try{for(var i=St(n),c=i.next();!c.done;c=i.next()){var p=c.value;a._entries.delete(p)}}catch(g){e={error:g}}finally{try{c&&!c.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return a},t.prototype.clear=function(){return new t},t}();w.instance();function jt(t){return t===void 0&&(t={}),new xt(new Map(Object.entries(t)))}function ct(t){return Symbol.for(t)}var Bt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,o){var a=new t(r._currentContext);return a._currentContext.set(n,o),a},r.deleteValue=function(n){var o=new t(r._currentContext);return o._currentContext.delete(n),o}}return t}(),Gt=new Bt,O=function(){var t=function(e,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,o){n.__proto__=o}||function(n,o){for(var a in o)Object.prototype.hasOwnProperty.call(o,a)&&(n[a]=o[a])},t(e,r)};return function(e,r){if(typeof r!="function"&&r!==null)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");t(e,r);function n(){this.constructor=e}e.prototype=r===null?Object.create(r):(n.prototype=r.prototype,new n)}}(),Dt=function(){function t(){}return t.prototype.createGauge=function(e,r){return Wt},t.prototype.createHistogram=function(e,r){return Ht},t.prototype.createCounter=function(e,r){return zt},t.prototype.createUpDownCounter=function(e,r){return Xt},t.prototype.createObservableGauge=function(e,r){return Kt},t.prototype.createObservableCounter=function(e,r){return Jt},t.prototype.createObservableUpDownCounter=function(e,r){return Yt},t.prototype.addBatchObservableCallback=function(e,r){},t.prototype.removeBatchObservableCallback=function(e){},t}(),$=function(){function t(){}return t}(),Ut=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),F=function(){function t(){}return t.prototype.addCallback=function(e){},t.prototype.removeCallback=function(e){},t}(),Lt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Ft=new Dt,zt=new Ut,Wt=new kt,Ht=new $t,Xt=new qt,Jt=new Lt,Kt=new Vt,Yt=new Qt,Zt={get:function(t,e){if(t!=null)return t[e]},keys:function(t){return t==null?[]:Object.keys(t)}},te={set:function(t,e,r){t!=null&&(t[e]=r)}},ee=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},re=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},ne=function(){function t(){}return t.prototype.active=function(){return Gt},t.prototype.with=function(e,r,n){for(var o=[],a=3;a<arguments.length;a++)o[a-3]=arguments[a];return r.call.apply(r,re([n],ee(o),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),oe=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},ae=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},z="context",ie=new ne,L=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return j(z,e,w.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var o,a=[],i=3;i<arguments.length;i++)a[i-3]=arguments[i];return(o=this._getContextManager()).with.apply(o,ae([e,r,n],oe(a),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return T(z)||ie},t.prototype.disable=function(){this._getContextManager().disable(),B(z,w.instance())},t}(),W;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(W||(W={}));var st="0000000000000000",ft="00000000000000000000000000000000",ue={traceId:ft,spanId:st,traceFlags:W.NONE},D=function(){function t(e){e===void 0&&(e=ue),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),H=ct("OpenTelemetry Context Key SPAN");function X(t){return t.getValue(H)||void 0}function ce(){return X(L.getInstance().active())}function J(t,e){return t.setValue(H,e)}function se(t){return t.deleteValue(H)}function fe(t,e){return J(t,new D(e))}function lt(t){var e;return(e=X(t))===null||e===void 0?void 0:e.spanContext()}var le=/^([0-9a-f]{32})$/i,pe=/^[0-9a-f]{16}$/i;function de(t){return le.test(t)&&t!==ft}function ge(t){return pe.test(t)&&t!==st}function pt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new D(t)}var K=L.getInstance(),dt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=K.active());var o=!!(r!=null&&r.root);if(o)return new D;var a=n&<(n);return he(a)&&pt(a)?new D(a):new D},t.prototype.startActiveSpan=function(e,r,n,o){var a,i,c;if(!(arguments.length<2)){arguments.length===2?c=r:arguments.length===3?(a=r,c=n):(a=r,i=n,c=o);var p=i??K.active(),g=this.startSpan(e,a,p),m=J(p,g);return K.with(m,c,void 0,g)}},t}();function he(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var ye=new dt,_e=function(){function t(e,r,n,o){this._provider=e,this.name=r,this.version=n,this.options=o}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,o){var a=this._getTracer();return Reflect.apply(a.startActiveSpan,a,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):ye},t}(),me=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new dt},t}(),be=new me,gt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var o;return(o=this.getDelegateTracer(e,r,n))!==null&&o!==void 0?o:new _e(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:be},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var o;return(o=this._delegate)===null||o===void 0?void 0:o.getTracer(e,r,n)},t}(),U;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(U||(U={}));var Y;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Y||(Y={})),L.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,Z="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return j(Z,e,w.instance())},t.prototype.getMeterProvider=function(){return T(Z)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){B(Z,w.instance())},t}();Ae.getInstance();var Ne=function(){function t(){}return t.prototype.inject=function(e,r){},t.prototype.extract=function(e,r){return e},t.prototype.fields=function(){return[]},t}(),tt=ct("OpenTelemetry Baggage Key");function vt(t){return t.getValue(tt)||void 0}function Pe(){return vt(L.getInstance().active())}function Ce(t,e){return t.setValue(tt,e)}function Te(t){return t.deleteValue(tt)}var et="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=vt,this.getActiveBaggage=Pe,this.setBaggage=Ce,this.deleteBaggage=Te}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalPropagator=function(e){return j(et,e,w.instance())},t.prototype.inject=function(e,r,n){return n===void 0&&(n=te),this._getGlobalPropagator().inject(e,r,n)},t.prototype.extract=function(e,r,n){return n===void 0&&(n=Zt),this._getGlobalPropagator().extract(e,r,n)},t.prototype.fields=function(){return this._getGlobalPropagator().fields()},t.prototype.disable=function(){B(et,w.instance())},t.prototype._getGlobalPropagator=function(){return T(et)||Oe},t}();Re.getInstance();var rt="trace",Ie=function(){function t(){this._proxyTracerProvider=new gt,this.wrapSpanContext=ve,this.isSpanContextValid=pt,this.deleteSpan=se,this.getSpan=X,this.getActiveSpan=ce,this.getSpanContext=lt,this.setSpan=J,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=j(rt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return T(rt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){B(rt,w.instance()),this._proxyTracerProvider=new gt},t}(),Me=Ie.getInstance();const nt="data: ",ht=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new v([],e);try{const n=JSON.parse(e),o=(r=n.detail)!=null&&r.length?n.detail.map(a=>a.msg).join(". "):e;throw new v(n.detail??[],o)}catch{throw new v([],e)}},Se=t=>ht(t).then(e=>e.json());function V(t,e,r,n={}){return t.startActiveSpan(e,n,o=>{const a=i=>{(i instanceof Error||typeof i=="string")&&o.recordException(i),o.setStatus({code:Y.ERROR}),o.end()};try{const i=r(o);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(c=>(o.end(),c),c=>{throw a(c),c}):(o.end(),i)}catch(i){throw a(i),i}})}const yt=t=>t==="http:"||t==="https:",ot=(...t)=>`/${t.map(r=>xe(r,"/")).filter(r=>r).join("/")}`,xe=(t,e)=>{const r=je(t,e);return Be(r,e)},je=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Be=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Ge=t=>{const e=Me.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!yt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const o={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},a=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},c=async u=>{const s=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(u!=null&&u.url)&&!s.url)throw new v([],"`ddn.url` is required");const l=new URL((u==null?void 0:u.url)||s.url);if(!yt(l.protocol))throw new Error(`invalid ddn.url protocol: ${l.protocol}`);return l.pathname.endsWith("/sql")||(l.pathname=ot(l.pathname,"v1/sql")),{url:l.toString(),headers:{...(s==null?void 0:s.headers)??{},...(u==null?void 0:u.headers)??{}}}},p=async(u,{llm:s,ai_primitives_llm:l,ddn:b,...d},N,E)=>{var P;if(!d.interactions.length||d.interactions.every(R=>!R.user_message.text))throw new v([],"require at least 1 interaction with non-empty user message content");s=s??t.llm,l=l??t.aiPrimitivesLlm;const y="v1",A=d.timezone||a;u.setAttributes({"promptql.request.interactions_length":d.interactions.length,"promptql.request.artifacts_length":((P=d.artifacts)==null?void 0:P.length)??0,"promptql.request.timezone":A,"promptql.request.version":y}),s!=null&&s.provider&&u.setAttribute("promptql.request.llm_provider",s.provider),l!=null&&l.provider&&u.setAttribute("promptql.request.ai_primitives_llm_provider",l.provider);const q=await c(b);u.setAttribute("promptql.request.ddn_url",q.url);const M=new URL(n);return M.pathname=ot(M.pathname,"query"),r(M,{...E,method:"POST",headers:{...o,...E==null?void 0:E.headers,"Content-Type":"application/json"},body:JSON.stringify({system_instructions:t.systemInstructions,...d,llm:s,ai_primitives_llm:l,ddn:q,timezone:A,version:y,stream:N})}).then(ht)};return{query:(u,s)=>V(e,"PromptQL Query",l=>p(l,u,!1,s).then(b=>b.json()).then(b=>{const d=b;return l.setAttributes({"promptql.response.assistant_actions_length":d.assistant_actions.length,"promptql.response.modified_artifacts_length":d.modified_artifacts.length}),d}),{kind:U.CLIENT}),queryStream:async(u,s,l)=>V(e,"PromptQL Query Stream",async b=>{var M;const d=await p(b,u,!0,l);if(typeof s!="function"||!d.body)return d;const N=d.body.getReader(),E=new TextDecoder("utf-8");let y="",A=0;const q=async P=>{if(!P&&!y)return;let R=y+P;y="",R.startsWith(nt)&&(R=P.substring(nt.length)),R.split(nt).forEach(async(at,mt)=>{const k=at.trim();if(k[k.length-1]!=="}"){y+=mt>0?`data: ${at}`:at;return}let it;try{it=JSON.parse(k)}catch{y+=mt>0?`data: ${k}`:k}it&&await s(it)})};for(;!((M=l==null?void 0:l.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const Q=E.decode(P);Q&&(A+=Q.length,await q(Q))}return b.setAttribute("http.response.body.size",A),y&&await q(""),d},{kind:U.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:l,...b},d)=>V(e,"PromptQL Execute Program",async N=>{if(!b.code)throw new v([],"`code` is required");N.setAttribute("promptql.request.artifacts_length",(s==null?void 0:s.length)??0),u=u??i,u!=null&&u.provider&&N.setAttribute("promptql.request.ai_primitives_llm_provider",u.provider);const E=await c(l);N.setAttribute("promptql.request.ddn_url",E.url);const y=new URL(n);return y.pathname=ot(y.pathname,"execute_program"),r(y,{...d,headers:{...o,...d==null?void 0:d.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...b,ai_primitives_llm:u,ddn:E,artifacts:s??[]})}).then(Se).then(A=>(N.setAttributes({"promptql.response.accessed_artifact_ids":A.accessed_artifact_ids,"promptql.response.modified_artifacts_length":A.modified_artifacts.length}),A))},{kind:U.CLIENT})}},_t=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],o=(t==null?void 0:t.modified_artifacts.reduce((f,u)=>{const s=e(u);return Object.assign(f,{[s]:u})},{}))??{},a=Object.keys(o).sort(),i=()=>_t({assistant_actions:n.map(f=>({...f})),modified_artifacts:a.map(f=>{const u=o[f];switch(u==null?void 0:u.artifact_type){case"table":{const{data:s,...l}=o[f];return Object.assign(l,{data:[...s]})}case"visualization":{const{data:s,...l}=o[f];return Object.assign(l,{data:{...s}})}default:return{...u}}})}),c=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const u=e(f.artifact),s=o[u];if(!s){o[u]=f.artifact,a.push(u),a.sort();return}switch(s.artifact_type){case"text":s.data+=f.artifact.data;break;case"table":s.data.push(...f.artifact.data);break;case"visualization":s.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let s=n.length;s<=f.index;s++)n.push({});const u=n[f.index];for(const s of Ue)u[s]=De(u[s],f[s]);break}}},p=(...f)=>{const u=i();for(const s of f)u.push(s);return u},g=()=>a.map(f=>o[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:g,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:g()}),push:c,append:p}},De=(t,e)=>t?e?t+e:t:e,Ue=["code","code_error","code_output","message","plan"];return _.PromptQLError=v,_.createPromptQLClient=Ge,_.createQueryChunks=_t,_.withActiveSpan=V,Object.defineProperty(_,Symbol.toStringTag,{value:"Module"}),_}({});
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var lt=Object.defineProperty;var dt=(t,e,r)=>e in t?lt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var Z=(t,e,r)=>dt(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class T extends Error{constructor(r,n){super(n||r.map(a=>a.msg).join(". "));Z(this,"detail",[]);this.detail=r}}var pt=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},N="1.9.0",tt=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function ht(t){var e=new Set([t]),r=new Set,n=t.match(tt);if(!n)return function(){return!1};var a={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(a.prerelease!=null)return function(d){return d===t};function o(u){return r.add(u),!1}function i(u){return e.add(u),!0}return function(d){if(e.has(d))return!0;if(r.has(d))return!1;var h=d.match(tt);if(!h)return o(d);var y={major:+h[1],minor:+h[2],patch:+h[3],prerelease:h[4]};return y.prerelease!=null||a.major!==y.major?o(d):a.major===0?a.minor===y.minor&&a.patch<=y.patch?i(d):o(d):a.minor<=y.minor?i(d):o(d)}}var gt=ht(N),vt=N.split(".")[0],O=Symbol.for("opentelemetry.js.api."+vt),j=pt;function W(t,e,r,n){var a;n===void 0&&(n=!1);var o=j[O]=(a=j[O])!==null&&a!==void 0?a:{version:N};if(!n&&o[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(o.version!==N){var i=new Error("@opentelemetry/api: Registration of version v"+o.version+" for "+t+" does not match previously registered API v"+N);return r.error(i.stack||i.message),!1}return o[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+N+"."),!0}function q(t){var e,r,n=(e=j[O])===null||e===void 0?void 0:e.version;if(!(!n||!gt(n)))return(r=j[O])===null||r===void 0?void 0:r[t]}function X(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+N+".");var r=j[O];r&&delete r[t]}var yt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},mt=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},_t=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("verbose",this._namespace,e)},t}();function S(t,e,r){var n=q("diag");if(n)return r.unshift(e),n[t].apply(n,mt([],yt(r),!1))}var v;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(v||(v={}));function bt(t,e){t<v.NONE?t=v.NONE:t>v.ALL&&(t=v.ALL),e=e||{};function r(n,a){var o=e[n];return typeof o=="function"&&t>=a?o.bind(e):function(){}}return{error:r("error",v.ERROR),warn:r("warn",v.WARN),info:r("info",v.INFO),debug:r("debug",v.DEBUG),verbose:r("verbose",v.VERBOSE)}}var wt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},At=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},Et="diag",k=function(){function t(){function e(a){return function(){for(var o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];var u=q("diag");if(u)return u[a].apply(u,At([],wt(o),!1))}}var r=this,n=function(a,o){var i,u,d;if(o===void 0&&(o={logLevel:v.INFO}),a===r){var h=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=h.stack)!==null&&i!==void 0?i:h.message),!1}typeof o=="number"&&(o={logLevel:o});var y=q("diag"),f=bt((u=o.logLevel)!==null&&u!==void 0?u:v.INFO,a);if(y&&!o.suppressOverrideMessage){var s=(d=new Error().stack)!==null&&d!==void 0?d:"<failed to generate stacktrace>";y.warn("Current logger will be overwritten from "+s),f.warn("Current logger will overwrite one already registered from "+s)}return W("diag",f,r,!0)};r.setLogger=n,r.disable=function(){X(Et,r)},r.createComponentLogger=function(a){return new _t(a)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}();function Nt(t){return Symbol.for(t)}var Tt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,a){var o=new t(r._currentContext);return o._currentContext.set(n,a),o},r.deleteValue=function(n){var a=new t(r._currentContext);return a._currentContext.delete(n),a}}return t}(),Rt=new Tt,Ct=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},St=function(){function t(){}return t.prototype.active=function(){return Rt},t.prototype.with=function(e,r,n){for(var a=[],o=3;o<arguments.length;o++)a[o-3]=arguments[o];return r.call.apply(r,Pt([n],Ct(a),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),xt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},It=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},L="context",Ot=new St,nt=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return W(L,e,k.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var a,o=[],i=3;i<arguments.length;i++)o[i-3]=arguments[i];return(a=this._getContextManager()).with.apply(a,It([e,r,n],xt(o),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return q(L)||Ot},t.prototype.disable=function(){this._getContextManager().disable(),X(L,k.instance())},t}(),z;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(z||(z={}));var at="0000000000000000",ot="00000000000000000000000000000000",jt={traceId:ot,spanId:at,traceFlags:z.NONE},x=function(){function t(e){e===void 0&&(e=jt),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),J=Nt("OpenTelemetry Context Key SPAN");function H(t){return t.getValue(J)||void 0}function qt(){return H(nt.getInstance().active())}function K(t,e){return t.setValue(J,e)}function Dt(t){return t.deleteValue(J)}function Mt(t,e){return K(t,new x(e))}function it(t){var e;return(e=H(t))===null||e===void 0?void 0:e.spanContext()}var kt=/^([0-9a-f]{32})$/i,Ut=/^[0-9a-f]{16}$/i;function $t(t){return kt.test(t)&&t!==ot}function Lt(t){return Ut.test(t)&&t!==at}function st(t){return $t(t.traceId)&&Lt(t.spanId)}function Qt(t){return new x(t)}var Q=nt.getInstance(),ct=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=Q.active());var a=!!(r!=null&&r.root);if(a)return new x;var o=n&&it(n);return Gt(o)&&st(o)?new x(o):new x},t.prototype.startActiveSpan=function(e,r,n,a){var o,i,u;if(!(arguments.length<2)){arguments.length===2?u=r:arguments.length===3?(o=r,u=n):(o=r,i=n,u=a);var d=i??Q.active(),h=this.startSpan(e,o,d),y=K(d,h);return Q.with(y,u,void 0,h)}},t}();function Gt(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var Bt=new ct,Ft=function(){function t(e,r,n,a){this._provider=e,this.name=r,this.version=n,this.options=a}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,a){var o=this._getTracer();return Reflect.apply(o.startActiveSpan,o,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):Bt},t}(),zt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new ct},t}(),Vt=new zt,et=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var a;return(a=this.getDelegateTracer(e,r,n))!==null&&a!==void 0?a:new Ft(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:Vt},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var a;return(a=this._delegate)===null||a===void 0?void 0:a.getTracer(e,r,n)},t}(),I;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(I||(I={}));var V;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(V||(V={}));var G="trace",Wt=function(){function t(){this._proxyTracerProvider=new et,this.wrapSpanContext=Qt,this.isSpanContextValid=st,this.deleteSpan=Dt,this.getSpan=H,this.getActiveSpan=qt,this.getSpanContext=it,this.setSpan=K,this.setSpanContext=Mt}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=W(G,this._proxyTracerProvider,k.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return q(G)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){X(G,k.instance()),this._proxyTracerProvider=new et},t}(),Xt=Wt.getInstance();const B="data: ",ut=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new T([],e);try{const n=JSON.parse(e),a=(r=n.detail)!=null&&r.length?n.detail.map(o=>o.msg).join(". "):e;throw new T(n.detail??[],a)}catch{throw new T([],e)}},Jt=t=>ut(t).then(e=>e.json());function M(t,e,r,n={}){return t.startActiveSpan(e,n,a=>{const o=i=>{(i instanceof Error||typeof i=="string")&&a.recordException(i),a.setStatus({code:V.ERROR}),a.end()};try{const i=r(a);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(u=>(a.end(),u),u=>{throw o(u),u}):(a.end(),i)}catch(i){throw o(i),i}})}const rt=t=>t==="http:"||t==="https:",F=(...t)=>`/${t.map(r=>Ht(r,"/")).filter(r=>r).join("/")}`,Ht=(t,e)=>{const r=Kt(t,e);return Yt(r,e)},Kt=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Yt=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Zt=t=>{const e=Xt.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!rt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const a={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},o=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},u=async s=>{const c=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(s!=null&&s.url)&&!c.url)throw new T([],"`ddn.url` is required");const l=new URL((s==null?void 0:s.url)||c.url);if(!rt(l.protocol))throw new Error(`invalid ddn.url protocol: ${l.protocol}`);return l.pathname.endsWith("/sql")||(l.pathname=F(l.pathname,"v1/sql")),{url:l.toString(),headers:{...(c==null?void 0:c.headers)??{},...(s==null?void 0:s.headers)??{}}}},d=async(s,{llm:c,ai_primitives_llm:l,ddn:m,...p},w,_)=>{var A;if(!p.interactions.length||p.interactions.every(E=>!E.user_message.text))throw new T([],"require at least 1 interaction with non-empty user message content");c=c??t.llm,l=l??t.aiPrimitivesLlm;const g="v1",b=p.timezone||o;s.setAttributes({"promptql.request.interactions_length":p.interactions.length,"promptql.request.artifacts_length":((A=p.artifacts)==null?void 0:A.length)??0,"promptql.request.timezone":b,"promptql.request.version":g}),c!=null&&c.provider&&s.setAttribute("promptql.request.llm_provider",c.provider),l!=null&&l.provider&&s.setAttribute("promptql.request.ai_primitives_llm_provider",l.provider);const C=await u(m);s.setAttribute("promptql.request.ddn_url",C.url);const R=new URL(n);return R.pathname=F(R.pathname,"query"),r(R,{..._,method:"POST",headers:{...a,..._==null?void 0:_.headers,"Content-Type":"application/json"},body:JSON.stringify({...p,llm:c,ai_primitives_llm:l,ddn:C,timezone:b,version:g,stream:w})}).then(ut)};return{query:(s,c)=>M(e,"PromptQL Query",l=>d(l,s,!1,c).then(m=>m.json()).then(m=>{const p=m;return l.setAttributes({"promptql.response.assistant_actions_length":p.assistant_actions.length,"promptql.response.modified_artifacts_length":p.modified_artifacts.length}),p}),{kind:I.CLIENT}),queryStream:async(s,c,l)=>M(e,"PromptQL Query Stream",async m=>{var R;const p=await d(m,s,!0,l);if(typeof c!="function"||!p.body)return p;const w=p.body.getReader(),_=new TextDecoder("utf-8");let g="",b=0;const C=async A=>{if(!A&&!g)return;let E=g+A;g="",E.startsWith(B)&&(E=A.substring(B.length)),E.split(B).forEach(async(U,Y)=>{const P=U.trim();if(P[P.length-1]!=="}"){g+=Y>0?`data: ${U}`:U;return}let $;try{$=JSON.parse(P)}catch{g+=Y>0?`data: ${P}`:P}$&&await c($)})};for(;!((R=l==null?void 0:l.signal)!=null&&R.aborted);){const{value:A,done:E}=await w.read();if(E)break;const D=_.decode(A);D&&(b+=D.length,await C(D))}return m.setAttribute("http.response.body.size",b),g&&await C(""),p},{kind:I.CLIENT}),executeProgram:async({ai_primitives_llm:s,artifacts:c,ddn:l,...m},p)=>M(e,"PromptQL Execute Program",async w=>{if(!m.code)throw new T([],"`code` is required");w.setAttribute("promptql.request.artifacts_length",(c==null?void 0:c.length)??0),s=s??i,s!=null&&s.provider&&w.setAttribute("promptql.request.ai_primitives_llm_provider",s.provider);const _=await u(l);w.setAttribute("promptql.request.ddn_url",_.url);const g=new URL(n);return g.pathname=F(g.pathname,"execute_program"),r(g,{...p,headers:{...a,...p==null?void 0:p.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...m,ai_primitives_llm:s,ddn:_,artifacts:c??[]})}).then(Jt).then(b=>(w.setAttributes({"promptql.response.accessed_artifact_ids":b.accessed_artifact_ids,"promptql.response.modified_artifacts_length":b.modified_artifacts.length}),b))},{kind:I.CLIENT})}},ft=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],a=(t==null?void 0:t.modified_artifacts.reduce((f,s)=>{const c=e(s);return Object.assign(f,{[c]:s})},{}))??{},o=Object.keys(a).sort(),i=()=>ft({assistant_actions:n.map(f=>({...f})),modified_artifacts:o.map(f=>{const s=a[f];switch(s==null?void 0:s.artifact_type){case"table":{const{data:c,...l}=a[f];return Object.assign(l,{data:[...c]})}case"visualization":{const{data:c,...l}=a[f];return Object.assign(l,{data:{...c}})}default:return{...s}}})}),u=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const s=e(f.artifact),c=a[s];if(!c){a[s]=f.artifact,o.push(s),o.sort();return}switch(c.artifact_type){case"text":c.data+=f.artifact.data;break;case"table":c.data.push(...f.artifact.data);break;case"visualization":c.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let c=n.length;c<=f.index;c++)n.push({});const s=n[f.index];for(const c of ee)s[c]=te(s[c],f[c]);break}}},d=(...f)=>{const s=i();for(const c of f)s.push(c);return s},h=()=>o.map(f=>a[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:h,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:h()}),push:u,append:d}},te=(t,e)=>t?e?t+e:t:e,ee=["code","code_error","code_output","message","plan"];exports.PromptQLError=T;exports.createPromptQLClient=Zt;exports.createQueryChunks=ft;exports.withActiveSpan=M;
|
|
1
|
+
"use strict";var lt=Object.defineProperty;var dt=(t,e,r)=>e in t?lt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var Z=(t,e,r)=>dt(t,typeof e!="symbol"?e+"":e,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class T extends Error{constructor(r,n){super(n||r.map(a=>a.msg).join(". "));Z(this,"detail",[]);this.detail=r}}var pt=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},N="1.9.0",tt=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function ht(t){var e=new Set([t]),r=new Set,n=t.match(tt);if(!n)return function(){return!1};var a={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(a.prerelease!=null)return function(d){return d===t};function o(u){return r.add(u),!1}function i(u){return e.add(u),!0}return function(d){if(e.has(d))return!0;if(r.has(d))return!1;var h=d.match(tt);if(!h)return o(d);var y={major:+h[1],minor:+h[2],patch:+h[3],prerelease:h[4]};return y.prerelease!=null||a.major!==y.major?o(d):a.major===0?a.minor===y.minor&&a.patch<=y.patch?i(d):o(d):a.minor<=y.minor?i(d):o(d)}}var gt=ht(N),vt=N.split(".")[0],O=Symbol.for("opentelemetry.js.api."+vt),j=pt;function W(t,e,r,n){var a;n===void 0&&(n=!1);var o=j[O]=(a=j[O])!==null&&a!==void 0?a:{version:N};if(!n&&o[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(o.version!==N){var i=new Error("@opentelemetry/api: Registration of version v"+o.version+" for "+t+" does not match previously registered API v"+N);return r.error(i.stack||i.message),!1}return o[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+N+"."),!0}function q(t){var e,r,n=(e=j[O])===null||e===void 0?void 0:e.version;if(!(!n||!gt(n)))return(r=j[O])===null||r===void 0?void 0:r[t]}function X(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+N+".");var r=j[O];r&&delete r[t]}var yt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},mt=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},_t=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return S("verbose",this._namespace,e)},t}();function S(t,e,r){var n=q("diag");if(n)return r.unshift(e),n[t].apply(n,mt([],yt(r),!1))}var v;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(v||(v={}));function bt(t,e){t<v.NONE?t=v.NONE:t>v.ALL&&(t=v.ALL),e=e||{};function r(n,a){var o=e[n];return typeof o=="function"&&t>=a?o.bind(e):function(){}}return{error:r("error",v.ERROR),warn:r("warn",v.WARN),info:r("info",v.INFO),debug:r("debug",v.DEBUG),verbose:r("verbose",v.VERBOSE)}}var wt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},At=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},Et="diag",k=function(){function t(){function e(a){return function(){for(var o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];var u=q("diag");if(u)return u[a].apply(u,At([],wt(o),!1))}}var r=this,n=function(a,o){var i,u,d;if(o===void 0&&(o={logLevel:v.INFO}),a===r){var h=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=h.stack)!==null&&i!==void 0?i:h.message),!1}typeof o=="number"&&(o={logLevel:o});var y=q("diag"),f=bt((u=o.logLevel)!==null&&u!==void 0?u:v.INFO,a);if(y&&!o.suppressOverrideMessage){var s=(d=new Error().stack)!==null&&d!==void 0?d:"<failed to generate stacktrace>";y.warn("Current logger will be overwritten from "+s),f.warn("Current logger will overwrite one already registered from "+s)}return W("diag",f,r,!0)};r.setLogger=n,r.disable=function(){X(Et,r)},r.createComponentLogger=function(a){return new _t(a)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}();function Nt(t){return Symbol.for(t)}var Tt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,a){var o=new t(r._currentContext);return o._currentContext.set(n,a),o},r.deleteValue=function(n){var a=new t(r._currentContext);return a._currentContext.delete(n),a}}return t}(),Rt=new Tt,Ct=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},St=function(){function t(){}return t.prototype.active=function(){return Rt},t.prototype.with=function(e,r,n){for(var a=[],o=3;o<arguments.length;o++)a[o-3]=arguments[o];return r.call.apply(r,Pt([n],Ct(a),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),xt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),a,o=[],i;try{for(;(e===void 0||e-- >0)&&!(a=n.next()).done;)o.push(a.value)}catch(u){i={error:u}}finally{try{a&&!a.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return o},It=function(t,e,r){if(r||arguments.length===2)for(var n=0,a=e.length,o;n<a;n++)(o||!(n in e))&&(o||(o=Array.prototype.slice.call(e,0,n)),o[n]=e[n]);return t.concat(o||Array.prototype.slice.call(e))},L="context",Ot=new St,nt=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return W(L,e,k.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var a,o=[],i=3;i<arguments.length;i++)o[i-3]=arguments[i];return(a=this._getContextManager()).with.apply(a,It([e,r,n],xt(o),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return q(L)||Ot},t.prototype.disable=function(){this._getContextManager().disable(),X(L,k.instance())},t}(),z;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(z||(z={}));var at="0000000000000000",ot="00000000000000000000000000000000",jt={traceId:ot,spanId:at,traceFlags:z.NONE},x=function(){function t(e){e===void 0&&(e=jt),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),J=Nt("OpenTelemetry Context Key SPAN");function H(t){return t.getValue(J)||void 0}function qt(){return H(nt.getInstance().active())}function K(t,e){return t.setValue(J,e)}function Dt(t){return t.deleteValue(J)}function Mt(t,e){return K(t,new x(e))}function it(t){var e;return(e=H(t))===null||e===void 0?void 0:e.spanContext()}var kt=/^([0-9a-f]{32})$/i,Ut=/^[0-9a-f]{16}$/i;function $t(t){return kt.test(t)&&t!==ot}function Lt(t){return Ut.test(t)&&t!==at}function st(t){return $t(t.traceId)&&Lt(t.spanId)}function Qt(t){return new x(t)}var Q=nt.getInstance(),ct=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=Q.active());var a=!!(r!=null&&r.root);if(a)return new x;var o=n&&it(n);return Gt(o)&&st(o)?new x(o):new x},t.prototype.startActiveSpan=function(e,r,n,a){var o,i,u;if(!(arguments.length<2)){arguments.length===2?u=r:arguments.length===3?(o=r,u=n):(o=r,i=n,u=a);var d=i??Q.active(),h=this.startSpan(e,o,d),y=K(d,h);return Q.with(y,u,void 0,h)}},t}();function Gt(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var Bt=new ct,Ft=function(){function t(e,r,n,a){this._provider=e,this.name=r,this.version=n,this.options=a}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,a){var o=this._getTracer();return Reflect.apply(o.startActiveSpan,o,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):Bt},t}(),zt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new ct},t}(),Vt=new zt,et=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var a;return(a=this.getDelegateTracer(e,r,n))!==null&&a!==void 0?a:new Ft(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:Vt},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var a;return(a=this._delegate)===null||a===void 0?void 0:a.getTracer(e,r,n)},t}(),I;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(I||(I={}));var V;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(V||(V={}));var G="trace",Wt=function(){function t(){this._proxyTracerProvider=new et,this.wrapSpanContext=Qt,this.isSpanContextValid=st,this.deleteSpan=Dt,this.getSpan=H,this.getActiveSpan=qt,this.getSpanContext=it,this.setSpan=K,this.setSpanContext=Mt}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=W(G,this._proxyTracerProvider,k.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return q(G)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){X(G,k.instance()),this._proxyTracerProvider=new et},t}(),Xt=Wt.getInstance();const B="data: ",ut=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new T([],e);try{const n=JSON.parse(e),a=(r=n.detail)!=null&&r.length?n.detail.map(o=>o.msg).join(". "):e;throw new T(n.detail??[],a)}catch{throw new T([],e)}},Jt=t=>ut(t).then(e=>e.json());function M(t,e,r,n={}){return t.startActiveSpan(e,n,a=>{const o=i=>{(i instanceof Error||typeof i=="string")&&a.recordException(i),a.setStatus({code:V.ERROR}),a.end()};try{const i=r(a);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(u=>(a.end(),u),u=>{throw o(u),u}):(a.end(),i)}catch(i){throw o(i),i}})}const rt=t=>t==="http:"||t==="https:",F=(...t)=>`/${t.map(r=>Ht(r,"/")).filter(r=>r).join("/")}`,Ht=(t,e)=>{const r=Kt(t,e);return Yt(r,e)},Kt=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Yt=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Zt=t=>{const e=Xt.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!rt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const a={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},o=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},u=async s=>{const c=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(s!=null&&s.url)&&!c.url)throw new T([],"`ddn.url` is required");const l=new URL((s==null?void 0:s.url)||c.url);if(!rt(l.protocol))throw new Error(`invalid ddn.url protocol: ${l.protocol}`);return l.pathname.endsWith("/sql")||(l.pathname=F(l.pathname,"v1/sql")),{url:l.toString(),headers:{...(c==null?void 0:c.headers)??{},...(s==null?void 0:s.headers)??{}}}},d=async(s,{llm:c,ai_primitives_llm:l,ddn:m,...p},w,_)=>{var A;if(!p.interactions.length||p.interactions.every(E=>!E.user_message.text))throw new T([],"require at least 1 interaction with non-empty user message content");c=c??t.llm,l=l??t.aiPrimitivesLlm;const g="v1",b=p.timezone||o;s.setAttributes({"promptql.request.interactions_length":p.interactions.length,"promptql.request.artifacts_length":((A=p.artifacts)==null?void 0:A.length)??0,"promptql.request.timezone":b,"promptql.request.version":g}),c!=null&&c.provider&&s.setAttribute("promptql.request.llm_provider",c.provider),l!=null&&l.provider&&s.setAttribute("promptql.request.ai_primitives_llm_provider",l.provider);const C=await u(m);s.setAttribute("promptql.request.ddn_url",C.url);const R=new URL(n);return R.pathname=F(R.pathname,"query"),r(R,{..._,method:"POST",headers:{...a,..._==null?void 0:_.headers,"Content-Type":"application/json"},body:JSON.stringify({system_instructions:t.systemInstructions,...p,llm:c,ai_primitives_llm:l,ddn:C,timezone:b,version:g,stream:w})}).then(ut)};return{query:(s,c)=>M(e,"PromptQL Query",l=>d(l,s,!1,c).then(m=>m.json()).then(m=>{const p=m;return l.setAttributes({"promptql.response.assistant_actions_length":p.assistant_actions.length,"promptql.response.modified_artifacts_length":p.modified_artifacts.length}),p}),{kind:I.CLIENT}),queryStream:async(s,c,l)=>M(e,"PromptQL Query Stream",async m=>{var R;const p=await d(m,s,!0,l);if(typeof c!="function"||!p.body)return p;const w=p.body.getReader(),_=new TextDecoder("utf-8");let g="",b=0;const C=async A=>{if(!A&&!g)return;let E=g+A;g="",E.startsWith(B)&&(E=A.substring(B.length)),E.split(B).forEach(async(U,Y)=>{const P=U.trim();if(P[P.length-1]!=="}"){g+=Y>0?`data: ${U}`:U;return}let $;try{$=JSON.parse(P)}catch{g+=Y>0?`data: ${P}`:P}$&&await c($)})};for(;!((R=l==null?void 0:l.signal)!=null&&R.aborted);){const{value:A,done:E}=await w.read();if(E)break;const D=_.decode(A);D&&(b+=D.length,await C(D))}return m.setAttribute("http.response.body.size",b),g&&await C(""),p},{kind:I.CLIENT}),executeProgram:async({ai_primitives_llm:s,artifacts:c,ddn:l,...m},p)=>M(e,"PromptQL Execute Program",async w=>{if(!m.code)throw new T([],"`code` is required");w.setAttribute("promptql.request.artifacts_length",(c==null?void 0:c.length)??0),s=s??i,s!=null&&s.provider&&w.setAttribute("promptql.request.ai_primitives_llm_provider",s.provider);const _=await u(l);w.setAttribute("promptql.request.ddn_url",_.url);const g=new URL(n);return g.pathname=F(g.pathname,"execute_program"),r(g,{...p,headers:{...a,...p==null?void 0:p.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...m,ai_primitives_llm:s,ddn:_,artifacts:c??[]})}).then(Jt).then(b=>(w.setAttributes({"promptql.response.accessed_artifact_ids":b.accessed_artifact_ids,"promptql.response.modified_artifacts_length":b.modified_artifacts.length}),b))},{kind:I.CLIENT})}},ft=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],a=(t==null?void 0:t.modified_artifacts.reduce((f,s)=>{const c=e(s);return Object.assign(f,{[c]:s})},{}))??{},o=Object.keys(a).sort(),i=()=>ft({assistant_actions:n.map(f=>({...f})),modified_artifacts:o.map(f=>{const s=a[f];switch(s==null?void 0:s.artifact_type){case"table":{const{data:c,...l}=a[f];return Object.assign(l,{data:[...c]})}case"visualization":{const{data:c,...l}=a[f];return Object.assign(l,{data:{...c}})}default:return{...s}}})}),u=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const s=e(f.artifact),c=a[s];if(!c){a[s]=f.artifact,o.push(s),o.sort();return}switch(c.artifact_type){case"text":c.data+=f.artifact.data;break;case"table":c.data.push(...f.artifact.data);break;case"visualization":c.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let c=n.length;c<=f.index;c++)n.push({});const s=n[f.index];for(const c of ee)s[c]=te(s[c],f[c]);break}}},d=(...f)=>{const s=i();for(const c of f)s.push(c);return s},h=()=>o.map(f=>a[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:h,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:h()}),push:u,append:d}},te=(t,e)=>t?e?t+e:t:e,ee=["code","code_error","code_output","message","plan"];exports.PromptQLError=T;exports.createPromptQLClient=Zt;exports.createQueryChunks=ft;exports.withActiveSpan=M;
|
package/dist/index.mjs
CHANGED
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(h,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(h=typeof globalThis<"u"?globalThis:h||self,g(h.promptql={}))})(this,function(h){"use strict";var qe=Object.defineProperty;var ke=(h,g,I)=>g in h?qe(h,g,{enumerable:!0,configurable:!0,writable:!0,value:I}):h[g]=I;var bt=(h,g,I)=>ke(h,typeof g!="symbol"?g+"":g,I);class g extends Error{constructor(r,n){super(n||r.map(o=>o.msg).join(". "));bt(this,"detail",[]);this.detail=r}}var I=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},C="1.9.0",ut=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ut);if(!n)return function(){return!1};var o={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(o.prerelease!=null)return function(l){return l===t};function a(c){return r.add(c),!1}function i(c){return e.add(c),!0}return function(l){if(e.has(l))return!0;if(r.has(l))return!1;var v=l.match(ut);if(!v)return a(l);var m={major:+v[1],minor:+v[2],patch:+v[3],prerelease:v[4]};return m.prerelease!=null||o.major!==m.major?a(l):o.major===0?o.minor===m.minor&&o.patch<=m.patch?i(l):a(l):o.minor<=m.minor?i(l):a(l)}}var Et=wt(C),At=C.split(".")[0],S=Symbol.for("opentelemetry.js.api."+At),x=I;function j(t,e,r,n){var o;n===void 0&&(n=!1);var a=x[S]=(o=x[S])!==null&&o!==void 0?o:{version:C};if(!n&&a[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(a.version!==C){var i=new Error("@opentelemetry/api: Registration of version v"+a.version+" for "+t+" does not match previously registered API v"+C);return r.error(i.stack||i.message),!1}return a[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+C+"."),!0}function T(t){var e,r,n=(e=x[S])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=x[S])===null||r===void 0?void 0:r[t]}function B(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+C+".");var r=x[S];r&&delete r[t]}var Nt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},Ct=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("verbose",this._namespace,e)},t}();function G(t,e,r){var n=T("diag");if(n)return r.unshift(e),n[t].apply(n,Pt([],Nt(r),!1))}var y;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(y||(y={}));function Tt(t,e){t<y.NONE?t=y.NONE:t>y.ALL&&(t=y.ALL),e=e||{};function r(n,o){var a=e[n];return typeof a=="function"&&t>=o?a.bind(e):function(){}}return{error:r("error",y.ERROR),warn:r("warn",y.WARN),info:r("info",y.INFO),debug:r("debug",y.DEBUG),verbose:r("verbose",y.VERBOSE)}}var Ot=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Rt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},It="diag",w=function(){function t(){function e(o){return function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];var c=T("diag");if(c)return c[o].apply(c,Rt([],Ot(a),!1))}}var r=this,n=function(o,a){var i,c,l;if(a===void 0&&(a={logLevel:y.INFO}),o===r){var v=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=v.stack)!==null&&i!==void 0?i:v.message),!1}typeof a=="number"&&(a={logLevel:a});var m=T("diag"),f=Tt((c=a.logLevel)!==null&&c!==void 0?c:y.INFO,o);if(m&&!a.suppressOverrideMessage){var u=(l=new Error().stack)!==null&&l!==void 0?l:"<failed to generate stacktrace>";m.warn("Current logger will be overwritten from "+u),f.warn("Current logger will overwrite one already registered from "+u)}return j("diag",f,r,!0)};r.setLogger=n,r.disable=function(){B(It,r)},r.createComponentLogger=function(o){return new Ct(o)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}(),Mt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},St=function(t){var e=typeof Symbol=="function"&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")},xt=function(){function t(e){this._entries=e?new Map(e):new Map}return t.prototype.getEntry=function(e){var r=this._entries.get(e);if(r)return Object.assign({},r)},t.prototype.getAllEntries=function(){return Array.from(this._entries.entries()).map(function(e){var r=Mt(e,2),n=r[0],o=r[1];return[n,o]})},t.prototype.setEntry=function(e,r){var n=new t(this._entries);return n._entries.set(e,r),n},t.prototype.removeEntry=function(e){var r=new t(this._entries);return r._entries.delete(e),r},t.prototype.removeEntries=function(){for(var e,r,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var a=new t(this._entries);try{for(var i=St(n),c=i.next();!c.done;c=i.next()){var l=c.value;a._entries.delete(l)}}catch(v){e={error:v}}finally{try{c&&!c.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return a},t.prototype.clear=function(){return new t},t}();w.instance();function jt(t){return t===void 0&&(t={}),new xt(new Map(Object.entries(t)))}function ct(t){return Symbol.for(t)}var Bt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,o){var a=new t(r._currentContext);return a._currentContext.set(n,o),a},r.deleteValue=function(n){var o=new t(r._currentContext);return o._currentContext.delete(n),o}}return t}(),Gt=new Bt,O=function(){var t=function(e,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,o){n.__proto__=o}||function(n,o){for(var a in o)Object.prototype.hasOwnProperty.call(o,a)&&(n[a]=o[a])},t(e,r)};return function(e,r){if(typeof r!="function"&&r!==null)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");t(e,r);function n(){this.constructor=e}e.prototype=r===null?Object.create(r):(n.prototype=r.prototype,new n)}}(),Dt=function(){function t(){}return t.prototype.createGauge=function(e,r){return Wt},t.prototype.createHistogram=function(e,r){return Ht},t.prototype.createCounter=function(e,r){return zt},t.prototype.createUpDownCounter=function(e,r){return Xt},t.prototype.createObservableGauge=function(e,r){return Kt},t.prototype.createObservableCounter=function(e,r){return Jt},t.prototype.createObservableUpDownCounter=function(e,r){return Yt},t.prototype.addBatchObservableCallback=function(e,r){},t.prototype.removeBatchObservableCallback=function(e){},t}(),$=function(){function t(){}return t}(),Ut=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),F=function(){function t(){}return t.prototype.addCallback=function(e){},t.prototype.removeCallback=function(e){},t}(),Lt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Ft=new Dt,zt=new Ut,Wt=new kt,Ht=new $t,Xt=new qt,Jt=new Lt,Kt=new Vt,Yt=new Qt,Zt={get:function(t,e){if(t!=null)return t[e]},keys:function(t){return t==null?[]:Object.keys(t)}},te={set:function(t,e,r){t!=null&&(t[e]=r)}},ee=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},re=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},ne=function(){function t(){}return t.prototype.active=function(){return Gt},t.prototype.with=function(e,r,n){for(var o=[],a=3;a<arguments.length;a++)o[a-3]=arguments[a];return r.call.apply(r,re([n],ee(o),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),oe=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},ae=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},z="context",ie=new ne,L=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return j(z,e,w.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var o,a=[],i=3;i<arguments.length;i++)a[i-3]=arguments[i];return(o=this._getContextManager()).with.apply(o,ae([e,r,n],oe(a),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return T(z)||ie},t.prototype.disable=function(){this._getContextManager().disable(),B(z,w.instance())},t}(),W;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(W||(W={}));var st="0000000000000000",ft="00000000000000000000000000000000",ue={traceId:ft,spanId:st,traceFlags:W.NONE},D=function(){function t(e){e===void 0&&(e=ue),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),H=ct("OpenTelemetry Context Key SPAN");function X(t){return t.getValue(H)||void 0}function ce(){return X(L.getInstance().active())}function J(t,e){return t.setValue(H,e)}function se(t){return t.deleteValue(H)}function fe(t,e){return J(t,new D(e))}function pt(t){var e;return(e=X(t))===null||e===void 0?void 0:e.spanContext()}var pe=/^([0-9a-f]{32})$/i,le=/^[0-9a-f]{16}$/i;function de(t){return pe.test(t)&&t!==ft}function ge(t){return le.test(t)&&t!==st}function lt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new D(t)}var K=L.getInstance(),dt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=K.active());var o=!!(r!=null&&r.root);if(o)return new D;var a=n&&pt(n);return he(a)&<(a)?new D(a):new D},t.prototype.startActiveSpan=function(e,r,n,o){var a,i,c;if(!(arguments.length<2)){arguments.length===2?c=r:arguments.length===3?(a=r,c=n):(a=r,i=n,c=o);var l=i??K.active(),v=this.startSpan(e,a,l),m=J(l,v);return K.with(m,c,void 0,v)}},t}();function he(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var ye=new dt,_e=function(){function t(e,r,n,o){this._provider=e,this.name=r,this.version=n,this.options=o}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,o){var a=this._getTracer();return Reflect.apply(a.startActiveSpan,a,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):ye},t}(),me=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new dt},t}(),be=new me,gt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var o;return(o=this.getDelegateTracer(e,r,n))!==null&&o!==void 0?o:new _e(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:be},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var o;return(o=this._delegate)===null||o===void 0?void 0:o.getTracer(e,r,n)},t}(),U;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(U||(U={}));var Y;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Y||(Y={})),L.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,Z="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return j(Z,e,w.instance())},t.prototype.getMeterProvider=function(){return T(Z)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){B(Z,w.instance())},t}();Ae.getInstance();var Ne=function(){function t(){}return t.prototype.inject=function(e,r){},t.prototype.extract=function(e,r){return e},t.prototype.fields=function(){return[]},t}(),tt=ct("OpenTelemetry Baggage Key");function vt(t){return t.getValue(tt)||void 0}function Pe(){return vt(L.getInstance().active())}function Ce(t,e){return t.setValue(tt,e)}function Te(t){return t.deleteValue(tt)}var et="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=vt,this.getActiveBaggage=Pe,this.setBaggage=Ce,this.deleteBaggage=Te}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalPropagator=function(e){return j(et,e,w.instance())},t.prototype.inject=function(e,r,n){return n===void 0&&(n=te),this._getGlobalPropagator().inject(e,r,n)},t.prototype.extract=function(e,r,n){return n===void 0&&(n=Zt),this._getGlobalPropagator().extract(e,r,n)},t.prototype.fields=function(){return this._getGlobalPropagator().fields()},t.prototype.disable=function(){B(et,w.instance())},t.prototype._getGlobalPropagator=function(){return T(et)||Oe},t}();Re.getInstance();var rt="trace",Ie=function(){function t(){this._proxyTracerProvider=new gt,this.wrapSpanContext=ve,this.isSpanContextValid=lt,this.deleteSpan=se,this.getSpan=X,this.getActiveSpan=ce,this.getSpanContext=pt,this.setSpan=J,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=j(rt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return T(rt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){B(rt,w.instance()),this._proxyTracerProvider=new gt},t}(),Me=Ie.getInstance();const nt="data: ",ht=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new g([],e);try{const n=JSON.parse(e),o=(r=n.detail)!=null&&r.length?n.detail.map(a=>a.msg).join(". "):e;throw new g(n.detail??[],o)}catch{throw new g([],e)}},Se=t=>ht(t).then(e=>e.json());function V(t,e,r,n={}){return t.startActiveSpan(e,n,o=>{const a=i=>{(i instanceof Error||typeof i=="string")&&o.recordException(i),o.setStatus({code:Y.ERROR}),o.end()};try{const i=r(o);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(c=>(o.end(),c),c=>{throw a(c),c}):(o.end(),i)}catch(i){throw a(i),i}})}const yt=t=>t==="http:"||t==="https:",ot=(...t)=>`/${t.map(r=>xe(r,"/")).filter(r=>r).join("/")}`,xe=(t,e)=>{const r=je(t,e);return Be(r,e)},je=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Be=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Ge=t=>{const e=Me.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!yt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const o={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},a=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},c=async u=>{const s=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(u!=null&&u.url)&&!s.url)throw new g([],"`ddn.url` is required");const p=new URL((u==null?void 0:u.url)||s.url);if(!yt(p.protocol))throw new Error(`invalid ddn.url protocol: ${p.protocol}`);return p.pathname.endsWith("/sql")||(p.pathname=ot(p.pathname,"v1/sql")),{url:p.toString(),headers:{...(s==null?void 0:s.headers)??{},...(u==null?void 0:u.headers)??{}}}},l=async(u,{llm:s,ai_primitives_llm:p,ddn:b,...d},N,E)=>{var P;if(!d.interactions.length||d.interactions.every(R=>!R.user_message.text))throw new g([],"require at least 1 interaction with non-empty user message content");s=s??t.llm,p=p??t.aiPrimitivesLlm;const _="v1",A=d.timezone||a;u.setAttributes({"promptql.request.interactions_length":d.interactions.length,"promptql.request.artifacts_length":((P=d.artifacts)==null?void 0:P.length)??0,"promptql.request.timezone":A,"promptql.request.version":_}),s!=null&&s.provider&&u.setAttribute("promptql.request.llm_provider",s.provider),p!=null&&p.provider&&u.setAttribute("promptql.request.ai_primitives_llm_provider",p.provider);const q=await c(b);u.setAttribute("promptql.request.ddn_url",q.url);const M=new URL(n);return M.pathname=ot(M.pathname,"query"),r(M,{...E,method:"POST",headers:{...o,...E==null?void 0:E.headers,"Content-Type":"application/json"},body:JSON.stringify({...d,llm:s,ai_primitives_llm:p,ddn:q,timezone:A,version:_,stream:N})}).then(ht)};return{query:(u,s)=>V(e,"PromptQL Query",p=>l(p,u,!1,s).then(b=>b.json()).then(b=>{const d=b;return p.setAttributes({"promptql.response.assistant_actions_length":d.assistant_actions.length,"promptql.response.modified_artifacts_length":d.modified_artifacts.length}),d}),{kind:U.CLIENT}),queryStream:async(u,s,p)=>V(e,"PromptQL Query Stream",async b=>{var M;const d=await l(b,u,!0,p);if(typeof s!="function"||!d.body)return d;const N=d.body.getReader(),E=new TextDecoder("utf-8");let _="",A=0;const q=async P=>{if(!P&&!_)return;let R=_+P;_="",R.startsWith(nt)&&(R=P.substring(nt.length)),R.split(nt).forEach(async(at,mt)=>{const k=at.trim();if(k[k.length-1]!=="}"){_+=mt>0?`data: ${at}`:at;return}let it;try{it=JSON.parse(k)}catch{_+=mt>0?`data: ${k}`:k}it&&await s(it)})};for(;!((M=p==null?void 0:p.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const Q=E.decode(P);Q&&(A+=Q.length,await q(Q))}return b.setAttribute("http.response.body.size",A),_&&await q(""),d},{kind:U.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:p,...b},d)=>V(e,"PromptQL Execute Program",async N=>{if(!b.code)throw new g([],"`code` is required");N.setAttribute("promptql.request.artifacts_length",(s==null?void 0:s.length)??0),u=u??i,u!=null&&u.provider&&N.setAttribute("promptql.request.ai_primitives_llm_provider",u.provider);const E=await c(p);N.setAttribute("promptql.request.ddn_url",E.url);const _=new URL(n);return _.pathname=ot(_.pathname,"execute_program"),r(_,{...d,headers:{...o,...d==null?void 0:d.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...b,ai_primitives_llm:u,ddn:E,artifacts:s??[]})}).then(Se).then(A=>(N.setAttributes({"promptql.response.accessed_artifact_ids":A.accessed_artifact_ids,"promptql.response.modified_artifacts_length":A.modified_artifacts.length}),A))},{kind:U.CLIENT})}},_t=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],o=(t==null?void 0:t.modified_artifacts.reduce((f,u)=>{const s=e(u);return Object.assign(f,{[s]:u})},{}))??{},a=Object.keys(o).sort(),i=()=>_t({assistant_actions:n.map(f=>({...f})),modified_artifacts:a.map(f=>{const u=o[f];switch(u==null?void 0:u.artifact_type){case"table":{const{data:s,...p}=o[f];return Object.assign(p,{data:[...s]})}case"visualization":{const{data:s,...p}=o[f];return Object.assign(p,{data:{...s}})}default:return{...u}}})}),c=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const u=e(f.artifact),s=o[u];if(!s){o[u]=f.artifact,a.push(u),a.sort();return}switch(s.artifact_type){case"text":s.data+=f.artifact.data;break;case"table":s.data.push(...f.artifact.data);break;case"visualization":s.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let s=n.length;s<=f.index;s++)n.push({});const u=n[f.index];for(const s of Ue)u[s]=De(u[s],f[s]);break}}},l=(...f)=>{const u=i();for(const s of f)u.push(s);return u},v=()=>a.map(f=>o[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:v,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:v()}),push:c,append:l}},De=(t,e)=>t?e?t+e:t:e,Ue=["code","code_error","code_output","message","plan"];h.PromptQLError=g,h.createPromptQLClient=Ge,h.createQueryChunks=_t,h.withActiveSpan=V,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(h,g){typeof exports=="object"&&typeof module<"u"?g(exports):typeof define=="function"&&define.amd?define(["exports"],g):(h=typeof globalThis<"u"?globalThis:h||self,g(h.promptql={}))})(this,function(h){"use strict";var qe=Object.defineProperty;var ke=(h,g,I)=>g in h?qe(h,g,{enumerable:!0,configurable:!0,writable:!0,value:I}):h[g]=I;var bt=(h,g,I)=>ke(h,typeof g!="symbol"?g+"":g,I);class g extends Error{constructor(r,n){super(n||r.map(o=>o.msg).join(". "));bt(this,"detail",[]);this.detail=r}}var I=typeof globalThis=="object"?globalThis:typeof self=="object"?self:typeof window=="object"?window:typeof global=="object"?global:{},C="1.9.0",ut=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ut);if(!n)return function(){return!1};var o={major:+n[1],minor:+n[2],patch:+n[3],prerelease:n[4]};if(o.prerelease!=null)return function(l){return l===t};function a(c){return r.add(c),!1}function i(c){return e.add(c),!0}return function(l){if(e.has(l))return!0;if(r.has(l))return!1;var v=l.match(ut);if(!v)return a(l);var m={major:+v[1],minor:+v[2],patch:+v[3],prerelease:v[4]};return m.prerelease!=null||o.major!==m.major?a(l):o.major===0?o.minor===m.minor&&o.patch<=m.patch?i(l):a(l):o.minor<=m.minor?i(l):a(l)}}var Et=wt(C),At=C.split(".")[0],S=Symbol.for("opentelemetry.js.api."+At),x=I;function j(t,e,r,n){var o;n===void 0&&(n=!1);var a=x[S]=(o=x[S])!==null&&o!==void 0?o:{version:C};if(!n&&a[t]){var i=new Error("@opentelemetry/api: Attempted duplicate registration of API: "+t);return r.error(i.stack||i.message),!1}if(a.version!==C){var i=new Error("@opentelemetry/api: Registration of version v"+a.version+" for "+t+" does not match previously registered API v"+C);return r.error(i.stack||i.message),!1}return a[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+C+"."),!0}function T(t){var e,r,n=(e=x[S])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=x[S])===null||r===void 0?void 0:r[t]}function B(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+C+".");var r=x[S];r&&delete r[t]}var Nt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Pt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},Ct=function(){function t(e){this._namespace=e.namespace||"DiagComponentLogger"}return t.prototype.debug=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return G("verbose",this._namespace,e)},t}();function G(t,e,r){var n=T("diag");if(n)return r.unshift(e),n[t].apply(n,Pt([],Nt(r),!1))}var y;(function(t){t[t.NONE=0]="NONE",t[t.ERROR=30]="ERROR",t[t.WARN=50]="WARN",t[t.INFO=60]="INFO",t[t.DEBUG=70]="DEBUG",t[t.VERBOSE=80]="VERBOSE",t[t.ALL=9999]="ALL"})(y||(y={}));function Tt(t,e){t<y.NONE?t=y.NONE:t>y.ALL&&(t=y.ALL),e=e||{};function r(n,o){var a=e[n];return typeof a=="function"&&t>=o?a.bind(e):function(){}}return{error:r("error",y.ERROR),warn:r("warn",y.WARN),info:r("info",y.INFO),debug:r("debug",y.DEBUG),verbose:r("verbose",y.VERBOSE)}}var Ot=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},Rt=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},It="diag",w=function(){function t(){function e(o){return function(){for(var a=[],i=0;i<arguments.length;i++)a[i]=arguments[i];var c=T("diag");if(c)return c[o].apply(c,Rt([],Ot(a),!1))}}var r=this,n=function(o,a){var i,c,l;if(a===void 0&&(a={logLevel:y.INFO}),o===r){var v=new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");return r.error((i=v.stack)!==null&&i!==void 0?i:v.message),!1}typeof a=="number"&&(a={logLevel:a});var m=T("diag"),f=Tt((c=a.logLevel)!==null&&c!==void 0?c:y.INFO,o);if(m&&!a.suppressOverrideMessage){var u=(l=new Error().stack)!==null&&l!==void 0?l:"<failed to generate stacktrace>";m.warn("Current logger will be overwritten from "+u),f.warn("Current logger will overwrite one already registered from "+u)}return j("diag",f,r,!0)};r.setLogger=n,r.disable=function(){B(It,r)},r.createComponentLogger=function(o){return new Ct(o)},r.verbose=e("verbose"),r.debug=e("debug"),r.info=e("info"),r.warn=e("warn"),r.error=e("error")}return t.instance=function(){return this._instance||(this._instance=new t),this._instance},t}(),Mt=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},St=function(t){var e=typeof Symbol=="function"&&Symbol.iterator,r=e&&t[e],n=0;if(r)return r.call(t);if(t&&typeof t.length=="number")return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")},xt=function(){function t(e){this._entries=e?new Map(e):new Map}return t.prototype.getEntry=function(e){var r=this._entries.get(e);if(r)return Object.assign({},r)},t.prototype.getAllEntries=function(){return Array.from(this._entries.entries()).map(function(e){var r=Mt(e,2),n=r[0],o=r[1];return[n,o]})},t.prototype.setEntry=function(e,r){var n=new t(this._entries);return n._entries.set(e,r),n},t.prototype.removeEntry=function(e){var r=new t(this._entries);return r._entries.delete(e),r},t.prototype.removeEntries=function(){for(var e,r,n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];var a=new t(this._entries);try{for(var i=St(n),c=i.next();!c.done;c=i.next()){var l=c.value;a._entries.delete(l)}}catch(v){e={error:v}}finally{try{c&&!c.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return a},t.prototype.clear=function(){return new t},t}();w.instance();function jt(t){return t===void 0&&(t={}),new xt(new Map(Object.entries(t)))}function ct(t){return Symbol.for(t)}var Bt=function(){function t(e){var r=this;r._currentContext=e?new Map(e):new Map,r.getValue=function(n){return r._currentContext.get(n)},r.setValue=function(n,o){var a=new t(r._currentContext);return a._currentContext.set(n,o),a},r.deleteValue=function(n){var o=new t(r._currentContext);return o._currentContext.delete(n),o}}return t}(),Gt=new Bt,O=function(){var t=function(e,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,o){n.__proto__=o}||function(n,o){for(var a in o)Object.prototype.hasOwnProperty.call(o,a)&&(n[a]=o[a])},t(e,r)};return function(e,r){if(typeof r!="function"&&r!==null)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");t(e,r);function n(){this.constructor=e}e.prototype=r===null?Object.create(r):(n.prototype=r.prototype,new n)}}(),Dt=function(){function t(){}return t.prototype.createGauge=function(e,r){return Wt},t.prototype.createHistogram=function(e,r){return Ht},t.prototype.createCounter=function(e,r){return zt},t.prototype.createUpDownCounter=function(e,r){return Xt},t.prototype.createObservableGauge=function(e,r){return Kt},t.prototype.createObservableCounter=function(e,r){return Jt},t.prototype.createObservableUpDownCounter=function(e,r){return Yt},t.prototype.addBatchObservableCallback=function(e,r){},t.prototype.removeBatchObservableCallback=function(e){},t}(),$=function(){function t(){}return t}(),Ut=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}($),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}($),F=function(){function t(){}return t.prototype.addCallback=function(e){},t.prototype.removeCallback=function(e){},t}(),Lt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(F),Ft=new Dt,zt=new Ut,Wt=new kt,Ht=new $t,Xt=new qt,Jt=new Lt,Kt=new Vt,Yt=new Qt,Zt={get:function(t,e){if(t!=null)return t[e]},keys:function(t){return t==null?[]:Object.keys(t)}},te={set:function(t,e,r){t!=null&&(t[e]=r)}},ee=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},re=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},ne=function(){function t(){}return t.prototype.active=function(){return Gt},t.prototype.with=function(e,r,n){for(var o=[],a=3;a<arguments.length;a++)o[a-3]=arguments[a];return r.call.apply(r,re([n],ee(o),!1))},t.prototype.bind=function(e,r){return r},t.prototype.enable=function(){return this},t.prototype.disable=function(){return this},t}(),oe=function(t,e){var r=typeof Symbol=="function"&&t[Symbol.iterator];if(!r)return t;var n=r.call(t),o,a=[],i;try{for(;(e===void 0||e-- >0)&&!(o=n.next()).done;)a.push(o.value)}catch(c){i={error:c}}finally{try{o&&!o.done&&(r=n.return)&&r.call(n)}finally{if(i)throw i.error}}return a},ae=function(t,e,r){if(r||arguments.length===2)for(var n=0,o=e.length,a;n<o;n++)(a||!(n in e))&&(a||(a=Array.prototype.slice.call(e,0,n)),a[n]=e[n]);return t.concat(a||Array.prototype.slice.call(e))},z="context",ie=new ne,L=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return j(z,e,w.instance())},t.prototype.active=function(){return this._getContextManager().active()},t.prototype.with=function(e,r,n){for(var o,a=[],i=3;i<arguments.length;i++)a[i-3]=arguments[i];return(o=this._getContextManager()).with.apply(o,ae([e,r,n],oe(a),!1))},t.prototype.bind=function(e,r){return this._getContextManager().bind(e,r)},t.prototype._getContextManager=function(){return T(z)||ie},t.prototype.disable=function(){this._getContextManager().disable(),B(z,w.instance())},t}(),W;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(W||(W={}));var st="0000000000000000",ft="00000000000000000000000000000000",ue={traceId:ft,spanId:st,traceFlags:W.NONE},D=function(){function t(e){e===void 0&&(e=ue),this._spanContext=e}return t.prototype.spanContext=function(){return this._spanContext},t.prototype.setAttribute=function(e,r){return this},t.prototype.setAttributes=function(e){return this},t.prototype.addEvent=function(e,r){return this},t.prototype.addLink=function(e){return this},t.prototype.addLinks=function(e){return this},t.prototype.setStatus=function(e){return this},t.prototype.updateName=function(e){return this},t.prototype.end=function(e){},t.prototype.isRecording=function(){return!1},t.prototype.recordException=function(e,r){},t}(),H=ct("OpenTelemetry Context Key SPAN");function X(t){return t.getValue(H)||void 0}function ce(){return X(L.getInstance().active())}function J(t,e){return t.setValue(H,e)}function se(t){return t.deleteValue(H)}function fe(t,e){return J(t,new D(e))}function pt(t){var e;return(e=X(t))===null||e===void 0?void 0:e.spanContext()}var pe=/^([0-9a-f]{32})$/i,le=/^[0-9a-f]{16}$/i;function de(t){return pe.test(t)&&t!==ft}function ge(t){return le.test(t)&&t!==st}function lt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new D(t)}var K=L.getInstance(),dt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=K.active());var o=!!(r!=null&&r.root);if(o)return new D;var a=n&&pt(n);return he(a)&<(a)?new D(a):new D},t.prototype.startActiveSpan=function(e,r,n,o){var a,i,c;if(!(arguments.length<2)){arguments.length===2?c=r:arguments.length===3?(a=r,c=n):(a=r,i=n,c=o);var l=i??K.active(),v=this.startSpan(e,a,l),m=J(l,v);return K.with(m,c,void 0,v)}},t}();function he(t){return typeof t=="object"&&typeof t.spanId=="string"&&typeof t.traceId=="string"&&typeof t.traceFlags=="number"}var ye=new dt,_e=function(){function t(e,r,n,o){this._provider=e,this.name=r,this.version=n,this.options=o}return t.prototype.startSpan=function(e,r,n){return this._getTracer().startSpan(e,r,n)},t.prototype.startActiveSpan=function(e,r,n,o){var a=this._getTracer();return Reflect.apply(a.startActiveSpan,a,arguments)},t.prototype._getTracer=function(){if(this._delegate)return this._delegate;var e=this._provider.getDelegateTracer(this.name,this.version,this.options);return e?(this._delegate=e,this._delegate):ye},t}(),me=function(){function t(){}return t.prototype.getTracer=function(e,r,n){return new dt},t}(),be=new me,gt=function(){function t(){}return t.prototype.getTracer=function(e,r,n){var o;return(o=this.getDelegateTracer(e,r,n))!==null&&o!==void 0?o:new _e(this,e,r,n)},t.prototype.getDelegate=function(){var e;return(e=this._delegate)!==null&&e!==void 0?e:be},t.prototype.setDelegate=function(e){this._delegate=e},t.prototype.getDelegateTracer=function(e,r,n){var o;return(o=this._delegate)===null||o===void 0?void 0:o.getTracer(e,r,n)},t}(),U;(function(t){t[t.INTERNAL=0]="INTERNAL",t[t.SERVER=1]="SERVER",t[t.CLIENT=2]="CLIENT",t[t.PRODUCER=3]="PRODUCER",t[t.CONSUMER=4]="CONSUMER"})(U||(U={}));var Y;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Y||(Y={})),L.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,Z="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return j(Z,e,w.instance())},t.prototype.getMeterProvider=function(){return T(Z)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){B(Z,w.instance())},t}();Ae.getInstance();var Ne=function(){function t(){}return t.prototype.inject=function(e,r){},t.prototype.extract=function(e,r){return e},t.prototype.fields=function(){return[]},t}(),tt=ct("OpenTelemetry Baggage Key");function vt(t){return t.getValue(tt)||void 0}function Pe(){return vt(L.getInstance().active())}function Ce(t,e){return t.setValue(tt,e)}function Te(t){return t.deleteValue(tt)}var et="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=vt,this.getActiveBaggage=Pe,this.setBaggage=Ce,this.deleteBaggage=Te}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalPropagator=function(e){return j(et,e,w.instance())},t.prototype.inject=function(e,r,n){return n===void 0&&(n=te),this._getGlobalPropagator().inject(e,r,n)},t.prototype.extract=function(e,r,n){return n===void 0&&(n=Zt),this._getGlobalPropagator().extract(e,r,n)},t.prototype.fields=function(){return this._getGlobalPropagator().fields()},t.prototype.disable=function(){B(et,w.instance())},t.prototype._getGlobalPropagator=function(){return T(et)||Oe},t}();Re.getInstance();var rt="trace",Ie=function(){function t(){this._proxyTracerProvider=new gt,this.wrapSpanContext=ve,this.isSpanContextValid=lt,this.deleteSpan=se,this.getSpan=X,this.getActiveSpan=ce,this.getSpanContext=pt,this.setSpan=J,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=j(rt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return T(rt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){B(rt,w.instance()),this._proxyTracerProvider=new gt},t}(),Me=Ie.getInstance();const nt="data: ",ht=async t=>{var r;if(t.status<400)return t;const e=t.body?await t.text():t.statusText;if(t.status>500||!t.body)throw new g([],e);try{const n=JSON.parse(e),o=(r=n.detail)!=null&&r.length?n.detail.map(a=>a.msg).join(". "):e;throw new g(n.detail??[],o)}catch{throw new g([],e)}},Se=t=>ht(t).then(e=>e.json());function V(t,e,r,n={}){return t.startActiveSpan(e,n,o=>{const a=i=>{(i instanceof Error||typeof i=="string")&&o.recordException(i),o.setStatus({code:Y.ERROR}),o.end()};try{const i=r(o);return typeof i=="object"&&i!==null&&"then"in i&&typeof i.then=="function"?i.then(c=>(o.end(),c),c=>{throw a(c),c}):(o.end(),i)}catch(i){throw a(i),i}})}const yt=t=>t==="http:"||t==="https:",ot=(...t)=>`/${t.map(r=>xe(r,"/")).filter(r=>r).join("/")}`,xe=(t,e)=>{const r=je(t,e);return Be(r,e)},je=(t,e)=>{for(let r=0;r<t.length;r++)if(t[r]!==e)return t.substring(r);return""},Be=(t,e)=>{for(let r=t.length-1;r>=0;r--)if(t[r]!==e)return t.substring(0,r+1);return""},Ge=t=>{const e=Me.getTracer("promptql-nodejs-sdk"),r=typeof t.fetch=="function"?t.fetch:fetch,n=new URL(t.baseUrl?t.baseUrl:"https://api.promptql.pro.hasura.io");if(!yt(n.protocol))throw new Error(`invalid promptql url: ${n}`);const o={...t.headers??{},Authorization:`Bearer ${t.apiKey}`},a=t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone,i=t.aiPrimitivesLlm??{provider:"hasura"},c=async u=>{const s=typeof t.ddn=="function"?await t.ddn():t.ddn;if(!(u!=null&&u.url)&&!s.url)throw new g([],"`ddn.url` is required");const p=new URL((u==null?void 0:u.url)||s.url);if(!yt(p.protocol))throw new Error(`invalid ddn.url protocol: ${p.protocol}`);return p.pathname.endsWith("/sql")||(p.pathname=ot(p.pathname,"v1/sql")),{url:p.toString(),headers:{...(s==null?void 0:s.headers)??{},...(u==null?void 0:u.headers)??{}}}},l=async(u,{llm:s,ai_primitives_llm:p,ddn:b,...d},N,E)=>{var P;if(!d.interactions.length||d.interactions.every(R=>!R.user_message.text))throw new g([],"require at least 1 interaction with non-empty user message content");s=s??t.llm,p=p??t.aiPrimitivesLlm;const _="v1",A=d.timezone||a;u.setAttributes({"promptql.request.interactions_length":d.interactions.length,"promptql.request.artifacts_length":((P=d.artifacts)==null?void 0:P.length)??0,"promptql.request.timezone":A,"promptql.request.version":_}),s!=null&&s.provider&&u.setAttribute("promptql.request.llm_provider",s.provider),p!=null&&p.provider&&u.setAttribute("promptql.request.ai_primitives_llm_provider",p.provider);const q=await c(b);u.setAttribute("promptql.request.ddn_url",q.url);const M=new URL(n);return M.pathname=ot(M.pathname,"query"),r(M,{...E,method:"POST",headers:{...o,...E==null?void 0:E.headers,"Content-Type":"application/json"},body:JSON.stringify({system_instructions:t.systemInstructions,...d,llm:s,ai_primitives_llm:p,ddn:q,timezone:A,version:_,stream:N})}).then(ht)};return{query:(u,s)=>V(e,"PromptQL Query",p=>l(p,u,!1,s).then(b=>b.json()).then(b=>{const d=b;return p.setAttributes({"promptql.response.assistant_actions_length":d.assistant_actions.length,"promptql.response.modified_artifacts_length":d.modified_artifacts.length}),d}),{kind:U.CLIENT}),queryStream:async(u,s,p)=>V(e,"PromptQL Query Stream",async b=>{var M;const d=await l(b,u,!0,p);if(typeof s!="function"||!d.body)return d;const N=d.body.getReader(),E=new TextDecoder("utf-8");let _="",A=0;const q=async P=>{if(!P&&!_)return;let R=_+P;_="",R.startsWith(nt)&&(R=P.substring(nt.length)),R.split(nt).forEach(async(at,mt)=>{const k=at.trim();if(k[k.length-1]!=="}"){_+=mt>0?`data: ${at}`:at;return}let it;try{it=JSON.parse(k)}catch{_+=mt>0?`data: ${k}`:k}it&&await s(it)})};for(;!((M=p==null?void 0:p.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const Q=E.decode(P);Q&&(A+=Q.length,await q(Q))}return b.setAttribute("http.response.body.size",A),_&&await q(""),d},{kind:U.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:p,...b},d)=>V(e,"PromptQL Execute Program",async N=>{if(!b.code)throw new g([],"`code` is required");N.setAttribute("promptql.request.artifacts_length",(s==null?void 0:s.length)??0),u=u??i,u!=null&&u.provider&&N.setAttribute("promptql.request.ai_primitives_llm_provider",u.provider);const E=await c(p);N.setAttribute("promptql.request.ddn_url",E.url);const _=new URL(n);return _.pathname=ot(_.pathname,"execute_program"),r(_,{...d,headers:{...o,...d==null?void 0:d.headers,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({...b,ai_primitives_llm:u,ddn:E,artifacts:s??[]})}).then(Se).then(A=>(N.setAttributes({"promptql.response.accessed_artifact_ids":A.accessed_artifact_ids,"promptql.response.modified_artifacts_length":A.modified_artifacts.length}),A))},{kind:U.CLIENT})}},_t=t=>{const e=f=>`${f.artifact_type}:${f.identifier}`;let r;const n=(t==null?void 0:t.assistant_actions)??[],o=(t==null?void 0:t.modified_artifacts.reduce((f,u)=>{const s=e(u);return Object.assign(f,{[s]:u})},{}))??{},a=Object.keys(o).sort(),i=()=>_t({assistant_actions:n.map(f=>({...f})),modified_artifacts:a.map(f=>{const u=o[f];switch(u==null?void 0:u.artifact_type){case"table":{const{data:s,...p}=o[f];return Object.assign(p,{data:[...s]})}case"visualization":{const{data:s,...p}=o[f];return Object.assign(p,{data:{...s}})}default:return{...u}}})}),c=f=>{switch(f.type){case"error_chunk":r?r.error+=f.error:r=f;break;case"artifact_update_chunk":{const u=e(f.artifact),s=o[u];if(!s){o[u]=f.artifact,a.push(u),a.sort();return}switch(s.artifact_type){case"text":s.data+=f.artifact.data;break;case"table":s.data.push(...f.artifact.data);break;case"visualization":s.data.html+=f.artifact.data.html;break}break}case"assistant_action_chunk":{for(let s=n.length;s<=f.index;s++)n.push({});const u=n[f.index];for(const s of Ue)u[s]=De(u[s],f[s]);break}}},l=(...f)=>{const u=i();for(const s of f)u.push(s);return u},v=()=>a.map(f=>o[f]);return{getAssistantActions:()=>n,getError:()=>r,isError:()=>r!==void 0,getModifiedArtifacts:v,toQueryResponse:()=>({assistant_actions:n,modified_artifacts:v()}),push:c,append:l}},De=(t,e)=>t?e?t+e:t:e,Ue=["code","code_error","code_output","message","plan"];h.PromptQLError=g,h.createPromptQLClient=Ge,h.createQueryChunks=_t,h.withActiveSpan=V,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})});
|
package/dist/types.d.ts
CHANGED
|
@@ -50,6 +50,12 @@ export type PromptQLClientConfig = {
|
|
|
50
50
|
* @type {?string}
|
|
51
51
|
*/
|
|
52
52
|
timezone?: string;
|
|
53
|
+
/**
|
|
54
|
+
* Default system instruction prompt.
|
|
55
|
+
*
|
|
56
|
+
* @type {?string}
|
|
57
|
+
*/
|
|
58
|
+
systemInstructions?: string;
|
|
53
59
|
/**
|
|
54
60
|
* Use a custom http client function. Default is fetch.
|
|
55
61
|
*
|