@aigur/client
Advanced tools
Comparing version 1.0.2 to 1.0.3
# @aigur/client | ||
## 1.0.3 | ||
### Patch Changes | ||
- 23e925f5: pipeline context to save internal invocation data | ||
## 1.0.2 | ||
@@ -4,0 +10,0 @@ |
@@ -78,3 +78,3 @@ import { z } from 'zod'; | ||
constructor(conf: PipelineConf<Input, Output>, flow: FlowBuilder<Input, Output, any, any>, apiKeys: APIKeys); | ||
invoke(input: Input, pipelineInstanceId?: string): Promise<Output>; | ||
invoke(input: Input, pipelineInstanceId?: string): Promise<{}>; | ||
invokeRemote(endpoint: string, input: Input): Promise<Output>; | ||
@@ -90,3 +90,2 @@ invokeStream(endpoint: string, input: Input, cb: (chunk: string) => void): Promise<void>; | ||
private processPipeline; | ||
private executeAction; | ||
private listenToEvents; | ||
@@ -93,0 +92,0 @@ private triggerListeners; |
@@ -1,2 +0,2 @@ | ||
var N=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var K=(n,e)=>{for(var t in e)N(n,t,{get:e[t],enumerable:!0})},B=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of k(e))!M.call(n,i)&&i!==t&&N(n,i,{get:()=>e[i],enumerable:!(o=D(e,i))||o.enumerable});return n};var $=n=>B(N({},"__esModule",{value:!0}),n);var Pe={};K(Pe,{Pipeline:()=>I,createClient:()=>H,dalleBase64TextToImage:()=>ee,dalleUrlTextToImage:()=>Z,enhanceWithKeywords:()=>ae,googleImageLabeling:()=>q,googleTextToSpeech:()=>ge,gpt3Prediction:()=>ue,gpt3PredictionStream:()=>me,inputSchema:()=>T,output:()=>R,outputSchema:()=>ce,replaceString:()=>pe,stabilityClipGuidancePreset:()=>C,stabilityModel:()=>U,stabilityTextToImage:()=>ne,stringToArrayBuffer:()=>ye,vercelGenericEdge:()=>V,whisperApi:()=>be});module.exports=$(Pe);async function R(n){return n}var x=class{constructor(e){this.nodes=e}static create(){return new x([])}node(e,t){let o=this.createDynamicPlaceholders("input"),i=this.nodes.length>0?this.nodes[this.nodes.length-1]:o,a={action:e,input:t({nodes:this.nodes,prev:i.output,input:o}),output:this.createDynamicPlaceholders(this.nodes.length)};return this.nodes.push(a),this}createDynamicPlaceholders(e){let t={},o={get:function(a,c){return`$context.${e}.${c}$`}};return new Proxy(t,o)}output(e){return this.node(R,e)}getNodes(){return this.nodes}};function O(n){return new Promise(e=>setTimeout(e,n))}function j(n,e){if(typeof n=="string")return i(n);let t={...n};return o(t);function o(r){let s={};for(let p in r)s[p]=i(r[p]);return s}function i(r){if(Array.isArray(r))return r.map(d=>o(d));if(typeof r=="object"&&r!==null)return o(r);let s=r,p=a(r);for(let d of p){let y=e[d.nodeId][d.property];if(y instanceof ArrayBuffer){s=y;continue}s=s?.replace(new RegExp(c(d.value)),y),s==="undefined"?s=void 0:s!==y&&s===y.toString()&&(s=y)}return s}function a(r){if(typeof r!="string")return[];let s=/\$context\.(\d+|input)\.(\w+)\$/g,p=r.matchAll(s),d=[];for(let m of p)d.push({value:m[0],nodeId:m[1],property:m[2]});return d}function c(r){return r.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}}function w(n=16){let e="",t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=t.length;for(let i=0;i<n;i++)e+=t.charAt(Math.floor(Math.random()*o));return e}var W=2,F=350,I=class{constructor(e,t,o){this.conf=e;this.flow=t;this.apiKeys=o;this.onProgressListeners=new Map;this.onStartListeners=new Map;this.onFinishListeners=new Map;this.pipelineInstanceId=w(16);this.eventIndex=0;this.vercel={invoke:e=>this.invokeRemote(`/api/pipelines/${this.conf.id}`,e),invokeStream:(e,t)=>this.invokeStream(`/api/pipelines/${this.conf.id}`,e,t)};this.listenToEvents()}invoke(e,t=this.pipelineInstanceId){return this.processPipeline(e,t)}invokeRemote(e,t){return fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId})}).then(o=>o.json())}async invokeStream(e,t,o){let i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId})});if(!i.ok)throw new Error(i.statusText);let a=i.body;if(!a)return;let c=a.getReader(),r=new TextDecoder,s=!1;for(;!s;){let{value:p,done:d}=await c.read();s=d;let m=r.decode(p);o(m)}}onProgress(e){let t=w();return this.onProgressListeners.set(t,e),()=>{this.onProgressListeners.delete(t)}}onStart(e){let t=w();return this.onStartListeners.set(t,e),()=>{this.onStartListeners.delete(t)}}onFinish(e){let t=w();return this.onFinishListeners.set(t,e),()=>{this.onFinishListeners.delete(t)}}async processPipeline(e,t){let o=this.conf.retries??W;try{let i=this.notifyEvent({type:"pipeline:start",pipelineInstanceId:t});if(this.conf.stream||await i,this.conf.validateInput){let p=this.conf.validateInput(e);if(!p.valid)throw new Error(p.message)}let a={input:e},c={},r=this.flow.getNodes();for(let p=0;p<r.length;p++){let d=this.notifyEvent({type:"node:start",pipelineInstanceId:t,data:{node:r[p].name,index:p}});this.conf.stream||await d;let m=0,y=!1;do{m++;try{c=await this.executeAction(r,p,a),a[p]=c,y=!0}catch(_){if(m>o)throw _;await O((this.conf.retryDelayInMs??F)*m)}}while(!y&&m<=o);let z=this.notifyEvent({type:"node:finish",pipelineInstanceId:t,data:{node:r[p].name,index:p}});this.conf.stream||await z}let s=this.notifyEvent({type:"pipeline:finish",pipelineInstanceId:t});return this.conf.stream||await s,c}catch(i){throw console.error(i),i}}async executeAction(e,t,o){let{action:i,input:a}=e[t],c=j(a,o);return i(c,this.apiKeys)}listenToEvents(){!this.conf.updateProgress||typeof window>"u"||!this.conf.eventListener||this.conf.eventListener(this.pipelineInstanceId,e=>{e.pipelineId===this.conf.id&&(e.type==="node:start"||e.type==="node:finish"?this.triggerListeners(this.onProgressListeners,e):e.type==="pipeline:start"?this.triggerListeners(this.onStartListeners,e):e.type==="pipeline:finish"&&this.triggerListeners(this.onFinishListeners,e))})}triggerListeners(e,...t){for(let o of e.values())o(...t)}notifyEvent(e){if(!(!this.conf.updateProgress||!this.conf.eventPublisher))return this.conf.eventPublisher(e.pipelineInstanceId,{pipelineId:this.conf.id,eventIndex:this.eventIndex++,...e})}};var J=2,G=350,H=n=>{let{apiKeys:e}=n;return{apiKeys:e,pipeline:{create:t=>{let o={...t,retries:t.retries??J,retryDelayInMs:t.retryDelayInMs??G,eventListener:n.eventListener,eventPublisher:n.eventPublisher},i=t.flow(new x([]));return new I(o,i,e)}}}};async function V(n,e){let{pipelineInstanceId:t,input:o}=await e.json(),{searchParams:i}=new URL(e.url);if(!i.has("id"))return new Response("Missing id",{status:400});let a=i.get("id"),c=n[a];return{output:await c.invoke(o,t),pipeline:c}}var b=require("zod"),Y=b.z.object({image:b.z.string()}),De=b.z.object({labels:b.z.array(b.z.string())});async function q(n,e){let t=Y.parse(n),o=`https://vision.googleapis.com/v1/images:annotate?key=${e.googleapis}`,i={requests:[{image:{content:t.image},features:[{type:"LABEL_DETECTION"}]}]};return{labels:(await(await fetch(o,{method:"POST",body:JSON.stringify(i)})).json()).responses[0].labelAnnotations.map(r=>r.description)}}var P=require("zod"),X=P.z.object({prompt:P.z.string(),size:P.z.enum(["256x256","512x512","1024x1024"]).default("512x512")}),Me=P.z.object({url:P.z.string().url()});async function Z(n,e){let t=X.parse(n),i=await fetch("https://api.openai.com/v1/images/generations",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)}),{data:a}=await i.json();return{url:a[0].url}}var S=require("zod"),Q=S.z.object({prompt:S.z.string(),response_format:S.z.literal("b64_json").default("b64_json"),size:S.z.enum(["256x256","512x512","1024x1024"]).default("512x512")}),Be=S.z.object({result:S.z.string()});async function ee(n,e){let t=Q.parse(n),i=await fetch("https://api.openai.com/v1/images/generations",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)}),{data:a}=await i.json();return{result:a[0].b64_json}}var u=require("zod"),U=u.z.enum(["stable-diffusion-v1-5","stable-diffusion-512-v2-0","stable-diffusion-768-v2-0","stable-diffusion-512-v2-1","stable-diffusion-768-v2-1"]),C=u.z.enum(["NONE","FAST_BLUE","FAST_GREEN","SIMPLE","SLOW","SLOWER","SLOWEST"]),te=u.z.object({text_prompts:u.z.array(u.z.object({text:u.z.string(),weight:u.z.number().min(-1).max(1).default(1)})).refine(n=>n.length>0,"Must have at least one text prompt"),model:U.default("stable-diffusion-v1-5"),clip_guidance_preset:C.optional(),steps:u.z.number().min(0).max(150).optional(),sampler:u.z.enum(["DDIM","DDPM","K_DPMPP_2M","K_DPMPP_2S_ANCESTRAL","K_DPM_2","K_DPM_2_ANCESTRAL","K_EULER","K_EULER_ANCESTRAL","K_HEUN","K_LMS"]).optional(),cfg_scale:u.z.number().min(0).max(35).optional(),seed:u.z.number().min(0).optional(),height:u.z.number().min(128).optional().refine(n=>typeof n<"u"?n%64===0:!0,"Must be a multiple of 64"),width:u.z.number().min(128).optional().refine(n=>typeof n<"u"?n%64===0:!0,"Must be a multiple of 64")}),We=u.z.object({result:u.z.instanceof(ArrayBuffer)});async function ne(n,e){let t=te.parse(n),o=`https://api.stability.ai/v1beta/generation/${t.model}/text-to-image`;return{result:await(await fetch(o,{headers:{"Content-Type":"application/json",Accept:"image/png",Authorization:e.stability},method:"POST",body:JSON.stringify(t)})).arrayBuffer()}}var v=require("zod"),oe=v.z.object({text:v.z.string(),amount:v.z.number().optional().default(8)}),Je=v.z.object({text:v.z.string()}),ie={"Colonial-style home":"Colonial, traditional, classic, historical, timeless, elegant, regal, grand, spacious, architectural, wood-framed, brick-exterior, symmetrical, gabled roof, columns, portico, fireplace, formal, ornate, landscaped".split(", "),"High-end penthouse apartment":"Luxury, high-end, penthouse, apartment, upscale, contemporary, modern, stylish, designer, elite, high-rise, rooftop, panoramic, views, spacious, open-plan, top-floor, amenities, concierge, service, exclusive".split(", ")};function re(n){return Object.entries(ie).map(([e,t])=>`Title: ${e} | ||
var N=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var K=(n,e)=>{for(var t in e)N(n,t,{get:e[t],enumerable:!0})},B=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of k(e))!M.call(n,i)&&i!==t&&N(n,i,{get:()=>e[i],enumerable:!(o=D(e,i))||o.enumerable});return n};var $=n=>B(N({},"__esModule",{value:!0}),n);var be={};K(be,{Pipeline:()=>w,createClient:()=>H,dalleBase64TextToImage:()=>ee,dalleUrlTextToImage:()=>Z,enhanceWithKeywords:()=>ae,googleImageLabeling:()=>q,googleTextToSpeech:()=>ge,gpt3Prediction:()=>ce,gpt3PredictionStream:()=>le,inputSchema:()=>T,output:()=>R,outputSchema:()=>ue,replaceString:()=>pe,stabilityClipGuidancePreset:()=>L,stabilityModel:()=>U,stabilityTextToImage:()=>ne,stringToArrayBuffer:()=>ye,vercelGenericEdge:()=>V,whisperApi:()=>Pe});module.exports=$(be);async function R(n){return n}var x=class{constructor(e){this.nodes=e}static create(){return new x([])}node(e,t){let o=this.createDynamicPlaceholders("input"),i=this.nodes.length>0?this.nodes[this.nodes.length-1]:o,p={action:e,input:t({nodes:this.nodes,prev:i.output,input:o}),output:this.createDynamicPlaceholders(this.nodes.length)};return this.nodes.push(p),this}createDynamicPlaceholders(e){let t={},o={get:function(p,a){return`$context.${e}.${a}$`}};return new Proxy(t,o)}output(e){return this.node(R,e)}getNodes(){return this.nodes}};function C(n){return new Promise(e=>setTimeout(e,n))}function j(n,e){if(typeof n=="string")return i(n);let t={...n};return o(t);function o(s){let r={};for(let d in s)r[d]=i(s[d]);return r}function i(s){if(Array.isArray(s))return s.map(c=>o(c));if(typeof s=="object"&&s!==null)return o(s);let r=s,d=p(s);for(let c of d){let y=e[c.nodeId][c.property];if(y instanceof ArrayBuffer){r=y;continue}r=r?.replace(new RegExp(a(c.value)),y),r==="undefined"?r=void 0:r!==y&&r===y.toString()&&(r=y)}return r}function p(s){if(typeof s!="string")return[];let r=/\$context\.(\d+|input)\.(\w+)\$/g,d=s.matchAll(r),c=[];for(let l of d)c.push({value:l[0],nodeId:l[1],property:l[2]});return c}function a(s){return s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}}function v(n=16){let e="",t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",o=t.length;for(let i=0;i<n;i++)e+=t.charAt(Math.floor(Math.random()*o));return e}function O(n){let e={input:n.input};return{pipelineInstanceId:n.pipelineInstanceId,input:n.input,output:{},values:e}}var W=2,F=350,w=class{constructor(e,t,o){this.conf=e;this.flow=t;this.apiKeys=o;this.onProgressListeners=new Map;this.onStartListeners=new Map;this.onFinishListeners=new Map;this.pipelineInstanceId=v(16);this.eventIndex=0;this.vercel={invoke:e=>this.invokeRemote(`/api/pipelines/${this.conf.id}`,e),invokeStream:(e,t)=>this.invokeStream(`/api/pipelines/${this.conf.id}`,e,t)};this.listenToEvents()}async invoke(e,t=this.pipelineInstanceId){let o=O({input:e,pipelineInstanceId:t});return await this.processPipeline(o),o.output}invokeRemote(e,t){return fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId})}).then(o=>o.json())}async invokeStream(e,t,o){let i=await fetch(e,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({input:t,pipelineInstanceId:this.pipelineInstanceId})});if(!i.ok)throw new Error(i.statusText);let p=i.body;if(!p)return;let a=p.getReader(),s=new TextDecoder,r=!1;for(;!r;){let{value:d,done:c}=await a.read();r=c;let l=s.decode(d);o(l)}}onProgress(e){let t=v();return this.onProgressListeners.set(t,e),()=>{this.onProgressListeners.delete(t)}}onStart(e){let t=v();return this.onStartListeners.set(t,e),()=>{this.onStartListeners.delete(t)}}onFinish(e){let t=v();return this.onFinishListeners.set(t,e),()=>{this.onFinishListeners.delete(t)}}async processPipeline(e){let t=this.conf.retries??W;try{let o=this.notifyEvent({type:"pipeline:start",context:e});if(this.conf.stream||await o,this.conf.validateInput){let a=this.conf.validateInput(e.input);if(!a.valid)throw new Error(a.message)}let i=this.flow.getNodes();for(let a=0;a<i.length;a++){let s=this.notifyEvent({type:"node:start",context:e,data:{node:i[a].name,index:a}});this.conf.stream||await s;let r=0,d=!1;do{r++;try{let{action:l,input:y}=i[a],_=j(y,e.values);e.values[a]=await l(_,this.apiKeys),d=!0}catch(l){if(r>t)throw l;await C((this.conf.retryDelayInMs??F)*r)}}while(!d&&r<=t);let c=this.notifyEvent({type:"node:finish",context:e,data:{node:i[a].name,index:a}});this.conf.stream||await c}let p=this.notifyEvent({type:"pipeline:finish",context:e});return this.conf.stream||await p,e.output=e.values[i.length-1],e}catch(o){throw console.error(o),o}}listenToEvents(){!this.conf.updateProgress||typeof window>"u"||!this.conf.eventListener||this.conf.eventListener(this.pipelineInstanceId,e=>{e.pipelineId===this.conf.id&&(e.type==="node:start"||e.type==="node:finish"?this.triggerListeners(this.onProgressListeners,e):e.type==="pipeline:start"?this.triggerListeners(this.onStartListeners,e):e.type==="pipeline:finish"&&this.triggerListeners(this.onFinishListeners,e))})}triggerListeners(e,...t){for(let o of e.values())o(...t)}notifyEvent(e){if(!(!this.conf.updateProgress||!this.conf.eventPublisher))return this.conf.eventPublisher(e.context.pipelineInstanceId,{pipelineId:this.conf.id,eventIndex:this.eventIndex++,type:e.type,data:e.data})}};var J=2,G=350,H=n=>{let{apiKeys:e}=n;return{apiKeys:e,pipeline:{create:t=>{let o={...t,retries:t.retries??J,retryDelayInMs:t.retryDelayInMs??G,eventListener:n.eventListener,eventPublisher:n.eventPublisher},i=t.flow(new x([]));return new w(o,i,e)}}}};async function V(n,e){let{pipelineInstanceId:t,input:o}=await e.json(),{searchParams:i}=new URL(e.url);if(!i.has("id"))return new Response("Missing id",{status:400});let p=i.get("id"),a=n[p];return{output:await a.invoke(o,t),pipeline:a}}var P=require("zod"),Y=P.z.object({image:P.z.string()}),Ke=P.z.object({labels:P.z.array(P.z.string())});async function q(n,e){let t=Y.parse(n),o=`https://vision.googleapis.com/v1/images:annotate?key=${e.googleapis}`,i={requests:[{image:{content:t.image},features:[{type:"LABEL_DETECTION"}]}]};return{labels:(await(await fetch(o,{method:"POST",body:JSON.stringify(i)})).json()).responses[0].labelAnnotations.map(s=>s.description)}}var b=require("zod"),X=b.z.object({prompt:b.z.string(),size:b.z.enum(["256x256","512x512","1024x1024"]).default("512x512")}),$e=b.z.object({url:b.z.string().url()});async function Z(n,e){let t=X.parse(n),i=await fetch("https://api.openai.com/v1/images/generations",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)}),{data:p}=await i.json();return{url:p[0].url}}var S=require("zod"),Q=S.z.object({prompt:S.z.string(),response_format:S.z.literal("b64_json").default("b64_json"),size:S.z.enum(["256x256","512x512","1024x1024"]).default("512x512")}),Fe=S.z.object({result:S.z.string()});async function ee(n,e){let t=Q.parse(n),i=await fetch("https://api.openai.com/v1/images/generations",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)}),{data:p}=await i.json();return{result:p[0].b64_json}}var u=require("zod"),U=u.z.enum(["stable-diffusion-v1-5","stable-diffusion-512-v2-0","stable-diffusion-768-v2-0","stable-diffusion-512-v2-1","stable-diffusion-768-v2-1"]),L=u.z.enum(["NONE","FAST_BLUE","FAST_GREEN","SIMPLE","SLOW","SLOWER","SLOWEST"]),te=u.z.object({text_prompts:u.z.array(u.z.object({text:u.z.string(),weight:u.z.number().min(-1).max(1).default(1)})).refine(n=>n.length>0,"Must have at least one text prompt"),model:U.default("stable-diffusion-v1-5"),clip_guidance_preset:L.optional(),steps:u.z.number().min(0).max(150).optional(),sampler:u.z.enum(["DDIM","DDPM","K_DPMPP_2M","K_DPMPP_2S_ANCESTRAL","K_DPM_2","K_DPM_2_ANCESTRAL","K_EULER","K_EULER_ANCESTRAL","K_HEUN","K_LMS"]).optional(),cfg_scale:u.z.number().min(0).max(35).optional(),seed:u.z.number().min(0).optional(),height:u.z.number().min(128).optional().refine(n=>typeof n<"u"?n%64===0:!0,"Must be a multiple of 64"),width:u.z.number().min(128).optional().refine(n=>typeof n<"u"?n%64===0:!0,"Must be a multiple of 64")}),Ge=u.z.object({result:u.z.instanceof(ArrayBuffer)});async function ne(n,e){let t=te.parse(n),o=`https://api.stability.ai/v1beta/generation/${t.model}/text-to-image`;return{result:await(await fetch(o,{headers:{"Content-Type":"application/json",Accept:"image/png",Authorization:e.stability},method:"POST",body:JSON.stringify(t)})).arrayBuffer()}}var I=require("zod"),oe=I.z.object({text:I.z.string(),amount:I.z.number().optional().default(8)}),Ve=I.z.object({text:I.z.string()}),ie={"Colonial-style home":"Colonial, traditional, classic, historical, timeless, elegant, regal, grand, spacious, architectural, wood-framed, brick-exterior, symmetrical, gabled roof, columns, portico, fireplace, formal, ornate, landscaped".split(", "),"High-end penthouse apartment":"Luxury, high-end, penthouse, apartment, upscale, contemporary, modern, stylish, designer, elite, high-rise, rooftop, panoramic, views, spacious, open-plan, top-floor, amenities, concierge, service, exclusive".split(", ")};function re(n){return Object.entries(ie).map(([e,t])=>`Title: ${e} | ||
Description: ${t.slice(0,n).join(", ")} | ||
@@ -7,2 +7,2 @@ `).join(` | ||
Title: ${e.text} | ||
Description:`}}var g=require("zod"),se=g.z.object({text:g.z.string().or(g.z.array(g.z.string())),modifier:g.z.string()}),He=g.z.object({text:g.z.string()});async function pe(n){let e=se.parse(n);return{text:e.modifier.replace(/\$\(text\)\$/gm,Array.isArray(e.text)?e.text.join(", "):e.text)}}var l=require("zod"),T=l.z.object({prompt:l.z.string(),model:l.z.enum(["text-davinci-003","text-curie-001","text-babbage-001","text-ada-001","code-davinci-002","code-cushman-002"]).default("text-davinci-003"),temperature:l.z.number().min(0).max(2).default(.7),top_p:l.z.number().min(0).max(1).default(1),frequency_penalty:l.z.number().min(-2).max(2).default(0),presence_penalty:l.z.number().min(-2).max(2).default(0),max_tokens:l.z.number().default(200),n:l.z.number().default(1)}),ce=l.z.object({text:l.z.string()});async function ue(n,e){let t=T.parse(n);return{text:(await(await fetch("https://api.openai.com/v1/completions",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)})).json()).choices[0]?.text.replace(/^(?:\n)+/gm,"")}}var L=require("eventsource-parser"),A=require("zod");var de=T.merge(A.z.object({stream:A.z.literal(!0).optional().default(!0)})),Qe=A.z.object({stream:A.z.instanceof(globalThis.ReadableStream??Object)});async function me(n,e){let t=de.parse(n),o=await fetch("https://api.openai.com/v1/completions",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)});return{stream:await le(o)}}async function le(n){let e=new TextEncoder,t=new TextDecoder,o=0;return new ReadableStream({async start(a){function c(s){if(s.type==="event"){let p=s.data;if(p==="[DONE]"){a.close();return}try{let m=JSON.parse(p).choices[0].text;if(o<2&&(m.match(/\n/)||[]).length)return;let y=e.encode(m);a.enqueue(y),o++}catch(d){a.error(d)}}}let r=(0,L.createParser)(c);for await(let s of n.body)r.feed(t.decode(s))}})}var E=require("zod"),fe=E.z.object({string:E.z.string()}),tt=E.z.object({arrayBuffer:E.z.instanceof(ArrayBuffer)});async function ye(n){let e=fe.parse(n);return{arrayBuffer:Uint8Array.from(atob(e.string),o=>o.charCodeAt(0)).buffer}}var f=require("zod"),he=f.z.object({text:f.z.string(),speakingRate:f.z.number().min(.25).max(4).optional().default(1),pitch:f.z.number().min(-20).max(20).optional().default(0),encoding:f.z.enum(["MP3","FLAC","LINEAR16","MULAW","AMR","AMR_WB","OGG_OPUS","SPEEX_WITH_HEADER_BYTE","WEBM_OPUS"]).optional().default("MP3"),voice:f.z.object({language:f.z.string().optional().default("en-US"),name:f.z.enum(["en-US-Standard-A","en-US-Standard-C","en-US-Standard-D","en-US-Standard-E","en-US-Standard-F","en-US-Standard-G","en-US-Standard-H","en-US-Standard-I","en-US-Standard-J","en-US-Studio-M","en-US-Studio-O","en-US-Wavenet-A","en-US-Wavenet-B","en-US-Wavenet-C","en-US-Wavenet-D","en-US-Wavenet-E","en-US-Wavenet-F","en-US-Wavenet-G","en-US-Wavenet-H","en-US-Wavenet-I","en-US-Wavenet-J","en-US-News-K","en-US-News-L","en-US-News-M","en-US-News-N","en-US-Standard-A","en-US-Standard-B","en-US-Standard-C","en-US-Standard-D","en-US-Standard-E","en-US-Standard-F","en-US-Standard-G","en-US-Standard-H","en-US-Standard-I","en-US-Standard-J"]).or(f.z.string()).optional().default("en-US-Neural2-C")}).optional().default({language:"en-US",name:"en-US-Neural2-C"})}),ot=f.z.object({audio:f.z.string()});async function ge(n,e){let t=he.parse(n),o=`https://us-central1-texttospeech.googleapis.com/v1beta1/text:synthesize?key=${e.googleapis}`,i={input:{text:t.text},voice:{languageCode:t.voice.language,name:t.voice.name},audioConfig:{audioEncoding:t.encoding,speakingRate:t.speakingRate,pitch:t.pitch}};return{audio:(await(await fetch(o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})).json()).audioContent}}var h=require("zod"),Se="https://transcribe.whisperapi.com",xe=h.z.object({audioUrl:h.z.string().url(),language:h.z.string().default("en"),autoDetectLanguage:h.z.boolean().default(!1),fileType:h.z.string().default("mp3"),task:h.z.enum(["transcribe","translate"]).default("transcribe")}),rt=h.z.object({text:h.z.string()});async function be(n,e){let t=xe.parse(n),o=new FormData;o.append("url",t.audioUrl),t.autoDetectLanguage||o.append("language",t.language),o.append("fileType",t.fileType),o.append("task",t.task);let i=await fetch(Se,{method:"POST",headers:{contentType:"application/json",Authorization:"Bearer "+e.whisperapi},body:o});if(!i.ok)throw new Error(i.statusText);return{text:(await i.json()).text.trim()}}0&&(module.exports={Pipeline,createClient,dalleBase64TextToImage,dalleUrlTextToImage,enhanceWithKeywords,googleImageLabeling,googleTextToSpeech,gpt3Prediction,gpt3PredictionStream,inputSchema,output,outputSchema,replaceString,stabilityClipGuidancePreset,stabilityModel,stabilityTextToImage,stringToArrayBuffer,vercelGenericEdge,whisperApi}); | ||
Description:`}}var g=require("zod"),se=g.z.object({text:g.z.string().or(g.z.array(g.z.string())),modifier:g.z.string()}),qe=g.z.object({text:g.z.string()});async function pe(n){let e=se.parse(n);return{text:e.modifier.replace(/\$\(text\)\$/gm,Array.isArray(e.text)?e.text.join(", "):e.text)}}var m=require("zod"),T=m.z.object({prompt:m.z.string(),model:m.z.enum(["text-davinci-003","text-curie-001","text-babbage-001","text-ada-001","code-davinci-002","code-cushman-002"]).default("text-davinci-003"),temperature:m.z.number().min(0).max(2).default(.7),top_p:m.z.number().min(0).max(1).default(1),frequency_penalty:m.z.number().min(-2).max(2).default(0),presence_penalty:m.z.number().min(-2).max(2).default(0),max_tokens:m.z.number().default(200),n:m.z.number().default(1)}),ue=m.z.object({text:m.z.string()});async function ce(n,e){let t=T.parse(n);return{text:(await(await fetch("https://api.openai.com/v1/completions",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)})).json()).choices[0]?.text.replace(/^(?:\n)+/gm,"")}}var z=require("eventsource-parser"),A=require("zod");var de=T.merge(A.z.object({stream:A.z.literal(!0).optional().default(!0)})),nt=A.z.object({stream:A.z.instanceof(globalThis.ReadableStream??Object)});async function le(n,e){let t=de.parse(n),o=await fetch("https://api.openai.com/v1/completions",{headers:{"Content-Type":"application/json",Authorization:`Bearer ${e.openai}`},method:"POST",body:JSON.stringify(t)});return{stream:await me(o)}}async function me(n){let e=new TextEncoder,t=new TextDecoder,o=0;return new ReadableStream({async start(p){function a(r){if(r.type==="event"){let d=r.data;if(d==="[DONE]"){p.close();return}try{let l=JSON.parse(d).choices[0].text;if(o<2&&(l.match(/\n/)||[]).length)return;let y=e.encode(l);p.enqueue(y),o++}catch(c){p.error(c)}}}let s=(0,z.createParser)(a);for await(let r of n.body)s.feed(t.decode(r))}})}var E=require("zod"),fe=E.z.object({string:E.z.string()}),it=E.z.object({arrayBuffer:E.z.instanceof(ArrayBuffer)});async function ye(n){let e=fe.parse(n);return{arrayBuffer:Uint8Array.from(atob(e.string),o=>o.charCodeAt(0)).buffer}}var f=require("zod"),he=f.z.object({text:f.z.string(),speakingRate:f.z.number().min(.25).max(4).optional().default(1),pitch:f.z.number().min(-20).max(20).optional().default(0),encoding:f.z.enum(["MP3","FLAC","LINEAR16","MULAW","AMR","AMR_WB","OGG_OPUS","SPEEX_WITH_HEADER_BYTE","WEBM_OPUS"]).optional().default("MP3"),voice:f.z.object({language:f.z.string().optional().default("en-US"),name:f.z.enum(["en-US-Standard-A","en-US-Standard-C","en-US-Standard-D","en-US-Standard-E","en-US-Standard-F","en-US-Standard-G","en-US-Standard-H","en-US-Standard-I","en-US-Standard-J","en-US-Studio-M","en-US-Studio-O","en-US-Wavenet-A","en-US-Wavenet-B","en-US-Wavenet-C","en-US-Wavenet-D","en-US-Wavenet-E","en-US-Wavenet-F","en-US-Wavenet-G","en-US-Wavenet-H","en-US-Wavenet-I","en-US-Wavenet-J","en-US-News-K","en-US-News-L","en-US-News-M","en-US-News-N","en-US-Standard-A","en-US-Standard-B","en-US-Standard-C","en-US-Standard-D","en-US-Standard-E","en-US-Standard-F","en-US-Standard-G","en-US-Standard-H","en-US-Standard-I","en-US-Standard-J"]).or(f.z.string()).optional().default("en-US-Neural2-C")}).optional().default({language:"en-US",name:"en-US-Neural2-C"})}),at=f.z.object({audio:f.z.string()});async function ge(n,e){let t=he.parse(n),o=`https://us-central1-texttospeech.googleapis.com/v1beta1/text:synthesize?key=${e.googleapis}`,i={input:{text:t.text},voice:{languageCode:t.voice.language,name:t.voice.name},audioConfig:{audioEncoding:t.encoding,speakingRate:t.speakingRate,pitch:t.pitch}};return{audio:(await(await fetch(o,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(i)})).json()).audioContent}}var h=require("zod"),Se="https://transcribe.whisperapi.com",xe=h.z.object({audioUrl:h.z.string().url(),language:h.z.string().default("en"),autoDetectLanguage:h.z.boolean().default(!1),fileType:h.z.string().default("mp3"),task:h.z.enum(["transcribe","translate"]).default("transcribe")}),pt=h.z.object({text:h.z.string()});async function Pe(n,e){let t=xe.parse(n),o=new FormData;o.append("url",t.audioUrl),t.autoDetectLanguage||o.append("language",t.language),o.append("fileType",t.fileType),o.append("task",t.task);let i=await fetch(Se,{method:"POST",headers:{contentType:"application/json",Authorization:"Bearer "+e.whisperapi},body:o});if(!i.ok)throw new Error(i.statusText);return{text:(await i.json()).text.trim()}}0&&(module.exports={Pipeline,createClient,dalleBase64TextToImage,dalleUrlTextToImage,enhanceWithKeywords,googleImageLabeling,googleTextToSpeech,gpt3Prediction,gpt3PredictionStream,inputSchema,output,outputSchema,replaceString,stabilityClipGuidancePreset,stabilityModel,stabilityTextToImage,stringToArrayBuffer,vercelGenericEdge,whisperApi}); |
{ | ||
"name": "@aigur/client", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -27,4 +27,6 @@ "main": "./dist/index.js", | ||
"build": "tsup src/index.ts --format cjs --dts --minify", | ||
"dev": "tsup src/index.ts --format cjs --dts --watch" | ||
"dev": "tsup src/index.ts --format cjs --dts --watch", | ||
"test": "vitest run", | ||
"test:dev": "vitest dev" | ||
} | ||
} |
import { z } from 'zod'; | ||
export function getInputByContext( | ||
export function getConcreteNodeInput( | ||
inputPlaceholders: Record<string, any> | z.ZodEffects<any>, | ||
@@ -5,0 +5,0 @@ values: Record<string, any> |
import { FlowBuilder } from './builder'; | ||
import { delay } from './delay'; | ||
import { getInputByContext } from './getInputByContext'; | ||
import { getConcreteNodeInput } from './getInputByContext'; | ||
import { makeid } from './makeid'; | ||
import { createContext, PipelineContext } from './PipelineContext'; | ||
import { | ||
@@ -35,4 +36,9 @@ APIKeys, | ||
public invoke(input: Input, pipelineInstanceId: string = this.pipelineInstanceId) { | ||
return this.processPipeline(input, pipelineInstanceId); | ||
public async invoke(input: Input, pipelineInstanceId: string = this.pipelineInstanceId) { | ||
const context = createContext({ | ||
input, | ||
pipelineInstanceId, | ||
}); | ||
await this.processPipeline(context); | ||
return context.output; | ||
} | ||
@@ -129,6 +135,6 @@ | ||
private async processPipeline(input: Input, pipelineInstanceId: string): Promise<Output> { | ||
private async processPipeline(context: PipelineContext<Input>): Promise<PipelineContext<Input>> { | ||
const retriesCount = this.conf.retries ?? DEFAULT_RETRIES; | ||
try { | ||
const pipelineStartPromise = this.notifyEvent({ type: 'pipeline:start', pipelineInstanceId }); | ||
const pipelineStartPromise = this.notifyEvent({ type: 'pipeline:start', context }); | ||
if (!this.conf.stream) { | ||
@@ -138,3 +144,3 @@ await pipelineStartPromise; | ||
if (this.conf.validateInput) { | ||
const result = this.conf.validateInput(input); | ||
const result = this.conf.validateInput(context.input); | ||
if (!result.valid) { | ||
@@ -145,13 +151,11 @@ // TODO: notifyEvent on error | ||
} | ||
const values: any = { input }; | ||
let output: any = {}; | ||
const nodes: any[] = this.flow.getNodes(); | ||
for (let i = 0; i < nodes.length; i++) { | ||
for (let nodeIndex = 0; nodeIndex < nodes.length; nodeIndex++) { | ||
const nodeStartPromise = this.notifyEvent({ | ||
type: 'node:start', | ||
pipelineInstanceId, | ||
context, | ||
data: { | ||
node: nodes[i].name, | ||
index: i, | ||
node: nodes[nodeIndex].name, | ||
index: nodeIndex, | ||
}, | ||
@@ -167,4 +171,5 @@ }); | ||
try { | ||
output = await this.executeAction(nodes, i, values); | ||
values[i] = output; | ||
const { action, input: inputPlaceholders } = nodes[nodeIndex]; | ||
const nodeInput = getConcreteNodeInput(inputPlaceholders, context.values); | ||
context.values[nodeIndex] = await action(nodeInput, this.apiKeys); | ||
isSuccess = true; | ||
@@ -180,6 +185,6 @@ } catch (e) { | ||
type: 'node:finish', | ||
pipelineInstanceId, | ||
context, | ||
data: { | ||
node: nodes[i].name, | ||
index: i, | ||
node: nodes[nodeIndex].name, | ||
index: nodeIndex, | ||
}, | ||
@@ -193,3 +198,3 @@ }); | ||
type: 'pipeline:finish', | ||
pipelineInstanceId, | ||
context, | ||
}); | ||
@@ -199,3 +204,5 @@ if (!this.conf.stream) { | ||
} | ||
return output; | ||
context.output = context.values[nodes.length - 1]; | ||
return context; | ||
} catch (e) { | ||
@@ -207,8 +214,2 @@ console.error(e); | ||
private async executeAction(nodes, index, values) { | ||
const { action, input } = nodes[index]; | ||
const inputByContext = getInputByContext(input, values); | ||
return action(inputByContext, this.apiKeys); | ||
} | ||
private listenToEvents() { | ||
@@ -242,3 +243,3 @@ if (!this.conf.updateProgress || typeof window === 'undefined' || !this.conf.eventListener) { | ||
type: EventType; | ||
pipelineInstanceId: string; | ||
context: PipelineContext<Input>; | ||
data?: Record<any, any>; | ||
@@ -249,8 +250,9 @@ }) { | ||
} | ||
return this.conf.eventPublisher(opts.pipelineInstanceId, { | ||
return this.conf.eventPublisher(opts.context.pipelineInstanceId, { | ||
pipelineId: this.conf.id, | ||
eventIndex: this.eventIndex++, | ||
...opts, | ||
type: opts.type, | ||
data: opts.data, | ||
}); | ||
} | ||
} |
@@ -41,2 +41,9 @@ import { z } from 'zod'; | ||
}; | ||
export type NodeContext< | ||
Input extends Record<string, unknown> | ReadableStream, | ||
Output extends Record<string, unknown> | ReadableStream | ||
> = { | ||
input: Input; | ||
output: Output; | ||
}; | ||
@@ -43,0 +50,0 @@ export type PipelineEvent = { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68293
33
1553