@hasura/promptql 0.2.0 → 0.3.0-alpha.2
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 +97 -94
- 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 ke=Object.defineProperty;var qe=(_,v,I)=>v in _?ke(_,v,{enumerable:!0,configurable:!0,writable:!0,value:I}):_[v]=I;var bt=(_,v,I)=>qe(_,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",ct=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ct);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(ct);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],x=Symbol.for("opentelemetry.js.api."+At),j=I;function B(t,e,r,n){var o;n===void 0&&(n=!1);var a=j[x]=(o=j[x])!==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=j[x])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=j[x])===null||r===void 0?void 0:r[t]}function G(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+C+".");var r=j[x];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 D("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("verbose",this._namespace,e)},t}();function D(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 B("diag",f,r,!0)};r.setLogger=n,r.disable=function(){G(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 st(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}(),V=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}(V),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}(V),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}(V),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}(V),z=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}(z),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(z),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(z),Ft=new Dt,zt=new Ut,Wt=new qt,Ht=new $t,Xt=new kt,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))},W="context",ie=new ne,Q=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return B(W,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(W)||ie},t.prototype.disable=function(){this._getContextManager().disable(),G(W,w.instance())},t}(),H;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(H||(H={}));var ft="0000000000000000",lt="00000000000000000000000000000000",ue={traceId:lt,spanId:ft,traceFlags:H.NONE},U=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}(),X=st("OpenTelemetry Context Key SPAN");function J(t){return t.getValue(X)||void 0}function ce(){return J(Q.getInstance().active())}function K(t,e){return t.setValue(X,e)}function se(t){return t.deleteValue(X)}function fe(t,e){return K(t,new U(e))}function pt(t){var e;return(e=J(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!==lt}function ge(t){return pe.test(t)&&t!==ft}function dt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new U(t)}var Y=Q.getInstance(),gt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=Y.active());var o=!!(r!=null&&r.root);if(o)return new U;var a=n&&pt(n);return he(a)&&dt(a)?new U(a):new U},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??Y.active(),g=this.startSpan(e,a,p),m=K(p,g);return Y.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 gt,_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 gt},t}(),be=new me,vt=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}(),k;(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"})(k||(k={}));var Z;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Z||(Z={})),Q.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,tt="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return B(tt,e,w.instance())},t.prototype.getMeterProvider=function(){return T(tt)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){G(tt,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}(),et=st("OpenTelemetry Baggage Key");function ht(t){return t.getValue(et)||void 0}function Pe(){return ht(Q.getInstance().active())}function Ce(t,e){return t.setValue(et,e)}function Te(t){return t.deleteValue(et)}var rt="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=ht,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 B(rt,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(){G(rt,w.instance())},t.prototype._getGlobalPropagator=function(){return T(rt)||Oe},t}();Re.getInstance();var nt="trace",Ie=function(){function t(){this._proxyTracerProvider=new vt,this.wrapSpanContext=ve,this.isSpanContextValid=dt,this.deleteSpan=se,this.getSpan=J,this.getActiveSpan=ce,this.getSpanContext=pt,this.setSpan=K,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=B(nt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return T(nt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){G(nt,w.instance()),this._proxyTracerProvider=new vt},t}(),Me=Ie.getInstance();const ot="data: ",yt=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=>yt(t).then(e=>e.json());function F(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:Z.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 _t=t=>t==="http:"||t==="https:",at=(...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(!_t(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(!_t(l.protocol))throw new Error(`invalid ddn.url protocol: ${l.protocol}`);return l.pathname.endsWith("/sql")||(l.pathname=at(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=at(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(yt)};return{query:(u,s)=>F(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:k.CLIENT}),queryStream:async(u,s,l)=>F(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(ot)&&(R=P.substring(ot.length));const S=R.split(ot);for(let $=0;$<S.length;$++){const it=S[$],L=it.trim();if(L[L.length-1]!=="}"){y+=$>0?`data: ${it}`:it;return}let ut;try{ut=JSON.parse(L)}catch{y+=$>0?`data: ${L}`:L}ut&&await s(ut)}};for(;!((M=l==null?void 0:l.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const S=E.decode(P);S&&(A+=S.length,await q(S))}return b.setAttribute("http.response.body.size",A),y&&await q(""),d},{kind:k.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:l,...b},d)=>F(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=at(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:k.CLIENT})}},mt=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=()=>mt({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=mt,_.withActiveSpan=F,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],q=Symbol.for("opentelemetry.js.api."+vt),k=pt;function X(t,e,r,n){var a;n===void 0&&(n=!1);var o=k[q]=(a=k[q])!==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 D(t){var e,r,n=(e=k[q])===null||e===void 0?void 0:e.version;if(!(!n||!gt(n)))return(r=k[q])===null||r===void 0?void 0:r[t]}function J(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+N+".");var r=k[q];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 I("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return I("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return I("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return I("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return I("verbose",this._namespace,e)},t}();function I(t,e,r){var n=D("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",U=function(){function t(){function e(a){return function(){for(var o=[],i=0;i<arguments.length;i++)o[i]=arguments[i];var u=D("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=D("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 X("diag",f,r,!0)};r.setLogger=n,r.disable=function(){J(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,Pt=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},Ct=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,Ct([n],Pt(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))},Q="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 X(Q,e,U.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 D(Q)||Ot},t.prototype.disable=function(){this._getContextManager().disable(),J(Q,U.instance())},t}(),V;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(V||(V={}));var at="0000000000000000",ot="00000000000000000000000000000000",jt={traceId:ot,spanId:at,traceFlags:V.NONE},O=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}(),H=Nt("OpenTelemetry Context Key SPAN");function K(t){return t.getValue(H)||void 0}function qt(){return K(nt.getInstance().active())}function Y(t,e){return t.setValue(H,e)}function kt(t){return t.deleteValue(H)}function Dt(t,e){return Y(t,new O(e))}function it(t){var e;return(e=K(t))===null||e===void 0?void 0:e.spanContext()}var Mt=/^([0-9a-f]{32})$/i,Ut=/^[0-9a-f]{16}$/i;function $t(t){return Mt.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 O(t)}var G=nt.getInstance(),ct=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=G.active());var a=!!(r!=null&&r.root);if(a)return new O;var o=n&&it(n);return Gt(o)&&st(o)?new O(o):new O},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??G.active(),h=this.startSpan(e,o,d),y=Y(d,h);return G.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}(),j;(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"})(j||(j={}));var W;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(W||(W={}));var B="trace",Wt=function(){function t(){this._proxyTracerProvider=new et,this.wrapSpanContext=Qt,this.isSpanContextValid=st,this.deleteSpan=kt,this.getSpan=K,this.getActiveSpan=qt,this.getSpanContext=it,this.setSpan=Y,this.setSpanContext=Dt}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=X(B,this._proxyTracerProvider,U.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return D(B)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){J(B,U.instance()),this._proxyTracerProvider=new et},t}(),Xt=Wt.getInstance();const F="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:W.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:",z=(...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=z(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=z(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:j.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(F)&&(E=A.substring(F.length));const P=E.split(F);for(let S=0;S<P.length;S++){const $=P[S],x=$.trim();if(x[x.length-1]!=="}"){g+=S>0?`data: ${$}`:$;return}let L;try{L=JSON.parse(x)}catch{g+=S>0?`data: ${x}`:x}L&&await c(L)}};for(;!((R=l==null?void 0:l.signal)!=null&&R.aborted);){const{value:A,done:E}=await w.read();if(E)break;const P=_.decode(A);P&&(b+=P.length,await C(P))}return m.setAttribute("http.response.body.size",b),g&&await C(""),p},{kind:j.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=z(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:j.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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var ft = Object.defineProperty;
|
|
2
2
|
var lt = (t, e, r) => e in t ? ft(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
|
|
3
3
|
var Z = (t, e, r) => lt(t, typeof e != "symbol" ? e + "" : e, r);
|
|
4
|
-
class
|
|
4
|
+
class C extends Error {
|
|
5
5
|
constructor(r, n) {
|
|
6
6
|
super(n || r.map((a) => a.msg).join(". "));
|
|
7
7
|
Z(this, "detail", []);
|
|
@@ -48,11 +48,11 @@ function pt(t) {
|
|
|
48
48
|
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);
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
var ht = pt(N), gt = N.split(".")[0],
|
|
52
|
-
function
|
|
51
|
+
var ht = pt(N), gt = N.split(".")[0], j = Symbol.for("opentelemetry.js.api." + gt), k = dt;
|
|
52
|
+
function X(t, e, r, n) {
|
|
53
53
|
var a;
|
|
54
54
|
n === void 0 && (n = !1);
|
|
55
|
-
var o =
|
|
55
|
+
var o = k[j] = (a = k[j]) !== null && a !== void 0 ? a : {
|
|
56
56
|
version: N
|
|
57
57
|
};
|
|
58
58
|
if (!n && o[t]) {
|
|
@@ -65,14 +65,14 @@ function W(t, e, r, n) {
|
|
|
65
65
|
}
|
|
66
66
|
return o[t] = e, r.debug("@opentelemetry/api: Registered a global for " + t + " v" + N + "."), !0;
|
|
67
67
|
}
|
|
68
|
-
function
|
|
69
|
-
var e, r, n = (e =
|
|
68
|
+
function D(t) {
|
|
69
|
+
var e, r, n = (e = k[j]) === null || e === void 0 ? void 0 : e.version;
|
|
70
70
|
if (!(!n || !ht(n)))
|
|
71
|
-
return (r =
|
|
71
|
+
return (r = k[j]) === null || r === void 0 ? void 0 : r[t];
|
|
72
72
|
}
|
|
73
|
-
function
|
|
73
|
+
function J(t, e) {
|
|
74
74
|
e.debug("@opentelemetry/api: Unregistering a global for " + t + " v" + N + ".");
|
|
75
|
-
var r =
|
|
75
|
+
var r = k[j];
|
|
76
76
|
r && delete r[t];
|
|
77
77
|
}
|
|
78
78
|
var vt = function(t, e) {
|
|
@@ -104,28 +104,28 @@ var vt = function(t, e) {
|
|
|
104
104
|
return t.prototype.debug = function() {
|
|
105
105
|
for (var e = [], r = 0; r < arguments.length; r++)
|
|
106
106
|
e[r] = arguments[r];
|
|
107
|
-
return
|
|
107
|
+
return I("debug", this._namespace, e);
|
|
108
108
|
}, t.prototype.error = function() {
|
|
109
109
|
for (var e = [], r = 0; r < arguments.length; r++)
|
|
110
110
|
e[r] = arguments[r];
|
|
111
|
-
return
|
|
111
|
+
return I("error", this._namespace, e);
|
|
112
112
|
}, t.prototype.info = function() {
|
|
113
113
|
for (var e = [], r = 0; r < arguments.length; r++)
|
|
114
114
|
e[r] = arguments[r];
|
|
115
|
-
return
|
|
115
|
+
return I("info", this._namespace, e);
|
|
116
116
|
}, t.prototype.warn = function() {
|
|
117
117
|
for (var e = [], r = 0; r < arguments.length; r++)
|
|
118
118
|
e[r] = arguments[r];
|
|
119
|
-
return
|
|
119
|
+
return I("warn", this._namespace, e);
|
|
120
120
|
}, t.prototype.verbose = function() {
|
|
121
121
|
for (var e = [], r = 0; r < arguments.length; r++)
|
|
122
122
|
e[r] = arguments[r];
|
|
123
|
-
return
|
|
123
|
+
return I("verbose", this._namespace, e);
|
|
124
124
|
}, t;
|
|
125
125
|
}()
|
|
126
126
|
);
|
|
127
|
-
function
|
|
128
|
-
var n =
|
|
127
|
+
function I(t, e, r) {
|
|
128
|
+
var n = D("diag");
|
|
129
129
|
if (n)
|
|
130
130
|
return r.unshift(e), n[t].apply(n, yt([], vt(r), !1));
|
|
131
131
|
}
|
|
@@ -176,7 +176,7 @@ var bt = function(t, e) {
|
|
|
176
176
|
return function() {
|
|
177
177
|
for (var o = [], i = 0; i < arguments.length; i++)
|
|
178
178
|
o[i] = arguments[i];
|
|
179
|
-
var u =
|
|
179
|
+
var u = D("diag");
|
|
180
180
|
if (u)
|
|
181
181
|
return u[a].apply(u, wt([], bt(o), !1));
|
|
182
182
|
};
|
|
@@ -190,15 +190,15 @@ var bt = function(t, e) {
|
|
|
190
190
|
typeof o == "number" && (o = {
|
|
191
191
|
logLevel: o
|
|
192
192
|
});
|
|
193
|
-
var y =
|
|
193
|
+
var y = D("diag"), f = _t((u = o.logLevel) !== null && u !== void 0 ? u : v.INFO, a);
|
|
194
194
|
if (y && !o.suppressOverrideMessage) {
|
|
195
195
|
var s = (d = new Error().stack) !== null && d !== void 0 ? d : "<failed to generate stacktrace>";
|
|
196
196
|
y.warn("Current logger will be overwritten from " + s), f.warn("Current logger will overwrite one already registered from " + s);
|
|
197
197
|
}
|
|
198
|
-
return
|
|
198
|
+
return X("diag", f, r, !0);
|
|
199
199
|
};
|
|
200
200
|
r.setLogger = n, r.disable = function() {
|
|
201
|
-
|
|
201
|
+
J(At, r);
|
|
202
202
|
}, r.createComponentLogger = function(a) {
|
|
203
203
|
return new mt(a);
|
|
204
204
|
}, r.verbose = e("verbose"), r.debug = e("debug"), r.info = e("info"), r.warn = e("warn"), r.error = e("error");
|
|
@@ -287,7 +287,7 @@ var Nt = (
|
|
|
287
287
|
if (r || arguments.length === 2) for (var n = 0, a = e.length, o; n < a; n++)
|
|
288
288
|
(o || !(n in e)) && (o || (o = Array.prototype.slice.call(e, 0, n)), o[n] = e[n]);
|
|
289
289
|
return t.concat(o || Array.prototype.slice.call(e));
|
|
290
|
-
},
|
|
290
|
+
}, L = "context", It = new Pt(), nt = (
|
|
291
291
|
/** @class */
|
|
292
292
|
function() {
|
|
293
293
|
function t() {
|
|
@@ -295,7 +295,7 @@ var Nt = (
|
|
|
295
295
|
return t.getInstance = function() {
|
|
296
296
|
return this._instance || (this._instance = new t()), this._instance;
|
|
297
297
|
}, t.prototype.setGlobalContextManager = function(e) {
|
|
298
|
-
return
|
|
298
|
+
return X(L, e, M.instance());
|
|
299
299
|
}, t.prototype.active = function() {
|
|
300
300
|
return this._getContextManager().active();
|
|
301
301
|
}, t.prototype.with = function(e, r, n) {
|
|
@@ -305,20 +305,20 @@ var Nt = (
|
|
|
305
305
|
}, t.prototype.bind = function(e, r) {
|
|
306
306
|
return this._getContextManager().bind(e, r);
|
|
307
307
|
}, t.prototype._getContextManager = function() {
|
|
308
|
-
return
|
|
308
|
+
return D(L) || It;
|
|
309
309
|
}, t.prototype.disable = function() {
|
|
310
|
-
this._getContextManager().disable(),
|
|
310
|
+
this._getContextManager().disable(), J(L, M.instance());
|
|
311
311
|
}, t;
|
|
312
312
|
}()
|
|
313
|
-
),
|
|
313
|
+
), V;
|
|
314
314
|
(function(t) {
|
|
315
315
|
t[t.NONE = 0] = "NONE", t[t.SAMPLED = 1] = "SAMPLED";
|
|
316
|
-
})(
|
|
316
|
+
})(V || (V = {}));
|
|
317
317
|
var at = "0000000000000000", ot = "00000000000000000000000000000000", Ot = {
|
|
318
318
|
traceId: ot,
|
|
319
319
|
spanId: at,
|
|
320
|
-
traceFlags:
|
|
321
|
-
},
|
|
320
|
+
traceFlags: V.NONE
|
|
321
|
+
}, O = (
|
|
322
322
|
/** @class */
|
|
323
323
|
function() {
|
|
324
324
|
function t(e) {
|
|
@@ -346,57 +346,57 @@ var at = "0000000000000000", ot = "00000000000000000000000000000000", Ot = {
|
|
|
346
346
|
}, t.prototype.recordException = function(e, r) {
|
|
347
347
|
}, t;
|
|
348
348
|
}()
|
|
349
|
-
),
|
|
350
|
-
function
|
|
351
|
-
return t.getValue(
|
|
349
|
+
), H = Et("OpenTelemetry Context Key SPAN");
|
|
350
|
+
function K(t) {
|
|
351
|
+
return t.getValue(H) || void 0;
|
|
352
352
|
}
|
|
353
353
|
function qt() {
|
|
354
|
-
return
|
|
354
|
+
return K(nt.getInstance().active());
|
|
355
355
|
}
|
|
356
|
-
function
|
|
357
|
-
return t.setValue(
|
|
356
|
+
function Y(t, e) {
|
|
357
|
+
return t.setValue(H, e);
|
|
358
358
|
}
|
|
359
359
|
function jt(t) {
|
|
360
|
-
return t.deleteValue(
|
|
360
|
+
return t.deleteValue(H);
|
|
361
361
|
}
|
|
362
|
-
function
|
|
363
|
-
return
|
|
362
|
+
function kt(t, e) {
|
|
363
|
+
return Y(t, new O(e));
|
|
364
364
|
}
|
|
365
365
|
function it(t) {
|
|
366
366
|
var e;
|
|
367
|
-
return (e =
|
|
367
|
+
return (e = K(t)) === null || e === void 0 ? void 0 : e.spanContext();
|
|
368
368
|
}
|
|
369
|
-
var
|
|
369
|
+
var Dt = /^([0-9a-f]{32})$/i, Mt = /^[0-9a-f]{16}$/i;
|
|
370
370
|
function Ut(t) {
|
|
371
|
-
return
|
|
371
|
+
return Dt.test(t) && t !== ot;
|
|
372
372
|
}
|
|
373
373
|
function $t(t) {
|
|
374
|
-
return
|
|
374
|
+
return Mt.test(t) && t !== at;
|
|
375
375
|
}
|
|
376
376
|
function st(t) {
|
|
377
377
|
return Ut(t.traceId) && $t(t.spanId);
|
|
378
378
|
}
|
|
379
379
|
function Lt(t) {
|
|
380
|
-
return new
|
|
380
|
+
return new O(t);
|
|
381
381
|
}
|
|
382
|
-
var
|
|
382
|
+
var G = nt.getInstance(), ct = (
|
|
383
383
|
/** @class */
|
|
384
384
|
function() {
|
|
385
385
|
function t() {
|
|
386
386
|
}
|
|
387
387
|
return t.prototype.startSpan = function(e, r, n) {
|
|
388
|
-
n === void 0 && (n =
|
|
388
|
+
n === void 0 && (n = G.active());
|
|
389
389
|
var a = !!(r != null && r.root);
|
|
390
390
|
if (a)
|
|
391
|
-
return new
|
|
391
|
+
return new O();
|
|
392
392
|
var o = n && it(n);
|
|
393
|
-
return Gt(o) && st(o) ? new
|
|
393
|
+
return Gt(o) && st(o) ? new O(o) : new O();
|
|
394
394
|
}, t.prototype.startActiveSpan = function(e, r, n, a) {
|
|
395
395
|
var o, i, u;
|
|
396
396
|
if (!(arguments.length < 2)) {
|
|
397
397
|
arguments.length === 2 ? u = r : arguments.length === 3 ? (o = r, u = n) : (o = r, i = n, u = a);
|
|
398
|
-
var d = i ??
|
|
399
|
-
return
|
|
398
|
+
var d = i ?? G.active(), h = this.startSpan(e, o, d), y = Y(d, h);
|
|
399
|
+
return G.with(y, u, void 0, h);
|
|
400
400
|
}
|
|
401
401
|
}, t;
|
|
402
402
|
}()
|
|
@@ -449,52 +449,52 @@ var Bt = new ct(), Ft = (
|
|
|
449
449
|
return (a = this._delegate) === null || a === void 0 ? void 0 : a.getTracer(e, r, n);
|
|
450
450
|
}, t;
|
|
451
451
|
}()
|
|
452
|
-
),
|
|
452
|
+
), q;
|
|
453
453
|
(function(t) {
|
|
454
454
|
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";
|
|
455
|
-
})(
|
|
456
|
-
var
|
|
455
|
+
})(q || (q = {}));
|
|
456
|
+
var W;
|
|
457
457
|
(function(t) {
|
|
458
458
|
t[t.UNSET = 0] = "UNSET", t[t.OK = 1] = "OK", t[t.ERROR = 2] = "ERROR";
|
|
459
|
-
})(
|
|
460
|
-
var
|
|
459
|
+
})(W || (W = {}));
|
|
460
|
+
var B = "trace", Vt = (
|
|
461
461
|
/** @class */
|
|
462
462
|
function() {
|
|
463
463
|
function t() {
|
|
464
|
-
this._proxyTracerProvider = new et(), this.wrapSpanContext = Lt, this.isSpanContextValid = st, this.deleteSpan = jt, this.getSpan =
|
|
464
|
+
this._proxyTracerProvider = new et(), this.wrapSpanContext = Lt, this.isSpanContextValid = st, this.deleteSpan = jt, this.getSpan = K, this.getActiveSpan = qt, this.getSpanContext = it, this.setSpan = Y, this.setSpanContext = kt;
|
|
465
465
|
}
|
|
466
466
|
return t.getInstance = function() {
|
|
467
467
|
return this._instance || (this._instance = new t()), this._instance;
|
|
468
468
|
}, t.prototype.setGlobalTracerProvider = function(e) {
|
|
469
|
-
var r =
|
|
469
|
+
var r = X(B, this._proxyTracerProvider, M.instance());
|
|
470
470
|
return r && this._proxyTracerProvider.setDelegate(e), r;
|
|
471
471
|
}, t.prototype.getTracerProvider = function() {
|
|
472
|
-
return
|
|
472
|
+
return D(B) || this._proxyTracerProvider;
|
|
473
473
|
}, t.prototype.getTracer = function(e, r) {
|
|
474
474
|
return this.getTracerProvider().getTracer(e, r);
|
|
475
475
|
}, t.prototype.disable = function() {
|
|
476
|
-
|
|
476
|
+
J(B, M.instance()), this._proxyTracerProvider = new et();
|
|
477
477
|
}, t;
|
|
478
478
|
}()
|
|
479
479
|
), Wt = Vt.getInstance();
|
|
480
|
-
const
|
|
480
|
+
const F = "data: ", ut = async (t) => {
|
|
481
481
|
var r;
|
|
482
482
|
if (t.status < 400)
|
|
483
483
|
return t;
|
|
484
484
|
const e = t.body ? await t.text() : t.statusText;
|
|
485
485
|
if (t.status > 500 || !t.body)
|
|
486
|
-
throw new
|
|
486
|
+
throw new C([], e);
|
|
487
487
|
try {
|
|
488
488
|
const n = JSON.parse(e), a = (r = n.detail) != null && r.length ? n.detail.map((o) => o.msg).join(". ") : e;
|
|
489
|
-
throw new
|
|
489
|
+
throw new C(n.detail ?? [], a);
|
|
490
490
|
} catch {
|
|
491
|
-
throw new
|
|
491
|
+
throw new C([], e);
|
|
492
492
|
}
|
|
493
493
|
}, Xt = (t) => ut(t).then((e) => e.json());
|
|
494
|
-
function
|
|
494
|
+
function z(t, e, r, n = {}) {
|
|
495
495
|
return t.startActiveSpan(e, n, (a) => {
|
|
496
496
|
const o = (i) => {
|
|
497
|
-
(i instanceof Error || typeof i == "string") && a.recordException(i), a.setStatus({ code:
|
|
497
|
+
(i instanceof Error || typeof i == "string") && a.recordException(i), a.setStatus({ code: W.ERROR }), a.end();
|
|
498
498
|
};
|
|
499
499
|
try {
|
|
500
500
|
const i = r(a);
|
|
@@ -509,7 +509,7 @@ function F(t, e, r, n = {}) {
|
|
|
509
509
|
}
|
|
510
510
|
});
|
|
511
511
|
}
|
|
512
|
-
const rt = (t) => t === "http:" || t === "https:",
|
|
512
|
+
const rt = (t) => t === "http:" || t === "https:", Q = (...t) => `/${t.map((r) => Jt(r, "/")).filter((r) => r).join("/")}`, Jt = (t, e) => {
|
|
513
513
|
const r = Ht(t, e);
|
|
514
514
|
return Kt(r, e);
|
|
515
515
|
}, Ht = (t, e) => {
|
|
@@ -536,11 +536,11 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
536
536
|
}, u = async (s) => {
|
|
537
537
|
const c = typeof t.ddn == "function" ? await t.ddn() : t.ddn;
|
|
538
538
|
if (!(s != null && s.url) && !c.url)
|
|
539
|
-
throw new
|
|
539
|
+
throw new C([], "`ddn.url` is required");
|
|
540
540
|
const l = new URL((s == null ? void 0 : s.url) || c.url);
|
|
541
541
|
if (!rt(l.protocol))
|
|
542
542
|
throw new Error(`invalid ddn.url protocol: ${l.protocol}`);
|
|
543
|
-
return l.pathname.endsWith("/sql") || (l.pathname =
|
|
543
|
+
return l.pathname.endsWith("/sql") || (l.pathname = Q(l.pathname, "v1/sql")), {
|
|
544
544
|
url: l.toString(),
|
|
545
545
|
headers: {
|
|
546
546
|
...(c == null ? void 0 : c.headers) ?? {},
|
|
@@ -550,7 +550,7 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
550
550
|
}, d = async (s, { llm: c, ai_primitives_llm: l, ddn: m, ...p }, w, _) => {
|
|
551
551
|
var A;
|
|
552
552
|
if (!p.interactions.length || p.interactions.every((E) => !E.user_message.text))
|
|
553
|
-
throw new
|
|
553
|
+
throw new C(
|
|
554
554
|
[],
|
|
555
555
|
"require at least 1 interaction with non-empty user message content"
|
|
556
556
|
);
|
|
@@ -565,10 +565,10 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
565
565
|
"promptql.request.ai_primitives_llm_provider",
|
|
566
566
|
l.provider
|
|
567
567
|
);
|
|
568
|
-
const
|
|
569
|
-
s.setAttribute("promptql.request.ddn_url",
|
|
568
|
+
const P = await u(m);
|
|
569
|
+
s.setAttribute("promptql.request.ddn_url", P.url);
|
|
570
570
|
const R = new URL(n);
|
|
571
|
-
return R.pathname =
|
|
571
|
+
return R.pathname = Q(R.pathname, "query"), r(R, {
|
|
572
572
|
..._,
|
|
573
573
|
method: "POST",
|
|
574
574
|
headers: {
|
|
@@ -577,10 +577,11 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
577
577
|
"Content-Type": "application/json"
|
|
578
578
|
},
|
|
579
579
|
body: JSON.stringify({
|
|
580
|
+
system_instructions: t.systemInstructions,
|
|
580
581
|
...p,
|
|
581
582
|
llm: c,
|
|
582
583
|
ai_primitives_llm: l,
|
|
583
|
-
ddn:
|
|
584
|
+
ddn: P,
|
|
584
585
|
timezone: b,
|
|
585
586
|
version: g,
|
|
586
587
|
stream: w
|
|
@@ -588,7 +589,7 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
588
589
|
}).then(ut);
|
|
589
590
|
};
|
|
590
591
|
return {
|
|
591
|
-
query: (s, c) =>
|
|
592
|
+
query: (s, c) => z(
|
|
592
593
|
e,
|
|
593
594
|
"PromptQL Query",
|
|
594
595
|
(l) => d(l, s, !1, c).then((m) => m.json()).then((m) => {
|
|
@@ -599,10 +600,10 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
599
600
|
}), p;
|
|
600
601
|
}),
|
|
601
602
|
{
|
|
602
|
-
kind:
|
|
603
|
+
kind: q.CLIENT
|
|
603
604
|
}
|
|
604
605
|
),
|
|
605
|
-
queryStream: async (s, c, l) =>
|
|
606
|
+
queryStream: async (s, c, l) => z(
|
|
606
607
|
e,
|
|
607
608
|
"PromptQL Query Stream",
|
|
608
609
|
async (m) => {
|
|
@@ -612,44 +613,46 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
612
613
|
return p;
|
|
613
614
|
const w = p.body.getReader(), _ = new TextDecoder("utf-8");
|
|
614
615
|
let g = "", b = 0;
|
|
615
|
-
const
|
|
616
|
+
const P = async (A) => {
|
|
616
617
|
if (!A && !g)
|
|
617
618
|
return;
|
|
618
619
|
let E = g + A;
|
|
619
|
-
g = "", E.startsWith(
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
620
|
+
g = "", E.startsWith(F) && (E = A.substring(F.length));
|
|
621
|
+
const T = E.split(F);
|
|
622
|
+
for (let S = 0; S < T.length; S++) {
|
|
623
|
+
const U = T[S], x = U.trim();
|
|
624
|
+
if (x[x.length - 1] !== "}") {
|
|
625
|
+
g += S > 0 ? `data: ${U}` : U;
|
|
623
626
|
return;
|
|
624
627
|
}
|
|
625
|
-
let
|
|
628
|
+
let $;
|
|
626
629
|
try {
|
|
627
|
-
|
|
630
|
+
$ = JSON.parse(x);
|
|
628
631
|
} catch {
|
|
629
|
-
g +=
|
|
632
|
+
g += S > 0 ? `data: ${x}` : x;
|
|
630
633
|
}
|
|
631
|
-
|
|
632
|
-
}
|
|
634
|
+
$ && await c($);
|
|
635
|
+
}
|
|
633
636
|
};
|
|
634
637
|
for (; !((R = l == null ? void 0 : l.signal) != null && R.aborted); ) {
|
|
635
638
|
const { value: A, done: E } = await w.read();
|
|
636
639
|
if (E)
|
|
637
640
|
break;
|
|
638
|
-
const
|
|
639
|
-
|
|
641
|
+
const T = _.decode(A);
|
|
642
|
+
T && (b += T.length, await P(T));
|
|
640
643
|
}
|
|
641
|
-
return m.setAttribute("http.response.body.size", b), g && await
|
|
644
|
+
return m.setAttribute("http.response.body.size", b), g && await P(""), p;
|
|
642
645
|
},
|
|
643
646
|
{
|
|
644
|
-
kind:
|
|
647
|
+
kind: q.CLIENT
|
|
645
648
|
}
|
|
646
649
|
),
|
|
647
|
-
executeProgram: async ({ ai_primitives_llm: s, artifacts: c, ddn: l, ...m }, p) =>
|
|
650
|
+
executeProgram: async ({ ai_primitives_llm: s, artifacts: c, ddn: l, ...m }, p) => z(
|
|
648
651
|
e,
|
|
649
652
|
"PromptQL Execute Program",
|
|
650
653
|
async (w) => {
|
|
651
654
|
if (!m.code)
|
|
652
|
-
throw new
|
|
655
|
+
throw new C([], "`code` is required");
|
|
653
656
|
w.setAttribute(
|
|
654
657
|
"promptql.request.artifacts_length",
|
|
655
658
|
(c == null ? void 0 : c.length) ?? 0
|
|
@@ -660,7 +663,7 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
660
663
|
const _ = await u(l);
|
|
661
664
|
w.setAttribute("promptql.request.ddn_url", _.url);
|
|
662
665
|
const g = new URL(n);
|
|
663
|
-
return g.pathname =
|
|
666
|
+
return g.pathname = Q(g.pathname, "execute_program"), r(g, {
|
|
664
667
|
...p,
|
|
665
668
|
headers: {
|
|
666
669
|
...a,
|
|
@@ -680,7 +683,7 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
680
683
|
}), b));
|
|
681
684
|
},
|
|
682
685
|
{
|
|
683
|
-
kind:
|
|
686
|
+
kind: q.CLIENT
|
|
684
687
|
}
|
|
685
688
|
)
|
|
686
689
|
};
|
|
@@ -778,8 +781,8 @@ const rt = (t) => t === "http:" || t === "https:", z = (...t) => `/${t.map((r) =
|
|
|
778
781
|
"plan"
|
|
779
782
|
];
|
|
780
783
|
export {
|
|
781
|
-
|
|
784
|
+
C as PromptQLError,
|
|
782
785
|
ne as createPromptQLClient,
|
|
783
786
|
Yt as createQueryChunks,
|
|
784
|
-
|
|
787
|
+
z as withActiveSpan
|
|
785
788
|
};
|
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 ke=Object.defineProperty;var qe=(h,g,I)=>g in h?ke(h,g,{enumerable:!0,configurable:!0,writable:!0,value:I}):h[g]=I;var bt=(h,g,I)=>qe(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:{},T="1.9.0",ct=/^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;function wt(t){var e=new Set([t]),r=new Set,n=t.match(ct);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(ct);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(T),At=T.split(".")[0],x=Symbol.for("opentelemetry.js.api."+At),j=I;function B(t,e,r,n){var o;n===void 0&&(n=!1);var a=j[x]=(o=j[x])!==null&&o!==void 0?o:{version:T};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!==T){var i=new Error("@opentelemetry/api: Registration of version v"+a.version+" for "+t+" does not match previously registered API v"+T);return r.error(i.stack||i.message),!1}return a[t]=e,r.debug("@opentelemetry/api: Registered a global for "+t+" v"+T+"."),!0}function C(t){var e,r,n=(e=j[x])===null||e===void 0?void 0:e.version;if(!(!n||!Et(n)))return(r=j[x])===null||r===void 0?void 0:r[t]}function G(t,e){e.debug("@opentelemetry/api: Unregistering a global for "+t+" v"+T+".");var r=j[x];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))},Tt=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 D("debug",this._namespace,e)},t.prototype.error=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("error",this._namespace,e)},t.prototype.info=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("info",this._namespace,e)},t.prototype.warn=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("warn",this._namespace,e)},t.prototype.verbose=function(){for(var e=[],r=0;r<arguments.length;r++)e[r]=arguments[r];return D("verbose",this._namespace,e)},t}();function D(t,e,r){var n=C("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 Ct(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=C("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=C("diag"),f=Ct((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 B("diag",f,r,!0)};r.setLogger=n,r.disable=function(){G(It,r)},r.createComponentLogger=function(o){return new Tt(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 st(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}(),V=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}(V),kt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.add=function(r,n){},e}(V),qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}(V),$t=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e.prototype.record=function(r,n){},e}(V),z=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}(z),Vt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(z),Qt=function(t){O(e,t);function e(){return t!==null&&t.apply(this,arguments)||this}return e}(z),Ft=new Dt,zt=new Ut,Wt=new qt,Ht=new $t,Xt=new kt,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))},W="context",ie=new ne,Q=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalContextManager=function(e){return B(W,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 C(W)||ie},t.prototype.disable=function(){this._getContextManager().disable(),G(W,w.instance())},t}(),H;(function(t){t[t.NONE=0]="NONE",t[t.SAMPLED=1]="SAMPLED"})(H||(H={}));var ft="0000000000000000",pt="00000000000000000000000000000000",ue={traceId:pt,spanId:ft,traceFlags:H.NONE},U=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}(),X=st("OpenTelemetry Context Key SPAN");function J(t){return t.getValue(X)||void 0}function ce(){return J(Q.getInstance().active())}function K(t,e){return t.setValue(X,e)}function se(t){return t.deleteValue(X)}function fe(t,e){return K(t,new U(e))}function lt(t){var e;return(e=J(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!==pt}function ge(t){return le.test(t)&&t!==ft}function dt(t){return de(t.traceId)&&ge(t.spanId)}function ve(t){return new U(t)}var Y=Q.getInstance(),gt=function(){function t(){}return t.prototype.startSpan=function(e,r,n){n===void 0&&(n=Y.active());var o=!!(r!=null&&r.root);if(o)return new U;var a=n&<(n);return he(a)&&dt(a)?new U(a):new U},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??Y.active(),v=this.startSpan(e,a,l),m=K(l,v);return Y.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 gt,_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 gt},t}(),be=new me,vt=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}(),k;(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"})(k||(k={}));var Z;(function(t){t[t.UNSET=0]="UNSET",t[t.OK=1]="OK",t[t.ERROR=2]="ERROR"})(Z||(Z={})),Q.getInstance(),w.instance();var we=function(){function t(){}return t.prototype.getMeter=function(e,r,n){return Ft},t}(),Ee=new we,tt="metrics",Ae=function(){function t(){}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalMeterProvider=function(e){return B(tt,e,w.instance())},t.prototype.getMeterProvider=function(){return C(tt)||Ee},t.prototype.getMeter=function(e,r,n){return this.getMeterProvider().getMeter(e,r,n)},t.prototype.disable=function(){G(tt,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}(),et=st("OpenTelemetry Baggage Key");function ht(t){return t.getValue(et)||void 0}function Pe(){return ht(Q.getInstance().active())}function Te(t,e){return t.setValue(et,e)}function Ce(t){return t.deleteValue(et)}var rt="propagation",Oe=new Ne,Re=function(){function t(){this.createBaggage=jt,this.getBaggage=ht,this.getActiveBaggage=Pe,this.setBaggage=Te,this.deleteBaggage=Ce}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalPropagator=function(e){return B(rt,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(){G(rt,w.instance())},t.prototype._getGlobalPropagator=function(){return C(rt)||Oe},t}();Re.getInstance();var nt="trace",Ie=function(){function t(){this._proxyTracerProvider=new vt,this.wrapSpanContext=ve,this.isSpanContextValid=dt,this.deleteSpan=se,this.getSpan=J,this.getActiveSpan=ce,this.getSpanContext=lt,this.setSpan=K,this.setSpanContext=fe}return t.getInstance=function(){return this._instance||(this._instance=new t),this._instance},t.prototype.setGlobalTracerProvider=function(e){var r=B(nt,this._proxyTracerProvider,w.instance());return r&&this._proxyTracerProvider.setDelegate(e),r},t.prototype.getTracerProvider=function(){return C(nt)||this._proxyTracerProvider},t.prototype.getTracer=function(e,r){return this.getTracerProvider().getTracer(e,r)},t.prototype.disable=function(){G(nt,w.instance()),this._proxyTracerProvider=new vt},t}(),Me=Ie.getInstance();const ot="data: ",yt=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=>yt(t).then(e=>e.json());function F(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:Z.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 _t=t=>t==="http:"||t==="https:",at=(...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(!_t(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(!_t(p.protocol))throw new Error(`invalid ddn.url protocol: ${p.protocol}`);return p.pathname.endsWith("/sql")||(p.pathname=at(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=at(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(yt)};return{query:(u,s)=>F(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:k.CLIENT}),queryStream:async(u,s,p)=>F(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(ot)&&(R=P.substring(ot.length));const S=R.split(ot);for(let $=0;$<S.length;$++){const it=S[$],L=it.trim();if(L[L.length-1]!=="}"){_+=$>0?`data: ${it}`:it;return}let ut;try{ut=JSON.parse(L)}catch{_+=$>0?`data: ${L}`:L}ut&&await s(ut)}};for(;!((M=p==null?void 0:p.signal)!=null&&M.aborted);){const{value:P,done:R}=await N.read();if(R)break;const S=E.decode(P);S&&(A+=S.length,await q(S))}return b.setAttribute("http.response.body.size",A),_&&await q(""),d},{kind:k.CLIENT}),executeProgram:async({ai_primitives_llm:u,artifacts:s,ddn:p,...b},d)=>F(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=at(_.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:k.CLIENT})}},mt=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=()=>mt({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=mt,h.withActiveSpan=F,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
|
*
|