@hasura/promptql 2.0.0-alpha.1 → 2.0.0-alpha.3

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.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var h=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var G=(e,t)=>{for(var r in t)h(e,r,{get:t[r],enumerable:!0})},D=(e,t,r,_)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of q(t))!z.call(e,a)&&a!==r&&h(e,a,{get:()=>t[a],enumerable:!(_=j(t,a))||_.enumerable});return e};var Q=e=>D(h({},"__esModule",{value:!0}),e);var fe={};G(fe,{PromptQLSdk:()=>v,createApolloClient:()=>k,createPromptQLAuthTokenGenerator:()=>B,diffThreadEvents:()=>K,getAgentGeneratedResponse:()=>Y,getAgentInteractionDecisionAcceptInteractionEvent:()=>X,getAgentInteractionDecisionDeclineInteractionEvent:()=>ee,getAgentMessageProcessingStartedEvent:()=>Z,getAgentOrchestratorContextExplorerCompletedEvent:()=>ie,getAgentOrchestratorContextExplorerStartedEvent:()=>ne,getAgentOrchestratorContextExplorerUpdateEvent:()=>d,getAgentOrchestratorSchemaExplorerCodeExecutionCompleteEvent:()=>me,getAgentOrchestratorSchemaExplorerCodeOutputAnalyzedEvent:()=>be,getAgentOrchestratorSchemaExplorerCompletedEvent:()=>le,getAgentOrchestratorSchemaExplorerGeneratedCodeEvent:()=>Ie,getAgentOrchestratorSchemaExplorerSchemaExploredEvent:()=>Me,getAgentOrchestratorSchemaExplorerStartedEvent:()=>ce,getAgentOrchestratorSchemaExplorerUpdateEvent:()=>y,getAgentOrchestratorSchemaTasksGeneratedEvent:()=>de,getAgentOrchestratorSchemaTasksGenerationCompletedEvent:()=>ye,getAgentOrchestratorSchemaTasksGenerationStartedEvent:()=>ue,getAgentOrchestratorSchemaTasksGenerationUpdateEvent:()=>S,getAgentOrchestratorStepProgrammerEvent:()=>Se,getAgentOrchestratorStepSavedProgramRunnerEvent:()=>he,getAgentOrchestratorStepUiProgrammerEvent:()=>ge,getAgentOrchestratorStepUpdateEvent:()=>g,getAgentOrchestratorUpdateEvent:()=>O,getAgentOrchestratorWikiExplorerCompletedEvent:()=>oe,getAgentOrchestratorWikiExplorerStartedEvent:()=>pe,getAgentOrchestratorWikiExplorerUpdateEvent:()=>M,getAgentOrchestratorWikiInfoGeneratedEvent:()=>se,getAgentPlanGenerationStartedEvent:()=>J,getAgentPlanStepGeneratedEvent:()=>$,getAgentPlanningDecisionCompletedEvent:()=>re,getAgentPlanningDecisionPlanningRequiredEvent:()=>ae,getAgentPlanningDecisionStartedEvent:()=>te,getAgentPlanningDecisionUpdateEvent:()=>I,getAgentStartingOrchestratorEvent:()=>_e,getUserCancelEvent:()=>L,getUserMessageEvent:()=>H,isEnrichTokenMutationResponse:()=>A,isLuxAuthPromptQLTokenFailedResponse:()=>f,isLuxAuthPromptQLTokenSuccessResponse:()=>x});module.exports=Q(fe);function c(e){return e=e?e.trim():"",e?e[e.length-1]==="/"?e.slice(0,e.length-1):e:""}function f(e){return e!=null&&typeof e=="object"&&"status"in e&&e.status==="failure"}function x(e){return e!=null&&typeof e=="object"&&"status"in e&&e.status==="success"&&"token"in e}function A(e){return e!=null&&typeof e=="object"&&("data"in e&&!!e.data&&typeof e.data=="object"&&"enrich_token"in e.data&&!!e.data.enrich_token&&typeof e.data.enrich_token=="object"&&"userDirectoryJWT"in e.data.enrich_token||"errors"in e&&Array.isArray(e.errors))}function B(e){let r=`${c(e.authHost)||"https://auth.pro.hasura.io"}/ddn/promptql/token`,_={token:"",expiry:new Date(0)},a=e.fetch||fetch,o=async()=>{let n=await a(r,{method:"POST",headers:{Authorization:`pat ${e.serviceAccountToken}`}});switch(n.status){case 200:{let i=await n.json();if(x(i))return i;throw new Error("malformed token response")}case 401:{let i=await n.json();throw f(i)?new Error(i?.error||n.statusText):new Error(n.statusText)}default:{let i=await n.text();throw new Error(i)}}},s=async n=>{let i=await a(e.promptqlGraphQLUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:"mutation EnrichToken($luxJWT: String!, $projectId: uuid!) { enrich_token(luxJWT: $luxJWT, projectId: $projectId) { userDirectoryJWT }}",variables:{luxJWT:n.token,projectId:e.projectId},operationName:"EnrichToken"})});switch(i.status){case 200:{let p=await i.json();if(!A(p)||!p.data?.enrich_token?.userDirectoryJWT)throw new Error(`malformed promptql token response: ${JSON.stringify(p)}`);return p.data.enrich_token.userDirectoryJWT}default:{let p=await i.text();throw new Error(p)}}};return async()=>{if(_.expiry.getTime()<=Date.now()-1e3){let n=await o(),i=await s(n);_.token=i;let p=new Date(n.expiry);if(Number.isNaN(p.getTime())){let C=new Date;C.setHours(1),_.expiry=C}else _.expiry=p}return _.token}}var u=require("@apollo/client"),T=require("@apollo/client/link/context"),E=require("@apollo/client/link/subscriptions"),P=require("graphql"),R=require("graphql-ws"),k=e=>{let t=new u.HttpLink({uri:e.url,fetch:e.fetch,headers:e.headers}),r=async s=>{let n=await e.getAuthToken();return{headers:{...s,Authorization:`Bearer ${n}`}}},_=new E.GraphQLWsLink((0,R.createClient)({url:e.url,connectionParams:()=>r(e.headers)})),a=new T.SetContextLink(({headers:s})=>r(s)),o=u.ApolloLink.split(({operationType:s})=>s===P.OperationTypeNode.SUBSCRIPTION,_,a.concat(t));return new u.ApolloClient({link:o,cache:new u.InMemoryCache})};var U={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"SendThreadMessage"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"message"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"timezone"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"threadId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"send_thread_message"},arguments:[{kind:"Argument",name:{kind:"Name",value:"threadId"},value:{kind:"Variable",name:{kind:"Name",value:"threadId"}}},{kind:"Argument",name:{kind:"Name",value:"timezone"},value:{kind:"Variable",name:{kind:"Name",value:"timezone"}}},{kind:"Argument",name:{kind:"Name",value:"message"},value:{kind:"Variable",name:{kind:"Name",value:"message"}}},{kind:"Argument",name:{kind:"Name",value:"buildFqdn"},value:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_event_id"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]},w={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"StartThread"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"message"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"projectId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"timezone"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildId"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"start_thread"},arguments:[{kind:"Argument",name:{kind:"Name",value:"message"},value:{kind:"Variable",name:{kind:"Name",value:"message"}}},{kind:"Argument",name:{kind:"Name",value:"projectId"},value:{kind:"Variable",name:{kind:"Name",value:"projectId"}}},{kind:"Argument",name:{kind:"Name",value:"timezone"},value:{kind:"Variable",name:{kind:"Name",value:"timezone"}}},{kind:"Argument",name:{kind:"Name",value:"buildId"},value:{kind:"Variable",name:{kind:"Name",value:"buildId"}}},{kind:"Argument",name:{kind:"Name",value:"buildFqdn"},value:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]},V={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetProjectInfo"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"projectId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"uuid"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"get_project_info"},arguments:[{kind:"Argument",name:{kind:"Name",value:"projectId"},value:{kind:"Variable",name:{kind:"Name",value:"projectId"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"projectId"}},{kind:"Field",name:{kind:"Name",value:"projectName"}},{kind:"Field",name:{kind:"Name",value:"promptqlConsoleUrl"}}]}}]}}]},W={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetThreads"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"where"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"threads_v2_bool_exp"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"limit"}},type:{kind:"NamedType",name:{kind:"Name",value:"Int"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"offset"}},type:{kind:"NamedType",name:{kind:"Name",value:"Int"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"order_by"}},type:{kind:"ListType",type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"threads_v2_order_by"}}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"threads_v2"},arguments:[{kind:"Argument",name:{kind:"Name",value:"where"},value:{kind:"Variable",name:{kind:"Name",value:"where"}}},{kind:"Argument",name:{kind:"Name",value:"limit"},value:{kind:"Variable",name:{kind:"Name",value:"limit"}}},{kind:"Argument",name:{kind:"Name",value:"offset"},value:{kind:"Variable",name:{kind:"Name",value:"offset"}}},{kind:"Argument",name:{kind:"Name",value:"order_by"},value:{kind:"Variable",name:{kind:"Name",value:"order_by"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"FragmentSpread",name:{kind:"Name",value:"Thread"}}]}}]}},{kind:"FragmentDefinition",name:{kind:"Name",value:"Thread"},typeCondition:{kind:"NamedType",name:{kind:"Name",value:"threads_v2"}},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"build_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"relevant_event_count"}},{kind:"Field",name:{kind:"Name",value:"visibility"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}},{kind:"Field",name:{kind:"Name",value:"user"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"promptql_user_id"}},{kind:"Field",name:{kind:"Name",value:"display_name"}},{kind:"Field",name:{kind:"Name",value:"email"}},{kind:"Field",name:{kind:"Name",value:"is_active"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}}]}}]}}]},F={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetThreadById"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"id"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"uuid"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"threads_v2_by_pk"},arguments:[{kind:"Argument",name:{kind:"Name",value:"thread_id"},value:{kind:"Variable",name:{kind:"Name",value:"id"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"FragmentSpread",name:{kind:"Name",value:"Thread"}}]}}]}},{kind:"FragmentDefinition",name:{kind:"Name",value:"Thread"},typeCondition:{kind:"NamedType",name:{kind:"Name",value:"threads_v2"}},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"build_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"relevant_event_count"}},{kind:"Field",name:{kind:"Name",value:"visibility"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}},{kind:"Field",name:{kind:"Name",value:"user"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"promptql_user_id"}},{kind:"Field",name:{kind:"Name",value:"display_name"}},{kind:"Field",name:{kind:"Name",value:"email"}},{kind:"Field",name:{kind:"Name",value:"is_active"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}}]}}]}}]},N={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"subscription",name:{kind:"Name",value:"SubscribeThreadEvents"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"where"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"thread_events_bool_exp"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_events"},arguments:[{kind:"Argument",name:{kind:"Name",value:"where"},value:{kind:"Variable",name:{kind:"Name",value:"where"}}},{kind:"Argument",name:{kind:"Name",value:"order_by"},value:{kind:"ObjectValue",fields:[{kind:"ObjectField",name:{kind:"Name",value:"thread_event_id"},value:{kind:"EnumValue",value:"asc"}}]}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_event_id"}},{kind:"Field",name:{kind:"Name",value:"event_data"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]};var l=class{_options;constructor(t){this._options=t}info(){return this._options.client.query({query:V,variables:{projectId:this._options.projectId}}).then(t=>{if(!t.data?.get_project_info)throw new Error(t.error?.message||"Project not found");return t.data.get_project_info})}};var b=require("rxjs/operators");var m=class{_options;constructor(t){this._options=t}list(t){return this._options.client.query({query:W,variables:{...t,limit:t.limit&&t.limit>0?t.limit:10},fetchPolicy:"no-cache"}).then(r=>r.data?.threads_v2??[])}get(t){if(!t)throw new Error("threadId is required");return this._options.client.query({query:F,variables:{id:t},fetchPolicy:"no-cache"}).then(r=>r.data?.threads_v2_by_pk)}start(t){if(!t.message.trim())throw new Error("message must not be empty");return this._options.client.mutate({mutation:w,variables:{...t,projectId:this._options.projectId,buildId:t.buildId||this._options.buildId,timezone:t.timezone||this._options.defaultTimezone}}).then(r=>{if(!r.data?.start_thread)throw new Error(r.error?.message||"Failed to start thread");return r.data.start_thread})}sendMessage(t){if(!t.threadId)throw new Error("threadId is required");if(!t.message.trim())throw new Error("message must not be empty");return this._options.client.mutate({mutation:U,variables:{...t,timezone:t.timezone||this._options.defaultTimezone}}).then(r=>{if(!r.data?.send_thread_message)throw new Error(r.error?.message||"Failed to continue thread");return r.data.send_thread_message})}subscribeEvents(t,r){if(!t)throw new Error("threadId is required");if(r!=null)try{Number.parseInt(r)}catch{throw new Error("invalid threadEventId")}let _={thread_id:{_eq:t}};return r&&(_.thread_event_id={_gte:r}),this._options.client.subscribe({query:N,variables:{where:_}}).pipe((0,b.map)(a=>{if(a.error)throw a.error;return a.data?.thread_events??[]}))}streamEvents(t,r){return this.subscribeEvents(t,r).pipe((0,b.takeWhile)(_=>{for(let a=_.length-1;a>0;a--){let o=_[a];if("AgentMessage"in o.event_data&&"InteractionFinished"in o.event_data.AgentMessage.update)return!1}return!0},!0))}};var v=class{constructor(t){let r=c(t.promptqlBaseUrl)||"https://promptql.ddn.hasura.app";if(!t.projectId)throw new Error("projectId must not be empty. You can find it in Project Settings > General Settings tab on https://promptql.console.hasura.io.");if(!t.serviceAccountToken)throw new Error("serviceAccountToken must not be empty. If you haven't created any service account token yet, check out Hasura docs https://hasura.io/docs/3.0/project-configuration/project-management/service-accounts/#how-to-create-service-account");let _=r.endsWith("/v1/graphql")?r:`${r}/playground-v2-hge/v1/graphql`,a=B({promptqlGraphQLUrl:_,authHost:t.authHost,projectId:t.projectId,serviceAccountToken:t.serviceAccountToken,fetch:t.fetch});this._options={projectId:t.projectId,buildId:t.buildId,client:k({getAuthToken:a,url:_,fetch:t.fetch,headers:t.headers}),defaultTimezone:t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone},this.thread=new m(this._options),this.project=new l(this._options)}_options;thread;project};function K(e,t){if(!t.length)return[];if(!e.length)return t;let r=e[e.length-1];if(!r)return t;let _=BigInt(r.thread_event_id),a=t.length-1;for(;a>=0;a--){let o=t[a];if(BigInt(o?.thread_event_id)<=_)break}return t.slice(a+1)}function H(e){return"UserMessage"in e?e.UserMessage:null}function L(e){return"UserCancel"in e?e.UserCancel:null}function J(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("PlanGenerationStarted"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.PlanGenerationStarted}function $(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("PlanStepGenerated"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.PlanStepGenerated}function Y(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("GeneratedResponse"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.GeneratedResponse}function Z(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("ProcessingStarted"in e.AgentMessage.update.MessageProcessingUpdate.update)?null:e.AgentMessage.update.MessageProcessingUpdate.update.ProcessingStarted}function X(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("InteractionDecision"in e.AgentMessage.update.MessageProcessingUpdate.update)||!("AcceptInteraction"in e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision)?null:e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision.AcceptInteraction}function ee(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("InteractionDecision"in e.AgentMessage.update.MessageProcessingUpdate.update)||!("DeclineInteraction"in e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision)?null:e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision.DeclineInteraction}function I(e){return!("AgentMessage"in e)||!("PlanningDecisionUpdate"in e.AgentMessage.update)?null:e.AgentMessage.update.PlanningDecisionUpdate.update}function te(e){let t=I(e);return!t||!("Started"in t)?null:t.Started}function re(e){let t=I(e);return!t||!("Completed"in t)?null:t.Completed}function ae(e){let t=I(e);return!t||!("PlanningRequired"in t)?null:t.PlanningRequired}function _e(e){return!("AgentMessage"in e)||!("StartingOrchestrator"in e.AgentMessage.update)?null:e.AgentMessage.update.StartingOrchestrator}function O(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)?null:e.AgentMessage.update.OrchestratorUpdate.update}function d(e){let t=O(e);return!t||!("ContextExplorerUpdate"in t)?null:t.ContextExplorerUpdate.update}function ne(e){let t=d(e);return!t||!("Started"in t)?null:t.Started}function ie(e){let t=d(e);return!t||!("Completed"in t)?null:t.Completed}function M(e){let t=d(e);return!t||!("WikiExplorerUpdate"in t)?null:t.WikiExplorerUpdate.update}function pe(e){let t=M(e);return!t||!("Started"in t)?null:t.Started}function oe(e){let t=M(e);return!t||!("Completed"in t)?null:t.Completed}function se(e){let t=M(e);return!t||!("WikiInfoGenerated"in t)?null:t.WikiInfoGenerated}function S(e){let t=d(e);return!t||!("SchemaTasksGenerationUpdate"in t)?null:t.SchemaTasksGenerationUpdate.update}function ue(e){let t=S(e);return!t||!("Started"in t)?null:t.Started}function ye(e){let t=S(e);return!t||!("Completed"in t)?null:t.Completed}function de(e){let t=S(e);return!t||!("TasksGenerated"in t)?null:t.TasksGenerated}function y(e){let t=d(e);return!t||!("SchemaExplorerUpdate"in t)?null:t.SchemaExplorerUpdate.update}function ce(e){let t=y(e);return!t||!("Started"in t)?null:t.Started}function le(e){let t=y(e);return!t||!("Completed"in t)?null:t.Completed}function me(e){let t=y(e);return!t||!("CodeExecutionComplete"in t)?null:t.CodeExecutionComplete}function be(e){let t=y(e);return!t||!("CodeOutputAnalyzed"in t)?null:t.CodeOutputAnalyzed}function Ie(e){let t=y(e);return!t||!("GeneratedCode"in t)?null:t.GeneratedCode}function Me(e){let t=y(e);return!t||!("SchemaExplored"in t)?null:t.SchemaExplored}function g(e){let t=O(e);return!t||!("StepUpdate"in t)?null:t.StepUpdate}function Se(e){let t=g(e);return!t||!("Programmer"in t.update)?null:t.update.Programmer}function ge(e){let t=g(e);return!t||!("UiProgrammer"in t.update)?null:t.update.UiProgrammer}function he(e){let t=g(e);return!t||!("SavedProgramRunner"in t.update)?null:t.update.SavedProgramRunner}0&&(module.exports={PromptQLSdk,createApolloClient,createPromptQLAuthTokenGenerator,diffThreadEvents,getAgentGeneratedResponse,getAgentInteractionDecisionAcceptInteractionEvent,getAgentInteractionDecisionDeclineInteractionEvent,getAgentMessageProcessingStartedEvent,getAgentOrchestratorContextExplorerCompletedEvent,getAgentOrchestratorContextExplorerStartedEvent,getAgentOrchestratorContextExplorerUpdateEvent,getAgentOrchestratorSchemaExplorerCodeExecutionCompleteEvent,getAgentOrchestratorSchemaExplorerCodeOutputAnalyzedEvent,getAgentOrchestratorSchemaExplorerCompletedEvent,getAgentOrchestratorSchemaExplorerGeneratedCodeEvent,getAgentOrchestratorSchemaExplorerSchemaExploredEvent,getAgentOrchestratorSchemaExplorerStartedEvent,getAgentOrchestratorSchemaExplorerUpdateEvent,getAgentOrchestratorSchemaTasksGeneratedEvent,getAgentOrchestratorSchemaTasksGenerationCompletedEvent,getAgentOrchestratorSchemaTasksGenerationStartedEvent,getAgentOrchestratorSchemaTasksGenerationUpdateEvent,getAgentOrchestratorStepProgrammerEvent,getAgentOrchestratorStepSavedProgramRunnerEvent,getAgentOrchestratorStepUiProgrammerEvent,getAgentOrchestratorStepUpdateEvent,getAgentOrchestratorUpdateEvent,getAgentOrchestratorWikiExplorerCompletedEvent,getAgentOrchestratorWikiExplorerStartedEvent,getAgentOrchestratorWikiExplorerUpdateEvent,getAgentOrchestratorWikiInfoGeneratedEvent,getAgentPlanGenerationStartedEvent,getAgentPlanStepGeneratedEvent,getAgentPlanningDecisionCompletedEvent,getAgentPlanningDecisionPlanningRequiredEvent,getAgentPlanningDecisionStartedEvent,getAgentPlanningDecisionUpdateEvent,getAgentStartingOrchestratorEvent,getUserCancelEvent,getUserMessageEvent,isEnrichTokenMutationResponse,isLuxAuthPromptQLTokenFailedResponse,isLuxAuthPromptQLTokenSuccessResponse});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ function d(e){return e=e?e.trim():"",e?e[e.length-1]==="/"?e.slice(0,e.length-1):e:""}function g(e){return e!=null&&typeof e=="object"&&"status"in e&&e.status==="failure"}function h(e){return e!=null&&typeof e=="object"&&"status"in e&&e.status==="success"&&"token"in e}function f(e){return e!=null&&typeof e=="object"&&("data"in e&&!!e.data&&typeof e.data=="object"&&"enrich_token"in e.data&&!!e.data.enrich_token&&typeof e.data.enrich_token=="object"&&"userDirectoryJWT"in e.data.enrich_token||"errors"in e&&Array.isArray(e.errors))}function x(e){let r=`${d(e.authHost)||"https://auth.pro.hasura.io"}/ddn/promptql/token`,_={token:"",expiry:new Date(0)},a=e.fetch||fetch,o=async()=>{let n=await a(r,{method:"POST",headers:{Authorization:`pat ${e.serviceAccountToken}`}});switch(n.status){case 200:{let i=await n.json();if(h(i))return i;throw new Error("malformed token response")}case 401:{let i=await n.json();throw g(i)?new Error(i?.error||n.statusText):new Error(n.statusText)}default:{let i=await n.text();throw new Error(i)}}},s=async n=>{let i=await a(e.promptqlGraphQLUrl,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:"mutation EnrichToken($luxJWT: String!, $projectId: uuid!) { enrich_token(luxJWT: $luxJWT, projectId: $projectId) { userDirectoryJWT }}",variables:{luxJWT:n.token,projectId:e.projectId},operationName:"EnrichToken"})});switch(i.status){case 200:{let p=await i.json();if(!f(p)||!p.data?.enrich_token?.userDirectoryJWT)throw new Error(`malformed promptql token response: ${JSON.stringify(p)}`);return p.data.enrich_token.userDirectoryJWT}default:{let p=await i.text();throw new Error(p)}}};return async()=>{if(_.expiry.getTime()<=Date.now()-1e3){let n=await o(),i=await s(n);_.token=i;let p=new Date(n.expiry);if(Number.isNaN(p.getTime())){let S=new Date;S.setHours(1),_.expiry=S}else _.expiry=p}return _.token}}import{ApolloClient as R,ApolloLink as U,HttpLink as w,InMemoryCache as V}from"@apollo/client";import{SetContextLink as W}from"@apollo/client/link/context";import{GraphQLWsLink as F}from"@apollo/client/link/subscriptions";import{OperationTypeNode as N}from"graphql";import{createClient as j}from"graphql-ws";var A=e=>{let t=new w({uri:e.url,fetch:e.fetch,headers:e.headers}),r=async s=>{let n=await e.getAuthToken();return{headers:{...s,Authorization:`Bearer ${n}`}}},_=new F(j({url:e.url,connectionParams:()=>r(e.headers)})),a=new W(({headers:s})=>r(s)),o=U.split(({operationType:s})=>s===N.SUBSCRIPTION,_,a.concat(t));return new R({link:o,cache:new V})};var B={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"SendThreadMessage"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"message"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"timezone"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"threadId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"send_thread_message"},arguments:[{kind:"Argument",name:{kind:"Name",value:"threadId"},value:{kind:"Variable",name:{kind:"Name",value:"threadId"}}},{kind:"Argument",name:{kind:"Name",value:"timezone"},value:{kind:"Variable",name:{kind:"Name",value:"timezone"}}},{kind:"Argument",name:{kind:"Name",value:"message"},value:{kind:"Variable",name:{kind:"Name",value:"message"}}},{kind:"Argument",name:{kind:"Name",value:"buildFqdn"},value:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_event_id"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]},k={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"mutation",name:{kind:"Name",value:"StartThread"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"message"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"projectId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"timezone"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildId"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}},type:{kind:"NamedType",name:{kind:"Name",value:"String"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"start_thread"},arguments:[{kind:"Argument",name:{kind:"Name",value:"message"},value:{kind:"Variable",name:{kind:"Name",value:"message"}}},{kind:"Argument",name:{kind:"Name",value:"projectId"},value:{kind:"Variable",name:{kind:"Name",value:"projectId"}}},{kind:"Argument",name:{kind:"Name",value:"timezone"},value:{kind:"Variable",name:{kind:"Name",value:"timezone"}}},{kind:"Argument",name:{kind:"Name",value:"buildId"},value:{kind:"Variable",name:{kind:"Name",value:"buildId"}}},{kind:"Argument",name:{kind:"Name",value:"buildFqdn"},value:{kind:"Variable",name:{kind:"Name",value:"buildFqdn"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]},v={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetProjectInfo"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"projectId"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"uuid"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"get_project_info"},arguments:[{kind:"Argument",name:{kind:"Name",value:"projectId"},value:{kind:"Variable",name:{kind:"Name",value:"projectId"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"projectId"}},{kind:"Field",name:{kind:"Name",value:"projectName"}},{kind:"Field",name:{kind:"Name",value:"promptqlConsoleUrl"}}]}}]}}]},O={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetThreads"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"where"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"threads_v2_bool_exp"}}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"limit"}},type:{kind:"NamedType",name:{kind:"Name",value:"Int"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"offset"}},type:{kind:"NamedType",name:{kind:"Name",value:"Int"}}},{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"order_by"}},type:{kind:"ListType",type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"threads_v2_order_by"}}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"threads_v2"},arguments:[{kind:"Argument",name:{kind:"Name",value:"where"},value:{kind:"Variable",name:{kind:"Name",value:"where"}}},{kind:"Argument",name:{kind:"Name",value:"limit"},value:{kind:"Variable",name:{kind:"Name",value:"limit"}}},{kind:"Argument",name:{kind:"Name",value:"offset"},value:{kind:"Variable",name:{kind:"Name",value:"offset"}}},{kind:"Argument",name:{kind:"Name",value:"order_by"},value:{kind:"Variable",name:{kind:"Name",value:"order_by"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"FragmentSpread",name:{kind:"Name",value:"Thread"}}]}}]}},{kind:"FragmentDefinition",name:{kind:"Name",value:"Thread"},typeCondition:{kind:"NamedType",name:{kind:"Name",value:"threads_v2"}},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"build_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"relevant_event_count"}},{kind:"Field",name:{kind:"Name",value:"visibility"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}},{kind:"Field",name:{kind:"Name",value:"user"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"promptql_user_id"}},{kind:"Field",name:{kind:"Name",value:"display_name"}},{kind:"Field",name:{kind:"Name",value:"email"}},{kind:"Field",name:{kind:"Name",value:"is_active"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}}]}}]}}]},C={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"query",name:{kind:"Name",value:"GetThreadById"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"id"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"uuid"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"threads_v2_by_pk"},arguments:[{kind:"Argument",name:{kind:"Name",value:"thread_id"},value:{kind:"Variable",name:{kind:"Name",value:"id"}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"FragmentSpread",name:{kind:"Name",value:"Thread"}}]}}]}},{kind:"FragmentDefinition",name:{kind:"Name",value:"Thread"},typeCondition:{kind:"NamedType",name:{kind:"Name",value:"threads_v2"}},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_id"}},{kind:"Field",name:{kind:"Name",value:"build_id"}},{kind:"Field",name:{kind:"Name",value:"title"}},{kind:"Field",name:{kind:"Name",value:"relevant_event_count"}},{kind:"Field",name:{kind:"Name",value:"visibility"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}},{kind:"Field",name:{kind:"Name",value:"user"},selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"promptql_user_id"}},{kind:"Field",name:{kind:"Name",value:"display_name"}},{kind:"Field",name:{kind:"Name",value:"email"}},{kind:"Field",name:{kind:"Name",value:"is_active"}},{kind:"Field",name:{kind:"Name",value:"created_at"}},{kind:"Field",name:{kind:"Name",value:"updated_at"}}]}}]}}]},T={kind:"Document",definitions:[{kind:"OperationDefinition",operation:"subscription",name:{kind:"Name",value:"SubscribeThreadEvents"},variableDefinitions:[{kind:"VariableDefinition",variable:{kind:"Variable",name:{kind:"Name",value:"where"}},type:{kind:"NonNullType",type:{kind:"NamedType",name:{kind:"Name",value:"thread_events_bool_exp"}}}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_events"},arguments:[{kind:"Argument",name:{kind:"Name",value:"where"},value:{kind:"Variable",name:{kind:"Name",value:"where"}}},{kind:"Argument",name:{kind:"Name",value:"order_by"},value:{kind:"ObjectValue",fields:[{kind:"ObjectField",name:{kind:"Name",value:"thread_event_id"},value:{kind:"EnumValue",value:"asc"}}]}}],selectionSet:{kind:"SelectionSet",selections:[{kind:"Field",name:{kind:"Name",value:"thread_event_id"}},{kind:"Field",name:{kind:"Name",value:"event_data"}},{kind:"Field",name:{kind:"Name",value:"created_at"}}]}}]}}]};var c=class{_options;constructor(t){this._options=t}info(){return this._options.client.query({query:v,variables:{projectId:this._options.projectId}}).then(t=>{if(!t.data?.get_project_info)throw new Error(t.error?.message||"Project not found");return t.data.get_project_info})}};import{map as q,takeWhile as z}from"rxjs/operators";var l=class{_options;constructor(t){this._options=t}list(t){return this._options.client.query({query:O,variables:{...t,limit:t.limit&&t.limit>0?t.limit:10},fetchPolicy:"no-cache"}).then(r=>r.data?.threads_v2??[])}get(t){if(!t)throw new Error("threadId is required");return this._options.client.query({query:C,variables:{id:t},fetchPolicy:"no-cache"}).then(r=>r.data?.threads_v2_by_pk)}start(t){if(!t.message.trim())throw new Error("message must not be empty");return this._options.client.mutate({mutation:k,variables:{...t,projectId:this._options.projectId,buildId:t.buildId||this._options.buildId,timezone:t.timezone||this._options.defaultTimezone}}).then(r=>{if(!r.data?.start_thread)throw new Error(r.error?.message||"Failed to start thread");return r.data.start_thread})}sendMessage(t){if(!t.threadId)throw new Error("threadId is required");if(!t.message.trim())throw new Error("message must not be empty");return this._options.client.mutate({mutation:B,variables:{...t,timezone:t.timezone||this._options.defaultTimezone}}).then(r=>{if(!r.data?.send_thread_message)throw new Error(r.error?.message||"Failed to continue thread");return r.data.send_thread_message})}subscribeEvents(t,r){if(!t)throw new Error("threadId is required");if(r!=null)try{Number.parseInt(r)}catch{throw new Error("invalid threadEventId")}let _={thread_id:{_eq:t}};return r&&(_.thread_event_id={_gte:r}),this._options.client.subscribe({query:T,variables:{where:_}}).pipe(q(a=>{if(a.error)throw a.error;return a.data?.thread_events??[]}))}streamEvents(t,r){return this.subscribeEvents(t,r).pipe(z(_=>{for(let a=_.length-1;a>0;a--){let o=_[a];if("AgentMessage"in o.event_data&&"InteractionFinished"in o.event_data.AgentMessage.update)return!1}return!0},!0))}};var E=class{constructor(t){let r=d(t.promptqlBaseUrl)||"https://promptql.ddn.hasura.app";if(!t.projectId)throw new Error("projectId must not be empty. You can find it in Project Settings > General Settings tab on https://promptql.console.hasura.io.");if(!t.serviceAccountToken)throw new Error("serviceAccountToken must not be empty. If you haven't created any service account token yet, check out Hasura docs https://hasura.io/docs/3.0/project-configuration/project-management/service-accounts/#how-to-create-service-account");let _=r.endsWith("/v1/graphql")?r:`${r}/playground-v2-hge/v1/graphql`,a=x({promptqlGraphQLUrl:_,authHost:t.authHost,projectId:t.projectId,serviceAccountToken:t.serviceAccountToken,fetch:t.fetch});this._options={projectId:t.projectId,buildId:t.buildId,client:A({getAuthToken:a,url:_,fetch:t.fetch,headers:t.headers}),defaultTimezone:t.timezone||Intl.DateTimeFormat().resolvedOptions().timeZone},this.thread=new l(this._options),this.project=new c(this._options)}_options;thread;project};function be(e,t){if(!t.length)return[];if(!e.length)return t;let r=e[e.length-1];if(!r)return t;let _=BigInt(r.thread_event_id),a=t.length-1;for(;a>=0;a--){let o=t[a];if(BigInt(o?.thread_event_id)<=_)break}return t.slice(a+1)}function Ie(e){return"UserMessage"in e?e.UserMessage:null}function Me(e){return"UserCancel"in e?e.UserCancel:null}function Se(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("PlanGenerationStarted"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.PlanGenerationStarted}function ge(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("PlanStepGenerated"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.PlanStepGenerated}function he(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)||!("GeneratedResponse"in e.AgentMessage.update.OrchestratorUpdate.update)?null:e.AgentMessage.update.OrchestratorUpdate.update.GeneratedResponse}function fe(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("ProcessingStarted"in e.AgentMessage.update.MessageProcessingUpdate.update)?null:e.AgentMessage.update.MessageProcessingUpdate.update.ProcessingStarted}function xe(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("InteractionDecision"in e.AgentMessage.update.MessageProcessingUpdate.update)||!("AcceptInteraction"in e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision)?null:e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision.AcceptInteraction}function Ae(e){return!("AgentMessage"in e)||!("MessageProcessingUpdate"in e.AgentMessage.update)||!("InteractionDecision"in e.AgentMessage.update.MessageProcessingUpdate.update)||!("DeclineInteraction"in e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision)?null:e.AgentMessage.update.MessageProcessingUpdate.update.InteractionDecision.decision.DeclineInteraction}function m(e){return!("AgentMessage"in e)||!("PlanningDecisionUpdate"in e.AgentMessage.update)?null:e.AgentMessage.update.PlanningDecisionUpdate.update}function Be(e){let t=m(e);return!t||!("Started"in t)?null:t.Started}function ke(e){let t=m(e);return!t||!("Completed"in t)?null:t.Completed}function ve(e){let t=m(e);return!t||!("PlanningRequired"in t)?null:t.PlanningRequired}function Oe(e){return!("AgentMessage"in e)||!("StartingOrchestrator"in e.AgentMessage.update)?null:e.AgentMessage.update.StartingOrchestrator}function P(e){return!("AgentMessage"in e)||!("OrchestratorUpdate"in e.AgentMessage.update)?null:e.AgentMessage.update.OrchestratorUpdate.update}function y(e){let t=P(e);return!t||!("ContextExplorerUpdate"in t)?null:t.ContextExplorerUpdate.update}function Ce(e){let t=y(e);return!t||!("Started"in t)?null:t.Started}function Te(e){let t=y(e);return!t||!("Completed"in t)?null:t.Completed}function b(e){let t=y(e);return!t||!("WikiExplorerUpdate"in t)?null:t.WikiExplorerUpdate.update}function Ee(e){let t=b(e);return!t||!("Started"in t)?null:t.Started}function Pe(e){let t=b(e);return!t||!("Completed"in t)?null:t.Completed}function Re(e){let t=b(e);return!t||!("WikiInfoGenerated"in t)?null:t.WikiInfoGenerated}function I(e){let t=y(e);return!t||!("SchemaTasksGenerationUpdate"in t)?null:t.SchemaTasksGenerationUpdate.update}function Ue(e){let t=I(e);return!t||!("Started"in t)?null:t.Started}function we(e){let t=I(e);return!t||!("Completed"in t)?null:t.Completed}function Ve(e){let t=I(e);return!t||!("TasksGenerated"in t)?null:t.TasksGenerated}function u(e){let t=y(e);return!t||!("SchemaExplorerUpdate"in t)?null:t.SchemaExplorerUpdate.update}function We(e){let t=u(e);return!t||!("Started"in t)?null:t.Started}function Fe(e){let t=u(e);return!t||!("Completed"in t)?null:t.Completed}function Ne(e){let t=u(e);return!t||!("CodeExecutionComplete"in t)?null:t.CodeExecutionComplete}function je(e){let t=u(e);return!t||!("CodeOutputAnalyzed"in t)?null:t.CodeOutputAnalyzed}function qe(e){let t=u(e);return!t||!("GeneratedCode"in t)?null:t.GeneratedCode}function ze(e){let t=u(e);return!t||!("SchemaExplored"in t)?null:t.SchemaExplored}function M(e){let t=P(e);return!t||!("StepUpdate"in t)?null:t.StepUpdate}function Ge(e){let t=M(e);return!t||!("Programmer"in t.update)?null:t.update.Programmer}function De(e){let t=M(e);return!t||!("UiProgrammer"in t.update)?null:t.update.UiProgrammer}function Qe(e){let t=M(e);return!t||!("SavedProgramRunner"in t.update)?null:t.update.SavedProgramRunner}export{E as PromptQLSdk,A as createApolloClient,x as createPromptQLAuthTokenGenerator,be as diffThreadEvents,he as getAgentGeneratedResponse,xe as getAgentInteractionDecisionAcceptInteractionEvent,Ae as getAgentInteractionDecisionDeclineInteractionEvent,fe as getAgentMessageProcessingStartedEvent,Te as getAgentOrchestratorContextExplorerCompletedEvent,Ce as getAgentOrchestratorContextExplorerStartedEvent,y as getAgentOrchestratorContextExplorerUpdateEvent,Ne as getAgentOrchestratorSchemaExplorerCodeExecutionCompleteEvent,je as getAgentOrchestratorSchemaExplorerCodeOutputAnalyzedEvent,Fe as getAgentOrchestratorSchemaExplorerCompletedEvent,qe as getAgentOrchestratorSchemaExplorerGeneratedCodeEvent,ze as getAgentOrchestratorSchemaExplorerSchemaExploredEvent,We as getAgentOrchestratorSchemaExplorerStartedEvent,u as getAgentOrchestratorSchemaExplorerUpdateEvent,Ve as getAgentOrchestratorSchemaTasksGeneratedEvent,we as getAgentOrchestratorSchemaTasksGenerationCompletedEvent,Ue as getAgentOrchestratorSchemaTasksGenerationStartedEvent,I as getAgentOrchestratorSchemaTasksGenerationUpdateEvent,Ge as getAgentOrchestratorStepProgrammerEvent,Qe as getAgentOrchestratorStepSavedProgramRunnerEvent,De as getAgentOrchestratorStepUiProgrammerEvent,M as getAgentOrchestratorStepUpdateEvent,P as getAgentOrchestratorUpdateEvent,Pe as getAgentOrchestratorWikiExplorerCompletedEvent,Ee as getAgentOrchestratorWikiExplorerStartedEvent,b as getAgentOrchestratorWikiExplorerUpdateEvent,Re as getAgentOrchestratorWikiInfoGeneratedEvent,Se as getAgentPlanGenerationStartedEvent,ge as getAgentPlanStepGeneratedEvent,ke as getAgentPlanningDecisionCompletedEvent,ve as getAgentPlanningDecisionPlanningRequiredEvent,Be as getAgentPlanningDecisionStartedEvent,m as getAgentPlanningDecisionUpdateEvent,Oe as getAgentStartingOrchestratorEvent,Me as getUserCancelEvent,Ie as getUserMessageEvent,f as isEnrichTokenMutationResponse,g as isLuxAuthPromptQLTokenFailedResponse,h as isLuxAuthPromptQLTokenSuccessResponse};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hasura/promptql",
3
3
  "description": "A Typescript SDK allows you to interact with PromptQL API",
4
- "version": "2.0.0-alpha.1",
4
+ "version": "2.0.0-alpha.3",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Hasura",
7
7
  "homepage": "https://promptql.hasura.io",
@@ -22,7 +22,7 @@
22
22
  "types": "./dist/index.d.ts",
23
23
  "scripts": {
24
24
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
25
- "build": "tsup src/index.ts --format cjs,esm --dts",
25
+ "build": "tsup src/index.ts --minify --clean --format cjs,esm --dts",
26
26
  "test": "bun test",
27
27
  "lint": "biome check .",
28
28
  "format": "biome check . --write",
@@ -1,24 +0,0 @@
1
- import { describe, expect, test } from "bun:test";
2
- import { createPromptQLAuthTokenGenerator } from "./auth";
3
-
4
- describe("fetchPromptQLAuthToken", () => {
5
- const promptqlProjectId = process.env.PROMPTQL_PROJECT_ID;
6
- const serviceAccountToken = process.env.PROMPTQL_SERVICE_ACCOUNT_TOKEN;
7
- if (!serviceAccountToken || !promptqlProjectId) {
8
- return;
9
- }
10
-
11
- const fetchAuthToken = createPromptQLAuthTokenGenerator({
12
- authHost: process.env.HASURA_AUTH_HOST,
13
- projectId: promptqlProjectId,
14
- promptqlGraphQLUrl: `${process.env.PROMPTQL_BASE_URL}/playground-v2-hge/v1/graphql`,
15
- serviceAccountToken,
16
- });
17
-
18
- test("should fetch promptql jwt token successfully", async () => {
19
- const token = await fetchAuthToken();
20
- expect(token).toStartWith("eyJh");
21
- const token2 = await fetchAuthToken();
22
- expect(token).toEqual(token2);
23
- });
24
- });
package/src/auth/auth.ts DELETED
@@ -1,130 +0,0 @@
1
- import { normalizeBaseUrl } from "../utils";
2
- import {
3
- isEnrichTokenMutationResponse,
4
- isLuxAuthPromptQLTokenFailedResponse,
5
- isLuxAuthPromptQLTokenSuccessResponse,
6
- type LuxAuthPromptQLTokenSuccessResponse,
7
- } from "./types";
8
-
9
- /**
10
- * Constructor options for a PromptQL authenticator.
11
- */
12
- export type PromptQLAuthTokenGeneratorOptions = {
13
- authHost?: string;
14
- projectId: string;
15
- promptqlGraphQLUrl: string;
16
- serviceAccountToken: string;
17
- fetch?: typeof fetch;
18
- };
19
-
20
- export function createPromptQLAuthTokenGenerator(
21
- options: PromptQLAuthTokenGeneratorOptions,
22
- ): () => Promise<string> {
23
- const authHost =
24
- normalizeBaseUrl(options.authHost) || "https://auth.pro.hasura.io";
25
- const luxAuthTokenUrl = `${authHost}/ddn/promptql/token`;
26
-
27
- const cachedToken = {
28
- token: "",
29
- expiry: new Date(0),
30
- };
31
-
32
- const customFetch = options.fetch || fetch;
33
-
34
- const getLuxToken =
35
- async (): Promise<LuxAuthPromptQLTokenSuccessResponse> => {
36
- const response = await customFetch(luxAuthTokenUrl, {
37
- method: "POST",
38
- headers: {
39
- Authorization: `pat ${options.serviceAccountToken}`,
40
- },
41
- });
42
-
43
- switch (response.status) {
44
- case 200: {
45
- const rawResponse = await response.json();
46
-
47
- if (isLuxAuthPromptQLTokenSuccessResponse(rawResponse)) {
48
- return rawResponse;
49
- }
50
-
51
- throw new Error("malformed token response");
52
- }
53
- case 401: {
54
- const errorBody = await response.json();
55
-
56
- if (isLuxAuthPromptQLTokenFailedResponse(errorBody)) {
57
- throw new Error(errorBody?.error || response.statusText);
58
- }
59
-
60
- throw new Error(response.statusText);
61
- }
62
- default: {
63
- const errorBody = await response.text();
64
- throw new Error(errorBody);
65
- }
66
- }
67
- };
68
-
69
- const enrichToken = async (
70
- luxToken: LuxAuthPromptQLTokenSuccessResponse,
71
- ): Promise<string> => {
72
- const response = await customFetch(options.promptqlGraphQLUrl, {
73
- method: "POST",
74
- headers: {
75
- "Content-Type": "application/json",
76
- },
77
- body: JSON.stringify({
78
- query:
79
- "mutation EnrichToken($luxJWT: String!, $projectId: uuid!) { enrich_token(luxJWT: $luxJWT, projectId: $projectId) { userDirectoryJWT }}",
80
- variables: {
81
- luxJWT: luxToken.token,
82
- projectId: options.projectId,
83
- },
84
- operationName: "EnrichToken",
85
- }),
86
- });
87
-
88
- switch (response.status) {
89
- case 200: {
90
- const rawResponse = await response.json();
91
- if (
92
- !isEnrichTokenMutationResponse(rawResponse) ||
93
- !rawResponse.data?.enrich_token?.userDirectoryJWT
94
- ) {
95
- throw new Error(
96
- `malformed promptql token response: ${JSON.stringify(rawResponse)}`,
97
- );
98
- }
99
-
100
- return rawResponse.data.enrich_token.userDirectoryJWT;
101
- }
102
- default: {
103
- const errorBody = await response.text();
104
- throw new Error(errorBody);
105
- }
106
- }
107
- };
108
-
109
- return async () => {
110
- if (cachedToken.expiry.getTime() <= Date.now() - 1000) {
111
- const luxToken = await getLuxToken();
112
- const promptqlToken = await enrichToken(luxToken);
113
-
114
- cachedToken.token = promptqlToken;
115
-
116
- const expiry = new Date(luxToken.expiry);
117
-
118
- if (Number.isNaN(expiry.getTime())) {
119
- // workaround the invalid expiry with a default 1-hour expiry.
120
- const nextHour = new Date();
121
- nextHour.setHours(1);
122
- cachedToken.expiry = nextHour;
123
- } else {
124
- cachedToken.expiry = expiry;
125
- }
126
- }
127
-
128
- return cachedToken.token;
129
- };
130
- }
package/src/auth/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./auth";
2
- export * from "./types";
package/src/auth/types.ts DELETED
@@ -1,56 +0,0 @@
1
- import type { GraphQLError } from "graphql";
2
- import type { EnrichTokenOutput } from "../generated/graphql";
3
-
4
- export type LuxAuthPromptQLTokenSuccessResponse = {
5
- status: "success";
6
- expiry: string;
7
- token: string;
8
- };
9
-
10
- type LuxAuthPromptQLTokenFailedResponse = { error: string; status: "failure" };
11
-
12
- export function isLuxAuthPromptQLTokenFailedResponse(
13
- input: unknown,
14
- ): input is LuxAuthPromptQLTokenFailedResponse {
15
- return (
16
- input !== null &&
17
- input !== undefined &&
18
- typeof input === "object" &&
19
- "status" in input &&
20
- input.status === "failure"
21
- );
22
- }
23
-
24
- export function isLuxAuthPromptQLTokenSuccessResponse(
25
- input: unknown,
26
- ): input is LuxAuthPromptQLTokenSuccessResponse {
27
- return (
28
- input !== null &&
29
- input !== undefined &&
30
- typeof input === "object" &&
31
- "status" in input &&
32
- input.status === "success" &&
33
- "token" in input
34
- );
35
- }
36
-
37
- export function isEnrichTokenMutationResponse(input: unknown): input is {
38
- data?: {
39
- enrich_token: EnrichTokenOutput;
40
- };
41
- errors?: GraphQLError[];
42
- } {
43
- return (
44
- input !== null &&
45
- input !== undefined &&
46
- typeof input === "object" &&
47
- (("data" in input &&
48
- !!input.data &&
49
- typeof input.data === "object" &&
50
- "enrich_token" in input.data &&
51
- !!input.data.enrich_token &&
52
- typeof input.data.enrich_token === "object" &&
53
- "userDirectoryJWT" in input.data.enrich_token) ||
54
- ("errors" in input && Array.isArray(input.errors)))
55
- );
56
- }